]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/trap.def
Imported from ../bash-3.0.tar.gz.
[thirdparty/bash.git] / builtins / trap.def
1 This file is trap.def, from which is created trap.c.
2 It implements the builtin "trap" in Bash.
3
4 Copyright (C) 1987-2004 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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
11 version.
12
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
16 for more details.
17
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.
21
22 $PRODUCES trap.c
23
24 $BUILTIN trap
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 $$".
41 $END
42
43 #include <config.h>
44
45 #if defined (HAVE_UNISTD_H)
46 # ifdef _MINIX
47 # include <sys/types.h>
48 # endif
49 # include <unistd.h>
50 #endif
51
52 #include "../bashtypes.h"
53 #include <signal.h>
54 #include <stdio.h>
55 #include "../bashansi.h"
56
57 #include "../shell.h"
58 #include "../trap.h"
59 #include "common.h"
60 #include "bashgetopt.h"
61
62 static void showtrap __P((int));
63 static int display_traps __P((WORD_LIST *));
64
65 /* The trap command:
66
67 trap <arg> <signal ...>
68 trap <signal ...>
69 trap -l
70 trap -p [sigspec ...]
71 trap [--]
72
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. */
78
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. */
83
84 extern int posixly_correct;
85
86 int
87 trap_builtin (list)
88 WORD_LIST *list;
89 {
90 int list_signal_names, display, result, opt;
91
92 list_signal_names = display = 0;
93 result = EXECUTION_SUCCESS;
94 reset_internal_getopt ();
95 while ((opt = internal_getopt (list, "lp")) != -1)
96 {
97 switch (opt)
98 {
99 case 'l':
100 list_signal_names++;
101 break;
102 case 'p':
103 display++;
104 break;
105 default:
106 builtin_usage ();
107 return (EX_USAGE);
108 }
109 }
110 list = loptend;
111
112 opt = DSIG_NOCASE|DSIG_SIGPREFIX; /* flags for decode_signal */
113
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));
118 else
119 {
120 char *first_arg;
121 int operation, sig;
122
123 operation = SET;
124 first_arg = list->word->word;
125 /* When in posix mode, the historical behavior of looking for a
126 missing first argument is disabled. To revert to the original
127 signal handling disposition, use `-' as the first argument. */
128 if (posixly_correct == 0 && first_arg && *first_arg &&
129 (*first_arg != '-' || first_arg[1]) &&
130 signal_object_p (first_arg, opt) && list->next == 0)
131 operation = REVERT;
132 else
133 {
134 list = list->next;
135 if (list == 0)
136 {
137 builtin_usage ();
138 return (EX_USAGE);
139 }
140 else if (*first_arg == '\0')
141 operation = IGNORE;
142 else if (first_arg[0] == '-' && !first_arg[1])
143 operation = REVERT;
144 }
145
146 while (list)
147 {
148 sig = decode_signal (list->word->word, opt);
149
150 if (sig == NO_SIG)
151 {
152 sh_invalidsig (list->word->word);
153 result = EXECUTION_FAILURE;
154 }
155 else
156 {
157 switch (operation)
158 {
159 case SET:
160 set_signal (sig, first_arg);
161 break;
162
163 case REVERT:
164 restore_default_signal (sig);
165
166 /* Signals that the shell treats specially need special
167 handling. */
168 switch (sig)
169 {
170 case SIGINT:
171 if (interactive)
172 set_signal_handler (SIGINT, sigint_sighandler);
173 else
174 set_signal_handler (SIGINT, termination_unwind_protect);
175 break;
176
177 case SIGQUIT:
178 /* Always ignore SIGQUIT. */
179 set_signal_handler (SIGQUIT, SIG_IGN);
180 break;
181 case SIGTERM:
182 #if defined (JOB_CONTROL)
183 case SIGTTIN:
184 case SIGTTOU:
185 case SIGTSTP:
186 #endif /* JOB_CONTROL */
187 if (interactive)
188 set_signal_handler (sig, SIG_IGN);
189 break;
190 }
191 break;
192
193 case IGNORE:
194 ignore_signal (sig);
195 break;
196 }
197 }
198 list = list->next;
199 }
200 }
201
202 return (result);
203 }
204
205 static void
206 showtrap (i)
207 int i;
208 {
209 char *t, *p, *sn;
210
211 p = trap_list[i];
212
213 if (p == (char *)DEFAULT_SIG)
214 return;
215
216 t = (p == (char *)IGNORE_SIG) ? (char *)NULL : sh_single_quote (p);
217 sn = signal_name (i);
218 /* Make sure that signals whose names are unknown (for whatever reason)
219 are printed as signal numbers. */
220 if (STREQN (sn, "SIGJUNK", 7) || STREQN (sn, "unknown", 7))
221 printf ("trap -- %s %d\n", t ? t : "''", i);
222 else if (posixly_correct)
223 {
224 if (STREQN (sn, "SIG", 3))
225 printf ("trap -- %s %s\n", t ? t : "''", sn+3);
226 else
227 printf ("trap -- %s %s\n", t ? t : "''", sn);
228 }
229 else
230 printf ("trap -- %s %s\n", t ? t : "''", sn);
231
232 FREE (t);
233 }
234
235 static int
236 display_traps (list)
237 WORD_LIST *list;
238 {
239 int result, i;
240
241 if (list == 0)
242 {
243 for (i = 0; i < BASH_NSIG; i++)
244 showtrap (i);
245 return (EXECUTION_SUCCESS);
246 }
247
248 for (result = EXECUTION_SUCCESS; list; list = list->next)
249 {
250 i = decode_signal (list->word->word, DSIG_NOCASE|DSIG_SIGPREFIX);
251 if (i == NO_SIG)
252 {
253 sh_invalidsig (list->word->word);
254 result = EXECUTION_FAILURE;
255 }
256 else
257 showtrap (i);
258 }
259
260 return (result);
261 }