xref: /openbsd-src/lib/libedit/sig.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: sig.c,v 1.9 2003/06/02 20:18:40 millert 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. 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 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)sig.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static const char rcsid[] = "$OpenBSD: sig.c,v 1.9 2003/06/02 20:18:40 millert 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 "sys.h"
50 #include "el.h"
51 #include <stdlib.h>
52 
53 private EditLine *sel = NULL;
54 
55 private int sighdl[] = {
56 #define _DO(a)	(a),
57     ALLSIGS
58 #undef _DO
59     -1
60 };
61 
62 private void sig_handler(int);
63 
64 /* sig_handler():
65  *	This is the handler called for all signals
66  *	XXX: we cannot pass any data so we just store the old editline
67  *	state in a private variable
68  */
69 private void
70 sig_handler(signo)
71     int signo;
72 {
73     int save_errno = errno;
74     int i;
75     sigset_t nset, oset;
76 
77     (void)sigemptyset(&nset);
78     (void)sigaddset(&nset, signo);
79     (void)sigprocmask(SIG_BLOCK, &nset, &oset);
80 
81     switch (signo) {
82     case SIGCONT:
83 	tty_rawmode(sel);				/* XXX signal race */
84 	if (ed_redisplay(sel, 0) == CC_REFRESH)		/* XXX signal race */
85 	    re_refresh(sel);				/* XXX signal race */
86 	term__flush();					/* XXX signal race */
87 	break;
88 
89     case SIGWINCH:
90 	el_resize(sel);					/* XXX signal race */
91 	break;
92 
93     default:
94 	tty_cookedmode(sel);				/* XXX signal race */
95 	break;
96     }
97 
98     for (i = 0; sighdl[i] != -1; i++)
99 	if (signo == sighdl[i])
100 	    break;
101 
102     (void)signal(signo, sel->el_signal[i]);
103     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
104     (void)kill(0, signo);
105     errno = save_errno;
106 }
107 
108 
109 /* sig_init():
110  *	Initialize all signal stuff
111  */
112 protected int
113 sig_init(el)
114     EditLine *el;
115 {
116     int i;
117     sigset_t nset, oset;
118 
119     (void)sigemptyset(&nset);
120 #define _DO(a) (void)sigaddset(&nset, a);
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(el)
142     EditLine *el;
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(el)
154     EditLine *el;
155 {
156     int i;
157     sigset_t nset, oset;
158 
159     (void)sigemptyset(&nset);
160 #define _DO(a) (void)sigaddset(&nset, a);
161     ALLSIGS
162 #undef _DO
163     (void)sigprocmask(SIG_BLOCK, &nset, &oset);
164 
165     for (i = 0; sighdl[i] != -1; i++) {
166 	sig_t s;
167 	/* This could happen if we get interrupted */
168 	if ((s = signal(sighdl[i], sig_handler)) != sig_handler)
169 	    el->el_signal[i] = s;
170     }
171     sel = el;
172     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
173 }
174 
175 
176 /* sig_clr():
177  *	clear all the signal handlers
178  */
179 protected void
180 sig_clr(el)
181     EditLine *el;
182 {
183     int i;
184     sigset_t nset, oset;
185 
186     (void)sigemptyset(&nset);
187 #define _DO(a) (void)sigaddset(&nset, a);
188     ALLSIGS
189 #undef _DO
190     (void)sigprocmask(SIG_BLOCK, &nset, &oset);
191 
192     for (i = 0; sighdl[i] != -1; i++)
193 	if (el->el_signal[i] != SIG_ERR)
194 	    (void)signal(sighdl[i], el->el_signal[i]);
195 
196     sel = NULL;	/* we are going to die if the handler is called */
197     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
198 }
199