Home | History | Annotate | Line # | Download | only in libcurses
tstp.c revision 1.42.6.1
      1 /*	$NetBSD: tstp.c,v 1.42.6.1 2018/09/27 14:59:28 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1981, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     18  *
     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
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)tstp.c	8.3 (Berkeley) 5/4/94";
     36 #else
     37 __RCSID("$NetBSD: tstp.c,v 1.42.6.1 2018/09/27 14:59:28 martin Exp $");
     38 #endif
     39 #endif				/* not lint */
     40 
     41 #include <sys/ioctl.h>
     42 
     43 #include <errno.h>
     44 #include <signal.h>
     45 #include <termios.h>
     46 #include <unistd.h>
     47 
     48 #include "curses.h"
     49 #include "curses_private.h"
     50 
     51 static int tstp_set;
     52 static int winch_set;
     53 
     54 static void (*otstpfn)(int) = SIG_DFL;
     55 
     56 static struct sigaction	owsa;
     57 #ifndef TCSASOFT
     58 #define TCSASOFT 0
     59 #endif
     60 
     61 /*
     62  * stop_signal_handler --
     63  *	Handle stop signals.
     64  */
     65 void
     66 __stop_signal_handler(/*ARGSUSED*/int signo)
     67 {
     68 	sigset_t oset, set;
     69 
     70 	/*
     71 	 * Block window change and timer signals.  The latter is because
     72 	 * applications use timers to decide when to repaint the screen.
     73 	 */
     74 	(void)sigemptyset(&set);
     75 	(void)sigaddset(&set, SIGALRM);
     76 	(void)sigaddset(&set, SIGWINCH);
     77 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
     78 
     79 	/*
     80 	 * End the window, which also resets the terminal state to the
     81 	 * original modes.
     82 	 */
     83 	__stopwin();
     84 
     85 	/* Unblock SIGTSTP. */
     86 	(void)sigemptyset(&set);
     87 	(void)sigaddset(&set, SIGTSTP);
     88 	(void)sigprocmask(SIG_UNBLOCK, &set, NULL);
     89 
     90 	/* Stop ourselves. */
     91 	(void)kill(0, SIGTSTP);
     92 
     93 	/* Time passes ... */
     94 
     95 	/* restart things */
     96 	__restartwin();
     97 
     98 	/* Reset the signals. */
     99 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    100 }
    101 
    102 /*
    103  * Set the TSTP handler.
    104  */
    105 void
    106 __set_stophandler(void)
    107 {
    108 
    109 #ifdef DEBUG
    110 	__CTRACE(__CTRACE_MISC, "__set_stophandler: %d\n", tstp_set);
    111 #endif
    112 	if (!tstp_set) {
    113 		otstpfn = signal(SIGTSTP, __stop_signal_handler);
    114 		tstp_set = 1;
    115 	}
    116 }
    117 
    118 /*
    119  * Restore the TSTP handler.
    120  */
    121 void
    122 __restore_stophandler(void)
    123 {
    124 
    125 #ifdef DEBUG
    126 	__CTRACE(__CTRACE_MISC, "__restore_stophandler: %d\n", tstp_set);
    127 #endif
    128 	if (tstp_set) {
    129 		(void)signal(SIGTSTP, otstpfn);
    130 		tstp_set = 0;
    131 	}
    132 }
    133 
    134 /*
    135  * winch_signal_handler --
    136  *	Handle winch signals by pushing KEY_RESIZE into the input stream.
    137  */
    138 void
    139 __winch_signal_handler(/*ARGSUSED*/int signo)
    140 {
    141 	struct winsize win;
    142 
    143 	if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 &&
    144 	    win.ws_row != 0 && win.ws_col != 0)
    145 	{
    146 		LINES = win.ws_row;
    147 		COLS = win.ws_col;
    148 	}
    149 	/*
    150 	 * If there was a previous handler, call that,
    151 	 * otherwise tell getch() to send KEY_RESIZE.
    152 	 */
    153 	if (owsa.sa_handler != SIG_DFL &&
    154 	    owsa.sa_handler != SIG_IGN &&
    155 	    owsa.sa_handler != SIG_ERR &&
    156 	    owsa.sa_handler != SIG_HOLD)
    157 		owsa.sa_handler(signo);
    158 	else
    159 		_cursesi_screen->resized = 1;
    160 }
    161 
    162 /*
    163  * Set the WINCH handler.
    164  */
    165 void
    166 __set_winchhandler(void)
    167 {
    168 
    169 #ifdef DEBUG
    170 	__CTRACE(__CTRACE_MISC, "__set_winchhandler: %d\n", winch_set);
    171 #endif
    172 	if (!winch_set) {
    173 		struct sigaction sa;
    174 
    175 		sa.sa_handler = __winch_signal_handler;
    176 		sa.sa_flags = 0;
    177 		sigemptyset(&sa.sa_mask);
    178 		sigaction(SIGWINCH, &sa, &owsa);
    179 		winch_set = 1;
    180 #ifdef DEBUG
    181 		__CTRACE(__CTRACE_MISC,
    182 		    "__set_winchhandler: owsa.sa_handler=%p\n",
    183 		    owsa.sa_handler);
    184 #endif
    185 	}
    186 }
    187 
    188 /*
    189  * Restore the WINCH handler.
    190  */
    191 void
    192 __restore_winchhandler(void)
    193 {
    194 
    195 #ifdef DEBUG
    196 	__CTRACE(__CTRACE_MISC, "__restore_winchhandler: %d\n", winch_set);
    197 #endif
    198 	if (winch_set > 0) {
    199 		struct sigaction cwsa;
    200 
    201 		sigaction(SIGWINCH, NULL, &cwsa);
    202 		if (cwsa.sa_handler == owsa.sa_handler) {
    203 			sigaction(SIGWINCH, &owsa, NULL);
    204 			winch_set = 0;
    205 		} else {
    206 			/*
    207 			 * We're now using the programs WINCH handler,
    208 			 * so don't restore the previous one.
    209 			 */
    210 			winch_set = -1;
    211 #ifdef DEBUG
    212 			__CTRACE(__CTRACE_MISC, "cwsa.sa_handler = %p\n",
    213 			    cwsa.sa_handler);
    214 #endif
    215 		}
    216 	}
    217 }
    218 
    219 /* To allow both SIGTSTP and endwin() to come back nicely, we provide
    220    the following routines. */
    221 
    222 int
    223 __stopwin(void)
    224 {
    225 
    226 #ifdef DEBUG
    227 	__CTRACE(__CTRACE_MISC, "__stopwin\n");
    228 #endif
    229 	if (_cursesi_screen == NULL)
    230 		return ERR;
    231 	if (_cursesi_screen->endwin)
    232 		return OK;
    233 
    234 	/* Get the current terminal state (which the user may have changed). */
    235 	(void)tcgetattr(fileno(_cursesi_screen->infd),
    236 			&_cursesi_screen->save_termios);
    237 
    238 	__restore_stophandler();
    239 	__restore_winchhandler();
    240 
    241 	if (curscr != NULL) {
    242 		__unsetattr(0);
    243 		__mvcur((int)curscr->cury, (int)curscr->curx,
    244 		    (int)curscr->maxy - 1, 0, 0);
    245 	}
    246 
    247 	if (meta_on != NULL)
    248 		(void)tputs(meta_on, 0, __cputchar);
    249 
    250 	if ((curscr != NULL) && (curscr->flags & __KEYPAD))
    251 		(void)tputs(keypad_local, 0, __cputchar);
    252 	(void)tputs(cursor_normal, 0, __cputchar);
    253 	(void)tputs(exit_ca_mode, 0, __cputchar);
    254 	(void)fflush(_cursesi_screen->outfd);
    255 	(void)setvbuf(_cursesi_screen->outfd, NULL, _IOLBF, 0);
    256 
    257 	_cursesi_screen->endwin = 1;
    258 
    259 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    260 	    &_cursesi_screen->orig_termios) ? ERR : OK;
    261 }
    262 
    263 void
    264 __restartwin(void)
    265 {
    266 	struct winsize win;
    267 	int nlines, ncols;
    268 
    269 #ifdef DEBUG
    270 	__CTRACE(__CTRACE_MISC, "__restartwin\n");
    271 #endif
    272 	if (!_cursesi_screen->endwin)
    273 		return;
    274 
    275 	/* Reset the curses SIGTSTP and SIGWINCH signal handlers. */
    276 	__set_stophandler();
    277 	__set_winchhandler();
    278 
    279 	/*
    280 	 * Check to see if the window size has changed.
    281 	 * If the application didn't update LINES and COLS,
    282 	 * set the * resized flag to tell getch() to push KEY_RESIZE.
    283 	 * Update curscr (which also updates __virtscr) and stdscr
    284 	 * to match the new size.
    285 	 */
    286 	if (ioctl(fileno(_cursesi_screen->outfd), TIOCGWINSZ, &win) != -1 &&
    287 	    win.ws_row != 0 && win.ws_col != 0)
    288 	{
    289 		if (win.ws_row != LINES) {
    290 			LINES = win.ws_row;
    291 			_cursesi_screen->resized = 1;
    292 		}
    293 		if (win.ws_col != COLS) {
    294 			COLS = win.ws_col;
    295 			_cursesi_screen->resized = 1;
    296 		}
    297 	}
    298 	/*
    299 	 * We need to make local copies of LINES and COLS, otherwise we
    300 	 * could lose if they are changed between wresize() calls.
    301 	 */
    302 	nlines = LINES;
    303 	ncols = COLS;
    304 	if (curscr->maxy != nlines || curscr->maxx != ncols)
    305 		wresize(curscr, nlines, ncols);
    306 	if (stdscr->maxy != nlines || stdscr->maxx != ncols)
    307 		wresize(stdscr, nlines, ncols);
    308 
    309 	/* save the new "default" terminal state */
    310 	(void)tcgetattr(fileno(_cursesi_screen->infd),
    311 			&_cursesi_screen->orig_termios);
    312 
    313 	/* Reset the terminal state to the mode just before we stopped. */
    314 	(void)tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    315 			&_cursesi_screen->save_termios);
    316 
    317 	/* Restore colours */
    318 	__restore_colors();
    319 
    320 	/* Reset meta */
    321 	__restore_meta_state();
    322 
    323 	/* Restart the screen. */
    324 	__startwin(_cursesi_screen);
    325 
    326 	/* Reset cursor visibility */
    327 	__restore_cursor_vis();
    328 
    329 	/* Repaint the screen. */
    330 	wrefresh(curscr);
    331 }
    332 
    333 int
    334 def_prog_mode(void)
    335 {
    336 
    337 	if (_cursesi_screen->endwin)
    338 		return ERR;
    339 
    340 	return tcgetattr(fileno(_cursesi_screen->infd),
    341 			 &_cursesi_screen->save_termios) ? ERR : OK;
    342 }
    343 
    344 int
    345 reset_prog_mode(void)
    346 {
    347 
    348 	return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN,
    349 			 &_cursesi_screen->save_termios) ? ERR : OK;
    350 }
    351 
    352 int
    353 def_shell_mode(void)
    354 {
    355 
    356 	return tcgetattr(fileno(_cursesi_screen->infd),
    357 			 &_cursesi_screen->orig_termios) ? ERR : OK;
    358 }
    359 
    360 int
    361 reset_shell_mode(void)
    362 {
    363 
    364 	return __stopwin();
    365 }
    366