signal.c revision 1.5 1 1.2 mycroft #ifndef lint
2 1.5 mycroft static char rcsid[] = "$NetBSD: signal.c,v 1.5 1995/12/21 11:27:51 mycroft Exp $";
3 1.2 mycroft #endif /* not lint */
4 1.2 mycroft
5 1.1 cgd #include <signal.h>
6 1.1 cgd #include "header.h" /* "Larn is copyrighted 1986 by Noah Morgan.\n" */
7 1.4 cgd #include <string.h>
8 1.4 cgd
9 1.1 cgd #define BIT(a) (1<<((a)-1))
10 1.1 cgd extern char savefilename[],wizard,predostuff,nosignal;
11 1.1 cgd static s2choose() /* text to be displayed if ^C during intro screen */
12 1.1 cgd {
13 1.1 cgd cursor(1,24); lprcat("Press "); setbold(); lprcat("return"); resetbold();
14 1.1 cgd lprcat(" to continue: "); lflush();
15 1.1 cgd }
16 1.1 cgd
17 1.1 cgd static void
18 1.1 cgd cntlc() /* what to do for a ^C */
19 1.1 cgd {
20 1.1 cgd if (nosignal) return; /* don't do anything if inhibited */
21 1.1 cgd signal(SIGQUIT,SIG_IGN); signal(SIGINT,SIG_IGN);
22 1.1 cgd quit(); if (predostuff==1) s2choose(); else showplayer();
23 1.1 cgd lflush();
24 1.1 cgd signal(SIGQUIT,cntlc); signal(SIGINT,cntlc);
25 1.1 cgd }
26 1.1 cgd
27 1.1 cgd /*
28 1.1 cgd * subroutine to save the game if a hangup signal
29 1.1 cgd */
30 1.1 cgd static void
31 1.1 cgd sgam()
32 1.1 cgd {
33 1.1 cgd savegame(savefilename); wizard=1; died(-257); /* hangup signal */
34 1.1 cgd }
35 1.1 cgd
36 1.1 cgd #ifdef SIGTSTP
37 1.1 cgd static void
38 1.1 cgd tstop() /* control Y */
39 1.1 cgd {
40 1.1 cgd if (nosignal) return; /* nothing if inhibited */
41 1.1 cgd lcreat((char*)0); clearvt100(); lflush(); signal(SIGTSTP,SIG_DFL);
42 1.1 cgd #ifdef SIGVTALRM
43 1.1 cgd /* looks like BSD4.2 or higher - must clr mask for signal to take effect*/
44 1.1 cgd sigsetmask(sigblock(0)& ~BIT(SIGTSTP));
45 1.1 cgd #endif
46 1.1 cgd kill(getpid(),SIGTSTP);
47 1.1 cgd
48 1.1 cgd setupvt100(); signal(SIGTSTP,tstop);
49 1.1 cgd if (predostuff==1) s2choose(); else drawscreen();
50 1.1 cgd showplayer(); lflush();
51 1.1 cgd }
52 1.1 cgd #endif SIGTSTP
53 1.1 cgd
54 1.1 cgd /*
55 1.1 cgd * subroutine to issue the needed signal traps called from main()
56 1.1 cgd */
57 1.1 cgd static void sigpanic();
58 1.1 cgd sigsetup()
59 1.1 cgd {
60 1.1 cgd signal(SIGQUIT, cntlc); signal(SIGINT, cntlc);
61 1.1 cgd signal(SIGKILL, SIG_IGN); signal(SIGHUP, sgam);
62 1.5 mycroft signal(SIGILL, sigpanic); signal(SIGTRAP, sigpanic);
63 1.5 mycroft signal(SIGIOT, sigpanic); signal(SIGEMT, sigpanic);
64 1.5 mycroft signal(SIGFPE, sigpanic); signal(SIGBUS, sigpanic);
65 1.5 mycroft signal(SIGSEGV, sigpanic); signal(SIGSYS, sigpanic);
66 1.5 mycroft signal(SIGPIPE, sigpanic); signal(SIGTERM, sigpanic);
67 1.1 cgd #ifdef SIGTSTP
68 1.1 cgd signal(SIGTSTP,tstop); signal(SIGSTOP,tstop);
69 1.1 cgd #endif SIGTSTP
70 1.1 cgd }
71 1.1 cgd
72 1.1 cgd /*
73 1.1 cgd * routine to process a fatal error signal
74 1.1 cgd */
75 1.1 cgd static void
76 1.1 cgd sigpanic(sig)
77 1.1 cgd int sig;
78 1.1 cgd {
79 1.1 cgd char buf[128];
80 1.1 cgd signal(sig,SIG_DFL);
81 1.5 mycroft sprintf(buf,"\nLarn - Panic! Signal %d received [SIG%s]",sig,sys_signame[sig]);
82 1.1 cgd write(2,buf,strlen(buf)); sleep(2);
83 1.1 cgd sncbr();
84 1.1 cgd savegame(savefilename);
85 1.1 cgd kill(getpid(),sig); /* this will terminate us */
86 1.1 cgd }
87