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