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