|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T t
Length: 5167 (0x142f) Types: TextFile Names: »trap.c«
└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89 └─⟦ca1f037a2⟧ »./bash-1.04.tar.Z« └─⟦46465a4db⟧ └─⟦this⟧ »bash-1.04/trap.c«
/* trap.c -- Not the trap command, but useful functions for manipulating those objects. The trap command is in builtins.c */ /* Copyright (C) 1987,1989 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. Bash is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. Bash is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Bash; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "trap.h" #include "shell.h" /* The list of things to do originally, before we started trapping. */ SigHandler *original_signals[NSIG]; /* For each signal, a slot for a string, which is a command to be executed when that signal is recieved. The slot can also contain DEFAULT_SIG, which means do whatever you were going to do before you were so rudely interrupted, or IGNORE_SIG, which says ignore this signal. */ char *trap_list[NSIG]; /* A translation list so we can be polite to our users. */ char *signal_names[NSIG] = { "ON_EXIT", /* `signal' 0 is what we do on exit. */ "SIGHUP", /* hangup */ "SIGINT", /* interrupt */ "SIGQUIT", /* quit */ "SIGILL", /* illegal instruction (not reset when caught) */ "SIGTRAP", /* trace trap (not reset when caught) */ #ifdef SIGABRT "SIGABRT", #else "SIGIOT", /* IOT instruction */ #endif "SIGEMT", /* EMT instruction */ "SIGFPE", /* floating point exception */ "SIGKILL", /* kill (cannot be caught or ignored) */ "SIGBUS", /* bus error */ "SIGSEGV", /* segmentation violation */ "SIGSYS", /* bad argument to system call */ "SIGPIPE", /* write on a pipe with no one to read it */ "SIGALRM", /* alarm clock */ "SIGTERM", /* software termination signal from kill */ #ifdef SYSV "SIGUSR1", "SIGUSR2", "SIGCLD", "SIGPWR", #if NSIG == 23 "SIGJUNK1", "SIGJUNK2", "SIGPOLL" #endif #else "SIGURG", /* urgent condition on IO channel */ "SIGSTOP", /* sendable stop signal not from tty */ "SIGTSTP", /* stop signal from tty */ "SIGCONT", /* continue a stopped process */ "SIGCHLD", /* to parent on child stop or exit */ "SIGTTIN", /* to readers pgrp upon background tty read */ "SIGTTOU", /* like TTIN for output if (tp->t_local<OSTOP) */ "SIGIO", /* input/output possible signal */ "SIGXCPU", /* exceeded CPU time limit */ "SIGXFSZ", /* exceeded file size limit */ "SIGVTALRM", /* virtual time alarm */ "SIGPROF", /* profiling time alarm */ "SIGWINCH", /* window changed */ "SIGLOST", /* resource lost (eg, record-lock lost) */ "SIGUSR1", /* user defined signal 1 */ "SIGUSR2" /* user defined signal 2 */ #endif }; initialize_traps () { register int i; for (i = 0; i < NSIG; i++) { trap_list[i] = (char *)DEFAULT_SIG; original_signals[i] = (SigHandler *)signal (i, SIG_DFL); signal (i, original_signals[i]); } } /* Return the print name of this signal. */ char * signal_name (signal) int signal; { if (signal > NSIG) return ("bad signal number"); else return (signal_names[signal]); } /* Turn a string into a signal number, or a number into a signal number. If STRING was "2", "SIGINT", or "INT", then (int)2 would be returned. */ int decode_signal (string) char *string; { int sig; if (sscanf (string, "%d", &sig) == 1) { if (sig < NSIG && sig >= 0) return (sig); else return (NO_SIG); } for (sig = 0; sig < NSIG; sig++) if ((strcmp (string, signal_names[sig]) == 0) || (strcmp (string, &(signal_names[sig])[3]) == 0)) return (sig); return (NO_SIG); } sighandler trap_handler (sig) int sig; { if ((sig >= NSIG) || (((int)trap_list[sig]) == 0)) programming_error ("trap_handler: Bad signal %d", sig); else parse_and_execute (savestring (trap_list[sig]), "trap"); } /* Set SIG to call STRING as a command. */ void set_signal (sig, string) int sig; char *string; { void change_signal (); signal (sig, SIG_IGN); change_signal (sig, savestring (string)); signal (sig, trap_handler); } /* If SIG has a string assigned to it, get rid of it. Then give it VALUE. */ void change_signal (sig, value) int sig; char *value; { if (((int)trap_list[sig]) > 0) free (trap_list[sig]); trap_list[sig] = value; } /* Restore the default action for SIG; i.e., the action the shell would have taken before you used the trap command. */ void restore_default_signal (sig) int sig; { signal (sig, original_signals[sig]); change_signal (sig, (char *)DEFAULT_SIG); } /* Make this signal be ignored. */ void ignore_signal (sig) int sig; { signal (sig, SIG_IGN); change_signal (sig, (char *)IGNORE_SIG); }