Commit | Line | Data |
---|---|---|
bfe13c81 KB |
1 | /* |
2 | * Copyright (c) 1988 Mark Nudleman | |
3 | * Copyright (c) 1988 Regents of the University of California. | |
4 | * All rights reserved. | |
5 | * | |
bfe13c81 KB |
6 | * Redistribution and use in source and binary forms are permitted |
7 | * provided that the above copyright notice and this paragraph are | |
8 | * duplicated in all such forms and that any documentation, | |
9 | * advertising materials, and other materials related to such | |
10 | * distribution and use acknowledge that the software was developed | |
a942b40b KB |
11 | * by Mark Nudleman and the University of California, Berkeley. The |
12 | * name of Mark Nudleman or the | |
bfe13c81 KB |
13 | * University may not be used to endorse or promote products derived |
14 | * from this software without specific prior written permission. | |
15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
17 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
18 | */ | |
19 | ||
20 | #ifndef lint | |
e0c897d0 | 21 | static char sccsid[] = "@(#)signal.c 5.6 (Berkeley) %G%"; |
bfe13c81 KB |
22 | #endif /* not lint */ |
23 | ||
24 | /* | |
25 | * Routines dealing with signals. | |
26 | * | |
27 | * A signal usually merely causes a bit to be set in the "signals" word. | |
28 | * At some convenient time, the mainline code checks to see if any | |
29 | * signals need processing by calling psignal(). | |
30 | * If we happen to be reading from a file [in iread()] at the time | |
31 | * the signal is received, we call intread to interrupt the iread. | |
32 | */ | |
33 | ||
966c6ec0 | 34 | #include <less.h> |
bfe13c81 KB |
35 | #include <signal.h> |
36 | ||
37 | /* | |
38 | * "sigs" contains bits indicating signals which need to be processed. | |
39 | */ | |
966c6ec0 | 40 | int sigs; |
bfe13c81 | 41 | |
bfe13c81 KB |
42 | #ifdef SIGTSTP |
43 | #define S_STOP 02 | |
44 | #endif | |
45 | #if defined(SIGWINCH) || defined(SIGWIND) | |
46 | #define S_WINCH 04 | |
47 | #endif | |
48 | ||
49 | extern int sc_width, sc_height; | |
50 | extern int screen_trashed; | |
51 | extern int lnloop; | |
52 | extern int linenums; | |
53 | extern int scroll; | |
54 | extern int reading; | |
55 | ||
bfe13c81 KB |
56 | #ifdef SIGTSTP |
57 | /* | |
58 | * "Stop" (^Z) signal handler. | |
59 | */ | |
966c6ec0 | 60 | static |
bfe13c81 KB |
61 | stop() |
62 | { | |
966c6ec0 | 63 | (void)signal(SIGTSTP, stop); |
bfe13c81 KB |
64 | sigs |= S_STOP; |
65 | if (reading) | |
66 | intread(); | |
67 | } | |
68 | #endif | |
69 | ||
70 | #ifdef SIGWINCH | |
71 | /* | |
72 | * "Window" change handler | |
73 | */ | |
bfe13c81 KB |
74 | winch() |
75 | { | |
966c6ec0 | 76 | (void)signal(SIGWINCH, winch); |
bfe13c81 KB |
77 | sigs |= S_WINCH; |
78 | if (reading) | |
79 | intread(); | |
80 | } | |
81 | #else | |
82 | #ifdef SIGWIND | |
83 | /* | |
84 | * "Window" change handler | |
85 | */ | |
bfe13c81 KB |
86 | winch() |
87 | { | |
966c6ec0 | 88 | (void)signal(SIGWIND, winch); |
bfe13c81 KB |
89 | sigs |= S_WINCH; |
90 | if (reading) | |
91 | intread(); | |
92 | } | |
93 | #endif | |
94 | #endif | |
95 | ||
e0c897d0 SL |
96 | static int |
97 | purgeandquit() | |
98 | { | |
99 | ||
100 | purge(); /* purge buffered output */ | |
101 | quit(); | |
102 | } | |
103 | ||
bfe13c81 KB |
104 | /* |
105 | * Set up the signal handlers. | |
106 | */ | |
bfe13c81 KB |
107 | init_signals(on) |
108 | int on; | |
109 | { | |
966c6ec0 KB |
110 | int quit(); |
111 | ||
bfe13c81 KB |
112 | if (on) |
113 | { | |
114 | /* | |
115 | * Set signal handlers. | |
116 | */ | |
e0c897d0 | 117 | (void)signal(SIGINT, purgeandquit); |
bfe13c81 | 118 | #ifdef SIGTSTP |
966c6ec0 | 119 | (void)signal(SIGTSTP, stop); |
bfe13c81 KB |
120 | #endif |
121 | #ifdef SIGWINCH | |
966c6ec0 | 122 | (void)signal(SIGWINCH, winch); |
bfe13c81 KB |
123 | #else |
124 | #ifdef SIGWIND | |
966c6ec0 | 125 | (void)signal(SIGWIND, winch); |
bfe13c81 KB |
126 | #endif |
127 | #endif | |
128 | } else | |
129 | { | |
130 | /* | |
131 | * Restore signals to defaults. | |
132 | */ | |
966c6ec0 | 133 | (void)signal(SIGINT, SIG_DFL); |
bfe13c81 | 134 | #ifdef SIGTSTP |
966c6ec0 | 135 | (void)signal(SIGTSTP, SIG_DFL); |
bfe13c81 KB |
136 | #endif |
137 | #ifdef SIGWINCH | |
966c6ec0 | 138 | (void)signal(SIGWINCH, SIG_IGN); |
bfe13c81 KB |
139 | #endif |
140 | #ifdef SIGWIND | |
966c6ec0 | 141 | (void)signal(SIGWIND, SIG_IGN); |
bfe13c81 KB |
142 | #endif |
143 | } | |
144 | } | |
145 | ||
146 | /* | |
147 | * Process any signals we have received. | |
148 | * A received signal cause a bit to be set in "sigs". | |
149 | */ | |
bfe13c81 KB |
150 | psignals() |
151 | { | |
152 | register int tsignals; | |
153 | ||
154 | if ((tsignals = sigs) == 0) | |
9bf656f5 | 155 | return; |
bfe13c81 KB |
156 | sigs = 0; |
157 | ||
158 | #ifdef S_WINCH | |
159 | if (tsignals & S_WINCH) | |
160 | { | |
161 | int old_width, old_height; | |
162 | /* | |
163 | * Re-execute get_term() to read the new window size. | |
164 | */ | |
165 | old_width = sc_width; | |
166 | old_height = sc_height; | |
167 | get_term(); | |
168 | if (sc_width != old_width || sc_height != old_height) | |
169 | { | |
170 | scroll = (sc_height + 1) / 2; | |
171 | screen_trashed = 1; | |
172 | } | |
173 | } | |
174 | #endif | |
175 | #ifdef SIGTSTP | |
176 | if (tsignals & S_STOP) | |
177 | { | |
178 | /* | |
179 | * Clean up the terminal. | |
180 | */ | |
181 | #ifdef SIGTTOU | |
966c6ec0 | 182 | (void)signal(SIGTTOU, SIG_IGN); |
bfe13c81 KB |
183 | #endif |
184 | lower_left(); | |
185 | clear_eol(); | |
186 | deinit(); | |
966c6ec0 | 187 | (void)flush(); |
bfe13c81 KB |
188 | raw_mode(0); |
189 | #ifdef SIGTTOU | |
966c6ec0 | 190 | (void)signal(SIGTTOU, SIG_DFL); |
bfe13c81 | 191 | #endif |
966c6ec0 KB |
192 | (void)signal(SIGTSTP, SIG_DFL); |
193 | (void)kill(getpid(), SIGTSTP); | |
bfe13c81 KB |
194 | /* |
195 | * ... Bye bye. ... | |
196 | * Hopefully we'll be back later and resume here... | |
197 | * Reset the terminal and arrange to repaint the | |
198 | * screen when we get back to the main command loop. | |
199 | */ | |
966c6ec0 | 200 | (void)signal(SIGTSTP, stop); |
bfe13c81 KB |
201 | raw_mode(1); |
202 | init(); | |
203 | screen_trashed = 1; | |
204 | } | |
205 | #endif | |
bfe13c81 | 206 | } |