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