2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)sig.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: sig.c,v 1.11 2003/08/07 16:44:33 agc Exp $
34 * $DragonFly: src/lib/libedit/sig.c,v 1.4 2005/11/13 11:58:30 corecode Exp $
40 * sig.c: Signal handling stuff.
41 * our policy is to trap all signals, set a good state
42 * and pass the ball to our caller.
47 private EditLine
*sel
= NULL
;
49 private const int sighdl
[] = {
56 private void sig_handler(int);
59 * This is the handler called for all signals
60 * XXX: we cannot pass any data so we just store the old editline
61 * state in a private variable
64 sig_handler(int signo
)
69 (void) sigemptyset(&nset
);
70 (void) sigaddset(&nset
, signo
);
71 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
76 if (ed_redisplay(sel
, 0) == CC_REFRESH
)
90 for (i
= 0; sighdl
[i
] != -1; i
++)
91 if (signo
== sighdl
[i
])
94 (void) signal(signo
, sel
->el_signal
[i
]);
95 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
96 (void) kill(0, signo
);
101 * Initialize all signal stuff
104 sig_init(EditLine
*el
)
109 (void) sigemptyset(&nset
);
110 #define _DO(a) (void) sigaddset(&nset, a);
113 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
115 #define SIGSIZE (sizeof(sighdl) / sizeof(sighdl[0]) * sizeof(el_signalhandler_t))
117 el
->el_signal
= (el_signalhandler_t
*) el_malloc(SIGSIZE
);
118 if (el
->el_signal
== NULL
)
120 for (i
= 0; sighdl
[i
] != -1; i
++)
121 el
->el_signal
[i
] = SIG_ERR
;
123 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
130 * Clear all signal stuff
133 sig_end(EditLine
*el
)
136 el_free((ptr_t
) el
->el_signal
);
137 el
->el_signal
= NULL
;
142 * set all the signal handlers
145 sig_set(EditLine
*el
)
150 (void) sigemptyset(&nset
);
151 #define _DO(a) (void) sigaddset(&nset, a);
154 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
156 for (i
= 0; sighdl
[i
] != -1; i
++) {
157 el_signalhandler_t s
;
158 /* This could happen if we get interrupted */
159 if ((s
= signal(sighdl
[i
], sig_handler
)) != sig_handler
)
160 el
->el_signal
[i
] = s
;
163 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
168 * clear all the signal handlers
171 sig_clr(EditLine
*el
)
176 (void) sigemptyset(&nset
);
177 #define _DO(a) (void) sigaddset(&nset, a);
180 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
182 for (i
= 0; sighdl
[i
] != -1; i
++)
183 if (el
->el_signal
[i
] != SIG_ERR
)
184 (void) signal(sighdl
[i
], el
->el_signal
[i
]);
186 sel
= NULL
; /* we are going to die if the handler is
188 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);