Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* execute_command.c -- Execute a COMMAND structure. */ |
| 2 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3 | /* Copyright (C) 1987-2005 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 9 | the Free Software Foundation; either version 2, or (at your option) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 19 | Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 20 | #include "config.h" |
| 21 | |
| 22 | #if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 23 | #pragma alloca |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 24 | #endif /* _AIX && RISC6000 && !__GNUC__ */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 25 | |
| 26 | #include <stdio.h> |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 27 | #include "chartypes.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 28 | #include "bashtypes.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 29 | #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 30 | # include <sys/file.h> |
| 31 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 32 | #include "filecntl.h" |
| 33 | #include "posixstat.h" |
| 34 | #include <signal.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 35 | #ifndef _MINIX |
| 36 | # include <sys/param.h> |
| 37 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 38 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | #if defined (HAVE_UNISTD_H) |
| 40 | # include <unistd.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 43 | #include "posixtime.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 44 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 45 | #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 53 | #include <errno.h> |
| 54 | |
| 55 | #if !defined (errno) |
| 56 | extern int errno; |
| 57 | #endif |
| 58 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | #include "bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 60 | #include "bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 61 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 62 | #include "memalloc.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 63 | #include "shell.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 64 | #include <y.tab.h> /* use <...> so we pick it up from the build directory */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 65 | #include "flags.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 66 | #include "builtins.h" |
| 67 | #include "hashlib.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 68 | #include "jobs.h" |
| 69 | #include "execute_cmd.h" |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 70 | #include "findcmd.h" |
| 71 | #include "redir.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 72 | #include "trap.h" |
| 73 | #include "pathexp.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 74 | #include "hashcmd.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 75 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 76 | #if defined (COND_COMMAND) |
| 77 | # include "test.h" |
| 78 | #endif |
| 79 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 80 | #include "builtins/common.h" |
| 81 | #include "builtins/builtext.h" /* list of builtins */ |
| 82 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 83 | #include <glob/strmatch.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 84 | #include <tilde/tilde.h> |
| 85 | |
| 86 | #if defined (BUFFERED_INPUT) |
| 87 | # include "input.h" |
| 88 | #endif |
| 89 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 90 | #if defined (ALIAS) |
| 91 | # include "alias.h" |
| 92 | #endif |
| 93 | |
| 94 | #if defined (HISTORY) |
| 95 | # include "bashhist.h" |
| 96 | #endif |
| 97 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 98 | extern int posixly_correct; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 99 | extern int breaking, continuing, loop_level; |
| 100 | extern int expand_aliases; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 101 | extern int parse_and_execute_level, running_trap; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 102 | extern int command_string_index, line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 103 | extern int dot_found_in_search; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 104 | extern int already_making_children; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 105 | extern int tempenv_assign_error; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 106 | extern char *the_printed_command, *shell_name; |
| 107 | extern pid_t last_command_subst_pid; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 108 | extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 109 | extern char **subshell_argv, **subshell_envp; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 110 | extern int subshell_argc; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 111 | #if 0 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 112 | extern char *glob_argv_flags; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 113 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 114 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 115 | extern int close __P((int)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 116 | |
| 117 | /* Static functions defined and used in this file. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 123 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 133 | #if defined (SELECT_COMMAND) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 137 | static char *select_query __P((WORD_LIST *, int, char *, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 138 | static int execute_select_command __P((SELECT_COM *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 139 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 140 | #if defined (DPAREN_ARITHMETIC) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 141 | static int execute_arith_command __P((ARITH_COM *)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 142 | #endif |
| 143 | #if defined (COND_COMMAND) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 144 | static int execute_cond_node __P((COND_COM *)); |
| 145 | static int execute_cond_command __P((COND_COM *)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 146 | #endif |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 147 | #if defined (COMMAND_TIMING) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 *)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 153 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 154 | #if defined (ARITH_FOR_COMMAND) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 155 | static intmax_t eval_arith_for_expr __P((WORD_LIST *, int *)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 156 | static int execute_arith_for_command __P((ARITH_FOR_COM *)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 157 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 *)); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 163 | static int execute_null_command __P((REDIRECT *, int, int, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 179 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 190 | /* The line number that the currently executing function starts on. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 191 | static int function_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 192 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 196 | |
| 197 | /* The name of the command that is currently being executed. |
| 198 | `test' needs this, for example. */ |
| 199 | char *this_command_name; |
| 200 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 206 | static COMMAND *currently_executing_command; |
| 207 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 208 | struct stat SB; /* used for debugging */ |
| 209 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 210 | static int special_builtin_failed; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 211 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 217 | /* For catching RETURN in a function. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 218 | int return_catch_flag; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 219 | int return_catch_value; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 220 | procenv_t return_catch; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 221 | |
| 222 | /* The value returned by the last synchronous command. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 223 | int last_command_exit_value; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 224 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 240 | int subshell_environment; |
| 241 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 242 | /* Count of nested subshells, like SHLVL. Available via $BASH_SUBSHELL */ |
| 243 | int subshell_level = 0; |
| 244 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 245 | /* Currently-executing shell function. */ |
| 246 | SHELL_VAR *this_shell_function; |
| 247 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 248 | /* If non-zero, matches in case and [[ ... ]] are case-insensitive */ |
| 249 | int match_ignore_case = 0; |
| 250 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 251 | struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL; |
| 252 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 253 | #define FD_BITMAP_DEFAULT_SIZE 32 |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 254 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 260 | int size; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 270 | ret->bitmap = (char *)xmalloc (size); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 271 | memset (ret->bitmap, '\0', size); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 303 | /* Return the line number of the currently executing command. */ |
| 304 | int |
| 305 | executing_line_number () |
| 306 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 307 | if (executing && showing_function_line == 0 && |
| 308 | (variable_context == 0 || interactive_shell == 0) && |
| 309 | currently_executing_command) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 310 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 311 | #if defined (COND_COMMAND) |
| 312 | if (currently_executing_command->type == cm_cond) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 313 | return currently_executing_command->value.Cond->line; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 314 | #endif |
| 315 | #if defined (DPAREN_ARITHMETIC) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 316 | else if (currently_executing_command->type == cm_arith) |
| 317 | return currently_executing_command->value.Arith->line; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 318 | #endif |
| 319 | #if defined (ARITH_FOR_COMMAND) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 320 | else if (currently_executing_command->type == cm_arith_for) |
| 321 | return currently_executing_command->value.ArithFor->line; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 322 | #endif |
| 323 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 324 | return line_number; |
| 325 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 326 | else |
| 327 | return line_number; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 337 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 372 | #if defined (ARITH_FOR_COMMAND) |
| 373 | case cm_arith_for: |
| 374 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 375 | #if defined (SELECT_COMMAND) |
| 376 | case cm_select: |
| 377 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 378 | #if defined (DPAREN_ARITHMETIC) |
| 379 | case cm_arith: |
| 380 | #endif |
| 381 | #if defined (COND_COMMAND) |
| 382 | case cm_cond: |
| 383 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 384 | case cm_case: |
| 385 | case cm_while: |
| 386 | case cm_until: |
| 387 | case cm_if: |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 388 | case cm_for: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 389 | case cm_group: |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 390 | case cm_function_def: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 404 | do_redirections (list, RX_ACTIVE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 405 | dispose_redirects (list); |
| 406 | } |
| 407 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 408 | #if 0 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 409 | /* Function to unwind_protect the redirections for functions and builtins. */ |
| 410 | static void |
| 411 | cleanup_func_redirects (list) |
| 412 | REDIRECT *list; |
| 413 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 414 | do_redirections (list, RX_ACTIVE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 415 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 416 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 417 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 418 | void |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 433 | sigset_t *set; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 434 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 435 | return (sigprocmask (SIG_SETMASK, set, (sigset_t *)NULL)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 436 | } |
| 437 | #endif /* JOB_CONTROL */ |
| 438 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 439 | #ifdef DEBUG |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 449 | fprintf (stderr, "pid %ld open files:", (long)getpid ()); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 457 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 458 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 471 | internal_error (_("cannot redirect standard input from /dev/null: %s"), strerror (errno)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 474 | #define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 488 | int |
| 489 | execute_command_internal (command, asynchronous, pipe_in, pipe_out, |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 490 | fds_to_close) |
| 491 | COMMAND *command; |
| 492 | int asynchronous; |
| 493 | int pipe_in, pipe_out; |
| 494 | struct fd_bitmap *fds_to_close; |
| 495 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 496 | int exec_result, invert, ignore_return, was_error_trap; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 497 | REDIRECT *my_undo_list, *exec_undo_list; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 498 | volatile int last_pid; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 499 | volatile int save_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 500 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 501 | if (command == 0 || breaking || continuing || read_but_dont_execute) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 502 | return (EXECUTION_SUCCESS); |
| 503 | |
| 504 | run_pending_traps (); |
| 505 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 506 | #if 0 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 507 | if (running_trap == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 508 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 509 | currently_executing_command = command; |
| 510 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 511 | invert = (command->flags & CMD_INVERT_RETURN) != 0; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 519 | exec_result = EXECUTION_SUCCESS; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 526 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 527 | if (command->type == cm_subshell || |
| 528 | (command->flags & (CMD_WANT_SUBSHELL|CMD_FORCE_SUBSHELL)) || |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 539 | exit (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close)); |
| 540 | /* NOTREACHED */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 551 | last command in the pipeline, then we wait for the subshell |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 558 | if (asynchronous == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 559 | { |
| 560 | last_command_exit_value = wait_for (paren_pid); |
| 561 | |
| 562 | /* If we have to, invert the return value. */ |
| 563 | if (invert) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 564 | exec_result = ((last_command_exit_value == EXECUTION_SUCCESS) |
| 565 | ? EXECUTION_FAILURE |
| 566 | : EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 567 | else |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 568 | exec_result = last_command_exit_value; |
| 569 | |
| 570 | return (last_command_exit_value = exec_result); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 571 | } |
| 572 | else |
| 573 | { |
| 574 | DESCRIBE_PID (paren_pid); |
| 575 | |
| 576 | run_pending_traps (); |
| 577 | |
| 578 | return (EXECUTION_SUCCESS); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 594 | #if 0 |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 595 | if (running_trap == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 596 | #endif |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 606 | /* Handle WHILE FOR CASE etc. with redirections. (Also '&' input |
| 607 | redirection.) */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 608 | if (do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE) != 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 649 | case cm_simple: |
| 650 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 651 | save_line_number = line_number; |
| 652 | /* We can't rely on variables retaining their values across a |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 655 | #if defined (RECYCLES_PIDS) |
| 656 | last_made_pid = NO_PID; |
| 657 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 658 | last_pid = last_made_pid; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 659 | was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 660 | |
| 661 | if (ignore_return && command->value.Simple) |
| 662 | command->value.Simple->flags |= CMD_IGNORE_RETURN; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 663 | if (command->flags & CMD_STDIN_REDIR) |
| 664 | command->value.Simple->flags |= CMD_STDIN_REDIR; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 665 | |
| 666 | line_number = command->value.Simple->line; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 667 | exec_result = |
| 668 | execute_simple_command (command->value.Simple, pipe_in, pipe_out, |
| 669 | asynchronous, fds_to_close); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 670 | line_number = save_line_number; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 && |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 688 | last_made_pid != last_pid) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 707 | exec_result = wait_for (last_made_pid); |
| 708 | } |
| 709 | } |
| 710 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 717 | if (ignore_return == 0 && invert == 0 && |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 718 | ((posixly_correct && interactive == 0 && special_builtin_failed) || |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 719 | (exit_immediately_on_error && (exec_result != EXECUTION_SUCCESS)))) |
| 720 | { |
| 721 | last_command_exit_value = exec_result; |
| 722 | run_pending_traps (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 723 | jump_to_top_level (ERREXIT); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | break; |
| 727 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 818 | case cm_connection: |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 819 | exec_result = execute_connection (command, asynchronous, |
| 820 | pipe_in, pipe_out, fds_to_close); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 821 | break; |
| 822 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 835 | save_line_number = line_number; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 836 | exec_result = execute_cond_command (command->value.Cond); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 837 | line_number = save_line_number; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 838 | break; |
| 839 | #endif |
| 840 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 841 | case cm_function_def: |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 842 | exec_result = execute_intern_function (command->value.Function_def->name, |
| 843 | command->value.Function_def->command); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 844 | break; |
| 845 | |
| 846 | default: |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 847 | command_error ("execute_command", CMDERR_BADTYPE, command->type, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | if (my_undo_list) |
| 851 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 852 | do_redirections (my_undo_list, RX_ACTIVE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 864 | exec_result = (exec_result == EXECUTION_SUCCESS) |
| 865 | ? EXECUTION_FAILURE |
| 866 | : EXECUTION_SUCCESS; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 867 | |
| 868 | last_command_exit_value = exec_result; |
| 869 | run_pending_traps (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 870 | #if 0 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 871 | if (running_trap == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 872 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 873 | currently_executing_command = (COMMAND *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 874 | return (last_command_exit_value); |
| 875 | } |
| 876 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 877 | #if defined (COMMAND_TIMING) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 878 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 879 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 *)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 883 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 895 | time_t sec; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 896 | int sec_fraction; |
| 897 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 898 | time_t min; |
| 899 | char abuf[INT_STRLEN_BOUND(time_t) + 1]; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 900 | int ind, aind; |
| 901 | |
| 902 | ind = 0; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 903 | abuf[sizeof(abuf) - 1] = '\0'; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 910 | aind = sizeof(abuf) - 2; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 911 | do |
| 912 | abuf[aind--] = (min % 10) + '0'; |
| 913 | while (min /= 10); |
| 914 | aind++; |
| 915 | while (abuf[aind]) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 916 | buf[ind++] = abuf[aind++]; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 917 | buf[ind++] = 'm'; |
| 918 | } |
| 919 | |
| 920 | /* Now add the seconds. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 921 | aind = sizeof (abuf) - 2; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 951 | %[prec][l][RUS] |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 958 | R number of seconds of `real' time |
| 959 | U number of seconds of `user' time |
| 960 | S number of seconds of `system' time |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 970 | time_t rs; |
| 971 | int rsf; |
| 972 | time_t us; |
| 973 | int usf; |
| 974 | time_t ss; |
| 975 | int ssf, cpu; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 976 | { |
| 977 | int prec, lng, len; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 978 | char *str, *s, ts[INT_STRLEN_BOUND (time_t) + sizeof ("mSS.FFFF")]; |
| 979 | time_t sum; |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 980 | int sum_frac; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 981 | int sindex, ssize; |
| 982 | |
| 983 | len = strlen (format); |
| 984 | ssize = (len + 64) - (len % 64); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 985 | str = (char *)xmalloc (ssize); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 986 | sindex = 0; |
| 987 | |
| 988 | for (s = format; *s; s++) |
| 989 | { |
| 990 | if (*s != '%' || s[1] == '\0') |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 991 | { |
| 992 | RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64); |
| 993 | str[sindex++] = *s; |
| 994 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 995 | else if (s[1] == '%') |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 996 | { |
| 997 | s++; |
| 998 | RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64); |
| 999 | str[sindex++] = *s; |
| 1000 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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++; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1018 | if (DIGIT (*s)) /* `precision' */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1036 | internal_error (_("TIMEFORMAT: `%c': invalid format character"), *s); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1059 | int rv, posix_time, old_flags; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1060 | time_t rs, us, ss; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1068 | # if defined (HAVE_STRUCT_TIMEZONE) |
| 1069 | struct timezone dtz; /* posix doesn't define this */ |
| 1070 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1080 | # if defined (HAVE_STRUCT_TIMEZONE) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1081 | gettimeofday (&before, &dtz); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1082 | # else |
| 1083 | gettimeofday (&before, (void *)NULL); |
| 1084 | # endif /* !HAVE_STRUCT_TIMEZONE */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1095 | old_flags = command->flags; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1096 | command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX); |
| 1097 | rv = execute_command_internal (command, asynchronous, pipe_in, pipe_out, fds_to_close); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1098 | command->flags = old_flags; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1099 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1100 | rs = us = ss = 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1101 | rsf = usf = ssf = cpu = 0; |
| 1102 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1103 | #if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1104 | # if defined (HAVE_STRUCT_TIMEZONE) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1105 | gettimeofday (&after, &dtz); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1106 | # else |
| 1107 | gettimeofday (&after, (void *)NULL); |
| 1108 | # endif /* !HAVE_STRUCT_TIMEZONE */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1137 | cpu = (real == 0) ? 0 : ((user + sys) * 10000) / real; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1138 | |
| 1139 | # else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1140 | rs = us = ss = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1157 | /* Execute a command that's supposed to be in a subshell. This must be |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1167 | int user_subshell, return_code, function_value, should_redir_stdin, invert; |
| 1168 | int ois; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1169 | COMMAND *tcom; |
| 1170 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1171 | USE_VAR(user_subshell); |
| 1172 | USE_VAR(invert); |
| 1173 | USE_VAR(tcom); |
| 1174 | USE_VAR(asynchronous); |
| 1175 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1176 | subshell_level++; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1177 | should_redir_stdin = (asynchronous && (command->flags & CMD_STDIN_REDIR) && |
| 1178 | pipe_in == NO_PIPE && |
| 1179 | stdin_redirects (command->redirects) == 0); |
| 1180 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1181 | invert = (command->flags & CMD_INVERT_RETURN) != 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1182 | user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1183 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1209 | ois = interactive_shell; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1210 | interactive_shell = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1267 | if (do_redirections (command->redirects, RX_ACTIVE) != 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1268 | exit (invert ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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. |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1288 | If we're timing the command or inverting its return value, however, |
| 1289 | we cannot do this optimization. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1290 | if (user_subshell && (tcom->type == cm_simple || tcom->type == cm_subshell) && |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1291 | ((tcom->flags & CMD_TIME_PIPELINE) == 0) && |
| 1292 | ((tcom->flags & CMD_INVERT_RETURN) == 0)) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1293 | { |
| 1294 | tcom->flags |= CMD_NO_FORK; |
| 1295 | if (tcom->type == cm_simple) |
| 1296 | tcom->value.Simple->flags |= CMD_NO_FORK; |
| 1297 | } |
| 1298 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1299 | invert = (tcom->flags & CMD_INVERT_RETURN) != 0; |
| 1300 | tcom->flags &= ~CMD_INVERT_RETURN; |
| 1301 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1327 | subshell_level--; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1328 | return (return_code); |
| 1329 | /* NOTREACHED */ |
| 1330 | } |
| 1331 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 1362 | UNBLOCK_CHILD (oset); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1398 | want all these file descriptors to be closed when |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1410 | add_unwind_protect (restore_signal_mask, &oset); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1451 | REDIRECT *rp; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1467 | if (ignore_return) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1468 | tc->flags |= CMD_IGNORE_RETURN; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1469 | tc->flags |= CMD_AMPERSAND; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1470 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1475 | #if defined (JOB_CONTROL) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1476 | if ((subshell_environment || !job_control) && !stdin_redir) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1477 | #else |
| 1478 | if (!stdin_redir) |
| 1479 | #endif /* JOB_CONTROL */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1480 | tc->flags |= CMD_STDIN_REDIR; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1481 | |
| 1482 | exec_result = execute_command_internal (tc, 1, pipe_in, pipe_out, fds_to_close); |
| 1483 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1484 | if (tc->flags & CMD_STDIN_REDIR) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1485 | tc->flags &= ~CMD_STDIN_REDIR; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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: |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1556 | command_error ("execute_connection", CMDERR_BADCONN, command->value.Connection->connector, 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1557 | jump_to_top_level (DISCARD); |
| 1558 | exec_result = EXECUTION_FAILURE; |
| 1559 | } |
| 1560 | |
| 1561 | return exec_result; |
| 1562 | } |
| 1563 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1564 | #define REAP() \ |
| 1565 | do \ |
| 1566 | { \ |
| 1567 | if (!interactive_shell) \ |
| 1568 | reap_dead_jobs (); \ |
| 1569 | } \ |
| 1570 | while (0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* Execute a FOR command. The syntax is: FOR word_desc IN word_list; |
| 1573 | DO command; DONE */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1574 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1575 | execute_for_command (for_command) |
| 1576 | FOR_COM *for_command; |
| 1577 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1578 | register WORD_LIST *releaser, *list; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1579 | SHELL_VAR *v; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1580 | char *identifier; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1581 | int retval, save_line_number; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1582 | #if 0 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1583 | SHELL_VAR *old_value = (SHELL_VAR *)NULL; /* Remember the old value of x. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1584 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1585 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1586 | save_line_number = line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1587 | if (check_identifier (for_command->name, 1) == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1588 | { |
| 1589 | if (posixly_correct && interactive_shell == 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1590 | { |
| 1591 | last_command_exit_value = EX_USAGE; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1592 | jump_to_top_level (ERREXIT); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1593 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1594 | return (EXECUTION_FAILURE); |
| 1595 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1605 | #if 0 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1612 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1613 | |
| 1614 | if (for_command->flags & CMD_IGNORE_RETURN) |
| 1615 | for_command->action->flags |= CMD_IGNORE_RETURN; |
| 1616 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1617 | for (retval = EXECUTION_SUCCESS; list; list = list->next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1618 | { |
| 1619 | QUIT; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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))) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1646 | this_command_name = (char *)NULL; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 1647 | v = bind_variable (identifier, list->word->word, 0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1648 | if (readonly_p (v) || noassign_p (v)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1649 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1650 | line_number = save_line_number; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1651 | if (readonly_p (v) && interactive_shell == 0 && posixly_correct) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1652 | { |
| 1653 | last_command_exit_value = EXECUTION_FAILURE; |
| 1654 | jump_to_top_level (FORCE_EOF); |
| 1655 | } |
| 1656 | else |
| 1657 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1658 | dispose_words (releaser); |
| 1659 | discard_unwind_frame ("for"); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1660 | loop_level--; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1661 | return (EXECUTION_FAILURE); |
| 1662 | } |
| 1663 | } |
| 1664 | retval = execute_command (for_command->action); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1665 | REAP (); |
| 1666 | QUIT; |
| 1667 | |
| 1668 | if (breaking) |
| 1669 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1670 | breaking--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | } |
| 1673 | |
| 1674 | if (continuing) |
| 1675 | { |
| 1676 | continuing--; |
| 1677 | if (continuing) |
| 1678 | break; |
| 1679 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | loop_level--; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1683 | line_number = save_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1684 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1685 | #if 0 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1686 | if (lexical_scoping) |
| 1687 | { |
| 1688 | if (!old_value) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1689 | unbind_variable (identifier); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1690 | else |
| 1691 | { |
| 1692 | SHELL_VAR *new_value; |
| 1693 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 1694 | new_value = bind_variable (identifier, value_cell(old_value), 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1695 | new_value->attributes = old_value->attributes; |
| 1696 | dispose_variable (old_value); |
| 1697 | } |
| 1698 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1699 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1700 | |
| 1701 | dispose_words (releaser); |
| 1702 | discard_unwind_frame ("for"); |
| 1703 | return (retval); |
| 1704 | } |
| 1705 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1722 | static intmax_t |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1723 | eval_arith_for_expr (l, okp) |
| 1724 | WORD_LIST *l; |
| 1725 | int *okp; |
| 1726 | { |
| 1727 | WORD_LIST *new; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1728 | intmax_t expresult; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1729 | int r; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1730 | |
| 1731 | new = expand_words_no_vars (l); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1732 | if (new) |
| 1733 | { |
| 1734 | if (echo_command_at_execute) |
| 1735 | xtrace_print_arith_cmd (new); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1736 | this_command_name = "(("; /* )) for expression error messages */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1737 | |
| 1738 | command_string_index = 0; |
| 1739 | print_arith_command (new); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1759 | expresult = evalexp (new->word->word, okp); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1760 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1761 | dispose_words (new); |
| 1762 | } |
| 1763 | else |
| 1764 | { |
| 1765 | expresult = 0; |
| 1766 | if (okp) |
| 1767 | *okp = 1; |
| 1768 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1769 | return (expresult); |
| 1770 | } |
| 1771 | |
| 1772 | static int |
| 1773 | execute_arith_for_command (arith_for_command) |
| 1774 | ARITH_FOR_COM *arith_for_command; |
| 1775 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1776 | intmax_t expresult; |
| 1777 | int expok, body_status, arith_lineno, save_lineno; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1778 | |
| 1779 | body_status = EXECUTION_SUCCESS; |
| 1780 | loop_level++; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1781 | save_lineno = line_number; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1791 | line_number = arith_lineno = arith_for_command->line; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1792 | if (variable_context && interactive_shell) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1793 | line_number -= function_line_number; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1794 | |
| 1795 | /* Evaluate the initialization expression. */ |
| 1796 | expresult = eval_arith_for_expr (arith_for_command->init, &expok); |
| 1797 | if (expok == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1798 | { |
| 1799 | line_number = save_lineno; |
| 1800 | return (EXECUTION_FAILURE); |
| 1801 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1802 | |
| 1803 | while (1) |
| 1804 | { |
| 1805 | /* Evaluate the test expression. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1806 | line_number = arith_lineno; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1807 | expresult = eval_arith_for_expr (arith_for_command->test, &expok); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1808 | line_number = save_lineno; |
| 1809 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1810 | if (expok == 0) |
| 1811 | { |
| 1812 | body_status = EXECUTION_FAILURE; |
| 1813 | break; |
| 1814 | } |
| 1815 | REAP (); |
| 1816 | if (expresult == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1817 | break; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1839 | line_number = arith_lineno; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1840 | expresult = eval_arith_for_expr (arith_for_command->step, &expok); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1841 | line_number = save_lineno; |
| 1842 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1843 | if (expok == 0) |
| 1844 | { |
| 1845 | body_status = EXECUTION_FAILURE; |
| 1846 | break; |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | loop_level--; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1851 | line_number = save_lineno; |
| 1852 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1853 | return (body_status); |
| 1854 | } |
| 1855 | #endif |
| 1856 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1882 | for (i = ind, l = list; l && --i; l = l->next) |
| 1883 | ; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1921 | cols = max_elem_len ? COLS / max_elem_len : 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1960 | static char * |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1961 | select_query (list, list_len, prompt, print_menu) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1962 | WORD_LIST *list; |
| 1963 | int list_len; |
| 1964 | char *prompt; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1965 | int print_menu; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1966 | { |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1967 | int max_elem_len, indices_len, len; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1968 | intmax_t reply; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1991 | max_elem_len = len; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1992 | } |
| 1993 | indices_len = NUMBER_LEN (list_len); |
| 1994 | max_elem_len += indices_len + RP_SPACE_LEN + 2; |
| 1995 | |
| 1996 | while (1) |
| 1997 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1998 | if (print_menu) |
| 1999 | print_select_list (list, list_len, max_elem_len, indices_len); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2000 | fprintf (stderr, "%s", prompt); |
| 2001 | fflush (stderr); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2011 | { |
| 2012 | print_menu = 1; |
| 2013 | continue; |
| 2014 | } |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 2015 | if (legal_number (repl_string, &reply) == 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2016 | return ""; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2017 | if (reply < 1 || reply > list_len) |
| 2018 | return ""; |
| 2019 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2020 | for (l = list; l && --reply; l = l->next) |
| 2021 | ; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2030 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2031 | execute_select_command (select_command) |
| 2032 | SELECT_COM *select_command; |
| 2033 | { |
| 2034 | WORD_LIST *releaser, *list; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2035 | SHELL_VAR *v; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2036 | char *identifier, *ps3_prompt, *selection; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2037 | int retval, list_len, show_menu, save_line_number; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2038 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2039 | if (check_identifier (select_command->name, 1) == 0) |
| 2040 | return (EXECUTION_FAILURE); |
| 2041 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2076 | line_number = save_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2077 | return (EXECUTION_SUCCESS); |
| 2078 | } |
| 2079 | |
| 2080 | begin_unwind_frame ("select"); |
| 2081 | add_unwind_protect (dispose_words, releaser); |
| 2082 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2083 | if (select_command->flags & CMD_IGNORE_RETURN) |
| 2084 | select_command->action->flags |= CMD_IGNORE_RETURN; |
| 2085 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2086 | retval = EXECUTION_SUCCESS; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2087 | show_menu = 1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2088 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2089 | while (1) |
| 2090 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2091 | line_number = select_command->line; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2092 | ps3_prompt = get_string_value ("PS3"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2093 | if (ps3_prompt == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2094 | ps3_prompt = "#? "; |
| 2095 | |
| 2096 | QUIT; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2097 | selection = select_query (list, list_len, ps3_prompt, show_menu); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2098 | QUIT; |
| 2099 | if (selection == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2106 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2107 | v = bind_variable (identifier, selection, 0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2108 | if (readonly_p (v) || noassign_p (v)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2109 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2110 | if (readonly_p (v) && interactive_shell == 0 && posixly_correct) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2111 | { |
| 2112 | last_command_exit_value = EXECUTION_FAILURE; |
| 2113 | jump_to_top_level (FORCE_EOF); |
| 2114 | } |
| 2115 | else |
| 2116 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2117 | dispose_words (releaser); |
| 2118 | discard_unwind_frame ("select"); |
| 2119 | loop_level--; |
| 2120 | line_number = save_line_number; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2121 | return (EXECUTION_FAILURE); |
| 2122 | } |
| 2123 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2124 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2125 | retval = execute_command (select_command->action); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2126 | |
| 2127 | REAP (); |
| 2128 | QUIT; |
| 2129 | |
| 2130 | if (breaking) |
| 2131 | { |
| 2132 | breaking--; |
| 2133 | break; |
| 2134 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2135 | |
| 2136 | if (continuing) |
| 2137 | { |
| 2138 | continuing--; |
| 2139 | if (continuing) |
| 2140 | break; |
| 2141 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | loop_level--; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2152 | line_number = save_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2153 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2154 | dispose_words (releaser); |
| 2155 | discard_unwind_frame ("select"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2164 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2165 | execute_case_command (case_command) |
| 2166 | CASE_COM *case_command; |
| 2167 | { |
| 2168 | register WORD_LIST *list; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2169 | WORD_LIST *wlist, *es; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2170 | PATTERN_LIST *clauses; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2171 | char *word, *pattern; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2183 | if (signal_in_progress (DEBUG_TRAP == 0) && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0))) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2199 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2200 | wlist = expand_word_unsplit (case_command->word, 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2201 | word = wlist ? string_list (wlist) : savestring (""); |
| 2202 | dispose_words (wlist); |
| 2203 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2204 | retval = EXECUTION_SUCCESS; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2205 | ignore_return = case_command->flags & CMD_IGNORE_RETURN; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2206 | |
| 2207 | begin_unwind_frame ("case"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2208 | add_unwind_protect ((Function *)xfree, word); |
| 2209 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2210 | #define EXIT_CASE() goto exit_case_command |
| 2211 | |
| 2212 | for (clauses = case_command->clauses; clauses; clauses = clauses->next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2213 | { |
| 2214 | QUIT; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2215 | for (list = clauses->patterns; list; list = list->next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2216 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2217 | es = expand_word_leave_quoted (list->word, 0); |
| 2218 | |
| 2219 | if (es && es->word && es->word->word && *(es->word->word)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2220 | pattern = quote_string_for_globbing (es->word->word, QGLOB_CVTNULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2221 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2222 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2223 | pattern = (char *)xmalloc (1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2224 | pattern[0] = '\0'; |
| 2225 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2226 | |
| 2227 | /* Since the pattern does not undergo quote removal (as per |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2228 | Posix.2, section 3.9.4.3), the strmatch () call must be able |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2229 | to recognize backslashes as escape characters. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2230 | match = strmatch (pattern, word, FNMATCH_EXTFLAG|FNMATCH_IGNCASE) != FNM_NOMATCH; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2231 | free (pattern); |
| 2232 | |
| 2233 | dispose_words (es); |
| 2234 | |
| 2235 | if (match) |
| 2236 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2237 | if (clauses->action && ignore_return) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2238 | clauses->action->flags |= CMD_IGNORE_RETURN; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2239 | retval = execute_command (clauses->action); |
| 2240 | EXIT_CASE (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2243 | QUIT; |
| 2244 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2247 | exit_case_command: |
| 2248 | free (word); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2249 | discard_unwind_frame ("case"); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2250 | line_number = save_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2260 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2268 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2280 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2304 | if (type == CMD_WHILE && return_value != EXECUTION_SUCCESS) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2305 | { |
| 2306 | if (breaking) |
| 2307 | breaking--; |
| 2308 | break; |
| 2309 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2310 | if (type == CMD_UNTIL && return_value == EXECUTION_SUCCESS) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2311 | { |
| 2312 | if (breaking) |
| 2313 | breaking--; |
| 2314 | break; |
| 2315 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2342 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2343 | execute_if_command (if_command) |
| 2344 | IF_COM *if_command; |
| 2345 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2346 | int return_value, save_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2347 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2348 | save_line_number = line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2349 | if_command->test->flags |= CMD_IGNORE_RETURN; |
| 2350 | return_value = execute_command (if_command->test); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2351 | line_number = save_line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2352 | |
| 2353 | if (return_value == EXECUTION_SUCCESS) |
| 2354 | { |
| 2355 | QUIT; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2356 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2357 | if (if_command->true_case && (if_command->flags & CMD_IGNORE_RETURN)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2358 | if_command->true_case->flags |= CMD_IGNORE_RETURN; |
| 2359 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2373 | #if defined (DPAREN_ARITHMETIC) |
| 2374 | static int |
| 2375 | execute_arith_command (arith_command) |
| 2376 | ARITH_COM *arith_command; |
| 2377 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2378 | int expok, save_line_number, retval; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2379 | intmax_t expresult; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2380 | WORD_LIST *new; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2381 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2382 | expresult = 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2383 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2384 | save_line_number = line_number; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2385 | this_command_name = "(("; /* )) */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2386 | line_number = arith_command->line; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2387 | /* If we're in a function, update the line number information. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2388 | if (variable_context && interactive_shell) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2389 | line_number -= function_line_number; |
| 2390 | |
| 2391 | command_string_index = 0; |
| 2392 | print_arith_command (arith_command->exp); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2399 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2413 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2414 | new = expand_words_no_vars (arith_command->exp); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2432 | |
| 2433 | if (expok == 0) |
| 2434 | return (EXECUTION_FAILURE); |
| 2435 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2436 | return (expresult == 0 ? EXECUTION_FAILURE : EXECUTION_SUCCESS); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2448 | int result, invert, patmatch, rmatch, mflags; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2449 | char *arg1, *arg2; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 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')); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2483 | #if defined (COND_REGEXP) |
| 2484 | rmatch = (cond->op->word[0] == '=' && cond->op->word[1] == '~' && |
| 2485 | cond->op->word[2] == '\0'); |
| 2486 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2519 | if (arg1 != nullstr) |
| 2520 | free (arg1); |
| 2521 | if (arg2 != nullstr) |
| 2522 | free (arg2); |
| 2523 | } |
| 2524 | else |
| 2525 | { |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2526 | command_error ("execute_cond_node", CMDERR_BADTYPE, cond->type, 0); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2541 | int retval, save_line_number; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2542 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2543 | retval = EXECUTION_SUCCESS; |
| 2544 | save_line_number = line_number; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2545 | |
| 2546 | this_command_name = "[["; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2547 | line_number = cond_command->line; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2548 | /* If we're in a function, update the line number information. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2549 | if (variable_context && interactive_shell) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2550 | line_number -= function_line_number; |
| 2551 | |
| 2552 | command_string_index = 0; |
| 2553 | print_cond_command (cond_command); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2560 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2561 | /* Run the debug trap before each conditional command, but do it after we |
| 2562 | update the line number information. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2573 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2574 | #if 0 |
| 2575 | debug_print_cond_command (cond_command); |
| 2576 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2577 | |
| 2578 | last_command_exit_value = retval = execute_cond_node (cond_command); |
| 2579 | line_number = save_line_number; |
| 2580 | return (retval); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2581 | } |
| 2582 | #endif /* COND_COMMAND */ |
| 2583 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2584 | static void |
| 2585 | bind_lastarg (arg) |
| 2586 | char *arg; |
| 2587 | { |
| 2588 | SHELL_VAR *var; |
| 2589 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2590 | if (arg == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2591 | arg = ""; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2592 | var = bind_variable ("_", arg, 0); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2593 | VUNSETATTR (var, att_exported); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2600 | execute_null_command (redirects, pipe_in, pipe_out, async) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2601 | REDIRECT *redirects; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2602 | int pipe_in, pipe_out, async; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2603 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2604 | int r; |
| 2605 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2606 | if (pipe_in != NO_PIPE || pipe_out != NO_PIPE || async) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2610 | if (make_child ((char *)NULL, async) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2611 | { |
| 2612 | /* Cancel traps, in trap.c. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2613 | restore_original_signals (); /* XXX */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2614 | |
| 2615 | do_piping (pipe_in, pipe_out); |
| 2616 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2617 | subshell_environment = SUBSHELL_ASYNC; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2618 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2619 | if (do_redirections (redirects, RX_ACTIVE) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2630 | return (EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2647 | return (EXECUTION_FAILURE); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2648 | else if (last_command_subst_pid != NO_PID) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2649 | return (last_command_exit_value); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2650 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2651 | return (EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2652 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2667 | b = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2668 | |
| 2669 | for (w = words; w; w = w->next) |
| 2670 | if (w->word->flags & W_ASSIGNMENT) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2677 | else if (b && (b->flags & ASSIGNMENT_BUILTIN)) |
| 2678 | words->word->flags |= W_ASSNBLTIN; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2679 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2680 | w->word->flags |= (W_NOSPLIT|W_NOGLOB|W_TILDEEXP|W_ASSIGNARG); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2681 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 2695 | int first_word_quoted, result, builtin_is_special, already_forked, dofork; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2696 | pid_t old_last_async_pid; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2697 | sh_builtin_func_t *builtin; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2698 | SHELL_VAR *func; |
| 2699 | |
| 2700 | result = EXECUTION_SUCCESS; |
| 2701 | special_builtin_failed = builtin_is_special = 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2702 | command_line = (char *)0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2703 | |
| 2704 | /* If we're in a function, update the line number information. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2705 | if (variable_context && interactive_shell) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2706 | line_number -= function_line_number; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2707 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2708 | /* Remember what this command line looks like at invocation. */ |
| 2709 | command_string_index = 0; |
| 2710 | print_simple_command (simple_command); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2711 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2712 | if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0))) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2713 | { |
| 2714 | FREE (the_printed_command_except_trap); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2715 | the_printed_command_except_trap = the_printed_command ? savestring (the_printed_command) : (char *)0; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2728 | first_word_quoted = |
| 2729 | simple_command->words ? (simple_command->words->word->flags & W_QUOTED): 0; |
| 2730 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2731 | last_command_subst_pid = NO_PID; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2732 | old_last_async_pid = last_asynchronous_pid; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2733 | |
Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 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) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2751 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2759 | { |
| 2760 | already_forked = 1; |
| 2761 | simple_command->flags |= CMD_NO_FORK; |
| 2762 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2768 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2775 | do_piping (pipe_in, pipe_out); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2776 | pipe_in = pipe_out = NO_PIPE; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2777 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2778 | last_asynchronous_pid = old_last_async_pid; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2809 | this_command_name = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2810 | result = execute_null_command (simple_command->redirects, |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2811 | pipe_in, pipe_out, |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 2812 | already_forked ? 0 : async); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | lastarg = (char *)NULL; |
| 2824 | |
| 2825 | begin_unwind_frame ("simple-command"); |
| 2826 | |
| 2827 | if (echo_command_at_execute) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2828 | xtrace_print_word_list (words, 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2829 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2830 | builtin = (sh_builtin_func_t *)NULL; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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? */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2868 | if (words->word->word[0] == '%' && already_forked == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2880 | if (job_control && already_forked == 0 && async == 0 && |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2889 | int job, jflags, started_status; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2890 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2900 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2905 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2906 | started_status = start_job (job, 1); |
| 2907 | return ((started_status < 0) ? EXECUTION_FAILURE : started_status); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2930 | if (already_forked) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2931 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2932 | /* reset_terminating_signals (); */ /* XXX */ |
| 2933 | /* Cancel traps, in trap.c. */ |
| 2934 | restore_original_signals (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2935 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2936 | if (async) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2937 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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 (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2943 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2944 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2945 | subshell_level++; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 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); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2950 | subshell_level--; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2961 | result = builtin_status (result); |
| 2962 | if (builtin_is_special) |
| 2963 | special_builtin_failed = 1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2976 | result = EXECUTION_FAILURE; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2979 | set_pipestatus_from_exit (result); |
| 2980 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2981 | goto return_result; |
| 2982 | } |
| 2983 | } |
| 2984 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2985 | if (command_line == 0) |
| 2986 | command_line = savestring (the_printed_command); |
| 2987 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2988 | execute_disk_command (words, simple_command->redirects, command_line, |
| 2989 | pipe_in, pipe_out, async, fds_to_close, |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2990 | simple_command->flags); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2991 | |
| 2992 | return_result: |
| 2993 | bind_lastarg (lastarg); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2994 | FREE (command_line); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2995 | dispose_words (words); |
| 2996 | discard_unwind_frame ("simple-command"); |
| 2997 | this_command_name = (char *)NULL; /* points to freed memory now */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2998 | return (result); |
| 2999 | } |
| 3000 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3027 | static int |
| 3028 | execute_builtin (builtin, words, flags, subshell) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3029 | sh_builtin_func_t *builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3030 | WORD_LIST *words; |
| 3031 | int flags, subshell; |
| 3032 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3033 | int old_e_flag, result, eval_unwind; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3034 | int isbltinenv; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3035 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3036 | old_e_flag = exit_immediately_on_error; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3048 | eval_unwind = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3049 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3050 | else |
| 3051 | eval_unwind = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3052 | |
| 3053 | /* The temporary environment for a builtin is supposed to apply to |
| 3054 | all commands executed by that builtin. Currently, this is a |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3055 | problem only with the `unset', `source' and `eval' builtins. */ |
| 3056 | |
| 3057 | isbltinenv = (builtin == source_builtin || builtin == eval_builtin || builtin == unset_builtin); |
| 3058 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3059 | if (isbltinenv) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3060 | { |
| 3061 | if (subshell == 0) |
| 3062 | begin_unwind_frame ("builtin_env"); |
| 3063 | |
| 3064 | if (temporary_env) |
| 3065 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3066 | push_scope (VC_BLTNENV, temporary_env); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3067 | if (subshell == 0) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3068 | add_unwind_protect (pop_scope, (flags & CMD_COMMAND_BUILTIN) ? 0 : "1"); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3069 | temporary_env = (HASH_TABLE *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3070 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | result = ((*builtin) (words->next)); |
| 3084 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3090 | if (subshell == 0 && isbltinenv) |
| 3091 | run_unwind_frame ("builtin_env"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3092 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3093 | if (eval_unwind) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3094 | { |
| 3095 | exit_immediately_on_error += old_e_flag; |
| 3096 | discard_unwind_frame ("eval_builtin"); |
| 3097 | } |
| 3098 | |
| 3099 | return (result); |
| 3100 | } |
| 3101 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3102 | static int |
| 3103 | execute_function (var, words, flags, fds_to_close, async, subshell) |
| 3104 | SHELL_VAR *var; |
| 3105 | WORD_LIST *words; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3106 | int flags; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3107 | struct fd_bitmap *fds_to_close; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3108 | int async, subshell; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3109 | { |
| 3110 | int return_val, result; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3111 | COMMAND *tc, *fc, *save_current; |
| 3112 | char *debug_trap, *error_trap, *return_trap; |
| 3113 | #if defined (ARRAY_VARS) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3114 | SHELL_VAR *funcname_v, *nfv, *bash_source_v, *bash_lineno_v; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3120 | |
| 3121 | USE_VAR(fc); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3122 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3129 | tc = (COMMAND *)copy_command (function_cell (var)); |
| 3130 | if (tc && (flags & CMD_IGNORE_RETURN)) |
| 3131 | tc->flags |= CMD_IGNORE_RETURN; |
| 3132 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3133 | if (subshell == 0) |
| 3134 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3135 | begin_unwind_frame ("function_calling"); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3136 | push_context (var->name, subshell, temporary_env); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3137 | add_unwind_protect (pop_context, (char *)NULL); |
| 3138 | unwind_protect_int (line_number); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3139 | unwind_protect_int (return_catch_flag); |
| 3140 | unwind_protect_jmp_buf (return_catch); |
| 3141 | add_unwind_protect (dispose_command, (char *)tc); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3142 | unwind_protect_pointer (this_shell_function); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3143 | unwind_protect_int (loop_level); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3144 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3145 | else |
| 3146 | push_context (var->name, subshell, temporary_env); /* don't unwind-protect for subshells */ |
| 3147 | |
| 3148 | temporary_env = (HASH_TABLE *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3149 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3150 | this_shell_function = var; |
| 3151 | make_funcname_visible (1); |
| 3152 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3153 | debug_trap = TRAP_STRING(DEBUG_TRAP); |
| 3154 | error_trap = TRAP_STRING(ERROR_TRAP); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3155 | return_trap = TRAP_STRING(RETURN_TRAP); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3156 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3165 | { |
| 3166 | if (subshell == 0) |
| 3167 | { |
| 3168 | debug_trap = savestring (debug_trap); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3169 | add_unwind_protect (xfree, debug_trap); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3170 | add_unwind_protect (set_debug_trap, debug_trap); |
| 3171 | } |
| 3172 | restore_default_signal (DEBUG_TRAP); |
| 3173 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3174 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3175 | /* error_trace_mode != 0 means that functions inherit the ERR trap. */ |
| 3176 | if (error_trap && error_trace_mode == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3187 | /* Shell functions inherit the RETURN trap if function tracing is on |
| 3188 | globally or on individually for this function. */ |
| 3189 | #if 0 |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3190 | if (return_trap && ((trace_p (var) == 0) && function_trace_mode == 0)) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3191 | #else |
| 3192 | if (return_trap && (signal_in_progress (DEBUG_TRAP) || ((trace_p (var) == 0) && function_trace_mode == 0))) |
| 3193 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3217 | /* The temporary environment for a function is supposed to apply to |
| 3218 | all commands executed within the function body. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3219 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3220 | remember_args (words->next, 1); |
| 3221 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3222 | /* Update BASH_ARGV and BASH_ARGC */ |
| 3223 | if (debugging_mode) |
| 3224 | push_args (words->next); |
| 3225 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3226 | /* Number of the line on which the function body starts. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3227 | line_number = function_line_number = tc->line; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3228 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3229 | #if defined (JOB_CONTROL) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3230 | if (subshell) |
| 3231 | stop_pipeline (async, (COMMAND *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3232 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3233 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3234 | fc = tc; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3235 | |
| 3236 | return_catch_flag++; |
| 3237 | return_val = setjmp (return_catch); |
| 3238 | |
| 3239 | if (return_val) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3247 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3270 | |
| 3271 | save_current = currently_executing_command; |
| 3272 | run_return_trap (); |
| 3273 | currently_executing_command = save_current; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3274 | #endif |
| 3275 | showing_function_line = 0; |
| 3276 | } |
| 3277 | |
| 3278 | /* Restore BASH_ARGC and BASH_ARGV */ |
| 3279 | if (debugging_mode) |
| 3280 | pop_args (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3281 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3282 | if (subshell == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3283 | run_unwind_frame ("function_calling"); |
| 3284 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3285 | funcnest--; |
| 3286 | #if defined (ARRAY_VARS) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3287 | /* These two variables cannot be unset, and cannot be affected by the |
| 3288 | function. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3289 | array_pop (bash_source_a); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3290 | array_pop (bash_lineno_a); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3297 | #endif |
| 3298 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3299 | if (variable_context == 0 || this_shell_function == 0) |
| 3300 | make_funcname_visible (0); |
| 3301 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3302 | return (result); |
| 3303 | } |
| 3304 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3339 | sh_builtin_func_t *builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3340 | SHELL_VAR *var; |
| 3341 | int pipe_in, pipe_out, async; |
| 3342 | struct fd_bitmap *fds_to_close; |
| 3343 | int flags; |
| 3344 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3345 | int result, r; |
| 3346 | #if defined (JOB_CONTROL) |
| 3347 | int jobs_hack; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3348 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3349 | jobs_hack = (builtin == jobs_builtin) && |
| 3350 | ((subshell_environment & SUBSHELL_ASYNC) == 0 || pipe_out != NO_PIPE); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3351 | #endif |
| 3352 | |
| 3353 | /* A subshell is neither a login shell nor interactive. */ |
| 3354 | login_shell = interactive = 0; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3355 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3356 | subshell_environment = SUBSHELL_ASYNC; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3357 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3358 | maybe_make_export_env (); /* XXX - is this needed? */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3366 | if (jobs_hack) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3376 | if (fds_to_close) |
| 3377 | close_fd_bitmap (fds_to_close); |
| 3378 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3379 | do_piping (pipe_in, pipe_out); |
| 3380 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3381 | if (do_redirections (redirects, RX_ACTIVE) != 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3382 | exit (EXECUTION_FAILURE); |
| 3383 | |
| 3384 | if (builtin) |
| 3385 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3395 | { |
| 3396 | r = execute_builtin (builtin, words, flags, 1); |
| 3397 | if (r == EX_USAGE) |
| 3398 | r = EX_BADUSAGE; |
| 3399 | exit (r); |
| 3400 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3401 | } |
| 3402 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3403 | exit (execute_function (var, words, flags, fds_to_close, async, 1)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3418 | sh_builtin_func_t *builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3419 | SHELL_VAR *var; |
| 3420 | REDIRECT *redirects; |
| 3421 | struct fd_bitmap *fds_to_close; |
| 3422 | int flags; |
| 3423 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3424 | int result; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3425 | REDIRECT *saved_undo_list; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3426 | sh_builtin_func_t *saved_this_shell_builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3427 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3428 | if (do_redirections (redirects, RX_ACTIVE|RX_UNDOABLE) != 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3429 | { |
| 3430 | cleanup_redirects (redirection_undo_list); |
| 3431 | redirection_undo_list = (REDIRECT *)NULL; |
| 3432 | dispose_exec_redirects (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3433 | return (EX_REDIRFAIL); /* was EXECUTION_FAILURE */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3436 | saved_this_shell_builtin = this_shell_builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3452 | add_unwind_protect (cleanup_redirects, (char *)saved_undo_list); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3462 | /* We do this before undoing the effects of any redirections. */ |
| 3463 | if (ferror (stdout)) |
| 3464 | clearerr (stdout); |
| 3465 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3489 | cleanup_redirects (redirection_undo_list); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3490 | redirection_undo_list = (REDIRECT *)NULL; |
| 3491 | } |
| 3492 | |
| 3493 | return (result); |
| 3494 | } |
| 3495 | |
| 3496 | void |
| 3497 | setup_async_signals () |
| 3498 | { |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3499 | #if defined (__BEOS__) |
| 3500 | set_signal_handler (SIGHUP, SIG_IGN); /* they want csh-like behavior */ |
| 3501 | #endif |
| 3502 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3529 | this gnarly hair, for no good reason. |
| 3530 | |
| 3531 | NOTE: callers expect this to fork or exit(). */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3532 | static void |
| 3533 | execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3534 | async, fds_to_close, cmdflags) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3540 | int cmdflags; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3541 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3542 | char *pathname, *command, **args; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3543 | int nofork; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3544 | pid_t pid; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3545 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3546 | nofork = (cmdflags & CMD_NO_FORK); /* Don't fork, just exec, if no pipes */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3547 | pathname = words->word->word; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3548 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3549 | #if defined (RESTRICTED_SHELL) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3550 | command = (char *)NULL; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3551 | if (restricted && xstrchr (pathname, '/')) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3552 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3553 | internal_error (_("%s: restricted: cannot specify `/' in command names"), |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3554 | pathname); |
| 3555 | last_command_exit_value = EXECUTION_FAILURE; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3563 | } |
| 3564 | #endif /* RESTRICTED_SHELL */ |
| 3565 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3566 | command = search_for_command (pathname); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3567 | |
| 3568 | if (command) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3569 | { |
| 3570 | maybe_make_export_env (); |
| 3571 | put_command_name_into_env (command); |
| 3572 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3573 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3574 | /* We have to make the child before we check for the non-existence |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3587 | #if 0 |
| 3588 | /* This has been disabled for the time being. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3589 | #if !defined (ARG_MAX) || ARG_MAX >= 10240 |
| 3590 | if (posixly_correct == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3591 | put_gnu_argv_flags_into_env ((long)getpid (), glob_argv_flags); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3592 | #endif |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3593 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3594 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3595 | /* Cancel traps, in trap.c. */ |
| 3596 | restore_original_signals (); |
| 3597 | |
| 3598 | /* restore_original_signals may have undone the work done |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3599 | by make_child to ensure that SIGINT and SIGQUIT are ignored |
| 3600 | in asynchronous children. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3601 | if (async) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3609 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3618 | do_piping (pipe_in, pipe_out); |
| 3619 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3620 | old_interactive = interactive; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3621 | if (async) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3622 | interactive = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3623 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3624 | subshell_environment = SUBSHELL_FORK; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3625 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3626 | if (redirects && (do_redirections (redirects, RX_ACTIVE) != 0)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3639 | if (command == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3640 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3641 | internal_error (_("%s: command not found"), pathname); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3642 | exit (EX_NOTFOUND); /* Posix.2 says the exit status is 127 */ |
| 3643 | } |
| 3644 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3648 | args = strvec_from_word_list (words, 0, 0, (int *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3649 | exit (shell_execve (command, args, export_env)); |
| 3650 | } |
| 3651 | else |
| 3652 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3653 | parent_return: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3703 | #if !defined (HAVE_HASH_BANG_EXEC) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3713 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3714 | static int |
| 3715 | execute_shell_script (sample, sample_len, command, args, env) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3716 | char *sample; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3717 | int sample_len; |
| 3718 | char *command; |
| 3719 | char **args, **env; |
| 3720 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3721 | char *execname, *firstarg; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3722 | int i, start, size_increment, larry; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3723 | |
| 3724 | /* Find the name of the interpreter to exec. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3725 | execname = getinterp (sample, sample_len, &i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3726 | size_increment = 1; |
| 3727 | |
| 3728 | /* Now the argument, if any. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3729 | for (firstarg = (char *)NULL, start = i; WHITECHAR(i); i++) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3730 | ; |
| 3731 | |
| 3732 | /* If there is more text on the line, then it is an argument for the |
| 3733 | interpreter. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3734 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3735 | if (STRINGCHAR(i)) |
| 3736 | { |
| 3737 | for (start = i; STRINGCHAR(i); i++) |
| 3738 | ; |
| 3739 | firstarg = substring ((char *)sample, start, i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3740 | size_increment = 2; |
| 3741 | } |
| 3742 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3743 | larry = strvec_len (args) + size_increment; |
| 3744 | args = strvec_resize (args, larry + 1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3762 | #undef STRINGCHAR |
| 3763 | #undef WHITECHAR |
| 3764 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3765 | #endif /* !HAVE_HASH_BANG_EXEC */ |
| 3766 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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 (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3785 | init_job_stats (); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 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; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3806 | /* If we're not interactive, close the file descriptor from which we're |
| 3807 | reading the current shell script. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 3808 | if (interactive_shell == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3809 | unset_bash_input (0); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3812 | #if defined (HAVE_SETOSTYPE) && defined (_POSIX_SOURCE) |
| 3813 | # define SETOSTYPE(x) __setostype(x) |
| 3814 | #else |
| 3815 | # define SETOSTYPE(x) |
| 3816 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3817 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3818 | #define READ_SAMPLE_BUF(file, buf, len) \ |
| 3819 | do \ |
| 3820 | { \ |
| 3821 | fd = open(file, O_RDONLY); \ |
| 3822 | if (fd >= 0) \ |
| 3823 | { \ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3824 | len = read (fd, buf, 80); \ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3825 | close (fd); \ |
| 3826 | } \ |
| 3827 | else \ |
| 3828 | len = -1; \ |
| 3829 | } \ |
| 3830 | while (0) |
| 3831 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3839 | struct stat finfo; |
| 3840 | int larray, i, fd; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3841 | char sample[80]; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3842 | int sample_len; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3843 | |
| 3844 | SETOSTYPE (0); /* Some systems use for USG/POSIX semantics */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3845 | execve (command, args, env); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3846 | i = errno; /* error from execve() */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3847 | SETOSTYPE (1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3848 | |
| 3849 | /* If we get to this point, then start checking out the file. |
| 3850 | Maybe it is something we can hack ourselves. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3851 | if (i != ENOEXEC) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3852 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3853 | if ((stat (command, &finfo) == 0) && (S_ISDIR (finfo.st_mode))) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3854 | internal_error (_("%s: is a directory"), command); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3855 | else if (executable_file (command) == 0) |
| 3856 | { |
| 3857 | errno = i; |
| 3858 | file_error (command); |
| 3859 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3866 | else |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3867 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3868 | /* The file has the execute bits set, but the kernel refuses to |
| 3869 | run it for some reason. See why. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3874 | char *interp; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3875 | int ilen; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3876 | |
| 3877 | interp = getinterp (sample, sample_len, (int *)NULL); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 3878 | ilen = strlen (interp); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3879 | errno = i; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame^] | 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 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3887 | sys_error (_("%s: %s: bad interpreter"), command, interp ? interp : ""); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3888 | FREE (interp); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3889 | return (EX_NOEXEC); |
| 3890 | } |
| 3891 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3892 | errno = i; |
| 3893 | file_error (command); |
| 3894 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3895 | return ((i == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); /* XXX Posix.2 says that exit status is 126 */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3916 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3917 | #if !defined (HAVE_HASH_BANG_EXEC) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3918 | if (sample_len > 2 && sample[0] == '#' && sample[1] == '!') |
| 3919 | return (execute_shell_script (sample, sample_len, command, args, env)); |
| 3920 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3921 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3922 | if (check_binary_file (sample, sample_len)) |
| 3923 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3924 | internal_error (_("%s: cannot execute binary file"), command); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3925 | return (EX_BINARY_FILE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3926 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3929 | /* We have committed to attempting to execute the contents of this file |
| 3930 | as shell commands. */ |
| 3931 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 3932 | initialize_subshell (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3933 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3934 | set_sigint_handler (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3935 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3936 | /* Insert the name of this shell into the argument list. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3937 | larray = strvec_len (args) + 1; |
| 3938 | args = strvec_resize (args, larray + 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3973 | /*NOTREACHED*/ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3988 | jump_to_top_level (ERREXIT); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3989 | } |
| 3990 | return (EXECUTION_FAILURE); |
| 3991 | } |
| 3992 | |
| 3993 | var = find_function (name->word); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3994 | if (var && (readonly_p (var) || noassign_p (var))) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3995 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3996 | if (readonly_p (var)) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3997 | internal_error (_("%s: readonly function"), var->name); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3998 | return (EXECUTION_FAILURE); |
| 3999 | } |
| 4000 | |
| 4001 | bind_function (name->word, function); |
| 4002 | return (EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4005 | #if defined (INCLUDE_UNUSED) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4006 | #if defined (PROCESS_SUBSTITUTION) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4014 | fd_table_size = 256; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4015 | |
| 4016 | for (i = 3; i < fd_table_size; i++) |
| 4017 | close (i); |
| 4018 | } |
| 4019 | #endif /* PROCESS_SUBSTITUTION */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4020 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4048 | dup_error (pipe_in, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4049 | if (pipe_in > 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4050 | close (pipe_in); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4051 | } |
| 4052 | if (pipe_out != NO_PIPE) |
| 4053 | { |
| 4054 | if (pipe_out != REDIRECT_BOTH) |
| 4055 | { |
| 4056 | if (dup2 (pipe_out, 1) < 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4057 | dup_error (pipe_out, 1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4058 | if (pipe_out == 0 || pipe_out > 1) |
| 4059 | close (pipe_out); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4060 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4061 | else |
| 4062 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4063 | if (dup2 (1, 2) < 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4064 | dup_error (1, 2); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4065 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4066 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4067 | } |