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. */
24 #include <sys/types.h>
26 #if !defined (NO_SYS_FILE)
27 # include <sys/file.h>
28 #endif /* !NO_SYS_FILE */
31 /* This is needed to include support for TIOCGWINSZ and window resizing. */
32 #if defined (OSF1) || defined (BSD386) || defined (_386BSD) || defined (__BSD_4_4__) || defined (AIX)
33 # include <sys/ioctl.h>
34 #endif /* OSF1 || BSD386 || _386BSD || __BSD_4_4__ || AIX */
37 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
42 /* System-specific feature definitions and include files. */
45 /* Some standard library routines. */
51 extern int readline_echoing_p;
52 extern int rl_pending_input;
54 extern int _rl_meta_flag;
57 extern void _rl_output_character_function (int);
59 extern void _rl_output_character_function ();
62 extern void free_undo_list ();
64 #if defined (VOID_SIGHANDLER)
65 # define sighandler void
67 # define sighandler int
68 #endif /* VOID_SIGHANDLER */
70 /* This typedef is equivalant to the one for Function; it allows us
71 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
72 typedef sighandler SigHandler ();
74 /* **************************************************************** */
78 /* **************************************************************** */
80 #if defined (HANDLE_SIGNALS)
82 #if defined (SIGWINCH)
83 static SigHandler *old_sigwinch = (SigHandler *)NULL;
86 rl_handle_sigwinch (sig)
89 if (readline_echoing_p)
91 _rl_set_screen_size (fileno (rl_instream), 1);
93 cr (); /* was crlf () */
94 rl_forced_update_display ();
98 old_sigwinch != (SigHandler *)SIG_IGN &&
99 old_sigwinch != (SigHandler *)SIG_DFL)
100 (*old_sigwinch) (sig);
101 #if !defined (VOID_SIGHANDLER)
103 #endif /* VOID_SIGHANDLER */
105 #endif /* SIGWINCH */
107 /* Interrupt handling. */
109 *old_int = (SigHandler *)NULL,
110 *old_tstp = (SigHandler *)NULL,
111 *old_ttou = (SigHandler *)NULL,
112 *old_ttin = (SigHandler *)NULL,
113 *old_cont = (SigHandler *)NULL,
114 *old_alrm = (SigHandler *)NULL;
116 /* Handle an interrupt character. */
118 rl_signal_handler (sig)
121 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
122 /* Since the signal will not be blocked while we are in the signal
123 handler, ignore it until rl_clear_signals resets the catcher. */
125 signal (sig, SIG_IGN);
126 #endif /* !HAVE_BSD_SIGNALS */
132 register HIST_ENTRY *entry;
136 entry = current_history ();
138 entry->data = (char *)NULL;
140 _rl_kill_kbd_macro ();
144 #if defined (SIGTSTP)
150 rl_clean_up_for_exit ();
151 rl_deprep_terminal ();
153 rl_pending_input = 0;
155 kill (getpid (), sig);
159 rl_prep_terminal (_rl_meta_flag);
163 #if !defined (VOID_SIGHANDLER)
165 #endif /* !VOID_SIGHANDLER */
168 #if defined (HAVE_POSIX_SIGNALS)
170 rl_set_sighandler (sig, handler)
174 struct sigaction act, oact;
176 act.sa_handler = handler;
178 sigemptyset (&act.sa_mask);
179 sigemptyset (&oact.sa_mask);
180 sigaction (sig, &act, &oact);
181 return (oact.sa_handler);
184 #else /* !HAVE_POSIX_SIGNALS */
185 # define rl_set_sighandler(sig, handler) (SigHandler *)signal (sig, handler)
186 #endif /* !HAVE_POSIX_SIGNALS */
190 old_int = (SigHandler *)rl_set_sighandler (SIGINT, rl_signal_handler);
191 if (old_int == (SigHandler *)SIG_IGN)
192 signal (SIGINT, SIG_IGN);
194 old_alrm = (SigHandler *)rl_set_sighandler (SIGALRM, rl_signal_handler);
195 if (old_alrm == (SigHandler *)SIG_IGN)
196 signal (SIGALRM, SIG_IGN);
198 #if defined (SIGTSTP)
199 old_tstp = (SigHandler *)rl_set_sighandler (SIGTSTP, rl_signal_handler);
200 if (old_tstp == (SigHandler *)SIG_IGN)
201 signal (SIGTSTP, SIG_IGN);
203 #if defined (SIGTTOU)
204 old_ttou = (SigHandler *)rl_set_sighandler (SIGTTOU, rl_signal_handler);
205 old_ttin = (SigHandler *)rl_set_sighandler (SIGTTIN, rl_signal_handler);
207 if (old_tstp == (SigHandler *)SIG_IGN)
209 signal (SIGTTOU, SIG_IGN);
210 signal (SIGTTIN, SIG_IGN);
214 #if defined (SIGWINCH)
216 (SigHandler *) rl_set_sighandler (SIGWINCH, rl_handle_sigwinch);
222 rl_set_sighandler (SIGINT, old_int);
223 rl_set_sighandler (SIGALRM, old_alrm);
225 #if defined (SIGTSTP)
226 signal (SIGTSTP, old_tstp);
229 #if defined (SIGTTOU)
230 signal (SIGTTOU, old_ttou);
231 signal (SIGTTIN, old_ttin);
234 #if defined (SIGWINCH)
235 signal (SIGWINCH, old_sigwinch);
239 /* Move to the start of the current line. */
243 extern char *term_cr;
246 tputs (term_cr, 1, _rl_output_character_function);
248 #endif /* HANDLE_SIGNALS */