Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* signals.c -- signal handling support for readline. */ |
| 2 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1987-2011 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | GNU General Public License for more details. |
| 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 22 | #define READLINE_LIBRARY |
| 23 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 24 | #if defined (HAVE_CONFIG_H) |
| 25 | # include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <stdio.h> /* Just for NULL. Yuck. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | |
| 32 | #if defined (HAVE_UNISTD_H) |
| 33 | # include <unistd.h> |
| 34 | #endif /* HAVE_UNISTD_H */ |
| 35 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
| 43 | /* Some standard library routines. */ |
| 44 | #include "readline.h" |
| 45 | #include "history.h" |
| 46 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 47 | #include "rlprivate.h" |
| 48 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 49 | #if defined (HANDLE_SIGNALS) |
| 50 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 58 | |
| 59 | #if defined (VOID_SIGHANDLER) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 60 | # define SIGHANDLER_RETURN return |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 61 | #else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 62 | # define SIGHANDLER_RETURN return (0) |
| 63 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 64 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 65 | /* This typedef is equivalent to the one for Function; it allows us |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 66 | to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 67 | typedef RETSIGTYPE SigHandler (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 68 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 76 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 77 | #ifndef SA_RESTART |
| 78 | # define SA_RESTART 0 |
| 79 | #endif |
| 80 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 81 | static SigHandler *rl_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 82 | static void rl_maybe_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *)); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 83 | static void rl_maybe_restore_sighandler PARAMS((int, sighandler_cxt *)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 84 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 85 | static RETSIGTYPE rl_signal_handler PARAMS((int)); |
| 86 | static RETSIGTYPE _rl_handle_signal PARAMS((int)); |
| 87 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 88 | /* Exported variables for use by applications. */ |
| 89 | |
| 90 | /* If non-zero, readline will install its own signal handlers for |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 91 | SIGINT, SIGTERM, SIGHUP, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 92 | int rl_catch_signals = 1; |
| 93 | |
| 94 | /* If non-zero, readline will install a signal handler for SIGWINCH. */ |
| 95 | #ifdef SIGWINCH |
| 96 | int rl_catch_sigwinch = 1; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 97 | #else |
| 98 | int rl_catch_sigwinch = 0; /* for the readline state struct in readline.c */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 101 | /* Private variables. */ |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 102 | int _rl_interrupt_immediately = 0; |
| 103 | int volatile _rl_caught_signal = 0; /* should be sig_atomic_t, but that requires including <signal.h> everywhere */ |
| 104 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 105 | /* If non-zero, print characters corresponding to received signals as long as |
| 106 | the user has indicated his desire to do so (_rl_echo_control_chars). */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 107 | int _rl_echoctl = 0; |
| 108 | |
| 109 | int _rl_intr_char = 0; |
| 110 | int _rl_quit_char = 0; |
| 111 | int _rl_susp_char = 0; |
| 112 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 113 | static int signals_set_flag; |
| 114 | static int sigwinch_set_flag; |
| 115 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 116 | /* **************************************************************** */ |
| 117 | /* */ |
| 118 | /* Signal Handling */ |
| 119 | /* */ |
| 120 | /* **************************************************************** */ |
| 121 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 122 | static sighandler_cxt old_int, old_term, old_hup, old_alrm, old_quit; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 123 | #if defined (SIGTSTP) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 124 | static sighandler_cxt old_tstp, old_ttou, old_ttin; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 125 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 126 | #if defined (SIGWINCH) |
| 127 | static sighandler_cxt old_winch; |
| 128 | #endif |
| 129 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 130 | _rl_sigcleanup_func_t *_rl_sigcleanup; |
| 131 | void *_rl_sigcleanarg; |
| 132 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 133 | /* Readline signal handler functions. */ |
| 134 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 135 | /* Called from RL_CHECK_SIGNALS() macro */ |
| 136 | RETSIGTYPE |
| 137 | _rl_signal_handler (sig) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 138 | int sig; |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 139 | { |
| 140 | _rl_caught_signal = 0; /* XXX */ |
| 141 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 142 | #if defined (SIGWINCH) |
| 143 | if (sig == SIGWINCH) |
| 144 | { |
| 145 | rl_resize_terminal (); |
| 146 | /* XXX - experimental for now */ |
| 147 | /* Call a signal hook because though we called the original signal handler |
| 148 | in rl_sigwinch_handler below, we will not resend the signal to |
| 149 | ourselves. */ |
| 150 | if (rl_signal_event_hook) |
| 151 | (*rl_signal_event_hook) (); |
| 152 | } |
| 153 | else |
| 154 | #endif |
| 155 | _rl_handle_signal (sig); |
| 156 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 157 | SIGHANDLER_RETURN; |
| 158 | } |
| 159 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 160 | static RETSIGTYPE |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 161 | rl_signal_handler (sig) |
| 162 | int sig; |
| 163 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 164 | if (_rl_interrupt_immediately) |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 165 | { |
| 166 | _rl_interrupt_immediately = 0; |
| 167 | _rl_handle_signal (sig); |
| 168 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 169 | else |
| 170 | _rl_caught_signal = sig; |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 171 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 172 | SIGHANDLER_RETURN; |
| 173 | } |
| 174 | |
| 175 | static RETSIGTYPE |
| 176 | _rl_handle_signal (sig) |
| 177 | int sig; |
| 178 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 179 | #if defined (HAVE_POSIX_SIGNALS) |
| 180 | sigset_t set; |
| 181 | #else /* !HAVE_POSIX_SIGNALS */ |
| 182 | # if defined (HAVE_BSD_SIGNALS) |
| 183 | long omask; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 184 | # else /* !HAVE_BSD_SIGNALS */ |
| 185 | sighandler_cxt dummy_cxt; /* needed for rl_set_sighandler call */ |
| 186 | # endif /* !HAVE_BSD_SIGNALS */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 187 | #endif /* !HAVE_POSIX_SIGNALS */ |
| 188 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 189 | RL_SETSTATE(RL_STATE_SIGHANDLER); |
| 190 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 191 | #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS) |
| 192 | /* Since the signal will not be blocked while we are in the signal |
| 193 | handler, ignore it until rl_clear_signals resets the catcher. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 194 | # if defined (SIGALRM) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 195 | if (sig == SIGINT || sig == SIGALRM) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 196 | # else |
| 197 | if (sig == SIGINT) |
| 198 | # endif |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 199 | rl_set_sighandler (sig, SIG_IGN, &dummy_cxt); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 200 | #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 201 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 202 | /* If there's a sig cleanup function registered, call it and `deregister' |
| 203 | the cleanup function to avoid multiple calls */ |
| 204 | if (_rl_sigcleanup) |
| 205 | { |
| 206 | (*_rl_sigcleanup) (sig, _rl_sigcleanarg); |
| 207 | _rl_sigcleanup = 0; |
| 208 | _rl_sigcleanarg = 0; |
| 209 | } |
| 210 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 211 | switch (sig) |
| 212 | { |
| 213 | case SIGINT: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 214 | _rl_reset_completion_state (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 215 | rl_free_line_state (); |
| 216 | /* FALLTHROUGH */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 217 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 218 | case SIGTERM: |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 219 | case SIGHUP: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 220 | #if defined (SIGTSTP) |
| 221 | case SIGTSTP: |
| 222 | case SIGTTOU: |
| 223 | case SIGTTIN: |
| 224 | #endif /* SIGTSTP */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 225 | #if defined (SIGALRM) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 226 | case SIGALRM: |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #if defined (SIGQUIT) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 229 | case SIGQUIT: |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 230 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 231 | rl_echo_signal_char (sig); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 232 | rl_cleanup_after_signal (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 233 | |
| 234 | #if defined (HAVE_POSIX_SIGNALS) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 235 | sigemptyset (&set); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 236 | sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set); |
| 237 | sigdelset (&set, sig); |
| 238 | #else /* !HAVE_POSIX_SIGNALS */ |
| 239 | # if defined (HAVE_BSD_SIGNALS) |
| 240 | omask = sigblock (0); |
| 241 | # endif /* HAVE_BSD_SIGNALS */ |
| 242 | #endif /* !HAVE_POSIX_SIGNALS */ |
| 243 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 244 | #if defined (__EMX__) |
| 245 | signal (sig, SIG_ACK); |
| 246 | #endif |
| 247 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 248 | #if defined (HAVE_KILL) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 249 | kill (getpid (), sig); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 250 | #else |
| 251 | raise (sig); /* assume we have raise */ |
| 252 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 253 | |
| 254 | /* Let the signal that we just sent through. */ |
| 255 | #if defined (HAVE_POSIX_SIGNALS) |
| 256 | sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL); |
| 257 | #else /* !HAVE_POSIX_SIGNALS */ |
| 258 | # if defined (HAVE_BSD_SIGNALS) |
| 259 | sigsetmask (omask & ~(sigmask (sig))); |
| 260 | # endif /* HAVE_BSD_SIGNALS */ |
| 261 | #endif /* !HAVE_POSIX_SIGNALS */ |
| 262 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 263 | rl_reset_after_signal (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 266 | RL_UNSETSTATE(RL_STATE_SIGHANDLER); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 267 | SIGHANDLER_RETURN; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 270 | #if defined (SIGWINCH) |
| 271 | static RETSIGTYPE |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 272 | rl_sigwinch_handler (sig) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 273 | int sig; |
| 274 | { |
| 275 | SigHandler *oh; |
| 276 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 277 | #if defined (MUST_REINSTALL_SIGHANDLERS) |
| 278 | sighandler_cxt dummy_winch; |
| 279 | |
| 280 | /* We don't want to change old_winch -- it holds the state of SIGWINCH |
| 281 | disposition set by the calling application. We need this state |
| 282 | because we call the application's SIGWINCH handler after updating |
| 283 | our own idea of the screen size. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 284 | rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 285 | #endif |
| 286 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 287 | RL_SETSTATE(RL_STATE_SIGHANDLER); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 288 | _rl_caught_signal = sig; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 289 | |
| 290 | /* If another sigwinch handler has been installed, call it. */ |
| 291 | oh = (SigHandler *)old_winch.sa_handler; |
| 292 | if (oh && oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL) |
| 293 | (*oh) (sig); |
| 294 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 295 | RL_UNSETSTATE(RL_STATE_SIGHANDLER); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 296 | SIGHANDLER_RETURN; |
| 297 | } |
| 298 | #endif /* SIGWINCH */ |
| 299 | |
| 300 | /* Functions to manage signal handling. */ |
| 301 | |
| 302 | #if !defined (HAVE_POSIX_SIGNALS) |
| 303 | static int |
| 304 | rl_sigaction (sig, nh, oh) |
| 305 | int sig; |
| 306 | sighandler_cxt *nh, *oh; |
| 307 | { |
| 308 | oh->sa_handler = signal (sig, nh->sa_handler); |
| 309 | return 0; |
| 310 | } |
| 311 | #endif /* !HAVE_POSIX_SIGNALS */ |
| 312 | |
| 313 | /* Set up a readline-specific signal handler, saving the old signal |
| 314 | information in OHANDLER. Return the old signal handler, like |
| 315 | signal(). */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 316 | static SigHandler * |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 317 | rl_set_sighandler (sig, handler, ohandler) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 318 | int sig; |
| 319 | SigHandler *handler; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 320 | sighandler_cxt *ohandler; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 321 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 322 | sighandler_cxt old_handler; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 323 | #if defined (HAVE_POSIX_SIGNALS) |
| 324 | struct sigaction act; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 325 | |
| 326 | act.sa_handler = handler; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 327 | # if defined (SIGWINCH) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 328 | act.sa_flags = (sig == SIGWINCH) ? SA_RESTART : 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 329 | # else |
| 330 | act.sa_flags = 0; |
| 331 | # endif /* SIGWINCH */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 332 | sigemptyset (&act.sa_mask); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 333 | sigemptyset (&ohandler->sa_mask); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 334 | sigaction (sig, &act, &old_handler); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 335 | #else |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 336 | old_handler.sa_handler = (SigHandler *)signal (sig, handler); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 337 | #endif /* !HAVE_POSIX_SIGNALS */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 338 | |
| 339 | /* XXX -- assume we have memcpy */ |
| 340 | /* If rl_set_signals is called twice in a row, don't set the old handler to |
| 341 | rl_signal_handler, because that would cause infinite recursion. */ |
| 342 | if (handler != rl_signal_handler || old_handler.sa_handler != rl_signal_handler) |
| 343 | memcpy (ohandler, &old_handler, sizeof (sighandler_cxt)); |
| 344 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 345 | return (ohandler->sa_handler); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 348 | /* Set disposition of SIG to HANDLER, returning old state in OHANDLER. Don't |
| 349 | change disposition if OHANDLER indicates the signal was ignored. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 350 | static void |
| 351 | rl_maybe_set_sighandler (sig, handler, ohandler) |
| 352 | int sig; |
| 353 | SigHandler *handler; |
| 354 | sighandler_cxt *ohandler; |
| 355 | { |
| 356 | sighandler_cxt dummy; |
| 357 | SigHandler *oh; |
| 358 | |
| 359 | sigemptyset (&dummy.sa_mask); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 360 | dummy.sa_flags = 0; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 361 | oh = rl_set_sighandler (sig, handler, ohandler); |
| 362 | if (oh == (SigHandler *)SIG_IGN) |
| 363 | rl_sigaction (sig, ohandler, &dummy); |
| 364 | } |
| 365 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 366 | /* Set the disposition of SIG to HANDLER, if HANDLER->sa_handler indicates the |
| 367 | signal was not being ignored. MUST only be called for signals whose |
| 368 | disposition was changed using rl_maybe_set_sighandler or for which the |
| 369 | SIG_IGN check was performed inline (e.g., SIGALRM below). */ |
| 370 | static void |
| 371 | rl_maybe_restore_sighandler (sig, handler) |
| 372 | int sig; |
| 373 | sighandler_cxt *handler; |
| 374 | { |
| 375 | sighandler_cxt dummy; |
| 376 | |
| 377 | sigemptyset (&dummy.sa_mask); |
| 378 | dummy.sa_flags = 0; |
| 379 | if (handler->sa_handler != SIG_IGN) |
| 380 | rl_sigaction (sig, handler, &dummy); |
| 381 | } |
| 382 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 383 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 384 | rl_set_signals () |
| 385 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 386 | sighandler_cxt dummy; |
| 387 | SigHandler *oh; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 388 | #if defined (HAVE_POSIX_SIGNALS) |
| 389 | static int sigmask_set = 0; |
| 390 | static sigset_t bset, oset; |
| 391 | #endif |
| 392 | |
| 393 | #if defined (HAVE_POSIX_SIGNALS) |
| 394 | if (rl_catch_signals && sigmask_set == 0) |
| 395 | { |
| 396 | sigemptyset (&bset); |
| 397 | |
| 398 | sigaddset (&bset, SIGINT); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 399 | sigaddset (&bset, SIGTERM); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 400 | sigaddset (&bset, SIGHUP); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 401 | #if defined (SIGQUIT) |
| 402 | sigaddset (&bset, SIGQUIT); |
| 403 | #endif |
| 404 | #if defined (SIGALRM) |
| 405 | sigaddset (&bset, SIGALRM); |
| 406 | #endif |
| 407 | #if defined (SIGTSTP) |
| 408 | sigaddset (&bset, SIGTSTP); |
| 409 | #endif |
| 410 | #if defined (SIGTTIN) |
| 411 | sigaddset (&bset, SIGTTIN); |
| 412 | #endif |
| 413 | #if defined (SIGTTOU) |
| 414 | sigaddset (&bset, SIGTTOU); |
| 415 | #endif |
| 416 | sigmask_set = 1; |
| 417 | } |
| 418 | #endif /* HAVE_POSIX_SIGNALS */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 419 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 420 | if (rl_catch_signals && signals_set_flag == 0) |
| 421 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 422 | #if defined (HAVE_POSIX_SIGNALS) |
| 423 | sigemptyset (&oset); |
| 424 | sigprocmask (SIG_BLOCK, &bset, &oset); |
| 425 | #endif |
| 426 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 427 | rl_maybe_set_sighandler (SIGINT, rl_signal_handler, &old_int); |
| 428 | rl_maybe_set_sighandler (SIGTERM, rl_signal_handler, &old_term); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 429 | rl_maybe_set_sighandler (SIGHUP, rl_signal_handler, &old_hup); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 430 | #if defined (SIGQUIT) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 431 | rl_maybe_set_sighandler (SIGQUIT, rl_signal_handler, &old_quit); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 432 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 433 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 434 | #if defined (SIGALRM) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 435 | oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm); |
| 436 | if (oh == (SigHandler *)SIG_IGN) |
| 437 | rl_sigaction (SIGALRM, &old_alrm, &dummy); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 438 | #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 439 | /* If the application using readline has already installed a signal |
| 440 | handler with SA_RESTART, SIGALRM will cause reads to be restarted |
| 441 | automatically, so readline should just get out of the way. Since |
| 442 | we tested for SIG_IGN above, we can just test for SIG_DFL here. */ |
| 443 | if (oh != (SigHandler *)SIG_DFL && (old_alrm.sa_flags & SA_RESTART)) |
| 444 | rl_sigaction (SIGALRM, &old_alrm, &dummy); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 445 | #endif /* HAVE_POSIX_SIGNALS */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 446 | #endif /* SIGALRM */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 447 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 448 | #if defined (SIGTSTP) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 449 | rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 450 | #endif /* SIGTSTP */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 451 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 452 | #if defined (SIGTTOU) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 453 | rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 454 | #endif /* SIGTTOU */ |
| 455 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 456 | #if defined (SIGTTIN) |
| 457 | rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin); |
| 458 | #endif /* SIGTTIN */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 459 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 460 | signals_set_flag = 1; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 461 | |
| 462 | #if defined (HAVE_POSIX_SIGNALS) |
| 463 | sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL); |
| 464 | #endif |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 465 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 466 | |
| 467 | #if defined (SIGWINCH) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 468 | if (rl_catch_sigwinch && sigwinch_set_flag == 0) |
| 469 | { |
| 470 | rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch); |
| 471 | sigwinch_set_flag = 1; |
| 472 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 473 | #endif /* SIGWINCH */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 474 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 478 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 479 | rl_clear_signals () |
| 480 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 481 | sighandler_cxt dummy; |
| 482 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 483 | if (rl_catch_signals && signals_set_flag == 1) |
| 484 | { |
| 485 | sigemptyset (&dummy.sa_mask); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 486 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 487 | /* Since rl_maybe_set_sighandler doesn't override a SIG_IGN handler, |
| 488 | we should in theory not have to restore a handler where |
| 489 | old_xxx.sa_handler == SIG_IGN. That's what rl_maybe_restore_sighandler |
| 490 | does. Fewer system calls should reduce readline's per-line |
| 491 | overhead */ |
| 492 | rl_maybe_restore_sighandler (SIGINT, &old_int); |
| 493 | rl_maybe_restore_sighandler (SIGTERM, &old_term); |
| 494 | rl_maybe_restore_sighandler (SIGHUP, &old_hup); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 495 | #if defined (SIGQUIT) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 496 | rl_maybe_restore_sighandler (SIGQUIT, &old_quit); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 497 | #endif |
| 498 | #if defined (SIGALRM) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 499 | rl_maybe_restore_sighandler (SIGALRM, &old_alrm); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 500 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 501 | |
| 502 | #if defined (SIGTSTP) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 503 | rl_maybe_restore_sighandler (SIGTSTP, &old_tstp); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 504 | #endif /* SIGTSTP */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 505 | |
| 506 | #if defined (SIGTTOU) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 507 | rl_maybe_restore_sighandler (SIGTTOU, &old_ttou); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 508 | #endif /* SIGTTOU */ |
| 509 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 510 | #if defined (SIGTTIN) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 511 | rl_maybe_restore_sighandler (SIGTTIN, &old_ttin); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 512 | #endif /* SIGTTIN */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 513 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 514 | signals_set_flag = 0; |
| 515 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 516 | |
| 517 | #if defined (SIGWINCH) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 518 | if (rl_catch_sigwinch && sigwinch_set_flag == 1) |
| 519 | { |
| 520 | sigemptyset (&dummy.sa_mask); |
| 521 | rl_sigaction (SIGWINCH, &old_winch, &dummy); |
| 522 | sigwinch_set_flag = 0; |
| 523 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 524 | #endif |
| 525 | |
| 526 | return 0; |
| 527 | } |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 528 | |
| 529 | /* Clean up the terminal and readline state after catching a signal, before |
| 530 | resending it to the calling application. */ |
| 531 | void |
| 532 | rl_cleanup_after_signal () |
| 533 | { |
| 534 | _rl_clean_up_for_exit (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 535 | if (rl_deprep_term_function) |
| 536 | (*rl_deprep_term_function) (); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 537 | rl_clear_pending_input (); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 538 | rl_clear_signals (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* Reset the terminal and readline state after a signal handler returns. */ |
| 542 | void |
| 543 | rl_reset_after_signal () |
| 544 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 545 | if (rl_prep_term_function) |
| 546 | (*rl_prep_term_function) (_rl_meta_flag); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 547 | rl_set_signals (); |
| 548 | } |
| 549 | |
| 550 | /* Free up the readline variable line state for the current line (undo list, |
| 551 | any partial history entry, any keyboard macros in progress, and any |
| 552 | numeric arguments in process) after catching a signal, before calling |
| 553 | rl_cleanup_after_signal(). */ |
| 554 | void |
| 555 | rl_free_line_state () |
| 556 | { |
| 557 | register HIST_ENTRY *entry; |
| 558 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 559 | rl_free_undo_list (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 560 | |
| 561 | entry = current_history (); |
| 562 | if (entry) |
| 563 | entry->data = (char *)NULL; |
| 564 | |
| 565 | _rl_kill_kbd_macro (); |
| 566 | rl_clear_message (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 567 | _rl_reset_argument (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 570 | #endif /* HANDLE_SIGNALS */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 571 | |
| 572 | /* **************************************************************** */ |
| 573 | /* */ |
| 574 | /* SIGINT Management */ |
| 575 | /* */ |
| 576 | /* **************************************************************** */ |
| 577 | |
| 578 | #if defined (HAVE_POSIX_SIGNALS) |
| 579 | static sigset_t sigint_set, sigint_oset; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 580 | static sigset_t sigwinch_set, sigwinch_oset; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 581 | #else /* !HAVE_POSIX_SIGNALS */ |
| 582 | # if defined (HAVE_BSD_SIGNALS) |
| 583 | static int sigint_oldmask; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 584 | static int sigwinch_oldmask; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 585 | # endif /* HAVE_BSD_SIGNALS */ |
| 586 | #endif /* !HAVE_POSIX_SIGNALS */ |
| 587 | |
| 588 | static int sigint_blocked; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 589 | static int sigwinch_blocked; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 590 | |
| 591 | /* Cause SIGINT to not be delivered until the corresponding call to |
| 592 | release_sigint(). */ |
| 593 | void |
| 594 | _rl_block_sigint () |
| 595 | { |
| 596 | if (sigint_blocked) |
| 597 | return; |
| 598 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 599 | sigint_blocked = 1; |
| 600 | } |
| 601 | |
| 602 | /* Allow SIGINT to be delivered. */ |
| 603 | void |
| 604 | _rl_release_sigint () |
| 605 | { |
| 606 | if (sigint_blocked == 0) |
| 607 | return; |
| 608 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 609 | sigint_blocked = 0; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 610 | RL_CHECK_SIGNALS (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 613 | /* Cause SIGWINCH to not be delivered until the corresponding call to |
| 614 | release_sigwinch(). */ |
| 615 | void |
| 616 | _rl_block_sigwinch () |
| 617 | { |
| 618 | if (sigwinch_blocked) |
| 619 | return; |
| 620 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 621 | #if defined (SIGWINCH) |
| 622 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 623 | #if defined (HAVE_POSIX_SIGNALS) |
| 624 | sigemptyset (&sigwinch_set); |
| 625 | sigemptyset (&sigwinch_oset); |
| 626 | sigaddset (&sigwinch_set, SIGWINCH); |
| 627 | sigprocmask (SIG_BLOCK, &sigwinch_set, &sigwinch_oset); |
| 628 | #else /* !HAVE_POSIX_SIGNALS */ |
| 629 | # if defined (HAVE_BSD_SIGNALS) |
| 630 | sigwinch_oldmask = sigblock (sigmask (SIGWINCH)); |
| 631 | # else /* !HAVE_BSD_SIGNALS */ |
| 632 | # if defined (HAVE_USG_SIGHOLD) |
| 633 | sighold (SIGWINCH); |
| 634 | # endif /* HAVE_USG_SIGHOLD */ |
| 635 | # endif /* !HAVE_BSD_SIGNALS */ |
| 636 | #endif /* !HAVE_POSIX_SIGNALS */ |
| 637 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 638 | #endif /* SIGWINCH */ |
| 639 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 640 | sigwinch_blocked = 1; |
| 641 | } |
| 642 | |
| 643 | /* Allow SIGWINCH to be delivered. */ |
| 644 | void |
| 645 | _rl_release_sigwinch () |
| 646 | { |
| 647 | if (sigwinch_blocked == 0) |
| 648 | return; |
| 649 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 650 | #if defined (SIGWINCH) |
| 651 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 652 | #if defined (HAVE_POSIX_SIGNALS) |
| 653 | sigprocmask (SIG_SETMASK, &sigwinch_oset, (sigset_t *)NULL); |
| 654 | #else |
| 655 | # if defined (HAVE_BSD_SIGNALS) |
| 656 | sigsetmask (sigwinch_oldmask); |
| 657 | # else /* !HAVE_BSD_SIGNALS */ |
| 658 | # if defined (HAVE_USG_SIGHOLD) |
| 659 | sigrelse (SIGWINCH); |
| 660 | # endif /* HAVE_USG_SIGHOLD */ |
| 661 | # endif /* !HAVE_BSD_SIGNALS */ |
| 662 | #endif /* !HAVE_POSIX_SIGNALS */ |
| 663 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 664 | #endif /* SIGWINCH */ |
| 665 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 666 | sigwinch_blocked = 0; |
| 667 | } |
| 668 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 669 | /* **************************************************************** */ |
| 670 | /* */ |
| 671 | /* Echoing special control characters */ |
| 672 | /* */ |
| 673 | /* **************************************************************** */ |
| 674 | void |
| 675 | rl_echo_signal_char (sig) |
| 676 | int sig; |
| 677 | { |
| 678 | char cstr[3]; |
| 679 | int cslen, c; |
| 680 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 681 | if (_rl_echoctl == 0 || _rl_echo_control_chars == 0) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 682 | return; |
| 683 | |
| 684 | switch (sig) |
| 685 | { |
| 686 | case SIGINT: c = _rl_intr_char; break; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 687 | #if defined (SIGQUIT) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 688 | case SIGQUIT: c = _rl_quit_char; break; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 689 | #endif |
| 690 | #if defined (SIGTSTP) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 691 | case SIGTSTP: c = _rl_susp_char; break; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 692 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 693 | default: return; |
| 694 | } |
| 695 | |
| 696 | if (CTRL_CHAR (c) || c == RUBOUT) |
| 697 | { |
| 698 | cstr[0] = '^'; |
| 699 | cstr[1] = CTRL_CHAR (c) ? UNCTRL (c) : '?'; |
| 700 | cstr[cslen = 2] = '\0'; |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | cstr[0] = c; |
| 705 | cstr[cslen = 1] = '\0'; |
| 706 | } |
| 707 | |
| 708 | _rl_output_some_chars (cstr, cslen); |
| 709 | } |