summaryrefslogtreecommitdiffstats
path: root/lib/libmytinfo/tty.c
blob: 2ba2a7ccf905a0ff31807b7cd3e1a6abf077e49a (plain)
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
/*
 * tty.c
 *
 * By Ross Ridge
 * Public Domain
 * 92/02/01 07:30:49
 *
 * Do some tty related things
 *
 */

#include "defs.h"
#include <term.h>

#ifdef USE_SCCS_IDS
static const char SCCSid[] = "@(#) mytinfo tty.c 3.2 92/02/01 public domain, By Ross Ridge";
#endif
#ifndef EXTA_IS
#define EXTA_IS 19200
#endif

#ifndef EXTB_IS
#define EXTB_IS 38400
#endif

#ifdef lint
#define ioctl _ioctl
/* shutup lint */
/* ARGSUSED */
/* VARARGS1 */
static int ioctl(a, b, p) int a; long b; anyptr *p;  { return 0; }
#endif

#if defined(USE_TERMIO) || defined(USE_TERMIOS)

#ifdef USE_TERMIOS
speed_t _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
		    2400, 4800, 9600, EXTA_IS, EXTB_IS
#ifdef B57600
		    ,57600
#endif
#ifdef B115200
		    ,115200
#endif
		    };
#else
#ifdef USE_SMALLMEM
unsigned short _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
			      1800, 2400, 4800, 9600, EXTA_IS, EXTB_IS};
#else
long _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
		    2400, 4800, 9600, EXTA_IS, EXTB_IS};
#endif
#endif

#ifdef USE_TERMIO
static struct termio old;
#else
static struct termios old;
#endif

int
_lit_output() {
#ifdef USE_TERMIO
	struct termio tmp;
	if (ioctl(cur_term->fd, TCGETA, &old) == -1)
		return 0;
#else
	struct termios tmp;
	if (tcgetattr(cur_term->fd, &old) == -1)
		return 0;
#endif
	if (!(old.c_oflag & OPOST) || !(old.c_oflag & ONLCR))
		return 0;
	memcpy((anyptr)&tmp, (anyptr)&old, sizeof(old));
	tmp.c_oflag &= ~OPOST;
#ifdef USE_TERMIO
	ioctl(cur_term->fd, TCSETA, &tmp);
#else
	tcsetattr(cur_term->fd, TCSADRAIN, &tmp);
#endif
	return 1;
}

void
_norm_output() {
#ifdef USE_TERMIO
	ioctl(cur_term->fd, TCSETA, &old);
#else
	tcsetattr(cur_term->fd, TCSADRAIN, &old);
#endif
}

int
_check_tty() {
	if ((cur_term->prog_mode.c_iflag & IXON) && cur_term->xon)
		cur_term->pad = 0;
	else
		cur_term->pad = 1;
#ifdef USE_TERMIO
	cur_term->baudrate = _baud_tbl[cur_term->prog_mode.c_cflag & CBAUD];
#else
	cur_term->baudrate = cfgetospeed(&cur_term->prog_mode);
#endif
	return OK;
}

int
def_prog_mode() {
#ifdef USE_WINSZ
#ifdef TIOCGWINSZ
	if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->prog_winsz) < 0)
		return ERR;
#endif
#endif
#ifdef USE_TERMIO
	if (ioctl(cur_term->fd, TCGETA, &cur_term->prog_mode) == 0
#else
	if (tcgetattr(cur_term->fd, &cur_term->prog_mode) == 0
#endif
            && _check_tty() == OK)
		return OK;
	return ERR;
}

int
def_shell_mode() {
	cur_term->termcap = 0;
#ifdef USE_WINSZ
#ifdef TIOCGWINSZ
	if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->shell_winsz) < 0)
		return ERR;
#endif
#endif
#ifdef USE_TERMIO
	return ioctl(cur_term->fd, TCGETA, &cur_term->shell_mode)==0 ? OK : ERR;
#else
	return tcgetattr(cur_term->fd, &cur_term->shell_mode)==0 ? OK : ERR;
#endif
}

int
reset_prog_mode() {
#ifdef USE_TERMIO
	return ioctl(cur_term->fd, TCSETA, &cur_term->prog_mode)==0 ? OK : ERR;
#else
	return tcsetattr(cur_term->fd, TCSADRAIN, &cur_term->prog_mode)==0 ? OK : ERR;
#endif
}

