diff --git a/pkg/term/termios_bsd.go b/pkg/term/termios_bsd.go new file mode 100644 index 0000000000..c47341e873 --- /dev/null +++ b/pkg/term/termios_bsd.go @@ -0,0 +1,42 @@ +// +build darwin freebsd openbsd + +package term + +import ( + "unsafe" + + "golang.org/x/sys/unix" +) + +const ( + getTermios = unix.TIOCGETA + setTermios = unix.TIOCSETA +) + +// Termios is the Unix API for terminal I/O. +type Termios unix.Termios + +// MakeRaw put the terminal connected to the given file descriptor into raw +// mode and returns the previous state of the terminal so that it can be +// restored. +func MakeRaw(fd uintptr) (*State, error) { + var oldState State + if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 { + return nil, err + } + + newState := oldState.termios + newState.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON) + newState.Oflag &^= unix.OPOST + newState.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN) + newState.Cflag &^= (unix.CSIZE | unix.PARENB) + newState.Cflag |= unix.CS8 + newState.Cc[unix.VMIN] = 1 + newState.Cc[unix.VTIME] = 0 + + if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 { + return nil, err + } + + return &oldState, nil +} diff --git a/pkg/term/termios_darwin.go b/pkg/term/termios_darwin.go deleted file mode 100644 index 1533434ac0..0000000000 --- a/pkg/term/termios_darwin.go +++ /dev/null @@ -1,70 +0,0 @@ -package term - -import ( - "unsafe" - - "golang.org/x/sys/unix" -) - -const ( - getTermios = unix.TIOCGETA - setTermios = unix.TIOCSETA -) - -// Termios magic numbers, passthrough to the ones defined in unix. -const ( - IGNBRK = unix.IGNBRK - PARMRK = unix.PARMRK - INLCR = unix.INLCR - IGNCR = unix.IGNCR - ECHONL = unix.ECHONL - CSIZE = unix.CSIZE - ICRNL = unix.ICRNL - ISTRIP = unix.ISTRIP - PARENB = unix.PARENB - ECHO = unix.ECHO - ICANON = unix.ICANON - ISIG = unix.ISIG - IXON = unix.IXON - BRKINT = unix.BRKINT - INPCK = unix.INPCK - OPOST = unix.OPOST - CS8 = unix.CS8 - IEXTEN = unix.IEXTEN -) - -// Termios is the Unix API for terminal I/O. -type Termios struct { - Iflag uint64 - Oflag uint64 - Cflag uint64 - Lflag uint64 - Cc [20]byte - Ispeed uint64 - Ospeed uint64 -} - -// MakeRaw put the terminal connected to the given file descriptor into raw -// mode and returns the previous state of the terminal so that it can be -// restored. -func MakeRaw(fd uintptr) (*State, error) { - var oldState State - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 { - return nil, err - } - - newState := oldState.termios - newState.Iflag &^= (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON) - newState.Oflag &^= OPOST - newState.Lflag &^= (ECHO | ECHONL | ICANON | ISIG | IEXTEN) - newState.Cflag &^= (CSIZE | PARENB) - newState.Cflag |= CS8 - newState.Cc[unix.VMIN] = 1 - newState.Cc[unix.VTIME] = 0 - - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 { - return nil, err - } - - return &oldState, nil -} diff --git a/pkg/term/termios_freebsd.go b/pkg/term/termios_freebsd.go deleted file mode 100644 index 88854fa544..0000000000 --- a/pkg/term/termios_freebsd.go +++ /dev/null @@ -1,70 +0,0 @@ -package term - -import ( - "unsafe" - - "golang.org/x/sys/unix" -) - -const ( - getTermios = unix.TIOCGETA - setTermios = unix.TIOCSETA -) - -// Termios magic numbers, passthrough to the ones defined in unix. -const ( - IGNBRK = unix.IGNBRK - PARMRK = unix.PARMRK - INLCR = unix.INLCR - IGNCR = unix.IGNCR - ECHONL = unix.ECHONL - CSIZE = unix.CSIZE - ICRNL = unix.ICRNL - ISTRIP = unix.ISTRIP - PARENB = unix.PARENB - ECHO = unix.ECHO - ICANON = unix.ICANON - ISIG = unix.ISIG - IXON = unix.IXON - BRKINT = unix.BRKINT - INPCK = unix.INPCK - OPOST = unix.OPOST - CS8 = unix.CS8 - IEXTEN = unix.IEXTEN -) - -// Termios is the Unix API for terminal I/O. -type Termios struct { - Iflag uint32 - Oflag uint32 - Cflag uint32 - Lflag uint32 - Cc [20]byte - Ispeed uint32 - Ospeed uint32 -} - -// MakeRaw put the terminal connected to the given file descriptor into raw -// mode and returns the previous state of the terminal so that it can be -// restored. -func MakeRaw(fd uintptr) (*State, error) { - var oldState State - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 { - return nil, err - } - - newState := oldState.termios - newState.Iflag &^= (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON) - newState.Oflag &^= OPOST - newState.Lflag &^= (ECHO | ECHONL | ICANON | ISIG | IEXTEN) - newState.Cflag &^= (CSIZE | PARENB) - newState.Cflag |= CS8 - newState.Cc[unix.VMIN] = 1 - newState.Cc[unix.VTIME] = 0 - - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 { - return nil, err - } - - return &oldState, nil -} diff --git a/pkg/term/termios_linux.go b/pkg/term/termios_linux.go index 39cac84ad5..b1d9c8d1b5 100644 --- a/pkg/term/termios_linux.go +++ b/pkg/term/termios_linux.go @@ -12,15 +12,7 @@ const ( ) // Termios is the Unix API for terminal I/O. -type Termios struct { - Iflag uint32 - Oflag uint32 - Cflag uint32 - Lflag uint32 - Cc [20]byte - Ispeed uint32 - Ospeed uint32 -} +type Termios unix.Termios // MakeRaw put the terminal connected to the given file descriptor into raw // mode and returns the previous state of the terminal so that it can be diff --git a/pkg/term/termios_openbsd.go b/pkg/term/termios_openbsd.go deleted file mode 100644 index 88854fa544..0000000000 --- a/pkg/term/termios_openbsd.go +++ /dev/null @@ -1,70 +0,0 @@ -package term - -import ( - "unsafe" - - "golang.org/x/sys/unix" -) - -const ( - getTermios = unix.TIOCGETA - setTermios = unix.TIOCSETA -) - -// Termios magic numbers, passthrough to the ones defined in unix. -const ( - IGNBRK = unix.IGNBRK - PARMRK = unix.PARMRK - INLCR = unix.INLCR - IGNCR = unix.IGNCR - ECHONL = unix.ECHONL - CSIZE = unix.CSIZE - ICRNL = unix.ICRNL - ISTRIP = unix.ISTRIP - PARENB = unix.PARENB - ECHO = unix.ECHO - ICANON = unix.ICANON - ISIG = unix.ISIG - IXON = unix.IXON - BRKINT = unix.BRKINT - INPCK = unix.INPCK - OPOST = unix.OPOST - CS8 = unix.CS8 - IEXTEN = unix.IEXTEN -) - -// Termios is the Unix API for terminal I/O. -type Termios struct { - Iflag uint32 - Oflag uint32 - Cflag uint32 - Lflag uint32 - Cc [20]byte - Ispeed uint32 - Ospeed uint32 -} - -// MakeRaw put the terminal connected to the given file descriptor into raw -// mode and returns the previous state of the terminal so that it can be -// restored. -func MakeRaw(fd uintptr) (*State, error) { - var oldState State - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 { - return nil, err - } - - newState := oldState.termios - newState.Iflag &^= (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON) - newState.Oflag &^= OPOST - newState.Lflag &^= (ECHO | ECHONL | ICANON | ISIG | IEXTEN) - newState.Cflag &^= (CSIZE | PARENB) - newState.Cflag |= CS8 - newState.Cc[unix.VMIN] = 1 - newState.Cc[unix.VTIME] = 0 - - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 { - return nil, err - } - - return &oldState, nil -}