]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - print_cmd.c
Bash-4.3 patch 32
[thirdparty/bash.git] / print_cmd.c
index 50b26a3fae1666cdc264c0c225eeb09b8ad39b8f..ea63dca46d942d9cdef1e64b8fbe25f4c0cb1c8f 100644 (file)
@@ -1,6 +1,6 @@
 /* print_command -- A way to make readable commands from a command tree. */
 
-/* Copyright (C) 1989-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2011 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -108,6 +108,20 @@ char *the_printed_command = (char *)NULL;
 int the_printed_command_size = 0;
 int command_string_index = 0;
 
+int xtrace_fd = -1;
+FILE *xtrace_fp = 0;
+
+#define CHECK_XTRACE_FP        xtrace_fp = (xtrace_fp ? xtrace_fp : stderr)
+
+/* shell expansion characters: used in print_redirection_list */
+#define EXPCHAR(c) ((c) == '{' || (c) == '~' || (c) == '$' || (c) == '`')
+
+#define PRINT_DEFERRED_HEREDOCS(x) \
+  do { \
+    if (deferred_heredocs) \
+      print_deferred_heredocs (x); \
+  } while (0)
+
 /* Non-zero means the stuff being printed is inside of a function def. */
 static int inside_function_def;
 static int skip_this_indent;
@@ -120,7 +134,8 @@ static REDIRECT *deferred_heredocs;
 static int group_command_nesting;
 
 /* A buffer to indicate the indirection level (PS4) when set -x is enabled. */
-static char indirection_string[100];
+static char *indirection_string = 0;
+static int indirection_stringsiz = 0;
 
 /* Print COMMAND (a command tree) on standard output. */
 void
@@ -261,17 +276,15 @@ make_command_string_internal (command)
              break;
 
            case ';':
-#if 0
-             if (was_heredoc == 0)
-               cprintf (";");
-             else
-               was_heredoc = 0;
-#else
              if (deferred_heredocs == 0)
-               cprintf (";");
+               {
+                 if (was_heredoc == 0)
+                   cprintf (";");
+                 else
+                   was_heredoc = 0;
+               }
              else
-               print_deferred_heredocs (";");
-#endif
+               print_deferred_heredocs (inside_function_def ? "" : ";");
 
              if (inside_function_def)
                cprintf ("\n");
@@ -290,6 +303,7 @@ make_command_string_internal (command)
            }
 
          make_command_string_internal (command->value.Connection->second);
+         PRINT_DEFERRED_HEREDOCS ("");
          printing_connection--;                  
          break;
 
@@ -305,6 +319,7 @@ make_command_string_internal (command)
          cprintf ("( ");
          skip_this_indent++;
          make_command_string_internal (command->value.Subshell->command);
+         PRINT_DEFERRED_HEREDOCS ("");
          cprintf (" )");
          break;
 
@@ -348,6 +363,57 @@ print_word_list (list, separator)
   _print_word_list (list, separator, xprintf);
 }
 
+void
+xtrace_set (fd, fp)
+     int fd;
+     FILE *fp;
+{
+  if (fd >= 0 && sh_validfd (fd) == 0)
+    {
+      internal_error (_("xtrace_set: %d: invalid file descriptor"), fd);
+      return;
+    }
+  if (fp == 0)
+    {
+      internal_error (_("xtrace_set: NULL file pointer"));
+      return;
+    }
+  if (fd >= 0 && fileno (fp) != fd)
+    internal_warning (_("xtrace fd (%d) != fileno xtrace fp (%d)"), fd, fileno (fp));
+  
+  xtrace_fd = fd;
+  xtrace_fp = fp;
+}
+
+void
+xtrace_init ()
+{
+  xtrace_set (-1, stderr);
+}
+
+void
+xtrace_reset ()
+{
+  if (xtrace_fd >= 0 && xtrace_fp)
+    {
+      fflush (xtrace_fp);
+      fclose (xtrace_fp);
+    }
+  else if (xtrace_fd >= 0)
+    close (xtrace_fd);
+
+  xtrace_fd = -1;
+  xtrace_fp = stderr;
+}
+
+void
+xtrace_fdchk (fd)
+     int fd;
+{
+  if (fd == xtrace_fd)
+    xtrace_reset ();
+}
+
 /* Return a string denoting what our indirection level is. */
 
 char *
