1 /* signals.c -- signal handling support for readline. */
3 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
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.
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
11 (at your option) any later version.
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
16 GNU General Public License for more details.
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/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <stdio.h> /* Just for NULL. Yuck. */
29 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 /* System-specific feature definitions and include files. */
39 #if defined (GWINSZ_IN_SYS_IOCTL)
40 # include <sys/ioctl.h>
41 #endif /* GWINSZ_IN_SYS_IOCTL */
43 /* Some standard library routines. */
47 #include "rlprivate.h"
49 #if defined (HANDLE_SIGNALS)
51 #if !defined (RETSIGTYPE)
52 # if defined (VOID_SIGHANDLER)
53 # define RETSIGTYPE void
55 # define RETSIGTYPE int
56 # endif /* !VOID_SIGHANDLER */
57 #endif /* !RETSIGTYPE */
59 #if defined (VOID_SIGHANDLER)
60 # define SIGHANDLER_RETURN return
62 # define SIGHANDLER_RETURN return (0)
65 /* This typedef is equivalent to the one for Function; it allows us
66 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
67 typedef RETSIGTYPE SigHandler ();
69 #if defined (HAVE_POSIX_SIGNALS)
70 typedef struct sigaction sighandler_cxt;
71 # define rl_sigaction(s, nh, oh) sigaction(s, nh, oh)
73 typedef struct { SigHandler *sa_handler; int sa_mask, sa_flags; } sighandler_cxt;
74 # define sigemptyset(m)
75 #endif /* !HAVE_POSIX_SIGNALS */
81 static SigHandler *rl_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *));
82 static void rl_maybe_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *));
84 /* Exported variables for use by applications. */
86 /* If non-zero, readline will install its own signal handlers for
87 SIGINT, SIGTERM, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */
88 int rl_catch_signals = 1;
90 /* If non-zero, readline will install a signal handler for SIGWINCH. */
92 int rl_catch_sigwinch = 1;
94 int rl_catch_sigwinch = 0; /* for the readline state struct in readline.c */
97 /* Private variables. */
98 /* If non-zero, print characters corresponding to received signals. */
101 int _rl_intr_char = 0;
102 int _rl_quit_char = 0;
103 int _rl_susp_char = 0;
105 static int signals_set_flag;
106 static int sigwinch_set_flag;
108 /* **************************************************************** */
110 /* Signal Handling */
112 /* **************************************************************** */
114 static sighandler_cxt old_int, old_term, old_alrm, old_quit;
115 #if defined (SIGTSTP)
116 static sighandler_cxt old_tstp, old_ttou, old_ttin;
118 #if defined (SIGWINCH)
119 static sighandler_cxt old_winch;
122 /* Readline signal handler functions. */
125 rl_signal_handler (sig)
128 #if defined (HAVE_POSIX_SIGNALS)
130 #else /* !HAVE_POSIX_SIGNALS */
131 # if defined (HAVE_BSD_SIGNALS)
133 # else /* !HAVE_BSD_SIGNALS */
134 sighandler_cxt dummy_cxt; /* needed for rl_set_sighandler call */
135 # endif /* !HAVE_BSD_SIGNALS */
136 #endif /* !HAVE_POSIX_SIGNALS */
138 RL_SETSTATE(RL_STATE_SIGHANDLER);
140 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
141 /* Since the signal will not be blocked while we are in the signal
142 handler, ignore it until rl_clear_signals resets the catcher. */
143 # if defined (SIGALRM)
144 if (sig == SIGINT || sig == SIGALRM)
148 rl_set_sighandler (sig, SIG_IGN, &dummy_cxt);
149 #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */
154 _rl_reset_completion_state ();
155 rl_free_line_state ();
159 #if defined (SIGTSTP)
164 #if defined (SIGALRM)
167 #if defined (SIGQUIT)
170 rl_echo_signal_char (sig);
171 rl_cleanup_after_signal ();
173 #if defined (HAVE_POSIX_SIGNALS)
175 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
176 sigdelset (&set, sig);
177 #else /* !HAVE_POSIX_SIGNALS */
178 # if defined (HAVE_BSD_SIGNALS)
179 omask = sigblock (0);
180 # endif /* HAVE_BSD_SIGNALS */
181 #endif /* !HAVE_POSIX_SIGNALS */
183 #if defined (__EMX__)
184 signal (sig, SIG_ACK);
187 #if defined (HAVE_KILL)
188 kill (getpid (), sig);
190 raise (sig); /* assume we have raise */
193 /* Let the signal that we just sent through. */
194 #if defined (HAVE_POSIX_SIGNALS)
195 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
196 #else /* !HAVE_POSIX_SIGNALS */
197 # if defined (HAVE_BSD_SIGNALS)
198 sigsetmask (omask & ~(sigmask (sig)));
199 # endif /* HAVE_BSD_SIGNALS */
200 #endif /* !HAVE_POSIX_SIGNALS */
202 rl_reset_after_signal ();
205 RL_UNSETSTATE(RL_STATE_SIGHANDLER);
209 #if defined (SIGWINCH)
211 rl_sigwinch_handler (sig)
216 #if defined (MUST_REINSTALL_SIGHANDLERS)
217 sighandler_cxt dummy_winch;
219 /* We don't want to change old_winch -- it holds the state of SIGWINCH
220 disposition set by the calling application. We need this state
221 because we call the application's SIGWINCH handler after updating
222 our own idea of the screen size. */
223 rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch);
226 RL_SETSTATE(RL_STATE_SIGHANDLER);
227 rl_resize_terminal ();
229 /* If another sigwinch handler has been installed, call it. */
230 oh = (SigHandler *)old_winch.sa_handler;
231 if (oh && oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL)
234 RL_UNSETSTATE(RL_STATE_SIGHANDLER);
237 #endif /* SIGWINCH */
239 /* Functions to manage signal handling. */
241 #if !defined (HAVE_POSIX_SIGNALS)
243 rl_sigaction (sig, nh, oh)
245 sighandler_cxt *nh, *oh;
247 oh->sa_handler = signal (sig, nh->sa_handler);
250 #endif /* !HAVE_POSIX_SIGNALS */
252 /* Set up a readline-specific signal handler, saving the old signal
253 information in OHANDLER. Return the old signal handler, like
256 rl_set_sighandler (sig, handler, ohandler)
259 sighandler_cxt *ohandler;
261 sighandler_cxt old_handler;
262 #if defined (HAVE_POSIX_SIGNALS)
263 struct sigaction act;
265 act.sa_handler = handler;
266 # if defined (SIGWINCH)
267 act.sa_flags = (sig == SIGWINCH) ? SA_RESTART : 0;
270 # endif /* SIGWINCH */
271 sigemptyset (&act.sa_mask);
272 sigemptyset (&ohandler->sa_mask);
273 sigaction (sig, &act, &old_handler);
275 old_handler.sa_handler = (SigHandler *)signal (sig, handler);
276 #endif /* !HAVE_POSIX_SIGNALS */
278 /* XXX -- assume we have memcpy */
279 /* If rl_set_signals is called twice in a row, don't set the old handler to
280 rl_signal_handler, because that would cause infinite recursion. */
281 if (handler != rl_signal_handler || old_handler.sa_handler != rl_signal_handler)
282 memcpy (ohandler, &old_handler, sizeof (sighandler_cxt));
284 return (ohandler->sa_handler);
288 rl_maybe_set_sighandler (sig, handler, ohandler)
291 sighandler_cxt *ohandler;
293 sighandler_cxt dummy;
296 sigemptyset (&dummy.sa_mask);
297 oh = rl_set_sighandler (sig, handler, ohandler);
298 if (oh == (SigHandler *)SIG_IGN)
299 rl_sigaction (sig, ohandler, &dummy);
305 sighandler_cxt dummy;
307 #if defined (HAVE_POSIX_SIGNALS)
308 static int sigmask_set = 0;
309 static sigset_t bset, oset;
312 #if defined (HAVE_POSIX_SIGNALS)
313 if (rl_catch_signals && sigmask_set == 0)
317 sigaddset (&bset, SIGINT);
318 sigaddset (&bset, SIGTERM);
319 #if defined (SIGQUIT)
320 sigaddset (&bset, SIGQUIT);
322 #if defined (SIGALRM)
323 sigaddset (&bset, SIGALRM);
325 #if defined (SIGTSTP)
326 sigaddset (&bset, SIGTSTP);
328 #if defined (SIGTTIN)
329 sigaddset (&bset, SIGTTIN);
331 #if defined (SIGTTOU)
332 sigaddset (&bset, SIGTTOU);
336 #endif /* HAVE_POSIX_SIGNALS */
338 if (rl_catch_signals && signals_set_flag == 0)
340 #if defined (HAVE_POSIX_SIGNALS)
342 sigprocmask (SIG_BLOCK, &bset, &oset);
345 rl_maybe_set_sighandler (SIGINT, rl_signal_handler, &old_int);
346 rl_maybe_set_sighandler (SIGTERM, rl_signal_handler, &old_term);
347 #if defined (SIGQUIT)
348 rl_maybe_set_sighandler (SIGQUIT, rl_signal_handler, &old_quit);
351 #if defined (SIGALRM)
352 oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm);
353 if (oh == (SigHandler *)SIG_IGN)
354 rl_sigaction (SIGALRM, &old_alrm, &dummy);
355 #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART)
356 /* If the application using readline has already installed a signal
357 handler with SA_RESTART, SIGALRM will cause reads to be restarted
358 automatically, so readline should just get out of the way. Since
359 we tested for SIG_IGN above, we can just test for SIG_DFL here. */
360 if (oh != (SigHandler *)SIG_DFL && (old_alrm.sa_flags & SA_RESTART))
361 rl_sigaction (SIGALRM, &old_alrm, &dummy);
362 #endif /* HAVE_POSIX_SIGNALS */
365 #if defined (SIGTSTP)
366 rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp);
369 #if defined (SIGTTOU)
370 rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou);
373 #if defined (SIGTTIN)
374 rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin);
377 signals_set_flag = 1;
379 #if defined (HAVE_POSIX_SIGNALS)
380 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
384 #if defined (SIGWINCH)
385 if (rl_catch_sigwinch && sigwinch_set_flag == 0)
387 rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch);
388 sigwinch_set_flag = 1;
390 #endif /* SIGWINCH */
398 sighandler_cxt dummy;
400 if (rl_catch_signals && signals_set_flag == 1)
402 sigemptyset (&dummy.sa_mask);
404 rl_sigaction (SIGINT, &old_int, &dummy);
405 rl_sigaction (SIGTERM, &old_term, &dummy);
406 #if defined (SIGQUIT)
407 rl_sigaction (SIGQUIT, &old_quit, &dummy);
409 #if defined (SIGALRM)
410 rl_sigaction (SIGALRM, &old_alrm, &dummy);
413 #if defined (SIGTSTP)
414 rl_sigaction (SIGTSTP, &old_tstp, &dummy);
417 #if defined (SIGTTOU)
418 rl_sigaction (SIGTTOU, &old_ttou, &dummy);
421 #if defined (SIGTTIN)
422 rl_sigaction (SIGTTIN, &old_ttin, &dummy);
425 signals_set_flag = 0;
428 #if defined (SIGWINCH)
429 if (rl_catch_sigwinch && sigwinch_set_flag == 1)
431 sigemptyset (&dummy.sa_mask);
432 rl_sigaction (SIGWINCH, &old_winch, &dummy);
433 sigwinch_set_flag = 0;
440 /* Clean up the terminal and readline state after catching a signal, before
441 resending it to the calling application. */
443 rl_cleanup_after_signal ()
445 _rl_clean_up_for_exit ();
446 if (rl_deprep_term_function)
447 (*rl_deprep_term_function) ();
448 rl_clear_pending_input ();
452 /* Reset the terminal and readline state after a signal handler returns. */
454 rl_reset_after_signal ()
456 if (rl_prep_term_function)
457 (*rl_prep_term_function) (_rl_meta_flag);
461 /* Free up the readline variable line state for the current line (undo list,
462 any partial history entry, any keyboard macros in progress, and any
463 numeric arguments in process) after catching a signal, before calling
464 rl_cleanup_after_signal(). */
466 rl_free_line_state ()
468 register HIST_ENTRY *entry;
470 rl_free_undo_list ();
472 entry = current_history ();
474 entry->data = (char *)NULL;
476 _rl_kill_kbd_macro ();
478 _rl_reset_argument ();
481 #endif /* HANDLE_SIGNALS */
483 /* **************************************************************** */
485 /* SIGINT Management */
487 /* **************************************************************** */
489 #if defined (HAVE_POSIX_SIGNALS)
490 static sigset_t sigint_set, sigint_oset;
491 #else /* !HAVE_POSIX_SIGNALS */
492 # if defined (HAVE_BSD_SIGNALS)
493 static int sigint_oldmask;
494 # endif /* HAVE_BSD_SIGNALS */
495 #endif /* !HAVE_POSIX_SIGNALS */
497 static int sigint_blocked;
499 /* Cause SIGINT to not be delivered until the corresponding call to
507 #if defined (HAVE_POSIX_SIGNALS)
508 sigemptyset (&sigint_set);
509 sigemptyset (&sigint_oset);
510 sigaddset (&sigint_set, SIGINT);
511 sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset);
512 #else /* !HAVE_POSIX_SIGNALS */
513 # if defined (HAVE_BSD_SIGNALS)
514 sigint_oldmask = sigblock (sigmask (SIGINT));
515 # else /* !HAVE_BSD_SIGNALS */
516 # if defined (HAVE_USG_SIGHOLD)
518 # endif /* HAVE_USG_SIGHOLD */
519 # endif /* !HAVE_BSD_SIGNALS */
520 #endif /* !HAVE_POSIX_SIGNALS */
525 /* Allow SIGINT to be delivered. */
527 _rl_release_sigint ()
529 if (sigint_blocked == 0)
532 #if defined (HAVE_POSIX_SIGNALS)
533 sigprocmask (SIG_SETMASK, &sigint_oset, (sigset_t *)NULL);
535 # if defined (HAVE_BSD_SIGNALS)
536 sigsetmask (sigint_oldmask);
537 # else /* !HAVE_BSD_SIGNALS */
538 # if defined (HAVE_USG_SIGHOLD)
540 # endif /* HAVE_USG_SIGHOLD */
541 # endif /* !HAVE_BSD_SIGNALS */
542 #endif /* !HAVE_POSIX_SIGNALS */
547 /* **************************************************************** */
549 /* Echoing special control characters */
551 /* **************************************************************** */
553 rl_echo_signal_char (sig)
559 if (_rl_echoctl == 0)
564 case SIGINT: c = _rl_intr_char; break;
565 case SIGQUIT: c = _rl_quit_char; break;
566 case SIGTSTP: c = _rl_susp_char; break;
570 if (CTRL_CHAR (c) || c == RUBOUT)
573 cstr[1] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
574 cstr[cslen = 2] = '\0';
579 cstr[cslen = 1] = '\0';
582 _rl_output_some_chars (cstr, cslen);