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