Imported from ../bash-3.2.48.tar.gz.
[platform/upstream/bash.git] / print_cmd.c
index 4aee8d3..d1dfd1a 100644 (file)
@@ -1,5 +1,6 @@
 /* print_command -- A way to make readable commands from a command tree. */
-/* Copyright (C) 1989 Free Software Foundation, Inc.
+
+/* Copyright (C) 1989-2005 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -31,22 +32,26 @@ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 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 !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;
 
@@ -87,12 +92,8 @@ 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 (DPAREN_ARITHMETIC)
-static void print_arith_command __P((ARITH_COM *));
-#endif
 #if defined (COND_COMMAND)
 static void print_cond_node __P((COND_COM *));
-static void print_cond_command __P((COND_COM *));
 #endif
 static void print_function_def __P((FUNCTION_DEF *));
 
@@ -112,6 +113,9 @@ static int was_heredoc;
    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[100];
+
 /* Print COMMAND (a command tree) on standard output. */
 void
 print_command (command)
@@ -194,7 +198,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
 
@@ -257,7 +261,7 @@ make_command_string_internal (command)
              break;
 
            default:
-             cprintf ("print_command: bad connector `%d'",
+             cprintf (_("print_command: bad connector `%d'"),
                       command->value.Connection->connector);
              break;
            }
@@ -314,15 +318,103 @@ print_word_list (list, separator)
   _print_word_list (list, separator, xprintf);
 }
 
+/* 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;
+
+  indirection_string[0] = '\0';
+  ps4 = get_string_value ("PS4");
+
+  if (ps4 == 0 || *ps4 == '\0')
+    return (indirection_string);
+
+  change_flag ('x', FLAG_OFF);
+  ps4 = decode_prompt_string (ps4);
+  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 || MB_INVALIDCH (ps4_firstc_len))
+    {
+      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
+      
+  for (i = j = 0; ps4_firstc[0] && j < indirection_level && i < 99; 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 < 99; 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;
+
+  if (xflags)
+    fprintf (stderr, "%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 (stderr, "%s=(%s)\n", name, nval);
+  else
+    fprintf (stderr, "%s=%s\n", name, nval);
+
+  if (nval != value)
+    FREE (nval);
+
+  fflush (stderr);
+}
+
 /* 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 ());
+  if (xtflags)
+    fprintf (stderr, "%s", indirection_level_string ());
+
   for (w = list; w; w = w->next)
     {
       t = w->word->word;
@@ -334,6 +426,12 @@ xtrace_print_word_list (list)
          fprintf (stderr, "%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 ? " " : "");
+         free (x);
+       }
       else
        fprintf (stderr, "%s%s", t, w->next ? " " : "");
     }
@@ -348,12 +446,29 @@ 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;
+{
+  fprintf (stderr, "%s", indirection_level_string ());
+  fprintf (stderr, "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;
@@ -368,13 +483,13 @@ static void
 print_arith_for_command (arith_for_command)
      ARITH_FOR_COM *arith_for_command;
 {
-  cprintf ("for (( ");
+  cprintf ("for ((");
   command_print_word_list (arith_for_command->init, " ");
-  cprintf (" ; ");
+  cprintf ("; ");
   command_print_word_list (arith_for_command->test, " ");
-  cprintf (" ; ");
+  cprintf ("; ");
   command_print_word_list (arith_for_command->step, " ");
-  cprintf (" ))");
+  cprintf ("))");
   newline ("do\n");
   indentation += indentation_amount;
   make_command_string_internal (arith_for_command->action);
@@ -385,12 +500,29 @@ print_arith_for_command (arith_for_command)
 #endif /* ARITH_FOR_COMMAND */
 
 #if defined (SELECT_COMMAND)
