]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - redir.c
Bash-5.0 patch 4: the wait builtin without arguments only waits for known children...
[thirdparty/bash.git] / redir.c
diff --git a/redir.c b/redir.c
index 182891763feaaac4deea26169d294fc004478212..0c0c9f2ba965be9b2285496ee7842e9a11242e3f 100644 (file)
--- a/redir.c
+++ b/redir.c
@@ -1,6 +1,6 @@
 /* redir.c -- Functions to perform input and output redirection. */
 
-/* Copyright (C) 1997-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1997-2016 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -52,6 +52,7 @@ extern int errno;
 #include "flags.h"
 #include "execute_cmd.h"
 #include "redir.h"
+#include "trap.h"
 
 #if defined (BUFFERED_INPUT)
 #  include "input.h"
@@ -61,14 +62,13 @@ extern int errno;
 
 int expanding_redir;
 
-extern int posixly_correct;
 extern REDIRECT *redirection_undo_list;
 extern REDIRECT *exec_redirection_undo_list;
 
 /* Static functions defined and used in this file. */
-static void add_undo_close_redirect __P((int));
 static void add_exec_redirect __P((REDIRECT *));
 static int add_undo_redirect __P((int, enum r_instruction, int));
+static int add_undo_close_redirect __P((int));
 static int expandable_redirection_filename __P((REDIRECT *));
 static int stdin_redirection __P((enum r_instruction, int));
 static int undoablefd __P((int));
@@ -82,6 +82,9 @@ static int redir_special_open __P((int, char *, int, int, enum r_instruction));
 static int noclobber_open __P((char *, int, int, enum r_instruction));
 static int redir_open __P((char *, int, int, enum r_instruction));
 
+static int redir_varassign __P((REDIRECT *, int));
+static int redir_varvalue __P((REDIRECT *));
+
 /* Spare redirector used when translating [N]>&WORD[-] or [N]<&WORD[-] to
    a new redirection and when creating the redirection undo list. */
 static REDIRECTEE rd;
@@ -90,6 +93,17 @@ static REDIRECTEE rd;
    Used to print a reasonable error message. */
 static int heredoc_errno;
 
+#define REDIRECTION_ERROR(r, e, fd) \
+do { \
+  if ((r) < 0) \
+    { \
+      if (fd >= 0) \
+       close (fd); \
+      last_command_exit_value = EXECUTION_FAILURE;\
+      return ((e) == 0 ? EINVAL : (e));\
+    } \
+} while (0)
+
 void
 redirection_error (temp, error)
      REDIRECT *temp;
@@ -99,13 +113,15 @@ redirection_error (temp, error)
   int oflags;
 
   allocname = 0;
-  if (temp->redirector < 0)
+  if ((temp->rflags & REDIR_VARASSIGN) && error < 0)
+    filename = allocname = savestring (temp->redirector.filename->word);
+  else if ((temp->rflags & REDIR_VARASSIGN) == 0 && temp->redirector.dest < 0)
     /* This can happen when read_token_word encounters overflow, like in
        exec 4294967297>x */
     filename = _("file descriptor out of range");
 #ifdef EBADF
   /* This error can never involve NOCLOBBER */
-  else if (error != NOCLOBBER_REDIRECT && temp->redirector >= 0 && error == EBADF)
+  else if (error != NOCLOBBER_REDIRECT && temp->redirector.dest >= 0 && error == EBADF)
     {
       /* If we're dealing with two file descriptors, we have to guess about
          which one is invalid; in the cases of r_{duplicating,move}_input and
@@ -118,27 +134,37 @@ redirection_error (temp, error)
         case r_move_output:
          filename = allocname = itos (temp->redirectee.dest);
          break;
+       case r_duplicating_input_word:
+         if (temp->redirector.dest == 0)       /* Guess */
+           filename = temp->redirectee.filename->word; /* XXX */
+         else
+           filename = allocname = itos (temp->redirector.dest);
+         break;
+       case r_duplicating_output_word:
+         if (temp->redirector.dest == 1)       /* Guess */
+           filename = temp->redirectee.filename->word; /* XXX */
+         else
+           filename = allocname = itos (temp->redirector.dest);
+         break;
        default:
-         filename = allocname = itos (temp->redirector);
+         filename = allocname = itos (temp->redirector.dest);
          break;
         }
     }
 #endif
   else if (expandable_redirection_filename (temp))
     {
+      oflags = temp->redirectee.filename->flags;
       if (posixly_correct && interactive_shell == 0)
-       {
-         oflags = temp->redirectee.filename->flags;
-         temp->redirectee.filename->flags |= W_NOGLOB;
-       }
+       temp->redirectee.filename->flags |= W_NOGLOB;
+      temp->redirectee.filename->flags |= W_NOCOMSUB;
       filename = allocname = redirection_expand (temp->redirectee.filename);
-      if (posixly_correct && interactive_shell == 0)
-       temp->redirectee.filename->flags = oflags;
+      temp->redirectee.filename->flags = oflags;
       if (filename == 0)
        filename = temp->redirectee.filename->word;
     }
   else if (temp->redirectee.dest < 0)
