File: final.c

package info (click to toggle)
trn4 4.0-test76-3
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 2,988 kB
  • ctags: 2,965
  • sloc: ansic: 48,337; sh: 6,770; tcl: 1,696; yacc: 660; perl: 108; makefile: 72
file content (327 lines) | stat: -rw-r--r-- 6,118 bytes parent folder | download | duplicates (5)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* final.c
 */
/* This software is copyrighted as detailed in the LICENSE file. */


#include "EXTERN.h"
#include "common.h"
#include "list.h"
#include "env.h"
#include "util.h"
#include "hash.h"
#include "cache.h"
#include "bits.h"
#include "term.h"
#include "color.h"
#include "ng.h"
#include "init.h"
#include "last.h"
#include "ngdata.h"
#include "nntpclient.h"
#include "nntpinit.h"
#include "datasrc.h"
#include "nntp.h"
#include "rcstuff.h"
#include "artio.h"
#include "intrp.h"
#include "kfile.h"
#ifdef SCORE
#include "score.h"
#include "scoresave.h"
#endif
#ifdef USE_TCL
#include "tkstuff.h"
#endif
#ifdef USE_FILTER
#include "filter.h"
#endif
#include "INTERN.h"
#include "final.h"

#ifndef sigmask
#define sigmask(m)	(1 << ((m)-1))
#endif

void
final_init()
{
#ifdef SIGTSTP
    sigset(SIGTSTP, stop_catcher);	/* job control signals */
    sigset(SIGTTOU, stop_catcher);	/* job control signals */
    sigset(SIGTTIN, stop_catcher);	/* job control signals */
#endif

    sigset(SIGINT, int_catcher);	/* always catch interrupts */
#ifdef SIGHUP
    sigset(SIGHUP, sig_catcher);	/* and hangups */
#endif
#ifdef SIGWINCH
    sigset(SIGWINCH, winch_catcher);
#endif
#ifdef SUPPORT_NNTP
    sigset(SIGPIPE,pipe_catcher);
#endif

#ifndef lint
#ifdef SIGEMT
    sigignore(SIGEMT);		/* Ignore EMT signals from old [t]rn's */
#endif
#endif

#ifdef DEBUG
    /* sometimes we WANT a core dump */
    if (debug & DEB_COREDUMPSOK)
	return;
#endif
    sigset(SIGILL, sig_catcher);
#ifdef SIGTRAP
    sigset(SIGTRAP, sig_catcher);
#endif
    sigset(SIGFPE, sig_catcher);
#ifdef SIGBUS
    sigset(SIGBUS, sig_catcher);
#endif
    sigset(SIGSEGV, sig_catcher);
#ifdef SIGSYS
    sigset(SIGSYS, sig_catcher);
#endif
    sigset(SIGTERM, sig_catcher);
#ifdef SIGXCPU
    sigset(SIGXCPU, sig_catcher);
#endif
#ifdef SIGXFSZ
    sigset(SIGXFSZ, sig_catcher);
#endif
}

void					/* very much void */
finalize(status)
int status;
{
#ifdef SUPPORT_NNTP
    int i;
    char* s;
#endif

#ifdef SCORE
    sc_sv_savefile();	/* save any scores from memory to disk */
#endif
#ifdef USE_FILTER
    filter_cleanup();
#endif
#ifdef KILLFILES
    update_thread_kfile();
#endif
    color_default();
    termlib_reset();
    if (bizarre)
	resetty();
    xmouse_off();	/* turn off mouse tracking (if on) */
    fflush(stdout);

    chdir(tmpdir);
    unuse_multirc(multirc);
    if (datasrc_list) {
	DATASRC* dp;
	for (dp = datasrc_first(); dp && dp->name; dp = datasrc_next(dp))
	    close_datasrc(dp);
    }
#ifdef SUPPORT_NNTP
    for (i = 0; i < MAX_NNTP_ARTICLES; i++) {
	s = nntp_tmpname(i);
	UNLINK(s);
    }
    cleanup_nntp();
#endif
    if (headname)
	UNLINK(headname);
#ifdef USE_TCL
    if (ttcl_running)
	ttcl_finalize(status);
#endif
    if (status < 0) {
	sigset(SIGILL,SIG_DFL);
#ifdef HAS_SIGBLOCK
	sigsetmask(sigblock(0) & ~(sigmask(SIGILL) | sigmask(SIGIOT)));
#endif
	abort();
    }
#ifdef RESTORE_ORIGDIR
    if (origdir)
	chdir(origdir);
#endif
    exit(status);
}

