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
8package eunix
9
10import (
11	"unsafe"
12
13	"golang.org/x/sys/unix"
14)
15
16// Termios represents terminal attributes.
17type Termios unix.Termios
18
19// TermiosForFd returns a pointer to a Termios structure if the file
20// descriptor is open on a terminal device.
21func 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.
27func (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.
32func (term *Termios) Copy() *Termios {
33	v := *term
34	return &v
35}
36
37// SetVTime sets the timeout in deciseconds for noncanonical read.
38func (term *Termios) SetVTime(v uint8) {
39	term.Cc[unix.VTIME] = v
40}
41
42// SetVMin sets the minimal number of characters for noncanonical read.
43func (term *Termios) SetVMin(v uint8) {
44	term.Cc[unix.VMIN] = v
45}
46
47// SetICanon sets the canonical flag.
48func (term *Termios) SetICanon(v bool) {
49	setFlag(&term.Lflag, unix.ICANON, v)
50}
51
52// SetIExten sets the iexten flag.
53func (term *Termios) SetIExten(v bool) {
54	setFlag(&term.Lflag, unix.IEXTEN, v)
55}
56
57// SetEcho sets the echo flag.
58func (term *Termios) SetEcho(v bool) {
59	setFlag(&term.Lflag, unix.ECHO, v)
60}
61
62// SetICRNL sets the CRNL iflag bit
63func (term *Termios) SetICRNL(v bool) {
64	setFlag(&term.Iflag, unix.ICRNL, v)
65}
66
67func setFlag(flag *termiosFlag, mask termiosFlag, v bool) {
68	if v {
69		*flag |= mask
70	} else {
71		*flag &= ^mask
72	}
73}
74