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