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 1, 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 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
25 #include <sys/types.h>
27 #if !defined (NO_SYS_FILE)
28 # include <sys/file.h>
29 #endif /* !NO_SYS_FILE */
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
43 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
48 #include "posixstat.h"
50 /* System-specific feature definitions and include files. */
53 #if defined (GWINSZ_IN_SYS_IOCTL)
54 # include <sys/ioctl.h>
55 #endif /* GWINSZ_IN_SYS_IOCTL */
57 /* Some standard library routines. */
61 extern int readline_echoing_p;
62 extern int rl_pending_input;
63 extern int _rl_meta_flag;
65 extern void free_undo_list ();
67 #if defined (VOID_SIGHANDLER)
68 # define sighandler void
70 # define sighandler int
71 #endif /* VOID_SIGHANDLER */
73 /* This typedef is equivalant to the one for Function; it allows us
74 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
75 typedef sighandler SigHandler ();
77 #if defined (__GO32__)
78 # undef HANDLE_SIGNALS
81 #if defined (STATIC_MALLOC)
82 static char *xmalloc (), *xrealloc ();
84 extern char *xmalloc (), *xrealloc ();
85 #endif /* STATIC_MALLOC */
88 /* **************************************************************** */
92 /* **************************************************************** */
94 #if defined (SIGWINCH)
95 static SigHandler *old_sigwinch = (SigHandler *)NULL;
98 rl_handle_sigwinch (sig)
101 if (readline_echoing_p)
103 _rl_set_screen_size (fileno (rl_instream), 1);
104 _rl_redisplay_after_sigwinch ();
108 old_sigwinch != (SigHandler *)SIG_IGN &&
109 old_sigwinch != (SigHandler *)SIG_DFL)
110 (*old_sigwinch) (sig);
111 #if !defined (VOID_SIGHANDLER)
113 #endif /* VOID_SIGHANDLER */
115 #endif /* SIGWINCH */
117 #if defined (HANDLE_SIGNALS)
118 /* Interrupt handling. */
120 *old_int = (SigHandler *)NULL,
121 *old_alrm = (SigHandler *)NULL;
124 *old_tstp = (SigHandler *)NULL,
125 *old_ttou = (SigHandler *)NULL,
126 *old_ttin = (SigHandler *)NULL,
127 *old_cont = (SigHandler *)NULL;
130 /* Handle an interrupt character. */
132 rl_signal_handler (sig)
135 #if defined (HAVE_POSIX_SIGNALS)
137 #else /* !HAVE_POSIX_SIGNALS */
138 # if defined (HAVE_BSD_SIGNALS)
140 # endif /* HAVE_BSD_SIGNALS */
141 #endif /* !HAVE_POSIX_SIGNALS */
143 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
144 /* Since the signal will not be blocked while we are in the signal
145 handler, ignore it until rl_clear_signals resets the catcher. */
147 signal (sig, SIG_IGN);
148 #endif /* !HAVE_BSD_SIGNALS */
154 register HIST_ENTRY *entry;
158 entry = current_history ();
160 entry->data = (char *)NULL;
162 _rl_kill_kbd_macro ();
166 #if defined (SIGTSTP)
172 rl_clean_up_for_exit ();
173 rl_deprep_terminal ();
175 rl_pending_input = 0;
177 #if defined (HAVE_POSIX_SIGNALS)
178 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
179 sigdelset (&set, sig);
180 #else /* !HAVE_POSIX_SIGNALS */
181 # if defined (HAVE_BSD_SIGNALS)
182 omask = sigblock (0);
183 # endif /* HAVE_BSD_SIGNALS */
184 #endif /* !HAVE_POSIX_SIGNALS */
186 kill (getpid (), sig);
188 /* Let the signal that we just sent through. */
189 #if defined (HAVE_POSIX_SIGNALS)
190 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
191 #else /* !HAVE_POSIX_SIGNALS */
192 # if defined (HAVE_BSD_SIGNALS)
193 sigsetmask (omask & ~(sigmask (sig)));
194 # endif /* HAVE_BSD_SIGNALS */
195 #endif /* !HAVE_POSIX_SIGNALS */
197 rl_prep_terminal (_rl_meta_flag);
201 #if !defined (VOID_SIGHANDLER)
203 #endif /* !VOID_SIGHANDLER */
206 #if defined (HAVE_POSIX_SIGNALS)
208 rl_set_sighandler (sig, handler)
212 struct sigaction act, oact;
214 act.sa_handler = handler;
216 sigemptyset (&act.sa_mask);
217 sigemptyset (&oact.sa_mask);
218 sigaction (sig, &act, &oact);
219 return (oact.sa_handler);
222 #else /* !HAVE_POSIX_SIGNALS */
223 # define rl_set_sighandler(sig, handler) (SigHandler *)signal (sig, handler)
224 #endif /* !HAVE_POSIX_SIGNALS */
228 old_int = (SigHandler *)rl_set_sighandler (SIGINT, rl_signal_handler);
229 if (old_int == (SigHandler *)SIG_IGN)
230 rl_set_sighandler (SIGINT, SIG_IGN);
232 old_alrm = (SigHandler *)rl_set_sighandler (SIGALRM, rl_signal_handler);
233 if (old_alrm == (SigHandler *)SIG_IGN)
234 rl_set_sighandler (SIGALRM, SIG_IGN);
238 #if defined (SIGTSTP)
239 old_tstp = (SigHandler *)rl_set_sighandler (SIGTSTP, rl_signal_handler);
240 if (old_tstp == (SigHandler *)SIG_IGN)
241 rl_set_sighandler (SIGTSTP, SIG_IGN);
243 #if defined (SIGTTOU)
244 old_ttou = (SigHandler *)rl_set_sighandler (SIGTTOU, rl_signal_handler);
245 old_ttin = (SigHandler *)rl_set_sighandler (SIGTTIN, rl_signal_handler);
247 if (old_tstp == (SigHandler *)SIG_IGN)
249 rl_set_sighandler (SIGTTOU, SIG_IGN);
250 rl_set_sighandler (SIGTTIN, SIG_IGN);
256 #if defined (SIGWINCH)
258 (SigHandler *) rl_set_sighandler (SIGWINCH, rl_handle_sigwinch);
259 #endif /* SIGWINCH */
265 rl_set_sighandler (SIGINT, old_int);
266 rl_set_sighandler (SIGALRM, old_alrm);
270 #if defined (SIGTSTP)
271 rl_set_sighandler (SIGTSTP, old_tstp);
274 #if defined (SIGTTOU)
275 rl_set_sighandler (SIGTTOU, old_ttou);
276 rl_set_sighandler (SIGTTIN, old_ttin);
281 #if defined (SIGWINCH)
282 rl_set_sighandler (SIGWINCH, old_sigwinch);
287 #endif /* HANDLE_SIGNALS */