1 /* $NetBSD: tstp.c,v 1.40 2013/10/15 13:00:52 christos 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.40 2013/10/15 13:00:52 christos 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
;
63 * stop_signal_handler --
64 * Handle stop signals.
67 __stop_signal_handler(/*ARGSUSED*/int signo
)
72 * Block window change and timer signals. The latter is because
73 * applications use timers to decide when to repaint the screen.
75 (void) sigemptyset(&set
);
76 (void) sigaddset(&set
, SIGALRM
);
77 (void) sigaddset(&set
, SIGWINCH
);
78 (void) sigprocmask(SIG_BLOCK
, &set
, &oset
);
81 * End the window, which also resets the terminal state to the
86 /* Unblock SIGTSTP. */
87 (void) sigemptyset(&set
);
88 (void) sigaddset(&set
, SIGTSTP
);
89 (void) sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
92 (void) kill(0, SIGTSTP
);
99 /* Reset the signals. */
100 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
104 * Set the TSTP handler.
107 __set_stophandler(void)
110 __CTRACE(__CTRACE_MISC
, "__set_stophandler: %d\n", tstp_set
);
113 otstpfn
= signal(SIGTSTP
, __stop_signal_handler
);
119 * Restore the TSTP handler.
122 __restore_stophandler(void)
125 __CTRACE(__CTRACE_MISC
, "__restore_stophandler: %d\n", tstp_set
);
128 (void) signal(SIGTSTP
, otstpfn
);
134 * winch_signal_handler --
135 * Handle winch signals by pushing KEY_RESIZE into the input stream.
138 __winch_signal_handler(/*ARGSUSED*/int signo
)
142 if (ioctl(fileno(_cursesi_screen
->outfd
), TIOCGWINSZ
, &win
) != -1 &&
143 win
.ws_row
!= 0 && win
.ws_col
!= 0) {
148 * If there was a previous handler, call that,
149 * otherwise tell getch() to send KEY_RESIZE.
151 if (owsa
.sa_handler
!= NULL
)
152 owsa
.sa_handler(signo
);
154 _cursesi_screen
->resized
= 1;
158 * Set the WINCH handler.
161 __set_winchhandler(void)
164 __CTRACE(__CTRACE_MISC
, "__set_winchhandler: %d\n", winch_set
);
169 sa
.sa_handler
= __winch_signal_handler
;
171 sigemptyset(&sa
.sa_mask
);
172 sigaction(SIGWINCH
, &sa
, &owsa
);
175 __CTRACE(__CTRACE_MISC
,
176 "__set_winchhandler: owsa.sa_handler=%p\n",
183 * Restore the WINCH handler.
186 __restore_winchhandler(void)
189 __CTRACE(__CTRACE_MISC
, "__restore_winchhandler: %d\n", winch_set
);
192 struct sigaction cwsa
;
194 sigaction(SIGWINCH
, NULL
, &cwsa
);
195 if (cwsa
.sa_handler
== owsa
.sa_handler
) {
196 sigaction(SIGWINCH
, &owsa
, NULL
);
200 * We're now using the programs WINCH handler,
201 * so don't restore the previous one.
205 __CTRACE(__CTRACE_MISC
, "cwsa.sa_handler = %p\n",
212 /* To allow both SIGTSTP and endwin() to come back nicely, we provide
213 the following routines. */
219 __CTRACE(__CTRACE_MISC
, "__stopwin\n");
221 if (_cursesi_screen
->endwin
)
224 /* Get the current terminal state (which the user may have changed). */
225 (void) tcgetattr(fileno(_cursesi_screen
->infd
),
226 &_cursesi_screen
->save_termios
);
228 __restore_stophandler();
229 __restore_winchhandler();
231 if (curscr
!= NULL
) {
233 __mvcur((int) curscr
->cury
, (int) curscr
->curx
,
234 (int) curscr
->maxy
- 1, 0, 0);
238 (void) tputs(meta_on
, 0, __cputchar
);
240 if ((curscr
!= NULL
) && (curscr
->flags
& __KEYPAD
))
241 (void) tputs(keypad_local
, 0, __cputchar
);
242 (void) tputs(cursor_normal
, 0, __cputchar
);
243 (void) tputs(exit_ca_mode
, 0, __cputchar
);
244 (void) fflush(_cursesi_screen
->outfd
);
245 (void) setvbuf(_cursesi_screen
->outfd
, NULL
, _IOLBF
, (size_t) 0);
247 _cursesi_screen
->endwin
= 1;
249 return tcsetattr(fileno(_cursesi_screen
->infd
), TCSASOFT
| TCSADRAIN
,
250 &_cursesi_screen
->orig_termios
) ? ERR
: OK
;
261 __CTRACE(__CTRACE_MISC
, "__restartwin\n");
263 if (!_cursesi_screen
->endwin
)
266 /* Reset the curses SIGTSTP and SIGWINCH signal handlers. */
268 __set_winchhandler();
271 * Check to see if the window size has changed.
272 * If the application didn't update LINES and COLS,
273 * set the * resized flag to tell getch() to push KEY_RESIZE.
274 * Update curscr (which also updates __virtscr) and stdscr
275 * to match the new size.
277 if (ioctl(fileno(_cursesi_screen
->outfd
), TIOCGWINSZ
, &win
) != -1 &&
278 win
.ws_row
!= 0 && win
.ws_col
!= 0) {
279 if (win
.ws_row
!= LINES
) {
281 _cursesi_screen
->resized
= 1;
283 if (win
.ws_col
!= COLS
) {
285 _cursesi_screen
->resized
= 1;
289 * We need to make local copies of LINES and COLS, otherwise we
290 * could lose if they are changed between wresize() calls.
294 if (curscr
->maxy
!= nlines
|| curscr
->maxx
!= ncols
)
295 wresize(curscr
, nlines
, ncols
);
296 if (stdscr
->maxy
!= nlines
|| stdscr
->maxx
!= ncols
)
297 wresize(stdscr
, nlines
, ncols
);
299 /* save the new "default" terminal state */
300 (void) tcgetattr(fileno(_cursesi_screen
->infd
),
301 &_cursesi_screen
->orig_termios
);
303 /* Reset the terminal state to the mode just before we stopped. */
304 (void) tcsetattr(fileno(_cursesi_screen
->infd
), TCSASOFT
| TCSADRAIN
,
305 &_cursesi_screen
->save_termios
);
307 /* Restore colours */
311 __restore_meta_state();
313 /* Restart the screen. */
314 __startwin(_cursesi_screen
);
316 /* Reset cursor visibility */
317 __restore_cursor_vis();
319 /* Repaint the screen. */
326 if (_cursesi_screen
->endwin
)
329 return tcgetattr(fileno(_cursesi_screen
->infd
),
330 &_cursesi_screen
->save_termios
) ? ERR
: OK
;
334 reset_prog_mode(void)
337 return tcsetattr(fileno(_cursesi_screen
->infd
), TCSASOFT
| 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)