@@ -356,17 +422,20 @@ indirection_level_string ()
   register int i, j;
   char *ps4;
   char ps4_firstc[MB_LEN_MAX+1];
-  int ps4_firstc_len, ps4_len;
+  int ps4_firstc_len, ps4_len, ineed, old;
 
-  indirection_string[0] = '\0';
   ps4 = get_string_value ("PS4");
+  if (indirection_string == 0)
+    indirection_string = xmalloc (indirection_stringsiz = 100);
+  indirection_string[0] = '\0';
 
   if (ps4 == 0 || *ps4 == '\0')
     return (indirection_string);
 
-  change_flag ('x', FLAG_OFF);
+  old = change_flag ('x', FLAG_OFF);
   ps4 = decode_prompt_string (ps4);
-  change_flag ('x', FLAG_ON);
+  if (old)
+    change_flag ('x', FLAG_ON);
 
   if (ps4 == 0 || *ps4 == '\0')
     return (indirection_string);
@@ -374,7 +443,7 @@ indirection_level_string ()
 #if defined (HANDLE_MULTIBYTE)
   ps4_len = strnlen (ps4, MB_CUR_MAX);
   ps4_firstc_len = MBLEN (ps4, ps4_len);
-  if (ps4_firstc_len == 1 || ps4_firstc_len == 0 || MB_INVALIDCH (ps4_firstc_len))
+  if (ps4_firstc_len == 1 || ps4_firstc_len == 0 || ps4_firstc_len < 0)
     {
       ps4_firstc[0] = ps4[0];
       ps4_firstc[ps4_firstc_len = 1] = '\0';
@@ -385,8 +454,17 @@ indirection_level_string ()
   ps4_firstc[0] = ps4[0];
   ps4_firstc[ps4_firstc_len = 1] = '\0';
 #endif
-      
-  for (i = j = 0; ps4_firstc[0] && j < indirection_level && i < 99; i += ps4_firstc_len, j++)
+
+  /* Dynamically resize indirection_string so we have room for everything
+     and we don't have to truncate ps4 */
+  ineed = (ps4_firstc_len * indirection_level) + strlen (ps4);
+  if (ineed > indirection_stringsiz - 1)
+    {
+      indirection_stringsiz = ineed + 1;
+      indirection_string = xrealloc (indirection_string, indirection_stringsiz);
+    }
+
+  for (i = j = 0; ps4_firstc[0] && j < indirection_level && i < indirection_stringsiz - 1; i += ps4_firstc_len, j++)
     {
       if (ps4_firstc_len == 1)
        indirection_string[i] = ps4_firstc[0];
@@ -394,7 +472,7 @@ indirection_level_string ()
        memcpy (indirection_string+i, ps4_firstc, ps4_firstc_len);
     }      
 
-  for (j = ps4_firstc_len; *ps4 && ps4[j] && i < 99; i++, j++)
+  for (j = ps4_firstc_len; *ps4 && ps4[j] && i < indirection_stringsiz - 1; i++, j++)
     indirection_string[i] = ps4[j];
 
   indirection_string[i] = '\0';
@@ -409,8 +487,10 @@ xtrace_print_assignment (name, value, assign_list, xflags)
 {
   char *nval;
 
+  CHECK_XTRACE_FP;
+
   if (xflags)
-    fprintf (stderr, "%s", indirection_level_string ());
+    fprintf (xtrace_fp, "%s", indirection_level_string ());
 
   /* VALUE should not be NULL when this is called. */
   if (*value == '\0' || assign_list)
@@ -423,14 +503,14 @@ xtrace_print_assignment (name, value, assign_list, xflags)
     nval = value;
 
   if (assign_list)
-    fprintf (stderr, "%s=(%s)\n", name, nval);
+    fprintf (xtrace_fp, "%s=(%s)\n", name, nval);
   else
-    fprintf (stderr, "%s=%s\n", name, nval);
+    fprintf (xtrace_fp, "%s=%s\n", name, nval);
 
   if (nval != value)
     FREE (nval);
 
-  fflush (stderr);
+  fflush (xtrace_fp);
 }
 
 /* A function to print the words of a simple command when set -x is on. */
@@ -442,30 +522,33 @@ xtrace_print_word_list (list, xtflags)
   WORD_LIST *w;
   char *t, *x;
 
+  CHECK_XTRACE_FP;
+
   if (xtflags)
-    fprintf (stderr, "%s", indirection_level_string ());
+    fprintf (xtrace_fp, "%s", indirection_level_string ());
 
   for (w = list; w; w = w->next)
     {
       t = w->word->word;
       if (t == 0 || *t == '\0')
-       fprintf (stderr, "''%s", w->next ? " " : "");
+       fprintf (xtrace_fp, "''%s", w->next ? " " : "");
       else if (sh_contains_shell_metas (t))
        {
          x = sh_single_quote (t);
-         fprintf (stderr, "%s%s", x, w->next ? " " : "");
+         fprintf (xtrace_fp, "%s%s", x, w->next ? " " : "");
          free (x);
        }
       else if (ansic_shouldquote (t))
        {
          x = ansic_quote (t, 0, (int *)0);
-         fprintf (stderr, "%s%s", x, w->next ? " " : "");
+         fprintf (xtrace_fp, "%s%s", x, w->next ? " " : "");
          free (x);
        }
       else
-       fprintf (stderr, "%s%s", t, w->next ? " " : "");
+       fprintf (xtrace_fp, "%s%s", t, w->next ? " " : "");
     }
-  fprintf (stderr, "\n");
+  fprintf (xtrace_fp, "\n");
+  fflush (xtrace_fp);
 }
 
 static void