-    filename = "file descriptor out of range";
+    filename = _("file descriptor out of range");
   else
     filename = allocname = itos (temp->redirectee.dest);
 
@@ -162,6 +188,10 @@ redirection_error (temp, error)
       internal_error (_("cannot create temp file for here-document: %s"), strerror (heredoc_errno));
       break;
 
+    case BADVAR_REDIRECT:
+      internal_error (_("%s: cannot assign fd to variable"), filename);
+      break;
+
     default:
       internal_error ("%s: %s", filename, strerror (error));
       break;
@@ -172,7 +202,7 @@ redirection_error (temp, error)
 
 /* Perform the redirections on LIST.  If flags & RX_ACTIVE, then actually
    make input and output file descriptors, otherwise just do whatever is
-   neccessary for side effecting.  flags & RX_UNDOABLE says to remember
+   necessary for side effecting.  flags & RX_UNDOABLE says to remember
    how to undo the redirections later, if non-zero.  If flags & RX_CLEXEC
    is non-zero, file descriptors opened in do_redirection () have their
    close-on-exec flag set. */
@@ -243,6 +273,7 @@ redirection_expand (word)
   char *result;
   WORD_LIST *tlist1, *tlist2;
   WORD_DESC *w;
+  int old;
 
   w = copy_word (word);
   if (posixly_correct)
@@ -250,11 +281,22 @@ redirection_expand (word)
 
   tlist1 = make_word_list (w, (WORD_LIST *)NULL);
   expanding_redir = 1;
+  /* Now that we've changed the variable search order to ignore the temp
+     environment, see if we need to change the cached IFS values. */
+  sv_ifs ("IFS");
   tlist2 = expand_words_no_vars (tlist1);
   expanding_redir = 0;
+  /* Now we need to change the variable search order back to include the temp
+     environment.  We force the temp environment search by forcing
+     executing_builtin to 1.  This is what makes `read' get the right values
+     for the IFS-related cached variables, for example. */
+  old = executing_builtin;
+  executing_builtin = 1;
+  sv_ifs ("IFS");
+  executing_builtin = old;
   dispose_words (tlist1);
 
