don't bother resolving onbld python module deps
[unleashed.git] / lib / libedit / sig.c
blobca5a881342dbf56fb7db79a82d7e5a78434a1f03
1 /* $OpenBSD: sig.c,v 1.19 2016/04/11 21:17:29 schwarze Exp $ */
2 /* $NetBSD: sig.c,v 1.25 2016/04/11 18:56:31 christos Exp $ */
4 /*-
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
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.
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.
36 #include "config.h"
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.
43 #include <errno.h>
44 #include <stdlib.h>
46 #include "el.h"
47 #include "common.h"
49 static EditLine *sel = NULL;
51 static const int sighdl[] = {
52 #define _DO(a) (a),
53 ALLSIGS
54 #undef _DO
55 - 1
58 static void sig_handler(int);
60 /* sig_handler():
61 * This is the handler called for all signals
62 * XXX: we cannot pass any data so we just store the old editline
63 * state in a private variable
65 static void
66 sig_handler(int signo)
68 int i, save_errno;
69 sigset_t nset, oset;
71 save_errno = errno;
72 (void) sigemptyset(&nset);
73 (void) sigaddset(&nset, signo);
74 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
76 sel->el_signal->sig_no = signo;
78 switch (signo) {
79 case SIGCONT:
80 tty_rawmode(sel);
81 if (ed_redisplay(sel, 0) == CC_REFRESH)
82 re_refresh(sel);
83 terminal__flush(sel);
84 break;
86 case SIGWINCH:
87 el_resize(sel);
88 break;
90 default:
91 tty_cookedmode(sel);
92 break;
95 for (i = 0; sighdl[i] != -1; i++)
96 if (signo == sighdl[i])
97 break;
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);
105 errno = save_errno;
109 /* sig_init():
110 * Initialize all signal stuff
112 protected int
113 sig_init(EditLine *el)
115 size_t i;
116 sigset_t *nset, oset;
118 el->el_signal = malloc(sizeof(*el->el_signal));
119 if (el->el_signal == NULL)
120 return -1;
122 nset = &el->el_signal->sig_set;
123 (void) sigemptyset(nset);
124 #define _DO(a) (void) sigaddset(nset, a);
125 ALLSIGS
126 #undef _DO
127 (void) sigprocmask(SIG_BLOCK, nset, &oset);
129 for (i = 0; sighdl[i] != -1; i++) {
130 el->el_signal->sig_action[i].sa_handler = SIG_ERR;
131 el->el_signal->sig_action[i].sa_flags = 0;
132 sigemptyset(&el->el_signal->sig_action[i].sa_mask);
135 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
137 return 0;
141 /* sig_end():
142 * Clear all signal stuff
144 protected void
145 sig_end(EditLine *el)
148 free(el->el_signal);
149 el->el_signal = NULL;
153 /* sig_set():
154 * set all the signal handlers
156 protected void
157 sig_set(EditLine *el)
159 size_t i;
160 sigset_t oset;
161 struct sigaction osa, nsa;
163 nsa.sa_handler = sig_handler;
164 nsa.sa_flags = 0;
165 sigemptyset(&nsa.sa_mask);
167 (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
169 for (i = 0; sighdl[i] != -1; i++) {
170 /* This could happen if we get interrupted */
171 if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
172 osa.sa_handler != sig_handler)
173 el->el_signal->sig_action[i] = osa;
175 sel = el;
176 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
180 /* sig_clr():
181 * clear all the signal handlers
183 protected void
184 sig_clr(EditLine *el)
186 size_t i;
187 sigset_t oset;
189 (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
191 for (i = 0; sighdl[i] != -1; i++)
192 if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
193 (void)sigaction(sighdl[i],
194 &el->el_signal->sig_action[i], NULL);
196 sel = NULL; /* we are going to die if the handler is
197 * called */
198 (void)sigprocmask(SIG_SETMASK, &oset, NULL);