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