-  if (!tlist2 || tlist2->next)
+  if (tlist2 == 0 || tlist2->next)
     {
       /* We expanded to no words, or to more than a single word.
         Dispose of the word list and return NULL. */
@@ -273,9 +315,23 @@ write_here_string (fd, redirectee)
      WORD_DESC *redirectee;
 {
   char *herestr;
-  int herelen, n, e;
+  int herelen, n, e, old;
+
+  expanding_redir = 1;
+  /* Now that we've changed the variable search order to ignore the temp
+     environment, see if we need to change the cached IFS values. */
+  sv_ifs ("IFS");
+  herestr = expand_string_unsplit_to_string (redirectee->word, 0);
+  expanding_redir = 0;
+  /* Now we need to change the variable search order back to include the temp
+     environment.  We force the temp environment search by forcing
+     executing_builtin to 1.  This is what makes `read' get the right values
+     for the IFS-related cached variables, for example. */
+  old = executing_builtin;
+  executing_builtin = 1;
+  sv_ifs ("IFS");
+  executing_builtin = old;
 
-  herestr = expand_string_to_string (redirectee->word, 0);
   herelen = STRLEN (herestr);
 
   n = write (fd, herestr, herelen);
@@ -304,7 +360,7 @@ write_here_document (fd, redirectee)
      WORD_DESC *redirectee;
 {
   char *document;
-  int document_len, fd2;
+  int document_len, fd2, old;
   FILE *fp;
   register WORD_LIST *t, *tlist;
 
@@ -327,7 +383,21 @@ write_here_document (fd, redirectee)
        return 0;
     }
 
+  expanding_redir = 1;
+  /* Now that we've changed the variable search order to ignore the temp
+     environment, see if we need to change the cached IFS values. */
+  sv_ifs ("IFS");
   tlist = expand_string (redirectee->word, Q_HERE_DOCUMENT);
+  expanding_redir = 0;
+  /* Now we need to change the variable search order back to include the temp
+     environment.  We force the temp environment search by forcing
+     executing_builtin to 1.  This is what makes `read' get the right values
+     for the IFS-related cached variables, for example. */
+  old = executing_builtin;
+  executing_builtin = 1;
+  sv_ifs ("IFS");
+  executing_builtin = old;
+
   if (tlist)
     {
       /* Try using buffered I/O (stdio) and writing a word
@@ -337,8 +407,11 @@ write_here_document (fd, redirectee)
         may need to be reconsidered later. */
       if ((fd2 = dup (fd)) < 0 || (fp = fdopen (fd2, "w")) == NULL)
        {
+         old = errno;
          if (fd2 >= 0)
            close (fd2);
+         dispose_words (tlist);
+         errno = old;
          return (errno);
        }
       errno = 0;
@@ -392,6 +465,9 @@ here_document_to_fd (redirectee, ri)
       return (fd);
     }
 
+  fchmod (fd, S_IRUSR | S_IWUSR);
+  SET_CLOSE_ON_EXEC (fd);
+
   errno = r = 0;               /* XXX */
   /* write_here_document returns 0 on success, errno on failure. */
   if (redirectee->word)
@@ -410,7 +486,7 @@ here_document_to_fd (redirectee, ri)
   /* In an attempt to avoid races, we close the first fd only after opening
      the second. */
   /* Make the document really temporary.  Also make it the input. */
-  fd2 = open (filename, O_RDONLY, 0600);
+  fd2 = open (filename, O_RDONLY|O_BINARY, 0600);
 
   if (fd2 < 0)
     {
@@ -426,14 +502,6 @@ here_document_to_fd (redirectee, ri)
   if (unlink (filename) < 0)
     {
       r = errno;
-#if defined (__CYGWIN__)
-      /* Under CygWin 1.1.0, the unlink will fail if the file is
-        open. This hack will allow the previous action of silently
-        ignoring the error, but will still leave the file there. This
-        needs some kind of magic. */
-      if (r == EACCES)
-       return (fd2);
-#endif /* __CYGWIN__ */
       close (fd2);
       free (filename);
       errno = r;
@@ -441,6 +509,8 @@ here_document_to_fd (redirectee, ri)
     }
 
   free (filename);
+
+  fchmod (fd2, S_IRUSR);
   return (fd2);
 }
 
@@ -511,6 +581,10 @@ redir_special_open (spec, filename, flags, mode, ri)
 #if defined (NETWORK_REDIRECTIONS)
     case RF_DEVTCP:
     case RF_DEVUDP:
+#if defined (RESTRICTED_SHELL)
+      if (restricted)
+       return (RESTRICTED_REDIRECT);
+#endif
 #if defined (HAVE_NETWORK)
       fd = netopen (filename);
 #else
@@ -585,7 +659,7 @@ redir_open (filename, flags, mode, ri)
      int flags, mode;
      enum r_instruction ri;
 {
-  int fd, r;
+  int fd, r, e;
 
   r = find_string_in_alist (filename, _redir_special_filenames, 1);
   if (r >= 0)
@@ -601,7 +675,19 @@ redir_open (filename, flags, mode, ri)
     }
   else
     {
-      fd = open (filename, flags, mode);
+      do
+       {
+         fd = open (filename, flags, mode);
+         e = errno;
+         if (fd < 0 && e == EINTR)
+           {
+             QUIT;
+             run_pending_traps ();
+           }
+         errno = e;
+       }
+      while (fd < 0 && errno == EINTR);
+
 #if defined (AFS)
       if ((fd < 0) && (errno == EACCES))
        {
@@ -628,7 +714,7 @@ undoablefd (fd)
 
 /* Do the specific redirection requested.  Returns errno or one of the
    special redirection errors (*_REDIRECT) in case of error, 0 on success.
-   If flags & RX_ACTIVE is zero, then just do whatever is neccessary to
+   If flags & RX_ACTIVE is zero, then just do whatever is necessary to
    produce the appropriate side effects.   flags & RX_UNDOABLE, if non-zero,
    says to remember how to undo each redirection.  If flags & RX_CLEXEC is
    non-zero, then we set all file descriptors > 2 that we open to be
@@ -644,10 +730,11 @@ do_redirection_internal (redirect, flags)
   char *redirectee_word;
   enum r_instruction ri;
   REDIRECT *new_redirect;
+  REDIRECTEE sd;
 
   redirectee = redirect->redirectee.filename;
   redir_fd = redirect->redirectee.dest;
-  redirector = redirect->redirector;
+  redirector = redirect->redirector.dest;
   ri = redirect->instruction;
 
   if (redirect->flags & RX_INTERNAL)
@@ -655,8 +742,9 @@ do_redirection_internal (redirect, flags)
 
   if (TRANSLATE_REDIRECT (ri))
     {
-      /* We have [N]>&WORD[-] or [N]<&WORD[-].  Expand WORD, then translate
-        the redirection into a new one and continue. */
+      /* We have [N]>&WORD[-] or [N]<&WORD[-] (or {V}>&WORD[-] or {V}<&WORD-).
+         and WORD, then translate the redirection into a new one and 
+        continue. */
       redirectee_word = redirection_expand (redirectee);
 
       /* XXX - what to do with [N]<&$w- where w is unset or null?  ksh93
@@ -665,11 +753,13 @@ do_redirection_internal (redirect, flags)
        return (AMBIGUOUS_REDIRECT);
       else if (redirectee_word[0] == '-' && redirectee_word[1] == '\0')
        {
+         sd = redirect->redirector;
          rd.dest = 0;
-         new_redirect = make_redirection (redirector, r_close_this, rd);
+         new_redirect = make_redirection (sd, r_close_this, rd, 0);
        }
       else if (all_digits (redirectee_word))
        {
+         sd = redirect->redirector;
          if (legal_number (redirectee_word, &lfd) && (int)lfd == lfd)
            rd.dest = lfd;
          else
@@ -677,23 +767,26 @@ do_redirection_internal (redirect, flags)
          switch (ri)
            {
            case r_duplicating_input_word:
-             new_redirect = make_redirection (redirector, r_duplicating_input, rd);
+             new_redirect = make_redirection (sd, r_duplicating_input, rd, 0);
              break;
            case r_duplicating_output_word:
-             new_redirect = make_redirection (redirector, r_duplicating_output, rd);
+             new_redirect = make_redirection (sd, r_duplicating_output, rd, 0);
              break;
            case r_move_input_word:
-             new_redirect = make_redirection (redirector, r_move_input, rd);
+             new_redirect = make_redirection (sd, r_move_input, rd, 0);
              break;
            case r_move_output_word:
-             new_redirect = make_redirection (redirector, r_move_output, rd);
+             new_redirect = make_redirection (sd, r_move_output, rd, 0);
              break;
+           default:
+             break;    /* shut up gcc */
            }
        }
-      else if (ri == r_duplicating_output_word && redirector == 1)
+      else if (ri == r_duplicating_output_word && (redirect->rflags & REDIR_VARASSIGN) == 0 && redirector == 1)
        {
+         sd = redirect->redirector;
          rd.filename = make_bare_word (redirectee_word);
-         new_redirect = make_redirection (1, r_err_and_out, rd);
+         new_redirect = make_redirection (sd, r_err_and_out, rd, 0);
        }
       else
        {
@@ -725,7 +818,7 @@ do_redirection_internal (redirect, flags)
        redirectee = new_redirect->redirectee.filename;
 
       redir_fd = new_redirect->redirectee.dest;
-      redirector = new_redirect->redirector;
+      redirector = new_redirect->redirector.dest;
       ri = new_redirect->instruction;
 
       /* Overwrite the flags element of the old redirect with the new value. */
@@ -766,7 +859,7 @@ do_redirection_internal (redirect, flags)
       fd = redir_open (redirectee_word, redirect->flags, 0666, ri);
       free (redirectee_word);
 
-      if (fd == NOCLOBBER_REDIRECT)
+      if (fd == NOCLOBBER_REDIRECT || fd == RESTRICTED_REDIRECT)
        return (fd);
 
       if (fd < 0)
@@ -774,17 +867,29 @@ do_redirection_internal (redirect, flags)
 
       if (flags & RX_ACTIVE)
        {
-         if (flags & RX_UNDOABLE)
+         if (redirect->rflags & REDIR_VARASSIGN)
+           {
+             redirector = fcntl (fd, F_DUPFD, SHELL_FD_BASE);          /* XXX try this for now */
+             r = errno;
+             if (redirector < 0)
+               sys_error (_("redirection error: cannot duplicate fd"));
+             REDIRECTION_ERROR (redirector, r, fd);
+           }
+
+         if ((flags & RX_UNDOABLE) && (redirect->rflags & REDIR_VARASSIGN) == 0)
            {
              /* Only setup to undo it if the thing to undo is active. */
              if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1))
-               add_undo_redirect (redirector, ri, -1);
+               r = add_undo_redirect (redirector, ri, -1);
              else
-               add_undo_close_redirect (redirector);
+               r = add_undo_close_redirect (redirector);
+             REDIRECTION_ERROR (r, errno, fd);
            }
 
 #if defined (BUFFERED_INPUT)
-         check_bash_input (redirector);
+         /* inhibit call to sync_buffered_stream() for async processes */
+         if (redirector != 0 || (subshell_environment & SUBSHELL_ASYNC) == 0)
+           check_bash_input (redirector);
 #endif
 
          /* Make sure there is no pending output before we change the state
@@ -801,8 +906,20 @@ do_redirection_internal (redirect, flags)
              fpurge (stderr);
            }
 
-         if ((fd != redirector) && (dup2 (fd, redirector) < 0))
-           return (errno);
+         if (redirect->rflags & REDIR_VARASSIGN)
+           {
+             if ((r = redir_varassign (redirect, redirector)) < 0)
+               {
+                 close (redirector);
+                 close (fd);
+                 return (r);   /* XXX */
+               }
+           }
+         else if ((fd != redirector) && (dup2 (fd, redirector) < 0))
+           {
+             close (fd);       /* dup2 failed? must be fd limit issue */
+             return (errno);
+           }
 
 #if defined (BUFFERED_INPUT)
          /* Do not change the buffered stream for an implicit redirection
@@ -837,7 +954,7 @@ do_redirection_internal (redirect, flags)
        }
 
       /* If we are hacking both stdout and stderr, do the stderr
-        redirection here. */
+        redirection here.  XXX - handle {var} here? */
       if (ri == r_err_and_out || ri == r_append_err_and_out)
        {
          if (flags & RX_ACTIVE)
@@ -865,21 +982,40 @@ do_redirection_internal (redirect, flags)
              return (HEREDOC_REDIRECT);
            }
 
