-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttymodes.c
329 lines (312 loc) · 6.8 KB
/
ttymodes.c
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* $Id: ttymodes.c,v 1.28 2024/10/20 21:54:50 tom Exp $ */
#include <vttest.h>
#include <ttymodes.h>
#include <esc.h> /* inflush() */
#undef tabs
#ifdef TAB3
# define tabs TAB3
#else
# ifdef XTABS
# define tabs XTABS
# else
# ifdef OXTABS
# define tabs OXTABS
# else
# define tabs 0
# endif
# endif
#endif
static TTY old_modes, new_modes;
/* *INDENT-OFF* */
static struct {
int name;
int code;
} speeds[] = {
{B0, 0},
{B50, 50},
{B75, 75},
{B110, 110},
{B134, 134},
{B150, 150},
{B200, 200},
{B300, 300},
{B600, 600},
{B1200, 1200},
{B1800, 1800},
{B2400, 2400},
{B4800, 4800},
{B9600, 9600},
#ifdef B19200
{B19200, 19200},
#else
#ifdef EXTA
{EXTA, 19200},
#endif
#endif
#ifdef B38400
{B38400, 38400},
#else
#ifdef EXTB
{EXTB, 38400},
#endif
#endif
#ifdef B57600
{B57600, 57600},
#endif
#ifdef B115200
{B115200, 115200},
#endif
};
/* *INDENT-ON* */
#if !USE_POSIX_TERMIOS && !USE_TERMIO && USE_SGTTY
static struct tchars old_tchars;
static struct ltchars old_ltchars;
#endif
#if USE_POSIX_TERMIOS || USE_TERMIO
static void
disable_control_chars(TTY * modes)
{
# if USE_POSIX_TERMIOS
int n;
int temp;
# ifdef HAVE_POSIX_VDISABLE
temp = _POSIX_VDISABLE;
# else
errno = 0;
temp = fpathconf(0, _PC_VDISABLE);
if (temp == -1) {
if (errno != 0) {
restore_ttymodes();
failed("Cannot disable special characters!\n");
}
temp = 0377;
}
# endif
for (n = 0; n < NCCS; n++)
modes->c_cc[n] = (unsigned char) temp;
# else /* USE_TERMIO */
# ifdef VSWTCH
modes->c_cc[VSWTCH] = VDISABLE;
# endif
modes->c_cc[VSUSP] = VDISABLE;
# if defined (VDSUSP) && defined(NCCS) && VDSUSP < NCCS
modes->c_cc[VDSUSP] = VDISABLE;
# endif
modes->c_cc[VSTART] = VDISABLE;
modes->c_cc[VSTOP] = VDISABLE;
# endif
modes->c_cc[VMIN] = 1;
modes->c_cc[VTIME] = 0;
}
#endif
static void
set_ttymodes(TTY * modes)
{
# if USE_POSIX_TERMIOS
fflush(stdout);
tcsetattr(0, TCSAFLUSH, modes);
# else
# if USE_TERMIO
tcsetattr(0, TCSETAF, modes);
# else /* USE_SGTTY */
stty(0, modes);
# endif
# endif
}
#ifndef log_ttymodes
void
log_ttymodes(char *file, int line)
{
if (LOG_ENABLED)
fprintf(log_fp, NOTE_STR "%s @%d\n", file, line);
}
#endif
#ifndef dump_ttymodes
void
dump_ttymodes(char *tag, int flag)
{
#ifdef UNIX
TTY tmp_modes;
if (LOG_ENABLED) {
fprintf(log_fp, NOTE_STR "%s (%d):\n", tag, flag);
# if USE_POSIX_TERMIOS || USE_TERMIO
tcgetattr(0, &tmp_modes);
fprintf(log_fp, NOTE_STR " iflag %08o\n", tmp_modes.c_iflag);
fprintf(log_fp, NOTE_STR " oflag %08o\n", tmp_modes.c_oflag);
fprintf(log_fp, NOTE_STR " lflag %08o\n", tmp_modes.c_lflag);
if (!(tmp_modes.c_lflag & ICANON)) {
fprintf(log_fp, NOTE_STR " %d:min =%d\n", VMIN, tmp_modes.c_cc[VMIN]);
fprintf(log_fp, NOTE_STR " %d:time =%d\n", VTIME, tmp_modes.c_cc[VTIME]);
}
# else
gtty(0, &tmp_modes);
fprintf(log_fp, NOTE_STR " flags %08o\n", tmp_modes.sg_flags);
# endif
}
#endif
}
#endif
void
close_tty(void)
{
restore_ttymodes();
}
void
init_ttymodes(int pn)
{
dump_ttymodes("init_ttymodes", pn);
#ifdef UNIX
if (pn == 0) {
int speed_code, n;
fflush(stdout);
# if USE_POSIX_TERMIOS || USE_TERMIO
tcgetattr(0, &old_modes);
speed_code = (int) cfgetospeed(&old_modes);
# else
# if USE_SGTTY
gtty(0, &old_modes);
ioctl(0, TIOCGETC, &old_tchars);
ioctl(0, TIOCGLTC, &old_ltchars);
speed_code = old_modes.sg_ospeed;
# endif
# endif
new_modes = old_modes;
for (n = 0; n < TABLESIZE(speeds); n++) {
if (speeds[n].name == speed_code) {
tty_speed = speeds[n].code;
break;
}
}
} else {
putchar(BEL);
fflush(stdout);
inflush();
new_modes = old_modes;
sleep(2);
}
# if USE_POSIX_TERMIOS || USE_TERMIO
new_modes.c_iflag = BRKINT | old_modes.c_iflag;
new_modes.c_oflag &= (unsigned) ~tabs;
# else /* USE_SGTTY */
new_modes.sg_flags = old_modes.sg_flags | CBREAK;
# endif
set_ttymodes(&new_modes);
# ifdef HAVE_FCNTL_H
# ifndef O_NDELAY
# define O_NDELAY O_NONBLOCK /* O_NONBLOCK is POSIX */
# endif
close(2);
if (open("/dev/tty", O_RDWR | O_NDELAY) != 2) {
restore_ttymodes();
failed("Cannot initialize tty modes!\n");
}
# endif
#endif /* UNIX */
dump_ttymodes("...init_ttymodes", pn);
}
void
restore_ttymodes(void)
{
dump_ttymodes("restore_ttymodes", -1);
#ifdef UNIX
set_ttymodes(&old_modes);
#endif
dump_ttymodes("...restore_ttymodes", -1);
}
void
set_tty_crmod(int enabled)
{
dump_ttymodes("set_tty_crmod", enabled);
#ifdef UNIX
# if USE_POSIX_TERMIOS || USE_TERMIO
# if USE_POSIX_TERMIOS
# define MASK_CRMOD ((unsigned) (ICRNL | IXON))
# else
# define MASK_CRMOD ((unsigned) (ICRNL))
# endif
if (enabled) {
new_modes.c_iflag |= MASK_CRMOD;
new_modes.c_lflag |= ICANON;
memcpy(new_modes.c_cc, old_modes.c_cc, sizeof(new_modes.c_cc));
} else {
new_modes.c_iflag &= ~MASK_CRMOD;
new_modes.c_lflag &= (unsigned) ~ICANON;
disable_control_chars(&new_modes);
}
# else
if (enabled)
new_modes.sg_flags |= CRMOD;
else
new_modes.sg_flags &= ~CRMOD;
# endif
set_ttymodes(&new_modes);
#endif
dump_ttymodes("...set_tty_crmod", enabled);
}
void
set_tty_echo(int enabled)
{
dump_ttymodes("set_tty_echo", enabled);
#ifdef UNIX
# if USE_POSIX_TERMIOS || USE_TERMIO
if (enabled)
new_modes.c_lflag |= ECHO;
else
new_modes.c_lflag &= (unsigned) ~ECHO;
# else /* USE_SGTTY */
if (enabled)
new_modes.sg_flags |= ECHO;
else
new_modes.sg_flags &= (unsigned) ~ECHO;
# endif
set_ttymodes(&new_modes);
#endif
dump_ttymodes("...set_tty_echo", enabled);
}
void
set_tty_raw(int enabled)
{
dump_ttymodes("set_tty_raw", enabled);
if (enabled) {
#ifdef UNIX
# if USE_POSIX_TERMIOS || USE_TERMIO
/* *INDENT-EQLS* */
new_modes.c_iflag = 0;
new_modes.c_lflag = 0;
new_modes.c_cc[VMIN] = 1;
new_modes.c_cc[VTIME] = 0;
set_ttymodes(&new_modes);
set_tty_crmod(FALSE);
# else /* USE_SGTTY */
# ifdef HAVE_FCNTL_H
new_modes.sg_flags &= ~CBREAK;
# endif
new_modes.sg_flags |= RAW;
set_ttymodes(&new_modes);
{
struct tchars tmp_tchars;
struct ltchars tmp_ltchars;
memset(&tmp_tchars, -1, sizeof(tmp_tchars));
memset(&tmp_ltchars, -1, sizeof(tmp_ltchars));
ioctl(0, TIOCSETC, &tmp_tchars);
ioctl(0, TIOCSLTC, &tmp_ltchars);
}
# endif
#endif
} else {
#ifdef UNIX
# if USE_POSIX_TERMIOS || USE_TERMIO
new_modes = old_modes;
# else /* USE_SGTTY */
new_modes.sg_flags &= ~RAW;
# ifdef HAVE_FCNTL_H
new_modes.sg_flags |= CBREAK;
# endif
ioctl(0, TIOCSETC, &old_tchars);
ioctl(0, TIOCSLTC, &old_ltchars);
# endif
set_ttymodes(&new_modes);
#endif
}
dump_ttymodes("...set_tty_raw", enabled);
}