blob: ce90d25b70ab388c0a53b5842f11aa8e2808c56e [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* execute_command.c -- Execute a COMMAND structure. */
2
Jari Aalto95732b42005-12-07 14:08:12 +00003/* Copyright (C) 1987-2005 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
7 Bash is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
Jari Aaltobb706242000-03-17 21:46:59 +00009 the Free Software Foundation; either version 2, or (at your option)
Jari Aalto726f6381996-08-26 18:22:31 +000010 any later version.
11
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
Jari Aaltobb706242000-03-17 21:46:59 +000019 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000020#include "config.h"
21
22#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
Jari Aalto726f6381996-08-26 18:22:31 +000023 #pragma alloca
Jari Aaltoccc6cda1996-12-23 17:02:34 +000024#endif /* _AIX && RISC6000 && !__GNUC__ */
Jari Aalto726f6381996-08-26 18:22:31 +000025
26#include <stdio.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000027#include "chartypes.h"
Jari Aalto726f6381996-08-26 18:22:31 +000028#include "bashtypes.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000029#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000030# include <sys/file.h>
31#endif
Jari Aalto726f6381996-08-26 18:22:31 +000032#include "filecntl.h"
33#include "posixstat.h"
34#include <signal.h>
Jari Aaltocce855b1998-04-17 19:52:44 +000035#ifndef _MINIX
36# include <sys/param.h>
37#endif
Jari Aalto726f6381996-08-26 18:22:31 +000038
Jari Aaltoccc6cda1996-12-23 17:02:34 +000039#if defined (HAVE_UNISTD_H)
40# include <unistd.h>
Jari Aalto726f6381996-08-26 18:22:31 +000041#endif
42
Jari Aaltobb706242000-03-17 21:46:59 +000043#include "posixtime.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000044
Jari Aaltocce855b1998-04-17 19:52:44 +000045#if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000046# include <sys/resource.h>
47#endif
48
49#if defined (HAVE_SYS_TIMES_H) && defined (HAVE_TIMES)
50# include <sys/times.h>
51#endif
52
Jari Aalto726f6381996-08-26 18:22:31 +000053#include <errno.h>
54
55#if !defined (errno)
56extern int errno;
57#endif
58
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059#include "bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000060#include "bashintl.h"
Jari Aalto726f6381996-08-26 18:22:31 +000061
Jari Aaltoccc6cda1996-12-23 17:02:34 +000062#include "memalloc.h"
Jari Aalto726f6381996-08-26 18:22:31 +000063#include "shell.h"
Jari Aaltod166f041997-06-05 14:59:13 +000064#include <y.tab.h> /* use <...> so we pick it up from the build directory */
Jari Aalto726f6381996-08-26 18:22:31 +000065#include "flags.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000066#include "builtins.h"
67#include "hashlib.h"
Jari Aalto726f6381996-08-26 18:22:31 +000068#include "jobs.h"
69#include "execute_cmd.h"
Jari Aaltocce855b1998-04-17 19:52:44 +000070#include "findcmd.h"
71#include "redir.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000072#include "trap.h"
73#include "pathexp.h"
Jari Aaltod166f041997-06-05 14:59:13 +000074#include "hashcmd.h"
Jari Aalto726f6381996-08-26 18:22:31 +000075
Jari Aaltocce855b1998-04-17 19:52:44 +000076#if defined (COND_COMMAND)
77# include "test.h"
78#endif
79
Jari Aalto726f6381996-08-26 18:22:31 +000080#include "builtins/common.h"
81#include "builtins/builtext.h" /* list of builtins */
82
Jari Aaltof73dda02001-11-13 17:56:06 +000083#include <glob/strmatch.h>
Jari Aalto726f6381996-08-26 18:22:31 +000084#include <tilde/tilde.h>
85
86#if defined (BUFFERED_INPUT)
87# include "input.h"
88#endif
89
Jari Aaltoccc6cda1996-12-23 17:02:34 +000090#if defined (ALIAS)
91# include "alias.h"
92#endif
93
94#if defined (HISTORY)
95# include "bashhist.h"
96#endif
97
Jari Aalto726f6381996-08-26 18:22:31 +000098extern int posixly_correct;
Jari Aaltof73dda02001-11-13 17:56:06 +000099extern int breaking, continuing, loop_level;
100extern int expand_aliases;
Jari Aaltob80f6442004-07-27 13:29:18 +0000101extern int parse_and_execute_level, running_trap;
Jari Aaltof73dda02001-11-13 17:56:06 +0000102extern int command_string_index, line_number;
Jari Aalto726f6381996-08-26 18:22:31 +0000103extern int dot_found_in_search;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000104extern int already_making_children;
Jari Aaltob80f6442004-07-27 13:29:18 +0000105extern int tempenv_assign_error;
Jari Aalto726f6381996-08-26 18:22:31 +0000106extern char *the_printed_command, *shell_name;
107extern pid_t last_command_subst_pid;
Jari Aaltof73dda02001-11-13 17:56:06 +0000108extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
Jari Aalto726f6381996-08-26 18:22:31 +0000109extern char **subshell_argv, **subshell_envp;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000110extern int subshell_argc;
Jari Aaltof73dda02001-11-13 17:56:06 +0000111#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000112extern char *glob_argv_flags;
Jari Aaltof73dda02001-11-13 17:56:06 +0000113#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000114
Jari Aaltof73dda02001-11-13 17:56:06 +0000115extern int close __P((int));
Jari Aalto726f6381996-08-26 18:22:31 +0000116
117/* Static functions defined and used in this file. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000118static void close_pipes __P((int, int));
119static void do_piping __P((int, int));
120static void bind_lastarg __P((char *));
121static int shell_control_structure __P((enum command_type));
122static void cleanup_redirects __P((REDIRECT *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000123
Jari Aaltof73dda02001-11-13 17:56:06 +0000124#if defined (JOB_CONTROL)
125static int restore_signal_mask __P((sigset_t *));
126#endif
127
128static void async_redirect_stdin __P((void));
129
130static int builtin_status __P((int));
131
132static int execute_for_command __P((FOR_COM *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000133#if defined (SELECT_COMMAND)
Jari Aaltof73dda02001-11-13 17:56:06 +0000134static int print_index_and_element __P((int, int, WORD_LIST *));
135static void indent __P((int, int));
136static void print_select_list __P((WORD_LIST *, int, int, int));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000137static char *select_query __P((WORD_LIST *, int, char *, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000138static int execute_select_command __P((SELECT_COM *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000139#endif
Jari Aaltocce855b1998-04-17 19:52:44 +0000140#if defined (DPAREN_ARITHMETIC)
Jari Aaltof73dda02001-11-13 17:56:06 +0000141static int execute_arith_command __P((ARITH_COM *));
Jari Aaltocce855b1998-04-17 19:52:44 +0000142#endif
143#if defined (COND_COMMAND)
Jari Aaltof73dda02001-11-13 17:56:06 +0000144static int execute_cond_node __P((COND_COM *));
145static int execute_cond_command __P((COND_COM *));
Jari Aaltocce855b1998-04-17 19:52:44 +0000146#endif
Jari Aaltod166f041997-06-05 14:59:13 +0000147#if defined (COMMAND_TIMING)
Jari Aaltof73dda02001-11-13 17:56:06 +0000148static int mkfmt __P((char *, int, int, time_t, int));
149static void print_formatted_time __P((FILE *, char *,
150 time_t, int, time_t, int,
151 time_t, int, int));
152static int time_command __P((COMMAND *, int, int, int, struct fd_bitmap *));
Jari Aaltod166f041997-06-05 14:59:13 +0000153#endif
Jari Aaltobb706242000-03-17 21:46:59 +0000154#if defined (ARITH_FOR_COMMAND)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000155static intmax_t eval_arith_for_expr __P((WORD_LIST *, int *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000156static int execute_arith_for_command __P((ARITH_FOR_COM *));
Jari Aaltobb706242000-03-17 21:46:59 +0000157#endif
Jari Aaltof73dda02001-11-13 17:56:06 +0000158static int execute_case_command __P((CASE_COM *));
159static int execute_while_command __P((WHILE_COM *));
160static int execute_until_command __P((WHILE_COM *));
161static int execute_while_or_until __P((WHILE_COM *, int));
162static int execute_if_command __P((IF_COM *));
Jari Aalto95732b42005-12-07 14:08:12 +0000163static int execute_null_command __P((REDIRECT *, int, int, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000164static void fix_assignment_words __P((WORD_LIST *));
165static int execute_simple_command __P((SIMPLE_COM *, int, int, int, struct fd_bitmap *));
166static int execute_builtin __P((sh_builtin_func_t *, WORD_LIST *, int, int));
167static int execute_function __P((SHELL_VAR *, WORD_LIST *, int, struct fd_bitmap *, int, int));
168static int execute_builtin_or_function __P((WORD_LIST *, sh_builtin_func_t *,
169 SHELL_VAR *,
170 REDIRECT *, struct fd_bitmap *, int));
171static void execute_subshell_builtin_or_function __P((WORD_LIST *, REDIRECT *,
172 sh_builtin_func_t *,
173 SHELL_VAR *,
174 int, int, int,
175 struct fd_bitmap *,
176 int));
177static void execute_disk_command __P((WORD_LIST *, REDIRECT *, char *,
178 int, int, int, struct fd_bitmap *, int));
Jari Aalto726f6381996-08-26 18:22:31 +0000179
Jari Aaltof73dda02001-11-13 17:56:06 +0000180static char *getinterp __P((char *, int, int *));
181static void initialize_subshell __P((void));
182static int execute_in_subshell __P((COMMAND *, int, int, int, struct fd_bitmap *));
183
184static int execute_pipeline __P((COMMAND *, int, int, int, struct fd_bitmap *));
185
186static int execute_connection __P((COMMAND *, int, int, int, struct fd_bitmap *));
187
188static int execute_intern_function __P((WORD_DESC *, COMMAND *));
189
Jari Aalto726f6381996-08-26 18:22:31 +0000190/* The line number that the currently executing function starts on. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000191static int function_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +0000192
Jari Aaltod166f041997-06-05 14:59:13 +0000193/* Set to 1 if fd 0 was the subject of redirection to a subshell. Global
194 so that reader_loop can set it to zero before executing a command. */
195int stdin_redir;
Jari Aalto726f6381996-08-26 18:22:31 +0000196
197/* The name of the command that is currently being executed.
198 `test' needs this, for example. */
199char *this_command_name;
200
Jari Aaltob80f6442004-07-27 13:29:18 +0000201/* The printed representation of the currently-executing command (same as
202 the_printed_command), except when a trap is being executed. Useful for
203 a debugger to know where exactly the program is currently executing. */
204char *the_printed_command_except_trap;
205
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000206static COMMAND *currently_executing_command;
207
Jari Aalto726f6381996-08-26 18:22:31 +0000208struct stat SB; /* used for debugging */
209
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000210static int special_builtin_failed;
Jari Aaltod166f041997-06-05 14:59:13 +0000211
Jari Aaltob80f6442004-07-27 13:29:18 +0000212/* XXX - set to 1 if we're running the DEBUG trap and we want to show the line
213 number containing the function name. Used by executing_line_number to
214 report the correct line number. Kind of a hack. */
215static int showing_function_line;
216
Jari Aalto726f6381996-08-26 18:22:31 +0000217/* For catching RETURN in a function. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000218int return_catch_flag;
Jari Aalto726f6381996-08-26 18:22:31 +0000219int return_catch_value;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000220procenv_t return_catch;
Jari Aalto726f6381996-08-26 18:22:31 +0000221
222/* The value returned by the last synchronous command. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000223int last_command_exit_value;
Jari Aalto726f6381996-08-26 18:22:31 +0000224
Jari Aaltob80f6442004-07-27 13:29:18 +0000225/* Whether or not the last command (corresponding to last_command_exit_value)
226 was terminated by a signal, and, if so, which one. */
227int last_command_exit_signal;
228
Jari Aalto726f6381996-08-26 18:22:31 +0000229/* The list of redirections to perform which will undo the redirections
230 that I made in the shell. */
231REDIRECT *redirection_undo_list = (REDIRECT *)NULL;
232
233/* The list of redirections to perform which will undo the internal
234 redirections performed by the `exec' builtin. These are redirections
235 that must be undone even when exec discards redirection_undo_list. */
236REDIRECT *exec_redirection_undo_list = (REDIRECT *)NULL;
237
238/* Non-zero if we have just forked and are currently running in a subshell
239 environment. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000240int subshell_environment;
241
Jari Aaltob80f6442004-07-27 13:29:18 +0000242/* Count of nested subshells, like SHLVL. Available via $BASH_SUBSHELL */
243int subshell_level = 0;
244
Jari Aaltobb706242000-03-17 21:46:59 +0000245/* Currently-executing shell function. */
246SHELL_VAR *this_shell_function;
247
Jari Aalto95732b42005-12-07 14:08:12 +0000248/* If non-zero, matches in case and [[ ... ]] are case-insensitive */
249int match_ignore_case = 0;
250
Jari Aalto726f6381996-08-26 18:22:31 +0000251struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL;
252
Jari Aaltof73dda02001-11-13 17:56:06 +0000253#define FD_BITMAP_DEFAULT_SIZE 32
Jari Aaltod166f041997-06-05 14:59:13 +0000254
Jari Aalto726f6381996-08-26 18:22:31 +0000255/* Functions to allocate and deallocate the structures used to pass
256 information from the shell to its children about file descriptors
257 to close. */
258struct fd_bitmap *
259new_fd_bitmap (size)
Jari Aaltof73dda02001-11-13 17:56:06 +0000260 int size;
Jari Aalto726f6381996-08-26 18:22:31 +0000261{
262 struct fd_bitmap *ret;
263
264 ret = (struct fd_bitmap *)xmalloc (sizeof (struct fd_bitmap));
265
266 ret->size = size;
267
268 if (size)
269 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000270 ret->bitmap = (char *)xmalloc (size);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000271 memset (ret->bitmap, '\0', size);
Jari Aalto726f6381996-08-26 18:22:31 +0000272 }
273 else
274 ret->bitmap = (char *)NULL;
275 return (ret);
276}
277
278void
279dispose_fd_bitmap (fdbp)
280 struct fd_bitmap *fdbp;
281{
282 FREE (fdbp->bitmap);
283 free (fdbp);
284}
285
286void
287close_fd_bitmap (fdbp)
288 struct fd_bitmap *fdbp;
289{
290 register int i;
291
292 if (fdbp)
293 {
294 for (i = 0; i < fdbp->size; i++)
295 if (fdbp->bitmap[i])
296 {
297 close (i);
298 fdbp->bitmap[i] = 0;
299 }
300 }
301}
302
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000303/* Return the line number of the currently executing command. */
304int
305executing_line_number ()
306{
Jari Aaltob80f6442004-07-27 13:29:18 +0000307 if (executing && showing_function_line == 0 &&
308 (variable_context == 0 || interactive_shell == 0) &&
309 currently_executing_command)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000310 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000311#if defined (COND_COMMAND)
312 if (currently_executing_command->type == cm_cond)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000313 return currently_executing_command->value.Cond->line;
Jari Aaltob80f6442004-07-27 13:29:18 +0000314#endif
315#if defined (DPAREN_ARITHMETIC)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000316 else if (currently_executing_command->type == cm_arith)
317 return currently_executing_command->value.Arith->line;
Jari Aaltob80f6442004-07-27 13:29:18 +0000318#endif
319#if defined (ARITH_FOR_COMMAND)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000320 else if (currently_executing_command->type == cm_arith_for)
321 return currently_executing_command->value.ArithFor->line;
Jari Aaltob80f6442004-07-27 13:29:18 +0000322#endif
323
Jari Aalto7117c2d2002-07-17 14:10:11 +0000324 return line_number;
325 }
Jari Aaltod166f041997-06-05 14:59:13 +0000326 else
327 return line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000328}
329
Jari Aalto726f6381996-08-26 18:22:31 +0000330/* Execute the command passed in COMMAND. COMMAND is exactly what
331 read_command () places into GLOBAL_COMMAND. See "command.h" for the
332 details of the command structure.
333
334 EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible
335 return values. Executing a command with nothing in it returns
336 EXECUTION_SUCCESS. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000337int
Jari Aalto726f6381996-08-26 18:22:31 +0000338execute_command (command)
339 COMMAND *command;
340{
341 struct fd_bitmap *bitmap;
342 int result;
343
344 current_fds_to_close = (struct fd_bitmap *)NULL;
345 bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE);
346 begin_unwind_frame ("execute-command");
347 add_unwind_protect (dispose_fd_bitmap, (char *)bitmap);
348
349 /* Just do the command, but not asynchronously. */
350 result = execute_command_internal (command, 0, NO_PIPE, NO_PIPE, bitmap);
351
352 dispose_fd_bitmap (bitmap);
353 discard_unwind_frame ("execute-command");
354
355#if defined (PROCESS_SUBSTITUTION)
Jari Aaltobb706242000-03-17 21:46:59 +0000356 /* don't unlink fifos if we're in a shell function; wait until the function
357 returns. */
358 if (variable_context == 0)
359 unlink_fifo_list ();
Jari Aalto726f6381996-08-26 18:22:31 +0000360#endif /* PROCESS_SUBSTITUTION */
361
362 return (result);
363}
364
365/* Return 1 if TYPE is a shell control structure type. */
366static int
367shell_control_structure (type)
368 enum command_type type;
369{
370 switch (type)
371 {
Jari Aaltobb706242000-03-17 21:46:59 +0000372#if defined (ARITH_FOR_COMMAND)
373 case cm_arith_for:
374#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000375#if defined (SELECT_COMMAND)
376 case cm_select:
377#endif
Jari Aaltocce855b1998-04-17 19:52:44 +0000378#if defined (DPAREN_ARITHMETIC)
379 case cm_arith:
380#endif
381#if defined (COND_COMMAND)
382 case cm_cond:
383#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000384 case cm_case:
385 case cm_while:
386 case cm_until:
387 case cm_if:
Jari Aalto95732b42005-12-07 14:08:12 +0000388 case cm_for:
Jari Aalto726f6381996-08-26 18:22:31 +0000389 case cm_group:
Jari Aalto95732b42005-12-07 14:08:12 +0000390 case cm_function_def:
Jari Aalto726f6381996-08-26 18:22:31 +0000391 return (1);
392
393 default:
394 return (0);
395 }
396}
397
398/* A function to use to unwind_protect the redirection undo list
399 for loops. */
400static void
401cleanup_redirects (list)
402 REDIRECT *list;
403{
Jari Aaltob80f6442004-07-27 13:29:18 +0000404 do_redirections (list, RX_ACTIVE);
Jari Aalto726f6381996-08-26 18:22:31 +0000405 dispose_redirects (list);
406}
407
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000408#if 0
Jari Aalto726f6381996-08-26 18:22:31 +0000409/* Function to unwind_protect the redirections for functions and builtins. */
410static void
411cleanup_func_redirects (list)
412 REDIRECT *list;
413{
Jari Aaltob80f6442004-07-27 13:29:18 +0000414 do_redirections (list, RX_ACTIVE);
Jari Aalto726f6381996-08-26 18:22:31 +0000415}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000416#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000417
Jari Aaltocce855b1998-04-17 19:52:44 +0000418void
Jari Aalto726f6381996-08-26 18:22:31 +0000419dispose_exec_redirects ()
420{
421 if (exec_redirection_undo_list)
422 {
423 dispose_redirects (exec_redirection_undo_list);
424 exec_redirection_undo_list = (REDIRECT *)NULL;
425 }
426}
427
428#if defined (JOB_CONTROL)
429/* A function to restore the signal mask to its proper value when the shell
430 is interrupted or errors occur while creating a pipeline. */
431static int
432restore_signal_mask (set)
Jari Aaltof73dda02001-11-13 17:56:06 +0000433 sigset_t *set;
Jari Aalto726f6381996-08-26 18:22:31 +0000434{
Jari Aaltof73dda02001-11-13 17:56:06 +0000435 return (sigprocmask (SIG_SETMASK, set, (sigset_t *)NULL));
Jari Aalto726f6381996-08-26 18:22:31 +0000436}
437#endif /* JOB_CONTROL */
438
Jari Aaltof73dda02001-11-13 17:56:06 +0000439#ifdef DEBUG
Jari Aalto726f6381996-08-26 18:22:31 +0000440/* A debugging function that can be called from gdb, for instance. */
441void
442open_files ()
443{
444 register int i;
445 int f, fd_table_size;
446
447 fd_table_size = getdtablesize ();
448
Jari Aaltof73dda02001-11-13 17:56:06 +0000449 fprintf (stderr, "pid %ld open files:", (long)getpid ());
Jari Aalto726f6381996-08-26 18:22:31 +0000450 for (i = 3; i < fd_table_size; i++)
451 {
452 if ((f = fcntl (i, F_GETFD, 0)) != -1)
453 fprintf (stderr, " %d (%s)", i, f ? "close" : "open");
454 }
455 fprintf (stderr, "\n");
456}
Jari Aaltof73dda02001-11-13 17:56:06 +0000457#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000458
Jari Aaltod166f041997-06-05 14:59:13 +0000459static void
460async_redirect_stdin ()
461{
462 int fd;
463
464 fd = open ("/dev/null", O_RDONLY);
465 if (fd > 0)
466 {
467 dup2 (fd, 0);
468 close (fd);
469 }
470 else if (fd < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000471 internal_error (_("cannot redirect standard input from /dev/null: %s"), strerror (errno));
Jari Aaltod166f041997-06-05 14:59:13 +0000472}
473
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000474#define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
Jari Aalto726f6381996-08-26 18:22:31 +0000475
476/* Execute the command passed in COMMAND, perhaps doing it asynchrounously.
477 COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
478 ASYNCHROUNOUS, if non-zero, says to do this command in the background.
479 PIPE_IN and PIPE_OUT are file descriptors saying where input comes
480 from and where it goes. They can have the value of NO_PIPE, which means
481 I/O is stdin/stdout.
482 FDS_TO_CLOSE is a list of file descriptors to close once the child has
483 been forked. This list often contains the unusable sides of pipes, etc.
484
485 EXECUTION_SUCCESS or EXECUTION_FAILURE are the only possible
486 return values. Executing a command with nothing in it returns
487 EXECUTION_SUCCESS. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000488int
489execute_command_internal (command, asynchronous, pipe_in, pipe_out,
Jari Aalto726f6381996-08-26 18:22:31 +0000490 fds_to_close)
491 COMMAND *command;
492 int asynchronous;
493 int pipe_in, pipe_out;
494 struct fd_bitmap *fds_to_close;
495{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000496 int exec_result, invert, ignore_return, was_error_trap;
Jari Aaltod166f041997-06-05 14:59:13 +0000497 REDIRECT *my_undo_list, *exec_undo_list;
Jari Aalto95732b42005-12-07 14:08:12 +0000498 volatile int last_pid;
Jari Aaltob80f6442004-07-27 13:29:18 +0000499 volatile int save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +0000500
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000501 if (command == 0 || breaking || continuing || read_but_dont_execute)
Jari Aalto726f6381996-08-26 18:22:31 +0000502 return (EXECUTION_SUCCESS);
503
504 run_pending_traps ();
505
Jari Aaltob80f6442004-07-27 13:29:18 +0000506#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000507 if (running_trap == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000508#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000509 currently_executing_command = command;
510
Jari Aalto726f6381996-08-26 18:22:31 +0000511 invert = (command->flags & CMD_INVERT_RETURN) != 0;
Jari Aaltod166f041997-06-05 14:59:13 +0000512
513 /* If we're inverting the return value and `set -e' has been executed,
514 we don't want a failing command to inadvertently cause the shell
515 to exit. */
516 if (exit_immediately_on_error && invert) /* XXX */
517 command->flags |= CMD_IGNORE_RETURN; /* XXX */
518
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000519 exec_result = EXECUTION_SUCCESS;
Jari Aalto726f6381996-08-26 18:22:31 +0000520
521 /* If a command was being explicitly run in a subshell, or if it is
522 a shell control-structure, and it has a pipe, then we do the command
523 in a subshell. */
Jari Aaltobb706242000-03-17 21:46:59 +0000524 if (command->type == cm_subshell && (command->flags & CMD_NO_FORK))
525 return (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close));
Jari Aalto726f6381996-08-26 18:22:31 +0000526
Jari Aaltobb706242000-03-17 21:46:59 +0000527 if (command->type == cm_subshell ||
528 (command->flags & (CMD_WANT_SUBSHELL|CMD_FORCE_SUBSHELL)) ||
Jari Aalto726f6381996-08-26 18:22:31 +0000529 (shell_control_structure (command->type) &&
530 (pipe_out != NO_PIPE || pipe_in != NO_PIPE || asynchronous)))
531 {
532 pid_t paren_pid;
533
534 /* Fork a subshell, turn off the subshell bit, turn off job
535 control and call execute_command () on the command again. */
536 paren_pid = make_child (savestring (make_command_string (command)),
537 asynchronous);
538 if (paren_pid == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000539 exit (execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close));
540 /* NOTREACHED */
Jari Aalto726f6381996-08-26 18:22:31 +0000541 else
542 {
543 close_pipes (pipe_in, pipe_out);
544
545#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
546 unlink_fifo_list ();
547#endif
548 /* If we are part of a pipeline, and not the end of the pipeline,
549 then we should simply return and let the last command in the
550 pipe be waited for. If we are not in a pipeline, or are the
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000551 last command in the pipeline, then we wait for the subshell
Jari Aalto726f6381996-08-26 18:22:31 +0000552 and return its exit status as usual. */
553 if (pipe_out != NO_PIPE)
554 return (EXECUTION_SUCCESS);
555
556 stop_pipeline (asynchronous, (COMMAND *)NULL);
557
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000558 if (asynchronous == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000559 {
560 last_command_exit_value = wait_for (paren_pid);
561
562 /* If we have to, invert the return value. */
563 if (invert)
Jari Aaltobb706242000-03-17 21:46:59 +0000564 exec_result = ((last_command_exit_value == EXECUTION_SUCCESS)
565 ? EXECUTION_FAILURE
566 : EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000567 else
Jari Aaltobb706242000-03-17 21:46:59 +0000568 exec_result = last_command_exit_value;
569
570 return (last_command_exit_value = exec_result);
Jari Aalto726f6381996-08-26 18:22:31 +0000571 }
572 else
573 {
574 DESCRIBE_PID (paren_pid);
575
576 run_pending_traps ();
577
578 return (EXECUTION_SUCCESS);
579 }
580 }
581 }
582
Jari Aaltod166f041997-06-05 14:59:13 +0000583#if defined (COMMAND_TIMING)
584 if (command->flags & CMD_TIME_PIPELINE)
585 {
586 if (asynchronous)
587 {
588 command->flags |= CMD_FORCE_SUBSHELL;
589 exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close);
590 }
591 else
592 {
593 exec_result = time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close);
Jari Aaltob80f6442004-07-27 13:29:18 +0000594#if 0
Jari Aaltod166f041997-06-05 14:59:13 +0000595 if (running_trap == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000596#endif
Jari Aaltod166f041997-06-05 14:59:13 +0000597 currently_executing_command = (COMMAND *)NULL;
598 }
599 return (exec_result);
600 }
601#endif /* COMMAND_TIMING */
602
603 if (shell_control_structure (command->type) && command->redirects)
604 stdin_redir = stdin_redirects (command->redirects);
605
Jari Aalto726f6381996-08-26 18:22:31 +0000606 /* Handle WHILE FOR CASE etc. with redirections. (Also '&' input
607 redirection.) */
Jari Aaltob80f6442004-07-27 13:29:18 +0000608 if (do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE) != 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000609 {
610 cleanup_redirects (redirection_undo_list);
611 redirection_undo_list = (REDIRECT *)NULL;
612 dispose_exec_redirects ();
613 return (EXECUTION_FAILURE);
614 }
615
616 if (redirection_undo_list)
617 {
618 my_undo_list = (REDIRECT *)copy_redirects (redirection_undo_list);
619 dispose_redirects (redirection_undo_list);
620 redirection_undo_list = (REDIRECT *)NULL;
621 }
622 else
623 my_undo_list = (REDIRECT *)NULL;
624
625 if (exec_redirection_undo_list)
626 {
627 exec_undo_list = (REDIRECT *)copy_redirects (exec_redirection_undo_list);
628 dispose_redirects (exec_redirection_undo_list);
629 exec_redirection_undo_list = (REDIRECT *)NULL;
630 }
631 else
632 exec_undo_list = (REDIRECT *)NULL;
633
634 if (my_undo_list || exec_undo_list)
635 begin_unwind_frame ("loop_redirections");
636
637 if (my_undo_list)
638 add_unwind_protect ((Function *)cleanup_redirects, my_undo_list);
639
640 if (exec_undo_list)
641 add_unwind_protect ((Function *)dispose_redirects, exec_undo_list);
642
643 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
644
645 QUIT;
646
647 switch (command->type)
648 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000649 case cm_simple:
650 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000651 save_line_number = line_number;
652 /* We can't rely on variables retaining their values across a
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000653 call to execute_simple_command if a longjmp occurs as the
654 result of a `return' builtin. This is true for sure with gcc. */
Jari Aalto95732b42005-12-07 14:08:12 +0000655#if defined (RECYCLES_PIDS)
656 last_made_pid = NO_PID;
657#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000658 last_pid = last_made_pid;
Jari Aaltof73dda02001-11-13 17:56:06 +0000659 was_error_trap = signal_is_trapped (ERROR_TRAP) && signal_is_ignored (ERROR_TRAP) == 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000660
661 if (ignore_return && command->value.Simple)
662 command->value.Simple->flags |= CMD_IGNORE_RETURN;
Jari Aaltod166f041997-06-05 14:59:13 +0000663 if (command->flags & CMD_STDIN_REDIR)
664 command->value.Simple->flags |= CMD_STDIN_REDIR;
Jari Aaltob80f6442004-07-27 13:29:18 +0000665
666 line_number = command->value.Simple->line;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000667 exec_result =
668 execute_simple_command (command->value.Simple, pipe_in, pipe_out,
669 asynchronous, fds_to_close);
Jari Aaltob80f6442004-07-27 13:29:18 +0000670 line_number = save_line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000671
672 /* The temporary environment should be used for only the simple
673 command immediately following its definition. */
674 dispose_used_env_vars ();
675
676#if (defined (ultrix) && defined (mips)) || defined (C_ALLOCA)
677 /* Reclaim memory allocated with alloca () on machines which
678 may be using the alloca emulation code. */
679 (void) alloca (0);
680#endif /* (ultrix && mips) || C_ALLOCA */
681
682 /* If we forked to do the command, then we must wait_for ()
683 the child. */
684
685 /* XXX - this is something to watch out for if there are problems
686 when the shell is compiled without job control. */
687 if (already_making_children && pipe_out == NO_PIPE &&
Jari Aalto95732b42005-12-07 14:08:12 +0000688 last_made_pid != last_pid)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000689 {
690 stop_pipeline (asynchronous, (COMMAND *)NULL);
691
692 if (asynchronous)
693 {
694 DESCRIBE_PID (last_made_pid);
695 }
696 else
697#if !defined (JOB_CONTROL)
698 /* Do not wait for asynchronous processes started from
699 startup files. */
700 if (last_made_pid != last_asynchronous_pid)
701#endif
702 /* When executing a shell function that executes other
703 commands, this causes the last simple command in
Jari Aaltob80f6442004-07-27 13:29:18 +0000704 the function to be waited for twice. This also causes
705 subshells forked to execute builtin commands (e.g., in
706 pipelines) to be waited for twice. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000707 exec_result = wait_for (last_made_pid);
708 }
709 }
710
Jari Aaltof73dda02001-11-13 17:56:06 +0000711 if (was_error_trap && ignore_return == 0 && invert == 0 && exec_result != EXECUTION_SUCCESS)
712 {
713 last_command_exit_value = exec_result;
714 run_error_trap ();
715 }
716
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000717 if (ignore_return == 0 && invert == 0 &&
Jari Aaltocce855b1998-04-17 19:52:44 +0000718 ((posixly_correct && interactive == 0 && special_builtin_failed) ||
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000719 (exit_immediately_on_error && (exec_result != EXECUTION_SUCCESS))))
720 {
721 last_command_exit_value = exec_result;
722 run_pending_traps ();
Jari Aaltob80f6442004-07-27 13:29:18 +0000723 jump_to_top_level (ERREXIT);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000724 }
725
726 break;
727
Jari Aalto726f6381996-08-26 18:22:31 +0000728 case cm_for:
729 if (ignore_return)
730 command->value.For->flags |= CMD_IGNORE_RETURN;
731 exec_result = execute_for_command (command->value.For);
732 break;
733
Jari Aaltobb706242000-03-17 21:46:59 +0000734#if defined (ARITH_FOR_COMMAND)
735 case cm_arith_for:
736 if (ignore_return)
737 command->value.ArithFor->flags |= CMD_IGNORE_RETURN;
738 exec_result = execute_arith_for_command (command->value.ArithFor);
739 break;
740#endif
741
Jari Aalto726f6381996-08-26 18:22:31 +0000742#if defined (SELECT_COMMAND)
743 case cm_select:
744 if (ignore_return)
745 command->value.Select->flags |= CMD_IGNORE_RETURN;
746 exec_result = execute_select_command (command->value.Select);
747 break;
748#endif
749
750 case cm_case:
751 if (ignore_return)
752 command->value.Case->flags |= CMD_IGNORE_RETURN;
753 exec_result = execute_case_command (command->value.Case);
754 break;
755
756 case cm_while:
757 if (ignore_return)
758 command->value.While->flags |= CMD_IGNORE_RETURN;
759 exec_result = execute_while_command (command->value.While);
760 break;
761
762 case cm_until:
763 if (ignore_return)
764 command->value.While->flags |= CMD_IGNORE_RETURN;
765 exec_result = execute_until_command (command->value.While);
766 break;
767
768 case cm_if:
769 if (ignore_return)
770 command->value.If->flags |= CMD_IGNORE_RETURN;
771 exec_result = execute_if_command (command->value.If);
772 break;
773
774 case cm_group:
775
776 /* This code can be executed from either of two paths: an explicit
777 '{}' command, or via a function call. If we are executed via a
778 function call, we have already taken care of the function being
779 executed in the background (down there in execute_simple_command ()),
780 and this command should *not* be marked as asynchronous. If we
781 are executing a regular '{}' group command, and asynchronous == 1,
782 we must want to execute the whole command in the background, so we
783 need a subshell, and we want the stuff executed in that subshell
784 (this group command) to be executed in the foreground of that
785 subshell (i.e. there will not be *another* subshell forked).
786
787 What we do is to force a subshell if asynchronous, and then call
788 execute_command_internal again with asynchronous still set to 1,
789 but with the original group command, so the printed command will
790 look right.
791
792 The code above that handles forking off subshells will note that
793 both subshell and async are on, and turn off async in the child
794 after forking the subshell (but leave async set in the parent, so
795 the normal call to describe_pid is made). This turning off
796 async is *crucial*; if it is not done, this will fall into an
797 infinite loop of executions through this spot in subshell after
798 subshell until the process limit is exhausted. */
799
800 if (asynchronous)
801 {
802 command->flags |= CMD_FORCE_SUBSHELL;
803 exec_result =
804 execute_command_internal (command, 1, pipe_in, pipe_out,
805 fds_to_close);
806 }
807 else
808 {
809 if (ignore_return && command->value.Group->command)
810 command->value.Group->command->flags |= CMD_IGNORE_RETURN;
811 exec_result =
812 execute_command_internal (command->value.Group->command,
813 asynchronous, pipe_in, pipe_out,
814 fds_to_close);
815 }
816 break;
817
Jari Aalto726f6381996-08-26 18:22:31 +0000818 case cm_connection:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000819 exec_result = execute_connection (command, asynchronous,
820 pipe_in, pipe_out, fds_to_close);
Jari Aalto726f6381996-08-26 18:22:31 +0000821 break;
822
Jari Aaltocce855b1998-04-17 19:52:44 +0000823#if defined (DPAREN_ARITHMETIC)
824 case cm_arith:
825 if (ignore_return)
826 command->value.Arith->flags |= CMD_IGNORE_RETURN;
827 exec_result = execute_arith_command (command->value.Arith);
828 break;
829#endif
830
831#if defined (COND_COMMAND)
832 case cm_cond:
833 if (ignore_return)
834 command->value.Cond->flags |= CMD_IGNORE_RETURN;
Jari Aaltob80f6442004-07-27 13:29:18 +0000835 save_line_number = line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +0000836 exec_result = execute_cond_command (command->value.Cond);
Jari Aaltob80f6442004-07-27 13:29:18 +0000837 line_number = save_line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +0000838 break;
839#endif
840
Jari Aalto726f6381996-08-26 18:22:31 +0000841 case cm_function_def:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000842 exec_result = execute_intern_function (command->value.Function_def->name,
843 command->value.Function_def->command);
Jari Aalto726f6381996-08-26 18:22:31 +0000844 break;
845
846 default:
Jari Aaltob72432f1999-02-19 17:11:39 +0000847 command_error ("execute_command", CMDERR_BADTYPE, command->type, 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000848 }
849
850 if (my_undo_list)
851 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000852 do_redirections (my_undo_list, RX_ACTIVE);
Jari Aalto726f6381996-08-26 18:22:31 +0000853 dispose_redirects (my_undo_list);
854 }
855
856 if (exec_undo_list)
857 dispose_redirects (exec_undo_list);
858
859 if (my_undo_list || exec_undo_list)
860 discard_unwind_frame ("loop_redirections");
861
862 /* Invert the return value if we have to */
863 if (invert)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000864 exec_result = (exec_result == EXECUTION_SUCCESS)
865 ? EXECUTION_FAILURE
866 : EXECUTION_SUCCESS;
Jari Aalto726f6381996-08-26 18:22:31 +0000867
868 last_command_exit_value = exec_result;
869 run_pending_traps ();
Jari Aaltob80f6442004-07-27 13:29:18 +0000870#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000871 if (running_trap == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000872#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000873 currently_executing_command = (COMMAND *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000874 return (last_command_exit_value);
875}
876
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000877#if defined (COMMAND_TIMING)
Jari Aaltobb706242000-03-17 21:46:59 +0000878
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000879#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
Jari Aaltof73dda02001-11-13 17:56:06 +0000880extern struct timeval *difftimeval __P((struct timeval *, struct timeval *, struct timeval *));
881extern struct timeval *addtimeval __P((struct timeval *, struct timeval *, struct timeval *));
882extern int timeval_to_cpu __P((struct timeval *, struct timeval *, struct timeval *));
Jari Aaltobb706242000-03-17 21:46:59 +0000883#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000884
885#define POSIX_TIMEFORMAT "real %2R\nuser %2U\nsys %2S"
886#define BASH_TIMEFORMAT "\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS"
887
888static int precs[] = { 0, 100, 10, 1 };
889
890/* Expand one `%'-prefixed escape sequence from a time format string. */
891static int
892mkfmt (buf, prec, lng, sec, sec_fraction)
893 char *buf;
894 int prec, lng;
Jari Aaltof73dda02001-11-13 17:56:06 +0000895 time_t sec;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000896 int sec_fraction;
897{
Jari Aaltof73dda02001-11-13 17:56:06 +0000898 time_t min;
899 char abuf[INT_STRLEN_BOUND(time_t) + 1];
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000900 int ind, aind;
901
902 ind = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +0000903 abuf[sizeof(abuf) - 1] = '\0';
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000904
905 /* If LNG is non-zero, we want to decompose SEC into minutes and seconds. */
906 if (lng)
907 {
908 min = sec / 60;
909 sec %= 60;
Jari Aaltof73dda02001-11-13 17:56:06 +0000910 aind = sizeof(abuf) - 2;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000911 do
912 abuf[aind--] = (min % 10) + '0';
913 while (min /= 10);
914 aind++;
915 while (abuf[aind])
Jari Aaltocce855b1998-04-17 19:52:44 +0000916 buf[ind++] = abuf[aind++];
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000917 buf[ind++] = 'm';
918 }
919
920 /* Now add the seconds. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000921 aind = sizeof (abuf) - 2;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000922 do
923 abuf[aind--] = (sec % 10) + '0';
924 while (sec /= 10);
925 aind++;
926 while (abuf[aind])
927 buf[ind++] = abuf[aind++];
928
929 /* We want to add a decimal point and PREC places after it if PREC is
930 nonzero. PREC is not greater than 3. SEC_FRACTION is between 0
931 and 999. */
932 if (prec != 0)
933 {
934 buf[ind++] = '.';
935 for (aind = 1; aind <= prec; aind++)
936 {
937 buf[ind++] = (sec_fraction / precs[aind]) + '0';
938 sec_fraction %= precs[aind];
939 }
940 }
941
942 if (lng)
943 buf[ind++] = 's';
944 buf[ind] = '\0';
945
946 return (ind);
947}
948
949/* Interpret the format string FORMAT, interpolating the following escape
950 sequences:
Jari Aalto7117c2d2002-07-17 14:10:11 +0000951 %[prec][l][RUS]
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000952
953 where the optional `prec' is a precision, meaning the number of
954 characters after the decimal point, the optional `l' means to format
955 using minutes and seconds (MMmNN[.FF]s), like the `times' builtin',
956 and the last character is one of
957
Jari Aalto7117c2d2002-07-17 14:10:11 +0000958 R number of seconds of `real' time
959 U number of seconds of `user' time
960 S number of seconds of `system' time
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000961
962 An occurrence of `%%' in the format string is translated to a `%'. The
963 result is printed to FP, a pointer to a FILE. The other variables are
964 the seconds and thousandths of a second of real, user, and system time,
965 resectively. */
966static void
967print_formatted_time (fp, format, rs, rsf, us, usf, ss, ssf, cpu)
968 FILE *fp;
969 char *format;
Jari Aaltof73dda02001-11-13 17:56:06 +0000970 time_t rs;
971 int rsf;
972 time_t us;
973 int usf;
974 time_t ss;
975 int ssf, cpu;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000976{
977 int prec, lng, len;
Jari Aaltof73dda02001-11-13 17:56:06 +0000978 char *str, *s, ts[INT_STRLEN_BOUND (time_t) + sizeof ("mSS.FFFF")];
979 time_t sum;
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000980 int sum_frac;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000981 int sindex, ssize;
982
983 len = strlen (format);
984 ssize = (len + 64) - (len % 64);
Jari Aaltof73dda02001-11-13 17:56:06 +0000985 str = (char *)xmalloc (ssize);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000986 sindex = 0;
987
988 for (s = format; *s; s++)
989 {
990 if (*s != '%' || s[1] == '\0')
Jari Aaltocce855b1998-04-17 19:52:44 +0000991 {
992 RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64);
993 str[sindex++] = *s;
994 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000995 else if (s[1] == '%')
Jari Aaltocce855b1998-04-17 19:52:44 +0000996 {
997 s++;
998 RESIZE_MALLOCED_BUFFER (str, sindex, 1, ssize, 64);
999 str[sindex++] = *s;
1000 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001001 else if (s[1] == 'P')
1002 {
1003 s++;
1004 if (cpu > 10000)
1005 cpu = 10000;
1006 sum = cpu / 100;
1007 sum_frac = (cpu % 100) * 10;
1008 len = mkfmt (ts, 2, 0, sum, sum_frac);
1009 RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64);
1010 strcpy (str + sindex, ts);
1011 sindex += len;
1012 }
1013 else
1014 {
1015 prec = 3; /* default is three places past the decimal point. */
1016 lng = 0; /* default is to not use minutes or append `s' */
1017 s++;
Jari Aaltof73dda02001-11-13 17:56:06 +00001018 if (DIGIT (*s)) /* `precision' */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001019 {
1020 prec = *s++ - '0';
1021 if (prec > 3) prec = 3;
1022 }
1023 if (*s == 'l') /* `length extender' */
1024 {
1025 lng = 1;
1026 s++;
1027 }
1028 if (*s == 'R' || *s == 'E')
1029 len = mkfmt (ts, prec, lng, rs, rsf);
1030 else if (*s == 'U')
1031 len = mkfmt (ts, prec, lng, us, usf);
1032 else if (*s == 'S')
1033 len = mkfmt (ts, prec, lng, ss, ssf);
1034 else
1035 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001036 internal_error (_("TIMEFORMAT: `%c': invalid format character"), *s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001037 free (str);
1038 return;
1039 }
1040 RESIZE_MALLOCED_BUFFER (str, sindex, len, ssize, 64);
1041 strcpy (str + sindex, ts);
1042 sindex += len;
1043 }
1044 }
1045
1046 str[sindex] = '\0';
1047 fprintf (fp, "%s\n", str);
1048 fflush (fp);
1049
1050 free (str);
1051}
1052
1053static int
1054time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close)
1055 COMMAND *command;
1056 int asynchronous, pipe_in, pipe_out;
1057 struct fd_bitmap *fds_to_close;
1058{
Jari Aaltod166f041997-06-05 14:59:13 +00001059 int rv, posix_time, old_flags;
Jari Aaltof73dda02001-11-13 17:56:06 +00001060 time_t rs, us, ss;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001061 int rsf, usf, ssf;
1062 int cpu;
1063 char *time_format;
1064
1065#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
1066 struct timeval real, user, sys;
1067 struct timeval before, after;
Jari Aaltob80f6442004-07-27 13:29:18 +00001068# if defined (HAVE_STRUCT_TIMEZONE)
1069 struct timezone dtz; /* posix doesn't define this */
1070# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001071 struct rusage selfb, selfa, kidsb, kidsa; /* a = after, b = before */
1072#else
1073# if defined (HAVE_TIMES)
1074 clock_t tbefore, tafter, real, user, sys;
1075 struct tms before, after;
1076# endif
1077#endif
1078
1079#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
Jari Aaltob80f6442004-07-27 13:29:18 +00001080# if defined (HAVE_STRUCT_TIMEZONE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001081 gettimeofday (&before, &dtz);
Jari Aaltob80f6442004-07-27 13:29:18 +00001082# else
1083 gettimeofday (&before, (void *)NULL);
1084# endif /* !HAVE_STRUCT_TIMEZONE */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001085 getrusage (RUSAGE_SELF, &selfb);
1086 getrusage (RUSAGE_CHILDREN, &kidsb);
1087#else
1088# if defined (HAVE_TIMES)
1089 tbefore = times (&before);
1090# endif
1091#endif
1092
1093 posix_time = (command->flags & CMD_TIME_POSIX);
1094
Jari Aaltod166f041997-06-05 14:59:13 +00001095 old_flags = command->flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001096 command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX);
1097 rv = execute_command_internal (command, asynchronous, pipe_in, pipe_out, fds_to_close);
Jari Aaltod166f041997-06-05 14:59:13 +00001098 command->flags = old_flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001099
Jari Aaltof73dda02001-11-13 17:56:06 +00001100 rs = us = ss = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +00001101 rsf = usf = ssf = cpu = 0;
1102
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001103#if defined (HAVE_GETRUSAGE) && defined (HAVE_GETTIMEOFDAY)
Jari Aaltob80f6442004-07-27 13:29:18 +00001104# if defined (HAVE_STRUCT_TIMEZONE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001105 gettimeofday (&after, &dtz);
Jari Aaltob80f6442004-07-27 13:29:18 +00001106# else
1107 gettimeofday (&after, (void *)NULL);
1108# endif /* !HAVE_STRUCT_TIMEZONE */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001109 getrusage (RUSAGE_SELF, &selfa);
1110 getrusage (RUSAGE_CHILDREN, &kidsa);
1111
1112 difftimeval (&real, &before, &after);
1113 timeval_to_secs (&real, &rs, &rsf);
1114
1115 addtimeval (&user, difftimeval(&after, &selfb.ru_utime, &selfa.ru_utime),
1116 difftimeval(&before, &kidsb.ru_utime, &kidsa.ru_utime));
1117 timeval_to_secs (&user, &us, &usf);
1118
1119 addtimeval (&sys, difftimeval(&after, &selfb.ru_stime, &selfa.ru_stime),
1120 difftimeval(&before, &kidsb.ru_stime, &kidsa.ru_stime));
1121 timeval_to_secs (&sys, &ss, &ssf);
1122
1123 cpu = timeval_to_cpu (&real, &user, &sys);
1124#else
1125# if defined (HAVE_TIMES)
1126 tafter = times (&after);
1127
1128 real = tafter - tbefore;
1129 clock_t_to_secs (real, &rs, &rsf);
1130
1131 user = (after.tms_utime - before.tms_utime) + (after.tms_cutime - before.tms_cutime);
1132 clock_t_to_secs (user, &us, &usf);
1133
1134 sys = (after.tms_stime - before.tms_stime) + (after.tms_cstime - before.tms_cstime);
1135 clock_t_to_secs (sys, &ss, &ssf);
1136
Jari Aaltod166f041997-06-05 14:59:13 +00001137 cpu = (real == 0) ? 0 : ((user + sys) * 10000) / real;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001138
1139# else
Jari Aaltof73dda02001-11-13 17:56:06 +00001140 rs = us = ss = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001141 rsf = usf = ssf = cpu = 0;
1142# endif
1143#endif
1144
1145 if (posix_time)
1146 time_format = POSIX_TIMEFORMAT;
1147 else if ((time_format = get_string_value ("TIMEFORMAT")) == 0)
1148 time_format = BASH_TIMEFORMAT;
1149
1150 if (time_format && *time_format)
1151 print_formatted_time (stderr, time_format, rs, rsf, us, usf, ss, ssf, cpu);
1152
1153 return rv;
1154}
1155#endif /* COMMAND_TIMING */
1156
Jari Aaltobb706242000-03-17 21:46:59 +00001157/* Execute a command that's supposed to be in a subshell. This must be
Jari Aalto28ef6c32001-04-06 19:14:31 +00001158 called after make_child and we must be running in the child process.
1159 The caller will return or exit() immediately with the value this returns. */
Jari Aaltobb706242000-03-17 21:46:59 +00001160static int
1161execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close)
1162 COMMAND *command;
1163 int asynchronous;
1164 int pipe_in, pipe_out;
1165 struct fd_bitmap *fds_to_close;
1166{
Jari Aalto28ef6c32001-04-06 19:14:31 +00001167 int user_subshell, return_code, function_value, should_redir_stdin, invert;
1168 int ois;
Jari Aaltobb706242000-03-17 21:46:59 +00001169 COMMAND *tcom;
1170
Jari Aaltof73dda02001-11-13 17:56:06 +00001171 USE_VAR(user_subshell);
1172 USE_VAR(invert);
1173 USE_VAR(tcom);
1174 USE_VAR(asynchronous);
1175
Jari Aaltob80f6442004-07-27 13:29:18 +00001176 subshell_level++;
Jari Aaltobb706242000-03-17 21:46:59 +00001177 should_redir_stdin = (asynchronous && (command->flags & CMD_STDIN_REDIR) &&
1178 pipe_in == NO_PIPE &&
1179 stdin_redirects (command->redirects) == 0);
1180
Jari Aalto28ef6c32001-04-06 19:14:31 +00001181 invert = (command->flags & CMD_INVERT_RETURN) != 0;
Jari Aaltobb706242000-03-17 21:46:59 +00001182 user_subshell = command->type == cm_subshell || ((command->flags & CMD_WANT_SUBSHELL) != 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +00001183
Jari Aaltobb706242000-03-17 21:46:59 +00001184 command->flags &= ~(CMD_FORCE_SUBSHELL | CMD_WANT_SUBSHELL | CMD_INVERT_RETURN);
1185
1186 /* If a command is asynchronous in a subshell (like ( foo ) & or
1187 the special case of an asynchronous GROUP command where the
1188 the subshell bit is turned on down in case cm_group: below),
1189 turn off `asynchronous', so that two subshells aren't spawned.
1190
1191 This seems semantically correct to me. For example,
1192 ( foo ) & seems to say ``do the command `foo' in a subshell
1193 environment, but don't wait for that subshell to finish'',
1194 and "{ foo ; bar ; } &" seems to me to be like functions or
1195 builtins in the background, which executed in a subshell
1196 environment. I just don't see the need to fork two subshells. */
1197
1198 /* Don't fork again, we are already in a subshell. A `doubly
1199 async' shell is not interactive, however. */
1200 if (asynchronous)
1201 {
1202#if defined (JOB_CONTROL)
1203 /* If a construct like ( exec xxx yyy ) & is given while job
1204 control is active, we want to prevent exec from putting the
1205 subshell back into the original process group, carefully
1206 undoing all the work we just did in make_child. */
1207 original_pgrp = -1;
1208#endif /* JOB_CONTROL */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001209 ois = interactive_shell;
Jari Aaltobb706242000-03-17 21:46:59 +00001210 interactive_shell = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001211 /* This test is to prevent alias expansion by interactive shells that
1212 run `(command) &' but to allow scripts that have enabled alias
1213 expansion with `shopt -s expand_alias' to continue to expand
1214 aliases. */
1215 if (ois != interactive_shell)
1216 expand_aliases = 0;
Jari Aaltobb706242000-03-17 21:46:59 +00001217 asynchronous = 0;
1218 }
1219
1220 /* Subshells are neither login nor interactive. */
1221 login_shell = interactive = 0;
1222
1223 subshell_environment = user_subshell ? SUBSHELL_PAREN : SUBSHELL_ASYNC;
1224
1225 reset_terminating_signals (); /* in sig.c */
1226 /* Cancel traps, in trap.c. */
1227 restore_original_signals ();
1228 if (asynchronous)
1229 setup_async_signals ();
1230
1231#if defined (JOB_CONTROL)
1232 set_sigchld_handler ();
1233#endif /* JOB_CONTROL */
1234
1235 set_sigint_handler ();
1236
1237#if defined (JOB_CONTROL)
1238 /* Delete all traces that there were any jobs running. This is
1239 only for subshells. */
1240 without_job_control ();
1241#endif /* JOB_CONTROL */
1242
1243 if (fds_to_close)
1244 close_fd_bitmap (fds_to_close);
1245
1246 do_piping (pipe_in, pipe_out);
1247
1248 /* If this is a user subshell, set a flag if stdin was redirected.
1249 This is used later to decide whether to redirect fd 0 to
1250 /dev/null for async commands in the subshell. This adds more
1251 sh compatibility, but I'm not sure it's the right thing to do. */
1252 if (user_subshell)
1253 {
1254 stdin_redir = stdin_redirects (command->redirects);
1255 restore_default_signal (0);
1256 }
1257
1258 /* If this is an asynchronous command (command &), we want to
1259 redirect the standard input from /dev/null in the absence of
1260 any specific redirection involving stdin. */
1261 if (should_redir_stdin && stdin_redir == 0)
1262 async_redirect_stdin ();
1263
1264 /* Do redirections, then dispose of them before recursive call. */
1265 if (command->redirects)
1266 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001267 if (do_redirections (command->redirects, RX_ACTIVE) != 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001268 exit (invert ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
Jari Aaltobb706242000-03-17 21:46:59 +00001269
1270 dispose_redirects (command->redirects);
1271 command->redirects = (REDIRECT *)NULL;
1272 }
1273
1274 tcom = (command->type == cm_subshell) ? command->value.Subshell->command : command;
1275
Jari Aalto95732b42005-12-07 14:08:12 +00001276 if (command->flags & CMD_TIME_PIPELINE)
1277 tcom->flags |= CMD_TIME_PIPELINE;
1278 if (command->flags & CMD_TIME_POSIX)
1279 tcom->flags |= CMD_TIME_POSIX;
1280
Jari Aaltof73dda02001-11-13 17:56:06 +00001281 /* Make sure the subshell inherits any CMD_IGNORE_RETURN flag. */
1282 if ((command->flags & CMD_IGNORE_RETURN) && tcom != command)
1283 tcom->flags |= CMD_IGNORE_RETURN;
1284
Jari Aaltobb706242000-03-17 21:46:59 +00001285 /* If this is a simple command, tell execute_disk_command that it
1286 might be able to get away without forking and simply exec.
1287 This means things like ( sleep 10 ) will only cause one fork.
Jari Aalto28ef6c32001-04-06 19:14:31 +00001288 If we're timing the command or inverting its return value, however,
1289 we cannot do this optimization. */
Jari Aaltobb706242000-03-17 21:46:59 +00001290 if (user_subshell && (tcom->type == cm_simple || tcom->type == cm_subshell) &&
Jari Aalto28ef6c32001-04-06 19:14:31 +00001291 ((tcom->flags & CMD_TIME_PIPELINE) == 0) &&
1292 ((tcom->flags & CMD_INVERT_RETURN) == 0))
Jari Aaltobb706242000-03-17 21:46:59 +00001293 {
1294 tcom->flags |= CMD_NO_FORK;
1295 if (tcom->type == cm_simple)
1296 tcom->value.Simple->flags |= CMD_NO_FORK;
1297 }
1298
Jari Aalto28ef6c32001-04-06 19:14:31 +00001299 invert = (tcom->flags & CMD_INVERT_RETURN) != 0;
1300 tcom->flags &= ~CMD_INVERT_RETURN;
1301
Jari Aaltobb706242000-03-17 21:46:59 +00001302 /* If we're inside a function while executing this subshell, we
1303 need to handle a possible `return'. */
1304 function_value = 0;
1305 if (return_catch_flag)
1306 function_value = setjmp (return_catch);
1307
1308 if (function_value)
1309 return_code = return_catch_value;
1310 else
1311 return_code = execute_command_internal
1312 (tcom, asynchronous, NO_PIPE, NO_PIPE, fds_to_close);
1313
Jari Aalto28ef6c32001-04-06 19:14:31 +00001314 /* If we are asked to, invert the return value. */
1315 if (invert)
1316 return_code = (return_code == EXECUTION_SUCCESS) ? EXECUTION_FAILURE
1317 : EXECUTION_SUCCESS;
1318
Jari Aaltobb706242000-03-17 21:46:59 +00001319 /* If we were explicitly placed in a subshell with (), we need
1320 to do the `shell cleanup' things, such as running traps[0]. */
1321 if (user_subshell && signal_is_trapped (0))
1322 {
1323 last_command_exit_value = return_code;
1324 return_code = run_exit_trap ();
1325 }
1326
Jari Aaltob80f6442004-07-27 13:29:18 +00001327 subshell_level--;
Jari Aaltobb706242000-03-17 21:46:59 +00001328 return (return_code);
1329 /* NOTREACHED */
1330}
1331
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001332static int
1333execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close)
1334 COMMAND *command;
1335 int asynchronous, pipe_in, pipe_out;
1336 struct fd_bitmap *fds_to_close;
1337{
1338 int prev, fildes[2], new_bitmap_size, dummyfd, ignore_return, exec_result;
1339 COMMAND *cmd;
1340 struct fd_bitmap *fd_bitmap;
1341
1342#if defined (JOB_CONTROL)
1343 sigset_t set, oset;
1344 BLOCK_CHILD (set, oset);
1345#endif /* JOB_CONTROL */
1346
1347 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
1348
1349 prev = pipe_in;
1350 cmd = command;
1351
1352 while (cmd && cmd->type == cm_connection &&
1353 cmd->value.Connection && cmd->value.Connection->connector == '|')
1354 {
1355 /* Make a pipeline between the two commands. */
1356 if (pipe (fildes) < 0)
1357 {
1358 sys_error ("pipe error");
1359#if defined (JOB_CONTROL)
1360 terminate_current_pipeline ();
1361 kill_current_pipeline ();
Jari Aalto95732b42005-12-07 14:08:12 +00001362 UNBLOCK_CHILD (oset);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001363#endif /* JOB_CONTROL */
1364 last_command_exit_value = EXECUTION_FAILURE;
1365 /* The unwind-protects installed below will take care
1366 of closing all of the open file descriptors. */
1367 throw_to_top_level ();
1368 return (EXECUTION_FAILURE); /* XXX */
1369 }
1370
1371 /* Here is a problem: with the new file close-on-exec
1372 code, the read end of the pipe (fildes[0]) stays open
1373 in the first process, so that process will never get a
1374 SIGPIPE. There is no way to signal the first process
1375 that it should close fildes[0] after forking, so it
1376 remains open. No SIGPIPE is ever sent because there
1377 is still a file descriptor open for reading connected
1378 to the pipe. We take care of that here. This passes
1379 around a bitmap of file descriptors that must be
1380 closed after making a child process in execute_simple_command. */
1381
1382 /* We need fd_bitmap to be at least as big as fildes[0].
1383 If fildes[0] is less than fds_to_close->size, then
1384 use fds_to_close->size. */
1385 new_bitmap_size = (fildes[0] < fds_to_close->size)
1386 ? fds_to_close->size
1387 : fildes[0] + 8;
1388
1389 fd_bitmap = new_fd_bitmap (new_bitmap_size);
1390
1391 /* Now copy the old information into the new bitmap. */
1392 xbcopy ((char *)fds_to_close->bitmap, (char *)fd_bitmap->bitmap, fds_to_close->size);
1393
1394 /* And mark the pipe file descriptors to be closed. */
1395 fd_bitmap->bitmap[fildes[0]] = 1;
1396
1397 /* In case there are pipe or out-of-processes errors, we
Jari Aaltocce855b1998-04-17 19:52:44 +00001398 want all these file descriptors to be closed when
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001399 unwind-protects are run, and the storage used for the
1400 bitmaps freed up. */
1401 begin_unwind_frame ("pipe-file-descriptors");
1402 add_unwind_protect (dispose_fd_bitmap, fd_bitmap);
1403 add_unwind_protect (close_fd_bitmap, fd_bitmap);
1404 if (prev >= 0)
1405 add_unwind_protect (close, prev);
1406 dummyfd = fildes[1];
1407 add_unwind_protect (close, dummyfd);
1408
1409#if defined (JOB_CONTROL)
Jari Aaltof73dda02001-11-13 17:56:06 +00001410 add_unwind_protect (restore_signal_mask, &oset);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001411#endif /* JOB_CONTROL */
1412
1413 if (ignore_return && cmd->value.Connection->first)
1414 cmd->value.Connection->first->flags |= CMD_IGNORE_RETURN;
1415 execute_command_internal (cmd->value.Connection->first, asynchronous,
1416 prev, fildes[1], fd_bitmap);
1417
1418 if (prev >= 0)
1419 close (prev);
1420
1421 prev = fildes[0];
1422 close (fildes[1]);
1423
1424 dispose_fd_bitmap (fd_bitmap);
1425 discard_unwind_frame ("pipe-file-descriptors");
1426
1427 cmd = cmd->value.Connection->second;
1428 }
1429
1430 /* Now execute the rightmost command in the pipeline. */
1431 if (ignore_return && cmd)
1432 cmd->flags |= CMD_IGNORE_RETURN;
1433 exec_result = execute_command_internal (cmd, asynchronous, prev, pipe_out, fds_to_close);
1434
1435 if (prev >= 0)
1436 close (prev);
1437
1438#if defined (JOB_CONTROL)
1439 UNBLOCK_CHILD (oset);
1440#endif
1441
1442 return (exec_result);
1443}
1444
1445static int
1446execute_connection (command, asynchronous, pipe_in, pipe_out, fds_to_close)
1447 COMMAND *command;
1448 int asynchronous, pipe_in, pipe_out;
1449 struct fd_bitmap *fds_to_close;
1450{
Jari Aaltod166f041997-06-05 14:59:13 +00001451 REDIRECT *rp;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001452 COMMAND *tc, *second;
1453 int ignore_return, exec_result;
1454
1455 ignore_return = (command->flags & CMD_IGNORE_RETURN) != 0;
1456
1457 switch (command->value.Connection->connector)
1458 {
1459 /* Do the first command asynchronously. */
1460 case '&':
1461 tc = command->value.Connection->first;
1462 if (tc == 0)
1463 return (EXECUTION_SUCCESS);
1464
1465 rp = tc->redirects;
1466
Jari Aaltod166f041997-06-05 14:59:13 +00001467 if (ignore_return)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001468 tc->flags |= CMD_IGNORE_RETURN;
Jari Aaltod166f041997-06-05 14:59:13 +00001469 tc->flags |= CMD_AMPERSAND;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001470
Jari Aaltof73dda02001-11-13 17:56:06 +00001471 /* If this shell was compiled without job control support,
1472 if we are currently in a subshell via `( xxx )', or if job
1473 control is not active then the standard input for an
1474 asynchronous command is forced to /dev/null. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001475#if defined (JOB_CONTROL)
Jari Aaltof73dda02001-11-13 17:56:06 +00001476 if ((subshell_environment || !job_control) && !stdin_redir)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001477#else
1478 if (!stdin_redir)
1479#endif /* JOB_CONTROL */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001480 tc->flags |= CMD_STDIN_REDIR;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001481
1482 exec_result = execute_command_internal (tc, 1, pipe_in, pipe_out, fds_to_close);
1483
Jari Aaltod166f041997-06-05 14:59:13 +00001484 if (tc->flags & CMD_STDIN_REDIR)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001485 tc->flags &= ~CMD_STDIN_REDIR;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001486
1487 second = command->value.Connection->second;
1488 if (second)
1489 {
1490 if (ignore_return)
1491 second->flags |= CMD_IGNORE_RETURN;
1492
1493 exec_result = execute_command_internal (second, asynchronous, pipe_in, pipe_out, fds_to_close);
1494 }
1495
1496 break;
1497
1498 /* Just call execute command on both sides. */
1499 case ';':
1500 if (ignore_return)
1501 {
1502 if (command->value.Connection->first)
1503 command->value.Connection->first->flags |= CMD_IGNORE_RETURN;
1504 if (command->value.Connection->second)
1505 command->value.Connection->second->flags |= CMD_IGNORE_RETURN;
1506 }
1507 QUIT;
1508 execute_command (command->value.Connection->first);
1509 QUIT;
1510 exec_result = execute_command_internal (command->value.Connection->second,
1511 asynchronous, pipe_in, pipe_out,
1512 fds_to_close);
1513 break;
1514
1515 case '|':
1516 exec_result = execute_pipeline (command, asynchronous, pipe_in, pipe_out, fds_to_close);
1517 break;
1518
1519 case AND_AND:
1520 case OR_OR:
1521 if (asynchronous)
1522 {
1523 /* If we have something like `a && b &' or `a || b &', run the
1524 && or || stuff in a subshell. Force a subshell and just call
1525 execute_command_internal again. Leave asynchronous on
1526 so that we get a report from the parent shell about the
1527 background job. */
1528 command->flags |= CMD_FORCE_SUBSHELL;
1529 exec_result = execute_command_internal (command, 1, pipe_in, pipe_out, fds_to_close);
1530 break;
1531 }
1532
1533 /* Execute the first command. If the result of that is successful
1534 and the connector is AND_AND, or the result is not successful
1535 and the connector is OR_OR, then execute the second command,
1536 otherwise return. */
1537
1538 if (command->value.Connection->first)
1539 command->value.Connection->first->flags |= CMD_IGNORE_RETURN;
1540
1541 exec_result = execute_command (command->value.Connection->first);
1542 QUIT;
1543 if (((command->value.Connection->connector == AND_AND) &&
1544 (exec_result == EXECUTION_SUCCESS)) ||
1545 ((command->value.Connection->connector == OR_OR) &&
1546 (exec_result != EXECUTION_SUCCESS)))
1547 {
1548 if (ignore_return && command->value.Connection->second)
1549 command->value.Connection->second->flags |= CMD_IGNORE_RETURN;
1550
1551 exec_result = execute_command (command->value.Connection->second);
1552 }
1553 break;
1554
1555 default:
Jari Aaltob72432f1999-02-19 17:11:39 +00001556 command_error ("execute_connection", CMDERR_BADCONN, command->value.Connection->connector, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001557 jump_to_top_level (DISCARD);
1558 exec_result = EXECUTION_FAILURE;
1559 }
1560
1561 return exec_result;
1562}
1563
Jari Aaltobb706242000-03-17 21:46:59 +00001564#define REAP() \
1565 do \
1566 { \
1567 if (!interactive_shell) \
1568 reap_dead_jobs (); \
1569 } \
1570 while (0)
Jari Aalto726f6381996-08-26 18:22:31 +00001571
1572/* Execute a FOR command. The syntax is: FOR word_desc IN word_list;
1573 DO command; DONE */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001574static int
Jari Aalto726f6381996-08-26 18:22:31 +00001575execute_for_command (for_command)
1576 FOR_COM *for_command;
1577{
Jari Aalto726f6381996-08-26 18:22:31 +00001578 register WORD_LIST *releaser, *list;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001579 SHELL_VAR *v;
Jari Aalto726f6381996-08-26 18:22:31 +00001580 char *identifier;
Jari Aaltob80f6442004-07-27 13:29:18 +00001581 int retval, save_line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001582#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00001583 SHELL_VAR *old_value = (SHELL_VAR *)NULL; /* Remember the old value of x. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001584#endif
Jari Aalto726f6381996-08-26 18:22:31 +00001585
Jari Aaltob80f6442004-07-27 13:29:18 +00001586 save_line_number = line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00001587 if (check_identifier (for_command->name, 1) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001588 {
1589 if (posixly_correct && interactive_shell == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00001590 {
1591 last_command_exit_value = EX_USAGE;
Jari Aaltob80f6442004-07-27 13:29:18 +00001592 jump_to_top_level (ERREXIT);
Jari Aaltocce855b1998-04-17 19:52:44 +00001593 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001594 return (EXECUTION_FAILURE);
1595 }
Jari Aalto726f6381996-08-26 18:22:31 +00001596
1597 loop_level++;
1598 identifier = for_command->name->word;
1599
1600 list = releaser = expand_words_no_vars (for_command->map_list);
1601
1602 begin_unwind_frame ("for");
1603 add_unwind_protect (dispose_words, releaser);
1604
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001605#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00001606 if (lexical_scoping)
1607 {
1608 old_value = copy_variable (find_variable (identifier));
1609 if (old_value)
1610 add_unwind_protect (dispose_variable, old_value);
1611 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001612#endif
Jari Aalto726f6381996-08-26 18:22:31 +00001613
1614 if (for_command->flags & CMD_IGNORE_RETURN)
1615 for_command->action->flags |= CMD_IGNORE_RETURN;
1616
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001617 for (retval = EXECUTION_SUCCESS; list; list = list->next)
Jari Aalto726f6381996-08-26 18:22:31 +00001618 {
1619 QUIT;
Jari Aaltob80f6442004-07-27 13:29:18 +00001620
1621 line_number = for_command->line;
1622
1623 /* Remember what this command looks like, for debugger. */
1624 command_string_index = 0;
1625 print_for_command_head (for_command);
1626
1627 if (echo_command_at_execute)
1628 xtrace_print_for_command_head (for_command);
1629
Jari Aalto95732b42005-12-07 14:08:12 +00001630 /* Save this command unless it's a trap command and we're not running
1631 a debug trap. */
1632 if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0)))
Jari Aaltob80f6442004-07-27 13:29:18 +00001633 {
1634 FREE (the_printed_command_except_trap);
1635 the_printed_command_except_trap = savestring (the_printed_command);
1636 }
1637
1638 retval = run_debug_trap ();
1639#if defined (DEBUGGER)
1640 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
1641 skip the command. */
1642 if (debugging_mode && retval != EXECUTION_SUCCESS)
1643 continue;
1644#endif
1645
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001646 this_command_name = (char *)NULL;
Jari Aalto95732b42005-12-07 14:08:12 +00001647 v = bind_variable (identifier, list->word->word, 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +00001648 if (readonly_p (v) || noassign_p (v))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001649 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001650 line_number = save_line_number;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001651 if (readonly_p (v) && interactive_shell == 0 && posixly_correct)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001652 {
1653 last_command_exit_value = EXECUTION_FAILURE;
1654 jump_to_top_level (FORCE_EOF);
1655 }
1656 else
1657 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001658 dispose_words (releaser);
1659 discard_unwind_frame ("for");
Jari Aaltod166f041997-06-05 14:59:13 +00001660 loop_level--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001661 return (EXECUTION_FAILURE);
1662 }
1663 }
1664 retval = execute_command (for_command->action);
Jari Aalto726f6381996-08-26 18:22:31 +00001665 REAP ();
1666 QUIT;
1667
1668 if (breaking)
1669 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001670 breaking--;
Jari Aalto726f6381996-08-26 18:22:31 +00001671 break;
1672 }
1673
1674 if (continuing)
1675 {
1676 continuing--;
1677 if (continuing)
1678 break;
1679 }
Jari Aalto726f6381996-08-26 18:22:31 +00001680 }
1681
1682 loop_level--;
Jari Aaltob80f6442004-07-27 13:29:18 +00001683 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00001684
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001685#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00001686 if (lexical_scoping)
1687 {
1688 if (!old_value)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001689 unbind_variable (identifier);
Jari Aalto726f6381996-08-26 18:22:31 +00001690 else
1691 {
1692 SHELL_VAR *new_value;
1693
Jari Aalto95732b42005-12-07 14:08:12 +00001694 new_value = bind_variable (identifier, value_cell(old_value), 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001695 new_value->attributes = old_value->attributes;
1696 dispose_variable (old_value);
1697 }
1698 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001699#endif
Jari Aalto726f6381996-08-26 18:22:31 +00001700
1701 dispose_words (releaser);
1702 discard_unwind_frame ("for");
1703 return (retval);
1704}
1705
Jari Aaltobb706242000-03-17 21:46:59 +00001706#if defined (ARITH_FOR_COMMAND)
1707/* Execute an arithmetic for command. The syntax is
1708
1709 for (( init ; step ; test ))
1710 do
1711 body
1712 done
1713
1714 The execution should be exactly equivalent to
1715
1716 eval \(\( init \)\)
1717 while eval \(\( test \)\) ; do
1718 body;
1719 eval \(\( step \)\)
1720 done
1721*/
Jari Aalto7117c2d2002-07-17 14:10:11 +00001722static intmax_t
Jari Aaltobb706242000-03-17 21:46:59 +00001723eval_arith_for_expr (l, okp)
1724 WORD_LIST *l;
1725 int *okp;
1726{
1727 WORD_LIST *new;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001728 intmax_t expresult;
Jari Aaltob80f6442004-07-27 13:29:18 +00001729 int r;
Jari Aaltobb706242000-03-17 21:46:59 +00001730
1731 new = expand_words_no_vars (l);
Jari Aaltof73dda02001-11-13 17:56:06 +00001732 if (new)
1733 {
1734 if (echo_command_at_execute)
1735 xtrace_print_arith_cmd (new);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001736 this_command_name = "(("; /* )) for expression error messages */
Jari Aaltob80f6442004-07-27 13:29:18 +00001737
1738 command_string_index = 0;
1739 print_arith_command (new);
Jari Aalto95732b42005-12-07 14:08:12 +00001740 if (signal_in_progress (DEBUG_TRAP) == 0)
1741 {
1742 FREE (the_printed_command_except_trap);
1743 the_printed_command_except_trap = savestring (the_printed_command);
1744 }
Jari Aaltob80f6442004-07-27 13:29:18 +00001745
1746 r = run_debug_trap ();
1747 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
1748 skip the command. */
1749#if defined (DEBUGGER)
1750 if (debugging_mode == 0 || r == EXECUTION_SUCCESS)
1751 expresult = evalexp (new->word->word, okp);
1752 else
1753 {
1754 expresult = 0;
1755 if (okp)
1756 *okp = 1;
1757 }
1758#else
Jari Aaltof73dda02001-11-13 17:56:06 +00001759 expresult = evalexp (new->word->word, okp);
Jari Aaltob80f6442004-07-27 13:29:18 +00001760#endif
Jari Aaltof73dda02001-11-13 17:56:06 +00001761 dispose_words (new);
1762 }
1763 else
1764 {
1765 expresult = 0;
1766 if (okp)
1767 *okp = 1;
1768 }
Jari Aaltobb706242000-03-17 21:46:59 +00001769 return (expresult);
1770}
1771
1772static int
1773execute_arith_for_command (arith_for_command)
1774 ARITH_FOR_COM *arith_for_command;
1775{
Jari Aalto7117c2d2002-07-17 14:10:11 +00001776 intmax_t expresult;
1777 int expok, body_status, arith_lineno, save_lineno;
Jari Aaltobb706242000-03-17 21:46:59 +00001778
1779 body_status = EXECUTION_SUCCESS;
1780 loop_level++;
Jari Aaltob80f6442004-07-27 13:29:18 +00001781 save_lineno = line_number;
Jari Aaltobb706242000-03-17 21:46:59 +00001782
1783 if (arith_for_command->flags & CMD_IGNORE_RETURN)
1784 arith_for_command->action->flags |= CMD_IGNORE_RETURN;
1785
1786 this_command_name = "(("; /* )) for expression error messages */
1787
Jari Aalto7117c2d2002-07-17 14:10:11 +00001788 /* save the starting line number of the command so we can reset
1789 line_number before executing each expression -- for $LINENO
1790 and the DEBUG trap. */
Jari Aaltob80f6442004-07-27 13:29:18 +00001791 line_number = arith_lineno = arith_for_command->line;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001792 if (variable_context && interactive_shell)
Jari Aaltob80f6442004-07-27 13:29:18 +00001793 line_number -= function_line_number;
Jari Aaltobb706242000-03-17 21:46:59 +00001794
1795 /* Evaluate the initialization expression. */
1796 expresult = eval_arith_for_expr (arith_for_command->init, &expok);
1797 if (expok == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00001798 {
1799 line_number = save_lineno;
1800 return (EXECUTION_FAILURE);
1801 }
Jari Aaltobb706242000-03-17 21:46:59 +00001802
1803 while (1)
1804 {
1805 /* Evaluate the test expression. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001806 line_number = arith_lineno;
Jari Aaltobb706242000-03-17 21:46:59 +00001807 expresult = eval_arith_for_expr (arith_for_command->test, &expok);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001808 line_number = save_lineno;
1809
Jari Aaltobb706242000-03-17 21:46:59 +00001810 if (expok == 0)
1811 {
1812 body_status = EXECUTION_FAILURE;
1813 break;
1814 }
1815 REAP ();
1816 if (expresult == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001817 break;
Jari Aaltobb706242000-03-17 21:46:59 +00001818
1819 /* Execute the body of the arithmetic for command. */
1820 QUIT;
1821 body_status = execute_command (arith_for_command->action);
1822 QUIT;
1823
1824 /* Handle any `break' or `continue' commands executed by the body. */
1825 if (breaking)
1826 {
1827 breaking--;
1828 break;
1829 }
1830
1831 if (continuing)
1832 {
1833 continuing--;
1834 if (continuing)
1835 break;
1836 }
1837
1838 /* Evaluate the step expression. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001839 line_number = arith_lineno;
Jari Aaltobb706242000-03-17 21:46:59 +00001840 expresult = eval_arith_for_expr (arith_for_command->step, &expok);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001841 line_number = save_lineno;
1842
Jari Aaltobb706242000-03-17 21:46:59 +00001843 if (expok == 0)
1844 {
1845 body_status = EXECUTION_FAILURE;
1846 break;
1847 }
1848 }
1849
1850 loop_level--;
Jari Aaltob80f6442004-07-27 13:29:18 +00001851 line_number = save_lineno;
1852
Jari Aaltobb706242000-03-17 21:46:59 +00001853 return (body_status);
1854}
1855#endif
1856
Jari Aalto726f6381996-08-26 18:22:31 +00001857#if defined (SELECT_COMMAND)
1858static int LINES, COLS, tabsize;
1859
1860#define RP_SPACE ") "
1861#define RP_SPACE_LEN 2
1862
1863/* XXX - does not handle numbers > 1000000 at all. */
1864#define NUMBER_LEN(s) \
1865((s < 10) ? 1 \
1866 : ((s < 100) ? 2 \
1867 : ((s < 1000) ? 3 \
1868 : ((s < 10000) ? 4 \
1869 : ((s < 100000) ? 5 \
1870 : 6)))))
1871
1872static int
1873print_index_and_element (len, ind, list)
1874 int len, ind;
1875 WORD_LIST *list;
1876{
1877 register WORD_LIST *l;
1878 register int i;
1879
1880 if (list == 0)
1881 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001882 for (i = ind, l = list; l && --i; l = l->next)
1883 ;
Jari Aalto726f6381996-08-26 18:22:31 +00001884 fprintf (stderr, "%*d%s%s", len, ind, RP_SPACE, l->word->word);
1885 return (STRLEN (l->word->word));
1886}
1887
1888static void
1889indent (from, to)
1890 int from, to;
1891{
1892 while (from < to)
1893 {
1894 if ((to / tabsize) > (from / tabsize))
1895 {
1896 putc ('\t', stderr);
1897 from += tabsize - from % tabsize;
1898 }
1899 else
1900 {
1901 putc (' ', stderr);
1902 from++;
1903 }
1904 }
1905}
1906
1907static void
1908print_select_list (list, list_len, max_elem_len, indices_len)
1909 WORD_LIST *list;
1910 int list_len, max_elem_len, indices_len;
1911{
1912 int ind, row, elem_len, pos, cols, rows;
1913 int first_column_indices_len, other_indices_len;
1914
1915 if (list == 0)
1916 {
1917 putc ('\n', stderr);
1918 return;
1919 }
1920
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001921 cols = max_elem_len ? COLS / max_elem_len : 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001922 if (cols == 0)
1923 cols = 1;
1924 rows = list_len ? list_len / cols + (list_len % cols != 0) : 1;
1925 cols = list_len ? list_len / rows + (list_len % rows != 0) : 1;
1926
1927 if (rows == 1)
1928 {
1929 rows = cols;
1930 cols = 1;
1931 }
1932
1933 first_column_indices_len = NUMBER_LEN (rows);
1934 other_indices_len = indices_len;
1935
1936 for (row = 0; row < rows; row++)
1937 {
1938 ind = row;
1939 pos = 0;
1940 while (1)
1941 {
1942 indices_len = (pos == 0) ? first_column_indices_len : other_indices_len;
1943 elem_len = print_index_and_element (indices_len, ind + 1, list);
1944 elem_len += indices_len + RP_SPACE_LEN;
1945 ind += rows;
1946 if (ind >= list_len)
1947 break;
1948 indent (pos + elem_len, pos + max_elem_len);
1949 pos += max_elem_len;
1950 }
1951 putc ('\n', stderr);
1952 }
1953}
1954
1955/* Print the elements of LIST, one per line, preceded by an index from 1 to
1956 LIST_LEN. Then display PROMPT and wait for the user to enter a number.
1957 If the number is between 1 and LIST_LEN, return that selection. If EOF
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001958 is read, return a null string. If a blank line is entered, or an invalid
1959 number is entered, the loop is executed again. */
Jari Aalto726f6381996-08-26 18:22:31 +00001960static char *
Jari Aalto7117c2d2002-07-17 14:10:11 +00001961select_query (list, list_len, prompt, print_menu)
Jari Aalto726f6381996-08-26 18:22:31 +00001962 WORD_LIST *list;
1963 int list_len;
1964 char *prompt;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001965 int print_menu;
Jari Aalto726f6381996-08-26 18:22:31 +00001966{
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001967 int max_elem_len, indices_len, len;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001968 intmax_t reply;
Jari Aalto726f6381996-08-26 18:22:31 +00001969 WORD_LIST *l;
1970 char *repl_string, *t;
1971
1972 t = get_string_value ("LINES");
1973 LINES = (t && *t) ? atoi (t) : 24;
1974 t = get_string_value ("COLUMNS");
1975 COLS = (t && *t) ? atoi (t) : 80;
1976
1977#if 0
1978 t = get_string_value ("TABSIZE");
1979 tabsize = (t && *t) ? atoi (t) : 8;
1980 if (tabsize <= 0)
1981 tabsize = 8;
1982#else
1983 tabsize = 8;
1984#endif
1985
1986 max_elem_len = 0;
1987 for (l = list; l; l = l->next)
1988 {
1989 len = STRLEN (l->word->word);
1990 if (len > max_elem_len)
Jari Aaltocce855b1998-04-17 19:52:44 +00001991 max_elem_len = len;
Jari Aalto726f6381996-08-26 18:22:31 +00001992 }
1993 indices_len = NUMBER_LEN (list_len);
1994 max_elem_len += indices_len + RP_SPACE_LEN + 2;
1995
1996 while (1)
1997 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00001998 if (print_menu)
1999 print_select_list (list, list_len, max_elem_len, indices_len);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002000 fprintf (stderr, "%s", prompt);
2001 fflush (stderr);
Jari Aalto726f6381996-08-26 18:22:31 +00002002 QUIT;
2003
2004 if (read_builtin ((WORD_LIST *)NULL) == EXECUTION_FAILURE)
2005 {
2006 putchar ('\n');
2007 return ((char *)NULL);
2008 }
2009 repl_string = get_string_value ("REPLY");
2010 if (*repl_string == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002011 {
2012 print_menu = 1;
2013 continue;
2014 }
Jari Aaltoe8ce7751997-09-22 20:22:27 +00002015 if (legal_number (repl_string, &reply) == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00002016 return "";
Jari Aalto726f6381996-08-26 18:22:31 +00002017 if (reply < 1 || reply > list_len)
2018 return "";
2019
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002020 for (l = list; l && --reply; l = l->next)
2021 ;
Jari Aalto726f6381996-08-26 18:22:31 +00002022 return (l->word->word);
2023 }
2024}
2025
2026/* Execute a SELECT command. The syntax is:
2027 SELECT word IN list DO command_list DONE
2028 Only `break' or `return' in command_list will terminate
2029 the command. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002030static int
Jari Aalto726f6381996-08-26 18:22:31 +00002031execute_select_command (select_command)
2032 SELECT_COM *select_command;
2033{
2034 WORD_LIST *releaser, *list;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002035 SHELL_VAR *v;
Jari Aalto726f6381996-08-26 18:22:31 +00002036 char *identifier, *ps3_prompt, *selection;
Jari Aaltob80f6442004-07-27 13:29:18 +00002037 int retval, list_len, show_menu, save_line_number;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002038
Jari Aalto726f6381996-08-26 18:22:31 +00002039 if (check_identifier (select_command->name, 1) == 0)
2040 return (EXECUTION_FAILURE);
2041
Jari Aaltob80f6442004-07-27 13:29:18 +00002042 save_line_number = line_number;
2043 line_number = select_command->line;
2044
2045 command_string_index = 0;
2046 print_select_command_head (select_command);
2047
2048 if (echo_command_at_execute)
2049 xtrace_print_select_command_head (select_command);
2050
Jari Aalto95732b42005-12-07 14:08:12 +00002051 if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0)))
2052 {
2053 FREE (the_printed_command_except_trap);
2054 the_printed_command_except_trap = savestring (the_printed_command);
2055 }
Jari Aaltob80f6442004-07-27 13:29:18 +00002056
2057 retval = run_debug_trap ();
2058#if defined (DEBUGGER)
2059 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
2060 skip the command. */
2061 if (debugging_mode && retval != EXECUTION_SUCCESS)
2062 return (EXECUTION_SUCCESS);
2063#endif
2064
Jari Aalto726f6381996-08-26 18:22:31 +00002065 loop_level++;
2066 identifier = select_command->name->word;
2067
2068 /* command and arithmetic substitution, parameter and variable expansion,
2069 word splitting, pathname expansion, and quote removal. */
2070 list = releaser = expand_words_no_vars (select_command->map_list);
2071 list_len = list_length (list);
2072 if (list == 0 || list_len == 0)
2073 {
2074 if (list)
2075 dispose_words (list);
Jari Aaltob80f6442004-07-27 13:29:18 +00002076 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002077 return (EXECUTION_SUCCESS);
2078 }
2079
2080 begin_unwind_frame ("select");
2081 add_unwind_protect (dispose_words, releaser);
2082
Jari Aalto726f6381996-08-26 18:22:31 +00002083 if (select_command->flags & CMD_IGNORE_RETURN)
2084 select_command->action->flags |= CMD_IGNORE_RETURN;
2085
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002086 retval = EXECUTION_SUCCESS;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002087 show_menu = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002088
Jari Aalto726f6381996-08-26 18:22:31 +00002089 while (1)
2090 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002091 line_number = select_command->line;
Jari Aalto726f6381996-08-26 18:22:31 +00002092 ps3_prompt = get_string_value ("PS3");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002093 if (ps3_prompt == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002094 ps3_prompt = "#? ";
2095
2096 QUIT;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002097 selection = select_query (list, list_len, ps3_prompt, show_menu);
Jari Aalto726f6381996-08-26 18:22:31 +00002098 QUIT;
2099 if (selection == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002100 {
2101 /* select_query returns EXECUTION_FAILURE if the read builtin
2102 fails, so we want to return failure in this case. */
2103 retval = EXECUTION_FAILURE;
2104 break;
2105 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002106
Jari Aalto95732b42005-12-07 14:08:12 +00002107 v = bind_variable (identifier, selection, 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002108 if (readonly_p (v) || noassign_p (v))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002109 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00002110 if (readonly_p (v) && interactive_shell == 0 && posixly_correct)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002111 {
2112 last_command_exit_value = EXECUTION_FAILURE;
2113 jump_to_top_level (FORCE_EOF);
2114 }
2115 else
2116 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002117 dispose_words (releaser);
2118 discard_unwind_frame ("select");
2119 loop_level--;
2120 line_number = save_line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002121 return (EXECUTION_FAILURE);
2122 }
2123 }
Jari Aalto726f6381996-08-26 18:22:31 +00002124
Jari Aaltof73dda02001-11-13 17:56:06 +00002125 retval = execute_command (select_command->action);
Jari Aalto726f6381996-08-26 18:22:31 +00002126
2127 REAP ();
2128 QUIT;
2129
2130 if (breaking)
2131 {
2132 breaking--;
2133 break;
2134 }
Jari Aaltobb706242000-03-17 21:46:59 +00002135
2136 if (continuing)
2137 {
2138 continuing--;
2139 if (continuing)
2140 break;
2141 }
Jari Aalto7117c2d2002-07-17 14:10:11 +00002142
2143#if defined (KSH_COMPATIBLE_SELECT)
2144 show_menu = 0;
2145 selection = get_string_value ("REPLY");
2146 if (selection && *selection == '\0')
2147 show_menu = 1;
2148#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002149 }
2150
2151 loop_level--;
Jari Aaltob80f6442004-07-27 13:29:18 +00002152 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002153
Jari Aaltob80f6442004-07-27 13:29:18 +00002154 dispose_words (releaser);
2155 discard_unwind_frame ("select");
Jari Aalto726f6381996-08-26 18:22:31 +00002156 return (retval);
2157}
2158#endif /* SELECT_COMMAND */
2159
2160/* Execute a CASE command. The syntax is: CASE word_desc IN pattern_list ESAC.
2161 The pattern_list is a linked list of pattern clauses; each clause contains
2162 some patterns to compare word_desc against, and an associated command to
2163 execute. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002164static int
Jari Aalto726f6381996-08-26 18:22:31 +00002165execute_case_command (case_command)
2166 CASE_COM *case_command;
2167{
2168 register WORD_LIST *list;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002169 WORD_LIST *wlist, *es;
Jari Aalto726f6381996-08-26 18:22:31 +00002170 PATTERN_LIST *clauses;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002171 char *word, *pattern;
Jari Aaltob80f6442004-07-27 13:29:18 +00002172 int retval, match, ignore_return, save_line_number;
2173
2174 save_line_number = line_number;
2175 line_number = case_command->line;
2176
2177 command_string_index = 0;
2178 print_case_command_head (case_command);
2179
2180 if (echo_command_at_execute)
2181 xtrace_print_case_command_head (case_command);
2182
Jari Aalto95732b42005-12-07 14:08:12 +00002183 if (signal_in_progress (DEBUG_TRAP == 0) && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0)))
Jari Aaltob80f6442004-07-27 13:29:18 +00002184 {
2185 FREE (the_printed_command_except_trap);
2186 the_printed_command_except_trap = savestring (the_printed_command);
2187 }
2188
2189 retval = run_debug_trap();
2190#if defined (DEBUGGER)
2191 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
2192 skip the command. */
2193 if (debugging_mode && retval != EXECUTION_SUCCESS)
2194 {
2195 line_number = save_line_number;
2196 return (EXECUTION_SUCCESS);
2197 }
2198#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002199
Jari Aalto28ef6c32001-04-06 19:14:31 +00002200 wlist = expand_word_unsplit (case_command->word, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002201 word = wlist ? string_list (wlist) : savestring ("");
2202 dispose_words (wlist);
2203
Jari Aalto726f6381996-08-26 18:22:31 +00002204 retval = EXECUTION_SUCCESS;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002205 ignore_return = case_command->flags & CMD_IGNORE_RETURN;
Jari Aalto726f6381996-08-26 18:22:31 +00002206
2207 begin_unwind_frame ("case");
Jari Aalto726f6381996-08-26 18:22:31 +00002208 add_unwind_protect ((Function *)xfree, word);
2209
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002210#define EXIT_CASE() goto exit_case_command
2211
2212 for (clauses = case_command->clauses; clauses; clauses = clauses->next)
Jari Aalto726f6381996-08-26 18:22:31 +00002213 {
2214 QUIT;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002215 for (list = clauses->patterns; list; list = list->next)
Jari Aalto726f6381996-08-26 18:22:31 +00002216 {
Jari Aalto726f6381996-08-26 18:22:31 +00002217 es = expand_word_leave_quoted (list->word, 0);
2218
2219 if (es && es->word && es->word->word && *(es->word->word))
Jari Aaltocce855b1998-04-17 19:52:44 +00002220 pattern = quote_string_for_globbing (es->word->word, QGLOB_CVTNULL);
Jari Aalto726f6381996-08-26 18:22:31 +00002221 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002222 {
Jari Aaltof73dda02001-11-13 17:56:06 +00002223 pattern = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002224 pattern[0] = '\0';
2225 }
Jari Aalto726f6381996-08-26 18:22:31 +00002226
2227 /* Since the pattern does not undergo quote removal (as per
Jari Aaltof73dda02001-11-13 17:56:06 +00002228 Posix.2, section 3.9.4.3), the strmatch () call must be able
Jari Aalto726f6381996-08-26 18:22:31 +00002229 to recognize backslashes as escape characters. */
Jari Aalto95732b42005-12-07 14:08:12 +00002230 match = strmatch (pattern, word, FNMATCH_EXTFLAG|FNMATCH_IGNCASE) != FNM_NOMATCH;
Jari Aalto726f6381996-08-26 18:22:31 +00002231 free (pattern);
2232
2233 dispose_words (es);
2234
2235 if (match)
2236 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002237 if (clauses->action && ignore_return)
Jari Aalto726f6381996-08-26 18:22:31 +00002238 clauses->action->flags |= CMD_IGNORE_RETURN;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002239 retval = execute_command (clauses->action);
2240 EXIT_CASE ();
Jari Aalto726f6381996-08-26 18:22:31 +00002241 }
2242
Jari Aalto726f6381996-08-26 18:22:31 +00002243 QUIT;
2244 }
Jari Aalto726f6381996-08-26 18:22:31 +00002245 }
2246
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002247exit_case_command:
2248 free (word);
Jari Aalto726f6381996-08-26 18:22:31 +00002249 discard_unwind_frame ("case");
Jari Aaltob80f6442004-07-27 13:29:18 +00002250 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002251 return (retval);
2252}
2253
2254#define CMD_WHILE 0
2255#define CMD_UNTIL 1
2256
2257/* The WHILE command. Syntax: WHILE test DO action; DONE.
2258 Repeatedly execute action while executing test produces
2259 EXECUTION_SUCCESS. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002260static int
Jari Aalto726f6381996-08-26 18:22:31 +00002261execute_while_command (while_command)
2262 WHILE_COM *while_command;
2263{
2264 return (execute_while_or_until (while_command, CMD_WHILE));
2265}
2266
2267/* UNTIL is just like WHILE except that the test result is negated. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002268static int
Jari Aalto726f6381996-08-26 18:22:31 +00002269execute_until_command (while_command)
2270 WHILE_COM *while_command;
2271{
2272 return (execute_while_or_until (while_command, CMD_UNTIL));
2273}
2274
2275/* The body for both while and until. The only difference between the
2276 two is that the test value is treated differently. TYPE is
2277 CMD_WHILE or CMD_UNTIL. The return value for both commands should
2278 be EXECUTION_SUCCESS if no commands in the body are executed, and
2279 the status of the last command executed in the body otherwise. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002280static int
Jari Aalto726f6381996-08-26 18:22:31 +00002281execute_while_or_until (while_command, type)
2282 WHILE_COM *while_command;
2283 int type;
2284{
2285 int return_value, body_status;
2286
2287 body_status = EXECUTION_SUCCESS;
2288 loop_level++;
2289
2290 while_command->test->flags |= CMD_IGNORE_RETURN;
2291 if (while_command->flags & CMD_IGNORE_RETURN)
2292 while_command->action->flags |= CMD_IGNORE_RETURN;
2293
2294 while (1)
2295 {
2296 return_value = execute_command (while_command->test);
2297 REAP ();
2298
Jari Aaltof73dda02001-11-13 17:56:06 +00002299 /* Need to handle `break' in the test when we would break out of the
2300 loop. The job control code will set `breaking' to loop_level
2301 when a job in a loop is stopped with SIGTSTP. If the stopped job
2302 is in the loop test, `breaking' will not be reset unless we do
2303 this, and the shell will cease to execute commands. */
Jari Aalto726f6381996-08-26 18:22:31 +00002304 if (type == CMD_WHILE && return_value != EXECUTION_SUCCESS)
Jari Aaltof73dda02001-11-13 17:56:06 +00002305 {
2306 if (breaking)
2307 breaking--;
2308 break;
2309 }
Jari Aalto726f6381996-08-26 18:22:31 +00002310 if (type == CMD_UNTIL && return_value == EXECUTION_SUCCESS)
Jari Aaltof73dda02001-11-13 17:56:06 +00002311 {
2312 if (breaking)
2313 breaking--;
2314 break;
2315 }
Jari Aalto726f6381996-08-26 18:22:31 +00002316
2317 QUIT;
2318 body_status = execute_command (while_command->action);
2319 QUIT;
2320
2321 if (breaking)
2322 {
2323 breaking--;
2324 break;
2325 }
2326
2327 if (continuing)
2328 {
2329 continuing--;
2330 if (continuing)
2331 break;
2332 }
2333 }
2334 loop_level--;
2335
2336 return (body_status);
2337}
2338
2339/* IF test THEN command [ELSE command].
2340 IF also allows ELIF in the place of ELSE IF, but
2341 the parser makes *that* stupidity transparent. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002342static int
Jari Aalto726f6381996-08-26 18:22:31 +00002343execute_if_command (if_command)
2344 IF_COM *if_command;
2345{
Jari Aaltob80f6442004-07-27 13:29:18 +00002346 int return_value, save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002347
Jari Aaltob80f6442004-07-27 13:29:18 +00002348 save_line_number = line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002349 if_command->test->flags |= CMD_IGNORE_RETURN;
2350 return_value = execute_command (if_command->test);
Jari Aaltob80f6442004-07-27 13:29:18 +00002351 line_number = save_line_number;
Jari Aalto726f6381996-08-26 18:22:31 +00002352
2353 if (return_value == EXECUTION_SUCCESS)
2354 {
2355 QUIT;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002356
Jari Aalto726f6381996-08-26 18:22:31 +00002357 if (if_command->true_case && (if_command->flags & CMD_IGNORE_RETURN))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002358 if_command->true_case->flags |= CMD_IGNORE_RETURN;
2359
Jari Aalto726f6381996-08-26 18:22:31 +00002360 return (execute_command (if_command->true_case));
2361 }
2362 else
2363 {
2364 QUIT;
2365
2366 if (if_command->false_case && (if_command->flags & CMD_IGNORE_RETURN))
2367 if_command->false_case->flags |= CMD_IGNORE_RETURN;
2368
2369 return (execute_command (if_command->false_case));
2370 }
2371}
2372
Jari Aaltocce855b1998-04-17 19:52:44 +00002373#if defined (DPAREN_ARITHMETIC)
2374static int
2375execute_arith_command (arith_command)
2376 ARITH_COM *arith_command;
2377{
Jari Aaltob80f6442004-07-27 13:29:18 +00002378 int expok, save_line_number, retval;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002379 intmax_t expresult;
Jari Aaltof73dda02001-11-13 17:56:06 +00002380 WORD_LIST *new;
Jari Aaltocce855b1998-04-17 19:52:44 +00002381
Jari Aaltof73dda02001-11-13 17:56:06 +00002382 expresult = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +00002383
Jari Aaltob80f6442004-07-27 13:29:18 +00002384 save_line_number = line_number;
Jari Aaltob72432f1999-02-19 17:11:39 +00002385 this_command_name = "(("; /* )) */
Jari Aaltob80f6442004-07-27 13:29:18 +00002386 line_number = arith_command->line;
Jari Aaltocce855b1998-04-17 19:52:44 +00002387 /* If we're in a function, update the line number information. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002388 if (variable_context && interactive_shell)
Jari Aaltob80f6442004-07-27 13:29:18 +00002389 line_number -= function_line_number;
2390
2391 command_string_index = 0;
2392 print_arith_command (arith_command->exp);
Jari Aalto95732b42005-12-07 14:08:12 +00002393
2394 if (signal_in_progress (DEBUG_TRAP) == 0)
2395 {
2396 FREE (the_printed_command_except_trap);
2397 the_printed_command_except_trap = savestring (the_printed_command);
2398 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002399
Jari Aalto7117c2d2002-07-17 14:10:11 +00002400 /* Run the debug trap before each arithmetic command, but do it after we
2401 update the line number information and before we expand the various
2402 words in the expression. */
Jari Aaltob80f6442004-07-27 13:29:18 +00002403 retval = run_debug_trap ();
2404#if defined (DEBUGGER)
2405 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
2406 skip the command. */
2407 if (debugging_mode && retval != EXECUTION_SUCCESS)
2408 {
2409 line_number = save_line_number;
2410 return (EXECUTION_SUCCESS);
2411 }
2412#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +00002413
Jari Aaltob80f6442004-07-27 13:29:18 +00002414 new = expand_words_no_vars (arith_command->exp);
Jari Aaltocce855b1998-04-17 19:52:44 +00002415
2416 /* If we're tracing, make a new word list with `((' at the front and `))'
2417 at the back and print it. */
2418 if (echo_command_at_execute)
2419 xtrace_print_arith_cmd (new);
2420
Jari Aaltob80f6442004-07-27 13:29:18 +00002421 if (new)
2422 {
2423 expresult = evalexp (new->word->word, &expok);
2424 line_number = save_line_number;
2425 dispose_words (new);
2426 }
2427 else
2428 {
2429 expresult = 0;
2430 expok = 1;
2431 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002432
2433 if (expok == 0)
2434 return (EXECUTION_FAILURE);
2435
Jari Aaltof73dda02001-11-13 17:56:06 +00002436 return (expresult == 0 ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
Jari Aaltocce855b1998-04-17 19:52:44 +00002437}
2438#endif /* DPAREN_ARITHMETIC */
2439
2440#if defined (COND_COMMAND)
2441
2442static char *nullstr = "";
2443
2444static int
2445execute_cond_node (cond)
2446 COND_COM *cond;
2447{
Jari Aaltob80f6442004-07-27 13:29:18 +00002448 int result, invert, patmatch, rmatch, mflags;
Jari Aaltof73dda02001-11-13 17:56:06 +00002449 char *arg1, *arg2;
Jari Aaltocce855b1998-04-17 19:52:44 +00002450
2451 invert = (cond->flags & CMD_INVERT_RETURN);
2452
2453 if (cond->type == COND_EXPR)
2454 result = execute_cond_node (cond->left);
2455 else if (cond->type == COND_OR)
2456 {
2457 result = execute_cond_node (cond->left);
2458 if (result != EXECUTION_SUCCESS)
2459 result = execute_cond_node (cond->right);
2460 }
2461 else if (cond->type == COND_AND)
2462 {
2463 result = execute_cond_node (cond->left);
2464 if (result == EXECUTION_SUCCESS)
2465 result = execute_cond_node (cond->right);
2466 }
2467 else if (cond->type == COND_UNARY)
2468 {
2469 arg1 = cond_expand_word (cond->left->op, 0);
2470 if (arg1 == 0)
2471 arg1 = nullstr;
2472 if (echo_command_at_execute)
2473 xtrace_print_cond_term (cond->type, invert, cond->op, arg1, (char *)NULL);
2474 result = unary_test (cond->op->word, arg1) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
2475 if (arg1 != nullstr)
2476 free (arg1);
2477 }
2478 else if (cond->type == COND_BINARY)
2479 {
Jari Aaltob72432f1999-02-19 17:11:39 +00002480 patmatch = ((cond->op->word[1] == '=') && (cond->op->word[2] == '\0') &&
2481 (cond->op->word[0] == '!' || cond->op->word[0] == '=') ||
2482 (cond->op->word[0] == '=' && cond->op->word[1] == '\0'));
Jari Aaltob80f6442004-07-27 13:29:18 +00002483#if defined (COND_REGEXP)
2484 rmatch = (cond->op->word[0] == '=' && cond->op->word[1] == '~' &&
2485 cond->op->word[2] == '\0');
2486#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00002487
2488 arg1 = cond_expand_word (cond->left->op, 0);
2489 if (arg1 == 0)
2490 arg1 = nullstr;
2491 arg2 = cond_expand_word (cond->right->op, patmatch);
2492 if (arg2 == 0)
2493 arg2 = nullstr;
2494
2495 if (echo_command_at_execute)
2496 xtrace_print_cond_term (cond->type, invert, cond->op, arg1, arg2);
2497
Jari Aaltob80f6442004-07-27 13:29:18 +00002498#if defined (COND_REGEXP)
2499 if (rmatch)
2500 {
2501 mflags = SHMAT_PWARN;
2502#if defined (ARRAY_VARS)
2503 mflags |= SHMAT_SUBEXP;
2504#endif
2505
2506 result = sh_regmatch (arg1, arg2, mflags);
2507 }
2508 else
2509#endif /* COND_REGEXP */
Jari Aalto95732b42005-12-07 14:08:12 +00002510 {
2511 int oe;
2512 oe = extended_glob;
2513 extended_glob = 1;
2514 result = binary_test (cond->op->word, arg1, arg2, TEST_PATMATCH|TEST_ARITHEXP)
2515 ? EXECUTION_SUCCESS
2516 : EXECUTION_FAILURE;
2517 extended_glob = oe;
2518 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002519 if (arg1 != nullstr)
2520 free (arg1);
2521 if (arg2 != nullstr)
2522 free (arg2);
2523 }
2524 else
2525 {
Jari Aaltob72432f1999-02-19 17:11:39 +00002526 command_error ("execute_cond_node", CMDERR_BADTYPE, cond->type, 0);
Jari Aaltocce855b1998-04-17 19:52:44 +00002527 jump_to_top_level (DISCARD);
2528 result = EXECUTION_FAILURE;
2529 }
2530
2531 if (invert)
2532 result = (result == EXECUTION_SUCCESS) ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
2533
2534 return result;
2535}
2536
2537static int
2538execute_cond_command (cond_command)
2539 COND_COM *cond_command;
2540{
Jari Aaltob80f6442004-07-27 13:29:18 +00002541 int retval, save_line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +00002542
Jari Aaltob80f6442004-07-27 13:29:18 +00002543 retval = EXECUTION_SUCCESS;
2544 save_line_number = line_number;
Jari Aaltocce855b1998-04-17 19:52:44 +00002545
2546 this_command_name = "[[";
Jari Aaltob80f6442004-07-27 13:29:18 +00002547 line_number = cond_command->line;
Jari Aaltocce855b1998-04-17 19:52:44 +00002548 /* If we're in a function, update the line number information. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002549 if (variable_context && interactive_shell)
Jari Aaltob80f6442004-07-27 13:29:18 +00002550 line_number -= function_line_number;
2551
2552 command_string_index = 0;
2553 print_cond_command (cond_command);
Jari Aalto95732b42005-12-07 14:08:12 +00002554
2555 if (signal_in_progress (DEBUG_TRAP) == 0)
2556 {
2557 FREE (the_printed_command_except_trap);
2558 the_printed_command_except_trap = savestring (the_printed_command);
2559 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002560
Jari Aalto7117c2d2002-07-17 14:10:11 +00002561 /* Run the debug trap before each conditional command, but do it after we
2562 update the line number information. */
Jari Aaltob80f6442004-07-27 13:29:18 +00002563 retval = run_debug_trap ();
2564#if defined (DEBUGGER)
2565 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
2566 skip the command. */
2567 if (debugging_mode && retval != EXECUTION_SUCCESS)
2568 {
2569 line_number = save_line_number;
2570 return (EXECUTION_SUCCESS);
2571 }
2572#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +00002573
Jari Aaltocce855b1998-04-17 19:52:44 +00002574#if 0
2575 debug_print_cond_command (cond_command);
2576#endif
Jari Aaltob80f6442004-07-27 13:29:18 +00002577
2578 last_command_exit_value = retval = execute_cond_node (cond_command);
2579 line_number = save_line_number;
2580 return (retval);
Jari Aaltocce855b1998-04-17 19:52:44 +00002581}
2582#endif /* COND_COMMAND */
2583
Jari Aalto726f6381996-08-26 18:22:31 +00002584static void
2585bind_lastarg (arg)
2586 char *arg;
2587{
2588 SHELL_VAR *var;
2589
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002590 if (arg == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002591 arg = "";
Jari Aalto95732b42005-12-07 14:08:12 +00002592 var = bind_variable ("_", arg, 0);
Jari Aaltobb706242000-03-17 21:46:59 +00002593 VUNSETATTR (var, att_exported);
Jari Aalto726f6381996-08-26 18:22:31 +00002594}
2595
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002596/* Execute a null command. Fork a subshell if the command uses pipes or is
2597 to be run asynchronously. This handles all the side effects that are
2598 supposed to take place. */
2599static int
Jari Aalto95732b42005-12-07 14:08:12 +00002600execute_null_command (redirects, pipe_in, pipe_out, async)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002601 REDIRECT *redirects;
Jari Aaltobb706242000-03-17 21:46:59 +00002602 int pipe_in, pipe_out, async;
Jari Aalto726f6381996-08-26 18:22:31 +00002603{
Jari Aaltob80f6442004-07-27 13:29:18 +00002604 int r;
2605
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002606 if (pipe_in != NO_PIPE || pipe_out != NO_PIPE || async)
Jari Aalto726f6381996-08-26 18:22:31 +00002607 {
2608 /* We have a null command, but we really want a subshell to take
2609 care of it. Just fork, do piping and redirections, and exit. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002610 if (make_child ((char *)NULL, async) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002611 {
2612 /* Cancel traps, in trap.c. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002613 restore_original_signals (); /* XXX */
Jari Aalto726f6381996-08-26 18:22:31 +00002614
2615 do_piping (pipe_in, pipe_out);
2616
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002617 subshell_environment = SUBSHELL_ASYNC;
Jari Aalto726f6381996-08-26 18:22:31 +00002618
Jari Aaltob80f6442004-07-27 13:29:18 +00002619 if (do_redirections (redirects, RX_ACTIVE) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002620 exit (EXECUTION_SUCCESS);
2621 else
2622 exit (EXECUTION_FAILURE);
2623 }
2624 else
2625 {
2626 close_pipes (pipe_in, pipe_out);
2627#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
2628 unlink_fifo_list ();
2629#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002630 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +00002631 }
2632 }
2633 else
2634 {
2635 /* Even if there aren't any command names, pretend to do the
2636 redirections that are specified. The user expects the side
2637 effects to take place. If the redirections fail, then return
2638 failure. Otherwise, if a command substitution took place while
2639 expanding the command or a redirection, return the value of that
2640 substitution. Otherwise, return EXECUTION_SUCCESS. */
2641
Jari Aaltob80f6442004-07-27 13:29:18 +00002642 r = do_redirections (redirects, RX_ACTIVE|RX_UNDOABLE);
2643 cleanup_redirects (redirection_undo_list);
2644 redirection_undo_list = (REDIRECT *)NULL;
2645
2646 if (r != 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002647 return (EXECUTION_FAILURE);
Jari Aalto95732b42005-12-07 14:08:12 +00002648 else if (last_command_subst_pid != NO_PID)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002649 return (last_command_exit_value);
Jari Aalto726f6381996-08-26 18:22:31 +00002650 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002651 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +00002652 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002653}
2654
2655/* This is a hack to suppress word splitting for assignment statements
2656 given as arguments to builtins with the ASSIGNMENT_BUILTIN flag set. */
2657static void
2658fix_assignment_words (words)
2659 WORD_LIST *words;
2660{
2661 WORD_LIST *w;
2662 struct builtin *b;
2663
2664 if (words == 0)
2665 return;
2666
Jari Aalto7117c2d2002-07-17 14:10:11 +00002667 b = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002668
2669 for (w = words; w; w = w->next)
2670 if (w->word->flags & W_ASSIGNMENT)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002671 {
2672 if (b == 0)
2673 {
2674 b = builtin_address_internal (words->word->word, 0);
2675 if (b == 0 || (b->flags & ASSIGNMENT_BUILTIN) == 0)
2676 return;
Jari Aalto95732b42005-12-07 14:08:12 +00002677 else if (b && (b->flags & ASSIGNMENT_BUILTIN))
2678 words->word->flags |= W_ASSNBLTIN;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002679 }
Jari Aalto95732b42005-12-07 14:08:12 +00002680 w->word->flags |= (W_NOSPLIT|W_NOGLOB|W_TILDEEXP|W_ASSIGNARG);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002681 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002682}
2683
2684/* The meaty part of all the executions. We have to start hacking the
2685 real execution of commands here. Fork a process, set things up,
2686 execute the command. */
2687static int
2688execute_simple_command (simple_command, pipe_in, pipe_out, async, fds_to_close)
2689 SIMPLE_COM *simple_command;
2690 int pipe_in, pipe_out, async;
2691 struct fd_bitmap *fds_to_close;
2692{
2693 WORD_LIST *words, *lastword;
2694 char *command_line, *lastarg, *temp;
Jari Aaltobc4cd231998-07-23 14:37:54 +00002695 int first_word_quoted, result, builtin_is_special, already_forked, dofork;
Jari Aalto95732b42005-12-07 14:08:12 +00002696 pid_t old_last_async_pid;
Jari Aaltof73dda02001-11-13 17:56:06 +00002697 sh_builtin_func_t *builtin;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002698 SHELL_VAR *func;
2699
2700 result = EXECUTION_SUCCESS;
2701 special_builtin_failed = builtin_is_special = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +00002702 command_line = (char *)0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002703
2704 /* If we're in a function, update the line number information. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002705 if (variable_context && interactive_shell)
Jari Aaltob80f6442004-07-27 13:29:18 +00002706 line_number -= function_line_number;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002707
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002708 /* Remember what this command line looks like at invocation. */
2709 command_string_index = 0;
2710 print_simple_command (simple_command);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002711
Jari Aalto95732b42005-12-07 14:08:12 +00002712 if (signal_in_progress (DEBUG_TRAP) == 0 && (this_command_name == 0 || (STREQ (this_command_name, "trap") == 0)))
Jari Aaltob80f6442004-07-27 13:29:18 +00002713 {
2714 FREE (the_printed_command_except_trap);
Jari Aalto95732b42005-12-07 14:08:12 +00002715 the_printed_command_except_trap = the_printed_command ? savestring (the_printed_command) : (char *)0;
Jari Aaltob80f6442004-07-27 13:29:18 +00002716 }
2717
2718 /* Run the debug trap before each simple command, but do it after we
2719 update the line number information. */
2720 result = run_debug_trap ();
2721#if defined (DEBUGGER)
2722 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
2723 skip the command. */
2724 if (debugging_mode && result != EXECUTION_SUCCESS)
2725 return (EXECUTION_SUCCESS);
2726#endif
2727
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002728 first_word_quoted =
2729 simple_command->words ? (simple_command->words->word->flags & W_QUOTED): 0;
2730
Jari Aalto95732b42005-12-07 14:08:12 +00002731 last_command_subst_pid = NO_PID;
Jari Aaltob72432f1999-02-19 17:11:39 +00002732 old_last_async_pid = last_asynchronous_pid;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002733
Jari Aaltobc4cd231998-07-23 14:37:54 +00002734 already_forked = dofork = 0;
2735
2736 /* If we're in a pipeline or run in the background, set DOFORK so we
2737 make the child early, before word expansion. This keeps assignment
2738 statements from affecting the parent shell's environment when they
2739 should not. */
2740 dofork = pipe_in != NO_PIPE || pipe_out != NO_PIPE || async;
2741
2742 /* Something like `%2 &' should restart job 2 in the background, not cause
2743 the shell to fork here. */
2744 if (dofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE &&
2745 simple_command->words && simple_command->words->word &&
2746 simple_command->words->word->word &&
2747 (simple_command->words->word->word[0] == '%'))
2748 dofork = 0;
2749
2750 if (dofork)
Jari Aaltocce855b1998-04-17 19:52:44 +00002751 {
Jari Aaltobb706242000-03-17 21:46:59 +00002752 /* Do this now, because execute_disk_command will do it anyway in the
2753 vast majority of cases. */
2754 maybe_make_export_env ();
2755
Jari Aalto95732b42005-12-07 14:08:12 +00002756 /* Don't let a DEBUG trap overwrite the command string to be saved with
2757 the process/job associated with this child. */
2758 if (make_child (savestring (the_printed_command_except_trap), async) == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00002759 {
2760 already_forked = 1;
2761 simple_command->flags |= CMD_NO_FORK;
2762
Jari Aalto95732b42005-12-07 14:08:12 +00002763 subshell_environment = SUBSHELL_FORK;
2764 if (pipe_in != NO_PIPE || pipe_out != NO_PIPE)
2765 subshell_environment |= SUBSHELL_PIPE;
2766 if (async)
2767 subshell_environment |= SUBSHELL_ASYNC;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002768
Jari Aaltobb706242000-03-17 21:46:59 +00002769 /* We need to do this before piping to handle some really
2770 pathological cases where one of the pipe file descriptors
2771 is < 2. */
2772 if (fds_to_close)
2773 close_fd_bitmap (fds_to_close);
2774
Jari Aaltocce855b1998-04-17 19:52:44 +00002775 do_piping (pipe_in, pipe_out);
Jari Aaltof73dda02001-11-13 17:56:06 +00002776 pipe_in = pipe_out = NO_PIPE;
Jari Aaltocce855b1998-04-17 19:52:44 +00002777
Jari Aaltob72432f1999-02-19 17:11:39 +00002778 last_asynchronous_pid = old_last_async_pid;
Jari Aaltocce855b1998-04-17 19:52:44 +00002779 }
2780 else
2781 {
2782 close_pipes (pipe_in, pipe_out);
2783#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
2784 unlink_fifo_list ();
2785#endif
2786 command_line = (char *)NULL; /* don't free this. */
2787 bind_lastarg ((char *)NULL);
2788 return (result);
2789 }
2790 }
2791
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002792 /* If we are re-running this as the result of executing the `command'
2793 builtin, do not expand the command words a second time. */
2794 if ((simple_command->flags & CMD_INHIBIT_EXPANSION) == 0)
2795 {
2796 current_fds_to_close = fds_to_close;
2797 fix_assignment_words (simple_command->words);
2798 words = expand_words (simple_command->words);
2799 current_fds_to_close = (struct fd_bitmap *)NULL;
2800 }
2801 else
2802 words = copy_word_list (simple_command->words);
2803
2804 /* It is possible for WORDS not to have anything left in it.
2805 Perhaps all the words consisted of `$foo', and there was
2806 no variable `$foo'. */
2807 if (words == 0)
2808 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002809 this_command_name = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002810 result = execute_null_command (simple_command->redirects,
Jari Aaltocce855b1998-04-17 19:52:44 +00002811 pipe_in, pipe_out,
Jari Aalto95732b42005-12-07 14:08:12 +00002812 already_forked ? 0 : async);
Jari Aaltocce855b1998-04-17 19:52:44 +00002813 if (already_forked)
2814 exit (result);
2815 else
2816 {
2817 bind_lastarg ((char *)NULL);
2818 set_pipestatus_from_exit (result);
2819 return (result);
2820 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002821 }
2822
2823 lastarg = (char *)NULL;
2824
2825 begin_unwind_frame ("simple-command");
2826
2827 if (echo_command_at_execute)
Jari Aaltob80f6442004-07-27 13:29:18 +00002828 xtrace_print_word_list (words, 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002829
Jari Aaltof73dda02001-11-13 17:56:06 +00002830 builtin = (sh_builtin_func_t *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002831 func = (SHELL_VAR *)NULL;
2832 if ((simple_command->flags & CMD_NO_FUNCTIONS) == 0)
2833 {
2834 /* Posix.2 says special builtins are found before functions. We
2835 don't set builtin_is_special anywhere other than here, because
2836 this path is followed only when the `command' builtin is *not*
2837 being used, and we don't want to exit the shell if a special
2838 builtin executed with `command builtin' fails. `command' is not
2839 a special builtin. */
2840 if (posixly_correct)
2841 {
2842 builtin = find_special_builtin (words->word->word);
2843 if (builtin)
2844 builtin_is_special = 1;
2845 }
2846 if (builtin == 0)
2847 func = find_function (words->word->word);
2848 }
2849
Jari Aaltob80f6442004-07-27 13:29:18 +00002850 /* In POSIX mode, assignment errors in the temporary environment cause a
2851 non-interactive shell to exit. */
2852 if (builtin_is_special && interactive_shell == 0 && tempenv_assign_error)
2853 {
2854 last_command_exit_value = EXECUTION_FAILURE;
2855 jump_to_top_level (ERREXIT);
2856 }
2857
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002858 add_unwind_protect (dispose_words, words);
2859 QUIT;
2860
2861 /* Bind the last word in this command to "$_" after execution. */
2862 for (lastword = words; lastword->next; lastword = lastword->next)
2863 ;
2864 lastarg = lastword->word->word;
2865
2866#if defined (JOB_CONTROL)
2867 /* Is this command a job control related thing? */
Jari Aaltocce855b1998-04-17 19:52:44 +00002868 if (words->word->word[0] == '%' && already_forked == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002869 {
2870 this_command_name = async ? "bg" : "fg";
2871 last_shell_builtin = this_shell_builtin;
2872 this_shell_builtin = builtin_address (this_command_name);
2873 result = (*this_shell_builtin) (words);
2874 goto return_result;
2875 }
2876
2877 /* One other possiblilty. The user may want to resume an existing job.
2878 If they do, find out whether this word is a candidate for a running
2879 job. */
Jari Aaltocce855b1998-04-17 19:52:44 +00002880 if (job_control && already_forked == 0 && async == 0 &&
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002881 !first_word_quoted &&
2882 !words->next &&
2883 words->word->word[0] &&
2884 !simple_command->redirects &&
2885 pipe_in == NO_PIPE &&
2886 pipe_out == NO_PIPE &&
2887 (temp = get_string_value ("auto_resume")))
2888 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00002889 int job, jflags, started_status;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002890
Jari Aalto7117c2d2002-07-17 14:10:11 +00002891 jflags = JM_STOPPED|JM_FIRSTMATCH;
2892 if (STREQ (temp, "exact"))
2893 jflags |= JM_EXACT;
2894 else if (STREQ (temp, "substring"))
2895 jflags |= JM_SUBSTRING;
2896 else
2897 jflags |= JM_PREFIX;
2898 job = get_job_by_name (words->word->word, jflags);
2899 if (job != NO_JOB)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002900 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00002901 run_unwind_frame ("simple-command");
2902 this_command_name = "fg";
2903 last_shell_builtin = this_shell_builtin;
2904 this_shell_builtin = builtin_address ("fg");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002905
Jari Aalto7117c2d2002-07-17 14:10:11 +00002906 started_status = start_job (job, 1);
2907 return ((started_status < 0) ? EXECUTION_FAILURE : started_status);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002908 }
2909 }
2910#endif /* JOB_CONTROL */
2911
2912 /* Remember the name of this command globally. */
2913 this_command_name = words->word->word;
2914
2915 QUIT;
2916
2917 /* This command could be a shell builtin or a user-defined function.
2918 We have already found special builtins by this time, so we do not
2919 set builtin_is_special. If this is a function or builtin, and we
2920 have pipes, then fork a subshell in here. Otherwise, just execute
2921 the command directly. */
2922 if (func == 0 && builtin == 0)
2923 builtin = find_shell_builtin (this_command_name);
2924
2925 last_shell_builtin = this_shell_builtin;
2926 this_shell_builtin = builtin;
2927
2928 if (builtin || func)
2929 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00002930 if (already_forked)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002931 {
Jari Aaltocce855b1998-04-17 19:52:44 +00002932 /* reset_terminating_signals (); */ /* XXX */
2933 /* Cancel traps, in trap.c. */
2934 restore_original_signals ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002935
Jari Aaltocce855b1998-04-17 19:52:44 +00002936 if (async)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002937 {
Jari Aaltocce855b1998-04-17 19:52:44 +00002938 if ((simple_command->flags & CMD_STDIN_REDIR) &&
2939 pipe_in == NO_PIPE &&
2940 (stdin_redirects (simple_command->redirects) == 0))
2941 async_redirect_stdin ();
2942 setup_async_signals ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002943 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002944
Jari Aaltob80f6442004-07-27 13:29:18 +00002945 subshell_level++;
Jari Aaltocce855b1998-04-17 19:52:44 +00002946 execute_subshell_builtin_or_function
2947 (words, simple_command->redirects, builtin, func,
2948 pipe_in, pipe_out, async, fds_to_close,
2949 simple_command->flags);
Jari Aaltob80f6442004-07-27 13:29:18 +00002950 subshell_level--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002951 }
2952 else
2953 {
2954 result = execute_builtin_or_function
2955 (words, builtin, func, simple_command->redirects, fds_to_close,
2956 simple_command->flags);
2957 if (builtin)
2958 {
2959 if (result > EX_SHERRBASE)
2960 {
Jari Aaltocce855b1998-04-17 19:52:44 +00002961 result = builtin_status (result);
2962 if (builtin_is_special)
2963 special_builtin_failed = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002964 }
2965 /* In POSIX mode, if there are assignment statements preceding
2966 a special builtin, they persist after the builtin
2967 completes. */
2968 if (posixly_correct && builtin_is_special && temporary_env)
2969 merge_temporary_env ();
2970 }
2971 else /* function */
2972 {
2973 if (result == EX_USAGE)
2974 result = EX_BADUSAGE;
2975 else if (result > EX_SHERRBASE)
Jari Aaltocce855b1998-04-17 19:52:44 +00002976 result = EXECUTION_FAILURE;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002977 }
2978
Jari Aaltocce855b1998-04-17 19:52:44 +00002979 set_pipestatus_from_exit (result);
2980
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002981 goto return_result;
2982 }
2983 }
2984
Jari Aaltocce855b1998-04-17 19:52:44 +00002985 if (command_line == 0)
2986 command_line = savestring (the_printed_command);
2987
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002988 execute_disk_command (words, simple_command->redirects, command_line,
2989 pipe_in, pipe_out, async, fds_to_close,
Jari Aaltod166f041997-06-05 14:59:13 +00002990 simple_command->flags);
Jari Aalto726f6381996-08-26 18:22:31 +00002991
2992 return_result:
2993 bind_lastarg (lastarg);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002994 FREE (command_line);
Jari Aaltob80f6442004-07-27 13:29:18 +00002995 dispose_words (words);
2996 discard_unwind_frame ("simple-command");
2997 this_command_name = (char *)NULL; /* points to freed memory now */
Jari Aalto726f6381996-08-26 18:22:31 +00002998 return (result);
2999}
3000
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003001/* Translate the special builtin exit statuses. We don't really need a
3002 function for this; it's a placeholder for future work. */
3003static int
3004builtin_status (result)
3005 int result;
3006{
3007 int r;
3008
3009 switch (result)
3010 {
3011 case EX_USAGE:
3012 r = EX_BADUSAGE;
3013 break;
3014 case EX_REDIRFAIL:
3015 case EX_BADSYNTAX:
3016 case EX_BADASSIGN:
3017 case EX_EXPFAIL:
3018 r = EXECUTION_FAILURE;
3019 break;
3020 default:
3021 r = EXECUTION_SUCCESS;
3022 break;
3023 }
3024 return (r);
3025}
3026
Jari Aalto726f6381996-08-26 18:22:31 +00003027static int
3028execute_builtin (builtin, words, flags, subshell)
Jari Aaltof73dda02001-11-13 17:56:06 +00003029 sh_builtin_func_t *builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00003030 WORD_LIST *words;
3031 int flags, subshell;
3032{
Jari Aaltod166f041997-06-05 14:59:13 +00003033 int old_e_flag, result, eval_unwind;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003034 int isbltinenv;
Jari Aalto726f6381996-08-26 18:22:31 +00003035
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003036 old_e_flag = exit_immediately_on_error;
Jari Aalto726f6381996-08-26 18:22:31 +00003037 /* The eval builtin calls parse_and_execute, which does not know about
3038 the setting of flags, and always calls the execution functions with
3039 flags that will exit the shell on an error if -e is set. If the
3040 eval builtin is being called, and we're supposed to ignore the exit
3041 value of the command, we turn the -e flag off ourselves, then
3042 restore it when the command completes. */
3043 if (subshell == 0 && builtin == eval_builtin && (flags & CMD_IGNORE_RETURN))
3044 {
3045 begin_unwind_frame ("eval_builtin");
3046 unwind_protect_int (exit_immediately_on_error);
3047 exit_immediately_on_error = 0;
Jari Aaltod166f041997-06-05 14:59:13 +00003048 eval_unwind = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00003049 }
Jari Aaltod166f041997-06-05 14:59:13 +00003050 else
3051 eval_unwind = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00003052
3053 /* The temporary environment for a builtin is supposed to apply to
3054 all commands executed by that builtin. Currently, this is a
Jari Aalto95732b42005-12-07 14:08:12 +00003055 problem only with the `unset', `source' and `eval' builtins. */
3056
3057 isbltinenv = (builtin == source_builtin || builtin == eval_builtin || builtin == unset_builtin);
3058
Jari Aalto7117c2d2002-07-17 14:10:11 +00003059 if (isbltinenv)
Jari Aalto726f6381996-08-26 18:22:31 +00003060 {
3061 if (subshell == 0)
3062 begin_unwind_frame ("builtin_env");
3063
3064 if (temporary_env)
3065 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00003066 push_scope (VC_BLTNENV, temporary_env);
Jari Aalto726f6381996-08-26 18:22:31 +00003067 if (subshell == 0)
Jari Aalto95732b42005-12-07 14:08:12 +00003068 add_unwind_protect (pop_scope, (flags & CMD_COMMAND_BUILTIN) ? 0 : "1");
Jari Aalto7117c2d2002-07-17 14:10:11 +00003069 temporary_env = (HASH_TABLE *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00003070 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00003071 }
3072
3073 /* `return' does a longjmp() back to a saved environment in execute_function.
3074 If a variable assignment list preceded the command, and the shell is
3075 running in POSIX mode, we need to merge that into the shell_variables
3076 table, since `return' is a POSIX special builtin. */
3077 if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env)
3078 {
3079 begin_unwind_frame ("return_temp_env");
3080 add_unwind_protect (merge_temporary_env, (char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00003081 }
3082
3083 result = ((*builtin) (words->next));
3084
Jari Aalto28ef6c32001-04-06 19:14:31 +00003085 /* This shouldn't happen, but in case `return' comes back instead of
3086 longjmp'ing, we need to unwind. */
3087 if (posixly_correct && subshell == 0 && builtin == return_builtin && temporary_env)
3088 discard_unwind_frame ("return_temp_env");
3089
Jari Aalto7117c2d2002-07-17 14:10:11 +00003090 if (subshell == 0 && isbltinenv)
3091 run_unwind_frame ("builtin_env");
Jari Aalto726f6381996-08-26 18:22:31 +00003092
Jari Aaltod166f041997-06-05 14:59:13 +00003093 if (eval_unwind)
Jari Aalto726f6381996-08-26 18:22:31 +00003094 {
3095 exit_immediately_on_error += old_e_flag;
3096 discard_unwind_frame ("eval_builtin");
3097 }
3098
3099 return (result);
3100}
3101
Jari Aalto726f6381996-08-26 18:22:31 +00003102static int
3103execute_function (var, words, flags, fds_to_close, async, subshell)
3104 SHELL_VAR *var;
3105 WORD_LIST *words;
Jari Aaltof73dda02001-11-13 17:56:06 +00003106 int flags;
Jari Aalto726f6381996-08-26 18:22:31 +00003107 struct fd_bitmap *fds_to_close;
Jari Aaltof73dda02001-11-13 17:56:06 +00003108 int async, subshell;
Jari Aalto726f6381996-08-26 18:22:31 +00003109{
3110 int return_val, result;
Jari Aaltob80f6442004-07-27 13:29:18 +00003111 COMMAND *tc, *fc, *save_current;
3112 char *debug_trap, *error_trap, *return_trap;
3113#if defined (ARRAY_VARS)
Jari Aalto95732b42005-12-07 14:08:12 +00003114 SHELL_VAR *funcname_v, *nfv, *bash_source_v, *bash_lineno_v;
Jari Aaltob80f6442004-07-27 13:29:18 +00003115 ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
3116#endif
3117 FUNCTION_DEF *shell_fn;
3118 char *sfile, *t;
3119 static int funcnest = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +00003120
3121 USE_VAR(fc);
Jari Aalto726f6381996-08-26 18:22:31 +00003122
Jari Aaltob80f6442004-07-27 13:29:18 +00003123#if defined (ARRAY_VARS)
3124 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
3125 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
3126 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
3127#endif
3128
Jari Aalto726f6381996-08-26 18:22:31 +00003129 tc = (COMMAND *)copy_command (function_cell (var));
3130 if (tc && (flags & CMD_IGNORE_RETURN))
3131 tc->flags |= CMD_IGNORE_RETURN;
3132
Jari Aalto726f6381996-08-26 18:22:31 +00003133 if (subshell == 0)
3134 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003135 begin_unwind_frame ("function_calling");
Jari Aalto7117c2d2002-07-17 14:10:11 +00003136 push_context (var->name, subshell, temporary_env);
Jari Aalto726f6381996-08-26 18:22:31 +00003137 add_unwind_protect (pop_context, (char *)NULL);
3138 unwind_protect_int (line_number);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003139 unwind_protect_int (return_catch_flag);
3140 unwind_protect_jmp_buf (return_catch);
3141 add_unwind_protect (dispose_command, (char *)tc);
Jari Aaltobb706242000-03-17 21:46:59 +00003142 unwind_protect_pointer (this_shell_function);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003143 unwind_protect_int (loop_level);
Jari Aalto726f6381996-08-26 18:22:31 +00003144 }
Jari Aalto7117c2d2002-07-17 14:10:11 +00003145 else
3146 push_context (var->name, subshell, temporary_env); /* don't unwind-protect for subshells */
3147
3148 temporary_env = (HASH_TABLE *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00003149
Jari Aaltobb706242000-03-17 21:46:59 +00003150 this_shell_function = var;
3151 make_funcname_visible (1);
3152
Jari Aaltof73dda02001-11-13 17:56:06 +00003153 debug_trap = TRAP_STRING(DEBUG_TRAP);
3154 error_trap = TRAP_STRING(ERROR_TRAP);
Jari Aaltob80f6442004-07-27 13:29:18 +00003155 return_trap = TRAP_STRING(RETURN_TRAP);
Jari Aaltof73dda02001-11-13 17:56:06 +00003156
Jari Aaltob80f6442004-07-27 13:29:18 +00003157 /* The order of the unwind protects for debug_trap, error_trap and
3158 return_trap is important here! unwind-protect commands are run
3159 in reverse order of registration. If this causes problems, take
3160 out the xfree unwind-protect calls and live with the small memory leak. */
3161
3162 /* function_trace_mode != 0 means that all functions inherit the DEBUG trap.
3163 if the function has the trace attribute set, it inherits the DEBUG trap */
3164 if (debug_trap && ((trace_p (var) == 0) && function_trace_mode == 0))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003165 {
3166 if (subshell == 0)
3167 {
3168 debug_trap = savestring (debug_trap);
Jari Aaltocce855b1998-04-17 19:52:44 +00003169 add_unwind_protect (xfree, debug_trap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003170 add_unwind_protect (set_debug_trap, debug_trap);
3171 }
3172 restore_default_signal (DEBUG_TRAP);
3173 }
Jari Aalto726f6381996-08-26 18:22:31 +00003174
Jari Aaltob80f6442004-07-27 13:29:18 +00003175 /* error_trace_mode != 0 means that functions inherit the ERR trap. */
3176 if (error_trap && error_trace_mode == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +00003177 {
3178 if (subshell == 0)
3179 {
3180 error_trap = savestring (error_trap);
3181 add_unwind_protect (xfree, error_trap);
3182 add_unwind_protect (set_error_trap, error_trap);
3183 }
3184 restore_default_signal (ERROR_TRAP);
3185 }
3186
Jari Aalto95732b42005-12-07 14:08:12 +00003187 /* Shell functions inherit the RETURN trap if function tracing is on
3188 globally or on individually for this function. */
3189#if 0
Jari Aaltob80f6442004-07-27 13:29:18 +00003190 if (return_trap && ((trace_p (var) == 0) && function_trace_mode == 0))
Jari Aalto95732b42005-12-07 14:08:12 +00003191#else
3192 if (return_trap && (signal_in_progress (DEBUG_TRAP) || ((trace_p (var) == 0) && function_trace_mode == 0)))
3193#endif
Jari Aaltob80f6442004-07-27 13:29:18 +00003194 {
3195 if (subshell == 0)
3196 {
3197 return_trap = savestring (return_trap);
3198 add_unwind_protect (xfree, return_trap);
3199 add_unwind_protect (set_return_trap, return_trap);
3200 }
3201 restore_default_signal (RETURN_TRAP);
3202 }
3203
3204 funcnest++;
3205#if defined (ARRAY_VARS)
3206 /* This is quite similar to the code in shell.c and elsewhere. */
3207 shell_fn = find_function_def (this_shell_function->name);
3208 sfile = shell_fn ? shell_fn->source_file : "";
3209 array_push (funcname_a, this_shell_function->name);
3210
3211 array_push (bash_source_a, sfile);
3212 t = itos (executing_line_number ());
3213 array_push (bash_lineno_a, t);
3214 free (t);
3215#endif
3216
Jari Aalto726f6381996-08-26 18:22:31 +00003217 /* The temporary environment for a function is supposed to apply to
3218 all commands executed within the function body. */
Jari Aalto726f6381996-08-26 18:22:31 +00003219
Jari Aalto726f6381996-08-26 18:22:31 +00003220 remember_args (words->next, 1);
3221
Jari Aaltob80f6442004-07-27 13:29:18 +00003222 /* Update BASH_ARGV and BASH_ARGC */
3223 if (debugging_mode)
3224 push_args (words->next);
3225
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003226 /* Number of the line on which the function body starts. */
Jari Aaltob80f6442004-07-27 13:29:18 +00003227 line_number = function_line_number = tc->line;
Jari Aalto726f6381996-08-26 18:22:31 +00003228
Jari Aalto726f6381996-08-26 18:22:31 +00003229#if defined (JOB_CONTROL)
Jari Aalto95732b42005-12-07 14:08:12 +00003230 if (subshell)
3231 stop_pipeline (async, (COMMAND *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00003232#endif
Jari Aalto726f6381996-08-26 18:22:31 +00003233
Jari Aalto95732b42005-12-07 14:08:12 +00003234 fc = tc;
Jari Aalto726f6381996-08-26 18:22:31 +00003235
3236 return_catch_flag++;
3237 return_val = setjmp (return_catch);
3238
3239 if (return_val)
Jari Aalto95732b42005-12-07 14:08:12 +00003240 {
3241 result = return_catch_value;
3242 /* Run the RETURN trap in the function's context. */
3243 save_current = currently_executing_command;
3244 run_return_trap ();
3245 currently_executing_command = save_current;
3246 }
Jari Aalto726f6381996-08-26 18:22:31 +00003247 else
Jari Aaltob80f6442004-07-27 13:29:18 +00003248 {
3249 /* Run the debug trap here so we can trap at the start of a function's
3250 execution rather than the execution of the body's first command. */
3251 showing_function_line = 1;
3252 save_current = currently_executing_command;
3253 result = run_debug_trap ();
3254#if defined (DEBUGGER)
3255 /* In debugging mode, if the DEBUG trap returns a non-zero status, we
3256 skip the command. */
3257 if (debugging_mode == 0 || result == EXECUTION_SUCCESS)
3258 {
3259 showing_function_line = 0;
3260 currently_executing_command = save_current;
3261 result = execute_command_internal (fc, 0, NO_PIPE, NO_PIPE, fds_to_close);
3262
3263 /* Run the RETURN trap in the function's context */
3264 save_current = currently_executing_command;
3265 run_return_trap ();
3266 currently_executing_command = save_current;
3267 }
3268#else
3269 result = execute_command_internal (fc, 0, NO_PIPE, NO_PIPE, fds_to_close);
Jari Aalto95732b42005-12-07 14:08:12 +00003270
3271 save_current = currently_executing_command;
3272 run_return_trap ();
3273 currently_executing_command = save_current;
Jari Aaltob80f6442004-07-27 13:29:18 +00003274#endif
3275 showing_function_line = 0;
3276 }
3277
3278 /* Restore BASH_ARGC and BASH_ARGV */
3279 if (debugging_mode)
3280 pop_args ();
Jari Aalto726f6381996-08-26 18:22:31 +00003281
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003282 if (subshell == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003283 run_unwind_frame ("function_calling");
3284
Jari Aaltob80f6442004-07-27 13:29:18 +00003285 funcnest--;
3286#if defined (ARRAY_VARS)
Jari Aalto95732b42005-12-07 14:08:12 +00003287 /* These two variables cannot be unset, and cannot be affected by the
3288 function. */
Jari Aaltob80f6442004-07-27 13:29:18 +00003289 array_pop (bash_source_a);
Jari Aaltob80f6442004-07-27 13:29:18 +00003290 array_pop (bash_lineno_a);
Jari Aalto95732b42005-12-07 14:08:12 +00003291
3292 /* FUNCNAME can be unset, and so can potentially be changed by the
3293 function. */
3294 GET_ARRAY_FROM_VAR ("FUNCNAME", nfv, funcname_a);
3295 if (nfv == funcname_v)
3296 array_pop (funcname_a);
Jari Aaltob80f6442004-07-27 13:29:18 +00003297#endif
3298
Jari Aaltobb706242000-03-17 21:46:59 +00003299 if (variable_context == 0 || this_shell_function == 0)
3300 make_funcname_visible (0);
3301
Jari Aalto726f6381996-08-26 18:22:31 +00003302 return (result);
3303}
3304
Jari Aaltobb706242000-03-17 21:46:59 +00003305/* A convenience routine for use by other parts of the shell to execute
3306 a particular shell function. */
3307int
3308execute_shell_function (var, words)
3309 SHELL_VAR *var;
3310 WORD_LIST *words;
3311{
3312 int ret;
3313 struct fd_bitmap *bitmap;
3314
3315 bitmap = new_fd_bitmap (FD_BITMAP_DEFAULT_SIZE);
3316 begin_unwind_frame ("execute-shell-function");
3317 add_unwind_protect (dispose_fd_bitmap, (char *)bitmap);
3318
3319 ret = execute_function (var, words, 0, bitmap, 0, 0);
3320
3321 dispose_fd_bitmap (bitmap);
3322 discard_unwind_frame ("execute-shell-function");
3323
3324 return ret;
3325}
3326
Jari Aalto726f6381996-08-26 18:22:31 +00003327/* Execute a shell builtin or function in a subshell environment. This
3328 routine does not return; it only calls exit(). If BUILTIN is non-null,
3329 it points to a function to call to execute a shell builtin; otherwise
3330 VAR points at the body of a function to execute. WORDS is the arguments
3331 to the command, REDIRECTS specifies redirections to perform before the
3332 command is executed. */
3333static void
3334execute_subshell_builtin_or_function (words, redirects, builtin, var,
3335 pipe_in, pipe_out, async, fds_to_close,
3336 flags)
3337 WORD_LIST *words;
3338 REDIRECT *redirects;
Jari Aaltof73dda02001-11-13 17:56:06 +00003339 sh_builtin_func_t *builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00003340 SHELL_VAR *var;
3341 int pipe_in, pipe_out, async;
3342 struct fd_bitmap *fds_to_close;
3343 int flags;
3344{
Jari Aaltobb706242000-03-17 21:46:59 +00003345 int result, r;
3346#if defined (JOB_CONTROL)
3347 int jobs_hack;
Jari Aalto726f6381996-08-26 18:22:31 +00003348
Jari Aaltob72432f1999-02-19 17:11:39 +00003349 jobs_hack = (builtin == jobs_builtin) &&
3350 ((subshell_environment & SUBSHELL_ASYNC) == 0 || pipe_out != NO_PIPE);
Jari Aaltobb706242000-03-17 21:46:59 +00003351#endif
3352
3353 /* A subshell is neither a login shell nor interactive. */
3354 login_shell = interactive = 0;
Jari Aaltob72432f1999-02-19 17:11:39 +00003355
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003356 subshell_environment = SUBSHELL_ASYNC;
Jari Aalto726f6381996-08-26 18:22:31 +00003357
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003358 maybe_make_export_env (); /* XXX - is this needed? */
Jari Aalto726f6381996-08-26 18:22:31 +00003359
3360#if defined (JOB_CONTROL)
3361 /* Eradicate all traces of job control after we fork the subshell, so
3362 all jobs begun by this subshell are in the same process group as
3363 the shell itself. */
3364
3365 /* Allow the output of `jobs' to be piped. */
Jari Aaltob72432f1999-02-19 17:11:39 +00003366 if (jobs_hack)
Jari Aalto726f6381996-08-26 18:22:31 +00003367 kill_current_pipeline ();
3368 else
3369 without_job_control ();
3370
3371 set_sigchld_handler ();
3372#endif /* JOB_CONTROL */
3373
3374 set_sigint_handler ();
3375
Jari Aalto726f6381996-08-26 18:22:31 +00003376 if (fds_to_close)
3377 close_fd_bitmap (fds_to_close);
3378
Jari Aaltobb706242000-03-17 21:46:59 +00003379 do_piping (pipe_in, pipe_out);
3380
Jari Aaltob80f6442004-07-27 13:29:18 +00003381 if (do_redirections (redirects, RX_ACTIVE) != 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003382 exit (EXECUTION_FAILURE);
3383
3384 if (builtin)
3385 {
Jari Aalto726f6381996-08-26 18:22:31 +00003386 /* Give builtins a place to jump back to on failure,
3387 so we don't go back up to main(). */
3388 result = setjmp (top_level);
3389
3390 if (result == EXITPROG)
3391 exit (last_command_exit_value);
3392 else if (result)
3393 exit (EXECUTION_FAILURE);
3394 else
Jari Aaltocce855b1998-04-17 19:52:44 +00003395 {
3396 r = execute_builtin (builtin, words, flags, 1);
3397 if (r == EX_USAGE)
3398 r = EX_BADUSAGE;
3399 exit (r);
3400 }
Jari Aalto726f6381996-08-26 18:22:31 +00003401 }
3402 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003403 exit (execute_function (var, words, flags, fds_to_close, async, 1));
Jari Aalto726f6381996-08-26 18:22:31 +00003404}
3405
3406/* Execute a builtin or function in the current shell context. If BUILTIN
3407 is non-null, it is the builtin command to execute, otherwise VAR points
3408 to the body of a function. WORDS are the command's arguments, REDIRECTS
3409 are the redirections to perform. FDS_TO_CLOSE is the usual bitmap of
3410 file descriptors to close.
3411
3412 If BUILTIN is exec_builtin, the redirections specified in REDIRECTS are
3413 not undone before this function returns. */
3414static int
3415execute_builtin_or_function (words, builtin, var, redirects,
3416 fds_to_close, flags)
3417 WORD_LIST *words;
Jari Aaltof73dda02001-11-13 17:56:06 +00003418 sh_builtin_func_t *builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00003419 SHELL_VAR *var;
3420 REDIRECT *redirects;
3421 struct fd_bitmap *fds_to_close;
3422 int flags;
3423{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003424 int result;
Jari Aalto726f6381996-08-26 18:22:31 +00003425 REDIRECT *saved_undo_list;
Jari Aaltof73dda02001-11-13 17:56:06 +00003426 sh_builtin_func_t *saved_this_shell_builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00003427
Jari Aaltob80f6442004-07-27 13:29:18 +00003428 if (do_redirections (redirects, RX_ACTIVE|RX_UNDOABLE) != 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003429 {
3430 cleanup_redirects (redirection_undo_list);
3431 redirection_undo_list = (REDIRECT *)NULL;
3432 dispose_exec_redirects ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003433 return (EX_REDIRFAIL); /* was EXECUTION_FAILURE */
Jari Aalto726f6381996-08-26 18:22:31 +00003434 }
3435
Jari Aalto28ef6c32001-04-06 19:14:31 +00003436 saved_this_shell_builtin = this_shell_builtin;
Jari Aalto726f6381996-08-26 18:22:31 +00003437 saved_undo_list = redirection_undo_list;
3438
3439 /* Calling the "exec" builtin changes redirections forever. */
3440 if (builtin == exec_builtin)
3441 {
3442 dispose_redirects (saved_undo_list);
3443 saved_undo_list = exec_redirection_undo_list;
3444 exec_redirection_undo_list = (REDIRECT *)NULL;
3445 }
3446 else
3447 dispose_exec_redirects ();
3448
3449 if (saved_undo_list)
3450 {
3451 begin_unwind_frame ("saved redirects");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003452 add_unwind_protect (cleanup_redirects, (char *)saved_undo_list);
Jari Aalto726f6381996-08-26 18:22:31 +00003453 }
3454
3455 redirection_undo_list = (REDIRECT *)NULL;
3456
3457 if (builtin)
3458 result = execute_builtin (builtin, words, flags, 0);
3459 else
3460 result = execute_function (var, words, flags, fds_to_close, 0, 0);
3461
Jari Aaltob80f6442004-07-27 13:29:18 +00003462 /* We do this before undoing the effects of any redirections. */
3463 if (ferror (stdout))
3464 clearerr (stdout);
3465
Jari Aalto28ef6c32001-04-06 19:14:31 +00003466 /* If we are executing the `command' builtin, but this_shell_builtin is
3467 set to `exec_builtin', we know that we have something like
3468 `command exec [redirection]', since otherwise `exec' would have
3469 overwritten the shell and we wouldn't get here. In this case, we
3470 want to behave as if the `command' builtin had not been specified
3471 and preserve the redirections. */
3472 if (builtin == command_builtin && this_shell_builtin == exec_builtin)
3473 {
3474 if (saved_undo_list)
3475 dispose_redirects (saved_undo_list);
3476 redirection_undo_list = exec_redirection_undo_list;
3477 saved_undo_list = exec_redirection_undo_list = (REDIRECT *)NULL;
3478 discard_unwind_frame ("saved_redirects");
3479 }
3480
Jari Aalto726f6381996-08-26 18:22:31 +00003481 if (saved_undo_list)
3482 {
3483 redirection_undo_list = saved_undo_list;
3484 discard_unwind_frame ("saved redirects");
3485 }
3486
3487 if (redirection_undo_list)
3488 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003489 cleanup_redirects (redirection_undo_list);
Jari Aalto726f6381996-08-26 18:22:31 +00003490 redirection_undo_list = (REDIRECT *)NULL;
3491 }
3492
3493 return (result);
3494}
3495
3496void
3497setup_async_signals ()
3498{
Jari Aaltob72432f1999-02-19 17:11:39 +00003499#if defined (__BEOS__)
3500 set_signal_handler (SIGHUP, SIG_IGN); /* they want csh-like behavior */
3501#endif
3502
Jari Aalto726f6381996-08-26 18:22:31 +00003503#if defined (JOB_CONTROL)
3504 if (job_control == 0)
3505#endif
3506 {
3507 set_signal_handler (SIGINT, SIG_IGN);
3508 set_signal_ignored (SIGINT);
3509 set_signal_handler (SIGQUIT, SIG_IGN);
3510 set_signal_ignored (SIGQUIT);
3511 }
3512}
3513
3514/* Execute a simple command that is hopefully defined in a disk file
3515 somewhere.
3516
3517 1) fork ()
3518 2) connect pipes
3519 3) look up the command
3520 4) do redirections
3521 5) execve ()
3522 6) If the execve failed, see if the file has executable mode set.
3523 If so, and it isn't a directory, then execute its contents as
3524 a shell script.
3525
3526 Note that the filename hashing stuff has to take place up here,
3527 in the parent. This is probably why the Bourne style shells
3528 don't handle it, since that would require them to go through
Jari Aaltob80f6442004-07-27 13:29:18 +00003529 this gnarly hair, for no good reason.
3530
3531 NOTE: callers expect this to fork or exit(). */
Jari Aalto726f6381996-08-26 18:22:31 +00003532static void
3533execute_disk_command (words, redirects, command_line, pipe_in, pipe_out,
Jari Aaltod166f041997-06-05 14:59:13 +00003534 async, fds_to_close, cmdflags)
Jari Aalto726f6381996-08-26 18:22:31 +00003535 WORD_LIST *words;
3536 REDIRECT *redirects;
3537 char *command_line;
3538 int pipe_in, pipe_out, async;
3539 struct fd_bitmap *fds_to_close;
Jari Aaltod166f041997-06-05 14:59:13 +00003540 int cmdflags;
Jari Aalto726f6381996-08-26 18:22:31 +00003541{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003542 char *pathname, *command, **args;
Jari Aaltod166f041997-06-05 14:59:13 +00003543 int nofork;
Jari Aaltobb706242000-03-17 21:46:59 +00003544 pid_t pid;
Jari Aalto726f6381996-08-26 18:22:31 +00003545
Jari Aaltod166f041997-06-05 14:59:13 +00003546 nofork = (cmdflags & CMD_NO_FORK); /* Don't fork, just exec, if no pipes */
Jari Aalto726f6381996-08-26 18:22:31 +00003547 pathname = words->word->word;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003548
Jari Aalto726f6381996-08-26 18:22:31 +00003549#if defined (RESTRICTED_SHELL)
Jari Aaltob80f6442004-07-27 13:29:18 +00003550 command = (char *)NULL;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003551 if (restricted && xstrchr (pathname, '/'))
Jari Aalto726f6381996-08-26 18:22:31 +00003552 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003553 internal_error (_("%s: restricted: cannot specify `/' in command names"),
Jari Aalto726f6381996-08-26 18:22:31 +00003554 pathname);
3555 last_command_exit_value = EXECUTION_FAILURE;
Jari Aaltob80f6442004-07-27 13:29:18 +00003556
3557 /* If we're not going to fork below, we must already be in a child
3558 process or a context in which it's safe to call exit(2). */
3559 if (nofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE)
3560 exit (last_command_exit_value);
3561 else
3562 goto parent_return;
Jari Aalto726f6381996-08-26 18:22:31 +00003563 }
3564#endif /* RESTRICTED_SHELL */
3565
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003566 command = search_for_command (pathname);
Jari Aalto726f6381996-08-26 18:22:31 +00003567
3568 if (command)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003569 {
3570 maybe_make_export_env ();
3571 put_command_name_into_env (command);
3572 }
Jari Aalto726f6381996-08-26 18:22:31 +00003573
Jari Aaltobb706242000-03-17 21:46:59 +00003574 /* We have to make the child before we check for the non-existence
Jari Aalto726f6381996-08-26 18:22:31 +00003575 of COMMAND, since we want the error messages to be redirected. */
3576 /* If we can get away without forking and there are no pipes to deal with,
3577 don't bother to fork, just directly exec the command. */
3578 if (nofork && pipe_in == NO_PIPE && pipe_out == NO_PIPE)
3579 pid = 0;
3580 else
3581 pid = make_child (savestring (command_line), async);
3582
3583 if (pid == 0)
3584 {
3585 int old_interactive;
3586
Jari Aaltod166f041997-06-05 14:59:13 +00003587#if 0
3588 /* This has been disabled for the time being. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003589#if !defined (ARG_MAX) || ARG_MAX >= 10240
3590 if (posixly_correct == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +00003591 put_gnu_argv_flags_into_env ((long)getpid (), glob_argv_flags);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003592#endif
Jari Aaltod166f041997-06-05 14:59:13 +00003593#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003594
Jari Aalto726f6381996-08-26 18:22:31 +00003595 /* Cancel traps, in trap.c. */
3596 restore_original_signals ();
3597
3598 /* restore_original_signals may have undone the work done
Jari Aaltocce855b1998-04-17 19:52:44 +00003599 by make_child to ensure that SIGINT and SIGQUIT are ignored
3600 in asynchronous children. */
Jari Aalto726f6381996-08-26 18:22:31 +00003601 if (async)
Jari Aaltod166f041997-06-05 14:59:13 +00003602 {
3603 if ((cmdflags & CMD_STDIN_REDIR) &&
3604 pipe_in == NO_PIPE &&
3605 (stdin_redirects (redirects) == 0))
3606 async_redirect_stdin ();
3607 setup_async_signals ();
3608 }
Jari Aalto726f6381996-08-26 18:22:31 +00003609
Jari Aaltobb706242000-03-17 21:46:59 +00003610 /* This functionality is now provided by close-on-exec of the
3611 file descriptors manipulated by redirection and piping.
3612 Some file descriptors still need to be closed in all children
3613 because of the way bash does pipes; fds_to_close is a
3614 bitmap of all such file descriptors. */
3615 if (fds_to_close)
3616 close_fd_bitmap (fds_to_close);
3617
Jari Aalto726f6381996-08-26 18:22:31 +00003618 do_piping (pipe_in, pipe_out);
3619
Jari Aaltof73dda02001-11-13 17:56:06 +00003620 old_interactive = interactive;
Jari Aalto726f6381996-08-26 18:22:31 +00003621 if (async)
Jari Aaltof73dda02001-11-13 17:56:06 +00003622 interactive = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00003623
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003624 subshell_environment = SUBSHELL_FORK;
Jari Aalto726f6381996-08-26 18:22:31 +00003625
Jari Aaltob80f6442004-07-27 13:29:18 +00003626 if (redirects && (do_redirections (redirects, RX_ACTIVE) != 0))
Jari Aalto726f6381996-08-26 18:22:31 +00003627 {
3628#if defined (PROCESS_SUBSTITUTION)
3629 /* Try to remove named pipes that may have been created as the
3630 result of redirections. */
3631 unlink_fifo_list ();
3632#endif /* PROCESS_SUBSTITUTION */
3633 exit (EXECUTION_FAILURE);
3634 }
3635
3636 if (async)
3637 interactive = old_interactive;
3638
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003639 if (command == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00003640 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003641 internal_error (_("%s: command not found"), pathname);
Jari Aalto726f6381996-08-26 18:22:31 +00003642 exit (EX_NOTFOUND); /* Posix.2 says the exit status is 127 */
3643 }
3644
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003645 /* Execve expects the command name to be in args[0]. So we
3646 leave it there, in the same format that the user used to
3647 type it in. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00003648 args = strvec_from_word_list (words, 0, 0, (int *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00003649 exit (shell_execve (command, args, export_env));
3650 }
3651 else
3652 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003653parent_return:
Jari Aalto726f6381996-08-26 18:22:31 +00003654 /* Make sure that the pipes are closed in the parent. */
3655 close_pipes (pipe_in, pipe_out);
3656#if defined (PROCESS_SUBSTITUTION) && defined (HAVE_DEV_FD)
3657 unlink_fifo_list ();
3658#endif
3659 FREE (command);
3660 }
3661}
3662
Jari Aaltof73dda02001-11-13 17:56:06 +00003663/* CPP defines to decide whether a particular index into the #! line
3664 corresponds to a valid interpreter name or argument character, or
3665 whitespace. The MSDOS define is to allow \r to be treated the same
3666 as \n. */
3667
3668#if !defined (MSDOS)
3669# define STRINGCHAR(ind) \
3670 (ind < sample_len && !whitespace (sample[ind]) && sample[ind] != '\n')
3671# define WHITECHAR(ind) \
3672 (ind < sample_len && whitespace (sample[ind]))
3673#else /* MSDOS */
3674# define STRINGCHAR(ind) \
3675 (ind < sample_len && !whitespace (sample[ind]) && sample[ind] != '\n' && sample[ind] != '\r')
3676# define WHITECHAR(ind) \
3677 (ind < sample_len && whitespace (sample[ind]))
3678#endif /* MSDOS */
3679
3680static char *
3681getinterp (sample, sample_len, endp)
3682 char *sample;
3683 int sample_len, *endp;
3684{
3685 register int i;
3686 char *execname;
3687 int start;
3688
3689 /* Find the name of the interpreter to exec. */
3690 for (i = 2; i < sample_len && whitespace (sample[i]); i++)
3691 ;
3692
3693 for (start = i; STRINGCHAR(i); i++)
3694 ;
3695
3696 execname = substring (sample, start, i);
3697
3698 if (endp)
3699 *endp = i;
3700 return execname;
3701}
3702
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003703#if !defined (HAVE_HASH_BANG_EXEC)
Jari Aalto726f6381996-08-26 18:22:31 +00003704/* If the operating system on which we're running does not handle
3705 the #! executable format, then help out. SAMPLE is the text read
3706 from the file, SAMPLE_LEN characters. COMMAND is the name of
3707 the script; it and ARGS, the arguments given by the user, will
3708 become arguments to the specified interpreter. ENV is the environment
3709 to pass to the interpreter.
3710
3711 The word immediately following the #! is the interpreter to execute.
3712 A single argument to the interpreter is allowed. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00003713
Jari Aalto726f6381996-08-26 18:22:31 +00003714static int
3715execute_shell_script (sample, sample_len, command, args, env)
Jari Aaltof73dda02001-11-13 17:56:06 +00003716 char *sample;
Jari Aalto726f6381996-08-26 18:22:31 +00003717 int sample_len;
3718 char *command;
3719 char **args, **env;
3720{
Jari Aalto726f6381996-08-26 18:22:31 +00003721 char *execname, *firstarg;
Jari Aaltof73dda02001-11-13 17:56:06 +00003722 int i, start, size_increment, larry;
Jari Aalto726f6381996-08-26 18:22:31 +00003723
3724 /* Find the name of the interpreter to exec. */
Jari Aaltof73dda02001-11-13 17:56:06 +00003725 execname = getinterp (sample, sample_len, &i);
Jari Aalto726f6381996-08-26 18:22:31 +00003726 size_increment = 1;
3727
3728 /* Now the argument, if any. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00003729 for (firstarg = (char *)NULL, start = i; WHITECHAR(i); i++)
Jari Aalto726f6381996-08-26 18:22:31 +00003730 ;
3731
3732 /* If there is more text on the line, then it is an argument for the
3733 interpreter. */
Jari Aalto726f6381996-08-26 18:22:31 +00003734
Jari Aalto28ef6c32001-04-06 19:14:31 +00003735 if (STRINGCHAR(i))
3736 {
3737 for (start = i; STRINGCHAR(i); i++)
3738 ;
3739 firstarg = substring ((char *)sample, start, i);
Jari Aalto726f6381996-08-26 18:22:31 +00003740 size_increment = 2;
3741 }
3742
Jari Aalto7117c2d2002-07-17 14:10:11 +00003743 larry = strvec_len (args) + size_increment;
3744 args = strvec_resize (args, larry + 1);
Jari Aalto726f6381996-08-26 18:22:31 +00003745
3746 for (i = larry - 1; i; i--)
3747 args[i] = args[i - size_increment];
3748
3749 args[0] = execname;
3750 if (firstarg)
3751 {
3752 args[1] = firstarg;
3753 args[2] = command;
3754 }
3755 else
3756 args[1] = command;
3757
3758 args[larry] = (char *)NULL;
3759
3760 return (shell_execve (execname, args, env));
3761}
Jari Aalto28ef6c32001-04-06 19:14:31 +00003762#undef STRINGCHAR
3763#undef WHITECHAR
3764
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003765#endif /* !HAVE_HASH_BANG_EXEC */
3766
Jari Aaltod166f041997-06-05 14:59:13 +00003767static void
3768initialize_subshell ()
3769{
3770#if defined (ALIAS)
3771 /* Forget about any aliases that we knew of. We are in a subshell. */
3772 delete_all_aliases ();
3773#endif /* ALIAS */
3774
3775#if defined (HISTORY)
3776 /* Forget about the history lines we have read. This is a non-interactive
3777 subshell. */
3778 history_lines_this_session = 0;
3779#endif
3780
3781#if defined (JOB_CONTROL)
3782 /* Forget about the way job control was working. We are in a subshell. */
3783 without_job_control ();
3784 set_sigchld_handler ();
Jari Aalto95732b42005-12-07 14:08:12 +00003785 init_job_stats ();
Jari Aaltod166f041997-06-05 14:59:13 +00003786#endif /* JOB_CONTROL */
3787
3788 /* Reset the values of the shell flags and options. */
3789 reset_shell_flags ();
3790 reset_shell_options ();
3791 reset_shopt_options ();
3792
Jari Aalto28ef6c32001-04-06 19:14:31 +00003793 /* Zero out builtin_env, since this could be a shell script run from a
3794 sourced file with a temporary environment supplied to the `source/.'
3795 builtin. Such variables are not supposed to be exported (empirical
Jari Aalto7117c2d2002-07-17 14:10:11 +00003796 testing with sh and ksh). Just throw it away; don't worry about a
3797 memory leak. */
3798 if (vc_isbltnenv (shell_variables))
3799 shell_variables = shell_variables->down;
Jari Aalto28ef6c32001-04-06 19:14:31 +00003800
3801 clear_unwind_protect_list (0);
3802
3803 /* We're no longer inside a shell function. */
3804 variable_context = return_catch_flag = 0;
3805
Jari Aaltod166f041997-06-05 14:59:13 +00003806 /* If we're not interactive, close the file descriptor from which we're
3807 reading the current shell script. */
Jari Aaltocce855b1998-04-17 19:52:44 +00003808 if (interactive_shell == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00003809 unset_bash_input (0);
Jari Aaltod166f041997-06-05 14:59:13 +00003810}
3811
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003812#if defined (HAVE_SETOSTYPE) && defined (_POSIX_SOURCE)
3813# define SETOSTYPE(x) __setostype(x)
3814#else
3815# define SETOSTYPE(x)
3816#endif
Jari Aalto726f6381996-08-26 18:22:31 +00003817
Jari Aalto28ef6c32001-04-06 19:14:31 +00003818#define READ_SAMPLE_BUF(file, buf, len) \
3819 do \
3820 { \
3821 fd = open(file, O_RDONLY); \
3822 if (fd >= 0) \
3823 { \
Jari Aaltof73dda02001-11-13 17:56:06 +00003824 len = read (fd, buf, 80); \
Jari Aalto28ef6c32001-04-06 19:14:31 +00003825 close (fd); \
3826 } \
3827 else \
3828 len = -1; \
3829 } \
3830 while (0)
3831
Jari Aalto726f6381996-08-26 18:22:31 +00003832/* Call execve (), handling interpreting shell scripts, and handling
3833 exec failures. */
3834int
3835shell_execve (command, args, env)
3836 char *command;
3837 char **args, **env;
3838{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003839 struct stat finfo;
3840 int larray, i, fd;
Jari Aaltof73dda02001-11-13 17:56:06 +00003841 char sample[80];
Jari Aalto28ef6c32001-04-06 19:14:31 +00003842 int sample_len;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003843
3844 SETOSTYPE (0); /* Some systems use for USG/POSIX semantics */
Jari Aalto726f6381996-08-26 18:22:31 +00003845 execve (command, args, env);
Jari Aalto28ef6c32001-04-06 19:14:31 +00003846 i = errno; /* error from execve() */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003847 SETOSTYPE (1);
Jari Aalto726f6381996-08-26 18:22:31 +00003848
3849 /* If we get to this point, then start checking out the file.
3850 Maybe it is something we can hack ourselves. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00003851 if (i != ENOEXEC)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003852 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003853 if ((stat (command, &finfo) == 0) && (S_ISDIR (finfo.st_mode)))
Jari Aaltob80f6442004-07-27 13:29:18 +00003854 internal_error (_("%s: is a directory"), command);
Jari Aalto7117c2d2002-07-17 14:10:11 +00003855 else if (executable_file (command) == 0)
3856 {
3857 errno = i;
3858 file_error (command);
3859 }
Jari Aalto95732b42005-12-07 14:08:12 +00003860 /* errors not involving the path argument to execve. */
3861 else if (i == E2BIG || i == ENOMEM)
3862 {
3863 errno = i;
3864 file_error (command);
3865 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003866 else
Jari Aalto726f6381996-08-26 18:22:31 +00003867 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00003868 /* The file has the execute bits set, but the kernel refuses to
3869 run it for some reason. See why. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00003870#if defined (HAVE_HASH_BANG_EXEC)
3871 READ_SAMPLE_BUF (command, sample, sample_len);
3872 if (sample_len > 2 && sample[0] == '#' && sample[1] == '!')
3873 {
Jari Aaltof73dda02001-11-13 17:56:06 +00003874 char *interp;
Jari Aalto95732b42005-12-07 14:08:12 +00003875 int ilen;
Jari Aaltof73dda02001-11-13 17:56:06 +00003876
3877 interp = getinterp (sample, sample_len, (int *)NULL);
Jari Aalto95732b42005-12-07 14:08:12 +00003878 ilen = strlen (interp);
Jari Aalto28ef6c32001-04-06 19:14:31 +00003879 errno = i;
Jari Aalto95732b42005-12-07 14:08:12 +00003880 if (interp[ilen - 1] == '\r')
3881 {
3882 interp = xrealloc (interp, ilen + 2);
3883 interp[ilen - 1] = '^';
3884 interp[ilen] = 'M';
3885 interp[ilen + 1] = '\0';
3886 }
Jari Aaltob80f6442004-07-27 13:29:18 +00003887 sys_error (_("%s: %s: bad interpreter"), command, interp ? interp : "");
Jari Aaltof73dda02001-11-13 17:56:06 +00003888 FREE (interp);
Jari Aalto28ef6c32001-04-06 19:14:31 +00003889 return (EX_NOEXEC);
3890 }
3891#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003892 errno = i;
3893 file_error (command);
3894 }
Jari Aaltod166f041997-06-05 14:59:13 +00003895 return ((i == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); /* XXX Posix.2 says that exit status is 126 */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003896 }
3897
3898 /* This file is executable.
3899 If it begins with #!, then help out people with losing operating
3900 systems. Otherwise, check to see if it is a binary file by seeing
Jari Aalto28ef6c32001-04-06 19:14:31 +00003901 if the contents of the first line (or up to 80 characters) are in the
3902 ASCII set. If it's a text file, execute the contents as shell commands,
3903 otherwise return 126 (EX_BINARY_FILE). */
3904 READ_SAMPLE_BUF (command, sample, sample_len);
3905
3906 if (sample_len == 0)
3907 return (EXECUTION_SUCCESS);
3908
3909 /* Is this supposed to be an executable script?
3910 If so, the format of the line is "#! interpreter [argument]".
3911 A single argument is allowed. The BSD kernel restricts
3912 the length of the entire line to 32 characters (32 bytes
3913 being the size of the BSD exec header), but we allow 80
3914 characters. */
3915 if (sample_len > 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003916 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003917#if !defined (HAVE_HASH_BANG_EXEC)
Jari Aalto28ef6c32001-04-06 19:14:31 +00003918 if (sample_len > 2 && sample[0] == '#' && sample[1] == '!')
3919 return (execute_shell_script (sample, sample_len, command, args, env));
3920 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003921#endif
Jari Aalto28ef6c32001-04-06 19:14:31 +00003922 if (check_binary_file (sample, sample_len))
3923 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003924 internal_error (_("%s: cannot execute binary file"), command);
Jari Aalto28ef6c32001-04-06 19:14:31 +00003925 return (EX_BINARY_FILE);
Jari Aalto726f6381996-08-26 18:22:31 +00003926 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003927 }
3928
Jari Aalto28ef6c32001-04-06 19:14:31 +00003929 /* We have committed to attempting to execute the contents of this file
3930 as shell commands. */
3931
Jari Aaltod166f041997-06-05 14:59:13 +00003932 initialize_subshell ();
Jari Aalto726f6381996-08-26 18:22:31 +00003933
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003934 set_sigint_handler ();
Jari Aalto726f6381996-08-26 18:22:31 +00003935
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003936 /* Insert the name of this shell into the argument list. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00003937 larray = strvec_len (args) + 1;
3938 args = strvec_resize (args, larray + 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003939
3940 for (i = larray - 1; i; i--)
3941 args[i] = args[i - 1];
3942
3943 args[0] = shell_name;
3944 args[1] = command;
3945 args[larray] = (char *)NULL;
3946
3947 if (args[0][0] == '-')
3948 args[0]++;
3949
3950#if defined (RESTRICTED_SHELL)
3951 if (restricted)
3952 change_flag ('r', FLAG_OFF);
3953#endif
3954
3955 if (subshell_argv)
3956 {
3957 /* Can't free subshell_argv[0]; that is shell_name. */
3958 for (i = 1; i < subshell_argc; i++)
3959 free (subshell_argv[i]);
3960 free (subshell_argv);
3961 }
3962
3963 dispose_command (currently_executing_command); /* XXX */
3964 currently_executing_command = (COMMAND *)NULL;
3965
3966 subshell_argc = larray;
3967 subshell_argv = args;
3968 subshell_envp = env;
3969
3970 unbind_args (); /* remove the positional parameters */
3971
3972 longjmp (subshell_top_level, 1);
Jari Aaltof73dda02001-11-13 17:56:06 +00003973 /*NOTREACHED*/
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003974}
3975
3976static int
3977execute_intern_function (name, function)
3978 WORD_DESC *name;
3979 COMMAND *function;
3980{
3981 SHELL_VAR *var;
3982
3983 if (check_identifier (name, posixly_correct) == 0)
3984 {
3985 if (posixly_correct && interactive_shell == 0)
3986 {
3987 last_command_exit_value = EX_USAGE;
Jari Aaltob80f6442004-07-27 13:29:18 +00003988 jump_to_top_level (ERREXIT);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003989 }
3990 return (EXECUTION_FAILURE);
3991 }
3992
3993 var = find_function (name->word);
Jari Aalto28ef6c32001-04-06 19:14:31 +00003994 if (var && (readonly_p (var) || noassign_p (var)))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003995 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00003996 if (readonly_p (var))
Jari Aaltob80f6442004-07-27 13:29:18 +00003997 internal_error (_("%s: readonly function"), var->name);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003998 return (EXECUTION_FAILURE);
3999 }
4000
4001 bind_function (name->word, function);
4002 return (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +00004003}
4004
Jari Aaltod166f041997-06-05 14:59:13 +00004005#if defined (INCLUDE_UNUSED)
Jari Aalto726f6381996-08-26 18:22:31 +00004006#if defined (PROCESS_SUBSTITUTION)
Jari Aalto726f6381996-08-26 18:22:31 +00004007void
4008close_all_files ()
4009{
4010 register int i, fd_table_size;
4011
4012 fd_table_size = getdtablesize ();
4013 if (fd_table_size > 256) /* clamp to a reasonable value */
Jari Aalto7117c2d2002-07-17 14:10:11 +00004014 fd_table_size = 256;
Jari Aalto726f6381996-08-26 18:22:31 +00004015
4016 for (i = 3; i < fd_table_size; i++)
4017 close (i);
4018}
4019#endif /* PROCESS_SUBSTITUTION */
Jari Aaltod166f041997-06-05 14:59:13 +00004020#endif
Jari Aalto726f6381996-08-26 18:22:31 +00004021
4022static void
4023close_pipes (in, out)
4024 int in, out;
4025{
4026 if (in >= 0)
4027 close (in);
4028 if (out >= 0)
4029 close (out);
4030}
4031
Jari Aaltob80f6442004-07-27 13:29:18 +00004032static void
4033dup_error (oldd, newd)
4034 int oldd, newd;
4035{
4036 sys_error (_("cannot duplicate fd %d to fd %d"), oldd, newd);
4037}
4038
Jari Aalto726f6381996-08-26 18:22:31 +00004039/* Redirect input and output to be from and to the specified pipes.
4040 NO_PIPE and REDIRECT_BOTH are handled correctly. */
4041static void
4042do_piping (pipe_in, pipe_out)
4043 int pipe_in, pipe_out;
4044{
4045 if (pipe_in != NO_PIPE)
4046 {
4047 if (dup2 (pipe_in, 0) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00004048 dup_error (pipe_in, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00004049 if (pipe_in > 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00004050 close (pipe_in);
Jari Aalto726f6381996-08-26 18:22:31 +00004051 }
4052 if (pipe_out != NO_PIPE)
4053 {
4054 if (pipe_out != REDIRECT_BOTH)
4055 {
4056 if (dup2 (pipe_out, 1) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00004057 dup_error (pipe_out, 1);
Jari Aalto726f6381996-08-26 18:22:31 +00004058 if (pipe_out == 0 || pipe_out > 1)
4059 close (pipe_out);
Jari Aalto726f6381996-08-26 18:22:31 +00004060 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004061 else
4062 {
Jari Aaltocce855b1998-04-17 19:52:44 +00004063 if (dup2 (1, 2) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +00004064 dup_error (1, 2);
Jari Aalto726f6381996-08-26 18:22:31 +00004065 }
Jari Aalto726f6381996-08-26 18:22:31 +00004066 }
Jari Aalto726f6381996-08-26 18:22:31 +00004067}