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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: raw.c 761 2007-10-23 22:35:18Z hubert@u.washington.edu $";
#endif
/*
* ========================================================================
* Copyright 2006 University of Washington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*/
/*
* TTY setup routines.
*/
#include <system.h>
#include <general.h>
#include "../estruct.h"
#include "raw.h"
#if HAS_TERMIOS
/*
* These are the TERMIOS-style (POSIX) routines.
*/
static struct termios _raw_tty, _original_tty;
#elif HAS_TERMIO
/*
* These are the TERMIO-style (System V) routines.
*/
static struct termio _raw_tty, _original_tty;
#else /* RAW_BSD */
/*
* These are for the BSD-style routines.
*/
static struct sgttyb _raw_tty, _original_tty;
static struct ltchars _raw_ltchars, _original_ltchars;
static struct tchars _raw_tchars, _original_tchars;
static int _raw_lmode, _original_lmode;
#endif
/*
* current raw state
*/
static short _inraw = 0;
/*
* Set up the tty driver
*
* Args: state -- which state to put it in. 1 means go into raw (cbreak),
* 0 out of raw.
*
* Result: returns 0 if successful and -1 if not.
*/
int
Raw(int state)
{
/** state is either ON or OFF, as indicated by call **/
/* Check return code only on first call. If it fails we're done for and
if it goes OK the others will probably go OK too. */
if(state == 0 && _inraw){
/*----- restore state to original -----*/
#if HAS_TERMIOS
if(tcsetattr(STDIN_FD, TCSADRAIN, &_original_tty) < 0)
return -1;
#elif HAS_TERMIO
if(ioctl(STDIN_FD, TCSETAW, &_original_tty) < 0)
return(-1);
#else /* RAW_BSD */
if(ioctl(STDIN_FD, TIOCSETP, &_original_tty) < 0)
return(-1);
(void)ioctl(STDIN_FD, TIOCSLTC, &_original_ltchars);
(void)ioctl(STDIN_FD, TIOCSETC, &_original_tchars);
(void)ioctl(STDIN_FD, TIOCLSET, &_original_lmode);
#endif
_inraw = 0;
}
else if(state == 1 && ! _inraw){
/*----- Go into raw mode (cbreak actually) ----*/
#if HAS_TERMIOS
if(tcgetattr(STDIN_FD, &_original_tty) < 0)
return -1;
tcgetattr(STDIN_FD, &_raw_tty);
_raw_tty.c_lflag &= ~(ICANON | ECHO | IEXTEN); /* noecho raw mode */
_raw_tty.c_lflag &= ~ISIG; /* disable signals */
_raw_tty.c_iflag &= ~ICRNL; /* turn off CR->NL on input */
_raw_tty.c_oflag &= ~ONLCR; /* turn off NL->CR on output */
_raw_tty.c_cc[VMIN] = '\01'; /* min # of chars to queue */
_raw_tty.c_cc[VTIME] = '\0'; /* min time to wait for input*/
_raw_tty.c_cc[VINTR] = ctrl('C'); /* make it our special char */
_raw_tty.c_cc[VQUIT] = 0;
_raw_tty.c_cc[VSUSP] = 0;
tcsetattr(STDIN_FD, TCSADRAIN, &_raw_tty);
#elif HAS_TERMIO
if(ioctl(STDIN_FD, TCGETA, &_original_tty) < 0)
return(-1);
(void)ioctl(STDIN_FD, TCGETA, &_raw_tty); /** again! **/
_raw_tty.c_lflag &= ~(ICANON | ECHO); /* noecho raw mode */
_raw_tty.c_lflag &= ~ISIG; /* disable signals */
_raw_tty.c_iflag &= ~ICRNL; /* turn off CR->NL on input */
_raw_tty.c_oflag &= ~ONLCR; /* turn off NL->CR on output */
_raw_tty.c_cc[VMIN] = 1; /* min # of chars to queue */
_raw_tty.c_cc[VTIME] = 0; /* min time to wait for input*/
_raw_tty.c_cc[VINTR] = ctrl('C'); /* make it our special char */
_raw_tty.c_cc[VQUIT] = 0;
(void)ioctl(STDIN_FD, TCSETAW, &_raw_tty);
#else /* RAW_BSD */
if(ioctl(STDIN_FD, TIOCGETP, &_original_tty) < 0)
return(-1);
(void)ioctl(STDIN_FD, TIOCGETP, &_raw_tty);
(void)ioctl(STDIN_FD, TIOCGETC, &_original_tchars);
(void)ioctl(STDIN_FD, TIOCGETC, &_raw_tchars);
(void)ioctl(STDIN_FD, TIOCGLTC, &_original_ltchars);
(void)ioctl(STDIN_FD, TIOCGLTC, &_raw_ltchars);
(void)ioctl(STDIN_FD, TIOCLGET, &_original_lmode);
(void)ioctl(STDIN_FD, TIOCLGET, &_raw_lmode);
_raw_tty.sg_flags &= ~(ECHO); /* echo off */
_raw_tty.sg_flags |= CBREAK; /* raw on */
_raw_tty.sg_flags &= ~CRMOD; /* Turn off CR -> LF mapping */
_raw_tchars.t_intrc = -1; /* Turn off ^C and ^D */
_raw_tchars.t_eofc = -1;
_raw_ltchars.t_lnextc = -1; /* Turn off ^V so we can use it */
_raw_ltchars.t_dsuspc = -1; /* Turn off ^Y so we can use it */
_raw_ltchars.t_suspc = -1; /* Turn off ^Z; we just read 'em */
_raw_ltchars.t_werasc = -1; /* Turn off ^w word erase */
_raw_ltchars.t_rprntc = -1; /* Turn off ^R reprint line */
_raw_ltchars.t_flushc = -1; /* Turn off ^O output flush */
(void)ioctl(STDIN_FD, TIOCSETP, &_raw_tty);
(void)ioctl(STDIN_FD, TIOCSLTC, &_raw_ltchars);
(void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
(void)ioctl(STDIN_FD, TIOCLSET, &_raw_lmode);
#endif
_inraw = 1;
}
return(0);
}
/*
* Set up the tty driver to use XON/XOFF flow control
*
* Args: state -- True to make sure XON/XOFF turned on, FALSE off.
*
* Result: none.
*/
void
xonxoff_proc(int state)
{
if(_inraw){
if(state){
#if HAS_TERMIOS
if(!(_raw_tty.c_iflag & IXON)){
_raw_tty.c_iflag |= IXON;
tcsetattr (STDIN_FD, TCSADRAIN, &_raw_tty);
}
#elif HAS_TERMIO
if(!(_raw_tty.c_iflag & IXON)){
_raw_tty.c_iflag |= IXON; /* turn ON ^S/^Q on input */
(void)ioctl(STDIN_FD, TCSETAW, &_raw_tty);
#else /* RAW_BSD */
if(_raw_tchars.t_startc == -1 || _raw_tchars.t_stopc == -1){
_raw_tchars.t_startc = 'Q' - '@'; /* Turn ON ^S/^Q */
_raw_tchars.t_stopc = 'S' - '@';
(void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
}
#endif
}
else{
#if HAS_TERMIOS
if(_raw_tty.c_iflag & IXON){
_raw_tty.c_iflag &= ~IXON; /* turn off ^S/^Q on input */
tcsetattr (STDIN_FD, TCSADRAIN, &_raw_tty);
}
#elif HAS_TERMIO
if(_raw_tty.c_iflag & IXON){
_raw_tty.c_iflag &= ~IXON; /* turn off ^S/^Q on input */
(void)ioctl(STDIN_FD, TCSETAW, &_raw_tty);
}
#else /* RAW_BSD */
if(!(_raw_tchars.t_startc == -1 && _raw_tchars.t_stopc == -1)){
_raw_tchars.t_startc = -1; /* Turn off ^S/^Q */
_raw_tchars.t_stopc = -1;
(void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
}
#endif
}
}
}
/*
* Set up the tty driver to do LF->CR translation
*
* Args: state -- True to turn on translation, false to write raw LF's
*
* Result: none.
*/
void
crlf_proc(int state)
{
if(_inraw){
if(state){ /* turn ON NL->CR on output */
#if HAS_TERMIOS
if(!(_raw_tty.c_oflag & ONLCR)){
_raw_tty.c_oflag |= ONLCR;
tcsetattr (STDIN_FD, TCSADRAIN, &_raw_tty);
}
#elif HAS_TERMIO
if(!(_raw_tty.c_oflag & ONLCR)){
_raw_tty.c_oflag |= ONLCR;
(void)ioctl(STDIN_FD, TCSETAW, &_raw_tty);
}
#else /* RAW_BSD */
if(!(_raw_tty.sg_flags & CRMOD)){
_raw_tty.sg_flags |= CRMOD;
(void)ioctl(STDIN_FD, TIOCSETP, &_raw_tty);
}
#endif
}
else{ /* turn OFF NL-CR on output */
#if HAS_TERMIOS
if(_raw_tty.c_oflag & ONLCR){
_raw_tty.c_oflag &= ~ONLCR;
tcsetattr (STDIN_FD, TCSADRAIN, &_raw_tty);
}
#elif HAS_TERMIO
if(_raw_tty.c_oflag & ONLCR){
_raw_tty.c_oflag &= ~ONLCR;
(void)ioctl(STDIN_FD, TCSETAW, &_raw_tty);
}
#else /* RAW_BSD */
if(_raw_tty.sg_flags & CRMOD){
_raw_tty.sg_flags &= ~CRMOD;
(void)ioctl(STDIN_FD, TIOCSETP, &_raw_tty);
}
#endif
}
}
}
/*
* Set up the tty driver to hanle interrupt char
*
* Args: state -- True to turn on interrupt char, false to not
*
* Result: tty driver that'll send us SIGINT or not
*/
void
intr_proc(int state)
{
if(_inraw){
if(state){
#if HAS_TERMIOS
_raw_tty.c_lflag |= ISIG; /* enable signals */
tcsetattr(STDIN_FD, TCSADRAIN, &_raw_tty);
#elif HAS_TERMIO
_raw_tty.c_lflag |= ISIG; /* enable signals */
(void)ioctl(STDIN_FD, TCSETAW, &_raw_tty);
#else /* RAW_BSD */
_raw_tchars.t_intrc = ctrl('C');
(void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
#endif
}
else{
#if HAS_TERMIOS
_raw_tty.c_lflag &= ~ISIG; /* disable signals */
tcsetattr(STDIN_FD, TCSADRAIN, &_raw_tty);
#elif HAS_TERMIO
_raw_tty.c_lflag &= ~ISIG; /* disable signals */
(void)ioctl(STDIN_FD, TCSETAW, &_raw_tty);
#else /* RAW_BSD */
_raw_tchars.t_intrc = -1;
(void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
#endif
}
}
}
/*
* Discard any pending input characters
*
* Args: none
*
* Result: pending input buffer flushed
*/
void
flush_input(void)
{
#if HAS_TERMIOS
tcflush(STDIN_FD, TCIFLUSH);
#elif HAS_TERMIO
ioctl(STDIN_FD, TCFLSH, 0);
#else /* RAW_BSD */
#ifdef TIOCFLUSH
#ifdef FREAD
int i = FREAD;
#else
int i = 1;
#endif
ioctl(STDIN_FD, TIOCFLUSH, &i);
#endif /* TIOCFLUSH */
#endif
}
/*
* Turn off hi bit stripping
*/
void
bit_strip_off(void)
{
#if HAS_TERMIOS
_raw_tty.c_iflag &= ~ISTRIP;
tcsetattr(STDIN_FD, TCSADRAIN, &_raw_tty);
#elif HAS_TERMIO
/* no op */
#else /* RAW_BSD */
#ifdef LPASS8
_raw_lmode |= LPASS8;
#endif
#ifdef LPASS8OUT
_raw_lmode |= LPASS8OUT;
#endif
(void)ioctl(STDIN_FD, TIOCLSET, &_raw_lmode);
#endif
}
/*
* Turn off quit character (^\) if possible
*/
void
quit_char_off(void)
{
#if HAS_TERMIOS
#elif HAS_TERMIO
#else /* RAW_BSD */
_raw_tchars.t_quitc = -1;
(void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
#endif
}
/*
* Returns TRUE if tty is < 4800, 0 otherwise.
*/
int
ttisslow(void)
{
#if HAS_TERMIOS
return(cfgetospeed(&_raw_tty) < B4800);
#elif HAS_TERMIO
return((_raw_tty.c_cflag&CBAUD) < (unsigned int)B4800);
#else /* RAW_BSD */
return((int)_raw_tty.sg_ispeed < B4800);
#endif
}
|