Skip to content

Commit

Permalink
fix termutil package for BSD platforms
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Castillo <[email protected]>
  • Loading branch information
peterctl committed Feb 27, 2019
1 parent 30921d9 commit cd2014f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
31 changes: 18 additions & 13 deletions pkg/termutil/termios_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@ type Termios unix.Termios
// mode and returns the previous state of the terminal so that it can be
// restored.
func makeRaw(fd uintptr) (*State, error) {
oldTios, err := getTermios(fd)
termios, err := getTermios(fd)
if err != nil {
return nil, err
}

newTios := *oldTios
newTios.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
newTios.Oflag &^= unix.OPOST
newTios.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
newTios.Cflag &^= (unix.CSIZE | unix.PARENB)
newTios.Cflag |= unix.CS8
newTios.Cc[unix.VMIN] = 1
newTios.Cc[unix.VTIME] = 0
var oldState State
oldState.termios = Termios(*termios)

if err := setTermios(fd, &newTios); err != nil {
termios.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
termios.Oflag &^= unix.OPOST
termios.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
termios.Cflag &^= (unix.CSIZE | unix.PARENB)
termios.Cflag |= unix.CS8
termios.Cc[unix.VMIN] = 1
termios.Cc[unix.VTIME] = 0

if err := setTermios(fd, termios); err != nil {
return nil, err
}

return &State{termios: oldTios}, nil
return &oldState, nil
}

func getTermios(fd uintptr) (*Termios, error) {
Expand All @@ -50,6 +52,9 @@ func getTermios(fd uintptr) (*Termios, error) {
}

func setTermios(fd uintptr, t *Termios) error {
_, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermiosOp, uintptr(unsafe.Pointer(&t)))
return err
_, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermiosOp, uintptr(unsafe.Pointer(t)))
if err != 0 {
return err
}
return nil
}
1 change: 1 addition & 0 deletions pkg/termutil/termios_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func makeRaw(fd uintptr) (*State, error) {
if err := setTermios(fd, termios); err != nil {
return nil, err
}

return &oldState, nil
}

Expand Down

0 comments on commit cd2014f

Please sign in to comment.