]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - print_cmd.c
Bash-4.3 patch 32
[thirdparty/bash.git] / print_cmd.c
index 44460e93d43d0b1df4106c338c113425ff1597c8..ea63dca46d942d9cdef1e64b8fbe25f4c0cb1c8f 100644 (file)
@@ -1,21 +1,22 @@
 /* print_command -- A way to make readable commands from a command tree. */
-/* Copyright (C) 1989 Free Software Foundation, Inc.
 
-This file is part of GNU Bash, the Bourne Again SHell.
+/* Copyright (C) 1989-2011 Free Software Foundation, Inc.
 
-Bash is free software; you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 1, or (at your option) any later
-version.
+   This file is part of GNU Bash, the Bourne Again SHell.
 
-Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-for more details.
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-You should have received a copy of the GNU General Public License along
-with Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include "config.h"
 
@@ -31,59 +32,74 @@ Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
 #if defined (PREFER_STDARG)
 #  include <stdarg.h>
 #else
-#  if defined (PREFER_VARARGS)
-#    include <varargs.h>
-#  endif
+#  include <varargs.h>
 #endif
 
 #include "bashansi.h"
+#include "bashintl.h"
 
 #include "shell.h"
+#include "flags.h"
 #include <y.tab.h>     /* use <...> so we pick it up from the build directory */
-#include "stdc.h"
+
+#include "shmbutil.h"
+
 #include "builtins/common.h"
 
-#if !defined (PRINTF_DECLARED)
+#if !HAVE_DECL_PRINTF
 extern int printf __P((const char *, ...));    /* Yuck.  Double yuck. */
 #endif
 
+extern int indirection_level;
+
 static int indentation;
 static int indentation_amount = 4;
 
 #if defined (PREFER_STDARG)
-static void cprintf __P((char *, ...));
+typedef void PFUNC __P((const char *, ...));
+
+static void cprintf __P((const char *, ...))  __attribute__((__format__ (printf, 1, 2)));
+static void xprintf __P((const char *, ...))  __attribute__((__format__ (printf, 1, 2)));
 #else
+#define PFUNC VFunction
 static void cprintf ();
-#endif
-
-static void newline (), indent (), the_printed_command_resize ();
-static void semicolon ();
 static void xprintf ();
+#endif
 
-static void make_command_string_internal ();
-static void command_print_word_list ();
-static void print_case_clauses ();
-static void print_redirection_list ();
-static void print_redirection ();
-
-static void print_for_command ();
-#if defined (SELECT_COMMAND)
-static void print_select_command ();
+static void reset_locals __P((void));
+static void newline __P((char *));
+static void indent __P((int));
+static void semicolon __P((void));
+static void the_printed_command_resize __P((int));
+
+static void make_command_string_internal __P((COMMAND *));
+static void _print_word_list __P((WORD_LIST *, char *, PFUNC *));
+static void command_print_word_list __P((WORD_LIST *, char *));
+static void print_case_clauses __P((PATTERN_LIST *));
+static void print_redirection_list __P((REDIRECT *));
+static void print_redirection __P((REDIRECT *));
+static void print_heredoc_header __P((REDIRECT *));
+static void print_heredoc_body __P((REDIRECT *));
+static void print_heredocs __P((REDIRECT *));
+static void print_deferred_heredocs __P((const char *));
+
+static void print_for_command __P((FOR_COM *));
+#if defined (ARITH_FOR_COMMAND)
+static void print_arith_for_command __P((ARITH_FOR_COM *));
 #endif
-static void print_group_command ();
-static void print_case_command ();
-static void print_while_command ();
-static void print_until_command ();
-static void print_until_or_while ();
-static void print_if_command ();
-static void print_function_def ();
-#if defined (DPAREN_ARITHMETIC)
-static void print_arith_command ();
+#if defined (SELECT_COMMAND)
+static void print_select_command __P((SELECT_COM *));
 #endif
+static void print_group_command __P((GROUP_COM *));
+static void print_case_command __P((CASE_COM *));
+static void print_while_command __P((WHILE_COM *));
+static void print_until_command __P((WHILE_COM *));
+static void print_until_or_while __P((WHILE_COM *, char *));
+static void print_if_command __P((IF_COM *));
 #if defined (COND_COMMAND)
-static void print_cond_node ();
-static void print_cond_command ();
+static void print_cond_node __P((COND_COM *));
 #endif
+static void print_function_def __P((FUNCTION_DEF *));
 
 #define PRINTED_COMMAND_INITIAL_SIZE 64
 #define PRINTED_COMMAND_GROW_SIZE 128
@@ -92,15 +108,35 @@ 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;
 static int was_heredoc;
