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