xref: /netbsd-src/lib/libedit/sig.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: sig.c,v 1.11 2003/08/07 16:44:33 agc 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.11 2003/08/07 16:44:33 agc 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 
52 private EditLine *sel = NULL;
53 
54 private const int sighdl[] = {
55 #define	_DO(a)	(a),
56 	ALLSIGS
57 #undef	_DO
58 	- 1
59 };
60 
61 private void sig_handler(int);
62 
63 /* sig_handler():
64  *	This is the handler called for all signals
65  *	XXX: we cannot pass any data so we just store the old editline
66  *	state in a private variable
67  */
68 private void
69 sig_handler(int signo)
70 {
71 	int i;
72 	sigset_t nset, oset;
73 
74 	(void) sigemptyset(&nset);
75 	(void) sigaddset(&nset, signo);
76 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
77 
78 	switch (signo) {
79 	case SIGCONT:
80 		tty_rawmode(sel);
81 		if (ed_redisplay(sel, 0) == CC_REFRESH)
82 			re_refresh(sel);
83 		term__flush();
84 		break;
85 
86 	case SIGWINCH:
87 		el_resize(sel);
88 		break;
89 
90 	default:
91 		tty_cookedmode(sel);
92 		break;
93 	}
94 
95 	for (i = 0; sighdl[i] != -1; i++)
96 		if (signo == sighdl[i])
97 			break;
98 
99 	(void) signal(signo, sel->el_signal[i]);
100 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
101 	(void) kill(0, signo);
102 }
103 
104 
105 /* sig_init():
106  *	Initialize all signal stuff
107  */
108 protected int
109 sig_init(EditLine *el)
110 {
111 	int i;
112 	sigset_t nset, oset;
113 
114 	(void) sigemptyset(&nset);
115 #define	_DO(a) (void) sigaddset(&nset, a);
116 	ALLSIGS
117 #undef	_DO
118 	    (void) sigprocmask(SIG_BLOCK, &nset, &oset);
119 
120 #define	SIGSIZE (sizeof(sighdl) / sizeof(sighdl[0]) * sizeof(el_signalhandler_t))
121 
122 	el->el_signal = (el_signalhandler_t *) el_malloc(SIGSIZE);
123 	if (el->el_signal == NULL)
124 		return (-1);
125 	for (i = 0; sighdl[i] != -1; i++)
126 		el->el_signal[i] = SIG_ERR;
127 
128 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
129 
130 	return (0);
131 }
132 
133 
134 /* sig_end():
135  *	Clear all signal stuff
136  */
137 protected void
138 sig_end(EditLine *el)
139 {
140 
141 	el_free((ptr_t) el->el_signal);
142 	el->el_signal = NULL;
143 }
144 
145 
146 /* sig_set():
147  *	set all the signal handlers
148  */
149 protected void
150 sig_set(EditLine *el)
151 {
152 	int i;
153 	sigset_t nset, oset;
154 
155 	(void) sigemptyset(&nset);
156 #define	_DO(a) (void) sigaddset(&nset, a);
157 	ALLSIGS
158 #undef	_DO
159 	    (void) sigprocmask(SIG_BLOCK, &nset, &oset);
160 
161 	for (i = 0; sighdl[i] != -1; i++) {
162 		el_signalhandler_t s;
163 		/* This could happen if we get interrupted */
164 		if ((s = signal(sighdl[i], sig_handler)) != sig_handler)
165 			el->el_signal[i] = s;
166 	}
167 	sel = el;
168 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
169 }
170 
171 
172 /* sig_clr():
173  *	clear all the signal handlers
174  */
175 protected void
176 sig_clr(EditLine *el)
177 {
178 	int i;
179 	sigset_t nset, oset;
180 
181 	(void) sigemptyset(&nset);
182 #define	_DO(a) (void) sigaddset(&nset, a);
183 	ALLSIGS
184 #undef	_DO
185 	    (void) sigprocmask(SIG_BLOCK, &nset, &oset);
186 
187 	for (i = 0; sighdl[i] != -1; i++)
188 		if (el->el_signal[i] != SIG_ERR)
189 			(void) signal(sighdl[i], el->el_signal[i]);
190 
191 	sel = NULL;		/* we are going to die if the handler is
192 				 * called */
193 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
194 }
195