+static int printing_connection;
+static REDIRECT *deferred_heredocs;
 
 /* The depth of the group commands that we are currently printing.  This
    includes the group command that is a function body. */
 static int group_command_nesting;
 
+/* A buffer to indicate the indirection level (PS4) when set -x is enabled. */
+static char *indirection_string = 0;
+static int indirection_stringsiz = 0;
+
 /* Print COMMAND (a command tree) on standard output. */
 void
 print_command (command)
@@ -119,6 +155,7 @@ make_command_string (command)
      COMMAND *command;
 {
   command_string_index = was_heredoc = 0;
+  deferred_heredocs = 0;
   make_command_string_internal (command);
   return (the_printed_command);
 }
@@ -128,6 +165,8 @@ static void
 make_command_string_internal (command)
      COMMAND *command;
 {
+  char s[3];
+
   if (command == 0)
     cprintf ("");
   else
@@ -137,9 +176,6 @@ make_command_string_internal (command)
       else
        indent (indentation);
 
-      if (command->flags & CMD_WANT_SUBSHELL)
-       cprintf ("( ");
-
       if (command->flags & CMD_TIME_PIPELINE)
        {
          cprintf ("time ");
@@ -156,6 +192,12 @@ make_command_string_internal (command)
          print_for_command (command->value.For);
          break;
 
+#if defined (ARITH_FOR_COMMAND)
+       case cm_arith_for:
+         print_arith_for_command (command->value.ArithFor);
+         break;
+#endif
+
 #if defined (SELECT_COMMAND)
        case cm_select:
          print_select_command (command->value.Select);
@@ -180,7 +222,7 @@ make_command_string_internal (command)
 
 #if defined (DPAREN_ARITHMETIC)
        case cm_arith:
-         print_arith_command (command->value.Arith);
+         print_arith_command (command->value.Arith->exp);
          break;
 #endif
 
@@ -197,6 +239,7 @@ make_command_string_internal (command)
        case cm_connection:
 
          skip_this_indent++;
+         printing_connection++;
          make_command_string_internal (command->value.Connection->first);
 
          switch (command->value.Connection->connector)
@@ -205,7 +248,13 @@ make_command_string_internal (command)
            case '|':
              {
                char c = command->value.Connection->connector;
-               cprintf (" %c", c);
+
+               s[0] = ' ';
+               s[1] = c;
+               s[2] = '\0';
+               
+               print_deferred_heredocs (s);
+
                if (c != '&' || command->value.Connection->second)
                  {
                    cprintf (" ");
@@ -215,22 +264,27 @@ make_command_string_internal (command)
              break;
 
            case AND_AND:
-             cprintf (" && ");
+             print_deferred_heredocs (" && ");
              if (command->value.Connection->second)
                skip_this_indent++;
              break;
 
            case OR_OR:
-             cprintf (" || ");
+             print_deferred_heredocs (" || ");
              if (command->value.Connection->second)
                skip_this_indent++;
              break;
 
            case ';':
-             if (was_heredoc == 0)
-               cprintf (";");
+             if (deferred_heredocs == 0)
+               {
+                 if (was_heredoc == 0)
+                   cprintf (";");
+                 else
+                   was_heredoc = 0;
+               }
              else
-               was_heredoc = 0;
+               print_deferred_heredocs (inside_function_def ? "" : ";");
 
              if (inside_function_def)
                cprintf ("\n");
@@ -243,12 +297,14 @@ make_command_string_internal (command)
              break;
 
            default:
-             cprintf ("print_command: bad connector `%d'",
+             cprintf (_("print_command: bad connector `%d'"),
                       command->value.Connection->connector);
              break;
            }
 
          make_command_string_internal (command->value.Connection->second);
+         PRINT_DEFERRED_HEREDOCS ("");
+         printing_connection--;                  
          break;
 
        case cm_function_def:
@@ -259,16 +315,31 @@ make_command_string_internal (command)
          print_group_command (command->value.Group);
          break;
 
+       case cm_subshell:
+         cprintf ("( ");
+         skip_this_indent++;
+         make_command_string_internal (command->value.Subshell->command);
+         PRINT_DEFERRED_HEREDOCS ("");
+         cprintf (" )");
+         break;
+
+       case cm_coproc:
+         cprintf ("coproc %s ", command->value.Coproc->name);
+         skip_this_indent++;
+         make_command_string_internal (command->value.Coproc->command);
+         break;
+
        default:
-         programming_error ("print_command: bad command type `%d'", command->type);
+         command_error ("print_command", CMDERR_BADTYPE, command->type, 0);
          break;
        }
 
-      if (command->flags & CMD_WANT_SUBSHELL)
-       cprintf (" )");
 
       if (command->redirects)
-       print_redirection_list (command->redirects);
+       {
+         cprintf (" ");
+         print_redirection_list (command->redirects);
+       }
     }
 }
 
@@ -276,7 +347,7 @@ static void
 _print_word_list (list, separator, pfunc)
      WORD_LIST *list;
      char *separator;
-     VFunction *pfunc;
+     PFUNC *pfunc;
 {
   WORD_LIST *w;
 
@@ -292,30 +363,192 @@ 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 *
+indirection_level_string ()
+{
+  register int i, j;
+  char *ps4;
+  char ps4_firstc[MB_LEN_MAX+1];
+  int ps4_firstc_len, ps4_len, ineed, old;
+
+  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);
+
+  old = change_flag ('x', FLAG_OFF);
+  ps4 = decode_prompt_string (ps4);
+  if (old)
+    change_flag ('x', FLAG_ON);
+
+  if (ps4 == 0 || *ps4 == '\0')
+    return (indirection_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 || ps4_firstc_len < 0)
+    {
+      ps4_firstc[0] = ps4[0];
+      ps4_firstc[ps4_firstc_len = 1] = '\0';
+    }
+  else
+    memcpy (ps4_firstc, ps4, ps4_firstc_len);
+#else
+  ps4_firstc[0] = ps4[0];
+  ps4_firstc[ps4_firstc_len = 1] = '\0';
+#endif
+
+  /* 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];
+      else
+       memcpy (indirection_string+i, ps4_firstc, ps4_firstc_len);
+    }      
+
+  for (j = ps4_firstc_len; *ps4 && ps4[j] && i < indirection_stringsiz - 1; i++, j++)
+    indirection_string[i] = ps4[j];
+
+  indirection_string[i] = '\0';
+  free (ps4);
+  return (indirection_string);
+}
+
+void
+xtrace_print_assignment (name, value, assign_list, xflags)
+     char *name, *value;
+     int assign_list, xflags;
+{
+  char *nval;
+
+  CHECK_XTRACE_FP;
+
+  if (xflags)
+    fprintf (xtrace_fp, "%s", indirection_level_string ());
+
+  /* VALUE should not be NULL when this is called. */
+  if (*value == '\0' || assign_list)
+    nval = value;
+  else if (sh_contains_shell_metas (value))
+    nval = sh_single_quote (value);
+  else if (ansic_shouldquote (value))
+    nval = ansic_quote (value, 0, (int *)0);
+  else
+    nval = value;
+
+  if (assign_list)
+    fprintf (xtrace_fp, "%s=(%s)\n", name, nval);
+  else
+    fprintf (xtrace_fp, "%s=%s\n", name, nval);
+
+  if (nval != value)
+    FREE (nval);
+
+  fflush (xtrace_fp);
+}
+
 /* A function to print the words of a simple command when set -x is on. */
 void
-xtrace_print_word_list (list)
+xtrace_print_word_list (list, xtflags)
      WORD_LIST *list;
+     int xtflags;
 {
   WORD_LIST *w;
   char *t, *x;
 
-  fprintf (stderr, "%s", indirection_level_string ());
+  CHECK_XTRACE_FP;
+
+  if (xtflags)
+    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 ? " " : "");
-      else if (contains_shell_metas (t))
+       fprintf (xtrace_fp, "''%s", w->next ? " " : "");
+      else if (sh_contains_shell_metas (t))
        {
-         x = single_quote (t);
-         fprintf (stderr, "%s%s", x, w->next ? " " : "");
+         x = sh_single_quote (t);
+         fprintf (xtrace_fp, "%s%s", x, w->next ? " " : "");
+         free (x);
+       }
+      else if (ansic_shouldquote (t))
+       {
+         x = ansic_quote (t, 0, (int *)0);
+         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
@@ -326,32 +559,93 @@ command_print_word_list (list, separator)
   _print_word_list (list, separator, cprintf);
 }
 
-static void
-print_for_command (for_command)
+void
+print_for_command_head (for_command)
      FOR_COM *for_command;
 {
   cprintf ("for %s in ", for_command->name->word);
   command_print_word_list (for_command->map_list, " ");
+}
+
+void
+xtrace_print_for_command_head (for_command)
+     FOR_COM *for_command;
+{
+  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);
+}
+
+static void
+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");
 }
 
