1// +build darwin dragonfly freebsd openbsd netbsd
2
3package term // import "github.com/moby/term"
4
5import (
6	"unsafe"
7
8	"golang.org/x/sys/unix"
9)
10
11const (
12	getTermios = unix.TIOCGETA
13	setTermios = unix.TIOCSETA
14)
15
16// Termios is the Unix API for terminal I/O.
17type Termios unix.Termios
18
19// MakeRaw put the terminal connected to the given file descriptor into raw
20// mode and returns the previous state of the terminal so that it can be
21// restored.
22func MakeRaw(fd uintptr) (*State, error) {
23	var oldState State
24	if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
25		return nil, err
26	}
27
28	newState := oldState.termios
29	newState.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
30	newState.Oflag &^= unix.OPOST
31	newState.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
32	newState.Cflag &^= (unix.CSIZE | unix.PARENB)
33	newState.Cflag |= unix.CS8
34	newState.Cc[unix.VMIN] = 1
35	newState.Cc[unix.VTIME] = 0
36
37	if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
38		return nil, err
39	}
40
41	return &oldState, nil
42}
43