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