1 /* signals.c -- signal handling support for readline. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
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.
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.
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
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 #if defined (HANDLE_SIGNALS)
44 /* Some standard library routines. */
48 #include "rlprivate.h"
50 #if !defined (RETSIGTYPE)
51 # if defined (VOID_SIGHANDLER)
52 # define RETSIGTYPE void
54 # define RETSIGTYPE int
55 # endif /* !VOID_SIGHANDLER */
56 #endif /* !RETSIGTYPE */
58 #if defined (VOID_SIGHANDLER)
59 # define SIGHANDLER_RETURN return
61 # define SIGHANDLER_RETURN return (0)
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 ();
68 #if defined (HAVE_POSIX_SIGNALS)
69 typedef struct sigaction sighandler_cxt;
70 # define rl_sigaction(s, nh, oh) sigaction(s, nh, oh)
72 typedef struct { SigHandler *sa_handler; int sa_mask, sa_flags; } sighandler_cxt;
73 # define sigemptyset(m)
74 #endif /* !HAVE_POSIX_SIGNALS */
76 static SigHandler *rl_set_sighandler __P((int, SigHandler *, sighandler_cxt *));
78 /* Exported variables for use by applications. */
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;
84 /* If non-zero, readline will install a signal handler for SIGWINCH. */
86 int rl_catch_sigwinch = 1;
89 static int signals_set_flag;
91 static int sigwinch_set_flag;
94 /* **************************************************************** */
98 /* **************************************************************** */
100 static sighandler_cxt old_int, old_term, old_alrm, old_quit;
101 #if defined (SIGTSTP)
102 static sighandler_cxt old_tstp, old_ttou, old_ttin;
104 #if defined (SIGWINCH)
105 static sighandler_cxt old_winch;
108 /* Readline signal handler functions. */
111 rl_signal_handler (sig)
114 #if defined (HAVE_POSIX_SIGNALS)
116 #else /* !HAVE_POSIX_SIGNALS */
117 # if defined (HAVE_BSD_SIGNALS)
119 # else /* !HAVE_BSD_SIGNALS */
120 sighandler_cxt dummy_cxt; /* needed for rl_set_sighandler call */
121 # endif /* !HAVE_BSD_SIGNALS */
122 #endif /* !HAVE_POSIX_SIGNALS */
124 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
125 /* Since the signal will not be blocked while we are in the signal
126 handler, ignore it until rl_clear_signals resets the catcher. */
127 if (sig == SIGINT || sig == SIGALRM)
128 rl_set_sighandler (sig, SIG_IGN, &dummy_cxt);
129 #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */
134 rl_free_line_state ();
137 #if defined (SIGTSTP)
145 rl_cleanup_after_signal ();
147 #if defined (HAVE_POSIX_SIGNALS)
148 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
149 sigdelset (&set, sig);
150 #else /* !HAVE_POSIX_SIGNALS */
151 # if defined (HAVE_BSD_SIGNALS)
152 omask = sigblock (0);
153 # endif /* HAVE_BSD_SIGNALS */
154 #endif /* !HAVE_POSIX_SIGNALS */
156 #if defined (__EMX__)
157 signal (sig, SIG_ACK);
160 kill (getpid (), sig);
162 /* Let the signal that we just sent through. */
163 #if defined (HAVE_POSIX_SIGNALS)
164 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
165 #else /* !HAVE_POSIX_SIGNALS */
166 # if defined (HAVE_BSD_SIGNALS)
167 sigsetmask (omask & ~(sigmask (sig)));
168 # endif /* HAVE_BSD_SIGNALS */
169 #endif /* !HAVE_POSIX_SIGNALS */
171 rl_reset_after_signal ();
177 #if defined (SIGWINCH)
179 rl_sigwinch_handler (sig)
184 #if defined (MUST_REINSTALL_SIGHANDLERS)
185 sighandler_cxt dummy_winch;
187 /* We don't want to change old_winch -- it holds the state of SIGWINCH
188 disposition set by the calling application. We need this state
189 because we call the application's SIGWINCH handler after updating
190 our own idea of the screen size. */
191 rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch);
194 rl_resize_terminal ();
196 /* If another sigwinch handler has been installed, call it. */
197 oh = (SigHandler *)old_winch.sa_handler;
198 if (oh && oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL)
203 #endif /* SIGWINCH */
205 /* Functions to manage signal handling. */
207 #if !defined (HAVE_POSIX_SIGNALS)
209 rl_sigaction (sig, nh, oh)
211 sighandler_cxt *nh, *oh;
213 oh->sa_handler = signal (sig, nh->sa_handler);
216 #endif /* !HAVE_POSIX_SIGNALS */
218 /* Set up a readline-specific signal handler, saving the old signal
219 information in OHANDLER. Return the old signal handler, like
222 rl_set_sighandler (sig, handler, ohandler)
225 sighandler_cxt *ohandler;
227 sighandler_cxt old_handler;
228 #if defined (HAVE_POSIX_SIGNALS)
229 struct sigaction act;
231 act.sa_handler = handler;
233 sigemptyset (&act.sa_mask);
234 sigemptyset (&ohandler->sa_mask);
235 sigaction (sig, &act, &old_handler);
237 old_handler.sa_handler = (SigHandler *)signal (sig, handler);
238 #endif /* !HAVE_POSIX_SIGNALS */
240 /* XXX -- assume we have memcpy */
241 /* If rl_set_signals is called twice in a row, don't set the old handler to
242 rl_signal_handler, because that would cause infinite recursion. */
243 if (handler != rl_signal_handler || old_handler.sa_handler != rl_signal_handler)
244 memcpy (ohandler, &old_handler, sizeof (sighandler_cxt));
246 return (ohandler->sa_handler);
250 rl_maybe_set_sighandler (sig, handler, ohandler)
253 sighandler_cxt *ohandler;
255 sighandler_cxt dummy;
258 sigemptyset (&dummy.sa_mask);
259 oh = rl_set_sighandler (sig, handler, ohandler);
260 if (oh == (SigHandler *)SIG_IGN)
261 rl_sigaction (sig, ohandler, &dummy);
267 sighandler_cxt dummy;
270 if (rl_catch_signals && signals_set_flag == 0)
272 rl_maybe_set_sighandler (SIGINT, rl_signal_handler, &old_int);
273 rl_maybe_set_sighandler (SIGTERM, rl_signal_handler, &old_term);
274 rl_maybe_set_sighandler (SIGQUIT, rl_signal_handler, &old_quit);
276 oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm);
277 if (oh == (SigHandler *)SIG_IGN)
278 rl_sigaction (SIGALRM, &old_alrm, &dummy);
279 #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART)
280 /* If the application using readline has already installed a signal
281 handler with SA_RESTART, SIGALRM will cause reads to be restarted
282 automatically, so readline should just get out of the way. Since
283 we tested for SIG_IGN above, we can just test for SIG_DFL here. */
284 if (oh != (SigHandler *)SIG_DFL && (old_alrm.sa_flags & SA_RESTART))
285 rl_sigaction (SIGALRM, &old_alrm, &dummy);
286 #endif /* HAVE_POSIX_SIGNALS */
288 #if defined (SIGTSTP)
289 rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp);
292 #if defined (SIGTTOU)
293 rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou);
296 #if defined (SIGTTIN)
297 rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin);
300 signals_set_flag = 1;
303 #if defined (SIGWINCH)
304 if (rl_catch_sigwinch && sigwinch_set_flag == 0)
306 rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch);
307 sigwinch_set_flag = 1;
309 #endif /* SIGWINCH */
317 sighandler_cxt dummy;
319 if (rl_catch_signals && signals_set_flag == 1)
321 sigemptyset (&dummy.sa_mask);
323 rl_sigaction (SIGINT, &old_int, &dummy);
324 rl_sigaction (SIGTERM, &old_term, &dummy);
325 rl_sigaction (SIGQUIT, &old_quit, &dummy);
326 rl_sigaction (SIGALRM, &old_alrm, &dummy);
328 #if defined (SIGTSTP)
329 rl_sigaction (SIGTSTP, &old_tstp, &dummy);
332 #if defined (SIGTTOU)
333 rl_sigaction (SIGTTOU, &old_ttou, &dummy);
336 #if defined (SIGTTIN)
337 rl_sigaction (SIGTTIN, &old_ttin, &dummy);
340 signals_set_flag = 0;
343 #if defined (SIGWINCH)
344 if (rl_catch_sigwinch && sigwinch_set_flag == 1)
346 sigemptyset (&dummy.sa_mask);
347 rl_sigaction (SIGWINCH, &old_winch, &dummy);
348 sigwinch_set_flag = 0;
355 /* Clean up the terminal and readline state after catching a signal, before
356 resending it to the calling application. */
358 rl_cleanup_after_signal ()
360 _rl_clean_up_for_exit ();
361 (*rl_deprep_term_function) ();
363 rl_pending_input = 0;
366 /* Reset the terminal and readline state after a signal handler returns. */
368 rl_reset_after_signal ()
370 (*rl_prep_term_function) (_rl_meta_flag);
374 /* Free up the readline variable line state for the current line (undo list,
375 any partial history entry, any keyboard macros in progress, and any
376 numeric arguments in process) after catching a signal, before calling
377 rl_cleanup_after_signal(). */
379 rl_free_line_state ()
381 register HIST_ENTRY *entry;
385 entry = current_history ();
387 entry->data = (char *)NULL;
389 _rl_kill_kbd_macro ();
391 _rl_init_argument ();
394 #endif /* HANDLE_SIGNALS */