1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
//go:build darwin || netbsd || freebsd || openbsd || linux || dragonfly || solaris
// +build darwin netbsd freebsd openbsd linux dragonfly solaris
package termios
import (
"syscall"
"golang.org/x/sys/unix"
)
// SetWinsize sets window size for an fd from a Winsize.
func SetWinsize(fd int, w *unix.Winsize) error {
return unix.IoctlSetWinsize(fd, ioctlSetWinSize, w)
}
// GetWinsize gets window size for an fd.
func GetWinsize(fd int) (*unix.Winsize, error) {
return unix.IoctlGetWinsize(fd, ioctlSetWinSize)
}
// GetTermios gets the termios of the given fd.
func GetTermios(fd int) (*unix.Termios, error) {
return unix.IoctlGetTermios(fd, ioctlGets)
}
// SetTermios sets the given termios over the given fd's current termios.
func SetTermios(
fd int,
ispeed uint32,
ospeed uint32,
cc map[CC]uint8,
iflag map[I]bool,
oflag map[O]bool,
cflag map[C]bool,
lflag map[L]bool,
) error {
term, err := unix.IoctlGetTermios(fd, ioctlGets)
if err != nil {
return err
}
setSpeed(term, ispeed, ospeed)
for key, value := range cc {
call, ok := allCcOpts[key]
if !ok {
continue
}
term.Cc[call] = value
}
for key, value := range iflag {
mask, ok := allInputOpts[key]
if ok {
if value {
term.Iflag |= bit(mask)
} else {
term.Iflag &= ^bit(mask)
}
}
}
for key, value := range oflag {
mask, ok := allOutputOpts[key]
if ok {
if value {
term.Oflag |= bit(mask)
} else {
term.Oflag &= ^bit(mask)
}
}
}
for key, value := range cflag {
mask, ok := allControlOpts[key]
if ok {
if value {
term.Cflag |= bit(mask)
} else {
term.Cflag &= ^bit(mask)
}
}
}
for key, value := range lflag {
mask, ok := allLineOpts[key]
if ok {
if value {
term.Lflag |= bit(mask)
} else {
term.Lflag &= ^bit(mask)
}
}
}
return unix.IoctlSetTermios(fd, ioctlSets, term)
}
// CC is the termios cc field.
//
// It stores an array of special characters related to terminal I/O.
type CC uint8
// CC possible values.
const (
INTR CC = iota
QUIT
ERASE
KILL
EOF
EOL
EOL2
START
STOP
SUSP
WERASE
RPRNT
LNEXT
DISCARD
STATUS
SWTCH
DSUSP
FLUSH
)
// https://www.man7.org/linux/man-pages/man3/termios.3.html
var allCcOpts = map[CC]int{
INTR: syscall.VINTR,
QUIT: syscall.VQUIT,
ERASE: syscall.VERASE,
KILL: syscall.VQUIT,
EOF: syscall.VEOF,
EOL: syscall.VEOL,
EOL2: syscall.VEOL2,
START: syscall.VSTART,
STOP: syscall.VSTOP,
SUSP: syscall.VSUSP,
WERASE: syscall.VWERASE,
RPRNT: syscall.VREPRINT,
LNEXT: syscall.VLNEXT,
DISCARD: syscall.VDISCARD,
// XXX: these syscalls don't exist for any OS
// FLUSH: syscall.VFLUSH,
}
// Input Controls
type I uint8
// Input possible values.
const (
IGNPAR I = iota
PARMRK
INPCK
ISTRIP
INLCR
IGNCR
ICRNL
IXON
IXANY
IXOFF
IMAXBEL
IUCLC
)
var allInputOpts = map[I]uint32{
IGNPAR: syscall.IGNPAR,
PARMRK: syscall.PARMRK,
INPCK: syscall.INPCK,
ISTRIP: syscall.ISTRIP,
INLCR: syscall.INLCR,
IGNCR: syscall.IGNCR,
ICRNL: syscall.ICRNL,
IXON: syscall.IXON,
IXANY: syscall.IXANY,
IXOFF: syscall.IXOFF,
IMAXBEL: syscall.IMAXBEL,
}
// Output Controls
type O uint8
// Output possible values.
const (
OPOST O = iota
ONLCR
OCRNL
ONOCR
ONLRET
OLCUC
)
var allOutputOpts = map[O]uint32{
OPOST: syscall.OPOST,
ONLCR: syscall.ONLCR,
OCRNL: syscall.OCRNL,
ONOCR: syscall.ONOCR,
ONLRET: syscall.ONLRET,
}
// Control
type C uint8
// Control possible values.
const (
CS7 C = iota
CS8
PARENB
PARODD
)
var allControlOpts = map[C]uint32{
CS7: syscall.CS7,
CS8: syscall.CS8,
PARENB: syscall.PARENB,
PARODD: syscall.PARODD,
}
// Line Controls.
type L uint8
// Line possible values.
const (
ISIG L = iota
ICANON
ECHO
ECHOE
ECHOK
ECHONL
NOFLSH
TOSTOP
IEXTEN
ECHOCTL
ECHOKE
PENDIN
IUTF8
XCASE
)
var allLineOpts = map[L]uint32{
ISIG: syscall.ISIG,
ICANON: syscall.ICANON,
ECHO: syscall.ECHO,
ECHOE: syscall.ECHOE,
ECHOK: syscall.ECHOK,
ECHONL: syscall.ECHONL,
NOFLSH: syscall.NOFLSH,
TOSTOP: syscall.TOSTOP,
IEXTEN: syscall.IEXTEN,
ECHOCTL: syscall.ECHOCTL,
ECHOKE: syscall.ECHOKE,
PENDIN: syscall.PENDIN,
}
|