-#if defined (SELECT_COMMAND)
+#if defined (ARITH_FOR_COMMAND)
 static void
-print_select_command (select_command)
+print_arith_for_command (arith_for_command)
+     ARITH_FOR_COM *arith_for_command;
+{
+  cprintf ("for ((");
+  command_print_word_list (arith_for_command->init, " ");
+  cprintf ("; ");
+  command_print_word_list (arith_for_command->test, " ");
+  cprintf ("; ");
+  command_print_word_list (arith_for_command->step, " ");
+  cprintf ("))");
+  newline ("do\n");
+  indentation += indentation_amount;
+  make_command_string_internal (arith_for_command->action);
+  PRINT_DEFERRED_HEREDOCS ("");
+  semicolon ();
+  indentation -= indentation_amount;
+  newline ("done");
+}
+#endif /* ARITH_FOR_COMMAND */
+
+#if defined (SELECT_COMMAND)
+void
+print_select_command_head (select_command)
      SELECT_COM *select_command;
 {
   cprintf ("select %s in ", select_command->name->word);
   command_print_word_list (select_command->map_list, " ");
+}
+
+void
+xtrace_print_select_command_head (select_command)
+     SELECT_COM *select_command;
+{
+  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);
+}
+
+static void
+print_select_command (select_command)
+     SELECT_COM *select_command;
+{
+  print_select_command_head (select_command);
+
   cprintf (";");
   newline ("do\n");
   indentation += indentation_amount;
   make_command_string_internal (select_command->action);
+  PRINT_DEFERRED_HEREDOCS ("");
   semicolon ();
   indentation -= indentation_amount;
   newline ("done");
@@ -377,6 +671,7 @@ print_group_command (group_command)
     }
 
   make_command_string_internal (group_command->command);
