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