/* come here on interrupt */

Signal_t
int_catcher(dummy)
int dummy;
{
    sigset(SIGINT,int_catcher);
#ifdef DEBUG
    if (debug)
	write(2,"int_catcher\n",12);
#endif
    if (!waiting) {
	if (int_count++) {		/* was there already an interrupt? */
	    if (int_count == 3 || int_count > 5) {
		write(2,"\nBye-bye.\n",10);
		sig_catcher(0);		/* emulate the other signals */
	    }
	    write(2,"\n(Interrupt -- one more to kill trn)\n",37);
	}
    }
}

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

Signal_t
sig_catcher(signo)
int signo;
{
#ifdef VERBOSE
    static 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 DEBUG
    if (debug) {
	printf("\nSIG%s--.newsrc not restored in debug\n",signame[signo]);
	finalize(-1);
    }
#endif
    if (panic) {
#ifdef HAS_SIGBLOCK
	sigsetmask(sigblock(0) & ~(sigmask(SIGILL) | sigmask(SIGIOT)));
#endif
	abort();
    }
    (void) sigset(SIGILL,SIG_DFL);
    panic = TRUE;			/* disable terminal I/O */
    if (doing_ng) {			/* need we reconstitute rc line? */
	yankback();
	bits_to_rc();			/* then do so (hope this works) */
    }
    doing_ng = FALSE;
    if (!write_newsrcs(multirc)) {	/* write anything that's changed */
	/*$$ get_old_newsrcs(multirc);  ?? */
    }
#ifdef KILLFILES
    update_thread_kfile();
#endif

#ifdef SIGHUP
    if (signo != SIGHUP) {
#endif
#ifdef VERBOSE
	IF(verbose)
	    printf("\nCaught %s%s--.newsrc restored\n",
		signo ? "a SIG" : "an internal error", signame[signo]);
	ELSE
#endif
#ifdef TERSE
	    printf("\nSignal %d--bye bye\n",signo);
#endif
#ifdef SIGHUP
    }
#endif
    switch (signo) {
#ifdef SIGBUS
    case SIGBUS:
#endif
    case SIGILL:
    case SIGSEGV:
	finalize(-signo);
    }
    finalize(1);				/* and blow up */
}

#ifdef SUPPORT_NNTP
Signal_t
pipe_catcher(signo)
int signo;
{
    ;/*$$ we lost the current nntp connection */
    sigset(SIGPIPE,pipe_catcher);
}
#endif

/* come here on stop signal */

#ifdef SIGTSTP
Signal_t
stop_catcher(signo)
int signo;
{
    if (!waiting) {
	xmouse_off();
	checkpoint_newsrcs();	/* good chance of crash while stopped */
	if (bos_on_stop) {
	    goto_xy(0, LINES-1);
	    putchar('\n') FLUSH;
	}
	termlib_reset();
	resetty();		/* this is the point of all this */
#ifdef DEBUG
	if (debug)
	    write(2,"stop_catcher\n",13);
#endif
	fflush(stdout);
	sigset(signo,SIG_DFL);	/* enable stop */
#ifdef HAS_SIGBLOCK
	sigsetmask(sigblock(0) & ~(1 << (signo-1)));
#endif
	kill(0,signo);		/* and do the stop */
    	savetty();
#ifdef MAILCALL
    	mailcount = 0;			/* force recheck */
#endif
    	if (!panic) {
	    if (!waiting) {
		termlib_init();
		noecho();			/* set no echo */
		crmode();			/* set cbreak mode */
		forceme("\f");			/* cause a refresh */
						/* (defined only if TIOCSTI defined) */
		errno = 0;			/* needed for getcmd */
	    }
    	}
	xmouse_check();
    }
    sigset(signo,stop_catcher);	/* unenable the stop */
}
#endif