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