Home | History | Annotate | Line # | Download | only in libcurses
tstp.c revision 1.15
      1 /*	$NetBSD: tstp.c,v 1.15 2000/04/12 21:36:36 jdc 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)tstp.c	8.3 (Berkeley) 5/4/94";
     40 #else
     41 __RCSID("$NetBSD: tstp.c,v 1.15 2000/04/12 21:36:36 jdc Exp $");
     42 #endif
     43 #endif				/* not lint */
     44 
     45 #include <errno.h>
     46 #include <signal.h>
     47 #include <termios.h>
     48 #include <unistd.h>
     49 
     50 #include "curses.h"
     51 #include "curses_private.h"
     52 
     53 /*
     54  * stop_signal_handler --
     55  *	Handle stop signals.
     56  */
     57 void
     58 __stop_signal_handler(/*ARGSUSED*/signo)
     59 	int	signo;
     60 {
     61 	sigset_t oset, set;
     62 
     63 	/*
     64 	 * Block window change and timer signals.  The latter is because
     65 	 * applications use timers to decide when to repaint the screen.
     66 	 */
     67 	(void) sigemptyset(&set);
     68 	(void) sigaddset(&set, SIGALRM);
     69 	(void) sigaddset(&set, SIGWINCH);
     70 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
     71 
     72 	/*
     73 	 * End the window, which also resets the terminal state to the
     74 	 * original modes.
     75 	 */
     76 	__stopwin();
     77 
     78 	/* Unblock SIGTSTP. */
     79 	(void) sigemptyset(&set);
     80 	(void) sigaddset(&set, SIGTSTP);
     81 	(void) sigprocmask(SIG_UNBLOCK, &set, NULL);
     82 
     83 	/* Stop ourselves. */
     84 	(void) kill(0, SIGTSTP);
     85 
     86 	/* Time passes ... */
     87 
     88 	/* restart things */
     89 	__restartwin();
     90 
     91 	/* Reset the signals. */
     92 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
     93 }
     94 
     95 static void (*otstpfn)
     96 __P((int)) = SIG_DFL;
     97 
     98 /*
     99  * Set the TSTP handler.
    100  */
    101 void
    102 __set_stophandler()
    103 {
    104 	otstpfn = signal(SIGTSTP, __stop_signal_handler);
    105 }
    106 
    107 /*
    108  * Restore the TSTP handler.
    109  */
    110 void
    111 __restore_stophandler()
    112 {
    113 	(void) signal(SIGTSTP, otstpfn);
    114 }
    115 
    116 
    117 /* To allow both SIGTSTP and endwin() to come back nicely, we provide
    118    the following routines. */
    119 
    120 static struct termios save_termios;
    121 
    122 int
    123 __stopwin()
    124 {
    125 	/* Get the current terminal state (which the user may have changed). */
    126 	(void) tcgetattr(STDIN_FILENO, &save_termios);
    127 
    128 	__restore_stophandler();
    129 
    130 	if (curscr != NULL) {
    131 		if (curscr->wattr & __STANDOUT && SE != NULL) {
    132 			tputs(SE, 0, __cputchar);
    133 			curscr->wattr &= ~__STANDOUT;
    134 			if (UE != NULL && !strcmp(SE, UE)) {
    135 				curscr->wattr &= ~__UNDERSCORE;
    136 			}
    137 			if (ME != NULL && !strcmp(SE, ME)) {
    138 				curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
    139 			}
    140 			if (OP != NULL && !strcmp(SE, OP)) {
    141 				curscr->wattr &= ~__COLOR;
    142 			}
    143 
    144 		}
    145 		if (curscr->wattr & __UNDERSCORE && UE != NULL) {
    146 			tputs(UE, 0, __cputchar);
    147 			curscr->wattr &= ~__UNDERSCORE;
    148 			if (ME != NULL && !strcmp(UE, ME)) {
    149 				curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
    150 			}
    151 			if (OP != NULL && !strcmp(UE, OP)) {
    152 				curscr->wattr &= ~__COLOR;
    153 			}
    154 		}
    155 		if (curscr->wattr & __ATTRIBUTES && ME != NULL) {
    156 			tputs(ME, 0, __cputchar);
    157 			curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
    158 			if (OP != NULL && !strcmp(ME, OP)) {
    159 				curscr->wattr &= ~__COLOR;
    160 			}
    161 		}
    162 		if (curscr->wattr & __ALTCHARSET && AE != NULL) {
    163 			tputs(AE, 0, __cputchar);
    164 			curscr->wattr &= ~__ALTCHARSET;
    165 		}
    166 		if (curscr->wattr & __COLOR) {
    167 			if (OC != NULL)
    168 				tputs(OC, 0, __cputchar);
    169 			if (OP != NULL)
    170 				tputs(OP, 0, __cputchar);
    171 			curscr->wattr &= ~__COLOR;
    172 		}
    173 		__mvcur((int) curscr->cury, (int) curscr->curx, (int) curscr->maxy - 1, 0, 0);
    174 	}
    175 
    176 	(void) tputs(KE, 0, __cputchar);
    177 	(void) tputs(VE, 0, __cputchar);
    178 	(void) tputs(TE, 0, __cputchar);
    179 	(void) fflush(stdout);
    180 	(void) setvbuf(stdout, NULL, _IOLBF, (size_t) 0);
    181 
    182 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    183 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios) ? ERR : OK);
    184 }
    185 
    186 
    187 void
    188 __restartwin()
    189 {
    190 	/* Reset the curses SIGTSTP signal handler. */
    191 	__set_stophandler();
    192 
    193 	/* save the new "default" terminal state */
    194 	(void) tcgetattr(STDIN_FILENO, &__orig_termios);
    195 
    196 	/* Reset the terminal state to the mode just before we stopped. */
    197 	(void) tcsetattr(STDIN_FILENO, __tcaction ?
    198 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &save_termios);
    199 
    200 	/* Restore colours */
    201 	__restore_colors();
    202 
    203 	/* Restart the screen. */
    204 	__startwin();
    205 
    206 	/* Repaint the screen. */
    207 	wrefresh(curscr);
    208 }
    209 
    210 int
    211 def_prog_mode()
    212 {
    213 	return (tcgetattr(STDIN_FILENO, &save_termios) ? ERR : OK);
    214 }
    215 
    216 int
    217 reset_prog_mode()
    218 {
    219 	return (tcsetattr(STDIN_FILENO, __tcaction ?
    220 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &save_termios) ? ERR : OK);
    221 }
    222