int
reset_shell_mode() {
#ifdef USE_TERMIO
	return ioctl(cur_term->fd, TCSETA, &cur_term->shell_mode)==0 ? OK : ERR;
#else
	return tcsetattr(cur_term->fd, TCSADRAIN, &cur_term->shell_mode)==0 ? OK : ERR;
#endif
}

int
_init_tty() {
	cur_term->true_lines = lines;
	cur_term->true_columns = columns;
	if (pad_char == NULL)
		cur_term->padch = '\000';
	else
		cur_term->padch = pad_char[0];
	if (def_shell_mode() == ERR || def_prog_mode() == ERR) {
		cur_term->pad = 0;
		cur_term->baudrate = 1;
		cur_term->xon = 0;
		return OK;
	}
	cur_term->xon = (xoff_character == NULL || xoff_character[0] == '\021')
			&& (xon_character == NULL || xon_character[0] == '\023')
			&& xon_xoff;
#ifdef USE_WINSZ
#ifdef TIOCGWINSZ
	if (cur_term->prog_winsz.ws_row != 0
	    && cur_term->prog_winsz.ws_col != 0) {
		lines = cur_term->prog_winsz.ws_row;
		columns = cur_term->prog_winsz.ws_col;
	}
#endif
#endif
	return OK;
}

#else

#ifdef USE_SGTTY

#ifdef USE_SMALLMEM
unsigned short _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
			      1800, 2400, 4800, 9600, EXTA_IS, EXTB_IS};
#else
long _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
		    1800, 2400, 4800, 9600, EXTA_IS, EXTB_IS};
#endif


#ifdef TIOCLGET

static int old;

int
_lit_output() {
	struct sgttyb buf;
	int tmp;

	ioctl(cur_term->fd, TIOCGETP, &buf);
	if (buf.sg_flags & RAW)
		return 0;
	ioctl(cur_term->fd, TIOCLGET, &old);
	if (old & LLITOUT)
		return 0;
	tmp = old | LLITOUT;
	ioctl(cur_term->fd, TIOCLSET, &tmp);
	return 1;
}

void
_norm_output() {
	ioctl(cur_term->fd, TIOCLSET, &old);
}

#else

static struct sgttyb old;

int
_lit_output() {
	struct sgttyb tmp;
	ioctl(cur_term->fd, TIOCGETP, &old);
	if (old.sg_flags & RAW)
		return 0;
	memcpy((anyptr)&tmp, (anyptr)&old, sizeof(old));
	tmp.sg_flags |= RAW;
	ioctl(cur_term->fd, TIOCSETP, &tmp);
	return 1;
}

void
_norm_output() {
	ioctl(cur_term->fd, TIOCSETP, &old);
}

#endif

int
_check_tty() {
	if (!(cur_term->prog_mode.v6.sg_flags & RAW) && cur_term->xon)
		cur_term->pad = 0;
	else
		cur_term->pad = 1;
	cur_term->baudrate = _baud_tbl[cur_term->prog_mode.v6.sg_ospeed & 0xf];
	return OK;
}

int
def_shell_mode() {
	if (ioctl(cur_term->fd, TIOCGETP, &cur_term->shell_mode.v6) < 0)
		return ERR;
#ifdef TIOCGETC
	if (ioctl(cur_term->fd, TIOCGETC, &cur_term->shell_mode.v7) < 0)
		return ERR;
#endif
#ifdef TIOCLGET
	if (ioctl(cur_term->fd, TIOCLGET, &cur_term->shell_mode.bsd) < 0)
		return ERR;
#endif
#ifdef TIOCGLTC
	if (ioctl(cur_term->fd, TIOCGLTC, &cur_term->shell_mode.bsd_new) < 0)
		return ERR;
#endif
#ifdef USE_WINSZ
#ifdef TIOCGWINSZ
	if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->shell_winsz)<0)
		return ERR;
#endif
#endif
	cur_term->termcap = 0;
	return OK;
}

