]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - warp/sig.c
a4cb1735e4d675ec8e766a2a640b0e967cf329fc
[bsdgames-darwin.git] / warp / sig.c
1 /* $Header: /cvsroot/src/games/warp/sig.c,v 1.1 2020/11/09 23:37:05 kamil Exp $ */
2
3 /* $Log: sig.c,v $
4 /* Revision 1.1 2020/11/09 23:37:05 kamil
5 /* Add Warp Kit, Version 7.0 by Larry Wall
6 /*
7 /* Warp is a real-time space war game that doesn't get boring very quickly.
8 /* Read warp.doc and the manual page for more information.
9 /*
10 /* games/warp originally distributed with 4.3BSD-Reno, is back to the BSD
11 /* world via NetBSD. Its remnants were still mentioned in games/Makefile.
12 /*
13 /* Larry Wall, the original author and the copyright holder, generously
14 /* donated the game and copyright to The NetBSD Foundation, Inc.
15 /*
16 /* Import the game sources as-is from 4.3BSD-Reno, with the cession
17 /* of the copyright and license to BSD-2-clause NetBSD-style.
18 /*
19 /* Signed-off-by: Larry Wall <larry@wall.org>
20 /* Signed-off-by: Kamil Rytarowski <kamil@netbsd.org>
21 /*
22 * Revision 7.0.1.1a 87/07/03 01:47:11 games
23 * Changed sigsetmask to use sigmask instead of calculating it (incorrectly)
24 * by hand.
25 *
26 * Revision 7.0.1.1 86/12/12 17:02:44 lwall
27 * Baseline for net release.
28 *
29 * Revision 7.0 86/10/08 15:13:24 lwall
30 * Split into separate files. Added amoebas and pirates.
31 *
32 */
33
34 #include "EXTERN.h"
35 #include "warp.h"
36 #include "play.h"
37 #include "score.h"
38 #include "term.h"
39 #include "util.h"
40 #include "INTERN.h"
41 #include "sig.h"
42
43 void
44 sig_init()
45 {
46 #ifdef lint
47 ;
48 #else
49 sigignore(SIGINT); /* for inquiry of existence via kill call */
50 #ifdef SIGTTOU
51 sigignore(SIGTTOU);
52 #endif
53
54 sigset(SIGHUP, sig_catcher);
55 if (!debugging) {
56 sigset(SIGQUIT, sig_catcher);
57 sigset(SIGILL, sig_catcher);
58 sigset(SIGFPE, sig_catcher);
59 sigset(SIGBUS, sig_catcher);
60 sigset(SIGSEGV, sig_catcher);
61 sigset(SIGSYS, sig_catcher);
62 sigset(SIGTERM, sig_catcher);
63 }
64 #ifdef SIGXCPU
65 sigset(SIGXCPU, sig_catcher);
66 #endif
67 #ifdef SIGCONT
68 sigset(SIGCONT, cont_catcher);
69 #endif
70 #ifdef SIGTSTP
71 sigset(SIGTSTP, stop_catcher);
72 sigset(SIGSTOP, stop_catcher);
73 #endif
74 #endif /* lint */
75 }
76
77 #ifdef SIGTSTP
78 void
79 cont_catcher()
80 {
81 #ifndef lint
82 sigset(SIGCONT,cont_catcher);
83 #endif
84 savetty();
85 crmode();
86 raw();
87 noecho();
88 nonl();
89 }
90 #endif
91
92 void
93 mytstp()
94 {
95 resetty();
96 #ifdef SIGTSTP
97 kill(0,SIGTSTP);
98 #else
99 if (fork())
100 wait(0);
101 else {
102 char *shell = getenv("SHELL");
103
104 setuid(getuid());
105 if (!*shell)
106 shell = "/bin/sh";
107 execl(shell,shell,0);
108 exit(1);
109 }
110 #endif
111 rewrite();
112 }
113
114 void /* very much void */
115 finalize(status)
116 int status;
117 {
118 if (bizarre)
119 resetty();
120 if (status < 0) {
121 chdir("/usr/tmp");
122 sigset(SIGILL,SIG_DFL);
123 abort();
124 }
125 exit(status);
126 }
127
128 /* come here on signal other than interrupt, stop, or cont */
129
130 void
131 sig_catcher(signo)
132 {
133 #ifdef VERBOSE
134 static char *signame[] = {
135 "",
136 "HUP",
137 "INT",
138 "QUIT",
139 "ILL",
140 "TRAP",
141 "IOT",
142 "EMT",
143 "FPE",
144 "KILL",
145 "BUS",
146 "SEGV",
147 "SYS",
148 "PIPE",
149 "ALRM",
150 "TERM",
151 "???"
152 #ifdef SIGTSTP
153 ,"STOP",
154 "TSTP",
155 "CONT",
156 "CHLD",
157 "TTIN",
158 "TTOU",
159 "TINT",
160 "XCPU",
161 "XFSZ"
162 #ifdef SIGPROF
163 ,"VTALARM",
164 "PROF"
165 #endif
166 #endif
167 };
168 #endif
169
170 #ifdef SIGTTOU
171 #ifndef lint
172 sigignore(SIGTTOU);
173 #endif /* lint */
174 #endif
175 #ifdef DEBUGGING
176 if (debug) {
177 printf("\r\nSIG%s--game not saved in debug\r\n",signame[signo]);
178 finalize(-1);
179 }
180 #endif
181 panic++;
182 if (panic >= 2) {
183 if (panic >= 3)
184 abort();
185 chdir(SAVEDIR);
186 kill(0,SIGIOT);
187 }
188 (void) sigset(SIGILL,SIG_DFL);
189 if (signo == SIGHUP && (timer < 10 || didkill))
190 signo = SIGQUIT;
191 if (signo == SIGQUIT) { /* can't let them bomb out without penalty */
192 if (smarts < 20)
193 smarts += 4;
194 else if (smarts < 35)
195 smarts += 2;
196 else
197 smarts++;
198 totalscore -= possiblescore / 2;
199 }
200 save_game();
201 if (signo != SIGHUP && signo != SIGQUIT)
202 #ifdef VERBOSE
203 IF(verbose)
204 printf("\r\nCaught %s%s--%s\r\n",
205 signo ? "a SIG" : "an internal error", signame[signo],
206 experimenting ? "game saved" : "bye bye");
207 ELSE
208 #endif
209 #ifdef TERSE
210 printf("\r\nSignal %d--bye bye\r\n",signo);
211 #endif
212 switch (signo) {
213 case SIGBUS:
214 case SIGILL:
215 case SIGSEGV:
216 finalize(-signo);
217 }
218 finalize(1); /* and blow up */
219 }
220
221 #ifdef SIGTSTP
222 /* come here on stop signal */
223
224 void
225 stop_catcher()
226 {
227 if (!waiting) {
228 resetty(); /* this is the point of all this */
229 #ifdef DEBUGGING
230 if (debug)
231 write(2,"stop_catcher\r\n",13);
232 #endif
233 sigset(SIGTSTP,SIG_DFL); /* enable stop */
234 #ifdef BSD42
235 sigsetmask(sigblock(0L) & ~sigmask(SIGTSTP));
236 #endif
237 kill(0,SIGTSTP); /* and do the stop */
238 }
239 #ifndef lint
240 sigset(SIGTSTP,stop_catcher); /* unenable the stop */
241 #endif
242 }
243 #endif