LCOV - code coverage report
Current view: top level - bash-4.4.23 - execute_cmd.c (source / functions) Hit Total Coverage
Test: cov-sh.info Lines: 945 2086 45.3 %
Date: 2020-10-29 14:49:55 Functions: 39 73 53.4 %

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

Generated by: LCOV version 1.14.0.6.4058