+  PRINT_DEFERRED_HEREDOCS ("");
 
   if (inside_function_def)
     {
@@ -395,11 +690,28 @@ print_group_command (group_command)
   group_command_nesting--;
 }
 
+void
+print_case_command_head (case_command)
+     CASE_COM *case_command;
+{
+  cprintf ("case %s in ", case_command->word->word);
+}
+
+void
+xtrace_print_case_command_head (case_command)
+     CASE_COM *case_command;
+{
+  CHECK_XTRACE_FP;
+  fprintf (xtrace_fp, "%s", indirection_level_string ());
+  fprintf (xtrace_fp, "case %s in\n", case_command->word->word);
+}
+
 static void
 print_case_command (case_command)
      CASE_COM *case_command;
 {
-  cprintf ("case %s in ", case_command->word->word);
+  print_case_command_head (case_command);
+
   if (case_command->clauses)
     print_case_clauses (case_command->clauses);
   newline ("esac");
@@ -418,7 +730,13 @@ print_case_clauses (clauses)
       indentation += indentation_amount;
       make_command_string_internal (clauses->action);
       indentation -= indentation_amount;
-      newline (";;");
+      PRINT_DEFERRED_HEREDOCS ("");
+      if (clauses->flags & CASEPAT_FALLTHROUGH)
+       newline (";&");
+      else if (clauses->flags & CASEPAT_TESTNEXT)
+       newline (";;&");
+      else
+       newline (";;");
       clauses = clauses->next;
     }
   indentation -= indentation_amount;
@@ -446,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");
@@ -466,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)
@@ -474,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 ();
@@ -481,14 +803,15 @@ print_if_command (if_command)
 }
 
 #if defined (DPAREN_ARITHMETIC)
-static void
-print_arith_command (arith_command)
-     ARITH_COM *arith_command;
+void
+print_arith_command (arith_cmd_list)
+     WORD_LIST *arith_cmd_list;
 {
-  cprintf ("(( ");
-  command_print_word_list (arith_command->exp, " ");
-  cprintf (" ))");
+  cprintf ("((");
+  command_print_word_list (arith_cmd_list, " ");
+  cprintf ("))");
 }
+#endif
 
 #if defined (COND_COMMAND)
 static void
@@ -518,7 +841,7 @@ print_cond_node (cond)
     }
   else if (cond->type == COND_UNARY)
     {
-      cprintf (cond->op->word);
+      cprintf ("%s", cond->op->word);
       cprintf (" ");
       print_cond_node (cond->left);
     }
@@ -526,17 +849,17 @@ print_cond_node (cond)
     {
       print_cond_node (cond->left);
       cprintf (" ");
-      cprintf (cond->op->word);
+      cprintf ("%s", cond->op->word);
       cprintf (" ");
       print_cond_node (cond->right);
     }
   else if (cond->type == COND_TERM)
     {
-      cprintf (cond->op->word);                /* need to add quoting here */
+      cprintf ("%s", cond->op->word);          /* need to add quoting here */
     }
 }
 
