]>
Commit | Line | Data |
---|---|---|
af12dacd | 1 | /* execute_cmd.c -- Execute a COMMAND structure. */ |
726f6388 | 2 | |
5a318736 | 3 | /* Copyright (C) 1987-2012 Free Software Foundation, Inc. |
726f6388 JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
2e4498b3 CR |
7 | Bash is free software: you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
726f6388 | 11 | |
2e4498b3 CR |
12 | Bash is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
726f6388 JA |
16 | |
17 | You should have received a copy of the GNU General Public License | |
2e4498b3 CR |
18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
19 | */ | |
20 | ||
ccc6cda3 JA |
21 | #include "config.h" |
22 | ||
23 | #if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX) | |
726f6388 | 24 | #pragma alloca |
ccc6cda3 | 25 | #endif /* _AIX && RISC6000 && !__GNUC__ */ |
726f6388 JA |
26 | |
27 | #include <stdio.h> | |
f73dda09 | 28 | #include "chartypes.h" |
726f6388 | 29 | #include "bashtypes.h" |
d3a24ed2 | 30 | #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H) |
cce855bc JA |
31 | # include <sys/file.h> |
32 | #endif | |
726f6388 JA |
33 | #include "filecntl.h" |
34 | #include "posixstat.h" | |
35 | #include <signal.h> | |
fd58d46e | 36 | #if defined (HAVE_SYS_PARAM_H) |
cce855bc JA |
37 | # include <sys/param.h> |
38 | #endif | |
726f6388 | 39 | |
ccc6cda3 JA |
40 | #if defined (HAVE_UNISTD_H) |
41 | # include <unistd.h> | |
42 | #endif | |
43 | ||
bb70624e | 44 | #include "posixtime.h" |
ccc6cda3 | 45 | |
cce855bc | 46 | #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE) |
ccc6cda3 JA |
47 | # include <sys/resource.h> |
48 | #endif | |
49 | ||
50 | #if defined (HAVE_SYS_TIMES_H) && defined (HAVE_TIMES) | |
51 | # include <sys/times.h> | |
726f6388 JA |
52 | #endif |
53 | ||
726f6388 JA |
54 | #include <errno.h> |
55 | ||
56 | #if !defined (errno) | |
57 | extern int errno; | |
58 | #endif | |
59 | ||
a3143574 CR |
60 | #define NEED_FPURGE_DECL |
61 | ||
ccc6cda3 | 62 | #include "bashansi.h" |
5e13499c | 63 | #include "bashintl.h" |
726f6388 | 64 | |
ccc6cda3 | 65 | #include "memalloc.h" |
726f6388 | 66 | #include "shell.h" |
d166f048 | 67 | #include <y.tab.h> /* use <...> so we pick it up from the build directory */ |
726f6388 | 68 | #include "flags.h" |
ccc6cda3 JA |
69 | #include "builtins.h" |
70 | #include "hashlib.h" | |
726f6388 JA |
71 | #include "jobs.h" |
72 | #include "execute_cmd.h" | |
cce855bc JA |
73 | #include "findcmd.h" |
74 | #include "redir.h" | |
ccc6cda3 JA |
75 | #include "trap.h" |
76 | #include "pathexp.h" | |
d166f048 | 77 | #include "hashcmd.h" |
726f6388 | 78 | |
cce855bc JA |
79 | #if defined (COND_COMMAND) |
80 | # include "test.h" | |
81 | #endif | |
82 | ||
726f6388 JA |
83 | #include "builtins/common.h" |
84 | #include "builtins/builtext.h" /* list of builtins */ | |
85 | ||
f73dda09 | 86 | #include <glob/strmatch.h> |
726f6388 JA |
87 | #include <tilde/tilde.h> |
88 | ||
89 | #if defined (BUFFERED_INPUT) | |
90 | # include "input.h" | |
91 | #endif | |
92 | ||
ccc6cda3 JA |
93 | #if defined (ALIAS) |
94 | # include "alias.h" | |
95 | #endif | |
96 | ||
97 | #if defined (HISTORY) | |
98 | # include "bashhist.h" | |
99 | #endif | |
100 | ||
7d92f73f | 101 | extern int dollar_dollar_pid; |
726f6388 | 102 | extern int posixly_correct; |
28157acd | 103 | extern int expand_aliases; |
d3ad40de CR |
104 | extern int autocd; |
105 | extern int breaking, continuing, loop_level; | |
f486d0a1 | 106 | extern int parse_and_execute_level, running_trap, sourcelevel; |
f73dda09 | 107 | extern int command_string_index, line_number; |
726f6388 | 108 | extern int dot_found_in_search; |
ccc6cda3 | 109 | extern int already_making_children; |
56299fa5 | 110 | extern int tempenv_assign_error; |
726f6388 JA |
111 | extern char *the_printed_command, *shell_name; |
112 | extern pid_t last_command_subst_pid; | |
f73dda09 | 113 | extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin; |
726f6388 | 114 | extern char **subshell_argv, **subshell_envp; |
ccc6cda3 | 115 | extern int subshell_argc; |
64419627 | 116 | extern time_t shell_start_time; |
f73dda09 | 117 | #if 0 |
ccc6cda3 | 118 | extern char *glob_argv_flags; |
f73dda09 | 119 | #endif |
726f6388 | 120 | |
c74f63f3 CR |
121 | extern int job_control; /* XXX */ |
122 | ||
f73dda09 | 123 | extern int close __P((int)); |
726f6388 JA |
124 | |
125 | /* Static functions defined and used in this file. */ | |
f73dda09 JA |
126 | static void close_pipes __P((int, int)); |
127 | static void do_piping __P((int, int)); | |
128 | static void bind_lastarg __P((char *)); | |
129 | static int shell_control_structure __P((enum command_type)); | |
130 | static void cleanup_redirects __P((REDIRECT *)); | |
131 | ||
132 | #if defined (JOB_CONTROL) | |
133 | static int restore_signal_mask __P((sigset_t *)); | |
134 | #endif | |
ccc6cda3 | 135 | |
f73dda09 JA |
136 | static void async_redirect_stdin __P((void)); |
137 | ||
138 | static int builtin_status __P((int)); | |
139 | ||
140 | static int execute_for_command __P((FOR_COM *)); | |
ccc6cda3 | 141 | #if defined (SELECT_COMMAND) |
220537f2 | 142 | static int displen __P((const char *)); |
f73dda09 JA |
143 | static int print_index_and_element __P((int, int, WORD_LIST *)); |
144 | static void indent __P((int, int)); | |
145 | static void print_select_list __P((WORD_LIST *, int, int, int)); | |
7117c2d2 | 146 | static char *select_query __P((WORD_LIST *, int, char *, int)); |
f73dda09 | 147 | static int execute_select_command __P((SELECT_COM *)); |
ccc6cda3 | 148 | #endif |
cce855bc | 149 | #if defined (DPAREN_ARITHMETIC) |
f73dda09 | 150 | static int execute_arith_command __P((ARITH_COM *)); |
cce855bc JA |
151 | #endif |
152 | #if defined (COND_COMMAND) | |
f73dda09 JA |
153 | static int execute_cond_node __P((COND_COM *)); |
154 | static int execute_cond_command __P((COND_COM *)); | |
cce855bc | 155 | #endif |
d166f048 | 156 | #if defined (COMMAND_TIMING) |
f73dda09 JA |
157 | static int mkfmt __P((char *, int, int, time_t, int)); |
158 | static void print_formatted_time __P((FILE *, char *, | |
159 | time_t, int, time_t, int, | |
160 | time_t, int, int)); | |
161 | static int time_command __P((COMMAND *, int, int, int, struct fd_bitmap *)); | |
d166f048 | 162 | #endif |
bb70624e | 163 | #if defined (ARITH_FOR_COMMAND) |
7117c2d2 | 164 | static intmax_t eval_arith_for_expr __P((WORD_LIST *, int *)); |
f73dda09 | 165 | static int execute_arith_for_command __P((ARITH_FOR_COM *)); |
bb70624e | 166 | #endif |
f73dda09 JA |
167 | static int execute_case_command __P((CASE_COM *)); |
168 | static int execute_while_command __P((WHILE_COM *)); | |
169 | static int execute_until_command __P((WHILE_COM *)); | |
170 | static int execute_while_or_until __P((WHILE_COM *, int)); | |
171 | static int execute_if_command __P((IF_COM *)); | |
43df7bbb | 172 | static int execute_null_command __P((REDIRECT *, int, int, int)); |
f73dda09 JA |
173 | static void fix_assignment_words __P((WORD_LIST *)); |
174 | static int execute_simple_command __P((SIMPLE_COM *, int, int, int, struct fd_bitmap *)); | |
175 | static int execute_builtin __P((sh_builtin_func_t *, WORD_LIST *, int, int)); | |
176 | static int execute_function __P((SHELL_VAR *, WORD_LIST *, int, struct fd_bitmap *, int, int)); | |
177 | static int execute_builtin_or_function __P((WORD_LIST *, sh_builtin_func_t *, | |
178 | SHELL_VAR *, | |
179 | REDIRECT *, struct fd_bitmap *, int)); | |
180 | static void execute_subshell_builtin_or_function __P((WORD_LIST *, REDIRECT *, | |
181 | sh_builtin_func_t *, | |
182 | SHELL_VAR *, | |
183 | int, int, int, | |
184 | struct fd_bitmap *, | |
185 | int)); | |
6faad625 | 186 | static int execute_disk_command __P((WORD_LIST *, REDIRECT *, char *, |
f73dda09 JA |
187 | int, int, int, struct fd_bitmap *, int)); |
188 | ||
189 | static char *getinterp __P((char *, int, int *)); | |
190 | static void initialize_subshell __P((void)); | |
191 | static int execute_in_subshell __P((COMMAND *, int, int, int, struct fd_bitmap *)); | |
09767ff0 CR |
192 | #if defined (COPROCESS_SUPPORT) |
193 | static int execute_coproc __P((COMMAND *, int, int, struct fd_bitmap *)); | |
194 | #endif | |
f73dda09 JA |
195 | |
196 | static int execute_pipeline __P((COMMAND *, int, int, int, struct fd_bitmap *)); | |
197 | ||
198 | static int execute_connection __P((COMMAND *, int, int, int, struct fd_bitmap *)); | |
199 | ||
ba4ab055 | 200 | static int execute_intern_function __P((WORD_DESC *, FUNCTION_DEF *)); |
f73dda09 | 201 | |
d166f048 JA |
202 | /* Set to 1 if fd 0 was the subject of redirection to a subshell. Global |
203 | so that reader_loop can set it to zero before executing a command. */ | |
204 | int stdin_redir; | |
726f6388 JA |
205 | |
206 | /* The name of the command that is currently being executed. | |
207 | `test' needs this, for example. */ | |
208 | char *this_command_name; | |
209 | ||
d3a24ed2 CR |
210 | /* The printed representation of the currently-executing command (same as |
211 | the_printed_command), except when a trap is being executed. Useful for | |
212 | a debugger to know where exactly the program is currently executing. */ | |
213 | char *the_printed_command_except_trap; | |
214 | ||
726f6388 | 215 | /* For catching RETURN in a function. */ |
ccc6cda3 | 216 | int return_catch_flag; |
726f6388 | 217 | int return_catch_value; |
ccc6cda3 | 218 | procenv_t return_catch; |
726f6388 JA |
219 | |
220 | /* The value returned by the last synchronous command. */ | |
ccc6cda3 | 221 | int last_command_exit_value; |
726f6388 | 222 | |
d3a24ed2 CR |
223 | /* Whether or not the last command (corresponding to last_command_exit_value) |
224 | was terminated by a signal, and, if so, which one. */ | |
225 | int last_command_exit_signal; | |
226 | ||
726f6388 JA |
227 | /* The list of redirections to perform which will undo the redirections |
228 | that I made in the shell. */ | |
229 | REDIRECT *redirection_undo_list = (REDIRECT *)NULL; | |
230 | ||
231 | /* The list of redirections to perform which will undo the internal | |
232 | redirections performed by the `exec' builtin. These are redirections | |
233 | that must be undone even when exec discards redirection_undo_list. */ | |
234 | REDIRECT *exec_redirection_undo_list = (REDIRECT *)NULL; | |
235 | ||
d7f49990 CR |
236 | /* When greater than zero, value is the `level' of builtins we are |
237 | currently executing (e.g. `eval echo a' would have it set to 2). */ | |
238 | int executing_builtin = 0; | |
239 | ||
f1c4df24 CR |
240 | /* Non-zero if we are executing a command list (a;b;c, etc.) */ |
241 | int executing_list = 0; | |
242 | ||
012bac39 CR |
243 | /* Non-zero if failing commands in a command substitution should not exit the |
244 | shell even if -e is set. Used to pass the CMD_IGNORE_RETURN flag down to | |
245 | commands run in command substitutions by parse_and_execute. */ | |
246 | int comsub_ignore_return = 0; | |
247 | ||
726f6388 JA |
248 | /* Non-zero if we have just forked and are currently running in a subshell |
249 | environment. */ | |
ccc6cda3 JA |
250 | int subshell_environment; |
251 | ||
d3a24ed2 CR |
252 | /* Count of nested subshells, like SHLVL. Available via $BASH_SUBSHELL */ |
253 | int subshell_level = 0; | |
254 | ||
bb70624e JA |
255 | /* Currently-executing shell function. */ |
256 | SHELL_VAR *this_shell_function; | |
257 | ||
2206f89a CR |
258 | /* If non-zero, matches in case and [[ ... ]] are case-insensitive */ |
259 | int match_ignore_case = 0; | |
260 | ||
aad3dfa2 CR |
261 | int executing_command_builtin = 0; |
262 | ||
d7f49990 CR |
263 | struct stat SB; /* used for debugging */ |
264 | ||
265 | static int special_builtin_failed; | |
266 | ||
267 | static COMMAND *currently_executing_command; | |
268 | ||
269 | /* The line number that the currently executing function starts on. */ | |
270 | static int function_line_number; | |
271 | ||
272 | /* XXX - set to 1 if we're running the DEBUG trap and we want to show the line | |
273 | number containing the function name. Used by executing_line_number to | |
274 | report the correct line number. Kind of a hack. */ | |
275 | static int showing_function_line; | |
276 | ||
8360b906 CR |
277 | /* $LINENO ($BASH_LINENO) for use by an ERR trap. Global so parse_and_execute |
278 | can save and restore it. */ | |
279 | int line_number_for_err_trap; | |
d7f49990 | 280 | |
f486d0a1 | 281 | /* A sort of function nesting level counter */ |
6faad625 | 282 | int funcnest = 0; |
d1fab3dc | 283 | int funcnest_max = 0; /* XXX - bash-4.2 */ |
f486d0a1 | 284 | |
7d92f73f CR |
285 | int lastpipe_opt = 0; |
286 | ||
726f6388 JA |
287 | struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL; |
288 | ||
f73dda09 | 289 | #define FD_BITMAP_DEFAULT_SIZE 32 |
d166f048 | 290 | |
726f6388 JA |
291 | /* Functions to allocate and deallocate the structures used to pass |
292 | information from the shell to its children about file descriptors | |
293 | to close. */ | |
294 | struct fd_bitmap * | |
295 | new_fd_bitmap (size) | |
f73dda09 | 296 | int size; |
726f6388 JA |
297 | { |
298 | struct fd_bitmap *ret; | |
299 | ||
300 | ret = (struct fd_bitmap *)xmalloc (sizeof (struct fd_bitmap)); | |
301 | ||
302 | ret->size = size; | |
303 | ||
304 | if (size) | |
305 | { | |
f73dda09 | 306 | ret->bitmap = (char *)xmalloc (size); |
7117c2d2 | 307 | memset (ret->bitmap, '\0', size); |
726f6388 JA |
308 | } |
309 | else | |
310 | ret->bitmap = (char *)NULL; | |
311 | return (ret); | |
312 | } | |
313 | ||
314 | void | |
315 | dispose_fd_bitmap (fdbp) | |
316 | struct fd_bitmap *fdbp; | |
317 | { | |
318 | FREE (fdbp->bitmap); | |
319 | free (fdbp); | |
320 | } | |
321 | ||
322 | void | |
323 | close_fd_bitmap (fdbp) | |
324 | struct fd_bitmap *fdbp; | |
325 | { | |
326 | register int i; | |
327 | ||
328 | if (fdbp) | |
329 | { | |
330 | for (i = 0; i < fdbp->size; i++) | |
331 | if (fdbp->bitmap[i]) | |
332 | { | |
333 | close (i); | |
334 | fdbp->bitmap[i] = 0; | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
ccc6cda3 JA |
339 | /* Return the line number of the currently executing command. */ |
340 | int | |
341 | executing_line_number () | |
342 | { | |
d3a24ed2 CR |
343 | if (executing && showing_function_line == 0 && |
344 | (variable_context == 0 || interactive_shell == 0) && | |
345 | currently_executing_command) | |
7117c2d2 | 346 | { |
d3a24ed2 CR |
347 | #if defined (COND_COMMAND) |
348 | if (currently_executing_command->type == cm_cond) | |
7117c2d2 | 349 | return currently_executing_command->value.Cond->line; |
d3a24ed2 CR |
350 | #endif |
351 | #if defined (DPAREN_ARITHMETIC) | |
7117c2d2 JA |
352 | else if (currently_executing_command->type == cm_arith) |
353 | return currently_executing_command->value.Arith->line; | |
d3a24ed2 CR |
354 | #endif |
355 | #if defined (ARITH_FOR_COMMAND) | |
7117c2d2 JA |
356 | else if (currently_executing_command->type == cm_arith_for) |
357 | return currently_executing_command->value.ArithFor->line; | |
d3a24ed2 CR |
358 | #endif |
359 | ||
7117c2d2 JA |
360 | return line_number; |
361 | } | |
d166f048 JA |
362 | else |
363 | return line_number; | |
ccc6cda3 JA |
364 | } |
365 | ||
726f6388 JA |
366 | /* Execute the command passed in COMMAND. COMMAND is exactly what |
367 | read_command () places into GLOBAL_COMMAND. See "command.h" for the | |
368 | details of the command structure. | |
369 | ||
370 | EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible | |
371 | return values. Executing a command with nothing in it returns | |
372 | EXECUTION_SUCCESS. */ | |
ccc6cda3 | 373 | int |
726f6388 JA |
374 | execute_command (command) |
375 | COMMAND *command; | |
376 | { | |
377 | struct fd_bitmap *bitmap; | |
378 | int result; | |
379 | ||
380 | current_fds_to_close = (struct fd_bitmap *)NULL; | |
381 | bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE); | |
382 | begin_unwind_frame ("execute-command"); | |
383 | add_unwind_protect (dispose_fd_bitmap, (char *)bitmap); | |
384 | ||
385 | /* Just do the command, but not asynchronously. */ | |
386 | result = execute_command_internal (command, 0, NO_PIPE, NO_PIPE, bitmap); | |
387 | ||
388 | dispose_fd_bitmap (bitmap); | |
389 | discard_unwind_frame ("execute-command"); | |
390 | ||
391 | #if defined (PROCESS_SUBSTITUTION) | |
bb70624e JA |
392 | /* don't unlink fifos if we're in a shell function; wait until the function |
393 | returns. */ | |
394 | if (variable_context == 0) | |
395 | unlink_fifo_list (); | |
726f6388 JA |
396 | #endif /* PROCESS_SUBSTITUTION */ |
397 | ||
b317c077 | 398 | QUIT; |
726f6388 JA |
399 | return (result); |
400 | } | |
401 | ||
402 | /* Return 1 if TYPE is a shell control structure type. */ | |
403 | static int | |
404 | shell_control_structure (type) | |
405 | enum command_type type; | |
406 | { | |
407 | switch (type) | |
408 | { | |
bb70624e JA |
409 | #if defined (ARITH_FOR_COMMAND) |
410 | case cm_arith_for: | |
411 | #endif | |
726f6388 JA |
412 | #if defined (SELECT_COMMAND) |
413 | case cm_select: | |
cce855bc JA |
414 | #endif |
415 | #if defined (DPAREN_ARITHMETIC) | |
416 | case cm_arith: | |
417 | #endif | |
418 | #if defined (COND_COMMAND) | |
419 | case cm_cond: | |
726f6388 JA |
420 | #endif |
421 | case cm_case: | |
422 | case cm_while: | |
423 | case cm_until: | |
424 | case cm_if: | |
233564d2 | 425 | case cm_for: |
726f6388 | 426 | case cm_group: |
233564d2 | 427 | case cm_function_def: |
726f6388 JA |
428 | return (1); |
429 | ||
430 | default: | |
431 | return (0); | |
432 | } | |
433 | } | |
434 | ||
435 | /* A function to use to unwind_protect the redirection undo list | |
436 | for loops. */ | |
437 | static void | |
438 | cleanup_redirects (list) | |
439 | REDIRECT *list; | |
440 | { | |
d3a24ed2 | 441 | do_redirections (list, RX_ACTIVE); |
726f6388 JA |
442 | dispose_redirects (list); |
443 | } | |
444 | ||
ccc6cda3 | 445 | #if 0 |
726f6388 JA |
446 | /* Function to unwind_protect the redirections for functions and builtins. */ |
447 | static void | |
448 | cleanup_func_redirects (list) | |
449 | REDIRECT *list; | |
450 | { | |
d3a24ed2 | 451 | do_redirections (list, RX_ACTIVE); |
726f6388 | 452 | } |
ccc6cda3 | 453 | #endif |
726f6388 | 454 | |
cce855bc | 455 | void |
726f6388 JA |
456 | dispose_exec_redirects () |
457 | { | |
458 | if (exec_redirection_undo_list) | |
459 | { | |
460 | dispose_redirects (exec_redirection_undo_list); | |
461 | exec_redirection_undo_list = (REDIRECT *)NULL; | |
462 | } | |
463 | } | |
464 | ||
465 | #if defined (JOB_CONTROL) | |
466 | /* A function to restore the signal mask to its proper value when the shell | |
467 | is interrupted or errors occur while creating a pipeline. */ | |
468 | static int | |
469 | restore_signal_mask (set) | |
f73dda09 | 470 | sigset_t *set; |
726f6388 | 471 | { |
f73dda09 | 472 | return (sigprocmask (SIG_SETMASK, set, (sigset_t *)NULL)); |
726f6388 JA |
473 | } |
474 | #endif /* JOB_CONTROL */ | |
475 | ||
f73dda09 | 476 | #ifdef DEBUG |
726f6388 JA |
477 | /* A debugging function that can be called from gdb, for instance. */ |
478 | void | |
479 | open_files () | |
480 | { | |
481 | register int i; | |
482 | int f, fd_table_size; | |
483 | ||
484 | fd_table_size = getdtablesize (); | |
485 | ||
f73dda09 | 486 | fprintf (stderr, "pid %ld open files:", (long)getpid ()); |
726f6388 JA |
487 | for (i = 3; i < fd_table_size; i++) |
488 | { | |
489 | if ((f = fcntl (i, F_GETFD, 0)) != -1) | |
490 | fprintf (stderr, " %d (%s)", i, f ? "close" : "open"); | |
491 | } | |
492 | fprintf (stderr, "\n"); | |
493 | } | |
f73dda09 | 494 | #endif |
726f6388 | 495 | |
d166f048 JA |
496 | static void |
497 | async_redirect_stdin () | |
498 | { | |
499 | int fd; | |
500 | ||
501 | fd = open ("/dev/null", O_RDONLY); | |
502 | if (fd > 0) | |
503 | { | |
504 | dup2 (fd, 0); | |
505 | close (fd); | |
506 | } | |
507 | else if (fd < 0) | |
5e13499c | 508 | internal_error (_("cannot redirect standard input from /dev/null: %s"), strerror (errno)); |
d166f048 JA |
509 | } |
510 | ||
ccc6cda3 | 511 | #define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0) |
726f6388 JA |
512 | |
513 | /* Execute the command passed in COMMAND, perhaps doing it asynchrounously. | |
514 | COMMAND is exactly what read_command () places into GLOBAL_COMMAND. | |
515 | ASYNCHROUNOUS, if non-zero, says to do this command in the background. | |
516 | PIPE_IN and PIPE_OUT are file descriptors saying where input comes | |
517 | from and where it goes. They can have the value of NO_PIPE, which means | |
518 | I/O is stdin/stdout. | |
519 | FDS_TO_CLOSE is a list of file descriptors to close once the child has | |
520 | been forked. This list often contains the unusable sides of pipes, etc. | |
521 | ||
522 | EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible | |
523 | return values. Executing a command with nothing in it returns | |
524 | EXECUTION_SUCCESS. */ | |
ccc6cda3 JA |
525 | int |
526 | execute_command_internal (command, asynchronous, pipe_in, pipe_out, | |
726f6388 JA |
527 | fds_to_close) |
528 | COMMAND *command; | |
529 | int asynchronous; | |
530 | int pipe_in, pipe_out; | |
531 | struct fd_bitmap *fds_to_close; | |
532 | { | |
a05a1337 | 533 | int exec_result, user_subshell, invert, ignore_return, was_error_trap; |
d166f048 | 534 | REDIRECT *my_undo_list, *exec_undo_list; |
cc87ba64 | 535 | volatile int last_pid; |
d3a24ed2 | 536 | volatile int save_line_number; |
122f603c CR |
537 | #if defined (PROCESS_SUBSTITUTION) |
538 | volatile int ofifo, nfifo, osize, saved_fifo; | |
539 | volatile char *ofifo_list; | |
540 | #endif | |
726f6388 | 541 | |
fdf670ea CR |
542 | if (breaking || continuing) |
543 | return (last_command_exit_value); | |
544 | if (command == 0 || read_but_dont_execute) | |
545 | return (EXECUTION_SUCCESS); | |
726f6388 | 546 | |
b317c077 | 547 | QUIT; |
726f6388 JA |
548 | run_pending_traps (); |
549 | ||
d3a24ed2 | 550 | #if 0 |
ccc6cda3 | 551 | if (running_trap == 0) |
d3a24ed2 | 552 | #endif |
ccc6cda3 JA |
553 | currently_executing_command = command; |
554 | ||
726f6388 | 555 | invert = (command->flags & CMD_INVERT_RETURN) != 0; |
d166f048 JA |
556 | |
557 | /* If we're inverting the return value and `set -e' has been executed, | |
558 | we don't want a failing command to inadvertently cause the shell | |
559 | to exit. */ | |
560 | if (exit_immediately_on_error && invert) /* XXX */ | |
561 | command->flags |= CMD_IGNORE_RETURN; /* XXX */ | |
562 | ||
ccc6cda3 | 563 | exec_result = EXECUTION_SUCCESS; |
726f6388 JA |
564 | |
565 | /* If a command was being explicitly run in a subshell, or if it is | |
566 | a shell control-structure, and it has a pipe, then we do the command | |
567 | in a subshell. */ | |
bb70624e JA |
568 | if (command->type == cm_subshell && (command->flags & CMD_NO_FORK)) |
569 | return (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close)); | |
726f6388 | 570 | |
09767ff0 CR |
571 | #if defined (COPROCESS_SUPPORT) |
572 | if (command->type == cm_coproc) | |
573 | return (execute_coproc (command, pipe_in, pipe_out, fds_to_close)); | |
574 | #endif | |
575 | ||
a05a1337 CR |
576 | user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0); |
577 | ||
bb70624e JA |
578 | if (command->type == cm_subshell || |
579 | (command->flags & (CMD_WANT_SUBSHELL|CMD_FORCE_SUBSHELL)) || | |
726f6388 JA |
580 | (shell_control_structure (command->type) && |
581 | (pipe_out != NO_PIPE || pipe_in != NO_PIPE || asynchronous))) | |
582 | { | |
583 | pid_t paren_pid; | |
fd58d46e | 584 | int s; |
726f6388 JA |
585 | |
586 | /* Fork a subshell, turn off the subshell bit, turn off job | |
587 | control and call execute_command () on the command again. */ | |
c302751c | 588 | line_number_for_err_trap = line_number; |
726f6388 JA |
589 | paren_pid = make_child (savestring (make_command_string (command)), |
590 | asynchronous); | |
df0e4bfe CR |
591 | |
592 | if (user_subshell && signal_is_trapped (ERROR_TRAP) && | |
593 | signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0) | |
594 | { | |
595 | FREE (the_printed_command_except_trap); | |
596 | the_printed_command_except_trap = savestring (the_printed_command); | |
597 | } | |
598 | ||
726f6388 | 599 | if (paren_pid == 0) |
fd58d46e CR |
600 | { |
601 | /* We want to run the exit trap for forced {} subshells, and we | |
602 | want to note this before execute_in_subshell modifies the | |
603 | COMMAND struct. Need to keep in mind that execute_in_subshell | |
604 | runs the exit trap for () subshells itself. */ | |
605 | s = user_subshell == 0 && command->type == cm_group && pipe_in == NO_PIPE && pipe_out == NO_PIPE && asynchronous; | |
df0e4bfe | 606 | |
fd58d46e CR |
607 | last_command_exit_value = execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close); |
608 | if (s) | |
609 | subshell_exit (last_command_exit_value); | |
610 | else | |
611 | exit (last_command_exit_value); | |
612 | /* NOTREACHED */ | |
613 | } | |
726f6388 JA |
614 | else |
615 | { | |
616 | close_pipes (pipe_in, pipe_out); | |
617 | ||
618 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
f308cbc4 CR |
619 | if (variable_context == 0) /* wait until shell function completes */ |
620 | unlink_fifo_list (); | |
726f6388 JA |
621 | #endif |
622 | /* If we are part of a pipeline, and not the end of the pipeline, | |
623 | then we should simply return and let the last command in the | |
624 | pipe be waited for. If we are not in a pipeline, or are the | |
ccc6cda3 | 625 | last command in the pipeline, then we wait for the subshell |
726f6388 JA |
626 | and return its exit status as usual. */ |
627 | if (pipe_out != NO_PIPE) | |
628 | return (EXECUTION_SUCCESS); | |
629 | ||
630 | stop_pipeline (asynchronous, (COMMAND *)NULL); | |
631 | ||
ccc6cda3 | 632 | if (asynchronous == 0) |
726f6388 | 633 | { |
a05a1337 CR |
634 | was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0; |
635 | invert = (command->flags & CMD_INVERT_RETURN) != 0; | |
636 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; | |
637 | ||
54a1fa7c | 638 | exec_result = wait_for (paren_pid); |
726f6388 JA |
639 | |
640 | /* If we have to, invert the return value. */ | |
641 | if (invert) | |
54a1fa7c | 642 | exec_result = ((exec_result == EXECUTION_SUCCESS) |
bb70624e JA |
643 | ? EXECUTION_FAILURE |
644 | : EXECUTION_SUCCESS); | |
bb70624e | 645 | |
54a1fa7c | 646 | last_command_exit_value = exec_result; |
a05a1337 CR |
647 | if (user_subshell && was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS) |
648 | { | |
c302751c CR |
649 | save_line_number = line_number; |
650 | line_number = line_number_for_err_trap; | |
a05a1337 | 651 | run_error_trap (); |
c302751c | 652 | line_number = save_line_number; |
a05a1337 CR |
653 | } |
654 | ||
655 | if (user_subshell && ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS) | |
656 | { | |
a05a1337 CR |
657 | run_pending_traps (); |
658 | jump_to_top_level (ERREXIT); | |
659 | } | |
660 | ||
54a1fa7c | 661 | return (last_command_exit_value); |
726f6388 JA |
662 | } |
663 | else | |
664 | { | |
665 | DESCRIBE_PID (paren_pid); | |
666 | ||
667 | run_pending_traps (); | |
668 | ||
669 | return (EXECUTION_SUCCESS); | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
d166f048 JA |
674 | #if defined (COMMAND_TIMING) |
675 | if (command->flags & CMD_TIME_PIPELINE) | |
676 | { | |
677 | if (asynchronous) | |
678 | { | |
679 | command->flags |= CMD_FORCE_SUBSHELL; | |
680 | exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close); | |
681 | } | |
682 | else | |
683 | { | |
684 | exec_result = time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close); | |
d3a24ed2 | 685 | #if 0 |
d166f048 | 686 | if (running_trap == 0) |
d3a24ed2 | 687 | #endif |
d166f048 JA |
688 | currently_executing_command = (COMMAND *)NULL; |
689 | } | |
690 | return (exec_result); | |
691 | } | |
692 | #endif /* COMMAND_TIMING */ | |
693 | ||
694 | if (shell_control_structure (command->type) && command->redirects) | |
695 | stdin_redir = stdin_redirects (command->redirects); | |
696 | ||
122f603c CR |
697 | #if defined (PROCESS_SUBSTITUTION) |
698 | if (variable_context != 0) | |
699 | { | |
700 | ofifo = num_fifos (); | |
701 | ofifo_list = copy_fifo_list (&osize); | |
702 | saved_fifo = 1; | |
703 | } | |
704 | else | |
705 | saved_fifo = 0; | |
706 | #endif | |
707 | ||
726f6388 JA |
708 | /* Handle WHILE FOR CASE etc. with redirections. (Also '&' input |
709 | redirection.) */ | |
d3a24ed2 | 710 | if (do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE) != 0) |
726f6388 JA |
711 | { |
712 | cleanup_redirects (redirection_undo_list); | |
713 | redirection_undo_list = (REDIRECT *)NULL; | |
714 | dispose_exec_redirects (); | |
122f603c | 715 | #if defined (PROCESS_SUBSTITUTION) |
1f6ec1a8 CR |
716 | if (saved_fifo) |
717 | free (ofifo_list); | |
122f603c | 718 | #endif |
97c2aab2 | 719 | return (last_command_exit_value = EXECUTION_FAILURE); |
726f6388 JA |
720 | } |
721 | ||
722 | if (redirection_undo_list) | |
723 | { | |
510e20a2 | 724 | /* XXX - why copy here? */ |
726f6388 JA |
725 | my_undo_list = (REDIRECT *)copy_redirects (redirection_undo_list); |
726 | dispose_redirects (redirection_undo_list); | |
727 | redirection_undo_list = (REDIRECT *)NULL; | |
728 | } | |
729 | else | |
730 | my_undo_list = (REDIRECT *)NULL; | |
731 | ||
732 | if (exec_redirection_undo_list) | |
733 | { | |
510e20a2 | 734 | /* XXX - why copy here? */ |
726f6388 JA |
735 | exec_undo_list = (REDIRECT *)copy_redirects (exec_redirection_undo_list); |
736 | dispose_redirects (exec_redirection_undo_list); | |
737 | exec_redirection_undo_list = (REDIRECT *)NULL; | |
738 | } | |
739 | else | |
740 | exec_undo_list = (REDIRECT *)NULL; | |
741 | ||
742 | if (my_undo_list || exec_undo_list) | |
743 | begin_unwind_frame ("loop_redirections"); | |
744 | ||
745 | if (my_undo_list) | |
746 | add_unwind_protect ((Function *)cleanup_redirects, my_undo_list); | |
747 | ||
748 | if (exec_undo_list) | |
749 | add_unwind_protect ((Function *)dispose_redirects, exec_undo_list); | |
750 | ||
751 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; | |
752 | ||
753 | QUIT; | |
754 | ||
755 | switch (command->type) | |
756 | { | |
ccc6cda3 JA |
757 | case cm_simple: |
758 | { | |
d3a24ed2 CR |
759 | save_line_number = line_number; |
760 | /* We can't rely on variables retaining their values across a | |
ccc6cda3 JA |
761 | call to execute_simple_command if a longjmp occurs as the |
762 | result of a `return' builtin. This is true for sure with gcc. */ | |
cc87ba64 | 763 | #if defined (RECYCLES_PIDS) |
43df7bbb | 764 | last_made_pid = NO_PID; |
cc87ba64 CR |
765 | #endif |
766 | last_pid = last_made_pid; | |
f73dda09 | 767 | was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0; |
ccc6cda3 JA |
768 | |
769 | if (ignore_return && command->value.Simple) | |
770 | command->value.Simple->flags |= CMD_IGNORE_RETURN; | |
d166f048 JA |
771 | if (command->flags & CMD_STDIN_REDIR) |
772 | command->value.Simple->flags |= CMD_STDIN_REDIR; | |
d3a24ed2 | 773 | |
d3ad40de | 774 | line_number_for_err_trap = line_number = command->value.Simple->line; |
ccc6cda3 JA |
775 | exec_result = |
776 | execute_simple_command (command->value.Simple, pipe_in, pipe_out, | |
777 | asynchronous, fds_to_close); | |
d3a24ed2 | 778 | line_number = save_line_number; |
ccc6cda3 JA |
779 | |
780 | /* The temporary environment should be used for only the simple | |
781 | command immediately following its definition. */ | |
782 | dispose_used_env_vars (); | |
783 | ||
784 | #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA) | |
785 | /* Reclaim memory allocated with alloca () on machines which | |
786 | may be using the alloca emulation code. */ | |
787 | (void) alloca (0); | |
788 | #endif /* (ultrix && mips) || C_ALLOCA */ | |
789 | ||
790 | /* If we forked to do the command, then we must wait_for () | |
791 | the child. */ | |
792 | ||
793 | /* XXX - this is something to watch out for if there are problems | |
fd58d46e CR |
794 | when the shell is compiled without job control. Don't worry about |
795 | whether or not last_made_pid == last_pid; already_making_children | |
796 | tells us whether or not there are unwaited-for children to wait | |
797 | for and reap. */ | |
798 | if (already_making_children && pipe_out == NO_PIPE) | |
ccc6cda3 JA |
799 | { |
800 | stop_pipeline (asynchronous, (COMMAND *)NULL); | |
801 | ||
802 | if (asynchronous) | |
803 | { | |
804 | DESCRIBE_PID (last_made_pid); | |
805 | } | |
806 | else | |
807 | #if !defined (JOB_CONTROL) | |
808 | /* Do not wait for asynchronous processes started from | |
809 | startup files. */ | |
810 | if (last_made_pid != last_asynchronous_pid) | |
811 | #endif | |
812 | /* When executing a shell function that executes other | |
813 | commands, this causes the last simple command in | |
d3a24ed2 CR |
814 | the function to be waited for twice. This also causes |
815 | subshells forked to execute builtin commands (e.g., in | |
816 | pipelines) to be waited for twice. */ | |
ccc6cda3 JA |
817 | exec_result = wait_for (last_made_pid); |
818 | } | |
819 | } | |
820 | ||
a05a1337 CR |
821 | /* 2009/02/13 -- pipeline failure is processed elsewhere. This handles |
822 | only the failure of a simple command. */ | |
e77a3058 | 823 | if (was_error_trap && ignore_return == 0 && invert == 0 && pipe_in == NO_PIPE && pipe_out == NO_PIPE && exec_result != EXECUTION_SUCCESS) |
f73dda09 JA |
824 | { |
825 | last_command_exit_value = exec_result; | |
c302751c | 826 | line_number = line_number_for_err_trap; |
f73dda09 | 827 | run_error_trap (); |
c302751c | 828 | line_number = save_line_number; |
f73dda09 JA |
829 | } |
830 | ||
ccc6cda3 | 831 | if (ignore_return == 0 && invert == 0 && |
cce855bc | 832 | ((posixly_correct && interactive == 0 && special_builtin_failed) || |
e77a3058 | 833 | (exit_immediately_on_error && pipe_in == NO_PIPE && pipe_out == NO_PIPE && exec_result != EXECUTION_SUCCESS))) |
ccc6cda3 JA |
834 | { |
835 | last_command_exit_value = exec_result; | |
836 | run_pending_traps (); | |
d3a24ed2 | 837 | jump_to_top_level (ERREXIT); |
ccc6cda3 JA |
838 | } |
839 | ||
840 | break; | |
841 | ||
726f6388 JA |
842 | case cm_for: |
843 | if (ignore_return) | |
844 | command->value.For->flags |= CMD_IGNORE_RETURN; | |
845 | exec_result = execute_for_command (command->value.For); | |
846 | break; | |
847 | ||
bb70624e JA |
848 | #if defined (ARITH_FOR_COMMAND) |
849 | case cm_arith_for: | |
850 | if (ignore_return) | |
851 | command->value.ArithFor->flags |= CMD_IGNORE_RETURN; | |
852 | exec_result = execute_arith_for_command (command->value.ArithFor); | |
853 | break; | |
854 | #endif | |
855 | ||
726f6388 JA |
856 | #if defined (SELECT_COMMAND) |
857 | case cm_select: | |
858 | if (ignore_return) | |
859 | command->value.Select->flags |= CMD_IGNORE_RETURN; | |
860 | exec_result = execute_select_command (command->value.Select); | |
861 | break; | |
862 | #endif | |
863 | ||
864 | case cm_case: | |
865 | if (ignore_return) | |
866 | command->value.Case->flags |= CMD_IGNORE_RETURN; | |
867 | exec_result = execute_case_command (command->value.Case); | |
868 | break; | |
869 | ||
870 | case cm_while: | |
871 | if (ignore_return) | |
872 | command->value.While->flags |= CMD_IGNORE_RETURN; | |
873 | exec_result = execute_while_command (command->value.While); | |
874 | break; | |
875 | ||
876 | case cm_until: | |
877 | if (ignore_return) | |
878 | command->value.While->flags |= CMD_IGNORE_RETURN; | |
879 | exec_result = execute_until_command (command->value.While); | |
880 | break; | |
881 | ||
882 | case cm_if: | |
883 | if (ignore_return) | |
884 | command->value.If->flags |= CMD_IGNORE_RETURN; | |
885 | exec_result = execute_if_command (command->value.If); | |
886 | break; | |
887 | ||
888 | case cm_group: | |
889 | ||
890 | /* This code can be executed from either of two paths: an explicit | |
891 | '{}' command, or via a function call. If we are executed via a | |
892 | function call, we have already taken care of the function being | |
893 | executed in the background (down there in execute_simple_command ()), | |
894 | and this command should *not* be marked as asynchronous. If we | |
895 | are executing a regular '{}' group command, and asynchronous == 1, | |
896 | we must want to execute the whole command in the background, so we | |
897 | need a subshell, and we want the stuff executed in that subshell | |
898 | (this group command) to be executed in the foreground of that | |
899 | subshell (i.e. there will not be *another* subshell forked). | |
900 | ||
901 | What we do is to force a subshell if asynchronous, and then call | |
902 | execute_command_internal again with asynchronous still set to 1, | |
903 | but with the original group command, so the printed command will | |
904 | look right. | |
905 | ||
906 | The code above that handles forking off subshells will note that | |
907 | both subshell and async are on, and turn off async in the child | |
908 | after forking the subshell (but leave async set in the parent, so | |
909 | the normal call to describe_pid is made). This turning off | |
910 | async is *crucial*; if it is not done, this will fall into an | |
911 | infinite loop of executions through this spot in subshell after | |
912 | subshell until the process limit is exhausted. */ | |
913 | ||
914 | if (asynchronous) | |
915 | { | |
916 | command->flags |= CMD_FORCE_SUBSHELL; | |
917 | exec_result = | |
918 | execute_command_internal (command, 1, pipe_in, pipe_out, | |
919 | fds_to_close); | |
920 | } | |
921 | else | |
922 | { | |
923 | if (ignore_return && command->value.Group->command) | |
924 | command->value.Group->command->flags |= CMD_IGNORE_RETURN; | |
925 | exec_result = | |
926 | execute_command_internal (command->value.Group->command, | |
927 | asynchronous, pipe_in, pipe_out, | |
928 | fds_to_close); | |
929 | } | |
930 | break; | |
931 | ||
ccc6cda3 JA |
932 | case cm_connection: |
933 | exec_result = execute_connection (command, asynchronous, | |
934 | pipe_in, pipe_out, fds_to_close); | |
935 | break; | |
726f6388 | 936 | |
cce855bc JA |
937 | #if defined (DPAREN_ARITHMETIC) |
938 | case cm_arith: | |
691aebcb | 939 | was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0; |
cce855bc JA |
940 | if (ignore_return) |
941 | command->value.Arith->flags |= CMD_IGNORE_RETURN; | |
691aebcb | 942 | line_number_for_err_trap = save_line_number = line_number; |
cce855bc | 943 | exec_result = execute_arith_command (command->value.Arith); |
691aebcb CR |
944 | line_number = save_line_number; |
945 | ||
946 | if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS) | |
947 | { | |
948 | last_command_exit_value = exec_result; | |
949 | save_line_number = line_number; | |
950 | line_number = line_number_for_err_trap; | |
951 | run_error_trap (); | |
952 | line_number = save_line_number; | |
953 | } | |
954 | ||
955 | if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS) | |
956 | { | |
957 | last_command_exit_value = exec_result; | |
958 | run_pending_traps (); | |
959 | jump_to_top_level (ERREXIT); | |
960 | } | |
961 | ||
cce855bc JA |
962 | break; |
963 | #endif | |
964 | ||
965 | #if defined (COND_COMMAND) | |
966 | case cm_cond: | |
3eb2d94a | 967 | was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0; |
cce855bc JA |
968 | if (ignore_return) |
969 | command->value.Cond->flags |= CMD_IGNORE_RETURN; | |
3eb2d94a CR |
970 | |
971 | line_number_for_err_trap = save_line_number = line_number; | |
cce855bc | 972 | exec_result = execute_cond_command (command->value.Cond); |
d3a24ed2 | 973 | line_number = save_line_number; |
3eb2d94a CR |
974 | |
975 | if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS) | |
976 | { | |
977 | last_command_exit_value = exec_result; | |
978 | save_line_number = line_number; | |
979 | line_number = line_number_for_err_trap; | |
980 | run_error_trap (); | |
981 | line_number = save_line_number; | |
982 | } | |
983 | ||
984 | if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS) | |
985 | { | |
986 | last_command_exit_value = exec_result; | |
987 | run_pending_traps (); | |
988 | jump_to_top_level (ERREXIT); | |
989 | } | |
990 | ||
cce855bc JA |
991 | break; |
992 | #endif | |
993 | ||
ccc6cda3 JA |
994 | case cm_function_def: |
995 | exec_result = execute_intern_function (command->value.Function_def->name, | |
ba4ab055 | 996 | command->value.Function_def); |
ccc6cda3 | 997 | break; |
726f6388 | 998 | |
ccc6cda3 | 999 | default: |
b72432fd | 1000 | command_error ("execute_command", CMDERR_BADTYPE, command->type, 0); |
ccc6cda3 | 1001 | } |
726f6388 | 1002 | |
ccc6cda3 JA |
1003 | if (my_undo_list) |
1004 | { | |
d3a24ed2 | 1005 | do_redirections (my_undo_list, RX_ACTIVE); |
ccc6cda3 JA |
1006 | dispose_redirects (my_undo_list); |
1007 | } | |
726f6388 | 1008 | |
ccc6cda3 JA |
1009 | if (exec_undo_list) |
1010 | dispose_redirects (exec_undo_list); | |
726f6388 | 1011 | |
ccc6cda3 JA |
1012 | if (my_undo_list || exec_undo_list) |
1013 | discard_unwind_frame ("loop_redirections"); | |
726f6388 | 1014 | |
122f603c CR |
1015 | #if defined (PROCESS_SUBSTITUTION) |
1016 | if (saved_fifo) | |
1017 | { | |
1018 | nfifo = num_fifos (); | |
1019 | if (nfifo > ofifo) | |
1020 | close_new_fifos (ofifo_list, osize); | |
1021 | free (ofifo_list); | |
1022 | } | |
1023 | #endif | |
1024 | ||
ccc6cda3 JA |
1025 | /* Invert the return value if we have to */ |
1026 | if (invert) | |
1027 | exec_result = (exec_result == EXECUTION_SUCCESS) | |
1028 | ? EXECUTION_FAILURE | |
1029 | : EXECUTION_SUCCESS; | |
726f6388 | 1030 | |
af12dacd CR |
1031 | #if defined (DPAREN_ARITHMETIC) || defined (COND_COMMAND) |
1032 | /* This is where we set PIPESTATUS from the exit status of the appropriate | |
1033 | compound commands (the ones that look enough like simple commands to | |
1034 | cause confusion). We might be able to optimize by not doing this if | |
1035 | subshell_environment != 0. */ | |
1036 | switch (command->type) | |
1037 | { | |
1038 | # if defined (DPAREN_ARITHMETIC) | |
1039 | case cm_arith: | |
1040 | # endif | |
1041 | # if defined (COND_COMMAND) | |
1042 | case cm_cond: | |
1043 | # endif | |
1044 | set_pipestatus_from_exit (exec_result); | |
1045 | break; | |
1046 | } | |
1047 | #endif | |
1048 | ||
ccc6cda3 JA |
1049 | last_command_exit_value = exec_result; |
1050 | run_pending_traps (); | |
d3a24ed2 | 1051 | #if 0 |
ccc6cda3 | 1052 | if (running_trap == 0) |
d3a24ed2 | 1053 | #endif |
ccc6cda3 | 1054 | currently_executing_command = (COMMAND *)NULL; |
122f603c | 1055 | |
ccc6cda3 JA |
1056 | return (last_command_exit_value); |
1057 | } | |
726f6388 | 1058 | |
ccc6cda3 | 1059 | #if defined (COMMAND_TIMING) |
726f6388 | 1060 | |
bb70624e | 1061 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) |
f73dda09 JA |
1062 | extern struct timeval *difftimeval __P((struct timeval *, struct timeval *, struct timeval *)); |
1063 | extern struct timeval *addtimeval __P((struct timeval *, struct timeval *, struct timeval *)); | |
1064 | extern int timeval_to_cpu __P((struct timeval *, struct timeval *, struct timeval *)); | |
bb70624e | 1065 | #endif |
726f6388 | 1066 | |
ccc6cda3 JA |
1067 | #define POSIX_TIMEFORMAT "real %2R\nuser %2U\nsys %2S" |
1068 | #define BASH_TIMEFORMAT "\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS" | |
726f6388 | 1069 | |
d3ad40de | 1070 | static const int precs[] = { 0, 100, 10, 1 }; |
726f6388 | 1071 | |
ccc6cda3 JA |
1072 | /* Expand one `%'-prefixed escape sequence from a time format string. */ |
1073 | static int | |
1074 | mkfmt (buf, prec, lng, sec, sec_fraction) | |
1075 | char *buf; | |
1076 | int prec, lng; | |
f73dda09 | 1077 | time_t sec; |
ccc6cda3 JA |
1078 | int sec_fraction; |
1079 | { | |
f73dda09 JA |
1080 | time_t min; |
1081 | char abuf[INT_STRLEN_BOUND(time_t) + 1]; | |
ccc6cda3 | 1082 | int ind, aind; |
726f6388 | 1083 | |
ccc6cda3 | 1084 | ind = 0; |
f73dda09 | 1085 | abuf[sizeof(abuf) - 1] = '\0'; |
726f6388 | 1086 | |
ccc6cda3 JA |
1087 | /* If LNG is non-zero, we want to decompose SEC into minutes and seconds. */ |
1088 | if (lng) | |
1089 | { | |
1090 | min = sec / 60; | |
1091 | sec %= 60; | |
f73dda09 | 1092 | aind = sizeof(abuf) - 2; |
ccc6cda3 JA |
1093 | do |
1094 | abuf[aind--] = (min % 10) + '0'; | |
1095 | while (min /= 10); | |
1096 | aind++; | |
1097 | while (abuf[aind]) | |
cce855bc | 1098 | buf[ind++] = abuf[aind++]; |
ccc6cda3 JA |
1099 | buf[ind++] = 'm'; |
1100 | } | |
726f6388 | 1101 | |
ccc6cda3 | 1102 | /* Now add the seconds. */ |
f73dda09 | 1103 | aind = sizeof (abuf) - 2; |
ccc6cda3 JA |
1104 | do |
1105 | abuf[aind--] = (sec % 10) + '0'; | |
1106 | while (sec /= 10); | |
1107 | aind++; | |
1108 | while (abuf[aind]) | |
1109 | buf[ind++] = abuf[aind++]; | |
1110 | ||
1111 | /* We want to add a decimal point and PREC places after it if PREC is | |
1112 | nonzero. PREC is not greater than 3. SEC_FRACTION is between 0 | |
1113 | and 999. */ | |
1114 | if (prec != 0) | |
1115 | { | |
1116 | buf[ind++] = '.'; | |
1117 | for (aind = 1; aind <= prec; aind++) | |
1118 | { | |
1119 | buf[ind++] = (sec_fraction / precs[aind]) + '0'; | |
1120 | sec_fraction %= precs[aind]; | |
1121 | } | |
1122 | } | |
726f6388 | 1123 | |
ccc6cda3 JA |
1124 | if (lng) |
1125 | buf[ind++] = 's'; | |
1126 | buf[ind] = '\0'; | |
726f6388 | 1127 | |
ccc6cda3 JA |
1128 | return (ind); |
1129 | } | |
726f6388 | 1130 | |
ccc6cda3 JA |
1131 | /* Interpret the format string FORMAT, interpolating the following escape |
1132 | sequences: | |
7117c2d2 | 1133 | %[prec][l][RUS] |
ccc6cda3 JA |
1134 | |
1135 | where the optional `prec' is a precision, meaning the number of | |
1136 | characters after the decimal point, the optional `l' means to format | |
1137 | using minutes and seconds (MMmNN[.FF]s), like the `times' builtin', | |
1138 | and the last character is one of | |
1139 | ||
7117c2d2 JA |
1140 | R number of seconds of `real' time |
1141 | U number of seconds of `user' time | |
1142 | S number of seconds of `system' time | |
ccc6cda3 JA |
1143 | |
1144 | An occurrence of `%%' in the format string is translated to a `%'. The | |
1145 | result is printed to FP, a pointer to a FILE. The other variables are | |
1146 | the seconds and thousandths of a second of real, user, and system time, | |
1147 | resectively. */ | |
1148 | static void | |
1149 | print_formatted_time (fp, format, rs, rsf, us, usf, ss, ssf, cpu) | |
1150 | FILE *fp; | |
1151 | char *format; | |
f73dda09 JA |
1152 | time_t rs; |
1153 | int rsf; | |
1154 | time_t us; | |
1155 | int usf; | |
1156 | time_t ss; | |
1157 | int ssf, cpu; | |
ccc6cda3 JA |
1158 | { |
1159 | int prec, lng, len; | |
f73dda09 JA |
1160 | char *str, *s, ts[INT_STRLEN_BOUND (time_t) + sizeof ("mSS.FFFF")]; |
1161 | time_t sum; | |
e8ce775d | 1162 | int sum_frac; |
ccc6cda3 | 1163 | int sindex, ssize; |
726f6388 | 1164 | |
ccc6cda3 JA |
1165 | len = strlen (format); |
1166 | ssize = (len + 64) - (len % 64); | |
f73dda09 | 1167 | str = (char *)xmalloc (ssize); |
ccc6cda3 JA |
1168 | sindex = 0; |
1169 | ||
1170 | for (s = format; *s; s++) | |
1171 | { | |
1172 | if (*s != '%' || s[1] == '\0') | |
cce855bc JA |
1173 | { |
1174 | RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64); | |
1175 | str[sindex++] = *s; | |
1176 | } | |
ccc6cda3 | 1177 | else if (s[1] == '%') |
cce855bc JA |
1178 | { |
1179 | s++; | |
1180 | RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64); | |
1181 | str[sindex++] = *s; | |
1182 | } | |
ccc6cda3 JA |
1183 | else if (s[1] == 'P') |
1184 | { | |
1185 | s++; | |
c1d39fb8 CR |
1186 | #if 0 |
1187 | /* clamp CPU usage at 100% */ | |
ccc6cda3 JA |
1188 | if (cpu > 10000) |
1189 | cpu = 10000; | |
c1d39fb8 | 1190 | #endif |
ccc6cda3 JA |
1191 | sum = cpu / 100; |
1192 | sum_frac = (cpu % 100) * 10; | |
1193 | len = mkfmt (ts, 2, 0, sum, sum_frac); | |
1194 | RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64); | |
1195 | strcpy (str + sindex, ts); | |
1196 | sindex += len; | |
1197 | } | |
1198 | else | |
1199 | { | |
1200 | prec = 3; /* default is three places past the decimal point. */ | |
1201 | lng = 0; /* default is to not use minutes or append `s' */ | |
1202 | s++; | |
f73dda09 | 1203 | if (DIGIT (*s)) /* `precision' */ |
726f6388 | 1204 | { |
ccc6cda3 JA |
1205 | prec = *s++ - '0'; |
1206 | if (prec > 3) prec = 3; | |
726f6388 | 1207 | } |
ccc6cda3 JA |
1208 | if (*s == 'l') /* `length extender' */ |
1209 | { | |
1210 | lng = 1; | |
1211 | s++; | |
1212 | } | |
1213 | if (*s == 'R' || *s == 'E') | |
1214 | len = mkfmt (ts, prec, lng, rs, rsf); | |
1215 | else if (*s == 'U') | |
1216 | len = mkfmt (ts, prec, lng, us, usf); | |
1217 | else if (*s == 'S') | |
1218 | len = mkfmt (ts, prec, lng, ss, ssf); | |
1219 | else | |
1220 | { | |
5e13499c | 1221 | internal_error (_("TIMEFORMAT: `%c': invalid format character"), *s); |
ccc6cda3 JA |
1222 | free (str); |
1223 | return; | |
1224 | } | |
1225 | RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64); | |
1226 | strcpy (str + sindex, ts); | |
1227 | sindex += len; | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | str[sindex] = '\0'; | |
1232 | fprintf (fp, "%s\n", str); | |
1233 | fflush (fp); | |
1234 | ||
1235 | free (str); | |
1236 | } | |
1237 | ||
1238 | static int | |
1239 | time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
1240 | COMMAND *command; | |
1241 | int asynchronous, pipe_in, pipe_out; | |
1242 | struct fd_bitmap *fds_to_close; | |
1243 | { | |
64419627 | 1244 | int rv, posix_time, old_flags, nullcmd; |
f73dda09 | 1245 | time_t rs, us, ss; |
ccc6cda3 JA |
1246 | int rsf, usf, ssf; |
1247 | int cpu; | |
1248 | char *time_format; | |
1249 | ||
1250 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) | |
1251 | struct timeval real, user, sys; | |
1252 | struct timeval before, after; | |
d3a24ed2 CR |
1253 | # if defined (HAVE_STRUCT_TIMEZONE) |
1254 | struct timezone dtz; /* posix doesn't define this */ | |
1255 | # endif | |
ccc6cda3 JA |
1256 | struct rusage selfb, selfa, kidsb, kidsa; /* a = after, b = before */ |
1257 | #else | |
1258 | # if defined (HAVE_TIMES) | |
1259 | clock_t tbefore, tafter, real, user, sys; | |
1260 | struct tms before, after; | |
1261 | # endif | |
1262 | #endif | |
1263 | ||
1264 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) | |
d3a24ed2 | 1265 | # if defined (HAVE_STRUCT_TIMEZONE) |
ccc6cda3 | 1266 | gettimeofday (&before, &dtz); |
d3a24ed2 CR |
1267 | # else |
1268 | gettimeofday (&before, (void *)NULL); | |
1269 | # endif /* !HAVE_STRUCT_TIMEZONE */ | |
ccc6cda3 JA |
1270 | getrusage (RUSAGE_SELF, &selfb); |
1271 | getrusage (RUSAGE_CHILDREN, &kidsb); | |
1272 | #else | |
1273 | # if defined (HAVE_TIMES) | |
1274 | tbefore = times (&before); | |
1275 | # endif | |
1276 | #endif | |
1277 | ||
631b20c6 | 1278 | posix_time = command && (command->flags & CMD_TIME_POSIX); |
ccc6cda3 | 1279 | |
64419627 CR |
1280 | nullcmd = (command == 0) || (command->type == cm_simple && command->value.Simple->words == 0 && command->value.Simple->redirects == 0); |
1281 | if (posixly_correct && nullcmd) | |
1282 | { | |
1283 | #if defined (HAVE_GETRUSAGE) | |
1284 | selfb.ru_utime.tv_sec = kidsb.ru_utime.tv_sec = selfb.ru_stime.tv_sec = kidsb.ru_stime.tv_sec = 0; | |
1285 | selfb.ru_utime.tv_usec = kidsb.ru_utime.tv_usec = selfb.ru_stime.tv_usec = kidsb.ru_stime.tv_usec = 0; | |
1286 | before.tv_sec = shell_start_time; | |
1287 | before.tv_usec = 0; | |
1288 | #else | |
1289 | before.tms_utime = before.tms_stime = before.tms_cutime = before.tms_cstime = 0; | |
1290 | tbefore = shell_start_time; | |
1291 | #endif | |
1292 | } | |
64419627 | 1293 | |
d166f048 | 1294 | old_flags = command->flags; |
ccc6cda3 JA |
1295 | command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX); |
1296 | rv = execute_command_internal (command, asynchronous, pipe_in, pipe_out, fds_to_close); | |
d166f048 | 1297 | command->flags = old_flags; |
ccc6cda3 | 1298 | |
f73dda09 | 1299 | rs = us = ss = 0; |
cce855bc JA |
1300 | rsf = usf = ssf = cpu = 0; |
1301 | ||
ccc6cda3 | 1302 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) |
d3a24ed2 | 1303 | # if defined (HAVE_STRUCT_TIMEZONE) |
ccc6cda3 | 1304 | gettimeofday (&after, &dtz); |
d3a24ed2 CR |
1305 | # else |
1306 | gettimeofday (&after, (void *)NULL); | |
1307 | # endif /* !HAVE_STRUCT_TIMEZONE */ | |
ccc6cda3 JA |
1308 | getrusage (RUSAGE_SELF, &selfa); |
1309 | getrusage (RUSAGE_CHILDREN, &kidsa); | |
1310 | ||
1311 | difftimeval (&real, &before, &after); | |
1312 | timeval_to_secs (&real, &rs, &rsf); | |
1313 | ||
1314 | addtimeval (&user, difftimeval(&after, &selfb.ru_utime, &selfa.ru_utime), | |
1315 | difftimeval(&before, &kidsb.ru_utime, &kidsa.ru_utime)); | |
1316 | timeval_to_secs (&user, &us, &usf); | |
1317 | ||
1318 | addtimeval (&sys, difftimeval(&after, &selfb.ru_stime, &selfa.ru_stime), | |
1319 | difftimeval(&before, &kidsb.ru_stime, &kidsa.ru_stime)); | |
1320 | timeval_to_secs (&sys, &ss, &ssf); | |
1321 | ||
1322 | cpu = timeval_to_cpu (&real, &user, &sys); | |
1323 | #else | |
1324 | # if defined (HAVE_TIMES) | |
1325 | tafter = times (&after); | |
1326 | ||
1327 | real = tafter - tbefore; | |
1328 | clock_t_to_secs (real, &rs, &rsf); | |
1329 | ||
1330 | user = (after.tms_utime - before.tms_utime) + (after.tms_cutime - before.tms_cutime); | |
1331 | clock_t_to_secs (user, &us, &usf); | |
1332 | ||
1333 | sys = (after.tms_stime - before.tms_stime) + (after.tms_cstime - before.tms_cstime); | |
1334 | clock_t_to_secs (sys, &ss, &ssf); | |
1335 | ||
d166f048 | 1336 | cpu = (real == 0) ? 0 : ((user + sys) * 10000) / real; |
ccc6cda3 JA |
1337 | |
1338 | # else | |
f73dda09 | 1339 | rs = us = ss = 0; |
ccc6cda3 JA |
1340 | rsf = usf = ssf = cpu = 0; |
1341 | # endif | |
1342 | #endif | |
1343 | ||
1344 | if (posix_time) | |
1345 | time_format = POSIX_TIMEFORMAT; | |
1346 | else if ((time_format = get_string_value ("TIMEFORMAT")) == 0) | |
64419627 | 1347 | { |
64419627 CR |
1348 | if (posixly_correct && nullcmd) |
1349 | time_format = "user\t%2lU\nsys\t%2lS"; | |
1350 | else | |
64419627 CR |
1351 | time_format = BASH_TIMEFORMAT; |
1352 | } | |
ccc6cda3 JA |
1353 | if (time_format && *time_format) |
1354 | print_formatted_time (stderr, time_format, rs, rsf, us, usf, ss, ssf, cpu); | |
1355 | ||
1356 | return rv; | |
1357 | } | |
1358 | #endif /* COMMAND_TIMING */ | |
1359 | ||
bb70624e | 1360 | /* Execute a command that's supposed to be in a subshell. This must be |
28ef6c31 JA |
1361 | called after make_child and we must be running in the child process. |
1362 | The caller will return or exit() immediately with the value this returns. */ | |
bb70624e JA |
1363 | static int |
1364 | execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
1365 | COMMAND *command; | |
1366 | int asynchronous; | |
1367 | int pipe_in, pipe_out; | |
1368 | struct fd_bitmap *fds_to_close; | |
1369 | { | |
28ef6c31 | 1370 | int user_subshell, return_code, function_value, should_redir_stdin, invert; |
09767ff0 | 1371 | int ois, user_coproc; |
e1e48bba | 1372 | int result; |
e3af9370 | 1373 | volatile COMMAND *tcom; |
bb70624e | 1374 | |
f73dda09 | 1375 | USE_VAR(user_subshell); |
09767ff0 | 1376 | USE_VAR(user_coproc); |
f73dda09 JA |
1377 | USE_VAR(invert); |
1378 | USE_VAR(tcom); | |
1379 | USE_VAR(asynchronous); | |
1380 | ||
d3a24ed2 | 1381 | subshell_level++; |
bb70624e JA |
1382 | should_redir_stdin = (asynchronous && (command->flags & CMD_STDIN_REDIR) && |
1383 | pipe_in == NO_PIPE && | |
1384 | stdin_redirects (command->redirects) == 0); | |
1385 | ||
28ef6c31 | 1386 | invert = (command->flags & CMD_INVERT_RETURN) != 0; |
bb70624e | 1387 | user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0); |
09767ff0 | 1388 | user_coproc = command->type == cm_coproc; |
28ef6c31 | 1389 | |
bb70624e JA |
1390 | command->flags &= ~(CMD_FORCE_SUBSHELL | CMD_WANT_SUBSHELL | CMD_INVERT_RETURN); |
1391 | ||
1392 | /* If a command is asynchronous in a subshell (like ( foo ) & or | |
1393 | the special case of an asynchronous GROUP command where the | |
1394 | the subshell bit is turned on down in case cm_group: below), | |
1395 | turn off `asynchronous', so that two subshells aren't spawned. | |
d3ad40de CR |
1396 | XXX - asynchronous used to be set to 0 in this block, but that |
1397 | means that setup_async_signals was never run. Now it's set to | |
1398 | 0 after subshell_environment is set appropriately and setup_async_signals | |
1399 | is run. | |
bb70624e JA |
1400 | |
1401 | This seems semantically correct to me. For example, | |
1402 | ( foo ) & seems to say ``do the command `foo' in a subshell | |
1403 | environment, but don't wait for that subshell to finish'', | |
1404 | and "{ foo ; bar ; } &" seems to me to be like functions or | |
1405 | builtins in the background, which executed in a subshell | |
1406 | environment. I just don't see the need to fork two subshells. */ | |
1407 | ||
1408 | /* Don't fork again, we are already in a subshell. A `doubly | |
1409 | async' shell is not interactive, however. */ | |
1410 | if (asynchronous) | |
1411 | { | |
1412 | #if defined (JOB_CONTROL) | |
1413 | /* If a construct like ( exec xxx yyy ) & is given while job | |
1414 | control is active, we want to prevent exec from putting the | |
1415 | subshell back into the original process group, carefully | |
1416 | undoing all the work we just did in make_child. */ | |
1417 | original_pgrp = -1; | |
1418 | #endif /* JOB_CONTROL */ | |
28ef6c31 | 1419 | ois = interactive_shell; |
bb70624e | 1420 | interactive_shell = 0; |
28ef6c31 JA |
1421 | /* This test is to prevent alias expansion by interactive shells that |
1422 | run `(command) &' but to allow scripts that have enabled alias | |
1423 | expansion with `shopt -s expand_alias' to continue to expand | |
1424 | aliases. */ | |
1425 | if (ois != interactive_shell) | |
1426 | expand_aliases = 0; | |
bb70624e JA |
1427 | } |
1428 | ||
1429 | /* Subshells are neither login nor interactive. */ | |
1430 | login_shell = interactive = 0; | |
1431 | ||
d3ad40de CR |
1432 | if (user_subshell) |
1433 | subshell_environment = SUBSHELL_PAREN; | |
1434 | else | |
1435 | { | |
1436 | subshell_environment = 0; /* XXX */ | |
1437 | if (asynchronous) | |
1438 | subshell_environment |= SUBSHELL_ASYNC; | |
1439 | if (pipe_in != NO_PIPE || pipe_out != NO_PIPE) | |
1440 | subshell_environment |= SUBSHELL_PIPE; | |
09767ff0 CR |
1441 | if (user_coproc) |
1442 | subshell_environment |= SUBSHELL_COPROC; | |
d3ad40de | 1443 | } |
bb70624e JA |
1444 | |
1445 | reset_terminating_signals (); /* in sig.c */ | |
1446 | /* Cancel traps, in trap.c. */ | |
d1fab3dc CR |
1447 | /* Reset the signal handlers in the child, but don't free the |
1448 | trap strings. Set a flag noting that we have to free the | |
1449 | trap strings if we run trap to change a signal disposition. */ | |
1450 | reset_signal_handlers (); | |
1451 | subshell_environment |= SUBSHELL_RESETTRAP; | |
d3ad40de CR |
1452 | |
1453 | /* Make sure restore_original_signals doesn't undo the work done by | |
1454 | make_child to ensure that asynchronous children are immune to SIGINT | |
1455 | and SIGQUIT. Turn off asynchronous to make sure more subshells are | |
1456 | not spawned. */ | |
bb70624e | 1457 | if (asynchronous) |
d3ad40de CR |
1458 | { |
1459 | setup_async_signals (); | |
1460 | asynchronous = 0; | |
1461 | } | |
bb70624e JA |
1462 | |
1463 | #if defined (JOB_CONTROL) | |
1464 | set_sigchld_handler (); | |
1465 | #endif /* JOB_CONTROL */ | |
1466 | ||
1467 | set_sigint_handler (); | |
1468 | ||
1469 | #if defined (JOB_CONTROL) | |
1470 | /* Delete all traces that there were any jobs running. This is | |
1471 | only for subshells. */ | |
1472 | without_job_control (); | |
1473 | #endif /* JOB_CONTROL */ | |
1474 | ||
1475 | if (fds_to_close) | |
1476 | close_fd_bitmap (fds_to_close); | |
1477 | ||
1478 | do_piping (pipe_in, pipe_out); | |
1479 | ||
09767ff0 | 1480 | #if defined (COPROCESS_SUPPORT) |
8e1a6eaa | 1481 | coproc_closeall (); |
09767ff0 CR |
1482 | #endif |
1483 | ||
bb70624e JA |
1484 | /* If this is a user subshell, set a flag if stdin was redirected. |
1485 | This is used later to decide whether to redirect fd 0 to | |
1486 | /dev/null for async commands in the subshell. This adds more | |
1487 | sh compatibility, but I'm not sure it's the right thing to do. */ | |
1488 | if (user_subshell) | |
1489 | { | |
1490 | stdin_redir = stdin_redirects (command->redirects); | |
1491 | restore_default_signal (0); | |
1492 | } | |
1493 | ||
1494 | /* If this is an asynchronous command (command &), we want to | |
1495 | redirect the standard input from /dev/null in the absence of | |
1496 | any specific redirection involving stdin. */ | |
1497 | if (should_redir_stdin && stdin_redir == 0) | |
1498 | async_redirect_stdin (); | |
1499 | ||
1500 | /* Do redirections, then dispose of them before recursive call. */ | |
1501 | if (command->redirects) | |
1502 | { | |
d3a24ed2 | 1503 | if (do_redirections (command->redirects, RX_ACTIVE) != 0) |
28ef6c31 | 1504 | exit (invert ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
bb70624e JA |
1505 | |
1506 | dispose_redirects (command->redirects); | |
1507 | command->redirects = (REDIRECT *)NULL; | |
1508 | } | |
1509 | ||
09767ff0 CR |
1510 | if (command->type == cm_subshell) |
1511 | tcom = command->value.Subshell->command; | |
1512 | else if (user_coproc) | |
1513 | tcom = command->value.Coproc->command; | |
1514 | else | |
1515 | tcom = command; | |
bb70624e | 1516 | |
301e2142 CR |
1517 | if (command->flags & CMD_TIME_PIPELINE) |
1518 | tcom->flags |= CMD_TIME_PIPELINE; | |
1519 | if (command->flags & CMD_TIME_POSIX) | |
1520 | tcom->flags |= CMD_TIME_POSIX; | |
1521 | ||
f73dda09 JA |
1522 | /* Make sure the subshell inherits any CMD_IGNORE_RETURN flag. */ |
1523 | if ((command->flags & CMD_IGNORE_RETURN) && tcom != command) | |
1524 | tcom->flags |= CMD_IGNORE_RETURN; | |
1525 | ||
bb70624e JA |
1526 | /* If this is a simple command, tell execute_disk_command that it |
1527 | might be able to get away without forking and simply exec. | |
1528 | This means things like ( sleep 10 ) will only cause one fork. | |
28ef6c31 JA |
1529 | If we're timing the command or inverting its return value, however, |
1530 | we cannot do this optimization. */ | |
09767ff0 | 1531 | if ((user_subshell || user_coproc) && (tcom->type == cm_simple || tcom->type == cm_subshell) && |
28ef6c31 JA |
1532 | ((tcom->flags & CMD_TIME_PIPELINE) == 0) && |
1533 | ((tcom->flags & CMD_INVERT_RETURN) == 0)) | |
bb70624e JA |
1534 | { |
1535 | tcom->flags |= CMD_NO_FORK; | |
1536 | if (tcom->type == cm_simple) | |
1537 | tcom->value.Simple->flags |= CMD_NO_FORK; | |
1538 | } | |
1539 | ||
28ef6c31 JA |
1540 | invert = (tcom->flags & CMD_INVERT_RETURN) != 0; |
1541 | tcom->flags &= ~CMD_INVERT_RETURN; | |
1542 | ||
e1e48bba CR |
1543 | result = setjmp (top_level); |
1544 | ||
bb70624e JA |
1545 | /* If we're inside a function while executing this subshell, we |
1546 | need to handle a possible `return'. */ | |
1547 | function_value = 0; | |
1548 | if (return_catch_flag) | |
1549 | function_value = setjmp (return_catch); | |
1550 | ||
e1e48bba CR |
1551 | /* If we're going to exit the shell, we don't want to invert the return |
1552 | status. */ | |
1553 | if (result == EXITPROG) | |
1554 | invert = 0, return_code = last_command_exit_value; | |
1555 | else if (result) | |
1556 | return_code = EXECUTION_FAILURE; | |
1557 | else if (function_value) | |
bb70624e JA |
1558 | return_code = return_catch_value; |
1559 | else | |
9c2db999 | 1560 | return_code = execute_command_internal ((COMMAND *)tcom, asynchronous, NO_PIPE, NO_PIPE, fds_to_close); |
bb70624e | 1561 | |
28ef6c31 JA |
1562 | /* If we are asked to, invert the return value. */ |
1563 | if (invert) | |
1564 | return_code = (return_code == EXECUTION_SUCCESS) ? EXECUTION_FAILURE | |
1565 | : EXECUTION_SUCCESS; | |
1566 | ||
bb70624e JA |
1567 | /* If we were explicitly placed in a subshell with (), we need |
1568 | to do the `shell cleanup' things, such as running traps[0]. */ | |
1569 | if (user_subshell && signal_is_trapped (0)) | |
1570 | { | |
1571 | last_command_exit_value = return_code; | |
1572 | return_code = run_exit_trap (); | |
1573 | } | |
1574 | ||
d3a24ed2 | 1575 | subshell_level--; |
bb70624e JA |
1576 | return (return_code); |
1577 | /* NOTREACHED */ | |
1578 | } | |
1579 | ||
09767ff0 | 1580 | #if defined (COPROCESS_SUPPORT) |
8e1a6eaa CR |
1581 | #define COPROC_MAX 16 |
1582 | ||
1583 | typedef struct cpelement | |
1584 | { | |
1585 | struct cpelement *next; | |
1586 | struct coproc *coproc; | |
1587 | } | |
1588 | cpelement_t; | |
1589 | ||
1590 | typedef struct cplist | |
1591 | { | |
1592 | struct cpelement *head; | |
1593 | struct cpelement *tail; | |
1594 | int ncoproc; | |
1595 | } | |
1596 | cplist_t; | |
1597 | ||
1598 | static struct cpelement *cpe_alloc __P((struct coproc *)); | |
1599 | static void cpe_dispose __P((struct cpelement *)); | |
1600 | static struct cpelement *cpl_add __P((struct coproc *)); | |
1601 | static struct cpelement *cpl_delete __P((pid_t)); | |
e141c35a | 1602 | static void cpl_reap __P((void)); |
8e1a6eaa | 1603 | static void cpl_flush __P((void)); |
f6da9f85 | 1604 | static void cpl_closeall __P((void)); |
8e1a6eaa | 1605 | static struct cpelement *cpl_search __P((pid_t)); |
f6da9f85 | 1606 | static struct cpelement *cpl_searchbyname __P((const char *)); |
8e1a6eaa CR |
1607 | static void cpl_prune __P((void)); |
1608 | ||
f6da9f85 CR |
1609 | static void coproc_free __P((struct coproc *)); |
1610 | ||
1611 | /* Will go away when there is fully-implemented support for multiple coprocs. */ | |
3d35553a | 1612 | Coproc sh_coproc = { 0, NO_PID, -1, -1, 0, 0, 0, 0 }; |
09767ff0 | 1613 | |
8e1a6eaa CR |
1614 | cplist_t coproc_list = {0, 0, 0}; |
1615 | ||
e141c35a | 1616 | /* Functions to manage the list of coprocs */ |
8e1a6eaa CR |
1617 | |
1618 | static struct cpelement * | |
1619 | cpe_alloc (cp) | |
1620 | Coproc *cp; | |
1621 | { | |
1622 | struct cpelement *cpe; | |
1623 | ||
1624 | cpe = (struct cpelement *)xmalloc (sizeof (struct cpelement)); | |
1625 | cpe->coproc = cp; | |
1626 | cpe->next = (struct cpelement *)0; | |
1627 | return cpe; | |
1628 | } | |
1629 | ||
1630 | static void | |
1631 | cpe_dispose (cpe) | |
1632 | struct cpelement *cpe; | |
1633 | { | |
1634 | free (cpe); | |
1635 | } | |
1636 | ||
1637 | static struct cpelement * | |
1638 | cpl_add (cp) | |
1639 | Coproc *cp; | |
1640 | { | |
1641 | struct cpelement *cpe; | |
1642 | ||
1643 | cpe = cpe_alloc (cp); | |
1644 | ||
1645 | if (coproc_list.head == 0) | |
1646 | { | |
1647 | coproc_list.head = coproc_list.tail = cpe; | |
1648 | coproc_list.ncoproc = 0; /* just to make sure */ | |
1649 | } | |
1650 | else | |
1651 | { | |
1652 | coproc_list.tail->next = cpe; | |
1653 | coproc_list.tail = cpe; | |
1654 | } | |
1655 | coproc_list.ncoproc++; | |
1656 | ||
1657 | return cpe; | |
1658 | } | |
1659 | ||
1660 | static struct cpelement * | |
1661 | cpl_delete (pid) | |
1662 | pid_t pid; | |
1663 | { | |
1664 | struct cpelement *prev, *p; | |
1665 | ||
1666 | for (prev = p = coproc_list.head; p; prev = p, p = p->next) | |
1667 | if (p->coproc->c_pid == pid) | |
1668 | { | |
1669 | prev->next = p->next; /* remove from list */ | |
1670 | break; | |
1671 | } | |
1672 | ||
1673 | if (p == 0) | |
1674 | return 0; /* not found */ | |
1675 | ||
1676 | #if defined (DEBUG) | |
1677 | itrace("cpl_delete: deleting %d", pid); | |
1678 | #endif | |
1679 | ||
1680 | /* Housekeeping in the border cases. */ | |
1681 | if (p == coproc_list.head) | |
1682 | coproc_list.head = coproc_list.head->next; | |
1683 | else if (p == coproc_list.tail) | |
1684 | coproc_list.tail = prev; | |
1685 | ||
1686 | coproc_list.ncoproc--; | |
1687 | if (coproc_list.ncoproc == 0) | |
1688 | coproc_list.head = coproc_list.tail = 0; | |
1689 | else if (coproc_list.ncoproc == 1) | |
1690 | coproc_list.tail = coproc_list.head; /* just to make sure */ | |
1691 | ||
1692 | return (p); | |
1693 | } | |
1694 | ||
e141c35a CR |
1695 | static void |
1696 | cpl_reap () | |
1697 | { | |
631b20c6 | 1698 | struct cpelement *p, *next, *nh, *nt; |
e141c35a | 1699 | |
631b20c6 CR |
1700 | /* Build a new list by removing dead coprocs and fix up the coproc_list |
1701 | pointers when done. */ | |
1702 | nh = nt = next = (struct cpelement *)0; | |
1703 | for (p = coproc_list.head; p; p = next) | |
1704 | { | |
1705 | next = p->next; | |
1706 | if (p->coproc->c_flags & COPROC_DEAD) | |
1707 | { | |
1708 | coproc_list.ncoproc--; /* keep running count, fix up pointers later */ | |
e141c35a CR |
1709 | |
1710 | #if defined (DEBUG) | |
631b20c6 | 1711 | itrace("cpl_reap: deleting %d", p->coproc->c_pid); |
e141c35a CR |
1712 | #endif |
1713 | ||
631b20c6 CR |
1714 | coproc_dispose (p->coproc); |
1715 | cpe_dispose (p); | |
1716 | } | |
1717 | else if (nh == 0) | |
1718 | nh = nt = p; | |
1719 | else | |
1720 | { | |
1721 | nt->next = p; | |
1722 | nt = nt->next; | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | if (coproc_list.ncoproc == 0) | |
1727 | coproc_list.head = coproc_list.tail = 0; | |
1728 | else | |
1729 | { | |
1730 | if (nt) | |
1731 | nt->next = 0; | |
1732 | coproc_list.head = nh; | |
1733 | coproc_list.tail = nt; | |
1734 | if (coproc_list.ncoproc == 1) | |
1735 | coproc_list.tail = coproc_list.head; /* just to make sure */ | |
1736 | } | |
e141c35a CR |
1737 | } |
1738 | ||
8e1a6eaa CR |
1739 | /* Clear out the list of saved statuses */ |
1740 | static void | |
1741 | cpl_flush () | |
1742 | { | |
1743 | struct cpelement *cpe, *p; | |
1744 | ||
1745 | for (cpe = coproc_list.head; cpe; ) | |
1746 | { | |
1747 | p = cpe; | |
1748 | cpe = cpe->next; | |
1749 | ||
1750 | coproc_dispose (p->coproc); | |
1751 | cpe_dispose (p); | |
1752 | } | |
1753 | ||
1754 | coproc_list.head = coproc_list.tail = 0; | |
1755 | coproc_list.ncoproc = 0; | |
1756 | } | |
1757 | ||
f6da9f85 CR |
1758 | static void |
1759 | cpl_closeall () | |
1760 | { | |
1761 | struct cpelement *cpe; | |
1762 | ||
af5751ec | 1763 | for (cpe = coproc_list.head; cpe; cpe = cpe->next) |
f6da9f85 CR |
1764 | coproc_close (cpe->coproc); |
1765 | } | |
1766 | ||
1767 | static void | |
1768 | cpl_fdchk (fd) | |
1769 | int fd; | |
1770 | { | |
1771 | struct cpelement *cpe; | |
1772 | ||
af5751ec | 1773 | for (cpe = coproc_list.head; cpe; cpe = cpe->next) |
f6da9f85 CR |
1774 | coproc_checkfd (cpe->coproc, fd); |
1775 | } | |
1776 | ||
8e1a6eaa CR |
1777 | /* Search for PID in the list of coprocs; return the cpelement struct if |
1778 | found. If not found, return NULL. */ | |
1779 | static struct cpelement * | |
1780 | cpl_search (pid) | |
1781 | pid_t pid; | |
1782 | { | |
f6da9f85 | 1783 | struct cpelement *cpe; |
8e1a6eaa | 1784 | |
f6da9f85 CR |
1785 | for (cpe = coproc_list.head ; cpe; cpe = cpe->next) |
1786 | if (cpe->coproc->c_pid == pid) | |
1787 | return cpe; | |
8e1a6eaa CR |
1788 | return (struct cpelement *)NULL; |
1789 | } | |
1790 | ||
1791 | /* Search for the coproc named NAME in the list of coprocs; return the | |
1792 | cpelement struct if found. If not found, return NULL. */ | |
1793 | static struct cpelement * | |
1794 | cpl_searchbyname (name) | |
f6da9f85 | 1795 | const char *name; |
8e1a6eaa CR |
1796 | { |
1797 | struct cpelement *cp; | |
1798 | ||
1799 | for (cp = coproc_list.head ; cp; cp = cp->next) | |
1800 | if (STREQ (cp->coproc->c_name, name)) | |
1801 | return cp; | |
1802 | return (struct cpelement *)NULL; | |
1803 | } | |
1804 | ||
1805 | #if 0 | |
1806 | static void | |
1807 | cpl_prune () | |
1808 | { | |
1809 | struct cpelement *cp; | |
1810 | ||
1811 | while (coproc_list.head && coproc_list.ncoproc > COPROC_MAX) | |
1812 | { | |
1813 | cp = coproc_list.head; | |
1814 | coproc_list.head = coproc_list.head->next; | |
1815 | coproc_dispose (cp->coproc); | |
1816 | cpe_dispose (cp); | |
1817 | coproc_list.ncoproc--; | |
1818 | } | |
1819 | } | |
1820 | #endif | |
1821 | ||
09767ff0 | 1822 | /* These currently use a single global "shell coproc" but are written in a |
8e1a6eaa CR |
1823 | way to not preclude additional coprocs later (using the list management |
1824 | package above). */ | |
09767ff0 CR |
1825 | |
1826 | struct coproc * | |
1827 | getcoprocbypid (pid) | |
1828 | pid_t pid; | |
1829 | { | |
f6da9f85 CR |
1830 | #if MULTIPLE_COPROCS |
1831 | struct cpelement *p; | |
1832 | ||
1833 | p = cpl_search (pid); | |
1834 | return (p ? p->coproc : 0); | |
1835 | #else | |
09767ff0 | 1836 | return (pid == sh_coproc.c_pid ? &sh_coproc : 0); |
f6da9f85 | 1837 | #endif |
09767ff0 CR |
1838 | } |
1839 | ||
1840 | struct coproc * | |
1841 | getcoprocbyname (name) | |
1842 | const char *name; | |
1843 | { | |
f6da9f85 CR |
1844 | #if MULTIPLE_COPROCS |
1845 | struct cpelement *p; | |
1846 | ||
1847 | p = cpl_searchbyname (name); | |
1848 | return (p ? p->coproc : 0); | |
1849 | #else | |
09767ff0 | 1850 | return ((sh_coproc.c_name && STREQ (sh_coproc.c_name, name)) ? &sh_coproc : 0); |
f6da9f85 | 1851 | #endif |
09767ff0 CR |
1852 | } |
1853 | ||
1854 | void | |
1855 | coproc_init (cp) | |
1856 | struct coproc *cp; | |
1857 | { | |
1858 | cp->c_name = 0; | |
1859 | cp->c_pid = NO_PID; | |
1860 | cp->c_rfd = cp->c_wfd = -1; | |
1861 | cp->c_rsave = cp->c_wsave = -1; | |
1862 | cp->c_flags = cp->c_status = 0; | |
1863 | } | |
1864 | ||
1865 | struct coproc * | |
1866 | coproc_alloc (name, pid) | |
1867 | char *name; | |
1868 | pid_t pid; | |
1869 | { | |
1870 | struct coproc *cp; | |
1871 | ||
f6da9f85 CR |
1872 | #if MULTIPLE_COPROCS |
1873 | cp = (struct coproc *)xmalloc (sizeof (struct coproc)); | |
1874 | #else | |
1875 | cp = &sh_coproc; | |
1876 | #endif | |
09767ff0 CR |
1877 | coproc_init (cp); |
1878 | ||
1879 | cp->c_name = savestring (name); | |
1880 | cp->c_pid = pid; | |
1881 | ||
f6da9f85 CR |
1882 | #if MULTIPLE_COPROCS |
1883 | cpl_add (cp); | |
1884 | #endif | |
1885 | ||
09767ff0 CR |
1886 | return (cp); |
1887 | } | |
1888 | ||
f6da9f85 CR |
1889 | static void |
1890 | coproc_free (cp) | |
1891 | struct coproc *cp; | |
1892 | { | |
1893 | free (cp); | |
1894 | } | |
1895 | ||
09767ff0 CR |
1896 | void |
1897 | coproc_dispose (cp) | |
1898 | struct coproc *cp; | |
1899 | { | |
1900 | if (cp == 0) | |
1901 | return; | |
1902 | ||
1903 | coproc_unsetvars (cp); | |
1904 | FREE (cp->c_name); | |
1905 | coproc_close (cp); | |
f6da9f85 CR |
1906 | #if MULTIPLE_COPROCS |
1907 | coproc_free (cp); | |
1908 | #else | |
09767ff0 | 1909 | coproc_init (cp); |
f6da9f85 | 1910 | #endif |
09767ff0 CR |
1911 | } |
1912 | ||
f6da9f85 | 1913 | /* Placeholder for now. Will require changes for multiple coprocs */ |
8e1a6eaa CR |
1914 | void |
1915 | coproc_flush () | |
1916 | { | |
f6da9f85 CR |
1917 | #if MULTIPLE_COPROCS |
1918 | cpl_flush (); | |
1919 | #else | |
8e1a6eaa | 1920 | coproc_dispose (&sh_coproc); |
f6da9f85 | 1921 | #endif |
8e1a6eaa CR |
1922 | } |
1923 | ||
09767ff0 CR |
1924 | void |
1925 | coproc_close (cp) | |
1926 | struct coproc *cp; | |
1927 | { | |
1928 | if (cp->c_rfd >= 0) | |
1929 | { | |
1930 | close (cp->c_rfd); | |
1931 | cp->c_rfd = -1; | |
1932 | } | |
1933 | if (cp->c_wfd >= 0) | |
1934 | { | |
1935 | close (cp->c_wfd); | |
1936 | cp->c_wfd = -1; | |
1937 | } | |
1938 | cp->c_rsave = cp->c_wsave = -1; | |
1939 | } | |
1940 | ||
8e1a6eaa CR |
1941 | void |
1942 | coproc_closeall () | |
1943 | { | |
f6da9f85 CR |
1944 | #if MULTIPLE_COPROCS |
1945 | cpl_closeall (); | |
1946 | #else | |
1947 | coproc_close (&sh_coproc); /* XXX - will require changes for multiple coprocs */ | |
1948 | #endif | |
8e1a6eaa CR |
1949 | } |
1950 | ||
e141c35a CR |
1951 | void |
1952 | coproc_reap () | |
1953 | { | |
f6da9f85 CR |
1954 | #if MULTIPLE_COPROCS |
1955 | cpl_reap (); | |
1956 | #else | |
e141c35a CR |
1957 | struct coproc *cp; |
1958 | ||
f6da9f85 | 1959 | cp = &sh_coproc; /* XXX - will require changes for multiple coprocs */ |
e141c35a CR |
1960 | if (cp && (cp->c_flags & COPROC_DEAD)) |
1961 | coproc_dispose (cp); | |
f6da9f85 | 1962 | #endif |
e141c35a CR |
1963 | } |
1964 | ||
09767ff0 CR |
1965 | void |
1966 | coproc_rclose (cp, fd) | |
1967 | struct coproc *cp; | |
1968 | int fd; | |
1969 | { | |
1970 | if (cp->c_rfd >= 0 && cp->c_rfd == fd) | |
1971 | { | |
1972 | close (cp->c_rfd); | |
1973 | cp->c_rfd = -1; | |
1974 | } | |
1975 | } | |
1976 | ||
1977 | void | |
1978 | coproc_wclose (cp, fd) | |
1979 | struct coproc *cp; | |
1980 | int fd; | |
1981 | { | |
1982 | if (cp->c_wfd >= 0 && cp->c_wfd == fd) | |
1983 | { | |
1984 | close (cp->c_wfd); | |
1985 | cp->c_wfd = -1; | |
1986 | } | |
1987 | } | |
1988 | ||
1989 | void | |
8e1a6eaa | 1990 | coproc_checkfd (cp, fd) |
09767ff0 CR |
1991 | struct coproc *cp; |
1992 | int fd; | |
1993 | { | |
1994 | int update; | |
1995 | ||
1996 | update = 0; | |
1997 | if (cp->c_rfd >= 0 && cp->c_rfd == fd) | |
1998 | update = cp->c_rfd = -1; | |
1999 | if (cp->c_wfd >= 0 && cp->c_wfd == fd) | |
2000 | update = cp->c_wfd = -1; | |
2001 | if (update) | |
2002 | coproc_setvars (cp); | |
2003 | } | |
2004 | ||
8e1a6eaa CR |
2005 | void |
2006 | coproc_fdchk (fd) | |
2007 | int fd; | |
2008 | { | |
f6da9f85 CR |
2009 | #if MULTIPLE_COPROCS |
2010 | cpl_fdchk (fd); | |
2011 | #else | |
8e1a6eaa | 2012 | coproc_checkfd (&sh_coproc, fd); |
f6da9f85 | 2013 | #endif |
8e1a6eaa CR |
2014 | } |
2015 | ||
09767ff0 CR |
2016 | void |
2017 | coproc_fdclose (cp, fd) | |
2018 | struct coproc *cp; | |
2019 | int fd; | |
2020 | { | |
2021 | coproc_rclose (cp, fd); | |
2022 | coproc_wclose (cp, fd); | |
2023 | coproc_setvars (cp); | |
2024 | } | |
2025 | ||
09767ff0 CR |
2026 | void |
2027 | coproc_fdsave (cp) | |
2028 | struct coproc *cp; | |
2029 | { | |
2030 | cp->c_rsave = cp->c_rfd; | |
2031 | cp->c_wsave = cp->c_wfd; | |
2032 | } | |
2033 | ||
2034 | void | |
2035 | coproc_fdrestore (cp) | |
2036 | struct coproc *cp; | |
2037 | { | |
2038 | cp->c_rfd = cp->c_rsave; | |
2039 | cp->c_wfd = cp->c_wsave; | |
2040 | } | |
e141c35a | 2041 | |
09767ff0 | 2042 | void |
e141c35a | 2043 | coproc_pidchk (pid, status) |
09767ff0 CR |
2044 | pid_t pid; |
2045 | { | |
2046 | struct coproc *cp; | |
2047 | ||
f6da9f85 CR |
2048 | #if MULTIPLE_COPROCS |
2049 | struct cpelement *cpe; | |
2050 | ||
2051 | cpe = cpl_delete (pid); | |
2052 | cp = cpe ? cpe->coproc : 0; | |
2053 | #else | |
09767ff0 | 2054 | cp = getcoprocbypid (pid); |
8e1a6eaa | 2055 | #endif |
09767ff0 | 2056 | if (cp) |
e141c35a | 2057 | { |
f6da9f85 CR |
2058 | #if 0 |
2059 | itrace("coproc_pidchk: pid %d has died", pid); | |
2060 | #endif | |
e141c35a CR |
2061 | cp->c_status = status; |
2062 | cp->c_flags |= COPROC_DEAD; | |
2063 | cp->c_flags &= ~COPROC_RUNNING; | |
f6da9f85 | 2064 | #if MULTIPLE_COPROCS |
e141c35a | 2065 | coproc_dispose (cp); |
f6da9f85 CR |
2066 | #else |
2067 | coproc_unsetvars (cp); | |
e141c35a CR |
2068 | #endif |
2069 | } | |
09767ff0 CR |
2070 | } |
2071 | ||
2072 | void | |
2073 | coproc_setvars (cp) | |
2074 | struct coproc *cp; | |
2075 | { | |
2076 | SHELL_VAR *v; | |
2077 | char *namevar, *t; | |
2078 | int l; | |
2079 | #if defined (ARRAY_VARS) | |
2080 | arrayind_t ind; | |
2081 | #endif | |
2082 | ||
2083 | if (cp->c_name == 0) | |
2084 | return; | |
2085 | ||
2086 | l = strlen (cp->c_name); | |
2087 | namevar = xmalloc (l + 16); | |
2088 | ||
2089 | #if defined (ARRAY_VARS) | |
2090 | v = find_variable (cp->c_name); | |
2091 | if (v == 0) | |
2092 | v = make_new_array_variable (cp->c_name); | |
2093 | if (array_p (v) == 0) | |
2094 | v = convert_var_to_array (v); | |
2095 | ||
2096 | t = itos (cp->c_rfd); | |
2097 | ind = 0; | |
2098 | v = bind_array_variable (cp->c_name, ind, t, 0); | |
2099 | free (t); | |
2100 | ||
2101 | t = itos (cp->c_wfd); | |
2102 | ind = 1; | |
2103 | bind_array_variable (cp->c_name, ind, t, 0); | |
2104 | free (t); | |
2105 | #else | |
2106 | sprintf (namevar, "%s_READ", cp->c_name); | |
2107 | t = itos (cp->c_rfd); | |
2108 | bind_variable (namevar, t, 0); | |
2109 | free (t); | |
2110 | sprintf (namevar, "%s_WRITE", cp->c_name); | |
2111 | t = itos (cp->c_wfd); | |
2112 | bind_variable (namevar, t, 0); | |
2113 | free (t); | |
2114 | #endif | |
2115 | ||
2116 | sprintf (namevar, "%s_PID", cp->c_name); | |
2117 | t = itos (cp->c_pid); | |
2118 | bind_variable (namevar, t, 0); | |
2119 | free (t); | |
2120 | ||
2121 | free (namevar); | |
2122 | } | |
2123 | ||
2124 | void | |
2125 | coproc_unsetvars (cp) | |
2126 | struct coproc *cp; | |
2127 | { | |
2128 | int l; | |
2129 | char *namevar; | |
2130 | ||
2131 | if (cp->c_name == 0) | |
2132 | return; | |
2133 | ||
2134 | l = strlen (cp->c_name); | |
2135 | namevar = xmalloc (l + 16); | |
2136 | ||
2137 | sprintf (namevar, "%s_PID", cp->c_name); | |
2138 | unbind_variable (namevar); | |
2139 | ||
2140 | #if defined (ARRAY_VARS) | |
2141 | unbind_variable (cp->c_name); | |
2142 | #else | |
2143 | sprintf (namevar, "%s_READ", cp->c_name); | |
2144 | unbind_variable (namevar); | |
2145 | sprintf (namevar, "%s_WRITE", cp->c_name); | |
2146 | unbind_variable (namevar); | |
2147 | #endif | |
2148 | ||
2149 | free (namevar); | |
2150 | } | |
2151 | ||
2152 | static int | |
2153 | execute_coproc (command, pipe_in, pipe_out, fds_to_close) | |
2154 | COMMAND *command; | |
2155 | int pipe_in, pipe_out; | |
2156 | struct fd_bitmap *fds_to_close; | |
2157 | { | |
af98a2a6 | 2158 | int rpipe[2], wpipe[2], estat; |
09767ff0 CR |
2159 | pid_t coproc_pid; |
2160 | Coproc *cp; | |
2161 | char *tcmd; | |
2162 | ||
f6da9f85 CR |
2163 | /* XXX -- can be removed after changes to handle multiple coprocs */ |
2164 | #if !MULTIPLE_COPROCS | |
30915f17 | 2165 | if (sh_coproc.c_pid != NO_PID) |
f6da9f85 | 2166 | internal_warning ("execute_coproc: coproc [%d:%s] still exists", sh_coproc.c_pid, sh_coproc.c_name); |
09767ff0 | 2167 | coproc_init (&sh_coproc); |
f6da9f85 | 2168 | #endif |
09767ff0 CR |
2169 | |
2170 | command_string_index = 0; | |
2171 | tcmd = make_command_string (command); | |
2172 | ||
2173 | sh_openpipe ((int *)&rpipe); /* 0 = parent read, 1 = child write */ | |
2174 | sh_openpipe ((int *)&wpipe); /* 0 = child read, 1 = parent write */ | |
2175 | ||
2176 | coproc_pid = make_child (savestring (tcmd), 1); | |
2177 | if (coproc_pid == 0) | |
2178 | { | |
2179 | close (rpipe[0]); | |
2180 | close (wpipe[1]); | |
2181 | ||
af98a2a6 CR |
2182 | estat = execute_in_subshell (command, 1, wpipe[0], rpipe[1], fds_to_close); |
2183 | ||
2184 | fflush (stdout); | |
2185 | fflush (stderr); | |
2186 | ||
2187 | exit (estat); | |
09767ff0 CR |
2188 | } |
2189 | ||
2190 | close (rpipe[1]); | |
2191 | close (wpipe[0]); | |
2192 | ||
2193 | cp = coproc_alloc (command->value.Coproc->name, coproc_pid); | |
2194 | cp->c_rfd = rpipe[0]; | |
2195 | cp->c_wfd = wpipe[1]; | |
2196 | ||
2197 | SET_CLOSE_ON_EXEC (cp->c_rfd); | |
2198 | SET_CLOSE_ON_EXEC (cp->c_wfd); | |
2199 | ||
2200 | coproc_setvars (cp); | |
2201 | ||
dd4f3dd8 | 2202 | #if 0 |
09767ff0 CR |
2203 | itrace ("execute_coproc: [%d] %s", coproc_pid, the_printed_command); |
2204 | #endif | |
2205 | ||
2206 | close_pipes (pipe_in, pipe_out); | |
2207 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
2208 | unlink_fifo_list (); | |
2209 | #endif | |
2210 | stop_pipeline (1, (COMMAND *)NULL); | |
2211 | DESCRIBE_PID (coproc_pid); | |
2212 | run_pending_traps (); | |
2213 | ||
2214 | return (EXECUTION_SUCCESS); | |
2215 | } | |
2216 | #endif | |
2217 | ||
7d92f73f CR |
2218 | static void |
2219 | restore_stdin (s) | |
2220 | int s; | |
2221 | { | |
2222 | dup2 (s, 0); | |
2223 | close (s); | |
2224 | } | |
2225 | ||
2226 | /* Catch-all cleanup function for lastpipe code for unwind-protects */ | |
2227 | static void | |
2228 | lastpipe_cleanup (s) | |
2229 | int s; | |
2230 | { | |
2231 | unfreeze_jobs_list (); | |
2232 | } | |
2233 | ||
ccc6cda3 JA |
2234 | static int |
2235 | execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
2236 | COMMAND *command; | |
2237 | int asynchronous, pipe_in, pipe_out; | |
2238 | struct fd_bitmap *fds_to_close; | |
2239 | { | |
2240 | int prev, fildes[2], new_bitmap_size, dummyfd, ignore_return, exec_result; | |
7d92f73f | 2241 | int lstdin, lastpipe_flag, lastpipe_jid; |
ccc6cda3 JA |
2242 | COMMAND *cmd; |
2243 | struct fd_bitmap *fd_bitmap; | |
7d92f73f | 2244 | pid_t lastpid; |
726f6388 JA |
2245 | |
2246 | #if defined (JOB_CONTROL) | |
ccc6cda3 JA |
2247 | sigset_t set, oset; |
2248 | BLOCK_CHILD (set, oset); | |
726f6388 JA |
2249 | #endif /* JOB_CONTROL */ |
2250 | ||
ccc6cda3 | 2251 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; |
726f6388 | 2252 | |
ccc6cda3 JA |
2253 | prev = pipe_in; |
2254 | cmd = command; | |
2255 | ||
2256 | while (cmd && cmd->type == cm_connection && | |
2257 | cmd->value.Connection && cmd->value.Connection->connector == '|') | |
2258 | { | |
2259 | /* Make a pipeline between the two commands. */ | |
2260 | if (pipe (fildes) < 0) | |
2261 | { | |
d3ad40de | 2262 | sys_error (_("pipe error")); |
726f6388 | 2263 | #if defined (JOB_CONTROL) |
ccc6cda3 JA |
2264 | terminate_current_pipeline (); |
2265 | kill_current_pipeline (); | |
22e63b05 | 2266 | UNBLOCK_CHILD (oset); |
726f6388 | 2267 | #endif /* JOB_CONTROL */ |
ccc6cda3 JA |
2268 | last_command_exit_value = EXECUTION_FAILURE; |
2269 | /* The unwind-protects installed below will take care | |
2270 | of closing all of the open file descriptors. */ | |
2271 | throw_to_top_level (); | |
2272 | return (EXECUTION_FAILURE); /* XXX */ | |
2273 | } | |
2274 | ||
2275 | /* Here is a problem: with the new file close-on-exec | |
2276 | code, the read end of the pipe (fildes[0]) stays open | |
2277 | in the first process, so that process will never get a | |
2278 | SIGPIPE. There is no way to signal the first process | |
2279 | that it should close fildes[0] after forking, so it | |
2280 | remains open. No SIGPIPE is ever sent because there | |
2281 | is still a file descriptor open for reading connected | |
2282 | to the pipe. We take care of that here. This passes | |
2283 | around a bitmap of file descriptors that must be | |
2284 | closed after making a child process in execute_simple_command. */ | |
2285 | ||
2286 | /* We need fd_bitmap to be at least as big as fildes[0]. | |
2287 | If fildes[0] is less than fds_to_close->size, then | |
2288 | use fds_to_close->size. */ | |
2289 | new_bitmap_size = (fildes[0] < fds_to_close->size) | |
2290 | ? fds_to_close->size | |
2291 | : fildes[0] + 8; | |
2292 | ||
2293 | fd_bitmap = new_fd_bitmap (new_bitmap_size); | |
2294 | ||
2295 | /* Now copy the old information into the new bitmap. */ | |
2296 | xbcopy ((char *)fds_to_close->bitmap, (char *)fd_bitmap->bitmap, fds_to_close->size); | |
2297 | ||
2298 | /* And mark the pipe file descriptors to be closed. */ | |
2299 | fd_bitmap->bitmap[fildes[0]] = 1; | |
2300 | ||
2301 | /* In case there are pipe or out-of-processes errors, we | |
cce855bc | 2302 | want all these file descriptors to be closed when |
ccc6cda3 JA |
2303 | unwind-protects are run, and the storage used for the |
2304 | bitmaps freed up. */ | |
2305 | begin_unwind_frame ("pipe-file-descriptors"); | |
2306 | add_unwind_protect (dispose_fd_bitmap, fd_bitmap); | |
2307 | add_unwind_protect (close_fd_bitmap, fd_bitmap); | |
2308 | if (prev >= 0) | |
2309 | add_unwind_protect (close, prev); | |
2310 | dummyfd = fildes[1]; | |
2311 | add_unwind_protect (close, dummyfd); | |
726f6388 JA |
2312 | |
2313 | #if defined (JOB_CONTROL) | |
f73dda09 | 2314 | add_unwind_protect (restore_signal_mask, &oset); |
726f6388 JA |
2315 | #endif /* JOB_CONTROL */ |
2316 | ||
ccc6cda3 JA |
2317 | if (ignore_return && cmd->value.Connection->first) |
2318 | cmd->value.Connection->first->flags |= CMD_IGNORE_RETURN; | |
2319 | execute_command_internal (cmd->value.Connection->first, asynchronous, | |
2320 | prev, fildes[1], fd_bitmap); | |
2321 | ||
2322 | if (prev >= 0) | |
2323 | close (prev); | |
2324 | ||
2325 | prev = fildes[0]; | |
2326 | close (fildes[1]); | |
2327 | ||
2328 | dispose_fd_bitmap (fd_bitmap); | |
2329 | discard_unwind_frame ("pipe-file-descriptors"); | |
726f6388 | 2330 | |
ccc6cda3 JA |
2331 | cmd = cmd->value.Connection->second; |
2332 | } | |
2333 | ||
7d92f73f CR |
2334 | lastpid = last_made_pid; |
2335 | ||
ccc6cda3 JA |
2336 | /* Now execute the rightmost command in the pipeline. */ |
2337 | if (ignore_return && cmd) | |
2338 | cmd->flags |= CMD_IGNORE_RETURN; | |
7d92f73f CR |
2339 | |
2340 | lastpipe_flag = 0; | |
c74f63f3 | 2341 | |
7d92f73f CR |
2342 | begin_unwind_frame ("lastpipe-exec"); |
2343 | lstdin = -1; | |
2344 | /* If the `lastpipe' option is set with shopt, and job control is not | |
2345 | enabled, execute the last element of non-async pipelines in the | |
2346 | current shell environment. */ | |
2347 | if (lastpipe_opt && job_control == 0 && asynchronous == 0 && pipe_out == NO_PIPE && prev > 0) | |
2348 | { | |
8cb3d567 | 2349 | lstdin = move_to_high_fd (0, 1, -1); |
7d92f73f CR |
2350 | if (lstdin > 0) |
2351 | { | |
2352 | do_piping (prev, pipe_out); | |
2353 | prev = NO_PIPE; | |
2354 | add_unwind_protect (restore_stdin, lstdin); | |
2355 | lastpipe_flag = 1; | |
2356 | freeze_jobs_list (); | |
2357 | lastpipe_jid = stop_pipeline (0, (COMMAND *)NULL); /* XXX */ | |
2358 | add_unwind_protect (lastpipe_cleanup, lastpipe_jid); | |
2359 | } | |
631b20c6 CR |
2360 | if (cmd) |
2361 | cmd->flags |= CMD_LASTPIPE; | |
7d92f73f CR |
2362 | } |
2363 | if (prev >= 0) | |
2364 | add_unwind_protect (close, prev); | |
2365 | ||
ccc6cda3 | 2366 | exec_result = execute_command_internal (cmd, asynchronous, prev, pipe_out, fds_to_close); |
726f6388 | 2367 | |
7d92f73f CR |
2368 | if (lstdin > 0) |
2369 | restore_stdin (lstdin); | |
2370 | ||
ccc6cda3 JA |
2371 | if (prev >= 0) |
2372 | close (prev); | |
726f6388 JA |
2373 | |
2374 | #if defined (JOB_CONTROL) | |
ccc6cda3 | 2375 | UNBLOCK_CHILD (oset); |
726f6388 | 2376 | #endif |
726f6388 | 2377 | |
c92a890b | 2378 | QUIT; |
7d92f73f CR |
2379 | |
2380 | if (lastpipe_flag) | |
2381 | { | |
2382 | #if defined (JOB_CONTROL) | |
2383 | append_process (savestring (the_printed_command), dollar_dollar_pid, exec_result, lastpipe_jid); | |
2384 | #endif | |
2385 | lstdin = wait_for (lastpid); | |
2386 | #if defined (JOB_CONTROL) | |
2387 | exec_result = job_exit_status (lastpipe_jid); | |
2388 | #endif | |
2389 | unfreeze_jobs_list (); | |
2390 | } | |
2391 | ||
2392 | discard_unwind_frame ("lastpipe-exec"); | |
2393 | ||
ccc6cda3 JA |
2394 | return (exec_result); |
2395 | } | |
2396 | ||
2397 | static int | |
2398 | execute_connection (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
2399 | COMMAND *command; | |
2400 | int asynchronous, pipe_in, pipe_out; | |
2401 | struct fd_bitmap *fds_to_close; | |
2402 | { | |
ccc6cda3 | 2403 | COMMAND *tc, *second; |
a05a1337 | 2404 | int ignore_return, exec_result, was_error_trap, invert; |
c302751c | 2405 | volatile int save_line_number; |
ccc6cda3 JA |
2406 | |
2407 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; | |
2408 | ||
2409 | switch (command->value.Connection->connector) | |
2410 | { | |
2411 | /* Do the first command asynchronously. */ | |
2412 | case '&': | |
2413 | tc = command->value.Connection->first; | |
2414 | if (tc == 0) | |
2415 | return (EXECUTION_SUCCESS); | |
2416 | ||
d166f048 | 2417 | if (ignore_return) |
ccc6cda3 | 2418 | tc->flags |= CMD_IGNORE_RETURN; |
d166f048 | 2419 | tc->flags |= CMD_AMPERSAND; |
ccc6cda3 | 2420 | |
f73dda09 JA |
2421 | /* If this shell was compiled without job control support, |
2422 | if we are currently in a subshell via `( xxx )', or if job | |
2423 | control is not active then the standard input for an | |
2424 | asynchronous command is forced to /dev/null. */ | |
ccc6cda3 | 2425 | #if defined (JOB_CONTROL) |
f73dda09 | 2426 | if ((subshell_environment || !job_control) && !stdin_redir) |
ccc6cda3 JA |
2427 | #else |
2428 | if (!stdin_redir) | |
2429 | #endif /* JOB_CONTROL */ | |
7117c2d2 | 2430 | tc->flags |= CMD_STDIN_REDIR; |
ccc6cda3 JA |
2431 | |
2432 | exec_result = execute_command_internal (tc, 1, pipe_in, pipe_out, fds_to_close); | |
f1c4df24 | 2433 | QUIT; |
ccc6cda3 | 2434 | |
d166f048 | 2435 | if (tc->flags & CMD_STDIN_REDIR) |
7117c2d2 | 2436 | tc->flags &= ~CMD_STDIN_REDIR; |
726f6388 | 2437 | |
ccc6cda3 JA |
2438 | second = command->value.Connection->second; |
2439 | if (second) | |
2440 | { | |
2441 | if (ignore_return) | |
2442 | second->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 2443 | |
ccc6cda3 JA |
2444 | exec_result = execute_command_internal (second, asynchronous, pipe_in, pipe_out, fds_to_close); |
2445 | } | |
726f6388 | 2446 | |
ccc6cda3 | 2447 | break; |
726f6388 | 2448 | |
ccc6cda3 JA |
2449 | /* Just call execute command on both sides. */ |
2450 | case ';': | |
2451 | if (ignore_return) | |
2452 | { | |
2453 | if (command->value.Connection->first) | |
2454 | command->value.Connection->first->flags |= CMD_IGNORE_RETURN; | |
2455 | if (command->value.Connection->second) | |
2456 | command->value.Connection->second->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 2457 | } |
f1c4df24 | 2458 | executing_list++; |
ccc6cda3 JA |
2459 | QUIT; |
2460 | execute_command (command->value.Connection->first); | |
2461 | QUIT; | |
2462 | exec_result = execute_command_internal (command->value.Connection->second, | |
2463 | asynchronous, pipe_in, pipe_out, | |
2464 | fds_to_close); | |
f1c4df24 | 2465 | executing_list--; |
726f6388 JA |
2466 | break; |
2467 | ||
ccc6cda3 | 2468 | case '|': |
a05a1337 CR |
2469 | was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0; |
2470 | invert = (command->flags & CMD_INVERT_RETURN) != 0; | |
2471 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; | |
2472 | ||
c302751c | 2473 | line_number_for_err_trap = line_number; |
ccc6cda3 | 2474 | exec_result = execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close); |
a05a1337 CR |
2475 | |
2476 | if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS) | |
2477 | { | |
2478 | last_command_exit_value = exec_result; | |
c302751c CR |
2479 | save_line_number = line_number; |
2480 | line_number = line_number_for_err_trap; | |
a05a1337 | 2481 | run_error_trap (); |
c302751c | 2482 | line_number = save_line_number; |
a05a1337 CR |
2483 | } |
2484 | ||
2485 | if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS) | |
2486 | { | |
2487 | last_command_exit_value = exec_result; | |
2488 | run_pending_traps (); | |
2489 | jump_to_top_level (ERREXIT); | |
2490 | } | |
2491 | ||
726f6388 JA |
2492 | break; |
2493 | ||
ccc6cda3 JA |
2494 | case AND_AND: |
2495 | case OR_OR: | |
2496 | if (asynchronous) | |
2497 | { | |
2498 | /* If we have something like `a && b &' or `a || b &', run the | |
2499 | && or || stuff in a subshell. Force a subshell and just call | |
2500 | execute_command_internal again. Leave asynchronous on | |
2501 | so that we get a report from the parent shell about the | |
2502 | background job. */ | |
2503 | command->flags |= CMD_FORCE_SUBSHELL; | |
2504 | exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close); | |
2505 | break; | |
2506 | } | |
726f6388 | 2507 | |
ccc6cda3 JA |
2508 | /* Execute the first command. If the result of that is successful |
2509 | and the connector is AND_AND, or the result is not successful | |
2510 | and the connector is OR_OR, then execute the second command, | |
2511 | otherwise return. */ | |
726f6388 | 2512 | |
f1c4df24 | 2513 | executing_list++; |
ccc6cda3 JA |
2514 | if (command->value.Connection->first) |
2515 | command->value.Connection->first->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 2516 | |
ccc6cda3 JA |
2517 | exec_result = execute_command (command->value.Connection->first); |
2518 | QUIT; | |
2519 | if (((command->value.Connection->connector == AND_AND) && | |
2520 | (exec_result == EXECUTION_SUCCESS)) || | |
2521 | ((command->value.Connection->connector == OR_OR) && | |
2522 | (exec_result != EXECUTION_SUCCESS))) | |
2523 | { | |
2524 | if (ignore_return && command->value.Connection->second) | |
2525 | command->value.Connection->second->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 2526 | |
ccc6cda3 JA |
2527 | exec_result = execute_command (command->value.Connection->second); |
2528 | } | |
f1c4df24 | 2529 | executing_list--; |
ccc6cda3 JA |
2530 | break; |
2531 | ||
2532 | default: | |
b72432fd | 2533 | command_error ("execute_connection", CMDERR_BADCONN, command->value.Connection->connector, 0); |
ccc6cda3 JA |
2534 | jump_to_top_level (DISCARD); |
2535 | exec_result = EXECUTION_FAILURE; | |
726f6388 JA |
2536 | } |
2537 | ||
ccc6cda3 | 2538 | return exec_result; |
726f6388 JA |
2539 | } |
2540 | ||
bb70624e JA |
2541 | #define REAP() \ |
2542 | do \ | |
2543 | { \ | |
2544 | if (!interactive_shell) \ | |
2545 | reap_dead_jobs (); \ | |
2546 | } \ | |
2547 | while (0) | |
726f6388 JA |
2548 | |
2549 | /* Execute a FOR command. The syntax is: FOR word_desc IN word_list; | |
2550 | DO command; DONE */ | |
ccc6cda3 | 2551 | static int |
726f6388 JA |
2552 | execute_for_command (for_command) |
2553 | FOR_COM *for_command; | |
2554 | { | |
726f6388 | 2555 | register WORD_LIST *releaser, *list; |
ccc6cda3 | 2556 | SHELL_VAR *v; |
726f6388 | 2557 | char *identifier; |
d3a24ed2 | 2558 | int retval, save_line_number; |
ccc6cda3 | 2559 | #if 0 |
726f6388 | 2560 | SHELL_VAR *old_value = (SHELL_VAR *)NULL; /* Remember the old value of x. */ |
ccc6cda3 | 2561 | #endif |
726f6388 | 2562 | |
d3a24ed2 | 2563 | save_line_number = line_number; |
726f6388 | 2564 | if (check_identifier (for_command->name, 1) == 0) |
ccc6cda3 JA |
2565 | { |
2566 | if (posixly_correct && interactive_shell == 0) | |
cce855bc | 2567 | { |
f2f9854d | 2568 | last_command_exit_value = EX_BADUSAGE; |
5e13499c | 2569 | jump_to_top_level (ERREXIT); |
cce855bc | 2570 | } |
ccc6cda3 JA |
2571 | return (EXECUTION_FAILURE); |
2572 | } | |
726f6388 JA |
2573 | |
2574 | loop_level++; | |
2575 | identifier = for_command->name->word; | |
2576 | ||
2577 | list = releaser = expand_words_no_vars (for_command->map_list); | |
2578 | ||
2579 | begin_unwind_frame ("for"); | |
2580 | add_unwind_protect (dispose_words, releaser); | |
2581 | ||
ccc6cda3 | 2582 | #if 0 |
726f6388 JA |
2583 | if (lexical_scoping) |
2584 | { | |
2585 | old_value = copy_variable (find_variable (identifier)); | |
2586 | if (old_value) | |
2587 | add_unwind_protect (dispose_variable, old_value); | |
2588 | } | |
ccc6cda3 | 2589 | #endif |
726f6388 JA |
2590 | |
2591 | if (for_command->flags & CMD_IGNORE_RETURN) | |
2592 | for_command->action->flags |= CMD_IGNORE_RETURN; | |
2593 | ||
ccc6cda3 | 2594 | for (retval = EXECUTION_SUCCESS; list; list = list->next) |
726f6388 JA |
2595 | { |
2596 | QUIT; | |
d3a24ed2 CR |
2597 | |
2598 | line_number = for_command->line; | |
2599 | ||
2600 | /* Remember what this command looks like, for debugger. */ | |
2601 | command_string_index = 0; | |
2602 | print_for_command_head (for_command); | |
2603 | ||
2604 | if (echo_command_at_execute) | |
2605 | xtrace_print_for_command_head (for_command); | |
2606 | ||
eb2bb562 CR |
2607 | /* Save this command unless it's a trap command and we're not running |
2608 | a debug trap. */ | |
d3ad40de | 2609 | if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0) |
d3a24ed2 CR |
2610 | { |
2611 | FREE (the_printed_command_except_trap); | |
2612 | the_printed_command_except_trap = savestring (the_printed_command); | |
2613 | } | |
2614 | ||
2615 | retval = run_debug_trap (); | |
2616 | #if defined (DEBUGGER) | |
2617 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
2618 | skip the command. */ | |
2619 | if (debugging_mode && retval != EXECUTION_SUCCESS) | |
2620 | continue; | |
2621 | #endif | |
2622 | ||
ccc6cda3 | 2623 | this_command_name = (char *)NULL; |
d42cb8c1 CR |
2624 | /* XXX - special ksh93 for command index variable handling */ |
2625 | v = find_variable_last_nameref (identifier); | |
2626 | if (v && nameref_p (v)) | |
2627 | { | |
2628 | v = bind_variable_value (v, list->word->word, 0); | |
2629 | } | |
2630 | else | |
2631 | v = bind_variable (identifier, list->word->word, 0); | |
28ef6c31 | 2632 | if (readonly_p (v) || noassign_p (v)) |
ccc6cda3 | 2633 | { |
d3a24ed2 | 2634 | line_number = save_line_number; |
28ef6c31 | 2635 | if (readonly_p (v) && interactive_shell == 0 && posixly_correct) |
ccc6cda3 JA |
2636 | { |
2637 | last_command_exit_value = EXECUTION_FAILURE; | |
2638 | jump_to_top_level (FORCE_EOF); | |
2639 | } | |
2640 | else | |
2641 | { | |
d3a24ed2 CR |
2642 | dispose_words (releaser); |
2643 | discard_unwind_frame ("for"); | |
d166f048 | 2644 | loop_level--; |
ccc6cda3 JA |
2645 | return (EXECUTION_FAILURE); |
2646 | } | |
2647 | } | |
2648 | retval = execute_command (for_command->action); | |
726f6388 JA |
2649 | REAP (); |
2650 | QUIT; | |
2651 | ||
2652 | if (breaking) | |
2653 | { | |
ccc6cda3 | 2654 | breaking--; |
726f6388 JA |
2655 | break; |
2656 | } | |
2657 | ||
2658 | if (continuing) | |
2659 | { | |
2660 | continuing--; | |
2661 | if (continuing) | |
2662 | break; | |
2663 | } | |
726f6388 JA |
2664 | } |
2665 | ||
2666 | loop_level--; | |
d3a24ed2 | 2667 | line_number = save_line_number; |
726f6388 | 2668 | |
ccc6cda3 | 2669 | #if 0 |
726f6388 JA |
2670 | if (lexical_scoping) |
2671 | { | |
2672 | if (!old_value) | |
7117c2d2 | 2673 | unbind_variable (identifier); |
726f6388 JA |
2674 | else |
2675 | { | |
2676 | SHELL_VAR *new_value; | |
2677 | ||
d11b8b46 | 2678 | new_value = bind_variable (identifier, value_cell(old_value), 0); |
726f6388 JA |
2679 | new_value->attributes = old_value->attributes; |
2680 | dispose_variable (old_value); | |
2681 | } | |
2682 | } | |
ccc6cda3 | 2683 | #endif |
726f6388 JA |
2684 | |
2685 | dispose_words (releaser); | |
2686 | discard_unwind_frame ("for"); | |
2687 | return (retval); | |
2688 | } | |
2689 | ||
bb70624e JA |
2690 | #if defined (ARITH_FOR_COMMAND) |
2691 | /* Execute an arithmetic for command. The syntax is | |
2692 | ||
2693 | for (( init ; step ; test )) | |
2694 | do | |
2695 | body | |
2696 | done | |
2697 | ||
2698 | The execution should be exactly equivalent to | |
2699 | ||
2700 | eval \(\( init \)\) | |
2701 | while eval \(\( test \)\) ; do | |
2702 | body; | |
2703 | eval \(\( step \)\) | |
2704 | done | |
2705 | */ | |
7117c2d2 | 2706 | static intmax_t |
bb70624e JA |
2707 | eval_arith_for_expr (l, okp) |
2708 | WORD_LIST *l; | |
2709 | int *okp; | |
2710 | { | |
2711 | WORD_LIST *new; | |
7117c2d2 | 2712 | intmax_t expresult; |
d3a24ed2 | 2713 | int r; |
bb70624e JA |
2714 | |
2715 | new = expand_words_no_vars (l); | |
f73dda09 JA |
2716 | if (new) |
2717 | { | |
2718 | if (echo_command_at_execute) | |
2719 | xtrace_print_arith_cmd (new); | |
7117c2d2 | 2720 | this_command_name = "(("; /* )) for expression error messages */ |
d3a24ed2 CR |
2721 | |
2722 | command_string_index = 0; | |
2723 | print_arith_command (new); | |
eb2bb562 CR |
2724 | if (signal_in_progress (DEBUG_TRAP) == 0) |
2725 | { | |
2726 | FREE (the_printed_command_except_trap); | |
2727 | the_printed_command_except_trap = savestring (the_printed_command); | |
2728 | } | |
d3a24ed2 CR |
2729 | |
2730 | r = run_debug_trap (); | |
2731 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
2732 | skip the command. */ | |
2733 | #if defined (DEBUGGER) | |
2734 | if (debugging_mode == 0 || r == EXECUTION_SUCCESS) | |
2735 | expresult = evalexp (new->word->word, okp); | |
2736 | else | |
2737 | { | |
2738 | expresult = 0; | |
2739 | if (okp) | |
2740 | *okp = 1; | |
2741 | } | |
2742 | #else | |
f73dda09 | 2743 | expresult = evalexp (new->word->word, okp); |
d3a24ed2 | 2744 | #endif |
f73dda09 JA |
2745 | dispose_words (new); |
2746 | } | |
2747 | else | |
2748 | { | |
2749 | expresult = 0; | |
2750 | if (okp) | |
2751 | *okp = 1; | |
2752 | } | |
bb70624e JA |
2753 | return (expresult); |
2754 | } | |
2755 | ||
2756 | static int | |
2757 | execute_arith_for_command (arith_for_command) | |
2758 | ARITH_FOR_COM *arith_for_command; | |
2759 | { | |
7117c2d2 JA |
2760 | intmax_t expresult; |
2761 | int expok, body_status, arith_lineno, save_lineno; | |
bb70624e JA |
2762 | |
2763 | body_status = EXECUTION_SUCCESS; | |
2764 | loop_level++; | |
d3a24ed2 | 2765 | save_lineno = line_number; |
bb70624e JA |
2766 | |
2767 | if (arith_for_command->flags & CMD_IGNORE_RETURN) | |
2768 | arith_for_command->action->flags |= CMD_IGNORE_RETURN; | |
2769 | ||
2770 | this_command_name = "(("; /* )) for expression error messages */ | |
2771 | ||
7117c2d2 JA |
2772 | /* save the starting line number of the command so we can reset |
2773 | line_number before executing each expression -- for $LINENO | |
2774 | and the DEBUG trap. */ | |
d3a24ed2 | 2775 | line_number = arith_lineno = arith_for_command->line; |
7117c2d2 | 2776 | if (variable_context && interactive_shell) |
d3a24ed2 | 2777 | line_number -= function_line_number; |
bb70624e JA |
2778 | |
2779 | /* Evaluate the initialization expression. */ | |
2780 | expresult = eval_arith_for_expr (arith_for_command->init, &expok); | |
2781 | if (expok == 0) | |
d3a24ed2 CR |
2782 | { |
2783 | line_number = save_lineno; | |
2784 | return (EXECUTION_FAILURE); | |
2785 | } | |
bb70624e JA |
2786 | |
2787 | while (1) | |
2788 | { | |
2789 | /* Evaluate the test expression. */ | |
7117c2d2 | 2790 | line_number = arith_lineno; |
bb70624e | 2791 | expresult = eval_arith_for_expr (arith_for_command->test, &expok); |
7117c2d2 JA |
2792 | line_number = save_lineno; |
2793 | ||
bb70624e JA |
2794 | if (expok == 0) |
2795 | { | |
2796 | body_status = EXECUTION_FAILURE; | |
2797 | break; | |
2798 | } | |
2799 | REAP (); | |
2800 | if (expresult == 0) | |
28ef6c31 | 2801 | break; |
bb70624e JA |
2802 | |
2803 | /* Execute the body of the arithmetic for command. */ | |
2804 | QUIT; | |
2805 | body_status = execute_command (arith_for_command->action); | |
2806 | QUIT; | |
2807 | ||
2808 | /* Handle any `break' or `continue' commands executed by the body. */ | |
2809 | if (breaking) | |
2810 | { | |
2811 | breaking--; | |
2812 | break; | |
2813 | } | |
2814 | ||
2815 | if (continuing) | |
2816 | { | |
2817 | continuing--; | |
2818 | if (continuing) | |
2819 | break; | |
2820 | } | |
2821 | ||
2822 | /* Evaluate the step expression. */ | |
7117c2d2 | 2823 | line_number = arith_lineno; |
bb70624e | 2824 | expresult = eval_arith_for_expr (arith_for_command->step, &expok); |
7117c2d2 JA |
2825 | line_number = save_lineno; |
2826 | ||
bb70624e JA |
2827 | if (expok == 0) |
2828 | { | |
2829 | body_status = EXECUTION_FAILURE; | |
2830 | break; | |
2831 | } | |
2832 | } | |
2833 | ||
2834 | loop_level--; | |
d3a24ed2 CR |
2835 | line_number = save_lineno; |
2836 | ||
bb70624e JA |
2837 | return (body_status); |
2838 | } | |
2839 | #endif | |
2840 | ||
726f6388 JA |
2841 | #if defined (SELECT_COMMAND) |
2842 | static int LINES, COLS, tabsize; | |
2843 | ||
2844 | #define RP_SPACE ") " | |
2845 | #define RP_SPACE_LEN 2 | |
2846 | ||
2847 | /* XXX - does not handle numbers > 1000000 at all. */ | |
2848 | #define NUMBER_LEN(s) \ | |
2849 | ((s < 10) ? 1 \ | |
2850 | : ((s < 100) ? 2 \ | |
2851 | : ((s < 1000) ? 3 \ | |
2852 | : ((s < 10000) ? 4 \ | |
2853 | : ((s < 100000) ? 5 \ | |
2854 | : 6))))) | |
2855 | ||
220537f2 CR |
2856 | static int |
2857 | displen (s) | |
2858 | const char *s; | |
2859 | { | |
2860 | #if defined (HANDLE_MULTIBYTE) | |
2861 | wchar_t *wcstr; | |
2862 | size_t wclen, slen; | |
2863 | ||
2864 | wcstr = 0; | |
2865 | slen = mbstowcs (wcstr, s, 0); | |
2866 | if (slen == -1) | |
2867 | slen = 0; | |
2868 | wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (slen + 1)); | |
2869 | mbstowcs (wcstr, s, slen + 1); | |
2870 | wclen = wcswidth (wcstr, slen); | |
2871 | free (wcstr); | |
2872 | return ((int)wclen); | |
2873 | #else | |
2874 | return (STRLEN (s)); | |
2875 | #endif | |
2876 | } | |
2877 | ||
726f6388 JA |
2878 | static int |
2879 | print_index_and_element (len, ind, list) | |
2880 | int len, ind; | |
2881 | WORD_LIST *list; | |
2882 | { | |
2883 | register WORD_LIST *l; | |
2884 | register int i; | |
2885 | ||
2886 | if (list == 0) | |
2887 | return (0); | |
ccc6cda3 JA |
2888 | for (i = ind, l = list; l && --i; l = l->next) |
2889 | ; | |
631b20c6 CR |
2890 | if (l == 0) /* don't think this can happen */ |
2891 | return (0); | |
726f6388 | 2892 | fprintf (stderr, "%*d%s%s", len, ind, RP_SPACE, l->word->word); |
220537f2 | 2893 | return (displen (l->word->word)); |
726f6388 JA |
2894 | } |
2895 | ||
2896 | static void | |
2897 | indent (from, to) | |
2898 | int from, to; | |
2899 | { | |
2900 | while (from < to) | |
2901 | { | |
2902 | if ((to / tabsize) > (from / tabsize)) | |
2903 | { | |
2904 | putc ('\t', stderr); | |
2905 | from += tabsize - from % tabsize; | |
2906 | } | |
2907 | else | |
2908 | { | |
2909 | putc (' ', stderr); | |
2910 | from++; | |
2911 | } | |
2912 | } | |
2913 | } | |
2914 | ||
2915 | static void | |
2916 | print_select_list (list, list_len, max_elem_len, indices_len) | |
2917 | WORD_LIST *list; | |
2918 | int list_len, max_elem_len, indices_len; | |
2919 | { | |
2920 | int ind, row, elem_len, pos, cols, rows; | |
2921 | int first_column_indices_len, other_indices_len; | |
2922 | ||
2923 | if (list == 0) | |
2924 | { | |
2925 | putc ('\n', stderr); | |
2926 | return; | |
2927 | } | |
2928 | ||
ccc6cda3 | 2929 | cols = max_elem_len ? COLS / max_elem_len : 1; |
726f6388 JA |
2930 | if (cols == 0) |
2931 | cols = 1; | |
2932 | rows = list_len ? list_len / cols + (list_len % cols != 0) : 1; | |
2933 | cols = list_len ? list_len / rows + (list_len % rows != 0) : 1; | |
2934 | ||
2935 | if (rows == 1) | |
2936 | { | |
2937 | rows = cols; | |
2938 | cols = 1; | |
2939 | } | |
2940 | ||
2941 | first_column_indices_len = NUMBER_LEN (rows); | |
2942 | other_indices_len = indices_len; | |
2943 | ||
2944 | for (row = 0; row < rows; row++) | |
2945 | { | |
2946 | ind = row; | |
2947 | pos = 0; | |
2948 | while (1) | |
2949 | { | |
2950 | indices_len = (pos == 0) ? first_column_indices_len : other_indices_len; | |
2951 | elem_len = print_index_and_element (indices_len, ind + 1, list); | |
2952 | elem_len += indices_len + RP_SPACE_LEN; | |
2953 | ind += rows; | |
2954 | if (ind >= list_len) | |
2955 | break; | |
2956 | indent (pos + elem_len, pos + max_elem_len); | |
2957 | pos += max_elem_len; | |
2958 | } | |
2959 | putc ('\n', stderr); | |
2960 | } | |
2961 | } | |
2962 | ||
2963 | /* Print the elements of LIST, one per line, preceded by an index from 1 to | |
2964 | LIST_LEN. Then display PROMPT and wait for the user to enter a number. | |
2965 | If the number is between 1 and LIST_LEN, return that selection. If EOF | |
e8ce775d JA |
2966 | is read, return a null string. If a blank line is entered, or an invalid |
2967 | number is entered, the loop is executed again. */ | |
726f6388 | 2968 | static char * |
7117c2d2 | 2969 | select_query (list, list_len, prompt, print_menu) |
726f6388 JA |
2970 | WORD_LIST *list; |
2971 | int list_len; | |
2972 | char *prompt; | |
7117c2d2 | 2973 | int print_menu; |
726f6388 | 2974 | { |
e8ce775d | 2975 | int max_elem_len, indices_len, len; |
7117c2d2 | 2976 | intmax_t reply; |
726f6388 JA |
2977 | WORD_LIST *l; |
2978 | char *repl_string, *t; | |
2979 | ||
3d8cce26 | 2980 | #if 0 |
726f6388 JA |
2981 | t = get_string_value ("LINES"); |
2982 | LINES = (t && *t) ? atoi (t) : 24; | |
3d8cce26 | 2983 | #endif |
726f6388 JA |
2984 | t = get_string_value ("COLUMNS"); |
2985 | COLS = (t && *t) ? atoi (t) : 80; | |
2986 | ||
2987 | #if 0 | |
2988 | t = get_string_value ("TABSIZE"); | |
2989 | tabsize = (t && *t) ? atoi (t) : 8; | |
2990 | if (tabsize <= 0) | |
2991 | tabsize = 8; | |
2992 | #else | |
2993 | tabsize = 8; | |
2994 | #endif | |
2995 | ||
2996 | max_elem_len = 0; | |
2997 | for (l = list; l; l = l->next) | |
2998 | { | |
220537f2 | 2999 | len = displen (l->word->word); |
726f6388 | 3000 | if (len > max_elem_len) |
cce855bc | 3001 | max_elem_len = len; |
726f6388 JA |
3002 | } |
3003 | indices_len = NUMBER_LEN (list_len); | |
3004 | max_elem_len += indices_len + RP_SPACE_LEN + 2; | |
3005 | ||
3006 | while (1) | |
3007 | { | |
7117c2d2 JA |
3008 | if (print_menu) |
3009 | print_select_list (list, list_len, max_elem_len, indices_len); | |
ccc6cda3 JA |
3010 | fprintf (stderr, "%s", prompt); |
3011 | fflush (stderr); | |
726f6388 JA |
3012 | QUIT; |
3013 | ||
deeba0d2 | 3014 | if (read_builtin ((WORD_LIST *)NULL) != EXECUTION_SUCCESS) |
726f6388 JA |
3015 | { |
3016 | putchar ('\n'); | |
3017 | return ((char *)NULL); | |
3018 | } | |
3019 | repl_string = get_string_value ("REPLY"); | |
3020 | if (*repl_string == 0) | |
7117c2d2 JA |
3021 | { |
3022 | print_menu = 1; | |
3023 | continue; | |
3024 | } | |
e8ce775d | 3025 | if (legal_number (repl_string, &reply) == 0) |
cce855bc | 3026 | return ""; |
726f6388 JA |
3027 | if (reply < 1 || reply > list_len) |
3028 | return ""; | |
3029 | ||
ccc6cda3 JA |
3030 | for (l = list; l && --reply; l = l->next) |
3031 | ; | |
631b20c6 | 3032 | return (l->word->word); /* XXX - can't be null? */ |
726f6388 JA |
3033 | } |
3034 | } | |
3035 | ||
3036 | /* Execute a SELECT command. The syntax is: | |
3037 | SELECT word IN list DO command_list DONE | |
3038 | Only `break' or `return' in command_list will terminate | |
3039 | the command. */ | |
ccc6cda3 | 3040 | static int |
726f6388 JA |
3041 | execute_select_command (select_command) |
3042 | SELECT_COM *select_command; | |
3043 | { | |
3044 | WORD_LIST *releaser, *list; | |
ccc6cda3 | 3045 | SHELL_VAR *v; |
726f6388 | 3046 | char *identifier, *ps3_prompt, *selection; |
d3a24ed2 | 3047 | int retval, list_len, show_menu, save_line_number; |
7117c2d2 | 3048 | |
726f6388 JA |
3049 | if (check_identifier (select_command->name, 1) == 0) |
3050 | return (EXECUTION_FAILURE); | |
3051 | ||
d3a24ed2 CR |
3052 | save_line_number = line_number; |
3053 | line_number = select_command->line; | |
3054 | ||
3055 | command_string_index = 0; | |
3056 | print_select_command_head (select_command); | |
3057 | ||
3058 | if (echo_command_at_execute) | |
3059 | xtrace_print_select_command_head (select_command); | |
3060 | ||
d3ad40de | 3061 | #if 0 |
eb2bb562 | 3062 | if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0))) |
d3ad40de CR |
3063 | #else |
3064 | if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0) | |
3065 | #endif | |
eb2bb562 CR |
3066 | { |
3067 | FREE (the_printed_command_except_trap); | |
3068 | the_printed_command_except_trap = savestring (the_printed_command); | |
3069 | } | |
d3a24ed2 CR |
3070 | |
3071 | retval = run_debug_trap (); | |
3072 | #if defined (DEBUGGER) | |
3073 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
3074 | skip the command. */ | |
3075 | if (debugging_mode && retval != EXECUTION_SUCCESS) | |
3076 | return (EXECUTION_SUCCESS); | |
3077 | #endif | |
3078 | ||
726f6388 JA |
3079 | loop_level++; |
3080 | identifier = select_command->name->word; | |
3081 | ||
3082 | /* command and arithmetic substitution, parameter and variable expansion, | |
3083 | word splitting, pathname expansion, and quote removal. */ | |
3084 | list = releaser = expand_words_no_vars (select_command->map_list); | |
3085 | list_len = list_length (list); | |
3086 | if (list == 0 || list_len == 0) | |
3087 | { | |
3088 | if (list) | |
3089 | dispose_words (list); | |
d3a24ed2 | 3090 | line_number = save_line_number; |
726f6388 JA |
3091 | return (EXECUTION_SUCCESS); |
3092 | } | |
3093 | ||
3094 | begin_unwind_frame ("select"); | |
3095 | add_unwind_protect (dispose_words, releaser); | |
3096 | ||
726f6388 JA |
3097 | if (select_command->flags & CMD_IGNORE_RETURN) |
3098 | select_command->action->flags |= CMD_IGNORE_RETURN; | |
3099 | ||
ccc6cda3 | 3100 | retval = EXECUTION_SUCCESS; |
7117c2d2 | 3101 | show_menu = 1; |
ccc6cda3 | 3102 | |
726f6388 JA |
3103 | while (1) |
3104 | { | |
d3a24ed2 | 3105 | line_number = select_command->line; |
726f6388 | 3106 | ps3_prompt = get_string_value ("PS3"); |
ccc6cda3 | 3107 | if (ps3_prompt == 0) |
726f6388 JA |
3108 | ps3_prompt = "#? "; |
3109 | ||
3110 | QUIT; | |
7117c2d2 | 3111 | selection = select_query (list, list_len, ps3_prompt, show_menu); |
726f6388 JA |
3112 | QUIT; |
3113 | if (selection == 0) | |
7117c2d2 JA |
3114 | { |
3115 | /* select_query returns EXECUTION_FAILURE if the read builtin | |
3116 | fails, so we want to return failure in this case. */ | |
3117 | retval = EXECUTION_FAILURE; | |
3118 | break; | |
3119 | } | |
ccc6cda3 | 3120 | |
d11b8b46 | 3121 | v = bind_variable (identifier, selection, 0); |
28ef6c31 | 3122 | if (readonly_p (v) || noassign_p (v)) |
ccc6cda3 | 3123 | { |
28ef6c31 | 3124 | if (readonly_p (v) && interactive_shell == 0 && posixly_correct) |
ccc6cda3 JA |
3125 | { |
3126 | last_command_exit_value = EXECUTION_FAILURE; | |
3127 | jump_to_top_level (FORCE_EOF); | |
3128 | } | |
3129 | else | |
3130 | { | |
d3a24ed2 CR |
3131 | dispose_words (releaser); |
3132 | discard_unwind_frame ("select"); | |
3133 | loop_level--; | |
3134 | line_number = save_line_number; | |
ccc6cda3 JA |
3135 | return (EXECUTION_FAILURE); |
3136 | } | |
3137 | } | |
726f6388 | 3138 | |
f73dda09 | 3139 | retval = execute_command (select_command->action); |
726f6388 JA |
3140 | |
3141 | REAP (); | |
3142 | QUIT; | |
3143 | ||
3144 | if (breaking) | |
3145 | { | |
3146 | breaking--; | |
3147 | break; | |
3148 | } | |
bb70624e JA |
3149 | |
3150 | if (continuing) | |
3151 | { | |
3152 | continuing--; | |
3153 | if (continuing) | |
3154 | break; | |
3155 | } | |
7117c2d2 JA |
3156 | |
3157 | #if defined (KSH_COMPATIBLE_SELECT) | |
3158 | show_menu = 0; | |
3159 | selection = get_string_value ("REPLY"); | |
3160 | if (selection && *selection == '\0') | |
3161 | show_menu = 1; | |
3162 | #endif | |
726f6388 JA |
3163 | } |
3164 | ||
3165 | loop_level--; | |
d3a24ed2 | 3166 | line_number = save_line_number; |
726f6388 | 3167 | |
d3a24ed2 CR |
3168 | dispose_words (releaser); |
3169 | discard_unwind_frame ("select"); | |
726f6388 JA |
3170 | return (retval); |
3171 | } | |
3172 | #endif /* SELECT_COMMAND */ | |
3173 | ||
3174 | /* Execute a CASE command. The syntax is: CASE word_desc IN pattern_list ESAC. | |
3175 | The pattern_list is a linked list of pattern clauses; each clause contains | |
3176 | some patterns to compare word_desc against, and an associated command to | |
3177 | execute. */ | |
ccc6cda3 | 3178 | static int |
726f6388 JA |
3179 | execute_case_command (case_command) |
3180 | CASE_COM *case_command; | |
3181 | { | |
3182 | register WORD_LIST *list; | |
ccc6cda3 | 3183 | WORD_LIST *wlist, *es; |
726f6388 | 3184 | PATTERN_LIST *clauses; |
ccc6cda3 | 3185 | char *word, *pattern; |
d3a24ed2 CR |
3186 | int retval, match, ignore_return, save_line_number; |
3187 | ||
3188 | save_line_number = line_number; | |
3189 | line_number = case_command->line; | |
3190 | ||
3191 | command_string_index = 0; | |
3192 | print_case_command_head (case_command); | |
3193 | ||
3194 | if (echo_command_at_execute) | |
3195 | xtrace_print_case_command_head (case_command); | |
3196 | ||
d3ad40de | 3197 | #if 0 |
5e4a95c7 | 3198 | if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0))) |
d3ad40de CR |
3199 | #else |
3200 | if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0) | |
3201 | #endif | |
d3a24ed2 CR |
3202 | { |
3203 | FREE (the_printed_command_except_trap); | |
3204 | the_printed_command_except_trap = savestring (the_printed_command); | |
3205 | } | |
3206 | ||
3207 | retval = run_debug_trap(); | |
3208 | #if defined (DEBUGGER) | |
3209 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
3210 | skip the command. */ | |
3211 | if (debugging_mode && retval != EXECUTION_SUCCESS) | |
3212 | { | |
3213 | line_number = save_line_number; | |
3214 | return (EXECUTION_SUCCESS); | |
3215 | } | |
3216 | #endif | |
726f6388 | 3217 | |
28ef6c31 | 3218 | wlist = expand_word_unsplit (case_command->word, 0); |
ccc6cda3 JA |
3219 | word = wlist ? string_list (wlist) : savestring (""); |
3220 | dispose_words (wlist); | |
3221 | ||
726f6388 | 3222 | retval = EXECUTION_SUCCESS; |
ccc6cda3 | 3223 | ignore_return = case_command->flags & CMD_IGNORE_RETURN; |
726f6388 JA |
3224 | |
3225 | begin_unwind_frame ("case"); | |
726f6388 JA |
3226 | add_unwind_protect ((Function *)xfree, word); |
3227 | ||
ccc6cda3 JA |
3228 | #define EXIT_CASE() goto exit_case_command |
3229 | ||
3230 | for (clauses = case_command->clauses; clauses; clauses = clauses->next) | |
726f6388 JA |
3231 | { |
3232 | QUIT; | |
ccc6cda3 | 3233 | for (list = clauses->patterns; list; list = list->next) |
726f6388 | 3234 | { |
726f6388 JA |
3235 | es = expand_word_leave_quoted (list->word, 0); |
3236 | ||
3237 | if (es && es->word && es->word->word && *(es->word->word)) | |
cce855bc | 3238 | pattern = quote_string_for_globbing (es->word->word, QGLOB_CVTNULL); |
726f6388 | 3239 | else |
ccc6cda3 | 3240 | { |
f73dda09 | 3241 | pattern = (char *)xmalloc (1); |
ccc6cda3 JA |
3242 | pattern[0] = '\0'; |
3243 | } | |
726f6388 JA |
3244 | |
3245 | /* Since the pattern does not undergo quote removal (as per | |
f73dda09 | 3246 | Posix.2, section 3.9.4.3), the strmatch () call must be able |
726f6388 | 3247 | to recognize backslashes as escape characters. */ |
2206f89a | 3248 | match = strmatch (pattern, word, FNMATCH_EXTFLAG|FNMATCH_IGNCASE) != FNM_NOMATCH; |
726f6388 JA |
3249 | free (pattern); |
3250 | ||
3251 | dispose_words (es); | |
3252 | ||
3253 | if (match) | |
3254 | { | |
8943768b CR |
3255 | do |
3256 | { | |
3257 | if (clauses->action && ignore_return) | |
3258 | clauses->action->flags |= CMD_IGNORE_RETURN; | |
3259 | retval = execute_command (clauses->action); | |
3260 | } | |
3261 | while ((clauses->flags & CASEPAT_FALLTHROUGH) && (clauses = clauses->next)); | |
1231ac47 | 3262 | if (clauses == 0 || (clauses->flags & CASEPAT_TESTNEXT) == 0) |
8943768b CR |
3263 | EXIT_CASE (); |
3264 | else | |
3265 | break; | |
726f6388 JA |
3266 | } |
3267 | ||
726f6388 JA |
3268 | QUIT; |
3269 | } | |
726f6388 JA |
3270 | } |
3271 | ||
ccc6cda3 JA |
3272 | exit_case_command: |
3273 | free (word); | |
726f6388 | 3274 | discard_unwind_frame ("case"); |
d3a24ed2 | 3275 | line_number = save_line_number; |
726f6388 JA |
3276 | return (retval); |
3277 | } | |
3278 | ||
3279 | #define CMD_WHILE 0 | |
3280 | #define CMD_UNTIL 1 | |
3281 | ||
3282 | /* The WHILE command. Syntax: WHILE test DO action; DONE. | |
3283 | Repeatedly execute action while executing test produces | |
3284 | EXECUTION_SUCCESS. */ | |
ccc6cda3 | 3285 | static int |
726f6388 JA |
3286 | execute_while_command (while_command) |
3287 | WHILE_COM *while_command; | |
3288 | { | |
3289 | return (execute_while_or_until (while_command, CMD_WHILE)); | |
3290 | } | |
3291 | ||
3292 | /* UNTIL is just like WHILE except that the test result is negated. */ | |
ccc6cda3 | 3293 | static int |
726f6388 JA |
3294 | execute_until_command (while_command) |
3295 | WHILE_COM *while_command; | |
3296 | { | |
3297 | return (execute_while_or_until (while_command, CMD_UNTIL)); | |
3298 | } | |
3299 | ||
3300 | /* The body for both while and until. The only difference between the | |
3301 | two is that the test value is treated differently. TYPE is | |
3302 | CMD_WHILE or CMD_UNTIL. The return value for both commands should | |
3303 | be EXECUTION_SUCCESS if no commands in the body are executed, and | |
3304 | the status of the last command executed in the body otherwise. */ | |
ccc6cda3 | 3305 | static int |
726f6388 JA |
3306 | execute_while_or_until (while_command, type) |
3307 | WHILE_COM *while_command; | |
3308 | int type; | |
3309 | { | |
3310 | int return_value, body_status; | |
3311 | ||
3312 | body_status = EXECUTION_SUCCESS; | |
3313 | loop_level++; | |
3314 | ||
3315 | while_command->test->flags |= CMD_IGNORE_RETURN; | |
3316 | if (while_command->flags & CMD_IGNORE_RETURN) | |
3317 | while_command->action->flags |= CMD_IGNORE_RETURN; | |
3318 | ||
3319 | while (1) | |
3320 | { | |
3321 | return_value = execute_command (while_command->test); | |
3322 | REAP (); | |
3323 | ||
f73dda09 JA |
3324 | /* Need to handle `break' in the test when we would break out of the |
3325 | loop. The job control code will set `breaking' to loop_level | |
3326 | when a job in a loop is stopped with SIGTSTP. If the stopped job | |
3327 | is in the loop test, `breaking' will not be reset unless we do | |
3328 | this, and the shell will cease to execute commands. */ | |
726f6388 | 3329 | if (type == CMD_WHILE && return_value != EXECUTION_SUCCESS) |
f73dda09 JA |
3330 | { |
3331 | if (breaking) | |
3332 | breaking--; | |
3333 | break; | |
3334 | } | |
726f6388 | 3335 | if (type == CMD_UNTIL && return_value == EXECUTION_SUCCESS) |
f73dda09 JA |
3336 | { |
3337 | if (breaking) | |
3338 | breaking--; | |
3339 | break; | |
3340 | } | |
726f6388 JA |
3341 | |
3342 | QUIT; | |
3343 | body_status = execute_command (while_command->action); | |
3344 | QUIT; | |
3345 | ||
3346 | if (breaking) | |
3347 | { | |
3348 | breaking--; | |
3349 | break; | |
3350 | } | |
3351 | ||
3352 | if (continuing) | |
3353 | { | |
3354 | continuing--; | |
3355 | if (continuing) | |
3356 | break; | |
3357 | } | |
3358 | } | |
3359 | loop_level--; | |
3360 | ||
3361 | return (body_status); | |
3362 | } | |
3363 | ||
3364 | /* IF test THEN command [ELSE command]. | |
3365 | IF also allows ELIF in the place of ELSE IF, but | |
3366 | the parser makes *that* stupidity transparent. */ | |
ccc6cda3 | 3367 | static int |
726f6388 JA |
3368 | execute_if_command (if_command) |
3369 | IF_COM *if_command; | |
3370 | { | |
d3a24ed2 | 3371 | int return_value, save_line_number; |
726f6388 | 3372 | |
d3a24ed2 | 3373 | save_line_number = line_number; |
726f6388 JA |
3374 | if_command->test->flags |= CMD_IGNORE_RETURN; |
3375 | return_value = execute_command (if_command->test); | |
d3a24ed2 | 3376 | line_number = save_line_number; |
726f6388 JA |
3377 | |
3378 | if (return_value == EXECUTION_SUCCESS) | |
3379 | { | |
3380 | QUIT; | |
ccc6cda3 | 3381 | |
726f6388 | 3382 | if (if_command->true_case && (if_command->flags & CMD_IGNORE_RETURN)) |
ccc6cda3 JA |
3383 | if_command->true_case->flags |= CMD_IGNORE_RETURN; |
3384 | ||
726f6388 JA |
3385 | return (execute_command (if_command->true_case)); |
3386 | } | |
3387 | else | |
3388 | { | |
3389 | QUIT; | |
3390 | ||
3391 | if (if_command->false_case && (if_command->flags & CMD_IGNORE_RETURN)) | |
3392 | if_command->false_case->flags |= CMD_IGNORE_RETURN; | |
3393 | ||
3394 | return (execute_command (if_command->false_case)); | |
3395 | } | |
3396 | } | |
3397 | ||
cce855bc JA |
3398 | #if defined (DPAREN_ARITHMETIC) |
3399 | static int | |
3400 | execute_arith_command (arith_command) | |
3401 | ARITH_COM *arith_command; | |
3402 | { | |
d3a24ed2 | 3403 | int expok, save_line_number, retval; |
7117c2d2 | 3404 | intmax_t expresult; |
f73dda09 | 3405 | WORD_LIST *new; |
dc8fbaf9 | 3406 | char *exp; |
cce855bc | 3407 | |
f73dda09 | 3408 | expresult = 0; |
cce855bc | 3409 | |
d3a24ed2 | 3410 | save_line_number = line_number; |
b72432fd | 3411 | this_command_name = "(("; /* )) */ |
d3a24ed2 | 3412 | line_number = arith_command->line; |
cce855bc | 3413 | /* If we're in a function, update the line number information. */ |
7117c2d2 | 3414 | if (variable_context && interactive_shell) |
d3a24ed2 CR |
3415 | line_number -= function_line_number; |
3416 | ||
3417 | command_string_index = 0; | |
3418 | print_arith_command (arith_command->exp); | |
eb2bb562 CR |
3419 | |
3420 | if (signal_in_progress (DEBUG_TRAP) == 0) | |
3421 | { | |
3422 | FREE (the_printed_command_except_trap); | |
3423 | the_printed_command_except_trap = savestring (the_printed_command); | |
3424 | } | |
cce855bc | 3425 | |
7117c2d2 JA |
3426 | /* Run the debug trap before each arithmetic command, but do it after we |
3427 | update the line number information and before we expand the various | |
3428 | words in the expression. */ | |
d3a24ed2 CR |
3429 | retval = run_debug_trap (); |
3430 | #if defined (DEBUGGER) | |
3431 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
3432 | skip the command. */ | |
3433 | if (debugging_mode && retval != EXECUTION_SUCCESS) | |
3434 | { | |
3435 | line_number = save_line_number; | |
3436 | return (EXECUTION_SUCCESS); | |
3437 | } | |
3438 | #endif | |
7117c2d2 | 3439 | |
d3a24ed2 | 3440 | new = expand_words_no_vars (arith_command->exp); |
cce855bc JA |
3441 | |
3442 | /* If we're tracing, make a new word list with `((' at the front and `))' | |
3443 | at the back and print it. */ | |
3444 | if (echo_command_at_execute) | |
3445 | xtrace_print_arith_cmd (new); | |
3446 | ||
d3a24ed2 CR |
3447 | if (new) |
3448 | { | |
dc8fbaf9 CR |
3449 | exp = new->next ? string_list (new) : new->word->word; |
3450 | expresult = evalexp (exp, &expok); | |
d3a24ed2 | 3451 | line_number = save_line_number; |
dc8fbaf9 CR |
3452 | if (exp != new->word->word) |
3453 | free (exp); | |
d3a24ed2 CR |
3454 | dispose_words (new); |
3455 | } | |
3456 | else | |
3457 | { | |
3458 | expresult = 0; | |
3459 | expok = 1; | |
3460 | } | |
cce855bc JA |
3461 | |
3462 | if (expok == 0) | |
3463 | return (EXECUTION_FAILURE); | |
3464 | ||
f73dda09 | 3465 | return (expresult == 0 ? EXECUTION_FAILURE : EXECUTION_SUCCESS); |
cce855bc JA |
3466 | } |
3467 | #endif /* DPAREN_ARITHMETIC */ | |
3468 | ||
3469 | #if defined (COND_COMMAND) | |
3470 | ||
d3ad40de | 3471 | static char * const nullstr = ""; |
cce855bc | 3472 | |
631b20c6 | 3473 | /* XXX - can COND ever be NULL when this is called? */ |
cce855bc JA |
3474 | static int |
3475 | execute_cond_node (cond) | |
3476 | COND_COM *cond; | |
3477 | { | |
cd0ef727 | 3478 | int result, invert, patmatch, rmatch, mflags, ignore; |
f73dda09 | 3479 | char *arg1, *arg2; |
cce855bc JA |
3480 | |
3481 | invert = (cond->flags & CMD_INVERT_RETURN); | |
cd0ef727 CR |
3482 | ignore = (cond->flags & CMD_IGNORE_RETURN); |
3483 | if (ignore) | |
3484 | { | |
f486d0a1 CR |
3485 | if (cond->left) |
3486 | cond->left->flags |= CMD_IGNORE_RETURN; | |
3487 | if (cond->right) | |
3488 | cond->right->flags |= CMD_IGNORE_RETURN; | |
cd0ef727 CR |
3489 | } |
3490 | ||
cce855bc JA |
3491 | if (cond->type == COND_EXPR) |
3492 | result = execute_cond_node (cond->left); | |
3493 | else if (cond->type == COND_OR) | |
3494 | { | |
3495 | result = execute_cond_node (cond->left); | |
3496 | if (result != EXECUTION_SUCCESS) | |
3497 | result = execute_cond_node (cond->right); | |
3498 | } | |
3499 | else if (cond->type == COND_AND) | |
3500 | { | |
3501 | result = execute_cond_node (cond->left); | |
3502 | if (result == EXECUTION_SUCCESS) | |
3503 | result = execute_cond_node (cond->right); | |
3504 | } | |
3505 | else if (cond->type == COND_UNARY) | |
3506 | { | |
cd0ef727 CR |
3507 | if (ignore) |
3508 | comsub_ignore_return++; | |
cce855bc | 3509 | arg1 = cond_expand_word (cond->left->op, 0); |
cd0ef727 CR |
3510 | if (ignore) |
3511 | comsub_ignore_return--; | |
cce855bc JA |
3512 | if (arg1 == 0) |
3513 | arg1 = nullstr; | |
3514 | if (echo_command_at_execute) | |
3515 | xtrace_print_cond_term (cond->type, invert, cond->op, arg1, (char *)NULL); | |
3516 | result = unary_test (cond->op->word, arg1) ? EXECUTION_SUCCESS : EXECUTION_FAILURE; | |
3517 | if (arg1 != nullstr) | |
3518 | free (arg1); | |
3519 | } | |
3520 | else if (cond->type == COND_BINARY) | |
3521 | { | |
d3ad40de | 3522 | rmatch = 0; |
e3db237e CR |
3523 | patmatch = (((cond->op->word[1] == '=') && (cond->op->word[2] == '\0') && |
3524 | (cond->op->word[0] == '!' || cond->op->word[0] == '=')) || | |
b72432fd | 3525 | (cond->op->word[0] == '=' && cond->op->word[1] == '\0')); |
d3a24ed2 CR |
3526 | #if defined (COND_REGEXP) |
3527 | rmatch = (cond->op->word[0] == '=' && cond->op->word[1] == '~' && | |
3528 | cond->op->word[2] == '\0'); | |
3529 | #endif | |
cce855bc | 3530 | |
cd0ef727 CR |
3531 | if (ignore) |
3532 | comsub_ignore_return++; | |
cce855bc | 3533 | arg1 = cond_expand_word (cond->left->op, 0); |
cd0ef727 CR |
3534 | if (ignore) |
3535 | comsub_ignore_return--; | |
cce855bc JA |
3536 | if (arg1 == 0) |
3537 | arg1 = nullstr; | |
cd0ef727 CR |
3538 | if (ignore) |
3539 | comsub_ignore_return++; | |
35ee8ea0 CR |
3540 | arg2 = cond_expand_word (cond->right->op, |
3541 | (rmatch && shell_compatibility_level > 31) ? 2 : (patmatch ? 1 : 0)); | |
cd0ef727 CR |
3542 | if (ignore) |
3543 | comsub_ignore_return--; | |
cce855bc JA |
3544 | if (arg2 == 0) |
3545 | arg2 = nullstr; | |
3546 | ||
3547 | if (echo_command_at_execute) | |
3548 | xtrace_print_cond_term (cond->type, invert, cond->op, arg1, arg2); | |
3549 | ||
d3a24ed2 CR |
3550 | #if defined (COND_REGEXP) |
3551 | if (rmatch) | |
3552 | { | |
3553 | mflags = SHMAT_PWARN; | |
3554 | #if defined (ARRAY_VARS) | |
3555 | mflags |= SHMAT_SUBEXP; | |
3556 | #endif | |
3557 | ||
3558 | result = sh_regmatch (arg1, arg2, mflags); | |
3559 | } | |
3560 | else | |
3561 | #endif /* COND_REGEXP */ | |
b954488b CR |
3562 | { |
3563 | int oe; | |
3564 | oe = extended_glob; | |
3565 | extended_glob = 1; | |
a8fd3f3e | 3566 | result = binary_test (cond->op->word, arg1, arg2, TEST_PATMATCH|TEST_ARITHEXP|TEST_LOCALE) |
b954488b CR |
3567 | ? EXECUTION_SUCCESS |
3568 | : EXECUTION_FAILURE; | |
3569 | extended_glob = oe; | |
3570 | } | |
cce855bc JA |
3571 | if (arg1 != nullstr) |
3572 | free (arg1); | |
3573 | if (arg2 != nullstr) | |
3574 | free (arg2); | |
3575 | } | |
3576 | else | |
3577 | { | |
b72432fd | 3578 | command_error ("execute_cond_node", CMDERR_BADTYPE, cond->type, 0); |
cce855bc JA |
3579 | jump_to_top_level (DISCARD); |
3580 | result = EXECUTION_FAILURE; | |
3581 | } | |
3582 | ||
3583 | if (invert) | |
3584 | result = (result == EXECUTION_SUCCESS) ? EXECUTION_FAILURE : EXECUTION_SUCCESS; | |
3585 | ||
3586 | return result; | |
3587 | } | |
3588 | ||
3589 | static int | |
3590 | execute_cond_command (cond_command) | |
3591 | COND_COM *cond_command; | |
3592 | { | |
d3a24ed2 | 3593 | int retval, save_line_number; |
cce855bc | 3594 | |
d3a24ed2 CR |
3595 | retval = EXECUTION_SUCCESS; |
3596 | save_line_number = line_number; | |
cce855bc JA |
3597 | |
3598 | this_command_name = "[["; | |
d3a24ed2 | 3599 | line_number = cond_command->line; |
cce855bc | 3600 | /* If we're in a function, update the line number information. */ |
7117c2d2 | 3601 | if (variable_context && interactive_shell) |
d3a24ed2 | 3602 | line_number -= function_line_number; |
d3a24ed2 CR |
3603 | command_string_index = 0; |
3604 | print_cond_command (cond_command); | |
eb2bb562 CR |
3605 | |
3606 | if (signal_in_progress (DEBUG_TRAP) == 0) | |
3607 | { | |
3608 | FREE (the_printed_command_except_trap); | |
3609 | the_printed_command_except_trap = savestring (the_printed_command); | |
3610 | } | |
cce855bc | 3611 | |
7117c2d2 JA |
3612 | /* Run the debug trap before each conditional command, but do it after we |
3613 | update the line number information. */ | |
d3a24ed2 CR |
3614 | retval = run_debug_trap (); |
3615 | #if defined (DEBUGGER) | |
3616 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
3617 | skip the command. */ | |
3618 | if (debugging_mode && retval != EXECUTION_SUCCESS) | |
3619 | { | |
3620 | line_number = save_line_number; | |
3621 | return (EXECUTION_SUCCESS); | |
3622 | } | |
3623 | #endif | |
7117c2d2 | 3624 | |
cce855bc JA |
3625 | #if 0 |
3626 | debug_print_cond_command (cond_command); | |
3627 | #endif | |
d3a24ed2 CR |
3628 | |
3629 | last_command_exit_value = retval = execute_cond_node (cond_command); | |
3630 | line_number = save_line_number; | |
3631 | return (retval); | |
cce855bc JA |
3632 | } |
3633 | #endif /* COND_COMMAND */ | |
3634 | ||
726f6388 JA |
3635 | static void |
3636 | bind_lastarg (arg) | |
3637 | char *arg; | |
3638 | { | |
3639 | SHELL_VAR *var; | |
3640 | ||
ccc6cda3 | 3641 | if (arg == 0) |
726f6388 | 3642 | arg = ""; |
d11b8b46 | 3643 | var = bind_variable ("_", arg, 0); |
bb70624e | 3644 | VUNSETATTR (var, att_exported); |
726f6388 JA |
3645 | } |
3646 | ||
ccc6cda3 JA |
3647 | /* Execute a null command. Fork a subshell if the command uses pipes or is |
3648 | to be run asynchronously. This handles all the side effects that are | |
3649 | supposed to take place. */ | |
3650 | static int | |
43df7bbb | 3651 | execute_null_command (redirects, pipe_in, pipe_out, async) |
ccc6cda3 | 3652 | REDIRECT *redirects; |
bb70624e | 3653 | int pipe_in, pipe_out, async; |
ccc6cda3 | 3654 | { |
d3a24ed2 | 3655 | int r; |
a8fd3f3e CR |
3656 | int forcefork; |
3657 | REDIRECT *rd; | |
d3a24ed2 | 3658 | |
a8fd3f3e CR |
3659 | for (forcefork = 0, rd = redirects; rd; rd = rd->next) |
3660 | forcefork += rd->rflags & REDIR_VARASSIGN; | |
3661 | ||
3662 | if (forcefork || pipe_in != NO_PIPE || pipe_out != NO_PIPE || async) | |
ccc6cda3 JA |
3663 | { |
3664 | /* We have a null command, but we really want a subshell to take | |
3665 | care of it. Just fork, do piping and redirections, and exit. */ | |
3666 | if (make_child ((char *)NULL, async) == 0) | |
3667 | { | |
3668 | /* Cancel traps, in trap.c. */ | |
3669 | restore_original_signals (); /* XXX */ | |
3670 | ||
3671 | do_piping (pipe_in, pipe_out); | |
3672 | ||
09767ff0 | 3673 | #if defined (COPROCESS_SUPPORT) |
8e1a6eaa | 3674 | coproc_closeall (); |
09767ff0 CR |
3675 | #endif |
3676 | ||
d3ad40de CR |
3677 | subshell_environment = 0; |
3678 | if (async) | |
3679 | subshell_environment |= SUBSHELL_ASYNC; | |
3680 | if (pipe_in != NO_PIPE || pipe_out != NO_PIPE) | |
3681 | subshell_environment |= SUBSHELL_PIPE; | |
ccc6cda3 | 3682 | |
d3a24ed2 | 3683 | if (do_redirections (redirects, RX_ACTIVE) == 0) |
ccc6cda3 JA |
3684 | exit (EXECUTION_SUCCESS); |
3685 | else | |
3686 | exit (EXECUTION_FAILURE); | |
3687 | } | |
3688 | else | |
3689 | { | |
3690 | close_pipes (pipe_in, pipe_out); | |
3691 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
3692 | unlink_fifo_list (); | |
3693 | #endif | |
3694 | return (EXECUTION_SUCCESS); | |
3695 | } | |
3696 | } | |
3697 | else | |
3698 | { | |
3699 | /* Even if there aren't any command names, pretend to do the | |
3700 | redirections that are specified. The user expects the side | |
3701 | effects to take place. If the redirections fail, then return | |
3702 | failure. Otherwise, if a command substitution took place while | |
3703 | expanding the command or a redirection, return the value of that | |
3704 | substitution. Otherwise, return EXECUTION_SUCCESS. */ | |
3705 | ||
d3a24ed2 CR |
3706 | r = do_redirections (redirects, RX_ACTIVE|RX_UNDOABLE); |
3707 | cleanup_redirects (redirection_undo_list); | |
3708 | redirection_undo_list = (REDIRECT *)NULL; | |
3709 | ||
3710 | if (r != 0) | |
ccc6cda3 | 3711 | return (EXECUTION_FAILURE); |
43df7bbb | 3712 | else if (last_command_subst_pid != NO_PID) |
ccc6cda3 JA |
3713 | return (last_command_exit_value); |
3714 | else | |
3715 | return (EXECUTION_SUCCESS); | |
3716 | } | |
3717 | } | |
3718 | ||
3719 | /* This is a hack to suppress word splitting for assignment statements | |
3720 | given as arguments to builtins with the ASSIGNMENT_BUILTIN flag set. */ | |
3721 | static void | |
3722 | fix_assignment_words (words) | |
3723 | WORD_LIST *words; | |
3724 | { | |
19baff85 | 3725 | WORD_LIST *w, *wcmd; |
ccc6cda3 | 3726 | struct builtin *b; |
ec860d76 | 3727 | int assoc, global; |
ccc6cda3 JA |
3728 | |
3729 | if (words == 0) | |
3730 | return; | |
3731 | ||
7117c2d2 | 3732 | b = 0; |
ec860d76 | 3733 | assoc = global = 0; |
ccc6cda3 | 3734 | |
19baff85 | 3735 | wcmd = words; |
ccc6cda3 JA |
3736 | for (w = words; w; w = w->next) |
3737 | if (w->word->flags & W_ASSIGNMENT) | |
7117c2d2 JA |
3738 | { |
3739 | if (b == 0) | |
3740 | { | |
19baff85 CR |
3741 | /* Posix (post-2008) says that `command' doesn't change whether |
3742 | or not the builtin it shadows is a `declaration command', even | |
3743 | though it removes other special builtin properties. In Posix | |
3744 | mode, we skip over one or more instances of `command' and | |
3745 | deal with the next word as the assignment builtin. */ | |
3746 | while (posixly_correct && wcmd && wcmd->word && wcmd->word->word && STREQ (wcmd->word->word, "command")) | |
3747 | wcmd = wcmd->next; | |
3748 | b = builtin_address_internal (wcmd->word->word, 0); | |
7117c2d2 JA |
3749 | if (b == 0 || (b->flags & ASSIGNMENT_BUILTIN) == 0) |
3750 | return; | |
43df7bbb | 3751 | else if (b && (b->flags & ASSIGNMENT_BUILTIN)) |
19baff85 | 3752 | wcmd->word->flags |= W_ASSNBLTIN; |
7117c2d2 | 3753 | } |
43df7bbb | 3754 | w->word->flags |= (W_NOSPLIT|W_NOGLOB|W_TILDEEXP|W_ASSIGNARG); |
fdf670ea CR |
3755 | #if defined (ARRAY_VARS) |
3756 | if (assoc) | |
3757 | w->word->flags |= W_ASSIGNASSOC; | |
ec860d76 CR |
3758 | if (global) |
3759 | w->word->flags |= W_ASSNGLOBAL; | |
fdf670ea CR |
3760 | #endif |
3761 | } | |
3762 | #if defined (ARRAY_VARS) | |
3763 | /* Note that we saw an associative array option to a builtin that takes | |
3764 | assignment statements. This is a bit of a kludge. */ | |
ec860d76 CR |
3765 | else if (w->word->word[0] == '-' && (strchr (w->word->word+1, 'A') || strchr (w->word->word+1, 'g'))) |
3766 | #else | |
3767 | else if (w->word->word[0] == '-' && strchr (w->word->word+1, 'g')) | |
3768 | #endif | |
fdf670ea CR |
3769 | { |
3770 | if (b == 0) | |
3771 | { | |
19baff85 CR |
3772 | while (posixly_correct && wcmd && wcmd->word && wcmd->word->word && STREQ (wcmd->word->word, "command")) |
3773 | wcmd = wcmd->next; | |
3774 | b = builtin_address_internal (wcmd->word->word, 0); | |
fdf670ea CR |
3775 | if (b == 0 || (b->flags & ASSIGNMENT_BUILTIN) == 0) |
3776 | return; | |
3777 | else if (b && (b->flags & ASSIGNMENT_BUILTIN)) | |
19baff85 | 3778 | wcmd->word->flags |= W_ASSNBLTIN; |
fdf670ea | 3779 | } |
ec860d76 | 3780 | if ((wcmd->word->flags & W_ASSNBLTIN) && strchr (w->word->word+1, 'A')) |
fdf670ea | 3781 | assoc = 1; |
ec860d76 CR |
3782 | if ((wcmd->word->flags & W_ASSNBLTIN) && strchr (w->word->word+1, 'g')) |
3783 | global = 1; | |
7117c2d2 | 3784 | } |
ccc6cda3 JA |
3785 | } |
3786 | ||
d3ad40de CR |
3787 | /* Return 1 if the file found by searching $PATH for PATHNAME, defaulting |
3788 | to PATHNAME, is a directory. Used by the autocd code below. */ | |
3789 | static int | |
3790 | is_dirname (pathname) | |
3791 | char *pathname; | |
3792 | { | |
3793 | char *temp; | |
631b20c6 CR |
3794 | int ret; |
3795 | ||
5a318736 | 3796 | temp = search_for_command (pathname, 0); |
631b20c6 CR |
3797 | ret = (temp ? file_isdir (temp) : file_isdir (pathname)); |
3798 | free (temp); | |
3799 | return ret; | |
d3ad40de CR |
3800 | } |
3801 | ||
726f6388 JA |
3802 | /* The meaty part of all the executions. We have to start hacking the |
3803 | real execution of commands here. Fork a process, set things up, | |
3804 | execute the command. */ | |
ccc6cda3 | 3805 | static int |
726f6388 JA |
3806 | execute_simple_command (simple_command, pipe_in, pipe_out, async, fds_to_close) |
3807 | SIMPLE_COM *simple_command; | |
3808 | int pipe_in, pipe_out, async; | |
3809 | struct fd_bitmap *fds_to_close; | |
3810 | { | |
3811 | WORD_LIST *words, *lastword; | |
ccc6cda3 | 3812 | char *command_line, *lastarg, *temp; |
bc4cd23c | 3813 | int first_word_quoted, result, builtin_is_special, already_forked, dofork; |
43df7bbb | 3814 | pid_t old_last_async_pid; |
f73dda09 | 3815 | sh_builtin_func_t *builtin; |
ccc6cda3 | 3816 | SHELL_VAR *func; |
f894a2d3 | 3817 | volatile int old_builtin, old_command_builtin; |
726f6388 JA |
3818 | |
3819 | result = EXECUTION_SUCCESS; | |
ccc6cda3 | 3820 | special_builtin_failed = builtin_is_special = 0; |
cce855bc | 3821 | command_line = (char *)0; |
726f6388 | 3822 | |
d9e1f41e CR |
3823 | QUIT; |
3824 | ||
ccc6cda3 | 3825 | /* If we're in a function, update the line number information. */ |
f486d0a1 | 3826 | if (variable_context && interactive_shell && sourcelevel == 0) |
d3a24ed2 | 3827 | line_number -= function_line_number; |
7117c2d2 | 3828 | |
726f6388 JA |
3829 | /* Remember what this command line looks like at invocation. */ |
3830 | command_string_index = 0; | |
3831 | print_simple_command (simple_command); | |
726f6388 | 3832 | |
d3ad40de | 3833 | #if 0 |
eb2bb562 | 3834 | if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0))) |
d3ad40de CR |
3835 | #else |
3836 | if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0) | |
3837 | #endif | |
d3a24ed2 CR |
3838 | { |
3839 | FREE (the_printed_command_except_trap); | |
43df7bbb | 3840 | the_printed_command_except_trap = the_printed_command ? savestring (the_printed_command) : (char *)0; |
d3a24ed2 CR |
3841 | } |
3842 | ||
3843 | /* Run the debug trap before each simple command, but do it after we | |
3844 | update the line number information. */ | |
3845 | result = run_debug_trap (); | |
3846 | #if defined (DEBUGGER) | |
3847 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
3848 | skip the command. */ | |
3849 | if (debugging_mode && result != EXECUTION_SUCCESS) | |
3850 | return (EXECUTION_SUCCESS); | |
3851 | #endif | |
3852 | ||
726f6388 | 3853 | first_word_quoted = |
d3ad40de | 3854 | simple_command->words ? (simple_command->words->word->flags & W_QUOTED) : 0; |
726f6388 | 3855 | |
43df7bbb | 3856 | last_command_subst_pid = NO_PID; |
b72432fd | 3857 | old_last_async_pid = last_asynchronous_pid; |
726f6388 | 3858 | |
bc4cd23c JA |
3859 | already_forked = dofork = 0; |
3860 | ||
3861 | /* If we're in a pipeline or run in the background, set DOFORK so we | |
3862 | make the child early, before word expansion. This keeps assignment | |
3863 | statements from affecting the parent shell's environment when they | |
3864 | should not. */ | |
3865 | dofork = pipe_in != NO_PIPE || pipe_out != NO_PIPE || async; | |
3866 | ||
3867 | /* Something like `%2 &' should restart job 2 in the background, not cause | |
3868 | the shell to fork here. */ | |
3869 | if (dofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE && | |
3870 | simple_command->words && simple_command->words->word && | |
3871 | simple_command->words->word->word && | |
3872 | (simple_command->words->word->word[0] == '%')) | |
3873 | dofork = 0; | |
3874 | ||
3875 | if (dofork) | |
cce855bc | 3876 | { |
bb70624e JA |
3877 | /* Do this now, because execute_disk_command will do it anyway in the |
3878 | vast majority of cases. */ | |
3879 | maybe_make_export_env (); | |
3880 | ||
6bf74e5a CR |
3881 | /* Don't let a DEBUG trap overwrite the command string to be saved with |
3882 | the process/job associated with this child. */ | |
3883 | if (make_child (savestring (the_printed_command_except_trap), async) == 0) | |
cce855bc JA |
3884 | { |
3885 | already_forked = 1; | |
3886 | simple_command->flags |= CMD_NO_FORK; | |
3887 | ||
43df7bbb CR |
3888 | subshell_environment = SUBSHELL_FORK; |
3889 | if (pipe_in != NO_PIPE || pipe_out != NO_PIPE) | |
3890 | subshell_environment |= SUBSHELL_PIPE; | |
3891 | if (async) | |
3892 | subshell_environment |= SUBSHELL_ASYNC; | |
28ef6c31 | 3893 | |
bb70624e JA |
3894 | /* We need to do this before piping to handle some really |
3895 | pathological cases where one of the pipe file descriptors | |
3896 | is < 2. */ | |
3897 | if (fds_to_close) | |
3898 | close_fd_bitmap (fds_to_close); | |
3899 | ||
cce855bc | 3900 | do_piping (pipe_in, pipe_out); |
f73dda09 | 3901 | pipe_in = pipe_out = NO_PIPE; |
09767ff0 | 3902 | #if defined (COPROCESS_SUPPORT) |
8e1a6eaa | 3903 | coproc_closeall (); |
09767ff0 | 3904 | #endif |
cce855bc | 3905 | |
b72432fd | 3906 | last_asynchronous_pid = old_last_async_pid; |
cce855bc JA |
3907 | } |
3908 | else | |
3909 | { | |
54a1fa7c CR |
3910 | /* Don't let simple commands that aren't the last command in a |
3911 | pipeline change $? for the rest of the pipeline (or at all). */ | |
3912 | if (pipe_out != NO_PIPE) | |
3913 | result = last_command_exit_value; | |
cce855bc JA |
3914 | close_pipes (pipe_in, pipe_out); |
3915 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
3916 | unlink_fifo_list (); | |
3917 | #endif | |
3918 | command_line = (char *)NULL; /* don't free this. */ | |
3919 | bind_lastarg ((char *)NULL); | |
3920 | return (result); | |
3921 | } | |
3922 | } | |
3923 | ||
726f6388 JA |
3924 | /* If we are re-running this as the result of executing the `command' |
3925 | builtin, do not expand the command words a second time. */ | |
3926 | if ((simple_command->flags & CMD_INHIBIT_EXPANSION) == 0) | |
3927 | { | |
3928 | current_fds_to_close = fds_to_close; | |
ccc6cda3 | 3929 | fix_assignment_words (simple_command->words); |
012bac39 CR |
3930 | /* Pass the ignore return flag down to command substitutions */ |
3931 | if (simple_command->flags & CMD_IGNORE_RETURN) /* XXX */ | |
3932 | comsub_ignore_return++; | |
726f6388 | 3933 | words = expand_words (simple_command->words); |
012bac39 CR |
3934 | if (simple_command->flags & CMD_IGNORE_RETURN) |
3935 | comsub_ignore_return--; | |
726f6388 JA |
3936 | current_fds_to_close = (struct fd_bitmap *)NULL; |
3937 | } | |
3938 | else | |
3939 | words = copy_word_list (simple_command->words); | |
3940 | ||
726f6388 JA |
3941 | /* It is possible for WORDS not to have anything left in it. |
3942 | Perhaps all the words consisted of `$foo', and there was | |
3943 | no variable `$foo'. */ | |
ccc6cda3 | 3944 | if (words == 0) |
726f6388 | 3945 | { |
d3a24ed2 | 3946 | this_command_name = 0; |
ccc6cda3 | 3947 | result = execute_null_command (simple_command->redirects, |
cce855bc | 3948 | pipe_in, pipe_out, |
43df7bbb | 3949 | already_forked ? 0 : async); |
cce855bc JA |
3950 | if (already_forked) |
3951 | exit (result); | |
3952 | else | |
3953 | { | |
3954 | bind_lastarg ((char *)NULL); | |
3955 | set_pipestatus_from_exit (result); | |
3956 | return (result); | |
3957 | } | |
ccc6cda3 | 3958 | } |
726f6388 | 3959 | |
ccc6cda3 | 3960 | lastarg = (char *)NULL; |
726f6388 | 3961 | |
ccc6cda3 | 3962 | begin_unwind_frame ("simple-command"); |
726f6388 | 3963 | |
ccc6cda3 | 3964 | if (echo_command_at_execute) |
d3a24ed2 | 3965 | xtrace_print_word_list (words, 1); |
726f6388 | 3966 | |
f73dda09 | 3967 | builtin = (sh_builtin_func_t *)NULL; |
ccc6cda3 JA |
3968 | func = (SHELL_VAR *)NULL; |
3969 | if ((simple_command->flags & CMD_NO_FUNCTIONS) == 0) | |
3970 | { | |
3971 | /* Posix.2 says special builtins are found before functions. We | |
3972 | don't set builtin_is_special anywhere other than here, because | |
3973 | this path is followed only when the `command' builtin is *not* | |
3974 | being used, and we don't want to exit the shell if a special | |
3975 | builtin executed with `command builtin' fails. `command' is not | |
3976 | a special builtin. */ | |
3977 | if (posixly_correct) | |
3978 | { | |
3979 | builtin = find_special_builtin (words->word->word); | |
3980 | if (builtin) | |
3981 | builtin_is_special = 1; | |
726f6388 | 3982 | } |
ccc6cda3 | 3983 | if (builtin == 0) |
726f6388 | 3984 | func = find_function (words->word->word); |
ccc6cda3 | 3985 | } |
726f6388 | 3986 | |
56299fa5 CR |
3987 | /* In POSIX mode, assignment errors in the temporary environment cause a |
3988 | non-interactive shell to exit. */ | |
3989 | if (builtin_is_special && interactive_shell == 0 && tempenv_assign_error) | |
3990 | { | |
3991 | last_command_exit_value = EXECUTION_FAILURE; | |
3992 | jump_to_top_level (ERREXIT); | |
3993 | } | |
3994 | ||
ccc6cda3 JA |
3995 | add_unwind_protect (dispose_words, words); |
3996 | QUIT; | |
726f6388 | 3997 | |
ccc6cda3 JA |
3998 | /* Bind the last word in this command to "$_" after execution. */ |
3999 | for (lastword = words; lastword->next; lastword = lastword->next) | |
4000 | ; | |
4001 | lastarg = lastword->word->word; | |
726f6388 JA |
4002 | |
4003 | #if defined (JOB_CONTROL) | |
ccc6cda3 | 4004 | /* Is this command a job control related thing? */ |
cce855bc | 4005 | if (words->word->word[0] == '%' && already_forked == 0) |
ccc6cda3 JA |
4006 | { |
4007 | this_command_name = async ? "bg" : "fg"; | |
726f6388 | 4008 | last_shell_builtin = this_shell_builtin; |
ccc6cda3 JA |
4009 | this_shell_builtin = builtin_address (this_command_name); |
4010 | result = (*this_shell_builtin) (words); | |
4011 | goto return_result; | |
4012 | } | |
726f6388 | 4013 | |
ccc6cda3 JA |
4014 | /* One other possiblilty. The user may want to resume an existing job. |
4015 | If they do, find out whether this word is a candidate for a running | |
4016 | job. */ | |
cce855bc | 4017 | if (job_control && already_forked == 0 && async == 0 && |
ccc6cda3 JA |
4018 | !first_word_quoted && |
4019 | !words->next && | |
4020 | words->word->word[0] && | |
4021 | !simple_command->redirects && | |
4022 | pipe_in == NO_PIPE && | |
4023 | pipe_out == NO_PIPE && | |
4024 | (temp = get_string_value ("auto_resume"))) | |
4025 | { | |
7117c2d2 | 4026 | int job, jflags, started_status; |
ccc6cda3 | 4027 | |
7117c2d2 JA |
4028 | jflags = JM_STOPPED|JM_FIRSTMATCH; |
4029 | if (STREQ (temp, "exact")) | |
4030 | jflags |= JM_EXACT; | |
4031 | else if (STREQ (temp, "substring")) | |
4032 | jflags |= JM_SUBSTRING; | |
4033 | else | |
4034 | jflags |= JM_PREFIX; | |
4035 | job = get_job_by_name (words->word->word, jflags); | |
4036 | if (job != NO_JOB) | |
726f6388 | 4037 | { |
7117c2d2 JA |
4038 | run_unwind_frame ("simple-command"); |
4039 | this_command_name = "fg"; | |
4040 | last_shell_builtin = this_shell_builtin; | |
4041 | this_shell_builtin = builtin_address ("fg"); | |
ccc6cda3 | 4042 | |
7117c2d2 JA |
4043 | started_status = start_job (job, 1); |
4044 | return ((started_status < 0) ? EXECUTION_FAILURE : started_status); | |
726f6388 | 4045 | } |
ccc6cda3 JA |
4046 | } |
4047 | #endif /* JOB_CONTROL */ | |
726f6388 | 4048 | |
d3ad40de | 4049 | run_builtin: |
ccc6cda3 JA |
4050 | /* Remember the name of this command globally. */ |
4051 | this_command_name = words->word->word; | |
726f6388 | 4052 | |
ccc6cda3 JA |
4053 | QUIT; |
4054 | ||
4055 | /* This command could be a shell builtin or a user-defined function. | |
4056 | We have already found special builtins by this time, so we do not | |
4057 | set builtin_is_special. If this is a function or builtin, and we | |
4058 | have pipes, then fork a subshell in here. Otherwise, just execute | |
4059 | the command directly. */ | |
4060 | if (func == 0 && builtin == 0) | |
4061 | builtin = find_shell_builtin (this_command_name); | |
4062 | ||
4063 | last_shell_builtin = this_shell_builtin; | |
4064 | this_shell_builtin = builtin; | |
4065 | ||
4066 | if (builtin || func) | |
726f6388 | 4067 | { |
d7f49990 | 4068 | if (builtin) |
aad3dfa2 | 4069 | { |
f894a2d3 CR |
4070 | old_builtin = executing_builtin; |
4071 | old_command_builtin = executing_command_builtin; | |
aad3dfa2 CR |
4072 | unwind_protect_int (executing_builtin); /* modified in execute_builtin */ |
4073 | unwind_protect_int (executing_command_builtin); /* ditto */ | |
4074 | } | |
28ef6c31 | 4075 | if (already_forked) |
726f6388 | 4076 | { |
cce855bc | 4077 | /* reset_terminating_signals (); */ /* XXX */ |
d1fab3dc CR |
4078 | /* Reset the signal handlers in the child, but don't free the |
4079 | trap strings. Set a flag noting that we have to free the | |
4080 | trap strings if we run trap to change a signal disposition. */ | |
4081 | reset_signal_handlers (); | |
4082 | subshell_environment |= SUBSHELL_RESETTRAP; | |
726f6388 | 4083 | |
cce855bc | 4084 | if (async) |
ccc6cda3 | 4085 | { |
cce855bc JA |
4086 | if ((simple_command->flags & CMD_STDIN_REDIR) && |
4087 | pipe_in == NO_PIPE && | |
4088 | (stdin_redirects (simple_command->redirects) == 0)) | |
4089 | async_redirect_stdin (); | |
4090 | setup_async_signals (); | |
ccc6cda3 | 4091 | } |
cce855bc | 4092 | |
d3a24ed2 | 4093 | subshell_level++; |
cce855bc JA |
4094 | execute_subshell_builtin_or_function |
4095 | (words, simple_command->redirects, builtin, func, | |
4096 | pipe_in, pipe_out, async, fds_to_close, | |
4097 | simple_command->flags); | |
d3a24ed2 | 4098 | subshell_level--; |
726f6388 JA |
4099 | } |
4100 | else | |
4101 | { | |
ccc6cda3 JA |
4102 | result = execute_builtin_or_function |
4103 | (words, builtin, func, simple_command->redirects, fds_to_close, | |
4104 | simple_command->flags); | |
4105 | if (builtin) | |
4106 | { | |
4107 | if (result > EX_SHERRBASE) | |
4108 | { | |
cce855bc JA |
4109 | result = builtin_status (result); |
4110 | if (builtin_is_special) | |
4111 | special_builtin_failed = 1; | |
ccc6cda3 JA |
4112 | } |
4113 | /* In POSIX mode, if there are assignment statements preceding | |
4114 | a special builtin, they persist after the builtin | |
4115 | completes. */ | |
4116 | if (posixly_correct && builtin_is_special && temporary_env) | |
4117 | merge_temporary_env (); | |
4118 | } | |
4119 | else /* function */ | |
4120 | { | |
4121 | if (result == EX_USAGE) | |
4122 | result = EX_BADUSAGE; | |
4123 | else if (result > EX_SHERRBASE) | |
cce855bc | 4124 | result = EXECUTION_FAILURE; |
ccc6cda3 JA |
4125 | } |
4126 | ||
cce855bc JA |
4127 | set_pipestatus_from_exit (result); |
4128 | ||
726f6388 JA |
4129 | goto return_result; |
4130 | } | |
4131 | } | |
726f6388 | 4132 | |
d3ad40de CR |
4133 | if (autocd && interactive && words->word && is_dirname (words->word->word)) |
4134 | { | |
4135 | words = make_word_list (make_word ("cd"), words); | |
4136 | xtrace_print_word_list (words, 0); | |
4137 | goto run_builtin; | |
4138 | } | |
4139 | ||
cce855bc | 4140 | if (command_line == 0) |
861a1900 | 4141 | command_line = savestring (the_printed_command_except_trap ? the_printed_command_except_trap : ""); |
d3ad40de CR |
4142 | |
4143 | #if defined (PROCESS_SUBSTITUTION) | |
4144 | if ((subshell_environment & SUBSHELL_COMSUB) && (simple_command->flags & CMD_NO_FORK) && fifos_pending() > 0) | |
4145 | simple_command->flags &= ~CMD_NO_FORK; | |
4146 | #endif | |
1384c96f | 4147 | |
6faad625 | 4148 | result = execute_disk_command (words, simple_command->redirects, command_line, |
ccc6cda3 | 4149 | pipe_in, pipe_out, async, fds_to_close, |
d166f048 | 4150 | simple_command->flags); |
726f6388 JA |
4151 | |
4152 | return_result: | |
4153 | bind_lastarg (lastarg); | |
ccc6cda3 | 4154 | FREE (command_line); |
d3a24ed2 | 4155 | dispose_words (words); |
f894a2d3 CR |
4156 | if (builtin) |
4157 | { | |
4158 | executing_builtin = old_builtin; | |
4159 | executing_command_builtin = old_command_builtin; | |
4160 | } | |
d3a24ed2 CR |
4161 | discard_unwind_frame ("simple-command"); |
4162 | this_command_name = (char *)NULL; /* points to freed memory now */ | |
726f6388 JA |
4163 | return (result); |
4164 | } | |
4165 | ||
ccc6cda3 JA |
4166 | /* Translate the special builtin exit statuses. We don't really need a |
4167 | function for this; it's a placeholder for future work. */ | |
4168 | static int | |
4169 | builtin_status (result) | |
4170 | int result; | |
4171 | { | |
4172 | int r; | |
4173 | ||
4174 | switch (result) | |
4175 | { | |
4176 | case EX_USAGE: | |
4177 | r = EX_BADUSAGE; | |
4178 | break; | |
4179 | case EX_REDIRFAIL: | |
4180 | case EX_BADSYNTAX: | |
4181 | case EX_BADASSIGN: | |
4182 | case EX_EXPFAIL: | |
4183 | r = EXECUTION_FAILURE; | |
4184 | break; | |
4185 | default: | |
4186 | r = EXECUTION_SUCCESS; | |
4187 | break; | |
4188 | } | |
4189 | return (r); | |
4190 | } | |
4191 | ||
726f6388 JA |
4192 | static int |
4193 | execute_builtin (builtin, words, flags, subshell) | |
f73dda09 | 4194 | sh_builtin_func_t *builtin; |
726f6388 JA |
4195 | WORD_LIST *words; |
4196 | int flags, subshell; | |
4197 | { | |
d166f048 | 4198 | int old_e_flag, result, eval_unwind; |
7117c2d2 | 4199 | int isbltinenv; |
dfc21851 | 4200 | char *error_trap; |
726f6388 | 4201 | |
dfc21851 | 4202 | error_trap = 0; |
ccc6cda3 | 4203 | old_e_flag = exit_immediately_on_error; |
726f6388 JA |
4204 | /* The eval builtin calls parse_and_execute, which does not know about |
4205 | the setting of flags, and always calls the execution functions with | |
4206 | flags that will exit the shell on an error if -e is set. If the | |
4207 | eval builtin is being called, and we're supposed to ignore the exit | |
dfc21851 CR |
4208 | value of the command, we turn the -e flag off ourselves and disable |
4209 | the ERR trap, then restore them when the command completes. This is | |
4210 | also a problem (as below) for the command and source/. builtins. */ | |
f13513ff CR |
4211 | if (subshell == 0 && (flags & CMD_IGNORE_RETURN) && |
4212 | (builtin == eval_builtin || builtin == command_builtin || builtin == source_builtin)) | |
726f6388 JA |
4213 | { |
4214 | begin_unwind_frame ("eval_builtin"); | |
4215 | unwind_protect_int (exit_immediately_on_error); | |
dfc21851 CR |
4216 | error_trap = TRAP_STRING (ERROR_TRAP); |
4217 | if (error_trap) | |
4218 | { | |
4219 | error_trap = savestring (error_trap); | |
4220 | add_unwind_protect (xfree, error_trap); | |
4221 | add_unwind_protect (set_error_trap, error_trap); | |
4222 | restore_default_signal (ERROR_TRAP); | |
4223 | } | |
726f6388 | 4224 | exit_immediately_on_error = 0; |
d166f048 | 4225 | eval_unwind = 1; |
726f6388 | 4226 | } |
d166f048 JA |
4227 | else |
4228 | eval_unwind = 0; | |
726f6388 JA |
4229 | |
4230 | /* The temporary environment for a builtin is supposed to apply to | |
4231 | all commands executed by that builtin. Currently, this is a | |
cdb32d45 CR |
4232 | problem only with the `unset', `source' and `eval' builtins. */ |
4233 | ||
19e1dd93 | 4234 | isbltinenv = (builtin == source_builtin || builtin == eval_builtin || builtin == unset_builtin); |
19e1dd93 | 4235 | |
7117c2d2 | 4236 | if (isbltinenv) |
726f6388 JA |
4237 | { |
4238 | if (subshell == 0) | |
4239 | begin_unwind_frame ("builtin_env"); | |
4240 | ||
4241 | if (temporary_env) | |
4242 | { | |
7117c2d2 | 4243 | push_scope (VC_BLTNENV, temporary_env); |
726f6388 | 4244 | if (subshell == 0) |
c3271763 | 4245 | add_unwind_protect (pop_scope, (flags & CMD_COMMAND_BUILTIN) ? 0 : "1"); |
7117c2d2 | 4246 | temporary_env = (HASH_TABLE *)NULL; |
726f6388 | 4247 | } |
28ef6c31 JA |
4248 | } |
4249 | ||
4250 | /* `return' does a longjmp() back to a saved environment in execute_function. | |
4251 | If a variable assignment list preceded the command, and the shell is | |
4252 | running in POSIX mode, we need to merge that into the shell_variables | |
4253 | table, since `return' is a POSIX special builtin. */ | |
4254 | if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env) | |
4255 | { | |
4256 | begin_unwind_frame ("return_temp_env"); | |
4257 | add_unwind_protect (merge_temporary_env, (char *)NULL); | |
726f6388 JA |
4258 | } |
4259 | ||
d7f49990 | 4260 | executing_builtin++; |
aad3dfa2 | 4261 | executing_command_builtin |= builtin == command_builtin; |
726f6388 JA |
4262 | result = ((*builtin) (words->next)); |
4263 | ||
28ef6c31 JA |
4264 | /* This shouldn't happen, but in case `return' comes back instead of |
4265 | longjmp'ing, we need to unwind. */ | |
4266 | if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env) | |
4267 | discard_unwind_frame ("return_temp_env"); | |
4268 | ||
7117c2d2 JA |
4269 | if (subshell == 0 && isbltinenv) |
4270 | run_unwind_frame ("builtin_env"); | |
726f6388 | 4271 | |
d166f048 | 4272 | if (eval_unwind) |
726f6388 JA |
4273 | { |
4274 | exit_immediately_on_error += old_e_flag; | |
dfc21851 CR |
4275 | if (error_trap) |
4276 | { | |
4277 | set_error_trap (error_trap); | |
4278 | xfree (error_trap); | |
4279 | } | |
726f6388 JA |
4280 | discard_unwind_frame ("eval_builtin"); |
4281 | } | |
4282 | ||
4283 | return (result); | |
4284 | } | |
4285 | ||
726f6388 JA |
4286 | static int |
4287 | execute_function (var, words, flags, fds_to_close, async, subshell) | |
4288 | SHELL_VAR *var; | |
4289 | WORD_LIST *words; | |
f73dda09 | 4290 | int flags; |
726f6388 | 4291 | struct fd_bitmap *fds_to_close; |
f73dda09 | 4292 | int async, subshell; |
726f6388 JA |
4293 | { |
4294 | int return_val, result; | |
d3a24ed2 CR |
4295 | COMMAND *tc, *fc, *save_current; |
4296 | char *debug_trap, *error_trap, *return_trap; | |
4297 | #if defined (ARRAY_VARS) | |
54cdd75a | 4298 | SHELL_VAR *funcname_v, *nfv, *bash_source_v, *bash_lineno_v; |
e3af9370 CR |
4299 | ARRAY *funcname_a; |
4300 | volatile ARRAY *bash_source_a; | |
4301 | volatile ARRAY *bash_lineno_a; | |
d3a24ed2 CR |
4302 | #endif |
4303 | FUNCTION_DEF *shell_fn; | |
4304 | char *sfile, *t; | |
f73dda09 JA |
4305 | |
4306 | USE_VAR(fc); | |
726f6388 | 4307 | |
f486d0a1 CR |
4308 | if (funcnest_max > 0 && funcnest >= funcnest_max) |
4309 | { | |
122f603c | 4310 | internal_error (_("%s: maximum function nesting level exceeded (%d)"), var->name, funcnest); |
6faad625 | 4311 | funcnest = 0; /* XXX - should we reset it somewhere else? */ |
f486d0a1 CR |
4312 | jump_to_top_level (DISCARD); |
4313 | } | |
f486d0a1 | 4314 | |
d3a24ed2 CR |
4315 | #if defined (ARRAY_VARS) |
4316 | GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a); | |
4317 | GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a); | |
4318 | GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a); | |
4319 | #endif | |
4320 | ||
726f6388 JA |
4321 | tc = (COMMAND *)copy_command (function_cell (var)); |
4322 | if (tc && (flags & CMD_IGNORE_RETURN)) | |
4323 | tc->flags |= CMD_IGNORE_RETURN; | |
4324 | ||
726f6388 JA |
4325 | if (subshell == 0) |
4326 | { | |
ccc6cda3 | 4327 | begin_unwind_frame ("function_calling"); |
7117c2d2 | 4328 | push_context (var->name, subshell, temporary_env); |
726f6388 JA |
4329 | add_unwind_protect (pop_context, (char *)NULL); |
4330 | unwind_protect_int (line_number); | |
ccc6cda3 JA |
4331 | unwind_protect_int (return_catch_flag); |
4332 | unwind_protect_jmp_buf (return_catch); | |
4333 | add_unwind_protect (dispose_command, (char *)tc); | |
bb70624e | 4334 | unwind_protect_pointer (this_shell_function); |
ccc6cda3 | 4335 | unwind_protect_int (loop_level); |
f486d0a1 | 4336 | unwind_protect_int (funcnest); |
726f6388 | 4337 | } |
7117c2d2 JA |
4338 | else |
4339 | push_context (var->name, subshell, temporary_env); /* don't unwind-protect for subshells */ | |
4340 | ||
4341 | temporary_env = (HASH_TABLE *)NULL; | |
726f6388 | 4342 | |
bb70624e JA |
4343 | this_shell_function = var; |
4344 | make_funcname_visible (1); | |
4345 | ||
f73dda09 JA |
4346 | debug_trap = TRAP_STRING(DEBUG_TRAP); |
4347 | error_trap = TRAP_STRING(ERROR_TRAP); | |
d3a24ed2 | 4348 | return_trap = TRAP_STRING(RETURN_TRAP); |
f73dda09 | 4349 | |
d3a24ed2 CR |
4350 | /* The order of the unwind protects for debug_trap, error_trap and |
4351 | return_trap is important here! unwind-protect commands are run | |
4352 | in reverse order of registration. If this causes problems, take | |
4353 | out the xfree unwind-protect calls and live with the small memory leak. */ | |
4354 | ||
4355 | /* function_trace_mode != 0 means that all functions inherit the DEBUG trap. | |
4356 | if the function has the trace attribute set, it inherits the DEBUG trap */ | |
4357 | if (debug_trap && ((trace_p (var) == 0) && function_trace_mode == 0)) | |
ccc6cda3 JA |
4358 | { |
4359 | if (subshell == 0) | |
4360 | { | |
4361 | debug_trap = savestring (debug_trap); | |
cce855bc | 4362 | add_unwind_protect (xfree, debug_trap); |
ccc6cda3 JA |
4363 | add_unwind_protect (set_debug_trap, debug_trap); |
4364 | } | |
4365 | restore_default_signal (DEBUG_TRAP); | |
4366 | } | |
726f6388 | 4367 | |
d3a24ed2 CR |
4368 | /* error_trace_mode != 0 means that functions inherit the ERR trap. */ |
4369 | if (error_trap && error_trace_mode == 0) | |
f73dda09 JA |
4370 | { |
4371 | if (subshell == 0) | |
4372 | { | |
4373 | error_trap = savestring (error_trap); | |
4374 | add_unwind_protect (xfree, error_trap); | |
4375 | add_unwind_protect (set_error_trap, error_trap); | |
4376 | } | |
4377 | restore_default_signal (ERROR_TRAP); | |
4378 | } | |
4379 | ||
76a8d78d CR |
4380 | /* Shell functions inherit the RETURN trap if function tracing is on |
4381 | globally or on individually for this function. */ | |
4382 | #if 0 | |
d3a24ed2 | 4383 | if (return_trap && ((trace_p (var) == 0) && function_trace_mode == 0)) |
76a8d78d CR |
4384 | #else |
4385 | if (return_trap && (signal_in_progress (DEBUG_TRAP) || ((trace_p (var) == 0) && function_trace_mode == 0))) | |
4386 | #endif | |
d3a24ed2 CR |
4387 | { |
4388 | if (subshell == 0) | |
4389 | { | |
4390 | return_trap = savestring (return_trap); | |
4391 | add_unwind_protect (xfree, return_trap); | |
4392 | add_unwind_protect (set_return_trap, return_trap); | |
4393 | } | |
4394 | restore_default_signal (RETURN_TRAP); | |
4395 | } | |
4396 | ||
4397 | funcnest++; | |
4398 | #if defined (ARRAY_VARS) | |
5e13499c | 4399 | /* This is quite similar to the code in shell.c and elsewhere. */ |
d3a24ed2 CR |
4400 | shell_fn = find_function_def (this_shell_function->name); |
4401 | sfile = shell_fn ? shell_fn->source_file : ""; | |
9c2db999 | 4402 | array_push ((ARRAY *)funcname_a, this_shell_function->name); |
5e13499c | 4403 | |
9c2db999 | 4404 | array_push ((ARRAY *)bash_source_a, sfile); |
d3a24ed2 | 4405 | t = itos (executing_line_number ()); |
9c2db999 | 4406 | array_push ((ARRAY *)bash_lineno_a, t); |
d3a24ed2 | 4407 | free (t); |
d3a24ed2 CR |
4408 | #endif |
4409 | ||
726f6388 JA |
4410 | /* The temporary environment for a function is supposed to apply to |
4411 | all commands executed within the function body. */ | |
726f6388 | 4412 | |
726f6388 JA |
4413 | remember_args (words->next, 1); |
4414 | ||
d3a24ed2 | 4415 | /* Update BASH_ARGV and BASH_ARGC */ |
5e13499c CR |
4416 | if (debugging_mode) |
4417 | push_args (words->next); | |
d3a24ed2 | 4418 | |
ccc6cda3 | 4419 | /* Number of the line on which the function body starts. */ |
d3a24ed2 | 4420 | line_number = function_line_number = tc->line; |
726f6388 | 4421 | |
726f6388 | 4422 | #if defined (JOB_CONTROL) |
cdb32d45 CR |
4423 | if (subshell) |
4424 | stop_pipeline (async, (COMMAND *)NULL); | |
726f6388 | 4425 | #endif |
726f6388 | 4426 | |
cdb32d45 | 4427 | fc = tc; |
726f6388 JA |
4428 | |
4429 | return_catch_flag++; | |
4430 | return_val = setjmp (return_catch); | |
4431 | ||
4432 | if (return_val) | |
54cdd75a CR |
4433 | { |
4434 | result = return_catch_value; | |
76a8d78d | 4435 | /* Run the RETURN trap in the function's context. */ |
54cdd75a CR |
4436 | save_current = currently_executing_command; |
4437 | run_return_trap (); | |
4438 | currently_executing_command = save_current; | |
54cdd75a | 4439 | } |
726f6388 | 4440 | else |
d3a24ed2 CR |
4441 | { |
4442 | /* Run the debug trap here so we can trap at the start of a function's | |
4443 | execution rather than the execution of the body's first command. */ | |
4444 | showing_function_line = 1; | |
4445 | save_current = currently_executing_command; | |
4446 | result = run_debug_trap (); | |
4447 | #if defined (DEBUGGER) | |
4448 | /* In debugging mode, if the DEBUG trap returns a non-zero status, we | |
4449 | skip the command. */ | |
4450 | if (debugging_mode == 0 || result == EXECUTION_SUCCESS) | |
4451 | { | |
4452 | showing_function_line = 0; | |
4453 | currently_executing_command = save_current; | |
4454 | result = execute_command_internal (fc, 0, NO_PIPE, NO_PIPE, fds_to_close); | |
4455 | ||
4456 | /* Run the RETURN trap in the function's context */ | |
4457 | save_current = currently_executing_command; | |
4458 | run_return_trap (); | |
4459 | currently_executing_command = save_current; | |
4460 | } | |
4461 | #else | |
4462 | result = execute_command_internal (fc, 0, NO_PIPE, NO_PIPE, fds_to_close); | |
54cdd75a CR |
4463 | |
4464 | save_current = currently_executing_command; | |
4465 | run_return_trap (); | |
4466 | currently_executing_command = save_current; | |
d3a24ed2 CR |
4467 | #endif |
4468 | showing_function_line = 0; | |
4469 | } | |
4470 | ||
4471 | /* Restore BASH_ARGC and BASH_ARGV */ | |
5e13499c CR |
4472 | if (debugging_mode) |
4473 | pop_args (); | |
726f6388 | 4474 | |
ccc6cda3 | 4475 | if (subshell == 0) |
726f6388 JA |
4476 | run_unwind_frame ("function_calling"); |
4477 | ||
d3a24ed2 | 4478 | #if defined (ARRAY_VARS) |
54cdd75a CR |
4479 | /* These two variables cannot be unset, and cannot be affected by the |
4480 | function. */ | |
9c2db999 CR |
4481 | array_pop ((ARRAY *)bash_source_a); |
4482 | array_pop ((ARRAY *)bash_lineno_a); | |
54cdd75a CR |
4483 | |
4484 | /* FUNCNAME can be unset, and so can potentially be changed by the | |
4485 | function. */ | |
4486 | GET_ARRAY_FROM_VAR ("FUNCNAME", nfv, funcname_a); | |
4487 | if (nfv == funcname_v) | |
4488 | array_pop (funcname_a); | |
d3a24ed2 CR |
4489 | #endif |
4490 | ||
bb70624e | 4491 | if (variable_context == 0 || this_shell_function == 0) |
6932f7f5 CR |
4492 | { |
4493 | make_funcname_visible (0); | |
984a1947 | 4494 | #if defined (PROCESS_SUBSTITUTION) |
6932f7f5 | 4495 | unlink_fifo_list (); |
984a1947 | 4496 | #endif |
6932f7f5 CR |
4497 | } |
4498 | ||
726f6388 JA |
4499 | return (result); |
4500 | } | |
4501 | ||
bb70624e JA |
4502 | /* A convenience routine for use by other parts of the shell to execute |
4503 | a particular shell function. */ | |
4504 | int | |
4505 | execute_shell_function (var, words) | |
4506 | SHELL_VAR *var; | |
4507 | WORD_LIST *words; | |
4508 | { | |
4509 | int ret; | |
4510 | struct fd_bitmap *bitmap; | |
4511 | ||
4512 | bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE); | |
4513 | begin_unwind_frame ("execute-shell-function"); | |
4514 | add_unwind_protect (dispose_fd_bitmap, (char *)bitmap); | |
4515 | ||
4516 | ret = execute_function (var, words, 0, bitmap, 0, 0); | |
4517 | ||
4518 | dispose_fd_bitmap (bitmap); | |
4519 | discard_unwind_frame ("execute-shell-function"); | |
4520 | ||
4521 | return ret; | |
4522 | } | |
4523 | ||
726f6388 JA |
4524 | /* Execute a shell builtin or function in a subshell environment. This |
4525 | routine does not return; it only calls exit(). If BUILTIN is non-null, | |
4526 | it points to a function to call to execute a shell builtin; otherwise | |
4527 | VAR points at the body of a function to execute. WORDS is the arguments | |
4528 | to the command, REDIRECTS specifies redirections to perform before the | |
4529 | command is executed. */ | |
4530 | static void | |
4531 | execute_subshell_builtin_or_function (words, redirects, builtin, var, | |
4532 | pipe_in, pipe_out, async, fds_to_close, | |
4533 | flags) | |
4534 | WORD_LIST *words; | |
4535 | REDIRECT *redirects; | |
f73dda09 | 4536 | sh_builtin_func_t *builtin; |
726f6388 JA |
4537 | SHELL_VAR *var; |
4538 | int pipe_in, pipe_out, async; | |
4539 | struct fd_bitmap *fds_to_close; | |
4540 | int flags; | |
4541 | { | |
d3ad40de | 4542 | int result, r, funcvalue; |
bb70624e JA |
4543 | #if defined (JOB_CONTROL) |
4544 | int jobs_hack; | |
726f6388 | 4545 | |
b72432fd JA |
4546 | jobs_hack = (builtin == jobs_builtin) && |
4547 | ((subshell_environment & SUBSHELL_ASYNC) == 0 || pipe_out != NO_PIPE); | |
bb70624e JA |
4548 | #endif |
4549 | ||
4550 | /* A subshell is neither a login shell nor interactive. */ | |
4551 | login_shell = interactive = 0; | |
b72432fd | 4552 | |
d3ad40de CR |
4553 | if (async) |
4554 | subshell_environment |= SUBSHELL_ASYNC; | |
4555 | if (pipe_in != NO_PIPE || pipe_out != NO_PIPE) | |
4556 | subshell_environment |= SUBSHELL_PIPE; | |
726f6388 | 4557 | |
ccc6cda3 | 4558 | maybe_make_export_env (); /* XXX - is this needed? */ |
726f6388 JA |
4559 | |
4560 | #if defined (JOB_CONTROL) | |
4561 | /* Eradicate all traces of job control after we fork the subshell, so | |
4562 | all jobs begun by this subshell are in the same process group as | |
4563 | the shell itself. */ | |
4564 | ||
4565 | /* Allow the output of `jobs' to be piped. */ | |
b72432fd | 4566 | if (jobs_hack) |
726f6388 JA |
4567 | kill_current_pipeline (); |
4568 | else | |
4569 | without_job_control (); | |
4570 | ||
4571 | set_sigchld_handler (); | |
4572 | #endif /* JOB_CONTROL */ | |
4573 | ||
4574 | set_sigint_handler (); | |
4575 | ||
726f6388 JA |
4576 | if (fds_to_close) |
4577 | close_fd_bitmap (fds_to_close); | |
4578 | ||
bb70624e JA |
4579 | do_piping (pipe_in, pipe_out); |
4580 | ||
d3a24ed2 | 4581 | if (do_redirections (redirects, RX_ACTIVE) != 0) |
726f6388 JA |
4582 | exit (EXECUTION_FAILURE); |
4583 | ||
4584 | if (builtin) | |
4585 | { | |
726f6388 JA |
4586 | /* Give builtins a place to jump back to on failure, |
4587 | so we don't go back up to main(). */ | |
4588 | result = setjmp (top_level); | |
4589 | ||
d3ad40de CR |
4590 | /* Give the return builtin a place to jump to when executed in a subshell |
4591 | or pipeline */ | |
4592 | funcvalue = 0; | |
4593 | if (return_catch_flag && builtin == return_builtin) | |
4594 | funcvalue = setjmp (return_catch); | |
4595 | ||
726f6388 JA |
4596 | if (result == EXITPROG) |
4597 | exit (last_command_exit_value); | |
4598 | else if (result) | |
4599 | exit (EXECUTION_FAILURE); | |
d3ad40de CR |
4600 | else if (funcvalue) |
4601 | exit (return_catch_value); | |
726f6388 | 4602 | else |
cce855bc JA |
4603 | { |
4604 | r = execute_builtin (builtin, words, flags, 1); | |
b709b946 | 4605 | fflush (stdout); |
cce855bc JA |
4606 | if (r == EX_USAGE) |
4607 | r = EX_BADUSAGE; | |
4608 | exit (r); | |
4609 | } | |
726f6388 JA |
4610 | } |
4611 | else | |
b709b946 CR |
4612 | { |
4613 | r = execute_function (var, words, flags, fds_to_close, async, 1); | |
4614 | fflush (stdout); | |
4615 | exit (r); | |
4616 | } | |
726f6388 JA |
4617 | } |
4618 | ||
4619 | /* Execute a builtin or function in the current shell context. If BUILTIN | |
4620 | is non-null, it is the builtin command to execute, otherwise VAR points | |
4621 | to the body of a function. WORDS are the command's arguments, REDIRECTS | |
4622 | are the redirections to perform. FDS_TO_CLOSE is the usual bitmap of | |
4623 | file descriptors to close. | |
4624 | ||
4625 | If BUILTIN is exec_builtin, the redirections specified in REDIRECTS are | |
4626 | not undone before this function returns. */ | |
4627 | static int | |
4628 | execute_builtin_or_function (words, builtin, var, redirects, | |
4629 | fds_to_close, flags) | |
4630 | WORD_LIST *words; | |
f73dda09 | 4631 | sh_builtin_func_t *builtin; |
726f6388 JA |
4632 | SHELL_VAR *var; |
4633 | REDIRECT *redirects; | |
4634 | struct fd_bitmap *fds_to_close; | |
4635 | int flags; | |
4636 | { | |
ccc6cda3 | 4637 | int result; |
726f6388 | 4638 | REDIRECT *saved_undo_list; |
112ff2a6 CR |
4639 | #if defined (PROCESS_SUBSTITUTION) |
4640 | int ofifo, nfifo, osize; | |
4641 | char *ofifo_list; | |
4642 | #endif | |
4643 | ||
4644 | ||
4645 | #if defined (PROCESS_SUBSTITUTION) | |
4646 | ofifo = num_fifos (); | |
4647 | ofifo_list = copy_fifo_list (&osize); | |
4648 | #endif | |
726f6388 | 4649 | |
d3a24ed2 | 4650 | if (do_redirections (redirects, RX_ACTIVE|RX_UNDOABLE) != 0) |
726f6388 JA |
4651 | { |
4652 | cleanup_redirects (redirection_undo_list); | |
4653 | redirection_undo_list = (REDIRECT *)NULL; | |
4654 | dispose_exec_redirects (); | |
112ff2a6 CR |
4655 | #if defined (PROCESS_SUBSTITUTION) |
4656 | free (ofifo_list); | |
4657 | #endif | |
ccc6cda3 | 4658 | return (EX_REDIRFAIL); /* was EXECUTION_FAILURE */ |
726f6388 JA |
4659 | } |
4660 | ||
4661 | saved_undo_list = redirection_undo_list; | |
4662 | ||
4663 | /* Calling the "exec" builtin changes redirections forever. */ | |
4664 | if (builtin == exec_builtin) | |
4665 | { | |
4666 | dispose_redirects (saved_undo_list); | |
4667 | saved_undo_list = exec_redirection_undo_list; | |
4668 | exec_redirection_undo_list = (REDIRECT *)NULL; | |
4669 | } | |
4670 | else | |
4671 | dispose_exec_redirects (); | |
4672 | ||
4673 | if (saved_undo_list) | |
4674 | { | |
4675 | begin_unwind_frame ("saved redirects"); | |
ccc6cda3 | 4676 | add_unwind_protect (cleanup_redirects, (char *)saved_undo_list); |
726f6388 JA |
4677 | } |
4678 | ||
4679 | redirection_undo_list = (REDIRECT *)NULL; | |
4680 | ||
4681 | if (builtin) | |
4682 | result = execute_builtin (builtin, words, flags, 0); | |
4683 | else | |
4684 | result = execute_function (var, words, flags, fds_to_close, 0, 0); | |
4685 | ||
d3a24ed2 | 4686 | /* We do this before undoing the effects of any redirections. */ |
b709b946 | 4687 | fflush (stdout); |
53ac45a3 | 4688 | fpurge (stdout); |
d3a24ed2 CR |
4689 | if (ferror (stdout)) |
4690 | clearerr (stdout); | |
4691 | ||
28ef6c31 JA |
4692 | /* If we are executing the `command' builtin, but this_shell_builtin is |
4693 | set to `exec_builtin', we know that we have something like | |
4694 | `command exec [redirection]', since otherwise `exec' would have | |
4695 | overwritten the shell and we wouldn't get here. In this case, we | |
4696 | want to behave as if the `command' builtin had not been specified | |
4697 | and preserve the redirections. */ | |
4698 | if (builtin == command_builtin && this_shell_builtin == exec_builtin) | |
4699 | { | |
048b249e CR |
4700 | int discard; |
4701 | ||
4702 | discard = 0; | |
28ef6c31 | 4703 | if (saved_undo_list) |
048b249e CR |
4704 | { |
4705 | dispose_redirects (saved_undo_list); | |
4706 | discard = 1; | |
4707 | } | |
28ef6c31 JA |
4708 | redirection_undo_list = exec_redirection_undo_list; |
4709 | saved_undo_list = exec_redirection_undo_list = (REDIRECT *)NULL; | |
048b249e CR |
4710 | if (discard) |
4711 | discard_unwind_frame ("saved redirects"); | |
28ef6c31 JA |
4712 | } |
4713 | ||
726f6388 JA |
4714 | if (saved_undo_list) |
4715 | { | |
4716 | redirection_undo_list = saved_undo_list; | |
4717 | discard_unwind_frame ("saved redirects"); | |
4718 | } | |
4719 | ||
4720 | if (redirection_undo_list) | |
4721 | { | |
ccc6cda3 | 4722 | cleanup_redirects (redirection_undo_list); |
726f6388 JA |
4723 | redirection_undo_list = (REDIRECT *)NULL; |
4724 | } | |
4725 | ||
112ff2a6 CR |
4726 | #if defined (PROCESS_SUBSTITUTION) |
4727 | /* Close any FIFOs created by this builtin or function. */ | |
4728 | nfifo = num_fifos (); | |
4729 | if (nfifo > ofifo) | |
4730 | close_new_fifos (ofifo_list, osize); | |
4731 | free (ofifo_list); | |
4732 | #endif | |
4733 | ||
726f6388 JA |
4734 | return (result); |
4735 | } | |
4736 | ||
4737 | void | |
4738 | setup_async_signals () | |
4739 | { | |
b72432fd JA |
4740 | #if defined (__BEOS__) |
4741 | set_signal_handler (SIGHUP, SIG_IGN); /* they want csh-like behavior */ | |
4742 | #endif | |
4743 | ||
726f6388 JA |
4744 | #if defined (JOB_CONTROL) |
4745 | if (job_control == 0) | |
4746 | #endif | |
4747 | { | |
4748 | set_signal_handler (SIGINT, SIG_IGN); | |
4749 | set_signal_ignored (SIGINT); | |
4750 | set_signal_handler (SIGQUIT, SIG_IGN); | |
4751 | set_signal_ignored (SIGQUIT); | |
4752 | } | |
4753 | } | |
4754 | ||
4755 | /* Execute a simple command that is hopefully defined in a disk file | |
4756 | somewhere. | |
4757 | ||
4758 | 1) fork () | |
4759 | 2) connect pipes | |
4760 | 3) look up the command | |
4761 | 4) do redirections | |
4762 | 5) execve () | |
4763 | 6) If the execve failed, see if the file has executable mode set. | |
4764 | If so, and it isn't a directory, then execute its contents as | |
4765 | a shell script. | |
4766 | ||
4767 | Note that the filename hashing stuff has to take place up here, | |
4768 | in the parent. This is probably why the Bourne style shells | |
4769 | don't handle it, since that would require them to go through | |
d3a24ed2 CR |
4770 | this gnarly hair, for no good reason. |
4771 | ||
4772 | NOTE: callers expect this to fork or exit(). */ | |
245a493c CR |
4773 | |
4774 | /* Name of a shell function to call when a command name is not found. */ | |
4775 | #ifndef NOTFOUND_HOOK | |
4776 | # define NOTFOUND_HOOK "command_not_found_handle" | |
4777 | #endif | |
4778 | ||
6faad625 | 4779 | static int |
726f6388 | 4780 | execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, |
d166f048 | 4781 | async, fds_to_close, cmdflags) |
726f6388 JA |
4782 | WORD_LIST *words; |
4783 | REDIRECT *redirects; | |
4784 | char *command_line; | |
4785 | int pipe_in, pipe_out, async; | |
4786 | struct fd_bitmap *fds_to_close; | |
d166f048 | 4787 | int cmdflags; |
726f6388 | 4788 | { |
ccc6cda3 | 4789 | char *pathname, *command, **args; |
6faad625 | 4790 | int nofork, result; |
bb70624e | 4791 | pid_t pid; |
245a493c CR |
4792 | SHELL_VAR *hookf; |
4793 | WORD_LIST *wl; | |
726f6388 | 4794 | |
d166f048 | 4795 | nofork = (cmdflags & CMD_NO_FORK); /* Don't fork, just exec, if no pipes */ |
726f6388 | 4796 | pathname = words->word->word; |
ccc6cda3 | 4797 | |
6faad625 | 4798 | result = EXECUTION_SUCCESS; |
726f6388 | 4799 | #if defined (RESTRICTED_SHELL) |
d3a24ed2 | 4800 | command = (char *)NULL; |
d0ca3503 | 4801 | if (restricted && mbschr (pathname, '/')) |
726f6388 | 4802 | { |
5e13499c | 4803 | internal_error (_("%s: restricted: cannot specify `/' in command names"), |
726f6388 | 4804 | pathname); |
6faad625 | 4805 | result = last_command_exit_value = EXECUTION_FAILURE; |
d3a24ed2 CR |
4806 | |
4807 | /* If we're not going to fork below, we must already be in a child | |
4808 | process or a context in which it's safe to call exit(2). */ | |
4809 | if (nofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE) | |
4810 | exit (last_command_exit_value); | |
4811 | else | |
4812 | goto parent_return; | |
726f6388 JA |
4813 | } |
4814 | #endif /* RESTRICTED_SHELL */ | |
4815 | ||
5a318736 | 4816 | command = search_for_command (pathname, 1); |
726f6388 | 4817 | |
ccc6cda3 | 4818 | if (command) |
726f6388 | 4819 | { |
ccc6cda3 JA |
4820 | maybe_make_export_env (); |
4821 | put_command_name_into_env (command); | |
726f6388 JA |
4822 | } |
4823 | ||
bb70624e | 4824 | /* We have to make the child before we check for the non-existence |
726f6388 JA |
4825 | of COMMAND, since we want the error messages to be redirected. */ |
4826 | /* If we can get away without forking and there are no pipes to deal with, | |
4827 | don't bother to fork, just directly exec the command. */ | |
4828 | if (nofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE) | |
4829 | pid = 0; | |
4830 | else | |
4831 | pid = make_child (savestring (command_line), async); | |
4832 | ||
4833 | if (pid == 0) | |
4834 | { | |
4835 | int old_interactive; | |
4836 | ||
d166f048 JA |
4837 | #if 0 |
4838 | /* This has been disabled for the time being. */ | |
ccc6cda3 JA |
4839 | #if !defined (ARG_MAX) || ARG_MAX >= 10240 |
4840 | if (posixly_correct == 0) | |
f73dda09 | 4841 | put_gnu_argv_flags_into_env ((long)getpid (), glob_argv_flags); |
d166f048 | 4842 | #endif |
ccc6cda3 JA |
4843 | #endif |
4844 | ||
9e51a74d | 4845 | reset_terminating_signals (); /* XXX */ |
726f6388 JA |
4846 | /* Cancel traps, in trap.c. */ |
4847 | restore_original_signals (); | |
4848 | ||
4849 | /* restore_original_signals may have undone the work done | |
cce855bc JA |
4850 | by make_child to ensure that SIGINT and SIGQUIT are ignored |
4851 | in asynchronous children. */ | |
726f6388 | 4852 | if (async) |
d166f048 JA |
4853 | { |
4854 | if ((cmdflags & CMD_STDIN_REDIR) && | |
4855 | pipe_in == NO_PIPE && | |
4856 | (stdin_redirects (redirects) == 0)) | |
4857 | async_redirect_stdin (); | |
4858 | setup_async_signals (); | |
4859 | } | |
726f6388 | 4860 | |
bb70624e JA |
4861 | /* This functionality is now provided by close-on-exec of the |
4862 | file descriptors manipulated by redirection and piping. | |
4863 | Some file descriptors still need to be closed in all children | |
4864 | because of the way bash does pipes; fds_to_close is a | |
4865 | bitmap of all such file descriptors. */ | |
4866 | if (fds_to_close) | |
4867 | close_fd_bitmap (fds_to_close); | |
4868 | ||
726f6388 JA |
4869 | do_piping (pipe_in, pipe_out); |
4870 | ||
f73dda09 | 4871 | old_interactive = interactive; |
726f6388 | 4872 | if (async) |
f73dda09 | 4873 | interactive = 0; |
726f6388 | 4874 | |
ccc6cda3 | 4875 | subshell_environment = SUBSHELL_FORK; |
726f6388 | 4876 | |
d3a24ed2 | 4877 | if (redirects && (do_redirections (redirects, RX_ACTIVE) != 0)) |
726f6388 JA |
4878 | { |
4879 | #if defined (PROCESS_SUBSTITUTION) | |
4880 | /* Try to remove named pipes that may have been created as the | |
4881 | result of redirections. */ | |
4882 | unlink_fifo_list (); | |
4883 | #endif /* PROCESS_SUBSTITUTION */ | |
4884 | exit (EXECUTION_FAILURE); | |
4885 | } | |
4886 | ||
4887 | if (async) | |
4888 | interactive = old_interactive; | |
4889 | ||
ccc6cda3 | 4890 | if (command == 0) |
726f6388 | 4891 | { |
245a493c CR |
4892 | hookf = find_function (NOTFOUND_HOOK); |
4893 | if (hookf == 0) | |
4894 | { | |
54a1fa7c CR |
4895 | /* Make sure filenames are displayed using printable characters */ |
4896 | if (ansic_shouldquote (pathname)) | |
4897 | pathname = ansic_quote (pathname, 0, NULL); | |
245a493c CR |
4898 | internal_error (_("%s: command not found"), pathname); |
4899 | exit (EX_NOTFOUND); /* Posix.2 says the exit status is 127 */ | |
4900 | } | |
4901 | ||
4902 | wl = make_word_list (make_word (NOTFOUND_HOOK), words); | |
4903 | exit (execute_shell_function (hookf, wl)); | |
726f6388 JA |
4904 | } |
4905 | ||
ccc6cda3 JA |
4906 | /* Execve expects the command name to be in args[0]. So we |
4907 | leave it there, in the same format that the user used to | |
4908 | type it in. */ | |
7117c2d2 | 4909 | args = strvec_from_word_list (words, 0, 0, (int *)NULL); |
726f6388 JA |
4910 | exit (shell_execve (command, args, export_env)); |
4911 | } | |
4912 | else | |
4913 | { | |
d3a24ed2 | 4914 | parent_return: |
d9e1f41e CR |
4915 | QUIT; |
4916 | ||
726f6388 JA |
4917 | /* Make sure that the pipes are closed in the parent. */ |
4918 | close_pipes (pipe_in, pipe_out); | |
4919 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
6932f7f5 CR |
4920 | if (variable_context == 0) |
4921 | unlink_fifo_list (); | |
726f6388 JA |
4922 | #endif |
4923 | FREE (command); | |
6faad625 | 4924 | return (result); |
726f6388 JA |
4925 | } |
4926 | } | |
4927 | ||
28ef6c31 JA |
4928 | /* CPP defines to decide whether a particular index into the #! line |
4929 | corresponds to a valid interpreter name or argument character, or | |
4930 | whitespace. The MSDOS define is to allow \r to be treated the same | |
4931 | as \n. */ | |
4932 | ||
4933 | #if !defined (MSDOS) | |
4934 | # define STRINGCHAR(ind) \ | |
f73dda09 | 4935 | (ind < sample_len && !whitespace (sample[ind]) && sample[ind] != '\n') |
28ef6c31 | 4936 | # define WHITECHAR(ind) \ |
f73dda09 | 4937 | (ind < sample_len && whitespace (sample[ind])) |
28ef6c31 JA |
4938 | #else /* MSDOS */ |
4939 | # define STRINGCHAR(ind) \ | |
f73dda09 | 4940 | (ind < sample_len && !whitespace (sample[ind]) && sample[ind] != '\n' && sample[ind] != '\r') |
28ef6c31 | 4941 | # define WHITECHAR(ind) \ |
f73dda09 | 4942 | (ind < sample_len && whitespace (sample[ind])) |
28ef6c31 JA |
4943 | #endif /* MSDOS */ |
4944 | ||
f73dda09 JA |
4945 | static char * |
4946 | getinterp (sample, sample_len, endp) | |
4947 | char *sample; | |
4948 | int sample_len, *endp; | |
726f6388 JA |
4949 | { |
4950 | register int i; | |
f73dda09 JA |
4951 | char *execname; |
4952 | int start; | |
726f6388 JA |
4953 | |
4954 | /* Find the name of the interpreter to exec. */ | |
f73dda09 | 4955 | for (i = 2; i < sample_len && whitespace (sample[i]); i++) |
726f6388 JA |
4956 | ; |
4957 | ||
28ef6c31 | 4958 | for (start = i; STRINGCHAR(i); i++) |
726f6388 JA |
4959 | ; |
4960 | ||
f73dda09 JA |
4961 | execname = substring (sample, start, i); |
4962 | ||
4963 | if (endp) | |
4964 | *endp = i; | |
4965 | return execname; | |
4966 | } | |
4967 | ||
4968 | #if !defined (HAVE_HASH_BANG_EXEC) | |
4969 | /* If the operating system on which we're running does not handle | |
4970 | the #! executable format, then help out. SAMPLE is the text read | |
4971 | from the file, SAMPLE_LEN characters. COMMAND is the name of | |
4972 | the script; it and ARGS, the arguments given by the user, will | |
4973 | become arguments to the specified interpreter. ENV is the environment | |
4974 | to pass to the interpreter. | |
4975 | ||
4976 | The word immediately following the #! is the interpreter to execute. | |
4977 | A single argument to the interpreter is allowed. */ | |
4978 | ||
4979 | static int | |
4980 | execute_shell_script (sample, sample_len, command, args, env) | |
4981 | char *sample; | |
4982 | int sample_len; | |
4983 | char *command; | |
4984 | char **args, **env; | |
4985 | { | |
4986 | char *execname, *firstarg; | |
4987 | int i, start, size_increment, larry; | |
4988 | ||
4989 | /* Find the name of the interpreter to exec. */ | |
4990 | execname = getinterp (sample, sample_len, &i); | |
726f6388 JA |
4991 | size_increment = 1; |
4992 | ||
4993 | /* Now the argument, if any. */ | |
28ef6c31 | 4994 | for (firstarg = (char *)NULL, start = i; WHITECHAR(i); i++) |
726f6388 JA |
4995 | ; |
4996 | ||
4997 | /* If there is more text on the line, then it is an argument for the | |
4998 | interpreter. */ | |
28ef6c31 JA |
4999 | |
5000 | if (STRINGCHAR(i)) | |
726f6388 | 5001 | { |
28ef6c31 | 5002 | for (start = i; STRINGCHAR(i); i++) |
726f6388 | 5003 | ; |
bb70624e | 5004 | firstarg = substring ((char *)sample, start, i); |
726f6388 JA |
5005 | size_increment = 2; |
5006 | } | |
5007 | ||
7117c2d2 JA |
5008 | larry = strvec_len (args) + size_increment; |
5009 | args = strvec_resize (args, larry + 1); | |
726f6388 JA |
5010 | |
5011 | for (i = larry - 1; i; i--) | |
5012 | args[i] = args[i - size_increment]; | |
5013 | ||
5014 | args[0] = execname; | |
5015 | if (firstarg) | |
5016 | { | |
5017 | args[1] = firstarg; | |
5018 | args[2] = command; | |
5019 | } | |
5020 | else | |
5021 | args[1] = command; | |
5022 | ||
5023 | args[larry] = (char *)NULL; | |
5024 | ||
5025 | return (shell_execve (execname, args, env)); | |
5026 | } | |
28ef6c31 JA |
5027 | #undef STRINGCHAR |
5028 | #undef WHITECHAR | |
5029 | ||
ccc6cda3 JA |
5030 | #endif /* !HAVE_HASH_BANG_EXEC */ |
5031 | ||
d166f048 JA |
5032 | static void |
5033 | initialize_subshell () | |
5034 | { | |
5035 | #if defined (ALIAS) | |
5036 | /* Forget about any aliases that we knew of. We are in a subshell. */ | |
5037 | delete_all_aliases (); | |
5038 | #endif /* ALIAS */ | |
5039 | ||
5040 | #if defined (HISTORY) | |
5041 | /* Forget about the history lines we have read. This is a non-interactive | |
5042 | subshell. */ | |
5043 | history_lines_this_session = 0; | |
5044 | #endif | |
5045 | ||
5046 | #if defined (JOB_CONTROL) | |
5047 | /* Forget about the way job control was working. We are in a subshell. */ | |
5048 | without_job_control (); | |
5049 | set_sigchld_handler (); | |
ff247e74 | 5050 | init_job_stats (); |
d166f048 JA |
5051 | #endif /* JOB_CONTROL */ |
5052 | ||
5053 | /* Reset the values of the shell flags and options. */ | |
5054 | reset_shell_flags (); | |
5055 | reset_shell_options (); | |
5056 | reset_shopt_options (); | |
5057 | ||
28ef6c31 JA |
5058 | /* Zero out builtin_env, since this could be a shell script run from a |
5059 | sourced file with a temporary environment supplied to the `source/.' | |
5060 | builtin. Such variables are not supposed to be exported (empirical | |
7117c2d2 JA |
5061 | testing with sh and ksh). Just throw it away; don't worry about a |
5062 | memory leak. */ | |
5063 | if (vc_isbltnenv (shell_variables)) | |
5064 | shell_variables = shell_variables->down; | |
28ef6c31 JA |
5065 | |
5066 | clear_unwind_protect_list (0); | |
b246176e CR |
5067 | /* XXX -- are there other things we should be resetting here? */ |
5068 | parse_and_execute_level = 0; /* nothing left to restore it */ | |
28ef6c31 JA |
5069 | |
5070 | /* We're no longer inside a shell function. */ | |
f486d0a1 | 5071 | variable_context = return_catch_flag = funcnest = 0; |
28ef6c31 | 5072 | |
f1c4df24 CR |
5073 | executing_list = 0; /* XXX */ |
5074 | ||
d166f048 JA |
5075 | /* If we're not interactive, close the file descriptor from which we're |
5076 | reading the current shell script. */ | |
cce855bc | 5077 | if (interactive_shell == 0) |
d3a24ed2 | 5078 | unset_bash_input (0); |
d166f048 JA |
5079 | } |
5080 | ||
ccc6cda3 JA |
5081 | #if defined (HAVE_SETOSTYPE) && defined (_POSIX_SOURCE) |
5082 | # define SETOSTYPE(x) __setostype(x) | |
5083 | #else | |
5084 | # define SETOSTYPE(x) | |
5085 | #endif | |
726f6388 | 5086 | |
28ef6c31 JA |
5087 | #define READ_SAMPLE_BUF(file, buf, len) \ |
5088 | do \ | |
5089 | { \ | |
5090 | fd = open(file, O_RDONLY); \ | |
5091 | if (fd >= 0) \ | |
5092 | { \ | |
f73dda09 | 5093 | len = read (fd, buf, 80); \ |
28ef6c31 JA |
5094 | close (fd); \ |
5095 | } \ | |
5096 | else \ | |
5097 | len = -1; \ | |
5098 | } \ | |
5099 | while (0) | |
5100 | ||
726f6388 JA |
5101 | /* Call execve (), handling interpreting shell scripts, and handling |
5102 | exec failures. */ | |
5103 | int | |
5104 | shell_execve (command, args, env) | |
5105 | char *command; | |
5106 | char **args, **env; | |
5107 | { | |
ccc6cda3 | 5108 | int larray, i, fd; |
f73dda09 | 5109 | char sample[80]; |
28ef6c31 | 5110 | int sample_len; |
ccc6cda3 JA |
5111 | |
5112 | SETOSTYPE (0); /* Some systems use for USG/POSIX semantics */ | |
726f6388 | 5113 | execve (command, args, env); |
28ef6c31 | 5114 | i = errno; /* error from execve() */ |
f1c4df24 | 5115 | CHECK_TERMSIG; |
ccc6cda3 | 5116 | SETOSTYPE (1); |
726f6388 JA |
5117 | |
5118 | /* If we get to this point, then start checking out the file. | |
5119 | Maybe it is something we can hack ourselves. */ | |
28ef6c31 | 5120 | if (i != ENOEXEC) |
ccc6cda3 | 5121 | { |
a9fac3b2 | 5122 | if (file_isdir (command)) |
aad3dfa2 CR |
5123 | #if defined (EISDIR) |
5124 | internal_error (_("%s: %s"), command, strerror (EISDIR)); | |
5125 | #else | |
5e13499c | 5126 | internal_error (_("%s: is a directory"), command); |
aad3dfa2 | 5127 | #endif |
7117c2d2 JA |
5128 | else if (executable_file (command) == 0) |
5129 | { | |
5130 | errno = i; | |
5131 | file_error (command); | |
5132 | } | |
668f5007 CR |
5133 | /* errors not involving the path argument to execve. */ |
5134 | else if (i == E2BIG || i == ENOMEM) | |
5135 | { | |
5136 | errno = i; | |
5137 | file_error (command); | |
5138 | } | |
ccc6cda3 JA |
5139 | else |
5140 | { | |
7117c2d2 JA |
5141 | /* The file has the execute bits set, but the kernel refuses to |
5142 | run it for some reason. See why. */ | |
28ef6c31 JA |
5143 | #if defined (HAVE_HASH_BANG_EXEC) |
5144 | READ_SAMPLE_BUF (command, sample, sample_len); | |
631b20c6 | 5145 | sample[sample_len - 1] = '\0'; |
28ef6c31 JA |
5146 | if (sample_len > 2 && sample[0] == '#' && sample[1] == '!') |
5147 | { | |
f73dda09 | 5148 | char *interp; |
bc7bed50 | 5149 | int ilen; |
f73dda09 JA |
5150 | |
5151 | interp = getinterp (sample, sample_len, (int *)NULL); | |
bc7bed50 | 5152 | ilen = strlen (interp); |
28ef6c31 | 5153 | errno = i; |
bc7bed50 CR |
5154 | if (interp[ilen - 1] == '\r') |
5155 | { | |
5156 | interp = xrealloc (interp, ilen + 2); | |
5157 | interp[ilen - 1] = '^'; | |
5158 | interp[ilen] = 'M'; | |
5159 | interp[ilen + 1] = '\0'; | |
5160 | } | |
5e13499c | 5161 | sys_error (_("%s: %s: bad interpreter"), command, interp ? interp : ""); |
f73dda09 | 5162 | FREE (interp); |
28ef6c31 JA |
5163 | return (EX_NOEXEC); |
5164 | } | |
5165 | #endif | |
ccc6cda3 | 5166 | errno = i; |
726f6388 | 5167 | file_error (command); |
ccc6cda3 | 5168 | } |
d166f048 | 5169 | return ((i == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); /* XXX Posix.2 says that exit status is 126 */ |
ccc6cda3 | 5170 | } |
726f6388 | 5171 | |
ccc6cda3 JA |
5172 | /* This file is executable. |
5173 | If it begins with #!, then help out people with losing operating | |
5174 | systems. Otherwise, check to see if it is a binary file by seeing | |
28ef6c31 JA |
5175 | if the contents of the first line (or up to 80 characters) are in the |
5176 | ASCII set. If it's a text file, execute the contents as shell commands, | |
5177 | otherwise return 126 (EX_BINARY_FILE). */ | |
5178 | READ_SAMPLE_BUF (command, sample, sample_len); | |
ccc6cda3 | 5179 | |
28ef6c31 JA |
5180 | if (sample_len == 0) |
5181 | return (EXECUTION_SUCCESS); | |
ccc6cda3 | 5182 | |
28ef6c31 JA |
5183 | /* Is this supposed to be an executable script? |
5184 | If so, the format of the line is "#! interpreter [argument]". | |
5185 | A single argument is allowed. The BSD kernel restricts | |
5186 | the length of the entire line to 32 characters (32 bytes | |
5187 | being the size of the BSD exec header), but we allow 80 | |
5188 | characters. */ | |
5189 | if (sample_len > 0) | |
5190 | { | |
ccc6cda3 | 5191 | #if !defined (HAVE_HASH_BANG_EXEC) |
28ef6c31 JA |
5192 | if (sample_len > 2 && sample[0] == '#' && sample[1] == '!') |
5193 | return (execute_shell_script (sample, sample_len, command, args, env)); | |
5194 | else | |
ccc6cda3 | 5195 | #endif |
28ef6c31 JA |
5196 | if (check_binary_file (sample, sample_len)) |
5197 | { | |
122f603c | 5198 | internal_error (_("%s: cannot execute binary file: %s"), command, strerror (i)); |
28ef6c31 | 5199 | return (EX_BINARY_FILE); |
726f6388 | 5200 | } |
ccc6cda3 JA |
5201 | } |
5202 | ||
28ef6c31 JA |
5203 | /* We have committed to attempting to execute the contents of this file |
5204 | as shell commands. */ | |
5205 | ||
d166f048 | 5206 | initialize_subshell (); |
726f6388 | 5207 | |
ccc6cda3 | 5208 | set_sigint_handler (); |
726f6388 | 5209 | |
ccc6cda3 | 5210 | /* Insert the name of this shell into the argument list. */ |
7117c2d2 JA |
5211 | larray = strvec_len (args) + 1; |
5212 | args = strvec_resize (args, larray + 1); | |
726f6388 | 5213 | |
ccc6cda3 JA |
5214 | for (i = larray - 1; i; i--) |
5215 | args[i] = args[i - 1]; | |
726f6388 | 5216 | |
ccc6cda3 JA |
5217 | args[0] = shell_name; |
5218 | args[1] = command; | |
5219 | args[larray] = (char *)NULL; | |
726f6388 | 5220 | |
ccc6cda3 JA |
5221 | if (args[0][0] == '-') |
5222 | args[0]++; | |
726f6388 | 5223 | |
ccc6cda3 JA |
5224 | #if defined (RESTRICTED_SHELL) |
5225 | if (restricted) | |
5226 | change_flag ('r', FLAG_OFF); | |
5227 | #endif | |
726f6388 | 5228 | |
ccc6cda3 JA |
5229 | if (subshell_argv) |
5230 | { | |
5231 | /* Can't free subshell_argv[0]; that is shell_name. */ | |
5232 | for (i = 1; i < subshell_argc; i++) | |
5233 | free (subshell_argv[i]); | |
5234 | free (subshell_argv); | |
5235 | } | |
5236 | ||
5237 | dispose_command (currently_executing_command); /* XXX */ | |
5238 | currently_executing_command = (COMMAND *)NULL; | |
5239 | ||
5240 | subshell_argc = larray; | |
5241 | subshell_argv = args; | |
5242 | subshell_envp = env; | |
5243 | ||
5244 | unbind_args (); /* remove the positional parameters */ | |
5245 | ||
5246 | longjmp (subshell_top_level, 1); | |
f73dda09 | 5247 | /*NOTREACHED*/ |
ccc6cda3 JA |
5248 | } |
5249 | ||
5250 | static int | |
ba4ab055 | 5251 | execute_intern_function (name, funcdef) |
ccc6cda3 | 5252 | WORD_DESC *name; |
ba4ab055 | 5253 | FUNCTION_DEF *funcdef; |
ccc6cda3 JA |
5254 | { |
5255 | SHELL_VAR *var; | |
5256 | ||
5257 | if (check_identifier (name, posixly_correct) == 0) | |
5258 | { | |
5259 | if (posixly_correct && interactive_shell == 0) | |
5260 | { | |
f2f9854d | 5261 | last_command_exit_value = EX_BADUSAGE; |
5e13499c | 5262 | jump_to_top_level (ERREXIT); |
ccc6cda3 JA |
5263 | } |
5264 | return (EXECUTION_FAILURE); | |
5265 | } | |
5266 | ||
b28ff8c9 CR |
5267 | /* Posix interpretation 383 */ |
5268 | if (posixly_correct && find_special_builtin (name->word)) | |
5269 | { | |
5270 | internal_error (_("`%s': is a special builtin"), name->word); | |
5271 | last_command_exit_value = EX_BADUSAGE; | |
5272 | jump_to_top_level (ERREXIT); | |
5273 | } | |
5274 | ||
ccc6cda3 | 5275 | var = find_function (name->word); |
28ef6c31 | 5276 | if (var && (readonly_p (var) || noassign_p (var))) |
ccc6cda3 | 5277 | { |
28ef6c31 | 5278 | if (readonly_p (var)) |
5e13499c | 5279 | internal_error (_("%s: readonly function"), var->name); |
ccc6cda3 JA |
5280 | return (EXECUTION_FAILURE); |
5281 | } | |
5282 | ||
ba4ab055 CR |
5283 | #if defined (DEBUGGER) |
5284 | bind_function_def (name->word, funcdef); | |
5285 | #endif | |
5286 | ||
5287 | bind_function (name->word, funcdef->command); | |
ccc6cda3 | 5288 | return (EXECUTION_SUCCESS); |
726f6388 JA |
5289 | } |
5290 | ||
d166f048 | 5291 | #if defined (INCLUDE_UNUSED) |
726f6388 | 5292 | #if defined (PROCESS_SUBSTITUTION) |
726f6388 JA |
5293 | void |
5294 | close_all_files () | |
5295 | { | |
5296 | register int i, fd_table_size; | |
5297 | ||
5298 | fd_table_size = getdtablesize (); | |
5299 | if (fd_table_size > 256) /* clamp to a reasonable value */ | |
7117c2d2 | 5300 | fd_table_size = 256; |
726f6388 JA |
5301 | |
5302 | for (i = 3; i < fd_table_size; i++) | |
5303 | close (i); | |
5304 | } | |
5305 | #endif /* PROCESS_SUBSTITUTION */ | |
d166f048 | 5306 | #endif |
726f6388 JA |
5307 | |
5308 | static void | |
5309 | close_pipes (in, out) | |
5310 | int in, out; | |
5311 | { | |
5312 | if (in >= 0) | |
5313 | close (in); | |
5314 | if (out >= 0) | |
5315 | close (out); | |
5316 | } | |
5317 | ||
5e13499c CR |
5318 | static void |
5319 | dup_error (oldd, newd) | |
5320 | int oldd, newd; | |
5321 | { | |
5322 | sys_error (_("cannot duplicate fd %d to fd %d"), oldd, newd); | |
5323 | } | |
5324 | ||
726f6388 JA |
5325 | /* Redirect input and output to be from and to the specified pipes. |
5326 | NO_PIPE and REDIRECT_BOTH are handled correctly. */ | |
5327 | static void | |
5328 | do_piping (pipe_in, pipe_out) | |
5329 | int pipe_in, pipe_out; | |
5330 | { | |
5331 | if (pipe_in != NO_PIPE) | |
5332 | { | |
5333 | if (dup2 (pipe_in, 0) < 0) | |
5e13499c | 5334 | dup_error (pipe_in, 0); |
726f6388 | 5335 | if (pipe_in > 0) |
cce855bc | 5336 | close (pipe_in); |
54a1fa7c CR |
5337 | #ifdef __CYGWIN__ |
5338 | /* Let stdio know the fd may have changed from text to binary mode. */ | |
5339 | freopen (NULL, "r", stdin); | |
5340 | #endif /* __CYGWIN__ */ | |
726f6388 JA |
5341 | } |
5342 | if (pipe_out != NO_PIPE) | |
5343 | { | |
5344 | if (pipe_out != REDIRECT_BOTH) | |
5345 | { | |
5346 | if (dup2 (pipe_out, 1) < 0) | |
5e13499c | 5347 | dup_error (pipe_out, 1); |
726f6388 JA |
5348 | if (pipe_out == 0 || pipe_out > 1) |
5349 | close (pipe_out); | |
5350 | } | |
5351 | else | |
ccc6cda3 | 5352 | { |
cce855bc | 5353 | if (dup2 (1, 2) < 0) |
5e13499c | 5354 | dup_error (1, 2); |
ccc6cda3 | 5355 | } |
54a1fa7c CR |
5356 | #ifdef __CYGWIN__ |
5357 | /* Let stdio know the fd may have changed from text to binary mode, and | |
5358 | make sure to preserve stdout line buffering. */ | |
5359 | freopen (NULL, "w", stdout); | |
5360 | sh_setlinebuf (stdout); | |
5361 | #endif /* __CYGWIN__ */ | |
d166f048 | 5362 | } |
726f6388 | 5363 | } |