+         if (redirect->rflags & REDIR_VARASSIGN)
+           {
+             redirector = fcntl (fd, F_DUPFD, SHELL_FD_BASE);          /* XXX try this for now */
+             r = errno;
+             if (redirector < 0)
+               sys_error (_("redirection error: cannot duplicate fd"));
+             REDIRECTION_ERROR (redirector, r, fd);
+           }
+
          if (flags & RX_ACTIVE)
            {
-             if (flags & RX_UNDOABLE)
+             if ((flags & RX_UNDOABLE) && (redirect->rflags & REDIR_VARASSIGN) == 0)
                {
                  /* Only setup to undo it if the thing to undo is active. */
                  if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1))
-                   add_undo_redirect (redirector, ri, -1);
+                   r = add_undo_redirect (redirector, ri, -1);
                  else
-                   add_undo_close_redirect (redirector);
+                   r = add_undo_close_redirect (redirector);
+                 REDIRECTION_ERROR (r, errno, fd);
                }
 
 #if defined (BUFFERED_INPUT)
              check_bash_input (redirector);
 #endif
-             if (fd != redirector && dup2 (fd, redirector) < 0)
+             if (redirect->rflags & REDIR_VARASSIGN)
+               {
+                 if ((r = redir_varassign (redirect, redirector)) < 0)
+                   {
+                     close (redirector);
+                     close (fd);
+                     return (r);       /* XXX */
+                   }
+               }
+             else if (fd != redirector && dup2 (fd, redirector) < 0)
                {
                  r = errno;
                  close (fd);
@@ -907,21 +1043,51 @@ do_redirection_internal (redirect, flags)
     case r_duplicating_output:
     case r_move_input:
     case r_move_output:
+      if ((flags & RX_ACTIVE) && (redirect->rflags & REDIR_VARASSIGN))
+        {
+         redirector = fcntl (redir_fd, F_DUPFD, SHELL_FD_BASE);                /* XXX try this for now */
+         r = errno;
+         if (redirector < 0)
+           sys_error (_("redirection error: cannot duplicate fd"));
+         REDIRECTION_ERROR (redirector, r, -1);
+        }
+
       if ((flags & RX_ACTIVE) && (redir_fd != redirector))
        {
-         if (flags & RX_UNDOABLE)
+         if ((flags & RX_UNDOABLE) && (redirect->rflags & REDIR_VARASSIGN) == 0)
            {
              /* Only setup to undo it if the thing to undo is active. */
              if (fcntl (redirector, F_GETFD, 0) != -1)
-               add_undo_redirect (redirector, ri, redir_fd);
+               r = add_undo_redirect (redirector, ri, redir_fd);
              else
-               add_undo_close_redirect (redirector);
+               r = add_undo_close_redirect (redirector);
+             REDIRECTION_ERROR (r, errno, -1);
+           }
+         if ((flags & RX_UNDOABLE) && (ri == r_move_input || ri == r_move_output))
+           {
+             /* r_move_input and r_move_output add an additional close()
+                that needs to be undone */
+             if (fcntl (redirector, F_GETFD, 0) != -1)
+               {
+                 r = add_undo_redirect (redir_fd, r_close_this, -1);
+                 REDIRECTION_ERROR (r, errno, -1);
+               }
            }
 #if defined (BUFFERED_INPUT)
-         check_bash_input (redirector);
+         /* inhibit call to sync_buffered_stream() for async processes */
+         if (redirector != 0 || (subshell_environment & SUBSHELL_ASYNC) == 0)
+           check_bash_input (redirector);
 #endif
+         if (redirect->rflags & REDIR_VARASSIGN)
+           {
+             if ((r = redir_varassign (redirect, redirector)) < 0)
+               {
+                 close (redirector);
+                 return (r);   /* XXX */
+               }
+           }
          /* This is correct.  2>&1 means dup2 (1, 2); */
-         if (dup2 (redir_fd, redirector) < 0)
+         else if (dup2 (redir_fd, redirector) < 0)
            return (errno);
 
 #if defined (BUFFERED_INPUT)
@@ -953,12 +1119,14 @@ do_redirection_internal (redirect, flags)
             file descriptors >= SHELL_FD_BASE, we set the saving fd to be
             close-on-exec and use a flag to decide how to set close-on-exec
             when the fd is restored. */
-         if ((redirect->flags & RX_INTERNAL) && (redirect->flags & RX_SAVCLEXEC) && redirector >= 3 && redir_fd >= SHELL_FD_BASE)
+         if ((redirect->flags & RX_INTERNAL) && (redirect->flags & RX_SAVCLEXEC) && redirector >= 3 && (redir_fd >= SHELL_FD_BASE || (redirect->flags & RX_SAVEFD)))
            SET_OPEN_ON_EXEC (redirector);
            
          /* dup-and-close redirection */
          if (ri == r_move_input || ri == r_move_output)
            {
+             xtrace_fdchk (redir_fd);
+
              close (redir_fd);
 #if defined (COPROCESS_SUPPORT)
              coproc_fdchk (redir_fd);  /* XXX - loses coproc fds */
@@ -970,24 +1138,47 @@ do_redirection_internal (redirect, flags)
     case r_close_this:
       if (flags & RX_ACTIVE)
        {
-         if ((flags & RX_UNDOABLE) && (fcntl (redirector, F_GETFD, 0) != -1))
-           add_undo_redirect (redirector, ri, -1);
+         if (redirect->rflags & REDIR_VARASSIGN)
+           {
+             redirector = redir_varvalue (redirect);
+             if (redirector < 0)
+               return AMBIGUOUS_REDIRECT;
+           }
+
+         r = 0;
+         /* XXX - only if REDIR_VARASSIGN not set? */
+         if (flags & RX_UNDOABLE)
+           {
+             if (fcntl (redirector, F_GETFD, 0) != -1)
+               r = add_undo_redirect (redirector, ri, -1);
+             else
+               r = add_undo_close_redirect (redirector);
+             REDIRECTION_ERROR (r, errno, redirector);
+           }
 
 #if defined (COPROCESS_SUPPORT)
          coproc_fdchk (redirector);
 #endif
+         xtrace_fdchk (redirector);
 
 #if defined (BUFFERED_INPUT)
-         check_bash_input (redirector);
-         close_buffered_fd (redirector);
+         /* inhibit call to sync_buffered_stream() for async processes */
+         if (redirector != 0 || (subshell_environment & SUBSHELL_ASYNC) == 0)
+           check_bash_input (redirector);
+         r = close_buffered_fd (redirector);
 #else /* !BUFFERED_INPUT */
-         close (redirector);
+         r = close (redirector);
 #endif /* !BUFFERED_INPUT */
+
+         if (r < 0 && (flags & RX_INTERNAL) && (errno == EIO || errno == ENOSPC))
+           REDIRECTION_ERROR (r, errno, -1);
        }
       break;
 
     case r_duplicating_input_word:
     case r_duplicating_output_word:
+    case r_move_input_word:
+    case r_move_output_word:
       break;
     }
   return (0);
@@ -1004,19 +1195,26 @@ do_redirection_internal (redirect, flags)
    since we're going to use it later (e.g., make sure we don't save fd 0
    to fd 10 if we have a redirection like 0<&10).  If the value of fdbase
    puts the process over its fd limit, causing fcntl to fail, we try
-   again with SHELL_FD_BASE. */
+   again with SHELL_FD_BASE.  Return 0 on success, -1 on error. */
 static int
 add_undo_redirect (fd, ri, fdbase)
      int fd;
      enum r_instruction ri;
      int fdbase;
 {
-  int new_fd, clexec_flag;
+  int new_fd, clexec_flag, savefd_flag;
   REDIRECT *new_redirect, *closer, *dummy_redirect;
+  REDIRECTEE sd;
 
+  savefd_flag = 0;
   new_fd = fcntl (fd, F_DUPFD, (fdbase < SHELL_FD_BASE) ? SHELL_FD_BASE : fdbase+1);
   if (new_fd < 0)
     new_fd = fcntl (fd, F_DUPFD, SHELL_FD_BASE);
+  if (new_fd < 0)
+    {
+      new_fd = fcntl (fd, F_DUPFD, 0);
+      savefd_flag = 1;
+    }
 
   if (new_fd < 0)
     {
@@ -1026,18 +1224,22 @@ add_undo_redirect (fd, ri, fdbase)
 
   clexec_flag = fcntl (fd, F_GETFD, 0);
 
+  sd.dest = new_fd;
   rd.dest = 0;
-  closer = make_redirection (new_fd, r_close_this, rd);
+  closer = make_redirection (sd, r_close_this, rd, 0);
   closer->flags |= RX_INTERNAL;
   dummy_redirect = copy_redirects (closer);
 
+  sd.dest = fd;
   rd.dest = new_fd;
   if (fd == 0)
-    new_redirect = make_redirection (fd, r_duplicating_input, rd);
+    new_redirect = make_redirection (sd, r_duplicating_input, rd, 0);
   else
-    new_redirect = make_redirection (fd, r_duplicating_output, rd);
+    new_redirect = make_redirection (sd, r_duplicating_output, rd, 0);
   new_redirect->flags |= RX_INTERNAL;
-  if (clexec_flag == 0 && fd >= 3 && new_fd >= SHELL_FD_BASE)
+  if (savefd_flag)
+    new_redirect->flags |= RX_SAVEFD;
+  if (clexec_flag == 0 && fd >= 3 && (new_fd >= SHELL_FD_BASE || savefd_flag))
     new_redirect->flags |= RX_SAVCLEXEC;
   new_redirect->next = closer;
 
@@ -1052,14 +1254,15 @@ add_undo_redirect (fd, ri, fdbase)
      above SHELL_FD_BASE, add a redirection to be undone if the exec builtin
      causes redirections to be discarded.  There needs to be a difference
      between fds that are used to save other fds and then are the target of
-     user redirctions and fds that are just the target of user redirections.
+     user redirections and fds that are just the target of user redirections.
      We use the close-on-exec flag to tell the difference; fds > SHELL_FD_BASE
      that have the close-on-exec flag set are assumed to be fds used internally
      to save others. */
   if (fd >= SHELL_FD_BASE && ri != r_close_this && clexec_flag)
     {
+      sd.dest = fd;
       rd.dest = new_fd;
-      new_redirect = make_redirection (fd, r_duplicating_output, rd);
+      new_redirect = make_redirection (sd, r_duplicating_output, rd, 0);
       new_redirect->flags |= RX_INTERNAL;
 
       add_exec_redirect (new_redirect);
@@ -1082,18 +1285,22 @@ add_undo_redirect (fd, ri, fdbase)
 }
 
 /* Set up to close FD when we are finished with the current command
-   and its redirections. */
-static void
+   and its redirections.  Return 0 on success, -1 on error. */
+static int
 add_undo_close_redirect (fd)
      int fd;
 {
   REDIRECT *closer;
+  REDIRECTEE sd;
 
+  sd.dest = fd;
   rd.dest = 0;
-  closer = make_redirection (fd, r_close_this, rd);
+  closer = make_redirection (sd, r_close_this, rd, 0);
   closer->flags |= RX_INTERNAL;
   closer->next = redirection_undo_list;
   redirection_undo_list = closer;
+
+  return 0;
 }
 
 static void
@@ -1131,6 +1338,10 @@ stdin_redirection (ri, redirector)
     case r_append_err_and_out:
     case r_output_force:
     case r_duplicating_output_word:
+    case r_move_input:
+    case r_move_output:
+    case r_move_input_word:
+    case r_move_output_word:
       return (0);
     }
   return (0);
@@ -1146,6 +1357,86 @@ stdin_redirects (redirs)
   int n;
 
   for (n = 0, rp = redirs; rp; rp = rp->next)
-    n += stdin_redirection (rp->instruction, rp->redirector);
+    if ((rp->rflags & REDIR_VARASSIGN) == 0)
+      n += stdin_redirection (rp->instruction, rp->redirector.dest);
   return n;
 }
+/* bind_var_to_int handles array references */
+static int
+redir_varassign (redir, fd)
+     REDIRECT *redir;
+     int fd;
+{
+  WORD_DESC *w;
+  SHELL_VAR *v;
+
+  w = redir->redirector.filename;
+  v = bind_var_to_int (w->word, fd);
+  if (v == 0 || readonly_p (v) || noassign_p (v))
+    return BADVAR_REDIRECT;
+
+  stupidly_hack_special_variables (w->word);
+  return 0;
+}
+
+/* Handles {array[ind]} for redirection words */
+static int
+redir_varvalue (redir)
+     REDIRECT *redir;
+{
+  SHELL_VAR *v;
+  char *val, *w;
+  intmax_t vmax;
+  int i;
+#if defined (ARRAY_VARS)
+  char *sub;
+  int len, vr;
+#endif
+
+  w = redir->redirector.filename->word;                /* shorthand */
+  /* XXX - handle set -u here? */
+#if defined (ARRAY_VARS)
+  if (vr = valid_array_reference (w, 0))
+    {
+      v = array_variable_part (w, 0, &sub, &len);
+    }
+  else
+#endif
+    {
+      v = find_variable (w);
+#if defined (ARRAY_VARS)
+      if (v == 0)
+       {
+         v = find_variable_last_nameref (w, 0);
+         if (v && nameref_p (v))
+           {
+             w = nameref_cell (v);
+             if (vr = valid_array_reference (w, 0))
+               v = array_variable_part (w, 0, &sub, &len);
+             else
+               v = find_variable (w);
+           }
+       }
+#endif
+    }
+       
+  if (v == 0 || invisible_p (v))
+    return -1;
+
+#if defined (ARRAY_VARS)
+  /* get_variable_value handles references to array variables without
+     subscripts */
+  if (vr && (array_p (v) || assoc_p (v)))
+    val = get_array_value (w, 0, (int *)NULL, (arrayind_t *)0);
+  else
+#endif
+  val = get_variable_value (v);
+  if (val == 0 || *val == 0)
+    return -1;
+
+  if (legal_number (val, &vmax) < 0)
+    return -1;
+
+  i = vmax;    /* integer truncation */
+  return i;
+}