Home | History | Annotate | Line # | Download | only in libedit
sig.c revision 1.5
      1 /*	$NetBSD: sig.c,v 1.5 1999/07/02 15:21:27 simonb 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if !defined(lint) && !defined(SCCSID)
     41 #if 0
     42 static char sccsid[] = "@(#)sig.c	8.1 (Berkeley) 6/4/93";
     43 #else
     44 __RCSID("$NetBSD: sig.c,v 1.5 1999/07/02 15:21:27 simonb Exp $");
     45 #endif
     46 #endif /* not lint && not SCCSID */
     47 
     48 /*
     49  * sig.c: Signal handling stuff.
     50  *	  our policy is to trap all signals, set a good state
     51  *	  and pass the ball to our caller.
     52  */
     53 #include "sys.h"
     54 #include "el.h"
     55 #include <stdlib.h>
     56 
     57 private EditLine *sel = NULL;
     58 
     59 private int sighdl[] = {
     60 #define _DO(a)	(a),
     61     ALLSIGS
     62 #undef _DO
     63     -1
     64 };
     65 
     66 private void sig_handler	__P((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(signo)
     75     int signo;
     76 {
     77     int i;
     78     sigset_t nset, oset;
     79 
     80     (void) sigemptyset(&nset);
     81     (void) sigaddset(&nset, signo);
     82     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
     83 
     84     switch (signo) {
     85     case SIGCONT:
     86 	tty_rawmode(sel);
     87 	if (ed_redisplay(sel, 0) == CC_REFRESH)
     88 	    re_refresh(sel);
     89 	term__flush();
     90 	break;
     91 
     92     case SIGWINCH:
     93 	el_resize(sel);
     94 	break;
     95 
     96     default:
     97 	tty_cookedmode(sel);
     98 	break;
     99     }
    100 
    101     for (i = 0; sighdl[i] != -1; i++)
    102 	if (signo == sighdl[i])
    103 	    break;
    104 
    105     (void) signal(signo, sel->el_signal[i]);
    106     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
    107     (void) kill(0, signo);
    108 }
    109 
    110 
    111 /* sig_init():
    112  *	Initialize all signal stuff
    113  */
    114 protected int
    115 sig_init(el)
    116     EditLine *el;
    117 {
    118     int i;
    119     sigset_t nset, oset;
    120 
    121     (void) sigemptyset(&nset);
    122 #define _DO(a) (void) sigaddset(&nset, SIGWINCH);
    123     ALLSIGS
    124 #undef _DO
    125     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
    126 
    127 #define SIGSIZE (sizeof(sighdl) / sizeof(sighdl[0]) * sizeof(sig_t))
    128 
    129     el->el_signal = (sig_t *) el_malloc(SIGSIZE);
    130     for (i = 0; sighdl[i] != -1; i++)
    131 	el->el_signal[i] = SIG_ERR;
    132 
    133     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
    134 
    135     return 0;
    136 }
    137 
    138 
    139 /* sig_end():
    140  *	Clear all signal stuff
    141  */
    142 protected void
    143 sig_end(el)
    144     EditLine *el;
    145 {
    146     el_free((ptr_t) el->el_signal);
    147     el->el_signal = NULL;
    148 }
    149 
    150 
    151 /* sig_set():
    152  *	set all the signal handlers
    153  */
    154 protected void
    155 sig_set(el)
    156     EditLine *el;
    157 {
    158     int i;
    159     sigset_t nset, oset;
    160 
    161     (void) sigemptyset(&nset);
    162 #define _DO(a) (void) sigaddset(&nset, SIGWINCH);
    163     ALLSIGS
    164 #undef _DO
    165     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
    166 
    167     for (i = 0; sighdl[i] != -1; i++) {
    168 	sig_t s;
    169 	/* This could happen if we get interrupted */
    170 	if ((s = signal(sighdl[i], sig_handler)) != sig_handler)
    171 	    el->el_signal[i] = s;
    172     }
    173     sel = el;
    174     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
    175 }
    176 
    177 
    178 /* sig_clr():
    179  *	clear all the signal handlers
    180  */
    181 protected void
    182 sig_clr(el)
    183     EditLine *el;
    184 {
    185     int i;
    186     sigset_t nset, oset;
    187 
    188     (void) sigemptyset(&nset);
    189 #define _DO(a) (void) sigaddset(&nset, SIGWINCH);
    190     ALLSIGS
    191 #undef _DO
    192     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
    193 
    194     for (i = 0; sighdl[i] != -1; i++)
    195 	if (el->el_signal[i] != SIG_ERR)
    196 	    (void) signal(sighdl[i], el->el_signal[i]);
    197 
    198     sel = NULL;	/* we are going to die if the handler is called */
    199     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
    200 }
    201