src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/sys/eunix/termios.go (about)

     1  //go:build unix
     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 eunix
     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  // TermiosForFd returns a pointer to a Termios structure if the file
    19  // descriptor is open on a terminal device.
    20  func TermiosForFd(fd int) (*Termios, error) {
    21  	term, err := unix.IoctlGetTermios(fd, getAttrIOCTL)
    22  	return (*Termios)(term), err
    23  }
    24  
    25  // ApplyToFd applies term to the given file descriptor.
    26  func (term *Termios) ApplyToFd(fd int) error {
    27  	return unix.IoctlSetTermios(fd, setAttrNowIOCTL, (*unix.Termios)(unsafe.Pointer(term)))
    28  }
    29  
    30  // Copy returns a copy of term.
    31  func (term *Termios) Copy() *Termios {
    32  	v := *term
    33  	return &v
    34  }
    35  
    36  // SetVTime sets the timeout in deciseconds for noncanonical read.
    37  func (term *Termios) SetVTime(v uint8) {
    38  	term.Cc[unix.VTIME] = v
    39  }
    40  
    41  // SetVMin sets the minimal number of characters for noncanonical read.
    42  func (term *Termios) SetVMin(v uint8) {
    43  	term.Cc[unix.VMIN] = v
    44  }
    45  
    46  // SetICanon sets the canonical flag.
    47  func (term *Termios) SetICanon(v bool) {
    48  	setFlag(&term.Lflag, unix.ICANON, v)
    49  }
    50  
    51  // SetIExten sets the iexten flag.
    52  func (term *Termios) SetIExten(v bool) {
    53  	setFlag(&term.Lflag, unix.IEXTEN, v)
    54  }
    55  
    56  // SetEcho sets the echo flag.
    57  func (term *Termios) SetEcho(v bool) {
    58  	setFlag(&term.Lflag, unix.ECHO, v)
    59  }
    60  
    61  // SetICRNL sets the CRNL iflag bit
    62  func (term *Termios) SetICRNL(v bool) {
    63  	setFlag(&term.Iflag, unix.ICRNL, v)
    64  }
    65  
    66  func setFlag(flag *termiosFlag, mask termiosFlag, v bool) {
    67  	if v {
    68  		*flag |= mask
    69  	} else {
    70  		*flag &= ^mask
    71  	}
    72  }