@@ -488,8 +571,9 @@ void
 xtrace_print_for_command_head (for_command)
      FOR_COM *for_command;
 {
-  fprintf (stderr, "%s", indirection_level_string ());
-  fprintf (stderr, "for %s in ", for_command->name->word);
+  CHECK_XTRACE_FP;
+  fprintf (xtrace_fp, "%s", indirection_level_string ());
+  fprintf (xtrace_fp, "for %s in ", for_command->name->word);
   xtrace_print_word_list (for_command->map_list, 0);
 }
 
@@ -498,13 +582,15 @@ print_for_command (for_command)
      FOR_COM *for_command;
 {
   print_for_command_head (for_command);
-
   cprintf (";");
   newline ("do\n");
+
   indentation += indentation_amount;
   make_command_string_internal (for_command->action);
+  PRINT_DEFERRED_HEREDOCS ("");
   semicolon ();
   indentation -= indentation_amount;
+
   newline ("done");
 }
 
@@ -523,6 +609,7 @@ print_arith_for_command (arith_for_command)
   newline ("do\n");
   indentation += indentation_amount;
   make_command_string_internal (arith_for_command->action);
+  PRINT_DEFERRED_HEREDOCS ("");
   semicolon ();
   indentation -= indentation_amount;
   newline ("done");
@@ -542,8 +629,9 @@ void
 xtrace_print_select_command_head (select_command)
      SELECT_COM *select_command;
 {
-  fprintf (stderr, "%s", indirection_level_string ());
-  fprintf (stderr, "select %s in ", select_command->name->word);
+  CHECK_XTRACE_FP;
+  fprintf (xtrace_fp, "%s", indirection_level_string ());
+  fprintf (xtrace_fp, "select %s in ", select_command->name->word);
   xtrace_print_word_list (select_command->map_list, 0);
 }
 
