Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / print_cmd.c
index 9c362d3..9429472 100644 (file)
@@ -31,62 +31,70 @@ 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 "shell.h"
+#include "flags.h"
 #include <y.tab.h>     /* use <...> so we pick it up from the build directory */
-#include "stdc.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 ();
+static void xprintf ();
 #endif
 
-static void newline (), indent (), the_printed_command_resize ();
-static void semicolon ();
-static void xprintf ();
+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 ();
-static void command_print_word_list ();
-static void print_case_clauses ();
-static void print_redirection_list ();
-static void print_redirection ();
+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_for_command ();
+static void print_for_command __P((FOR_COM *));
+#if defined (ARITH_FOR_COMMAND)
+static void print_arith_for_command __P((ARITH_FOR_COM *));
+#endif
 #if defined (SELECT_COMMAND)
-static void print_select_command ();
+static void print_select_command __P((SELECT_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 ();
+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 (DPAREN_ARITHMETIC)
-static void print_arith_command ();
+static void print_arith_command __P((ARITH_COM *));
 #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 ();
+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 *));
 
 #define PRINTED_COMMAND_INITIAL_SIZE 64
 #define PRINTED_COMMAND_GROW_SIZE 128
@@ -104,6 +112,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)
@@ -290,7 +301,7 @@ static void
 _print_word_list (list, separator, pfunc)
      WORD_LIST *list;
      char *separator;
-     VFunction *pfunc;
+     PFUNC *pfunc;
 {
   WORD_LIST *w;
 
@@ -306,6 +317,35 @@ 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;
+
+  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);
+
+  for (i = 0; *ps4 && i < indirection_level && i < 99; i++)
+    indirection_string[i] = *ps4;
+
+  for (j = 1; *ps4 && ps4[j] && i < 99; i++, j++)
+    indirection_string[i] = ps4[j];
+
+  indirection_string[i] = '\0';
+  free (ps4);
+  return (indirection_string);
+}
+
 /* A function to print the words of a simple command when set -x is on. */
 void
 xtrace_print_word_list (list)
@@ -326,6 +366,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 ? " " : "");
     }
@@ -755,6 +801,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;
@@ -771,6 +831,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;
@@ -807,6 +883,7 @@ print_function_def (func)
   COMMAND *cmdcopy;
   REDIRECT *func_redirects;
 
+  func_redirects = NULL;
   cprintf ("function %s () \n", func->name->word);
   add_unwind_protect (reset_locals, 0);
 
@@ -961,106 +1038,29 @@ 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)
-     char *format, *arg1, *arg2;
-{
-  register char *s;
-  char char_arg[2], *argp, *args[2], intbuf[32];
-  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 = inttostr (pointer_to_int (args[arg_index]), 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)
-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, intbuf[32];
+  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);
@@ -1069,13 +1069,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
@@ -1095,8 +1094,18 @@ 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 = inttostr (digit_arg, intbuf, sizeof (intbuf));
+             if (digit_arg < 0)
+               {
+                 sprintf (intbuf, "%u", (unsigned)-1);
+                 argp = intbuf;
+               }
+             else
+               argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
              arg_len = strlen (argp);
              break;
 
@@ -1117,14 +1126,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. */
@@ -1135,7 +1141,7 @@ 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)
@@ -1147,11 +1153,13 @@ the_printed_command_resize (length)
       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);
+      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)
@@ -1164,11 +1172,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);
@@ -1178,9 +1182,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 */