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
|
|
|
|
2013-06-19 10:50:58 -04:00
|
|
|
ECHO = 0x00000008
|
|
|
|
ONLCR = 0x2
|
|
|
|
ISTRIP = 0x20
|
|
|
|
INLCR = 0x40
|
|
|
|
ISIG = 0x80
|
|
|
|
IGNCR = 0x80
|
|
|
|
ICANON = 0x100
|
|
|
|
ICRNL = 0x100
|
|
|
|
IXOFF = 0x400
|
|
|
|
IXON = 0x200
|
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
|
2013-06-01 19:19:50 -04:00
|
|
|
newState.Iflag &^= (ISTRIP | INLCR | IGNCR | IXON | IXOFF)
|
2013-03-30 02:58:30 -04:00
|
|
|
newState.Iflag |= ICRNL
|
|
|
|
newState.Oflag |= ONLCR
|
2013-06-01 19:19:50 -04:00
|
|
|
newState.Lflag &^= (ECHO | ICANON | ISIG)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|