Debian Codegraph @

libsrc/readline/signals.c

raw
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 1, 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 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #if !defined (NO_SYS_FILE)
28 # include <sys/file.h>
29 #endif /* !NO_SYS_FILE */
30 #include <signal.h>
31
32 #if defined (HAVE_UNISTD_H)
33 # include <unistd.h>
34 #endif /* HAVE_UNISTD_H */
35
36 #if defined (HAVE_STDLIB_H)
37 # include <stdlib.h>
38 #else
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
41
42 #include <errno.h>
43 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
44 #if !defined (errno)
45 extern int errno;
46 #endif /* !errno */
47
48 #include "posixstat.h"
49
50 /* System-specific feature definitions and include files. */
51 #include "rldefs.h"
52
53 #if defined (WINSIZE_IN_IOCTL_H)
54 # include <sys/ioctl.h>
55 #endif /* WINSIZE_IN_IOCTL_H */
56
57 /* Some standard library routines. */
58 #include "readline.h"
59 #include "history.h"
60
61 static void cr ();
62
63 extern int readline_echoing_p;
64 extern int rl_pending_input;
65 extern char *term_cr;
66
67 extern int _rl_meta_flag;
68
69 extern int _rl_output_character_function ();
70
71 extern void free_undo_list ();
72
73 #if defined (VOID_SIGHANDLER)
74 # define sighandler void
75 #else
76 # define sighandler int
77 #endif /* VOID_SIGHANDLER */
78
79 /* This typedef is equivalant to the one for Function; it allows us
80 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
81 typedef sighandler SigHandler ();
82
83 #if defined (__GO32__)
84 # undef HANDLE_SIGNALS
85 #endif /* __GO32__ */
86
87 #if defined (STATIC_MALLOC)
88 static char *xmalloc (), *xrealloc ();
89 #else
90 extern char *xmalloc (), *xrealloc ();
91 #endif /* STATIC_MALLOC */
92
93
94 /* **************************************************************** */
95 /* */
96 /* Signal Handling */
97 /* */
98 /* **************************************************************** */
99
100 #if defined (SIGWINCH)
101 static SigHandler *old_sigwinch = (SigHandler *)NULL;
102
103 static sighandler
104 rl_handle_sigwinch (sig)
105 int sig;
106 {
107 if (readline_echoing_p)
108 {
109 _rl_set_screen_size (fileno (rl_instream), 1);
110
111 cr (); /* was crlf () */
112 rl_forced_update_display ();
113 }
114
115 if (old_sigwinch &&
116 old_sigwinch != (SigHandler *)SIG_IGN &&
117 old_sigwinch != (SigHandler *)SIG_DFL)
118 (*old_sigwinch) (sig);
119 #if !defined (VOID_SIGHANDLER)
120 return (0);
121 #endif /* VOID_SIGHANDLER */
122 }
123 #endif /* SIGWINCH */
124
125 #if defined (HANDLE_SIGNALS)
126 /* Interrupt handling. */
127 static SigHandler
128 *old_int = (SigHandler *)NULL,
129 *old_alrm = (SigHandler *)NULL;
130 #if !defined (SHELL)
131 static SigHandler
132 *old_tstp = (SigHandler *)NULL,
133 *old_ttou = (SigHandler *)NULL,
134 *old_ttin = (SigHandler *)NULL,
135 *old_cont = (SigHandler *)NULL;
136 #endif /* !SHELL */
137
138 /* Handle an interrupt character. */
139 static sighandler
140 rl_signal_handler (sig)
141 int sig;
142 {
143 #if defined (HAVE_POSIX_SIGNALS)
144 sigset_t set;
145 #else /* !HAVE_POSIX_SIGNALS */
146 # if defined (HAVE_BSD_SIGNALS)
147 long omask;
148 # endif /* HAVE_BSD_SIGNALS */
149 #endif /* !HAVE_POSIX_SIGNALS */
150
151 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
152 /* Since the signal will not be blocked while we are in the signal
153 handler, ignore it until rl_clear_signals resets the catcher. */
154 if (sig == SIGINT)
155 signal (sig, SIG_IGN);
156 #endif /* !HAVE_BSD_SIGNALS */
157
158 switch (sig)
159 {
160 case SIGINT:
161 {
162 register HIST_ENTRY *entry;
163
164 free_undo_list ();
165
166 entry = current_history ();
167 if (entry)
168 entry->data = (char *)NULL;
169 }
170 _rl_kill_kbd_macro ();
171 rl_clear_message ();
172 rl_init_argument ();
173
174 #if defined (SIGTSTP)
175 case SIGTSTP:
176 case SIGTTOU:
177 case SIGTTIN:
178 #endif /* SIGTSTP */
179 case SIGALRM:
180 rl_clean_up_for_exit ();
181 rl_deprep_terminal ();
182 rl_clear_signals ();
183 rl_pending_input = 0;
184
185 #if defined (HAVE_POSIX_SIGNALS)
186 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
187 sigdelset (&set, sig);
188 #else /* !HAVE_POSIX_SIGNALS */
189 # if defined (HAVE_BSD_SIGNALS)
190 omask = sigblock (0);
191 # endif /* HAVE_BSD_SIGNALS */
192 #endif /* !HAVE_POSIX_SIGNALS */
193
194 kill (getpid (), sig);
195
196 /* Let the signal that we just sent through. */
197 #if defined (HAVE_POSIX_SIGNALS)
198 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
199 #else /* !HAVE_POSIX_SIGNALS */
200 # if defined (HAVE_BSD_SIGNALS)
201 sigsetmask (omask & ~(sigmask (sig)));
202 # endif /* HAVE_BSD_SIGNALS */
203 #endif /* !HAVE_POSIX_SIGNALS */
204
205 rl_prep_terminal (_rl_meta_flag);
206 rl_set_signals ();
207 }
208
209 #if !defined (VOID_SIGHANDLER)
210 return (0);
211 #endif /* !VOID_SIGHANDLER */
212 }
213
214 #if defined (HAVE_POSIX_SIGNALS)
215 static SigHandler *
216 rl_set_sighandler (sig, handler)
217 int sig;
218 SigHandler *handler;
219 {
220 struct sigaction act, oact;
221
222 act.sa_handler = handler;
223 act.sa_flags = 0;
224 sigemptyset (&act.sa_mask);
225 sigemptyset (&oact.sa_mask);
226 sigaction (sig, &act, &oact);
227 return (oact.sa_handler);
228 }
229
230 #else /* !HAVE_POSIX_SIGNALS */
231 # define rl_set_sighandler(sig, handler) (SigHandler *)signal (sig, handler)
232 #endif /* !HAVE_POSIX_SIGNALS */
233
234 rl_set_signals ()
235 {
236 old_int = (SigHandler *)rl_set_sighandler (SIGINT, rl_signal_handler);
237 if (old_int == (SigHandler *)SIG_IGN)
238 signal (SIGINT, SIG_IGN);
239
240 old_alrm = (SigHandler *)rl_set_sighandler (SIGALRM, rl_signal_handler);
241 if (old_alrm == (SigHandler *)SIG_IGN)
242 signal (SIGALRM, SIG_IGN);
243
244 #if !defined (SHELL)
245
246 #if defined (SIGTSTP)
247 old_tstp = (SigHandler *)rl_set_sighandler (SIGTSTP, rl_signal_handler);
248 if (old_tstp == (SigHandler *)SIG_IGN)
249 signal (SIGTSTP, SIG_IGN);
250 #endif /* SIGTSTP */
251 #if defined (SIGTTOU)
252 old_ttou = (SigHandler *)rl_set_sighandler (SIGTTOU, rl_signal_handler);
253 old_ttin = (SigHandler *)rl_set_sighandler (SIGTTIN, rl_signal_handler);
254
255 if (old_tstp == (SigHandler *)SIG_IGN)
256 {
257 signal (SIGTTOU, SIG_IGN);
258 signal (SIGTTIN, SIG_IGN);
259 }
260 #endif /* SIGTTOU */
261
262 #endif /* !SHELL */
263
264 #if defined (SIGWINCH)
265 old_sigwinch =
266 (SigHandler *) rl_set_sighandler (SIGWINCH, rl_handle_sigwinch);
267 #endif /* SIGWINCH */
268 return 0;
269 }
270
271 rl_clear_signals ()
272 {
273 rl_set_sighandler (SIGINT, old_int);
274 rl_set_sighandler (SIGALRM, old_alrm);
275
276 #if !defined (SHELL)
277
278 #if defined (SIGTSTP)
279 signal (SIGTSTP, old_tstp);
280 #endif
281
282 #if defined (SIGTTOU)
283 signal (SIGTTOU, old_ttou);
284 signal (SIGTTIN, old_ttin);
285 #endif /* SIGTTOU */
286
287 #endif /* !SHELL */
288
289 #if defined (SIGWINCH)
290 signal (SIGWINCH, old_sigwinch);
291 #endif
292
293 return 0;
294 }
295
296 /* Move to the start of the current line. */
297 static void
298 cr ()
299 {
300 if (term_cr)
301 tputs (term_cr, 1, _rl_output_character_function);
302 }
303 #endif /* HANDLE_SIGNALS */