@@ -557,6 +645,7 @@ print_select_command (select_command)
   newline ("do\n");
   indentation += indentation_amount;
   make_command_string_internal (select_command->action);
+  PRINT_DEFERRED_HEREDOCS ("");
   semicolon ();
   indentation -= indentation_amount;
   newline ("done");
@@ -582,6 +671,7 @@ print_group_command (group_command)
     }
 
   make_command_string_internal (group_command->command);
+  PRINT_DEFERRED_HEREDOCS ("");
 
   if (inside_function_def)
     {
@@ -611,8 +701,9 @@ void
 xtrace_print_case_command_head (case_command)
      CASE_COM *case_command;
 {
-  fprintf (stderr, "%s", indirection_level_string ());
-  fprintf (stderr, "case %s in\n", case_command->word->word);
+  CHECK_XTRACE_FP;
+  fprintf (xtrace_fp, "%s", indirection_level_string ());
+  fprintf (xtrace_fp, "case %s in\n", case_command->word->word);
 }
 
 static void
@@ -639,6 +730,7 @@ print_case_clauses (clauses)
       indentation += indentation_amount;
       make_command_string_internal (clauses->action);
       indentation -= indentation_amount;
+      PRINT_DEFERRED_HEREDOCS ("");
       if (clauses->flags & CASEPAT_FALLTHROUGH)
        newline (";&");
       else if (clauses->flags & CASEPAT_TESTNEXT)
@@ -672,10 +764,12 @@ print_until_or_while (while_command, which)
   cprintf ("%s ", which);
   skip_this_indent++;
   make_command_string_internal (while_command->test);
+  PRINT_DEFERRED_HEREDOCS ("");
   semicolon ();
   cprintf (" do\n");   /* was newline ("do\n"); */
   indentation += indentation_amount;
   make_command_string_internal (while_command->action);
+  PRINT_DEFERRED_HEREDOCS ("");
   indentation -= indentation_amount;
   semicolon ();
   newline ("done");
@@ -692,6 +786,7 @@ print_if_command (if_command)
   cprintf (" then\n");
   indentation += indentation_amount;
   make_command_string_internal (if_command->true_case);
+  PRINT_DEFERRED_HEREDOCS ("");
   indentation -= indentation_amount;
 
   if (if_command->false_case)
@@ -700,6 +795,7 @@ print_if_command (if_command)
       newline ("else\n");
       indentation += indentation_amount;
       make_command_string_internal (if_command->false_case);
+      PRINT_DEFERRED_HEREDOCS ("");
       indentation -= indentation_amount;
     }
   semicolon ();
@@ -773,6 +869,21 @@ print_cond_command (cond)
 }
 
 #ifdef DEBUG
+void
+debug_print_word_list (s, list, sep)
+     char *s;
+     WORD_LIST *list;
+     char *sep;
+{
+  WORD_LIST *w;
+
+  if (s)
+    fprintf (stderr, "%s: ", s);
+  for (w = list; w; w = w->next)
+    fprintf (stderr, "%s%s", w->word->word, w->next ? sep : "");
+  fprintf (stderr, "\n");
+}
+
 void
 debug_print_cond_command (cond)
      COND_COM *cond;