int
def_prog_mode() {
	if (ioctl(cur_term->fd, TIOCGETP, &cur_term->prog_mode.v6) < 0)
		return ERR;
#ifdef TIOCGETC
	if (ioctl(cur_term->fd, TIOCGETC, &cur_term->prog_mode.v7) < 0)
		return ERR;
#endif
#ifdef TIOCLGET
	if (ioctl(cur_term->fd, TIOCLGET, &cur_term->prog_mode.bsd) < 0)
		return ERR;
#endif
#ifdef TIOCGLTC
	if (ioctl(cur_term->fd, TIOCGLTC, &cur_term->prog_mode.bsd_new) < 0)
		return ERR;
#endif
#ifdef USE_WINSZ
#ifdef TIOCGWINSZ
	if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->prog_winsz)<0)
		return ERR;
#endif
#endif
	return _check_tty();
}

int
reset_shell_mode() {
	if (ioctl(cur_term->fd, TIOCSETP, &cur_term->shell_mode.v6) < 0)
		return ERR;
#ifdef TIOCGETC
	if (ioctl(cur_term->fd, TIOCSETC, &cur_term->shell_mode.v7) < 0)
		return ERR;
#endif
#ifdef TIOCLGET
	if (ioctl(cur_term->fd, TIOCLSET, &cur_term->shell_mode.bsd) < 0)
		return ERR;
#endif
#ifdef TIOCGLTC
	if (ioctl(cur_term->fd, TIOCSLTC, &cur_term->shell_mode.bsd_new) < 0)
		return ERR;
#endif
	return OK;
}

int
reset_prog_mode() {
	if (ioctl(cur_term->fd, TIOCSETP, &cur_term->prog_mode.v6) < 0)
		return ERR;
#ifdef TIOCGETC
	if (ioctl(cur_term->fd, TIOCSETC, &cur_term->prog_mode.v7) < 0)
		return ERR;
#endif
#ifdef TIOCLGET
	if (ioctl(cur_term->fd, TIOCLSET, &cur_term->prog_mode.bsd) < 0)
		return ERR;
#endif
#ifdef TIOCGLTC
	if (ioctl(cur_term->fd, TIOCSLTC, &cur_term->prog_mode.bsd_new) < 0)
		return ERR;
#endif
	return OK;
}

int
_init_tty() {
	cur_term->true_lines = lines;
	cur_term->true_columns = columns;
	if (pad_char == NULL)
		cur_term->padch = '\000';
	else
		cur_term->padch = pad_char[0];
	if (def_shell_mode() == ERR || def_prog_mode() == ERR) {
		cur_term->pad = 0;
		cur_term->baudrate = 1;
		cur_term->xon = 0;
		return OK;
	}
#ifndef TIOCGETC
	cur_term->xon = (xoff_character == NULL || xoff_character[0] == '\021')
			&& (xon_character == NULL || xon_character[0] == '\023')
		        && xon_xoff;
#else
	if (xon_xoff) {
		if (xon_character != NULL) {
			cur_term->prog_mode.v7.t_startc = xon_character[0];
			if (ioctl(cur_term->fd, TIOCSETC,
			    &cur_term->prog_mode.v7) < 0)
				return ERR;
		}
		if (xoff_character != NULL) {
			cur_term->prog_mode.v7.t_stopc = xoff_character[0];
			if (ioctl(cur_term->fd, TIOCSETC,
			          &cur_term->prog_mode.v7) < 0)
				return ERR;
		}
	}
	cur_term->xon = xon_xoff;
#endif
#ifdef USE_WINSZ
#ifdef TIOCGWINSZ
	if (cur_term->prog_winsz.ws_row != 0
	    && cur_term->prog_winsz.ws_col != 0) {
		lines = cur_term->prog_winsz.ws_row;
		columns = cur_term->prog_winsz.ws_col;
	}
#endif
#endif
	return OK;
}

#else

int
_lit_output() {
	return 0;
}

void
_norm_output() {
	return;
}

int
_check_tty() {
	return OK;
}

int
def_prog_mode() {
	return OK;
}

int
reset_prog_mode() {
	return OK;
}

int
def_shell_mode() {
	return OK;
}

int
reset_shell_mode() {
	return OK;
}

int
_init_tty() {
	cur_term->pad = 1;
	cur_term->padch = 0;
	cur_term->baudrate = 1200;
	cur_term->xon = 0;
	cur_term->termcap = 0;
	cur_term->true_lines = lines;
	cur_term->true_columns = columns;
}

#endif

#endif
OpenPOWER on IntegriCloud