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