2013-03-13 03:29:40 -04:00
|
|
|
package term
|
2013-01-28 20:06:46 -05:00
|
|
|
|
2013-03-22 07:24:03 -04:00
|
|
|
import (
|
2013-03-30 02:58:30 -04:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2013-03-22 07:24:03 -04:00
|
|
|
)
|
2013-01-28 20:07:38 -05:00
|
|
|
|
2013-01-28 20:06:46 -05:00
|
|
|
const (
|
2013-01-28 21:37:54 -05:00
|
|
|
getTermios = syscall.TIOCGETA
|
|
|
|
setTermios = syscall.TIOCSETA
|
2013-06-01 19:19:50 -04:00
|
|
|
|
2014-03-13 14:11:02 -04:00
|
|
|
IGNBRK = syscall.IGNBRK
|
|
|
|
PARMRK = syscall.PARMRK
|
|
|
|
INLCR = syscall.INLCR
|
|
|
|
IGNCR = syscall.IGNCR
|
|
|
|
ECHONL = syscall.ECHONL
|
|
|
|
CSIZE = syscall.CSIZE
|
|
|
|
ICRNL = syscall.ICRNL
|
|
|
|
ISTRIP = syscall.ISTRIP
|
|
|
|
PARENB = syscall.PARENB
|
|
|
|
ECHO = syscall.ECHO
|
|
|
|
ICANON = syscall.ICANON
|
|
|
|
ISIG = syscall.ISIG
|
|
|
|
IXON = syscall.IXON
|
|
|
|
BRKINT = syscall.BRKINT
|
|
|
|
INPCK = syscall.INPCK
|
|
|
|
OPOST = syscall.OPOST
|
|
|
|
CS8 = syscall.CS8
|
|
|
|
IEXTEN = syscall.IEXTEN
|
2013-01-28 20:06:46 -05:00
|
|
|
)
|
2013-03-22 07:24:03 -04:00
|
|
|
|
2013-06-01 19:19:50 -04:00
|
|
|
type Termios struct {
|
|
|
|
Iflag uint64
|
|
|
|
Oflag uint64
|
|
|
|
Cflag uint64
|
|
|
|
Lflag uint64
|
|
|
|
Cc [20]byte
|
|
|
|
Ispeed uint64
|
|
|
|
Ospeed uint64
|
|
|
|
}
|
|
|
|
|
2013-03-22 07:24:03 -04:00
|
|
|
// MakeRaw put the terminal connected to the given file descriptor into raw
|
|
|
|
// mode and returns the previous state of the terminal so that it can be
|
|
|
|
// restored.
|
2013-06-01 19:19:50 -04:00
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
2013-03-30 02:58:30 -04:00
|
|
|
var oldState State
|
2013-06-01 19:19:50 -04:00
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
|
2013-03-30 02:58:30 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-03-22 07:24:03 -04:00
|
|
|
|
2013-03-30 02:58:30 -04:00
|
|
|
newState := oldState.termios
|
2014-03-13 14:11:02 -04:00
|
|
|
newState.Iflag &^= (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON)
|
|
|
|
newState.Oflag &^= OPOST
|
|
|
|
newState.Lflag &^= (ECHO | ECHONL | ICANON | ISIG | IEXTEN)
|
|
|
|
newState.Cflag &^= (CSIZE | PARENB)
|
|
|
|
newState.Cflag |= CS8
|
|
|
|
newState.Cc[syscall.VMIN] = 1
|
|
|
|
newState.Cc[syscall.VTIME] = 0
|
2013-06-01 19:19:50 -04:00
|
|
|
|
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 {
|
2013-03-30 02:58:30 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-03-22 07:24:03 -04:00
|
|
|
|
2013-03-30 02:58:30 -04:00
|
|
|
return &oldState, nil
|
|
|
|
}
|