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