github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/elvish/sys/termios.go (about)

     1  // +build !windows,!plan9
     2  
     3  // Copyright 2015 go-termios Author. All Rights Reserved.
     4  // https://github.com/go-termios/termios
     5  // Author: John Lenton <chipaca@github.com>
     6  
     7  package sys
     8  
     9  import (
    10  	"unsafe"
    11  
    12  	"golang.org/x/sys/unix"
    13  )
    14  
    15  // Termios represents terminal attributes.
    16  type Termios unix.Termios
    17  
    18  // NewTermiosFromFd extracts the terminal attribute of the given file
    19  // descriptor.
    20  func NewTermiosFromFd(fd int) (*Termios, error) {
    21  	var term Termios
    22  	if err := Ioctl(fd, getAttrIOCTL, uintptr(unsafe.Pointer(&term))); err != nil {
    23  		return nil, err
    24  	}
    25  	return &term, nil
    26  }
    27  
    28  // ApplyToFd applies term to the given file descriptor.
    29  func (term *Termios) ApplyToFd(fd int) error {
    30  	return Ioctl(fd, setAttrNowIOCTL, uintptr(unsafe.Pointer(term)))
    31  }
    32  
    33  // Copy returns a copy of term.
    34  func (term *Termios) Copy() *Termios {
    35  	v := *term
    36  	return &v
    37  }
    38  
    39  // SetVTime sets the timeout in deciseconds for noncanonical read.
    40  func (term *Termios) SetVTime(v uint8) {
    41  	term.Cc[unix.VTIME] = v
    42  }
    43  
    44  // SetVMin sets the minimal number of characters for noncanonical read.
    45  func (term *Termios) SetVMin(v uint8) {
    46  	term.Cc[unix.VMIN] = v
    47  }
    48  
    49  // SetICanon sets the canonical flag.
    50  func (term *Termios) SetICanon(v bool) {
    51  	setFlag(&term.Lflag, unix.ICANON, v)
    52  }
    53  
    54  // SetEcho sets the echo flag.
    55  func (term *Termios) SetEcho(v bool) {
    56  	setFlag(&term.Lflag, unix.ECHO, v)
    57  }
    58  
    59  // SetICRNL sets the CRNL iflag bit
    60  func (term *Termios) SetICRNL(v bool) {
    61  	setFlag(&term.Iflag, unix.ICRNL, v)
    62  }
    63  
    64  // FlushInput discards data written to a file descriptor but not read.
    65  func FlushInput(fd int) error {
    66  	return Ioctl(fd, flushIOCTL, uintptr(unix.TCIFLUSH))
    67  }