From ba2a7ca72e71bfaba3692949ae39b1d64882856e Mon Sep 17 00:00:00 2001 From: ichizok Date: Wed, 19 Jan 2022 18:47:22 +0900 Subject: [PATCH] Fix solaris support --- go.mod | 1 - go.sum | 4 ---- tty_bsd.go | 7 ++++--- tty_linux.go | 9 +++++++-- tty_solaris.go | 13 +++++++++++++ tty_sys5.go | 12 ------------ tty_unix.go | 37 +++++++++++++++++++------------------ tty_windows.go | 1 + 8 files changed, 44 insertions(+), 40 deletions(-) create mode 100644 tty_solaris.go delete mode 100644 tty_sys5.go diff --git a/go.mod b/go.mod index e8b512e..49a9099 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,5 @@ require ( github.com/mattn/go-colorable v0.1.4 github.com/mattn/go-isatty v0.0.10 github.com/mattn/go-runewidth v0.0.7 - golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e ) diff --git a/go.sum b/go.sum index 1d5a1d4..a516a4a 100644 --- a/go.sum +++ b/go.sum @@ -3,12 +3,8 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= -github.com/mattn/go-runewidth v0.0.6 h1:V2iyH+aX9C5fsYCpK60U8BYIvmhqxuOL3JZcqc1NB7k= -github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA= diff --git a/tty_bsd.go b/tty_bsd.go index e0a51fc..8c01b6c 100644 --- a/tty_bsd.go +++ b/tty_bsd.go @@ -1,12 +1,13 @@ +//go:build darwin || dragonfly || freebsd || netbsd || openbsd // +build darwin dragonfly freebsd netbsd openbsd package tty import ( - "syscall" + "golang.org/x/sys/unix" ) const ( - ioctlReadTermios = syscall.TIOCGETA - ioctlWriteTermios = syscall.TIOCSETA + ioctlReadTermios = unix.TIOCGETA + ioctlWriteTermios = unix.TIOCSETA ) diff --git a/tty_linux.go b/tty_linux.go index 1b9e8ce..4ddbd78 100644 --- a/tty_linux.go +++ b/tty_linux.go @@ -1,8 +1,13 @@ +//go:build linux // +build linux package tty +import ( + "golang.org/x/sys/unix" +) + const ( - ioctlReadTermios = 0x5401 // syscall.TCGETS - ioctlWriteTermios = 0x5402 // syscall.TCSETS + ioctlReadTermios = unix.TCGETS + ioctlWriteTermios = unix.TCSETS ) diff --git a/tty_solaris.go b/tty_solaris.go new file mode 100644 index 0000000..1ea64da --- /dev/null +++ b/tty_solaris.go @@ -0,0 +1,13 @@ +//go:build solaris +// +build solaris + +package tty + +import ( + "golang.org/x/sys/unix" +) + +const ( + ioctlReadTermios = unix.TCGETS + ioctlWriteTermios = unix.TCSETS +) diff --git a/tty_sys5.go b/tty_sys5.go deleted file mode 100644 index 7dc30ed..0000000 --- a/tty_sys5.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build solaris - -package tty - -import ( - "golang.org/x/sys/unix" -) - -const ( - ioctlReadTermios = unix.TCGETA - ioctlWriteTermios = unix.TCSETA -) diff --git a/tty_unix.go b/tty_unix.go index 3cb9450..c9a8f17 100644 --- a/tty_unix.go +++ b/tty_unix.go @@ -1,5 +1,5 @@ -// +build !windows -// +build !plan9 +//go:build !windows && !plan9 +// +build !windows,!plan9 package tty @@ -7,8 +7,6 @@ import ( "bufio" "os" "os/signal" - "syscall" - "unsafe" "golang.org/x/sys/unix" ) @@ -17,7 +15,7 @@ type TTY struct { in *os.File bin *bufio.Reader out *os.File - termios syscall.Termios + termios unix.Termios ss chan os.Signal } @@ -31,19 +29,23 @@ func open(path string) (*TTY, error) { tty.in = in tty.bin = bufio.NewReader(in) - out, err := os.OpenFile(path, syscall.O_WRONLY, 0) + out, err := os.OpenFile(path, unix.O_WRONLY, 0) if err != nil { return nil, err } tty.out = out - if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&tty.termios))); err != 0 { + termios, err := unix.IoctlGetTermios(int(tty.in.Fd()), ioctlReadTermios) + if err != nil { return nil, err } - newios := tty.termios - newios.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXOFF - newios.Lflag &^= syscall.ECHO | syscall.ICANON /*| syscall.ISIG*/ - if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&newios))); err != 0 { + tty.termios = *termios + + termios.Iflag &^= unix.ISTRIP | unix.INLCR | unix.ICRNL | unix.IGNCR | unix.IXOFF + termios.Lflag &^= unix.ECHO | unix.ICANON /*| unix.ISIG*/ + termios.Cc[unix.VMIN] = 1 + termios.Cc[unix.VTIME] = 0 + if err := unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, termios); err != nil { return nil, err } @@ -64,8 +66,7 @@ func (tty *TTY) readRune() (rune, error) { func (tty *TTY) close() error { signal.Stop(tty.ss) close(tty.ss) - _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&tty.termios))) - return err + return unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, &tty.termios) } func (tty *TTY) size() (int, int, error) { @@ -74,11 +75,11 @@ func (tty *TTY) size() (int, int, error) { } func (tty *TTY) sizePixel() (int, int, int, int, error) { - var dim [4]uint16 - if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.out.Fd()), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dim))); err != 0 { + ws, err := unix.IoctlGetWinsize(int(tty.out.Fd()), unix.TIOCGWINSZ) + if err != nil { return -1, -1, -1, -1, err } - return int(dim[1]), int(dim[0]), int(dim[2]), int(dim[3]), nil + return int(ws.Row), int(ws.Col), int(ws.Xpixel), int(ws.Ypixel), nil } func (tty *TTY) input() *os.File { @@ -116,13 +117,13 @@ func (tty *TTY) raw() (func() error, error) { } func (tty *TTY) sigwinch() <-chan WINSIZE { - signal.Notify(tty.ss, syscall.SIGWINCH) + signal.Notify(tty.ss, unix.SIGWINCH) ws := make(chan WINSIZE) go func() { defer close(ws) for sig := range tty.ss { - if sig != syscall.SIGWINCH { + if sig != unix.SIGWINCH { continue } diff --git a/tty_windows.go b/tty_windows.go index acc0b4f..3db0cf7 100644 --- a/tty_windows.go +++ b/tty_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package tty