1 /* $NetBSD: tstp.c,v 1.36 2007/08/27 19:54:29 jdc Exp $ */
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)tstp.c 8.3 (Berkeley) 5/4/94";
37 __RCSID("$NetBSD: tstp.c,v 1.36 2007/08/27 19:54:29 jdc Exp $");
41 #include <sys/ioctl.h>
49 #include "curses_private.h"
51 static int tstp_set
= 0;
52 static int winch_set
= 0;
54 static void (*otstpfn
)
57 static struct sigaction owsa
;
60 * stop_signal_handler --
61 * Handle stop signals.
64 __stop_signal_handler(/*ARGSUSED*/int signo
)
69 * Block window change and timer signals. The latter is because
70 * applications use timers to decide when to repaint the screen.
72 (void) sigemptyset(&set
);
73 (void) sigaddset(&set
, SIGALRM
);
74 (void) sigaddset(&set
, SIGWINCH
);
75 (void) sigprocmask(SIG_BLOCK
, &set
, &oset
);
78 * End the window, which also resets the terminal state to the
83 /* Unblock SIGTSTP. */
84 (void) sigemptyset(&set
);
85 (void) sigaddset(&set
, SIGTSTP
);
86 (void) sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
89 (void) kill(0, SIGTSTP
);
96 /* Reset the signals. */
97 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
101 * Set the TSTP handler.
104 __set_stophandler(void)
107 __CTRACE(__CTRACE_MISC
, "__set_stophandler: %d\n", tstp_set
);
110 otstpfn
= signal(SIGTSTP
, __stop_signal_handler
);
116 * Restore the TSTP handler.
119 __restore_stophandler(void)
122 __CTRACE(__CTRACE_MISC
, "__restore_stophandler: %d\n", tstp_set
);
125 (void) signal(SIGTSTP
, otstpfn
);
131 * winch_signal_handler --
132 * Handle winch signals by pushing KEY_RESIZE into the input stream.
135 __winch_signal_handler(/*ARGSUSED*/int signo
)
139 if (ioctl(fileno(_cursesi_screen
->outfd
), TIOCGWINSZ
, &win
) != -1 &&
140 win
.ws_row
!= 0 && win
.ws_col
!= 0) {
145 * If there was a previous handler, call that,
146 * otherwise tell getch() to send KEY_RESIZE.
148 if (owsa
.sa_handler
!= NULL
)
149 owsa
.sa_handler(signo
);
151 _cursesi_screen
->resized
= 1;
155 * Set the WINCH handler.
158 __set_winchhandler(void)
161 __CTRACE(__CTRACE_MISC
, "__set_winchhandler: %d\n", winch_set
);
166 sa
.sa_handler
= __winch_signal_handler
;
168 sigemptyset(&sa
.sa_mask
);
169 sigaction(SIGWINCH
, &sa
, &owsa
);
172 __CTRACE(__CTRACE_MISC
,
173 "__set_winchhandler: owsa.sa_handler=%p\n",
180 * Restore the WINCH handler.
183 __restore_winchhandler(void)
186 __CTRACE(__CTRACE_MISC
, "__restore_winchhandler: %d\n", winch_set
);
189 struct sigaction cwsa
;
191 sigaction(SIGWINCH
, NULL
, &cwsa
);
192 if (cwsa
.sa_handler
== owsa
.sa_handler
) {
193 sigaction(SIGWINCH
, &owsa
, NULL
);
197 * We're now using the programs WINCH handler,
198 * so don't restore the previous one.
202 __CTRACE(__CTRACE_MISC
, "cwsa.sa_handler = %p\n",
209 /* To allow both SIGTSTP and endwin() to come back nicely, we provide
210 the following routines. */
216 __CTRACE(__CTRACE_MISC
, "__stopwin\n");
218 if (_cursesi_screen
->endwin
)
221 /* Get the current terminal state (which the user may have changed). */
222 (void) tcgetattr(fileno(_cursesi_screen
->infd
),
223 &_cursesi_screen
->save_termios
);
225 __restore_stophandler();
226 __restore_winchhandler();
228 if (curscr
!= NULL
) {
230 __mvcur((int) curscr
->cury
, (int) curscr
->curx
,
231 (int) curscr
->maxy
- 1, 0, 0);
235 (void) tputs(__tc_mo
, 0, __cputchar
);
237 if ((curscr
!= NULL
) && (curscr
->flags
& __KEYPAD
))
238 (void) tputs(__tc_ke
, 0, __cputchar
);
239 (void) tputs(__tc_ve
, 0, __cputchar
);
240 (void) tputs(__tc_te
, 0, __cputchar
);
241 (void) fflush(_cursesi_screen
->outfd
);
242 (void) setvbuf(_cursesi_screen
->outfd
, NULL
, _IOLBF
, (size_t) 0);
244 _cursesi_screen
->endwin
= 1;
246 return (tcsetattr(fileno(_cursesi_screen
->infd
),
247 __tcaction
? TCSASOFT
| TCSADRAIN
: TCSADRAIN
,
248 &_cursesi_screen
->orig_termios
) ? ERR
: OK
);
259 __CTRACE(__CTRACE_MISC
, "__restartwin\n");
261 if (!_cursesi_screen
->endwin
)
264 /* Reset the curses SIGTSTP and SIGWINCH signal handlers. */
266 __set_winchhandler();
269 * Check to see if the window size has changed.
270 * If the application didn't update LINES and COLS,
271 * set the * resized flag to tell getch() to push KEY_RESIZE.
272 * Update curscr (which also updates __virtscr) and stdscr
273 * to match the new size.
275 if (ioctl(fileno(_cursesi_screen
->outfd
), TIOCGWINSZ
, &win
) != -1 &&
276 win
.ws_row
!= 0 && win
.ws_col
!= 0) {
277 if (win
.ws_row
!= LINES
) {
279 _cursesi_screen
->resized
= 1;
281 if (win
.ws_col
!= COLS
) {
283 _cursesi_screen
->resized
= 1;
287 * We need to make local copies of LINES and COLS, otherwise we
288 * could lose if they are changed between wresize() calls.
292 if (curscr
->maxy
!= nlines
|| curscr
->maxx
!= ncols
)
293 wresize(curscr
, nlines
, ncols
);
294 if (stdscr
->maxy
!= nlines
|| stdscr
->maxx
!= ncols
)
295 wresize(stdscr
, nlines
, ncols
);
297 /* save the new "default" terminal state */
298 (void) tcgetattr(fileno(_cursesi_screen
->infd
),
299 &_cursesi_screen
->orig_termios
);
301 /* Reset the terminal state to the mode just before we stopped. */
302 (void) tcsetattr(fileno(_cursesi_screen
->infd
),
303 __tcaction
? TCSASOFT
| TCSADRAIN
: TCSADRAIN
,
304 &_cursesi_screen
->save_termios
);
306 /* Restore colours */
310 __restore_meta_state();
312 /* Restart the screen. */
313 __startwin(_cursesi_screen
);
315 /* Reset cursor visibility */
316 __restore_cursor_vis();
318 /* Repaint the screen. */
325 if (_cursesi_screen
->endwin
)
328 return (tcgetattr(fileno(_cursesi_screen
->infd
),
329 &_cursesi_screen
->save_termios
) ? ERR
: OK
);
333 reset_prog_mode(void)
336 return tcsetattr(fileno(_cursesi_screen
->infd
),
337 __tcaction
? TCSASOFT
| TCSADRAIN
: TCSADRAIN
,
338 &_cursesi_screen
->save_termios
) ? ERR
: OK
;
344 return (tcgetattr(fileno(_cursesi_screen
->infd
),
345 &_cursesi_screen
->orig_termios
) ? ERR
: OK
);
349 reset_shell_mode(void)
351 return (__stopwin());