-static void
+void
 print_cond_command (cond)
      COND_COM *cond;
 {
@@ -545,6 +868,22 @@ print_cond_command (cond)
   cprintf (" ]]");
 }
 
+#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;
@@ -554,6 +893,7 @@ debug_print_cond_command (cond)
   print_cond_command (cond);
   fprintf (stderr, "%s\n", the_printed_command);
 }
+#endif
 
 void
 xtrace_print_cond_term (type, invert, op, arg1, arg2)
@@ -561,28 +901,32 @@ 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 */
 
+#if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
 /* A function to print the words of an arithmetic command when set -x is on. */
 void
 xtrace_print_arith_cmd (list)
@@ -590,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
 
@@ -611,11 +958,63 @@ print_simple_command (simple_command)
     }
 }
 
+static void
+print_heredocs (heredocs)
+     REDIRECT *heredocs;
+{
+  REDIRECT *hdtail;
+
+  cprintf (" "); 
+  for (hdtail = heredocs; hdtail; hdtail = hdtail->next)
+    {
+      print_redirection (hdtail);
+      cprintf ("\n");
+    }
+  was_heredoc = 1;
+}
+
+/* Print heredocs that are attached to the command before the connector
+   represented by CSTRING.  The parsing semantics require us to print the
+   here-doc delimiters, then the connector (CSTRING), then the here-doc
+   bodies.  We don't print the connector if it's a `;', but we use it to
+   note not to print an extra space after the last heredoc body and
+   newline. */
+static void
+print_deferred_heredocs (cstring)
+     const char *cstring;
+{
+  REDIRECT *hdtail;    
+
+  for (hdtail = deferred_heredocs; hdtail; hdtail = hdtail->next)
+    {
+      cprintf (" ");
+      print_heredoc_header (hdtail);
+    }
+  if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
+    cprintf ("%s", cstring); 
+  if (deferred_heredocs)
+    cprintf ("\n");
+  for (hdtail = deferred_heredocs; hdtail; hdtail = hdtail->next)
+    {
+      print_heredoc_body (hdtail);
+      cprintf ("\n");
+    }
+  if (deferred_heredocs)
+    {
+      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;
+}
+      
 static void
 print_redirection_list (redirects)
      REDIRECT *redirects;
 {
   REDIRECT *heredocs, *hdtail, *newredir;
+  char *rw;
 
   heredocs = (REDIRECT *)NULL;
   hdtail = heredocs;
@@ -637,6 +1036,15 @@ print_redirection_list (redirects)
          else
            hdtail = heredocs = newredir;
        }
+      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. */
+         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;
+       }
       else
        print_redirection (redirects);
 
@@ -647,109 +1055,205 @@ print_redirection_list (redirects)
 
   /* Now that we've printed all the other redirections (on one line),
      print the here documents. */
-  if (heredocs)
+  if (heredocs && printing_connection)
+    deferred_heredocs = heredocs;
+  else if (heredocs)
     {
-      cprintf (" "); 
-      for (hdtail = heredocs; hdtail; hdtail = hdtail->next)
-        {
-         print_redirection (hdtail);
-         cprintf ("\n");
-        }
+      print_heredocs (heredocs);
       dispose_redirects (heredocs);
-      was_heredoc = 1;
     }
 }
 
+static void
+print_heredoc_header (redirect)
+     REDIRECT *redirect;
+{
+  int kill_leading;
+  char *x;
+
+  kill_leading = redirect->instruction == r_deblank_reading_until;
+
+  /* Here doc header */
+  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)
+    {
+      x = sh_single_quote (redirect->here_doc_eof);
+      cprintf ("<<%s%s", kill_leading ? "-" : "", x);
+      free (x);
+    }
+  else
+    cprintf ("<<%s%s", kill_leading ? "-" : "", redirect->here_doc_eof);
+}
+
+static void
+print_heredoc_body (redirect)
+     REDIRECT *redirect;
+{
+  /* Here doc body */
+  cprintf ("%s%s", redirect->redirectee.filename->word, redirect->here_doc_eof);
+}
+
 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);
+      cprintf ("<> %s", redirectee->word);
       break;
 
     case r_deblank_reading_until:
-      kill_leading++;
-      /* ... */
     case r_reading_until:
-      if (redirector != 0)
+      print_heredoc_header (redirect);
+      cprintf ("\n");
+      print_heredoc_body (redirect);
+      break;
+
+    case r_reading_string:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}", redir_word->word);
+      else if (redirector != 0)
        cprintf ("%d", redirector);
