moby--moby/pkg/term/termios_linux.go

48 lines
1.2 KiB
Go
Raw Normal View History

// +build !cgo
package term
2013-01-29 01:06:46 +00:00
2013-03-22 11:24:03 +00:00
import (
2013-03-26 01:43:25 +00:00
"syscall"
"unsafe"
2013-03-22 11:24:03 +00:00
)
2013-01-29 01:06:46 +00:00
const (
getTermios = syscall.TCGETS
setTermios = syscall.TCSETS
)
2013-03-22 11:24:03 +00:00
// Termios is the Unix API for terminal I/O.
type Termios struct {
Iflag uint32
Oflag uint32
Cflag uint32
Lflag uint32
Cc [20]byte
Ispeed uint32
Ospeed uint32
}
2013-03-22 11:24:03 +00: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 22:55:05 +00:00
func MakeRaw(fd uintptr) (*State, error) {
2013-03-26 01:43:25 +00:00
var oldState State
2013-06-01 22:55:05 +00:00
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
2013-03-26 01:43:25 +00:00
return nil, err
}
2013-06-01 22:51:45 +00:00
newState := oldState.termios
2013-03-26 01:43:25 +00:00
2013-06-01 22:51:45 +00: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-26 01:43:25 +00:00
2013-06-01 22:55:05 +00:00
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
2013-06-01 22:51:45 +00:00
return nil, err
}
return &oldState, nil
2013-03-26 01:43:25 +00:00
}