Home | History | Annotate | Line # | Download | only in libcurses
tstp.c revision 1.6
      1 /*
      2  * Copyright (c) 1981, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /* from: static char sccsid[] = "@(#)tstp.c	8.2 (Berkeley) 11/2/93"; */
     36 static char *rcsid = "$Id: tstp.c,v 1.6 1993/11/09 04:08:55 cgd Exp $";
     37 #endif /* not lint */
     38 
     39 #include <curses.h>
     40 #include <errno.h>
     41 #include <signal.h>
     42 #include <termios.h>
     43 #include <unistd.h>
     44 
     45 
     46 /*
     47  * stop_signal_handler --
     48  *	Handle stop signals.
     49  */
     50 void
     51 __stop_signal_handler(signo)
     52 	int signo;
     53 {
     54 	struct termios save;
     55 	sigset_t oset, set;
     56 
     57 	/* Get the current terminal state (which the user may have changed). */
     58 	if (tcgetattr(STDIN_FILENO, &save))
     59 		return;
     60 
     61 	/*
     62 	 * Block window change and timer signals.  The latter is because
     63 	 * applications use timers to decide when to repaint the screen.
     64 	 */
     65 	(void)sigemptyset(&set);
     66 	(void)sigaddset(&set, SIGALRM);
     67 	(void)sigaddset(&set, SIGWINCH);
     68 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
     69 
     70 	/*
     71 	 * End the window, which also resets the terminal state to the
     72 	 * original modes.
     73 	 */
     74 	endwin();
     75 
     76 	/* Unblock SIGTSTP. */
     77 	(void)sigemptyset(&set);
     78 	(void)sigaddset(&set, SIGTSTP);
     79 	(void)sigprocmask(SIG_UNBLOCK, &set, NULL);
     80 
     81 	/* Stop ourselves. */
     82 	__restore_stophandler();
     83 	(void)kill(0, SIGTSTP);
     84 
     85 	/* Time passes ... */
     86 
     87 	/* Reset the curses SIGTSTP signal handler. */
     88 	__set_stophandler();
     89 
     90 	/* save the new "default" terminal state */
     91 	(void)tcgetattr(STDIN_FILENO, &__orig_termios);
     92 
     93 	/* Reset the terminal state to the mode just before we stopped. */
     94 	(void)tcsetattr(STDIN_FILENO, __tcaction ?
     95 	    TCSASOFT | TCSADRAIN : TCSADRAIN, &save);
     96 
     97 	/* Restart the screen. */
     98 	__startwin();
     99 
    100 	/* Repaint the screen. */
    101 	wrefresh(curscr);
    102 
    103 	/* Reset the signals. */
    104 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    105 }
    106 
    107 static void (*otstpfn)() = SIG_DFL;
    108 
    109 /*
    110  * Set the TSTP handler.
    111  */
    112 void
    113 __set_stophandler()
    114 {
    115 	otstpfn = signal(SIGTSTP, __stop_signal_handler);
    116 }
    117 
    118 /*
    119  * Restore the TSTP handler.
    120  */
    121 void
    122 __restore_stophandler()
    123 {
    124 	(void)signal(SIGTSTP, otstpfn);
    125 }
    126