summaryrefslogtreecommitdiffstats
path: root/warp/sig.c
blob: a6f1c38dbd8cdad2ae7c58d92a0f585a8acd956a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Header: /usr/src/games/warp/RCS/sig.c,v 1.1 87/07/03 01:47:11 games Exp */

/* Log:	sig.c,v
 * Revision 7.0.1.1a  87/07/03  01:47:11  games
 * Changed sigsetmask to use sigmask instead of calculating it (incorrectly)
 * by hand.
 *
 * Revision 7.0.1.1  86/12/12  17:02:44  lwall
 * Baseline for net release.
 *
 * Revision 7.0  86/10/08  15:13:24  lwall
 * Split into separate files.  Added amoebas and pirates.
 *
 */

#include "EXTERN.h"
#include "warp.h"
#include "play.h"
#include "score.h"
#include "term.h"
#include "util.h"
#include "INTERN.h"
#include "sig.h"

void
sig_init(void)
{
    sigignore(SIGINT);  /* for inquiry of existence via kill call */
#ifdef SIGTTOU
    sigignore(SIGTTOU);
#endif

    sigset(SIGHUP, sig_catcher);
    if (!debugging) {
	sigset(SIGQUIT, sig_catcher);
	sigset(SIGILL, sig_catcher);
	sigset(SIGFPE, sig_catcher);
#if 0
	sigset(SIGBUS, sig_catcher);
	sigset(SIGSEGV, sig_catcher);
#endif
	sigset(SIGSYS, sig_catcher);
	sigset(SIGTERM, sig_catcher);
    }
#ifdef SIGXCPU
    sigset(SIGXCPU, sig_catcher);
#endif
#ifdef SIGCONT
    sigset(SIGCONT, cont_catcher);
#endif
#ifdef SIGTSTP
    sigset(SIGTSTP, stop_catcher);
    sigset(SIGSTOP, stop_catcher);
#endif
}

#ifdef SIGTSTP
void
cont_catcher(int x)
{
#ifndef lint
    sigset(SIGCONT,cont_catcher);
#endif
    savetty();
    crmode();
    raw();
    noecho();
    nonl();
}
#endif

void
mytstp(void)
{
    resetty();
#ifdef SIGTSTP
    kill(0,SIGTSTP);
#else
    if (fork())
	wait(0);
    else {
	char *shell = getenv("SHELL");

	setuid(getuid());
	if (!*shell)
	    shell = "/bin/sh";
	execl(shell,shell,0);
	exit(1);
    }
#endif
    rewrite();
}

void					/* very much void */
finalize(int status)
{
    if (bizarre)
	resetty();
    if (status < 0) {
	chdir("/usr/tmp");
	sigset(SIGILL,SIG_DFL);
	abort();
    }
    exit(status);
}

/* come here on signal other than interrupt, stop, or cont */

void
sig_catcher(int signo)
{
#ifdef VERBOSE
    static const char *signame[] = {
	"",
	"HUP",
	"INT",
	"QUIT",
	"ILL",
	"TRAP",
	"IOT",
	"EMT",
	"FPE",
	"KILL",
	"BUS",
	"SEGV",
	"SYS",
	"PIPE",
	"ALRM",
	"TERM",
	"???"
#ifdef SIGTSTP
	,"STOP",
	"TSTP",
	"CONT",
	"CHLD",
	"TTIN",
	"TTOU",
	"TINT",
	"XCPU",
	"XFSZ"
#ifdef SIGPROF
	,"VTALARM",
	"PROF"
#endif
#endif
	};
#endif

#ifdef SIGTTOU
#ifndef lint
    sigignore(SIGTTOU);
#endif /* lint */
#endif
#ifdef DEBUGGING
    if (debug) {
	printf("\r\nSIG%s--game not saved in debug\r\n",signame[signo]);
	finalize(-1);
    }
#endif
    panic++;
    if (panic >= 2) {
	if (panic >= 3)
	    abort();
	chdir(SAVEDIR);
	kill(0,SIGIOT);
    }
    (void) sigset(SIGILL,SIG_DFL);
    if (signo == SIGHUP && (timer < 10 || didkill))
	signo = SIGQUIT;
    if (signo == SIGQUIT) {	/* can't let them bomb out without penalty */
	if (smarts < 20)
	    smarts += 4;
	else if (smarts < 35)
	    smarts += 2;
	else
	    smarts++;
	totalscore -= possiblescore / 2;
    }
    save_game();
    if (signo != SIGHUP && signo != SIGQUIT) {
#ifdef VERBOSE
	IF(verbose)
	    printf("\r\nCaught %s%s--%s\r\n",
		signo ? "a SIG" : "an internal error", signame[signo],
		experimenting ? "game saved" : "bye bye");
	ELSE
#endif
#ifdef TERSE
	    printf("\r\nsignal %d--bye bye\r\n",signo);
#endif
    }
    switch (signo) {
    case SIGBUS:
    case SIGILL:
    case SIGSEGV:
	finalize(-signo);
    }
    finalize(1);				/* and blow up */
}

#ifdef SIGTSTP
/* come here on stop signal */

void
stop_catcher(int sig)
{
    if (!waiting) {
	resetty();			/* this is the point of all this */
#ifdef DEBUGGING
	if (debug)
	    write(2,"stop_catcher\r\n",13);
#endif
	sigset(SIGTSTP,SIG_DFL);	/* enable stop */
#ifdef BSD42
	sigsetmask(sigblock(0L) & ~sigmask(SIGTSTP));
#endif
	kill(0,SIGTSTP);		/* and do the stop */
    }
#ifndef lint
    sigset(SIGTSTP,stop_catcher);	/* unenable the stop */
#endif
}
#endif