Codebase list tack / fresh-snapshots/main sysdep.c
fresh-snapshots/main

Tree @fresh-snapshots/main (Download .tar.gz)

sysdep.c @fresh-snapshots/mainraw · history · blame

  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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
** Copyright (C) 1991, 1997-2010,2012 Free Software Foundation, Inc.
**
** This file is part of TACK.
**
** TACK is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** TACK is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with TACK; see the file COPYING.  If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA
*/
/*
 * Operating system dependent functions.  We assume the POSIX API.
 * Note: on strict-POSIX systems (including BSD/OS) the select_delay_type
 * global has no effect.
 */

#include <tack.h>

#include <term.h>
#include <errno.h>

#if defined(__BEOS__)
#undef false
#undef true
#include <OS.h>
#endif

#if HAVE_SELECT
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#endif

MODULE_ID("$Id: sysdep.c,v 1.23 2012/09/16 15:16:00 Adrian.Bunk Exp $")

#ifdef TERMIOS
#define PUT_TTY(fd, buf) tcsetattr(fd, TCSAFLUSH, buf)
#else
#define PUT_TTY(fd, buf) stty(fd, buf)
#endif

/* globals */
int tty_frame_size;		/* asynch frame size times 2 */
unsigned tty_baud_rate;		/* baud rate - bits per second */
SIG_ATOMIC_T not_a_tty;		/* TRUE if output is not a tty (i.e. pipe) */
int nodelay_read;		/* TRUE if NDELAY is set */

#ifdef TERMIOS
#define TTY_IS_NOECHO	!(new_modes.c_lflag & ECHO)
#define TTY_IS_OUT_TRANS (new_modes.c_oflag & OPOST)
#define TTY_IS_CHAR_MODE !(new_modes.c_lflag & ICANON)
#define TTY_WAS_CS8 ((old_modes.c_cflag & CSIZE) == CS8)
#define TTY_WAS_XON_XOFF (old_modes.c_iflag & (IXON|IXOFF))
#else
#define TTY_IS_NOECHO	!(new_modes.sg_flags & (ECHO))
#define TTY_IS_OUT_TRANS (new_modes.sg_flags & (CRMOD))
#define TTY_IS_CHAR_MODE (new_modes.sg_flags & (RAW|CBREAK))
#define TTY_WAS_CS8	 (old_modes.sg_flags & (PASS8))
#define TTY_WAS_XON_XOFF (old_modes.sg_flags & (TANDEM|MDMBUF|DECCTQ))
#endif

static TTY old_modes, new_modes;

void catchsig(void);

/*
 * These are a sneaky way of conditionalizing bit unsets so strict-POSIX
 * systems won't see them.
 */
#ifndef XCASE
#define XCASE	0
#endif
#ifndef OLCUC
#define OLCUC	0
#endif
#ifndef IUCLC
#define IUCLC	0
#endif
#ifndef TABDLY
#define	TABDLY	0
#endif
#ifndef IXANY
#define	IXANY	0
#endif

void
tty_raw(int minch GCC_UNUSED, int mask)
{				/* set tty to raw noecho */
    new_modes = old_modes;
#ifdef TERMIOS
#if HAVE_SELECT
    new_modes.c_cc[VMIN] = 1;
#else
    new_modes.c_cc[VMIN] = minch;
#endif
    new_modes.c_cc[VTIME] = 2;
    new_modes.c_lflag &=
	(unsigned) ~(ISIG | ICANON | XCASE | ECHO | ECHOE | ECHOK | ECHONL);
#ifdef LOBLK
    new_modes.c_lflag &= ~LOBLK;
#endif
    new_modes.c_oflag &= (unsigned) ~(OPOST | OLCUC | TABDLY);
    if (mask == ALLOW_PARITY) {
	new_modes.c_cflag &= (unsigned) ~(CSIZE | PARENB | HUPCL);
	new_modes.c_cflag |= CS8;
    }
    new_modes.c_iflag &=
	(unsigned) ~(IGNBRK
		     | BRKINT
		     | IGNPAR
		     | PARMRK
		     | INPCK
		     | ISTRIP
		     | INLCR
		     | IGNCR
		     | ICRNL
		     | IUCLC
		     | IXON
		     | IXANY
		     | IXOFF);
#else
    new_modes.sg_flags |= RAW;
#endif
    if (not_a_tty)
	return;
    PUT_TTY(fileno(stdin), &new_modes);
}

