1 This file is trap.def, from which is created trap.c.
2 It implements the builtin "trap" in Bash.
4 Copyright (C) 1987-2006 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION trap_builtin
26 $SHORT_DOC trap [-lp] [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 (and a single SIGNAL_SPEC
29 is supplied) or `-', each specified signal is reset to its original
30 value. If ARG is the null string each SIGNAL_SPEC is ignored by the
31 shell and by the commands it invokes. If a SIGNAL_SPEC is EXIT (0)
32 the command ARG is executed on exit from the shell. If a SIGNAL_SPEC
33 is DEBUG, ARG is executed after every simple command. If the`-p' option
34 is supplied then the trap commands associated with each SIGNAL_SPEC are
35 displayed. If no arguments are supplied or if only `-p' is given, trap
36 prints the list of commands associated with each signal. Each SIGNAL_SPEC
37 is either a signal name in <signal.h> or a signal number. Signal names
38 are case insensitive and the SIG prefix is optional. `trap -l' prints
39 a list of signal names and their corresponding numbers. Note that a
40 signal can be sent to the shell with "kill -signal $$".
45 #if defined (HAVE_UNISTD_H)
47 # include <sys/types.h>
52 #include "../bashtypes.h"
55 #include "../bashansi.h"
60 #include "bashgetopt.h"
62 static void showtrap __P((int));
63 static int display_traps __P((WORD_LIST *));
67 trap <arg> <signal ...>
73 Set things up so that ARG is executed when SIGNAL(s) N is recieved.
74 If ARG is the empty string, then ignore the SIGNAL(s). If there is
75 no ARG, then set the trap for SIGNAL(s) to its original value. Just
76 plain "trap" means to print out the list of commands associated with
77 each signal number. Single arg of "-l" means list the signal names. */
79 /* Possible operations to perform on the list of signals.*/
80 #define SET 0 /* Set this signal to first_arg. */
81 #define REVERT 1 /* Revert to this signals original value. */
82 #define IGNORE 2 /* Ignore this signal. */
84 extern int posixly_correct;
90 int list_signal_names, display, result, opt;
92 list_signal_names = display = 0;
93 result = EXECUTION_SUCCESS;
94 reset_internal_getopt ();
95 while ((opt = internal_getopt (list, "lp")) != -1)
112 opt = DSIG_NOCASE|DSIG_SIGPREFIX; /* flags for decode_signal */
114 if (list_signal_names)
115 return (display_signal_list ((WORD_LIST *)NULL, 1));
116 else if (display || list == 0)
117 return (display_traps (list));
121 int operation, sig, first_signal;
124 first_arg = list->word->word;
125 first_signal = first_arg && *first_arg && all_digits (first_arg) && signal_object_p (first_arg, opt);
127 /* Backwards compatibility. XXX - question about whether or not we
128 should throw an error if an all-digit argument doesn't correspond
129 to a valid signal number (e.g., if it's `50' on a system with only
133 /* When in posix mode, the historical behavior of looking for a
134 missing first argument is disabled. To revert to the original
135 signal handling disposition, use `-' as the first argument. */
136 else if (posixly_correct == 0 && first_arg && *first_arg &&
137 (*first_arg != '-' || first_arg[1]) &&
138 signal_object_p (first_arg, opt) && list->next == 0)
148 else if (*first_arg == '\0')
150 else if (first_arg[0] == '-' && !first_arg[1])
156 sig = decode_signal (list->word->word, opt);
160 sh_invalidsig (list->word->word);
161 result = EXECUTION_FAILURE;
168 set_signal (sig, first_arg);
172 restore_default_signal (sig);
174 /* Signals that the shell treats specially need special
180 set_signal_handler (SIGINT, sigint_sighandler);
182 set_signal_handler (SIGINT, termsig_sighandler);
186 /* Always ignore SIGQUIT. */
187 set_signal_handler (SIGQUIT, SIG_IGN);
190 #if defined (JOB_CONTROL)
194 #endif /* JOB_CONTROL */
196 set_signal_handler (sig, SIG_IGN);
220 if (p == (char *)DEFAULT_SIG)
223 t = (p == (char *)IGNORE_SIG) ? (char *)NULL : sh_single_quote (p);
224 sn = signal_name (i);
225 /* Make sure that signals whose names are unknown (for whatever reason)
226 are printed as signal numbers. */
227 if (STREQN (sn, "SIGJUNK", 7) || STREQN (sn, "unknown", 7))
228 printf ("trap -- %s %d\n", t ? t : "''", i);
229 else if (posixly_correct)
231 if (STREQN (sn, "SIG", 3))
232 printf ("trap -- %s %s\n", t ? t : "''", sn+3);
234 printf ("trap -- %s %s\n", t ? t : "''", sn);
237 printf ("trap -- %s %s\n", t ? t : "''", sn);
250 for (i = 0; i < BASH_NSIG; i++)
252 return (EXECUTION_SUCCESS);
255 for (result = EXECUTION_SUCCESS; list; list = list->next)
257 i = decode_signal (list->word->word, DSIG_NOCASE|DSIG_SIGPREFIX);
260 sh_invalidsig (list->word->word);
261 result = EXECUTION_FAILURE;