2020-09-15 10:00:50 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2020-04-16 05:02:09 -04:00
|
|
|
package term
|
2013-01-28 20:06:46 -05:00
|
|
|
|
2013-03-22 07:24:03 -04:00
|
|
|
import (
|
2017-05-04 20:52:19 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2013-03-22 07:24:03 -04:00
|
|
|
)
|
2013-01-28 20:06:46 -05:00
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Termios is the Unix API for terminal I/O.
|
2020-09-15 10:00:50 -04:00
|
|
|
type Termios = unix.Termios
|
2013-06-01 19:19:50 -04:00
|
|
|
|
2020-09-15 10:00:50 -04:00
|
|
|
// MakeRaw puts the terminal connected to the given file descriptor into raw
|
2013-03-22 07:24:03 -04:00
|
|
|
// mode and returns the previous state of the terminal so that it can be
|
|
|
|
// restored.
|
2013-06-01 18:55:05 -04:00
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
2020-09-15 10:00:50 -04:00
|
|
|
termios, err := tcget(fd)
|
2017-07-17 04:36:52 -04:00
|
|
|
if err != nil {
|
2013-03-25 21:43:25 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-15 10:00:50 -04:00
|
|
|
oldState := State{termios: *termios}
|
2013-03-25 21:43:25 -04:00
|
|
|
|
2017-07-17 04:36:52 -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
|
2017-08-03 07:18:29 -04:00
|
|
|
termios.Cc[unix.VMIN] = 1
|
|
|
|
termios.Cc[unix.VTIME] = 0
|
2013-03-25 21:43:25 -04:00
|
|
|
|
2020-09-15 10:00:50 -04:00
|
|
|
if err := tcset(fd, termios); err != nil {
|
2013-06-01 18:51:45 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &oldState, nil
|
2013-03-25 21:43:25 -04:00
|
|
|
}
|