3 /* signals.c -- install and maintain signal handlers.
4 Id: signals.c,v 1.7 2004/04/11 17:56:46 karl Exp
6 Copyright (C) 1993, 1994, 1995, 1998, 2002, 2003, 2004 Free Software
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Originally written by Brian Fox (bfox@ai.mit.edu). */
28 void initialize_info_signal_handler (void);
30 /* **************************************************************** */
32 /* Pretending That We Have POSIX Signals */
34 /* **************************************************************** */
36 #if !defined (HAVE_SIGPROCMASK) && defined (HAVE_SIGSETMASK)
37 /* Perform OPERATION on NEWSET, perhaps leaving information in OLDSET. */
39 sigprocmask (int operation
, int *newset
, int *oldset
)
44 sigsetmask (sigblock (0) & ~(*newset
));
48 *oldset
= sigblock (*newset
);
59 #endif /* !HAVE_SIGPROCMASK && HAVE_SIGSETMASK */
61 /* **************************************************************** */
63 /* Signal Handling for Info */
65 /* **************************************************************** */
67 #if defined (HAVE_SIGACTION) || defined (HAVE_SIGPROCMASK) ||\
68 defined (HAVE_SIGSETMASK)
70 mask_termsig (sigset_t
*set
)
72 # if defined (SIGTSTP)
73 sigaddset (set
, SIGTSTP
);
74 sigaddset (set
, SIGTTOU
);
75 sigaddset (set
, SIGTTIN
);
77 # if defined (SIGWINCH)
78 sigaddset (set
, SIGWINCH
);
81 sigaddset (set
, SIGQUIT
);
84 sigaddset (set
, SIGINT
);
86 # if defined (SIGUSR1)
87 sigaddset (set
, SIGUSR1
);
90 #endif /* HAVE_SIGACTION || HAVE_SIGPROCMASK || HAVE_SIGSETMASK */
92 static RETSIGTYPE
info_signal_proc (int sig
);
93 #if defined (HAVE_SIGACTION)
94 typedef struct sigaction signal_info
;
95 signal_info info_signal_handler
;
98 set_termsig (int sig
, signal_info
*old
)
100 sigaction (sig
, &info_signal_handler
, old
);
104 restore_termsig (int sig
, const signal_info
*saved
)
106 sigaction (sig
, saved
, NULL
);
108 #else /* !HAVE_SIGACTION */
109 typedef RETSIGTYPE (*signal_info
) ();
110 #define set_termsig(sig, old) (void)(*(old) = signal (sig, info_signal_proc))
111 #define restore_termsig(sig, saved) (void)signal (sig, *(saved))
112 #define info_signal_handler info_signal_proc
113 static int term_conf_busy
= 0;
114 #endif /* !HAVE_SIGACTION */
116 static signal_info old_TSTP
, old_TTOU
, old_TTIN
;
117 static signal_info old_WINCH
, old_INT
, old_USR1
;
118 static signal_info old_QUIT
;
121 initialize_info_signal_handler (void)
124 /* (Based on info from Paul Eggert found in coreutils.) Don't use
125 HAVE_SIGACTION to decide whether to use the sa_handler, sa_flags,
126 sa_mask members, as some systems (Solaris 7+) don't define them. Use
127 SA_NOCLDSTOP instead; it's been part of POSIX.1 since day 1 (in 1988). */
128 info_signal_handler
.sa_handler
= info_signal_proc
;
129 info_signal_handler
.sa_flags
= 0;
130 mask_termsig (&info_signal_handler
.sa_mask
);
131 #endif /* SA_NOCLDSTOP */
133 #if defined (SIGTSTP)
134 set_termsig (SIGTSTP
, &old_TSTP
);
135 set_termsig (SIGTTOU
, &old_TTOU
);
136 set_termsig (SIGTTIN
, &old_TTIN
);
139 #if defined (SIGWINCH)
140 set_termsig (SIGWINCH
, &old_WINCH
);
143 #if defined (SIGQUIT)
144 set_termsig (SIGQUIT
, &old_QUIT
);
148 set_termsig (SIGINT
, &old_INT
);
151 #if defined (SIGUSR1)
152 /* Used by DJGPP to simulate SIGTSTP on Ctrl-Z. */
153 set_termsig (SIGUSR1
, &old_USR1
);
158 redisplay_after_signal (void)
160 terminal_clear_screen ();
161 display_clear_display (the_display
);
162 window_mark_chain (windows
, W_UpdateWindow
);
163 display_update_display (windows
);
164 display_cursor_at_point (active_window
);
169 reset_info_window_sizes (void)
171 terminal_goto_xy (0, 0);
173 terminal_unprep_terminal ();
174 terminal_get_screen_size ();
175 terminal_prep_terminal ();
176 display_initialize_display (screenwidth
, screenheight
);
177 window_new_screen_size (screenwidth
, screenheight
);
178 redisplay_after_signal ();
182 info_signal_proc (int sig
)
184 signal_info
*old_signal_handler
= NULL
;
186 #if !defined (HAVE_SIGACTION)
187 /* best effort: first increment this counter and later block signals */
191 #if defined (HAVE_SIGPROCMASK) || defined (HAVE_SIGSETMASK)
195 mask_termsig (&nvar
);
196 sigprocmask (SIG_BLOCK
, &nvar
, &ovar
);
198 #endif /* HAVE_SIGPROCMASK || HAVE_SIGSETMASK */
199 #endif /* !HAVE_SIGACTION */
202 #if defined (SIGTSTP)
207 #if defined (SIGQUIT)
214 #if defined (SIGTSTP)
216 old_signal_handler
= &old_TSTP
;
218 old_signal_handler
= &old_TTOU
;
220 old_signal_handler
= &old_TTIN
;
222 #if defined (SIGQUIT)
224 old_signal_handler
= &old_QUIT
;
228 old_signal_handler
= &old_INT
;
231 /* For stop signals, restore the terminal IO, leave the cursor
232 at the bottom of the window, and stop us. */
233 terminal_goto_xy (0, screenheight
- 1);
234 terminal_clear_to_eol ();
236 terminal_unprep_terminal ();
237 restore_termsig (sig
, old_signal_handler
);
238 UNBLOCK_SIGNAL (sig
);
239 kill (getpid (), sig
);
241 /* The program is returning now. Restore our signal handler,
242 turn on terminal handling, redraw the screen, and place the
243 cursor where it belongs. */
244 terminal_prep_terminal ();
245 set_termsig (sig
, old_signal_handler
);
246 /* window size might be changed while sleeping */
247 reset_info_window_sizes ();
251 #if defined (SIGWINCH) || defined (SIGUSR1)
259 /* Turn off terminal IO, tell our parent that the window has changed,
260 then reinitialize the terminal and rebuild our windows. */
263 old_signal_handler
= &old_WINCH
;
267 old_signal_handler
= &old_USR1
;
269 terminal_goto_xy (0, 0);
271 terminal_unprep_terminal (); /* needless? */
272 restore_termsig (sig
, old_signal_handler
);
273 UNBLOCK_SIGNAL (sig
);
274 kill (getpid (), sig
);
276 /* After our old signal handler returns... */
277 set_termsig (sig
, old_signal_handler
); /* needless? */
278 terminal_prep_terminal ();
279 reset_info_window_sizes ();
282 #endif /* SIGWINCH || SIGUSR1 */
284 #if !defined (HAVE_SIGACTION)
285 /* at this time it is safer to perform unblock after decrement */
287 #if defined (HAVE_SIGPROCMASK) || defined (HAVE_SIGSETMASK)
291 mask_termsig (&nvar
);
292 sigprocmask (SIG_UNBLOCK
, &nvar
, &ovar
);
294 #endif /* HAVE_SIGPROCMASK || HAVE_SIGSETMASK */
295 #endif /* !HAVE_SIGACTION */
297 /* vim: set sw=2 cino={1s>2sn-s^-se-s: */