1
#pragma once
2
3
#include <sys/types.h>
4
5
// termios structures and flags used by tty clients
6
// https://pubs.opengroup.org/onlinepubs/7908799/xsh/termios.h.html
7
// https://www.man7.org/linux/man-pages/man3/termios.3.html
8
9
#define NCCS 32
10
11
typedef unsigned char cc_t;
12
typedef unsigned int speed_t;
13
typedef unsigned int tcflag_t;
14
15
struct termios {
16
tcflag_t c_iflag;17
tcflag_t c_oflag;18
tcflag_t c_cflag;19
tcflag_t c_lflag;20
cc_t c_cc[NCCS];
21
22
speed_t c_ispeed;23
speed_t c_ospeed;24
};
25
26
struct winsize {
27
unsigned short ws_row;
28
unsigned short ws_col;
29
unsigned short ws_xpixel;
30
unsigned short ws_ypixel;
31
};
32
33
#ifndef NO_LIBC_EXTENTIONS
34
typedef struct termios termios_t;
35
typedef struct winsize winsize_t;
36
#endif37
38
// https://www.gnu.org/software/libc/manual/html_node/Editing-Characters.html
39
enum termios_cc {
40
VEOF, // ^D, canonical only
41
VEOL, // null, canonical only
42
VERASE, // ^H, canonical only
43
VINTR, // ^C, all modes
44
VKILL, // ^U, canonical only
45
VMIN, // minimum number of characters for raw read
46
VQUIT, // ^\, all modes
47
VSTART, // ^Q, all modes
48
VSTOP, // ^S, all modes
49
VSUSP, // ^Z, all modes
50
VTIME, // timeout in deciseconds for raw read
51
VWERASE, // ^W, canonical only
52
VLNEXT, // ^V, all modes
53
VREPRINT, // ^R, canonical only
54
};
55
56
enum termios_iflags {
57
BRKINT = 1 << 0,
58
ICRNL = 1 << 1,
59
IGNBRK = 1 << 2,
60
IGNCR = 1 << 3,
61
IGNPAR = 1 << 4,
62
INLCR = 1 << 5,
63
INPCK = 1 << 6,
64
ISTRIP = 1 << 7,
65
IUCLC = 1 << 8,
66
IXANY = 1 << 9,
67
IXOFF = 1 << 10,
68
IXON = 1 << 11,
69
PARMRK = 1 << 12,
70
};
71
72
enum termios_oflags {
73
OPOST = 1 << 0,
74
ONLCR = 1 << 1,
75
OCRNL = 1 << 2,
76
ONOCR = 1 << 3,
77
ONLRET = 1 << 4,
78
OFILL = 1 << 5,
79
80
NLDLY = 1 << 6,
81
NL0 = 0,
82
NL1 = 1,
83
84
CRDLY = 1 << 7 | 1 << 8,
85
CR0 = 0 << 7,
86
CR1 = 1 << 7,
87
CR2 = 2 << 7,
88
CR3 = 3 << 7,
89
90
TABDLY = 1 << 9 | 1 << 10,
91
TAB0 = 0 << 9,
92
TAB1 = 1 << 9,
93
TAB2 = 2 << 9,
94
TAB3 = 3 << 9,
95
96
BSDLY = 1 << 11,
97
BS0 = 0 << 11,
98
BS1 = 1 << 11,
99
100
VTDLY = 1 << 12,
101
VT0 = 0 << 12,
102
VT1 = 1 << 12,
103
104
FFDLY = 1 << 13,
105
FF0 = 0 << 13,
106
FF1 = 1 << 13,
107
};
108
109
enum termios_cflags {
110
CSIZE = 1 << 0 | 1 << 1,
111
CS5 = 0 << 0,
112
CS6 = 1 << 0,
113
CS7 = 2 << 0,
114
CS8 = 3 << 0,
115
116
CSTOPB = 1 << 2,
117
CREAD = 1 << 3,
118
PARENB = 1 << 4,
119
PARODD = 1 << 5,
120
HUPCL = 1 << 6,
121
CLOCAL = 1 << 7,
122
CMSPAR = 1 << 8,
123
};
124
125
enum termios_lflags {
126
ECHO = 1 << 0,
127
ECHOE = 1 << 1,
128
ECHOK = 1 << 2,
129
ECHONL = 1 << 3,
130
ECHOCTL = 1 << 4,
131
ICANON = 1 << 5,
132
IEXTEN = 1 << 6,
133
ISIG = 1 << 7,
134
NOFLSH = 1 << 8,
135
TOSTOP = 1 << 9,
136
};
137
138
enum termios_baud {
139
B0 = 0, // hang up
140
B50 = 50,
141
B75 = 75,
142
B110 = 110,
143
B134 = 134,
144
B150 = 150,
145
B200 = 200,
146
B300 = 300,
147
B600 = 600,
148
B1200 = 1200,
149
B1800 = 1800,
150
B2400 = 2400,
151
B4800 = 4800,
152
B9600 = 9600, // traditional default
153
B19200 = 19200,
154
B38400 = 38400,
155
B57600 = 57600,
156
B115200 = 115200,
157
};
158
159
enum termios_set_action {
160
TCSANOW = 0,
161
TCSADRAIN = 1,
162
TCSAFLUSH = 2,
163
};
164
165
enum termios_flush_queue {
166
TCIFLUSH = 0,
167
TCOFLUSH = 1,
168
TCIOFLUSH = 2,
169
};
170
171
// some sane defaults for termios to use
172
#ifndef NO_LIBC_EXTENTIONS
173
static inline termios_t *__termios_default_init(termios_t *tos) {
174
if (!tos)
175
return tos;
176
177
tos->c_cc[VEOF] = 'D' - 0x40;
178
tos->c_cc[VEOL] = 0;
179
tos->c_cc[VERASE] = 'H' - 0x40;
180
tos->c_cc[VINTR] = 'C' - 0x40;
181
tos->c_cc[VKILL] = 'U' - 0x40;
182
tos->c_cc[VQUIT] = '\\' - 0x40;
183
tos->c_cc[VSTART] = 'Q' - 0x40;
184
tos->c_cc[VSTOP] = 'S' - 0x40;
185
tos->c_cc[VSUSP] = 'Z' - 0x40;
186
tos->c_cc[VWERASE] = 'W' - 0x40;
187
tos->c_cc[VLNEXT] = 'V' - 0x40;
188
tos->c_cc[VREPRINT] = 'R' - 0x40;
189
tos->c_cc[VTIME] = 0;
190
tos->c_cc[VMIN] = 1;
191
192
tos->c_iflag = BRKINT | ICRNL;
193
tos->c_oflag = OPOST | ONLCR;
194
tos->c_cflag = CS8 | CREAD | HUPCL;
195
tos->c_lflag = ECHO | ECHOE | ECHOK | ECHOCTL | ICANON | IEXTEN | ISIG;
196
197
tos->c_ispeed = B115200;
198
tos->c_ospeed = B115200;
199
200
return tos;
201
}202
#endif203
204
#ifndef _KERNEL
205
int tcgetattr(int fd, struct termios *tos);
206
int tcsetattr(int fd, int optional_actions, const struct termios *tos);
207
speed_t cfgetispeed(const struct termios *tos);
208
speed_t cfgetospeed(const struct termios *tos);
209
int cfsetispeed(struct termios *tos, speed_t speed);
210
int cfsetospeed(struct termios *tos, speed_t speed);
211
void cfmakeraw(struct termios *tos);
212
pid_t tcgetpgrp(int fd);
213
int tcsetpgrp(int fd, pid_t pgrp);
214
#endif