@@ -790,25 +901,28 @@ xtrace_print_cond_term (type, invert, op, arg1, arg2)
      WORD_DESC *op;
      char *arg1, *arg2;
 {
+  CHECK_XTRACE_FP;
   command_string_index = 0;
-  fprintf (stderr, "%s", indirection_level_string ());
-  fprintf (stderr, "[[ ");
+  fprintf (xtrace_fp, "%s", indirection_level_string ());
+  fprintf (xtrace_fp, "[[ ");
   if (invert)
-    fprintf (stderr, "! ");
+    fprintf (xtrace_fp, "! ");
 
   if (type == COND_UNARY)
     {
-      fprintf (stderr, "%s ", op->word);
-      fprintf (stderr, "%s", (arg1 && *arg1) ? arg1 : "''");
+      fprintf (xtrace_fp, "%s ", op->word);
+      fprintf (xtrace_fp, "%s", (arg1 && *arg1) ? arg1 : "''");
     }
   else if (type == COND_BINARY)
     {
-      fprintf (stderr, "%s", (arg1 && *arg1) ? arg1 : "''");
-      fprintf (stderr, " %s ", op->word);
-      fprintf (stderr, "%s", (arg2 && *arg2) ? arg2 : "''");
+      fprintf (xtrace_fp, "%s", (arg1 && *arg1) ? arg1 : "''");
+      fprintf (xtrace_fp, " %s ", op->word);
+      fprintf (xtrace_fp, "%s", (arg2 && *arg2) ? arg2 : "''");
     }
 
-  fprintf (stderr, " ]]\n");
+  fprintf (xtrace_fp, " ]]\n");
+
+  fflush (xtrace_fp);
 }        
 #endif /* COND_COMMAND */
 
@@ -820,11 +934,14 @@ xtrace_print_arith_cmd (list)
 {
   WORD_LIST *w;
 
-  fprintf (stderr, "%s", indirection_level_string ());
-  fprintf (stderr, "(( ");
+  CHECK_XTRACE_FP;
+  fprintf (xtrace_fp, "%s", indirection_level_string ());
+  fprintf (xtrace_fp, "(( ");
   for (w = list; w; w = w->next)
-    fprintf (stderr, "%s%s", w->word->word, w->next ? " " : "");
-  fprintf (stderr, " ))\n");
+    fprintf (xtrace_fp, "%s%s", w->word->word, w->next ? " " : "");
+  fprintf (xtrace_fp, " ))\n");
+
+  fflush (xtrace_fp);
 }
 #endif
 
@@ -873,7 +990,7 @@ print_deferred_heredocs (cstring)
       cprintf (" ");
       print_heredoc_header (hdtail);
     }
-  if (cstring[0] != ';' || cstring[1])
+  if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
     cprintf ("%s", cstring); 
   if (deferred_heredocs)
     cprintf ("\n");
@@ -884,9 +1001,10 @@ print_deferred_heredocs (cstring)
     }
   if (deferred_heredocs)
     {
-      if (cstring[0] != ';' || cstring[1])
+      if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
        cprintf (" ");  /* make sure there's at least one space */
       dispose_redirects (deferred_heredocs);
+      was_heredoc = 1;
     }
   deferred_heredocs = (REDIRECT *)NULL;
 }
@@ -896,6 +1014,7 @@ print_redirection_list (redirects)
      REDIRECT *redirects;
 {
   REDIRECT *heredocs, *hdtail, *newredir;
+  char *rw;
 
   heredocs = (REDIRECT *)NULL;
   hdtail = heredocs;
@@ -917,10 +1036,12 @@ print_redirection_list (redirects)
          else
            hdtail = heredocs = newredir;
        }
-      else if (redirects->instruction == r_duplicating_output_word && redirects->redirector == 1)
+      else if (redirects->instruction == r_duplicating_output_word && (redirects->flags & REDIR_VARASSIGN) == 0 && redirects->redirector.dest == 1)
        {
          /* Temporarily translate it as the execution code does. */
-         redirects->instruction = r_err_and_out;
+         rw = redirects->redirectee.filename->word;
+         if (rw && *rw != '-' && DIGIT (*rw) == 0 && EXPCHAR (*rw) == 0)
+           redirects->instruction = r_err_and_out;
          print_redirection (redirects);
          redirects->instruction = r_duplicating_output_word;
        }
@@ -953,8 +1074,10 @@ print_heredoc_header (redirect)
   kill_leading = redirect->instruction == r_deblank_reading_until;
 
   /* Here doc header */
