github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/pkg/term/tc_linux_cgo.go (about)

     1  // +build linux,cgo
     2  
     3  package term
     4  
     5  import (
     6  	"syscall"
     7  	"unsafe"
     8  )
     9  
    10  // #include <termios.h>
    11  import "C"
    12  
    13  // Termios is the Unix API for terminal I/O.
    14  // It is passthrough for syscall.Termios in order to make it portable with
    15  // other platforms where it is not available or handled differently.
    16  type Termios syscall.Termios
    17  
    18  // MakeRaw put the terminal connected to the given file descriptor into raw
    19  // mode and returns the previous state of the terminal so that it can be
    20  // restored.
    21  func MakeRaw(fd uintptr) (*State, error) {
    22  	var oldState State
    23  	if err := tcget(fd, &oldState.termios); err != 0 {
    24  		return nil, err
    25  	}
    26  
    27  	newState := oldState.termios
    28  
    29  	C.cfmakeraw((*C.struct_termios)(unsafe.Pointer(&newState)))
    30  	if err := tcset(fd, &newState); err != 0 {
    31  		return nil, err
    32  	}
    33  	return &oldState, nil
    34  }
    35  
    36  func tcget(fd uintptr, p *Termios) syscall.Errno {
    37  	ret, err := C.tcgetattr(C.int(fd), (*C.struct_termios)(unsafe.Pointer(p)))
    38  	if ret != 0 {
    39  		return err.(syscall.Errno)
    40  	}
    41  	return 0
    42  }
    43  
    44  func tcset(fd uintptr, p *Termios) syscall.Errno {
    45  	ret, err := C.tcsetattr(C.int(fd), C.TCSANOW, (*C.struct_termios)(unsafe.Pointer(p)))
    46  	if ret != 0 {
    47  		return err.(syscall.Errno)
    48  	}
    49  	return 0
    50  }