2020-09-17 19:53:01 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2020-04-16 05:23:37 -04:00
|
|
|
package term
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
import (
|
2017-05-10 18:41:46 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Termios is the Unix API for terminal I/O.
|
2020-09-17 19:53:01 -04:00
|
|
|
type Termios = unix.Termios
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2020-09-17 19:53:01 -04:00
|
|
|
// MakeRaw puts the terminal connected to the given file descriptor into raw
|
2017-04-17 18:08:24 -04:00
|
|
|
// mode and returns the previous state of the terminal so that it can be
|
|
|
|
// restored.
|
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
2020-09-17 19:53:01 -04:00
|
|
|
termios, err := tcget(fd)
|
2017-08-07 05:52:40 -04:00
|
|
|
if err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:53:01 -04:00
|
|
|
oldState := State{termios: *termios}
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
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
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2020-09-17 19:53:01 -04:00
|
|
|
if err := tcset(fd, termios); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &oldState, nil
|
|
|
|
}
|