signal.c revision 1.6 1 1.6 chs /* $NetBSD: signal.c,v 1.6 2004/09/01 01:46:56 chs Exp $ */
2 1.2 perry
3 1.1 cjs /*
4 1.5 agc * Copyright (c) 1988 Mark Nudelman
5 1.1 cjs * Copyright (c) 1988, 1993
6 1.1 cjs * The Regents of the University of California. All rights reserved.
7 1.1 cjs *
8 1.1 cjs * Redistribution and use in source and binary forms, with or without
9 1.1 cjs * modification, are permitted provided that the following conditions
10 1.1 cjs * are met:
11 1.1 cjs * 1. Redistributions of source code must retain the above copyright
12 1.1 cjs * notice, this list of conditions and the following disclaimer.
13 1.1 cjs * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cjs * notice, this list of conditions and the following disclaimer in the
15 1.1 cjs * documentation and/or other materials provided with the distribution.
16 1.4 agc * 3. Neither the name of the University nor the names of its contributors
17 1.4 agc * may be used to endorse or promote products derived from this software
18 1.4 agc * without specific prior written permission.
19 1.4 agc *
20 1.4 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 1.4 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.4 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.4 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 1.4 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.4 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.4 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.4 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.4 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.4 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.4 agc * SUCH DAMAGE.
31 1.4 agc */
32 1.4 agc
33 1.3 christos #include <sys/cdefs.h>
34 1.1 cjs #ifndef lint
35 1.3 christos #if 0
36 1.1 cjs static char sccsid[] = "@(#)signal.c 8.1 (Berkeley) 6/6/93";
37 1.3 christos #else
38 1.6 chs __RCSID("$NetBSD: signal.c,v 1.6 2004/09/01 01:46:56 chs Exp $");
39 1.3 christos #endif
40 1.1 cjs #endif /* not lint */
41 1.1 cjs
42 1.1 cjs /*
43 1.1 cjs * Routines dealing with signals.
44 1.1 cjs *
45 1.1 cjs * A signal usually merely causes a bit to be set in the "signals" word.
46 1.1 cjs * At some convenient time, the mainline code checks to see if any
47 1.1 cjs * signals need processing by calling psignal().
48 1.1 cjs * If we happen to be reading from a file [in iread()] at the time
49 1.1 cjs * the signal is received, we call intread to interrupt the iread.
50 1.1 cjs */
51 1.1 cjs
52 1.1 cjs #include <signal.h>
53 1.3 christos #include <unistd.h>
54 1.3 christos
55 1.3 christos #include "less.h"
56 1.3 christos #include "extern.h"
57 1.1 cjs
58 1.1 cjs /*
59 1.1 cjs * "sigs" contains bits indicating signals which need to be processed.
60 1.1 cjs */
61 1.1 cjs int sigs;
62 1.1 cjs
63 1.1 cjs #ifdef SIGTSTP
64 1.1 cjs #define S_STOP 02
65 1.1 cjs #endif
66 1.1 cjs #if defined(SIGWINCH) || defined(SIGWIND)
67 1.1 cjs #define S_WINCH 04
68 1.1 cjs #endif
69 1.1 cjs
70 1.3 christos
71 1.3 christos static void purgeandquit __P((int));
72 1.1 cjs
73 1.1 cjs #ifdef SIGTSTP
74 1.3 christos static void stop __P((int));
75 1.1 cjs /*
76 1.1 cjs * "Stop" (^Z) signal handler.
77 1.1 cjs */
78 1.1 cjs static void
79 1.3 christos stop(n)
80 1.3 christos int n;
81 1.1 cjs {
82 1.1 cjs (void)signal(SIGTSTP, stop);
83 1.1 cjs sigs |= S_STOP;
84 1.1 cjs if (reading)
85 1.1 cjs intread();
86 1.1 cjs }
87 1.1 cjs #endif
88 1.1 cjs
89 1.1 cjs #ifdef SIGWINCH
90 1.1 cjs /*
91 1.1 cjs * "Window" change handler
92 1.1 cjs */
93 1.1 cjs void
94 1.3 christos winch(n)
95 1.3 christos int n;
96 1.1 cjs {
97 1.1 cjs (void)signal(SIGWINCH, winch);
98 1.1 cjs sigs |= S_WINCH;
99 1.1 cjs if (reading)
100 1.1 cjs intread();
101 1.1 cjs }
102 1.1 cjs #else
103 1.1 cjs #ifdef SIGWIND
104 1.1 cjs /*
105 1.1 cjs * "Window" change handler
106 1.1 cjs */
107 1.1 cjs winch()
108 1.1 cjs {
109 1.1 cjs (void)signal(SIGWIND, winch);
110 1.1 cjs sigs |= S_WINCH;
111 1.1 cjs if (reading)
112 1.1 cjs intread();
113 1.1 cjs }
114 1.1 cjs #endif
115 1.1 cjs #endif
116 1.1 cjs
117 1.1 cjs static void
118 1.3 christos purgeandquit(n)
119 1.3 christos int n;
120 1.1 cjs {
121 1.1 cjs
122 1.1 cjs purge(); /* purge buffered output */
123 1.1 cjs quit();
124 1.1 cjs }
125 1.1 cjs
126 1.1 cjs /*
127 1.1 cjs * Set up the signal handlers.
128 1.1 cjs */
129 1.3 christos void
130 1.1 cjs init_signals(on)
131 1.1 cjs int on;
132 1.1 cjs {
133 1.1 cjs if (on)
134 1.1 cjs {
135 1.1 cjs /*
136 1.1 cjs * Set signal handlers.
137 1.1 cjs */
138 1.1 cjs (void)signal(SIGINT, purgeandquit);
139 1.1 cjs #ifdef SIGTSTP
140 1.1 cjs (void)signal(SIGTSTP, stop);
141 1.1 cjs #endif
142 1.1 cjs #ifdef SIGWINCH
143 1.1 cjs (void)signal(SIGWINCH, winch);
144 1.1 cjs #else
145 1.1 cjs #ifdef SIGWIND
146 1.1 cjs (void)signal(SIGWIND, winch);
147 1.1 cjs #endif
148 1.1 cjs #endif
149 1.1 cjs } else
150 1.1 cjs {
151 1.1 cjs /*
152 1.1 cjs * Restore signals to defaults.
153 1.1 cjs */
154 1.1 cjs (void)signal(SIGINT, SIG_DFL);
155 1.1 cjs #ifdef SIGTSTP
156 1.1 cjs (void)signal(SIGTSTP, SIG_DFL);
157 1.1 cjs #endif
158 1.1 cjs #ifdef SIGWINCH
159 1.1 cjs (void)signal(SIGWINCH, SIG_IGN);
160 1.1 cjs #endif
161 1.1 cjs #ifdef SIGWIND
162 1.1 cjs (void)signal(SIGWIND, SIG_IGN);
163 1.1 cjs #endif
164 1.1 cjs }
165 1.1 cjs }
166 1.1 cjs
167 1.1 cjs /*
168 1.1 cjs * Process any signals we have received.
169 1.1 cjs * A received signal cause a bit to be set in "sigs".
170 1.1 cjs */
171 1.3 christos void
172 1.1 cjs psignals()
173 1.1 cjs {
174 1.3 christos int tsignals;
175 1.1 cjs
176 1.1 cjs if ((tsignals = sigs) == 0)
177 1.1 cjs return;
178 1.1 cjs sigs = 0;
179 1.1 cjs
180 1.1 cjs #ifdef S_WINCH
181 1.1 cjs if (tsignals & S_WINCH)
182 1.1 cjs {
183 1.1 cjs int old_width, old_height;
184 1.1 cjs /*
185 1.1 cjs * Re-execute get_term() to read the new window size.
186 1.1 cjs */
187 1.1 cjs old_width = sc_width;
188 1.1 cjs old_height = sc_height;
189 1.1 cjs get_term();
190 1.1 cjs if (sc_width != old_width || sc_height != old_height)
191 1.1 cjs {
192 1.6 chs scroll_lines = (sc_height + 1) / 2;
193 1.1 cjs screen_trashed = 1;
194 1.1 cjs }
195 1.1 cjs }
196 1.1 cjs #endif
197 1.1 cjs #ifdef SIGTSTP
198 1.1 cjs if (tsignals & S_STOP)
199 1.1 cjs {
200 1.1 cjs /*
201 1.1 cjs * Clean up the terminal.
202 1.1 cjs */
203 1.1 cjs #ifdef SIGTTOU
204 1.1 cjs (void)signal(SIGTTOU, SIG_IGN);
205 1.1 cjs #endif
206 1.1 cjs lower_left();
207 1.1 cjs clear_eol();
208 1.1 cjs deinit();
209 1.1 cjs (void)flush();
210 1.1 cjs raw_mode(0);
211 1.1 cjs #ifdef SIGTTOU
212 1.1 cjs (void)signal(SIGTTOU, SIG_DFL);
213 1.1 cjs #endif
214 1.1 cjs (void)signal(SIGTSTP, SIG_DFL);
215 1.1 cjs (void)kill(getpid(), SIGTSTP);
216 1.1 cjs /*
217 1.1 cjs * ... Bye bye. ...
218 1.1 cjs * Hopefully we'll be back later and resume here...
219 1.1 cjs * Reset the terminal and arrange to repaint the
220 1.1 cjs * screen when we get back to the main command loop.
221 1.1 cjs */
222 1.1 cjs (void)signal(SIGTSTP, stop);
223 1.1 cjs raw_mode(1);
224 1.1 cjs init();
225 1.1 cjs screen_trashed = 1;
226 1.1 cjs }
227 1.1 cjs #endif
228 1.1 cjs }
229