-  if (redirect->redirector != 0)
-    cprintf ("%d", redirect->redirector);
+  if (redirect->rflags & REDIR_VARASSIGN)
+    cprintf ("{%s}", redirect->redirector.filename->word);
+  else if (redirect->redirector.dest != 0)
+    cprintf ("%d", redirect->redirector.dest);
 
   /* If the here document delimiter is quoted, single-quote it. */
   if (redirect->redirectee.filename->flags & W_QUOTED)
@@ -979,38 +1102,61 @@ static void
 print_redirection (redirect)
      REDIRECT *redirect;
 {
-  int kill_leading, redirector, redir_fd;
-  WORD_DESC *redirectee;
+  int redirector, redir_fd;
+  WORD_DESC *redirectee, *redir_word;
 
-  kill_leading = 0;
   redirectee = redirect->redirectee.filename;
-  redirector = redirect->redirector;
   redir_fd = redirect->redirectee.dest;
 
+  redir_word = redirect->redirector.filename;
+  redirector = redirect->redirector.dest;
+
   switch (redirect->instruction)
     {
-    case r_output_direction:
-      if (redirector != 1)
+    case r_input_direction:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}", redir_word->word);
+      else if (redirector != 0)
        cprintf ("%d", redirector);
-      cprintf ("> %s", redirectee->word);
+      cprintf ("< %s", redirectee->word);
       break;
 
-    case r_input_direction:
-      if (redirector != 0)
+    case r_output_direction:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}", redir_word->word);
+      else if (redirector != 1)
        cprintf ("%d", redirector);
-      cprintf ("< %s", redirectee->word);
+      cprintf ("> %s", redirectee->word);
       break;
 
     case r_inputa_direction:   /* Redirection created by the shell. */
       cprintf ("&");
       break;
 
+    case r_output_force:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}", redir_word->word);
+      else if (redirector != 1)
+       cprintf ("%d", redirector);
+      cprintf (">| %s", redirectee->word);
+      break;
+
     case r_appending_to:
-      if (redirector != 1)
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}", redir_word->word);
+      else if (redirector != 1)
        cprintf ("%d", redirector);
       cprintf (">> %s", redirectee->word);
       break;
 
+    case r_input_output:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}", redir_word->word);
+      else if (redirector != 1)
+       cprintf ("%d", redirector);
+      cprintf ("<> %s", redirectee->word);
+      break;
+
     case r_deblank_reading_until:
     case r_reading_until:
       print_heredoc_header (redirect);
@@ -1019,8 +1165,14 @@ print_redirection (redirect)
       break;
 
     case r_reading_string:
-      if (redirector != 0)
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}", redir_word->word);
+      else if (redirector != 0)
        cprintf ("%d", redirector);
+#if 0
+      /* Don't need to check whether or not to requote, since original quotes
+         are still intact.  The only thing that has happened is that $'...'
+         has been replaced with 'expanded ...'. */
       if (ansic_shouldquote (redirect->redirectee.filename->word))
        {
          char *x;
@@ -1029,63 +1181,79 @@ print_redirection (redirect)
          free (x);
        }
       else
+#endif
        cprintf ("<<< %s", redirect->redirectee.filename->word);
       break;
 
     case r_duplicating_input:
-      cprintf ("%d<&%d", redirector, redir_fd);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}<&%d", redir_word->word, redir_fd);
+      else
+       cprintf ("%d<&%d", redirector, redir_fd);
       break;
 
     case r_duplicating_output:
-      cprintf ("%d>&%d", redirector, redir_fd);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}>&%d", redir_word->word, redir_fd);
+      else
+       cprintf ("%d>&%d", redirector, redir_fd);
       break;
 
     case r_duplicating_input_word:
-      cprintf ("%d<&%s", redirector, redirectee->word);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}<&%s", redir_word->word, redirectee->word);
+      else
+       cprintf ("%d<&%s", redirector, redirectee->word);
       break;
 
     case r_duplicating_output_word:
-      cprintf ("%d>&%s", redirector, redirectee->word);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}>&%s", redir_word->word, redirectee->word);
+      else
+       cprintf ("%d>&%s", redirector, redirectee->word);
       break;
 
     case r_move_input:
