Documentation ¶
Overview ¶
Package termios is a simple Go wrapper to the POSIX Terminal Interface (POSIX Termios). See [1].
Similatities to the C API ¶
The API exported by this package is very similar, though not identical, to the POSIX Termios C API. Some familiarity with the latter is required in order to use this package. See termios(3) or [2]. Any differences are covered by the package documentation.
The equivalent to the POSIX termios structure is defined as type Termios in this package, which is a structure with *no* exported fields. To access the standard POSIX termios flag fields (c_cflag, c_oflag, etc) use the methods:
Termios.CFlag() // Returns ptr to the c_cflag field equivalent. Termios.OFlag() // Returns ptr to the c_oflag field equivalent. ... etc ...
The pointers returned by these methods are of type *TcFlag, wich provides convinience methods to set, clear, and test individual flags or groups of flags. E.g:
Termios.CFlag().Clr(termios.CSIZE).Set(termios.CS8) Termios.CFlag().Clr(termios.CLOCAL|termios.HUPCL)
To access the contol-characters vector in termios (c_cc) use the following methods.
Termios.Cc(i) // Return control-char @ index i Termios.CcSet(i, c) // Set control-char @ index i to c.
C API functions that take a termios-structure argument, like tc{get,set}attr, cf{set,get}{i,o}speed, etc. are mapped to Termios methods:
tcsetattr --> Termios.SetFd tcgetattr --> Termios.GetFd cfget{i,o}speed --> Termios.Get{I,O}Speed cfset{i,o}speed --> Termios.Set{I,O}Speed cfmakeraw --> Termios.MakeRaw
Unlike their C-API equivalent the speed-setting and speed-getting methods take and return numeric baudrate values (integers) expressed in bits-per-second (not Bxxx speed-codes).
C API functions that operate directly on fd's are mapped to similarly named functions:
tcflush --> Flush tcdrain --> Drain tcsendbreak --> SendBreak
C API constants (macros), such as mode-flags (e.g. CLOCAL, OPOST), retain their names.
Supported systems ¶
Package termios should work on all systems that support the POSIX terminal interface, that is, on most Unix-like systems. Depending on the system, package termios can either be built to use the system's LIBC functions and macros through CGo, or as a pure-go package that issues system calls directly. In either case, the API presented by the package is identical.
References
[1] POSIX Terminal Interface (https://en.wikipedia.org/wiki/POSIX_terminal_interface) [2] POSIX.1-2008, Ch. 11: General Terminal Interface (http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap11.html)
Index ¶
- Constants
- func Drain(fd int) error
- func Flow(fd int, act int) error
- func Flush(fd int, qsel int) error
- func GetPgrp(fd int) (pgid int, err error)
- func SendBreak(fd int) error
- func SetPgrp(fd int, pgid int) error
- type Cc
- type TcFlag
- type Termios
- func (t *Termios) CFlag() *TcFlag
- func (t *Termios) Cc(idx int) Cc
- func (t *Termios) CcSet(idx int, c Cc)
- func (t *Termios) GetFd(fd int) error
- func (t *Termios) GetISpeed() (speed int, err error)
- func (t *Termios) GetOSpeed() (speed int, err error)
- func (t *Termios) IFlag() *TcFlag
- func (t *Termios) LFlag() *TcFlag
- func (t *Termios) MakeRaw()
- func (t *Termios) OFlag() *TcFlag
- func (t *Termios) SetFd(fd int, act int) error
- func (t *Termios) SetISpeed(speed int) error
- func (t *Termios) SetOSpeed(speed int) error
Constants ¶
const ( // Input mode flags (access with Termios.IFlag()) BRKINT = C.BRKINT ICRNL = C.ICRNL IGNBRK = C.IGNBRK IGNCR = C.IGNCR IGNPAR = C.IGNPAR INLCR = C.INLCR INPCK = C.INPCK ISTRIP = C.ISTRIP IXANY = C.IXANY IXOFF = C.IXOFF IXON = C.IXON PARMRK = C.PARMRK // Non-standard (0 if unavailable) IMAXBEL = C.IMAXBEL IUCLC = C.IUCLC IUTF8 = C.IUTF8 // Output mode flags (access with Termios.OFlag()) OPOST = C.OPOST // Control mode flags (access with Termios.CFlag()) CSIZE = C.CSIZE CS5 = C.CS5 CS6 = C.CS6 CS7 = C.CS7 CS8 = C.CS8 CSTOPB = C.CSTOPB CREAD = C.CREAD PARENB = C.PARENB PARODD = C.PARODD HUPCL = C.HUPCL CLOCAL = C.CLOCAL // Non-standard (0 if unavailable) CRTSCTS = C.CRTSCTS CMSPAR = C.CMSPAR // Local mode flags (access with Termios.LFlag()) ECHO = C.ECHO ECHOE = C.ECHOE ECHOK = C.ECHOK ECHONL = C.ECHONL ICANON = C.ICANON IEXTEN = C.IEXTEN ISIG = C.ISIG NOFLSH = C.NOFLSH TOSTOP = C.TOSTOP // Non-standard (0 if unavailable) PENDIN = C.PENDIN ECHOCTL = C.ECHOCTL ECHOPRT = C.ECHOPRT ECHOKE = C.ECHOKE FLUSHO = C.FLUSHO EXTPROC = C.EXTPROC // Cc subscript names (access with Termios.{Cc,CcSet}) VEOF = C.VEOF VEOL = C.VEOL VERASE = C.VERASE VINTR = C.VINTR VKILL = C.VKILL VMIN = C.VMIN VQUIT = C.VQUIT VSTART = C.VSTART VSTOP = C.VSTOP VSUSP = C.VSUSP VTIME = C.VTIME // Non-standard (-1 if unavailable) VREPRINT = C.VREPRINT VDISCARD = C.VDISCARD VWERASE = C.VWERASE VLNEXT = C.VLNEXT VEOL2 = C.VEOL2 // Values for the act argument of Termios.SetFd TCSANOW = C.TCSANOW TCSADRAIN = C.TCSADRAIN TCSAFLUSH = C.TCSAFLUSH // Queue selectors for function Flush TCIFLUSH = C.TCIFLUSH TCOFLUSH = C.TCOFLUSH TCIOFLUSH = C.TCIOFLUSH // Values for the act argument of Flow TCOOFF = C.TCOOFF TCOON = C.TCOON TCIOFF = C.TCIOFF TCION = C.TCION )
Variables ¶
This section is empty.
Functions ¶
func Drain ¶
Drain blocks until all data written to the terminal fd are transmitted. See also tcdrain(3). If the system call is interrupted by a signal, Drain retries it automatically.
func Flow ¶
Flow suspends or resumes the transmission or reception of data on the terminal associated with fd, depending on the value of the act argument. The act argument value must be one of: TCOOFF (suspend transmission), TCOON (resume transmission), TCIOFF (suspend reception by sending a STOP char), TCION (resume reception by sending a START char). See also tcflow(3).
func Flush ¶
Flush discards data received but not yet read (input queue), and/or data written but not yet transmitted (output queue), depending on the value of the qsel argument. Argument qsel must be one of the constants TCIFLUSH (flush input queue), TCOFLUSH (flush output queue), TCIOFLUSH (flush both queues). See also tcflush(3).
func GetPgrp ¶
GetPgrp returns the process group ID of the foreground process group associated with the terminal. See tcgetpgrp(3).
Types ¶
type TcFlag ¶
TcFlag is a word containing Termios mode-flags
func (*TcFlag) Clr ¶
Clr clears (lowers) in w the flags that are set in f. It does not affect the other flags in w. Returns (a pointer to) w.
func (*TcFlag) Msk ¶
Msk returns the flags in w masked by f (that is, the values of the flags in w for which the equivalent flags in f are set). It does not modify w.
type Termios ¶
type Termios struct {
// contains filtered or unexported fields
}
Termios is the terminal-attributes structure
func (*Termios) GetFd ¶
GetFd reads the attributes of the terminal corresponding to the file-descriptor fd and stores them in the Termios structure t. See tcgetattr(3) for more.
func (*Termios) GetISpeed ¶
GetISpeed returns the input (receiver) baudrate in Termios structure t as a numerical (integer) value in bits-per-second. Returns err == syscal.EINVAL if the baudrate in t cannot be decoded. See also getispeed(3).
func (*Termios) GetOSpeed ¶
GetOSpeed returns the output (transmitter) baudrate in Termios structure t as a numerical (integer) value in bits-per-second. Returns err == syscal.EINVAL if the baudrate in t cannot be decoded. See also getospeed(3).
func (*Termios) MakeRaw ¶
func (t *Termios) MakeRaw()
MakeRaw sets the terminal attributes in t to values appropriate for configuring the terminal to "raw" mode: Input available character by character, echoing disabled, and all special processing of terminal input and output characters disabled. Notice that MakeRaw does not actually configure the terminal, it only sets the attributes in t. In order to configure the terminal, you must subsequently call the t.SetFd method.
func (*Termios) SetFd ¶
SetFd configures the terminal corresponding to the file-descriptor fd using the attributes in the Termios structure t. Argument act must be one of the constants TCSANOW, TCSADRAIN, TCSAFLUSH. See tcsetattr(3) for more. If the system call is interrupted by a signal, SetFd retries it automatically.
func (*Termios) SetISpeed ¶
SetISpeed sets the input (receiver) baudrate in Termios structure t to speed. Argument speed must be a numerical (integer) baudrate value in bits-per-second. Returns syscall.EINVAL if the requested baudrate is not supported. See also cfsetispeed(3).