Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / print_cmd.c
index 4b95d8f..9c362d3 100644 (file)
@@ -5,7 +5,7 @@ This file is part of GNU Bash, the Bourne Again SHell.
 
 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
+Software Foundation; either version 2, or (at your option) any later
 version.
 
 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -15,13 +15,16 @@ for more details.
 
 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. */
+Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 #include "config.h"
 
 #include <stdio.h>
 
 #if defined (HAVE_UNISTD_H)
+#  ifdef _MINIX
+#    include <sys/types.h>
+#  endif
 #  include <unistd.h>
 #endif
 
@@ -36,7 +39,7 @@ Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
 #include "bashansi.h"
 
 #include "shell.h"
-#include "y.tab.h"
+#include <y.tab.h>     /* use <...> so we pick it up from the build directory */
 #include "stdc.h"
 #include "builtins/common.h"
 
@@ -47,7 +50,11 @@ extern int printf __P((const char *, ...));  /* Yuck.  Double yuck. */
 static int indentation;
 static int indentation_amount = 4;
 
+#if defined (PREFER_STDARG)
 static void cprintf __P((char *, ...));
+#else
+static void cprintf ();
+#endif
 
 static void newline (), indent (), the_printed_command_resize ();
 static void semicolon ();
@@ -70,8 +77,19 @@ 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 ();
+#endif
+#if defined (COND_COMMAND)
+static void print_cond_node ();
+static void print_cond_command ();
+#endif
+#if defined (ARITH_FOR_COMMAND)
+static void print_arith_for_command ();
+#endif
 
-#define PRINTED_COMMAND_GROW_SIZE 1024
+#define PRINTED_COMMAND_INITIAL_SIZE 64
+#define PRINTED_COMMAND_GROW_SIZE 128
 
 char *the_printed_command = (char *)NULL;
 int the_printed_command_size = 0;
@@ -80,6 +98,7 @@ int command_string_index = 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;
 
 /* The depth of the group commands that we are currently printing.  This
    includes the group command that is a function body. */
@@ -102,7 +121,7 @@ char *
 make_command_string (command)
      COMMAND *command;
 {
-  command_string_index = 0;
+  command_string_index = was_heredoc = 0;
   make_command_string_internal (command);
   return (the_printed_command);
 }
@@ -121,11 +140,12 @@ make_command_string_internal (command)
       else
        indent (indentation);
 
-      if (command->flags & CMD_WANT_SUBSHELL)
-       cprintf ("( ");
-
       if (command->flags & CMD_TIME_PIPELINE)
-       cprintf ("time ");
+       {
+         cprintf ("time ");
+         if (command->flags & CMD_TIME_POSIX)
+           cprintf ("-p ");
+       }
 
       if (command->flags & CMD_INVERT_RETURN)
        cprintf ("! ");
@@ -136,6 +156,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);
@@ -158,6 +184,18 @@ make_command_string_internal (command)
          print_if_command (command->value.If);
          break;
 
+#if defined (DPAREN_ARITHMETIC)
+       case cm_arith:
+         print_arith_command (command->value.Arith);
+         break;
+#endif
+
+#if defined (COND_COMMAND)
+       case cm_cond:
+         print_cond_command (command->value.Cond);
+         break;
+#endif
+
        case cm_simple:
          print_simple_command (command->value.Simple);
          break;
@@ -195,7 +233,10 @@ make_command_string_internal (command)
              break;
 
            case ';':
-             cprintf (";");
+             if (was_heredoc == 0)
+               cprintf (";");
+             else
+               was_heredoc = 0;
 
              if (inside_function_def)
                cprintf ("\n");
@@ -224,16 +265,24 @@ 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);
+         cprintf (" )");
+         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);
+       }
     }
 }
 
@@ -263,18 +312,22 @@ xtrace_print_word_list (list)
      WORD_LIST *list;
 {
   WORD_LIST *w;
-  char *t;
+  char *t, *x;
 
   fprintf (stderr, "%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 (stderr, "'%s'%s", t, w->next ? " " : "");
+       fprintf (stderr, "''%s", w->next ? " " : "");
+      else if (sh_contains_shell_metas (t))
+       {
+         x = sh_single_quote (t);
+         fprintf (stderr, "%s%s", x, w->next ? " " : "");
+         free (x);
+       }
       else
-        fprintf (stderr, "%s%s", t, w->next ? " " : "");
+       fprintf (stderr, "%s%s", t, w->next ? " " : "");
     }
   fprintf (stderr, "\n");
 }
@@ -302,6 +355,27 @@ print_for_command (for_command)
   newline ("done");
 }
 
+#if defined (ARITH_FOR_COMMAND)
+static void
+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);
+  semicolon ();
+  indentation -= indentation_amount;
+  newline ("done");
+}
+#endif /* ARITH_FOR_COMMAND */
+
 #if defined (SELECT_COMMAND)
 static void
 print_select_command (select_command)
@@ -441,6 +515,128 @@ print_if_command (if_command)
   newline ("fi");
 }
 
