]>
Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* signals.c -- signal handling support for readline. */ |
2 | ||
5bdf8622 | 3 | /* Copyright (C) 1987-2005 Free Software Foundation, Inc. |
d60d9f65 SS |
4 | |
5 | This file is part of the GNU Readline Library, a library for | |
6 | reading lines of text with interactive input and history editing. | |
7 | ||
8 | The GNU Readline Library is free software; you can redistribute it | |
9 | and/or modify it under the terms of the GNU General Public License | |
1b17e766 | 10 | as published by the Free Software Foundation; either version 2, or |
d60d9f65 SS |
11 | (at your option) any later version. |
12 | ||
13 | The GNU Readline Library is distributed in the hope that it will be | |
14 | useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
15 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | The GNU General Public License is often shipped with GNU software, and | |
19 | is generally kept in a file called COPYING or LICENSE. If you do not | |
20 | have a copy of the license, write to the Free Software Foundation, | |
1b17e766 | 21 | 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
d60d9f65 SS |
22 | #define READLINE_LIBRARY |
23 | ||
24 | #if defined (HAVE_CONFIG_H) | |
25 | # include <config.h> | |
26 | #endif | |
27 | ||
28 | #include <stdio.h> /* Just for NULL. Yuck. */ | |
29 | #include <sys/types.h> | |
30 | #include <signal.h> | |
31 | ||
32 | #if defined (HAVE_UNISTD_H) | |
33 | # include <unistd.h> | |
34 | #endif /* HAVE_UNISTD_H */ | |
35 | ||
36 | /* System-specific feature definitions and include files. */ | |
37 | #include "rldefs.h" | |
38 | ||
39 | #if defined (GWINSZ_IN_SYS_IOCTL) | |
40 | # include <sys/ioctl.h> | |
41 | #endif /* GWINSZ_IN_SYS_IOCTL */ | |
42 | ||
d60d9f65 SS |
43 | #if defined (HANDLE_SIGNALS) |
44 | /* Some standard library routines. */ | |
45 | #include "readline.h" | |
46 | #include "history.h" | |
47 | ||
1b17e766 EZ |
48 | #include "rlprivate.h" |
49 | ||
d60d9f65 SS |
50 | #if !defined (RETSIGTYPE) |
51 | # if defined (VOID_SIGHANDLER) | |
52 | # define RETSIGTYPE void | |
53 | # else | |
54 | # define RETSIGTYPE int | |
55 | # endif /* !VOID_SIGHANDLER */ | |
56 | #endif /* !RETSIGTYPE */ | |
57 | ||
58 | #if defined (VOID_SIGHANDLER) | |
59 | # define SIGHANDLER_RETURN return | |
60 | #else | |
61 | # define SIGHANDLER_RETURN return (0) | |
62 | #endif | |
63 | ||
9255ee31 | 64 | /* This typedef is equivalent to the one for Function; it allows us |
d60d9f65 SS |
65 | to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */ |
66 | typedef RETSIGTYPE SigHandler (); | |
67 | ||
1b17e766 EZ |
68 | #if defined (HAVE_POSIX_SIGNALS) |
69 | typedef struct sigaction sighandler_cxt; | |
70 | # define rl_sigaction(s, nh, oh) sigaction(s, nh, oh) | |
71 | #else | |
72 | typedef struct { SigHandler *sa_handler; int sa_mask, sa_flags; } sighandler_cxt; | |
73 | # define sigemptyset(m) | |
74 | #endif /* !HAVE_POSIX_SIGNALS */ | |
c862e87b | 75 | |
5bdf8622 DJ |
76 | #ifndef SA_RESTART |
77 | # define SA_RESTART 0 | |
78 | #endif | |
79 | ||
9255ee31 EZ |
80 | static SigHandler *rl_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *)); |
81 | static void rl_maybe_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *)); | |
d60d9f65 | 82 | |
c862e87b JM |
83 | /* Exported variables for use by applications. */ |
84 | ||
85 | /* If non-zero, readline will install its own signal handlers for | |
86 | SIGINT, SIGTERM, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */ | |
87 | int rl_catch_signals = 1; | |
88 | ||
89 | /* If non-zero, readline will install a signal handler for SIGWINCH. */ | |
90 | #ifdef SIGWINCH | |
91 | int rl_catch_sigwinch = 1; | |
5bdf8622 DJ |
92 | #else |
93 | int rl_catch_sigwinch = 0; /* for the readline state struct in readline.c */ | |
c862e87b JM |
94 | #endif |
95 | ||
96 | static int signals_set_flag; | |
97 | static int sigwinch_set_flag; | |
98 | ||
d60d9f65 SS |
99 | /* **************************************************************** */ |
100 | /* */ | |
101 | /* Signal Handling */ | |
102 | /* */ | |
103 | /* **************************************************************** */ | |
104 | ||
c862e87b JM |
105 | static sighandler_cxt old_int, old_term, old_alrm, old_quit; |
106 | #if defined (SIGTSTP) | |
d60d9f65 | 107 | static sighandler_cxt old_tstp, old_ttou, old_ttin; |
d60d9f65 | 108 | #endif |
d60d9f65 SS |
109 | #if defined (SIGWINCH) |
110 | static sighandler_cxt old_winch; | |
111 | #endif | |
112 | ||
113 | /* Readline signal handler functions. */ | |
114 | ||
115 | static RETSIGTYPE | |
116 | rl_signal_handler (sig) | |
117 | int sig; | |
118 | { | |
119 | #if defined (HAVE_POSIX_SIGNALS) | |
120 | sigset_t set; | |
121 | #else /* !HAVE_POSIX_SIGNALS */ | |
122 | # if defined (HAVE_BSD_SIGNALS) | |
123 | long omask; | |
124 | # else /* !HAVE_BSD_SIGNALS */ | |
125 | sighandler_cxt dummy_cxt; /* needed for rl_set_sighandler call */ | |
126 | # endif /* !HAVE_BSD_SIGNALS */ | |
127 | #endif /* !HAVE_POSIX_SIGNALS */ | |
128 | ||
9255ee31 EZ |
129 | RL_SETSTATE(RL_STATE_SIGHANDLER); |
130 | ||
d60d9f65 SS |
131 | #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS) |
132 | /* Since the signal will not be blocked while we are in the signal | |
133 | handler, ignore it until rl_clear_signals resets the catcher. */ | |
5bdf8622 DJ |
134 | # if defined (SIGALRM) |
135 | if (sig == SIGINT || sig == SIGALRM) | |
136 | # else | |
137 | if (sig == SIGINT) | |
138 | # endif | |
d60d9f65 SS |
139 | rl_set_sighandler (sig, SIG_IGN, &dummy_cxt); |
140 | #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */ | |
141 | ||
142 | switch (sig) | |
143 | { | |
144 | case SIGINT: | |
c862e87b JM |
145 | rl_free_line_state (); |
146 | /* FALLTHROUGH */ | |
d60d9f65 | 147 | |
5bdf8622 | 148 | case SIGTERM: |
d60d9f65 SS |
149 | #if defined (SIGTSTP) |
150 | case SIGTSTP: | |
151 | case SIGTTOU: | |
152 | case SIGTTIN: | |
153 | #endif /* SIGTSTP */ | |
5bdf8622 | 154 | #if defined (SIGALRM) |
d60d9f65 | 155 | case SIGALRM: |
430b7832 | 156 | #endif |
5bdf8622 | 157 | #if defined (SIGQUIT) |
c862e87b | 158 | case SIGQUIT: |
430b7832 | 159 | #endif |
c862e87b | 160 | rl_cleanup_after_signal (); |
d60d9f65 SS |
161 | |
162 | #if defined (HAVE_POSIX_SIGNALS) | |
163 | sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set); | |
164 | sigdelset (&set, sig); | |
165 | #else /* !HAVE_POSIX_SIGNALS */ | |
166 | # if defined (HAVE_BSD_SIGNALS) | |
167 | omask = sigblock (0); | |
168 | # endif /* HAVE_BSD_SIGNALS */ | |
169 | #endif /* !HAVE_POSIX_SIGNALS */ | |
170 | ||
1b17e766 EZ |
171 | #if defined (__EMX__) |
172 | signal (sig, SIG_ACK); | |
173 | #endif | |
174 | ||
5bdf8622 | 175 | #if defined (HAVE_KILL) |
d60d9f65 | 176 | kill (getpid (), sig); |
430b7832 | 177 | #else |
5bdf8622 | 178 | raise (sig); /* assume we have raise */ |
430b7832 | 179 | #endif |
d60d9f65 SS |
180 | |
181 | /* Let the signal that we just sent through. */ | |
182 | #if defined (HAVE_POSIX_SIGNALS) | |
183 | sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL); | |
184 | #else /* !HAVE_POSIX_SIGNALS */ | |
185 | # if defined (HAVE_BSD_SIGNALS) | |
186 | sigsetmask (omask & ~(sigmask (sig))); | |
187 | # endif /* HAVE_BSD_SIGNALS */ | |
188 | #endif /* !HAVE_POSIX_SIGNALS */ | |
189 | ||
c862e87b | 190 | rl_reset_after_signal (); |
d60d9f65 SS |
191 | } |
192 | ||
9255ee31 | 193 | RL_UNSETSTATE(RL_STATE_SIGHANDLER); |
d60d9f65 SS |
194 | SIGHANDLER_RETURN; |
195 | } | |
196 | ||
197 | #if defined (SIGWINCH) | |
198 | static RETSIGTYPE | |
c862e87b | 199 | rl_sigwinch_handler (sig) |
d60d9f65 SS |
200 | int sig; |
201 | { | |
202 | SigHandler *oh; | |
203 | ||
204 | #if defined (MUST_REINSTALL_SIGHANDLERS) | |
205 | sighandler_cxt dummy_winch; | |
206 | ||
207 | /* We don't want to change old_winch -- it holds the state of SIGWINCH | |
208 | disposition set by the calling application. We need this state | |
209 | because we call the application's SIGWINCH handler after updating | |
210 | our own idea of the screen size. */ | |
c862e87b | 211 | rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch); |
d60d9f65 SS |
212 | #endif |
213 | ||
9255ee31 | 214 | RL_SETSTATE(RL_STATE_SIGHANDLER); |
c862e87b | 215 | rl_resize_terminal (); |
d60d9f65 SS |
216 | |
217 | /* If another sigwinch handler has been installed, call it. */ | |
218 | oh = (SigHandler *)old_winch.sa_handler; | |
219 | if (oh && oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL) | |
220 | (*oh) (sig); | |
221 | ||
9255ee31 | 222 | RL_UNSETSTATE(RL_STATE_SIGHANDLER); |
d60d9f65 SS |
223 | SIGHANDLER_RETURN; |
224 | } | |
225 | #endif /* SIGWINCH */ | |
226 | ||
227 | /* Functions to manage signal handling. */ | |
228 | ||
229 | #if !defined (HAVE_POSIX_SIGNALS) | |
230 | static int | |
231 | rl_sigaction (sig, nh, oh) | |
232 | int sig; | |
233 | sighandler_cxt *nh, *oh; | |
234 | { | |
235 | oh->sa_handler = signal (sig, nh->sa_handler); | |
236 | return 0; | |
237 | } | |
238 | #endif /* !HAVE_POSIX_SIGNALS */ | |
239 | ||
240 | /* Set up a readline-specific signal handler, saving the old signal | |
241 | information in OHANDLER. Return the old signal handler, like | |
242 | signal(). */ | |
243 | static SigHandler * | |
244 | rl_set_sighandler (sig, handler, ohandler) | |
245 | int sig; | |
246 | SigHandler *handler; | |
247 | sighandler_cxt *ohandler; | |
248 | { | |
1b17e766 | 249 | sighandler_cxt old_handler; |
d60d9f65 SS |
250 | #if defined (HAVE_POSIX_SIGNALS) |
251 | struct sigaction act; | |
252 | ||
253 | act.sa_handler = handler; | |
230335c4 | 254 | #if defined (SIGWINCH) |
5bdf8622 | 255 | act.sa_flags = (sig == SIGWINCH) ? SA_RESTART : 0; |
230335c4 PA |
256 | #else |
257 | act.sa_flags = 0; | |
258 | #endif | |
d60d9f65 SS |
259 | sigemptyset (&act.sa_mask); |
260 | sigemptyset (&ohandler->sa_mask); | |
1b17e766 | 261 | sigaction (sig, &act, &old_handler); |
d60d9f65 | 262 | #else |
1b17e766 | 263 | old_handler.sa_handler = (SigHandler *)signal (sig, handler); |
d60d9f65 | 264 | #endif /* !HAVE_POSIX_SIGNALS */ |
1b17e766 EZ |
265 | |
266 | /* XXX -- assume we have memcpy */ | |
267 | /* If rl_set_signals is called twice in a row, don't set the old handler to | |
268 | rl_signal_handler, because that would cause infinite recursion. */ | |
269 | if (handler != rl_signal_handler || old_handler.sa_handler != rl_signal_handler) | |
270 | memcpy (ohandler, &old_handler, sizeof (sighandler_cxt)); | |
271 | ||
d60d9f65 SS |
272 | return (ohandler->sa_handler); |
273 | } | |
274 | ||
c862e87b JM |
275 | static void |
276 | rl_maybe_set_sighandler (sig, handler, ohandler) | |
277 | int sig; | |
278 | SigHandler *handler; | |
279 | sighandler_cxt *ohandler; | |
d60d9f65 SS |
280 | { |
281 | sighandler_cxt dummy; | |
282 | SigHandler *oh; | |
283 | ||
d60d9f65 | 284 | sigemptyset (&dummy.sa_mask); |
c862e87b | 285 | oh = rl_set_sighandler (sig, handler, ohandler); |
d60d9f65 | 286 | if (oh == (SigHandler *)SIG_IGN) |
c862e87b JM |
287 | rl_sigaction (sig, ohandler, &dummy); |
288 | } | |
d60d9f65 | 289 | |
c862e87b JM |
290 | int |
291 | rl_set_signals () | |
292 | { | |
293 | sighandler_cxt dummy; | |
294 | SigHandler *oh; | |
295 | ||
296 | if (rl_catch_signals && signals_set_flag == 0) | |
297 | { | |
298 | rl_maybe_set_sighandler (SIGINT, rl_signal_handler, &old_int); | |
299 | rl_maybe_set_sighandler (SIGTERM, rl_signal_handler, &old_term); | |
5bdf8622 | 300 | #if defined (SIGQUIT) |
c862e87b | 301 | rl_maybe_set_sighandler (SIGQUIT, rl_signal_handler, &old_quit); |
430b7832 | 302 | #endif |
c862e87b | 303 | |
5bdf8622 | 304 | #if defined (SIGALRM) |
c862e87b JM |
305 | oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm); |
306 | if (oh == (SigHandler *)SIG_IGN) | |
307 | rl_sigaction (SIGALRM, &old_alrm, &dummy); | |
d60d9f65 | 308 | #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART) |
c862e87b JM |
309 | /* If the application using readline has already installed a signal |
310 | handler with SA_RESTART, SIGALRM will cause reads to be restarted | |
311 | automatically, so readline should just get out of the way. Since | |
312 | we tested for SIG_IGN above, we can just test for SIG_DFL here. */ | |
313 | if (oh != (SigHandler *)SIG_DFL && (old_alrm.sa_flags & SA_RESTART)) | |
314 | rl_sigaction (SIGALRM, &old_alrm, &dummy); | |
d60d9f65 | 315 | #endif /* HAVE_POSIX_SIGNALS */ |
430b7832 | 316 | #endif /* SIGALRM */ |
d60d9f65 | 317 | |
d60d9f65 | 318 | #if defined (SIGTSTP) |
c862e87b | 319 | rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp); |
d60d9f65 SS |
320 | #endif /* SIGTSTP */ |
321 | ||
322 | #if defined (SIGTTOU) | |
c862e87b | 323 | rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou); |
d60d9f65 SS |
324 | #endif /* SIGTTOU */ |
325 | ||
c862e87b JM |
326 | #if defined (SIGTTIN) |
327 | rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin); | |
328 | #endif /* SIGTTIN */ | |
d60d9f65 | 329 | |
c862e87b JM |
330 | signals_set_flag = 1; |
331 | } | |
d60d9f65 SS |
332 | |
333 | #if defined (SIGWINCH) | |
c862e87b JM |
334 | if (rl_catch_sigwinch && sigwinch_set_flag == 0) |
335 | { | |
336 | rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch); | |
337 | sigwinch_set_flag = 1; | |
338 | } | |
d60d9f65 SS |
339 | #endif /* SIGWINCH */ |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
344 | int | |
345 | rl_clear_signals () | |
346 | { | |
347 | sighandler_cxt dummy; | |
348 | ||
c862e87b JM |
349 | if (rl_catch_signals && signals_set_flag == 1) |
350 | { | |
351 | sigemptyset (&dummy.sa_mask); | |
d60d9f65 | 352 | |
c862e87b JM |
353 | rl_sigaction (SIGINT, &old_int, &dummy); |
354 | rl_sigaction (SIGTERM, &old_term, &dummy); | |
5bdf8622 | 355 | #if defined (SIGQUIT) |
c862e87b | 356 | rl_sigaction (SIGQUIT, &old_quit, &dummy); |
430b7832 | 357 | #endif |
5bdf8622 | 358 | #if defined (SIGALRM) |
c862e87b | 359 | rl_sigaction (SIGALRM, &old_alrm, &dummy); |
430b7832 | 360 | #endif |
d60d9f65 SS |
361 | |
362 | #if defined (SIGTSTP) | |
c862e87b JM |
363 | rl_sigaction (SIGTSTP, &old_tstp, &dummy); |
364 | #endif /* SIGTSTP */ | |
d60d9f65 SS |
365 | |
366 | #if defined (SIGTTOU) | |
c862e87b | 367 | rl_sigaction (SIGTTOU, &old_ttou, &dummy); |
d60d9f65 SS |
368 | #endif /* SIGTTOU */ |
369 | ||
c862e87b JM |
370 | #if defined (SIGTTIN) |
371 | rl_sigaction (SIGTTIN, &old_ttin, &dummy); | |
372 | #endif /* SIGTTIN */ | |
d60d9f65 | 373 | |
c862e87b JM |
374 | signals_set_flag = 0; |
375 | } | |
d60d9f65 SS |
376 | |
377 | #if defined (SIGWINCH) | |
c862e87b JM |
378 | if (rl_catch_sigwinch && sigwinch_set_flag == 1) |
379 | { | |
380 | sigemptyset (&dummy.sa_mask); | |
381 | rl_sigaction (SIGWINCH, &old_winch, &dummy); | |
382 | sigwinch_set_flag = 0; | |
383 | } | |
d60d9f65 SS |
384 | #endif |
385 | ||
386 | return 0; | |
387 | } | |
c862e87b JM |
388 | |
389 | /* Clean up the terminal and readline state after catching a signal, before | |
390 | resending it to the calling application. */ | |
391 | void | |
392 | rl_cleanup_after_signal () | |
393 | { | |
394 | _rl_clean_up_for_exit (); | |
5bdf8622 DJ |
395 | if (rl_deprep_term_function) |
396 | (*rl_deprep_term_function) (); | |
c862e87b | 397 | rl_clear_signals (); |
9255ee31 | 398 | rl_clear_pending_input (); |
c862e87b JM |
399 | } |
400 | ||
401 | /* Reset the terminal and readline state after a signal handler returns. */ | |
402 | void | |
403 | rl_reset_after_signal () | |
404 | { | |
5bdf8622 DJ |
405 | if (rl_prep_term_function) |
406 | (*rl_prep_term_function) (_rl_meta_flag); | |
c862e87b JM |
407 | rl_set_signals (); |
408 | } | |
409 | ||
410 | /* Free up the readline variable line state for the current line (undo list, | |
411 | any partial history entry, any keyboard macros in progress, and any | |
412 | numeric arguments in process) after catching a signal, before calling | |
413 | rl_cleanup_after_signal(). */ | |
414 | void | |
415 | rl_free_line_state () | |
416 | { | |
417 | register HIST_ENTRY *entry; | |
418 | ||
9255ee31 | 419 | rl_free_undo_list (); |
c862e87b JM |
420 | |
421 | entry = current_history (); | |
422 | if (entry) | |
423 | entry->data = (char *)NULL; | |
424 | ||
425 | _rl_kill_kbd_macro (); | |
426 | rl_clear_message (); | |
5bdf8622 | 427 | _rl_reset_argument (); |
c862e87b JM |
428 | } |
429 | ||
d60d9f65 | 430 | #endif /* HANDLE_SIGNALS */ |