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