2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: sig.c,v 1.15 2009/02/19 15:20:22 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)sig.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * sig.c: Signal handling stuff.
43 * our policy is to trap all signals, set a good state
44 * and pass the ball to our caller.
50 private EditLine *sel = NULL;
52 private const int sighdl[] = {
59 private void sig_handler(int);
62 * This is the handler called for all signals
63 * XXX: we cannot pass any data so we just store the old editline
64 * state in a private variable
67 sig_handler(int signo)
72 (void) sigemptyset(&nset);
73 (void) sigaddset(&nset, signo);
74 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
76 sel->el_signal->sig_no = signo;
81 if (ed_redisplay(sel, 0) == CC_REFRESH)
95 for (i = 0; sighdl[i] != -1; i++)
96 if (signo == sighdl[i])
99 (void) sigaction(signo, &sel->el_signal->sig_action[i], NULL);
100 sel->el_signal->sig_action[i].sa_handler = SIG_ERR;
101 sel->el_signal->sig_action[i].sa_flags = 0;
102 sigemptyset(&sel->el_signal->sig_action[i].sa_mask);
103 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
104 (void) kill(0, signo);
109 * Initialize all signal stuff
112 sig_init(EditLine *el)
115 sigset_t *nset, oset;
117 el->el_signal = el_malloc(sizeof(*el->el_signal));
118 if (el->el_signal == NULL)
121 nset = &el->el_signal->sig_set;
122 (void) sigemptyset(nset);
123 #define _DO(a) (void) sigaddset(nset, a);
126 (void) sigprocmask(SIG_BLOCK, nset, &oset);
128 for (i = 0; sighdl[i] != -1; i++) {
129 el->el_signal->sig_action[i].sa_handler = SIG_ERR;
130 el->el_signal->sig_action[i].sa_flags = 0;
131 sigemptyset(&el->el_signal->sig_action[i].sa_mask);
134 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
141 * Clear all signal stuff
144 sig_end(EditLine *el)
147 el_free((ptr_t) el->el_signal);
148 el->el_signal = NULL;
153 * set all the signal handlers
156 sig_set(EditLine *el)
160 struct sigaction osa, nsa;
162 nsa.sa_handler = sig_handler;
164 sigemptyset(&nsa.sa_mask);
166 (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
168 for (i = 0; sighdl[i] != -1; i++) {
169 /* This could happen if we get interrupted */
170 if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
171 osa.sa_handler != sig_handler)
172 el->el_signal->sig_action[i] = osa;
175 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
180 * clear all the signal handlers
183 sig_clr(EditLine *el)
188 (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
190 for (i = 0; sighdl[i] != -1; i++)
191 if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
192 (void)sigaction(sighdl[i],
193 &el->el_signal->sig_action[i], NULL);
195 sel = NULL; /* we are going to die if the handler is
197 (void)sigprocmask(SIG_SETMASK, &oset, NULL);