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