github.com/mephux/docker@v1.6.0-rc5/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  type Termios syscall.Termios
    14  
    15  // MakeRaw put the terminal connected to the given file descriptor into raw
    16  // mode and returns the previous state of the terminal so that it can be
    17  // restored.
    18  func MakeRaw(fd uintptr) (*State, error) {
    19  	var oldState State
    20  	if err := tcget(fd, &oldState.termios); err != 0 {
    21  		return nil, err
    22  	}
    23  
    24  	newState := oldState.termios
    25  
    26  	C.cfmakeraw((*C.struct_termios)(unsafe.Pointer(&newState)))
    27  	if err := tcset(fd, &newState); err != 0 {
    28  		return nil, err
    29  	}
    30  	return &oldState, nil
    31  }
    32  
    33  func tcget(fd uintptr, p *Termios) syscall.Errno {
    34  	ret, err := C.tcgetattr(C.int(fd), (*C.struct_termios)(unsafe.Pointer(p)))
    35  	if ret != 0 {
    36  		return err.(syscall.Errno)
    37  	}
    38  	return 0
    39  }
    40  
    41  func tcset(fd uintptr, p *Termios) syscall.Errno {
    42  	ret, err := C.tcsetattr(C.int(fd), C.TCSANOW, (*C.struct_termios)(unsafe.Pointer(p)))
    43  	if ret != 0 {
    44  		return err.(syscall.Errno)
    45  	}
    46  	return 0
    47  }