-static void
-print_select_command (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;
+{
+  fprintf (stderr, "%s", indirection_level_string ());
+  fprintf (stderr, "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;
@@ -438,11 +570,27 @@ 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;
+{
+  fprintf (stderr, "%s", indirection_level_string ());
+  fprintf (stderr, "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");
@@ -524,13 +672,13 @@ 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
 
@@ -580,7 +728,7 @@ print_cond_node (cond)
     }
 }
 
-static void
+void
 print_cond_command (cond)
      COND_COM *cond;
 {
@@ -684,6 +832,13 @@ print_redirection_list (redirects)
          else
            hdtail = heredocs = newredir;
        }
+      else if (redirects->instruction == r_duplicating_output_word && redirects->redirector == 1)
+       {
+         /* Temporarily translate it as the execution code does. */
+         redirects->instruction = r_err_and_out;
+         print_redirection (redirects);
+         redirects->instruction = r_duplicating_output_word;
+       }
       else
        print_redirection (redirects);
 
@@ -724,13 +879,13 @@ print_redirection (redirect)
     case r_output_direction:
       if (redirector != 1)
        cprintf ("%d", redirector);
-      cprintf (">%s", redirectee->word);
+      cprintf ("> %s", redirectee->word);
       break;
 
     case r_input_direction:
       if (redirector != 0)
        cprintf ("%d", redirector);
-      cprintf ("<%s", redirectee->word);
+      cprintf ("< %s", redirectee->word);
       break;
 
     case r_inputa_direction:   /* Redirection created by the shell. */
@@ -740,7 +895,7 @@ print_redirection (redirect)
     case r_appending_to:
       if (redirector != 1)
        cprintf ("%d", redirector);
-      cprintf (">>%s", redirectee->word);
+      cprintf (">> %s", redirectee->word);
       break;
 
     case r_deblank_reading_until:
@@ -763,6 +918,20 @@ print_redirection (redirect)
               redirect->redirectee.filename->word, redirect->here_doc_eof);
       break;
 
+    case r_reading_string:
+      if (redirector != 0)
+       cprintf ("%d", redirector);
+      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", redirect->redirectee.filename->word);
+      break;
+
     case r_duplicating_input:
       cprintf ("%d<&%d", redirector, redir_fd);
       break;
@@ -779,6 +948,22 @@ print_redirection (redirect)
       cprintf ("%d>&%s", redirector, redirectee->word);
       break;
 
+    case r_move_input:
+      cprintf ("%d<&%d-", redirector, redir_fd);
+      break;
+
+    case r_move_output:
+      cprintf ("%d>&%d-", redirector, redir_fd);
+      break;
+
+    case r_move_input_word:
+      cprintf ("%d<&%s-", redirector, redirectee->word);
+      break;
+
+    case r_move_output_word:
+      cprintf ("%d>&%s-", redirector, redirectee->word);
+      break;
+
     case r_close_this:
       cprintf ("%d>&-", redirector);
       break;
@@ -790,7 +975,7 @@ print_redirection (redirect)
     case r_input_output:
       if (redirector != 1)
        cprintf ("%d", redirector);
-      cprintf ("<>%s", redirectee->word);
+      cprintf ("<> %s", redirectee->word);
       break;
 
     case r_output_force:
@@ -970,99 +1155,13 @@ indent (amount)
 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)
-     const char *format;
-     char *arg1, *arg2;
-{
-  register const char *s;
-  char char_arg[2], *argp, *args[2], intbuf[INT_STRLEN_BOUND(int) + 1];
-  int arg_len, c, arg_index, digit_arg;
-
-  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)
-       {
-         char_arg[0] = c;
-         argp = char_arg;
-         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':
-             /* 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 = pointer_to_int (args[arg_index]);
-             if (digit_arg < 0)
-               {
-                 sprintf (intbuf, "%u", (unsigned)-1);
-                 argp = intbuf;
-               }
-             else
-               argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
-             arg_index++;
-             arg_len = strlen (argp);
-             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)
@@ -1078,11 +1177,7 @@ cprintf (control, va_alist)
   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);
@@ -1091,8 +1186,6 @@ cprintf (control, va_alist)
   s = control;
   while (s && *s)
     {
-      int free_argp;
-      free_argp = 0;
       c = *s++;
       argp = (char *)NULL;
       if (c != '%' || !*s)
@@ -1140,7 +1233,7 @@ cprintf (control, va_alist)
              break;
 
            default:
-             programming_error ("cprintf: bad `%%' argument (%c)", c);
+             programming_error (_("cprintf: `%c': invalid format character"), c);
              /*NOTREACHED*/
            }
        }
@@ -1150,14 +1243,11 @@ 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);
        }
     }
 
   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. */
@@ -1199,11 +1289,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);