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