void
tty_set(void)
{				/* set tty to special modes */
    new_modes = old_modes;
#ifdef TERMIOS
    new_modes.c_cc[VMIN] = 1;
    new_modes.c_cc[VTIME] = 1;
    new_modes.c_lflag &= (unsigned) ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
#if defined(ONLCR) && defined(OCRNL) && defined(ONLRET) && defined(OFILL)
    new_modes.c_oflag &= (unsigned) ~(ONLCR | OCRNL | ONLRET | OFILL);
#else
    new_modes.c_oflag &= (unsigned) ~(OPOST);
#endif
    if (char_mask == ALLOW_PARITY)
	new_modes.c_iflag &= (unsigned) ~ISTRIP;
    switch (select_xon_xoff) {
    case 0:
	new_modes.c_iflag &= (unsigned) ~(IXON | IXOFF);
	break;
    case 1:
#if defined(sequent) && sequent
	/* the sequent System V emulation is broken */
	new_modes = old_modes;
	new_modes.c_cc[VEOL] = 6;	/* control F  (ACK) */
#endif
	new_modes.c_iflag |= IXON | IXOFF;
	break;
    }
    switch (select_delay_type) {
    case 0:
#ifdef NLDLY
	new_modes.c_oflag &=
	    (unsigned) ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
#endif /* NLDLY */
	break;
    case 1:
#ifdef NLDLY
	new_modes.c_oflag &=
	    (unsigned) ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
#endif /* NLDLY */
#ifdef NL1
	new_modes.c_oflag |= NL1 | CR2;
#endif /* NL1 */
	break;
    }
    if ((new_modes.c_oflag & (unsigned long) ~OPOST) == 0)
	new_modes.c_oflag &= (unsigned long) ~OPOST;
#else
    new_modes.sg_flags |= RAW;
    if (not_a_tty)
	return;
#endif
    PUT_TTY(fileno(stdin), &new_modes);
}

void
tty_reset(void)
{				/* reset the tty to the original modes */
    fflush(stdout);
    if (not_a_tty)
	return;
    PUT_TTY(fileno(stdin), &old_modes);
}

void
tty_init(void)
{				/* ATT terminal init */
#if defined(F_GETFL) && defined(O_NDELAY)
    int flags;

    flags = fcntl(fileno(stdin), F_GETFL, 0);
    nodelay_read = flags & O_NDELAY;
#else
    nodelay_read = FALSE;
#endif
    not_a_tty = FALSE;
    if (GET_TTY(fileno(stdin), &old_modes) == -1) {
	if (errno == ENOTTY) {
	    tty_frame_size = 20;
	    not_a_tty = TRUE;
	    return;
	}
	printf("tcgetattr error: %d\n", errno);
	ExitProgram(EXIT_FAILURE);
    }
    /* if TAB3 is set then setterm() wipes out tabs (ht) */
    new_modes = old_modes;
#ifdef TERMIOS
#ifdef TABDLY
    new_modes.c_oflag &= (unsigned) ~TABDLY;
#endif /* TABDLY */
#endif
    if (PUT_TTY(fileno(stdin), &new_modes) == -1) {
	printf("tcsetattr error: %d\n", errno);
	ExitProgram(EXIT_FAILURE);
    }
#ifdef sequent
    /* the sequent ATT emulation is broken soooo. */
    old_modes.c_cflag &= ~(CSIZE | CSTOPB);
    old_modes.c_cflag |= CS7 | PARENB;
#endif
    catchsig();
#ifdef TERMIOS
    switch (old_modes.c_cflag & CSIZE) {
#if defined(CS5) && (CS5 != 0)
    case CS5:
	tty_frame_size = 10;
	break;
#endif
#if defined(CS6) && (CS6 != 0)
    case CS6:
	tty_frame_size = 12;
	break;
#endif
#if defined(CS7) && (CS7 != 0)
    case CS7:
	tty_frame_size = 14;
	break;
#endif
#if defined(CS8) && (CS8 != 0)
    case CS8:
	tty_frame_size = 16;
	break;
#endif
    }
    tty_frame_size += 2 +
	((old_modes.c_cflag & PARENB) ? 2 : 0) +
	((old_modes.c_cflag & CSTOPB) ? 4 : 2);
#else
    tty_frame_size = 6 +
	(old_modes.sg_flags & PASS8) ? 16 : 14;
#endif
}

/*
**	stty_query(question)
**
**	Does the current driver settings have this property?
*/
int
stty_query(int q)
{
    switch (q) {
    case TTY_NOECHO:
	return TTY_IS_NOECHO;
    case TTY_OUT_TRANS:
	return (int) TTY_IS_OUT_TRANS;
    case TTY_CHAR_MODE:
	return TTY_IS_CHAR_MODE;
    }
    return (-1);
}

