Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

stdin.fd

Michael Yangf38353d6b91c
diff --git a/readline/readline.go b/readline/readline.go
index d8c3676ea..e90a5e01f 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 76cff8c8f..d48b91769 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 b4e96b255..a131d0ef7 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 9d747162c..5584cd257 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 04b912f04..80bee6b3e 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 2d6211dd6..e9ed07451 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 cfdfd672f..3b35149b8 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)
}