]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libedit/sig.c
Merge ACPICA 20161222.
[FreeBSD/FreeBSD.git] / lib / libedit / sig.c
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 __RCSID("$NetBSD: sig.c,v 1.17 2011/07/28 20:50:55 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 /*
47  * sig.c: Signal handling stuff.
48  *        our policy is to trap all signals, set a good state
49  *        and pass the ball to our caller.
50  */
51 #include "el.h"
52 #include <stdlib.h>
53
54 private EditLine *sel = NULL;
55
56 private const int sighdl[] = {
57 #define _DO(a)  (a),
58         ALLSIGS
59 #undef  _DO
60         - 1
61 };
62
63 private void sig_handler(int);
64
65 /* sig_handler():
66  *      This is the handler called for all signals
67  *      XXX: we cannot pass any data so we just store the old editline
68  *      state in a private variable
69  */
70 private void
71 sig_handler(int signo)
72 {
73         int i;
74         sigset_t nset, oset;
75
76         (void) sigemptyset(&nset);
77         (void) sigaddset(&nset, signo);
78         (void) sigprocmask(SIG_BLOCK, &nset, &oset);
79
80         sel->el_signal->sig_no = signo;
81
82         switch (signo) {
83         case SIGCONT:
84                 tty_rawmode(sel);
85                 if (ed_redisplay(sel, 0) == CC_REFRESH)
86                         re_refresh(sel);
87                 terminal__flush(sel);
88                 break;
89
90         case SIGWINCH:
91                 el_resize(sel);
92                 break;
93
94         default:
95                 tty_cookedmode(sel);
96                 break;
97         }
98
99         for (i = 0; sighdl[i] != -1; i++)
100                 if (signo == sighdl[i])
101                         break;
102
103         (void) sigaction(signo, &sel->el_signal->sig_action[i], NULL);
104         sel->el_signal->sig_action[i].sa_handler = SIG_ERR;
105         sel->el_signal->sig_action[i].sa_flags = 0;
106         sigemptyset(&sel->el_signal->sig_action[i].sa_mask);
107         (void) sigprocmask(SIG_SETMASK, &oset, NULL);
108         (void) kill(0, signo);
109 }
110
111
112 /* sig_init():
113  *      Initialize all signal stuff
114  */
115 protected int
116 sig_init(EditLine *el)
117 {
118         size_t i;
119         sigset_t *nset, oset;
120
121         el->el_signal = el_malloc(sizeof(*el->el_signal));
122         if (el->el_signal == NULL)
123                 return -1;
124
125         nset = &el->el_signal->sig_set;
126         (void) sigemptyset(nset);
127 #define _DO(a) (void) sigaddset(nset, a);
128         ALLSIGS
129 #undef  _DO
130         (void) sigprocmask(SIG_BLOCK, nset, &oset);
131
132         for (i = 0; sighdl[i] != -1; i++) {
133                 el->el_signal->sig_action[i].sa_handler = SIG_ERR;
134                 el->el_signal->sig_action[i].sa_flags = 0;
135                 sigemptyset(&el->el_signal->sig_action[i].sa_mask);
136         }
137
138         (void) sigprocmask(SIG_SETMASK, &oset, NULL);
139
140         return 0;
141 }
142
143
144 /* sig_end():
145  *      Clear all signal stuff
146  */
147 protected void
148 sig_end(EditLine *el)
149 {
150
151         el_free(el->el_signal);
152         el->el_signal = NULL;
153 }
154
155
156 /* sig_set():
157  *      set all the signal handlers
158  */
159 protected void
160 sig_set(EditLine *el)
161 {
162         size_t i;
163         sigset_t oset;
164         struct sigaction osa, nsa;
165
166         nsa.sa_handler = sig_handler;
167         nsa.sa_flags = 0;
168         sigemptyset(&nsa.sa_mask);
169
170         (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
171
172         for (i = 0; sighdl[i] != -1; i++) {
173                 /* This could happen if we get interrupted */
174                 if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
175                     osa.sa_handler != sig_handler)
176                         el->el_signal->sig_action[i] = osa;
177         }
178         sel = el;
179         (void) sigprocmask(SIG_SETMASK, &oset, NULL);
180 }
181
182
183 /* sig_clr():
184  *      clear all the signal handlers
185  */
186 protected void
187 sig_clr(EditLine *el)
188 {
189         size_t i;
190         sigset_t oset;
191
192         (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
193
194         for (i = 0; sighdl[i] != -1; i++)
195                 if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
196                         (void)sigaction(sighdl[i],
197                             &el->el_signal->sig_action[i], NULL);
198
199         sel = NULL;             /* we are going to die if the handler is
200                                  * called */
201         (void)sigprocmask(SIG_SETMASK, &oset, NULL);
202 }