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