("" < 3.4) always evaluates to true, which unconditionally
[dragonfly.git] / contrib / less-381 / signal.c
bloba641d99ee9de0fd49723e141ea111dfdbcc20fd1
1 /*
2 * Copyright (C) 1984-2002 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Routines dealing with signals.
15 * A signal usually merely causes a bit to be set in the "signals" word.
16 * At some convenient time, the mainline code checks to see if any
17 * signals need processing by calling psignal().
18 * If we happen to be reading from a file [in iread()] at the time
19 * the signal is received, we call intread to interrupt the iread.
22 #include "less.h"
23 #include <signal.h>
26 * "sigs" contains bits indicating signals which need to be processed.
28 public int sigs;
30 extern int sc_width, sc_height;
31 extern int screen_trashed;
32 extern int lnloop;
33 extern int linenums;
34 extern int wscroll;
35 extern int reading;
38 * Interrupt signal handler.
40 /* ARGSUSED*/
41 static RETSIGTYPE
42 u_interrupt(type)
43 int type;
45 #if OS2
46 LSIGNAL(SIGINT, SIG_ACK);
47 #endif
48 LSIGNAL(SIGINT, u_interrupt);
49 sigs |= S_INTERRUPT;
50 #if MSDOS_COMPILER==DJGPPC
52 * If a keyboard has been hit, it must be Ctrl-C
53 * (as opposed to Ctrl-Break), so consume it.
54 * (Otherwise, Less will beep when it sees Ctrl-C from keyboard.)
56 if (kbhit())
57 getkey();
58 #endif
59 if (reading)
60 intread();
63 #ifdef SIGTSTP
65 * "Stop" (^Z) signal handler.
67 /* ARGSUSED*/
68 static RETSIGTYPE
69 stop(type)
70 int type;
72 LSIGNAL(SIGTSTP, stop);
73 sigs |= S_STOP;
74 if (reading)
75 intread();
77 #endif
79 #ifdef SIGWINCH
81 * "Window" change handler
83 /* ARGSUSED*/
84 public RETSIGTYPE
85 winch(type)
86 int type;
88 LSIGNAL(SIGWINCH, winch);
89 sigs |= S_WINCH;
90 if (reading)
91 intread();
93 #else
94 #ifdef SIGWIND
96 * "Window" change handler
98 /* ARGSUSED*/
99 public RETSIGTYPE
100 winch(type)
101 int type;
103 LSIGNAL(SIGWIND, winch);
104 sigs |= S_WINCH;
105 if (reading)
106 intread();
108 #endif
109 #endif
111 #if MSDOS_COMPILER==WIN32C
113 * Handle CTRL-C and CTRL-BREAK keys.
115 #include "windows.h"
117 static BOOL WINAPI
118 wbreak_handler(dwCtrlType)
119 DWORD dwCtrlType;
121 switch (dwCtrlType)
123 case CTRL_C_EVENT:
124 case CTRL_BREAK_EVENT:
125 sigs |= S_INTERRUPT;
126 return (TRUE);
127 default:
128 break;
130 return (FALSE);
132 #endif
135 * Set up the signal handlers.
137 public void
138 init_signals(on)
139 int on;
141 if (on)
144 * Set signal handlers.
146 (void) LSIGNAL(SIGINT, u_interrupt);
147 #if MSDOS_COMPILER==WIN32C
148 SetConsoleCtrlHandler(wbreak_handler, TRUE);
149 #endif
150 #ifdef SIGTSTP
151 (void) LSIGNAL(SIGTSTP, stop);
152 #endif
153 #ifdef SIGWINCH
154 (void) LSIGNAL(SIGWINCH, winch);
155 #else
156 #ifdef SIGWIND
157 (void) LSIGNAL(SIGWIND, winch);
158 #endif
159 #ifdef SIGQUIT
160 (void) LSIGNAL(SIGQUIT, SIG_IGN);
161 #endif
162 #endif
163 } else
166 * Restore signals to defaults.
168 (void) LSIGNAL(SIGINT, SIG_DFL);
169 #if MSDOS_COMPILER==WIN32C
170 SetConsoleCtrlHandler(wbreak_handler, FALSE);
171 #endif
172 #ifdef SIGTSTP
173 (void) LSIGNAL(SIGTSTP, SIG_DFL);
174 #endif
175 #ifdef SIGWINCH
176 (void) LSIGNAL(SIGWINCH, SIG_IGN);
177 #endif
178 #ifdef SIGWIND
179 (void) LSIGNAL(SIGWIND, SIG_IGN);
180 #endif
181 #ifdef SIGQUIT
182 (void) LSIGNAL(SIGQUIT, SIG_DFL);
183 #endif
188 * Process any signals we have received.
189 * A received signal cause a bit to be set in "sigs".
191 public void
192 psignals()
194 register int tsignals;
196 if ((tsignals = sigs) == 0)
197 return;
198 sigs = 0;
200 #ifdef SIGTSTP
201 if (tsignals & S_STOP)
204 * Clean up the terminal.
206 #ifdef SIGTTOU
207 LSIGNAL(SIGTTOU, SIG_IGN);
208 #endif
209 clear_bot();
210 deinit();
211 flush();
212 raw_mode(0);
213 #ifdef SIGTTOU
214 LSIGNAL(SIGTTOU, SIG_DFL);
215 #endif
216 LSIGNAL(SIGTSTP, SIG_DFL);
217 kill(getpid(), SIGTSTP);
219 * ... Bye bye. ...
220 * Hopefully we'll be back later and resume here...
221 * Reset the terminal and arrange to repaint the
222 * screen when we get back to the main command loop.
224 LSIGNAL(SIGTSTP, stop);
225 raw_mode(1);
226 init();
227 screen_trashed = 1;
228 tsignals |= S_WINCH;
230 #endif
231 #ifdef S_WINCH
232 if (tsignals & S_WINCH)
234 int old_width, old_height;
236 * Re-execute scrsize() to read the new window size.
238 old_width = sc_width;
239 old_height = sc_height;
240 get_term();
241 if (sc_width != old_width || sc_height != old_height)
243 wscroll = (sc_height + 1) / 2;
244 screen_trashed = 1;
247 #endif
248 if (tsignals & S_INTERRUPT)
250 bell();
252 * {{ You may wish to replace the bell() with
253 * error("Interrupt", NULL_PARG); }}
257 * If we were interrupted while in the "calculating
258 * line numbers" loop, turn off line numbers.
260 if (lnloop)
262 lnloop = 0;
263 if (linenums == 2)
264 screen_trashed = 1;
265 linenums = 0;
266 error("Line numbers turned off", NULL_PARG);