]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/signals.c
861019d336302b0d7bf518d82eee5ad07fd92fd3
[thirdparty/bash.git] / lib / readline / signals.c
1 /* signals.c -- signal handling support for readline. */
2
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
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
10 as published by the Free Software Foundation; either version 2, or
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,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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
43 #if defined (HANDLE_SIGNALS)
44 /* Some standard library routines. */
45 #include "readline.h"
46 #include "history.h"
47
48 #include "rlprivate.h"
49
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
64 /* This typedef is equivalant to the one for Function; it allows us
65 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
66 typedef RETSIGTYPE SigHandler ();
67
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 */
75
76 static SigHandler *rl_set_sighandler __P((int, SigHandler *, sighandler_cxt *));
77
78 /* Exported variables for use by applications. */
79
80 /* If non-zero, readline will install its own signal handlers for
81 SIGINT, SIGTERM, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */
82 int rl_catch_signals = 1;
83
84 /* If non-zero, readline will install a signal handler for SIGWINCH. */
85 #ifdef SIGWINCH
86 int rl_catch_sigwinch = 1;
87 #endif
88
89 static int signals_set_flag;
90 static int sigwinch_set_flag;
91
92 /* **************************************************************** */
93 /* */
94 /* Signal Handling */
95 /* */
96 /* **************************************************************** */
97
98 static sighandler_cxt old_int, old_term, old_alrm, old_quit;
99 #if defined (SIGTSTP)
100 static sighandler_cxt old_tstp, old_ttou, old_ttin;
101 #endif
102 #if defined (SIGWINCH)
103 static sighandler_cxt old_winch;
104 #endif
105
106 /* Readline signal handler functions. */
107
108 static RETSIGTYPE
109 rl_signal_handler (sig)
110 int sig;
111 {
112 #if defined (HAVE_POSIX_SIGNALS)
113 sigset_t set;
114 #else /* !HAVE_POSIX_SIGNALS */
115 # if defined (HAVE_BSD_SIGNALS)
116 long omask;
117 # else /* !HAVE_BSD_SIGNALS */
118 sighandler_cxt dummy_cxt; /* needed for rl_set_sighandler call */
119 # endif /* !HAVE_BSD_SIGNALS */
120 #endif /* !HAVE_POSIX_SIGNALS */
121
122 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
123 /* Since the signal will not be blocked while we are in the signal
124 handler, ignore it until rl_clear_signals resets the catcher. */
125 if (sig == SIGINT || sig == SIGALRM)
126 rl_set_sighandler (sig, SIG_IGN, &dummy_cxt);
127 #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */
128
129 switch (sig)
130 {
131 case SIGINT:
132 rl_free_line_state ();
133 /* FALLTHROUGH */
134
135 #if defined (SIGTSTP)
136 case SIGTSTP:
137 case SIGTTOU:
138 case SIGTTIN:
139 #endif /* SIGTSTP */
140 case SIGALRM:
141 case SIGTERM:
142 case SIGQUIT:
143 rl_cleanup_after_signal ();
144
145 #if defined (HAVE_POSIX_SIGNALS)
146 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
147 sigdelset (&set, sig);
148 #else /* !HAVE_POSIX_SIGNALS */
149 # if defined (HAVE_BSD_SIGNALS)
150 omask = sigblock (0);
151 # endif /* HAVE_BSD_SIGNALS */
152 #endif /* !HAVE_POSIX_SIGNALS */
153
154 #if defined (__EMX__)
155 signal (sig, SIG_ACK);
156 #endif
157
158 kill (getpid (), sig);
159
160 /* Let the signal that we just sent through. */
161 #if defined (HAVE_POSIX_SIGNALS)
162 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
163 #else /* !HAVE_POSIX_SIGNALS */
164 # if defined (HAVE_BSD_SIGNALS)
165 sigsetmask (omask & ~(sigmask (sig)));
166 # endif /* HAVE_BSD_SIGNALS */
167 #endif /* !HAVE_POSIX_SIGNALS */
168
169 rl_reset_after_signal ();
170 }
171
172 SIGHANDLER_RETURN;
173 }
174
175 #if defined (SIGWINCH)
176 static RETSIGTYPE
177 rl_sigwinch_handler (sig)
178 int sig;
179 {
180 SigHandler *oh;
181
182 #if defined (MUST_REINSTALL_SIGHANDLERS)
183 sighandler_cxt dummy_winch;
184
185 /* We don't want to change old_winch -- it holds the state of SIGWINCH
186 disposition set by the calling application. We need this state
187 because we call the application's SIGWINCH handler after updating
188 our own idea of the screen size. */
189 rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch);
190 #endif
191
192 rl_resize_terminal ();
193
194 /* If another sigwinch handler has been installed, call it. */
195 oh = (SigHandler *)old_winch.sa_handler;
196 if (oh && oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL)
197 (*oh) (sig);
198
199 SIGHANDLER_RETURN;
200 }
201 #endif /* SIGWINCH */
202
203 /* Functions to manage signal handling. */
204
205 #if !defined (HAVE_POSIX_SIGNALS)
206 static int
207 rl_sigaction (sig, nh, oh)
208 int sig;
209 sighandler_cxt *nh, *oh;
210 {
211 oh->sa_handler = signal (sig, nh->sa_handler);
212 return 0;
213 }
214 #endif /* !HAVE_POSIX_SIGNALS */
215
216 /* Set up a readline-specific signal handler, saving the old signal
217 information in OHANDLER. Return the old signal handler, like
218 signal(). */
219 static SigHandler *
220 rl_set_sighandler (sig, handler, ohandler)
221 int sig;
222 SigHandler *handler;
223 sighandler_cxt *ohandler;
224 {
225 sighandler_cxt old_handler;
226 #if defined (HAVE_POSIX_SIGNALS)
227 struct sigaction act;
228
229 act.sa_handler = handler;
230 act.sa_flags = 0;
231 sigemptyset (&act.sa_mask);
232 sigemptyset (&ohandler->sa_mask);
233 sigaction (sig, &act, &old_handler);
234 #else
235 old_handler.sa_handler = (SigHandler *)signal (sig, handler);
236 #endif /* !HAVE_POSIX_SIGNALS */
237
238 /* XXX -- assume we have memcpy */
239 /* If rl_set_signals is called twice in a row, don't set the old handler to
240 rl_signal_handler, because that would cause infinite recursion. */
241 if (handler != rl_signal_handler || old_handler.sa_handler != rl_signal_handler)
242 memcpy (ohandler, &old_handler, sizeof (sighandler_cxt));
243
244 return (ohandler->sa_handler);
245 }
246
247 static void
248 rl_maybe_set_sighandler (sig, handler, ohandler)
249 int sig;
250 SigHandler *handler;
251 sighandler_cxt *ohandler;
252 {
253 sighandler_cxt dummy;
254 SigHandler *oh;
255
256 sigemptyset (&dummy.sa_mask);
257 oh = rl_set_sighandler (sig, handler, ohandler);
258 if (oh == (SigHandler *)SIG_IGN)
259 rl_sigaction (sig, ohandler, &dummy);
260 }
261
262 int
263 rl_set_signals ()
264 {
265 sighandler_cxt dummy;
266 SigHandler *oh;
267
268 if (rl_catch_signals && signals_set_flag == 0)
269 {
270 rl_maybe_set_sighandler (SIGINT, rl_signal_handler, &old_int);
271 rl_maybe_set_sighandler (SIGTERM, rl_signal_handler, &old_term);
272 rl_maybe_set_sighandler (SIGQUIT, rl_signal_handler, &old_quit);
273
274 oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm);
275 if (oh == (SigHandler *)SIG_IGN)
276 rl_sigaction (SIGALRM, &old_alrm, &dummy);
277 #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART)
278 /* If the application using readline has already installed a signal
279 handler with SA_RESTART, SIGALRM will cause reads to be restarted
280 automatically, so readline should just get out of the way. Since
281 we tested for SIG_IGN above, we can just test for SIG_DFL here. */
282 if (oh != (SigHandler *)SIG_DFL && (old_alrm.sa_flags & SA_RESTART))
283 rl_sigaction (SIGALRM, &old_alrm, &dummy);
284 #endif /* HAVE_POSIX_SIGNALS */
285
286 #if defined (SIGTSTP)
287 rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp);
288 #endif /* SIGTSTP */
289
290 #if defined (SIGTTOU)
291 rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou);
292 #endif /* SIGTTOU */
293
294 #if defined (SIGTTIN)
295 rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin);
296 #endif /* SIGTTIN */
297
298 signals_set_flag = 1;
299 }
300
301 #if defined (SIGWINCH)
302 if (rl_catch_sigwinch && sigwinch_set_flag == 0)
303 {
304 rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch);
305 sigwinch_set_flag = 1;
306 }
307 #endif /* SIGWINCH */
308
309 return 0;
310 }
311
312 int
313 rl_clear_signals ()
314 {
315 sighandler_cxt dummy;
316
317 if (rl_catch_signals && signals_set_flag == 1)
318 {
319 sigemptyset (&dummy.sa_mask);
320
321 rl_sigaction (SIGINT, &old_int, &dummy);
322 rl_sigaction (SIGTERM, &old_term, &dummy);
323 rl_sigaction (SIGQUIT, &old_quit, &dummy);
324 rl_sigaction (SIGALRM, &old_alrm, &dummy);
325
326 #if defined (SIGTSTP)
327 rl_sigaction (SIGTSTP, &old_tstp, &dummy);
328 #endif /* SIGTSTP */
329
330 #if defined (SIGTTOU)
331 rl_sigaction (SIGTTOU, &old_ttou, &dummy);
332 #endif /* SIGTTOU */
333
334 #if defined (SIGTTIN)
335 rl_sigaction (SIGTTIN, &old_ttin, &dummy);
336 #endif /* SIGTTIN */
337
338 signals_set_flag = 0;
339 }
340
341 #if defined (SIGWINCH)
342 if (rl_catch_sigwinch && sigwinch_set_flag == 1)
343 {
344 sigemptyset (&dummy.sa_mask);
345 rl_sigaction (SIGWINCH, &old_winch, &dummy);
346 sigwinch_set_flag = 0;
347 }
348 #endif
349
350 return 0;
351 }
352
353 /* Clean up the terminal and readline state after catching a signal, before
354 resending it to the calling application. */
355 void
356 rl_cleanup_after_signal ()
357 {
358 _rl_clean_up_for_exit ();
359 (*rl_deprep_term_function) ();
360 rl_clear_signals ();
361 rl_pending_input = 0;
362 }
363
364 /* Reset the terminal and readline state after a signal handler returns. */
365 void
366 rl_reset_after_signal ()
367 {
368 (*rl_prep_term_function) (_rl_meta_flag);
369 rl_set_signals ();
370 }
371
372 /* Free up the readline variable line state for the current line (undo list,
373 any partial history entry, any keyboard macros in progress, and any
374 numeric arguments in process) after catching a signal, before calling
375 rl_cleanup_after_signal(). */
376 void
377 rl_free_line_state ()
378 {
379 register HIST_ENTRY *entry;
380
381 free_undo_list ();
382
383 entry = current_history ();
384 if (entry)
385 entry->data = (char *)NULL;
386
387 _rl_kill_kbd_macro ();
388 rl_clear_message ();
389 _rl_init_argument ();
390 }
391
392 #endif /* HANDLE_SIGNALS */