2 ** Copyright (C) 1991, 1997 Free Software Foundation, Inc.
4 ** This file is part of TACK.
6 ** TACK is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2, or (at your option)
11 ** TACK is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with TACK; see the file COPYING. If not, write to
18 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 ** Boston, MA 02111-1307, USA.
22 * Operating system dependant functions. We assume the POSIX API.
23 * Note: on strict-POSIX systems (including BSD/OS) the select_delay_type
24 * global has no effect.
38 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
42 #include <sys/select.h>
46 MODULE_ID("$Id: sysdep.c,v 1.6 1999/09/04 13:45:00 tom Exp $")
53 int tty_frame_size; /* asynch frame size times 2 */
54 unsigned long tty_baud_rate; /* baud rate - bits per second */
55 int not_a_tty; /* TRUE if output is not a tty (i.e. pipe) */
56 int nodelay_read; /* TRUE if NDELAY is set */
58 #define TTY_IS_NOECHO !(new_modes.c_lflag & ECHO)
59 #define TTY_IS_OUT_TRANS (new_modes.c_oflag & OPOST)
60 #define TTY_IS_CHAR_MODE !(new_modes.c_lflag & ICANON)
61 #define TTY_WAS_CS8 ((old_modes.c_cflag & CSIZE) == CS8)
62 #define TTY_WAS_XON_XOFF (old_modes.c_iflag & (IXON|IXOFF))
64 static TTY old_modes, new_modes;
69 * These are a sneaky way of conditionalizing bit unsets so strict-POSIX
70 * systems won't see them.
89 tty_raw(int minch GCC_UNUSED, int mask)
90 { /* set tty to raw noecho */
91 new_modes = old_modes;
93 new_modes.c_cc[VMIN] = 1;
95 new_modes.c_cc[VMIN] = minch;
97 new_modes.c_cc[VTIME] = 2;
99 ~(ISIG | ICANON | XCASE | ECHO | ECHOE | ECHOK | ECHONL);
101 new_modes.c_lflag &= ~LOBLK;
103 new_modes.c_oflag &= ~(OPOST | OLCUC | TABDLY);
104 if (mask == ALLOW_PARITY) {
105 new_modes.c_cflag &= ~(CSIZE | PARENB | HUPCL);
106 new_modes.c_cflag |= CS8;
109 ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL |
110 IUCLC | IXON | IXANY | IXOFF);
113 tcsetattr(fileno(stdin), TCSAFLUSH, &new_modes);
118 { /* set tty to special modes */
119 new_modes = old_modes;
120 new_modes.c_cc[VMIN] = 1;
121 new_modes.c_cc[VTIME] = 1;
122 new_modes.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
123 #if defined(ONLCR) && defined(OCRNL) && defined(ONLRET) && defined(OFILL)
124 new_modes.c_oflag &= ~(ONLCR | OCRNL | ONLRET | OFILL);
126 new_modes.c_oflag &= ~(OPOST);
128 if (char_mask == ALLOW_PARITY)
129 new_modes.c_iflag &= ~ISTRIP;
130 switch (select_xon_xoff) {
132 new_modes.c_iflag &= ~(IXON | IXOFF);
136 /* the sequent System V emulation is broken */
137 new_modes = old_modes;
138 new_modes.c_cc[VEOL] = 6; /* control F (ACK) */
140 new_modes.c_iflag |= IXON | IXOFF;
143 switch (select_delay_type) {
147 ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
153 ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
156 new_modes.c_oflag |= NL1 | CR2;
160 if (!(new_modes.c_oflag & ~OPOST))
161 new_modes.c_oflag &= ~OPOST;
164 tcsetattr(fileno(stdin), TCSAFLUSH, &new_modes);
170 { /* reset the tty to the original modes */
174 tcsetattr(fileno(stdin), TCSAFLUSH, &old_modes);
180 { /* ATT terminal init */
181 #if defined(F_GETFL) && defined(O_NDELAY)
184 flags = fcntl(fileno(stdin), F_GETFL, 0);
185 nodelay_read = flags & O_NDELAY;
187 nodelay_read = FALSE;
190 if (tcgetattr(fileno(stdin), &old_modes) == -1) {
191 if (errno == ENOTTY) {
196 printf("tcgetattr error: %d\n", errno);
199 /* if TAB3 is set then setterm() wipes out tabs (ht) */
200 new_modes = old_modes;
202 new_modes.c_oflag &= ~TABDLY;
204 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new_modes) == -1) {
205 printf("tcsetattr error: %d\n", errno);
209 /* the sequent ATT emulation is broken soooo. */
210 old_modes.c_cflag &= ~(CSIZE | CSTOPB);
211 old_modes.c_cflag |= CS7 | PARENB;
214 switch (old_modes.c_cflag & CSIZE) {
215 #if defined(CS5) && (CS5 != 0)
220 #if defined(CS6) && (CS6 != 0)
225 #if defined(CS7) && (CS7 != 0)
230 #if defined(CS8) && (CS8 != 0)
236 tty_frame_size += 2 +
237 ((old_modes.c_cflag & PARENB) ? 2 : 0) +
238 ((old_modes.c_cflag & CSTOPB) ? 4 : 2);
242 ** stty_query(question)
244 ** Does the current driver settings have this property?
251 return TTY_IS_NOECHO;
253 return TTY_IS_OUT_TRANS;
255 return TTY_IS_CHAR_MODE;
261 ** initial_stty_query(question)
263 ** Did the initial driver settings have this property?
266 initial_stty_query(int q)
272 return TTY_WAS_XON_XOFF;
277 #if HAVE_SELECT && defined(FD_ZERO)
286 FD_SET(fileno(stdin), &ifds);
289 n = select(fileno(stdin)+1, &ifds, NULL, NULL, &tv);
300 /* the following loop has to be tuned for each computer */
301 for (j = 0; j < 1000; j++) {
302 ioctl(fileno(stdin), FIONREAD, &i);
310 #if defined(__BEOS__)
315 int howmany = ioctl(0, 'ichr', &n);
316 return (howmany >= 0 && n > 0);
319 #define char_ready() 1
327 ** Wait for the input stream to stop.
328 ** Throw away all input characters.
333 unsigned char buf[64];
336 event_start(TIME_FLUSH); /* start the timer */
339 (void) read(fileno(stdin), &buf, sizeof(buf));
341 } while (event_time(TIME_FLUSH) < 400000);
345 ** read_key(input-buffer, length-of-buffer)
347 ** read one function key from the input stream.
348 ** A null character is converted to 0x80.
351 read_key(char *buf, int max)
359 /* ATT unix may return 0 or 1, Berkeley Unix should be 1 */
360 while (read(fileno(stdin), s, 1) == 0);
363 while (max > 0 && (ask = char_ready())) {
367 if ((got = read(fileno(stdin), s, ask))) {
376 for (s = buf, i = 0; i < l; i++) {
377 if ((*s & 0x7f) == 0) {
378 /* convert nulls to 0x80 */
379 *(unsigned char *)s = 128;
381 /* strip high order bits (if any) */
392 signal(SIGINT, SIG_IGN);
393 signal(SIGHUP, SIG_IGN);
394 signal(SIGQUIT, SIG_IGN);
395 signal(SIGTERM, SIG_IGN);
396 signal(SIGALRM, SIG_IGN);
402 is the interrupt handling routine onintr turns off interrupts while doing
405 onintr always exits fatally
410 onintr(int sig GCC_UNUSED)
421 set up to field interrupts (via function onintr( )) so that if interrupted
422 we can restore the correct terminal modes
424 catchsig simply returns
431 if ((signal(SIGINT, SIG_IGN)) == SIG_DFL)
432 signal(SIGINT, onintr);
434 if ((signal(SIGHUP, SIG_IGN)) == SIG_DFL)
435 signal(SIGHUP, onintr);
437 if ((signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
438 signal(SIGQUIT, onintr);
440 if ((signal(SIGTERM, SIG_IGN)) == SIG_DFL)
441 signal(SIGTERM, onintr);
448 ** Come here for an alarm event
458 ** set_alarm_clock(seconds)
460 ** Set the alarm clock to fire in <seconds>
466 signal(SIGALRM, alarm_event);
468 (void) alarm(seconds);