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