--- gforth/Attic/io.c 1994/02/11 16:30:46 1.1 +++ gforth/Attic/io.c 1995/10/16 18:33:10 1.10 @@ -3,12 +3,28 @@ The following is stolen from the readline library for bash */ +/* + Use -D_POSIX_VERSION for POSIX systems. +*/ + +#include + +#ifdef apollo +#define _POSIX_VERSION +#endif + #include #include +#include #include +#ifndef apollo #include +#endif #include #include +#include +#include "forth.h" +#include "io.h" #if defined (__GNUC__) # define alloca __builtin_alloca @@ -18,13 +34,15 @@ # endif #endif -#if defined (HAVE_UNISTD_H) -# include -#endif - #define NEW_TTY_DRIVER #define HAVE_BSD_SIGNALS +/* +#ifndef apollo #define USE_XON_XOFF +#endif +*/ + +#define HANDLE_SIGNALS /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */ #if defined (USG) && !defined (hpux) @@ -33,7 +51,7 @@ /* System V machines use termio. */ #if !defined (_POSIX_VERSION) -# if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX) || defined (ultrix) +# if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX) || defined (ultrix) || defined (Solaris) # undef NEW_TTY_DRIVER # define TERMIO_TTY_DRIVER # include @@ -89,7 +107,7 @@ extern int errno; # endif /* USGr3 */ #endif /* USG && hpux */ -#if defined (_POSIX_VERSION) || defined (USGr3) +#if (defined (_POSIX_VERSION) || defined (USGr3)) && !defined(apollo) # include # define direct dirent # if defined (_POSIX_VERSION) @@ -553,6 +571,8 @@ long key_avail (stream) long chars_avail = pending; int result; + if(!terminal_prepped) prep_terminal(); + #if defined (FIONREAD) result = ioctl (tty, FIONREAD, &chars_avail); #endif @@ -588,6 +608,8 @@ unsigned char getkey(stream) int result; unsigned char c; + if(!terminal_prepped) prep_terminal(); + while (pending < 0) { result = read (fileno (stream), &c, sizeof (char)); @@ -598,7 +620,7 @@ unsigned char getkey(stream) /* If zero characters are returned, then the file that we are reading from is empty! Return EOF in that case. */ if (result == 0) - return (0); + return CTRL('D'); /* If the error that we received was SIGINT, then try again, this is simply an interrupted system call to read (). @@ -653,74 +675,218 @@ int main() } #endif -/* signal handling taken from pfe by Dirk Zoller (Copylefted) - anton */ -/* !! needs cleanup */ -char * -sigmsg (int sig) -{ - static char buf [25]; - static char *msg [] = - { - "Hangup", /* These strings are cited from */ - "Interrupt", /* Rochkind: Advanced UNIX programming */ - "Quit", - "Illegal Instruction", - "Trace Trap", - "IOT instruction", - "EMT instruction", - "Floating point exception", - "Kill", - "Bus error", - "Segmentation Violation", - "Bad arg to system call", - "Broken pipe", - "Alarm clock", - "Terminate signal", - "User signal 1", - "User signal 2", - }; - - if ((unsigned)sig <= 17) - return msg [sig - 1]; - sprintf (buf, "signal %d received", sig); - return buf; -} + +/* signal handling adapted from pfe by Dirk Zoller (Copylefted) - anton */ static void graceful_exit (int sig) { deprep_terminal(); - if ((unsigned)sig <= 17) - fprintf (stderr, "\n\n%s.\n", sigmsg (sig)); - else - fprintf (stderr, "\n\nSignal %d received, terminated.\n", sig); - exit (sig); + fprintf (stderr, "\n\n%s.\n", strsignal (sig)); + exit (0x80|sig); +} + +jmp_buf throw_jmp_buf; + +static void +signal_throw(int sig) +{ + int code; + struct { + int signal; + int throwcode; + } *p, throwtable[] = { + { SIGINT, -28 }, + { SIGFPE, -55 }, + { SIGBUS, -23 }, + { SIGSEGV, -9 }, + }; + signal(sig,signal_throw); + for (code=-256-sig, p=throwtable; psignal) { + code = p->throwcode; + break; + } + longjmp(throw_jmp_buf,code); /* or use siglongjmp ? */ +} + +static void +termprep (int sig) +{ + terminal_prepped=0; + signal(sig,termprep); } void install_signal_handlers (void) { -/* !! These definitions seem to be system dependent - We could have them in the machine.h file, - but I would like something more automatic - anton */ -#define SIGS_TO_IGNORE SIGCHLD -#define SIGS_TO_ABORT SIGINT, SIGILL, SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, \ - SIGALRM, SIGEMT, SIGBUS, SIGSYS -#define SIGS_TO_QUIT SIGHUP, SIGQUIT, SIGABRT, SIGPIPE, \ - SIGTERM - - static short sigs_to_ignore [] = { SIGS_TO_IGNORE }; - static short sigs_to_abort [] = { SIGS_TO_ABORT }; - static short sigs_to_quit [] = { SIGS_TO_QUIT }; + +#if 0 +/* these signals are handled right by default, no need to handle them; + they are listed here just for fun */ + static short sigs_to_default [] = { +#ifdef SIGCHLD + SIGCHLD, +#endif +#ifdef SIGINFO + SIGINFO, +#endif +#ifdef SIGIO + SIGIO, +#endif +#ifdef SIGLOST + SIGLOST, +#endif +#ifdef SIGKILL + SIGKILL, +#endif +#ifdef SIGSTOP + SIGSTOP, +#endif +#ifdef SIGPWR + SIGPWR, +#endif +#ifdef SIGMSG + SIGMSG, +#endif +#ifdef SIGDANGER + SIGDANGER, +#endif +#ifdef SIGMIGRATE + SIGMIGRATE, +#endif +#ifdef SIGPRE + SIGPRE, +#endif +#ifdef SIGVIRT + SIGVIRT, +#endif +#ifdef SIGGRANT + SIGGRANT, +#endif +#ifdef SIGRETRACT + SIGRETRACT, +#endif +#ifdef SIGSOUND + SIGSOUND, +#endif +#ifdef SIGSAK + SIGSAK, +#endif +#ifdef SIGTSTP + SIGTSTP, +#endif +#ifdef SIGTTIN + SIGTTIN, +#endif +#ifdef SIGTTOU + SIGTTOU, +#endif +#ifdef SIGWINCH + SIGWINCH, +#endif +#ifdef SIGSTKFLT + SIGSTKFLT, +#endif +#ifdef SIGUNUSED + SIGUNUSED, +#endif + }; +#endif + + static short sigs_to_throw [] = { +#ifdef SIGBREAK + SIGBREAK, +#endif +#ifdef SIGINT + SIGINT, +#endif +#ifdef SIGILL + SIGILL, +#endif +#ifdef SIGEMT + SIGEMT, +#endif +#ifdef SIGFPE + SIGFPE, +#endif +#ifdef SIGIOT + SIGIOT, +#endif +#ifdef SIGSEGV + SIGSEGV, +#endif +#ifdef SIGALRM + SIGALRM, +#endif +#ifdef SIGPOLL + SIGPOLL, +#endif +#ifdef SIGPROF + SIGPROF, +#endif +#ifdef SIGBUS + SIGBUS, +#endif +#ifdef SIGSYS + SIGSYS, +#endif +#ifdef SIGTRAP + SIGTRAP, +#endif +#ifdef SIGURG + SIGURG, +#endif +#ifdef SIGUSR1 + SIGUSR1, +#endif +#ifdef SIGUSR2 + SIGUSR2, +#endif +#ifdef SIGVTALRM + SIGVTALRM, +#endif +#ifdef SIGXFSZ + SIGXFSZ, +#endif + }; + static short sigs_to_quit [] = { +#ifdef SIGHUP + SIGHUP, +#endif +#ifdef SIGQUIT + SIGQUIT, +#endif +#ifdef SIGABRT + SIGABRT, +#endif +#ifdef SIGPIPE + SIGPIPE, +#endif +#ifdef SIGTERM + SIGTERM, +#endif +#ifdef SIGXCPU + SIGXCPU, +#endif + }; + static short sigs_to_termprep [] = { +#ifdef SIGCONT + SIGCONT, +#endif + }; int i; #define DIM(X) (sizeof (X) / sizeof *(X)) +/* for (i = 0; i < DIM (sigs_to_ignore); i++) - if (sigs_to_ignore [i]) - signal (sigs_to_ignore [i], SIG_IGN); - for (i = 0; i < DIM (sigs_to_abort); i++) - signal (sigs_to_abort [i], graceful_exit); /* !! change to throw */ + signal (sigs_to_ignore [i], SIG_IGN); +*/ + for (i = 0; i < DIM (sigs_to_throw); i++) + signal (sigs_to_throw [i], signal_throw); for (i = 0; i < DIM (sigs_to_quit); i++) signal (sigs_to_quit [i], graceful_exit); + for (i = 0; i < DIM (sigs_to_termprep); i++) + signal (sigs_to_termprep [i], termprep); } /* end signal handling */