-      /* If the here document delimiter is quoted, single-quote it. */
-      if (redirect->redirectee.filename->flags & W_QUOTED)
-        {
-          char *x;
-          x = single_quote (redirect->here_doc_eof);
-         cprintf ("<<%s%s\n", kill_leading? "-" : "", x);
-          free (x);
-        }
+#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;
+         x = ansic_quote (redirect->redirectee.filename->word, 0, (int *)0);
+         cprintf ("<<< %s", x);
+         free (x);
+       }
       else
-       cprintf ("<<%s%s\n", kill_leading? "-" : "", redirect->here_doc_eof);
-      cprintf ("%s%s",
-              redirect->redirectee.filename->word, redirect->here_doc_eof);
+#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_close_this:
-      cprintf ("%d>&-", redirector);
+    case r_move_input:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}<&%d-", redir_word->word, redir_fd);
+      else
+       cprintf ("%d<&%d-", redirector, redir_fd);
       break;
 
-    case r_err_and_out:
-      cprintf (">&%s", redirectee->word);
+    case r_move_output:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}>&%d-", redir_word->word, redir_fd);
+      else
+       cprintf ("%d>&%d-", redirector, redir_fd);
       break;
 
-    case r_input_output:
-      if (redirector != 1)
-       cprintf ("%d", redirector);
-      cprintf ("<>%s", redirectee->word);
+    case r_move_input_word:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}<&%s-", redir_word->word, redirectee->word);
+      else
+       cprintf ("%d<&%s-", redirector, redirectee->word);
       break;
 
-    case r_output_force:
-      if (redirector != 1)
-       cprintf ("%d", redirector);
-      cprintf (">|%s", redirectee->word);
+    case r_move_output_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:
+      if (redirect->rflags & REDIR_VARASSIGN)
+       cprintf ("{%s}>&-", redir_word->word);
+      else
+       cprintf ("%d>&-", redirector);
+      break;
+
+    case r_err_and_out:
+      cprintf ("&> %s", redirectee->word);
+      break;
+
+    case r_append_err_and_out:
+      cprintf ("&>> %s", redirectee->word);
       break;
     }
 }
@@ -759,12 +1263,18 @@ reset_locals ()
 {
   inside_function_def = 0;
   indentation = 0;
+  printing_connection = 0;
+  deferred_heredocs = 0;
 }
 
 static void
 print_function_def (func)
      FUNCTION_DEF *func;
 {
+  COMMAND *cmdcopy;
+  REDIRECT *func_redirects;
+
+  func_redirects = NULL;
   cprintf ("function %s () \n", func->name->word);
   add_unwind_protect (reset_locals, 0);
 
@@ -774,41 +1284,61 @@ print_function_def (func)
   inside_function_def++;
   indentation += indentation_amount;
 
-  make_command_string_internal (func->command->type == cm_group
-                                       ? func->command->value.Group->command
-                                       : func->command);
+  cmdcopy = copy_command (func->command);
+  if (cmdcopy->type == cm_group)
+    {
+      func_redirects = cmdcopy->redirects;
+      cmdcopy->redirects = (REDIRECT *)NULL;
+    }
+  make_command_string_internal (cmdcopy->type == cm_group
+                                       ? cmdcopy->value.Group->command
+                                       : cmdcopy);
+  /* XXX - PRINT_DEFERRED_HEREDOCS (""); ? */
 
   remove_unwind_protect ();
   indentation -= indentation_amount;
   inside_function_def--;
 
-  newline ("}");
+  if (func_redirects)
+    { /* { */
+      newline ("} ");
+      print_redirection_list (func_redirects);
+      cmdcopy->redirects = func_redirects;
+    }
+  else
+    newline ("}");
+
+  dispose_command (cmdcopy);
 }
 
 /* Return the string representation of the named function.
    NAME is the name of the function.
    COMMAND is the function body.  It should be a GROUP_COM.
-   MULTI_LINE is non-zero to pretty-print, or zero for all on one line.
+   flags&FUNC_MULTILINE is non-zero to pretty-print, or zero for all on one line.
+   flags&FUNC_EXTERNAL means convert from internal to external form
   */
 char *
-named_function_string (name, command, multi_line)
+named_function_string (name, command, flags)
      char *name;
      COMMAND *command;
