]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | /* execute_command.c -- Execute a COMMAND structure. */ |
2 | ||
3 | /* Copyright (C) 1987,1991 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
7 | Bash is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by | |
bb70624e | 9 | the Free Software Foundation; either version 2, or (at your option) |
726f6388 JA |
10 | any later version. |
11 | ||
12 | Bash is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
15 | License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Bash; see the file COPYING. If not, write to the Free | |
bb70624e | 19 | Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
ccc6cda3 JA |
20 | #include "config.h" |
21 | ||
22 | #if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX) | |
726f6388 | 23 | #pragma alloca |
ccc6cda3 | 24 | #endif /* _AIX && RISC6000 && !__GNUC__ */ |
726f6388 JA |
25 | |
26 | #include <stdio.h> | |
27 | #include <ctype.h> | |
28 | #include "bashtypes.h" | |
cce855bc JA |
29 | #ifndef _MINIX |
30 | # include <sys/file.h> | |
31 | #endif | |
726f6388 JA |
32 | #include "filecntl.h" |
33 | #include "posixstat.h" | |
34 | #include <signal.h> | |
cce855bc JA |
35 | #ifndef _MINIX |
36 | # include <sys/param.h> | |
37 | #endif | |
726f6388 | 38 | |
ccc6cda3 JA |
39 | #if defined (HAVE_UNISTD_H) |
40 | # include <unistd.h> | |
41 | #endif | |
42 | ||
43 | #if defined (HAVE_LIMITS_H) | |
44 | # include <limits.h> | |
45 | #endif | |
46 | ||
bb70624e | 47 | #include "posixtime.h" |
ccc6cda3 | 48 | |
cce855bc | 49 | #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE) |
ccc6cda3 JA |
50 | # include <sys/resource.h> |
51 | #endif | |
52 | ||
53 | #if defined (HAVE_SYS_TIMES_H) && defined (HAVE_TIMES) | |
54 | # include <sys/times.h> | |
726f6388 JA |
55 | #endif |
56 | ||
726f6388 JA |
57 | #include <errno.h> |
58 | ||
59 | #if !defined (errno) | |
60 | extern int errno; | |
61 | #endif | |
62 | ||
ccc6cda3 | 63 | #include "bashansi.h" |
726f6388 | 64 | |
ccc6cda3 | 65 | #include "memalloc.h" |
726f6388 | 66 | #include "shell.h" |
d166f048 | 67 | #include <y.tab.h> /* use <...> so we pick it up from the build directory */ |
726f6388 | 68 | #include "flags.h" |
ccc6cda3 JA |
69 | #include "builtins.h" |
70 | #include "hashlib.h" | |
726f6388 JA |
71 | #include "jobs.h" |
72 | #include "execute_cmd.h" | |
cce855bc JA |
73 | #include "findcmd.h" |
74 | #include "redir.h" | |
ccc6cda3 JA |
75 | #include "trap.h" |
76 | #include "pathexp.h" | |
d166f048 | 77 | #include "hashcmd.h" |
726f6388 | 78 | |
cce855bc JA |
79 | #if defined (COND_COMMAND) |
80 | # include "test.h" | |
81 | #endif | |
82 | ||
726f6388 JA |
83 | #include "builtins/common.h" |
84 | #include "builtins/builtext.h" /* list of builtins */ | |
85 | ||
86 | #include <glob/fnmatch.h> | |
87 | #include <tilde/tilde.h> | |
88 | ||
89 | #if defined (BUFFERED_INPUT) | |
90 | # include "input.h" | |
91 | #endif | |
92 | ||
ccc6cda3 JA |
93 | #if defined (ALIAS) |
94 | # include "alias.h" | |
95 | #endif | |
96 | ||
97 | #if defined (HISTORY) | |
98 | # include "bashhist.h" | |
99 | #endif | |
100 | ||
726f6388 | 101 | extern int posixly_correct; |
ccc6cda3 JA |
102 | extern int executing, breaking, continuing, loop_level; |
103 | extern int interactive, interactive_shell, login_shell, expand_aliases; | |
d166f048 | 104 | extern int parse_and_execute_level, running_trap, trap_line_number; |
726f6388 JA |
105 | extern int command_string_index, variable_context, line_number; |
106 | extern int dot_found_in_search; | |
ccc6cda3 | 107 | extern int already_making_children; |
726f6388 JA |
108 | extern char **temporary_env, **function_env, **builtin_env; |
109 | extern char *the_printed_command, *shell_name; | |
110 | extern pid_t last_command_subst_pid; | |
111 | extern Function *last_shell_builtin, *this_shell_builtin; | |
726f6388 | 112 | extern char **subshell_argv, **subshell_envp; |
ccc6cda3 JA |
113 | extern int subshell_argc; |
114 | extern char *glob_argv_flags; | |
726f6388 JA |
115 | |
116 | extern int getdtablesize (); | |
117 | extern int close (); | |
118 | ||
119 | /* Static functions defined and used in this file. */ | |
ccc6cda3 JA |
120 | static void close_pipes (), do_piping (), bind_lastarg (); |
121 | static void cleanup_redirects (); | |
ccc6cda3 JA |
122 | |
123 | static int execute_for_command (); | |
124 | #if defined (SELECT_COMMAND) | |
125 | static int execute_select_command (); | |
126 | #endif | |
cce855bc JA |
127 | #if defined (DPAREN_ARITHMETIC) |
128 | static int execute_arith_command (); | |
129 | #endif | |
130 | #if defined (COND_COMMAND) | |
131 | static int execute_cond_command (); | |
132 | #endif | |
d166f048 | 133 | #if defined (COMMAND_TIMING) |
ccc6cda3 | 134 | static int time_command (); |
d166f048 | 135 | #endif |
bb70624e JA |
136 | #if defined (ARITH_FOR_COMMAND) |
137 | static int execute_arith_for_command (); | |
138 | #endif | |
ccc6cda3 JA |
139 | static int execute_case_command (); |
140 | static int execute_while_command (), execute_until_command (); | |
141 | static int execute_while_or_until (); | |
142 | static int execute_if_command (); | |
143 | static int execute_simple_command (); | |
144 | static int execute_builtin (), execute_function (); | |
145 | static int execute_builtin_or_function (); | |
146 | static int builtin_status (); | |
147 | static void execute_subshell_builtin_or_function (); | |
148 | static void execute_disk_command (); | |
149 | static int execute_connection (); | |
150 | static int execute_intern_function (); | |
726f6388 | 151 | |
bb70624e JA |
152 | static int execute_in_subshell (); |
153 | ||
726f6388 | 154 | /* The line number that the currently executing function starts on. */ |
ccc6cda3 | 155 | static int function_line_number; |
726f6388 | 156 | |
d166f048 JA |
157 | /* Set to 1 if fd 0 was the subject of redirection to a subshell. Global |
158 | so that reader_loop can set it to zero before executing a command. */ | |
159 | int stdin_redir; | |
726f6388 JA |
160 | |
161 | /* The name of the command that is currently being executed. | |
162 | `test' needs this, for example. */ | |
163 | char *this_command_name; | |
164 | ||
ccc6cda3 JA |
165 | static COMMAND *currently_executing_command; |
166 | ||
726f6388 JA |
167 | struct stat SB; /* used for debugging */ |
168 | ||
ccc6cda3 | 169 | static int special_builtin_failed; |
d166f048 | 170 | |
726f6388 | 171 | /* For catching RETURN in a function. */ |
ccc6cda3 | 172 | int return_catch_flag; |
726f6388 | 173 | int return_catch_value; |
ccc6cda3 | 174 | procenv_t return_catch; |
726f6388 JA |
175 | |
176 | /* The value returned by the last synchronous command. */ | |
ccc6cda3 | 177 | int last_command_exit_value; |
726f6388 JA |
178 | |
179 | /* The list of redirections to perform which will undo the redirections | |
180 | that I made in the shell. */ | |
181 | REDIRECT *redirection_undo_list = (REDIRECT *)NULL; | |
182 | ||
183 | /* The list of redirections to perform which will undo the internal | |
184 | redirections performed by the `exec' builtin. These are redirections | |
185 | that must be undone even when exec discards redirection_undo_list. */ | |
186 | REDIRECT *exec_redirection_undo_list = (REDIRECT *)NULL; | |
187 | ||
188 | /* Non-zero if we have just forked and are currently running in a subshell | |
189 | environment. */ | |
ccc6cda3 JA |
190 | int subshell_environment; |
191 | ||
bb70624e JA |
192 | /* Currently-executing shell function. */ |
193 | SHELL_VAR *this_shell_function; | |
194 | ||
726f6388 JA |
195 | struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL; |
196 | ||
d166f048 JA |
197 | #define FD_BITMAP_DEFAULT_SIZE 32L |
198 | ||
726f6388 JA |
199 | /* Functions to allocate and deallocate the structures used to pass |
200 | information from the shell to its children about file descriptors | |
201 | to close. */ | |
202 | struct fd_bitmap * | |
203 | new_fd_bitmap (size) | |
204 | long size; | |
205 | { | |
206 | struct fd_bitmap *ret; | |
207 | ||
208 | ret = (struct fd_bitmap *)xmalloc (sizeof (struct fd_bitmap)); | |
209 | ||
210 | ret->size = size; | |
211 | ||
212 | if (size) | |
213 | { | |
214 | ret->bitmap = xmalloc (size); | |
215 | bzero (ret->bitmap, size); | |
216 | } | |
217 | else | |
218 | ret->bitmap = (char *)NULL; | |
219 | return (ret); | |
220 | } | |
221 | ||
222 | void | |
223 | dispose_fd_bitmap (fdbp) | |
224 | struct fd_bitmap *fdbp; | |
225 | { | |
226 | FREE (fdbp->bitmap); | |
227 | free (fdbp); | |
228 | } | |
229 | ||
230 | void | |
231 | close_fd_bitmap (fdbp) | |
232 | struct fd_bitmap *fdbp; | |
233 | { | |
234 | register int i; | |
235 | ||
236 | if (fdbp) | |
237 | { | |
238 | for (i = 0; i < fdbp->size; i++) | |
239 | if (fdbp->bitmap[i]) | |
240 | { | |
241 | close (i); | |
242 | fdbp->bitmap[i] = 0; | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
ccc6cda3 JA |
247 | /* Return the line number of the currently executing command. */ |
248 | int | |
249 | executing_line_number () | |
250 | { | |
251 | if (executing && variable_context == 0 && currently_executing_command && | |
252 | currently_executing_command->type == cm_simple) | |
253 | return currently_executing_command->value.Simple->line; | |
d166f048 JA |
254 | else if (running_trap) |
255 | return trap_line_number; | |
256 | else | |
257 | return line_number; | |
ccc6cda3 JA |
258 | } |
259 | ||
726f6388 JA |
260 | /* Execute the command passed in COMMAND. COMMAND is exactly what |
261 | read_command () places into GLOBAL_COMMAND. See "command.h" for the | |
262 | details of the command structure. | |
263 | ||
264 | EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible | |
265 | return values. Executing a command with nothing in it returns | |
266 | EXECUTION_SUCCESS. */ | |
ccc6cda3 | 267 | int |
726f6388 JA |
268 | execute_command (command) |
269 | COMMAND *command; | |
270 | { | |
271 | struct fd_bitmap *bitmap; | |
272 | int result; | |
273 | ||
274 | current_fds_to_close = (struct fd_bitmap *)NULL; | |
275 | bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE); | |
276 | begin_unwind_frame ("execute-command"); | |
277 | add_unwind_protect (dispose_fd_bitmap, (char *)bitmap); | |
278 | ||
279 | /* Just do the command, but not asynchronously. */ | |
280 | result = execute_command_internal (command, 0, NO_PIPE, NO_PIPE, bitmap); | |
281 | ||
282 | dispose_fd_bitmap (bitmap); | |
283 | discard_unwind_frame ("execute-command"); | |
284 | ||
285 | #if defined (PROCESS_SUBSTITUTION) | |
bb70624e JA |
286 | /* don't unlink fifos if we're in a shell function; wait until the function |
287 | returns. */ | |
288 | if (variable_context == 0) | |
289 | unlink_fifo_list (); | |
726f6388 JA |
290 | #endif /* PROCESS_SUBSTITUTION */ |
291 | ||
292 | return (result); | |
293 | } | |
294 | ||
295 | /* Return 1 if TYPE is a shell control structure type. */ | |
296 | static int | |
297 | shell_control_structure (type) | |
298 | enum command_type type; | |
299 | { | |
300 | switch (type) | |
301 | { | |
302 | case cm_for: | |
bb70624e JA |
303 | #if defined (ARITH_FOR_COMMAND) |
304 | case cm_arith_for: | |
305 | #endif | |
726f6388 JA |
306 | #if defined (SELECT_COMMAND) |
307 | case cm_select: | |
cce855bc JA |
308 | #endif |
309 | #if defined (DPAREN_ARITHMETIC) | |
310 | case cm_arith: | |
311 | #endif | |
312 | #if defined (COND_COMMAND) | |
313 | case cm_cond: | |
726f6388 JA |
314 | #endif |
315 | case cm_case: | |
316 | case cm_while: | |
317 | case cm_until: | |
318 | case cm_if: | |
319 | case cm_group: | |
320 | return (1); | |
321 | ||
322 | default: | |
323 | return (0); | |
324 | } | |
325 | } | |
326 | ||
327 | /* A function to use to unwind_protect the redirection undo list | |
328 | for loops. */ | |
329 | static void | |
330 | cleanup_redirects (list) | |
331 | REDIRECT *list; | |
332 | { | |
333 | do_redirections (list, 1, 0, 0); | |
334 | dispose_redirects (list); | |
335 | } | |
336 | ||
ccc6cda3 | 337 | #if 0 |
726f6388 JA |
338 | /* Function to unwind_protect the redirections for functions and builtins. */ |
339 | static void | |
340 | cleanup_func_redirects (list) | |
341 | REDIRECT *list; | |
342 | { | |
343 | do_redirections (list, 1, 0, 0); | |
344 | } | |
ccc6cda3 | 345 | #endif |
726f6388 | 346 | |
cce855bc | 347 | void |
726f6388 JA |
348 | dispose_exec_redirects () |
349 | { | |
350 | if (exec_redirection_undo_list) | |
351 | { | |
352 | dispose_redirects (exec_redirection_undo_list); | |
353 | exec_redirection_undo_list = (REDIRECT *)NULL; | |
354 | } | |
355 | } | |
356 | ||
357 | #if defined (JOB_CONTROL) | |
358 | /* A function to restore the signal mask to its proper value when the shell | |
359 | is interrupted or errors occur while creating a pipeline. */ | |
360 | static int | |
361 | restore_signal_mask (set) | |
362 | sigset_t set; | |
363 | { | |
364 | return (sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL)); | |
365 | } | |
366 | #endif /* JOB_CONTROL */ | |
367 | ||
368 | /* A debugging function that can be called from gdb, for instance. */ | |
369 | void | |
370 | open_files () | |
371 | { | |
372 | register int i; | |
373 | int f, fd_table_size; | |
374 | ||
375 | fd_table_size = getdtablesize (); | |
376 | ||
ccc6cda3 | 377 | fprintf (stderr, "pid %d open files:", (int)getpid ()); |
726f6388 JA |
378 | for (i = 3; i < fd_table_size; i++) |
379 | { | |
380 | if ((f = fcntl (i, F_GETFD, 0)) != -1) | |
381 | fprintf (stderr, " %d (%s)", i, f ? "close" : "open"); | |
382 | } | |
383 | fprintf (stderr, "\n"); | |
384 | } | |
385 | ||
d166f048 JA |
386 | static void |
387 | async_redirect_stdin () | |
388 | { | |
389 | int fd; | |
390 | ||
391 | fd = open ("/dev/null", O_RDONLY); | |
392 | if (fd > 0) | |
393 | { | |
394 | dup2 (fd, 0); | |
395 | close (fd); | |
396 | } | |
397 | else if (fd < 0) | |
398 | internal_error ("cannot redirect standard input from /dev/null: %s", strerror (errno)); | |
399 | } | |
400 | ||
ccc6cda3 | 401 | #define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0) |
726f6388 JA |
402 | |
403 | /* Execute the command passed in COMMAND, perhaps doing it asynchrounously. | |
404 | COMMAND is exactly what read_command () places into GLOBAL_COMMAND. | |
405 | ASYNCHROUNOUS, if non-zero, says to do this command in the background. | |
406 | PIPE_IN and PIPE_OUT are file descriptors saying where input comes | |
407 | from and where it goes. They can have the value of NO_PIPE, which means | |
408 | I/O is stdin/stdout. | |
409 | FDS_TO_CLOSE is a list of file descriptors to close once the child has | |
410 | been forked. This list often contains the unusable sides of pipes, etc. | |
411 | ||
412 | EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible | |
413 | return values. Executing a command with nothing in it returns | |
414 | EXECUTION_SUCCESS. */ | |
ccc6cda3 JA |
415 | int |
416 | execute_command_internal (command, asynchronous, pipe_in, pipe_out, | |
726f6388 JA |
417 | fds_to_close) |
418 | COMMAND *command; | |
419 | int asynchronous; | |
420 | int pipe_in, pipe_out; | |
421 | struct fd_bitmap *fds_to_close; | |
422 | { | |
ccc6cda3 | 423 | int exec_result, invert, ignore_return, was_debug_trap; |
d166f048 | 424 | REDIRECT *my_undo_list, *exec_undo_list; |
cce855bc | 425 | volatile pid_t last_pid; |
726f6388 | 426 | |
ccc6cda3 | 427 | if (command == 0 || breaking || continuing || read_but_dont_execute) |
726f6388 JA |
428 | return (EXECUTION_SUCCESS); |
429 | ||
430 | run_pending_traps (); | |
431 | ||
ccc6cda3 JA |
432 | if (running_trap == 0) |
433 | currently_executing_command = command; | |
434 | ||
726f6388 | 435 | invert = (command->flags & CMD_INVERT_RETURN) != 0; |
d166f048 JA |
436 | |
437 | /* If we're inverting the return value and `set -e' has been executed, | |
438 | we don't want a failing command to inadvertently cause the shell | |
439 | to exit. */ | |
440 | if (exit_immediately_on_error && invert) /* XXX */ | |
441 | command->flags |= CMD_IGNORE_RETURN; /* XXX */ | |
442 | ||
ccc6cda3 | 443 | exec_result = EXECUTION_SUCCESS; |
726f6388 JA |
444 | |
445 | /* If a command was being explicitly run in a subshell, or if it is | |
446 | a shell control-structure, and it has a pipe, then we do the command | |
447 | in a subshell. */ | |
bb70624e JA |
448 | if (command->type == cm_subshell && (command->flags & CMD_NO_FORK)) |
449 | return (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close)); | |
726f6388 | 450 | |
bb70624e JA |
451 | if (command->type == cm_subshell || |
452 | (command->flags & (CMD_WANT_SUBSHELL|CMD_FORCE_SUBSHELL)) || | |
726f6388 JA |
453 | (shell_control_structure (command->type) && |
454 | (pipe_out != NO_PIPE || pipe_in != NO_PIPE || asynchronous))) | |
455 | { | |
456 | pid_t paren_pid; | |
457 | ||
458 | /* Fork a subshell, turn off the subshell bit, turn off job | |
459 | control and call execute_command () on the command again. */ | |
460 | paren_pid = make_child (savestring (make_command_string (command)), | |
461 | asynchronous); | |
462 | if (paren_pid == 0) | |
28ef6c31 JA |
463 | exit (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close)); |
464 | /* NOTREACHED */ | |
726f6388 JA |
465 | else |
466 | { | |
467 | close_pipes (pipe_in, pipe_out); | |
468 | ||
469 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
470 | unlink_fifo_list (); | |
471 | #endif | |
472 | /* If we are part of a pipeline, and not the end of the pipeline, | |
473 | then we should simply return and let the last command in the | |
474 | pipe be waited for. If we are not in a pipeline, or are the | |
ccc6cda3 | 475 | last command in the pipeline, then we wait for the subshell |
726f6388 JA |
476 | and return its exit status as usual. */ |
477 | if (pipe_out != NO_PIPE) | |
478 | return (EXECUTION_SUCCESS); | |
479 | ||
480 | stop_pipeline (asynchronous, (COMMAND *)NULL); | |
481 | ||
ccc6cda3 | 482 | if (asynchronous == 0) |
726f6388 JA |
483 | { |
484 | last_command_exit_value = wait_for (paren_pid); | |
485 | ||
486 | /* If we have to, invert the return value. */ | |
487 | if (invert) | |
bb70624e JA |
488 | exec_result = ((last_command_exit_value == EXECUTION_SUCCESS) |
489 | ? EXECUTION_FAILURE | |
490 | : EXECUTION_SUCCESS); | |
726f6388 | 491 | else |
bb70624e JA |
492 | exec_result = last_command_exit_value; |
493 | ||
494 | return (last_command_exit_value = exec_result); | |
726f6388 JA |
495 | } |
496 | else | |
497 | { | |
498 | DESCRIBE_PID (paren_pid); | |
499 | ||
500 | run_pending_traps (); | |
501 | ||
502 | return (EXECUTION_SUCCESS); | |
503 | } | |
504 | } | |
505 | } | |
506 | ||
d166f048 JA |
507 | #if defined (COMMAND_TIMING) |
508 | if (command->flags & CMD_TIME_PIPELINE) | |
509 | { | |
510 | if (asynchronous) | |
511 | { | |
512 | command->flags |= CMD_FORCE_SUBSHELL; | |
513 | exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close); | |
514 | } | |
515 | else | |
516 | { | |
517 | exec_result = time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close); | |
518 | if (running_trap == 0) | |
519 | currently_executing_command = (COMMAND *)NULL; | |
520 | } | |
521 | return (exec_result); | |
522 | } | |
523 | #endif /* COMMAND_TIMING */ | |
524 | ||
525 | if (shell_control_structure (command->type) && command->redirects) | |
526 | stdin_redir = stdin_redirects (command->redirects); | |
527 | ||
726f6388 JA |
528 | /* Handle WHILE FOR CASE etc. with redirections. (Also '&' input |
529 | redirection.) */ | |
530 | if (do_redirections (command->redirects, 1, 1, 0) != 0) | |
531 | { | |
532 | cleanup_redirects (redirection_undo_list); | |
533 | redirection_undo_list = (REDIRECT *)NULL; | |
534 | dispose_exec_redirects (); | |
535 | return (EXECUTION_FAILURE); | |
536 | } | |
537 | ||
538 | if (redirection_undo_list) | |
539 | { | |
540 | my_undo_list = (REDIRECT *)copy_redirects (redirection_undo_list); | |
541 | dispose_redirects (redirection_undo_list); | |
542 | redirection_undo_list = (REDIRECT *)NULL; | |
543 | } | |
544 | else | |
545 | my_undo_list = (REDIRECT *)NULL; | |
546 | ||
547 | if (exec_redirection_undo_list) | |
548 | { | |
549 | exec_undo_list = (REDIRECT *)copy_redirects (exec_redirection_undo_list); | |
550 | dispose_redirects (exec_redirection_undo_list); | |
551 | exec_redirection_undo_list = (REDIRECT *)NULL; | |
552 | } | |
553 | else | |
554 | exec_undo_list = (REDIRECT *)NULL; | |
555 | ||
556 | if (my_undo_list || exec_undo_list) | |
557 | begin_unwind_frame ("loop_redirections"); | |
558 | ||
559 | if (my_undo_list) | |
560 | add_unwind_protect ((Function *)cleanup_redirects, my_undo_list); | |
561 | ||
562 | if (exec_undo_list) | |
563 | add_unwind_protect ((Function *)dispose_redirects, exec_undo_list); | |
564 | ||
565 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; | |
566 | ||
567 | QUIT; | |
568 | ||
569 | switch (command->type) | |
570 | { | |
ccc6cda3 JA |
571 | case cm_simple: |
572 | { | |
573 | /* We can't rely on this variable retaining its value across a | |
574 | call to execute_simple_command if a longjmp occurs as the | |
575 | result of a `return' builtin. This is true for sure with gcc. */ | |
576 | last_pid = last_made_pid; | |
577 | was_debug_trap = signal_is_trapped (DEBUG_TRAP) && signal_is_ignored (DEBUG_TRAP) == 0; | |
578 | ||
579 | if (ignore_return && command->value.Simple) | |
580 | command->value.Simple->flags |= CMD_IGNORE_RETURN; | |
d166f048 JA |
581 | if (command->flags & CMD_STDIN_REDIR) |
582 | command->value.Simple->flags |= CMD_STDIN_REDIR; | |
ccc6cda3 JA |
583 | exec_result = |
584 | execute_simple_command (command->value.Simple, pipe_in, pipe_out, | |
585 | asynchronous, fds_to_close); | |
586 | ||
587 | /* The temporary environment should be used for only the simple | |
588 | command immediately following its definition. */ | |
589 | dispose_used_env_vars (); | |
590 | ||
591 | #if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA) | |
592 | /* Reclaim memory allocated with alloca () on machines which | |
593 | may be using the alloca emulation code. */ | |
594 | (void) alloca (0); | |
595 | #endif /* (ultrix && mips) || C_ALLOCA */ | |
596 | ||
597 | /* If we forked to do the command, then we must wait_for () | |
598 | the child. */ | |
599 | ||
600 | /* XXX - this is something to watch out for if there are problems | |
601 | when the shell is compiled without job control. */ | |
602 | if (already_making_children && pipe_out == NO_PIPE && | |
603 | last_pid != last_made_pid) | |
604 | { | |
605 | stop_pipeline (asynchronous, (COMMAND *)NULL); | |
606 | ||
607 | if (asynchronous) | |
608 | { | |
609 | DESCRIBE_PID (last_made_pid); | |
610 | } | |
611 | else | |
612 | #if !defined (JOB_CONTROL) | |
613 | /* Do not wait for asynchronous processes started from | |
614 | startup files. */ | |
615 | if (last_made_pid != last_asynchronous_pid) | |
616 | #endif | |
617 | /* When executing a shell function that executes other | |
618 | commands, this causes the last simple command in | |
619 | the function to be waited for twice. */ | |
620 | exec_result = wait_for (last_made_pid); | |
b72432fd JA |
621 | #if defined (RECYCLES_PIDS) |
622 | /* LynxOS, for one, recycles pids very quickly -- so quickly | |
623 | that a new process may have the same pid as the last one | |
624 | created. This has been reported to fix the problem. */ | |
625 | if (exec_result == 0) | |
626 | last_made_pid = NO_PID; | |
627 | #endif | |
ccc6cda3 JA |
628 | } |
629 | } | |
630 | ||
631 | if (was_debug_trap) | |
bb70624e JA |
632 | { |
633 | last_command_exit_value = exec_result; | |
634 | run_debug_trap (); | |
635 | } | |
ccc6cda3 JA |
636 | |
637 | if (ignore_return == 0 && invert == 0 && | |
cce855bc | 638 | ((posixly_correct && interactive == 0 && special_builtin_failed) || |
ccc6cda3 JA |
639 | (exit_immediately_on_error && (exec_result != EXECUTION_SUCCESS)))) |
640 | { | |
641 | last_command_exit_value = exec_result; | |
642 | run_pending_traps (); | |
643 | jump_to_top_level (EXITPROG); | |
644 | } | |
645 | ||
646 | break; | |
647 | ||
726f6388 JA |
648 | case cm_for: |
649 | if (ignore_return) | |
650 | command->value.For->flags |= CMD_IGNORE_RETURN; | |
651 | exec_result = execute_for_command (command->value.For); | |
652 | break; | |
653 | ||
bb70624e JA |
654 | #if defined (ARITH_FOR_COMMAND) |
655 | case cm_arith_for: | |
656 | if (ignore_return) | |
657 | command->value.ArithFor->flags |= CMD_IGNORE_RETURN; | |
658 | exec_result = execute_arith_for_command (command->value.ArithFor); | |
659 | break; | |
660 | #endif | |
661 | ||
726f6388 JA |
662 | #if defined (SELECT_COMMAND) |
663 | case cm_select: | |
664 | if (ignore_return) | |
665 | command->value.Select->flags |= CMD_IGNORE_RETURN; | |
666 | exec_result = execute_select_command (command->value.Select); | |
667 | break; | |
668 | #endif | |
669 | ||
670 | case cm_case: | |
671 | if (ignore_return) | |
672 | command->value.Case->flags |= CMD_IGNORE_RETURN; | |
673 | exec_result = execute_case_command (command->value.Case); | |
674 | break; | |
675 | ||
676 | case cm_while: | |
677 | if (ignore_return) | |
678 | command->value.While->flags |= CMD_IGNORE_RETURN; | |
679 | exec_result = execute_while_command (command->value.While); | |
680 | break; | |
681 | ||
682 | case cm_until: | |
683 | if (ignore_return) | |
684 | command->value.While->flags |= CMD_IGNORE_RETURN; | |
685 | exec_result = execute_until_command (command->value.While); | |
686 | break; | |
687 | ||
688 | case cm_if: | |
689 | if (ignore_return) | |
690 | command->value.If->flags |= CMD_IGNORE_RETURN; | |
691 | exec_result = execute_if_command (command->value.If); | |
692 | break; | |
693 | ||
694 | case cm_group: | |
695 | ||
696 | /* This code can be executed from either of two paths: an explicit | |
697 | '{}' command, or via a function call. If we are executed via a | |
698 | function call, we have already taken care of the function being | |
699 | executed in the background (down there in execute_simple_command ()), | |
700 | and this command should *not* be marked as asynchronous. If we | |
701 | are executing a regular '{}' group command, and asynchronous == 1, | |
702 | we must want to execute the whole command in the background, so we | |
703 | need a subshell, and we want the stuff executed in that subshell | |
704 | (this group command) to be executed in the foreground of that | |
705 | subshell (i.e. there will not be *another* subshell forked). | |
706 | ||
707 | What we do is to force a subshell if asynchronous, and then call | |
708 | execute_command_internal again with asynchronous still set to 1, | |
709 | but with the original group command, so the printed command will | |
710 | look right. | |
711 | ||
712 | The code above that handles forking off subshells will note that | |
713 | both subshell and async are on, and turn off async in the child | |
714 | after forking the subshell (but leave async set in the parent, so | |
715 | the normal call to describe_pid is made). This turning off | |
716 | async is *crucial*; if it is not done, this will fall into an | |
717 | infinite loop of executions through this spot in subshell after | |
718 | subshell until the process limit is exhausted. */ | |
719 | ||
720 | if (asynchronous) | |
721 | { | |
722 | command->flags |= CMD_FORCE_SUBSHELL; | |
723 | exec_result = | |
724 | execute_command_internal (command, 1, pipe_in, pipe_out, | |
725 | fds_to_close); | |
726 | } | |
727 | else | |
728 | { | |
729 | if (ignore_return && command->value.Group->command) | |
730 | command->value.Group->command->flags |= CMD_IGNORE_RETURN; | |
731 | exec_result = | |
732 | execute_command_internal (command->value.Group->command, | |
733 | asynchronous, pipe_in, pipe_out, | |
734 | fds_to_close); | |
735 | } | |
736 | break; | |
737 | ||
ccc6cda3 JA |
738 | case cm_connection: |
739 | exec_result = execute_connection (command, asynchronous, | |
740 | pipe_in, pipe_out, fds_to_close); | |
741 | break; | |
726f6388 | 742 | |
cce855bc JA |
743 | #if defined (DPAREN_ARITHMETIC) |
744 | case cm_arith: | |
745 | if (ignore_return) | |
746 | command->value.Arith->flags |= CMD_IGNORE_RETURN; | |
747 | exec_result = execute_arith_command (command->value.Arith); | |
748 | break; | |
749 | #endif | |
750 | ||
751 | #if defined (COND_COMMAND) | |
752 | case cm_cond: | |
753 | if (ignore_return) | |
754 | command->value.Cond->flags |= CMD_IGNORE_RETURN; | |
755 | exec_result = execute_cond_command (command->value.Cond); | |
756 | break; | |
757 | #endif | |
758 | ||
ccc6cda3 JA |
759 | case cm_function_def: |
760 | exec_result = execute_intern_function (command->value.Function_def->name, | |
761 | command->value.Function_def->command); | |
762 | break; | |
726f6388 | 763 | |
ccc6cda3 | 764 | default: |
b72432fd | 765 | command_error ("execute_command", CMDERR_BADTYPE, command->type, 0); |
ccc6cda3 | 766 | } |
726f6388 | 767 | |
ccc6cda3 JA |
768 | if (my_undo_list) |
769 | { | |
770 | do_redirections (my_undo_list, 1, 0, 0); | |
771 | dispose_redirects (my_undo_list); | |
772 | } | |
726f6388 | 773 | |
ccc6cda3 JA |
774 | if (exec_undo_list) |
775 | dispose_redirects (exec_undo_list); | |
726f6388 | 776 | |
ccc6cda3 JA |
777 | if (my_undo_list || exec_undo_list) |
778 | discard_unwind_frame ("loop_redirections"); | |
726f6388 | 779 | |
ccc6cda3 JA |
780 | /* Invert the return value if we have to */ |
781 | if (invert) | |
782 | exec_result = (exec_result == EXECUTION_SUCCESS) | |
783 | ? EXECUTION_FAILURE | |
784 | : EXECUTION_SUCCESS; | |
726f6388 | 785 | |
ccc6cda3 JA |
786 | last_command_exit_value = exec_result; |
787 | run_pending_traps (); | |
788 | if (running_trap == 0) | |
789 | currently_executing_command = (COMMAND *)NULL; | |
790 | return (last_command_exit_value); | |
791 | } | |
726f6388 | 792 | |
ccc6cda3 | 793 | #if defined (COMMAND_TIMING) |
726f6388 | 794 | |
bb70624e JA |
795 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) |
796 | extern struct timeval *difftimeval(); | |
797 | extern struct timeval *addtimeval(); | |
798 | extern int timeval_to_cpu(); | |
799 | #endif | |
726f6388 | 800 | |
ccc6cda3 JA |
801 | #define POSIX_TIMEFORMAT "real %2R\nuser %2U\nsys %2S" |
802 | #define BASH_TIMEFORMAT "\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS" | |
726f6388 | 803 | |
ccc6cda3 | 804 | static int precs[] = { 0, 100, 10, 1 }; |
726f6388 | 805 | |
ccc6cda3 JA |
806 | /* Expand one `%'-prefixed escape sequence from a time format string. */ |
807 | static int | |
808 | mkfmt (buf, prec, lng, sec, sec_fraction) | |
809 | char *buf; | |
810 | int prec, lng; | |
811 | long sec; | |
812 | int sec_fraction; | |
813 | { | |
814 | long min; | |
815 | char abuf[16]; | |
816 | int ind, aind; | |
726f6388 | 817 | |
ccc6cda3 JA |
818 | ind = 0; |
819 | abuf[15] = '\0'; | |
726f6388 | 820 | |
ccc6cda3 JA |
821 | /* If LNG is non-zero, we want to decompose SEC into minutes and seconds. */ |
822 | if (lng) | |
823 | { | |
824 | min = sec / 60; | |
825 | sec %= 60; | |
826 | aind = 14; | |
827 | do | |
828 | abuf[aind--] = (min % 10) + '0'; | |
829 | while (min /= 10); | |
830 | aind++; | |
831 | while (abuf[aind]) | |
cce855bc | 832 | buf[ind++] = abuf[aind++]; |
ccc6cda3 JA |
833 | buf[ind++] = 'm'; |
834 | } | |
726f6388 | 835 | |
ccc6cda3 JA |
836 | /* Now add the seconds. */ |
837 | aind = 14; | |
838 | do | |
839 | abuf[aind--] = (sec % 10) + '0'; | |
840 | while (sec /= 10); | |
841 | aind++; | |
842 | while (abuf[aind]) | |
843 | buf[ind++] = abuf[aind++]; | |
844 | ||
845 | /* We want to add a decimal point and PREC places after it if PREC is | |
846 | nonzero. PREC is not greater than 3. SEC_FRACTION is between 0 | |
847 | and 999. */ | |
848 | if (prec != 0) | |
849 | { | |
850 | buf[ind++] = '.'; | |
851 | for (aind = 1; aind <= prec; aind++) | |
852 | { | |
853 | buf[ind++] = (sec_fraction / precs[aind]) + '0'; | |
854 | sec_fraction %= precs[aind]; | |
855 | } | |
856 | } | |
726f6388 | 857 | |
ccc6cda3 JA |
858 | if (lng) |
859 | buf[ind++] = 's'; | |
860 | buf[ind] = '\0'; | |
726f6388 | 861 | |
ccc6cda3 JA |
862 | return (ind); |
863 | } | |
726f6388 | 864 | |
ccc6cda3 JA |
865 | /* Interpret the format string FORMAT, interpolating the following escape |
866 | sequences: | |
867 | %[prec][l][RUS] | |
868 | ||
869 | where the optional `prec' is a precision, meaning the number of | |
870 | characters after the decimal point, the optional `l' means to format | |
871 | using minutes and seconds (MMmNN[.FF]s), like the `times' builtin', | |
872 | and the last character is one of | |
873 | ||
874 | R number of seconds of `real' time | |
875 | U number of seconds of `user' time | |
876 | S number of seconds of `system' time | |
877 | ||
878 | An occurrence of `%%' in the format string is translated to a `%'. The | |
879 | result is printed to FP, a pointer to a FILE. The other variables are | |
880 | the seconds and thousandths of a second of real, user, and system time, | |
881 | resectively. */ | |
882 | static void | |
883 | print_formatted_time (fp, format, rs, rsf, us, usf, ss, ssf, cpu) | |
884 | FILE *fp; | |
885 | char *format; | |
886 | long rs, us, ss; | |
887 | int rsf, usf, ssf, cpu; | |
888 | { | |
889 | int prec, lng, len; | |
890 | char *str, *s, ts[32]; | |
e8ce775d JA |
891 | long sum; |
892 | int sum_frac; | |
ccc6cda3 | 893 | int sindex, ssize; |
726f6388 | 894 | |
ccc6cda3 JA |
895 | len = strlen (format); |
896 | ssize = (len + 64) - (len % 64); | |
897 | str = xmalloc (ssize); | |
898 | sindex = 0; | |
899 | ||
900 | for (s = format; *s; s++) | |
901 | { | |
902 | if (*s != '%' || s[1] == '\0') | |
cce855bc JA |
903 | { |
904 | RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64); | |
905 | str[sindex++] = *s; | |
906 | } | |
ccc6cda3 | 907 | else if (s[1] == '%') |
cce855bc JA |
908 | { |
909 | s++; | |
910 | RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64); | |
911 | str[sindex++] = *s; | |
912 | } | |
ccc6cda3 JA |
913 | else if (s[1] == 'P') |
914 | { | |
915 | s++; | |
916 | if (cpu > 10000) | |
917 | cpu = 10000; | |
918 | sum = cpu / 100; | |
919 | sum_frac = (cpu % 100) * 10; | |
920 | len = mkfmt (ts, 2, 0, sum, sum_frac); | |
921 | RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64); | |
922 | strcpy (str + sindex, ts); | |
923 | sindex += len; | |
924 | } | |
925 | else | |
926 | { | |
927 | prec = 3; /* default is three places past the decimal point. */ | |
928 | lng = 0; /* default is to not use minutes or append `s' */ | |
929 | s++; | |
930 | if (isdigit (*s)) /* `precision' */ | |
726f6388 | 931 | { |
ccc6cda3 JA |
932 | prec = *s++ - '0'; |
933 | if (prec > 3) prec = 3; | |
726f6388 | 934 | } |
ccc6cda3 JA |
935 | if (*s == 'l') /* `length extender' */ |
936 | { | |
937 | lng = 1; | |
938 | s++; | |
939 | } | |
940 | if (*s == 'R' || *s == 'E') | |
941 | len = mkfmt (ts, prec, lng, rs, rsf); | |
942 | else if (*s == 'U') | |
943 | len = mkfmt (ts, prec, lng, us, usf); | |
944 | else if (*s == 'S') | |
945 | len = mkfmt (ts, prec, lng, ss, ssf); | |
946 | else | |
947 | { | |
948 | internal_error ("bad format character in time format: %c", *s); | |
949 | free (str); | |
950 | return; | |
951 | } | |
952 | RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64); | |
953 | strcpy (str + sindex, ts); | |
954 | sindex += len; | |
955 | } | |
956 | } | |
957 | ||
958 | str[sindex] = '\0'; | |
959 | fprintf (fp, "%s\n", str); | |
960 | fflush (fp); | |
961 | ||
962 | free (str); | |
963 | } | |
964 | ||
965 | static int | |
966 | time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
967 | COMMAND *command; | |
968 | int asynchronous, pipe_in, pipe_out; | |
969 | struct fd_bitmap *fds_to_close; | |
970 | { | |
d166f048 | 971 | int rv, posix_time, old_flags; |
ccc6cda3 JA |
972 | long rs, us, ss; |
973 | int rsf, usf, ssf; | |
974 | int cpu; | |
975 | char *time_format; | |
976 | ||
977 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) | |
978 | struct timeval real, user, sys; | |
979 | struct timeval before, after; | |
980 | struct timezone dtz; | |
981 | struct rusage selfb, selfa, kidsb, kidsa; /* a = after, b = before */ | |
982 | #else | |
983 | # if defined (HAVE_TIMES) | |
984 | clock_t tbefore, tafter, real, user, sys; | |
985 | struct tms before, after; | |
986 | # endif | |
987 | #endif | |
988 | ||
989 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) | |
990 | gettimeofday (&before, &dtz); | |
991 | getrusage (RUSAGE_SELF, &selfb); | |
992 | getrusage (RUSAGE_CHILDREN, &kidsb); | |
993 | #else | |
994 | # if defined (HAVE_TIMES) | |
995 | tbefore = times (&before); | |
996 | # endif | |
997 | #endif | |
998 | ||
999 | posix_time = (command->flags & CMD_TIME_POSIX); | |
1000 | ||
d166f048 | 1001 | old_flags = command->flags; |
ccc6cda3 JA |
1002 | command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX); |
1003 | rv = execute_command_internal (command, asynchronous, pipe_in, pipe_out, fds_to_close); | |
d166f048 | 1004 | command->flags = old_flags; |
ccc6cda3 | 1005 | |
cce855bc JA |
1006 | rs = us = ss = 0L; |
1007 | rsf = usf = ssf = cpu = 0; | |
1008 | ||
ccc6cda3 JA |
1009 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) |
1010 | gettimeofday (&after, &dtz); | |
1011 | getrusage (RUSAGE_SELF, &selfa); | |
1012 | getrusage (RUSAGE_CHILDREN, &kidsa); | |
1013 | ||
1014 | difftimeval (&real, &before, &after); | |
1015 | timeval_to_secs (&real, &rs, &rsf); | |
1016 | ||
1017 | addtimeval (&user, difftimeval(&after, &selfb.ru_utime, &selfa.ru_utime), | |
1018 | difftimeval(&before, &kidsb.ru_utime, &kidsa.ru_utime)); | |
1019 | timeval_to_secs (&user, &us, &usf); | |
1020 | ||
1021 | addtimeval (&sys, difftimeval(&after, &selfb.ru_stime, &selfa.ru_stime), | |
1022 | difftimeval(&before, &kidsb.ru_stime, &kidsa.ru_stime)); | |
1023 | timeval_to_secs (&sys, &ss, &ssf); | |
1024 | ||
1025 | cpu = timeval_to_cpu (&real, &user, &sys); | |
1026 | #else | |
1027 | # if defined (HAVE_TIMES) | |
1028 | tafter = times (&after); | |
1029 | ||
1030 | real = tafter - tbefore; | |
1031 | clock_t_to_secs (real, &rs, &rsf); | |
1032 | ||
1033 | user = (after.tms_utime - before.tms_utime) + (after.tms_cutime - before.tms_cutime); | |
1034 | clock_t_to_secs (user, &us, &usf); | |
1035 | ||
1036 | sys = (after.tms_stime - before.tms_stime) + (after.tms_cstime - before.tms_cstime); | |
1037 | clock_t_to_secs (sys, &ss, &ssf); | |
1038 | ||
d166f048 | 1039 | cpu = (real == 0) ? 0 : ((user + sys) * 10000) / real; |
ccc6cda3 JA |
1040 | |
1041 | # else | |
1042 | rs = us = ss = 0L; | |
1043 | rsf = usf = ssf = cpu = 0; | |
1044 | # endif | |
1045 | #endif | |
1046 | ||
1047 | if (posix_time) | |
1048 | time_format = POSIX_TIMEFORMAT; | |
1049 | else if ((time_format = get_string_value ("TIMEFORMAT")) == 0) | |
1050 | time_format = BASH_TIMEFORMAT; | |
1051 | ||
1052 | if (time_format && *time_format) | |
1053 | print_formatted_time (stderr, time_format, rs, rsf, us, usf, ss, ssf, cpu); | |
1054 | ||
1055 | return rv; | |
1056 | } | |
1057 | #endif /* COMMAND_TIMING */ | |
1058 | ||
bb70624e | 1059 | /* Execute a command that's supposed to be in a subshell. This must be |
28ef6c31 JA |
1060 | called after make_child and we must be running in the child process. |
1061 | The caller will return or exit() immediately with the value this returns. */ | |
bb70624e JA |
1062 | static int |
1063 | execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
1064 | COMMAND *command; | |
1065 | int asynchronous; | |
1066 | int pipe_in, pipe_out; | |
1067 | struct fd_bitmap *fds_to_close; | |
1068 | { | |
28ef6c31 JA |
1069 | int user_subshell, return_code, function_value, should_redir_stdin, invert; |
1070 | int ois; | |
bb70624e JA |
1071 | COMMAND *tcom; |
1072 | ||
1073 | should_redir_stdin = (asynchronous && (command->flags & CMD_STDIN_REDIR) && | |
1074 | pipe_in == NO_PIPE && | |
1075 | stdin_redirects (command->redirects) == 0); | |
1076 | ||
28ef6c31 | 1077 | invert = (command->flags & CMD_INVERT_RETURN) != 0; |
bb70624e | 1078 | user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0); |
28ef6c31 | 1079 | |
bb70624e JA |
1080 | command->flags &= ~(CMD_FORCE_SUBSHELL | CMD_WANT_SUBSHELL | CMD_INVERT_RETURN); |
1081 | ||
1082 | /* If a command is asynchronous in a subshell (like ( foo ) & or | |
1083 | the special case of an asynchronous GROUP command where the | |
1084 | the subshell bit is turned on down in case cm_group: below), | |
1085 | turn off `asynchronous', so that two subshells aren't spawned. | |
1086 | ||
1087 | This seems semantically correct to me. For example, | |
1088 | ( foo ) & seems to say ``do the command `foo' in a subshell | |
1089 | environment, but don't wait for that subshell to finish'', | |
1090 | and "{ foo ; bar ; } &" seems to me to be like functions or | |
1091 | builtins in the background, which executed in a subshell | |
1092 | environment. I just don't see the need to fork two subshells. */ | |
1093 | ||
1094 | /* Don't fork again, we are already in a subshell. A `doubly | |
1095 | async' shell is not interactive, however. */ | |
1096 | if (asynchronous) | |
1097 | { | |
1098 | #if defined (JOB_CONTROL) | |
1099 | /* If a construct like ( exec xxx yyy ) & is given while job | |
1100 | control is active, we want to prevent exec from putting the | |
1101 | subshell back into the original process group, carefully | |
1102 | undoing all the work we just did in make_child. */ | |
1103 | original_pgrp = -1; | |
1104 | #endif /* JOB_CONTROL */ | |
28ef6c31 | 1105 | ois = interactive_shell; |
bb70624e | 1106 | interactive_shell = 0; |
28ef6c31 JA |
1107 | /* This test is to prevent alias expansion by interactive shells that |
1108 | run `(command) &' but to allow scripts that have enabled alias | |
1109 | expansion with `shopt -s expand_alias' to continue to expand | |
1110 | aliases. */ | |
1111 | if (ois != interactive_shell) | |
1112 | expand_aliases = 0; | |
bb70624e JA |
1113 | asynchronous = 0; |
1114 | } | |
1115 | ||
1116 | /* Subshells are neither login nor interactive. */ | |
1117 | login_shell = interactive = 0; | |
1118 | ||
1119 | subshell_environment = user_subshell ? SUBSHELL_PAREN : SUBSHELL_ASYNC; | |
1120 | ||
1121 | reset_terminating_signals (); /* in sig.c */ | |
1122 | /* Cancel traps, in trap.c. */ | |
1123 | restore_original_signals (); | |
1124 | if (asynchronous) | |
1125 | setup_async_signals (); | |
1126 | ||
1127 | #if defined (JOB_CONTROL) | |
1128 | set_sigchld_handler (); | |
1129 | #endif /* JOB_CONTROL */ | |
1130 | ||
1131 | set_sigint_handler (); | |
1132 | ||
1133 | #if defined (JOB_CONTROL) | |
1134 | /* Delete all traces that there were any jobs running. This is | |
1135 | only for subshells. */ | |
1136 | without_job_control (); | |
1137 | #endif /* JOB_CONTROL */ | |
1138 | ||
1139 | if (fds_to_close) | |
1140 | close_fd_bitmap (fds_to_close); | |
1141 | ||
1142 | do_piping (pipe_in, pipe_out); | |
1143 | ||
1144 | /* If this is a user subshell, set a flag if stdin was redirected. | |
1145 | This is used later to decide whether to redirect fd 0 to | |
1146 | /dev/null for async commands in the subshell. This adds more | |
1147 | sh compatibility, but I'm not sure it's the right thing to do. */ | |
1148 | if (user_subshell) | |
1149 | { | |
1150 | stdin_redir = stdin_redirects (command->redirects); | |
1151 | restore_default_signal (0); | |
1152 | } | |
1153 | ||
1154 | /* If this is an asynchronous command (command &), we want to | |
1155 | redirect the standard input from /dev/null in the absence of | |
1156 | any specific redirection involving stdin. */ | |
1157 | if (should_redir_stdin && stdin_redir == 0) | |
1158 | async_redirect_stdin (); | |
1159 | ||
1160 | /* Do redirections, then dispose of them before recursive call. */ | |
1161 | if (command->redirects) | |
1162 | { | |
1163 | if (do_redirections (command->redirects, 1, 0, 0) != 0) | |
28ef6c31 | 1164 | exit (invert ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
bb70624e JA |
1165 | |
1166 | dispose_redirects (command->redirects); | |
1167 | command->redirects = (REDIRECT *)NULL; | |
1168 | } | |
1169 | ||
1170 | tcom = (command->type == cm_subshell) ? command->value.Subshell->command : command; | |
1171 | ||
1172 | /* If this is a simple command, tell execute_disk_command that it | |
1173 | might be able to get away without forking and simply exec. | |
1174 | This means things like ( sleep 10 ) will only cause one fork. | |
28ef6c31 JA |
1175 | If we're timing the command or inverting its return value, however, |
1176 | we cannot do this optimization. */ | |
bb70624e | 1177 | if (user_subshell && (tcom->type == cm_simple || tcom->type == cm_subshell) && |
28ef6c31 JA |
1178 | ((tcom->flags & CMD_TIME_PIPELINE) == 0) && |
1179 | ((tcom->flags & CMD_INVERT_RETURN) == 0)) | |
bb70624e JA |
1180 | { |
1181 | tcom->flags |= CMD_NO_FORK; | |
1182 | if (tcom->type == cm_simple) | |
1183 | tcom->value.Simple->flags |= CMD_NO_FORK; | |
1184 | } | |
1185 | ||
28ef6c31 JA |
1186 | invert = (tcom->flags & CMD_INVERT_RETURN) != 0; |
1187 | tcom->flags &= ~CMD_INVERT_RETURN; | |
1188 | ||
bb70624e JA |
1189 | /* If we're inside a function while executing this subshell, we |
1190 | need to handle a possible `return'. */ | |
1191 | function_value = 0; | |
1192 | if (return_catch_flag) | |
1193 | function_value = setjmp (return_catch); | |
1194 | ||
1195 | if (function_value) | |
1196 | return_code = return_catch_value; | |
1197 | else | |
1198 | return_code = execute_command_internal | |
1199 | (tcom, asynchronous, NO_PIPE, NO_PIPE, fds_to_close); | |
1200 | ||
28ef6c31 JA |
1201 | /* If we are asked to, invert the return value. */ |
1202 | if (invert) | |
1203 | return_code = (return_code == EXECUTION_SUCCESS) ? EXECUTION_FAILURE | |
1204 | : EXECUTION_SUCCESS; | |
1205 | ||
bb70624e JA |
1206 | /* If we were explicitly placed in a subshell with (), we need |
1207 | to do the `shell cleanup' things, such as running traps[0]. */ | |
1208 | if (user_subshell && signal_is_trapped (0)) | |
1209 | { | |
1210 | last_command_exit_value = return_code; | |
1211 | return_code = run_exit_trap (); | |
1212 | } | |
1213 | ||
1214 | return (return_code); | |
1215 | /* NOTREACHED */ | |
1216 | } | |
1217 | ||
ccc6cda3 JA |
1218 | static int |
1219 | execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
1220 | COMMAND *command; | |
1221 | int asynchronous, pipe_in, pipe_out; | |
1222 | struct fd_bitmap *fds_to_close; | |
1223 | { | |
1224 | int prev, fildes[2], new_bitmap_size, dummyfd, ignore_return, exec_result; | |
1225 | COMMAND *cmd; | |
1226 | struct fd_bitmap *fd_bitmap; | |
726f6388 JA |
1227 | |
1228 | #if defined (JOB_CONTROL) | |
ccc6cda3 JA |
1229 | sigset_t set, oset; |
1230 | BLOCK_CHILD (set, oset); | |
726f6388 JA |
1231 | #endif /* JOB_CONTROL */ |
1232 | ||
ccc6cda3 | 1233 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; |
726f6388 | 1234 | |
ccc6cda3 JA |
1235 | prev = pipe_in; |
1236 | cmd = command; | |
1237 | ||
1238 | while (cmd && cmd->type == cm_connection && | |
1239 | cmd->value.Connection && cmd->value.Connection->connector == '|') | |
1240 | { | |
1241 | /* Make a pipeline between the two commands. */ | |
1242 | if (pipe (fildes) < 0) | |
1243 | { | |
1244 | sys_error ("pipe error"); | |
726f6388 | 1245 | #if defined (JOB_CONTROL) |
ccc6cda3 JA |
1246 | terminate_current_pipeline (); |
1247 | kill_current_pipeline (); | |
726f6388 | 1248 | #endif /* JOB_CONTROL */ |
ccc6cda3 JA |
1249 | last_command_exit_value = EXECUTION_FAILURE; |
1250 | /* The unwind-protects installed below will take care | |
1251 | of closing all of the open file descriptors. */ | |
1252 | throw_to_top_level (); | |
1253 | return (EXECUTION_FAILURE); /* XXX */ | |
1254 | } | |
1255 | ||
1256 | /* Here is a problem: with the new file close-on-exec | |
1257 | code, the read end of the pipe (fildes[0]) stays open | |
1258 | in the first process, so that process will never get a | |
1259 | SIGPIPE. There is no way to signal the first process | |
1260 | that it should close fildes[0] after forking, so it | |
1261 | remains open. No SIGPIPE is ever sent because there | |
1262 | is still a file descriptor open for reading connected | |
1263 | to the pipe. We take care of that here. This passes | |
1264 | around a bitmap of file descriptors that must be | |
1265 | closed after making a child process in execute_simple_command. */ | |
1266 | ||
1267 | /* We need fd_bitmap to be at least as big as fildes[0]. | |
1268 | If fildes[0] is less than fds_to_close->size, then | |
1269 | use fds_to_close->size. */ | |
1270 | new_bitmap_size = (fildes[0] < fds_to_close->size) | |
1271 | ? fds_to_close->size | |
1272 | : fildes[0] + 8; | |
1273 | ||
1274 | fd_bitmap = new_fd_bitmap (new_bitmap_size); | |
1275 | ||
1276 | /* Now copy the old information into the new bitmap. */ | |
1277 | xbcopy ((char *)fds_to_close->bitmap, (char *)fd_bitmap->bitmap, fds_to_close->size); | |
1278 | ||
1279 | /* And mark the pipe file descriptors to be closed. */ | |
1280 | fd_bitmap->bitmap[fildes[0]] = 1; | |
1281 | ||
1282 | /* In case there are pipe or out-of-processes errors, we | |
cce855bc | 1283 | want all these file descriptors to be closed when |
ccc6cda3 JA |
1284 | unwind-protects are run, and the storage used for the |
1285 | bitmaps freed up. */ | |
1286 | begin_unwind_frame ("pipe-file-descriptors"); | |
1287 | add_unwind_protect (dispose_fd_bitmap, fd_bitmap); | |
1288 | add_unwind_protect (close_fd_bitmap, fd_bitmap); | |
1289 | if (prev >= 0) | |
1290 | add_unwind_protect (close, prev); | |
1291 | dummyfd = fildes[1]; | |
1292 | add_unwind_protect (close, dummyfd); | |
726f6388 JA |
1293 | |
1294 | #if defined (JOB_CONTROL) | |
ccc6cda3 | 1295 | add_unwind_protect (restore_signal_mask, oset); |
726f6388 JA |
1296 | #endif /* JOB_CONTROL */ |
1297 | ||
ccc6cda3 JA |
1298 | if (ignore_return && cmd->value.Connection->first) |
1299 | cmd->value.Connection->first->flags |= CMD_IGNORE_RETURN; | |
1300 | execute_command_internal (cmd->value.Connection->first, asynchronous, | |
1301 | prev, fildes[1], fd_bitmap); | |
1302 | ||
1303 | if (prev >= 0) | |
1304 | close (prev); | |
1305 | ||
1306 | prev = fildes[0]; | |
1307 | close (fildes[1]); | |
1308 | ||
1309 | dispose_fd_bitmap (fd_bitmap); | |
1310 | discard_unwind_frame ("pipe-file-descriptors"); | |
726f6388 | 1311 | |
ccc6cda3 JA |
1312 | cmd = cmd->value.Connection->second; |
1313 | } | |
1314 | ||
1315 | /* Now execute the rightmost command in the pipeline. */ | |
1316 | if (ignore_return && cmd) | |
1317 | cmd->flags |= CMD_IGNORE_RETURN; | |
1318 | exec_result = execute_command_internal (cmd, asynchronous, prev, pipe_out, fds_to_close); | |
726f6388 | 1319 | |
ccc6cda3 JA |
1320 | if (prev >= 0) |
1321 | close (prev); | |
726f6388 JA |
1322 | |
1323 | #if defined (JOB_CONTROL) | |
ccc6cda3 | 1324 | UNBLOCK_CHILD (oset); |
726f6388 | 1325 | #endif |
726f6388 | 1326 | |
ccc6cda3 JA |
1327 | return (exec_result); |
1328 | } | |
1329 | ||
1330 | static int | |
1331 | execute_connection (command, asynchronous, pipe_in, pipe_out, fds_to_close) | |
1332 | COMMAND *command; | |
1333 | int asynchronous, pipe_in, pipe_out; | |
1334 | struct fd_bitmap *fds_to_close; | |
1335 | { | |
d166f048 JA |
1336 | #if 0 |
1337 | REDIRECT *tr, *tl; | |
1338 | #endif | |
1339 | REDIRECT *rp; | |
ccc6cda3 JA |
1340 | COMMAND *tc, *second; |
1341 | int ignore_return, exec_result; | |
1342 | ||
1343 | ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0; | |
1344 | ||
1345 | switch (command->value.Connection->connector) | |
1346 | { | |
1347 | /* Do the first command asynchronously. */ | |
1348 | case '&': | |
1349 | tc = command->value.Connection->first; | |
1350 | if (tc == 0) | |
1351 | return (EXECUTION_SUCCESS); | |
1352 | ||
1353 | rp = tc->redirects; | |
1354 | ||
d166f048 | 1355 | if (ignore_return) |
ccc6cda3 | 1356 | tc->flags |= CMD_IGNORE_RETURN; |
d166f048 | 1357 | tc->flags |= CMD_AMPERSAND; |
ccc6cda3 JA |
1358 | |
1359 | /* If this shell was compiled without job control support, if | |
1360 | the shell is not running interactively, if we are currently | |
1361 | in a subshell via `( xxx )', or if job control is not active | |
1362 | then the standard input for an asynchronous command is | |
1363 | forced to /dev/null. */ | |
1364 | #if defined (JOB_CONTROL) | |
1365 | if ((!interactive_shell || subshell_environment || !job_control) && !stdin_redir) | |
1366 | #else | |
1367 | if (!stdin_redir) | |
1368 | #endif /* JOB_CONTROL */ | |
1369 | { | |
d166f048 | 1370 | #if 0 |
ccc6cda3 JA |
1371 | rd.filename = make_bare_word ("/dev/null"); |
1372 | tr = make_redirection (0, r_inputa_direction, rd); | |
1373 | tr->next = tc->redirects; | |
1374 | tc->redirects = tr; | |
d166f048 JA |
1375 | #endif |
1376 | tc->flags |= CMD_STDIN_REDIR; | |
ccc6cda3 JA |
1377 | } |
1378 | ||
1379 | exec_result = execute_command_internal (tc, 1, pipe_in, pipe_out, fds_to_close); | |
1380 | ||
d166f048 | 1381 | if (tc->flags & CMD_STDIN_REDIR) |
ccc6cda3 | 1382 | { |
d166f048 | 1383 | #if 0 |
ccc6cda3 JA |
1384 | /* Remove the redirection we added above. It matters, |
1385 | especially for loops, which call execute_command () | |
1386 | multiple times with the same command. */ | |
1387 | tr = tc->redirects; | |
1388 | do | |
726f6388 | 1389 | { |
ccc6cda3 JA |
1390 | tl = tc->redirects; |
1391 | tc->redirects = tc->redirects->next; | |
726f6388 | 1392 | } |
ccc6cda3 | 1393 | while (tc->redirects && tc->redirects != rp); |
726f6388 | 1394 | |
ccc6cda3 JA |
1395 | tl->next = (REDIRECT *)NULL; |
1396 | dispose_redirects (tr); | |
d166f048 JA |
1397 | #endif |
1398 | tc->flags &= ~CMD_STDIN_REDIR; | |
ccc6cda3 | 1399 | } |
726f6388 | 1400 | |
ccc6cda3 JA |
1401 | second = command->value.Connection->second; |
1402 | if (second) | |
1403 | { | |
1404 | if (ignore_return) | |
1405 | second->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 1406 | |
ccc6cda3 JA |
1407 | exec_result = execute_command_internal (second, asynchronous, pipe_in, pipe_out, fds_to_close); |
1408 | } | |
726f6388 | 1409 | |
ccc6cda3 | 1410 | break; |
726f6388 | 1411 | |
ccc6cda3 JA |
1412 | /* Just call execute command on both sides. */ |
1413 | case ';': | |
1414 | if (ignore_return) | |
1415 | { | |
1416 | if (command->value.Connection->first) | |
1417 | command->value.Connection->first->flags |= CMD_IGNORE_RETURN; | |
1418 | if (command->value.Connection->second) | |
1419 | command->value.Connection->second->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 1420 | } |
ccc6cda3 JA |
1421 | QUIT; |
1422 | execute_command (command->value.Connection->first); | |
1423 | QUIT; | |
1424 | exec_result = execute_command_internal (command->value.Connection->second, | |
1425 | asynchronous, pipe_in, pipe_out, | |
1426 | fds_to_close); | |
726f6388 JA |
1427 | break; |
1428 | ||
ccc6cda3 JA |
1429 | case '|': |
1430 | exec_result = execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close); | |
726f6388 JA |
1431 | break; |
1432 | ||
ccc6cda3 JA |
1433 | case AND_AND: |
1434 | case OR_OR: | |
1435 | if (asynchronous) | |
1436 | { | |
1437 | /* If we have something like `a && b &' or `a || b &', run the | |
1438 | && or || stuff in a subshell. Force a subshell and just call | |
1439 | execute_command_internal again. Leave asynchronous on | |
1440 | so that we get a report from the parent shell about the | |
1441 | background job. */ | |
1442 | command->flags |= CMD_FORCE_SUBSHELL; | |
1443 | exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close); | |
1444 | break; | |
1445 | } | |
726f6388 | 1446 | |
ccc6cda3 JA |
1447 | /* Execute the first command. If the result of that is successful |
1448 | and the connector is AND_AND, or the result is not successful | |
1449 | and the connector is OR_OR, then execute the second command, | |
1450 | otherwise return. */ | |
726f6388 | 1451 | |
ccc6cda3 JA |
1452 | if (command->value.Connection->first) |
1453 | command->value.Connection->first->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 1454 | |
ccc6cda3 JA |
1455 | exec_result = execute_command (command->value.Connection->first); |
1456 | QUIT; | |
1457 | if (((command->value.Connection->connector == AND_AND) && | |
1458 | (exec_result == EXECUTION_SUCCESS)) || | |
1459 | ((command->value.Connection->connector == OR_OR) && | |
1460 | (exec_result != EXECUTION_SUCCESS))) | |
1461 | { | |
1462 | if (ignore_return && command->value.Connection->second) | |
1463 | command->value.Connection->second->flags |= CMD_IGNORE_RETURN; | |
726f6388 | 1464 | |
ccc6cda3 JA |
1465 | exec_result = execute_command (command->value.Connection->second); |
1466 | } | |
1467 | break; | |
1468 | ||
1469 | default: | |
b72432fd | 1470 | command_error ("execute_connection", CMDERR_BADCONN, command->value.Connection->connector, 0); |
ccc6cda3 JA |
1471 | jump_to_top_level (DISCARD); |
1472 | exec_result = EXECUTION_FAILURE; | |
726f6388 JA |
1473 | } |
1474 | ||
ccc6cda3 | 1475 | return exec_result; |
726f6388 JA |
1476 | } |
1477 | ||
bb70624e JA |
1478 | #define REAP() \ |
1479 | do \ | |
1480 | { \ | |
1481 | if (!interactive_shell) \ | |
1482 | reap_dead_jobs (); \ | |
1483 | } \ | |
1484 | while (0) | |
726f6388 JA |
1485 | |
1486 | /* Execute a FOR command. The syntax is: FOR word_desc IN word_list; | |
1487 | DO command; DONE */ | |
ccc6cda3 | 1488 | static int |
726f6388 JA |
1489 | execute_for_command (for_command) |
1490 | FOR_COM *for_command; | |
1491 | { | |
726f6388 | 1492 | register WORD_LIST *releaser, *list; |
ccc6cda3 | 1493 | SHELL_VAR *v; |
726f6388 | 1494 | char *identifier; |
ccc6cda3 JA |
1495 | int retval; |
1496 | #if 0 | |
726f6388 | 1497 | SHELL_VAR *old_value = (SHELL_VAR *)NULL; /* Remember the old value of x. */ |
ccc6cda3 | 1498 | #endif |
726f6388 JA |
1499 | |
1500 | if (check_identifier (for_command->name, 1) == 0) | |
ccc6cda3 JA |
1501 | { |
1502 | if (posixly_correct && interactive_shell == 0) | |
cce855bc JA |
1503 | { |
1504 | last_command_exit_value = EX_USAGE; | |
1505 | jump_to_top_level (EXITPROG); | |
1506 | } | |
ccc6cda3 JA |
1507 | return (EXECUTION_FAILURE); |
1508 | } | |
726f6388 JA |
1509 | |
1510 | loop_level++; | |
1511 | identifier = for_command->name->word; | |
1512 | ||
1513 | list = releaser = expand_words_no_vars (for_command->map_list); | |
1514 | ||
1515 | begin_unwind_frame ("for"); | |
1516 | add_unwind_protect (dispose_words, releaser); | |
1517 | ||
ccc6cda3 | 1518 | #if 0 |
726f6388 JA |
1519 | if (lexical_scoping) |
1520 | { | |
1521 | old_value = copy_variable (find_variable (identifier)); | |
1522 | if (old_value) | |
1523 | add_unwind_protect (dispose_variable, old_value); | |
1524 | } | |
ccc6cda3 | 1525 | #endif |
726f6388 JA |
1526 | |
1527 | if (for_command->flags & CMD_IGNORE_RETURN) | |
1528 | for_command->action->flags |= CMD_IGNORE_RETURN; | |
1529 | ||
ccc6cda3 | 1530 | for (retval = EXECUTION_SUCCESS; list; list = list->next) |
726f6388 JA |
1531 | { |
1532 | QUIT; | |
ccc6cda3 JA |
1533 | this_command_name = (char *)NULL; |
1534 | v = bind_variable (identifier, list->word->word); | |
28ef6c31 | 1535 | if (readonly_p (v) || noassign_p (v)) |
ccc6cda3 | 1536 | { |
28ef6c31 | 1537 | if (readonly_p (v) && interactive_shell == 0 && posixly_correct) |
ccc6cda3 JA |
1538 | { |
1539 | last_command_exit_value = EXECUTION_FAILURE; | |
1540 | jump_to_top_level (FORCE_EOF); | |
1541 | } | |
1542 | else | |
1543 | { | |
1544 | run_unwind_frame ("for"); | |
d166f048 | 1545 | loop_level--; |
ccc6cda3 JA |
1546 | return (EXECUTION_FAILURE); |
1547 | } | |
1548 | } | |
1549 | retval = execute_command (for_command->action); | |
726f6388 JA |
1550 | REAP (); |
1551 | QUIT; | |
1552 | ||
1553 | if (breaking) | |
1554 | { | |
ccc6cda3 | 1555 | breaking--; |
726f6388 JA |
1556 | break; |
1557 | } | |
1558 | ||
1559 | if (continuing) | |
1560 | { | |
1561 | continuing--; | |
1562 | if (continuing) | |
1563 | break; | |
1564 | } | |
726f6388 JA |
1565 | } |
1566 | ||
1567 | loop_level--; | |
1568 | ||
ccc6cda3 | 1569 | #if 0 |
726f6388 JA |
1570 | if (lexical_scoping) |
1571 | { | |
1572 | if (!old_value) | |
1573 | makunbound (identifier, shell_variables); | |
1574 | else | |
1575 | { | |
1576 | SHELL_VAR *new_value; | |
1577 | ||
1578 | new_value = bind_variable (identifier, value_cell(old_value)); | |
1579 | new_value->attributes = old_value->attributes; | |
1580 | dispose_variable (old_value); | |
1581 | } | |
1582 | } | |
ccc6cda3 | 1583 | #endif |
726f6388 JA |
1584 | |
1585 | dispose_words (releaser); | |
1586 | discard_unwind_frame ("for"); | |
1587 | return (retval); | |
1588 | } | |
1589 | ||
bb70624e JA |
1590 | #if defined (ARITH_FOR_COMMAND) |
1591 | /* Execute an arithmetic for command. The syntax is | |
1592 | ||
1593 | for (( init ; step ; test )) | |
1594 | do | |
1595 | body | |
1596 | done | |
1597 | ||
1598 | The execution should be exactly equivalent to | |
1599 | ||
1600 | eval \(\( init \)\) | |
1601 | while eval \(\( test \)\) ; do | |
1602 | body; | |
1603 | eval \(\( step \)\) | |
1604 | done | |
1605 | */ | |
1606 | static long | |
1607 | eval_arith_for_expr (l, okp) | |
1608 | WORD_LIST *l; | |
1609 | int *okp; | |
1610 | { | |
1611 | WORD_LIST *new; | |
1612 | long expresult; | |
1613 | ||
1614 | new = expand_words_no_vars (l); | |
1615 | if (echo_command_at_execute) | |
1616 | xtrace_print_arith_cmd (new); | |
1617 | expresult = evalexp (new->word->word, okp); | |
1618 | dispose_words (new); | |
1619 | return (expresult); | |
1620 | } | |
1621 | ||
1622 | static int | |
1623 | execute_arith_for_command (arith_for_command) | |
1624 | ARITH_FOR_COM *arith_for_command; | |
1625 | { | |
1626 | long expresult; | |
1627 | int expok, result, body_status; | |
1628 | ||
1629 | body_status = EXECUTION_SUCCESS; | |
1630 | loop_level++; | |
1631 | ||
1632 | if (arith_for_command->flags & CMD_IGNORE_RETURN) | |
1633 | arith_for_command->action->flags |= CMD_IGNORE_RETURN; | |
1634 | ||
1635 | this_command_name = "(("; /* )) for expression error messages */ | |
1636 | ||
1637 | if (variable_context) | |
1638 | line_number = arith_for_command->line - function_line_number; | |
1639 | ||
1640 | /* Evaluate the initialization expression. */ | |
1641 | expresult = eval_arith_for_expr (arith_for_command->init, &expok); | |
1642 | if (expok == 0) | |
1643 | return (EXECUTION_FAILURE); | |
1644 | ||
1645 | while (1) | |
1646 | { | |
1647 | /* Evaluate the test expression. */ | |
1648 | expresult = eval_arith_for_expr (arith_for_command->test, &expok); | |
1649 | if (expok == 0) | |
1650 | { | |
1651 | body_status = EXECUTION_FAILURE; | |
1652 | break; | |
1653 | } | |
1654 | REAP (); | |
1655 | if (expresult == 0) | |
28ef6c31 | 1656 | break; |
bb70624e JA |
1657 | |
1658 | /* Execute the body of the arithmetic for command. */ | |
1659 | QUIT; | |
1660 | body_status = execute_command (arith_for_command->action); | |
1661 | QUIT; | |
1662 | ||
1663 | /* Handle any `break' or `continue' commands executed by the body. */ | |
1664 | if (breaking) | |
1665 | { | |
1666 | breaking--; | |
1667 | break; | |
1668 | } | |
1669 | ||
1670 | if (continuing) | |
1671 | { | |
1672 | continuing--; | |
1673 | if (continuing) | |
1674 | break; | |
1675 | } | |
1676 | ||
1677 | /* Evaluate the step expression. */ | |
1678 | expresult = eval_arith_for_expr (arith_for_command->step, &expok); | |
1679 | if (expok == 0) | |
1680 | { | |
1681 | body_status = EXECUTION_FAILURE; | |
1682 | break; | |
1683 | } | |
1684 | } | |
1685 | ||
1686 | loop_level--; | |
1687 | return (body_status); | |
1688 | } | |
1689 | #endif | |
1690 | ||
726f6388 JA |
1691 | #if defined (SELECT_COMMAND) |
1692 | static int LINES, COLS, tabsize; | |
1693 | ||
1694 | #define RP_SPACE ") " | |
1695 | #define RP_SPACE_LEN 2 | |
1696 | ||
1697 | /* XXX - does not handle numbers > 1000000 at all. */ | |
1698 | #define NUMBER_LEN(s) \ | |
1699 | ((s < 10) ? 1 \ | |
1700 | : ((s < 100) ? 2 \ | |
1701 | : ((s < 1000) ? 3 \ | |
1702 | : ((s < 10000) ? 4 \ | |
1703 | : ((s < 100000) ? 5 \ | |
1704 | : 6))))) | |
1705 | ||
1706 | static int | |
1707 | print_index_and_element (len, ind, list) | |
1708 | int len, ind; | |
1709 | WORD_LIST *list; | |
1710 | { | |
1711 | register WORD_LIST *l; | |
1712 | register int i; | |
1713 | ||
1714 | if (list == 0) | |
1715 | return (0); | |
ccc6cda3 JA |
1716 | for (i = ind, l = list; l && --i; l = l->next) |
1717 | ; | |
726f6388 JA |
1718 | fprintf (stderr, "%*d%s%s", len, ind, RP_SPACE, l->word->word); |
1719 | return (STRLEN (l->word->word)); | |
1720 | } | |
1721 | ||
1722 | static void | |
1723 | indent (from, to) | |
1724 | int from, to; | |
1725 | { | |
1726 | while (from < to) | |
1727 | { | |
1728 | if ((to / tabsize) > (from / tabsize)) | |
1729 | { | |
1730 | putc ('\t', stderr); | |
1731 | from += tabsize - from % tabsize; | |
1732 | } | |
1733 | else | |
1734 | { | |
1735 | putc (' ', stderr); | |
1736 | from++; | |
1737 | } | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | static void | |
1742 | print_select_list (list, list_len, max_elem_len, indices_len) | |
1743 | WORD_LIST *list; | |
1744 | int list_len, max_elem_len, indices_len; | |
1745 | { | |
1746 | int ind, row, elem_len, pos, cols, rows; | |
1747 | int first_column_indices_len, other_indices_len; | |
1748 | ||
1749 | if (list == 0) | |
1750 | { | |
1751 | putc ('\n', stderr); | |
1752 | return; | |
1753 | } | |
1754 | ||
ccc6cda3 | 1755 | cols = max_elem_len ? COLS / max_elem_len : 1; |
726f6388 JA |
1756 | if (cols == 0) |
1757 | cols = 1; | |
1758 | rows = list_len ? list_len / cols + (list_len % cols != 0) : 1; | |
1759 | cols = list_len ? list_len / rows + (list_len % rows != 0) : 1; | |
1760 | ||
1761 | if (rows == 1) | |
1762 | { | |
1763 | rows = cols; | |
1764 | cols = 1; | |
1765 | } | |
1766 | ||
1767 | first_column_indices_len = NUMBER_LEN (rows); | |
1768 | other_indices_len = indices_len; | |
1769 | ||
1770 | for (row = 0; row < rows; row++) | |
1771 | { | |
1772 | ind = row; | |
1773 | pos = 0; | |
1774 | while (1) | |
1775 | { | |
1776 | indices_len = (pos == 0) ? first_column_indices_len : other_indices_len; | |
1777 | elem_len = print_index_and_element (indices_len, ind + 1, list); | |
1778 | elem_len += indices_len + RP_SPACE_LEN; | |
1779 | ind += rows; | |
1780 | if (ind >= list_len) | |
1781 | break; | |
1782 | indent (pos + elem_len, pos + max_elem_len); | |
1783 | pos += max_elem_len; | |
1784 | } | |
1785 | putc ('\n', stderr); | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | /* Print the elements of LIST, one per line, preceded by an index from 1 to | |
1790 | LIST_LEN. Then display PROMPT and wait for the user to enter a number. | |
1791 | If the number is between 1 and LIST_LEN, return that selection. If EOF | |
e8ce775d JA |
1792 | is read, return a null string. If a blank line is entered, or an invalid |
1793 | number is entered, the loop is executed again. */ | |
726f6388 JA |
1794 | static char * |
1795 | select_query (list, list_len, prompt) | |
1796 | WORD_LIST *list; | |
1797 | int list_len; | |
1798 | char *prompt; | |
1799 | { | |
e8ce775d JA |
1800 | int max_elem_len, indices_len, len; |
1801 | long reply; | |
726f6388 JA |
1802 | WORD_LIST *l; |
1803 | char *repl_string, *t; | |
1804 | ||
1805 | t = get_string_value ("LINES"); | |
1806 | LINES = (t && *t) ? atoi (t) : 24; | |
1807 | t = get_string_value ("COLUMNS"); | |
1808 | COLS = (t && *t) ? atoi (t) : 80; | |
1809 | ||
1810 | #if 0 | |
1811 | t = get_string_value ("TABSIZE"); | |
1812 | tabsize = (t && *t) ? atoi (t) : 8; | |
1813 | if (tabsize <= 0) | |
1814 | tabsize = 8; | |
1815 | #else | |
1816 | tabsize = 8; | |
1817 | #endif | |
1818 | ||
1819 | max_elem_len = 0; | |
1820 | for (l = list; l; l = l->next) | |
1821 | { | |
1822 | len = STRLEN (l->word->word); | |
1823 | if (len > max_elem_len) | |
cce855bc | 1824 | max_elem_len = len; |
726f6388 JA |
1825 | } |
1826 | indices_len = NUMBER_LEN (list_len); | |
1827 | max_elem_len += indices_len + RP_SPACE_LEN + 2; | |
1828 | ||
1829 | while (1) | |
1830 | { | |
1831 | print_select_list (list, list_len, max_elem_len, indices_len); | |
ccc6cda3 JA |
1832 | fprintf (stderr, "%s", prompt); |
1833 | fflush (stderr); | |
726f6388 JA |
1834 | QUIT; |
1835 | ||
1836 | if (read_builtin ((WORD_LIST *)NULL) == EXECUTION_FAILURE) | |
1837 | { | |
1838 | putchar ('\n'); | |
1839 | return ((char *)NULL); | |
1840 | } | |
1841 | repl_string = get_string_value ("REPLY"); | |
1842 | if (*repl_string == 0) | |
1843 | continue; | |
e8ce775d | 1844 | if (legal_number (repl_string, &reply) == 0) |
cce855bc | 1845 | return ""; |
726f6388 JA |
1846 | if (reply < 1 || reply > list_len) |
1847 | return ""; | |
1848 | ||
ccc6cda3 JA |
1849 | for (l = list; l && --reply; l = l->next) |
1850 | ; | |
726f6388 JA |
1851 | return (l->word->word); |
1852 | } | |
1853 | } | |
1854 | ||
1855 | /* Execute a SELECT command. The syntax is: | |
1856 | SELECT word IN list DO command_list DONE | |
1857 | Only `break' or `return' in command_list will terminate | |
1858 | the command. */ | |
ccc6cda3 | 1859 | static int |
726f6388 JA |
1860 | execute_select_command (select_command) |
1861 | SELECT_COM *select_command; | |
1862 | { | |
1863 | WORD_LIST *releaser, *list; | |
ccc6cda3 | 1864 | SHELL_VAR *v; |
726f6388 JA |
1865 | char *identifier, *ps3_prompt, *selection; |
1866 | int retval, list_len, return_val; | |
726f6388 JA |
1867 | |
1868 | if (check_identifier (select_command->name, 1) == 0) | |
1869 | return (EXECUTION_FAILURE); | |
1870 | ||
1871 | loop_level++; | |
1872 | identifier = select_command->name->word; | |
1873 | ||
1874 | /* command and arithmetic substitution, parameter and variable expansion, | |
1875 | word splitting, pathname expansion, and quote removal. */ | |
1876 | list = releaser = expand_words_no_vars (select_command->map_list); | |
1877 | list_len = list_length (list); | |
1878 | if (list == 0 || list_len == 0) | |
1879 | { | |
1880 | if (list) | |
1881 | dispose_words (list); | |
1882 | return (EXECUTION_SUCCESS); | |
1883 | } | |
1884 | ||
1885 | begin_unwind_frame ("select"); | |
1886 | add_unwind_protect (dispose_words, releaser); | |
1887 | ||
726f6388 JA |
1888 | if (select_command->flags & CMD_IGNORE_RETURN) |
1889 | select_command->action->flags |= CMD_IGNORE_RETURN; | |
1890 | ||
ccc6cda3 JA |
1891 | retval = EXECUTION_SUCCESS; |
1892 | ||
726f6388 JA |
1893 | unwind_protect_int (return_catch_flag); |
1894 | unwind_protect_jmp_buf (return_catch); | |
1895 | return_catch_flag++; | |
1896 | ||
1897 | while (1) | |
1898 | { | |
1899 | ps3_prompt = get_string_value ("PS3"); | |
ccc6cda3 | 1900 | if (ps3_prompt == 0) |
726f6388 JA |
1901 | ps3_prompt = "#? "; |
1902 | ||
1903 | QUIT; | |
1904 | selection = select_query (list, list_len, ps3_prompt); | |
1905 | QUIT; | |
1906 | if (selection == 0) | |
1907 | break; | |
ccc6cda3 JA |
1908 | |
1909 | v = bind_variable (identifier, selection); | |
28ef6c31 | 1910 | if (readonly_p (v) || noassign_p (v)) |
ccc6cda3 | 1911 | { |
28ef6c31 | 1912 | if (readonly_p (v) && interactive_shell == 0 && posixly_correct) |
ccc6cda3 JA |
1913 | { |
1914 | last_command_exit_value = EXECUTION_FAILURE; | |
1915 | jump_to_top_level (FORCE_EOF); | |
1916 | } | |
1917 | else | |
1918 | { | |
1919 | run_unwind_frame ("select"); | |
1920 | return (EXECUTION_FAILURE); | |
1921 | } | |
1922 | } | |
726f6388 JA |
1923 | |
1924 | return_val = setjmp (return_catch); | |
1925 | ||
1926 | if (return_val) | |
cce855bc | 1927 | { |
726f6388 JA |
1928 | retval = return_catch_value; |
1929 | break; | |
cce855bc | 1930 | } |
726f6388 | 1931 | else |
cce855bc | 1932 | retval = execute_command (select_command->action); |
726f6388 JA |
1933 | |
1934 | REAP (); | |
1935 | QUIT; | |
1936 | ||
1937 | if (breaking) | |
1938 | { | |
1939 | breaking--; | |
1940 | break; | |
1941 | } | |
bb70624e JA |
1942 | |
1943 | if (continuing) | |
1944 | { | |
1945 | continuing--; | |
1946 | if (continuing) | |
1947 | break; | |
1948 | } | |
726f6388 JA |
1949 | } |
1950 | ||
1951 | loop_level--; | |
1952 | ||
726f6388 JA |
1953 | run_unwind_frame ("select"); |
1954 | return (retval); | |
1955 | } | |
1956 | #endif /* SELECT_COMMAND */ | |
1957 | ||
1958 | /* Execute a CASE command. The syntax is: CASE word_desc IN pattern_list ESAC. | |
1959 | The pattern_list is a linked list of pattern clauses; each clause contains | |
1960 | some patterns to compare word_desc against, and an associated command to | |
1961 | execute. */ | |
ccc6cda3 | 1962 | static int |
726f6388 JA |
1963 | execute_case_command (case_command) |
1964 | CASE_COM *case_command; | |
1965 | { | |
1966 | register WORD_LIST *list; | |
ccc6cda3 | 1967 | WORD_LIST *wlist, *es; |
726f6388 | 1968 | PATTERN_LIST *clauses; |
ccc6cda3 JA |
1969 | char *word, *pattern; |
1970 | int retval, match, ignore_return; | |
726f6388 JA |
1971 | |
1972 | /* Posix.2 specifies that the WORD is tilde expanded. */ | |
1973 | if (member ('~', case_command->word->word)) | |
1974 | { | |
ccc6cda3 | 1975 | word = bash_tilde_expand (case_command->word->word); |
726f6388 JA |
1976 | free (case_command->word->word); |
1977 | case_command->word->word = word; | |
1978 | } | |
1979 | ||
28ef6c31 | 1980 | wlist = expand_word_unsplit (case_command->word, 0); |
ccc6cda3 JA |
1981 | word = wlist ? string_list (wlist) : savestring (""); |
1982 | dispose_words (wlist); | |
1983 | ||
726f6388 | 1984 | retval = EXECUTION_SUCCESS; |
ccc6cda3 | 1985 | ignore_return = case_command->flags & CMD_IGNORE_RETURN; |
726f6388 JA |
1986 | |
1987 | begin_unwind_frame ("case"); | |
726f6388 JA |
1988 | add_unwind_protect ((Function *)xfree, word); |
1989 | ||
ccc6cda3 JA |
1990 | #define EXIT_CASE() goto exit_case_command |
1991 | ||
1992 | for (clauses = case_command->clauses; clauses; clauses = clauses->next) | |
726f6388 JA |
1993 | { |
1994 | QUIT; | |
ccc6cda3 | 1995 | for (list = clauses->patterns; list; list = list->next) |
726f6388 | 1996 | { |
726f6388 JA |
1997 | /* Posix.2 specifies to tilde expand each member of the pattern |
1998 | list. */ | |
1999 | if (member ('~', list->word->word)) | |
2000 | { | |
ccc6cda3 | 2001 | pattern = bash_tilde_expand (list->word->word); |
726f6388 | 2002 | free (list->word->word); |
ccc6cda3 | 2003 | list->word->word = pattern; |
726f6388 JA |
2004 | } |
2005 | ||
2006 | es = expand_word_leave_quoted (list->word, 0); | |
2007 | ||
2008 | if (es && es->word && es->word->word && *(es->word->word)) | |
cce855bc | 2009 | pattern = quote_string_for_globbing (es->word->word, QGLOB_CVTNULL); |
726f6388 | 2010 | else |
ccc6cda3 JA |
2011 | { |
2012 | pattern = xmalloc (1); | |
2013 | pattern[0] = '\0'; | |
2014 | } | |
726f6388 JA |
2015 | |
2016 | /* Since the pattern does not undergo quote removal (as per | |
2017 | Posix.2, section 3.9.4.3), the fnmatch () call must be able | |
2018 | to recognize backslashes as escape characters. */ | |
cce855bc | 2019 | match = fnmatch (pattern, word, FNMATCH_EXTFLAG) != FNM_NOMATCH; |
726f6388 JA |
2020 | free (pattern); |
2021 | ||
2022 | dispose_words (es); | |
2023 | ||
2024 | if (match) | |
2025 | { | |
ccc6cda3 | 2026 | if (clauses->action && ignore_return) |
726f6388 | 2027 | clauses->action->flags |= CMD_IGNORE_RETURN; |
ccc6cda3 JA |
2028 | retval = execute_command (clauses->action); |
2029 | EXIT_CASE (); | |
726f6388 JA |
2030 | } |
2031 | ||
726f6388 JA |
2032 | QUIT; |
2033 | } | |
726f6388 JA |
2034 | } |
2035 | ||
ccc6cda3 JA |
2036 | exit_case_command: |
2037 | free (word); | |
726f6388 | 2038 | discard_unwind_frame ("case"); |
726f6388 JA |
2039 | return (retval); |
2040 | } | |
2041 | ||
2042 | #define CMD_WHILE 0 | |
2043 | #define CMD_UNTIL 1 | |
2044 | ||
2045 | /* The WHILE command. Syntax: WHILE test DO action; DONE. | |
2046 | Repeatedly execute action while executing test produces | |
2047 | EXECUTION_SUCCESS. */ | |
ccc6cda3 | 2048 | static int |
726f6388 JA |
2049 | execute_while_command (while_command) |
2050 | WHILE_COM *while_command; | |
2051 | { | |
2052 | return (execute_while_or_until (while_command, CMD_WHILE)); | |
2053 | } | |
2054 | ||
2055 | /* UNTIL is just like WHILE except that the test result is negated. */ | |
ccc6cda3 | 2056 | static int |
726f6388 JA |
2057 | execute_until_command (while_command) |
2058 | WHILE_COM *while_command; | |
2059 | { | |
2060 | return (execute_while_or_until (while_command, CMD_UNTIL)); | |
2061 | } | |
2062 | ||
2063 | /* The body for both while and until. The only difference between the | |
2064 | two is that the test value is treated differently. TYPE is | |
2065 | CMD_WHILE or CMD_UNTIL. The return value for both commands should | |
2066 | be EXECUTION_SUCCESS if no commands in the body are executed, and | |
2067 | the status of the last command executed in the body otherwise. */ | |
ccc6cda3 | 2068 | static int |
726f6388 JA |
2069 | execute_while_or_until (while_command, type) |
2070 | WHILE_COM *while_command; | |
2071 | int type; | |
2072 | { | |
2073 | int return_value, body_status; | |
2074 | ||
2075 | body_status = EXECUTION_SUCCESS; | |
2076 | loop_level++; | |
2077 | ||
2078 | while_command->test->flags |= CMD_IGNORE_RETURN; | |
2079 | if (while_command->flags & CMD_IGNORE_RETURN) | |
2080 | while_command->action->flags |= CMD_IGNORE_RETURN; | |
2081 | ||
2082 | while (1) | |
2083 | { | |
2084 | return_value = execute_command (while_command->test); | |
2085 | REAP (); | |
2086 | ||
2087 | if (type == CMD_WHILE && return_value != EXECUTION_SUCCESS) | |
2088 | break; | |
2089 | if (type == CMD_UNTIL && return_value == EXECUTION_SUCCESS) | |
2090 | break; | |
2091 | ||
2092 | QUIT; | |
2093 | body_status = execute_command (while_command->action); | |
2094 | QUIT; | |
2095 | ||
2096 | if (breaking) | |
2097 | { | |
2098 | breaking--; | |
2099 | break; | |
2100 | } | |
2101 | ||
2102 | if (continuing) | |
2103 | { | |
2104 | continuing--; | |
2105 | if (continuing) | |
2106 | break; | |
2107 | } | |
2108 | } | |
2109 | loop_level--; | |
2110 | ||
2111 | return (body_status); | |
2112 | } | |
2113 | ||
2114 | /* IF test THEN command [ELSE command]. | |
2115 | IF also allows ELIF in the place of ELSE IF, but | |
2116 | the parser makes *that* stupidity transparent. */ | |
ccc6cda3 | 2117 | static int |
726f6388 JA |
2118 | execute_if_command (if_command) |
2119 | IF_COM *if_command; | |
2120 | { | |
2121 | int return_value; | |
2122 | ||
2123 | if_command->test->flags |= CMD_IGNORE_RETURN; | |
2124 | return_value = execute_command (if_command->test); | |
2125 | ||
2126 | if (return_value == EXECUTION_SUCCESS) | |
2127 | { | |
2128 | QUIT; | |
ccc6cda3 | 2129 | |
726f6388 | 2130 | if (if_command->true_case && (if_command->flags & CMD_IGNORE_RETURN)) |
ccc6cda3 JA |
2131 | if_command->true_case->flags |= CMD_IGNORE_RETURN; |
2132 | ||
726f6388 JA |
2133 | return (execute_command (if_command->true_case)); |
2134 | } | |
2135 | else | |
2136 | { | |
2137 | QUIT; | |
2138 | ||
2139 | if (if_command->false_case && (if_command->flags & CMD_IGNORE_RETURN)) | |
2140 | if_command->false_case->flags |= CMD_IGNORE_RETURN; | |
2141 | ||
2142 | return (execute_command (if_command->false_case)); | |
2143 | } | |
2144 | } | |
2145 | ||
cce855bc JA |
2146 | #if defined (DPAREN_ARITHMETIC) |
2147 | static int | |
2148 | execute_arith_command (arith_command) | |
2149 | ARITH_COM *arith_command; | |
2150 | { | |
2151 | int result, expok, expresult; | |
2152 | WORD_LIST *new, *p, *printit; | |
2153 | WORD_DESC *w; | |
2154 | ||
2155 | result = 0; | |
2156 | ||
b72432fd | 2157 | this_command_name = "(("; /* )) */ |
cce855bc JA |
2158 | /* If we're in a function, update the line number information. */ |
2159 | if (variable_context) | |
2160 | line_number = arith_command->line - function_line_number; | |
2161 | ||
2162 | new = expand_words (arith_command->exp); | |
2163 | ||
2164 | /* If we're tracing, make a new word list with `((' at the front and `))' | |
2165 | at the back and print it. */ | |
2166 | if (echo_command_at_execute) | |
2167 | xtrace_print_arith_cmd (new); | |
2168 | ||
2169 | result = evalexp (new->word->word, &expok); | |
2170 | dispose_words (new); | |
2171 | ||
2172 | if (expok == 0) | |
2173 | return (EXECUTION_FAILURE); | |
2174 | ||
2175 | return (result == 0 ? EXECUTION_FAILURE : EXECUTION_SUCCESS); | |
2176 | } | |
2177 | #endif /* DPAREN_ARITHMETIC */ | |
2178 | ||
2179 | #if defined (COND_COMMAND) | |
2180 | ||
2181 | static char *nullstr = ""; | |
2182 | ||
2183 | static int | |
2184 | execute_cond_node (cond) | |
2185 | COND_COM *cond; | |
2186 | { | |
b72432fd | 2187 | int result, invert, patmatch, flags; |
cce855bc JA |
2188 | char *arg1, *arg2, *print2; |
2189 | ||
2190 | invert = (cond->flags & CMD_INVERT_RETURN); | |
2191 | ||
2192 | if (cond->type == COND_EXPR) | |
2193 | result = execute_cond_node (cond->left); | |
2194 | else if (cond->type == COND_OR) | |
2195 | { | |
2196 | result = execute_cond_node (cond->left); | |
2197 | if (result != EXECUTION_SUCCESS) | |
2198 | result = execute_cond_node (cond->right); | |
2199 | } | |
2200 | else if (cond->type == COND_AND) | |
2201 | { | |
2202 | result = execute_cond_node (cond->left); | |
2203 | if (result == EXECUTION_SUCCESS) | |
2204 | result = execute_cond_node (cond->right); | |
2205 | } | |
2206 | else if (cond->type == COND_UNARY) | |
2207 | { | |
2208 | arg1 = cond_expand_word (cond->left->op, 0); | |
2209 | if (arg1 == 0) | |
2210 | arg1 = nullstr; | |
2211 | if (echo_command_at_execute) | |
2212 | xtrace_print_cond_term (cond->type, invert, cond->op, arg1, (char *)NULL); | |
2213 | result = unary_test (cond->op->word, arg1) ? EXECUTION_SUCCESS : EXECUTION_FAILURE; | |
2214 | if (arg1 != nullstr) | |
2215 | free (arg1); | |
2216 | } | |
2217 | else if (cond->type == COND_BINARY) | |
2218 | { | |
b72432fd JA |
2219 | patmatch = ((cond->op->word[1] == '=') && (cond->op->word[2] == '\0') && |
2220 | (cond->op->word[0] == '!' || cond->op->word[0] == '=') || | |
2221 | (cond->op->word[0] == '=' && cond->op->word[1] == '\0')); | |
cce855bc JA |
2222 | |
2223 | arg1 = cond_expand_word (cond->left->op, 0); | |
2224 | if (arg1 == 0) | |
2225 | arg1 = nullstr; | |
2226 | arg2 = cond_expand_word (cond->right->op, patmatch); | |
2227 | if (arg2 == 0) | |
2228 | arg2 = nullstr; | |
2229 | ||
2230 | if (echo_command_at_execute) | |
2231 | xtrace_print_cond_term (cond->type, invert, cond->op, arg1, arg2); | |
2232 | ||
2233 | result = binary_test (cond->op->word, arg1, arg2, TEST_PATMATCH|TEST_ARITHEXP) | |
2234 | ? EXECUTION_SUCCESS | |
2235 | : EXECUTION_FAILURE; | |
2236 | if (arg1 != nullstr) | |
2237 | free (arg1); | |
2238 | if (arg2 != nullstr) | |
2239 | free (arg2); | |
2240 | } | |
2241 | else | |
2242 | { | |
b72432fd | 2243 | command_error ("execute_cond_node", CMDERR_BADTYPE, cond->type, 0); |
cce855bc JA |
2244 | jump_to_top_level (DISCARD); |
2245 | result = EXECUTION_FAILURE; | |
2246 | } | |
2247 | ||
2248 | if (invert) | |
2249 | result = (result == EXECUTION_SUCCESS) ? EXECUTION_FAILURE : EXECUTION_SUCCESS; | |
2250 | ||
2251 | return result; | |
2252 | } | |
2253 | ||
2254 | static int | |
2255 | execute_cond_command (cond_command) | |
2256 | COND_COM *cond_command; | |
2257 | { | |
2258 | int result; | |
2259 | ||
2260 | result = EXECUTION_SUCCESS; | |
2261 | ||
2262 | this_command_name = "[["; | |
2263 | /* If we're in a function, update the line number information. */ | |
2264 | if (variable_context) | |
2265 | line_number = cond_command->line - function_line_number; | |
2266 | ||
2267 | #if 0 | |
2268 | debug_print_cond_command (cond_command); | |
2269 | #endif | |
2270 | last_command_exit_value = result = execute_cond_node (cond_command); | |
2271 | return (result); | |
2272 | } | |
2273 | #endif /* COND_COMMAND */ | |
2274 | ||
726f6388 JA |
2275 | static void |
2276 | bind_lastarg (arg) | |
2277 | char *arg; | |
2278 | { | |
2279 | SHELL_VAR *var; | |
2280 | ||
ccc6cda3 | 2281 | if (arg == 0) |
726f6388 JA |
2282 | arg = ""; |
2283 | var = bind_variable ("_", arg); | |
bb70624e | 2284 | VUNSETATTR (var, att_exported); |
726f6388 JA |
2285 | } |
2286 | ||
ccc6cda3 JA |
2287 | /* Execute a null command. Fork a subshell if the command uses pipes or is |
2288 | to be run asynchronously. This handles all the side effects that are | |
2289 | supposed to take place. */ | |
2290 | static int | |
2291 | execute_null_command (redirects, pipe_in, pipe_out, async, old_last_command_subst_pid) | |
2292 | REDIRECT *redirects; | |
bb70624e JA |
2293 | int pipe_in, pipe_out, async; |
2294 | pid_t old_last_command_subst_pid; | |
ccc6cda3 JA |
2295 | { |
2296 | if (pipe_in != NO_PIPE || pipe_out != NO_PIPE || async) | |
2297 | { | |
2298 | /* We have a null command, but we really want a subshell to take | |
2299 | care of it. Just fork, do piping and redirections, and exit. */ | |
2300 | if (make_child ((char *)NULL, async) == 0) | |
2301 | { | |
2302 | /* Cancel traps, in trap.c. */ | |
2303 | restore_original_signals (); /* XXX */ | |
2304 | ||
2305 | do_piping (pipe_in, pipe_out); | |
2306 | ||
2307 | subshell_environment = SUBSHELL_ASYNC; | |
2308 | ||
2309 | if (do_redirections (redirects, 1, 0, 0) == 0) | |
2310 | exit (EXECUTION_SUCCESS); | |
2311 | else | |
2312 | exit (EXECUTION_FAILURE); | |
2313 | } | |
2314 | else | |
2315 | { | |
2316 | close_pipes (pipe_in, pipe_out); | |
2317 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
2318 | unlink_fifo_list (); | |
2319 | #endif | |
2320 | return (EXECUTION_SUCCESS); | |
2321 | } | |
2322 | } | |
2323 | else | |
2324 | { | |
2325 | /* Even if there aren't any command names, pretend to do the | |
2326 | redirections that are specified. The user expects the side | |
2327 | effects to take place. If the redirections fail, then return | |
2328 | failure. Otherwise, if a command substitution took place while | |
2329 | expanding the command or a redirection, return the value of that | |
2330 | substitution. Otherwise, return EXECUTION_SUCCESS. */ | |
2331 | ||
2332 | if (do_redirections (redirects, 0, 0, 0) != 0) | |
2333 | return (EXECUTION_FAILURE); | |
2334 | else if (old_last_command_subst_pid != last_command_subst_pid) | |
2335 | return (last_command_exit_value); | |
2336 | else | |
2337 | return (EXECUTION_SUCCESS); | |
2338 | } | |
2339 | } | |
2340 | ||
2341 | /* This is a hack to suppress word splitting for assignment statements | |
2342 | given as arguments to builtins with the ASSIGNMENT_BUILTIN flag set. */ | |
2343 | static void | |
2344 | fix_assignment_words (words) | |
2345 | WORD_LIST *words; | |
2346 | { | |
2347 | WORD_LIST *w; | |
2348 | struct builtin *b; | |
2349 | ||
2350 | if (words == 0) | |
2351 | return; | |
2352 | ||
e8ce775d | 2353 | b = builtin_address_internal (words->word->word, 0); |
ccc6cda3 JA |
2354 | if (b == 0 || (b->flags & ASSIGNMENT_BUILTIN) == 0) |
2355 | return; | |
2356 | ||
2357 | for (w = words; w; w = w->next) | |
2358 | if (w->word->flags & W_ASSIGNMENT) | |
b72432fd | 2359 | w->word->flags |= (W_NOSPLIT|W_NOGLOB); |
ccc6cda3 JA |
2360 | } |
2361 | ||
726f6388 JA |
2362 | /* The meaty part of all the executions. We have to start hacking the |
2363 | real execution of commands here. Fork a process, set things up, | |
2364 | execute the command. */ | |
ccc6cda3 | 2365 | static int |
726f6388 JA |
2366 | execute_simple_command (simple_command, pipe_in, pipe_out, async, fds_to_close) |
2367 | SIMPLE_COM *simple_command; | |
2368 | int pipe_in, pipe_out, async; | |
2369 | struct fd_bitmap *fds_to_close; | |
2370 | { | |
2371 | WORD_LIST *words, *lastword; | |
ccc6cda3 | 2372 | char *command_line, *lastarg, *temp; |
bc4cd23c | 2373 | int first_word_quoted, result, builtin_is_special, already_forked, dofork; |
b72432fd | 2374 | pid_t old_last_command_subst_pid, old_last_async_pid; |
ccc6cda3 JA |
2375 | Function *builtin; |
2376 | SHELL_VAR *func; | |
726f6388 JA |
2377 | |
2378 | result = EXECUTION_SUCCESS; | |
ccc6cda3 | 2379 | special_builtin_failed = builtin_is_special = 0; |
cce855bc | 2380 | command_line = (char *)0; |
726f6388 | 2381 | |
ccc6cda3 | 2382 | /* If we're in a function, update the line number information. */ |
726f6388 JA |
2383 | if (variable_context) |
2384 | line_number = simple_command->line - function_line_number; | |
2385 | ||
2386 | /* Remember what this command line looks like at invocation. */ | |
2387 | command_string_index = 0; | |
2388 | print_simple_command (simple_command); | |
726f6388 JA |
2389 | |
2390 | first_word_quoted = | |
ccc6cda3 | 2391 | simple_command->words ? (simple_command->words->word->flags & W_QUOTED): 0; |
726f6388 JA |
2392 | |
2393 | old_last_command_subst_pid = last_command_subst_pid; | |
b72432fd | 2394 | old_last_async_pid = last_asynchronous_pid; |
726f6388 | 2395 | |
bc4cd23c JA |
2396 | already_forked = dofork = 0; |
2397 | ||
2398 | /* If we're in a pipeline or run in the background, set DOFORK so we | |
2399 | make the child early, before word expansion. This keeps assignment | |
2400 | statements from affecting the parent shell's environment when they | |
2401 | should not. */ | |
2402 | dofork = pipe_in != NO_PIPE || pipe_out != NO_PIPE || async; | |
2403 | ||
2404 | /* Something like `%2 &' should restart job 2 in the background, not cause | |
2405 | the shell to fork here. */ | |
2406 | if (dofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE && | |
2407 | simple_command->words && simple_command->words->word && | |
2408 | simple_command->words->word->word && | |
2409 | (simple_command->words->word->word[0] == '%')) | |
2410 | dofork = 0; | |
2411 | ||
2412 | if (dofork) | |
cce855bc JA |
2413 | { |
2414 | /* XXX memory leak if expand_words() error causes a jump_to_top_level */ | |
2415 | command_line = savestring (the_printed_command); | |
2416 | ||
bb70624e JA |
2417 | /* Do this now, because execute_disk_command will do it anyway in the |
2418 | vast majority of cases. */ | |
2419 | maybe_make_export_env (); | |
2420 | ||
cce855bc JA |
2421 | if (make_child (command_line, async) == 0) |
2422 | { | |
2423 | already_forked = 1; | |
2424 | simple_command->flags |= CMD_NO_FORK; | |
2425 | ||
28ef6c31 JA |
2426 | subshell_environment = (pipe_in != NO_PIPE || pipe_out != NO_PIPE) |
2427 | ? (SUBSHELL_PIPE|SUBSHELL_FORK) | |
2428 | : (SUBSHELL_ASYNC|SUBSHELL_FORK); | |
2429 | ||
bb70624e JA |
2430 | /* We need to do this before piping to handle some really |
2431 | pathological cases where one of the pipe file descriptors | |
2432 | is < 2. */ | |
2433 | if (fds_to_close) | |
2434 | close_fd_bitmap (fds_to_close); | |
2435 | ||
cce855bc JA |
2436 | do_piping (pipe_in, pipe_out); |
2437 | pipe_in = pipe_out = -1; | |
2438 | ||
b72432fd | 2439 | last_asynchronous_pid = old_last_async_pid; |
cce855bc JA |
2440 | } |
2441 | else | |
2442 | { | |
2443 | close_pipes (pipe_in, pipe_out); | |
2444 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
2445 | unlink_fifo_list (); | |
2446 | #endif | |
2447 | command_line = (char *)NULL; /* don't free this. */ | |
2448 | bind_lastarg ((char *)NULL); | |
2449 | return (result); | |
2450 | } | |
2451 | } | |
2452 | ||
726f6388 JA |
2453 | /* If we are re-running this as the result of executing the `command' |
2454 | builtin, do not expand the command words a second time. */ | |
2455 | if ((simple_command->flags & CMD_INHIBIT_EXPANSION) == 0) | |
2456 | { | |
2457 | current_fds_to_close = fds_to_close; | |
ccc6cda3 | 2458 | fix_assignment_words (simple_command->words); |
726f6388 JA |
2459 | words = expand_words (simple_command->words); |
2460 | current_fds_to_close = (struct fd_bitmap *)NULL; | |
2461 | } | |
2462 | else | |
2463 | words = copy_word_list (simple_command->words); | |
2464 | ||
726f6388 JA |
2465 | /* It is possible for WORDS not to have anything left in it. |
2466 | Perhaps all the words consisted of `$foo', and there was | |
2467 | no variable `$foo'. */ | |
ccc6cda3 | 2468 | if (words == 0) |
726f6388 | 2469 | { |
ccc6cda3 | 2470 | result = execute_null_command (simple_command->redirects, |
cce855bc JA |
2471 | pipe_in, pipe_out, |
2472 | already_forked ? 0 : async, | |
ccc6cda3 | 2473 | old_last_command_subst_pid); |
cce855bc JA |
2474 | if (already_forked) |
2475 | exit (result); | |
2476 | else | |
2477 | { | |
2478 | bind_lastarg ((char *)NULL); | |
2479 | set_pipestatus_from_exit (result); | |
2480 | return (result); | |
2481 | } | |
ccc6cda3 | 2482 | } |
726f6388 | 2483 | |
ccc6cda3 | 2484 | lastarg = (char *)NULL; |
726f6388 | 2485 | |
ccc6cda3 | 2486 | begin_unwind_frame ("simple-command"); |
726f6388 | 2487 | |
ccc6cda3 JA |
2488 | if (echo_command_at_execute) |
2489 | xtrace_print_word_list (words); | |
726f6388 | 2490 | |
ccc6cda3 JA |
2491 | builtin = (Function *)NULL; |
2492 | func = (SHELL_VAR *)NULL; | |
2493 | if ((simple_command->flags & CMD_NO_FUNCTIONS) == 0) | |
2494 | { | |
2495 | /* Posix.2 says special builtins are found before functions. We | |
2496 | don't set builtin_is_special anywhere other than here, because | |
2497 | this path is followed only when the `command' builtin is *not* | |
2498 | being used, and we don't want to exit the shell if a special | |
2499 | builtin executed with `command builtin' fails. `command' is not | |
2500 | a special builtin. */ | |
2501 | if (posixly_correct) | |
2502 | { | |
2503 | builtin = find_special_builtin (words->word->word); | |
2504 | if (builtin) | |
2505 | builtin_is_special = 1; | |
726f6388 | 2506 | } |
ccc6cda3 | 2507 | if (builtin == 0) |
726f6388 | 2508 | func = find_function (words->word->word); |
ccc6cda3 | 2509 | } |
726f6388 | 2510 | |
ccc6cda3 JA |
2511 | add_unwind_protect (dispose_words, words); |
2512 | QUIT; | |
726f6388 | 2513 | |
ccc6cda3 JA |
2514 | /* Bind the last word in this command to "$_" after execution. */ |
2515 | for (lastword = words; lastword->next; lastword = lastword->next) | |
2516 | ; | |
2517 | lastarg = lastword->word->word; | |
726f6388 JA |
2518 | |
2519 | #if defined (JOB_CONTROL) | |
ccc6cda3 | 2520 | /* Is this command a job control related thing? */ |
cce855bc | 2521 | if (words->word->word[0] == '%' && already_forked == 0) |
ccc6cda3 JA |
2522 | { |
2523 | this_command_name = async ? "bg" : "fg"; | |
726f6388 | 2524 | last_shell_builtin = this_shell_builtin; |
ccc6cda3 JA |
2525 | this_shell_builtin = builtin_address (this_command_name); |
2526 | result = (*this_shell_builtin) (words); | |
2527 | goto return_result; | |
2528 | } | |
726f6388 | 2529 | |
ccc6cda3 JA |
2530 | /* One other possiblilty. The user may want to resume an existing job. |
2531 | If they do, find out whether this word is a candidate for a running | |
2532 | job. */ | |
cce855bc | 2533 | if (job_control && already_forked == 0 && async == 0 && |
ccc6cda3 JA |
2534 | !first_word_quoted && |
2535 | !words->next && | |
2536 | words->word->word[0] && | |
2537 | !simple_command->redirects && | |
2538 | pipe_in == NO_PIPE && | |
2539 | pipe_out == NO_PIPE && | |
2540 | (temp = get_string_value ("auto_resume"))) | |
2541 | { | |
2542 | char *word; | |
2543 | register int i; | |
2544 | int wl, cl, exact, substring, match, started_status; | |
2545 | register PROCESS *p; | |
2546 | ||
2547 | word = words->word->word; | |
2548 | exact = STREQ (temp, "exact"); | |
2549 | substring = STREQ (temp, "substring"); | |
2550 | wl = strlen (word); | |
2551 | for (i = job_slots - 1; i > -1; i--) | |
726f6388 | 2552 | { |
ccc6cda3 JA |
2553 | if (jobs[i] == 0 || (JOBSTATE (i) != JSTOPPED)) |
2554 | continue; | |
2555 | ||
2556 | p = jobs[i]->pipe; | |
2557 | do | |
726f6388 | 2558 | { |
ccc6cda3 | 2559 | if (exact) |
726f6388 | 2560 | { |
ccc6cda3 JA |
2561 | cl = strlen (p->command); |
2562 | match = STREQN (p->command, word, cl); | |
726f6388 | 2563 | } |
ccc6cda3 JA |
2564 | else if (substring) |
2565 | match = strindex (p->command, word) != (char *)0; | |
726f6388 | 2566 | else |
ccc6cda3 JA |
2567 | match = STREQN (p->command, word, wl); |
2568 | ||
2569 | if (match == 0) | |
726f6388 | 2570 | { |
ccc6cda3 JA |
2571 | p = p->next; |
2572 | continue; | |
726f6388 | 2573 | } |
726f6388 | 2574 | |
ccc6cda3 JA |
2575 | run_unwind_frame ("simple-command"); |
2576 | this_command_name = "fg"; | |
2577 | last_shell_builtin = this_shell_builtin; | |
2578 | this_shell_builtin = builtin_address ("fg"); | |
2579 | ||
2580 | started_status = start_job (i, 1); | |
2581 | return ((started_status < 0) ? EXECUTION_FAILURE : started_status); | |
726f6388 | 2582 | } |
ccc6cda3 | 2583 | while (p != jobs[i]->pipe); |
726f6388 | 2584 | } |
ccc6cda3 JA |
2585 | } |
2586 | #endif /* JOB_CONTROL */ | |
726f6388 | 2587 | |
ccc6cda3 JA |
2588 | /* Remember the name of this command globally. */ |
2589 | this_command_name = words->word->word; | |
726f6388 | 2590 | |
ccc6cda3 JA |
2591 | QUIT; |
2592 | ||
2593 | /* This command could be a shell builtin or a user-defined function. | |
2594 | We have already found special builtins by this time, so we do not | |
2595 | set builtin_is_special. If this is a function or builtin, and we | |
2596 | have pipes, then fork a subshell in here. Otherwise, just execute | |
2597 | the command directly. */ | |
2598 | if (func == 0 && builtin == 0) | |
2599 | builtin = find_shell_builtin (this_command_name); | |
2600 | ||
2601 | last_shell_builtin = this_shell_builtin; | |
2602 | this_shell_builtin = builtin; | |
2603 | ||
2604 | if (builtin || func) | |
726f6388 | 2605 | { |
28ef6c31 | 2606 | if (already_forked) |
726f6388 | 2607 | { |
cce855bc JA |
2608 | /* reset_terminating_signals (); */ /* XXX */ |
2609 | /* Cancel traps, in trap.c. */ | |
2610 | restore_original_signals (); | |
726f6388 | 2611 | |
cce855bc | 2612 | if (async) |
ccc6cda3 | 2613 | { |
cce855bc JA |
2614 | if ((simple_command->flags & CMD_STDIN_REDIR) && |
2615 | pipe_in == NO_PIPE && | |
2616 | (stdin_redirects (simple_command->redirects) == 0)) | |
2617 | async_redirect_stdin (); | |
2618 | setup_async_signals (); | |
ccc6cda3 | 2619 | } |
cce855bc JA |
2620 | |
2621 | execute_subshell_builtin_or_function | |
2622 | (words, simple_command->redirects, builtin, func, | |
2623 | pipe_in, pipe_out, async, fds_to_close, | |
2624 | simple_command->flags); | |
726f6388 JA |
2625 | } |
2626 | else | |
2627 | { | |
ccc6cda3 JA |
2628 | result = execute_builtin_or_function |
2629 | (words, builtin, func, simple_command->redirects, fds_to_close, | |
2630 | simple_command->flags); | |
2631 | if (builtin) | |
2632 | { | |
2633 | if (result > EX_SHERRBASE) | |
2634 | { | |
cce855bc JA |
2635 | result = builtin_status (result); |
2636 | if (builtin_is_special) | |
2637 | special_builtin_failed = 1; | |
ccc6cda3 JA |
2638 | } |
2639 | /* In POSIX mode, if there are assignment statements preceding | |
2640 | a special builtin, they persist after the builtin | |
2641 | completes. */ | |
2642 | if (posixly_correct && builtin_is_special && temporary_env) | |
2643 | merge_temporary_env (); | |
2644 | } | |
2645 | else /* function */ | |
2646 | { | |
2647 | if (result == EX_USAGE) | |
2648 | result = EX_BADUSAGE; | |
2649 | else if (result > EX_SHERRBASE) | |
cce855bc | 2650 | result = EXECUTION_FAILURE; |
ccc6cda3 JA |
2651 | } |
2652 | ||
cce855bc JA |
2653 | set_pipestatus_from_exit (result); |
2654 | ||
726f6388 JA |
2655 | goto return_result; |
2656 | } | |
2657 | } | |
726f6388 | 2658 | |
cce855bc JA |
2659 | if (command_line == 0) |
2660 | command_line = savestring (the_printed_command); | |
2661 | ||
ccc6cda3 JA |
2662 | execute_disk_command (words, simple_command->redirects, command_line, |
2663 | pipe_in, pipe_out, async, fds_to_close, | |
d166f048 | 2664 | simple_command->flags); |
726f6388 JA |
2665 | |
2666 | return_result: | |
2667 | bind_lastarg (lastarg); | |
ccc6cda3 JA |
2668 | FREE (command_line); |
2669 | run_unwind_frame ("simple-command"); | |
726f6388 JA |
2670 | return (result); |
2671 | } | |
2672 | ||
ccc6cda3 JA |
2673 | /* Translate the special builtin exit statuses. We don't really need a |
2674 | function for this; it's a placeholder for future work. */ | |
2675 | static int | |
2676 | builtin_status (result) | |
2677 | int result; | |
2678 | { | |
2679 | int r; | |
2680 | ||
2681 | switch (result) | |
2682 | { | |
2683 | case EX_USAGE: | |
2684 | r = EX_BADUSAGE; | |
2685 | break; | |
2686 | case EX_REDIRFAIL: | |
2687 | case EX_BADSYNTAX: | |
2688 | case EX_BADASSIGN: | |
2689 | case EX_EXPFAIL: | |
2690 | r = EXECUTION_FAILURE; | |
2691 | break; | |
2692 | default: | |
2693 | r = EXECUTION_SUCCESS; | |
2694 | break; | |
2695 | } | |
2696 | return (r); | |
2697 | } | |
2698 | ||
726f6388 JA |
2699 | static int |
2700 | execute_builtin (builtin, words, flags, subshell) | |
2701 | Function *builtin; | |
2702 | WORD_LIST *words; | |
2703 | int flags, subshell; | |
2704 | { | |
d166f048 | 2705 | int old_e_flag, result, eval_unwind; |
726f6388 | 2706 | |
ccc6cda3 | 2707 | old_e_flag = exit_immediately_on_error; |
726f6388 JA |
2708 | /* The eval builtin calls parse_and_execute, which does not know about |
2709 | the setting of flags, and always calls the execution functions with | |
2710 | flags that will exit the shell on an error if -e is set. If the | |
2711 | eval builtin is being called, and we're supposed to ignore the exit | |
2712 | value of the command, we turn the -e flag off ourselves, then | |
2713 | restore it when the command completes. */ | |
2714 | if (subshell == 0 && builtin == eval_builtin && (flags & CMD_IGNORE_RETURN)) | |
2715 | { | |
2716 | begin_unwind_frame ("eval_builtin"); | |
2717 | unwind_protect_int (exit_immediately_on_error); | |
2718 | exit_immediately_on_error = 0; | |
d166f048 | 2719 | eval_unwind = 1; |
726f6388 | 2720 | } |
d166f048 JA |
2721 | else |
2722 | eval_unwind = 0; | |
726f6388 JA |
2723 | |
2724 | /* The temporary environment for a builtin is supposed to apply to | |
2725 | all commands executed by that builtin. Currently, this is a | |
d166f048 JA |
2726 | problem only with the `source' and `eval' builtins. */ |
2727 | if (builtin == source_builtin || builtin == eval_builtin) | |
726f6388 JA |
2728 | { |
2729 | if (subshell == 0) | |
2730 | begin_unwind_frame ("builtin_env"); | |
2731 | ||
2732 | if (temporary_env) | |
2733 | { | |
2734 | builtin_env = copy_array (temporary_env); | |
2735 | if (subshell == 0) | |
2736 | add_unwind_protect (dispose_builtin_env, (char *)NULL); | |
2737 | dispose_used_env_vars (); | |
2738 | } | |
28ef6c31 JA |
2739 | /* Otherwise we inherit builtin_env from our caller. */ |
2740 | } | |
2741 | ||
2742 | /* `return' does a longjmp() back to a saved environment in execute_function. | |
2743 | If a variable assignment list preceded the command, and the shell is | |
2744 | running in POSIX mode, we need to merge that into the shell_variables | |
2745 | table, since `return' is a POSIX special builtin. */ | |
2746 | if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env) | |
2747 | { | |
2748 | begin_unwind_frame ("return_temp_env"); | |
2749 | add_unwind_protect (merge_temporary_env, (char *)NULL); | |
726f6388 JA |
2750 | } |
2751 | ||
2752 | result = ((*builtin) (words->next)); | |
2753 | ||
28ef6c31 JA |
2754 | /* This shouldn't happen, but in case `return' comes back instead of |
2755 | longjmp'ing, we need to unwind. */ | |
2756 | if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env) | |
2757 | discard_unwind_frame ("return_temp_env"); | |
2758 | ||
d166f048 | 2759 | if (subshell == 0 && (builtin == source_builtin || builtin == eval_builtin)) |
726f6388 | 2760 | { |
d166f048 JA |
2761 | /* In POSIX mode, if any variable assignments precede the `.' or |
2762 | `eval' builtin, they persist after the builtin completes, since `.' | |
2763 | and `eval' are special builtins. */ | |
ccc6cda3 JA |
2764 | if (posixly_correct && builtin_env) |
2765 | merge_builtin_env (); | |
28ef6c31 | 2766 | |
cce855bc | 2767 | run_unwind_frame ("builtin_env"); |
726f6388 JA |
2768 | } |
2769 | ||
d166f048 | 2770 | if (eval_unwind) |
726f6388 JA |
2771 | { |
2772 | exit_immediately_on_error += old_e_flag; | |
2773 | discard_unwind_frame ("eval_builtin"); | |
2774 | } | |
2775 | ||
2776 | return (result); | |
2777 | } | |
2778 | ||
726f6388 JA |
2779 | static int |
2780 | execute_function (var, words, flags, fds_to_close, async, subshell) | |
2781 | SHELL_VAR *var; | |
2782 | WORD_LIST *words; | |
2783 | int flags, subshell, async; | |
2784 | struct fd_bitmap *fds_to_close; | |
2785 | { | |
2786 | int return_val, result; | |
2787 | COMMAND *tc, *fc; | |
ccc6cda3 | 2788 | char *debug_trap; |
bb70624e | 2789 | SHELL_VAR *old_shell_function; |
726f6388 JA |
2790 | |
2791 | tc = (COMMAND *)copy_command (function_cell (var)); | |
2792 | if (tc && (flags & CMD_IGNORE_RETURN)) | |
2793 | tc->flags |= CMD_IGNORE_RETURN; | |
2794 | ||
726f6388 JA |
2795 | if (subshell == 0) |
2796 | { | |
ccc6cda3 | 2797 | begin_unwind_frame ("function_calling"); |
726f6388 JA |
2798 | push_context (); |
2799 | add_unwind_protect (pop_context, (char *)NULL); | |
2800 | unwind_protect_int (line_number); | |
ccc6cda3 JA |
2801 | unwind_protect_int (return_catch_flag); |
2802 | unwind_protect_jmp_buf (return_catch); | |
2803 | add_unwind_protect (dispose_command, (char *)tc); | |
bb70624e | 2804 | unwind_protect_pointer (this_shell_function); |
ccc6cda3 | 2805 | unwind_protect_int (loop_level); |
726f6388 | 2806 | } |
726f6388 | 2807 | |
bb70624e JA |
2808 | this_shell_function = var; |
2809 | make_funcname_visible (1); | |
2810 | ||
ccc6cda3 JA |
2811 | debug_trap = (signal_is_trapped (DEBUG_TRAP) && signal_is_ignored (DEBUG_TRAP) == 0) |
2812 | ? trap_list[DEBUG_TRAP] | |
2813 | : (char *)NULL; | |
2814 | if (debug_trap) | |
2815 | { | |
2816 | if (subshell == 0) | |
2817 | { | |
2818 | debug_trap = savestring (debug_trap); | |
cce855bc JA |
2819 | /* XXX order is important here! unwind-protect commands are run |
2820 | in reverse order of registering. If this causes problems, | |
2821 | take out the xfree unwind-protect and live with the small | |
2822 | memory leak. */ | |
2823 | add_unwind_protect (xfree, debug_trap); | |
ccc6cda3 JA |
2824 | add_unwind_protect (set_debug_trap, debug_trap); |
2825 | } | |
2826 | restore_default_signal (DEBUG_TRAP); | |
2827 | } | |
726f6388 JA |
2828 | |
2829 | /* The temporary environment for a function is supposed to apply to | |
2830 | all commands executed within the function body. */ | |
2831 | if (temporary_env) | |
2832 | { | |
2833 | function_env = copy_array (temporary_env); | |
28ef6c31 JA |
2834 | /* In POSIX mode, variable assignments preceding function names are |
2835 | supposed to persist in the environment after the function returns, | |
2836 | as if a special builtin command had been executed. */ | |
ccc6cda3 | 2837 | if (subshell == 0) |
28ef6c31 JA |
2838 | { |
2839 | if (posixly_correct) | |
2840 | add_unwind_protect (merge_function_env, (char *)NULL); | |
2841 | else | |
2842 | add_unwind_protect (dispose_function_env, (char *)NULL); | |
2843 | } | |
726f6388 JA |
2844 | dispose_used_env_vars (); |
2845 | } | |
28ef6c31 | 2846 | /* Otherwise, we inherit function_env from our caller. */ |
726f6388 | 2847 | |
726f6388 JA |
2848 | remember_args (words->next, 1); |
2849 | ||
ccc6cda3 | 2850 | /* Number of the line on which the function body starts. */ |
726f6388 JA |
2851 | line_number = function_line_number = tc->line; |
2852 | ||
2853 | if (subshell) | |
2854 | { | |
2855 | #if defined (JOB_CONTROL) | |
2856 | stop_pipeline (async, (COMMAND *)NULL); | |
2857 | #endif | |
ccc6cda3 | 2858 | fc = (tc->type == cm_group) ? tc->value.Group->command : tc; |
726f6388 JA |
2859 | |
2860 | if (fc && (flags & CMD_IGNORE_RETURN)) | |
2861 | fc->flags |= CMD_IGNORE_RETURN; | |
2862 | ||
2863 | variable_context++; | |
2864 | } | |
2865 | else | |
2866 | fc = tc; | |
2867 | ||
2868 | return_catch_flag++; | |
2869 | return_val = setjmp (return_catch); | |
2870 | ||
2871 | if (return_val) | |
2872 | result = return_catch_value; | |
2873 | else | |
2874 | result = execute_command_internal (fc, 0, NO_PIPE, NO_PIPE, fds_to_close); | |
2875 | ||
ccc6cda3 | 2876 | if (subshell == 0) |
726f6388 JA |
2877 | run_unwind_frame ("function_calling"); |
2878 | ||
bb70624e JA |
2879 | if (variable_context == 0 || this_shell_function == 0) |
2880 | make_funcname_visible (0); | |
2881 | ||
726f6388 JA |
2882 | return (result); |
2883 | } | |
2884 | ||
bb70624e JA |
2885 | /* A convenience routine for use by other parts of the shell to execute |
2886 | a particular shell function. */ | |
2887 | int | |
2888 | execute_shell_function (var, words) | |
2889 | SHELL_VAR *var; | |
2890 | WORD_LIST *words; | |
2891 | { | |
2892 | int ret; | |
2893 | struct fd_bitmap *bitmap; | |
2894 | ||
2895 | bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE); | |
2896 | begin_unwind_frame ("execute-shell-function"); | |
2897 | add_unwind_protect (dispose_fd_bitmap, (char *)bitmap); | |
2898 | ||
2899 | ret = execute_function (var, words, 0, bitmap, 0, 0); | |
2900 | ||
2901 | dispose_fd_bitmap (bitmap); | |
2902 | discard_unwind_frame ("execute-shell-function"); | |
2903 | ||
2904 | return ret; | |
2905 | } | |
2906 | ||
726f6388 JA |
2907 | /* Execute a shell builtin or function in a subshell environment. This |
2908 | routine does not return; it only calls exit(). If BUILTIN is non-null, | |
2909 | it points to a function to call to execute a shell builtin; otherwise | |
2910 | VAR points at the body of a function to execute. WORDS is the arguments | |
2911 | to the command, REDIRECTS specifies redirections to perform before the | |
2912 | command is executed. */ | |
2913 | static void | |
2914 | execute_subshell_builtin_or_function (words, redirects, builtin, var, | |
2915 | pipe_in, pipe_out, async, fds_to_close, | |
2916 | flags) | |
2917 | WORD_LIST *words; | |
2918 | REDIRECT *redirects; | |
2919 | Function *builtin; | |
2920 | SHELL_VAR *var; | |
2921 | int pipe_in, pipe_out, async; | |
2922 | struct fd_bitmap *fds_to_close; | |
2923 | int flags; | |
2924 | { | |
bb70624e JA |
2925 | int result, r; |
2926 | #if defined (JOB_CONTROL) | |
2927 | int jobs_hack; | |
726f6388 | 2928 | |
b72432fd JA |
2929 | jobs_hack = (builtin == jobs_builtin) && |
2930 | ((subshell_environment & SUBSHELL_ASYNC) == 0 || pipe_out != NO_PIPE); | |
bb70624e JA |
2931 | #endif |
2932 | ||
2933 | /* A subshell is neither a login shell nor interactive. */ | |
2934 | login_shell = interactive = 0; | |
b72432fd | 2935 | |
ccc6cda3 | 2936 | subshell_environment = SUBSHELL_ASYNC; |
726f6388 | 2937 | |
ccc6cda3 | 2938 | maybe_make_export_env (); /* XXX - is this needed? */ |
726f6388 JA |
2939 | |
2940 | #if defined (JOB_CONTROL) | |
2941 | /* Eradicate all traces of job control after we fork the subshell, so | |
2942 | all jobs begun by this subshell are in the same process group as | |
2943 | the shell itself. */ | |
2944 | ||
2945 | /* Allow the output of `jobs' to be piped. */ | |
b72432fd | 2946 | if (jobs_hack) |
726f6388 JA |
2947 | kill_current_pipeline (); |
2948 | else | |
2949 | without_job_control (); | |
2950 | ||
2951 | set_sigchld_handler (); | |
2952 | #endif /* JOB_CONTROL */ | |
2953 | ||
2954 | set_sigint_handler (); | |
2955 | ||
726f6388 JA |
2956 | if (fds_to_close) |
2957 | close_fd_bitmap (fds_to_close); | |
2958 | ||
bb70624e JA |
2959 | do_piping (pipe_in, pipe_out); |
2960 | ||
726f6388 JA |
2961 | if (do_redirections (redirects, 1, 0, 0) != 0) |
2962 | exit (EXECUTION_FAILURE); | |
2963 | ||
2964 | if (builtin) | |
2965 | { | |
726f6388 JA |
2966 | /* Give builtins a place to jump back to on failure, |
2967 | so we don't go back up to main(). */ | |
2968 | result = setjmp (top_level); | |
2969 | ||
2970 | if (result == EXITPROG) | |
2971 | exit (last_command_exit_value); | |
2972 | else if (result) | |
2973 | exit (EXECUTION_FAILURE); | |
2974 | else | |
cce855bc JA |
2975 | { |
2976 | r = execute_builtin (builtin, words, flags, 1); | |
2977 | if (r == EX_USAGE) | |
2978 | r = EX_BADUSAGE; | |
2979 | exit (r); | |
2980 | } | |
726f6388 JA |
2981 | } |
2982 | else | |
ccc6cda3 | 2983 | exit (execute_function (var, words, flags, fds_to_close, async, 1)); |
726f6388 JA |
2984 | } |
2985 | ||
2986 | /* Execute a builtin or function in the current shell context. If BUILTIN | |
2987 | is non-null, it is the builtin command to execute, otherwise VAR points | |
2988 | to the body of a function. WORDS are the command's arguments, REDIRECTS | |
2989 | are the redirections to perform. FDS_TO_CLOSE is the usual bitmap of | |
2990 | file descriptors to close. | |
2991 | ||
2992 | If BUILTIN is exec_builtin, the redirections specified in REDIRECTS are | |
2993 | not undone before this function returns. */ | |
2994 | static int | |
2995 | execute_builtin_or_function (words, builtin, var, redirects, | |
2996 | fds_to_close, flags) | |
2997 | WORD_LIST *words; | |
2998 | Function *builtin; | |
2999 | SHELL_VAR *var; | |
3000 | REDIRECT *redirects; | |
3001 | struct fd_bitmap *fds_to_close; | |
3002 | int flags; | |
3003 | { | |
ccc6cda3 | 3004 | int result; |
726f6388 | 3005 | REDIRECT *saved_undo_list; |
28ef6c31 | 3006 | Function *saved_this_shell_builtin; |
726f6388 JA |
3007 | |
3008 | if (do_redirections (redirects, 1, 1, 0) != 0) | |
3009 | { | |
3010 | cleanup_redirects (redirection_undo_list); | |
3011 | redirection_undo_list = (REDIRECT *)NULL; | |
3012 | dispose_exec_redirects (); | |
ccc6cda3 | 3013 | return (EX_REDIRFAIL); /* was EXECUTION_FAILURE */ |
726f6388 JA |
3014 | } |
3015 | ||
28ef6c31 | 3016 | saved_this_shell_builtin = this_shell_builtin; |
726f6388 JA |
3017 | saved_undo_list = redirection_undo_list; |
3018 | ||
3019 | /* Calling the "exec" builtin changes redirections forever. */ | |
3020 | if (builtin == exec_builtin) | |
3021 | { | |
3022 | dispose_redirects (saved_undo_list); | |
3023 | saved_undo_list = exec_redirection_undo_list; | |
3024 | exec_redirection_undo_list = (REDIRECT *)NULL; | |
3025 | } | |
3026 | else | |
3027 | dispose_exec_redirects (); | |
3028 | ||
3029 | if (saved_undo_list) | |
3030 | { | |
3031 | begin_unwind_frame ("saved redirects"); | |
ccc6cda3 | 3032 | add_unwind_protect (cleanup_redirects, (char *)saved_undo_list); |
726f6388 JA |
3033 | } |
3034 | ||
3035 | redirection_undo_list = (REDIRECT *)NULL; | |
3036 | ||
3037 | if (builtin) | |
3038 | result = execute_builtin (builtin, words, flags, 0); | |
3039 | else | |
3040 | result = execute_function (var, words, flags, fds_to_close, 0, 0); | |
3041 | ||
28ef6c31 JA |
3042 | /* If we are executing the `command' builtin, but this_shell_builtin is |
3043 | set to `exec_builtin', we know that we have something like | |
3044 | `command exec [redirection]', since otherwise `exec' would have | |
3045 | overwritten the shell and we wouldn't get here. In this case, we | |
3046 | want to behave as if the `command' builtin had not been specified | |
3047 | and preserve the redirections. */ | |
3048 | if (builtin == command_builtin && this_shell_builtin == exec_builtin) | |
3049 | { | |
3050 | if (saved_undo_list) | |
3051 | dispose_redirects (saved_undo_list); | |
3052 | redirection_undo_list = exec_redirection_undo_list; | |
3053 | saved_undo_list = exec_redirection_undo_list = (REDIRECT *)NULL; | |
3054 | discard_unwind_frame ("saved_redirects"); | |
3055 | } | |
3056 | ||
726f6388 JA |
3057 | if (saved_undo_list) |
3058 | { | |
3059 | redirection_undo_list = saved_undo_list; | |
3060 | discard_unwind_frame ("saved redirects"); | |
3061 | } | |
3062 | ||
3063 | if (redirection_undo_list) | |
3064 | { | |
ccc6cda3 | 3065 | cleanup_redirects (redirection_undo_list); |
726f6388 JA |
3066 | redirection_undo_list = (REDIRECT *)NULL; |
3067 | } | |
3068 | ||
3069 | return (result); | |
3070 | } | |
3071 | ||
3072 | void | |
3073 | setup_async_signals () | |
3074 | { | |
b72432fd JA |
3075 | #if defined (__BEOS__) |
3076 | set_signal_handler (SIGHUP, SIG_IGN); /* they want csh-like behavior */ | |
3077 | #endif | |
3078 | ||
726f6388 JA |
3079 | #if defined (JOB_CONTROL) |
3080 | if (job_control == 0) | |
3081 | #endif | |
3082 | { | |
3083 | set_signal_handler (SIGINT, SIG_IGN); | |
3084 | set_signal_ignored (SIGINT); | |
3085 | set_signal_handler (SIGQUIT, SIG_IGN); | |
3086 | set_signal_ignored (SIGQUIT); | |
3087 | } | |
3088 | } | |
3089 | ||
3090 | /* Execute a simple command that is hopefully defined in a disk file | |
3091 | somewhere. | |
3092 | ||
3093 | 1) fork () | |
3094 | 2) connect pipes | |
3095 | 3) look up the command | |
3096 | 4) do redirections | |
3097 | 5) execve () | |
3098 | 6) If the execve failed, see if the file has executable mode set. | |
3099 | If so, and it isn't a directory, then execute its contents as | |
3100 | a shell script. | |
3101 | ||
3102 | Note that the filename hashing stuff has to take place up here, | |
3103 | in the parent. This is probably why the Bourne style shells | |
3104 | don't handle it, since that would require them to go through | |
3105 | this gnarly hair, for no good reason. */ | |
3106 | static void | |
3107 | execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, | |
d166f048 | 3108 | async, fds_to_close, cmdflags) |
726f6388 JA |
3109 | WORD_LIST *words; |
3110 | REDIRECT *redirects; | |
3111 | char *command_line; | |
3112 | int pipe_in, pipe_out, async; | |
3113 | struct fd_bitmap *fds_to_close; | |
d166f048 | 3114 | int cmdflags; |
726f6388 | 3115 | { |
ccc6cda3 | 3116 | char *pathname, *command, **args; |
d166f048 | 3117 | int nofork; |
bb70624e | 3118 | pid_t pid; |
726f6388 | 3119 | |
d166f048 | 3120 | nofork = (cmdflags & CMD_NO_FORK); /* Don't fork, just exec, if no pipes */ |
726f6388 | 3121 | pathname = words->word->word; |
ccc6cda3 | 3122 | |
726f6388 JA |
3123 | #if defined (RESTRICTED_SHELL) |
3124 | if (restricted && strchr (pathname, '/')) | |
3125 | { | |
ccc6cda3 | 3126 | internal_error ("%s: restricted: cannot specify `/' in command names", |
726f6388 JA |
3127 | pathname); |
3128 | last_command_exit_value = EXECUTION_FAILURE; | |
3129 | return; | |
3130 | } | |
3131 | #endif /* RESTRICTED_SHELL */ | |
3132 | ||
ccc6cda3 | 3133 | command = search_for_command (pathname); |
726f6388 | 3134 | |
ccc6cda3 | 3135 | if (command) |
726f6388 | 3136 | { |
ccc6cda3 JA |
3137 | maybe_make_export_env (); |
3138 | put_command_name_into_env (command); | |
726f6388 JA |
3139 | } |
3140 | ||
bb70624e | 3141 | /* We have to make the child before we check for the non-existence |
726f6388 JA |
3142 | of COMMAND, since we want the error messages to be redirected. */ |
3143 | /* If we can get away without forking and there are no pipes to deal with, | |
3144 | don't bother to fork, just directly exec the command. */ | |
3145 | if (nofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE) | |
3146 | pid = 0; | |
3147 | else | |
3148 | pid = make_child (savestring (command_line), async); | |
3149 | ||
3150 | if (pid == 0) | |
3151 | { | |
3152 | int old_interactive; | |
3153 | ||
d166f048 JA |
3154 | #if 0 |
3155 | /* This has been disabled for the time being. */ | |
ccc6cda3 JA |
3156 | #if !defined (ARG_MAX) || ARG_MAX >= 10240 |
3157 | if (posixly_correct == 0) | |
3158 | put_gnu_argv_flags_into_env ((int)getpid (), glob_argv_flags); | |
d166f048 | 3159 | #endif |
ccc6cda3 JA |
3160 | #endif |
3161 | ||
726f6388 JA |
3162 | /* Cancel traps, in trap.c. */ |
3163 | restore_original_signals (); | |
3164 | ||
3165 | /* restore_original_signals may have undone the work done | |
cce855bc JA |
3166 | by make_child to ensure that SIGINT and SIGQUIT are ignored |
3167 | in asynchronous children. */ | |
726f6388 | 3168 | if (async) |
d166f048 JA |
3169 | { |
3170 | if ((cmdflags & CMD_STDIN_REDIR) && | |
3171 | pipe_in == NO_PIPE && | |
3172 | (stdin_redirects (redirects) == 0)) | |
3173 | async_redirect_stdin (); | |
3174 | setup_async_signals (); | |
3175 | } | |
726f6388 | 3176 | |
bb70624e JA |
3177 | /* This functionality is now provided by close-on-exec of the |
3178 | file descriptors manipulated by redirection and piping. | |
3179 | Some file descriptors still need to be closed in all children | |
3180 | because of the way bash does pipes; fds_to_close is a | |
3181 | bitmap of all such file descriptors. */ | |
3182 | if (fds_to_close) | |
3183 | close_fd_bitmap (fds_to_close); | |
3184 | ||
726f6388 JA |
3185 | do_piping (pipe_in, pipe_out); |
3186 | ||
726f6388 JA |
3187 | if (async) |
3188 | { | |
3189 | old_interactive = interactive; | |
3190 | interactive = 0; | |
3191 | } | |
3192 | ||
ccc6cda3 | 3193 | subshell_environment = SUBSHELL_FORK; |
726f6388 | 3194 | |
726f6388 JA |
3195 | if (redirects && (do_redirections (redirects, 1, 0, 0) != 0)) |
3196 | { | |
3197 | #if defined (PROCESS_SUBSTITUTION) | |
3198 | /* Try to remove named pipes that may have been created as the | |
3199 | result of redirections. */ | |
3200 | unlink_fifo_list (); | |
3201 | #endif /* PROCESS_SUBSTITUTION */ | |
3202 | exit (EXECUTION_FAILURE); | |
3203 | } | |
3204 | ||
3205 | if (async) | |
3206 | interactive = old_interactive; | |
3207 | ||
ccc6cda3 | 3208 | if (command == 0) |
726f6388 | 3209 | { |
ccc6cda3 | 3210 | internal_error ("%s: command not found", pathname); |
726f6388 JA |
3211 | exit (EX_NOTFOUND); /* Posix.2 says the exit status is 127 */ |
3212 | } | |
3213 | ||
ccc6cda3 JA |
3214 | /* Execve expects the command name to be in args[0]. So we |
3215 | leave it there, in the same format that the user used to | |
3216 | type it in. */ | |
3217 | args = word_list_to_argv (words, 0, 0, (int *)NULL); | |
726f6388 JA |
3218 | exit (shell_execve (command, args, export_env)); |
3219 | } | |
3220 | else | |
3221 | { | |
3222 | /* Make sure that the pipes are closed in the parent. */ | |
3223 | close_pipes (pipe_in, pipe_out); | |
3224 | #if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD) | |
3225 | unlink_fifo_list (); | |
3226 | #endif | |
3227 | FREE (command); | |
3228 | } | |
3229 | } | |
3230 | ||
ccc6cda3 | 3231 | #if !defined (HAVE_HASH_BANG_EXEC) |
726f6388 JA |
3232 | /* If the operating system on which we're running does not handle |
3233 | the #! executable format, then help out. SAMPLE is the text read | |
3234 | from the file, SAMPLE_LEN characters. COMMAND is the name of | |
3235 | the script; it and ARGS, the arguments given by the user, will | |
3236 | become arguments to the specified interpreter. ENV is the environment | |
3237 | to pass to the interpreter. | |
3238 | ||
3239 | The word immediately following the #! is the interpreter to execute. | |
3240 | A single argument to the interpreter is allowed. */ | |
28ef6c31 JA |
3241 | |
3242 | /* CPP defines to decide whether a particular index into the #! line | |
3243 | corresponds to a valid interpreter name or argument character, or | |
3244 | whitespace. The MSDOS define is to allow \r to be treated the same | |
3245 | as \n. */ | |
3246 | ||
3247 | #if !defined (MSDOS) | |
3248 | # define STRINGCHAR(ind) \ | |
3249 | (!whitespace (sample[ind]) && sample[ind] != '\n' && ind < sample_len) | |
3250 | # define WHITECHAR(ind) \ | |
3251 | (whitespace (sample[ind]) && sample[ind] != '\n' && ind < sample_len) | |
3252 | #else /* MSDOS */ | |
3253 | # define STRINGCHAR(ind) \ | |
3254 | (!whitespace (sample[ind]) && sample[ind] != '\n' && sample[ind] != '\r' && ind < sample_len) | |
3255 | # define WHITECHAR(ind) \ | |
3256 | (whitespace (sample[ind]) && sample[ind] != '\n' && sample[ind] != '\r' && ind < sample_len) | |
3257 | #endif /* MSDOS */ | |
3258 | ||
726f6388 JA |
3259 | static int |
3260 | execute_shell_script (sample, sample_len, command, args, env) | |
3261 | unsigned char *sample; | |
3262 | int sample_len; | |
3263 | char *command; | |
3264 | char **args, **env; | |
3265 | { | |
3266 | register int i; | |
3267 | char *execname, *firstarg; | |
3268 | int start, size_increment, larry; | |
3269 | ||
3270 | /* Find the name of the interpreter to exec. */ | |
3271 | for (i = 2; whitespace (sample[i]) && i < sample_len; i++) | |
3272 | ; | |
3273 | ||
28ef6c31 | 3274 | for (start = i; STRINGCHAR(i); i++) |
726f6388 JA |
3275 | ; |
3276 | ||
bb70624e | 3277 | execname = substring ((char *)sample, start, i); |
726f6388 JA |
3278 | size_increment = 1; |
3279 | ||
3280 | /* Now the argument, if any. */ | |
28ef6c31 | 3281 | for (firstarg = (char *)NULL, start = i; WHITECHAR(i); i++) |
726f6388 JA |
3282 | ; |
3283 | ||
3284 | /* If there is more text on the line, then it is an argument for the | |
3285 | interpreter. */ | |
28ef6c31 JA |
3286 | |
3287 | if (STRINGCHAR(i)) | |
726f6388 | 3288 | { |
28ef6c31 | 3289 | for (start = i; STRINGCHAR(i); i++) |
726f6388 | 3290 | ; |
bb70624e | 3291 | firstarg = substring ((char *)sample, start, i); |
726f6388 JA |
3292 | size_increment = 2; |
3293 | } | |
3294 | ||
3295 | larry = array_len (args) + size_increment; | |
3296 | ||
3297 | args = (char **)xrealloc ((char *)args, (1 + larry) * sizeof (char *)); | |
3298 | ||
3299 | for (i = larry - 1; i; i--) | |
3300 | args[i] = args[i - size_increment]; | |
3301 | ||
3302 | args[0] = execname; | |
3303 | if (firstarg) | |
3304 | { | |
3305 | args[1] = firstarg; | |
3306 | args[2] = command; | |
3307 | } | |
3308 | else | |
3309 | args[1] = command; | |
3310 | ||
3311 | args[larry] = (char *)NULL; | |
3312 | ||
3313 | return (shell_execve (execname, args, env)); | |
3314 | } | |
28ef6c31 JA |
3315 | #undef STRINGCHAR |
3316 | #undef WHITECHAR | |
3317 | ||
ccc6cda3 JA |
3318 | #endif /* !HAVE_HASH_BANG_EXEC */ |
3319 | ||
d166f048 JA |
3320 | static void |
3321 | initialize_subshell () | |
3322 | { | |
3323 | #if defined (ALIAS) | |
3324 | /* Forget about any aliases that we knew of. We are in a subshell. */ | |
3325 | delete_all_aliases (); | |
3326 | #endif /* ALIAS */ | |
3327 | ||
3328 | #if defined (HISTORY) | |
3329 | /* Forget about the history lines we have read. This is a non-interactive | |
3330 | subshell. */ | |
3331 | history_lines_this_session = 0; | |
3332 | #endif | |
3333 | ||
3334 | #if defined (JOB_CONTROL) | |
3335 | /* Forget about the way job control was working. We are in a subshell. */ | |
3336 | without_job_control (); | |
3337 | set_sigchld_handler (); | |
3338 | #endif /* JOB_CONTROL */ | |
3339 | ||
3340 | /* Reset the values of the shell flags and options. */ | |
3341 | reset_shell_flags (); | |
3342 | reset_shell_options (); | |
3343 | reset_shopt_options (); | |
3344 | ||
28ef6c31 JA |
3345 | /* Zero out builtin_env, since this could be a shell script run from a |
3346 | sourced file with a temporary environment supplied to the `source/.' | |
3347 | builtin. Such variables are not supposed to be exported (empirical | |
3348 | testing with sh and ksh). */ | |
3349 | builtin_env = 0; | |
3350 | ||
3351 | clear_unwind_protect_list (0); | |
3352 | ||
3353 | /* We're no longer inside a shell function. */ | |
3354 | variable_context = return_catch_flag = 0; | |
3355 | ||
d166f048 JA |
3356 | /* If we're not interactive, close the file descriptor from which we're |
3357 | reading the current shell script. */ | |
cce855bc JA |
3358 | if (interactive_shell == 0) |
3359 | unset_bash_input (1); | |
d166f048 JA |
3360 | } |
3361 | ||
ccc6cda3 JA |
3362 | #if defined (HAVE_SETOSTYPE) && defined (_POSIX_SOURCE) |
3363 | # define SETOSTYPE(x) __setostype(x) | |
3364 | #else | |
3365 | # define SETOSTYPE(x) | |
3366 | #endif | |
726f6388 | 3367 | |
28ef6c31 JA |
3368 | #define READ_SAMPLE_BUF(file, buf, len) \ |
3369 | do \ | |
3370 | { \ | |
3371 | fd = open(file, O_RDONLY); \ | |
3372 | if (fd >= 0) \ | |
3373 | { \ | |
3374 | len = read (fd, (char *)buf, 80); \ | |
3375 | close (fd); \ | |
3376 | } \ | |
3377 | else \ | |
3378 | len = -1; \ | |
3379 | } \ | |
3380 | while (0) | |
3381 | ||
726f6388 JA |
3382 | /* Call execve (), handling interpreting shell scripts, and handling |
3383 | exec failures. */ | |
3384 | int | |
3385 | shell_execve (command, args, env) | |
3386 | char *command; | |
3387 | char **args, **env; | |
3388 | { | |
ccc6cda3 JA |
3389 | struct stat finfo; |
3390 | int larray, i, fd; | |
28ef6c31 JA |
3391 | unsigned char sample[80]; |
3392 | int sample_len; | |
ccc6cda3 JA |
3393 | |
3394 | SETOSTYPE (0); /* Some systems use for USG/POSIX semantics */ | |
726f6388 | 3395 | execve (command, args, env); |
28ef6c31 | 3396 | i = errno; /* error from execve() */ |
ccc6cda3 | 3397 | SETOSTYPE (1); |
726f6388 JA |
3398 | |
3399 | /* If we get to this point, then start checking out the file. | |
3400 | Maybe it is something we can hack ourselves. */ | |
28ef6c31 | 3401 | if (i != ENOEXEC) |
ccc6cda3 | 3402 | { |
ccc6cda3 JA |
3403 | if ((stat (command, &finfo) == 0) && (S_ISDIR (finfo.st_mode))) |
3404 | internal_error ("%s: is a directory", command); | |
3405 | else | |
3406 | { | |
28ef6c31 JA |
3407 | #if defined (HAVE_HASH_BANG_EXEC) |
3408 | READ_SAMPLE_BUF (command, sample, sample_len); | |
3409 | if (sample_len > 2 && sample[0] == '#' && sample[1] == '!') | |
3410 | { | |
3411 | errno = i; | |
3412 | sys_error ("%s: bad interpreter", command); | |
3413 | return (EX_NOEXEC); | |
3414 | } | |
3415 | #endif | |
ccc6cda3 | 3416 | errno = i; |
726f6388 | 3417 | file_error (command); |
ccc6cda3 | 3418 | } |
d166f048 | 3419 | return ((i == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); /* XXX Posix.2 says that exit status is 126 */ |
ccc6cda3 | 3420 | } |
726f6388 | 3421 | |
ccc6cda3 JA |
3422 | /* This file is executable. |
3423 | If it begins with #!, then help out people with losing operating | |
3424 | systems. Otherwise, check to see if it is a binary file by seeing | |
28ef6c31 JA |
3425 | if the contents of the first line (or up to 80 characters) are in the |
3426 | ASCII set. If it's a text file, execute the contents as shell commands, | |
3427 | otherwise return 126 (EX_BINARY_FILE). */ | |
3428 | READ_SAMPLE_BUF (command, sample, sample_len); | |
ccc6cda3 | 3429 | |
28ef6c31 JA |
3430 | if (sample_len == 0) |
3431 | return (EXECUTION_SUCCESS); | |
ccc6cda3 | 3432 | |
28ef6c31 JA |
3433 | /* Is this supposed to be an executable script? |
3434 | If so, the format of the line is "#! interpreter [argument]". | |
3435 | A single argument is allowed. The BSD kernel restricts | |
3436 | the length of the entire line to 32 characters (32 bytes | |
3437 | being the size of the BSD exec header), but we allow 80 | |
3438 | characters. */ | |
3439 | if (sample_len > 0) | |
3440 | { | |
ccc6cda3 | 3441 | #if !defined (HAVE_HASH_BANG_EXEC) |
28ef6c31 JA |
3442 | if (sample_len > 2 && sample[0] == '#' && sample[1] == '!') |
3443 | return (execute_shell_script (sample, sample_len, command, args, env)); | |
3444 | else | |
ccc6cda3 | 3445 | #endif |
28ef6c31 JA |
3446 | if (check_binary_file (sample, sample_len)) |
3447 | { | |
3448 | internal_error ("%s: cannot execute binary file", command); | |
3449 | return (EX_BINARY_FILE); | |
726f6388 | 3450 | } |
ccc6cda3 JA |
3451 | } |
3452 | ||
28ef6c31 JA |
3453 | /* We have committed to attempting to execute the contents of this file |
3454 | as shell commands. */ | |
3455 | ||
d166f048 | 3456 | initialize_subshell (); |
726f6388 | 3457 | |
ccc6cda3 | 3458 | set_sigint_handler (); |
726f6388 | 3459 | |
ccc6cda3 | 3460 | /* Insert the name of this shell into the argument list. */ |
d166f048 | 3461 | larray = array_len (args) + 1; |
ccc6cda3 | 3462 | args = (char **)xrealloc ((char *)args, (1 + larray) * sizeof (char *)); |
726f6388 | 3463 | |
ccc6cda3 JA |
3464 | for (i = larray - 1; i; i--) |
3465 | args[i] = args[i - 1]; | |
726f6388 | 3466 | |
ccc6cda3 JA |
3467 | args[0] = shell_name; |
3468 | args[1] = command; | |
3469 | args[larray] = (char *)NULL; | |
726f6388 | 3470 | |
ccc6cda3 JA |
3471 | if (args[0][0] == '-') |
3472 | args[0]++; | |
726f6388 | 3473 | |
ccc6cda3 JA |
3474 | #if defined (RESTRICTED_SHELL) |
3475 | if (restricted) | |
3476 | change_flag ('r', FLAG_OFF); | |
3477 | #endif | |
726f6388 | 3478 | |
ccc6cda3 JA |
3479 | if (subshell_argv) |
3480 | { | |
3481 | /* Can't free subshell_argv[0]; that is shell_name. */ | |
3482 | for (i = 1; i < subshell_argc; i++) | |
3483 | free (subshell_argv[i]); | |
3484 | free (subshell_argv); | |
3485 | } | |
3486 | ||
3487 | dispose_command (currently_executing_command); /* XXX */ | |
3488 | currently_executing_command = (COMMAND *)NULL; | |
3489 | ||
3490 | subshell_argc = larray; | |
3491 | subshell_argv = args; | |
3492 | subshell_envp = env; | |
3493 | ||
3494 | unbind_args (); /* remove the positional parameters */ | |
3495 | ||
3496 | longjmp (subshell_top_level, 1); | |
3497 | } | |
3498 | ||
3499 | static int | |
3500 | execute_intern_function (name, function) | |
3501 | WORD_DESC *name; | |
3502 | COMMAND *function; | |
3503 | { | |
3504 | SHELL_VAR *var; | |
3505 | ||
3506 | if (check_identifier (name, posixly_correct) == 0) | |
3507 | { | |
3508 | if (posixly_correct && interactive_shell == 0) | |
3509 | { | |
3510 | last_command_exit_value = EX_USAGE; | |
3511 | jump_to_top_level (EXITPROG); | |
3512 | } | |
3513 | return (EXECUTION_FAILURE); | |
3514 | } | |
3515 | ||
3516 | var = find_function (name->word); | |
28ef6c31 | 3517 | if (var && (readonly_p (var) || noassign_p (var))) |
ccc6cda3 | 3518 | { |
28ef6c31 JA |
3519 | if (readonly_p (var)) |
3520 | internal_error ("%s: readonly function", var->name); | |
ccc6cda3 JA |
3521 | return (EXECUTION_FAILURE); |
3522 | } | |
3523 | ||
3524 | bind_function (name->word, function); | |
3525 | return (EXECUTION_SUCCESS); | |
726f6388 JA |
3526 | } |
3527 | ||
d166f048 | 3528 | #if defined (INCLUDE_UNUSED) |
726f6388 | 3529 | #if defined (PROCESS_SUBSTITUTION) |
726f6388 JA |
3530 | void |
3531 | close_all_files () | |
3532 | { | |
3533 | register int i, fd_table_size; | |
3534 | ||
3535 | fd_table_size = getdtablesize (); | |
3536 | if (fd_table_size > 256) /* clamp to a reasonable value */ | |
3537 | fd_table_size = 256; | |
3538 | ||
3539 | for (i = 3; i < fd_table_size; i++) | |
3540 | close (i); | |
3541 | } | |
3542 | #endif /* PROCESS_SUBSTITUTION */ | |
d166f048 | 3543 | #endif |
726f6388 JA |
3544 | |
3545 | static void | |
3546 | close_pipes (in, out) | |
3547 | int in, out; | |
3548 | { | |
3549 | if (in >= 0) | |
3550 | close (in); | |
3551 | if (out >= 0) | |
3552 | close (out); | |
3553 | } | |
3554 | ||
3555 | /* Redirect input and output to be from and to the specified pipes. | |
3556 | NO_PIPE and REDIRECT_BOTH are handled correctly. */ | |
3557 | static void | |
3558 | do_piping (pipe_in, pipe_out) | |
3559 | int pipe_in, pipe_out; | |
3560 | { | |
3561 | if (pipe_in != NO_PIPE) | |
3562 | { | |
3563 | if (dup2 (pipe_in, 0) < 0) | |
ccc6cda3 | 3564 | sys_error ("cannot duplicate fd %d to fd 0", pipe_in); |
726f6388 | 3565 | if (pipe_in > 0) |
cce855bc | 3566 | close (pipe_in); |
726f6388 JA |
3567 | } |
3568 | if (pipe_out != NO_PIPE) | |
3569 | { | |
3570 | if (pipe_out != REDIRECT_BOTH) | |
3571 | { | |
3572 | if (dup2 (pipe_out, 1) < 0) | |
ccc6cda3 | 3573 | sys_error ("cannot duplicate fd %d to fd 1", pipe_out); |
726f6388 JA |
3574 | if (pipe_out == 0 || pipe_out > 1) |
3575 | close (pipe_out); | |
3576 | } | |
3577 | else | |
ccc6cda3 | 3578 | { |
cce855bc JA |
3579 | if (dup2 (1, 2) < 0) |
3580 | sys_error ("cannot duplicate fd 1 to fd 2"); | |
ccc6cda3 | 3581 | } |
d166f048 | 3582 | } |
726f6388 | 3583 | } |