Home | History | Annotate | Line # | Download | only in libedit
sig.c revision 1.9
      1 /*	$NetBSD: sig.c,v 1.9 2002/03/18 16:00:58 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Christos Zoulas of Cornell University.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include "config.h"
     40 #if !defined(lint) && !defined(SCCSID)
     41 #if 0
     42 static char sccsid[] = "@(#)sig.c	8.1 (Berkeley) 6/4/93";
     43 #else
     44 __RCSID("$NetBSD: sig.c,v 1.9 2002/03/18 16:00:58 christos Exp $");
     45 #endif
     46 #endif /* not lint && not SCCSID */
     47 
     48 /*
     49  * sig.c: Signal handling stuff.
     50  *	  our policy is to trap all signals, set a good state
     51  *	  and pass the ball to our caller.
     52  */
     53 #include "el.h"
     54 #include <stdlib.h>
     55 
     56 private EditLine *sel = NULL;
     57 
     58 private const int sighdl[] = {
     59 #define	_DO(a)	(a),
     60 	ALLSIGS
     61 #undef	_DO
     62 	- 1
     63 };
     64 
     65 private void sig_handler(int);
     66 
     67 /* sig_handler():
     68  *	This is the handler called for all signals
     69  *	XXX: we cannot pass any data so we just store the old editline
     70  *	state in a private variable
     71  */
     72 private void
     73 sig_handler(int signo)
     74 {
     75 	int i;
     76 	sigset_t nset, oset;
     77 
     78 	(void) sigemptyset(&nset);
     79 	(void) sigaddset(&nset, signo);
     80 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
     81 
     82 	switch (signo) {
     83 	case SIGCONT:
     84 		tty_rawmode(sel);
     85 		if (ed_redisplay(sel, 0) == CC_REFRESH)
     86 			re_refresh(sel);
     87 		term__flush();
     88 		break;
     89 
     90 	case SIGWINCH:
     91 		el_resize(sel);
     92 		break;
     93 
     94 	default:
     95 		tty_cookedmode(sel);
     96 		break;
     97 	}
     98 
     99 	for (i = 0; sighdl[i] != -1; i++)
    100 		if (signo == sighdl[i])
    101 			break;
    102 
    103 	(void) signal(signo, sel->el_signal[i]);
    104 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    105 	(void) kill(0, signo);
    106 }
    107 
    108 
    109 /* sig_init():
    110  *	Initialize all signal stuff
    111  */
    112 protected int
    113 sig_init(EditLine *el)
    114 {
    115 	int i;
    116 	sigset_t nset, oset;
    117 
    118 	(void) sigemptyset(&nset);
    119 #define	_DO(a) (void) sigaddset(&nset, a);
    120 	ALLSIGS
    121 #undef	_DO
    122 	    (void) sigprocmask(SIG_BLOCK, &nset, &oset);
    123 
    124 #define	SIGSIZE (sizeof(sighdl) / sizeof(sighdl[0]) * sizeof(sig_t))
    125 
    126 	el->el_signal = (sig_t *) el_malloc(SIGSIZE);
    127 	if (el->el_signal == NULL)
    128 		return (-1);
    129 	for (i = 0; sighdl[i] != -1; i++)
    130 		el->el_signal[i] = SIG_ERR;
    131 
    132 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    133 
    134 	return (0);
    135 }
    136 
    137 
    138 /* sig_end():
    139  *	Clear all signal stuff
    140  */
    141 protected void
    142 sig_end(EditLine *el)
    143 {
    144 
    145 	el_free((ptr_t) el->el_signal);
    146 	el->el_signal = NULL;
    147 }
    148 
    149 
    150 /* sig_set():
    151  *	set all the signal handlers
    152  */
    153 protected void
    154 sig_set(EditLine *el)
    155 {
    156 	int i;
    157 	sigset_t nset, oset;
    158 
    159 	(void) sigemptyset(&nset);
    160 #define	_DO(a) (void) sigaddset(&nset, a);
    161 	ALLSIGS
    162 #undef	_DO
    163 	    (void) sigprocmask(SIG_BLOCK, &nset, &oset);
    164 
    165 	for (i = 0; sighdl[i] != -1; i++) {
    166 		sig_t s;
    167 		/* This could happen if we get interrupted */
    168 		if ((s = signal(sighdl[i], sig_handler)) != sig_handler)
    169 			el->el_signal[i] = s;
    170 	}
    171 	sel = el;
    172 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    173 }
    174 
    175 
    176 /* sig_clr():
    177  *	clear all the signal handlers
    178  */
    179 protected void
    180 sig_clr(EditLine *el)
    181 {
    182 	int i;
    183 	sigset_t nset, oset;
    184 
    185 	(void) sigemptyset(&nset);
    186 #define	_DO(a) (void) sigaddset(&nset, a);
    187 	ALLSIGS
    188 #undef	_DO
    189 	    (void) sigprocmask(SIG_BLOCK, &nset, &oset);
    190 
    191 	for (i = 0; sighdl[i] != -1; i++)
    192 		if (el->el_signal[i] != SIG_ERR)
    193 			(void) signal(sighdl[i], el->el_signal[i]);
    194 
    195 	sel = NULL;		/* we are going to die if the handler is
    196 				 * called */
    197 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    198 }
    199