github.com/carlanton/docker@v1.8.0-rc1/pkg/term/termios_linux.go (about)

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