1 This file is trap.def, from which is created trap.c.
2 It implements the builtin "trap" in Bash.
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION trap_builtin
26 $SHORT_DOC trap [arg] [signal_spec]
27 The command ARG is to be read and executed when the shell receives
28 signal(s) SIGNAL_SPEC. If ARG is absent all specified signals are
29 reset to their original values. If ARG is the null string this
30 signal is ignored by the shell and by the commands it invokes. If
31 SIGNAL_SPEC is EXIT (0) the command ARG is executed on exit from
32 the shell. The trap command with no arguments prints the list of
33 commands associated with each signal number. SIGNAL_SPEC is either
34 a signal name in <signal.h>, or a signal number. The syntax `trap -l'
35 prints a list of signal names and their corresponding numbers.
36 Note that a signal can be sent to the shell with "kill -signal $$".
39 #include <sys/types.h>
47 trap <arg> <signal ...>
52 Set things up so that ARG is executed when SIGNAL(s) N is recieved.
53 If ARG is the empty string, then ignore the SIGNAL(s). If there is
54 no ARG, then set the trap for SIGNAL(s) to its original value. Just
55 plain "trap" means to print out the list of commands associated with
56 each signal number. Single arg of "-l" means list the signal names. */
58 /* Possible operations to perform on the list of signals.*/
59 #define SET 0 /* Set this signal to first_arg. */
60 #define REVERT 1 /* Revert to this signals original value. */
61 #define IGNORE 2 /* Ignore this signal. */
63 extern int interactive;
69 int list_signal_names = 0;
73 if (ISOPTION (list->word->word, 'l'))
78 else if (ISOPTION (list->word->word, '-'))
83 else if ((*list->word->word == '-') && list->word->word[1])
85 bad_option (list->word->word);
86 builtin_error ("usage: trap [-l] [arg] [sigspec]");
93 if (list_signal_names)
97 for (i = 0; i < NSIG; i++)
99 printf ("%2d) %s", i, signal_name (i));
110 return (EXECUTION_SUCCESS);
115 char *first_arg = list->word->word;
116 int operation = SET, any_failed = 0;
118 if (signal_object_p (first_arg))
123 if (*first_arg == '\0')
125 else if (first_arg[0] == '-' && !first_arg[1])
133 sig = decode_signal (list->word->word);
137 builtin_error ("%s: not a signal specification",
146 set_signal (sig, first_arg);
150 restore_default_signal (sig);
152 /* Signals that the shell treats specially need special
158 set_signal_handler (SIGINT, sigint_sighandler);
160 set_signal_handler (SIGINT, termination_unwind_protect);
164 /* Always ignore SIGQUIT. */
165 set_signal_handler (SIGQUIT, SIG_IGN);
168 #if defined (JOB_CONTROL)
172 #endif /* JOB_CONTROL */
174 set_signal_handler (sig, SIG_IGN);
186 return ((!any_failed) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
189 for (i = 0; i < NSIG; i++)
195 if (p == (char *)DEFAULT_SIG)
198 t = (p == (char *)IGNORE_SIG) ? (char *)NULL : single_quote (p);
199 printf ("trap -- %s %s\n", t ? t : "''", signal_name (i));
203 return (EXECUTION_SUCCESS);