]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - lib/libedit/sig.c
MFC r232197 (by phk):
[FreeBSD/releng/9.1.git] / lib / libedit / sig.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Christos Zoulas of Cornell University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  *      $NetBSD: sig.c,v 1.14 2009/02/18 15:04:40 christos Exp $
33  */
34
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$");
40
41 /*
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.
45  */
46 #include "sys.h"
47 #include "el.h"
48 #include <stdlib.h>
49
50 private EditLine *sel = NULL;
51
52 private const int sighdl[] = {
53 #define _DO(a)  (a),
54         ALLSIGS
55 #undef  _DO
56         - 1
57 };
58
59 private void sig_handler(int);
60
61 /* sig_handler():
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
65  */
66 private void
67 sig_handler(int signo)
68 {
69         int i;
70         sigset_t nset, oset;
71
72         (void) sigemptyset(&nset);
73         (void) sigaddset(&nset, signo);
74         (void) sigprocmask(SIG_BLOCK, &nset, &oset);
75
76         switch (signo) {
77         case SIGCONT:
78                 tty_rawmode(sel);
79                 if (ed_redisplay(sel, 0) == CC_REFRESH)
80                         re_refresh(sel);
81                 term__flush(sel);
82                 break;
83
84         case SIGWINCH:
85                 el_resize(sel);
86                 break;
87
88         default:
89                 tty_cookedmode(sel);
90                 break;
91         }
92
93         for (i = 0; sighdl[i] != -1; i++)
94                 if (signo == sighdl[i])
95                         break;
96
97         (void) sigaction(signo, &sel->el_signal->sig_action[i], NULL);
98         sel->el_signal->sig_action[i].sa_handler = SIG_ERR;
99         sel->el_signal->sig_action[i].sa_flags = 0;
100         sigemptyset(&sel->el_signal->sig_action[i].sa_mask);
101         (void) sigprocmask(SIG_SETMASK, &oset, NULL);
102         (void) kill(0, signo);
103 }
104
105
106 /* sig_init():
107  *      Initialize all signal stuff
108  */
109 protected int
110 sig_init(EditLine *el)
111 {
112         size_t i;
113         sigset_t *nset, oset;
114
115         el->el_signal = el_malloc(sizeof(*el->el_signal));
116         if (el->el_signal == NULL)
117                 return -1;
118
119         nset = &el->el_signal->sig_set;
120         (void) sigemptyset(nset);
121 #define _DO(a) (void) sigaddset(nset, a);
122         ALLSIGS
123 #undef  _DO
124         (void) sigprocmask(SIG_BLOCK, nset, &oset);
125
126         for (i = 0; sighdl[i] != -1; i++) {
127                 el->el_signal->sig_action[i].sa_handler = SIG_ERR;
128                 el->el_signal->sig_action[i].sa_flags = 0;
129                 sigemptyset(&el->el_signal->sig_action[i].sa_mask);
130         }
131
132         (void) sigprocmask(SIG_SETMASK, &oset, NULL);
133
134         return 0;
135 }
136
137
138 /* sig_end():
139  *      Clear all signal stuff
140  */
141 protected void
142 sig_end(EditLine *el)
143 {
144
145         el_free((ptr_t) el->el_signal);
146         el->el_signal = NULL;
147 }
148
149
150 /* sig_set():
151  *      set all the signal handlers
152  */
153 protected void
154 sig_set(EditLine *el)
155 {
156         size_t i;
157         sigset_t oset;
158         struct sigaction osa, nsa;
159
160         nsa.sa_handler = sig_handler;
161         sigemptyset(&nsa.sa_mask);
162
163         (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
164
165         for (i = 0; sighdl[i] != -1; i++) {
166                 nsa.sa_flags = SIGINT ? 0 : SA_RESTART;
167                 /* This could happen if we get interrupted */
168                 if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
169                     osa.sa_handler != sig_handler)
170                         el->el_signal->sig_action[i] = osa;
171         }
172         sel = el;
173         (void) sigprocmask(SIG_SETMASK, &oset, NULL);
174 }
175
176
177 /* sig_clr():
178  *      clear all the signal handlers
179  */
180 protected void
181 sig_clr(EditLine *el)
182 {
183         size_t i;
184         sigset_t oset;
185
186         (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
187
188         for (i = 0; sighdl[i] != -1; i++)
189                 if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
190                         (void)sigaction(sighdl[i],
191                             &el->el_signal->sig_action[i], NULL);
192
193         sel = NULL;             /* we are going to die if the handler is
194                                  * called */
195         (void)sigprocmask(SIG_SETMASK, &oset, NULL);
196 }