Home | History | Annotate | Line # | Download | only in larn
signal.c revision 1.4
      1 #ifndef lint
      2 static char rcsid[] = "$NetBSD: signal.c,v 1.4 1995/04/24 12:24:12 cgd Exp $";
      3 #endif /* not lint */
      4 
      5 #include <signal.h>
      6 #include "header.h"			/* "Larn is copyrighted 1986 by Noah Morgan.\n" */
      7 #include <string.h>
      8 
      9 #define BIT(a) (1<<((a)-1))
     10 extern char savefilename[],wizard,predostuff,nosignal;
     11 static s2choose()	/* text to be displayed if ^C during intro screen */
     12 	{
     13 	cursor(1,24); lprcat("Press "); setbold(); lprcat("return"); resetbold();
     14 	lprcat(" to continue: ");   lflush();
     15 	}
     16 
     17 static void
     18 cntlc()	/* what to do for a ^C */
     19 	{
     20 	if (nosignal) return;	/* don't do anything if inhibited */
     21 	signal(SIGQUIT,SIG_IGN);	signal(SIGINT,SIG_IGN);
     22 	quit(); if (predostuff==1) s2choose(); else showplayer();
     23 	lflush();
     24 	signal(SIGQUIT,cntlc);	signal(SIGINT,cntlc);
     25 	}
     26 
     27 /*
     28  *	subroutine to save the game if a hangup signal
     29  */
     30 static void
     31 sgam()
     32 	{
     33 	savegame(savefilename);  wizard=1;  died(-257); /* hangup signal */
     34 	}
     35 
     36 #ifdef SIGTSTP
     37 static void
     38 tstop() /* control Y	*/
     39 	{
     40 	if (nosignal)   return;  /* nothing if inhibited */
     41 	lcreat((char*)0);  clearvt100();	lflush();	  signal(SIGTSTP,SIG_DFL);
     42 #ifdef SIGVTALRM
     43 	/* looks like BSD4.2 or higher - must clr mask for signal to take effect*/
     44 	sigsetmask(sigblock(0)& ~BIT(SIGTSTP));
     45 #endif
     46 	kill(getpid(),SIGTSTP);
     47 
     48 	setupvt100();  signal(SIGTSTP,tstop);
     49 	if (predostuff==1) s2choose(); else drawscreen();
     50 	showplayer();	lflush();
     51 	}
     52 #endif SIGTSTP
     53 
     54 /*
     55  *	subroutine to issue the needed signal traps  called from main()
     56  */
     57 static void sigpanic();
     58 static void sigill()	{ sigpanic(SIGILL); }
     59 static void sigtrap()	{ sigpanic(SIGTRAP); }
     60 static void sigiot()	{ sigpanic(SIGIOT); }
     61 static void sigemt()	{ sigpanic(SIGEMT); }
     62 static void sigfpe()	{ sigpanic(SIGFPE); }
     63 static void sigbus()	{ sigpanic(SIGBUS); }
     64 static void sigsegv()	{ sigpanic(SIGSEGV); }
     65 static void sigsys()	{ sigpanic(SIGSYS); }
     66 static void sigpipe()	{ sigpanic(SIGPIPE); }
     67 static void sigterm()	{ sigpanic(SIGTERM); }
     68 sigsetup()
     69 	{
     70 	signal(SIGQUIT, cntlc); 		signal(SIGINT,  cntlc);
     71 	signal(SIGKILL, SIG_IGN);		signal(SIGHUP,  sgam);
     72 	signal(SIGILL,  sigill);		signal(SIGTRAP, sigtrap);
     73 	signal(SIGIOT,  sigiot);		signal(SIGEMT,  sigemt);
     74 	signal(SIGFPE,  sigfpe);		signal(SIGBUS,  sigbus);
     75 	signal(SIGSEGV, sigsegv);		signal(SIGSYS,  sigsys);
     76 	signal(SIGPIPE, sigpipe);		signal(SIGTERM, sigterm);
     77 #ifdef SIGTSTP
     78 	signal(SIGTSTP,tstop);		signal(SIGSTOP,tstop);
     79 #endif SIGTSTP
     80 	}
     81 
     82 #ifdef BSD	/* for BSD UNIX? */
     83 
     84 static char *signame[NSIG] = { "",
     85 "SIGHUP",  /*	1	 hangup */
     86 "SIGINT",  /*	2	 interrupt */
     87 "SIGQUIT", /*	3	 quit */
     88 "SIGILL",  /*	4	 illegal instruction (not reset when caught) */
     89 "SIGTRAP", /*	5	 trace trap (not reset when caught) */
     90 "SIGIOT",  /*	6	 IOT instruction */
     91 "SIGEMT",  /*	7	 EMT instruction */
     92 "SIGFPE",  /*	8	 floating point exception */
     93 "SIGKILL", /*	9	 kill (cannot be caught or ignored) */
     94 "SIGBUS",  /*	10	 bus error */
     95 "SIGSEGV", /*	11	 segmentation violation */
     96 "SIGSYS",  /*	12	 bad argument to system call */
     97 "SIGPIPE", /*	13	 write on a pipe with no one to read it */
     98 "SIGALRM", /*	14	 alarm clock */
     99 "SIGTERM", /*	15	 software termination signal from kill */
    100 "SIGURG",  /*	16	 urgent condition on IO channel */
    101 "SIGSTOP", /*	17	 sendable stop signal not from tty */
    102 "SIGTSTP", /*	18	 stop signal from tty */
    103 "SIGCONT", /*	19	 continue a stopped process */
    104 "SIGCHLD", /*	20	 to parent on child stop or exit */
    105 "SIGTTIN", /*	21	 to readers pgrp upon background tty read */
    106 "SIGTTOU", /*	22	 like TTIN for output if (tp->t_local&LTOSTOP) */
    107 "SIGIO",   /*	23	 input/output possible signal */
    108 "SIGXCPU", /*	24	 exceeded CPU time limit */
    109 "SIGXFSZ", /*	25	 exceeded file size limit */
    110 "SIGVTALRM",/*  26	 virtual time alarm */
    111 "SIGPROF", /*	27	 profiling time alarm */
    112 "","","","" };
    113 
    114 #else BSD	/* for system V? */
    115 
    116 static char *signame[NSIG] = { "",
    117 "SIGHUP",  /*	1	 hangup */
    118 "SIGINT",  /*	2	 interrupt */
    119 "SIGQUIT", /*	3	 quit */
    120 "SIGILL",  /*	4	 illegal instruction (not reset when caught) */
    121 "SIGTRAP", /*	5	 trace trap (not reset when caught) */
    122 "SIGIOT",  /*	6	 IOT instruction */
    123 "SIGEMT",  /*	7	 EMT instruction */
    124 "SIGFPE",  /*	8	 floating point exception */
    125 "SIGKILL", /*	9	 kill (cannot be caught or ignored) */
    126 "SIGBUS",  /*	10	 bus error */
    127 "SIGSEGV", /*	11	 segmentation violation */
    128 "SIGSYS",  /*	12	 bad argument to system call */
    129 "SIGPIPE", /*	13	 write on a pipe with no one to read it */
    130 "SIGALRM", /*	14	 alarm clock */
    131 "SIGTERM", /*	15	 software termination signal from kill */
    132 "SIGUSR1",  /*	16	 user defines signal 1 */
    133 "SIGUSR2", /*	17	 user defines signal 2 */
    134 "SIGCLD",  /*	18	 child death */
    135 "SIGPWR",  /*	19	 power fail */
    136 "","","","","","","","","","","","" };
    137 
    138 #endif BSD
    139 
    140 /*
    141  *	routine to process a fatal error signal
    142  */
    143 static void
    144 sigpanic(sig)
    145 	int sig;
    146 	{
    147 	char buf[128];
    148 	signal(sig,SIG_DFL);
    149 	sprintf(buf,"\nLarn - Panic! Signal %d received [%s]",sig,signame[sig]);
    150 	write(2,buf,strlen(buf));  sleep(2);
    151 	sncbr();
    152 	savegame(savefilename);
    153 	kill(getpid(),sig); /* this will terminate us */
    154 	}
    155