From 5c3894ff5122cfa60724b24a11196a571e94a497 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 5 Apr 2024 19:37:01 +0300 Subject: [PATCH] OpenBSD removed syscall() calls, use proper ioctl()s instead --- go.mod | 1 + terminal/runereader_posix.go | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 4837ce75..cc045092 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/mattn/go-isatty v0.0.8 github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b github.com/stretchr/testify v1.6.1 + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 golang.org/x/text v0.4.0 ) diff --git a/terminal/runereader_posix.go b/terminal/runereader_posix.go index 563a0811..4c1e88ae 100644 --- a/terminal/runereader_posix.go +++ b/terminal/runereader_posix.go @@ -14,7 +14,7 @@ import ( "bytes" "fmt" "syscall" - "unsafe" + "golang.org/x/sys/unix" ) const ( @@ -23,7 +23,7 @@ const ( ) type runeReaderState struct { - term syscall.Termios + term unix.Termios reader *bufio.Reader buf *bytes.Buffer } @@ -45,19 +45,20 @@ func (rr *RuneReader) Buffer() *bytes.Buffer { // For reading runes we just want to disable echo. func (rr *RuneReader) SetTermMode() error { - if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(rr.stdio.In.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&rr.state.term)), 0, 0, 0); err != 0 { + termios, err := unix.IoctlGetTermios((int)(rr.stdio.In.Fd()), ioctlReadTermios) + if err != nil { return err } - newState := rr.state.term - newState.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG + rr.state.term = *termios + termios.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG // Because we are clearing canonical mode, we need to ensure VMIN & VTIME are // set to the values we expect. This combination puts things in standard // "blocking read" mode (see termios(3)). - newState.Cc[syscall.VMIN] = 1 - newState.Cc[syscall.VTIME] = 0 + termios.Cc[syscall.VMIN] = 1 + termios.Cc[syscall.VTIME] = 0 - if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(rr.stdio.In.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 { + if err = unix.IoctlSetTermios((int)(rr.stdio.In.Fd()), ioctlWriteTermios, termios); err != nil { return err } @@ -65,7 +66,7 @@ func (rr *RuneReader) SetTermMode() error { } func (rr *RuneReader) RestoreTermMode() error { - if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(rr.stdio.In.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&rr.state.term)), 0, 0, 0); err != 0 { + if err := unix.IoctlSetTermios((int)(rr.stdio.In.Fd()), ioctlWriteTermios, &rr.state.term); err != nil { return err } return nil