Home | History | Annotate | Line # | Download | only in libedit
sig.c revision 1.21
      1 /*	$NetBSD: sig.c,v 1.21 2016/02/16 14:08:25 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.21 2016/02/16 14:08:25 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 <errno.h>
     50 #include <stdlib.h>
     51 
     52 #include "histedit.h"
     53 #include "chartype.h"
     54 #include "el.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, save_errno;
     76 	sigset_t nset, oset;
     77 
     78 	save_errno = errno;
     79 	(void) sigemptyset(&nset);
     80 	(void) sigaddset(&nset, signo);
     81 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
     82 
     83 	sel->el_signal->sig_no = signo;
     84 
     85 	switch (signo) {
     86 	case SIGCONT:
     87 		tty_rawmode(sel);
     88 		if (ed_redisplay(sel, 0) == CC_REFRESH)
     89 			re_refresh(sel);
     90 		terminal__flush(sel);
     91 		break;
     92 
     93 	case SIGWINCH:
     94 		el_resize(sel);
     95 		break;
     96 
     97 	default:
     98 		tty_cookedmode(sel);
     99 		break;
    100 	}
    101 
    102 	for (i = 0; sighdl[i] != -1; i++)
    103 		if (signo == sighdl[i])
    104 			break;
    105 
    106 	(void) sigaction(signo, &sel->el_signal->sig_action[i], NULL);
    107 	sel->el_signal->sig_action[i].sa_handler = SIG_ERR;
    108 	sel->el_signal->sig_action[i].sa_flags = 0;
    109 	sigemptyset(&sel->el_signal->sig_action[i].sa_mask);
    110 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    111 	(void) kill(0, signo);
    112 	errno = save_errno;
    113 }
    114 
    115 
    116 /* sig_init():
    117  *	Initialize all signal stuff
    118  */
    119 protected int
    120 sig_init(EditLine *el)
    121 {
    122 	size_t i;
    123 	sigset_t *nset, oset;
    124 
    125 	el->el_signal = el_malloc(sizeof(*el->el_signal));
    126 	if (el->el_signal == NULL)
    127 		return -1;
    128 
    129 	nset = &el->el_signal->sig_set;
    130 	(void) sigemptyset(nset);
    131 #define	_DO(a) (void) sigaddset(nset, a);
    132 	ALLSIGS
    133 #undef	_DO
    134 	(void) sigprocmask(SIG_BLOCK, nset, &oset);
    135 
    136 	for (i = 0; sighdl[i] != -1; i++) {
    137 		el->el_signal->sig_action[i].sa_handler = SIG_ERR;
    138 		el->el_signal->sig_action[i].sa_flags = 0;
    139 		sigemptyset(&el->el_signal->sig_action[i].sa_mask);
    140 	}
    141 
    142 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    143 
    144 	return 0;
    145 }
    146 
    147 
    148 /* sig_end():
    149  *	Clear all signal stuff
    150  */
    151 protected void
    152 sig_end(EditLine *el)
    153 {
    154 
    155 	el_free(el->el_signal);
    156 	el->el_signal = NULL;
    157 }
    158 
    159 
    160 /* sig_set():
    161  *	set all the signal handlers
    162  */
    163 protected void
    164 sig_set(EditLine *el)
    165 {
    166 	size_t i;
    167 	sigset_t oset;
    168 	struct sigaction osa, nsa;
    169 
    170 	nsa.sa_handler = sig_handler;
    171 	nsa.sa_flags = 0;
    172 	sigemptyset(&nsa.sa_mask);
    173 
    174 	(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
    175 
    176 	for (i = 0; sighdl[i] != -1; i++) {
    177 		/* This could happen if we get interrupted */
    178 		if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
    179 		    osa.sa_handler != sig_handler)
    180 			el->el_signal->sig_action[i] = osa;
    181 	}
    182 	sel = el;
    183 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    184 }
    185 
    186 
    187 /* sig_clr():
    188  *	clear all the signal handlers
    189  */
    190 protected void
    191 sig_clr(EditLine *el)
    192 {
    193 	size_t i;
    194 	sigset_t oset;
    195 
    196 	(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
    197 
    198 	for (i = 0; sighdl[i] != -1; i++)
    199 		if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
    200 			(void)sigaction(sighdl[i],
    201 			    &el->el_signal->sig_action[i], NULL);
    202 
    203 	sel = NULL;		/* we are going to die if the handler is
    204 				 * called */
    205 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    206 }
    207