+#if defined (DPAREN_ARITHMETIC)
+static void
+print_arith_command (arith_command)
+     ARITH_COM *arith_command;
+{
+  cprintf ("(( ");
+  command_print_word_list (arith_command->exp, " ");
+  cprintf (" ))");
+}
+#endif
+
+#if defined (COND_COMMAND)
+static void
+print_cond_node (cond)
+     COND_COM *cond;
+{
+  if (cond->flags & CMD_INVERT_RETURN)
+    cprintf ("! ");
+
+  if (cond->type == COND_EXPR)
+    {
+      cprintf ("( ");
+      print_cond_node (cond->left);
+      cprintf (" )");
+    }
+  else if (cond->type == COND_AND)
+    {
+      print_cond_node (cond->left);
+      cprintf (" && ");
+      print_cond_node (cond->right);
+    }
+  else if (cond->type == COND_OR)
+    {
+      print_cond_node (cond->left);
+      cprintf (" || ");
+      print_cond_node (cond->right);
+    }
+  else if (cond->type == COND_UNARY)
+    {
+      cprintf ("%s", cond->op->word);
+      cprintf (" ");
+      print_cond_node (cond->left);
+    }
+  else if (cond->type == COND_BINARY)
+    {
+      print_cond_node (cond->left);
+      cprintf (" ");
+      cprintf ("%s", cond->op->word);
+      cprintf (" ");
+      print_cond_node (cond->right);
+    }
+  else if (cond->type == COND_TERM)
+    {
+      cprintf ("%s", cond->op->word);          /* need to add quoting here */
+    }
+}
+
+static void
+print_cond_command (cond)
+     COND_COM *cond;
+{
+  cprintf ("[[ ");
+  print_cond_node (cond);
+  cprintf (" ]]");
+}
+
+#ifdef DEBUG
+void
+debug_print_cond_command (cond)
+     COND_COM *cond;
+{
+  fprintf (stderr, "DEBUG: ");
+  command_string_index = 0;
+  print_cond_command (cond);
+  fprintf (stderr, "%s\n", the_printed_command);
+}
+#endif
+
+void
+xtrace_print_cond_term (type, invert, op, arg1, arg2)
+     int type, invert;
+     WORD_DESC *op;
+     char *arg1, *arg2;
+{
+  command_string_index = 0;
+  fprintf (stderr, "%s", indirection_level_string ());
+  fprintf (stderr, "[[ ");
+  if (invert)
+    fprintf (stderr, "! ");
+
+  if (type == COND_UNARY)
+    {
+      fprintf (stderr, "%s ", op->word);
+      fprintf (stderr, "%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 (stderr, " ]]\n");
+}        
+#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)
+     WORD_LIST *list;
+{
+  WORD_LIST *w;
+
+  fprintf (stderr, "%s", indirection_level_string ());
+  fprintf (stderr, "(( ");
+  for (w = list; w; w = w->next)
+    fprintf (stderr, "%s%s", w->word->word, w->next ? " " : "");
+  fprintf (stderr, " ))\n");
+}
+#endif
+
 void
 print_simple_command (simple_command)
      SIMPLE_COM *simple_command;
