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