Commit | Line | Data |
---|---|---|
d60d9f65 SS |
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 | #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 (__GO32__) | |
44 | # undef HANDLE_SIGNALS | |
45 | #endif /* __GO32__ */ | |
46 | ||
47 | #if defined (HANDLE_SIGNALS) | |
48 | /* Some standard library routines. */ | |
49 | #include "readline.h" | |
50 | #include "history.h" | |
51 | ||
d60d9f65 SS |
52 | #if !defined (RETSIGTYPE) |
53 | # if defined (VOID_SIGHANDLER) | |
54 | # define RETSIGTYPE void | |
55 | # else | |
56 | # define RETSIGTYPE int | |
57 | # endif /* !VOID_SIGHANDLER */ | |
58 | #endif /* !RETSIGTYPE */ | |
59 | ||
60 | #if defined (VOID_SIGHANDLER) | |
61 | # define SIGHANDLER_RETURN return | |
62 | #else | |
63 | # define SIGHANDLER_RETURN return (0) | |
64 | #endif | |
65 | ||
66 | /* This typedef is equivalant to the one for Function; it allows us | |
67 | to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */ | |
68 | typedef RETSIGTYPE SigHandler (); | |
69 | ||
c862e87b JM |
70 | extern int readline_echoing_p; |
71 | extern int rl_pending_input; | |
72 | extern int _rl_meta_flag; | |
73 | ||
74 | extern void free_undo_list (); | |
75 | extern void _rl_get_screen_size (); | |
76 | extern void _rl_redisplay_after_sigwinch (); | |
77 | extern void _rl_clean_up_for_exit (); | |
78 | extern void _rl_kill_kbd_macro (); | |
79 | extern void _rl_init_argument (); | |
80 | extern void rl_deprep_terminal (), rl_prep_terminal (); | |
81 | ||
d60d9f65 SS |
82 | static SigHandler *rl_set_sighandler (); |
83 | ||
c862e87b JM |
84 | /* Exported variables for use by applications. */ |
85 | ||
86 | /* If non-zero, readline will install its own signal handlers for | |
87 | SIGINT, SIGTERM, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */ | |
88 | int rl_catch_signals = 1; | |
89 | ||
90 | /* If non-zero, readline will install a signal handler for SIGWINCH. */ | |
91 | #ifdef SIGWINCH | |
92 | int rl_catch_sigwinch = 1; | |
93 | #endif | |
94 | ||
95 | static int signals_set_flag; | |
96 | static int sigwinch_set_flag; | |
97 | ||
d60d9f65 SS |
98 | /* **************************************************************** */ |
99 | /* */ | |
100 | /* Signal Handling */ | |
101 | /* */ | |
102 | /* **************************************************************** */ | |
103 | ||
d60d9f65 SS |
104 | #if defined (HAVE_POSIX_SIGNALS) |
105 | typedef struct sigaction sighandler_cxt; | |
106 | # define rl_sigaction(s, nh, oh) sigaction(s, nh, oh) | |
107 | #else | |
c862e87b | 108 | typedef struct { SigHandler *sa_handler; int sa_mask, sa_flags; } sighandler_cxt; |
d60d9f65 SS |
109 | # define sigemptyset(m) |
110 | #endif /* !HAVE_POSIX_SIGNALS */ | |
111 | ||
c862e87b JM |
112 | static sighandler_cxt old_int, old_term, old_alrm, old_quit; |
113 | #if defined (SIGTSTP) | |
d60d9f65 | 114 | static sighandler_cxt old_tstp, old_ttou, old_ttin; |
d60d9f65 | 115 | #endif |
d60d9f65 SS |
116 | #if defined (SIGWINCH) |
117 | static sighandler_cxt old_winch; | |
118 | #endif | |
119 | ||
120 | /* Readline signal handler functions. */ | |
121 | ||
122 | static RETSIGTYPE | |
123 | rl_signal_handler (sig) | |
124 | int sig; | |
125 | { | |
126 | #if defined (HAVE_POSIX_SIGNALS) | |
127 | sigset_t set; | |
128 | #else /* !HAVE_POSIX_SIGNALS */ | |
129 | # if defined (HAVE_BSD_SIGNALS) | |
130 | long omask; | |
131 | # else /* !HAVE_BSD_SIGNALS */ | |
132 | sighandler_cxt dummy_cxt; /* needed for rl_set_sighandler call */ | |
133 | # endif /* !HAVE_BSD_SIGNALS */ | |
134 | #endif /* !HAVE_POSIX_SIGNALS */ | |
135 | ||
136 | #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS) | |
137 | /* Since the signal will not be blocked while we are in the signal | |
138 | handler, ignore it until rl_clear_signals resets the catcher. */ | |
139 | if (sig == SIGINT || sig == SIGALRM) | |
140 | rl_set_sighandler (sig, SIG_IGN, &dummy_cxt); | |
141 | #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */ | |
142 | ||
143 | switch (sig) | |
144 | { | |
145 | case SIGINT: | |
c862e87b JM |
146 | rl_free_line_state (); |
147 | /* FALLTHROUGH */ | |
d60d9f65 SS |
148 | |
149 | #if defined (SIGTSTP) | |
150 | case SIGTSTP: | |
151 | case SIGTTOU: | |
152 | case SIGTTIN: | |
153 | #endif /* SIGTSTP */ | |
154 | case SIGALRM: | |
155 | case SIGTERM: | |
c862e87b JM |
156 | case SIGQUIT: |
157 | rl_cleanup_after_signal (); | |
d60d9f65 SS |
158 | |
159 | #if defined (HAVE_POSIX_SIGNALS) | |
160 | sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set); | |
161 | sigdelset (&set, sig); | |
162 | #else /* !HAVE_POSIX_SIGNALS */ | |
163 | # if defined (HAVE_BSD_SIGNALS) | |
164 | omask = sigblock (0); | |
165 | # endif /* HAVE_BSD_SIGNALS */ | |
166 | #endif /* !HAVE_POSIX_SIGNALS */ | |
167 | ||
168 | kill (getpid (), sig); | |
169 | ||
170 | /* Let the signal that we just sent through. */ | |
171 | #if defined (HAVE_POSIX_SIGNALS) | |
172 | sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL); | |
173 | #else /* !HAVE_POSIX_SIGNALS */ | |
174 | # if defined (HAVE_BSD_SIGNALS) | |
175 | sigsetmask (omask & ~(sigmask (sig))); | |
176 | # endif /* HAVE_BSD_SIGNALS */ | |
177 | #endif /* !HAVE_POSIX_SIGNALS */ | |
178 | ||
c862e87b | 179 | rl_reset_after_signal (); |
d60d9f65 SS |
180 | } |
181 | ||
182 | SIGHANDLER_RETURN; | |
183 | } | |
184 | ||
185 | #if defined (SIGWINCH) | |
186 | static RETSIGTYPE | |
c862e87b | 187 | rl_sigwinch_handler (sig) |
d60d9f65 SS |
188 | int sig; |
189 | { | |
190 | SigHandler *oh; | |
191 | ||
192 | #if defined (MUST_REINSTALL_SIGHANDLERS) | |
193 | sighandler_cxt dummy_winch; | |
194 | ||
195 | /* We don't want to change old_winch -- it holds the state of SIGWINCH | |
196 | disposition set by the calling application. We need this state | |
197 | because we call the application's SIGWINCH handler after updating | |
198 | our own idea of the screen size. */ | |
c862e87b | 199 | rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch); |
d60d9f65 SS |
200 | #endif |
201 | ||
c862e87b | 202 | rl_resize_terminal (); |
d60d9f65 SS |
203 | |
204 | /* If another sigwinch handler has been installed, call it. */ | |
205 | oh = (SigHandler *)old_winch.sa_handler; | |
206 | if (oh && oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL) | |
207 | (*oh) (sig); | |
208 | ||
209 | SIGHANDLER_RETURN; | |
210 | } | |
211 | #endif /* SIGWINCH */ | |
212 | ||
213 | /* Functions to manage signal handling. */ | |
214 | ||
215 | #if !defined (HAVE_POSIX_SIGNALS) | |
216 | static int | |
217 | rl_sigaction (sig, nh, oh) | |
218 | int sig; | |
219 | sighandler_cxt *nh, *oh; | |
220 | { | |
221 | oh->sa_handler = signal (sig, nh->sa_handler); | |
222 | return 0; | |
223 | } | |
224 | #endif /* !HAVE_POSIX_SIGNALS */ | |
225 | ||
226 | /* Set up a readline-specific signal handler, saving the old signal | |
227 | information in OHANDLER. Return the old signal handler, like | |
228 | signal(). */ | |
229 | static SigHandler * | |
230 | rl_set_sighandler (sig, handler, ohandler) | |
231 | int sig; | |
232 | SigHandler *handler; | |
233 | sighandler_cxt *ohandler; | |
234 | { | |
235 | #if defined (HAVE_POSIX_SIGNALS) | |
236 | struct sigaction act; | |
237 | ||
238 | act.sa_handler = handler; | |
239 | act.sa_flags = 0; | |
240 | sigemptyset (&act.sa_mask); | |
241 | sigemptyset (&ohandler->sa_mask); | |
242 | sigaction (sig, &act, ohandler); | |
243 | #else | |
244 | ohandler->sa_handler = (SigHandler *)signal (sig, handler); | |
245 | #endif /* !HAVE_POSIX_SIGNALS */ | |
246 | return (ohandler->sa_handler); | |
247 | } | |
248 | ||
c862e87b JM |
249 | static void |
250 | rl_maybe_set_sighandler (sig, handler, ohandler) | |
251 | int sig; | |
252 | SigHandler *handler; | |
253 | sighandler_cxt *ohandler; | |
d60d9f65 SS |
254 | { |
255 | sighandler_cxt dummy; | |
256 | SigHandler *oh; | |
257 | ||
d60d9f65 | 258 | sigemptyset (&dummy.sa_mask); |
c862e87b | 259 | oh = rl_set_sighandler (sig, handler, ohandler); |
d60d9f65 | 260 | if (oh == (SigHandler *)SIG_IGN) |
c862e87b JM |
261 | rl_sigaction (sig, ohandler, &dummy); |
262 | } | |
d60d9f65 | 263 | |
c862e87b JM |
264 | int |
265 | rl_set_signals () | |
266 | { | |
267 | sighandler_cxt dummy; | |
268 | SigHandler *oh; | |
269 | ||
270 | if (rl_catch_signals && signals_set_flag == 0) | |
271 | { | |
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); | |
275 | ||
276 | oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm); | |
277 | if (oh == (SigHandler *)SIG_IGN) | |
278 | rl_sigaction (SIGALRM, &old_alrm, &dummy); | |
d60d9f65 | 279 | #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART) |
c862e87b JM |
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); | |
d60d9f65 SS |
286 | #endif /* HAVE_POSIX_SIGNALS */ |
287 | ||
d60d9f65 | 288 | #if defined (SIGTSTP) |
c862e87b | 289 | rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp); |
d60d9f65 SS |
290 | #endif /* SIGTSTP */ |
291 | ||
292 | #if defined (SIGTTOU) | |
c862e87b | 293 | rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou); |
d60d9f65 SS |
294 | #endif /* SIGTTOU */ |
295 | ||
c862e87b JM |
296 | #if defined (SIGTTIN) |
297 | rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin); | |
298 | #endif /* SIGTTIN */ | |
d60d9f65 | 299 | |
c862e87b JM |
300 | signals_set_flag = 1; |
301 | } | |
d60d9f65 SS |
302 | |
303 | #if defined (SIGWINCH) | |
c862e87b JM |
304 | if (rl_catch_sigwinch && sigwinch_set_flag == 0) |
305 | { | |
306 | rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch); | |
307 | sigwinch_set_flag = 1; | |
308 | } | |
d60d9f65 SS |
309 | #endif /* SIGWINCH */ |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
314 | int | |
315 | rl_clear_signals () | |
316 | { | |
317 | sighandler_cxt dummy; | |
318 | ||
c862e87b JM |
319 | if (rl_catch_signals && signals_set_flag == 1) |
320 | { | |
321 | sigemptyset (&dummy.sa_mask); | |
d60d9f65 | 322 | |
c862e87b JM |
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); | |
d60d9f65 SS |
327 | |
328 | #if defined (SIGTSTP) | |
c862e87b JM |
329 | rl_sigaction (SIGTSTP, &old_tstp, &dummy); |
330 | #endif /* SIGTSTP */ | |
d60d9f65 SS |
331 | |
332 | #if defined (SIGTTOU) | |
c862e87b | 333 | rl_sigaction (SIGTTOU, &old_ttou, &dummy); |
d60d9f65 SS |
334 | #endif /* SIGTTOU */ |
335 | ||
c862e87b JM |
336 | #if defined (SIGTTIN) |
337 | rl_sigaction (SIGTTIN, &old_ttin, &dummy); | |
338 | #endif /* SIGTTIN */ | |
d60d9f65 | 339 | |
c862e87b JM |
340 | signals_set_flag = 0; |
341 | } | |
d60d9f65 SS |
342 | |
343 | #if defined (SIGWINCH) | |
c862e87b JM |
344 | if (rl_catch_sigwinch && sigwinch_set_flag == 1) |
345 | { | |
346 | sigemptyset (&dummy.sa_mask); | |
347 | rl_sigaction (SIGWINCH, &old_winch, &dummy); | |
348 | sigwinch_set_flag = 0; | |
349 | } | |
d60d9f65 SS |
350 | #endif |
351 | ||
352 | return 0; | |
353 | } | |
c862e87b JM |
354 | |
355 | /* Clean up the terminal and readline state after catching a signal, before | |
356 | resending it to the calling application. */ | |
357 | void | |
358 | rl_cleanup_after_signal () | |
359 | { | |
360 | _rl_clean_up_for_exit (); | |
361 | (*rl_deprep_term_function) (); | |
362 | rl_clear_signals (); | |
363 | rl_pending_input = 0; | |
364 | } | |
365 | ||
366 | /* Reset the terminal and readline state after a signal handler returns. */ | |
367 | void | |
368 | rl_reset_after_signal () | |
369 | { | |
370 | (*rl_prep_term_function) (_rl_meta_flag); | |
371 | rl_set_signals (); | |
372 | } | |
373 | ||
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(). */ | |
378 | void | |
379 | rl_free_line_state () | |
380 | { | |
381 | register HIST_ENTRY *entry; | |
382 | ||
383 | free_undo_list (); | |
384 | ||
385 | entry = current_history (); | |
386 | if (entry) | |
387 | entry->data = (char *)NULL; | |
388 | ||
389 | _rl_kill_kbd_macro (); | |
390 | rl_clear_message (); | |
391 | _rl_init_argument (); | |
392 | } | |
393 | ||
d60d9f65 | 394 | #endif /* HANDLE_SIGNALS */ |