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