From f38353d6b91cf9c4f9c086eddfa6d337172d281f Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 22 May 2024 09:00:38 -0700 Subject: [PATCH] stdin.fd --- readline/readline.go | 9 ++++----- readline/readline_unix.go | 2 +- readline/readline_windows.go | 2 +- readline/term.go | 6 +++--- readline/term_bsd.go | 8 ++++---- readline/term_linux.go | 8 ++++---- readline/term_windows.go | 6 +++--- 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/readline/readline.go b/readline/readline.go index d8c3676e..e90a5e01 100644 --- a/readline/readline.go +++ b/readline/readline.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "os" - "syscall" ) type Prompt struct { @@ -63,7 +62,7 @@ func New(prompt Prompt) (*Instance, error) { func (i *Instance) Readline() (string, error) { if !i.Terminal.rawmode { - fd := syscall.Stdin + fd := os.Stdin.Fd() termios, err := SetRawMode(fd) if err != nil { return "", err @@ -80,7 +79,7 @@ func (i *Instance) Readline() (string, error) { fmt.Print(prompt) defer func() { - fd := syscall.Stdin + fd := os.Stdin.Fd() //nolint:errcheck UnsetRawMode(fd, i.Terminal.termios) i.Terminal.rawmode = false @@ -216,7 +215,7 @@ func (i *Instance) Readline() (string, error) { case CharCtrlW: buf.DeleteWord() case CharCtrlZ: - fd := syscall.Stdin + fd := os.Stdin.Fd() return handleCharCtrlZ(fd, i.Terminal.termios) case CharEnter, CharCtrlJ: output := buf.String() @@ -248,7 +247,7 @@ func (i *Instance) HistoryDisable() { } func NewTerminal() (*Terminal, error) { - fd := syscall.Stdin + fd := os.Stdin.Fd() termios, err := SetRawMode(fd) if err != nil { return nil, err diff --git a/readline/readline_unix.go b/readline/readline_unix.go index 76cff8c8..d48b9176 100644 --- a/readline/readline_unix.go +++ b/readline/readline_unix.go @@ -6,7 +6,7 @@ import ( "syscall" ) -func handleCharCtrlZ(fd int, termios any) (string, error) { +func handleCharCtrlZ(fd uintptr, termios any) (string, error) { t := termios.(*Termios) if err := UnsetRawMode(fd, t); err != nil { return "", err diff --git a/readline/readline_windows.go b/readline/readline_windows.go index b4e96b25..a131d0ef 100644 --- a/readline/readline_windows.go +++ b/readline/readline_windows.go @@ -1,6 +1,6 @@ package readline -func handleCharCtrlZ(fd int, state any) (string, error) { +func handleCharCtrlZ(fd uintptr, state any) (string, error) { // not supported return "", nil } diff --git a/readline/term.go b/readline/term.go index 9d747162..5584cd25 100644 --- a/readline/term.go +++ b/readline/term.go @@ -8,7 +8,7 @@ import ( type Termios syscall.Termios -func SetRawMode(fd int) (*Termios, error) { +func SetRawMode(fd uintptr) (*Termios, error) { termios, err := getTermios(fd) if err != nil { return nil, err @@ -25,13 +25,13 @@ func SetRawMode(fd int) (*Termios, error) { return termios, setTermios(fd, &newTermios) } -func UnsetRawMode(fd int, termios any) error { +func UnsetRawMode(fd uintptr, termios any) error { t := termios.(*Termios) return setTermios(fd, t) } // IsTerminal returns true if the given file descriptor is a terminal. -func IsTerminal(fd int) bool { +func IsTerminal(fd uintptr) bool { _, err := getTermios(fd) return err == nil } diff --git a/readline/term_bsd.go b/readline/term_bsd.go index 04b912f0..80bee6b3 100644 --- a/readline/term_bsd.go +++ b/readline/term_bsd.go @@ -7,17 +7,17 @@ import ( "unsafe" ) -func getTermios(fd int) (*Termios, error) { +func getTermios(fd uintptr) (*Termios, error) { termios := new(Termios) - _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TIOCGETA, uintptr(unsafe.Pointer(termios)), 0, 0, 0) + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, syscall.TIOCGETA, uintptr(unsafe.Pointer(termios)), 0, 0, 0) if err != 0 { return nil, err } return termios, nil } -func setTermios(fd int, termios *Termios) error { - _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TIOCSETA, uintptr(unsafe.Pointer(termios)), 0, 0, 0) +func setTermios(fd uintptr, termios *Termios) error { + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, syscall.TIOCSETA, uintptr(unsafe.Pointer(termios)), 0, 0, 0) if err != 0 { return err } diff --git a/readline/term_linux.go b/readline/term_linux.go index 2d6211dd..e9ed0745 100644 --- a/readline/term_linux.go +++ b/readline/term_linux.go @@ -10,17 +10,17 @@ import ( const tcgets = 0x5401 const tcsets = 0x5402 -func getTermios(fd int) (*Termios, error) { +func getTermios(fd uintptr) (*Termios, error) { termios := new(Termios) - _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), tcgets, uintptr(unsafe.Pointer(termios)), 0, 0, 0) + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, tcgets, uintptr(unsafe.Pointer(termios)), 0, 0, 0) if err != 0 { return nil, err } return termios, nil } -func setTermios(fd int, termios *Termios) error { - _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), tcsets, uintptr(unsafe.Pointer(termios)), 0, 0, 0) +func setTermios(fd uintptr, termios *Termios) error { + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, tcsets, uintptr(unsafe.Pointer(termios)), 0, 0, 0) if err != 0 { return err } diff --git a/readline/term_windows.go b/readline/term_windows.go index cfdfd672..3b35149b 100644 --- a/readline/term_windows.go +++ b/readline/term_windows.go @@ -9,13 +9,13 @@ type State struct { } // IsTerminal checks if the given file descriptor is associated with a terminal -func IsTerminal(fd int) bool { +func IsTerminal(fd uintptr) bool { var st uint32 err := windows.GetConsoleMode(windows.Handle(fd), &st) return err == nil } -func SetRawMode(fd int) (*State, error) { +func SetRawMode(fd uintptr) (*State, error) { var st uint32 if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil { return nil, err @@ -32,7 +32,7 @@ func SetRawMode(fd int) (*State, error) { return &State{st}, nil } -func UnsetRawMode(fd int, state any) error { +func UnsetRawMode(fd uintptr, state any) error { s := state.(*State) return windows.SetConsoleMode(windows.Handle(fd), s.mode) }