github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/pkg/term/termios_linux.go (about)

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