Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* signals.c -- signal handling support for readline. */ |
2 | ||
5836a818 | 3 | /* Copyright (C) 1987-2009 Free Software Foundation, Inc. |
d60d9f65 | 4 | |
cc88a640 JK |
5 | This file is part of the GNU Readline Library (Readline), a library |
6 | for reading lines of text with interactive input and history editing. | |
d60d9f65 | 7 | |
cc88a640 JK |
8 | Readline is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
d60d9f65 SS |
11 | (at your option) any later version. |
12 | ||
cc88a640 JK |
13 | Readline is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
d60d9f65 SS |
16 | GNU General Public License for more details. |
17 | ||
cc88a640 JK |
18 | You should have received a copy of the GNU General Public License |
19 | along with Readline. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
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 | /* Some standard library routines. */ |
44 | #include "readline.h" | |
45 | #include "history.h" | |
46 | ||
1b17e766 EZ |
47 | #include "rlprivate.h" |
48 | ||
cc88a640 JK |
49 | #if defined (HANDLE_SIGNALS) |
50 | ||
d60d9f65 SS |
51 | #if !defined (RETSIGTYPE) |
52 | # if defined (VOID_SIGHANDLER) | |
53 | # define RETSIGTYPE void | |
54 | # else | |
55 | # define RETSIGTYPE int | |
56 | # endif /* !VOID_SIGHANDLER */ | |
57 | #endif /* !RETSIGTYPE */ | |
58 | ||
59 | #if defined (VOID_SIGHANDLER) | |
60 | # define SIGHANDLER_RETURN return | |
61 | #else | |
62 | # define SIGHANDLER_RETURN return (0) | |
63 | #endif | |
64 | ||
9255ee31 | 65 | /* This typedef is equivalent to the one for Function; it allows us |
d60d9f65 SS |
66 | to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */ |
67 | typedef RETSIGTYPE SigHandler (); | |
68 | ||
1b17e766 EZ |
69 | #if defined (HAVE_POSIX_SIGNALS) |
70 | typedef struct sigaction sighandler_cxt; | |
71 | # define rl_sigaction(s, nh, oh) sigaction(s, nh, oh) | |
72 | #else | |
73 | typedef struct { SigHandler *sa_handler; int sa_mask, sa_flags; } sighandler_cxt; | |
74 | # define sigemptyset(m) | |
75 | #endif /* !HAVE_POSIX_SIGNALS */ | |
c862e87b | 76 | |
5bdf8622 DJ |
77 | #ifndef SA_RESTART |
78 | # define SA_RESTART 0 | |
79 | #endif | |
80 | ||
9255ee31 EZ |
81 | static SigHandler *rl_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *)); |
82 | static void rl_maybe_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *)); | |
d60d9f65 | 83 | |
cc88a640 JK |
84 | static RETSIGTYPE rl_signal_handler PARAMS((int)); |
85 | static RETSIGTYPE _rl_handle_signal PARAMS((int)); | |
86 | ||
c862e87b JM |
87 | /* Exported variables for use by applications. */ |
88 | ||
89 | /* If non-zero, readline will install its own signal handlers for | |
5836a818 | 90 | SIGINT, SIGTERM, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */ |
c862e87b JM |
91 | int rl_catch_signals = 1; |
92 | ||
93 | /* If non-zero, readline will install a signal handler for SIGWINCH. */ | |
94 | #ifdef SIGWINCH | |
95 | int rl_catch_sigwinch = 1; | |
5bdf8622 DJ |
96 | #else |
97 | int rl_catch_sigwinch = 0; /* for the readline state struct in readline.c */ | |
c862e87b JM |
98 | #endif |
99 | ||
cc88a640 JK |
100 | /* Private variables. */ |
101 | int _rl_interrupt_immediately = 0; | |
102 | int volatile _rl_caught_signal = 0; /* should be sig_atomic_t, but that requires including <signal.h> everywhere */ | |
103 | ||
104 | /* If non-zero, print characters corresponding to received signals as long as | |
105 | the user has indicated his desire to do so (_rl_echo_control_chars). */ | |
106 | int _rl_echoctl = 0; | |
107 | ||
108 | int _rl_intr_char = 0; | |
109 | int _rl_quit_char = 0; | |
110 | int _rl_susp_char = 0; | |
111 | ||
c862e87b JM |
112 | static int signals_set_flag; |
113 | static int sigwinch_set_flag; | |
114 | ||
d60d9f65 SS |
115 | /* **************************************************************** */ |
116 | /* */ | |
117 | /* Signal Handling */ | |
118 | /* */ | |
119 | /* **************************************************************** */ | |
120 | ||
5836a818 | 121 | static sighandler_cxt old_int, old_term, old_alrm, old_quit; |
c862e87b | 122 | #if defined (SIGTSTP) |
d60d9f65 | 123 | static sighandler_cxt old_tstp, old_ttou, old_ttin; |
d60d9f65 | 124 | #endif |
d60d9f65 SS |
125 | #if defined (SIGWINCH) |
126 | static sighandler_cxt old_winch; | |
127 | #endif | |
128 | ||
129 | /* Readline signal handler functions. */ | |
130 | ||
cc88a640 JK |
131 | /* Called from RL_CHECK_SIGNALS() macro */ |
132 | RETSIGTYPE | |
133 | _rl_signal_handler (sig) | |
134 | int sig; | |
135 | { | |
136 | _rl_caught_signal = 0; /* XXX */ | |
137 | ||
5836a818 | 138 | _rl_handle_signal (sig); |
cc88a640 JK |
139 | SIGHANDLER_RETURN; |
140 | } | |
141 | ||
d60d9f65 SS |
142 | static RETSIGTYPE |
143 | rl_signal_handler (sig) | |
144 | int sig; | |
cc88a640 | 145 | { |
5836a818 | 146 | if (_rl_interrupt_immediately || RL_ISSTATE(RL_STATE_CALLBACK)) |
cc88a640 JK |
147 | { |
148 | _rl_interrupt_immediately = 0; | |
149 | _rl_handle_signal (sig); | |
150 | } | |
151 | else | |
152 | _rl_caught_signal = sig; | |
153 | ||
154 | SIGHANDLER_RETURN; | |
155 | } | |
156 | ||
157 | static RETSIGTYPE | |
158 | _rl_handle_signal (sig) | |
159 | int sig; | |
d60d9f65 SS |
160 | { |
161 | #if defined (HAVE_POSIX_SIGNALS) | |
162 | sigset_t set; | |
163 | #else /* !HAVE_POSIX_SIGNALS */ | |
164 | # if defined (HAVE_BSD_SIGNALS) | |
165 | long omask; | |
166 | # else /* !HAVE_BSD_SIGNALS */ | |
167 | sighandler_cxt dummy_cxt; /* needed for rl_set_sighandler call */ | |
168 | # endif /* !HAVE_BSD_SIGNALS */ | |
169 | #endif /* !HAVE_POSIX_SIGNALS */ | |
170 | ||
9255ee31 EZ |
171 | RL_SETSTATE(RL_STATE_SIGHANDLER); |
172 | ||
d60d9f65 SS |
173 | #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS) |
174 | /* Since the signal will not be blocked while we are in the signal | |
175 | handler, ignore it until rl_clear_signals resets the catcher. */ | |
5bdf8622 DJ |
176 | # if defined (SIGALRM) |
177 | if (sig == SIGINT || sig == SIGALRM) | |
178 | # else | |
179 | if (sig == SIGINT) | |
180 | # endif | |
d60d9f65 SS |
181 | rl_set_sighandler (sig, SIG_IGN, &dummy_cxt); |
182 | #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */ | |
183 | ||
184 | switch (sig) | |
185 | { | |
186 | case SIGINT: | |
cc88a640 | 187 | _rl_reset_completion_state (); |
c862e87b JM |
188 | rl_free_line_state (); |
189 | /* FALLTHROUGH */ | |
d60d9f65 | 190 | |
5836a818 | 191 | case SIGTERM: |
d60d9f65 SS |
192 | #if defined (SIGTSTP) |
193 | case SIGTSTP: | |
4a11f206 | 194 | case SIGTTOU: |
5836a818 | 195 | case SIGTTIN: |
d60d9f65 | 196 | #endif /* SIGTSTP */ |
5bdf8622 | 197 | #if defined (SIGALRM) |
d60d9f65 | 198 | case SIGALRM: |
430b7832 | 199 | #endif |
5bdf8622 | 200 | #if defined (SIGQUIT) |
c862e87b | 201 | case SIGQUIT: |
430b7832 | 202 | #endif |
cc88a640 | 203 | rl_echo_signal_char (sig); |
c862e87b | 204 | rl_cleanup_after_signal (); |
d60d9f65 SS |
205 | |
206 | #if defined (HAVE_POSIX_SIGNALS) | |
cc88a640 | 207 | sigemptyset (&set); |
d60d9f65 SS |
208 | sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set); |
209 | sigdelset (&set, sig); | |
210 | #else /* !HAVE_POSIX_SIGNALS */ | |
211 | # if defined (HAVE_BSD_SIGNALS) | |
212 | omask = sigblock (0); | |
213 | # endif /* HAVE_BSD_SIGNALS */ | |
214 | #endif /* !HAVE_POSIX_SIGNALS */ | |
215 | ||
1b17e766 EZ |
216 | #if defined (__EMX__) |
217 | signal (sig, SIG_ACK); | |
218 | #endif | |
219 | ||
5bdf8622 | 220 | #if defined (HAVE_KILL) |
d60d9f65 | 221 | kill (getpid (), sig); |
430b7832 | 222 | #else |
5bdf8622 | 223 | raise (sig); /* assume we have raise */ |
430b7832 | 224 | #endif |
d60d9f65 SS |
225 | |
226 | /* Let the signal that we just sent through. */ | |
227 | #if defined (HAVE_POSIX_SIGNALS) | |
228 | sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL); | |
229 | #else /* !HAVE_POSIX_SIGNALS */ | |
230 | # if defined (HAVE_BSD_SIGNALS) | |
231 | sigsetmask (omask & ~(sigmask (sig))); | |
232 | # endif /* HAVE_BSD_SIGNALS */ | |
233 | #endif /* !HAVE_POSIX_SIGNALS */ | |
234 | ||
5836a818 | 235 | rl_reset_after_signal (); |
d60d9f65 SS |
236 | } |
237 | ||
9255ee31 | 238 | RL_UNSETSTATE(RL_STATE_SIGHANDLER); |
d60d9f65 SS |
239 | SIGHANDLER_RETURN; |
240 | } | |
241 | ||
242 | #if defined (SIGWINCH) | |
243 | static RETSIGTYPE | |
c862e87b | 244 | rl_sigwinch_handler (sig) |
d60d9f65 SS |
245 | int sig; |
246 | { | |
247 | SigHandler *oh; | |
248 | ||
249 | #if defined (MUST_REINSTALL_SIGHANDLERS) | |
250 | sighandler_cxt dummy_winch; | |
251 | ||
252 | /* We don't want to change old_winch -- it holds the state of SIGWINCH | |
253 | disposition set by the calling application. We need this state | |
254 | because we call the application's SIGWINCH handler after updating | |
255 | our own idea of the screen size. */ | |
c862e87b | 256 | rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch); |
d60d9f65 SS |
257 | #endif |
258 | ||
9255ee31 | 259 | RL_SETSTATE(RL_STATE_SIGHANDLER); |
5836a818 | 260 | rl_resize_terminal (); |
d60d9f65 SS |
261 | |
262 | /* If another sigwinch handler has been installed, call it. */ | |
263 | oh = (SigHandler *)old_winch.sa_handler; | |
264 | if (oh && oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL) | |
265 | (*oh) (sig); | |
266 | ||
9255ee31 | 267 | RL_UNSETSTATE(RL_STATE_SIGHANDLER); |
d60d9f65 SS |
268 | SIGHANDLER_RETURN; |
269 | } | |
270 | #endif /* SIGWINCH */ | |
271 | ||
272 | /* Functions to manage signal handling. */ | |
273 | ||
274 | #if !defined (HAVE_POSIX_SIGNALS) | |
275 | static int | |
276 | rl_sigaction (sig, nh, oh) | |
277 | int sig; | |
278 | sighandler_cxt *nh, *oh; | |
279 | { | |
280 | oh->sa_handler = signal (sig, nh->sa_handler); | |
281 | return 0; | |
282 | } | |
283 | #endif /* !HAVE_POSIX_SIGNALS */ | |
284 | ||
285 | /* Set up a readline-specific signal handler, saving the old signal | |
286 | information in OHANDLER. Return the old signal handler, like | |
287 | signal(). */ | |
288 | static SigHandler * | |
289 | rl_set_sighandler (sig, handler, ohandler) | |
290 | int sig; | |
291 | SigHandler *handler; | |
292 | sighandler_cxt *ohandler; | |
293 | { | |
1b17e766 | 294 | sighandler_cxt old_handler; |
d60d9f65 SS |
295 | #if defined (HAVE_POSIX_SIGNALS) |
296 | struct sigaction act; | |
297 | ||
298 | act.sa_handler = handler; | |
cc88a640 | 299 | # if defined (SIGWINCH) |
5bdf8622 | 300 | act.sa_flags = (sig == SIGWINCH) ? SA_RESTART : 0; |
cc88a640 | 301 | # else |
230335c4 | 302 | act.sa_flags = 0; |
cc88a640 | 303 | # endif /* SIGWINCH */ |
d60d9f65 SS |
304 | sigemptyset (&act.sa_mask); |
305 | sigemptyset (&ohandler->sa_mask); | |
1b17e766 | 306 | sigaction (sig, &act, &old_handler); |
d60d9f65 | 307 | #else |
1b17e766 | 308 | old_handler.sa_handler = (SigHandler *)signal (sig, handler); |
d60d9f65 | 309 | #endif /* !HAVE_POSIX_SIGNALS */ |
1b17e766 EZ |
310 | |
311 | /* XXX -- assume we have memcpy */ | |
312 | /* If rl_set_signals is called twice in a row, don't set the old handler to | |
313 | rl_signal_handler, because that would cause infinite recursion. */ | |
314 | if (handler != rl_signal_handler || old_handler.sa_handler != rl_signal_handler) | |
315 | memcpy (ohandler, &old_handler, sizeof (sighandler_cxt)); | |
316 | ||
d60d9f65 SS |
317 | return (ohandler->sa_handler); |
318 | } | |
319 | ||
c862e87b JM |
320 | static void |
321 | rl_maybe_set_sighandler (sig, handler, ohandler) | |
322 | int sig; | |
323 | SigHandler *handler; | |
324 | sighandler_cxt *ohandler; | |
d60d9f65 SS |
325 | { |
326 | sighandler_cxt dummy; | |
327 | SigHandler *oh; | |
328 | ||
d60d9f65 | 329 | sigemptyset (&dummy.sa_mask); |
c862e87b | 330 | oh = rl_set_sighandler (sig, handler, ohandler); |
d60d9f65 | 331 | if (oh == (SigHandler *)SIG_IGN) |
c862e87b JM |
332 | rl_sigaction (sig, ohandler, &dummy); |
333 | } | |
d60d9f65 | 334 | |
c862e87b JM |
335 | int |
336 | rl_set_signals () | |
337 | { | |
338 | sighandler_cxt dummy; | |
339 | SigHandler *oh; | |
cc88a640 JK |
340 | #if defined (HAVE_POSIX_SIGNALS) |
341 | static int sigmask_set = 0; | |
342 | static sigset_t bset, oset; | |
343 | #endif | |
344 | ||
345 | #if defined (HAVE_POSIX_SIGNALS) | |
346 | if (rl_catch_signals && sigmask_set == 0) | |
347 | { | |
348 | sigemptyset (&bset); | |
349 | ||
350 | sigaddset (&bset, SIGINT); | |
351 | sigaddset (&bset, SIGTERM); | |
352 | #if defined (SIGQUIT) | |
353 | sigaddset (&bset, SIGQUIT); | |
354 | #endif | |
355 | #if defined (SIGALRM) | |
356 | sigaddset (&bset, SIGALRM); | |
357 | #endif | |
358 | #if defined (SIGTSTP) | |
359 | sigaddset (&bset, SIGTSTP); | |
360 | #endif | |
361 | #if defined (SIGTTIN) | |
362 | sigaddset (&bset, SIGTTIN); | |
363 | #endif | |
364 | #if defined (SIGTTOU) | |
365 | sigaddset (&bset, SIGTTOU); | |
366 | #endif | |
367 | sigmask_set = 1; | |
368 | } | |
369 | #endif /* HAVE_POSIX_SIGNALS */ | |
c862e87b JM |
370 | |
371 | if (rl_catch_signals && signals_set_flag == 0) | |
372 | { | |
cc88a640 JK |
373 | #if defined (HAVE_POSIX_SIGNALS) |
374 | sigemptyset (&oset); | |
375 | sigprocmask (SIG_BLOCK, &bset, &oset); | |
376 | #endif | |
377 | ||
c862e87b JM |
378 | rl_maybe_set_sighandler (SIGINT, rl_signal_handler, &old_int); |
379 | rl_maybe_set_sighandler (SIGTERM, rl_signal_handler, &old_term); | |
5bdf8622 | 380 | #if defined (SIGQUIT) |
c862e87b | 381 | rl_maybe_set_sighandler (SIGQUIT, rl_signal_handler, &old_quit); |
430b7832 | 382 | #endif |
c862e87b | 383 | |
5bdf8622 | 384 | #if defined (SIGALRM) |
c862e87b JM |
385 | oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm); |
386 | if (oh == (SigHandler *)SIG_IGN) | |
387 | rl_sigaction (SIGALRM, &old_alrm, &dummy); | |
d60d9f65 | 388 | #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART) |
c862e87b JM |
389 | /* If the application using readline has already installed a signal |
390 | handler with SA_RESTART, SIGALRM will cause reads to be restarted | |
391 | automatically, so readline should just get out of the way. Since | |
392 | we tested for SIG_IGN above, we can just test for SIG_DFL here. */ | |
393 | if (oh != (SigHandler *)SIG_DFL && (old_alrm.sa_flags & SA_RESTART)) | |
394 | rl_sigaction (SIGALRM, &old_alrm, &dummy); | |
d60d9f65 | 395 | #endif /* HAVE_POSIX_SIGNALS */ |
430b7832 | 396 | #endif /* SIGALRM */ |
d60d9f65 | 397 | |
d60d9f65 | 398 | #if defined (SIGTSTP) |
c862e87b | 399 | rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp); |
d60d9f65 SS |
400 | #endif /* SIGTSTP */ |
401 | ||
402 | #if defined (SIGTTOU) | |
c862e87b | 403 | rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou); |
d60d9f65 SS |
404 | #endif /* SIGTTOU */ |
405 | ||
c862e87b JM |
406 | #if defined (SIGTTIN) |
407 | rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin); | |
408 | #endif /* SIGTTIN */ | |
d60d9f65 | 409 | |
c862e87b | 410 | signals_set_flag = 1; |
cc88a640 JK |
411 | |
412 | #if defined (HAVE_POSIX_SIGNALS) | |
413 | sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL); | |
414 | #endif | |
c862e87b | 415 | } |
d60d9f65 SS |
416 | |
417 | #if defined (SIGWINCH) | |
c862e87b JM |
418 | if (rl_catch_sigwinch && sigwinch_set_flag == 0) |
419 | { | |
420 | rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch); | |
421 | sigwinch_set_flag = 1; | |
422 | } | |
d60d9f65 SS |
423 | #endif /* SIGWINCH */ |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | int | |
429 | rl_clear_signals () | |
430 | { | |
431 | sighandler_cxt dummy; | |
432 | ||
c862e87b JM |
433 | if (rl_catch_signals && signals_set_flag == 1) |
434 | { | |
435 | sigemptyset (&dummy.sa_mask); | |
d60d9f65 | 436 | |
5836a818 PP |
437 | rl_sigaction (SIGINT, &old_int, &dummy); |
438 | rl_sigaction (SIGTERM, &old_term, &dummy); | |
5bdf8622 | 439 | #if defined (SIGQUIT) |
5836a818 | 440 | rl_sigaction (SIGQUIT, &old_quit, &dummy); |
430b7832 | 441 | #endif |
5bdf8622 | 442 | #if defined (SIGALRM) |
5836a818 | 443 | rl_sigaction (SIGALRM, &old_alrm, &dummy); |
430b7832 | 444 | #endif |
d60d9f65 SS |
445 | |
446 | #if defined (SIGTSTP) | |
5836a818 | 447 | rl_sigaction (SIGTSTP, &old_tstp, &dummy); |
c862e87b | 448 | #endif /* SIGTSTP */ |
d60d9f65 SS |
449 | |
450 | #if defined (SIGTTOU) | |
5836a818 | 451 | rl_sigaction (SIGTTOU, &old_ttou, &dummy); |
d60d9f65 SS |
452 | #endif /* SIGTTOU */ |
453 | ||
c862e87b | 454 | #if defined (SIGTTIN) |
5836a818 | 455 | rl_sigaction (SIGTTIN, &old_ttin, &dummy); |
c862e87b | 456 | #endif /* SIGTTIN */ |
d60d9f65 | 457 | |
c862e87b JM |
458 | signals_set_flag = 0; |
459 | } | |
d60d9f65 SS |
460 | |
461 | #if defined (SIGWINCH) | |
c862e87b JM |
462 | if (rl_catch_sigwinch && sigwinch_set_flag == 1) |
463 | { | |
464 | sigemptyset (&dummy.sa_mask); | |
465 | rl_sigaction (SIGWINCH, &old_winch, &dummy); | |
466 | sigwinch_set_flag = 0; | |
467 | } | |
d60d9f65 SS |
468 | #endif |
469 | ||
470 | return 0; | |
471 | } | |
c862e87b JM |
472 | |
473 | /* Clean up the terminal and readline state after catching a signal, before | |
474 | resending it to the calling application. */ | |
475 | void | |
476 | rl_cleanup_after_signal () | |
477 | { | |
478 | _rl_clean_up_for_exit (); | |
5bdf8622 DJ |
479 | if (rl_deprep_term_function) |
480 | (*rl_deprep_term_function) (); | |
9255ee31 | 481 | rl_clear_pending_input (); |
cc88a640 | 482 | rl_clear_signals (); |
c862e87b JM |
483 | } |
484 | ||
485 | /* Reset the terminal and readline state after a signal handler returns. */ | |
486 | void | |
487 | rl_reset_after_signal () | |
488 | { | |
5bdf8622 DJ |
489 | if (rl_prep_term_function) |
490 | (*rl_prep_term_function) (_rl_meta_flag); | |
c862e87b JM |
491 | rl_set_signals (); |
492 | } | |
493 | ||
494 | /* Free up the readline variable line state for the current line (undo list, | |
495 | any partial history entry, any keyboard macros in progress, and any | |
496 | numeric arguments in process) after catching a signal, before calling | |
497 | rl_cleanup_after_signal(). */ | |
498 | void | |
499 | rl_free_line_state () | |
500 | { | |
501 | register HIST_ENTRY *entry; | |
502 | ||
9255ee31 | 503 | rl_free_undo_list (); |
c862e87b JM |
504 | |
505 | entry = current_history (); | |
506 | if (entry) | |
507 | entry->data = (char *)NULL; | |
508 | ||
509 | _rl_kill_kbd_macro (); | |
510 | rl_clear_message (); | |
5bdf8622 | 511 | _rl_reset_argument (); |
c862e87b JM |
512 | } |
513 | ||
d60d9f65 | 514 | #endif /* HANDLE_SIGNALS */ |
cc88a640 JK |
515 | |
516 | /* **************************************************************** */ | |
517 | /* */ | |
518 | /* SIGINT Management */ | |
519 | /* */ | |
520 | /* **************************************************************** */ | |
521 | ||
522 | #if defined (HAVE_POSIX_SIGNALS) | |
523 | static sigset_t sigint_set, sigint_oset; | |
524 | static sigset_t sigwinch_set, sigwinch_oset; | |
525 | #else /* !HAVE_POSIX_SIGNALS */ | |
526 | # if defined (HAVE_BSD_SIGNALS) | |
527 | static int sigint_oldmask; | |
528 | static int sigwinch_oldmask; | |
529 | # endif /* HAVE_BSD_SIGNALS */ | |
530 | #endif /* !HAVE_POSIX_SIGNALS */ | |
531 | ||
532 | static int sigint_blocked; | |
533 | static int sigwinch_blocked; | |
534 | ||
535 | /* Cause SIGINT to not be delivered until the corresponding call to | |
536 | release_sigint(). */ | |
537 | void | |
538 | _rl_block_sigint () | |
539 | { | |
540 | if (sigint_blocked) | |
541 | return; | |
542 | ||
5836a818 PP |
543 | #if defined (HAVE_POSIX_SIGNALS) |
544 | sigemptyset (&sigint_set); | |
545 | sigemptyset (&sigint_oset); | |
546 | sigaddset (&sigint_set, SIGINT); | |
547 | sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset); | |
548 | #else /* !HAVE_POSIX_SIGNALS */ | |
549 | # if defined (HAVE_BSD_SIGNALS) | |
550 | sigint_oldmask = sigblock (sigmask (SIGINT)); | |
551 | # else /* !HAVE_BSD_SIGNALS */ | |
552 | # if defined (HAVE_USG_SIGHOLD) | |
553 | sighold (SIGINT); | |
554 | # endif /* HAVE_USG_SIGHOLD */ | |
555 | # endif /* !HAVE_BSD_SIGNALS */ | |
556 | #endif /* !HAVE_POSIX_SIGNALS */ | |
557 | ||
cc88a640 JK |
558 | sigint_blocked = 1; |
559 | } | |
560 | ||
561 | /* Allow SIGINT to be delivered. */ | |
562 | void | |
563 | _rl_release_sigint () | |
564 | { | |
565 | if (sigint_blocked == 0) | |
566 | return; | |
567 | ||
5836a818 PP |
568 | #if defined (HAVE_POSIX_SIGNALS) |
569 | sigprocmask (SIG_SETMASK, &sigint_oset, (sigset_t *)NULL); | |
570 | #else | |
571 | # if defined (HAVE_BSD_SIGNALS) | |
572 | sigsetmask (sigint_oldmask); | |
573 | # else /* !HAVE_BSD_SIGNALS */ | |
574 | # if defined (HAVE_USG_SIGHOLD) | |
575 | sigrelse (SIGINT); | |
576 | # endif /* HAVE_USG_SIGHOLD */ | |
577 | # endif /* !HAVE_BSD_SIGNALS */ | |
578 | #endif /* !HAVE_POSIX_SIGNALS */ | |
579 | ||
cc88a640 JK |
580 | sigint_blocked = 0; |
581 | } | |
582 | ||
5836a818 | 583 | #ifdef SIGWINCH |
cc88a640 JK |
584 | /* Cause SIGWINCH to not be delivered until the corresponding call to |
585 | release_sigwinch(). */ | |
586 | void | |
587 | _rl_block_sigwinch () | |
588 | { | |
589 | if (sigwinch_blocked) | |
590 | return; | |
591 | ||
592 | #if defined (HAVE_POSIX_SIGNALS) | |
593 | sigemptyset (&sigwinch_set); | |
594 | sigemptyset (&sigwinch_oset); | |
595 | sigaddset (&sigwinch_set, SIGWINCH); | |
596 | sigprocmask (SIG_BLOCK, &sigwinch_set, &sigwinch_oset); | |
597 | #else /* !HAVE_POSIX_SIGNALS */ | |
598 | # if defined (HAVE_BSD_SIGNALS) | |
599 | sigwinch_oldmask = sigblock (sigmask (SIGWINCH)); | |
600 | # else /* !HAVE_BSD_SIGNALS */ | |
601 | # if defined (HAVE_USG_SIGHOLD) | |
602 | sighold (SIGWINCH); | |
603 | # endif /* HAVE_USG_SIGHOLD */ | |
604 | # endif /* !HAVE_BSD_SIGNALS */ | |
605 | #endif /* !HAVE_POSIX_SIGNALS */ | |
606 | ||
607 | sigwinch_blocked = 1; | |
608 | } | |
609 | ||
610 | /* Allow SIGWINCH to be delivered. */ | |
611 | void | |
612 | _rl_release_sigwinch () | |
613 | { | |
614 | if (sigwinch_blocked == 0) | |
615 | return; | |
616 | ||
617 | #if defined (HAVE_POSIX_SIGNALS) | |
618 | sigprocmask (SIG_SETMASK, &sigwinch_oset, (sigset_t *)NULL); | |
619 | #else | |
620 | # if defined (HAVE_BSD_SIGNALS) | |
621 | sigsetmask (sigwinch_oldmask); | |
622 | # else /* !HAVE_BSD_SIGNALS */ | |
623 | # if defined (HAVE_USG_SIGHOLD) | |
624 | sigrelse (SIGWINCH); | |
625 | # endif /* HAVE_USG_SIGHOLD */ | |
626 | # endif /* !HAVE_BSD_SIGNALS */ | |
627 | #endif /* !HAVE_POSIX_SIGNALS */ | |
628 | ||
629 | sigwinch_blocked = 0; | |
630 | } | |
5836a818 | 631 | #endif /* SIGWINCH */ |
cc88a640 JK |
632 | |
633 | /* **************************************************************** */ | |
634 | /* */ | |
635 | /* Echoing special control characters */ | |
636 | /* */ | |
637 | /* **************************************************************** */ | |
638 | void | |
639 | rl_echo_signal_char (sig) | |
640 | int sig; | |
641 | { | |
642 | char cstr[3]; | |
643 | int cslen, c; | |
644 | ||
645 | if (_rl_echoctl == 0 || _rl_echo_control_chars == 0) | |
646 | return; | |
647 | ||
648 | switch (sig) | |
649 | { | |
650 | case SIGINT: c = _rl_intr_char; break; | |
651 | #if defined (SIGQUIT) | |
652 | case SIGQUIT: c = _rl_quit_char; break; | |
653 | #endif | |
654 | #if defined (SIGTSTP) | |
655 | case SIGTSTP: c = _rl_susp_char; break; | |
656 | #endif | |
657 | default: return; | |
658 | } | |
659 | ||
660 | if (CTRL_CHAR (c) || c == RUBOUT) | |
661 | { | |
662 | cstr[0] = '^'; | |
663 | cstr[1] = CTRL_CHAR (c) ? UNCTRL (c) : '?'; | |
664 | cstr[cslen = 2] = '\0'; | |
665 | } | |
666 | else | |
667 | { | |
668 | cstr[0] = c; | |
669 | cstr[cslen = 1] = '\0'; | |
670 | } | |
671 | ||
672 | _rl_output_some_chars (cstr, cslen); | |
673 | } |