xref: /openbsd/lib/libedit/sig.c (revision fd84ef7e)
1 /*	$OpenBSD: sig.c,v 1.6 2001/12/06 04:26:00 deraadt Exp $	*/
2 /*	$NetBSD: sig.c,v 1.3 1997/04/11 17:52:48 christos 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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #if !defined(lint) && !defined(SCCSID)
41 #if 0
42 static char sccsid[] = "@(#)sig.c	8.1 (Berkeley) 6/4/93";
43 #else
44 static char rcsid[] = "$OpenBSD: sig.c,v 1.6 2001/12/06 04:26:00 deraadt 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	__P((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(signo)
75     int signo;
76 {
77     int save_errno = errno;
78     int i;
79     sigset_t nset, oset;
80 
81     (void)sigemptyset(&nset);
82     (void)sigaddset(&nset, signo);
83     (void)sigprocmask(SIG_BLOCK, &nset, &oset);
84 
85     switch (signo) {
86     case SIGCONT:
87 	tty_rawmode(sel);				/* XXX signal race */
88 	if (ed_redisplay(sel, 0) == CC_REFRESH)		/* XXX signal race */
89 	    re_refresh(sel);				/* XXX signal race */
90 	term__flush();					/* XXX signal race */
91 	break;
92 
93     case SIGWINCH:
94 	el_resize(sel);					/* XXX signal race */
95 	break;
96 
97     default:
98 	tty_cookedmode(sel);				/* XXX signal race */
99 	break;
100     }
101 
102     for (i = 0; sighdl[i] != -1; i++)
103 	if (signo == sighdl[i])
104 	    break;
105 
106     (void)signal(signo, sel->el_signal[i]);
107     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
108     (void)kill(0, signo);
109     errno = save_errno;
110 }
111 
112 
113 /* sig_init():
114  *	Initialize all signal stuff
115  */
116 protected int
117 sig_init(el)
118     EditLine *el;
119 {
120     int i;
121     sigset_t nset, oset;
122 
123     (void)sigemptyset(&nset);
124 #define _DO(a) (void)sigaddset(&nset, a);
125     ALLSIGS
126 #undef _DO
127     (void)sigprocmask(SIG_BLOCK, &nset, &oset);
128 
129 #define SIGSIZE (sizeof(sighdl) / sizeof(sighdl[0]) * sizeof(sig_t))
130 
131     el->el_signal = (sig_t *) el_malloc(SIGSIZE);
132     for (i = 0; sighdl[i] != -1; i++)
133 	el->el_signal[i] = SIG_ERR;
134 
135     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
136 
137     return 0;
138 }
139 
140 
141 /* sig_end():
142  *	Clear all signal stuff
143  */
144 protected void
145 sig_end(el)
146     EditLine *el;
147 {
148     el_free((ptr_t) el->el_signal);
149     el->el_signal = NULL;
150 }
151 
152 
153 /* sig_set():
154  *	set all the signal handlers
155  */
156 protected void
157 sig_set(el)
158     EditLine *el;
159 {
160     int i;
161     sigset_t nset, oset;
162 
163     (void)sigemptyset(&nset);
164 #define _DO(a) (void)sigaddset(&nset, a);
165     ALLSIGS
166 #undef _DO
167     (void)sigprocmask(SIG_BLOCK, &nset, &oset);
168 
169     for (i = 0; sighdl[i] != -1; i++) {
170 	sig_t s;
171 	/* This could happen if we get interrupted */
172 	if ((s = signal(sighdl[i], sig_handler)) != sig_handler)
173 	    el->el_signal[i] = s;
174     }
175     sel = el;
176     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
177 }
178 
179 
180 /* sig_clr():
181  *	clear all the signal handlers
182  */
183 protected void
184 sig_clr(el)
185     EditLine *el;
186 {
187     int i;
188     sigset_t nset, oset;
189 
190     (void)sigemptyset(&nset);
191 #define _DO(a) (void)sigaddset(&nset, a);
192     ALLSIGS
193 #undef _DO
194     (void)sigprocmask(SIG_BLOCK, &nset, &oset);
195 
196     for (i = 0; sighdl[i] != -1; i++)
197 	if (el->el_signal[i] != SIG_ERR)
198 	    (void)signal(sighdl[i], el->el_signal[i]);
199 
200     sel = NULL;	/* we are going to die if the handler is called */
201     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
202 }
203