github.com/ld86/docker@v1.7.1-rc3/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  	newState.Oflag = newState.Oflag | C.OPOST
    28  	if err := tcset(fd, &newState); err != 0 {
    29  		return nil, err
    30  	}
    31  	return &oldState, nil
    32  }
    33  
    34  func tcget(fd uintptr, p *Termios) syscall.Errno {
    35  	ret, err := C.tcgetattr(C.int(fd), (*C.struct_termios)(unsafe.Pointer(p)))
    36  	if ret != 0 {
    37  		return err.(syscall.Errno)
    38  	}
    39  	return 0
    40  }
    41  
    42  func tcset(fd uintptr, p *Termios) syscall.Errno {
    43  	ret, err := C.tcsetattr(C.int(fd), C.TCSANOW, (*C.struct_termios)(unsafe.Pointer(p)))
    44  	if ret != 0 {
    45  		return err.(syscall.Errno)
    46  	}
    47  	return 0
    48  }