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