xref: /original-bsd/lib/libedit/sig.c (revision 5da099d4)
115e8aa4eSbostic /*-
2*5da099d4Sbostic  * Copyright (c) 1992, 1993
3*5da099d4Sbostic  *	The Regents of the University of California.  All rights reserved.
415e8aa4eSbostic  *
515e8aa4eSbostic  * This code is derived from software contributed to Berkeley by
615e8aa4eSbostic  * Christos Zoulas of Cornell University.
715e8aa4eSbostic  *
815e8aa4eSbostic  * %sccs.include.redist.c%
915e8aa4eSbostic  */
1015e8aa4eSbostic 
118cbf4338Schristos #if !defined(lint) && !defined(SCCSID)
12*5da099d4Sbostic static char sccsid[] = "@(#)sig.c	8.1 (Berkeley) 06/04/93";
138cbf4338Schristos #endif /* not lint && not SCCSID */
1415e8aa4eSbostic 
1515e8aa4eSbostic /*
168cbf4338Schristos  * sig.c: Signal handling stuff.
1715e8aa4eSbostic  *	  our policy is to trap all signals, set a good state
1815e8aa4eSbostic  *	  and pass the ball to our caller.
1915e8aa4eSbostic  */
2015e8aa4eSbostic #include "sys.h"
2115e8aa4eSbostic #include "el.h"
2215e8aa4eSbostic #include <stdlib.h>
2315e8aa4eSbostic 
2415e8aa4eSbostic private EditLine *sel = NULL;
2515e8aa4eSbostic 
2615e8aa4eSbostic private int sighdl[] = {
2715e8aa4eSbostic #define _DO(a)	(a),
2815e8aa4eSbostic     ALLSIGS
2915e8aa4eSbostic #undef _DO
3015e8aa4eSbostic     -1
3115e8aa4eSbostic };
3215e8aa4eSbostic 
3315e8aa4eSbostic private void sig_handler	__P((int));
3415e8aa4eSbostic 
3515e8aa4eSbostic /* sig_handler():
3615e8aa4eSbostic  *	This is the handler called for all signals
3715e8aa4eSbostic  *	XXX: we cannot pass any data so we just store the old editline
3815e8aa4eSbostic  *	state in a private variable
3915e8aa4eSbostic  */
4015e8aa4eSbostic private void
sig_handler(signo)4115e8aa4eSbostic sig_handler(signo)
4215e8aa4eSbostic     int signo;
4315e8aa4eSbostic {
4415e8aa4eSbostic     int i;
4515e8aa4eSbostic     sigset_t nset, oset;
4615e8aa4eSbostic 
4715e8aa4eSbostic     (void) sigemptyset(&nset);
4815e8aa4eSbostic     (void) sigaddset(&nset, signo);
4915e8aa4eSbostic     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
5015e8aa4eSbostic 
5115e8aa4eSbostic     switch (signo) {
5215e8aa4eSbostic     case SIGCONT:
5315e8aa4eSbostic 	tty_rawmode(sel);
5415e8aa4eSbostic 	if (ed_redisplay(sel, 0) == CC_REFRESH)
5515e8aa4eSbostic 	    re_refresh(sel);
5615e8aa4eSbostic 	term__flush();
5715e8aa4eSbostic 	break;
5815e8aa4eSbostic 
5915e8aa4eSbostic     case SIGWINCH:
6015e8aa4eSbostic 	el_resize(sel);
6115e8aa4eSbostic 	break;
6215e8aa4eSbostic 
6315e8aa4eSbostic     default:
6415e8aa4eSbostic 	tty_cookedmode(sel);
6515e8aa4eSbostic 	break;
6615e8aa4eSbostic     }
6715e8aa4eSbostic 
6815e8aa4eSbostic     for (i = 0; sighdl[i] != -1; i++)
6915e8aa4eSbostic 	if (signo == sighdl[i])
7015e8aa4eSbostic 	    break;
7115e8aa4eSbostic 
7215e8aa4eSbostic     (void) signal(signo, sel->el_signal[i]);
7315e8aa4eSbostic     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
7415e8aa4eSbostic     (void) kill(0, signo);
7515e8aa4eSbostic }
7615e8aa4eSbostic 
7715e8aa4eSbostic 
7815e8aa4eSbostic /* sig_init():
7915e8aa4eSbostic  *	Initialize all signal stuff
8015e8aa4eSbostic  */
8115e8aa4eSbostic protected int
sig_init(el)8215e8aa4eSbostic sig_init(el)
8315e8aa4eSbostic     EditLine *el;
8415e8aa4eSbostic {
8515e8aa4eSbostic     int i;
8615e8aa4eSbostic     sigset_t nset, oset;
8715e8aa4eSbostic 
8815e8aa4eSbostic     (void) sigemptyset(&nset);
8915e8aa4eSbostic #define _DO(a) (void) sigaddset(&nset, SIGWINCH);
9015e8aa4eSbostic     ALLSIGS
9115e8aa4eSbostic #undef _DO
9215e8aa4eSbostic     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
9315e8aa4eSbostic 
9415e8aa4eSbostic #define SIGSIZE (sizeof(sighdl) / sizeof(sighdl[0]) * sizeof(sig_t))
9515e8aa4eSbostic 
9615e8aa4eSbostic     el->el_signal = (sig_t *) el_malloc(SIGSIZE);
9715e8aa4eSbostic     for (i = 0; sighdl[i] != -1; i++)
9815e8aa4eSbostic 	el->el_signal[i] = BADSIG;
9915e8aa4eSbostic 
10015e8aa4eSbostic     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
10115e8aa4eSbostic 
10215e8aa4eSbostic     return 0;
10315e8aa4eSbostic }
10415e8aa4eSbostic 
10515e8aa4eSbostic 
10615e8aa4eSbostic /* sig_end():
10715e8aa4eSbostic  *	Clear all signal stuff
10815e8aa4eSbostic  */
10915e8aa4eSbostic protected void
sig_end(el)11015e8aa4eSbostic sig_end(el)
11115e8aa4eSbostic     EditLine *el;
11215e8aa4eSbostic {
11315e8aa4eSbostic     el_free((ptr_t) el->el_signal);
11415e8aa4eSbostic     el->el_signal = NULL;
11515e8aa4eSbostic }
11615e8aa4eSbostic 
11715e8aa4eSbostic 
11815e8aa4eSbostic /* sig_set():
11915e8aa4eSbostic  *	set all the signal handlers
12015e8aa4eSbostic  */
12115e8aa4eSbostic protected void
sig_set(el)12215e8aa4eSbostic sig_set(el)
12315e8aa4eSbostic     EditLine *el;
12415e8aa4eSbostic {
12515e8aa4eSbostic     int i;
12615e8aa4eSbostic     sigset_t nset, oset;
12715e8aa4eSbostic 
12815e8aa4eSbostic     (void) sigemptyset(&nset);
12915e8aa4eSbostic #define _DO(a) (void) sigaddset(&nset, SIGWINCH);
13015e8aa4eSbostic     ALLSIGS
13115e8aa4eSbostic #undef _DO
13215e8aa4eSbostic     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
13315e8aa4eSbostic 
13415e8aa4eSbostic     for (i = 0; sighdl[i] != -1; i++) {
13515e8aa4eSbostic 	sig_t s;
13615e8aa4eSbostic 	/* This could happen if we get interrupted */
13715e8aa4eSbostic 	if ((s = signal(sighdl[i], sig_handler)) != sig_handler)
13815e8aa4eSbostic 	    el->el_signal[i] = s;
13915e8aa4eSbostic     }
14015e8aa4eSbostic     sel = el;
14115e8aa4eSbostic     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
14215e8aa4eSbostic }
14315e8aa4eSbostic 
14415e8aa4eSbostic 
14515e8aa4eSbostic /* sig_clr():
14615e8aa4eSbostic  *	clear all the signal handlers
14715e8aa4eSbostic  */
14815e8aa4eSbostic protected void
sig_clr(el)14915e8aa4eSbostic sig_clr(el)
15015e8aa4eSbostic     EditLine *el;
15115e8aa4eSbostic {
15215e8aa4eSbostic     int i;
15315e8aa4eSbostic     sigset_t nset, oset;
15415e8aa4eSbostic 
15515e8aa4eSbostic     (void) sigemptyset(&nset);
15615e8aa4eSbostic #define _DO(a) (void) sigaddset(&nset, SIGWINCH);
15715e8aa4eSbostic     ALLSIGS
15815e8aa4eSbostic #undef _DO
15915e8aa4eSbostic     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
16015e8aa4eSbostic 
16115e8aa4eSbostic     for (i = 0; sighdl[i] != -1; i++)
16215e8aa4eSbostic 	if (el->el_signal[i] != BADSIG)
16315e8aa4eSbostic 	    (void) signal(sighdl[i], el->el_signal[i]);
16415e8aa4eSbostic 
16515e8aa4eSbostic     sel = NULL;	/* we are going to die if the handler is called */
16615e8aa4eSbostic     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
16715e8aa4eSbostic }
168