-     int multi_line;
+     int flags;
 {
   char *result;
   int old_indent, old_amount;
+  COMMAND *cmdcopy;
+  REDIRECT *func_redirects;
 
   old_indent = indentation;
   old_amount = indentation_amount;
   command_string_index = was_heredoc = 0;
+  deferred_heredocs = 0;
 
   if (name && *name)
     cprintf ("%s ", name);
 
   cprintf ("() ");
 
-  if (multi_line == 0)
+  if ((flags & FUNC_MULTILINE) == 0)
     {
       indentation = 1;
       indentation_amount = 0;
@@ -821,21 +1351,38 @@ named_function_string (name, command, multi_line)
 
   inside_function_def++;
 
-  cprintf (multi_line ? "{ \n" : "{ ");
+  cprintf ((flags & FUNC_MULTILINE) ? "{ \n" : "{ ");
 
-  make_command_string_internal (command->type == cm_group
-                                       ? command->value.Group->command
-                                       : command);
+  cmdcopy = copy_command (command);
+  /* Take any redirections specified in the function definition (which should
+     apply to the function as a whole) and save them for printing later. */
+  func_redirects = (REDIRECT *)NULL;
+  if (cmdcopy->type == cm_group)
+    {
+      func_redirects = cmdcopy->redirects;
+      cmdcopy->redirects = (REDIRECT *)NULL;
+    }
+  make_command_string_internal (cmdcopy->type == cm_group
+                                       ? cmdcopy->value.Group->command
+                                       : cmdcopy);
+  /* XXX - PRINT_DEFERRED_HEREDOCS (""); ? */
 
   indentation = old_indent;
   indentation_amount = old_amount;
   inside_function_def--;
 
-  newline ("}");
+  if (func_redirects)
+    { /* { */
+      newline ("} ");
+      print_redirection_list (func_redirects);
+      cmdcopy->redirects = func_redirects;
+    }
+  else
+    newline ("}");
 
   result = the_printed_command;
 
-  if (!multi_line)
+  if ((flags & FUNC_MULTILINE) == 0)
     {
 #if 0
       register int i;
@@ -847,10 +1394,16 @@ named_function_string (name, command, multi_line)
          }
 #else
       if (result[2] == '\n')   /* XXX -- experimental */
-        strcpy (result + 2, result + 3);
+       memmove (result + 2, result + 3, strlen (result) - 2);
+       
 #endif
     }
 
+  dispose_command (cmdcopy);
+
+  if (flags & FUNC_EXTERNAL)
+    result = remove_quoted_escapes (result);
+
   return (result);
 }
 
@@ -878,113 +1431,35 @@ indent (amount)
   for (i = 0; amount > 0; amount--)
     indentation_string[i++] = ' ';
   indentation_string[i] = '\0';
-  cprintf (indentation_string);
+  cprintf ("%s", indentation_string);
 }
 
 static void
 semicolon ()
 {
-  if (command_string_index > 0 && the_printed_command[command_string_index - 1] == '&')
+  if (command_string_index > 0 &&
+       (the_printed_command[command_string_index - 1] == '&' ||
+       the_printed_command[command_string_index - 1] == '\n'))
     return;
   cprintf (";");
 }
 
-#if !defined (USE_VARARGS)
-/* How to make the string. */
-static void
-cprintf (format, arg1, arg2)
-     char *format, *arg1, *arg2;
-{
-  register char *s;
-  char char_arg[2], *argp, *args[2];
-  int arg_len, c, arg_index;
-
-  args[arg_index = 0] = arg1;
-  args[1] = arg2;
-
-  arg_len = strlen (format);
-  the_printed_command_resize (arg_len + 1);
-
-  char_arg[1] = '\0';
-  s = format;
-  while (s && *s)
-    {
-      int free_argp = 0;
-      c = *s++;
-      if (c != '%' || !*s)
-       {
-         argp = s;
-         arg_len = 1;
-       }
-      else
-       {
-         c = *s++;
-         switch (c)
-           {
-           case '%':
-             char_arg[0] = c;
-             argp = char_arg;
-             arg_len = 1;
-             break;
-
-           case 's':
-             argp = (char *)args[arg_index++];
-             arg_len = strlen (argp);
-             break;
-
-           case 'd':
-             argp = itos (pointer_to_int (args[arg_index]));
-             arg_index++;
-             arg_len = strlen (argp);
-             free_argp = 1;
-             break;
-
-           case 'c':
-             char_arg[0] = pointer_to_int (args[arg_index]);
-             arg_index++;
-             argp = char_arg;
-             arg_len = 1;
-             break;
-
-           default:
-             programming_error ("cprintf: bad `%%' argument (%c)", c);
-           }
-       }
-      if (argp)
-       {
-         the_printed_command_resize (arg_len + 1);
-         FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
-         command_string_index += arg_len;
-         if (free_argp)
-           free (argp);
-       }
-    }
-
-  the_printed_command[command_string_index] = '\0';
-}
-
-#else /* We have support for varargs. */
-
 /* How to make the string. */
 static void
 #if defined (PREFER_STDARG)