-      cprintf ("%d<&%d-", redirector, redir_fd);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}<&%d-", redir_word->word, redir_fd);
+      else
+       cprintf ("%d<&%d-", redirector, redir_fd);
       break;
 
     case r_move_output:
-      cprintf ("%d>&%d-", redirector, redir_fd);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}>&%d-", redir_word->word, redir_fd);
+      else
+       cprintf ("%d>&%d-", redirector, redir_fd);
       break;
 
     case r_move_input_word:
-      cprintf ("%d<&%s-", redirector, redirectee->word);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}<&%s-", redir_word->word, redirectee->word);
+      else
+       cprintf ("%d<&%s-", redirector, redirectee->word);
       break;
 
     case r_move_output_word:
-      cprintf ("%d>&%s-", redirector, redirectee->word);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}>&%s-", redir_word->word, redirectee->word);
+      else
+       cprintf ("%d>&%s-", redirector, redirectee->word);
       break;
 
     case r_close_this:
-      cprintf ("%d>&-", redirector);
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}>&-", redir_word->word);
+      else
+       cprintf ("%d>&-", redirector);
       break;
 
     case r_err_and_out:
-      cprintf ("&>%s", redirectee->word);
+      cprintf ("&> %s", redirectee->word);
       break;
 
     case r_append_err_and_out:
-      cprintf ("&>>%s", redirectee->word);
-      break;
-
-    case r_input_output:
-      if (redirector != 1)
-       cprintf ("%d", redirector);
-      cprintf ("<> %s", redirectee->word);
-      break;
-
-    case r_output_force:
-      if (redirector != 1)
-       cprintf ("%d", redirector);
-      cprintf (">|%s", redirectee->word);
+      cprintf ("&>> %s", redirectee->word);
       break;
     }
 }
@@ -1125,6 +1293,7 @@ print_function_def (func)
   make_command_string_internal (cmdcopy->type == cm_group
                                        ? cmdcopy->value.Group->command
                                        : cmdcopy);
+  /* XXX - PRINT_DEFERRED_HEREDOCS (""); ? */
 
   remove_unwind_protect ();
   indentation -= indentation_amount;
@@ -1196,6 +1365,7 @@ named_function_string (name, command, flags)
   make_command_string_internal (cmdcopy->type == cm_group
                                        ? cmdcopy->value.Group->command
                                        : cmdcopy);
+  /* XXX - PRINT_DEFERRED_HEREDOCS (""); ? */
 
   indentation = old_indent;
   indentation_amount = old_amount;
@@ -1224,7 +1394,8 @@ named_function_string (name, command, flags)
          }
 #else
       if (result[2] == '\n')   /* XXX -- experimental */
-       strcpy (result + 2, result + 3);
+       memmove (result + 2, result + 3, strlen (result) - 2);
+       
 #endif
     }
 
@@ -1260,7 +1431,7 @@ indent (amount)
   for (i = 0; amount > 0; amount--)
     indentation_string[i++] = ' ';
   indentation_string[i] = '\0';
-  cprintf (indentation_string);
+  cprintf ("%s", indentation_string);
 }
 
 static void
@@ -1268,7 +1439,7 @@ semicolon ()
 {
   if (command_string_index > 0 &&
        (the_printed_command[command_string_index - 1] == '&' ||
-        the_printed_command[command_string_index - 1] == '\n'))
+       the_printed_command[command_string_index - 1] == '\n'))
     return;
   cprintf (";");
 }
@@ -1333,7 +1504,7 @@ cprintf (control, va_alist)
                  argp = intbuf;
                }
              else
-               argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
+               argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
              arg_len = strlen (argp);
              break;
 
@@ -1357,6 +1528,8 @@ cprintf (control, va_alist)
        }
     }
 
+  va_end (args);
+
   the_printed_command[command_string_index] = '\0';
 }