1 This file is trap.def, from which is created trap.c.
2 It implements the builtin "trap" in Bash.
4 Copyright (C) 1987-2015 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $FUNCTION trap_builtin
25 $SHORT_DOC trap [-lp] [[arg] signal_spec ...]
26 Trap signals and other events.
28 Defines and activates handlers to be run when the shell receives signals
31 ARG is a command to be read and executed when the shell receives the
32 signal(s) SIGNAL_SPEC. If ARG is absent (and a single SIGNAL_SPEC
33 is supplied) or `-', each specified signal is reset to its original
34 value. If ARG is the null string each SIGNAL_SPEC is ignored by the
35 shell and by the commands it invokes.
37 If a SIGNAL_SPEC is EXIT (0) ARG is executed on exit from the shell. If
38 a SIGNAL_SPEC is DEBUG, ARG is executed before every simple command. If
39 a SIGNAL_SPEC is RETURN, ARG is executed each time a shell function or a
40 script run by the . or source builtins finishes executing. A SIGNAL_SPEC
41 of ERR means to execute ARG each time a command's failure would cause the
42 shell to exit when the -e option is enabled.
44 If no arguments are supplied, trap prints the list of commands associated
48 -l print a list of signal names and their corresponding numbers
49 -p display the trap commands associated with each SIGNAL_SPEC
51 Each SIGNAL_SPEC is either a signal name in <signal.h> or a signal number.
52 Signal names are case insensitive and the SIG prefix is optional. A
53 signal may be sent to the shell with "kill -signal $$".
56 Returns success unless a SIGSPEC is invalid or an invalid option is given.
61 #if defined (HAVE_UNISTD_H)
63 # include <sys/types.h>
68 #include "../bashtypes.h"
71 #include "../bashansi.h"
76 #include "bashgetopt.h"
78 static void showtrap __P((int));
79 static int display_traps __P((WORD_LIST *));
83 trap <arg> <signal ...>
89 Set things up so that ARG is executed when SIGNAL(s) N is received.
90 If ARG is the empty string, then ignore the SIGNAL(s). If there is
91 no ARG, then set the trap for SIGNAL(s) to its original value. Just
92 plain "trap" means to print out the list of commands associated with
93 each signal number. Single arg of "-l" means list the signal names. */
95 /* Possible operations to perform on the list of signals.*/
96 #define SET 0 /* Set this signal to first_arg. */
97 #define REVERT 1 /* Revert to this signals original value. */
98 #define IGNORE 2 /* Ignore this signal. */
100 extern int posixly_correct, subshell_environment;
106 int list_signal_names, display, result, opt;
108 list_signal_names = display = 0;
109 result = EXECUTION_SUCCESS;
111 reset_internal_getopt ();
112 while ((opt = internal_getopt (list, "lp")) != -1)
130 opt = DSIG_NOCASE|DSIG_SIGPREFIX; /* flags for decode_signal */
132 if (list_signal_names)
133 return (sh_chkwrite (display_signal_list ((WORD_LIST *)NULL, 1)));
134 else if (display || list == 0)
136 initialize_terminating_signals ();
137 get_all_original_signals ();
138 return (sh_chkwrite (display_traps (list)));
143 int operation, sig, first_signal;
146 first_arg = list->word->word;
147 first_signal = first_arg && *first_arg && all_digits (first_arg) && signal_object_p (first_arg, opt);
149 /* Backwards compatibility. XXX - question about whether or not we
150 should throw an error if an all-digit argument doesn't correspond
151 to a valid signal number (e.g., if it's `50' on a system with only
155 /* When in posix mode, the historical behavior of looking for a
156 missing first argument is disabled. To revert to the original
157 signal handling disposition, use `-' as the first argument. */
158 else if (posixly_correct == 0 && first_arg && *first_arg &&
159 (*first_arg != '-' || first_arg[1]) &&
160 signal_object_p (first_arg, opt) && list->next == 0)
170 else if (*first_arg == '\0')
172 else if (first_arg[0] == '-' && !first_arg[1])
176 /* If we're in a command substitution, we haven't freed the trap strings
177 (though we reset the signal handlers). If we're setting a trap to
178 handle a signal here, free the rest of the trap strings since they
179 don't apply any more. */
180 if (subshell_environment & SUBSHELL_RESETTRAP)
182 free_trap_strings ();
183 subshell_environment &= ~SUBSHELL_RESETTRAP;
188 sig = decode_signal (list->word->word, opt);
192 sh_invalidsig (list->word->word);
193 result = EXECUTION_FAILURE;
200 set_signal (sig, first_arg);
204 restore_default_signal (sig);
206 /* Signals that the shell treats specially need special
211 /* XXX - should we do this if original disposition
214 set_signal_handler (SIGINT, sigint_sighandler);
216 set_signal_handler (SIGINT, termsig_sighandler);
220 /* Always ignore SIGQUIT. */
221 set_signal_handler (SIGQUIT, SIG_IGN);
224 #if defined (JOB_CONTROL)
228 #endif /* JOB_CONTROL */
230 set_signal_handler (sig, SIG_IGN);
254 if (p == (char *)DEFAULT_SIG && signal_is_hard_ignored (i) == 0)
256 else if (signal_is_hard_ignored (i))
259 t = (p == (char *)IGNORE_SIG) ? (char *)NULL : sh_single_quote (p);
261 sn = signal_name (i);
262 /* Make sure that signals whose names are unknown (for whatever reason)
263 are printed as signal numbers. */
264 if (STREQN (sn, "SIGJUNK", 7) || STREQN (sn, "unknown", 7))
265 printf ("trap -- %s %d\n", t ? t : "''", i);
266 else if (posixly_correct)
268 if (STREQN (sn, "SIG", 3))
269 printf ("trap -- %s %s\n", t ? t : "''", sn+3);
271 printf ("trap -- %s %s\n", t ? t : "''", sn);
274 printf ("trap -- %s %s\n", t ? t : "''", sn);
287 for (i = 0; i < BASH_NSIG; i++)
289 return (EXECUTION_SUCCESS);
292 for (result = EXECUTION_SUCCESS; list; list = list->next)
294 i = decode_signal (list->word->word, DSIG_NOCASE|DSIG_SIGPREFIX);
297 sh_invalidsig (list->word->word);
298 result = EXECUTION_FAILURE;