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