Home | History | Annotate | Line # | Download | only in libedit
sig.c revision 1.23
      1 /*	$NetBSD: sig.c,v 1.23 2016/02/16 15:54:15 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. 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 #include "config.h"
     36 #if !defined(lint) && !defined(SCCSID)
     37 #if 0
     38 static char sccsid[] = "@(#)sig.c	8.1 (Berkeley) 6/4/93";
     39 #else
     40 __RCSID("$NetBSD: sig.c,v 1.23 2016/02/16 15:54:15 christos Exp $");
     41 #endif
     42 #endif /* not lint && not SCCSID */
     43 
     44 /*
     45  * sig.c: Signal handling stuff.
     46  *	  our policy is to trap all signals, set a good state
     47  *	  and pass the ball to our caller.
     48  */
     49 #include "el.h"
     50 #include <stdlib.h>
     51 #include <errno.h>
     52 
     53 private EditLine *sel = NULL;
     54 
     55 private const int sighdl[] = {
     56 #define	_DO(a)	(a),
     57 	ALLSIGS
     58 #undef	_DO
     59 	- 1
     60 };
     61 
     62 private void sig_handler(int);
     63 
     64 /* sig_handler():
     65  *	This is the handler called for all signals
     66  *	XXX: we cannot pass any data so we just store the old editline
     67  *	state in a private variable
     68  */
     69 private void
     70 sig_handler(int signo)
     71 {
     72 	int i, save_errno;
     73 	sigset_t nset, oset;
     74 
     75 	save_errno = errno;
     76 	(void) sigemptyset(&nset);
     77 	(void) sigaddset(&nset, signo);
     78 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
     79 
     80 	sel->el_signal->sig_no = signo;
     81 
     82 	switch (signo) {
     83 	case SIGCONT:
     84 		tty_rawmode(sel);
     85 		if (ed_redisplay(sel, 0) == CC_REFRESH)
     86 			re_refresh(sel);
     87 		terminal__flush(sel);
     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) sigaction(signo, &sel->el_signal->sig_action[i], NULL);
    104 	sel->el_signal->sig_action[i].sa_handler = SIG_ERR;
    105 	sel->el_signal->sig_action[i].sa_flags = 0;
    106 	sigemptyset(&sel->el_signal->sig_action[i].sa_mask);
    107 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    108 	(void) kill(0, signo);
    109 	errno = save_errno;
    110 }
    111 
    112 
    113 /* sig_init():
    114  *	Initialize all signal stuff
    115  */
    116 protected int
    117 sig_init(EditLine *el)
    118 {
    119 	size_t i;
    120 	sigset_t *nset, oset;
    121 
    122 	el->el_signal = el_malloc(sizeof(*el->el_signal));
    123 	if (el->el_signal == NULL)
    124 		return -1;
    125 
    126 	nset = &el->el_signal->sig_set;
    127 	(void) sigemptyset(nset);
    128 #define	_DO(a) (void) sigaddset(nset, a);
    129 	ALLSIGS
    130 #undef	_DO
    131 	(void) sigprocmask(SIG_BLOCK, nset, &oset);
    132 
    133 	for (i = 0; sighdl[i] != -1; i++) {
    134 		el->el_signal->sig_action[i].sa_handler = SIG_ERR;
    135 		el->el_signal->sig_action[i].sa_flags = 0;
    136 		sigemptyset(&el->el_signal->sig_action[i].sa_mask);
    137 	}
    138 
    139 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    140 
    141 	return 0;
    142 }
    143 
    144 
    145 /* sig_end():
    146  *	Clear all signal stuff
    147  */
    148 protected void
    149 sig_end(EditLine *el)
    150 {
    151 
    152 	el_free(el->el_signal);
    153 	el->el_signal = NULL;
    154 }
    155 
    156 
    157 /* sig_set():
    158  *	set all the signal handlers
    159  */
    160 protected void
    161 sig_set(EditLine *el)
    162 {
    163 	size_t i;
    164 	sigset_t oset;
    165 	struct sigaction osa, nsa;
    166 
    167 	nsa.sa_handler = sig_handler;
    168 	nsa.sa_flags = 0;
    169 	sigemptyset(&nsa.sa_mask);
    170 
    171 	(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
    172 
    173 	for (i = 0; sighdl[i] != -1; i++) {
    174 		/* This could happen if we get interrupted */
    175 		if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
    176 		    osa.sa_handler != sig_handler)
    177 			el->el_signal->sig_action[i] = osa;
    178 	}
    179 	sel = el;
    180 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    181 }
    182 
    183 
    184 /* sig_clr():
    185  *	clear all the signal handlers
    186  */
    187 protected void
    188 sig_clr(EditLine *el)
    189 {
    190 	size_t i;
    191 	sigset_t oset;
    192 
    193 	(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
    194 
    195 	for (i = 0; sighdl[i] != -1; i++)
    196 		if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
    197 			(void)sigaction(sighdl[i],
    198 			    &el->el_signal->sig_action[i], NULL);
    199 
    200 	sel = NULL;		/* we are going to die if the handler is
    201 				 * called */
    202 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    203 }
    204