-cprintf (char *control, ...)
+cprintf (const char *control, ...)
 #else
 cprintf (control, va_alist)
-     char *control;
+     const char *control;
      va_dcl
 #endif
 {
-  register char *s;
-  char char_arg[2], *argp;
+  register const char *s;
+  char char_arg[2], *argp, intbuf[INT_STRLEN_BOUND (int) + 1];
   int digit_arg, arg_len, c;
   va_list args;
 
-#if defined (PREFER_STDARG)
-  va_start (args, control);
-#else
-  va_start (args);
-#endif
+  SH_VA_START (args, control);
 
   arg_len = strlen (control);
   the_printed_command_resize (arg_len + 1);
@@ -993,13 +1468,12 @@ cprintf (control, va_alist)
   s = control;
   while (s && *s)
     {
-      int free_argp;
-      free_argp = 0;
       c = *s++;
       argp = (char *)NULL;
       if (c != '%' || !*s)
        {
-         argp = s - 1;
+         char_arg[0] = c;
+         argp = char_arg;
          arg_len = 1;
        }
       else
@@ -1019,10 +1493,19 @@ cprintf (control, va_alist)
              break;
 
            case 'd':
+             /* Represent an out-of-range file descriptor with an out-of-range
+                integer value.  We can do this because the only use of `%d' in
+                the calls to cprintf is to output a file descriptor number for
+                a redirection. */
              digit_arg = va_arg (args, int);
-             argp = itos (digit_arg);
+             if (digit_arg < 0)
+               {
+                 sprintf (intbuf, "%u", (unsigned)-1);
+                 argp = intbuf;
+               }
+             else
+               argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
              arg_len = strlen (argp);
-             free_argp = 1;
              break;
 
            case 'c':
@@ -1032,7 +1515,7 @@ cprintf (control, va_alist)
              break;
 
            default:
-             programming_error ("cprintf: bad `%%' argument (%c)", c);
+             programming_error (_("cprintf: `%c': invalid format character"), c);
              /*NOTREACHED*/
            }
        }
@@ -1042,14 +1525,13 @@ cprintf (control, va_alist)
          the_printed_command_resize (arg_len + 1);
          FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
          command_string_index += arg_len;
-         if (free_argp)
-           free (argp);
        }
     }
 
+  va_end (args);
+
   the_printed_command[command_string_index] = '\0';
 }
-#endif /* HAVE_VARARGS_H */
 
 /* Ensure that there is enough space to stuff LENGTH characters into
    THE_PRINTED_COMMAND. */
@@ -1060,26 +1542,25 @@ the_printed_command_resize (length)
   if (the_printed_command == 0)
     {
       the_printed_command_size = (length + PRINTED_COMMAND_INITIAL_SIZE - 1) & ~(PRINTED_COMMAND_INITIAL_SIZE - 1);
-      the_printed_command = xmalloc (the_printed_command_size);
+      the_printed_command = (char *)xmalloc (the_printed_command_size);
       command_string_index = 0;
     }
   else if ((command_string_index + length) >= the_printed_command_size)
     {
       int new;
       new = command_string_index + length + 1;
-#if 1
+
       /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */
       new = (new + PRINTED_COMMAND_GROW_SIZE - 1) & ~(PRINTED_COMMAND_GROW_SIZE - 1);
-#else
-      new = new + 2 * PRINTED_COMMAND_GROW_SIZE - 1;
-      new -= new % PRINTED_COMMAND_GROW_SIZE;
-#endif
       the_printed_command_size = new;
-      the_printed_command = xrealloc (the_printed_command, the_printed_command_size);
+
+      the_printed_command = (char *)xrealloc (the_printed_command, the_printed_command_size);
     }
 }
 
-#if defined (HAVE_VFPRINTF)
+#if defined (HAVE_VPRINTF)
+/* ``If vprintf is available, you may assume that vfprintf and vsprintf are
+     also available.'' */
 
 static void
 #if defined (PREFER_STDARG)
@@ -1092,11 +1573,7 @@ xprintf (format, va_alist)
 {
   va_list args;
 
-#if defined (PREFER_STDARG)
-  va_start (args, format);
-#else
-  va_start (args);
-#endif
+  SH_VA_START (args, format);
 
   vfprintf (stdout, format, args);
   va_end (args);
@@ -1106,9 +1583,9 @@ xprintf (format, va_alist)
 
 static void
 xprintf (format, arg1, arg2, arg3, arg4, arg5)
-     char *format;
+     const char *format;
 {
   printf (format, arg1, arg2, arg3, arg4, arg5);
 }
 
-#endif /* !HAVE_VFPRINTF */
+#endif /* !HAVE_VPRINTF */