/* $NetBSD: sig.c,v 1.28 2024/12/18 15:38:52 christos Exp $ */12/*-3* Copyright (c) 1992, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Christos Zoulas of Cornell University.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "config.h"35#if !defined(lint) && !defined(SCCSID)36#if 037static char sccsid[] = "@(#)sig.c 8.1 (Berkeley) 6/4/93";38#else39__RCSID("$NetBSD: sig.c,v 1.28 2024/12/18 15:38:52 christos Exp $");40#endif41#endif /* not lint && not SCCSID */4243/*44* sig.c: Signal handling stuff.45* our policy is to trap all signals, set a good state46* and pass the ball to our caller.47*/48#include <errno.h>49#include <stdlib.h>5051#include "el.h"52#include "common.h"5354static EditLine *sel = NULL;5556static const int sighdl[] = {57#define _DO(a) (a),58ALLSIGS59#undef _DO60- 161};6263static void sig_handler(int);6465/* sig_handler():66* This is the handler called for all signals67* XXX: we cannot pass any data so we just store the old editline68* state in a private variable69*/70static void71sig_handler(int signo)72{73int i, save_errno;74sigset_t nset, oset;7576save_errno = errno;77(void) sigemptyset(&nset);78(void) sigaddset(&nset, signo);79(void) sigprocmask(SIG_BLOCK, &nset, &oset);8081sel->el_signal->sig_no = signo;8283switch (signo) {84case SIGCONT:85tty_rawmode(sel);86if (ed_redisplay(sel, 0) == CC_REFRESH)87re_refresh(sel);88terminal__flush(sel);89break;9091case SIGWINCH:92el_resize(sel);93break;9495default:96tty_cookedmode(sel);97break;98}99100for (i = 0; sighdl[i] != -1; i++)101if (signo == sighdl[i])102break;103104(void) sigaction(signo, &sel->el_signal->sig_action[i], NULL);105sel->el_signal->sig_action[i].sa_handler = SIG_ERR;106sel->el_signal->sig_action[i].sa_flags = 0;107sigemptyset(&sel->el_signal->sig_action[i].sa_mask);108(void) sigprocmask(SIG_SETMASK, &oset, NULL);109(void) kill(0, signo);110errno = save_errno;111}112113114/* sig_init():115* Initialize all signal stuff116*/117libedit_private int118sig_init(EditLine *el)119{120size_t i;121sigset_t *nset, oset;122123el->el_signal = el_malloc(sizeof(*el->el_signal));124if (el->el_signal == NULL)125return -1;126127nset = &el->el_signal->sig_set;128(void) sigemptyset(nset);129#define _DO(a) (void) sigaddset(nset, a);130ALLSIGS131#undef _DO132(void) sigprocmask(SIG_BLOCK, nset, &oset);133134for (i = 0; sighdl[i] != -1; i++) {135el->el_signal->sig_action[i].sa_handler = SIG_ERR;136el->el_signal->sig_action[i].sa_flags = 0;137sigemptyset(&el->el_signal->sig_action[i].sa_mask);138}139140(void) sigprocmask(SIG_SETMASK, &oset, NULL);141142return 0;143}144145146/* sig_end():147* Clear all signal stuff148*/149libedit_private void150sig_end(EditLine *el)151{152153el_free(el->el_signal);154el->el_signal = NULL;155}156157158/* sig_set():159* set all the signal handlers160*/161libedit_private void162sig_set(EditLine *el)163{164size_t i;165sigset_t oset;166struct sigaction osa, nsa;167168nsa.sa_handler = sig_handler;169nsa.sa_flags = SA_ONSTACK;170sigemptyset(&nsa.sa_mask);171172sel = el;173(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);174175for (i = 0; sighdl[i] != -1; i++) {176/* This could happen if we get interrupted */177if (sigaction(sighdl[i], &nsa, &osa) != -1 &&178osa.sa_handler != sig_handler)179el->el_signal->sig_action[i] = osa;180}181(void) sigprocmask(SIG_SETMASK, &oset, NULL);182}183184185/* sig_clr():186* clear all the signal handlers187*/188libedit_private void189sig_clr(EditLine *el)190{191size_t i;192sigset_t oset;193194(void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);195196for (i = 0; sighdl[i] != -1; i++)197if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)198(void)sigaction(sighdl[i],199&el->el_signal->sig_action[i], NULL);200201(void)sigprocmask(SIG_SETMASK, &oset, NULL);202}203204205