github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/sys/eunix/termios.go (about)

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