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-25 21:43:25 -04:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2013-03-22 07:24:03 -04:00
|
|
|
)
|
2013-01-28 20:06:46 -05:00
|
|
|
|
|
|
|
const (
|
|
|
|
getTermios = syscall.TCGETS
|
|
|
|
setTermios = syscall.TCSETS
|
|
|
|
)
|
2013-03-22 07:24:03 -04:00
|
|
|
|
2013-06-01 19:19:50 -04:00
|
|
|
type Termios struct {
|
|
|
|
Iflag uint32
|
|
|
|
Oflag uint32
|
|
|
|
Cflag uint32
|
|
|
|
Lflag uint32
|
|
|
|
Cc [20]byte
|
|
|
|
Ispeed uint32
|
|
|
|
Ospeed uint32
|
|
|
|
}
|
|
|
|
|
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 18:55:05 -04:00
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
2013-03-25 21:43:25 -04:00
|
|
|
var oldState State
|
2013-06-01 18:55:05 -04:00
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
|
2013-03-25 21:43:25 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-06-01 18:51:45 -04:00
|
|
|
newState := oldState.termios
|
2013-03-25 21:43:25 -04:00
|
|
|
|
2013-06-01 18:51:45 -04:00
|
|
|
newState.Iflag &^= (syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON)
|
|
|
|
newState.Oflag &^= syscall.OPOST
|
|
|
|
newState.Lflag &^= (syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN)
|
|
|
|
newState.Cflag &^= (syscall.CSIZE | syscall.PARENB)
|
|
|
|
newState.Cflag |= syscall.CS8
|
2013-03-25 21:43:25 -04:00
|
|
|
|
2013-06-01 18:55:05 -04:00
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
|
2013-06-01 18:51:45 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &oldState, nil
|
2013-03-25 21:43:25 -04:00
|
|
|
}
|