/*
**	initial_stty_query(question)
**
**	Did the initial driver settings have this property?
*/
int
initial_stty_query(int q)
{
    switch (q) {
    case TTY_8_BIT:
	return TTY_WAS_CS8;
    case TTY_XON_XOFF:
	return (int) TTY_WAS_XON_XOFF;
    }
    return (-1);
}

#if HAVE_SELECT && defined(FD_ZERO)
static int
char_ready(void)
{
    int n;
    fd_set ifds;
    struct timeval tv;

    FD_ZERO(&ifds);
    FD_SET(fileno(stdin), &ifds);
    tv.tv_sec = 0;
    tv.tv_usec = 200000;
    n = select(fileno(stdin) + 1, &ifds, NULL, NULL, &tv);
    return (n != 0);
}

#else
#ifdef FIONREAD
int
char_ready(void)
{
    int i, j;

    /* the following loop has to be tuned for each computer */
    for (j = 0; j < 1000; j++) {
	ioctl(fileno(stdin), FIONREAD, &i);
	if (i)
	    return i;
    }
    return i;
}

#else
#if defined(__BEOS__)
int
char_ready(void)
{
    int n = 0;
    int howmany = ioctl(0, 'ichr', &n);
    return (howmany >= 0 && n > 0);
}
#else
#define char_ready() 1
#endif
#endif
#endif

/*
**	spin_flush()
**
**	Wait for the input stream to stop.
**	Throw away all input characters.
*/
void
spin_flush(void)
{
    unsigned char buf[64];

    fflush(stdout);
    event_start(TIME_FLUSH);	/* start the timer */
    do {
	if (char_ready()) {
	    if (read(fileno(stdin), &buf, sizeof(buf)))
		break;
	}
    } while (event_time(TIME_FLUSH) < 400000);
}

/*
**	read_key(input-buffer, length-of-buffer)
**
**	read one function key from the input stream.
**	A null character is converted to 0x80.
*/
void
read_key(char *buf, size_t max)
{
    int got, ask, i, l;
    char *s;

    *buf = '\0';
    s = buf;
    fflush(stdout);
    /* ATT unix may return 0 or 1, Berkeley Unix should be 1 */
    while (read(fileno(stdin), s, (size_t) 1) == 0) {
	;			/* EMPTY */
    }
    ++s;
    --max;
    while ((int) max > 0 && (ask = char_ready()) > 0) {
	if (ask > (int) max) {
	    ask = (int) max;
	}
	if ((got = (int) read(fileno(stdin), s, (size_t) ask)) > 0) {
	    s += got;
	} else {
	    break;
	}
	max -= (size_t) got;
    }
    *s = '\0';
    l = (int) (s - buf);
    for (s = buf, i = 0; i < l; i++) {
	if ((*s & 0x7f) == 0) {
	    /* convert nulls to 0x80 */
	    *(unsigned char *) s = 128;
	} else {
	    /* strip high order bits (if any) */
	    *s = (char) (*s & char_mask);
	}
    }
}

void
ignoresig(void)
{
    /* ignore signals */
    signal(SIGINT, SIG_IGN);
    signal(SIGHUP, SIG_IGN);
    signal(SIGQUIT, SIG_IGN);
    signal(SIGTERM, SIG_IGN);
    signal(SIGALRM, SIG_IGN);
}

/*
 * onintr( )
 *
 * is the interrupt handling routine.
 * onintr turns off interrupts while doing clean-up.
 *
 * onintr always exits fatally
 */
static void
onintr(int sig GCC_UNUSED)
{
    ignoresig();
    tty_reset();
    ExitProgram(EXIT_FAILURE);
}

/*
 * catchsig( )
 *
 * set up to field interrupts (via function onintr( )) so that if interrupted
 * we can restore the correct terminal modes
 *
 * catchsig simply returns
 */
void
catchsig(void)
{
    if ((signal(SIGINT, SIG_IGN)) == SIG_DFL)
	signal(SIGINT, onintr);

    if ((signal(SIGHUP, SIG_IGN)) == SIG_DFL)
	signal(SIGHUP, onintr);

    if ((signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
	signal(SIGQUIT, onintr);

    if ((signal(SIGTERM, SIG_IGN)) == SIG_DFL)
	signal(SIGTERM, onintr);

}

/*
**	alarm_event(sig)
**
**	Come here for an alarm event
*/
static void
alarm_event(
	       int sig GCC_UNUSED)
{
    no_alarm_event = 0;
}

/*
**	set_alarm_clock(seconds)
**
**	Set the alarm clock to fire in <seconds>
*/
void
set_alarm_clock(
		   int seconds)
{
    signal(SIGALRM, alarm_event);
    no_alarm_event = 1;
    (void) alarm((unsigned) seconds);
}