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