Debian Codegraph @

termios/termios.go

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