[BACK]Return to tstp.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libcurses

Annotation of src/lib/libcurses/tstp.c, Revision 1.21

1.21    ! jdc         1: /*     $NetBSD: tstp.c,v 1.20 2000/05/22 05:54:37 jdc Exp $    */
1.9       mikel       2:
1.1       cgd         3: /*
1.8       cgd         4:  * Copyright (c) 1981, 1993, 1994
1.5       cgd         5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         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:
1.9       mikel      36: #include <sys/cdefs.h>
1.1       cgd        37: #ifndef lint
1.9       mikel      38: #if 0
1.8       cgd        39: static char sccsid[] = "@(#)tstp.c     8.3 (Berkeley) 5/4/94";
1.9       mikel      40: #else
1.21    ! jdc        41: __RCSID("$NetBSD: tstp.c,v 1.20 2000/05/22 05:54:37 jdc Exp $");
1.9       mikel      42: #endif
1.13      mrg        43: #endif                         /* not lint */
1.1       cgd        44:
1.3       mycroft    45: #include <errno.h>
                     46: #include <signal.h>
                     47: #include <termios.h>
                     48: #include <unistd.h>
1.1       cgd        49:
1.8       cgd        50: #include "curses.h"
1.14      blymn      51: #include "curses_private.h"
1.5       cgd        52:
1.1       cgd        53: /*
1.5       cgd        54:  * stop_signal_handler --
                     55:  *     Handle stop signals.
1.1       cgd        56:  */
                     57: void
1.16      blymn      58: __stop_signal_handler(/*ARGSUSED*/int signo)
1.3       mycroft    59: {
1.5       cgd        60:        sigset_t oset, set;
1.1       cgd        61:
1.5       cgd        62:        /*
                     63:         * Block window change and timer signals.  The latter is because
                     64:         * applications use timers to decide when to repaint the screen.
                     65:         */
1.13      mrg        66:        (void) sigemptyset(&set);
                     67:        (void) sigaddset(&set, SIGALRM);
                     68:        (void) sigaddset(&set, SIGWINCH);
                     69:        (void) sigprocmask(SIG_BLOCK, &set, &oset);
                     70:
1.5       cgd        71:        /*
                     72:         * End the window, which also resets the terminal state to the
                     73:         * original modes.
                     74:         */
1.10      phil       75:        __stopwin();
1.3       mycroft    76:
1.5       cgd        77:        /* Unblock SIGTSTP. */
1.13      mrg        78:        (void) sigemptyset(&set);
                     79:        (void) sigaddset(&set, SIGTSTP);
                     80:        (void) sigprocmask(SIG_UNBLOCK, &set, NULL);
1.5       cgd        81:
                     82:        /* Stop ourselves. */
1.13      mrg        83:        (void) kill(0, SIGTSTP);
1.3       mycroft    84:
                     85:        /* Time passes ... */
                     86:
1.10      phil       87:        /* restart things */
                     88:        __restartwin();
1.5       cgd        89:
                     90:        /* Reset the signals. */
1.13      mrg        91:        (void) sigprocmask(SIG_SETMASK, &oset, NULL);
1.5       cgd        92: }
                     93:
1.13      mrg        94: static void (*otstpfn)
                     95: __P((int)) = SIG_DFL;
1.5       cgd        96:
                     97: /*
                     98:  * Set the TSTP handler.
                     99:  */
                    100: void
1.16      blymn     101: __set_stophandler(void)
1.5       cgd       102: {
                    103:        otstpfn = signal(SIGTSTP, __stop_signal_handler);
                    104: }
                    105:
                    106: /*
                    107:  * Restore the TSTP handler.
                    108:  */
                    109: void
1.16      blymn     110: __restore_stophandler(void)
1.5       cgd       111: {
1.13      mrg       112:        (void) signal(SIGTSTP, otstpfn);
1.10      phil      113: }
                    114:
                    115:
                    116: /* To allow both SIGTSTP and endwin() to come back nicely, we provide
                    117:    the following routines. */
                    118:
1.11      phil      119: static struct termios save_termios;
1.10      phil      120:
                    121: int
1.16      blymn     122: __stopwin(void)
1.10      phil      123: {
                    124:        /* Get the current terminal state (which the user may have changed). */
1.13      mrg       125:        (void) tcgetattr(STDIN_FILENO, &save_termios);
1.10      phil      126:
                    127:        __restore_stophandler();
                    128:
                    129:        if (curscr != NULL) {
1.18      jdc       130:                __unsetattr(0);
1.13      mrg       131:                __mvcur((int) curscr->cury, (int) curscr->curx, (int) curscr->maxy - 1, 0, 0);
1.10      phil      132:        }
                    133:
1.17      blymn     134:        if (MO != NULL)
                    135:                (void) tputs(MO, 0, __cputchar);
                    136:
1.19      jdc       137:        if (curscr->flags & __KEYPAD)
                    138:                (void) tputs(KE, 0, __cputchar);
1.13      mrg       139:        (void) tputs(VE, 0, __cputchar);
                    140:        (void) tputs(TE, 0, __cputchar);
                    141:        (void) fflush(stdout);
1.14      blymn     142:        (void) setvbuf(stdout, NULL, _IOLBF, (size_t) 0);
1.21    ! jdc       143:
        !           144:        __endwin = 1;
1.10      phil      145:
                    146:        return (tcsetattr(STDIN_FILENO, __tcaction ?
                    147:            TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios) ? ERR : OK);
                    148: }
                    149:
                    150:
                    151: void
1.16      blymn     152: __restartwin(void)
1.10      phil      153: {
                    154:        /* Reset the curses SIGTSTP signal handler. */
                    155:        __set_stophandler();
                    156:
                    157:        /* save the new "default" terminal state */
1.13      mrg       158:        (void) tcgetattr(STDIN_FILENO, &__orig_termios);
1.10      phil      159:
                    160:        /* Reset the terminal state to the mode just before we stopped. */
1.13      mrg       161:        (void) tcsetattr(STDIN_FILENO, __tcaction ?
1.11      phil      162:            TCSASOFT | TCSADRAIN : TCSADRAIN, &save_termios);
1.10      phil      163:
1.15      jdc       164:        /* Restore colours */
                    165:        __restore_colors();
                    166:
1.17      blymn     167:        /* Reset meta */
                    168:        __restore_meta_state();
                    169:
                    170:        /* Reset cursor visibility */
                    171:        __restore_cursor_vis();
                    172:
1.10      phil      173:        /* Restart the screen. */
                    174:        __startwin();
1.11      phil      175:
                    176:        /* Repaint the screen. */
                    177:        wrefresh(curscr);
1.15      jdc       178: }
                    179:
                    180: int
1.16      blymn     181: def_prog_mode(void)
1.15      jdc       182: {
                    183:        return (tcgetattr(STDIN_FILENO, &save_termios) ? ERR : OK);
                    184: }
                    185:
                    186: int
1.16      blymn     187: reset_prog_mode(void)
1.15      jdc       188: {
1.20      jdc       189:        __restartwin();
                    190:        return(OK);
                    191: }
                    192:
                    193: int
                    194: def_shell_mode(void)
                    195: {
                    196:        return (tcgetattr(STDIN_FILENO, &__orig_termios) ? ERR : OK);
                    197: }
                    198:
                    199: int
                    200: reset_shell_mode(void)
                    201: {
                    202:        return (__stopwin());
1.1       cgd       203: }

CVSweb <webmaster@jp.NetBSD.org>