Home | History | Annotate | Line # | Download | only in larn
signal.c revision 1.5
      1 #ifndef lint
      2 static char rcsid[] = "$NetBSD: signal.c,v 1.5 1995/12/21 11:27:51 mycroft 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 sigsetup()
     59 	{
     60 	signal(SIGQUIT, cntlc); 		signal(SIGINT,  cntlc);
     61 	signal(SIGKILL, SIG_IGN);		signal(SIGHUP,  sgam);
     62 	signal(SIGILL,  sigpanic);		signal(SIGTRAP, sigpanic);
     63 	signal(SIGIOT,  sigpanic);		signal(SIGEMT,  sigpanic);
     64 	signal(SIGFPE,  sigpanic);		signal(SIGBUS,  sigpanic);
     65 	signal(SIGSEGV, sigpanic);		signal(SIGSYS,  sigpanic);
     66 	signal(SIGPIPE, sigpanic);		signal(SIGTERM, sigpanic);
     67 #ifdef SIGTSTP
     68 	signal(SIGTSTP,tstop);		signal(SIGSTOP,tstop);
     69 #endif SIGTSTP
     70 	}
     71 
     72 /*
     73  *	routine to process a fatal error signal
     74  */
     75 static void
     76 sigpanic(sig)
     77 	int sig;
     78 	{
     79 	char buf[128];
     80 	signal(sig,SIG_DFL);
     81 	sprintf(buf,"\nLarn - Panic! Signal %d received [SIG%s]",sig,sys_signame[sig]);
     82 	write(2,buf,strlen(buf));  sleep(2);
     83 	sncbr();
     84 	savegame(savefilename);
     85 	kill(getpid(),sig); /* this will terminate us */
     86 	}
     87