moby--moby/pkg/term/termios_darwin.go

70 lines
1.7 KiB
Go
Raw Normal View History

package term
2013-01-29 01:06:46 +00:00
2013-03-22 11:24:03 +00:00
import (
2013-03-30 06:58:30 +00:00
"syscall"
"unsafe"
2013-03-22 11:24:03 +00:00
)
2013-01-29 01:07:38 +00:00
2013-01-29 01:06:46 +00:00
const (
2013-01-29 02:37:54 +00:00
getTermios = syscall.TIOCGETA
setTermios = syscall.TIOCSETA
)
// Termios magic numbers, passthrough to the ones defined in syscall.
const (
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-29 01:06:46 +00:00
)
2013-03-22 11:24:03 +00:00
// Termios is the Unix API for terminal I/O.
type Termios struct {
Iflag uint64
Oflag uint64
Cflag uint64
Lflag uint64
Cc [20]byte
Ispeed uint64
Ospeed uint64
}
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.
func MakeRaw(fd uintptr) (*State, error) {
2013-03-30 06:58:30 +00:00
var oldState State
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
2013-03-30 06:58:30 +00:00
return nil, err
}
2013-03-22 11:24:03 +00:00
2013-03-30 06:58:30 +00:00
newState := oldState.termios
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
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 {
2013-03-30 06:58:30 +00:00
return nil, err
}
2013-03-22 11:24:03 +00:00
2013-03-30 06:58:30 +00:00
return &oldState, nil
}