@@ -458,23 +654,62 @@ static void
 print_redirection_list (redirects)
      REDIRECT *redirects;
 {
+  REDIRECT *heredocs, *hdtail, *newredir;
+
+  heredocs = (REDIRECT *)NULL;
+  hdtail = heredocs;
+
+  was_heredoc = 0;
   while (redirects)
     {
-      print_redirection (redirects);
+      /* Defer printing the here documents until we've printed the
+        rest of the redirections. */
+      if (redirects->instruction == r_reading_until || redirects->instruction == r_deblank_reading_until)
+       {
+         newredir = copy_redirect (redirects);
+         newredir->next = (REDIRECT *)NULL;
+         if (heredocs)
+           {
+             hdtail->next = newredir;
+             hdtail = newredir;
+           }
+         else
+           hdtail = heredocs = newredir;
+       }
+      else
+       print_redirection (redirects);
+
       redirects = redirects->next;
       if (redirects)
        cprintf (" ");
     }
+
+  /* Now that we've printed all the other redirections (on one line),
+     print the here documents. */
+  if (heredocs)
+    {
+      cprintf (" "); 
+      for (hdtail = heredocs; hdtail; hdtail = hdtail->next)
+       {
+         print_redirection (hdtail);
+         cprintf ("\n");
+       }
+      dispose_redirects (heredocs);
+      was_heredoc = 1;
+    }
 }
 
 static void
 print_redirection (redirect)
      REDIRECT *redirect;
 {
-  int kill_leading = 0;
-  int redirector = redirect->redirector;
-  WORD_DESC *redirectee = redirect->redirectee.filename;
-  int redir_fd = redirect->redirectee.dest;
+  int kill_leading, redirector, redir_fd;
+  WORD_DESC *redirectee;
+
+  kill_leading = 0;
+  redirectee = redirect->redirectee.filename;
+  redirector = redirect->redirector;
+  redir_fd = redirect->redirectee.dest;
 
   switch (redirect->instruction)
     {
@@ -508,12 +743,12 @@ print_redirection (redirect)
        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);
+       {
+         char *x;
+         x = sh_single_quote (redirect->here_doc_eof);
          cprintf ("<<%s%s\n", kill_leading? "-" : "", x);
-          free (x);
-        }
+         free (x);
+       }
       else
        cprintf ("<<%s%s\n", kill_leading? "-" : "", redirect->here_doc_eof);
       cprintf ("%s%s",
@@ -569,6 +804,9 @@ static void
 print_function_def (func)
      FUNCTION_DEF *func;
 {
+  COMMAND *cmdcopy;
+  REDIRECT *func_redirects;
+
   cprintf ("function %s () \n", func->name->word);
   add_unwind_protect (reset_locals, 0);
 
@@ -578,15 +816,30 @@ 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);
 
   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.
@@ -602,10 +855,12 @@ named_function_string (name, command, multi_line)
 {
   char *result;
   int old_indent, old_amount;
+  COMMAND *cmdcopy;
+  REDIRECT *func_redirects;
 
   old_indent = indentation;
   old_amount = indentation_amount;
-  command_string_index = 0;
+  command_string_index = was_heredoc = 0;
 
   if (name && *name)
     cprintf ("%s ", name);
@@ -627,15 +882,31 @@ named_function_string (name, command, multi_line)
 
   cprintf (multi_line ? "{ \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);
 
   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;
 
@@ -651,10 +922,12 @@ named_function_string (name, command, multi_line)
          }
 #else
       if (result[2] == '\n')   /* XXX -- experimental */
-        strcpy (result + 2, result + 3);
+       strcpy (result + 2, result + 3);
 #endif
     }
 
+  dispose_command (cmdcopy);
+
   return (result);
 }
 
@@ -700,7 +973,7 @@ cprintf (format, arg1, arg2)
      char *format, *arg1, *arg2;
 {
   register char *s;
-  char char_arg[2], *argp, *args[2];
+  char char_arg[2], *argp, *args[2], intbuf[32];
   int arg_len, c, arg_index;
 
   args[arg_index = 0] = arg1;
@@ -737,10 +1010,9 @@ cprintf (format, arg1, arg2)
              break;
 
            case 'd':
-             argp = itos (pointer_to_int (args[arg_index]));
+             argp = inttostr (pointer_to_int (args[arg_index]), intbuf, sizeof (intbuf));
              arg_index++;
              arg_len = strlen (argp);
-             free_argp = 1;
              break;
 
            case 'c':
@@ -780,7 +1052,7 @@ cprintf (control, va_alist)
 #endif
 {
   register char *s;
-  char char_arg[2], *argp;
+  char char_arg[2], *argp, intbuf[32];
   int digit_arg, arg_len, c;
   va_list args;
 
@@ -824,9 +1096,8 @@ cprintf (control, va_alist)
 
            case 'd':
              digit_arg = va_arg (args, int);
-             argp = itos (digit_arg);
+             argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
              arg_len = strlen (argp);
-             free_argp = 1;
              break;
 
            case 'c':
@@ -861,9 +1132,9 @@ static void
 the_printed_command_resize (length)
      int length;
 {
-  if (!the_printed_command)
+  if (the_printed_command == 0)
     {
-      the_printed_command_size = length + 1;
+      the_printed_command_size = (length + PRINTED_COMMAND_INITIAL_SIZE - 1) & ~(PRINTED_COMMAND_INITIAL_SIZE - 1);
       the_printed_command = xmalloc (the_printed_command_size);
       command_string_index = 0;
     }
@@ -871,9 +1142,11 @@ the_printed_command_resize (length)
     {
       int new;
       new = command_string_index + length + 1;
-      new = new + 2 * PRINTED_COMMAND_GROW_SIZE - 1;
-      new -= new % PRINTED_COMMAND_GROW_SIZE;
+
+      /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */
+      new = (new + PRINTED_COMMAND_GROW_SIZE - 1) & ~(PRINTED_COMMAND_GROW_SIZE - 1);
       the_printed_command_size = new;
+
       the_printed_command = xrealloc (the_printed_command, the_printed_command_size);
     }
 }
@@ -881,14 +1154,22 @@ the_printed_command_resize (length)
 #if defined (HAVE_VFPRINTF)
 
 static void
-xprintf (va_alist)
+#if defined (PREFER_STDARG)
+xprintf (const char *format, ...)
+#else
+xprintf (format, va_alist)
+     const char *format;
      va_dcl
+#endif
 {
   va_list args;
-  char *format;
 
+#if defined (PREFER_STDARG)
+  va_start (args, format);
+#else
   va_start (args);
-  format = va_arg (args, char *);
+#endif
+
   vfprintf (stdout, format, args);
   va_end (args);
 }