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