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 dependent 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.9 2000/09/02 19:17:39 tom Exp $")
53 #define PUT_TTY(fd, buf) tcsetattr(fd, TCSAFLUSH, buf)
55 #define PUT_TTY(fd, buf) stty(fd, buf)
59 int tty_frame_size; /* asynch frame size times 2 */
60 unsigned long tty_baud_rate; /* baud rate - bits per second */
61 int not_a_tty; /* TRUE if output is not a tty (i.e. pipe) */
62 int nodelay_read; /* TRUE if NDELAY is set */
65 #define TTY_IS_NOECHO !(new_modes.c_lflag & ECHO)
66 #define TTY_IS_OUT_TRANS (new_modes.c_oflag & OPOST)
67 #define TTY_IS_CHAR_MODE !(new_modes.c_lflag & ICANON)
68 #define TTY_WAS_CS8 ((old_modes.c_cflag & CSIZE) == CS8)
69 #define TTY_WAS_XON_XOFF (old_modes.c_iflag & (IXON|IXOFF))
71 #define TTY_IS_NOECHO !(new_modes.sg_flags & (ECHO))
72 #define TTY_IS_OUT_TRANS (new_modes.sg_flags & (CRMOD))
73 #define TTY_IS_CHAR_MODE (new_modes.sg_flags & (RAW|CBREAK))
74 #define TTY_WAS_CS8 (old_modes.sg_flags & (PASS8))
75 #define TTY_WAS_XON_XOFF (old_modes.sg_flags & (TANDEM|MDMBUF|DECCTQ))
78 static TTY old_modes, new_modes;
83 * These are a sneaky way of conditionalizing bit unsets so strict-POSIX
84 * systems won't see them.
103 tty_raw(int minch GCC_UNUSED, int mask)
104 { /* set tty to raw noecho */
105 new_modes = old_modes;
108 new_modes.c_cc[VMIN] = 1;
110 new_modes.c_cc[VMIN] = minch;
112 new_modes.c_cc[VTIME] = 2;
114 ~(ISIG | ICANON | XCASE | ECHO | ECHOE | ECHOK | ECHONL);
116 new_modes.c_lflag &= ~LOBLK;
118 new_modes.c_oflag &= ~(OPOST | OLCUC | TABDLY);
119 if (mask == ALLOW_PARITY) {
120 new_modes.c_cflag &= ~(CSIZE | PARENB | HUPCL);
121 new_modes.c_cflag |= CS8;
124 ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL |
125 IUCLC | IXON | IXANY | IXOFF);
127 new_modes.sg_flags |= RAW;
131 PUT_TTY(fileno(stdin), &new_modes);
136 { /* set tty to special modes */
137 new_modes = old_modes;
139 new_modes.c_cc[VMIN] = 1;
140 new_modes.c_cc[VTIME] = 1;
141 new_modes.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
142 #if defined(ONLCR) && defined(OCRNL) && defined(ONLRET) && defined(OFILL)
143 new_modes.c_oflag &= ~(ONLCR | OCRNL | ONLRET | OFILL);
145 new_modes.c_oflag &= ~(OPOST);
147 if (char_mask == ALLOW_PARITY)
148 new_modes.c_iflag &= ~ISTRIP;
149 switch (select_xon_xoff) {
151 new_modes.c_iflag &= ~(IXON | IXOFF);
154 #if defined(sequent) && sequent
155 /* the sequent System V emulation is broken */
156 new_modes = old_modes;
157 new_modes.c_cc[VEOL] = 6; /* control F (ACK) */
159 new_modes.c_iflag |= IXON | IXOFF;
162 switch (select_delay_type) {
166 ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
172 ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
175 new_modes.c_oflag |= NL1 | CR2;
179 if (!(new_modes.c_oflag & ~OPOST))
180 new_modes.c_oflag &= ~OPOST;
182 new_modes.sg_flags |= RAW;
186 PUT_TTY(fileno(stdin), &new_modes);
192 { /* reset the tty to the original modes */
196 PUT_TTY(fileno(stdin), &old_modes);
202 { /* ATT terminal init */
203 #if defined(F_GETFL) && defined(O_NDELAY)
206 flags = fcntl(fileno(stdin), F_GETFL, 0);
207 nodelay_read = flags & O_NDELAY;
209 nodelay_read = FALSE;
212 if (GET_TTY(fileno(stdin), &old_modes) == -1) {
213 if (errno == ENOTTY) {
218 printf("tcgetattr error: %d\n", errno);
221 /* if TAB3 is set then setterm() wipes out tabs (ht) */
222 new_modes = old_modes;
225 new_modes.c_oflag &= ~TABDLY;
228 if (PUT_TTY(fileno(stdin), &new_modes) == -1) {
229 printf("tcsetattr error: %d\n", errno);
233 /* the sequent ATT emulation is broken soooo. */
234 old_modes.c_cflag &= ~(CSIZE | CSTOPB);
235 old_modes.c_cflag |= CS7 | PARENB;
239 switch (old_modes.c_cflag & CSIZE) {
240 #if defined(CS5) && (CS5 != 0)
245 #if defined(CS6) && (CS6 != 0)
250 #if defined(CS7) && (CS7 != 0)
255 #if defined(CS8) && (CS8 != 0)
261 tty_frame_size += 2 +
262 ((old_modes.c_cflag & PARENB) ? 2 : 0) +
263 ((old_modes.c_cflag & CSTOPB) ? 4 : 2);
266 (old_modes.sg_flags & PASS8) ? 16 : 14;
271 ** stty_query(question)
273 ** Does the current driver settings have this property?
280 return TTY_IS_NOECHO;
282 return TTY_IS_OUT_TRANS;
284 return TTY_IS_CHAR_MODE;
290 ** initial_stty_query(question)
292 ** Did the initial driver settings have this property?
295 initial_stty_query(int q)
301 return TTY_WAS_XON_XOFF;
306 #if HAVE_SELECT && defined(FD_ZERO)
315 FD_SET(fileno(stdin), &ifds);
318 n = select(fileno(stdin)+1, &ifds, NULL, NULL, &tv);
329 /* the following loop has to be tuned for each computer */
330 for (j = 0; j < 1000; j++) {
331 ioctl(fileno(stdin), FIONREAD, &i);
339 #if defined(__BEOS__)
344 int howmany = ioctl(0, 'ichr', &n);
345 return (howmany >= 0 && n > 0);
348 #define char_ready() 1
356 ** Wait for the input stream to stop.
357 ** Throw away all input characters.
362 unsigned char buf[64];
365 event_start(TIME_FLUSH); /* start the timer */
368 (void) read(fileno(stdin), &buf, sizeof(buf));
370 } while (event_time(TIME_FLUSH) < 400000);
374 ** read_key(input-buffer, length-of-buffer)
376 ** read one function key from the input stream.
377 ** A null character is converted to 0x80.
380 read_key(char *buf, int max)
388 /* ATT unix may return 0 or 1, Berkeley Unix should be 1 */
389 while (read(fileno(stdin), s, 1) == 0);
392 while (max > 0 && (ask = char_ready())) {
396 if ((got = read(fileno(stdin), s, ask))) {
405 for (s = buf, i = 0; i < l; i++) {
406 if ((*s & 0x7f) == 0) {
407 /* convert nulls to 0x80 */
408 *(unsigned char *)s = 128;
410 /* strip high order bits (if any) */
421 signal(SIGINT, SIG_IGN);
422 signal(SIGHUP, SIG_IGN);
423 signal(SIGQUIT, SIG_IGN);
424 signal(SIGTERM, SIG_IGN);
425 signal(SIGALRM, SIG_IGN);
431 is the interrupt handling routine onintr turns off interrupts while doing
434 onintr always exits fatally
439 onintr(int sig GCC_UNUSED)
450 set up to field interrupts (via function onintr( )) so that if interrupted
451 we can restore the correct terminal modes
453 catchsig simply returns
460 if ((signal(SIGINT, SIG_IGN)) == SIG_DFL)
461 signal(SIGINT, onintr);
463 if ((signal(SIGHUP, SIG_IGN)) == SIG_DFL)
464 signal(SIGHUP, onintr);
466 if ((signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
467 signal(SIGQUIT, onintr);
469 if ((signal(SIGTERM, SIG_IGN)) == SIG_DFL)
470 signal(SIGTERM, onintr);
477 ** Come here for an alarm event
487 ** set_alarm_clock(seconds)
489 ** Set the alarm clock to fire in <seconds>
495 signal(SIGALRM, alarm_event);
497 (void) alarm(seconds);