Annotation of src/usr.bin/more/signal.c, Revision 1.1
1.1 ! deraadt 1: /*
! 2: * Copyright (c) 1988 Mark Nudleman
! 3: * Copyright (c) 1988 Regents of the University of California.
! 4: * All rights reserved.
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: * 1. Redistributions of source code must retain the above copyright
! 10: * notice, this list of conditions and the following disclaimer.
! 11: * 2. Redistributions in binary form must reproduce the above copyright
! 12: * notice, this list of conditions and the following disclaimer in the
! 13: * documentation and/or other materials provided with the distribution.
! 14: * 3. All advertising materials mentioning features or use of this software
! 15: * must display the following acknowledgement:
! 16: * This product includes software developed by the University of
! 17: * California, Berkeley and its contributors.
! 18: * 4. Neither the name of the University nor the names of its contributors
! 19: * may be used to endorse or promote products derived from this software
! 20: * without specific prior written permission.
! 21: *
! 22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 32: * SUCH DAMAGE.
! 33: */
! 34:
! 35: #ifndef lint
! 36: /* from: static char sccsid[] = "@(#)signal.c 5.8 (Berkeley) 3/1/91"; */
! 37: static char *rcsid = "$Id: signal.c,v 1.2 1993/11/09 05:13:25 cgd Exp $";
! 38: #endif /* not lint */
! 39:
! 40: /*
! 41: * Routines dealing with signals.
! 42: *
! 43: * A signal usually merely causes a bit to be set in the "signals" word.
! 44: * At some convenient time, the mainline code checks to see if any
! 45: * signals need processing by calling psignal().
! 46: * If we happen to be reading from a file [in iread()] at the time
! 47: * the signal is received, we call intread to interrupt the iread.
! 48: */
! 49:
! 50: #include <less.h>
! 51: #include <signal.h>
! 52:
! 53: /*
! 54: * "sigs" contains bits indicating signals which need to be processed.
! 55: */
! 56: int sigs;
! 57:
! 58: #ifdef SIGTSTP
! 59: #define S_STOP 02
! 60: #endif
! 61: #if defined(SIGWINCH) || defined(SIGWIND)
! 62: #define S_WINCH 04
! 63: #endif
! 64:
! 65: extern int sc_width, sc_height;
! 66: extern int screen_trashed;
! 67: extern int lnloop;
! 68: extern int linenums;
! 69: extern int scroll;
! 70: extern int reading;
! 71:
! 72: #ifdef SIGTSTP
! 73: /*
! 74: * "Stop" (^Z) signal handler.
! 75: */
! 76: static void
! 77: stop()
! 78: {
! 79: (void)signal(SIGTSTP, stop);
! 80: sigs |= S_STOP;
! 81: if (reading)
! 82: intread();
! 83: }
! 84: #endif
! 85:
! 86: #ifdef SIGWINCH
! 87: /*
! 88: * "Window" change handler
! 89: */
! 90: void
! 91: winch()
! 92: {
! 93: (void)signal(SIGWINCH, winch);
! 94: sigs |= S_WINCH;
! 95: if (reading)
! 96: intread();
! 97: }
! 98: #else
! 99: #ifdef SIGWIND
! 100: /*
! 101: * "Window" change handler
! 102: */
! 103: winch()
! 104: {
! 105: (void)signal(SIGWIND, winch);
! 106: sigs |= S_WINCH;
! 107: if (reading)
! 108: intread();
! 109: }
! 110: #endif
! 111: #endif
! 112:
! 113: static void
! 114: purgeandquit()
! 115: {
! 116:
! 117: purge(); /* purge buffered output */
! 118: quit();
! 119: }
! 120:
! 121: /*
! 122: * Set up the signal handlers.
! 123: */
! 124: init_signals(on)
! 125: int on;
! 126: {
! 127: if (on)
! 128: {
! 129: /*
! 130: * Set signal handlers.
! 131: */
! 132: (void)signal(SIGINT, purgeandquit);
! 133: #ifdef SIGTSTP
! 134: (void)signal(SIGTSTP, stop);
! 135: #endif
! 136: #ifdef SIGWINCH
! 137: (void)signal(SIGWINCH, winch);
! 138: #else
! 139: #ifdef SIGWIND
! 140: (void)signal(SIGWIND, winch);
! 141: #endif
! 142: #endif
! 143: } else
! 144: {
! 145: /*
! 146: * Restore signals to defaults.
! 147: */
! 148: (void)signal(SIGINT, SIG_DFL);
! 149: #ifdef SIGTSTP
! 150: (void)signal(SIGTSTP, SIG_DFL);
! 151: #endif
! 152: #ifdef SIGWINCH
! 153: (void)signal(SIGWINCH, SIG_IGN);
! 154: #endif
! 155: #ifdef SIGWIND
! 156: (void)signal(SIGWIND, SIG_IGN);
! 157: #endif
! 158: }
! 159: }
! 160:
! 161: /*
! 162: * Process any signals we have received.
! 163: * A received signal cause a bit to be set in "sigs".
! 164: */
! 165: psignals()
! 166: {
! 167: register int tsignals;
! 168:
! 169: if ((tsignals = sigs) == 0)
! 170: return;
! 171: sigs = 0;
! 172:
! 173: #ifdef S_WINCH
! 174: if (tsignals & S_WINCH)
! 175: {
! 176: int old_width, old_height;
! 177: /*
! 178: * Re-execute get_term() to read the new window size.
! 179: */
! 180: old_width = sc_width;
! 181: old_height = sc_height;
! 182: get_term();
! 183: if (sc_width != old_width || sc_height != old_height)
! 184: {
! 185: scroll = (sc_height + 1) / 2;
! 186: screen_trashed = 1;
! 187: }
! 188: }
! 189: #endif
! 190: #ifdef SIGTSTP
! 191: if (tsignals & S_STOP)
! 192: {
! 193: /*
! 194: * Clean up the terminal.
! 195: */
! 196: #ifdef SIGTTOU
! 197: (void)signal(SIGTTOU, SIG_IGN);
! 198: #endif
! 199: lower_left();
! 200: clear_eol();
! 201: deinit();
! 202: (void)flush();
! 203: raw_mode(0);
! 204: #ifdef SIGTTOU
! 205: (void)signal(SIGTTOU, SIG_DFL);
! 206: #endif
! 207: (void)signal(SIGTSTP, SIG_DFL);
! 208: (void)kill(getpid(), SIGTSTP);
! 209: /*
! 210: * ... Bye bye. ...
! 211: * Hopefully we'll be back later and resume here...
! 212: * Reset the terminal and arrange to repaint the
! 213: * screen when we get back to the main command loop.
! 214: */
! 215: (void)signal(SIGTSTP, stop);
! 216: raw_mode(1);
! 217: init();
! 218: screen_trashed = 1;
! 219: }
! 220: #endif
! 221: }