blob: 9cebaef34fd48f36a1a4ed768fa19815ef5005fe [file] [log] [blame]
Jari Aalto06285672006-10-10 14:15:34 +00001/* execute_cmd.c -- Execute a COMMAND structure. */
Jari Aalto726f6381996-08-26 18:22:31 +00002
Chet Rameyac50fba2014-02-26 09:36:43 -05003/* Copyright (C) 1987-2013 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 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 Aalto726f6381996-08-26 18:22:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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 Aalto726f6381996-08-26 18:22:31 +000016
17 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000018 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
20
Jari Aaltoccc6cda1996-12-23 17:02:34 +000021#include "config.h"
22
23#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
Jari Aalto726f6381996-08-26 18:22:31 +000024 #pragma alloca
Jari Aaltoccc6cda1996-12-23 17:02:34 +000025#endif /* _AIX && RISC6000 && !__GNUC__ */
Jari Aalto726f6381996-08-26 18:22:31 +000026
27#include <stdio.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000028#include "chartypes.h"
Jari Aalto726f6381996-08-26 18:22:31 +000029#include "bashtypes.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000030#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000031# include <sys/file.h>
32#endif
Jari Aalto726f6381996-08-26 18:22:31 +000033#include "filecntl.h"
34#include "posixstat.h"
35#include <signal.h>
Chet Rameyac50fba2014-02-26 09:36:43 -050036#if defined (HAVE_SYS_PARAM_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000037# include <sys/param.h>
38#endif
Jari Aalto726f6381996-08-26 18:22:31 +000039
Jari Aaltoccc6cda1996-12-23 17:02:34 +000040#if defined (HAVE_UNISTD_H)
41# include <unistd.h>
Jari Aalto726f6381996-08-26 18:22:31 +000042#endif
43
Jari Aaltobb706242000-03-17 21:46:59 +000044#include "posixtime.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000045
Jari Aaltocce855b1998-04-17 19:52:44 +000046#if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000047# 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 Aalto726f6381996-08-26 18:22:31 +000054#include <errno.h>
55
56#if !defined (errno)
57extern int errno;
58#endif
59
Chet Ramey00018032011-11-21 20:51:19 -050060#define NEED_FPURGE_DECL
61
Jari Aaltoccc6cda1996-12-23 17:02:34 +000062#include "bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000063#include "bashintl.h"
Jari Aalto726f6381996-08-26 18:22:31 +000064
Jari Aaltoccc6cda1996-12-23 17:02:34 +000065#include "memalloc.h"
Jari Aalto726f6381996-08-26 18:22:31 +000066#include "shell.h"
Jari Aaltod166f041997-06-05 14:59:13 +000067#include <y.tab.h> /* use <...> so we pick it up from the build directory */
Jari Aalto726f6381996-08-26 18:22:31 +000068#include "flags.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000069#include "builtins.h"
70#include "hashlib.h"
Jari Aalto726f6381996-08-26 18:22:31 +000071#include "jobs.h"
72#include "execute_cmd.h"
Jari Aaltocce855b1998-04-17 19:52:44 +000073#include "findcmd.h"
74#include "redir.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000075#include "trap.h"
76#include "pathexp.h"
Jari Aaltod166f041997-06-05 14:59:13 +000077#include "hashcmd.h"
Jari Aalto726f6381996-08-26 18:22:31 +000078
Jari Aaltocce855b1998-04-17 19:52:44 +000079#if defined (COND_COMMAND)
80# include "test.h"
81#endif
82
Jari Aalto726f6381996-08-26 18:22:31 +000083#include "builtins/common.h"
84#include "builtins/builtext.h" /* list of builtins */
85
Jari Aaltof73dda02001-11-13 17:56:06 +000086#include <glob/strmatch.h>
Jari Aalto726f6381996-08-26 18:22:31 +000087#include <tilde/tilde.h>
88
89#if defined (BUFFERED_INPUT)
90# include "input.h"
91#endif
92
Jari Aaltoccc6cda1996-12-23 17:02:34 +000093#if defined (ALIAS)
94# include "alias.h"
95#endif
96
97#if defined (HISTORY)
98# include "bashhist.h"
99#endif
100
Chet Ramey495aee42011-11-22 19:11:26 -0500101extern int dollar_dollar_pid;
Jari Aalto726f6381996-08-26 18:22:31 +0000102extern int posixly_correct;
Jari Aaltof73dda02001-11-13 17:56:06 +0000103extern int expand_aliases;
Jari Aalto31859422009-01-12 13:36:28 +0000104extern int autocd;
105extern int breaking, continuing, loop_level;
Chet Ramey00018032011-11-21 20:51:19 -0500106extern int parse_and_execute_level, running_trap, sourcelevel;
Jari Aaltof73dda02001-11-13 17:56:06 +0000107extern int command_string_index, line_number;
Jari Aalto726f6381996-08-26 18:22:31 +0000108extern int dot_found_in_search;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000109extern int already_making_children;
Jari Aaltob80f6442004-07-27 13:29:18 +0000110extern int tempenv_assign_error;
Jari Aalto726f6381996-08-26 18:22:31 +0000111extern char *the_printed_command, *shell_name;
112extern pid_t last_command_subst_pid;
Jari Aaltof73dda02001-11-13 17:56:06 +0000113extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
Jari Aalto726f6381996-08-26 18:22:31 +0000114extern char **subshell_argv, **subshell_envp;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000115extern int subshell_argc;
Chet Ramey495aee42011-11-22 19:11:26 -0500116extern time_t shell_start_time;
Jari Aaltof73dda02001-11-13 17:56:06 +0000117#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000118extern char *glob_argv_flags;
Jari Aaltof73dda02001-11-13 17:56:06 +0000119#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000120
Chet Rameyac50fba2014-02-26 09:36:43 -0500121extern int job_control; /* XXX */
122
Jari Aaltof73dda02001-11-13 17:56:06 +0000123extern int close __P((int));
Jari Aalto726f6381996-08-26 18:22:31 +0000124
125/* Static functions defined and used in this file. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000126static void close_pipes __P((int, int));
127static void do_piping __P((int, int));
128static void bind_lastarg __P((char *));
129static int shell_control_structure __P((enum command_type));
130static void cleanup_redirects __P((REDIRECT *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000131
Jari Aaltof73dda02001-11-13 17:56:06 +0000132#if defined (JOB_CONTROL)
133static int restore_signal_mask __P((sigset_t *));
134#endif
135
136static void async_redirect_stdin __P((void));
137
138static int builtin_status __P((int));
139
140static int execute_for_command __P((FOR_COM *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000141#if defined (SELECT_COMMAND)
Chet Ramey495aee42011-11-22 19:11:26 -0500142static int displen __P((const char *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000143static int print_index_and_element __P((int, int, WORD_LIST *));
144static void indent __P((int, int));
145static void print_select_list __P((WORD_LIST *, int, int, int));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000146static char *select_query __P((WORD_LIST *, int, char *, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000147static int execute_select_command __P((SELECT_COM *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000148#endif
Jari Aaltocce855b1998-04-17 19:52:44 +0000149#if defined (DPAREN_ARITHMETIC)
Jari Aaltof73dda02001-11-13 17:56:06 +0000150static int execute_arith_command __P((ARITH_COM *));
Jari Aaltocce855b1998-04-17 19:52:44 +0000151#endif
152#if defined (COND_COMMAND)
Jari Aaltof73dda02001-11-13 17:56:06 +0000153static int execute_cond_node __P((COND_COM *));
154static int execute_cond_command __P((COND_COM *));
Jari Aaltocce855b1998-04-17 19:52:44 +0000155#endif
Jari Aaltod166f041997-06-05 14:59:13 +0000156#if defined (COMMAND_TIMING)
Jari Aaltof73dda02001-11-13 17:56:06 +0000157static int mkfmt __P((char *, int, int, time_t, int));
158static void print_formatted_time __P((FILE *, char *,
159 time_t, int, time_t, int,
160 time_t, int, int));
161static int time_command __P((COMMAND *, int, int, int, struct fd_bitmap *));
Jari Aaltod166f041997-06-05 14:59:13 +0000162#endif
Jari Aaltobb706242000-03-17 21:46:59 +0000163#if defined (ARITH_FOR_COMMAND)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000164static intmax_t eval_arith_for_expr __P((WORD_LIST *, int *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000165static int execute_arith_for_command __P((ARITH_FOR_COM *));
Jari Aaltobb706242000-03-17 21:46:59 +0000166#endif
Jari Aaltof73dda02001-11-13 17:56:06 +0000167static int execute_case_command __P((CASE_COM *));
168static int execute_while_command __P((WHILE_COM *));
169static int execute_until_command __P((WHILE_COM *));
170static int execute_while_or_until __P((WHILE_COM *, int));
171static int execute_if_command __P((IF_COM *));
Jari Aalto95732b42005-12-07 14:08:12 +0000172static int execute_null_command __P((REDIRECT *, int, int, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000173static void fix_assignment_words __P((WORD_LIST *));
174static int execute_simple_command __P((SIMPLE_COM *, int, int, int, struct fd_bitmap *));
175static int execute_builtin __P((sh_builtin_func_t *, WORD_LIST *, int, int));
176static int execute_function __P((SHELL_VAR *, WORD_LIST *, int, struct fd_bitmap *, int, int));
177static int execute_builtin_or_function __P((WORD_LIST *, sh_builtin_func_t *,
178 SHELL_VAR *,
179 REDIRECT *, struct fd_bitmap *, int));
180static void execute_subshell_builtin_or_function __P((WORD_LIST *, REDIRECT *,
181 sh_builtin_func_t *,
182 SHELL_VAR *,
183 int, int, int,
184 struct fd_bitmap *,
185 int));
Chet Ramey495aee42011-11-22 19:11:26 -0500186static int execute_disk_command __P((WORD_LIST *, REDIRECT *, char *,
Jari Aaltof73dda02001-11-13 17:56:06 +0000187 int, int, int, struct fd_bitmap *, int));
Jari Aalto726f6381996-08-26 18:22:31 +0000188
Jari Aaltof73dda02001-11-13 17:56:06 +0000189static char *getinterp __P((char *, int, int *));
190static void initialize_subshell __P((void));
191static int execute_in_subshell __P((COMMAND *, int, int, int, struct fd_bitmap *));
Jari Aalto31859422009-01-12 13:36:28 +0000192#if defined (COPROCESS_SUPPORT)
193static int execute_coproc __P((COMMAND *, int, int, struct fd_bitmap *));
194#endif
Jari Aaltof73dda02001-11-13 17:56:06 +0000195
196static int execute_pipeline __P((COMMAND *, int, int, int, struct fd_bitmap *));
197
198static int execute_connection __P((COMMAND *, int, int, int, struct fd_bitmap *));
199
Chet Rameyac50fba2014-02-26 09:36:43 -0500200static int execute_intern_function __P((WORD_DESC *, FUNCTION_DEF *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000201
Jari Aaltod166f041997-06-05 14:59:13 +0000202/* Set to 1 if fd 0 was the subject of redirection to a subshell. Global
203 so that reader_loop can set it to zero before executing a command. */
204int stdin_redir;
Jari Aalto726f6381996-08-26 18:22:31 +0000205
206/* The name of the command that is currently being executed.
207 `test' needs this, for example. */
208char *this_command_name;
209
Jari Aaltob80f6442004-07-27 13:29:18 +0000210/* The printed representation of the currently-executing command (same as
211 the_printed_command), except when a trap is being executed. Useful for
212 a debugger to know where exactly the program is currently executing. */
213char *the_printed_command_except_trap;
214
Jari Aalto726f6381996-08-26 18:22:31 +0000215/* For catching RETURN in a function. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000216int return_catch_flag;
Jari Aalto726f6381996-08-26 18:22:31 +0000217int return_catch_value;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000218procenv_t return_catch;
Jari Aalto726f6381996-08-26 18:22:31 +0000219
220/* The value returned by the last synchronous command. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000221int last_command_exit_value;
Jari Aalto726f6381996-08-26 18:22:31 +0000222
Jari Aaltob80f6442004-07-27 13:29:18 +0000223/* Whether or not the last command (corresponding to last_command_exit_value)
224 was terminated by a signal, and, if so, which one. */
225int last_command_exit_signal;
226
Chet Rameyac50fba2014-02-26 09:36:43 -0500227/* Are we currently ignoring the -e option for the duration of a builtin's
228 execution? */
229int builtin_ignoring_errexit = 0;
230
Jari Aalto726f6381996-08-26 18:22:31 +0000231/* The list of redirections to perform which will undo the redirections
232 that I made in the shell. */
233REDIRECT *redirection_undo_list = (REDIRECT *)NULL;
234
235/* The list of redirections to perform which will undo the internal
236 redirections performed by the `exec' builtin. These are redirections
237 that must be undone even when exec discards redirection_undo_list. */
238REDIRECT *exec_redirection_undo_list = (REDIRECT *)NULL;
239
Jari Aalto31859422009-01-12 13:36:28 +0000240/* When greater than zero, value is the `level' of builtins we are
241 currently executing (e.g. `eval echo a' would have it set to 2). */
242int executing_builtin = 0;
243
244/* Non-zero if we are executing a command list (a;b;c, etc.) */
245int executing_list = 0;
246
247/* Non-zero if failing commands in a command substitution should not exit the
248 shell even if -e is set. Used to pass the CMD_IGNORE_RETURN flag down to
249 commands run in command substitutions by parse_and_execute. */
250int comsub_ignore_return = 0;
251
Jari Aalto726f6381996-08-26 18:22:31 +0000252/* Non-zero if we have just forked and are currently running in a subshell
253 environment. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000254int subshell_environment;
255
Jari Aaltob80f6442004-07-27 13:29:18 +0000256/* Count of nested subshells, like SHLVL. Available via $BASH_SUBSHELL */
257int subshell_level = 0;
258
Jari Aaltobb706242000-03-17 21:46:59 +0000259/* Currently-executing shell function. */
260SHELL_VAR *this_shell_function;
261
Jari Aalto95732b42005-12-07 14:08:12 +0000262/* If non-zero, matches in case and [[ ... ]] are case-insensitive */
263int match_ignore_case = 0;
264
Chet Ramey495aee42011-11-22 19:11:26 -0500265int executing_command_builtin = 0;
266
Jari Aalto31859422009-01-12 13:36:28 +0000267struct stat SB; /* used for debugging */
268
269static int special_builtin_failed;
270
271static COMMAND *currently_executing_command;
272
273/* The line number that the currently executing function starts on. */
274static int function_line_number;
275
276/* XXX - set to 1 if we're running the DEBUG trap and we want to show the line
277 number containing the function name. Used by executing_line_number to
278 report the correct line number. Kind of a hack. */
279static int showing_function_line;
280
Chet Rameyac50fba2014-02-26 09:36:43 -0500281/* $LINENO ($BASH_LINENO) for use by an ERR trap. Global so parse_and_execute
282 can save and restore it. */
283int line_number_for_err_trap;
Jari Aalto31859422009-01-12 13:36:28 +0000284
Chet Ramey00018032011-11-21 20:51:19 -0500285/* A sort of function nesting level counter */
Chet Ramey495aee42011-11-22 19:11:26 -0500286int funcnest = 0;
Chet Rameyac50fba2014-02-26 09:36:43 -0500287int funcnest_max = 0; /* bash-4.2 */
Chet Ramey495aee42011-11-22 19:11:26 -0500288
289int lastpipe_opt = 0;
Chet Ramey00018032011-11-21 20:51:19 -0500290
Jari Aalto726f6381996-08-26 18:22:31 +0000291struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL;
292
Jari Aaltof73dda02001-11-13 17:56:06 +0000293#define FD_BITMAP_DEFAULT_SIZE 32
Jari Aaltod166f041997-06-05 14:59:13 +0000294
Jari Aalto726f6381996-08-26 18:22:31 +0000295/* Functions to allocate and deallocate the structures used to pass
296 information from the shell to its children about file descriptors
297 to close. */
298struct fd_bitmap *
299new_fd_bitmap (size)
Jari Aaltof73dda02001-11-13 17:56:06 +0000300 int size;
Jari Aalto726f6381996-08-26 18:22:31 +0000301{
302 struct fd_bitmap *ret;
303
304 ret = (struct fd_bitmap *)xmalloc (sizeof (struct fd_bitmap));
305
306 ret->size = size;
307
308 if (size)
309 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000310 ret->bitmap = (char *)xmalloc (size);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000311 memset (ret->bitmap, '\0', size);
Jari Aalto726f6381996-08-26 18:22:31 +0000312 }
313 else
314 ret->bitmap = (char *)NULL;
315 return (ret);
316}
317
318void
319dispose_fd_bitmap (fdbp)
320 struct fd_bitmap *fdbp;
321{
322 FREE (fdbp->bitmap);
323 free (fdbp);
324}
325
326void
327close_fd_bitmap (fdbp)
328 struct fd_bitmap *fdbp;
329{
330 register int i;
331
332 if (fdbp)
333 {
334 for (i = 0; i < fdbp->size; i++)
335 if (fdbp->bitmap[i])
336 {
337 close (i);
338 fdbp->bitmap[i] = 0;
339 }
340 }
341}
342
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000343/* Return the line number of the currently executing command. */
344int
345executing_line_number ()
346{
Jari Aaltob80f6442004-07-27 13:29:18 +0000347 if (executing && showing_function_line == 0 &&
348 (variable_context == 0 || interactive_shell == 0) &&
349 currently_executing_command)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000350 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000351#if defined (COND_COMMAND)
352 if (currently_executing_command->type == cm_cond)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000353 return currently_executing_command->value.Cond->line;
Jari Aaltob80f6442004-07-27 13:29:18 +0000354#endif
355#if defined (DPAREN_ARITHMETIC)
Chet Rameyac50fba2014-02-26 09:36:43 -0500356 if (currently_executing_command->type == cm_arith)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000357 return currently_executing_command->value.Arith->line;
Jari Aaltob80f6442004-07-27 13:29:18 +0000358#endif
359#if defined (ARITH_FOR_COMMAND)
Chet Rameyac50fba2014-02-26 09:36:43 -0500360 if (currently_executing_command->type == cm_arith_for)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000361 return currently_executing_command->value.ArithFor->line;
Jari Aaltob80f6442004-07-27 13:29:18 +0000362#endif
363
Jari Aalto7117c2d2002-07-17 14:10:11 +0000364 return line_number;
365 }
Jari Aaltod166f041997-06-05 14:59:13 +0000366 else
367 return line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000368}
369
Jari Aalto726f6381996-08-26 18:22:31 +0000370/* Execute the command passed in COMMAND. COMMAND is exactly what
371 read_command () places into GLOBAL_COMMAND. See "command.h" for the
372 details of the command structure.
373
374 EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible
375 return values. Executing a command with nothing in it returns
376 EXECUTION_SUCCESS. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000377int
Jari Aalto726f6381996-08-26 18:22:31 +0000378execute_command (command)
379 COMMAND *command;
380{
381 struct fd_bitmap *bitmap;
382 int result;
383
384 current_fds_to_close = (struct fd_bitmap *)NULL;
385 bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE);
386 begin_unwind_frame ("execute-command");
387 add_unwind_protect (dispose_fd_bitmap, (char *)bitmap);
388
389 /* Just do the command, but not asynchronously. */
390 result = execute_command_internal (command, 0, NO_PIPE, NO_PIPE, bitmap);
391
392 dispose_fd_bitmap (bitmap);
393 discard_unwind_frame ("execute-command");
394
395#if defined (PROCESS_SUBSTITUTION)
Jari Aaltobb706242000-03-17 21:46:59 +0000396 /* don't unlink fifos if we're in a shell function; wait until the function
397 returns. */
398 if (variable_context == 0)
399 unlink_fifo_list ();
Jari Aalto726f6381996-08-26 18:22:31 +0000400#endif /* PROCESS_SUBSTITUTION */
401
Jari Aalto06285672006-10-10 14:15:34 +0000402 QUIT;
Jari Aalto726f6381996-08-26 18:22:31 +0000403 return (result);
404}
405
406/* Return 1 if TYPE is a shell control structure type. */
407static int
408shell_control_structure (type)
409 enum command_type type;
410{
411 switch (type)
412 {
Jari Aaltobb706242000-03-17 21:46:59 +0000413#if defined (ARITH_FOR_COMMAND)
414 case cm_arith_for:
415#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000416#if defined (SELECT_COMMAND)
417 case cm_select:
418#endif
Jari Aaltocce855b1998-04-17 19:52:44 +0000419#if defined (DPAREN_ARITHMETIC)
420 case cm_arith:
421#endif
422#if defined (COND_COMMAND)
423 case cm_cond:
424#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000425 case cm_case:
426 case cm_while:
427 case cm_until:
428 case cm_if:
Jari Aalto95732b42005-12-07 14:08:12 +0000429 case cm_for:
Jari Aalto726f6381996-08-26 18:22:31 +0000430 case cm_group:
Jari Aalto95732b42005-12-07 14:08:12 +0000431 case cm_function_def:
Jari Aalto726f6381996-08-26 18:22:31 +0000432 return (1);
433
434 default:
435 return (0);
436 }
437}
438
439/* A function to use to unwind_protect the redirection undo list
440 for loops. */
441static void
442cleanup_redirects (list)
443 REDIRECT *list;
444{
Jari Aaltob80f6442004-07-27 13:29:18 +0000445 do_redirections (list, RX_ACTIVE);
Jari Aalto726f6381996-08-26 18:22:31 +0000446 dispose_redirects (list);
447}
448
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000449#if 0
Jari Aalto726f6381996-08-26 18:22:31 +0000450/* Function to unwind_protect the redirections for functions and builtins. */
451static void
452cleanup_func_redirects (list)
453 REDIRECT *list;
454{
Jari Aaltob80f6442004-07-27 13:29:18 +0000455 do_redirections (list, RX_ACTIVE);
Jari Aalto726f6381996-08-26 18:22:31 +0000456}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000457#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000458
Jari Aaltocce855b1998-04-17 19:52:44 +0000459void
Jari Aalto726f6381996-08-26 18:22:31 +0000460dispose_exec_redirects ()
461{
462 if (exec_redirection_undo_list)
463 {
464 dispose_redirects (exec_redirection_undo_list);
465 exec_redirection_undo_list = (REDIRECT *)NULL;
466 }
467}
468
469#if defined (JOB_CONTROL)
470/* A function to restore the signal mask to its proper value when the shell
471 is interrupted or errors occur while creating a pipeline. */
472static int
473restore_signal_mask (set)
Jari Aaltof73dda02001-11-13 17:56:06 +0000474 sigset_t *set;
Jari Aalto726f6381996-08-26 18:22:31 +0000475{
Jari Aaltof73dda02001-11-13 17:56:06 +0000476 return (sigprocmask (SIG_SETMASK, set, (sigset_t *)NULL));
Jari Aalto726f6381996-08-26 18:22:31 +0000477}
478#endif /* JOB_CONTROL */
479
Jari Aaltof73dda02001-11-13 17:56:06 +0000480#ifdef DEBUG
Jari Aalto726f6381996-08-26 18:22:31 +0000481/* A debugging function that can be called from gdb, for instance. */
482void
483open_files ()
484{
485 register int i;
486 int f, fd_table_size;
487
488 fd_table_size = getdtablesize ();
489
Jari Aaltof73dda02001-11-13 17:56:06 +0000490 fprintf (stderr, "pid %ld open files:", (long)getpid ());
Jari Aalto726f6381996-08-26 18:22:31 +0000491 for (i = 3; i < fd_table_size; i++)
492 {
493 if ((f = fcntl (i, F_GETFD, 0)) != -1)
494 fprintf (stderr, " %d (%s)", i, f ? "close" : "open");
495 }
496 fprintf (stderr, "\n");
497}
Jari Aaltof73dda02001-11-13 17:56:06 +0000498#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000499
Jari Aaltod166f041997-06-05 14:59:13 +0000500static void
501async_redirect_stdin ()
502{
503 int fd;
504
505 fd = open ("/dev/null", O_RDONLY);
506 if (fd > 0)
507 {
508 dup2 (fd, 0);
509 close (fd);
510 }
511 else if (fd < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000512 internal_error (_("cannot redirect standard input from /dev/null: %s"), strerror (errno));
Jari Aaltod166f041997-06-05 14:59:13 +0000513}
514
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000515#define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
Jari Aalto726f6381996-08-26 18:22:31 +0000516
Chet Rameyac50fba2014-02-26 09:36:43 -0500517/* Execute the command passed in COMMAND, perhaps doing it asynchronously.
Jari Aalto726f6381996-08-26 18:22:31 +0000518 COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
519 ASYNCHROUNOUS, if non-zero, says to do this command in the background.
520 PIPE_IN and PIPE_OUT are file descriptors saying where input comes
521 from and where it goes. They can have the value of NO_PIPE, which means
522 I/O is stdin/stdout.
523 FDS_TO_CLOSE is a list of file descriptors to close once the child has
524 been forked. This list often contains the unusable sides of pipes, etc.
525
526 EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible
527 return values. Executing a command with nothing in it returns
528 EXECUTION_SUCCESS. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000529int
530execute_command_internal (command, asynchronous, pipe_in, pipe_out,
Jari Aalto726f6381996-08-26 18:22:31 +0000531 fds_to_close)
532 COMMAND *command;
533 int asynchronous;
534 int pipe_in, pipe_out;
535 struct fd_bitmap *fds_to_close;
536{
Jari Aalto17345e52009-02-19 22:21:29 +0000537 int exec_result, user_subshell, invert, ignore_return, was_error_trap;
Jari Aaltod166f041997-06-05 14:59:13 +0000538 REDIRECT *my_undo_list, *exec_undo_list;
Chet Rameyac50fba2014-02-26 09:36:43 -0500539 char *tcmd;
Jari Aalto95732b42005-12-07 14:08:12 +0000540 volatile int last_pid;
Jari Aaltob80f6442004-07-27 13:29:18 +0000541 volatile int save_line_number;
Chet Rameyac50fba2014-02-26 09:36:43 -0500542#if defined (PROCESS_SUBSTITUTION)
543 volatile int ofifo, nfifo, osize, saved_fifo;
544 volatile char *ofifo_list;
545#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000546
Jari Aaltof1be6662008-11-18 13:15:12 +0000547 if (breaking || continuing)
548 return (last_command_exit_value);
549 if (command == 0 || read_but_dont_execute)
550 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000551
Jari Aalto06285672006-10-10 14:15:34 +0000552 QUIT;
Jari Aalto726f6381996-08-26 18:22:31 +0000553 run_pending_traps ();
554
Jari Aaltob80f6442004-07-27 13:29:18 +0000555#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000556 if (running_trap == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000557#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000558 currently_executing_command = command;
559
Jari Aalto726f6381996-08-26 18:22:31 +0000560 invert = (command->flags & CMD_INVERT_RETURN) != 0;
Jari Aaltod166f041997-06-05 14:59:13 +0000561
562 /* If we're inverting the return value and `set -e' has been executed,
563 we don't want a failing command to inadvertently cause the shell
564 to exit. */
565 if (exit_immediately_on_error && invert) /* XXX */
566 command->flags |= CMD_IGNORE_RETURN; /* XXX */
567
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000568 exec_result = EXECUTION_SUCCESS;
Jari Aalto726f6381996-08-26 18:22:31 +0000569
570 /* If a command was being explicitly run in a subshell, or if it is
571 a shell control-structure, and it has a pipe, then we do the command
572 in a subshell. */
Jari Aaltobb706242000-03-17 21:46:59 +0000573 if (command->type == cm_subshell && (command->flags & CMD_NO_FORK))
574 return (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close));
Jari Aalto726f6381996-08-26 18:22:31 +0000575
Jari Aalto31859422009-01-12 13:36:28 +0000576#if defined (COPROCESS_SUPPORT)
577 if (command->type == cm_coproc)
578 return (execute_coproc (command, pipe_in, pipe_out, fds_to_close));
579#endif
580
Jari Aalto17345e52009-02-19 22:21:29 +0000581 user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0);
582
Jari Aaltobb706242000-03-17 21:46:59 +0000583 if (command->type == cm_subshell ||
584 (command->flags & (CMD_WANT_SUBSHELL|CMD_FORCE_SUBSHELL)) ||
Jari Aalto726f6381996-08-26 18:22:31 +0000585 (shell_control_structure (command->type) &&
586 (pipe_out != NO_PIPE || pipe_in != NO_PIPE || asynchronous)))
587 {
588 pid_t paren_pid;
Chet Rameyac50fba2014-02-26 09:36:43 -0500589 int s;
Jari Aalto726f6381996-08-26 18:22:31 +0000590
591 /* Fork a subshell, turn off the subshell bit, turn off job
592 control and call execute_command () on the command again. */
Chet Ramey89a92862011-11-21 20:49:12 -0500593 line_number_for_err_trap = line_number;
Chet Rameyac50fba2014-02-26 09:36:43 -0500594 tcmd = make_command_string (command);
595 paren_pid = make_child (savestring (tcmd), asynchronous);
596
597 if (user_subshell && signal_is_trapped (ERROR_TRAP) &&
598 signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0)
599 {
600 FREE (the_printed_command_except_trap);
601 the_printed_command_except_trap = savestring (the_printed_command);
602 }
603
Jari Aalto726f6381996-08-26 18:22:31 +0000604 if (paren_pid == 0)
Chet Rameyac50fba2014-02-26 09:36:43 -0500605 {
606 /* We want to run the exit trap for forced {} subshells, and we
607 want to note this before execute_in_subshell modifies the
608 COMMAND struct. Need to keep in mind that execute_in_subshell
609 runs the exit trap for () subshells itself. */
610 /* This handles { command; } & */
611 s = user_subshell == 0 && command->type == cm_group && pipe_in == NO_PIPE && pipe_out == NO_PIPE && asynchronous;
612 /* run exit trap for : | { ...; } and { ...; } | : */
613 /* run exit trap for : | ( ...; ) and ( ...; ) | : */
614 s += user_subshell == 0 && command->type == cm_group && (pipe_in != NO_PIPE || pipe_out != NO_PIPE) && asynchronous == 0;
615
616 last_command_exit_value = execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close);
617 if (s)
618 subshell_exit (last_command_exit_value);
619 else
620 exit (last_command_exit_value);
621 /* NOTREACHED */
622 }
Jari Aalto726f6381996-08-26 18:22:31 +0000623 else
624 {
625 close_pipes (pipe_in, pipe_out);
626
627#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
Chet Rameyac50fba2014-02-26 09:36:43 -0500628 if (variable_context == 0) /* wait until shell function completes */
629 unlink_fifo_list ();
Jari Aalto726f6381996-08-26 18:22:31 +0000630#endif
631 /* If we are part of a pipeline, and not the end of the pipeline,
632 then we should simply return and let the last command in the
633 pipe be waited for. If we are not in a pipeline, or are the
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000634 last command in the pipeline, then we wait for the subshell
Jari Aalto726f6381996-08-26 18:22:31 +0000635 and return its exit status as usual. */
636 if (pipe_out != NO_PIPE)
637 return (EXECUTION_SUCCESS);
638
639 stop_pipeline (asynchronous, (COMMAND *)NULL);
640
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000641 if (asynchronous == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000642 {
Jari Aalto17345e52009-02-19 22:21:29 +0000643 was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
644 invert = (command->flags & CMD_INVERT_RETURN) != 0;
645 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
646
Chet Ramey495aee42011-11-22 19:11:26 -0500647 exec_result = wait_for (paren_pid);
Jari Aalto726f6381996-08-26 18:22:31 +0000648
649 /* If we have to, invert the return value. */
650 if (invert)
Chet Ramey495aee42011-11-22 19:11:26 -0500651 exec_result = ((exec_result == EXECUTION_SUCCESS)
Jari Aaltobb706242000-03-17 21:46:59 +0000652 ? EXECUTION_FAILURE
653 : EXECUTION_SUCCESS);
Jari Aaltobb706242000-03-17 21:46:59 +0000654
Chet Ramey495aee42011-11-22 19:11:26 -0500655 last_command_exit_value = exec_result;
Jari Aalto17345e52009-02-19 22:21:29 +0000656 if (user_subshell && was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
657 {
Chet Ramey89a92862011-11-21 20:49:12 -0500658 save_line_number = line_number;
659 line_number = line_number_for_err_trap;
Jari Aalto17345e52009-02-19 22:21:29 +0000660 run_error_trap ();
Chet Ramey89a92862011-11-21 20:49:12 -0500661 line_number = save_line_number;
Jari Aalto17345e52009-02-19 22:21:29 +0000662 }
663
664 if (user_subshell && ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS)
665 {
Jari Aalto17345e52009-02-19 22:21:29 +0000666 run_pending_traps ();
667 jump_to_top_level (ERREXIT);
668 }
669
Chet Ramey495aee42011-11-22 19:11:26 -0500670 return (last_command_exit_value);
Jari Aalto726f6381996-08-26 18:22:31 +0000671 }
672 else
673 {
674 DESCRIBE_PID (paren_pid);
675
676 run_pending_traps ();
677
Chet Rameyac50fba2014-02-26 09:36:43 -0500678 /* Posix 2013 2.9.3.1: "the exit status of an asynchronous list
679 shall be zero." */
680 last_command_exit_value = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000681 return (EXECUTION_SUCCESS);
682 }
683 }
684 }
685
Jari Aaltod166f041997-06-05 14:59:13 +0000686#if defined (COMMAND_TIMING)
687 if (command->flags & CMD_TIME_PIPELINE)
688 {
689 if (asynchronous)
690 {
691 command->flags |= CMD_FORCE_SUBSHELL;
692 exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close);
693 }
694 else
695 {
696 exec_result = time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close);
Jari Aaltob80f6442004-07-27 13:29:18 +0000697#if 0
Jari Aaltod166f041997-06-05 14:59:13 +0000698 if (running_trap == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000699#endif
Jari Aaltod166f041997-06-05 14:59:13 +0000700 currently_executing_command = (COMMAND *)NULL;
701 }
702 return (exec_result);
703 }
704#endif /* COMMAND_TIMING */
705
706 if (shell_control_structure (command->type) && command->redirects)
707 stdin_redir = stdin_redirects (command->redirects);
708
Chet Rameyac50fba2014-02-26 09:36:43 -0500709#if defined (PROCESS_SUBSTITUTION)
710 if (variable_context != 0)
711 {
712 ofifo = num_fifos ();
713 ofifo_list = copy_fifo_list ((int *)&osize);
714 saved_fifo = 1;
715 }
716 else
717 saved_fifo = 0;
718#endif
719
Jari Aalto726f6381996-08-26 18:22:31 +0000720 /* Handle WHILE FOR CASE etc. with redirections. (Also '&' input
721 redirection.) */
Jari Aaltob80f6442004-07-27 13:29:18 +0000722 if (do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE) != 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000723 {
724 cleanup_redirects (redirection_undo_list);
725 redirection_undo_list = (REDIRECT *)NULL;
726 dispose_exec_redirects ();
Chet Rameyac50fba2014-02-26 09:36:43 -0500727#if defined (PROCESS_SUBSTITUTION)
728 if (saved_fifo)
729 free ((void *)ofifo_list);
730#endif
Jari Aaltof1be6662008-11-18 13:15:12 +0000731 return (last_command_exit_value = EXECUTION_FAILURE);
Jari Aalto726f6381996-08-26 18:22:31 +0000732 }
733
734 if (redirection_undo_list)
735 {
Chet Ramey495aee42011-11-22 19:11:26 -0500736 /* XXX - why copy here? */
Jari Aalto726f6381996-08-26 18:22:31 +0000737 my_undo_list = (REDIRECT *)copy_redirects (redirection_undo_list);
738 dispose_redirects (redirection_undo_list);
739 redirection_undo_list = (REDIRECT *)NULL;
740 }
741 else
742 my_undo_list = (REDIRECT *)NULL;
743
744 if (exec_redirection_undo_list)
745 {
Chet Ramey495aee42011-11-22 19:11:26 -0500746 /* XXX - why copy here? */
Jari Aalto726f6381996-08-26 18:22:31 +0000747 exec_undo_list = (REDIRECT *)copy_redirects (exec_redirection_undo_list);
748 dispose_redirects (exec_redirection_undo_list);
749 exec_redirection_undo_list = (REDIRECT *)NULL;
750 }
751 else
752 exec_undo_list = (REDIRECT *)NULL;
753
754 if (my_undo_list || exec_undo_list)
755 begin_unwind_frame ("loop_redirections");
756
757 if (my_undo_list)
758 add_unwind_protect ((Function *)cleanup_redirects, my_undo_list);
759
760 if (exec_undo_list)
761 add_unwind_protect ((Function *)dispose_redirects, exec_undo_list);
762
763 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
764
765 QUIT;
766
767 switch (command->type)
768 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000769 case cm_simple:
770 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000771 save_line_number = line_number;
772 /* We can't rely on variables retaining their values across a
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000773 call to execute_simple_command if a longjmp occurs as the
774 result of a `return' builtin. This is true for sure with gcc. */
Jari Aalto95732b42005-12-07 14:08:12 +0000775#if defined (RECYCLES_PIDS)
776 last_made_pid = NO_PID;
777#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000778 last_pid = last_made_pid;
Jari Aaltof73dda02001-11-13 17:56:06 +0000779 was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000780
781 if (ignore_return && command->value.Simple)
782 command->value.Simple->flags |= CMD_IGNORE_RETURN;
Jari Aaltod166f041997-06-05 14:59:13 +0000783 if (command->flags & CMD_STDIN_REDIR)
784 command->value.Simple->flags |= CMD_STDIN_REDIR;
Jari Aaltob80f6442004-07-27 13:29:18 +0000785
Jari Aalto06285672006-10-10 14:15:34 +0000786 line_number_for_err_trap = line_number = command->value.Simple->line;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000787 exec_result =
788 execute_simple_command (command->value.Simple, pipe_in, pipe_out,
789 asynchronous, fds_to_close);
Jari Aaltob80f6442004-07-27 13:29:18 +0000790 line_number = save_line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000791
792 /* The temporary environment should be used for only the simple
793 command immediately following its definition. */
794 dispose_used_env_vars ();
795
796#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
797 /* Reclaim memory allocated with alloca () on machines which
798 may be using the alloca emulation code. */
799 (void) alloca (0);
800#endif /* (ultrix && mips) || C_ALLOCA */
801
802 /* If we forked to do the command, then we must wait_for ()
803 the child. */
804
805 /* XXX - this is something to watch out for if there are problems
Chet Rameyac50fba2014-02-26 09:36:43 -0500806 when the shell is compiled without job control. Don't worry about
807 whether or not last_made_pid == last_pid; already_making_children
808 tells us whether or not there are unwaited-for children to wait
809 for and reap. */
810 if (already_making_children && pipe_out == NO_PIPE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000811 {
812 stop_pipeline (asynchronous, (COMMAND *)NULL);
813
814 if (asynchronous)
815 {
816 DESCRIBE_PID (last_made_pid);
817 }
818 else
819#if !defined (JOB_CONTROL)
820 /* Do not wait for asynchronous processes started from
821 startup files. */
822 if (last_made_pid != last_asynchronous_pid)
823#endif
824 /* When executing a shell function that executes other
825 commands, this causes the last simple command in
Jari Aaltob80f6442004-07-27 13:29:18 +0000826 the function to be waited for twice. This also causes
827 subshells forked to execute builtin commands (e.g., in
828 pipelines) to be waited for twice. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000829 exec_result = wait_for (last_made_pid);
830 }
831 }
832
Jari Aalto17345e52009-02-19 22:21:29 +0000833 /* 2009/02/13 -- pipeline failure is processed elsewhere. This handles
834 only the failure of a simple command. */
Jari Aalto31859422009-01-12 13:36:28 +0000835 if (was_error_trap && ignore_return == 0 && invert == 0 && pipe_in == NO_PIPE && pipe_out == NO_PIPE && exec_result != EXECUTION_SUCCESS)
Jari Aaltof73dda02001-11-13 17:56:06 +0000836 {
837 last_command_exit_value = exec_result;
Chet Ramey89a92862011-11-21 20:49:12 -0500838 line_number = line_number_for_err_trap;
Jari Aaltof73dda02001-11-13 17:56:06 +0000839 run_error_trap ();
Chet Ramey89a92862011-11-21 20:49:12 -0500840 line_number = save_line_number;
Jari Aaltof73dda02001-11-13 17:56:06 +0000841 }
842
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000843 if (ignore_return == 0 && invert == 0 &&
Jari Aaltocce855b1998-04-17 19:52:44 +0000844 ((posixly_correct && interactive == 0 && special_builtin_failed) ||
Jari Aalto31859422009-01-12 13:36:28 +0000845 (exit_immediately_on_error && pipe_in == NO_PIPE && pipe_out == NO_PIPE && exec_result != EXECUTION_SUCCESS)))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000846 {
847 last_command_exit_value = exec_result;
848 run_pending_traps ();
Jari Aaltob80f6442004-07-27 13:29:18 +0000849 jump_to_top_level (ERREXIT);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000850 }
851
852 break;
853
Jari Aalto726f6381996-08-26 18:22:31 +0000854 case cm_for:
855 if (ignore_return)
856 command->value.For->flags |= CMD_IGNORE_RETURN;
857 exec_result = execute_for_command (command->value.For);
858 break;
859
Jari Aaltobb706242000-03-17 21:46:59 +0000860#if defined (ARITH_FOR_COMMAND)
861 case cm_arith_for:
862 if (ignore_return)
863 command->value.ArithFor->flags |= CMD_IGNORE_RETURN;
864 exec_result = execute_arith_for_command (command->value.ArithFor);
865 break;
866#endif
867
Jari Aalto726f6381996-08-26 18:22:31 +0000868#if defined (SELECT_COMMAND)
869 case cm_select:
870 if (ignore_return)
871 command->value.Select->flags |= CMD_IGNORE_RETURN;
872 exec_result = execute_select_command (command->value.Select);
873 break;
874#endif
875
876 case cm_case:
877 if (ignore_return)
878 command->value.Case->flags |= CMD_IGNORE_RETURN;
879 exec_result = execute_case_command (command->value.Case);
880 break;
881
882 case cm_while:
883 if (ignore_return)
884 command->value.While->flags |= CMD_IGNORE_RETURN;
885 exec_result = execute_while_command (command->value.While);
886 break;
887
888 case cm_until:
889 if (ignore_return)
890 command->value.While->flags |= CMD_IGNORE_RETURN;
891 exec_result = execute_until_command (command->value.While);
892 break;
893
894 case cm_if:
895 if (ignore_return)
896 command->value.If->flags |= CMD_IGNORE_RETURN;
897 exec_result = execute_if_command (command->value.If);
898 break;
899
900 case cm_group:
901
902 /* This code can be executed from either of two paths: an explicit
903 '{}' command, or via a function call. If we are executed via a
904 function call, we have already taken care of the function being
905 executed in the background (down there in execute_simple_command ()),
906 and this command should *not* be marked as asynchronous. If we
907 are executing a regular '{}' group command, and asynchronous == 1,
908 we must want to execute the whole command in the background, so we
909 need a subshell, and we want the stuff executed in that subshell
910 (this group command) to be executed in the foreground of that
911 subshell (i.e. there will not be *another* subshell forked).
912
913 What we do is to force a subshell if asynchronous, and then call
914 execute_command_internal again with asynchronous still set to 1,
915 but with the original group command, so the printed command will
916 look right.
917
918 The code above that handles forking off subshells will note that
919 both subshell and async are on, and turn off async in the child
920 after forking the subshell (but leave async set in the parent, so
921 the normal call to describe_pid is made). This turning off
922 async is *crucial*; if it is not done, this will fall into an
923 infinite loop of executions through this spot in subshell after
924 subshell until the process limit is exhausted. */
925
926 if (asynchronous)
927 {
928 command->flags |= CMD_FORCE_SUBSHELL;
929 exec_result =
930 execute_command_internal (command, 1, pipe_in, pipe_out,
931 fds_to_close);
932 }
933 else
934 {
935 if (ignore_return && command->value.Group->command)
936 command->value.Group->command->flags |= CMD_IGNORE_RETURN;
937 exec_result =
938 execute_command_internal (command->value.Group->command,
939 asynchronous, pipe_in, pipe_out,
940 fds_to_close);
941 }
942 break;
943
Jari Aalto726f6381996-08-26 18:22:31 +0000944 case cm_connection:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000945 exec_result = execute_connection (command, asynchronous,
946 pipe_in, pipe_out, fds_to_close);
Jari Aalto726f6381996-08-26 18:22:31 +0000947 break;
948
Jari Aaltocce855b1998-04-17 19:52:44 +0000949#if defined (DPAREN_ARITHMETIC)
950 case cm_arith:
Chet Ramey00018032011-11-21 20:51:19 -0500951 was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
Jari Aaltocce855b1998-04-17 19:52:44 +0000952 if (ignore_return)
953 command->value.Arith->flags |= CMD_IGNORE_RETURN;
Chet Ramey00018032011-11-21 20:51:19 -0500954 line_number_for_err_trap = save_line_number = line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +0000955 exec_result = execute_arith_command (command->value.Arith);
Chet Ramey00018032011-11-21 20:51:19 -0500956 line_number = save_line_number;
957
958 if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
959 {
960 last_command_exit_value = exec_result;
961 save_line_number = line_number;
962 line_number = line_number_for_err_trap;
963 run_error_trap ();
964 line_number = save_line_number;
965 }
966
967 if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS)
968 {
969 last_command_exit_value = exec_result;
970 run_pending_traps ();
971 jump_to_top_level (ERREXIT);
972 }
973
Jari Aaltocce855b1998-04-17 19:52:44 +0000974 break;
975#endif
976
977#if defined (COND_COMMAND)
978 case cm_cond:
Chet Ramey00018032011-11-21 20:51:19 -0500979 was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
Jari Aaltocce855b1998-04-17 19:52:44 +0000980 if (ignore_return)
981 command->value.Cond->flags |= CMD_IGNORE_RETURN;
Chet Ramey00018032011-11-21 20:51:19 -0500982
983 line_number_for_err_trap = save_line_number = line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +0000984 exec_result = execute_cond_command (command->value.Cond);
Jari Aaltob80f6442004-07-27 13:29:18 +0000985 line_number = save_line_number;
Chet Ramey00018032011-11-21 20:51:19 -0500986
987 if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
988 {
989 last_command_exit_value = exec_result;
990 save_line_number = line_number;
991 line_number = line_number_for_err_trap;
992 run_error_trap ();
993 line_number = save_line_number;
994 }
995
996 if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS)
997 {
998 last_command_exit_value = exec_result;
999 run_pending_traps ();
1000 jump_to_top_level (ERREXIT);
1001 }
1002
Jari Aaltocce855b1998-04-17 19:52:44 +00001003 break;
1004#endif
1005
Jari Aalto726f6381996-08-26 18:22:31 +00001006 case cm_function_def:
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001007 exec_result = execute_intern_function (command->value.Function_def->name,
Chet Rameyac50fba2014-02-26 09:36:43 -05001008 command->value.Function_def);
Jari Aalto726f6381996-08-26 18:22:31 +00001009 break;
1010
1011 default:
Jari Aaltob72432f1999-02-19 17:11:39 +00001012 command_error ("execute_command", CMDERR_BADTYPE, command->type, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001013 }
1014
1015 if (my_undo_list)
1016 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001017 do_redirections (my_undo_list, RX_ACTIVE);
Jari Aalto726f6381996-08-26 18:22:31 +00001018 dispose_redirects (my_undo_list);
1019 }
1020
1021 if (exec_undo_list)
1022 dispose_redirects (exec_undo_list);
1023
1024 if (my_undo_list || exec_undo_list)
1025 discard_unwind_frame ("loop_redirections");
1026
Chet Rameyac50fba2014-02-26 09:36:43 -05001027#if defined (PROCESS_SUBSTITUTION)
1028 if (saved_fifo)
1029 {
1030 nfifo = num_fifos ();
1031 if (nfifo > ofifo)
1032 close_new_fifos ((char *)ofifo_list, osize);
1033 free ((void *)ofifo_list);
1034 }
1035#endif
1036
Jari Aalto726f6381996-08-26 18:22:31 +00001037 /* Invert the return value if we have to */
1038 if (invert)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001039 exec_result = (exec_result == EXECUTION_SUCCESS)
1040 ? EXECUTION_FAILURE
1041 : EXECUTION_SUCCESS;
Jari Aalto726f6381996-08-26 18:22:31 +00001042
Jari Aalto06285672006-10-10 14:15:34 +00001043#if defined (DPAREN_ARITHMETIC) || defined (COND_COMMAND)
1044 /* This is where we set PIPESTATUS from the exit status of the appropriate
1045 compound commands (the ones that look enough like simple commands to
1046 cause confusion). We might be able to optimize by not doing this if
1047 subshell_environment != 0. */
1048 switch (command->type)
1049 {
1050# if defined (DPAREN_ARITHMETIC)
1051 case cm_arith:
1052# endif
1053# if defined (COND_COMMAND)
1054 case cm_cond:
1055# endif
1056 set_pipestatus_from_exit (exec_result);
1057 break;
1058 }
1059#endif
1060
Jari Aalto726f6381996-08-26 18:22:31 +00001061 last_command_exit_value = exec_result;
1062 run_pending_traps ();
Jari Aaltob80f6442004-07-27 13:29:18 +00001063#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001064 if (running_trap == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00001065#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001066 currently_executing_command = (COMMAND *)NULL;
Chet Rameyac50fba2014-02-26 09:36:43 -05001067
Jari Aalto726f6381996-08-26 18:22:31 +00001068 return (last_command_exit_value);
1069}
1070
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001071#if defined (COMMAND_TIMING)
Jari Aaltobb706242000-03-17 21:46:59 +00001072
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001073#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
Jari Aaltof73dda02001-11-13 17:56:06 +00001074extern struct timeval *difftimeval __P((struct timeval *, struct timeval *, struct timeval *));
1075extern struct timeval *addtimeval __P((struct timeval *, struct timeval *, struct timeval *));
1076extern int timeval_to_cpu __P((struct timeval *, struct timeval *, struct timeval *));
Jari Aaltobb706242000-03-17 21:46:59 +00001077#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001078
1079#define POSIX_TIMEFORMAT "real %2R\nuser %2U\nsys %2S"
1080#define BASH_TIMEFORMAT "\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS"
1081
Jari Aalto31859422009-01-12 13:36:28 +00001082static const int precs[] = { 0, 100, 10, 1 };
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001083
1084/* Expand one `%'-prefixed escape sequence from a time format string. */
1085static int
1086mkfmt (buf, prec, lng, sec, sec_fraction)
1087 char *buf;
1088 int prec, lng;
Jari Aaltof73dda02001-11-13 17:56:06 +00001089 time_t sec;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001090 int sec_fraction;
1091{
Jari Aaltof73dda02001-11-13 17:56:06 +00001092 time_t min;
1093 char abuf[INT_STRLEN_BOUND(time_t) + 1];
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001094 int ind, aind;
1095
1096 ind = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +00001097 abuf[sizeof(abuf) - 1] = '\0';
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001098
1099 /* If LNG is non-zero, we want to decompose SEC into minutes and seconds. */
1100 if (lng)
1101 {
1102 min = sec / 60;
1103 sec %= 60;
Jari Aaltof73dda02001-11-13 17:56:06 +00001104 aind = sizeof(abuf) - 2;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001105 do
1106 abuf[aind--] = (min % 10) + '0';
1107 while (min /= 10);
1108 aind++;
1109 while (abuf[aind])
Jari Aaltocce855b1998-04-17 19:52:44 +00001110 buf[ind++] = abuf[aind++];
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001111 buf[ind++] = 'm';
1112 }
1113
1114 /* Now add the seconds. */
Jari Aaltof73dda02001-11-13 17:56:06 +00001115 aind = sizeof (abuf) - 2;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001116 do
1117 abuf[aind--] = (sec % 10) + '0';
1118 while (sec /= 10);
1119 aind++;
1120 while (abuf[aind])
1121 buf[ind++] = abuf[aind++];
1122
1123 /* We want to add a decimal point and PREC places after it if PREC is
1124 nonzero. PREC is not greater than 3. SEC_FRACTION is between 0
1125 and 999. */
1126 if (prec != 0)
1127 {
1128 buf[ind++] = '.';
1129 for (aind = 1; aind <= prec; aind++)
1130 {
1131 buf[ind++] = (sec_fraction / precs[aind]) + '0';
1132 sec_fraction %= precs[aind];
1133 }
1134 }
1135
1136 if (lng)
1137 buf[ind++] = 's';
1138 buf[ind] = '\0';
1139
1140 return (ind);
1141}
1142
1143/* Interpret the format string FORMAT, interpolating the following escape
1144 sequences:
Jari Aalto7117c2d2002-07-17 14:10:11 +00001145 %[prec][l][RUS]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001146
1147 where the optional `prec' is a precision, meaning the number of
1148 characters after the decimal point, the optional `l' means to format
1149 using minutes and seconds (MMmNN[.FF]s), like the `times' builtin',
1150 and the last character is one of
1151
Jari Aalto7117c2d2002-07-17 14:10:11 +00001152 R number of seconds of `real' time
1153 U number of seconds of `user' time
1154 S number of seconds of `system' time
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001155
1156 An occurrence of `%%' in the format string is translated to a `%'. The
1157 result is printed to FP, a pointer to a FILE. The other variables are
1158 the seconds and thousandths of a second of real, user, and system time,
1159 resectively. */
1160static void
1161print_formatted_time (fp, format, rs, rsf, us, usf, ss, ssf, cpu)
1162 FILE *fp;
1163 char *format;
Jari Aaltof73dda02001-11-13 17:56:06 +00001164 time_t rs;
1165 int rsf;
1166 time_t us;
1167 int usf;
1168 time_t ss;
1169 int ssf, cpu;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001170{
1171 int prec, lng, len;
Jari Aaltof73dda02001-11-13 17:56:06 +00001172 char *str, *s, ts[INT_STRLEN_BOUND (time_t) + sizeof ("mSS.FFFF")];
1173 time_t sum;
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001174 int sum_frac;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001175 int sindex, ssize;
1176
1177 len = strlen (format);
1178 ssize = (len + 64) - (len % 64);
Jari Aaltof73dda02001-11-13 17:56:06 +00001179 str = (char *)xmalloc (ssize);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001180 sindex = 0;
1181
1182 for (s = format; *s; s++)
1183 {
1184 if (*s != '%' || s[1] == '\0')
Jari Aaltocce855b1998-04-17 19:52:44 +00001185 {
1186 RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64);
1187 str[sindex++] = *s;
1188 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001189 else if (s[1] == '%')
Jari Aaltocce855b1998-04-17 19:52:44 +00001190 {
1191 s++;
1192 RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64);
1193 str[sindex++] = *s;
1194 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001195 else if (s[1] == 'P')
1196 {
1197 s++;
Jari Aalto17345e52009-02-19 22:21:29 +00001198#if 0
1199 /* clamp CPU usage at 100% */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001200 if (cpu > 10000)
1201 cpu = 10000;
Jari Aalto17345e52009-02-19 22:21:29 +00001202#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001203 sum = cpu / 100;
1204 sum_frac = (cpu % 100) * 10;
1205 len = mkfmt (ts, 2, 0, sum, sum_frac);
1206 RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64);
1207 strcpy (str + sindex, ts);
1208 sindex += len;
1209 }
1210 else
1211 {
1212 prec = 3; /* default is three places past the decimal point. */
1213 lng = 0; /* default is to not use minutes or append `s' */
1214 s++;
Jari Aaltof73dda02001-11-13 17:56:06 +00001215 if (DIGIT (*s)) /* `precision' */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001216 {
1217 prec = *s++ - '0';
1218 if (prec > 3) prec = 3;
1219 }
1220 if (*s == 'l') /* `length extender' */
1221 {
1222 lng = 1;
1223 s++;
1224 }
1225 if (*s == 'R' || *s == 'E')
1226 len = mkfmt (ts, prec, lng, rs, rsf);
1227 else if (*s == 'U')
1228 len = mkfmt (ts, prec, lng, us, usf);
1229 else if (*s == 'S')
1230 len = mkfmt (ts, prec, lng, ss, ssf);
1231 else
1232 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001233 internal_error (_("TIMEFORMAT: `%c': invalid format character"), *s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001234 free (str);
1235 return;
1236 }
1237 RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64);
1238 strcpy (str + sindex, ts);
1239 sindex += len;
1240 }
1241 }
1242
1243 str[sindex] = '\0';
1244 fprintf (fp, "%s\n", str);
1245 fflush (fp);
1246
1247 free (str);
1248}
1249
1250static int
1251time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close)
1252 COMMAND *command;
1253 int asynchronous, pipe_in, pipe_out;
1254 struct fd_bitmap *fds_to_close;
1255{
Chet Ramey495aee42011-11-22 19:11:26 -05001256 int rv, posix_time, old_flags, nullcmd;
Jari Aaltof73dda02001-11-13 17:56:06 +00001257 time_t rs, us, ss;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001258 int rsf, usf, ssf;
1259 int cpu;
1260 char *time_format;
1261
1262#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
1263 struct timeval real, user, sys;
1264 struct timeval before, after;
Jari Aaltob80f6442004-07-27 13:29:18 +00001265# if defined (HAVE_STRUCT_TIMEZONE)
1266 struct timezone dtz; /* posix doesn't define this */
1267# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001268 struct rusage selfb, selfa, kidsb, kidsa; /* a = after, b = before */
1269#else
1270# if defined (HAVE_TIMES)
1271 clock_t tbefore, tafter, real, user, sys;
1272 struct tms before, after;
1273# endif
1274#endif
1275
1276#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
Jari Aaltob80f6442004-07-27 13:29:18 +00001277# if defined (HAVE_STRUCT_TIMEZONE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001278 gettimeofday (&before, &dtz);
Jari Aaltob80f6442004-07-27 13:29:18 +00001279# else
1280 gettimeofday (&before, (void *)NULL);
1281# endif /* !HAVE_STRUCT_TIMEZONE */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001282 getrusage (RUSAGE_SELF, &selfb);
1283 getrusage (RUSAGE_CHILDREN, &kidsb);
1284#else
1285# if defined (HAVE_TIMES)
1286 tbefore = times (&before);
1287# endif
1288#endif
1289
Chet Rameyac50fba2014-02-26 09:36:43 -05001290 posix_time = command && (command->flags & CMD_TIME_POSIX);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001291
Chet Ramey495aee42011-11-22 19:11:26 -05001292 nullcmd = (command == 0) || (command->type == cm_simple && command->value.Simple->words == 0 && command->value.Simple->redirects == 0);
1293 if (posixly_correct && nullcmd)
1294 {
1295#if defined (HAVE_GETRUSAGE)
1296 selfb.ru_utime.tv_sec = kidsb.ru_utime.tv_sec = selfb.ru_stime.tv_sec = kidsb.ru_stime.tv_sec = 0;
1297 selfb.ru_utime.tv_usec = kidsb.ru_utime.tv_usec = selfb.ru_stime.tv_usec = kidsb.ru_stime.tv_usec = 0;
1298 before.tv_sec = shell_start_time;
1299 before.tv_usec = 0;
1300#else
1301 before.tms_utime = before.tms_stime = before.tms_cutime = before.tms_cstime = 0;
1302 tbefore = shell_start_time;
1303#endif
1304 }
1305
Jari Aaltod166f041997-06-05 14:59:13 +00001306 old_flags = command->flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001307 command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX);
1308 rv = execute_command_internal (command, asynchronous, pipe_in, pipe_out, fds_to_close);
Jari Aaltod166f041997-06-05 14:59:13 +00001309 command->flags = old_flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001310
Jari Aaltof73dda02001-11-13 17:56:06 +00001311 rs = us = ss = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +00001312 rsf = usf = ssf = cpu = 0;
1313
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001314#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
Jari Aaltob80f6442004-07-27 13:29:18 +00001315# if defined (HAVE_STRUCT_TIMEZONE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001316 gettimeofday (&after, &dtz);
Jari Aaltob80f6442004-07-27 13:29:18 +00001317# else
1318 gettimeofday (&after, (void *)NULL);
1319# endif /* !HAVE_STRUCT_TIMEZONE */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001320 getrusage (RUSAGE_SELF, &selfa);
1321 getrusage (RUSAGE_CHILDREN, &kidsa);
1322
1323 difftimeval (&real, &before, &after);
1324 timeval_to_secs (&real, &rs, &rsf);
1325
1326 addtimeval (&user, difftimeval(&after, &selfb.ru_utime, &selfa.ru_utime),
1327 difftimeval(&before, &kidsb.ru_utime, &kidsa.ru_utime));
1328 timeval_to_secs (&user, &us, &usf);
1329
1330 addtimeval (&sys, difftimeval(&after, &selfb.ru_stime, &selfa.ru_stime),
1331 difftimeval(&before, &kidsb.ru_stime, &kidsa.ru_stime));
1332 timeval_to_secs (&sys, &ss, &ssf);
1333
1334 cpu = timeval_to_cpu (&real, &user, &sys);
1335#else
1336# if defined (HAVE_TIMES)
1337 tafter = times (&after);
1338
1339 real = tafter - tbefore;
1340 clock_t_to_secs (real, &rs, &rsf);
1341
1342 user = (after.tms_utime - before.tms_utime) + (after.tms_cutime - before.tms_cutime);
1343 clock_t_to_secs (user, &us, &usf);
1344
1345 sys = (after.tms_stime - before.tms_stime) + (after.tms_cstime - before.tms_cstime);
1346 clock_t_to_secs (sys, &ss, &ssf);
1347
Jari Aaltod166f041997-06-05 14:59:13 +00001348 cpu = (real == 0) ? 0 : ((user + sys) * 10000) / real;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001349
1350# else
Jari Aaltof73dda02001-11-13 17:56:06 +00001351 rs = us = ss = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001352 rsf = usf = ssf = cpu = 0;
1353# endif
1354#endif
1355
1356 if (posix_time)
1357 time_format = POSIX_TIMEFORMAT;
1358 else if ((time_format = get_string_value ("TIMEFORMAT")) == 0)
Chet Ramey495aee42011-11-22 19:11:26 -05001359 {
1360 if (posixly_correct && nullcmd)
1361 time_format = "user\t%2lU\nsys\t%2lS";
1362 else
1363 time_format = BASH_TIMEFORMAT;
1364 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001365 if (time_format && *time_format)
1366 print_formatted_time (stderr, time_format, rs, rsf, us, usf, ss, ssf, cpu);
1367
1368 return rv;
1369}
1370#endif /* COMMAND_TIMING */
1371
Jari Aaltobb706242000-03-17 21:46:59 +00001372/* Execute a command that's supposed to be in a subshell. This must be
Jari Aalto28ef6c32001-04-06 19:14:31 +00001373 called after make_child and we must be running in the child process.
1374 The caller will return or exit() immediately with the value this returns. */
Jari Aaltobb706242000-03-17 21:46:59 +00001375static int
1376execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close)
1377 COMMAND *command;
1378 int asynchronous;
1379 int pipe_in, pipe_out;
1380 struct fd_bitmap *fds_to_close;
1381{
Jari Aalto28ef6c32001-04-06 19:14:31 +00001382 int user_subshell, return_code, function_value, should_redir_stdin, invert;
Jari Aalto31859422009-01-12 13:36:28 +00001383 int ois, user_coproc;
Chet Ramey00018032011-11-21 20:51:19 -05001384 int result;
Chet Ramey495aee42011-11-22 19:11:26 -05001385 volatile COMMAND *tcom;
Jari Aaltobb706242000-03-17 21:46:59 +00001386
Jari Aaltof73dda02001-11-13 17:56:06 +00001387 USE_VAR(user_subshell);
Jari Aalto31859422009-01-12 13:36:28 +00001388 USE_VAR(user_coproc);
Jari Aaltof73dda02001-11-13 17:56:06 +00001389 USE_VAR(invert);
1390 USE_VAR(tcom);
1391 USE_VAR(asynchronous);
1392
Jari Aaltob80f6442004-07-27 13:29:18 +00001393 subshell_level++;
Jari Aaltobb706242000-03-17 21:46:59 +00001394 should_redir_stdin = (asynchronous && (command->flags & CMD_STDIN_REDIR) &&
1395 pipe_in == NO_PIPE &&
1396 stdin_redirects (command->redirects) == 0);
1397
Jari Aalto28ef6c32001-04-06 19:14:31 +00001398 invert = (command->flags & CMD_INVERT_RETURN) != 0;
Jari Aaltobb706242000-03-17 21:46:59 +00001399 user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0);
Jari Aalto31859422009-01-12 13:36:28 +00001400 user_coproc = command->type == cm_coproc;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001401
Jari Aaltobb706242000-03-17 21:46:59 +00001402 command->flags &= ~(CMD_FORCE_SUBSHELL | CMD_WANT_SUBSHELL | CMD_INVERT_RETURN);
1403
1404 /* If a command is asynchronous in a subshell (like ( foo ) & or
1405 the special case of an asynchronous GROUP command where the
1406 the subshell bit is turned on down in case cm_group: below),
1407 turn off `asynchronous', so that two subshells aren't spawned.
Jari Aalto06285672006-10-10 14:15:34 +00001408 XXX - asynchronous used to be set to 0 in this block, but that
1409 means that setup_async_signals was never run. Now it's set to
1410 0 after subshell_environment is set appropriately and setup_async_signals
1411 is run.
Jari Aaltobb706242000-03-17 21:46:59 +00001412
1413 This seems semantically correct to me. For example,
1414 ( foo ) & seems to say ``do the command `foo' in a subshell
1415 environment, but don't wait for that subshell to finish'',
1416 and "{ foo ; bar ; } &" seems to me to be like functions or
1417 builtins in the background, which executed in a subshell
1418 environment. I just don't see the need to fork two subshells. */
1419
1420 /* Don't fork again, we are already in a subshell. A `doubly
1421 async' shell is not interactive, however. */
1422 if (asynchronous)
1423 {
1424#if defined (JOB_CONTROL)
1425 /* If a construct like ( exec xxx yyy ) & is given while job
1426 control is active, we want to prevent exec from putting the
1427 subshell back into the original process group, carefully
1428 undoing all the work we just did in make_child. */
1429 original_pgrp = -1;
1430#endif /* JOB_CONTROL */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001431 ois = interactive_shell;
Jari Aaltobb706242000-03-17 21:46:59 +00001432 interactive_shell = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001433 /* This test is to prevent alias expansion by interactive shells that
1434 run `(command) &' but to allow scripts that have enabled alias
1435 expansion with `shopt -s expand_alias' to continue to expand
1436 aliases. */
1437 if (ois != interactive_shell)
1438 expand_aliases = 0;
Jari Aaltobb706242000-03-17 21:46:59 +00001439 }
1440
1441 /* Subshells are neither login nor interactive. */
1442 login_shell = interactive = 0;
1443
Jari Aalto06285672006-10-10 14:15:34 +00001444 if (user_subshell)
1445 subshell_environment = SUBSHELL_PAREN;
1446 else
1447 {
1448 subshell_environment = 0; /* XXX */
1449 if (asynchronous)
1450 subshell_environment |= SUBSHELL_ASYNC;
1451 if (pipe_in != NO_PIPE || pipe_out != NO_PIPE)
1452 subshell_environment |= SUBSHELL_PIPE;
Jari Aalto31859422009-01-12 13:36:28 +00001453 if (user_coproc)
1454 subshell_environment |= SUBSHELL_COPROC;
Jari Aalto06285672006-10-10 14:15:34 +00001455 }
Jari Aaltobb706242000-03-17 21:46:59 +00001456
1457 reset_terminating_signals (); /* in sig.c */
1458 /* Cancel traps, in trap.c. */
Chet Ramey495aee42011-11-22 19:11:26 -05001459 /* Reset the signal handlers in the child, but don't free the
1460 trap strings. Set a flag noting that we have to free the
1461 trap strings if we run trap to change a signal disposition. */
1462 reset_signal_handlers ();
1463 subshell_environment |= SUBSHELL_RESETTRAP;
Jari Aalto06285672006-10-10 14:15:34 +00001464
1465 /* Make sure restore_original_signals doesn't undo the work done by
1466 make_child to ensure that asynchronous children are immune to SIGINT
1467 and SIGQUIT. Turn off asynchronous to make sure more subshells are
1468 not spawned. */
Jari Aaltobb706242000-03-17 21:46:59 +00001469 if (asynchronous)
Jari Aalto06285672006-10-10 14:15:34 +00001470 {
1471 setup_async_signals ();
1472 asynchronous = 0;
1473 }
Jari Aaltobb706242000-03-17 21:46:59 +00001474
1475#if defined (JOB_CONTROL)
1476 set_sigchld_handler ();
1477#endif /* JOB_CONTROL */
1478
1479 set_sigint_handler ();
1480
1481#if defined (JOB_CONTROL)
1482 /* Delete all traces that there were any jobs running. This is
1483 only for subshells. */
1484 without_job_control ();
1485#endif /* JOB_CONTROL */
1486
1487 if (fds_to_close)
1488 close_fd_bitmap (fds_to_close);
1489
1490 do_piping (pipe_in, pipe_out);
1491
Jari Aalto31859422009-01-12 13:36:28 +00001492#if defined (COPROCESS_SUPPORT)
1493 coproc_closeall ();
1494#endif
1495
Jari Aaltobb706242000-03-17 21:46:59 +00001496 /* If this is a user subshell, set a flag if stdin was redirected.
1497 This is used later to decide whether to redirect fd 0 to
1498 /dev/null for async commands in the subshell. This adds more
1499 sh compatibility, but I'm not sure it's the right thing to do. */
1500 if (user_subshell)
1501 {
1502 stdin_redir = stdin_redirects (command->redirects);
Chet Rameyac50fba2014-02-26 09:36:43 -05001503 restore_default_signal (EXIT_TRAP);
Jari Aaltobb706242000-03-17 21:46:59 +00001504 }
1505
1506 /* If this is an asynchronous command (command &), we want to
1507 redirect the standard input from /dev/null in the absence of
1508 any specific redirection involving stdin. */
1509 if (should_redir_stdin && stdin_redir == 0)
1510 async_redirect_stdin ();
1511
1512 /* Do redirections, then dispose of them before recursive call. */
1513 if (command->redirects)
1514 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001515 if (do_redirections (command->redirects, RX_ACTIVE) != 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001516 exit (invert ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
Jari Aaltobb706242000-03-17 21:46:59 +00001517
1518 dispose_redirects (command->redirects);
1519 command->redirects = (REDIRECT *)NULL;
1520 }
1521
Jari Aalto31859422009-01-12 13:36:28 +00001522 if (command->type == cm_subshell)
1523 tcom = command->value.Subshell->command;
1524 else if (user_coproc)
1525 tcom = command->value.Coproc->command;
1526 else
1527 tcom = command;
Jari Aaltobb706242000-03-17 21:46:59 +00001528
Jari Aalto95732b42005-12-07 14:08:12 +00001529 if (command->flags & CMD_TIME_PIPELINE)
1530 tcom->flags |= CMD_TIME_PIPELINE;
1531 if (command->flags & CMD_TIME_POSIX)
1532 tcom->flags |= CMD_TIME_POSIX;
1533
Jari Aaltof73dda02001-11-13 17:56:06 +00001534 /* Make sure the subshell inherits any CMD_IGNORE_RETURN flag. */
1535 if ((command->flags & CMD_IGNORE_RETURN) && tcom != command)
1536 tcom->flags |= CMD_IGNORE_RETURN;
1537
Jari Aaltobb706242000-03-17 21:46:59 +00001538 /* If this is a simple command, tell execute_disk_command that it
1539 might be able to get away without forking and simply exec.
1540 This means things like ( sleep 10 ) will only cause one fork.
Jari Aalto28ef6c32001-04-06 19:14:31 +00001541 If we're timing the command or inverting its return value, however,
1542 we cannot do this optimization. */
Jari Aalto31859422009-01-12 13:36:28 +00001543 if ((user_subshell || user_coproc) && (tcom->type == cm_simple || tcom->type == cm_subshell) &&
Jari Aalto28ef6c32001-04-06 19:14:31 +00001544 ((tcom->flags & CMD_TIME_PIPELINE) == 0) &&
1545 ((tcom->flags & CMD_INVERT_RETURN) == 0))
Jari Aaltobb706242000-03-17 21:46:59 +00001546 {
1547 tcom->flags |= CMD_NO_FORK;
1548 if (tcom->type == cm_simple)
1549 tcom->value.Simple->flags |= CMD_NO_FORK;
1550 }
1551
Jari Aalto28ef6c32001-04-06 19:14:31 +00001552 invert = (tcom->flags & CMD_INVERT_RETURN) != 0;
1553 tcom->flags &= ~CMD_INVERT_RETURN;
1554
Chet Rameyac50fba2014-02-26 09:36:43 -05001555 result = setjmp_nosigs (top_level);
Chet Ramey00018032011-11-21 20:51:19 -05001556
Jari Aaltobb706242000-03-17 21:46:59 +00001557 /* If we're inside a function while executing this subshell, we
1558 need to handle a possible `return'. */
1559 function_value = 0;
1560 if (return_catch_flag)
Chet Rameyac50fba2014-02-26 09:36:43 -05001561 function_value = setjmp_nosigs (return_catch);
Jari Aaltobb706242000-03-17 21:46:59 +00001562
Chet Ramey00018032011-11-21 20:51:19 -05001563 /* If we're going to exit the shell, we don't want to invert the return
1564 status. */
1565 if (result == EXITPROG)
1566 invert = 0, return_code = last_command_exit_value;
1567 else if (result)
1568 return_code = EXECUTION_FAILURE;
1569 else if (function_value)
Jari Aaltobb706242000-03-17 21:46:59 +00001570 return_code = return_catch_value;
1571 else
Chet Rameyac50fba2014-02-26 09:36:43 -05001572 return_code = execute_command_internal ((COMMAND *)tcom, asynchronous, NO_PIPE, NO_PIPE, fds_to_close);
Jari Aaltobb706242000-03-17 21:46:59 +00001573
Jari Aalto28ef6c32001-04-06 19:14:31 +00001574 /* If we are asked to, invert the return value. */
1575 if (invert)
1576 return_code = (return_code == EXECUTION_SUCCESS) ? EXECUTION_FAILURE
1577 : EXECUTION_SUCCESS;
1578
Jari Aaltobb706242000-03-17 21:46:59 +00001579 /* If we were explicitly placed in a subshell with (), we need
1580 to do the `shell cleanup' things, such as running traps[0]. */
1581 if (user_subshell && signal_is_trapped (0))
1582 {
1583 last_command_exit_value = return_code;
1584 return_code = run_exit_trap ();
1585 }
1586
Jari Aaltob80f6442004-07-27 13:29:18 +00001587 subshell_level--;
Jari Aaltobb706242000-03-17 21:46:59 +00001588 return (return_code);
1589 /* NOTREACHED */
1590}
1591
Jari Aalto31859422009-01-12 13:36:28 +00001592#if defined (COPROCESS_SUPPORT)
1593#define COPROC_MAX 16
1594
1595typedef struct cpelement
1596 {
1597 struct cpelement *next;
1598 struct coproc *coproc;
1599 }
1600cpelement_t;
1601
1602typedef struct cplist
1603 {
1604 struct cpelement *head;
1605 struct cpelement *tail;
1606 int ncoproc;
Chet Rameyac50fba2014-02-26 09:36:43 -05001607 int lock;
Jari Aalto31859422009-01-12 13:36:28 +00001608 }
1609cplist_t;
1610
1611static struct cpelement *cpe_alloc __P((struct coproc *));
1612static void cpe_dispose __P((struct cpelement *));
1613static struct cpelement *cpl_add __P((struct coproc *));
1614static struct cpelement *cpl_delete __P((pid_t));
Jari Aalto17345e52009-02-19 22:21:29 +00001615static void cpl_reap __P((void));
Jari Aalto31859422009-01-12 13:36:28 +00001616static void cpl_flush __P((void));
Chet Rameyac50fba2014-02-26 09:36:43 -05001617static void cpl_closeall __P((void));
Jari Aalto31859422009-01-12 13:36:28 +00001618static struct cpelement *cpl_search __P((pid_t));
Chet Rameyac50fba2014-02-26 09:36:43 -05001619static struct cpelement *cpl_searchbyname __P((const char *));
Jari Aalto31859422009-01-12 13:36:28 +00001620static void cpl_prune __P((void));
1621
Chet Rameyac50fba2014-02-26 09:36:43 -05001622static void coproc_free __P((struct coproc *));
1623
1624/* Will go away when there is fully-implemented support for multiple coprocs. */
1625Coproc sh_coproc = { 0, NO_PID, -1, -1, 0, 0, 0, 0, 0 };
Jari Aalto31859422009-01-12 13:36:28 +00001626
1627cplist_t coproc_list = {0, 0, 0};
1628
Jari Aalto17345e52009-02-19 22:21:29 +00001629/* Functions to manage the list of coprocs */
Jari Aalto31859422009-01-12 13:36:28 +00001630
1631static struct cpelement *
1632cpe_alloc (cp)
1633 Coproc *cp;
1634{
1635 struct cpelement *cpe;
1636
1637 cpe = (struct cpelement *)xmalloc (sizeof (struct cpelement));
1638 cpe->coproc = cp;
1639 cpe->next = (struct cpelement *)0;
1640 return cpe;
1641}
1642
1643static void
1644cpe_dispose (cpe)
1645 struct cpelement *cpe;
1646{
1647 free (cpe);
1648}
1649
1650static struct cpelement *
1651cpl_add (cp)
1652 Coproc *cp;
1653{
1654 struct cpelement *cpe;
1655
1656 cpe = cpe_alloc (cp);
1657
1658 if (coproc_list.head == 0)
1659 {
1660 coproc_list.head = coproc_list.tail = cpe;
1661 coproc_list.ncoproc = 0; /* just to make sure */
1662 }
1663 else
1664 {
1665 coproc_list.tail->next = cpe;
1666 coproc_list.tail = cpe;
1667 }
1668 coproc_list.ncoproc++;
1669
1670 return cpe;
1671}
1672
1673static struct cpelement *
1674cpl_delete (pid)
1675 pid_t pid;
1676{
1677 struct cpelement *prev, *p;
1678
1679 for (prev = p = coproc_list.head; p; prev = p, p = p->next)
1680 if (p->coproc->c_pid == pid)
1681 {
1682 prev->next = p->next; /* remove from list */
1683 break;
1684 }
1685
1686 if (p == 0)
1687 return 0; /* not found */
1688
1689#if defined (DEBUG)
1690 itrace("cpl_delete: deleting %d", pid);
1691#endif
1692
1693 /* Housekeeping in the border cases. */
1694 if (p == coproc_list.head)
1695 coproc_list.head = coproc_list.head->next;
1696 else if (p == coproc_list.tail)
1697 coproc_list.tail = prev;
1698
1699 coproc_list.ncoproc--;
1700 if (coproc_list.ncoproc == 0)
1701 coproc_list.head = coproc_list.tail = 0;
1702 else if (coproc_list.ncoproc == 1)
1703 coproc_list.tail = coproc_list.head; /* just to make sure */
1704
1705 return (p);
1706}
1707
Jari Aalto17345e52009-02-19 22:21:29 +00001708static void
1709cpl_reap ()
1710{
Chet Rameyac50fba2014-02-26 09:36:43 -05001711 struct cpelement *p, *next, *nh, *nt;
Jari Aalto17345e52009-02-19 22:21:29 +00001712
Chet Rameyac50fba2014-02-26 09:36:43 -05001713 /* Build a new list by removing dead coprocs and fix up the coproc_list
1714 pointers when done. */
1715 nh = nt = next = (struct cpelement *)0;
1716 for (p = coproc_list.head; p; p = next)
1717 {
1718 next = p->next;
1719 if (p->coproc->c_flags & COPROC_DEAD)
1720 {
1721 coproc_list.ncoproc--; /* keep running count, fix up pointers later */
Jari Aalto17345e52009-02-19 22:21:29 +00001722
1723#if defined (DEBUG)
Chet Rameyac50fba2014-02-26 09:36:43 -05001724 itrace("cpl_reap: deleting %d", p->coproc->c_pid);
Jari Aalto17345e52009-02-19 22:21:29 +00001725#endif
1726
Chet Rameyac50fba2014-02-26 09:36:43 -05001727 coproc_dispose (p->coproc);
1728 cpe_dispose (p);
1729 }
1730 else if (nh == 0)
1731 nh = nt = p;
1732 else
1733 {
1734 nt->next = p;
1735 nt = nt->next;
1736 }
1737 }
1738
1739 if (coproc_list.ncoproc == 0)
1740 coproc_list.head = coproc_list.tail = 0;
1741 else
1742 {
1743 if (nt)
1744 nt->next = 0;
1745 coproc_list.head = nh;
1746 coproc_list.tail = nt;
1747 if (coproc_list.ncoproc == 1)
1748 coproc_list.tail = coproc_list.head; /* just to make sure */
1749 }
Jari Aalto17345e52009-02-19 22:21:29 +00001750}
1751
Jari Aalto31859422009-01-12 13:36:28 +00001752/* Clear out the list of saved statuses */
1753static void
1754cpl_flush ()
1755{
1756 struct cpelement *cpe, *p;
1757
1758 for (cpe = coproc_list.head; cpe; )
1759 {
1760 p = cpe;
1761 cpe = cpe->next;
1762
1763 coproc_dispose (p->coproc);
1764 cpe_dispose (p);
1765 }
1766
1767 coproc_list.head = coproc_list.tail = 0;
1768 coproc_list.ncoproc = 0;
1769}
1770
Chet Rameyac50fba2014-02-26 09:36:43 -05001771static void
1772cpl_closeall ()
1773{
1774 struct cpelement *cpe;
1775
1776 for (cpe = coproc_list.head; cpe; cpe = cpe->next)
1777 coproc_close (cpe->coproc);
1778}
1779
1780static void
1781cpl_fdchk (fd)
1782 int fd;
1783{
1784 struct cpelement *cpe;
1785
1786 for (cpe = coproc_list.head; cpe; cpe = cpe->next)
1787 coproc_checkfd (cpe->coproc, fd);
1788}
1789
Jari Aalto31859422009-01-12 13:36:28 +00001790/* Search for PID in the list of coprocs; return the cpelement struct if
1791 found. If not found, return NULL. */
1792static struct cpelement *
1793cpl_search (pid)
1794 pid_t pid;
1795{
Chet Rameyac50fba2014-02-26 09:36:43 -05001796 struct cpelement *cpe;
Jari Aalto31859422009-01-12 13:36:28 +00001797
Chet Rameyac50fba2014-02-26 09:36:43 -05001798 for (cpe = coproc_list.head ; cpe; cpe = cpe->next)
1799 if (cpe->coproc->c_pid == pid)
1800 return cpe;
Jari Aalto31859422009-01-12 13:36:28 +00001801 return (struct cpelement *)NULL;
1802}
1803
1804/* Search for the coproc named NAME in the list of coprocs; return the
1805 cpelement struct if found. If not found, return NULL. */
1806static struct cpelement *
1807cpl_searchbyname (name)
Chet Rameyac50fba2014-02-26 09:36:43 -05001808 const char *name;
Jari Aalto31859422009-01-12 13:36:28 +00001809{
1810 struct cpelement *cp;
1811
1812 for (cp = coproc_list.head ; cp; cp = cp->next)
1813 if (STREQ (cp->coproc->c_name, name))
1814 return cp;
1815 return (struct cpelement *)NULL;
1816}
1817
1818#if 0
1819static void
1820cpl_prune ()
1821{
1822 struct cpelement *cp;
1823
1824 while (coproc_list.head && coproc_list.ncoproc > COPROC_MAX)
1825 {
1826 cp = coproc_list.head;
1827 coproc_list.head = coproc_list.head->next;
1828 coproc_dispose (cp->coproc);
1829 cpe_dispose (cp);
1830 coproc_list.ncoproc--;
1831 }
1832}
1833#endif
1834
1835/* These currently use a single global "shell coproc" but are written in a
1836 way to not preclude additional coprocs later (using the list management
1837 package above). */
1838
1839struct coproc *
1840getcoprocbypid (pid)
1841 pid_t pid;
1842{
Chet Rameyac50fba2014-02-26 09:36:43 -05001843#if MULTIPLE_COPROCS
1844 struct cpelement *p;
1845
1846 p = cpl_search (pid);
1847 return (p ? p->coproc : 0);
1848#else
Jari Aalto31859422009-01-12 13:36:28 +00001849 return (pid == sh_coproc.c_pid ? &sh_coproc : 0);
Chet Rameyac50fba2014-02-26 09:36:43 -05001850#endif
Jari Aalto31859422009-01-12 13:36:28 +00001851}
1852
1853struct coproc *
1854getcoprocbyname (name)
1855 const char *name;
1856{
Chet Rameyac50fba2014-02-26 09:36:43 -05001857#if MULTIPLE_COPROCS
1858 struct cpelement *p;
1859
1860 p = cpl_searchbyname (name);
1861 return (p ? p->coproc : 0);
1862#else
Jari Aalto31859422009-01-12 13:36:28 +00001863 return ((sh_coproc.c_name && STREQ (sh_coproc.c_name, name)) ? &sh_coproc : 0);
Chet Rameyac50fba2014-02-26 09:36:43 -05001864#endif
Jari Aalto31859422009-01-12 13:36:28 +00001865}
1866
1867void
1868coproc_init (cp)
1869 struct coproc *cp;
1870{
1871 cp->c_name = 0;
1872 cp->c_pid = NO_PID;
1873 cp->c_rfd = cp->c_wfd = -1;
1874 cp->c_rsave = cp->c_wsave = -1;
Chet Rameyac50fba2014-02-26 09:36:43 -05001875 cp->c_flags = cp->c_status = cp->c_lock = 0;
Jari Aalto31859422009-01-12 13:36:28 +00001876}
1877
1878struct coproc *
1879coproc_alloc (name, pid)
1880 char *name;
1881 pid_t pid;
1882{
1883 struct coproc *cp;
1884
Chet Rameyac50fba2014-02-26 09:36:43 -05001885#if MULTIPLE_COPROCS
1886 cp = (struct coproc *)xmalloc (sizeof (struct coproc));
1887#else
1888 cp = &sh_coproc;
1889#endif
Jari Aalto31859422009-01-12 13:36:28 +00001890 coproc_init (cp);
Chet Rameyac50fba2014-02-26 09:36:43 -05001891 cp->c_lock = 2;
Jari Aalto31859422009-01-12 13:36:28 +00001892
Jari Aalto31859422009-01-12 13:36:28 +00001893 cp->c_pid = pid;
Chet Rameyac50fba2014-02-26 09:36:43 -05001894 cp->c_name = savestring (name);
1895#if MULTIPLE_COPROCS
1896 cpl_add (cp);
1897#endif
1898 cp->c_lock = 0;
Jari Aalto31859422009-01-12 13:36:28 +00001899 return (cp);
1900}
1901
Chet Rameyac50fba2014-02-26 09:36:43 -05001902static void
1903coproc_free (cp)
1904 struct coproc *cp;
1905{
1906 free (cp);
1907}
1908
Jari Aalto31859422009-01-12 13:36:28 +00001909void
1910coproc_dispose (cp)
1911 struct coproc *cp;
1912{
Chet Rameyac50fba2014-02-26 09:36:43 -05001913 sigset_t set, oset;
1914
Jari Aalto31859422009-01-12 13:36:28 +00001915 if (cp == 0)
1916 return;
1917
Chet Rameyac50fba2014-02-26 09:36:43 -05001918 BLOCK_SIGNAL (SIGCHLD, set, oset);
1919 cp->c_lock = 3;
Jari Aalto31859422009-01-12 13:36:28 +00001920 coproc_unsetvars (cp);
1921 FREE (cp->c_name);
1922 coproc_close (cp);
Chet Rameyac50fba2014-02-26 09:36:43 -05001923#if MULTIPLE_COPROCS
1924 coproc_free (cp);
1925#else
Jari Aalto31859422009-01-12 13:36:28 +00001926 coproc_init (cp);
Chet Rameyac50fba2014-02-26 09:36:43 -05001927 cp->c_lock = 0;
1928#endif
1929 UNBLOCK_SIGNAL (oset);
Jari Aalto31859422009-01-12 13:36:28 +00001930}
1931
Chet Rameyac50fba2014-02-26 09:36:43 -05001932/* Placeholder for now. Will require changes for multiple coprocs */
Jari Aalto31859422009-01-12 13:36:28 +00001933void
1934coproc_flush ()
1935{
Chet Rameyac50fba2014-02-26 09:36:43 -05001936#if MULTIPLE_COPROCS
1937 cpl_flush ();
1938#else
Jari Aalto31859422009-01-12 13:36:28 +00001939 coproc_dispose (&sh_coproc);
Chet Rameyac50fba2014-02-26 09:36:43 -05001940#endif
Jari Aalto31859422009-01-12 13:36:28 +00001941}
1942
1943void
1944coproc_close (cp)
1945 struct coproc *cp;
1946{
1947 if (cp->c_rfd >= 0)
1948 {
1949 close (cp->c_rfd);
1950 cp->c_rfd = -1;
1951 }
1952 if (cp->c_wfd >= 0)
1953 {
1954 close (cp->c_wfd);
1955 cp->c_wfd = -1;
1956 }
1957 cp->c_rsave = cp->c_wsave = -1;
1958}
1959
1960void
1961coproc_closeall ()
1962{
Chet Rameyac50fba2014-02-26 09:36:43 -05001963#if MULTIPLE_COPROCS
1964 cpl_closeall ();
1965#else
1966 coproc_close (&sh_coproc); /* XXX - will require changes for multiple coprocs */
1967#endif
Jari Aalto31859422009-01-12 13:36:28 +00001968}
1969
1970void
Jari Aalto17345e52009-02-19 22:21:29 +00001971coproc_reap ()
1972{
Chet Rameyac50fba2014-02-26 09:36:43 -05001973#if MULTIPLE_COPROCS
1974 cpl_reap ();
1975#else
Jari Aalto17345e52009-02-19 22:21:29 +00001976 struct coproc *cp;
1977
Chet Rameyac50fba2014-02-26 09:36:43 -05001978 cp = &sh_coproc; /* XXX - will require changes for multiple coprocs */
Jari Aalto17345e52009-02-19 22:21:29 +00001979 if (cp && (cp->c_flags & COPROC_DEAD))
1980 coproc_dispose (cp);
Chet Rameyac50fba2014-02-26 09:36:43 -05001981#endif
Jari Aalto17345e52009-02-19 22:21:29 +00001982}
1983
1984void
Jari Aalto31859422009-01-12 13:36:28 +00001985coproc_rclose (cp, fd)
1986 struct coproc *cp;
1987 int fd;
1988{
1989 if (cp->c_rfd >= 0 && cp->c_rfd == fd)
1990 {
1991 close (cp->c_rfd);
1992 cp->c_rfd = -1;
1993 }
1994}
1995
1996void
1997coproc_wclose (cp, fd)
1998 struct coproc *cp;
1999 int fd;
2000{
2001 if (cp->c_wfd >= 0 && cp->c_wfd == fd)
2002 {
2003 close (cp->c_wfd);
2004 cp->c_wfd = -1;
2005 }
2006}
2007
2008void
2009coproc_checkfd (cp, fd)
2010 struct coproc *cp;
2011 int fd;
2012{
2013 int update;
2014
2015 update = 0;
2016 if (cp->c_rfd >= 0 && cp->c_rfd == fd)
2017 update = cp->c_rfd = -1;
2018 if (cp->c_wfd >= 0 && cp->c_wfd == fd)
2019 update = cp->c_wfd = -1;
2020 if (update)
2021 coproc_setvars (cp);
2022}
2023
2024void
2025coproc_fdchk (fd)
2026 int fd;
2027{
Chet Rameyac50fba2014-02-26 09:36:43 -05002028#if MULTIPLE_COPROCS
2029 cpl_fdchk (fd);
2030#else
Jari Aalto31859422009-01-12 13:36:28 +00002031 coproc_checkfd (&sh_coproc, fd);
Chet Rameyac50fba2014-02-26 09:36:43 -05002032#endif
Jari Aalto31859422009-01-12 13:36:28 +00002033}
2034
2035void
2036coproc_fdclose (cp, fd)
2037 struct coproc *cp;
2038 int fd;
2039{
2040 coproc_rclose (cp, fd);
2041 coproc_wclose (cp, fd);
2042 coproc_setvars (cp);
2043}
2044
2045void
2046coproc_fdsave (cp)
2047 struct coproc *cp;
2048{
2049 cp->c_rsave = cp->c_rfd;
2050 cp->c_wsave = cp->c_wfd;
2051}
2052
2053void
2054coproc_fdrestore (cp)
2055 struct coproc *cp;
2056{
2057 cp->c_rfd = cp->c_rsave;
2058 cp->c_wfd = cp->c_wsave;
2059}
Jari Aalto17345e52009-02-19 22:21:29 +00002060
Jari Aalto31859422009-01-12 13:36:28 +00002061void
Jari Aalto17345e52009-02-19 22:21:29 +00002062coproc_pidchk (pid, status)
Jari Aalto31859422009-01-12 13:36:28 +00002063 pid_t pid;
2064{
2065 struct coproc *cp;
2066
Chet Rameyac50fba2014-02-26 09:36:43 -05002067#if MULTIPLE_COPROCS
2068 struct cpelement *cpe;
2069
2070 cpe = cpl_delete (pid);
2071 cp = cpe ? cpe->coproc : 0;
2072#else
Jari Aalto31859422009-01-12 13:36:28 +00002073 cp = getcoprocbypid (pid);
Jari Aalto31859422009-01-12 13:36:28 +00002074#endif
2075 if (cp)
Jari Aalto17345e52009-02-19 22:21:29 +00002076 {
Chet Rameyac50fba2014-02-26 09:36:43 -05002077 cp->c_lock = 4;
Jari Aalto17345e52009-02-19 22:21:29 +00002078 cp->c_status = status;
2079 cp->c_flags |= COPROC_DEAD;
2080 cp->c_flags &= ~COPROC_RUNNING;
Chet Rameyac50fba2014-02-26 09:36:43 -05002081 /* Don't dispose the coproc or unset the COPROC_XXX variables because
2082 this is executed in a signal handler context. Wait until coproc_reap
2083 takes care of it. */
2084 cp->c_lock = 0;
Jari Aalto17345e52009-02-19 22:21:29 +00002085 }
Jari Aalto31859422009-01-12 13:36:28 +00002086}
2087
2088void
2089coproc_setvars (cp)
2090 struct coproc *cp;
2091{
2092 SHELL_VAR *v;
2093 char *namevar, *t;
2094 int l;
2095#if defined (ARRAY_VARS)
2096 arrayind_t ind;
2097#endif
2098
2099 if (cp->c_name == 0)
2100 return;
2101
2102 l = strlen (cp->c_name);
2103 namevar = xmalloc (l + 16);
2104
2105#if defined (ARRAY_VARS)
2106 v = find_variable (cp->c_name);
2107 if (v == 0)
2108 v = make_new_array_variable (cp->c_name);
2109 if (array_p (v) == 0)
2110 v = convert_var_to_array (v);
2111
2112 t = itos (cp->c_rfd);
2113 ind = 0;
2114 v = bind_array_variable (cp->c_name, ind, t, 0);
2115 free (t);
2116
2117 t = itos (cp->c_wfd);
2118 ind = 1;
2119 bind_array_variable (cp->c_name, ind, t, 0);
2120 free (t);
2121#else
2122 sprintf (namevar, "%s_READ", cp->c_name);
2123 t = itos (cp->c_rfd);
2124 bind_variable (namevar, t, 0);
2125 free (t);
2126 sprintf (namevar, "%s_WRITE", cp->c_name);
2127 t = itos (cp->c_wfd);
2128 bind_variable (namevar, t, 0);
2129 free (t);
2130#endif
2131
2132 sprintf (namevar, "%s_PID", cp->c_name);
2133 t = itos (cp->c_pid);
2134 bind_variable (namevar, t, 0);
2135 free (t);
2136
2137 free (namevar);
2138}
2139
2140void
2141coproc_unsetvars (cp)
2142 struct coproc *cp;
2143{
2144 int l;
2145 char *namevar;
2146
2147 if (cp->c_name == 0)
2148 return;
2149
2150 l = strlen (cp->c_name);
2151 namevar = xmalloc (l + 16);
2152
2153 sprintf (namevar, "%s_PID", cp->c_name);
2154 unbind_variable (namevar);
2155
2156#if defined (ARRAY_VARS)
2157 unbind_variable (cp->c_name);
2158#else
2159 sprintf (namevar, "%s_READ", cp->c_name);
2160 unbind_variable (namevar);
2161 sprintf (namevar, "%s_WRITE", cp->c_name);
2162 unbind_variable (namevar);
2163#endif
2164
2165 free (namevar);
2166}
2167
2168static int
2169execute_coproc (command, pipe_in, pipe_out, fds_to_close)
2170 COMMAND *command;
2171 int pipe_in, pipe_out;
2172 struct fd_bitmap *fds_to_close;
2173{
Chet Rameyac50fba2014-02-26 09:36:43 -05002174 int rpipe[2], wpipe[2], estat, invert;
Jari Aalto31859422009-01-12 13:36:28 +00002175 pid_t coproc_pid;
2176 Coproc *cp;
2177 char *tcmd;
Chet Rameyac50fba2014-02-26 09:36:43 -05002178 sigset_t set, oset;
Jari Aalto31859422009-01-12 13:36:28 +00002179
Chet Rameyac50fba2014-02-26 09:36:43 -05002180 /* XXX -- can be removed after changes to handle multiple coprocs */
2181#if !MULTIPLE_COPROCS
Jari Aalto31859422009-01-12 13:36:28 +00002182 if (sh_coproc.c_pid != NO_PID)
Chet Rameyac50fba2014-02-26 09:36:43 -05002183 internal_warning ("execute_coproc: coproc [%d:%s] still exists", sh_coproc.c_pid, sh_coproc.c_name);
Jari Aalto31859422009-01-12 13:36:28 +00002184 coproc_init (&sh_coproc);
Chet Rameyac50fba2014-02-26 09:36:43 -05002185#endif
Jari Aalto31859422009-01-12 13:36:28 +00002186
Chet Rameyac50fba2014-02-26 09:36:43 -05002187 invert = (command->flags & CMD_INVERT_RETURN) != 0;
Jari Aalto31859422009-01-12 13:36:28 +00002188 command_string_index = 0;
2189 tcmd = make_command_string (command);
2190
2191 sh_openpipe ((int *)&rpipe); /* 0 = parent read, 1 = child write */
2192 sh_openpipe ((int *)&wpipe); /* 0 = child read, 1 = parent write */
2193
Chet Rameyac50fba2014-02-26 09:36:43 -05002194 BLOCK_SIGNAL (SIGCHLD, set, oset);
2195
Jari Aalto31859422009-01-12 13:36:28 +00002196 coproc_pid = make_child (savestring (tcmd), 1);
Chet Rameyac50fba2014-02-26 09:36:43 -05002197
Jari Aalto31859422009-01-12 13:36:28 +00002198 if (coproc_pid == 0)
2199 {
2200 close (rpipe[0]);
2201 close (wpipe[1]);
2202
Chet Rameyac50fba2014-02-26 09:36:43 -05002203 UNBLOCK_SIGNAL (oset);
Chet Ramey00018032011-11-21 20:51:19 -05002204 estat = execute_in_subshell (command, 1, wpipe[0], rpipe[1], fds_to_close);
2205
2206 fflush (stdout);
2207 fflush (stderr);
2208
2209 exit (estat);
Jari Aalto31859422009-01-12 13:36:28 +00002210 }
2211
2212 close (rpipe[1]);
2213 close (wpipe[0]);
2214
Chet Rameyac50fba2014-02-26 09:36:43 -05002215 /* XXX - possibly run Coproc->name through word expansion? */
Jari Aalto31859422009-01-12 13:36:28 +00002216 cp = coproc_alloc (command->value.Coproc->name, coproc_pid);
2217 cp->c_rfd = rpipe[0];
2218 cp->c_wfd = wpipe[1];
2219
2220 SET_CLOSE_ON_EXEC (cp->c_rfd);
2221 SET_CLOSE_ON_EXEC (cp->c_wfd);
2222
2223 coproc_setvars (cp);
2224
Chet Rameyac50fba2014-02-26 09:36:43 -05002225 UNBLOCK_SIGNAL (oset);
2226
Jari Aalto31859422009-01-12 13:36:28 +00002227#if 0
2228 itrace ("execute_coproc: [%d] %s", coproc_pid, the_printed_command);
2229#endif
2230
2231 close_pipes (pipe_in, pipe_out);
2232#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
2233 unlink_fifo_list ();
2234#endif
2235 stop_pipeline (1, (COMMAND *)NULL);
2236 DESCRIBE_PID (coproc_pid);
2237 run_pending_traps ();
2238
Chet Rameyac50fba2014-02-26 09:36:43 -05002239 return (invert ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
Jari Aalto31859422009-01-12 13:36:28 +00002240}
2241#endif
2242
Chet Ramey495aee42011-11-22 19:11:26 -05002243static void
2244restore_stdin (s)
2245 int s;
2246{
2247 dup2 (s, 0);
2248 close (s);
2249}
2250
2251/* Catch-all cleanup function for lastpipe code for unwind-protects */
2252static void
2253lastpipe_cleanup (s)
2254 int s;
2255{
2256 unfreeze_jobs_list ();
2257}
2258
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002259static int
2260execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close)
2261 COMMAND *command;
2262 int asynchronous, pipe_in, pipe_out;
2263 struct fd_bitmap *fds_to_close;
2264{
2265 int prev, fildes[2], new_bitmap_size, dummyfd, ignore_return, exec_result;
Chet Ramey495aee42011-11-22 19:11:26 -05002266 int lstdin, lastpipe_flag, lastpipe_jid;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002267 COMMAND *cmd;
2268 struct fd_bitmap *fd_bitmap;
Chet Ramey495aee42011-11-22 19:11:26 -05002269 pid_t lastpid;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002270
2271#if defined (JOB_CONTROL)
2272 sigset_t set, oset;
2273 BLOCK_CHILD (set, oset);
2274#endif /* JOB_CONTROL */
2275
2276 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
2277
2278 prev = pipe_in;
2279 cmd = command;
2280
2281 while (cmd && cmd->type == cm_connection &&
2282 cmd->value.Connection && cmd->value.Connection->connector == '|')
2283 {
2284 /* Make a pipeline between the two commands. */
2285 if (pipe (fildes) < 0)
2286 {
Jari Aalto31859422009-01-12 13:36:28 +00002287 sys_error (_("pipe error"));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002288#if defined (JOB_CONTROL)
2289 terminate_current_pipeline ();
2290 kill_current_pipeline ();
Jari Aalto95732b42005-12-07 14:08:12 +00002291 UNBLOCK_CHILD (oset);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002292#endif /* JOB_CONTROL */
2293 last_command_exit_value = EXECUTION_FAILURE;
2294 /* The unwind-protects installed below will take care
2295 of closing all of the open file descriptors. */
2296 throw_to_top_level ();
2297 return (EXECUTION_FAILURE); /* XXX */
2298 }
2299
2300 /* Here is a problem: with the new file close-on-exec
2301 code, the read end of the pipe (fildes[0]) stays open
2302 in the first process, so that process will never get a
2303 SIGPIPE. There is no way to signal the first process
2304 that it should close fildes[0] after forking, so it
2305 remains open. No SIGPIPE is ever sent because there
2306 is still a file descriptor open for reading connected
2307 to the pipe. We take care of that here. This passes
2308 around a bitmap of file descriptors that must be
2309 closed after making a child process in execute_simple_command. */
2310
2311 /* We need fd_bitmap to be at least as big as fildes[0].
2312 If fildes[0] is less than fds_to_close->size, then
2313 use fds_to_close->size. */
2314 new_bitmap_size = (fildes[0] < fds_to_close->size)
2315 ? fds_to_close->size
2316 : fildes[0] + 8;
2317
2318 fd_bitmap = new_fd_bitmap (new_bitmap_size);
2319
2320 /* Now copy the old information into the new bitmap. */
2321 xbcopy ((char *)fds_to_close->bitmap, (char *)fd_bitmap->bitmap, fds_to_close->size);
2322
2323 /* And mark the pipe file descriptors to be closed. */
2324 fd_bitmap->bitmap[fildes[0]] = 1;
2325
2326 /* In case there are pipe or out-of-processes errors, we
Jari Aaltocce855b1998-04-17 19:52:44 +00002327 want all these file descriptors to be closed when
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002328 unwind-protects are run, and the storage used for the
2329 bitmaps freed up. */
2330 begin_unwind_frame ("pipe-file-descriptors");
2331 add_unwind_protect (dispose_fd_bitmap, fd_bitmap);
2332 add_unwind_protect (close_fd_bitmap, fd_bitmap);
2333 if (prev >= 0)
2334 add_unwind_protect (close, prev);
2335 dummyfd = fildes[1];
2336 add_unwind_protect (close, dummyfd);
2337
2338#if defined (JOB_CONTROL)
Jari Aaltof73dda02001-11-13 17:56:06 +00002339 add_unwind_protect (restore_signal_mask, &oset);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002340#endif /* JOB_CONTROL */
2341
2342 if (ignore_return && cmd->value.Connection->first)
2343 cmd->value.Connection->first->flags |= CMD_IGNORE_RETURN;
2344 execute_command_internal (cmd->value.Connection->first, asynchronous,
2345 prev, fildes[1], fd_bitmap);
2346
2347 if (prev >= 0)
2348 close (prev);
2349
2350 prev = fildes[0];
2351 close (fildes[1]);
2352
2353 dispose_fd_bitmap (fd_bitmap);
2354 discard_unwind_frame ("pipe-file-descriptors");
2355
2356 cmd = cmd->value.Connection->second;
2357 }
2358
Chet Ramey495aee42011-11-22 19:11:26 -05002359 lastpid = last_made_pid;
2360
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002361 /* Now execute the rightmost command in the pipeline. */
2362 if (ignore_return && cmd)
2363 cmd->flags |= CMD_IGNORE_RETURN;
Chet Ramey495aee42011-11-22 19:11:26 -05002364
2365 lastpipe_flag = 0;
Chet Rameyac50fba2014-02-26 09:36:43 -05002366
Chet Ramey495aee42011-11-22 19:11:26 -05002367 begin_unwind_frame ("lastpipe-exec");
2368 lstdin = -1;
2369 /* If the `lastpipe' option is set with shopt, and job control is not
2370 enabled, execute the last element of non-async pipelines in the
2371 current shell environment. */
2372 if (lastpipe_opt && job_control == 0 && asynchronous == 0 && pipe_out == NO_PIPE && prev > 0)
2373 {
Chet Ramey72347972012-05-07 16:23:06 -04002374 lstdin = move_to_high_fd (0, 1, -1);
Chet Ramey495aee42011-11-22 19:11:26 -05002375 if (lstdin > 0)
2376 {
2377 do_piping (prev, pipe_out);
2378 prev = NO_PIPE;
2379 add_unwind_protect (restore_stdin, lstdin);
2380 lastpipe_flag = 1;
2381 freeze_jobs_list ();
2382 lastpipe_jid = stop_pipeline (0, (COMMAND *)NULL); /* XXX */
2383 add_unwind_protect (lastpipe_cleanup, lastpipe_jid);
2384 }
Chet Ramey335de492011-11-22 20:03:16 -05002385 if (cmd)
2386 cmd->flags |= CMD_LASTPIPE;
Chet Ramey495aee42011-11-22 19:11:26 -05002387 }
2388 if (prev >= 0)
2389 add_unwind_protect (close, prev);
2390
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002391 exec_result = execute_command_internal (cmd, asynchronous, prev, pipe_out, fds_to_close);
2392
Chet Ramey495aee42011-11-22 19:11:26 -05002393 if (lstdin > 0)
2394 restore_stdin (lstdin);
2395
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002396 if (prev >= 0)
2397 close (prev);
2398
2399#if defined (JOB_CONTROL)
2400 UNBLOCK_CHILD (oset);
2401#endif
2402
Jari Aalto31859422009-01-12 13:36:28 +00002403 QUIT;
Chet Ramey495aee42011-11-22 19:11:26 -05002404
2405 if (lastpipe_flag)
2406 {
2407#if defined (JOB_CONTROL)
2408 append_process (savestring (the_printed_command), dollar_dollar_pid, exec_result, lastpipe_jid);
2409#endif
2410 lstdin = wait_for (lastpid);
2411#if defined (JOB_CONTROL)
Chet Rameydf2c55d2014-08-01 15:31:14 -04002412 /* If wait_for removes the job from the jobs table, use result of last
2413 command as pipeline's exit status as usual. The jobs list can get
2414 frozen and unfrozen at inconvenient times if there are multiple pipelines
2415 running simultaneously. */
2416 if (INVALID_JOB (lastpipe_jid) == 0)
2417 exec_result = job_exit_status (lastpipe_jid);
2418 else if (pipefail_opt)
2419 exec_result = exec_result | lstdin; /* XXX */
2420 /* otherwise we use exec_result */
2421
Chet Ramey495aee42011-11-22 19:11:26 -05002422#endif
2423 unfreeze_jobs_list ();
2424 }
2425
2426 discard_unwind_frame ("lastpipe-exec");
2427
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002428 return (exec_result);
2429}
2430
2431static int
2432execute_connection (command, asynchronous, pipe_in, pipe_out, fds_to_close)
2433 COMMAND *command;
2434 int asynchronous, pipe_in, pipe_out;
2435 struct fd_bitmap *fds_to_close;
2436{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002437 COMMAND *tc, *second;
Jari Aalto17345e52009-02-19 22:21:29 +00002438 int ignore_return, exec_result, was_error_trap, invert;
Chet Ramey89a92862011-11-21 20:49:12 -05002439 volatile int save_line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002440
2441 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
2442
2443 switch (command->value.Connection->connector)
2444 {
2445 /* Do the first command asynchronously. */
2446 case '&':
2447 tc = command->value.Connection->first;
2448 if (tc == 0)
2449 return (EXECUTION_SUCCESS);
2450
Jari Aaltod166f041997-06-05 14:59:13 +00002451 if (ignore_return)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002452 tc->flags |= CMD_IGNORE_RETURN;
Jari Aaltod166f041997-06-05 14:59:13 +00002453 tc->flags |= CMD_AMPERSAND;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002454
Jari Aaltof73dda02001-11-13 17:56:06 +00002455 /* If this shell was compiled without job control support,
2456 if we are currently in a subshell via `( xxx )', or if job
2457 control is not active then the standard input for an
2458 asynchronous command is forced to /dev/null. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002459#if defined (JOB_CONTROL)
Jari Aaltof73dda02001-11-13 17:56:06 +00002460 if ((subshell_environment || !job_control) && !stdin_redir)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002461#else
2462 if (!stdin_redir)
2463#endif /* JOB_CONTROL */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002464 tc->flags |= CMD_STDIN_REDIR;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002465
2466 exec_result = execute_command_internal (tc, 1, pipe_in, pipe_out, fds_to_close);
Jari Aalto31859422009-01-12 13:36:28 +00002467 QUIT;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002468
Jari Aaltod166f041997-06-05 14:59:13 +00002469 if (tc->flags & CMD_STDIN_REDIR)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002470 tc->flags &= ~CMD_STDIN_REDIR;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002471
2472 second = command->value.Connection->second;
2473 if (second)
2474 {
2475 if (ignore_return)
2476 second->flags |= CMD_IGNORE_RETURN;
2477
2478 exec_result = execute_command_internal (second, asynchronous, pipe_in, pipe_out, fds_to_close);
2479 }
2480
2481 break;
2482
2483 /* Just call execute command on both sides. */
2484 case ';':
2485 if (ignore_return)
2486 {
2487 if (command->value.Connection->first)
2488 command->value.Connection->first->flags |= CMD_IGNORE_RETURN;
2489 if (command->value.Connection->second)
2490 command->value.Connection->second->flags |= CMD_IGNORE_RETURN;
2491 }
Jari Aalto31859422009-01-12 13:36:28 +00002492 executing_list++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002493 QUIT;
2494 execute_command (command->value.Connection->first);
2495 QUIT;
2496 exec_result = execute_command_internal (command->value.Connection->second,
2497 asynchronous, pipe_in, pipe_out,
2498 fds_to_close);
Jari Aalto31859422009-01-12 13:36:28 +00002499 executing_list--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002500 break;
2501
2502 case '|':
Jari Aalto17345e52009-02-19 22:21:29 +00002503 was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
2504 invert = (command->flags & CMD_INVERT_RETURN) != 0;
2505 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
2506
Chet Ramey89a92862011-11-21 20:49:12 -05002507 line_number_for_err_trap = line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002508 exec_result = execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close);
Jari Aalto17345e52009-02-19 22:21:29 +00002509
2510 if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
2511 {
2512 last_command_exit_value = exec_result;
Chet Ramey89a92862011-11-21 20:49:12 -05002513 save_line_number = line_number;
2514 line_number = line_number_for_err_trap;
Jari Aalto17345e52009-02-19 22:21:29 +00002515 run_error_trap ();
Chet Ramey89a92862011-11-21 20:49:12 -05002516 line_number = save_line_number;
Jari Aalto17345e52009-02-19 22:21:29 +00002517 }
2518
2519 if (ignore_return == 0 && invert == 0 && exit_immediately_on_error && exec_result != EXECUTION_SUCCESS)
2520 {
2521 last_command_exit_value = exec_result;
2522 run_pending_traps ();
2523 jump_to_top_level (ERREXIT);
2524 }
2525
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002526 break;
2527
2528 case AND_AND:
2529 case OR_OR:
2530 if (asynchronous)
2531 {
2532 /* If we have something like `a && b &' or `a || b &', run the
2533 && or || stuff in a subshell. Force a subshell and just call
2534 execute_command_internal again. Leave asynchronous on
2535 so that we get a report from the parent shell about the
2536 background job. */
2537 command->flags |= CMD_FORCE_SUBSHELL;
2538 exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close);
2539 break;
2540 }
2541
2542 /* Execute the first command. If the result of that is successful
2543 and the connector is AND_AND, or the result is not successful
2544 and the connector is OR_OR, then execute the second command,
2545 otherwise return. */
2546
Jari Aalto31859422009-01-12 13:36:28 +00002547 executing_list++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002548 if (command->value.Connection->first)
2549 command->value.Connection->first->flags |= CMD_IGNORE_RETURN;
2550
2551 exec_result = execute_command (command->value.Connection->first);
2552 QUIT;
2553 if (((command->value.Connection->connector == AND_AND) &&
2554 (exec_result == EXECUTION_SUCCESS)) ||
2555 ((command->value.Connection->connector == OR_OR) &&
2556 (exec_result != EXECUTION_SUCCESS)))
2557 {
2558 if (ignore_return && command->value.Connection->second)
2559 command->value.Connection->second->flags |= CMD_IGNORE_RETURN;
2560
2561 exec_result = execute_command (command->value.Connection->second);
2562 }
Jari Aalto31859422009-01-12 13:36:28 +00002563 executing_list--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002564 break;
2565
2566 default:
Jari Aaltob72432f1999-02-19 17:11:39 +00002567 command_error ("execute_connection", CMDERR_BADCONN, command->value.Connection->connector, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002568 jump_to_top_level (DISCARD);
2569 exec_result = EXECUTION_FAILURE;
2570 }
2571
2572 return exec_result;
2573}
2574
Jari Aaltobb706242000-03-17 21:46:59 +00002575#define REAP() \
2576 do \
2577 { \
2578 if (!interactive_shell) \
2579 reap_dead_jobs (); \
2580 } \
2581 while (0)
Jari Aalto726f6381996-08-26 18:22:31 +00002582
2583/* Execute a FOR command. The syntax is: FOR word_desc IN word_list;
2584 DO command; DONE */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002585static int
Jari Aalto726f6381996-08-26 18:22:31 +00002586execute_for_command (for_command)
2587 FOR_COM *for_command;
2588{
Jari Aalto726f6381996-08-26 18:22:31 +00002589 register WORD_LIST *releaser, *list;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002590 SHELL_VAR *v;
Jari Aalto726f6381996-08-26 18:22:31 +00002591 char *identifier;
Jari Aaltob80f6442004-07-27 13:29:18 +00002592 int retval, save_line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002593#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00002594 SHELL_VAR *old_value = (SHELL_VAR *)NULL; /* Remember the old value of x. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002595#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002596
Jari Aaltob80f6442004-07-27 13:29:18 +00002597 save_line_number = line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002598 if (check_identifier (for_command->name, 1) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002599 {
2600 if (posixly_correct && interactive_shell == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00002601 {
Jari Aalto31859422009-01-12 13:36:28 +00002602 last_command_exit_value = EX_BADUSAGE;
Jari Aaltob80f6442004-07-27 13:29:18 +00002603 jump_to_top_level (ERREXIT);
Jari Aaltocce855b1998-04-17 19:52:44 +00002604 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002605 return (EXECUTION_FAILURE);
2606 }
Jari Aalto726f6381996-08-26 18:22:31 +00002607
2608 loop_level++;
2609 identifier = for_command->name->word;
2610
Chet Rameyac50fba2014-02-26 09:36:43 -05002611 line_number = for_command->line; /* for expansion error messages */
Jari Aalto726f6381996-08-26 18:22:31 +00002612 list = releaser = expand_words_no_vars (for_command->map_list);
2613
2614 begin_unwind_frame ("for");
2615 add_unwind_protect (dispose_words, releaser);
2616
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002617#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00002618 if (lexical_scoping)
2619 {
2620 old_value = copy_variable (find_variable (identifier));
2621 if (old_value)
2622 add_unwind_protect (dispose_variable, old_value);
2623 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002624#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002625
2626 if (for_command->flags & CMD_IGNORE_RETURN)
2627 for_command->action->flags |= CMD_IGNORE_RETURN;
2628
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002629 for (retval = EXECUTION_SUCCESS; list; list = list->next)
Jari Aalto726f6381996-08-26 18:22:31 +00002630 {
2631 QUIT;
Jari Aaltob80f6442004-07-27 13:29:18 +00002632
2633 line_number = for_command->line;
2634
2635 /* Remember what this command looks like, for debugger. */
2636 command_string_index = 0;
2637 print_for_command_head (for_command);
2638
2639 if (echo_command_at_execute)
2640 xtrace_print_for_command_head (for_command);
2641
Jari Aalto95732b42005-12-07 14:08:12 +00002642 /* Save this command unless it's a trap command and we're not running
2643 a debug trap. */
Jari Aalto06285672006-10-10 14:15:34 +00002644 if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00002645 {
2646 FREE (the_printed_command_except_trap);
2647 the_printed_command_except_trap = savestring (the_printed_command);
2648 }
2649
2650 retval = run_debug_trap ();
2651#if defined (DEBUGGER)
2652 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
2653 skip the command. */
2654 if (debugging_mode && retval != EXECUTION_SUCCESS)
2655 continue;
2656#endif
2657
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002658 this_command_name = (char *)NULL;
Chet Rameyac50fba2014-02-26 09:36:43 -05002659 /* XXX - special ksh93 for command index variable handling */
2660 v = find_variable_last_nameref (identifier);
2661 if (v && nameref_p (v))
2662 {
2663 v = bind_variable_value (v, list->word->word, 0);
2664 }
2665 else
2666 v = bind_variable (identifier, list->word->word, 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002667 if (readonly_p (v) || noassign_p (v))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002668 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002669 line_number = save_line_number;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002670 if (readonly_p (v) && interactive_shell == 0 && posixly_correct)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002671 {
2672 last_command_exit_value = EXECUTION_FAILURE;
2673 jump_to_top_level (FORCE_EOF);
2674 }
2675 else
2676 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002677 dispose_words (releaser);
2678 discard_unwind_frame ("for");
Jari Aaltod166f041997-06-05 14:59:13 +00002679 loop_level--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002680 return (EXECUTION_FAILURE);
2681 }
2682 }
2683 retval = execute_command (for_command->action);
Jari Aalto726f6381996-08-26 18:22:31 +00002684 REAP ();
2685 QUIT;
2686
2687 if (breaking)
2688 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002689 breaking--;
Jari Aalto726f6381996-08-26 18:22:31 +00002690 break;
2691 }
2692
2693 if (continuing)
2694 {
2695 continuing--;
2696 if (continuing)
2697 break;
2698 }
Jari Aalto726f6381996-08-26 18:22:31 +00002699 }
2700
2701 loop_level--;
Jari Aaltob80f6442004-07-27 13:29:18 +00002702 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002703
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002704#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00002705 if (lexical_scoping)
2706 {
2707 if (!old_value)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002708 unbind_variable (identifier);
Jari Aalto726f6381996-08-26 18:22:31 +00002709 else
2710 {
2711 SHELL_VAR *new_value;
2712
Jari Aalto95732b42005-12-07 14:08:12 +00002713 new_value = bind_variable (identifier, value_cell(old_value), 0);
Jari Aalto726f6381996-08-26 18:22:31 +00002714 new_value->attributes = old_value->attributes;
2715 dispose_variable (old_value);
2716 }
2717 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002718#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002719
2720 dispose_words (releaser);
2721 discard_unwind_frame ("for");
2722 return (retval);
2723}
2724
Jari Aaltobb706242000-03-17 21:46:59 +00002725#if defined (ARITH_FOR_COMMAND)
2726/* Execute an arithmetic for command. The syntax is
2727
2728 for (( init ; step ; test ))
2729 do
2730 body
2731 done
2732
2733 The execution should be exactly equivalent to
2734
2735 eval \(\( init \)\)
2736 while eval \(\( test \)\) ; do
2737 body;
2738 eval \(\( step \)\)
2739 done
2740*/
Jari Aalto7117c2d2002-07-17 14:10:11 +00002741static intmax_t
Jari Aaltobb706242000-03-17 21:46:59 +00002742eval_arith_for_expr (l, okp)
2743 WORD_LIST *l;
2744 int *okp;
2745{
2746 WORD_LIST *new;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002747 intmax_t expresult;
Jari Aaltob80f6442004-07-27 13:29:18 +00002748 int r;
Jari Aaltobb706242000-03-17 21:46:59 +00002749
2750 new = expand_words_no_vars (l);
Jari Aaltof73dda02001-11-13 17:56:06 +00002751 if (new)
2752 {
2753 if (echo_command_at_execute)
2754 xtrace_print_arith_cmd (new);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002755 this_command_name = "(("; /* )) for expression error messages */
Jari Aaltob80f6442004-07-27 13:29:18 +00002756
2757 command_string_index = 0;
2758 print_arith_command (new);
Jari Aalto95732b42005-12-07 14:08:12 +00002759 if (signal_in_progress (DEBUG_TRAP) == 0)
2760 {
2761 FREE (the_printed_command_except_trap);
2762 the_printed_command_except_trap = savestring (the_printed_command);
2763 }
Jari Aaltob80f6442004-07-27 13:29:18 +00002764
2765 r = run_debug_trap ();
2766 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
2767 skip the command. */
2768#if defined (DEBUGGER)
2769 if (debugging_mode == 0 || r == EXECUTION_SUCCESS)
2770 expresult = evalexp (new->word->word, okp);
2771 else
2772 {
2773 expresult = 0;
2774 if (okp)
2775 *okp = 1;
2776 }
2777#else
Jari Aaltof73dda02001-11-13 17:56:06 +00002778 expresult = evalexp (new->word->word, okp);
Jari Aaltob80f6442004-07-27 13:29:18 +00002779#endif
Jari Aaltof73dda02001-11-13 17:56:06 +00002780 dispose_words (new);
2781 }
2782 else
2783 {
2784 expresult = 0;
2785 if (okp)
2786 *okp = 1;
2787 }
Jari Aaltobb706242000-03-17 21:46:59 +00002788 return (expresult);
2789}
2790
2791static int
2792execute_arith_for_command (arith_for_command)
2793 ARITH_FOR_COM *arith_for_command;
2794{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002795 intmax_t expresult;
2796 int expok, body_status, arith_lineno, save_lineno;
Jari Aaltobb706242000-03-17 21:46:59 +00002797
2798 body_status = EXECUTION_SUCCESS;
2799 loop_level++;
Jari Aaltob80f6442004-07-27 13:29:18 +00002800 save_lineno = line_number;
Jari Aaltobb706242000-03-17 21:46:59 +00002801
2802 if (arith_for_command->flags & CMD_IGNORE_RETURN)
2803 arith_for_command->action->flags |= CMD_IGNORE_RETURN;
2804
2805 this_command_name = "(("; /* )) for expression error messages */
2806
Jari Aalto7117c2d2002-07-17 14:10:11 +00002807 /* save the starting line number of the command so we can reset
2808 line_number before executing each expression -- for $LINENO
2809 and the DEBUG trap. */
Jari Aaltob80f6442004-07-27 13:29:18 +00002810 line_number = arith_lineno = arith_for_command->line;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002811 if (variable_context && interactive_shell)
Jari Aaltob80f6442004-07-27 13:29:18 +00002812 line_number -= function_line_number;
Jari Aaltobb706242000-03-17 21:46:59 +00002813
2814 /* Evaluate the initialization expression. */
2815 expresult = eval_arith_for_expr (arith_for_command->init, &expok);
2816 if (expok == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00002817 {
2818 line_number = save_lineno;
2819 return (EXECUTION_FAILURE);
2820 }
Jari Aaltobb706242000-03-17 21:46:59 +00002821
2822 while (1)
2823 {
2824 /* Evaluate the test expression. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002825 line_number = arith_lineno;
Jari Aaltobb706242000-03-17 21:46:59 +00002826 expresult = eval_arith_for_expr (arith_for_command->test, &expok);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002827 line_number = save_lineno;
2828
Jari Aaltobb706242000-03-17 21:46:59 +00002829 if (expok == 0)
2830 {
2831 body_status = EXECUTION_FAILURE;
2832 break;
2833 }
2834 REAP ();
2835 if (expresult == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002836 break;
Jari Aaltobb706242000-03-17 21:46:59 +00002837
2838 /* Execute the body of the arithmetic for command. */
2839 QUIT;
2840 body_status = execute_command (arith_for_command->action);
2841 QUIT;
2842
2843 /* Handle any `break' or `continue' commands executed by the body. */
2844 if (breaking)
2845 {
2846 breaking--;
2847 break;
2848 }
2849
2850 if (continuing)
2851 {
2852 continuing--;
2853 if (continuing)
2854 break;
2855 }
2856
2857 /* Evaluate the step expression. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002858 line_number = arith_lineno;
Jari Aaltobb706242000-03-17 21:46:59 +00002859 expresult = eval_arith_for_expr (arith_for_command->step, &expok);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002860 line_number = save_lineno;
2861
Jari Aaltobb706242000-03-17 21:46:59 +00002862 if (expok == 0)
2863 {
2864 body_status = EXECUTION_FAILURE;
2865 break;
2866 }
2867 }
2868
2869 loop_level--;
Jari Aaltob80f6442004-07-27 13:29:18 +00002870 line_number = save_lineno;
2871
Jari Aaltobb706242000-03-17 21:46:59 +00002872 return (body_status);
2873}
2874#endif
2875
Jari Aalto726f6381996-08-26 18:22:31 +00002876#if defined (SELECT_COMMAND)
2877static int LINES, COLS, tabsize;
2878
2879#define RP_SPACE ") "
2880#define RP_SPACE_LEN 2
2881
2882/* XXX - does not handle numbers > 1000000 at all. */
2883#define NUMBER_LEN(s) \
2884((s < 10) ? 1 \
2885 : ((s < 100) ? 2 \
2886 : ((s < 1000) ? 3 \
2887 : ((s < 10000) ? 4 \
2888 : ((s < 100000) ? 5 \
2889 : 6)))))
2890
2891static int
Chet Ramey495aee42011-11-22 19:11:26 -05002892displen (s)
2893 const char *s;
2894{
2895#if defined (HANDLE_MULTIBYTE)
2896 wchar_t *wcstr;
Chet Rameyac50fba2014-02-26 09:36:43 -05002897 size_t slen;
2898 int wclen;
Chet Ramey495aee42011-11-22 19:11:26 -05002899
2900 wcstr = 0;
2901 slen = mbstowcs (wcstr, s, 0);
2902 if (slen == -1)
2903 slen = 0;
2904 wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (slen + 1));
2905 mbstowcs (wcstr, s, slen + 1);
2906 wclen = wcswidth (wcstr, slen);
2907 free (wcstr);
Chet Rameyac50fba2014-02-26 09:36:43 -05002908 return (wclen < 0 ? STRLEN(s) : wclen);
Chet Ramey495aee42011-11-22 19:11:26 -05002909#else
2910 return (STRLEN (s));
2911#endif
2912}
2913
2914static int
Jari Aalto726f6381996-08-26 18:22:31 +00002915print_index_and_element (len, ind, list)
2916 int len, ind;
2917 WORD_LIST *list;
2918{
2919 register WORD_LIST *l;
2920 register int i;
2921
2922 if (list == 0)
2923 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002924 for (i = ind, l = list; l && --i; l = l->next)
2925 ;
Chet Rameyac50fba2014-02-26 09:36:43 -05002926 if (l == 0) /* don't think this can happen */
2927 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +00002928 fprintf (stderr, "%*d%s%s", len, ind, RP_SPACE, l->word->word);
Chet Ramey495aee42011-11-22 19:11:26 -05002929 return (displen (l->word->word));
Jari Aalto726f6381996-08-26 18:22:31 +00002930}
2931
2932static void
2933indent (from, to)
2934 int from, to;
2935{
2936 while (from < to)
2937 {
2938 if ((to / tabsize) > (from / tabsize))
2939 {
2940 putc ('\t', stderr);
2941 from += tabsize - from % tabsize;
2942 }
2943 else
2944 {
2945 putc (' ', stderr);
2946 from++;
2947 }
2948 }
2949}
2950
2951static void
2952print_select_list (list, list_len, max_elem_len, indices_len)
2953 WORD_LIST *list;
2954 int list_len, max_elem_len, indices_len;
2955{
2956 int ind, row, elem_len, pos, cols, rows;
2957 int first_column_indices_len, other_indices_len;
2958
2959 if (list == 0)
2960 {
2961 putc ('\n', stderr);
2962 return;
2963 }
2964
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002965 cols = max_elem_len ? COLS / max_elem_len : 1;
Jari Aalto726f6381996-08-26 18:22:31 +00002966 if (cols == 0)
2967 cols = 1;
2968 rows = list_len ? list_len / cols + (list_len % cols != 0) : 1;
2969 cols = list_len ? list_len / rows + (list_len % rows != 0) : 1;
2970
2971 if (rows == 1)
2972 {
2973 rows = cols;
2974 cols = 1;
2975 }
2976
2977 first_column_indices_len = NUMBER_LEN (rows);
2978 other_indices_len = indices_len;
2979
2980 for (row = 0; row < rows; row++)
2981 {
2982 ind = row;
2983 pos = 0;
2984 while (1)
2985 {
2986 indices_len = (pos == 0) ? first_column_indices_len : other_indices_len;
2987 elem_len = print_index_and_element (indices_len, ind + 1, list);
2988 elem_len += indices_len + RP_SPACE_LEN;
2989 ind += rows;
2990 if (ind >= list_len)
2991 break;
2992 indent (pos + elem_len, pos + max_elem_len);
2993 pos += max_elem_len;
2994 }
2995 putc ('\n', stderr);
2996 }
2997}
2998
2999/* Print the elements of LIST, one per line, preceded by an index from 1 to
3000 LIST_LEN. Then display PROMPT and wait for the user to enter a number.
3001 If the number is between 1 and LIST_LEN, return that selection. If EOF
Jari Aaltoe8ce7751997-09-22 20:22:27 +00003002 is read, return a null string. If a blank line is entered, or an invalid
3003 number is entered, the loop is executed again. */
Jari Aalto726f6381996-08-26 18:22:31 +00003004static char *
Jari Aalto7117c2d2002-07-17 14:10:11 +00003005select_query (list, list_len, prompt, print_menu)
Jari Aalto726f6381996-08-26 18:22:31 +00003006 WORD_LIST *list;
3007 int list_len;
3008 char *prompt;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003009 int print_menu;
Jari Aalto726f6381996-08-26 18:22:31 +00003010{
Jari Aaltoe8ce7751997-09-22 20:22:27 +00003011 int max_elem_len, indices_len, len;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003012 intmax_t reply;
Jari Aalto726f6381996-08-26 18:22:31 +00003013 WORD_LIST *l;
3014 char *repl_string, *t;
3015
Chet Ramey495aee42011-11-22 19:11:26 -05003016#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00003017 t = get_string_value ("LINES");
3018 LINES = (t && *t) ? atoi (t) : 24;
Chet Ramey495aee42011-11-22 19:11:26 -05003019#endif
Jari Aalto726f6381996-08-26 18:22:31 +00003020 t = get_string_value ("COLUMNS");
3021 COLS = (t && *t) ? atoi (t) : 80;
3022
3023#if 0
3024 t = get_string_value ("TABSIZE");
3025 tabsize = (t && *t) ? atoi (t) : 8;
3026 if (tabsize <= 0)
3027 tabsize = 8;
3028#else
3029 tabsize = 8;
3030#endif
3031
3032 max_elem_len = 0;
3033 for (l = list; l; l = l->next)
3034 {
Chet Ramey495aee42011-11-22 19:11:26 -05003035 len = displen (l->word->word);
Jari Aalto726f6381996-08-26 18:22:31 +00003036 if (len > max_elem_len)
Jari Aaltocce855b1998-04-17 19:52:44 +00003037 max_elem_len = len;
Jari Aalto726f6381996-08-26 18:22:31 +00003038 }
3039 indices_len = NUMBER_LEN (list_len);
3040 max_elem_len += indices_len + RP_SPACE_LEN + 2;
3041
3042 while (1)
3043 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00003044 if (print_menu)
3045 print_select_list (list, list_len, max_elem_len, indices_len);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003046 fprintf (stderr, "%s", prompt);
3047 fflush (stderr);
Jari Aalto726f6381996-08-26 18:22:31 +00003048 QUIT;
3049
Chet Ramey495aee42011-11-22 19:11:26 -05003050 if (read_builtin ((WORD_LIST *)NULL) != EXECUTION_SUCCESS)
Jari Aalto726f6381996-08-26 18:22:31 +00003051 {
3052 putchar ('\n');
3053 return ((char *)NULL);
3054 }
3055 repl_string = get_string_value ("REPLY");
3056 if (*repl_string == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +00003057 {
3058 print_menu = 1;
3059 continue;
3060 }
Jari Aaltoe8ce7751997-09-22 20:22:27 +00003061 if (legal_number (repl_string, &reply) == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00003062 return "";
Jari Aalto726f6381996-08-26 18:22:31 +00003063 if (reply < 1 || reply > list_len)
3064 return "";
3065
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003066 for (l = list; l && --reply; l = l->next)
3067 ;
Chet Rameyac50fba2014-02-26 09:36:43 -05003068 return (l->word->word); /* XXX - can't be null? */
Jari Aalto726f6381996-08-26 18:22:31 +00003069 }
3070}
3071
3072/* Execute a SELECT command. The syntax is:
3073 SELECT word IN list DO command_list DONE
3074 Only `break' or `return' in command_list will terminate
3075 the command. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003076static int
Jari Aalto726f6381996-08-26 18:22:31 +00003077execute_select_command (select_command)
3078 SELECT_COM *select_command;
3079{
3080 WORD_LIST *releaser, *list;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003081 SHELL_VAR *v;
Jari Aalto726f6381996-08-26 18:22:31 +00003082 char *identifier, *ps3_prompt, *selection;
Jari Aaltob80f6442004-07-27 13:29:18 +00003083 int retval, list_len, show_menu, save_line_number;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003084
Jari Aalto726f6381996-08-26 18:22:31 +00003085 if (check_identifier (select_command->name, 1) == 0)
3086 return (EXECUTION_FAILURE);
3087
Jari Aaltob80f6442004-07-27 13:29:18 +00003088 save_line_number = line_number;
3089 line_number = select_command->line;
3090
3091 command_string_index = 0;
3092 print_select_command_head (select_command);
3093
3094 if (echo_command_at_execute)
3095 xtrace_print_select_command_head (select_command);
3096
Jari Aalto06285672006-10-10 14:15:34 +00003097#if 0
Jari Aalto95732b42005-12-07 14:08:12 +00003098 if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0)))
Jari Aalto06285672006-10-10 14:15:34 +00003099#else
3100 if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0)
3101#endif
Jari Aalto95732b42005-12-07 14:08:12 +00003102 {
3103 FREE (the_printed_command_except_trap);
3104 the_printed_command_except_trap = savestring (the_printed_command);
3105 }
Jari Aaltob80f6442004-07-27 13:29:18 +00003106
3107 retval = run_debug_trap ();
3108#if defined (DEBUGGER)
3109 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
3110 skip the command. */
3111 if (debugging_mode && retval != EXECUTION_SUCCESS)
3112 return (EXECUTION_SUCCESS);
3113#endif
3114
Jari Aalto726f6381996-08-26 18:22:31 +00003115 loop_level++;
3116 identifier = select_command->name->word;
3117
3118 /* command and arithmetic substitution, parameter and variable expansion,
3119 word splitting, pathname expansion, and quote removal. */
3120 list = releaser = expand_words_no_vars (select_command->map_list);
3121 list_len = list_length (list);
3122 if (list == 0 || list_len == 0)
3123 {
3124 if (list)
3125 dispose_words (list);
Jari Aaltob80f6442004-07-27 13:29:18 +00003126 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00003127 return (EXECUTION_SUCCESS);
3128 }
3129
3130 begin_unwind_frame ("select");
3131 add_unwind_protect (dispose_words, releaser);
3132
Jari Aalto726f6381996-08-26 18:22:31 +00003133 if (select_command->flags & CMD_IGNORE_RETURN)
3134 select_command->action->flags |= CMD_IGNORE_RETURN;
3135
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003136 retval = EXECUTION_SUCCESS;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003137 show_menu = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003138
Jari Aalto726f6381996-08-26 18:22:31 +00003139 while (1)
3140 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003141 line_number = select_command->line;
Jari Aalto726f6381996-08-26 18:22:31 +00003142 ps3_prompt = get_string_value ("PS3");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003143 if (ps3_prompt == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003144 ps3_prompt = "#? ";
3145
3146 QUIT;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003147 selection = select_query (list, list_len, ps3_prompt, show_menu);
Jari Aalto726f6381996-08-26 18:22:31 +00003148 QUIT;
3149 if (selection == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +00003150 {
3151 /* select_query returns EXECUTION_FAILURE if the read builtin
3152 fails, so we want to return failure in this case. */
3153 retval = EXECUTION_FAILURE;
3154 break;
3155 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003156
Jari Aalto95732b42005-12-07 14:08:12 +00003157 v = bind_variable (identifier, selection, 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +00003158 if (readonly_p (v) || noassign_p (v))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003159 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00003160 if (readonly_p (v) && interactive_shell == 0 && posixly_correct)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003161 {
3162 last_command_exit_value = EXECUTION_FAILURE;
3163 jump_to_top_level (FORCE_EOF);
3164 }
3165 else
3166 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003167 dispose_words (releaser);
3168 discard_unwind_frame ("select");
3169 loop_level--;
3170 line_number = save_line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003171 return (EXECUTION_FAILURE);
3172 }
3173 }
Jari Aalto726f6381996-08-26 18:22:31 +00003174
Jari Aaltof73dda02001-11-13 17:56:06 +00003175 retval = execute_command (select_command->action);
Jari Aalto726f6381996-08-26 18:22:31 +00003176
3177 REAP ();
3178 QUIT;
3179
3180 if (breaking)
3181 {
3182 breaking--;
3183 break;
3184 }
Jari Aaltobb706242000-03-17 21:46:59 +00003185
3186 if (continuing)
3187 {
3188 continuing--;
3189 if (continuing)
3190 break;
3191 }
Jari Aalto7117c2d2002-07-17 14:10:11 +00003192
3193#if defined (KSH_COMPATIBLE_SELECT)
3194 show_menu = 0;
3195 selection = get_string_value ("REPLY");
3196 if (selection && *selection == '\0')
3197 show_menu = 1;
3198#endif
Jari Aalto726f6381996-08-26 18:22:31 +00003199 }
3200
3201 loop_level--;
Jari Aaltob80f6442004-07-27 13:29:18 +00003202 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00003203
Jari Aaltob80f6442004-07-27 13:29:18 +00003204 dispose_words (releaser);
3205 discard_unwind_frame ("select");
Jari Aalto726f6381996-08-26 18:22:31 +00003206 return (retval);
3207}
3208#endif /* SELECT_COMMAND */
3209
3210/* Execute a CASE command. The syntax is: CASE word_desc IN pattern_list ESAC.
3211 The pattern_list is a linked list of pattern clauses; each clause contains
3212 some patterns to compare word_desc against, and an associated command to
3213 execute. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003214static int
Jari Aalto726f6381996-08-26 18:22:31 +00003215execute_case_command (case_command)
3216 CASE_COM *case_command;
3217{
3218 register WORD_LIST *list;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003219 WORD_LIST *wlist, *es;
Jari Aalto726f6381996-08-26 18:22:31 +00003220 PATTERN_LIST *clauses;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003221 char *word, *pattern;
Jari Aaltob80f6442004-07-27 13:29:18 +00003222 int retval, match, ignore_return, save_line_number;
3223
3224 save_line_number = line_number;
3225 line_number = case_command->line;
3226
3227 command_string_index = 0;
3228 print_case_command_head (case_command);
3229
3230 if (echo_command_at_execute)
3231 xtrace_print_case_command_head (case_command);
3232
Jari Aalto06285672006-10-10 14:15:34 +00003233#if 0
3234 if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0)))
3235#else
3236 if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0)
3237#endif
Jari Aaltob80f6442004-07-27 13:29:18 +00003238 {
3239 FREE (the_printed_command_except_trap);
3240 the_printed_command_except_trap = savestring (the_printed_command);
3241 }
3242
3243 retval = run_debug_trap();
3244#if defined (DEBUGGER)
3245 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
3246 skip the command. */
3247 if (debugging_mode && retval != EXECUTION_SUCCESS)
3248 {
3249 line_number = save_line_number;
3250 return (EXECUTION_SUCCESS);
3251 }
3252#endif
Jari Aalto726f6381996-08-26 18:22:31 +00003253
Jari Aalto28ef6c32001-04-06 19:14:31 +00003254 wlist = expand_word_unsplit (case_command->word, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003255 word = wlist ? string_list (wlist) : savestring ("");
3256 dispose_words (wlist);
3257
Jari Aalto726f6381996-08-26 18:22:31 +00003258 retval = EXECUTION_SUCCESS;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003259 ignore_return = case_command->flags & CMD_IGNORE_RETURN;
Jari Aalto726f6381996-08-26 18:22:31 +00003260
3261 begin_unwind_frame ("case");
Jari Aalto726f6381996-08-26 18:22:31 +00003262 add_unwind_protect ((Function *)xfree, word);
3263
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003264#define EXIT_CASE() goto exit_case_command
3265
3266 for (clauses = case_command->clauses; clauses; clauses = clauses->next)
Jari Aalto726f6381996-08-26 18:22:31 +00003267 {
3268 QUIT;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003269 for (list = clauses->patterns; list; list = list->next)
Jari Aalto726f6381996-08-26 18:22:31 +00003270 {
Jari Aalto726f6381996-08-26 18:22:31 +00003271 es = expand_word_leave_quoted (list->word, 0);
3272
3273 if (es && es->word && es->word->word && *(es->word->word))
Jari Aaltocce855b1998-04-17 19:52:44 +00003274 pattern = quote_string_for_globbing (es->word->word, QGLOB_CVTNULL);
Jari Aalto726f6381996-08-26 18:22:31 +00003275 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003276 {
Jari Aaltof73dda02001-11-13 17:56:06 +00003277 pattern = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003278 pattern[0] = '\0';
3279 }
Jari Aalto726f6381996-08-26 18:22:31 +00003280
3281 /* Since the pattern does not undergo quote removal (as per
Jari Aaltof73dda02001-11-13 17:56:06 +00003282 Posix.2, section 3.9.4.3), the strmatch () call must be able
Jari Aalto726f6381996-08-26 18:22:31 +00003283 to recognize backslashes as escape characters. */
Jari Aalto95732b42005-12-07 14:08:12 +00003284 match = strmatch (pattern, word, FNMATCH_EXTFLAG|FNMATCH_IGNCASE) != FNM_NOMATCH;
Jari Aalto726f6381996-08-26 18:22:31 +00003285 free (pattern);
3286
3287 dispose_words (es);
3288
3289 if (match)
3290 {
Jari Aalto31859422009-01-12 13:36:28 +00003291 do
3292 {
3293 if (clauses->action && ignore_return)
3294 clauses->action->flags |= CMD_IGNORE_RETURN;
3295 retval = execute_command (clauses->action);
3296 }
3297 while ((clauses->flags & CASEPAT_FALLTHROUGH) && (clauses = clauses->next));
Chet Ramey89a92862011-11-21 20:49:12 -05003298 if (clauses == 0 || (clauses->flags & CASEPAT_TESTNEXT) == 0)
Jari Aalto31859422009-01-12 13:36:28 +00003299 EXIT_CASE ();
3300 else
3301 break;
Jari Aalto726f6381996-08-26 18:22:31 +00003302 }
3303
Jari Aalto726f6381996-08-26 18:22:31 +00003304 QUIT;
3305 }
Jari Aalto726f6381996-08-26 18:22:31 +00003306 }
3307
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003308exit_case_command:
3309 free (word);
Jari Aalto726f6381996-08-26 18:22:31 +00003310 discard_unwind_frame ("case");
Jari Aaltob80f6442004-07-27 13:29:18 +00003311 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00003312 return (retval);
3313}
3314
3315#define CMD_WHILE 0
3316#define CMD_UNTIL 1
3317
3318/* The WHILE command. Syntax: WHILE test DO action; DONE.
3319 Repeatedly execute action while executing test produces
3320 EXECUTION_SUCCESS. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003321static int
Jari Aalto726f6381996-08-26 18:22:31 +00003322execute_while_command (while_command)
3323 WHILE_COM *while_command;
3324{
3325 return (execute_while_or_until (while_command, CMD_WHILE));
3326}
3327
3328/* UNTIL is just like WHILE except that the test result is negated. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003329static int
Jari Aalto726f6381996-08-26 18:22:31 +00003330execute_until_command (while_command)
3331 WHILE_COM *while_command;
3332{
3333 return (execute_while_or_until (while_command, CMD_UNTIL));
3334}
3335
3336/* The body for both while and until. The only difference between the
3337 two is that the test value is treated differently. TYPE is
3338 CMD_WHILE or CMD_UNTIL. The return value for both commands should
3339 be EXECUTION_SUCCESS if no commands in the body are executed, and
3340 the status of the last command executed in the body otherwise. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003341static int
Jari Aalto726f6381996-08-26 18:22:31 +00003342execute_while_or_until (while_command, type)
3343 WHILE_COM *while_command;
3344 int type;
3345{
3346 int return_value, body_status;
3347
3348 body_status = EXECUTION_SUCCESS;
3349 loop_level++;
3350
3351 while_command->test->flags |= CMD_IGNORE_RETURN;
3352 if (while_command->flags & CMD_IGNORE_RETURN)
3353 while_command->action->flags |= CMD_IGNORE_RETURN;
3354
3355 while (1)
3356 {
3357 return_value = execute_command (while_command->test);
3358 REAP ();
3359
Jari Aaltof73dda02001-11-13 17:56:06 +00003360 /* Need to handle `break' in the test when we would break out of the
3361 loop. The job control code will set `breaking' to loop_level
3362 when a job in a loop is stopped with SIGTSTP. If the stopped job
3363 is in the loop test, `breaking' will not be reset unless we do
3364 this, and the shell will cease to execute commands. */
Jari Aalto726f6381996-08-26 18:22:31 +00003365 if (type == CMD_WHILE && return_value != EXECUTION_SUCCESS)
Jari Aaltof73dda02001-11-13 17:56:06 +00003366 {
3367 if (breaking)
3368 breaking--;
3369 break;
3370 }
Jari Aalto726f6381996-08-26 18:22:31 +00003371 if (type == CMD_UNTIL && return_value == EXECUTION_SUCCESS)
Jari Aaltof73dda02001-11-13 17:56:06 +00003372 {
3373 if (breaking)
3374 breaking--;
3375 break;
3376 }
Jari Aalto726f6381996-08-26 18:22:31 +00003377
3378 QUIT;
3379 body_status = execute_command (while_command->action);
3380 QUIT;
3381
3382 if (breaking)
3383 {
3384 breaking--;
3385 break;
3386 }
3387
3388 if (continuing)
3389 {
3390 continuing--;
3391 if (continuing)
3392 break;
3393 }
3394 }
3395 loop_level--;
3396
3397 return (body_status);
3398}
3399
3400/* IF test THEN command [ELSE command].
3401 IF also allows ELIF in the place of ELSE IF, but
3402 the parser makes *that* stupidity transparent. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003403static int
Jari Aalto726f6381996-08-26 18:22:31 +00003404execute_if_command (if_command)
3405 IF_COM *if_command;
3406{
Jari Aaltob80f6442004-07-27 13:29:18 +00003407 int return_value, save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00003408
Jari Aaltob80f6442004-07-27 13:29:18 +00003409 save_line_number = line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00003410 if_command->test->flags |= CMD_IGNORE_RETURN;
3411 return_value = execute_command (if_command->test);
Jari Aaltob80f6442004-07-27 13:29:18 +00003412 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00003413
3414 if (return_value == EXECUTION_SUCCESS)
3415 {
3416 QUIT;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003417
Jari Aalto726f6381996-08-26 18:22:31 +00003418 if (if_command->true_case && (if_command->flags & CMD_IGNORE_RETURN))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003419 if_command->true_case->flags |= CMD_IGNORE_RETURN;
3420
Jari Aalto726f6381996-08-26 18:22:31 +00003421 return (execute_command (if_command->true_case));
3422 }
3423 else
3424 {
3425 QUIT;
3426
3427 if (if_command->false_case && (if_command->flags & CMD_IGNORE_RETURN))
3428 if_command->false_case->flags |= CMD_IGNORE_RETURN;
3429
3430 return (execute_command (if_command->false_case));
3431 }
3432}
3433
Jari Aaltocce855b1998-04-17 19:52:44 +00003434#if defined (DPAREN_ARITHMETIC)
3435static int
3436execute_arith_command (arith_command)
3437 ARITH_COM *arith_command;
3438{
Jari Aaltob80f6442004-07-27 13:29:18 +00003439 int expok, save_line_number, retval;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003440 intmax_t expresult;
Jari Aaltof73dda02001-11-13 17:56:06 +00003441 WORD_LIST *new;
Jari Aalto06285672006-10-10 14:15:34 +00003442 char *exp;
Jari Aaltocce855b1998-04-17 19:52:44 +00003443
Jari Aaltof73dda02001-11-13 17:56:06 +00003444 expresult = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +00003445
Jari Aaltob80f6442004-07-27 13:29:18 +00003446 save_line_number = line_number;
Jari Aaltob72432f1999-02-19 17:11:39 +00003447 this_command_name = "(("; /* )) */
Jari Aaltob80f6442004-07-27 13:29:18 +00003448 line_number = arith_command->line;
Jari Aaltocce855b1998-04-17 19:52:44 +00003449 /* If we're in a function, update the line number information. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00003450 if (variable_context && interactive_shell)
Jari Aaltob80f6442004-07-27 13:29:18 +00003451 line_number -= function_line_number;
3452
3453 command_string_index = 0;
3454 print_arith_command (arith_command->exp);
Jari Aalto95732b42005-12-07 14:08:12 +00003455
3456 if (signal_in_progress (DEBUG_TRAP) == 0)
3457 {
3458 FREE (the_printed_command_except_trap);
3459 the_printed_command_except_trap = savestring (the_printed_command);
3460 }
Jari Aaltocce855b1998-04-17 19:52:44 +00003461
Jari Aalto7117c2d2002-07-17 14:10:11 +00003462 /* Run the debug trap before each arithmetic command, but do it after we
3463 update the line number information and before we expand the various
3464 words in the expression. */
Jari Aaltob80f6442004-07-27 13:29:18 +00003465 retval = run_debug_trap ();
3466#if defined (DEBUGGER)
3467 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
3468 skip the command. */
3469 if (debugging_mode && retval != EXECUTION_SUCCESS)
3470 {
3471 line_number = save_line_number;
3472 return (EXECUTION_SUCCESS);
3473 }
3474#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +00003475
Jari Aaltob80f6442004-07-27 13:29:18 +00003476 new = expand_words_no_vars (arith_command->exp);
Jari Aaltocce855b1998-04-17 19:52:44 +00003477
3478 /* If we're tracing, make a new word list with `((' at the front and `))'
3479 at the back and print it. */
3480 if (echo_command_at_execute)
3481 xtrace_print_arith_cmd (new);
3482
Jari Aaltob80f6442004-07-27 13:29:18 +00003483 if (new)
3484 {
Jari Aalto06285672006-10-10 14:15:34 +00003485 exp = new->next ? string_list (new) : new->word->word;
3486 expresult = evalexp (exp, &expok);
Jari Aaltob80f6442004-07-27 13:29:18 +00003487 line_number = save_line_number;
Jari Aalto06285672006-10-10 14:15:34 +00003488 if (exp != new->word->word)
3489 free (exp);
Jari Aaltob80f6442004-07-27 13:29:18 +00003490 dispose_words (new);
3491 }
3492 else
3493 {
3494 expresult = 0;
3495 expok = 1;
3496 }
Jari Aaltocce855b1998-04-17 19:52:44 +00003497
3498 if (expok == 0)
3499 return (EXECUTION_FAILURE);
3500
Jari Aaltof73dda02001-11-13 17:56:06 +00003501 return (expresult == 0 ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
Jari Aaltocce855b1998-04-17 19:52:44 +00003502}
3503#endif /* DPAREN_ARITHMETIC */
3504
3505#if defined (COND_COMMAND)
3506
Jari Aalto31859422009-01-12 13:36:28 +00003507static char * const nullstr = "";
Jari Aaltocce855b1998-04-17 19:52:44 +00003508
Chet Rameyac50fba2014-02-26 09:36:43 -05003509/* XXX - can COND ever be NULL when this is called? */
Jari Aaltocce855b1998-04-17 19:52:44 +00003510static int
3511execute_cond_node (cond)
3512 COND_COM *cond;
3513{
Chet Ramey00018032011-11-21 20:51:19 -05003514 int result, invert, patmatch, rmatch, mflags, ignore;
Jari Aaltof73dda02001-11-13 17:56:06 +00003515 char *arg1, *arg2;
Chet Rameyac50fba2014-02-26 09:36:43 -05003516#if 0
3517 char *t1, *t2;
3518#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00003519
3520 invert = (cond->flags & CMD_INVERT_RETURN);
Chet Ramey00018032011-11-21 20:51:19 -05003521 ignore = (cond->flags & CMD_IGNORE_RETURN);
3522 if (ignore)
3523 {
3524 if (cond->left)
3525 cond->left->flags |= CMD_IGNORE_RETURN;
3526 if (cond->right)
3527 cond->right->flags |= CMD_IGNORE_RETURN;
3528 }
3529
Jari Aaltocce855b1998-04-17 19:52:44 +00003530 if (cond->type == COND_EXPR)
3531 result = execute_cond_node (cond->left);
3532 else if (cond->type == COND_OR)
3533 {
3534 result = execute_cond_node (cond->left);
3535 if (result != EXECUTION_SUCCESS)
3536 result = execute_cond_node (cond->right);
3537 }
3538 else if (cond->type == COND_AND)
3539 {
3540 result = execute_cond_node (cond->left);
3541 if (result == EXECUTION_SUCCESS)
3542 result = execute_cond_node (cond->right);
3543 }
3544 else if (cond->type == COND_UNARY)
3545 {
Chet Ramey00018032011-11-21 20:51:19 -05003546 if (ignore)
3547 comsub_ignore_return++;
Jari Aaltocce855b1998-04-17 19:52:44 +00003548 arg1 = cond_expand_word (cond->left->op, 0);
Chet Ramey00018032011-11-21 20:51:19 -05003549 if (ignore)
3550 comsub_ignore_return--;
Jari Aaltocce855b1998-04-17 19:52:44 +00003551 if (arg1 == 0)
3552 arg1 = nullstr;
3553 if (echo_command_at_execute)
3554 xtrace_print_cond_term (cond->type, invert, cond->op, arg1, (char *)NULL);
3555 result = unary_test (cond->op->word, arg1) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
3556 if (arg1 != nullstr)
3557 free (arg1);
3558 }
3559 else if (cond->type == COND_BINARY)
3560 {
Jari Aalto06285672006-10-10 14:15:34 +00003561 rmatch = 0;
Chet Rameyac50fba2014-02-26 09:36:43 -05003562 patmatch = (((cond->op->word[1] == '=') && (cond->op->word[2] == '\0') &&
3563 (cond->op->word[0] == '!' || cond->op->word[0] == '=')) ||
Jari Aaltob72432f1999-02-19 17:11:39 +00003564 (cond->op->word[0] == '=' && cond->op->word[1] == '\0'));
Jari Aaltob80f6442004-07-27 13:29:18 +00003565#if defined (COND_REGEXP)
3566 rmatch = (cond->op->word[0] == '=' && cond->op->word[1] == '~' &&
3567 cond->op->word[2] == '\0');
3568#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00003569
Chet Ramey00018032011-11-21 20:51:19 -05003570 if (ignore)
3571 comsub_ignore_return++;
Jari Aaltocce855b1998-04-17 19:52:44 +00003572 arg1 = cond_expand_word (cond->left->op, 0);
Chet Ramey00018032011-11-21 20:51:19 -05003573 if (ignore)
3574 comsub_ignore_return--;
Jari Aaltocce855b1998-04-17 19:52:44 +00003575 if (arg1 == 0)
3576 arg1 = nullstr;
Chet Ramey00018032011-11-21 20:51:19 -05003577 if (ignore)
3578 comsub_ignore_return++;
Jari Aaltof1be6662008-11-18 13:15:12 +00003579 arg2 = cond_expand_word (cond->right->op,
3580 (rmatch && shell_compatibility_level > 31) ? 2 : (patmatch ? 1 : 0));
Chet Ramey00018032011-11-21 20:51:19 -05003581 if (ignore)
3582 comsub_ignore_return--;
Jari Aaltocce855b1998-04-17 19:52:44 +00003583 if (arg2 == 0)
3584 arg2 = nullstr;
3585
3586 if (echo_command_at_execute)
3587 xtrace_print_cond_term (cond->type, invert, cond->op, arg1, arg2);
3588
Jari Aaltob80f6442004-07-27 13:29:18 +00003589#if defined (COND_REGEXP)
3590 if (rmatch)
3591 {
3592 mflags = SHMAT_PWARN;
3593#if defined (ARRAY_VARS)
3594 mflags |= SHMAT_SUBEXP;
3595#endif
3596
Chet Rameyac50fba2014-02-26 09:36:43 -05003597#if 0
3598 t1 = strescape(arg1);
3599 t2 = strescape(arg2);
3600 itrace("execute_cond_node: sh_regmatch on `%s' and `%s'", t1, t2);
3601 free(t1);
3602 free(t2);
3603#endif
3604
Jari Aaltob80f6442004-07-27 13:29:18 +00003605 result = sh_regmatch (arg1, arg2, mflags);
3606 }
3607 else
3608#endif /* COND_REGEXP */
Jari Aalto95732b42005-12-07 14:08:12 +00003609 {
3610 int oe;
3611 oe = extended_glob;
3612 extended_glob = 1;
Chet Ramey00018032011-11-21 20:51:19 -05003613 result = binary_test (cond->op->word, arg1, arg2, TEST_PATMATCH|TEST_ARITHEXP|TEST_LOCALE)
Jari Aalto95732b42005-12-07 14:08:12 +00003614 ? EXECUTION_SUCCESS
3615 : EXECUTION_FAILURE;
3616 extended_glob = oe;
3617 }
Jari Aaltocce855b1998-04-17 19:52:44 +00003618 if (arg1 != nullstr)
3619 free (arg1);
3620 if (arg2 != nullstr)
3621 free (arg2);
3622 }
3623 else
3624 {
Jari Aaltob72432f1999-02-19 17:11:39 +00003625 command_error ("execute_cond_node", CMDERR_BADTYPE, cond->type, 0);
Jari Aaltocce855b1998-04-17 19:52:44 +00003626 jump_to_top_level (DISCARD);
3627 result = EXECUTION_FAILURE;
3628 }
3629
3630 if (invert)
3631 result = (result == EXECUTION_SUCCESS) ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
3632
3633 return result;
3634}
3635
3636static int
3637execute_cond_command (cond_command)
3638 COND_COM *cond_command;
3639{
Jari Aaltob80f6442004-07-27 13:29:18 +00003640 int retval, save_line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +00003641
Jari Aaltob80f6442004-07-27 13:29:18 +00003642 retval = EXECUTION_SUCCESS;
3643 save_line_number = line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +00003644
3645 this_command_name = "[[";
Jari Aaltob80f6442004-07-27 13:29:18 +00003646 line_number = cond_command->line;
Jari Aaltocce855b1998-04-17 19:52:44 +00003647 /* If we're in a function, update the line number information. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00003648 if (variable_context && interactive_shell)
Jari Aaltob80f6442004-07-27 13:29:18 +00003649 line_number -= function_line_number;
Jari Aaltob80f6442004-07-27 13:29:18 +00003650 command_string_index = 0;
3651 print_cond_command (cond_command);
Jari Aalto95732b42005-12-07 14:08:12 +00003652
3653 if (signal_in_progress (DEBUG_TRAP) == 0)
3654 {
3655 FREE (the_printed_command_except_trap);
3656 the_printed_command_except_trap = savestring (the_printed_command);
3657 }
Jari Aaltocce855b1998-04-17 19:52:44 +00003658
Jari Aalto7117c2d2002-07-17 14:10:11 +00003659 /* Run the debug trap before each conditional command, but do it after we
3660 update the line number information. */
Jari Aaltob80f6442004-07-27 13:29:18 +00003661 retval = run_debug_trap ();
3662#if defined (DEBUGGER)
3663 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
3664 skip the command. */
3665 if (debugging_mode && retval != EXECUTION_SUCCESS)
3666 {
3667 line_number = save_line_number;
3668 return (EXECUTION_SUCCESS);
3669 }
3670#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +00003671
Jari Aaltocce855b1998-04-17 19:52:44 +00003672#if 0
3673 debug_print_cond_command (cond_command);
3674#endif
Jari Aaltob80f6442004-07-27 13:29:18 +00003675
3676 last_command_exit_value = retval = execute_cond_node (cond_command);
3677 line_number = save_line_number;
3678 return (retval);
Jari Aaltocce855b1998-04-17 19:52:44 +00003679}
3680#endif /* COND_COMMAND */
3681
Jari Aalto726f6381996-08-26 18:22:31 +00003682static void
3683bind_lastarg (arg)
3684 char *arg;
3685{
3686 SHELL_VAR *var;
3687
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003688 if (arg == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003689 arg = "";
Jari Aalto95732b42005-12-07 14:08:12 +00003690 var = bind_variable ("_", arg, 0);
Jari Aaltobb706242000-03-17 21:46:59 +00003691 VUNSETATTR (var, att_exported);
Jari Aalto726f6381996-08-26 18:22:31 +00003692}
3693
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003694/* Execute a null command. Fork a subshell if the command uses pipes or is
3695 to be run asynchronously. This handles all the side effects that are
3696 supposed to take place. */
3697static int
Jari Aalto95732b42005-12-07 14:08:12 +00003698execute_null_command (redirects, pipe_in, pipe_out, async)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003699 REDIRECT *redirects;
Jari Aaltobb706242000-03-17 21:46:59 +00003700 int pipe_in, pipe_out, async;
Jari Aalto726f6381996-08-26 18:22:31 +00003701{
Jari Aaltob80f6442004-07-27 13:29:18 +00003702 int r;
Chet Ramey00018032011-11-21 20:51:19 -05003703 int forcefork;
3704 REDIRECT *rd;
Jari Aaltob80f6442004-07-27 13:29:18 +00003705
Chet Ramey00018032011-11-21 20:51:19 -05003706 for (forcefork = 0, rd = redirects; rd; rd = rd->next)
3707 forcefork += rd->rflags & REDIR_VARASSIGN;
3708
3709 if (forcefork || pipe_in != NO_PIPE || pipe_out != NO_PIPE || async)
Jari Aalto726f6381996-08-26 18:22:31 +00003710 {
3711 /* We have a null command, but we really want a subshell to take
3712 care of it. Just fork, do piping and redirections, and exit. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003713 if (make_child ((char *)NULL, async) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003714 {
3715 /* Cancel traps, in trap.c. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003716 restore_original_signals (); /* XXX */
Jari Aalto726f6381996-08-26 18:22:31 +00003717
3718 do_piping (pipe_in, pipe_out);
3719
Jari Aalto31859422009-01-12 13:36:28 +00003720#if defined (COPROCESS_SUPPORT)
3721 coproc_closeall ();
3722#endif
3723
3724 subshell_environment = 0;
Jari Aalto06285672006-10-10 14:15:34 +00003725 if (async)
3726 subshell_environment |= SUBSHELL_ASYNC;
3727 if (pipe_in != NO_PIPE || pipe_out != NO_PIPE)
3728 subshell_environment |= SUBSHELL_PIPE;
Jari Aalto726f6381996-08-26 18:22:31 +00003729
Jari Aaltob80f6442004-07-27 13:29:18 +00003730 if (do_redirections (redirects, RX_ACTIVE) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003731 exit (EXECUTION_SUCCESS);
3732 else
3733 exit (EXECUTION_FAILURE);
3734 }
3735 else
3736 {
3737 close_pipes (pipe_in, pipe_out);
3738#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
Chet Rameyac50fba2014-02-26 09:36:43 -05003739 if (pipe_out == NO_PIPE)
3740 unlink_fifo_list ();
Jari Aalto726f6381996-08-26 18:22:31 +00003741#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003742 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +00003743 }
3744 }
3745 else
3746 {
3747 /* Even if there aren't any command names, pretend to do the
3748 redirections that are specified. The user expects the side
3749 effects to take place. If the redirections fail, then return
3750 failure. Otherwise, if a command substitution took place while
3751 expanding the command or a redirection, return the value of that
3752 substitution. Otherwise, return EXECUTION_SUCCESS. */
3753
Jari Aaltob80f6442004-07-27 13:29:18 +00003754 r = do_redirections (redirects, RX_ACTIVE|RX_UNDOABLE);
3755 cleanup_redirects (redirection_undo_list);
3756 redirection_undo_list = (REDIRECT *)NULL;
3757
3758 if (r != 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003759 return (EXECUTION_FAILURE);
Jari Aalto95732b42005-12-07 14:08:12 +00003760 else if (last_command_subst_pid != NO_PID)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003761 return (last_command_exit_value);
Jari Aalto726f6381996-08-26 18:22:31 +00003762 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003763 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +00003764 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003765}
3766
3767/* This is a hack to suppress word splitting for assignment statements
3768 given as arguments to builtins with the ASSIGNMENT_BUILTIN flag set. */
3769static void
3770fix_assignment_words (words)
3771 WORD_LIST *words;
3772{
Chet Rameyac50fba2014-02-26 09:36:43 -05003773 WORD_LIST *w, *wcmd;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003774 struct builtin *b;
Chet Rameyac50fba2014-02-26 09:36:43 -05003775 int assoc, global, array, integer;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003776
3777 if (words == 0)
3778 return;
3779
Jari Aalto7117c2d2002-07-17 14:10:11 +00003780 b = 0;
Chet Rameyac50fba2014-02-26 09:36:43 -05003781 assoc = global = array = integer = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003782
Chet Rameyac50fba2014-02-26 09:36:43 -05003783 /* Skip over assignment statements preceding a command name */
3784 wcmd = words;
3785 for (wcmd = words; wcmd; wcmd = wcmd->next)
3786 if ((wcmd->word->flags & W_ASSIGNMENT) == 0)
3787 break;
3788
3789 for (w = wcmd; w; w = w->next)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003790 if (w->word->flags & W_ASSIGNMENT)
Jari Aalto7117c2d2002-07-17 14:10:11 +00003791 {
3792 if (b == 0)
3793 {
Chet Rameyac50fba2014-02-26 09:36:43 -05003794 /* Posix (post-2008) says that `command' doesn't change whether
3795 or not the builtin it shadows is a `declaration command', even
3796 though it removes other special builtin properties. In Posix
3797 mode, we skip over one or more instances of `command' and
3798 deal with the next word as the assignment builtin. */
3799 while (posixly_correct && wcmd && wcmd->word && wcmd->word->word && STREQ (wcmd->word->word, "command"))
3800 wcmd = wcmd->next;
3801 b = builtin_address_internal (wcmd->word->word, 0);
Jari Aalto7117c2d2002-07-17 14:10:11 +00003802 if (b == 0 || (b->flags & ASSIGNMENT_BUILTIN) == 0)
3803 return;
Jari Aalto95732b42005-12-07 14:08:12 +00003804 else if (b && (b->flags & ASSIGNMENT_BUILTIN))
Chet Rameyac50fba2014-02-26 09:36:43 -05003805 wcmd->word->flags |= W_ASSNBLTIN;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003806 }
Jari Aalto95732b42005-12-07 14:08:12 +00003807 w->word->flags |= (W_NOSPLIT|W_NOGLOB|W_TILDEEXP|W_ASSIGNARG);
Jari Aalto31859422009-01-12 13:36:28 +00003808#if defined (ARRAY_VARS)
3809 if (assoc)
3810 w->word->flags |= W_ASSIGNASSOC;
Chet Rameyac50fba2014-02-26 09:36:43 -05003811 if (array)
3812 w->word->flags |= W_ASSIGNARRAY;
3813#endif
Chet Ramey6d41b712012-05-07 16:22:49 -04003814 if (global)
3815 w->word->flags |= W_ASSNGLOBAL;
Chet Rameyac50fba2014-02-26 09:36:43 -05003816 if (integer)
3817 w->word->flags |= W_ASSIGNINT;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003818 }
Jari Aalto31859422009-01-12 13:36:28 +00003819#if defined (ARRAY_VARS)
3820 /* Note that we saw an associative array option to a builtin that takes
3821 assignment statements. This is a bit of a kludge. */
Chet Rameyac50fba2014-02-26 09:36:43 -05003822 else if (w->word->word[0] == '-' && (strchr (w->word->word+1, 'A') || strchr (w->word->word+1, 'a') || strchr (w->word->word+1, 'g')))
Chet Ramey6d41b712012-05-07 16:22:49 -04003823#else
3824 else if (w->word->word[0] == '-' && strchr (w->word->word+1, 'g'))
3825#endif
Jari Aalto31859422009-01-12 13:36:28 +00003826 {
3827 if (b == 0)
3828 {
Chet Rameyac50fba2014-02-26 09:36:43 -05003829 while (posixly_correct && wcmd && wcmd->word && wcmd->word->word && STREQ (wcmd->word->word, "command"))
3830 wcmd = wcmd->next;
3831 b = builtin_address_internal (wcmd->word->word, 0);
Jari Aalto31859422009-01-12 13:36:28 +00003832 if (b == 0 || (b->flags & ASSIGNMENT_BUILTIN) == 0)
3833 return;
3834 else if (b && (b->flags & ASSIGNMENT_BUILTIN))
Chet Rameyac50fba2014-02-26 09:36:43 -05003835 wcmd->word->flags |= W_ASSNBLTIN;
Jari Aalto31859422009-01-12 13:36:28 +00003836 }
Chet Rameyac50fba2014-02-26 09:36:43 -05003837 if ((wcmd->word->flags & W_ASSNBLTIN) && strchr (w->word->word+1, 'A'))
Jari Aalto31859422009-01-12 13:36:28 +00003838 assoc = 1;
Chet Rameyac50fba2014-02-26 09:36:43 -05003839 else if ((wcmd->word->flags & W_ASSNBLTIN) && strchr (w->word->word+1, 'a'))
3840 array = 1;
3841 if ((wcmd->word->flags & W_ASSNBLTIN) && strchr (w->word->word+1, 'g'))
Chet Ramey6d41b712012-05-07 16:22:49 -04003842 global = 1;
Chet Rameyac50fba2014-02-26 09:36:43 -05003843 if ((wcmd->word->flags & W_ASSNBLTIN) && strchr (w->word->word+1, 'i'))
3844 integer = 1;
Jari Aalto31859422009-01-12 13:36:28 +00003845 }
Jari Aalto31859422009-01-12 13:36:28 +00003846}
3847
3848/* Return 1 if the file found by searching $PATH for PATHNAME, defaulting
3849 to PATHNAME, is a directory. Used by the autocd code below. */
3850static int
3851is_dirname (pathname)
3852 char *pathname;
3853{
3854 char *temp;
Chet Rameyac50fba2014-02-26 09:36:43 -05003855 int ret;
3856
3857 temp = search_for_command (pathname, 0);
3858 ret = (temp ? file_isdir (temp) : file_isdir (pathname));
3859 free (temp);
3860 return ret;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003861}
3862
3863/* The meaty part of all the executions. We have to start hacking the
3864 real execution of commands here. Fork a process, set things up,
3865 execute the command. */
3866static int
3867execute_simple_command (simple_command, pipe_in, pipe_out, async, fds_to_close)
3868 SIMPLE_COM *simple_command;
3869 int pipe_in, pipe_out, async;
3870 struct fd_bitmap *fds_to_close;
3871{
3872 WORD_LIST *words, *lastword;
3873 char *command_line, *lastarg, *temp;
Jari Aaltobc4cd231998-07-23 14:37:54 +00003874 int first_word_quoted, result, builtin_is_special, already_forked, dofork;
Jari Aalto95732b42005-12-07 14:08:12 +00003875 pid_t old_last_async_pid;
Jari Aaltof73dda02001-11-13 17:56:06 +00003876 sh_builtin_func_t *builtin;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003877 SHELL_VAR *func;
Chet Ramey495aee42011-11-22 19:11:26 -05003878 volatile int old_builtin, old_command_builtin;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003879
3880 result = EXECUTION_SUCCESS;
3881 special_builtin_failed = builtin_is_special = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +00003882 command_line = (char *)0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003883
Chet Rameyac50fba2014-02-26 09:36:43 -05003884 QUIT;
3885
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003886 /* If we're in a function, update the line number information. */
Chet Ramey00018032011-11-21 20:51:19 -05003887 if (variable_context && interactive_shell && sourcelevel == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00003888 line_number -= function_line_number;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003889
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003890 /* Remember what this command line looks like at invocation. */
3891 command_string_index = 0;
3892 print_simple_command (simple_command);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003893
Jari Aalto06285672006-10-10 14:15:34 +00003894#if 0
Jari Aalto95732b42005-12-07 14:08:12 +00003895 if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0)))
Jari Aalto06285672006-10-10 14:15:34 +00003896#else
3897 if (signal_in_progress (DEBUG_TRAP) == 0 && running_trap == 0)
3898#endif
Jari Aaltob80f6442004-07-27 13:29:18 +00003899 {
3900 FREE (the_printed_command_except_trap);
Jari Aalto95732b42005-12-07 14:08:12 +00003901 the_printed_command_except_trap = the_printed_command ? savestring (the_printed_command) : (char *)0;
Jari Aaltob80f6442004-07-27 13:29:18 +00003902 }
3903
3904 /* Run the debug trap before each simple command, but do it after we
3905 update the line number information. */
3906 result = run_debug_trap ();
3907#if defined (DEBUGGER)
3908 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
3909 skip the command. */
3910 if (debugging_mode && result != EXECUTION_SUCCESS)
3911 return (EXECUTION_SUCCESS);
3912#endif
3913
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003914 first_word_quoted =
Jari Aalto31859422009-01-12 13:36:28 +00003915 simple_command->words ? (simple_command->words->word->flags & W_QUOTED) : 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003916
Jari Aalto95732b42005-12-07 14:08:12 +00003917 last_command_subst_pid = NO_PID;
Jari Aaltob72432f1999-02-19 17:11:39 +00003918 old_last_async_pid = last_asynchronous_pid;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003919
Jari Aaltobc4cd231998-07-23 14:37:54 +00003920 already_forked = dofork = 0;
3921
3922 /* If we're in a pipeline or run in the background, set DOFORK so we
3923 make the child early, before word expansion. This keeps assignment
3924 statements from affecting the parent shell's environment when they
3925 should not. */
3926 dofork = pipe_in != NO_PIPE || pipe_out != NO_PIPE || async;
3927
3928 /* Something like `%2 &' should restart job 2 in the background, not cause
3929 the shell to fork here. */
3930 if (dofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE &&
3931 simple_command->words && simple_command->words->word &&
3932 simple_command->words->word->word &&
3933 (simple_command->words->word->word[0] == '%'))
3934 dofork = 0;
3935
3936 if (dofork)
Jari Aaltocce855b1998-04-17 19:52:44 +00003937 {
Jari Aaltobb706242000-03-17 21:46:59 +00003938 /* Do this now, because execute_disk_command will do it anyway in the
3939 vast majority of cases. */
3940 maybe_make_export_env ();
3941
Jari Aalto95732b42005-12-07 14:08:12 +00003942 /* Don't let a DEBUG trap overwrite the command string to be saved with
3943 the process/job associated with this child. */
3944 if (make_child (savestring (the_printed_command_except_trap), async) == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00003945 {
3946 already_forked = 1;
3947 simple_command->flags |= CMD_NO_FORK;
3948
Jari Aalto95732b42005-12-07 14:08:12 +00003949 subshell_environment = SUBSHELL_FORK;
3950 if (pipe_in != NO_PIPE || pipe_out != NO_PIPE)
3951 subshell_environment |= SUBSHELL_PIPE;
3952 if (async)
3953 subshell_environment |= SUBSHELL_ASYNC;
Jari Aalto28ef6c32001-04-06 19:14:31 +00003954
Jari Aaltobb706242000-03-17 21:46:59 +00003955 /* We need to do this before piping to handle some really
3956 pathological cases where one of the pipe file descriptors
3957 is < 2. */
3958 if (fds_to_close)
3959 close_fd_bitmap (fds_to_close);
3960
Jari Aaltocce855b1998-04-17 19:52:44 +00003961 do_piping (pipe_in, pipe_out);
Jari Aaltof73dda02001-11-13 17:56:06 +00003962 pipe_in = pipe_out = NO_PIPE;
Jari Aalto31859422009-01-12 13:36:28 +00003963#if defined (COPROCESS_SUPPORT)
3964 coproc_closeall ();
3965#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00003966
Jari Aaltob72432f1999-02-19 17:11:39 +00003967 last_asynchronous_pid = old_last_async_pid;
Chet Rameyac50fba2014-02-26 09:36:43 -05003968
3969 CHECK_SIGTERM;
Jari Aaltocce855b1998-04-17 19:52:44 +00003970 }
3971 else
3972 {
Chet Ramey495aee42011-11-22 19:11:26 -05003973 /* Don't let simple commands that aren't the last command in a
3974 pipeline change $? for the rest of the pipeline (or at all). */
3975 if (pipe_out != NO_PIPE)
3976 result = last_command_exit_value;
Jari Aaltocce855b1998-04-17 19:52:44 +00003977 close_pipes (pipe_in, pipe_out);
3978#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
Chet Rameyac50fba2014-02-26 09:36:43 -05003979 /* Close /dev/fd file descriptors in the parent after forking the
3980 last child in a (possibly one-element) pipeline. Defer this
3981 until any running shell function completes. */
3982 if (pipe_out == NO_PIPE && variable_context == 0) /* XXX */
3983 unlink_fifo_list (); /* XXX */
Jari Aaltocce855b1998-04-17 19:52:44 +00003984#endif
3985 command_line = (char *)NULL; /* don't free this. */
3986 bind_lastarg ((char *)NULL);
3987 return (result);
3988 }
3989 }
3990
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003991 /* If we are re-running this as the result of executing the `command'
3992 builtin, do not expand the command words a second time. */
3993 if ((simple_command->flags & CMD_INHIBIT_EXPANSION) == 0)
3994 {
3995 current_fds_to_close = fds_to_close;
3996 fix_assignment_words (simple_command->words);
Jari Aalto31859422009-01-12 13:36:28 +00003997 /* Pass the ignore return flag down to command substitutions */
3998 if (simple_command->flags & CMD_IGNORE_RETURN) /* XXX */
3999 comsub_ignore_return++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004000 words = expand_words (simple_command->words);
Jari Aalto31859422009-01-12 13:36:28 +00004001 if (simple_command->flags & CMD_IGNORE_RETURN)
4002 comsub_ignore_return--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004003 current_fds_to_close = (struct fd_bitmap *)NULL;
4004 }
4005 else
4006 words = copy_word_list (simple_command->words);
4007
4008 /* It is possible for WORDS not to have anything left in it.
4009 Perhaps all the words consisted of `$foo', and there was
4010 no variable `$foo'. */
4011 if (words == 0)
4012 {
Jari Aaltob80f6442004-07-27 13:29:18 +00004013 this_command_name = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004014 result = execute_null_command (simple_command->redirects,
Jari Aaltocce855b1998-04-17 19:52:44 +00004015 pipe_in, pipe_out,
Jari Aalto95732b42005-12-07 14:08:12 +00004016 already_forked ? 0 : async);
Jari Aaltocce855b1998-04-17 19:52:44 +00004017 if (already_forked)
4018 exit (result);
4019 else
4020 {
4021 bind_lastarg ((char *)NULL);
4022 set_pipestatus_from_exit (result);
4023 return (result);
4024 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004025 }
4026
4027 lastarg = (char *)NULL;
4028
4029 begin_unwind_frame ("simple-command");
4030
4031 if (echo_command_at_execute)
Jari Aaltob80f6442004-07-27 13:29:18 +00004032 xtrace_print_word_list (words, 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004033
Jari Aaltof73dda02001-11-13 17:56:06 +00004034 builtin = (sh_builtin_func_t *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004035 func = (SHELL_VAR *)NULL;
4036 if ((simple_command->flags & CMD_NO_FUNCTIONS) == 0)
4037 {
4038 /* Posix.2 says special builtins are found before functions. We
4039 don't set builtin_is_special anywhere other than here, because
4040 this path is followed only when the `command' builtin is *not*
4041 being used, and we don't want to exit the shell if a special
4042 builtin executed with `command builtin' fails. `command' is not
4043 a special builtin. */
4044 if (posixly_correct)
4045 {
4046 builtin = find_special_builtin (words->word->word);
4047 if (builtin)
4048 builtin_is_special = 1;
4049 }
4050 if (builtin == 0)
4051 func = find_function (words->word->word);
4052 }
4053
Jari Aaltob80f6442004-07-27 13:29:18 +00004054 /* In POSIX mode, assignment errors in the temporary environment cause a
4055 non-interactive shell to exit. */
Chet Rameyac50fba2014-02-26 09:36:43 -05004056 if (posixly_correct && builtin_is_special && interactive_shell == 0 && tempenv_assign_error)
Jari Aaltob80f6442004-07-27 13:29:18 +00004057 {
4058 last_command_exit_value = EXECUTION_FAILURE;
4059 jump_to_top_level (ERREXIT);
4060 }
Chet Rameyac50fba2014-02-26 09:36:43 -05004061 tempenv_assign_error = 0; /* don't care about this any more */
Jari Aaltob80f6442004-07-27 13:29:18 +00004062
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004063 add_unwind_protect (dispose_words, words);
4064 QUIT;
4065
4066 /* Bind the last word in this command to "$_" after execution. */
4067 for (lastword = words; lastword->next; lastword = lastword->next)
4068 ;
4069 lastarg = lastword->word->word;
4070
4071#if defined (JOB_CONTROL)
4072 /* Is this command a job control related thing? */
Jari Aaltocce855b1998-04-17 19:52:44 +00004073 if (words->word->word[0] == '%' && already_forked == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004074 {
4075 this_command_name = async ? "bg" : "fg";
4076 last_shell_builtin = this_shell_builtin;
4077 this_shell_builtin = builtin_address (this_command_name);
4078 result = (*this_shell_builtin) (words);
4079 goto return_result;
4080 }
4081
Chet Rameyac50fba2014-02-26 09:36:43 -05004082 /* One other possibililty. The user may want to resume an existing job.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004083 If they do, find out whether this word is a candidate for a running
4084 job. */
Jari Aaltocce855b1998-04-17 19:52:44 +00004085 if (job_control && already_forked == 0 && async == 0 &&
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004086 !first_word_quoted &&
4087 !words->next &&
4088 words->word->word[0] &&
4089 !simple_command->redirects &&
4090 pipe_in == NO_PIPE &&
4091 pipe_out == NO_PIPE &&
4092 (temp = get_string_value ("auto_resume")))
4093 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00004094 int job, jflags, started_status;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004095
Jari Aalto7117c2d2002-07-17 14:10:11 +00004096 jflags = JM_STOPPED|JM_FIRSTMATCH;
4097 if (STREQ (temp, "exact"))
4098 jflags |= JM_EXACT;
4099 else if (STREQ (temp, "substring"))
4100 jflags |= JM_SUBSTRING;
4101 else
4102 jflags |= JM_PREFIX;
4103 job = get_job_by_name (words->word->word, jflags);
4104 if (job != NO_JOB)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004105 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00004106 run_unwind_frame ("simple-command");
4107 this_command_name = "fg";
4108 last_shell_builtin = this_shell_builtin;
4109 this_shell_builtin = builtin_address ("fg");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004110
Jari Aalto7117c2d2002-07-17 14:10:11 +00004111 started_status = start_job (job, 1);
4112 return ((started_status < 0) ? EXECUTION_FAILURE : started_status);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004113 }
4114 }
4115#endif /* JOB_CONTROL */
4116
Jari Aalto31859422009-01-12 13:36:28 +00004117run_builtin:
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004118 /* Remember the name of this command globally. */
4119 this_command_name = words->word->word;
4120
4121 QUIT;
4122
4123 /* This command could be a shell builtin or a user-defined function.
4124 We have already found special builtins by this time, so we do not
4125 set builtin_is_special. If this is a function or builtin, and we
4126 have pipes, then fork a subshell in here. Otherwise, just execute
4127 the command directly. */
4128 if (func == 0 && builtin == 0)
4129 builtin = find_shell_builtin (this_command_name);
4130
4131 last_shell_builtin = this_shell_builtin;
4132 this_shell_builtin = builtin;
4133
4134 if (builtin || func)
4135 {
Jari Aalto31859422009-01-12 13:36:28 +00004136 if (builtin)
Chet Ramey495aee42011-11-22 19:11:26 -05004137 {
4138 old_builtin = executing_builtin;
4139 old_command_builtin = executing_command_builtin;
4140 unwind_protect_int (executing_builtin); /* modified in execute_builtin */
4141 unwind_protect_int (executing_command_builtin); /* ditto */
4142 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00004143 if (already_forked)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004144 {
Jari Aaltocce855b1998-04-17 19:52:44 +00004145 /* reset_terminating_signals (); */ /* XXX */
Chet Ramey495aee42011-11-22 19:11:26 -05004146 /* Reset the signal handlers in the child, but don't free the
4147 trap strings. Set a flag noting that we have to free the
4148 trap strings if we run trap to change a signal disposition. */
4149 reset_signal_handlers ();
4150 subshell_environment |= SUBSHELL_RESETTRAP;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004151
Jari Aaltocce855b1998-04-17 19:52:44 +00004152 if (async)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004153 {
Jari Aaltocce855b1998-04-17 19:52:44 +00004154 if ((simple_command->flags & CMD_STDIN_REDIR) &&
4155 pipe_in == NO_PIPE &&
4156 (stdin_redirects (simple_command->redirects) == 0))
4157 async_redirect_stdin ();
4158 setup_async_signals ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004159 }
Jari Aaltocce855b1998-04-17 19:52:44 +00004160
Jari Aaltob80f6442004-07-27 13:29:18 +00004161 subshell_level++;
Jari Aaltocce855b1998-04-17 19:52:44 +00004162 execute_subshell_builtin_or_function
4163 (words, simple_command->redirects, builtin, func,
4164 pipe_in, pipe_out, async, fds_to_close,
4165 simple_command->flags);
Jari Aaltob80f6442004-07-27 13:29:18 +00004166 subshell_level--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004167 }
4168 else
4169 {
4170 result = execute_builtin_or_function
4171 (words, builtin, func, simple_command->redirects, fds_to_close,
4172 simple_command->flags);
4173 if (builtin)
4174 {
4175 if (result > EX_SHERRBASE)
4176 {
Chet Rameyac50fba2014-02-26 09:36:43 -05004177 switch (result)
4178 {
4179 case EX_REDIRFAIL:
4180 case EX_BADASSIGN:
4181 case EX_EXPFAIL:
4182 /* These errors cause non-interactive posix mode shells to exit */
4183 if (posixly_correct && builtin_is_special && interactive_shell == 0)
4184 {
4185 last_command_exit_value = EXECUTION_FAILURE;
4186 jump_to_top_level (ERREXIT);
4187 }
4188 }
Jari Aaltocce855b1998-04-17 19:52:44 +00004189 result = builtin_status (result);
4190 if (builtin_is_special)
4191 special_builtin_failed = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004192 }
4193 /* In POSIX mode, if there are assignment statements preceding
4194 a special builtin, they persist after the builtin
4195 completes. */
4196 if (posixly_correct && builtin_is_special && temporary_env)
4197 merge_temporary_env ();
4198 }
4199 else /* function */
4200 {
4201 if (result == EX_USAGE)
4202 result = EX_BADUSAGE;
4203 else if (result > EX_SHERRBASE)
Jari Aaltocce855b1998-04-17 19:52:44 +00004204 result = EXECUTION_FAILURE;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004205 }
4206
Jari Aaltocce855b1998-04-17 19:52:44 +00004207 set_pipestatus_from_exit (result);
4208
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004209 goto return_result;
4210 }
4211 }
4212
Jari Aalto31859422009-01-12 13:36:28 +00004213 if (autocd && interactive && words->word && is_dirname (words->word->word))
4214 {
4215 words = make_word_list (make_word ("cd"), words);
4216 xtrace_print_word_list (words, 0);
4217 goto run_builtin;
4218 }
4219
Jari Aaltocce855b1998-04-17 19:52:44 +00004220 if (command_line == 0)
Chet Rameyac50fba2014-02-26 09:36:43 -05004221 command_line = savestring (the_printed_command_except_trap ? the_printed_command_except_trap : "");
Jari Aaltocce855b1998-04-17 19:52:44 +00004222
Jari Aaltof1be6662008-11-18 13:15:12 +00004223#if defined (PROCESS_SUBSTITUTION)
4224 if ((subshell_environment & SUBSHELL_COMSUB) && (simple_command->flags & CMD_NO_FORK) && fifos_pending() > 0)
4225 simple_command->flags &= ~CMD_NO_FORK;
4226#endif
4227
Chet Ramey495aee42011-11-22 19:11:26 -05004228 result = execute_disk_command (words, simple_command->redirects, command_line,
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004229 pipe_in, pipe_out, async, fds_to_close,
Jari Aaltod166f041997-06-05 14:59:13 +00004230 simple_command->flags);
Jari Aalto726f6381996-08-26 18:22:31 +00004231
4232 return_result:
4233 bind_lastarg (lastarg);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004234 FREE (command_line);
Jari Aaltob80f6442004-07-27 13:29:18 +00004235 dispose_words (words);
Chet Ramey495aee42011-11-22 19:11:26 -05004236 if (builtin)
4237 {
4238 executing_builtin = old_builtin;
4239 executing_command_builtin = old_command_builtin;
4240 }
Jari Aaltob80f6442004-07-27 13:29:18 +00004241 discard_unwind_frame ("simple-command");
4242 this_command_name = (char *)NULL; /* points to freed memory now */
Jari Aalto726f6381996-08-26 18:22:31 +00004243 return (result);
4244}
4245
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004246/* Translate the special builtin exit statuses. We don't really need a
4247 function for this; it's a placeholder for future work. */
4248static int
4249builtin_status (result)
4250 int result;
4251{
4252 int r;
4253
4254 switch (result)
4255 {
4256 case EX_USAGE:
4257 r = EX_BADUSAGE;
4258 break;
4259 case EX_REDIRFAIL:
4260 case EX_BADSYNTAX:
4261 case EX_BADASSIGN:
4262 case EX_EXPFAIL:
4263 r = EXECUTION_FAILURE;
4264 break;
4265 default:
4266 r = EXECUTION_SUCCESS;
4267 break;
4268 }
4269 return (r);
4270}
4271
Jari Aalto726f6381996-08-26 18:22:31 +00004272static int
4273execute_builtin (builtin, words, flags, subshell)
Jari Aaltof73dda02001-11-13 17:56:06 +00004274 sh_builtin_func_t *builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00004275 WORD_LIST *words;
4276 int flags, subshell;
4277{
Jari Aaltod166f041997-06-05 14:59:13 +00004278 int old_e_flag, result, eval_unwind;
Jari Aalto7117c2d2002-07-17 14:10:11 +00004279 int isbltinenv;
Chet Ramey00018032011-11-21 20:51:19 -05004280 char *error_trap;
Jari Aalto726f6381996-08-26 18:22:31 +00004281
Chet Ramey00018032011-11-21 20:51:19 -05004282 error_trap = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004283 old_e_flag = exit_immediately_on_error;
Chet Rameyac50fba2014-02-26 09:36:43 -05004284
Jari Aalto726f6381996-08-26 18:22:31 +00004285 /* The eval builtin calls parse_and_execute, which does not know about
4286 the setting of flags, and always calls the execution functions with
4287 flags that will exit the shell on an error if -e is set. If the
4288 eval builtin is being called, and we're supposed to ignore the exit
Chet Ramey00018032011-11-21 20:51:19 -05004289 value of the command, we turn the -e flag off ourselves and disable
4290 the ERR trap, then restore them when the command completes. This is
4291 also a problem (as below) for the command and source/. builtins. */
Jari Aalto31859422009-01-12 13:36:28 +00004292 if (subshell == 0 && (flags & CMD_IGNORE_RETURN) &&
4293 (builtin == eval_builtin || builtin == command_builtin || builtin == source_builtin))
Jari Aalto726f6381996-08-26 18:22:31 +00004294 {
4295 begin_unwind_frame ("eval_builtin");
4296 unwind_protect_int (exit_immediately_on_error);
Chet Rameyac50fba2014-02-26 09:36:43 -05004297 unwind_protect_int (builtin_ignoring_errexit);
Chet Ramey00018032011-11-21 20:51:19 -05004298 error_trap = TRAP_STRING (ERROR_TRAP);
4299 if (error_trap)
4300 {
4301 error_trap = savestring (error_trap);
4302 add_unwind_protect (xfree, error_trap);
4303 add_unwind_protect (set_error_trap, error_trap);
4304 restore_default_signal (ERROR_TRAP);
4305 }
Jari Aalto726f6381996-08-26 18:22:31 +00004306 exit_immediately_on_error = 0;
Chet Rameyac50fba2014-02-26 09:36:43 -05004307 builtin_ignoring_errexit = 1;
Jari Aaltod166f041997-06-05 14:59:13 +00004308 eval_unwind = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00004309 }
Jari Aaltod166f041997-06-05 14:59:13 +00004310 else
4311 eval_unwind = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00004312
4313 /* The temporary environment for a builtin is supposed to apply to
4314 all commands executed by that builtin. Currently, this is a
Chet Rameyac50fba2014-02-26 09:36:43 -05004315 problem only with the `unset', `source' and `eval' builtins.
4316 `mapfile' is a special case because it uses evalstring (same as
4317 eval or source) to run its callbacks. */
4318 isbltinenv = (builtin == source_builtin || builtin == eval_builtin || builtin == unset_builtin || builtin == mapfile_builtin);
Jari Aalto95732b42005-12-07 14:08:12 +00004319
Jari Aalto7117c2d2002-07-17 14:10:11 +00004320 if (isbltinenv)
Jari Aalto726f6381996-08-26 18:22:31 +00004321 {
4322 if (subshell == 0)
4323 begin_unwind_frame ("builtin_env");
4324
4325 if (temporary_env)
4326 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00004327 push_scope (VC_BLTNENV, temporary_env);
Jari Aalto726f6381996-08-26 18:22:31 +00004328 if (subshell == 0)
Jari Aalto95732b42005-12-07 14:08:12 +00004329 add_unwind_protect (pop_scope, (flags & CMD_COMMAND_BUILTIN) ? 0 : "1");
Jari Aalto7117c2d2002-07-17 14:10:11 +00004330 temporary_env = (HASH_TABLE *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00004331 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00004332 }
4333
4334 /* `return' does a longjmp() back to a saved environment in execute_function.
4335 If a variable assignment list preceded the command, and the shell is
4336 running in POSIX mode, we need to merge that into the shell_variables
4337 table, since `return' is a POSIX special builtin. */
4338 if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env)
4339 {
4340 begin_unwind_frame ("return_temp_env");
4341 add_unwind_protect (merge_temporary_env, (char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00004342 }
4343
Jari Aalto31859422009-01-12 13:36:28 +00004344 executing_builtin++;
Chet Ramey495aee42011-11-22 19:11:26 -05004345 executing_command_builtin |= builtin == command_builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00004346 result = ((*builtin) (words->next));
4347
Jari Aalto28ef6c32001-04-06 19:14:31 +00004348 /* This shouldn't happen, but in case `return' comes back instead of
4349 longjmp'ing, we need to unwind. */
4350 if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env)
4351 discard_unwind_frame ("return_temp_env");
4352
Jari Aalto7117c2d2002-07-17 14:10:11 +00004353 if (subshell == 0 && isbltinenv)
4354 run_unwind_frame ("builtin_env");
Jari Aalto726f6381996-08-26 18:22:31 +00004355
Jari Aaltod166f041997-06-05 14:59:13 +00004356 if (eval_unwind)
Jari Aalto726f6381996-08-26 18:22:31 +00004357 {
Chet Rameyac50fba2014-02-26 09:36:43 -05004358 exit_immediately_on_error = errexit_flag;
4359 builtin_ignoring_errexit = 0;
Chet Ramey00018032011-11-21 20:51:19 -05004360 if (error_trap)
4361 {
4362 set_error_trap (error_trap);
4363 xfree (error_trap);
4364 }
Jari Aalto726f6381996-08-26 18:22:31 +00004365 discard_unwind_frame ("eval_builtin");
4366 }
4367
4368 return (result);
4369}
4370
Jari Aalto726f6381996-08-26 18:22:31 +00004371static int
4372execute_function (var, words, flags, fds_to_close, async, subshell)
4373 SHELL_VAR *var;
4374 WORD_LIST *words;
Jari Aaltof73dda02001-11-13 17:56:06 +00004375 int flags;
Jari Aalto726f6381996-08-26 18:22:31 +00004376 struct fd_bitmap *fds_to_close;
Jari Aaltof73dda02001-11-13 17:56:06 +00004377 int async, subshell;
Jari Aalto726f6381996-08-26 18:22:31 +00004378{
4379 int return_val, result;
Jari Aaltob80f6442004-07-27 13:29:18 +00004380 COMMAND *tc, *fc, *save_current;
4381 char *debug_trap, *error_trap, *return_trap;
4382#if defined (ARRAY_VARS)
Jari Aalto95732b42005-12-07 14:08:12 +00004383 SHELL_VAR *funcname_v, *nfv, *bash_source_v, *bash_lineno_v;
Chet Ramey495aee42011-11-22 19:11:26 -05004384 ARRAY *funcname_a;
4385 volatile ARRAY *bash_source_a;
4386 volatile ARRAY *bash_lineno_a;
Jari Aaltob80f6442004-07-27 13:29:18 +00004387#endif
4388 FUNCTION_DEF *shell_fn;
4389 char *sfile, *t;
Jari Aaltof73dda02001-11-13 17:56:06 +00004390
4391 USE_VAR(fc);
Jari Aalto726f6381996-08-26 18:22:31 +00004392
Chet Ramey00018032011-11-21 20:51:19 -05004393 if (funcnest_max > 0 && funcnest >= funcnest_max)
4394 {
Chet Rameyac50fba2014-02-26 09:36:43 -05004395 internal_error (_("%s: maximum function nesting level exceeded (%d)"), var->name, funcnest);
Chet Ramey495aee42011-11-22 19:11:26 -05004396 funcnest = 0; /* XXX - should we reset it somewhere else? */
Chet Ramey00018032011-11-21 20:51:19 -05004397 jump_to_top_level (DISCARD);
4398 }
Chet Ramey00018032011-11-21 20:51:19 -05004399
Jari Aaltob80f6442004-07-27 13:29:18 +00004400#if defined (ARRAY_VARS)
4401 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
4402 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
4403 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
4404#endif
4405
Jari Aalto726f6381996-08-26 18:22:31 +00004406 tc = (COMMAND *)copy_command (function_cell (var));
4407 if (tc && (flags & CMD_IGNORE_RETURN))
4408 tc->flags |= CMD_IGNORE_RETURN;
4409
Jari Aalto726f6381996-08-26 18:22:31 +00004410 if (subshell == 0)
4411 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004412 begin_unwind_frame ("function_calling");
Jari Aalto7117c2d2002-07-17 14:10:11 +00004413 push_context (var->name, subshell, temporary_env);
Jari Aalto726f6381996-08-26 18:22:31 +00004414 add_unwind_protect (pop_context, (char *)NULL);
4415 unwind_protect_int (line_number);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004416 unwind_protect_int (return_catch_flag);
4417 unwind_protect_jmp_buf (return_catch);
4418 add_unwind_protect (dispose_command, (char *)tc);
Jari Aaltobb706242000-03-17 21:46:59 +00004419 unwind_protect_pointer (this_shell_function);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004420 unwind_protect_int (loop_level);
Chet Ramey00018032011-11-21 20:51:19 -05004421 unwind_protect_int (funcnest);
Jari Aalto726f6381996-08-26 18:22:31 +00004422 }
Jari Aalto7117c2d2002-07-17 14:10:11 +00004423 else
4424 push_context (var->name, subshell, temporary_env); /* don't unwind-protect for subshells */
4425
4426 temporary_env = (HASH_TABLE *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00004427
Jari Aaltobb706242000-03-17 21:46:59 +00004428 this_shell_function = var;
4429 make_funcname_visible (1);
4430
Jari Aaltof73dda02001-11-13 17:56:06 +00004431 debug_trap = TRAP_STRING(DEBUG_TRAP);
4432 error_trap = TRAP_STRING(ERROR_TRAP);
Jari Aaltob80f6442004-07-27 13:29:18 +00004433 return_trap = TRAP_STRING(RETURN_TRAP);
Jari Aaltof73dda02001-11-13 17:56:06 +00004434
Jari Aaltob80f6442004-07-27 13:29:18 +00004435 /* The order of the unwind protects for debug_trap, error_trap and
4436 return_trap is important here! unwind-protect commands are run
4437 in reverse order of registration. If this causes problems, take
4438 out the xfree unwind-protect calls and live with the small memory leak. */
4439
4440 /* function_trace_mode != 0 means that all functions inherit the DEBUG trap.
4441 if the function has the trace attribute set, it inherits the DEBUG trap */
4442 if (debug_trap && ((trace_p (var) == 0) && function_trace_mode == 0))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004443 {
4444 if (subshell == 0)
4445 {
4446 debug_trap = savestring (debug_trap);
Jari Aaltocce855b1998-04-17 19:52:44 +00004447 add_unwind_protect (xfree, debug_trap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004448 add_unwind_protect (set_debug_trap, debug_trap);
4449 }
4450 restore_default_signal (DEBUG_TRAP);
4451 }
Jari Aalto726f6381996-08-26 18:22:31 +00004452
Jari Aaltob80f6442004-07-27 13:29:18 +00004453 /* error_trace_mode != 0 means that functions inherit the ERR trap. */
4454 if (error_trap && error_trace_mode == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +00004455 {
4456 if (subshell == 0)
4457 {
4458 error_trap = savestring (error_trap);
4459 add_unwind_protect (xfree, error_trap);
4460 add_unwind_protect (set_error_trap, error_trap);
4461 }
4462 restore_default_signal (ERROR_TRAP);
4463 }
4464
Jari Aalto95732b42005-12-07 14:08:12 +00004465 /* Shell functions inherit the RETURN trap if function tracing is on
4466 globally or on individually for this function. */
4467#if 0
Jari Aaltob80f6442004-07-27 13:29:18 +00004468 if (return_trap && ((trace_p (var) == 0) && function_trace_mode == 0))
Jari Aalto95732b42005-12-07 14:08:12 +00004469#else
4470 if (return_trap && (signal_in_progress (DEBUG_TRAP) || ((trace_p (var) == 0) && function_trace_mode == 0)))
4471#endif
Jari Aaltob80f6442004-07-27 13:29:18 +00004472 {
4473 if (subshell == 0)
4474 {
4475 return_trap = savestring (return_trap);
4476 add_unwind_protect (xfree, return_trap);
4477 add_unwind_protect (set_return_trap, return_trap);
4478 }
4479 restore_default_signal (RETURN_TRAP);
4480 }
4481
4482 funcnest++;
4483#if defined (ARRAY_VARS)
4484 /* This is quite similar to the code in shell.c and elsewhere. */
4485 shell_fn = find_function_def (this_shell_function->name);
4486 sfile = shell_fn ? shell_fn->source_file : "";
Chet Rameyac50fba2014-02-26 09:36:43 -05004487 array_push ((ARRAY *)funcname_a, this_shell_function->name);
Jari Aaltob80f6442004-07-27 13:29:18 +00004488
Chet Rameyac50fba2014-02-26 09:36:43 -05004489 array_push ((ARRAY *)bash_source_a, sfile);
Jari Aaltob80f6442004-07-27 13:29:18 +00004490 t = itos (executing_line_number ());
Chet Rameyac50fba2014-02-26 09:36:43 -05004491 array_push ((ARRAY *)bash_lineno_a, t);
Jari Aaltob80f6442004-07-27 13:29:18 +00004492 free (t);
4493#endif
4494
Jari Aalto726f6381996-08-26 18:22:31 +00004495 /* The temporary environment for a function is supposed to apply to
4496 all commands executed within the function body. */
Jari Aalto726f6381996-08-26 18:22:31 +00004497
Jari Aalto726f6381996-08-26 18:22:31 +00004498 remember_args (words->next, 1);
4499
Jari Aaltob80f6442004-07-27 13:29:18 +00004500 /* Update BASH_ARGV and BASH_ARGC */
4501 if (debugging_mode)
4502 push_args (words->next);
4503
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004504 /* Number of the line on which the function body starts. */
Jari Aaltob80f6442004-07-27 13:29:18 +00004505 line_number = function_line_number = tc->line;
Jari Aalto726f6381996-08-26 18:22:31 +00004506
Jari Aalto726f6381996-08-26 18:22:31 +00004507#if defined (JOB_CONTROL)
Jari Aalto95732b42005-12-07 14:08:12 +00004508 if (subshell)
4509 stop_pipeline (async, (COMMAND *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00004510#endif
Jari Aalto726f6381996-08-26 18:22:31 +00004511
Jari Aalto95732b42005-12-07 14:08:12 +00004512 fc = tc;
Jari Aalto726f6381996-08-26 18:22:31 +00004513
4514 return_catch_flag++;
Chet Rameyac50fba2014-02-26 09:36:43 -05004515 return_val = setjmp_nosigs (return_catch);
Jari Aalto726f6381996-08-26 18:22:31 +00004516
4517 if (return_val)
Jari Aalto95732b42005-12-07 14:08:12 +00004518 {
4519 result = return_catch_value;
4520 /* Run the RETURN trap in the function's context. */
4521 save_current = currently_executing_command;
4522 run_return_trap ();
4523 currently_executing_command = save_current;
4524 }
Jari Aalto726f6381996-08-26 18:22:31 +00004525 else
Jari Aaltob80f6442004-07-27 13:29:18 +00004526 {
4527 /* Run the debug trap here so we can trap at the start of a function's
4528 execution rather than the execution of the body's first command. */
4529 showing_function_line = 1;
4530 save_current = currently_executing_command;
4531 result = run_debug_trap ();
4532#if defined (DEBUGGER)
4533 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
4534 skip the command. */
4535 if (debugging_mode == 0 || result == EXECUTION_SUCCESS)
4536 {
4537 showing_function_line = 0;
4538 currently_executing_command = save_current;
4539 result = execute_command_internal (fc, 0, NO_PIPE, NO_PIPE, fds_to_close);
4540
4541 /* Run the RETURN trap in the function's context */
4542 save_current = currently_executing_command;
4543 run_return_trap ();
4544 currently_executing_command = save_current;
4545 }
4546#else
4547 result = execute_command_internal (fc, 0, NO_PIPE, NO_PIPE, fds_to_close);
Jari Aalto95732b42005-12-07 14:08:12 +00004548
4549 save_current = currently_executing_command;
4550 run_return_trap ();
4551 currently_executing_command = save_current;
Jari Aaltob80f6442004-07-27 13:29:18 +00004552#endif
4553 showing_function_line = 0;
4554 }
4555
4556 /* Restore BASH_ARGC and BASH_ARGV */
4557 if (debugging_mode)
4558 pop_args ();
Jari Aalto726f6381996-08-26 18:22:31 +00004559
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004560 if (subshell == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00004561 run_unwind_frame ("function_calling");
4562
Jari Aaltob80f6442004-07-27 13:29:18 +00004563#if defined (ARRAY_VARS)
Jari Aalto95732b42005-12-07 14:08:12 +00004564 /* These two variables cannot be unset, and cannot be affected by the
4565 function. */
Chet Rameyac50fba2014-02-26 09:36:43 -05004566 array_pop ((ARRAY *)bash_source_a);
4567 array_pop ((ARRAY *)bash_lineno_a);
Jari Aalto95732b42005-12-07 14:08:12 +00004568
4569 /* FUNCNAME can be unset, and so can potentially be changed by the
4570 function. */
4571 GET_ARRAY_FROM_VAR ("FUNCNAME", nfv, funcname_a);
4572 if (nfv == funcname_v)
4573 array_pop (funcname_a);
Jari Aaltob80f6442004-07-27 13:29:18 +00004574#endif
4575
Jari Aaltobb706242000-03-17 21:46:59 +00004576 if (variable_context == 0 || this_shell_function == 0)
Chet Ramey00018032011-11-21 20:51:19 -05004577 {
4578 make_funcname_visible (0);
4579#if defined (PROCESS_SUBSTITUTION)
4580 unlink_fifo_list ();
4581#endif
4582 }
4583
Jari Aalto726f6381996-08-26 18:22:31 +00004584 return (result);
4585}
4586
Jari Aaltobb706242000-03-17 21:46:59 +00004587/* A convenience routine for use by other parts of the shell to execute
4588 a particular shell function. */
4589int
4590execute_shell_function (var, words)
4591 SHELL_VAR *var;
4592 WORD_LIST *words;
4593{
4594 int ret;
4595 struct fd_bitmap *bitmap;
4596
4597 bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE);
4598 begin_unwind_frame ("execute-shell-function");
4599 add_unwind_protect (dispose_fd_bitmap, (char *)bitmap);
4600
4601 ret = execute_function (var, words, 0, bitmap, 0, 0);
4602
4603 dispose_fd_bitmap (bitmap);
4604 discard_unwind_frame ("execute-shell-function");
4605
4606 return ret;
4607}
4608
Jari Aalto726f6381996-08-26 18:22:31 +00004609/* Execute a shell builtin or function in a subshell environment. This
4610 routine does not return; it only calls exit(). If BUILTIN is non-null,
4611 it points to a function to call to execute a shell builtin; otherwise
4612 VAR points at the body of a function to execute. WORDS is the arguments
4613 to the command, REDIRECTS specifies redirections to perform before the
4614 command is executed. */
4615static void
4616execute_subshell_builtin_or_function (words, redirects, builtin, var,
4617 pipe_in, pipe_out, async, fds_to_close,
4618 flags)
4619 WORD_LIST *words;
4620 REDIRECT *redirects;
Jari Aaltof73dda02001-11-13 17:56:06 +00004621 sh_builtin_func_t *builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00004622 SHELL_VAR *var;
4623 int pipe_in, pipe_out, async;
4624 struct fd_bitmap *fds_to_close;
4625 int flags;
4626{
Jari Aalto06285672006-10-10 14:15:34 +00004627 int result, r, funcvalue;
Jari Aaltobb706242000-03-17 21:46:59 +00004628#if defined (JOB_CONTROL)
4629 int jobs_hack;
Jari Aalto726f6381996-08-26 18:22:31 +00004630
Jari Aaltob72432f1999-02-19 17:11:39 +00004631 jobs_hack = (builtin == jobs_builtin) &&
4632 ((subshell_environment & SUBSHELL_ASYNC) == 0 || pipe_out != NO_PIPE);
Jari Aaltobb706242000-03-17 21:46:59 +00004633#endif
4634
4635 /* A subshell is neither a login shell nor interactive. */
4636 login_shell = interactive = 0;
Jari Aaltob72432f1999-02-19 17:11:39 +00004637
Jari Aalto06285672006-10-10 14:15:34 +00004638 if (async)
4639 subshell_environment |= SUBSHELL_ASYNC;
4640 if (pipe_in != NO_PIPE || pipe_out != NO_PIPE)
4641 subshell_environment |= SUBSHELL_PIPE;
Jari Aalto726f6381996-08-26 18:22:31 +00004642
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004643 maybe_make_export_env (); /* XXX - is this needed? */
Jari Aalto726f6381996-08-26 18:22:31 +00004644
4645#if defined (JOB_CONTROL)
4646 /* Eradicate all traces of job control after we fork the subshell, so
4647 all jobs begun by this subshell are in the same process group as
4648 the shell itself. */
4649
4650 /* Allow the output of `jobs' to be piped. */
Jari Aaltob72432f1999-02-19 17:11:39 +00004651 if (jobs_hack)
Jari Aalto726f6381996-08-26 18:22:31 +00004652 kill_current_pipeline ();
4653 else
4654 without_job_control ();
4655
4656 set_sigchld_handler ();
4657#endif /* JOB_CONTROL */
4658
4659 set_sigint_handler ();
4660
Jari Aalto726f6381996-08-26 18:22:31 +00004661 if (fds_to_close)
4662 close_fd_bitmap (fds_to_close);
4663
Jari Aaltobb706242000-03-17 21:46:59 +00004664 do_piping (pipe_in, pipe_out);
4665
Jari Aaltob80f6442004-07-27 13:29:18 +00004666 if (do_redirections (redirects, RX_ACTIVE) != 0)
Jari Aalto726f6381996-08-26 18:22:31 +00004667 exit (EXECUTION_FAILURE);
4668
4669 if (builtin)
4670 {
Jari Aalto726f6381996-08-26 18:22:31 +00004671 /* Give builtins a place to jump back to on failure,
4672 so we don't go back up to main(). */
Chet Rameyac50fba2014-02-26 09:36:43 -05004673 result = setjmp_nosigs (top_level);
Jari Aalto726f6381996-08-26 18:22:31 +00004674
Jari Aalto06285672006-10-10 14:15:34 +00004675 /* Give the return builtin a place to jump to when executed in a subshell
4676 or pipeline */
4677 funcvalue = 0;
4678 if (return_catch_flag && builtin == return_builtin)
Chet Rameyac50fba2014-02-26 09:36:43 -05004679 funcvalue = setjmp_nosigs (return_catch);
Jari Aalto06285672006-10-10 14:15:34 +00004680
Jari Aalto726f6381996-08-26 18:22:31 +00004681 if (result == EXITPROG)
4682 exit (last_command_exit_value);
4683 else if (result)
4684 exit (EXECUTION_FAILURE);
Jari Aalto06285672006-10-10 14:15:34 +00004685 else if (funcvalue)
4686 exit (return_catch_value);
Jari Aalto726f6381996-08-26 18:22:31 +00004687 else
Jari Aaltocce855b1998-04-17 19:52:44 +00004688 {
4689 r = execute_builtin (builtin, words, flags, 1);
Jari Aalto31859422009-01-12 13:36:28 +00004690 fflush (stdout);
Jari Aaltocce855b1998-04-17 19:52:44 +00004691 if (r == EX_USAGE)
4692 r = EX_BADUSAGE;
4693 exit (r);
4694 }
Jari Aalto726f6381996-08-26 18:22:31 +00004695 }
4696 else
Jari Aalto31859422009-01-12 13:36:28 +00004697 {
4698 r = execute_function (var, words, flags, fds_to_close, async, 1);
4699 fflush (stdout);
4700 exit (r);
4701 }
Jari Aalto726f6381996-08-26 18:22:31 +00004702}
4703
4704/* Execute a builtin or function in the current shell context. If BUILTIN
4705 is non-null, it is the builtin command to execute, otherwise VAR points
4706 to the body of a function. WORDS are the command's arguments, REDIRECTS
4707 are the redirections to perform. FDS_TO_CLOSE is the usual bitmap of
4708 file descriptors to close.
4709
4710 If BUILTIN is exec_builtin, the redirections specified in REDIRECTS are
4711 not undone before this function returns. */
4712static int
4713execute_builtin_or_function (words, builtin, var, redirects,
4714 fds_to_close, flags)
4715 WORD_LIST *words;
Jari Aaltof73dda02001-11-13 17:56:06 +00004716 sh_builtin_func_t *builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00004717 SHELL_VAR *var;
4718 REDIRECT *redirects;
4719 struct fd_bitmap *fds_to_close;
4720 int flags;
4721{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004722 int result;
Jari Aalto726f6381996-08-26 18:22:31 +00004723 REDIRECT *saved_undo_list;
Chet Ramey495aee42011-11-22 19:11:26 -05004724#if defined (PROCESS_SUBSTITUTION)
4725 int ofifo, nfifo, osize;
4726 char *ofifo_list;
4727#endif
4728
4729
4730#if defined (PROCESS_SUBSTITUTION)
4731 ofifo = num_fifos ();
4732 ofifo_list = copy_fifo_list (&osize);
4733#endif
Jari Aalto726f6381996-08-26 18:22:31 +00004734
Jari Aaltob80f6442004-07-27 13:29:18 +00004735 if (do_redirections (redirects, RX_ACTIVE|RX_UNDOABLE) != 0)
Jari Aalto726f6381996-08-26 18:22:31 +00004736 {
4737 cleanup_redirects (redirection_undo_list);
4738 redirection_undo_list = (REDIRECT *)NULL;
4739 dispose_exec_redirects ();
Chet Ramey495aee42011-11-22 19:11:26 -05004740#if defined (PROCESS_SUBSTITUTION)
4741 free (ofifo_list);
4742#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004743 return (EX_REDIRFAIL); /* was EXECUTION_FAILURE */
Jari Aalto726f6381996-08-26 18:22:31 +00004744 }
4745
4746 saved_undo_list = redirection_undo_list;
4747
4748 /* Calling the "exec" builtin changes redirections forever. */
4749 if (builtin == exec_builtin)
4750 {
4751 dispose_redirects (saved_undo_list);
4752 saved_undo_list = exec_redirection_undo_list;
4753 exec_redirection_undo_list = (REDIRECT *)NULL;
4754 }
4755 else
4756 dispose_exec_redirects ();
4757
4758 if (saved_undo_list)
4759 {
4760 begin_unwind_frame ("saved redirects");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004761 add_unwind_protect (cleanup_redirects, (char *)saved_undo_list);
Jari Aalto726f6381996-08-26 18:22:31 +00004762 }
4763
4764 redirection_undo_list = (REDIRECT *)NULL;
4765
4766 if (builtin)
4767 result = execute_builtin (builtin, words, flags, 0);
4768 else
4769 result = execute_function (var, words, flags, fds_to_close, 0, 0);
4770
Jari Aaltob80f6442004-07-27 13:29:18 +00004771 /* We do this before undoing the effects of any redirections. */
Jari Aalto31859422009-01-12 13:36:28 +00004772 fflush (stdout);
4773 fpurge (stdout);
Jari Aaltob80f6442004-07-27 13:29:18 +00004774 if (ferror (stdout))
4775 clearerr (stdout);
4776
Jari Aalto28ef6c32001-04-06 19:14:31 +00004777 /* If we are executing the `command' builtin, but this_shell_builtin is
4778 set to `exec_builtin', we know that we have something like
4779 `command exec [redirection]', since otherwise `exec' would have
4780 overwritten the shell and we wouldn't get here. In this case, we
4781 want to behave as if the `command' builtin had not been specified
4782 and preserve the redirections. */
4783 if (builtin == command_builtin && this_shell_builtin == exec_builtin)
4784 {
Chet Ramey495aee42011-11-22 19:11:26 -05004785 int discard;
4786
4787 discard = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00004788 if (saved_undo_list)
Chet Ramey495aee42011-11-22 19:11:26 -05004789 {
4790 dispose_redirects (saved_undo_list);
4791 discard = 1;
4792 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00004793 redirection_undo_list = exec_redirection_undo_list;
4794 saved_undo_list = exec_redirection_undo_list = (REDIRECT *)NULL;
Chet Ramey495aee42011-11-22 19:11:26 -05004795 if (discard)
4796 discard_unwind_frame ("saved redirects");
Jari Aalto28ef6c32001-04-06 19:14:31 +00004797 }
4798
Jari Aalto726f6381996-08-26 18:22:31 +00004799 if (saved_undo_list)
4800 {
4801 redirection_undo_list = saved_undo_list;
4802 discard_unwind_frame ("saved redirects");
4803 }
4804
4805 if (redirection_undo_list)
4806 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004807 cleanup_redirects (redirection_undo_list);
Jari Aalto726f6381996-08-26 18:22:31 +00004808 redirection_undo_list = (REDIRECT *)NULL;
4809 }
4810
Chet Ramey495aee42011-11-22 19:11:26 -05004811#if defined (PROCESS_SUBSTITUTION)
4812 /* Close any FIFOs created by this builtin or function. */
4813 nfifo = num_fifos ();
4814 if (nfifo > ofifo)
4815 close_new_fifos (ofifo_list, osize);
4816 free (ofifo_list);
4817#endif
4818
Jari Aalto726f6381996-08-26 18:22:31 +00004819 return (result);
4820}
4821
4822void
4823setup_async_signals ()
4824{
Jari Aaltob72432f1999-02-19 17:11:39 +00004825#if defined (__BEOS__)
4826 set_signal_handler (SIGHUP, SIG_IGN); /* they want csh-like behavior */
4827#endif
4828
Jari Aalto726f6381996-08-26 18:22:31 +00004829#if defined (JOB_CONTROL)
4830 if (job_control == 0)
4831#endif
4832 {
Chet Rameyac50fba2014-02-26 09:36:43 -05004833 /* Make sure we get the original signal dispositions now so we don't
4834 confuse the trap builtin later if the subshell tries to use it to
4835 reset SIGINT/SIGQUIT. Don't call set_signal_ignored; that sets
4836 the value of original_signals to SIG_IGN. Posix interpretation 751. */
4837 get_original_signal (SIGINT);
Jari Aalto726f6381996-08-26 18:22:31 +00004838 set_signal_handler (SIGINT, SIG_IGN);
Chet Rameyac50fba2014-02-26 09:36:43 -05004839
4840 get_original_signal (SIGQUIT);
Jari Aalto726f6381996-08-26 18:22:31 +00004841 set_signal_handler (SIGQUIT, SIG_IGN);
Jari Aalto726f6381996-08-26 18:22:31 +00004842 }
4843}
4844
4845/* Execute a simple command that is hopefully defined in a disk file
4846 somewhere.
4847
4848 1) fork ()
4849 2) connect pipes
4850 3) look up the command
4851 4) do redirections
4852 5) execve ()
4853 6) If the execve failed, see if the file has executable mode set.
4854 If so, and it isn't a directory, then execute its contents as
4855 a shell script.
4856
4857 Note that the filename hashing stuff has to take place up here,
4858 in the parent. This is probably why the Bourne style shells
4859 don't handle it, since that would require them to go through
Jari Aaltob80f6442004-07-27 13:29:18 +00004860 this gnarly hair, for no good reason.
4861
4862 NOTE: callers expect this to fork or exit(). */
Jari Aalto31859422009-01-12 13:36:28 +00004863
4864/* Name of a shell function to call when a command name is not found. */
4865#ifndef NOTFOUND_HOOK
4866# define NOTFOUND_HOOK "command_not_found_handle"
4867#endif
4868
Chet Ramey495aee42011-11-22 19:11:26 -05004869static int
Jari Aalto726f6381996-08-26 18:22:31 +00004870execute_disk_command (words, redirects, command_line, pipe_in, pipe_out,
Jari Aaltod166f041997-06-05 14:59:13 +00004871 async, fds_to_close, cmdflags)
Jari Aalto726f6381996-08-26 18:22:31 +00004872 WORD_LIST *words;
4873 REDIRECT *redirects;
4874 char *command_line;
4875 int pipe_in, pipe_out, async;
4876 struct fd_bitmap *fds_to_close;
Jari Aaltod166f041997-06-05 14:59:13 +00004877 int cmdflags;
Jari Aalto726f6381996-08-26 18:22:31 +00004878{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004879 char *pathname, *command, **args;
Chet Ramey495aee42011-11-22 19:11:26 -05004880 int nofork, result;
Jari Aaltobb706242000-03-17 21:46:59 +00004881 pid_t pid;
Jari Aalto31859422009-01-12 13:36:28 +00004882 SHELL_VAR *hookf;
4883 WORD_LIST *wl;
Jari Aalto726f6381996-08-26 18:22:31 +00004884
Jari Aaltod166f041997-06-05 14:59:13 +00004885 nofork = (cmdflags & CMD_NO_FORK); /* Don't fork, just exec, if no pipes */
Jari Aalto726f6381996-08-26 18:22:31 +00004886 pathname = words->word->word;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004887
Chet Ramey495aee42011-11-22 19:11:26 -05004888 result = EXECUTION_SUCCESS;
Jari Aalto726f6381996-08-26 18:22:31 +00004889#if defined (RESTRICTED_SHELL)
Jari Aaltob80f6442004-07-27 13:29:18 +00004890 command = (char *)NULL;
Chet Ramey00018032011-11-21 20:51:19 -05004891 if (restricted && mbschr (pathname, '/'))
Jari Aalto726f6381996-08-26 18:22:31 +00004892 {
Jari Aaltob80f6442004-07-27 13:29:18 +00004893 internal_error (_("%s: restricted: cannot specify `/' in command names"),
Jari Aalto726f6381996-08-26 18:22:31 +00004894 pathname);
Chet Ramey495aee42011-11-22 19:11:26 -05004895 result = last_command_exit_value = EXECUTION_FAILURE;
Jari Aaltob80f6442004-07-27 13:29:18 +00004896
4897 /* If we're not going to fork below, we must already be in a child
4898 process or a context in which it's safe to call exit(2). */
4899 if (nofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE)
4900 exit (last_command_exit_value);
4901 else
4902 goto parent_return;
Jari Aalto726f6381996-08-26 18:22:31 +00004903 }
4904#endif /* RESTRICTED_SHELL */
4905
Chet Rameyac50fba2014-02-26 09:36:43 -05004906 command = search_for_command (pathname, 1);
Jari Aalto726f6381996-08-26 18:22:31 +00004907
4908 if (command)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004909 {
4910 maybe_make_export_env ();
4911 put_command_name_into_env (command);
4912 }
Jari Aalto726f6381996-08-26 18:22:31 +00004913
Jari Aaltobb706242000-03-17 21:46:59 +00004914 /* We have to make the child before we check for the non-existence
Jari Aalto726f6381996-08-26 18:22:31 +00004915 of COMMAND, since we want the error messages to be redirected. */
4916 /* If we can get away without forking and there are no pipes to deal with,
4917 don't bother to fork, just directly exec the command. */
4918 if (nofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE)
4919 pid = 0;
4920 else
4921 pid = make_child (savestring (command_line), async);
4922
4923 if (pid == 0)
4924 {
4925 int old_interactive;
4926
Chet Ramey495aee42011-11-22 19:11:26 -05004927 reset_terminating_signals (); /* XXX */
Jari Aalto726f6381996-08-26 18:22:31 +00004928 /* Cancel traps, in trap.c. */
4929 restore_original_signals ();
4930
Chet Rameyac50fba2014-02-26 09:36:43 -05004931 CHECK_SIGTERM;
4932
Jari Aalto726f6381996-08-26 18:22:31 +00004933 /* restore_original_signals may have undone the work done
Jari Aaltocce855b1998-04-17 19:52:44 +00004934 by make_child to ensure that SIGINT and SIGQUIT are ignored
4935 in asynchronous children. */
Jari Aalto726f6381996-08-26 18:22:31 +00004936 if (async)
Jari Aaltod166f041997-06-05 14:59:13 +00004937 {
4938 if ((cmdflags & CMD_STDIN_REDIR) &&
4939 pipe_in == NO_PIPE &&
4940 (stdin_redirects (redirects) == 0))
4941 async_redirect_stdin ();
4942 setup_async_signals ();
4943 }
Jari Aalto726f6381996-08-26 18:22:31 +00004944
Jari Aaltobb706242000-03-17 21:46:59 +00004945 /* This functionality is now provided by close-on-exec of the
4946 file descriptors manipulated by redirection and piping.
4947 Some file descriptors still need to be closed in all children
4948 because of the way bash does pipes; fds_to_close is a
4949 bitmap of all such file descriptors. */
4950 if (fds_to_close)
4951 close_fd_bitmap (fds_to_close);
4952
Jari Aalto726f6381996-08-26 18:22:31 +00004953 do_piping (pipe_in, pipe_out);
4954
Jari Aaltof73dda02001-11-13 17:56:06 +00004955 old_interactive = interactive;
Jari Aalto726f6381996-08-26 18:22:31 +00004956 if (async)
Jari Aaltof73dda02001-11-13 17:56:06 +00004957 interactive = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00004958
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004959 subshell_environment = SUBSHELL_FORK;
Jari Aalto726f6381996-08-26 18:22:31 +00004960
Jari Aaltob80f6442004-07-27 13:29:18 +00004961 if (redirects && (do_redirections (redirects, RX_ACTIVE) != 0))
Jari Aalto726f6381996-08-26 18:22:31 +00004962 {
4963#if defined (PROCESS_SUBSTITUTION)
4964 /* Try to remove named pipes that may have been created as the
4965 result of redirections. */
4966 unlink_fifo_list ();
4967#endif /* PROCESS_SUBSTITUTION */
4968 exit (EXECUTION_FAILURE);
4969 }
4970
4971 if (async)
4972 interactive = old_interactive;
4973
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004974 if (command == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00004975 {
Jari Aalto31859422009-01-12 13:36:28 +00004976 hookf = find_function (NOTFOUND_HOOK);
4977 if (hookf == 0)
4978 {
Chet Ramey495aee42011-11-22 19:11:26 -05004979 /* Make sure filenames are displayed using printable characters */
4980 if (ansic_shouldquote (pathname))
4981 pathname = ansic_quote (pathname, 0, NULL);
Jari Aalto31859422009-01-12 13:36:28 +00004982 internal_error (_("%s: command not found"), pathname);
4983 exit (EX_NOTFOUND); /* Posix.2 says the exit status is 127 */
4984 }
4985
Chet Rameyac50fba2014-02-26 09:36:43 -05004986#if defined (JOB_CONTROL)
4987 /* May need to reinitialize more of the job control state here. */
4988 kill_current_pipeline ();
4989#endif
4990
Jari Aalto31859422009-01-12 13:36:28 +00004991 wl = make_word_list (make_word (NOTFOUND_HOOK), words);
4992 exit (execute_shell_function (hookf, wl));
Jari Aalto726f6381996-08-26 18:22:31 +00004993 }
4994
Chet Rameyac50fba2014-02-26 09:36:43 -05004995 CHECK_SIGTERM;
4996
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004997 /* Execve expects the command name to be in args[0]. So we
4998 leave it there, in the same format that the user used to
4999 type it in. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00005000 args = strvec_from_word_list (words, 0, 0, (int *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00005001 exit (shell_execve (command, args, export_env));
5002 }
5003 else
5004 {
Jari Aaltob80f6442004-07-27 13:29:18 +00005005parent_return:
Chet Rameyac50fba2014-02-26 09:36:43 -05005006 QUIT;
5007
Jari Aalto726f6381996-08-26 18:22:31 +00005008 /* Make sure that the pipes are closed in the parent. */
5009 close_pipes (pipe_in, pipe_out);
5010#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
Chet Ramey00018032011-11-21 20:51:19 -05005011 if (variable_context == 0)
5012 unlink_fifo_list ();
Jari Aalto726f6381996-08-26 18:22:31 +00005013#endif
5014 FREE (command);
Chet Ramey495aee42011-11-22 19:11:26 -05005015 return (result);
Jari Aalto726f6381996-08-26 18:22:31 +00005016 }
5017}
5018
Jari Aaltof73dda02001-11-13 17:56:06 +00005019/* CPP defines to decide whether a particular index into the #! line
5020 corresponds to a valid interpreter name or argument character, or
5021 whitespace. The MSDOS define is to allow \r to be treated the same
5022 as \n. */
5023
5024#if !defined (MSDOS)
5025# define STRINGCHAR(ind) \
5026 (ind < sample_len && !whitespace (sample[ind]) && sample[ind] != '\n')
5027# define WHITECHAR(ind) \
5028 (ind < sample_len && whitespace (sample[ind]))
5029#else /* MSDOS */
5030# define STRINGCHAR(ind) \
5031 (ind < sample_len && !whitespace (sample[ind]) && sample[ind] != '\n' && sample[ind] != '\r')
5032# define WHITECHAR(ind) \
5033 (ind < sample_len && whitespace (sample[ind]))
5034#endif /* MSDOS */
5035
5036static char *
5037getinterp (sample, sample_len, endp)
5038 char *sample;
5039 int sample_len, *endp;
5040{
5041 register int i;
5042 char *execname;
5043 int start;
5044
5045 /* Find the name of the interpreter to exec. */
5046 for (i = 2; i < sample_len && whitespace (sample[i]); i++)
5047 ;
5048
5049 for (start = i; STRINGCHAR(i); i++)
5050 ;
5051
5052 execname = substring (sample, start, i);
5053
5054 if (endp)
5055 *endp = i;
5056 return execname;
5057}
5058
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005059#if !defined (HAVE_HASH_BANG_EXEC)
Jari Aalto726f6381996-08-26 18:22:31 +00005060/* If the operating system on which we're running does not handle
5061 the #! executable format, then help out. SAMPLE is the text read
5062 from the file, SAMPLE_LEN characters. COMMAND is the name of
5063 the script; it and ARGS, the arguments given by the user, will
5064 become arguments to the specified interpreter. ENV is the environment
5065 to pass to the interpreter.
5066
5067 The word immediately following the #! is the interpreter to execute.
5068 A single argument to the interpreter is allowed. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00005069
Jari Aalto726f6381996-08-26 18:22:31 +00005070static int
5071execute_shell_script (sample, sample_len, command, args, env)
Jari Aaltof73dda02001-11-13 17:56:06 +00005072 char *sample;
Jari Aalto726f6381996-08-26 18:22:31 +00005073 int sample_len;
5074 char *command;
5075 char **args, **env;
5076{
Jari Aalto726f6381996-08-26 18:22:31 +00005077 char *execname, *firstarg;
Jari Aaltof73dda02001-11-13 17:56:06 +00005078 int i, start, size_increment, larry;
Jari Aalto726f6381996-08-26 18:22:31 +00005079
5080 /* Find the name of the interpreter to exec. */
Jari Aaltof73dda02001-11-13 17:56:06 +00005081 execname = getinterp (sample, sample_len, &i);
Jari Aalto726f6381996-08-26 18:22:31 +00005082 size_increment = 1;
5083
5084 /* Now the argument, if any. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00005085 for (firstarg = (char *)NULL, start = i; WHITECHAR(i); i++)
Jari Aalto726f6381996-08-26 18:22:31 +00005086 ;
5087
5088 /* If there is more text on the line, then it is an argument for the
5089 interpreter. */
Jari Aalto726f6381996-08-26 18:22:31 +00005090
Jari Aalto28ef6c32001-04-06 19:14:31 +00005091 if (STRINGCHAR(i))
5092 {
5093 for (start = i; STRINGCHAR(i); i++)
5094 ;
5095 firstarg = substring ((char *)sample, start, i);
Jari Aalto726f6381996-08-26 18:22:31 +00005096 size_increment = 2;
5097 }
5098
Jari Aalto7117c2d2002-07-17 14:10:11 +00005099 larry = strvec_len (args) + size_increment;
5100 args = strvec_resize (args, larry + 1);
Jari Aalto726f6381996-08-26 18:22:31 +00005101
5102 for (i = larry - 1; i; i--)
5103 args[i] = args[i - size_increment];
5104
5105 args[0] = execname;
5106 if (firstarg)
5107 {
5108 args[1] = firstarg;
5109 args[2] = command;
5110 }
5111 else
5112 args[1] = command;
5113
5114 args[larry] = (char *)NULL;
5115
5116 return (shell_execve (execname, args, env));
5117}
Jari Aalto28ef6c32001-04-06 19:14:31 +00005118#undef STRINGCHAR
5119#undef WHITECHAR
5120
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005121#endif /* !HAVE_HASH_BANG_EXEC */
5122
Jari Aaltod166f041997-06-05 14:59:13 +00005123static void
5124initialize_subshell ()
5125{
5126#if defined (ALIAS)
5127 /* Forget about any aliases that we knew of. We are in a subshell. */
5128 delete_all_aliases ();
5129#endif /* ALIAS */
5130
5131#if defined (HISTORY)
5132 /* Forget about the history lines we have read. This is a non-interactive
5133 subshell. */
5134 history_lines_this_session = 0;
5135#endif
5136
5137#if defined (JOB_CONTROL)
5138 /* Forget about the way job control was working. We are in a subshell. */
5139 without_job_control ();
5140 set_sigchld_handler ();
Jari Aalto95732b42005-12-07 14:08:12 +00005141 init_job_stats ();
Jari Aaltod166f041997-06-05 14:59:13 +00005142#endif /* JOB_CONTROL */
5143
5144 /* Reset the values of the shell flags and options. */
5145 reset_shell_flags ();
5146 reset_shell_options ();
5147 reset_shopt_options ();
5148
Jari Aalto28ef6c32001-04-06 19:14:31 +00005149 /* Zero out builtin_env, since this could be a shell script run from a
5150 sourced file with a temporary environment supplied to the `source/.'
5151 builtin. Such variables are not supposed to be exported (empirical
Jari Aalto7117c2d2002-07-17 14:10:11 +00005152 testing with sh and ksh). Just throw it away; don't worry about a
5153 memory leak. */
5154 if (vc_isbltnenv (shell_variables))
5155 shell_variables = shell_variables->down;
Jari Aalto28ef6c32001-04-06 19:14:31 +00005156
5157 clear_unwind_protect_list (0);
Jari Aaltof1be6662008-11-18 13:15:12 +00005158 /* XXX -- are there other things we should be resetting here? */
5159 parse_and_execute_level = 0; /* nothing left to restore it */
Jari Aalto28ef6c32001-04-06 19:14:31 +00005160
5161 /* We're no longer inside a shell function. */
Chet Ramey00018032011-11-21 20:51:19 -05005162 variable_context = return_catch_flag = funcnest = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00005163
Jari Aalto31859422009-01-12 13:36:28 +00005164 executing_list = 0; /* XXX */
5165
Jari Aaltod166f041997-06-05 14:59:13 +00005166 /* If we're not interactive, close the file descriptor from which we're
5167 reading the current shell script. */
Jari Aaltocce855b1998-04-17 19:52:44 +00005168 if (interactive_shell == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00005169 unset_bash_input (0);
Jari Aaltod166f041997-06-05 14:59:13 +00005170}
5171
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005172#if defined (HAVE_SETOSTYPE) && defined (_POSIX_SOURCE)
5173# define SETOSTYPE(x) __setostype(x)
5174#else
5175# define SETOSTYPE(x)
5176#endif
Jari Aalto726f6381996-08-26 18:22:31 +00005177
Jari Aalto28ef6c32001-04-06 19:14:31 +00005178#define READ_SAMPLE_BUF(file, buf, len) \
5179 do \
5180 { \
5181 fd = open(file, O_RDONLY); \
5182 if (fd >= 0) \
5183 { \
Jari Aaltof73dda02001-11-13 17:56:06 +00005184 len = read (fd, buf, 80); \
Jari Aalto28ef6c32001-04-06 19:14:31 +00005185 close (fd); \
5186 } \
5187 else \
5188 len = -1; \
5189 } \
5190 while (0)
5191
Jari Aalto726f6381996-08-26 18:22:31 +00005192/* Call execve (), handling interpreting shell scripts, and handling
5193 exec failures. */
5194int
5195shell_execve (command, args, env)
5196 char *command;
5197 char **args, **env;
5198{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005199 int larray, i, fd;
Jari Aaltof73dda02001-11-13 17:56:06 +00005200 char sample[80];
Jari Aalto28ef6c32001-04-06 19:14:31 +00005201 int sample_len;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005202
5203 SETOSTYPE (0); /* Some systems use for USG/POSIX semantics */
Jari Aalto726f6381996-08-26 18:22:31 +00005204 execve (command, args, env);
Jari Aalto28ef6c32001-04-06 19:14:31 +00005205 i = errno; /* error from execve() */
Jari Aalto31859422009-01-12 13:36:28 +00005206 CHECK_TERMSIG;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005207 SETOSTYPE (1);
Jari Aalto726f6381996-08-26 18:22:31 +00005208
5209 /* If we get to this point, then start checking out the file.
5210 Maybe it is something we can hack ourselves. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00005211 if (i != ENOEXEC)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005212 {
Jari Aalto31859422009-01-12 13:36:28 +00005213 if (file_isdir (command))
Chet Ramey495aee42011-11-22 19:11:26 -05005214#if defined (EISDIR)
5215 internal_error (_("%s: %s"), command, strerror (EISDIR));
5216#else
Jari Aaltob80f6442004-07-27 13:29:18 +00005217 internal_error (_("%s: is a directory"), command);
Chet Ramey495aee42011-11-22 19:11:26 -05005218#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +00005219 else if (executable_file (command) == 0)
5220 {
5221 errno = i;
5222 file_error (command);
5223 }
Jari Aalto95732b42005-12-07 14:08:12 +00005224 /* errors not involving the path argument to execve. */
5225 else if (i == E2BIG || i == ENOMEM)
5226 {
5227 errno = i;
5228 file_error (command);
5229 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005230 else
Jari Aalto726f6381996-08-26 18:22:31 +00005231 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00005232 /* The file has the execute bits set, but the kernel refuses to
5233 run it for some reason. See why. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00005234#if defined (HAVE_HASH_BANG_EXEC)
5235 READ_SAMPLE_BUF (command, sample, sample_len);
Chet Rameyac50fba2014-02-26 09:36:43 -05005236 sample[sample_len - 1] = '\0';
Jari Aalto28ef6c32001-04-06 19:14:31 +00005237 if (sample_len > 2 && sample[0] == '#' && sample[1] == '!')
5238 {
Jari Aaltof73dda02001-11-13 17:56:06 +00005239 char *interp;
Jari Aalto95732b42005-12-07 14:08:12 +00005240 int ilen;
Jari Aaltof73dda02001-11-13 17:56:06 +00005241
5242 interp = getinterp (sample, sample_len, (int *)NULL);
Jari Aalto95732b42005-12-07 14:08:12 +00005243 ilen = strlen (interp);
Jari Aalto28ef6c32001-04-06 19:14:31 +00005244 errno = i;
Jari Aalto95732b42005-12-07 14:08:12 +00005245 if (interp[ilen - 1] == '\r')
5246 {
5247 interp = xrealloc (interp, ilen + 2);
5248 interp[ilen - 1] = '^';
5249 interp[ilen] = 'M';
5250 interp[ilen + 1] = '\0';
5251 }
Jari Aaltob80f6442004-07-27 13:29:18 +00005252 sys_error (_("%s: %s: bad interpreter"), command, interp ? interp : "");
Jari Aaltof73dda02001-11-13 17:56:06 +00005253 FREE (interp);
Jari Aalto28ef6c32001-04-06 19:14:31 +00005254 return (EX_NOEXEC);
5255 }
5256#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005257 errno = i;
5258 file_error (command);
5259 }
Jari Aaltod166f041997-06-05 14:59:13 +00005260 return ((i == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); /* XXX Posix.2 says that exit status is 126 */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005261 }
5262
5263 /* This file is executable.
5264 If it begins with #!, then help out people with losing operating
5265 systems. Otherwise, check to see if it is a binary file by seeing
Jari Aalto28ef6c32001-04-06 19:14:31 +00005266 if the contents of the first line (or up to 80 characters) are in the
5267 ASCII set. If it's a text file, execute the contents as shell commands,
5268 otherwise return 126 (EX_BINARY_FILE). */
5269 READ_SAMPLE_BUF (command, sample, sample_len);
5270
5271 if (sample_len == 0)
5272 return (EXECUTION_SUCCESS);
5273
5274 /* Is this supposed to be an executable script?
5275 If so, the format of the line is "#! interpreter [argument]".
5276 A single argument is allowed. The BSD kernel restricts
5277 the length of the entire line to 32 characters (32 bytes
5278 being the size of the BSD exec header), but we allow 80
5279 characters. */
5280 if (sample_len > 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005281 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005282#if !defined (HAVE_HASH_BANG_EXEC)
Jari Aalto28ef6c32001-04-06 19:14:31 +00005283 if (sample_len > 2 && sample[0] == '#' && sample[1] == '!')
5284 return (execute_shell_script (sample, sample_len, command, args, env));
5285 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005286#endif
Jari Aalto28ef6c32001-04-06 19:14:31 +00005287 if (check_binary_file (sample, sample_len))
5288 {
Chet Rameyac50fba2014-02-26 09:36:43 -05005289 internal_error (_("%s: cannot execute binary file: %s"), command, strerror (i));
Jari Aalto28ef6c32001-04-06 19:14:31 +00005290 return (EX_BINARY_FILE);
Jari Aalto726f6381996-08-26 18:22:31 +00005291 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005292 }
5293
Jari Aalto28ef6c32001-04-06 19:14:31 +00005294 /* We have committed to attempting to execute the contents of this file
5295 as shell commands. */
5296
Jari Aaltod166f041997-06-05 14:59:13 +00005297 initialize_subshell ();
Jari Aalto726f6381996-08-26 18:22:31 +00005298
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005299 set_sigint_handler ();
Jari Aalto726f6381996-08-26 18:22:31 +00005300
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005301 /* Insert the name of this shell into the argument list. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00005302 larray = strvec_len (args) + 1;
5303 args = strvec_resize (args, larray + 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005304
5305 for (i = larray - 1; i; i--)
5306 args[i] = args[i - 1];
5307
5308 args[0] = shell_name;
5309 args[1] = command;
5310 args[larray] = (char *)NULL;
5311
5312 if (args[0][0] == '-')
5313 args[0]++;
5314
5315#if defined (RESTRICTED_SHELL)
5316 if (restricted)
5317 change_flag ('r', FLAG_OFF);
5318#endif
5319
5320 if (subshell_argv)
5321 {
5322 /* Can't free subshell_argv[0]; that is shell_name. */
5323 for (i = 1; i < subshell_argc; i++)
5324 free (subshell_argv[i]);
5325 free (subshell_argv);
5326 }
5327
5328 dispose_command (currently_executing_command); /* XXX */
5329 currently_executing_command = (COMMAND *)NULL;
5330
5331 subshell_argc = larray;
5332 subshell_argv = args;
5333 subshell_envp = env;
5334
5335 unbind_args (); /* remove the positional parameters */
5336
5337 longjmp (subshell_top_level, 1);
Jari Aaltof73dda02001-11-13 17:56:06 +00005338 /*NOTREACHED*/
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005339}
5340
5341static int
Chet Rameyac50fba2014-02-26 09:36:43 -05005342execute_intern_function (name, funcdef)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005343 WORD_DESC *name;
Chet Rameyac50fba2014-02-26 09:36:43 -05005344 FUNCTION_DEF *funcdef;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005345{
5346 SHELL_VAR *var;
5347
5348 if (check_identifier (name, posixly_correct) == 0)
5349 {
5350 if (posixly_correct && interactive_shell == 0)
5351 {
Jari Aalto31859422009-01-12 13:36:28 +00005352 last_command_exit_value = EX_BADUSAGE;
Jari Aaltob80f6442004-07-27 13:29:18 +00005353 jump_to_top_level (ERREXIT);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005354 }
5355 return (EXECUTION_FAILURE);
5356 }
5357
Chet Rameyac50fba2014-02-26 09:36:43 -05005358 /* Posix interpretation 383 */
5359 if (posixly_correct && find_special_builtin (name->word))
5360 {
5361 internal_error (_("`%s': is a special builtin"), name->word);
5362 last_command_exit_value = EX_BADUSAGE;
5363 jump_to_top_level (ERREXIT);
5364 }
5365
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005366 var = find_function (name->word);
Jari Aalto28ef6c32001-04-06 19:14:31 +00005367 if (var && (readonly_p (var) || noassign_p (var)))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005368 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00005369 if (readonly_p (var))
Jari Aaltob80f6442004-07-27 13:29:18 +00005370 internal_error (_("%s: readonly function"), var->name);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005371 return (EXECUTION_FAILURE);
5372 }
5373
Chet Rameyac50fba2014-02-26 09:36:43 -05005374#if defined (DEBUGGER)
5375 bind_function_def (name->word, funcdef);
5376#endif
5377
5378 bind_function (name->word, funcdef->command);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005379 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +00005380}
5381
Jari Aaltod166f041997-06-05 14:59:13 +00005382#if defined (INCLUDE_UNUSED)
Jari Aalto726f6381996-08-26 18:22:31 +00005383#if defined (PROCESS_SUBSTITUTION)
Jari Aalto726f6381996-08-26 18:22:31 +00005384void
5385close_all_files ()
5386{
5387 register int i, fd_table_size;
5388
5389 fd_table_size = getdtablesize ();
5390 if (fd_table_size > 256) /* clamp to a reasonable value */
Jari Aalto7117c2d2002-07-17 14:10:11 +00005391 fd_table_size = 256;
Jari Aalto726f6381996-08-26 18:22:31 +00005392
5393 for (i = 3; i < fd_table_size; i++)
5394 close (i);
5395}
5396#endif /* PROCESS_SUBSTITUTION */
Jari Aaltod166f041997-06-05 14:59:13 +00005397#endif
Jari Aalto726f6381996-08-26 18:22:31 +00005398
5399static void
5400close_pipes (in, out)
5401 int in, out;
5402{
5403 if (in >= 0)
5404 close (in);
5405 if (out >= 0)
5406 close (out);
5407}
5408
Jari Aaltob80f6442004-07-27 13:29:18 +00005409static void
5410dup_error (oldd, newd)
5411 int oldd, newd;
5412{
5413 sys_error (_("cannot duplicate fd %d to fd %d"), oldd, newd);
5414}
5415
Jari Aalto726f6381996-08-26 18:22:31 +00005416/* Redirect input and output to be from and to the specified pipes.
5417 NO_PIPE and REDIRECT_BOTH are handled correctly. */
5418static void
5419do_piping (pipe_in, pipe_out)
5420 int pipe_in, pipe_out;
5421{
5422 if (pipe_in != NO_PIPE)
5423 {
5424 if (dup2 (pipe_in, 0) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00005425 dup_error (pipe_in, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00005426 if (pipe_in > 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00005427 close (pipe_in);
Chet Ramey495aee42011-11-22 19:11:26 -05005428#ifdef __CYGWIN__
5429 /* Let stdio know the fd may have changed from text to binary mode. */
5430 freopen (NULL, "r", stdin);
5431#endif /* __CYGWIN__ */
Jari Aalto726f6381996-08-26 18:22:31 +00005432 }
5433 if (pipe_out != NO_PIPE)
5434 {
5435 if (pipe_out != REDIRECT_BOTH)
5436 {
5437 if (dup2 (pipe_out, 1) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00005438 dup_error (pipe_out, 1);
Jari Aalto726f6381996-08-26 18:22:31 +00005439 if (pipe_out == 0 || pipe_out > 1)
5440 close (pipe_out);
Jari Aalto726f6381996-08-26 18:22:31 +00005441 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005442 else
5443 {
Jari Aaltocce855b1998-04-17 19:52:44 +00005444 if (dup2 (1, 2) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00005445 dup_error (1, 2);
Jari Aalto726f6381996-08-26 18:22:31 +00005446 }
Chet Ramey495aee42011-11-22 19:11:26 -05005447#ifdef __CYGWIN__
5448 /* Let stdio know the fd may have changed from text to binary mode, and
5449 make sure to preserve stdout line buffering. */
5450 freopen (NULL, "w", stdout);
5451 sh_setlinebuf (stdout);
5452#endif /* __CYGWIN__ */
Jari Aalto726f6381996-08-26 18:22:31 +00005453 }
Jari Aalto726f6381996-08-26 18:22:31 +00005454}