1 /* print_command -- A way to make readable commands from a command tree. */
2 /* Copyright (C) 1989 Free Software Foundation, Inc.
4 This file is part of GNU Bash, the Bourne Again SHell.
6 Bash is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License along
17 with Bash; see the file COPYING. If not, write to the Free Software
18 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #if defined (HAVE_UNISTD_H)
26 # include <sys/types.h>
31 #if defined (PREFER_STDARG)
34 # if defined (PREFER_VARARGS)
42 #include <y.tab.h> /* use <...> so we pick it up from the build directory */
44 #include "builtins/common.h"
47 extern int printf __P((const char *, ...)); /* Yuck. Double yuck. */
50 static int indentation;
51 static int indentation_amount = 4;
53 #if defined (PREFER_STDARG)
54 typedef void PFUNC __P((const char *, ...));
56 static void cprintf __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
57 static void xprintf __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
59 #define PFUNC VFunction
60 static void cprintf ();
61 static void xprintf ();
64 static void reset_locals __P((void));
65 static void newline __P((char *));
66 static void indent __P((int));
67 static void semicolon __P((void));
68 static void the_printed_command_resize __P((int));
70 static void make_command_string_internal __P((COMMAND *));
71 static void _print_word_list __P((WORD_LIST *, char *, PFUNC *));
72 static void command_print_word_list __P((WORD_LIST *, char *));
73 static void print_case_clauses __P((PATTERN_LIST *));
74 static void print_redirection_list __P((REDIRECT *));
75 static void print_redirection __P((REDIRECT *));
77 static void print_for_command __P((FOR_COM *));
78 #if defined (ARITH_FOR_COMMAND)
79 static void print_arith_for_command __P((ARITH_FOR_COM *));
81 #if defined (SELECT_COMMAND)
82 static void print_select_command __P((SELECT_COM *));
84 static void print_group_command __P((GROUP_COM *));
85 static void print_case_command __P((CASE_COM *));
86 static void print_while_command __P((WHILE_COM *));
87 static void print_until_command __P((WHILE_COM *));
88 static void print_until_or_while __P((WHILE_COM *, char *));
89 static void print_if_command __P((IF_COM *));
90 #if defined (DPAREN_ARITHMETIC)
91 static void print_arith_command __P((ARITH_COM *));
93 #if defined (COND_COMMAND)
94 static void print_cond_node __P((COND_COM *));
95 static void print_cond_command __P((COND_COM *));
97 static void print_function_def __P((FUNCTION_DEF *));
99 #define PRINTED_COMMAND_INITIAL_SIZE 64
100 #define PRINTED_COMMAND_GROW_SIZE 128
102 char *the_printed_command = (char *)NULL;
103 int the_printed_command_size = 0;
104 int command_string_index = 0;
106 /* Non-zero means the stuff being printed is inside of a function def. */
107 static int inside_function_def;
108 static int skip_this_indent;
109 static int was_heredoc;
111 /* The depth of the group commands that we are currently printing. This
112 includes the group command that is a function body. */
113 static int group_command_nesting;
115 /* Print COMMAND (a command tree) on standard output. */
117 print_command (command)
120 command_string_index = 0;
121 printf ("%s", make_command_string (command));
124 /* Make a string which is the printed representation of the command
125 tree in COMMAND. We return this string. However, the string is
126 not consed, so you have to do that yourself if you want it to
129 make_command_string (command)
132 command_string_index = was_heredoc = 0;
133 make_command_string_internal (command);
134 return (the_printed_command);
137 /* The internal function. This is the real workhorse. */
139 make_command_string_internal (command)
146 if (skip_this_indent)
149 indent (indentation);
151 if (command->flags & CMD_TIME_PIPELINE)
154 if (command->flags & CMD_TIME_POSIX)
158 if (command->flags & CMD_INVERT_RETURN)
161 switch (command->type)
164 print_for_command (command->value.For);
167 #if defined (ARITH_FOR_COMMAND)
169 print_arith_for_command (command->value.ArithFor);
173 #if defined (SELECT_COMMAND)
175 print_select_command (command->value.Select);
180 print_case_command (command->value.Case);
184 print_while_command (command->value.While);
188 print_until_command (command->value.While);
192 print_if_command (command->value.If);
195 #if defined (DPAREN_ARITHMETIC)
197 print_arith_command (command->value.Arith);
201 #if defined (COND_COMMAND)
203 print_cond_command (command->value.Cond);
208 print_simple_command (command->value.Simple);
214 make_command_string_internal (command->value.Connection->first);
216 switch (command->value.Connection->connector)
221 char c = command->value.Connection->connector;
223 if (c != '&' || command->value.Connection->second)
233 if (command->value.Connection->second)
239 if (command->value.Connection->second)
244 if (was_heredoc == 0)
249 if (inside_function_def)
254 if (command->value.Connection->second)
260 cprintf ("print_command: bad connector `%d'",
261 command->value.Connection->connector);
265 make_command_string_internal (command->value.Connection->second);
268 case cm_function_def:
269 print_function_def (command->value.Function_def);
273 print_group_command (command->value.Group);
279 make_command_string_internal (command->value.Subshell->command);
284 command_error ("print_command", CMDERR_BADTYPE, command->type, 0);
289 if (command->redirects)
292 print_redirection_list (command->redirects);
298 _print_word_list (list, separator, pfunc)
305 for (w = list; w; w = w->next)
306 (*pfunc) ("%s%s", w->word->word, w->next ? separator : "");
310 print_word_list (list, separator)
314 _print_word_list (list, separator, xprintf);
317 /* A function to print the words of a simple command when set -x is on. */
319 xtrace_print_word_list (list)
325 fprintf (stderr, "%s", indirection_level_string ());
326 for (w = list; w; w = w->next)
329 if (t == 0 || *t == '\0')
330 fprintf (stderr, "''%s", w->next ? " " : "");
331 else if (sh_contains_shell_metas (t))
333 x = sh_single_quote (t);
334 fprintf (stderr, "%s%s", x, w->next ? " " : "");
338 fprintf (stderr, "%s%s", t, w->next ? " " : "");
340 fprintf (stderr, "\n");
344 command_print_word_list (list, separator)
348 _print_word_list (list, separator, cprintf);
352 print_for_command (for_command)
353 FOR_COM *for_command;
355 cprintf ("for %s in ", for_command->name->word);
356 command_print_word_list (for_command->map_list, " ");
359 indentation += indentation_amount;
360 make_command_string_internal (for_command->action);
362 indentation -= indentation_amount;
366 #if defined (ARITH_FOR_COMMAND)
368 print_arith_for_command (arith_for_command)
369 ARITH_FOR_COM *arith_for_command;
372 command_print_word_list (arith_for_command->init, " ");
374 command_print_word_list (arith_for_command->test, " ");
376 command_print_word_list (arith_for_command->step, " ");
379 indentation += indentation_amount;
380 make_command_string_internal (arith_for_command->action);
382 indentation -= indentation_amount;
385 #endif /* ARITH_FOR_COMMAND */
387 #if defined (SELECT_COMMAND)
389 print_select_command (select_command)
390 SELECT_COM *select_command;
392 cprintf ("select %s in ", select_command->name->word);
393 command_print_word_list (select_command->map_list, " ");
396 indentation += indentation_amount;
397 make_command_string_internal (select_command->action);
399 indentation -= indentation_amount;
402 #endif /* SELECT_COMMAND */
405 print_group_command (group_command)
406 GROUP_COM *group_command;
408 group_command_nesting++;
411 if (inside_function_def == 0)
415 /* This is a group command { ... } inside of a function
416 definition, and should be printed as a multiline group
417 command, using the current indentation. */
419 indentation += indentation_amount;
422 make_command_string_internal (group_command->command);
424 if (inside_function_def)
427 indentation -= indentation_amount;
428 indent (indentation);
438 group_command_nesting--;
442 print_case_command (case_command)
443 CASE_COM *case_command;
445 cprintf ("case %s in ", case_command->word->word);
446 if (case_command->clauses)
447 print_case_clauses (case_command->clauses);
452 print_case_clauses (clauses)
453 PATTERN_LIST *clauses;
455 indentation += indentation_amount;
459 command_print_word_list (clauses->patterns, " | ");
461 indentation += indentation_amount;
462 make_command_string_internal (clauses->action);
463 indentation -= indentation_amount;
465 clauses = clauses->next;
467 indentation -= indentation_amount;
471 print_while_command (while_command)
472 WHILE_COM *while_command;
474 print_until_or_while (while_command, "while");
478 print_until_command (while_command)
479 WHILE_COM *while_command;
481 print_until_or_while (while_command, "until");
485 print_until_or_while (while_command, which)
486 WHILE_COM *while_command;
489 cprintf ("%s ", which);
491 make_command_string_internal (while_command->test);
493 cprintf (" do\n"); /* was newline ("do\n"); */
494 indentation += indentation_amount;
495 make_command_string_internal (while_command->action);
496 indentation -= indentation_amount;
502 print_if_command (if_command)
507 make_command_string_internal (if_command->test);
510 indentation += indentation_amount;
511 make_command_string_internal (if_command->true_case);
512 indentation -= indentation_amount;
514 if (if_command->false_case)
518 indentation += indentation_amount;
519 make_command_string_internal (if_command->false_case);
520 indentation -= indentation_amount;
526 #if defined (DPAREN_ARITHMETIC)
528 print_arith_command (arith_command)
529 ARITH_COM *arith_command;
532 command_print_word_list (arith_command->exp, " ");
537 #if defined (COND_COMMAND)
539 print_cond_node (cond)
542 if (cond->flags & CMD_INVERT_RETURN)
545 if (cond->type == COND_EXPR)
548 print_cond_node (cond->left);
551 else if (cond->type == COND_AND)
553 print_cond_node (cond->left);
555 print_cond_node (cond->right);
557 else if (cond->type == COND_OR)
559 print_cond_node (cond->left);
561 print_cond_node (cond->right);
563 else if (cond->type == COND_UNARY)
565 cprintf ("%s", cond->op->word);
567 print_cond_node (cond->left);
569 else if (cond->type == COND_BINARY)
571 print_cond_node (cond->left);
573 cprintf ("%s", cond->op->word);
575 print_cond_node (cond->right);
577 else if (cond->type == COND_TERM)
579 cprintf ("%s", cond->op->word); /* need to add quoting here */
584 print_cond_command (cond)
588 print_cond_node (cond);
594 debug_print_cond_command (cond)
597 fprintf (stderr, "DEBUG: ");
598 command_string_index = 0;
599 print_cond_command (cond);
600 fprintf (stderr, "%s\n", the_printed_command);
605 xtrace_print_cond_term (type, invert, op, arg1, arg2)
610 command_string_index = 0;
611 fprintf (stderr, "%s", indirection_level_string ());
612 fprintf (stderr, "[[ ");
614 fprintf (stderr, "! ");
616 if (type == COND_UNARY)
618 fprintf (stderr, "%s ", op->word);
619 fprintf (stderr, "%s", (arg1 && *arg1) ? arg1 : "''");
621 else if (type == COND_BINARY)
623 fprintf (stderr, "%s", (arg1 && *arg1) ? arg1 : "''");
624 fprintf (stderr, " %s ", op->word);
625 fprintf (stderr, "%s", (arg2 && *arg2) ? arg2 : "''");
628 fprintf (stderr, " ]]\n");
630 #endif /* COND_COMMAND */
632 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
633 /* A function to print the words of an arithmetic command when set -x is on. */
635 xtrace_print_arith_cmd (list)
640 fprintf (stderr, "%s", indirection_level_string ());
641 fprintf (stderr, "(( ");
642 for (w = list; w; w = w->next)
643 fprintf (stderr, "%s%s", w->word->word, w->next ? " " : "");
644 fprintf (stderr, " ))\n");
649 print_simple_command (simple_command)
650 SIMPLE_COM *simple_command;
652 command_print_word_list (simple_command->words, " ");
654 if (simple_command->redirects)
657 print_redirection_list (simple_command->redirects);
662 print_redirection_list (redirects)
665 REDIRECT *heredocs, *hdtail, *newredir;
667 heredocs = (REDIRECT *)NULL;
673 /* Defer printing the here documents until we've printed the
674 rest of the redirections. */
675 if (redirects->instruction == r_reading_until || redirects->instruction == r_deblank_reading_until)
677 newredir = copy_redirect (redirects);
678 newredir->next = (REDIRECT *)NULL;
681 hdtail->next = newredir;
685 hdtail = heredocs = newredir;
688 print_redirection (redirects);
690 redirects = redirects->next;
695 /* Now that we've printed all the other redirections (on one line),
696 print the here documents. */
700 for (hdtail = heredocs; hdtail; hdtail = hdtail->next)
702 print_redirection (hdtail);
705 dispose_redirects (heredocs);
711 print_redirection (redirect)
714 int kill_leading, redirector, redir_fd;
715 WORD_DESC *redirectee;
718 redirectee = redirect->redirectee.filename;
719 redirector = redirect->redirector;
720 redir_fd = redirect->redirectee.dest;
722 switch (redirect->instruction)
724 case r_output_direction:
726 cprintf ("%d", redirector);
727 cprintf (">%s", redirectee->word);
730 case r_input_direction:
732 cprintf ("%d", redirector);
733 cprintf ("<%s", redirectee->word);
736 case r_inputa_direction: /* Redirection created by the shell. */
742 cprintf ("%d", redirector);
743 cprintf (">>%s", redirectee->word);
746 case r_deblank_reading_until:
749 case r_reading_until:
751 cprintf ("%d", redirector);
752 /* If the here document delimiter is quoted, single-quote it. */
753 if (redirect->redirectee.filename->flags & W_QUOTED)
756 x = sh_single_quote (redirect->here_doc_eof);
757 cprintf ("<<%s%s\n", kill_leading? "-" : "", x);
761 cprintf ("<<%s%s\n", kill_leading? "-" : "", redirect->here_doc_eof);
763 redirect->redirectee.filename->word, redirect->here_doc_eof);
766 case r_duplicating_input:
767 cprintf ("%d<&%d", redirector, redir_fd);
770 case r_duplicating_output:
771 cprintf ("%d>&%d", redirector, redir_fd);
774 case r_duplicating_input_word:
775 cprintf ("%d<&%s", redirector, redirectee->word);
778 case r_duplicating_output_word:
779 cprintf ("%d>&%s", redirector, redirectee->word);
783 cprintf ("%d>&-", redirector);
787 cprintf (">&%s", redirectee->word);
792 cprintf ("%d", redirector);
793 cprintf ("<>%s", redirectee->word);
798 cprintf ("%d", redirector);
799 cprintf (">|%s", redirectee->word);
807 inside_function_def = 0;
812 print_function_def (func)
816 REDIRECT *func_redirects;
818 func_redirects = NULL;
819 cprintf ("function %s () \n", func->name->word);
820 add_unwind_protect (reset_locals, 0);
822 indent (indentation);
825 inside_function_def++;
826 indentation += indentation_amount;
828 cmdcopy = copy_command (func->command);
829 if (cmdcopy->type == cm_group)
831 func_redirects = cmdcopy->redirects;
832 cmdcopy->redirects = (REDIRECT *)NULL;
834 make_command_string_internal (cmdcopy->type == cm_group
835 ? cmdcopy->value.Group->command
838 remove_unwind_protect ();
839 indentation -= indentation_amount;
840 inside_function_def--;
845 print_redirection_list (func_redirects);
846 cmdcopy->redirects = func_redirects;
851 dispose_command (cmdcopy);
854 /* Return the string representation of the named function.
855 NAME is the name of the function.
856 COMMAND is the function body. It should be a GROUP_COM.
857 MULTI_LINE is non-zero to pretty-print, or zero for all on one line.
860 named_function_string (name, command, multi_line)
866 int old_indent, old_amount;
868 REDIRECT *func_redirects;
870 old_indent = indentation;
871 old_amount = indentation_amount;
872 command_string_index = was_heredoc = 0;
875 cprintf ("%s ", name);
882 indentation_amount = 0;
887 indentation += indentation_amount;
890 inside_function_def++;
892 cprintf (multi_line ? "{ \n" : "{ ");
894 cmdcopy = copy_command (command);
895 /* Take any redirections specified in the function definition (which should
896 apply to the function as a whole) and save them for printing later. */
897 func_redirects = (REDIRECT *)NULL;
898 if (cmdcopy->type == cm_group)
900 func_redirects = cmdcopy->redirects;
901 cmdcopy->redirects = (REDIRECT *)NULL;
903 make_command_string_internal (cmdcopy->type == cm_group
904 ? cmdcopy->value.Group->command
907 indentation = old_indent;
908 indentation_amount = old_amount;
909 inside_function_def--;
914 print_redirection_list (func_redirects);
915 cmdcopy->redirects = func_redirects;
920 result = the_printed_command;
926 for (i = 0; result[i]; i++)
927 if (result[i] == '\n')
929 strcpy (result + i, result + i + 1);
933 if (result[2] == '\n') /* XXX -- experimental */
934 strcpy (result + 2, result + 3);
938 dispose_command (cmdcopy);
948 indent (indentation);
949 if (string && *string)
950 cprintf ("%s", string);
953 static char *indentation_string;
954 static int indentation_size;
962 RESIZE_MALLOCED_BUFFER (indentation_string, 0, amount, indentation_size, 16);
964 for (i = 0; amount > 0; amount--)
965 indentation_string[i++] = ' ';
966 indentation_string[i] = '\0';
967 cprintf (indentation_string);
973 if (command_string_index > 0 && the_printed_command[command_string_index - 1] == '&')
978 #if !defined (USE_VARARGS)
979 /* How to make the string. */
981 cprintf (format, arg1, arg2)
985 register const char *s;
986 char char_arg[2], *argp, *args[2], intbuf[INT_STRLEN_BOUND(int) + 1];
987 int arg_len, c, arg_index, digit_arg;
989 args[arg_index = 0] = arg1;
992 arg_len = strlen (format);
993 the_printed_command_resize (arg_len + 1);
1001 if (c != '%' || !*s)
1019 argp = (char *)args[arg_index++];
1020 arg_len = strlen (argp);
1024 /* Represent an out-of-range file descriptor with an out-of-range
1025 integer value. We can do this because the only use of `%d' in
1026 the calls to cprintf is to output a file descriptor number for
1028 digit_arg = pointer_to_int (args[arg_index]);
1031 sprintf (intbuf, "%u", (unsigned)-1);
1035 argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
1037 arg_len = strlen (argp);
1041 char_arg[0] = pointer_to_int (args[arg_index]);
1048 programming_error ("cprintf: bad `%%' argument (%c)", c);
1053 the_printed_command_resize (arg_len + 1);
1054 FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
1055 command_string_index += arg_len;
1061 the_printed_command[command_string_index] = '\0';
1064 #else /* We have support for varargs. */
1066 /* How to make the string. */
1068 #if defined (PREFER_STDARG)
1069 cprintf (const char *control, ...)
1071 cprintf (control, va_alist)
1072 const char *control;
1076 register const char *s;
1077 char char_arg[2], *argp, intbuf[INT_STRLEN_BOUND (int) + 1];
1078 int digit_arg, arg_len, c;
1081 #if defined (PREFER_STDARG)
1082 va_start (args, control);
1087 arg_len = strlen (control);
1088 the_printed_command_resize (arg_len + 1);
1097 argp = (char *)NULL;
1098 if (c != '%' || !*s)
1116 argp = va_arg (args, char *);
1117 arg_len = strlen (argp);
1121 /* Represent an out-of-range file descriptor with an out-of-range
1122 integer value. We can do this because the only use of `%d' in
1123 the calls to cprintf is to output a file descriptor number for
1125 digit_arg = va_arg (args, int);
1128 sprintf (intbuf, "%u", (unsigned)-1);
1132 argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
1133 arg_len = strlen (argp);
1137 char_arg[0] = va_arg (args, int);
1143 programming_error ("cprintf: bad `%%' argument (%c)", c);
1148 if (argp && arg_len)
1150 the_printed_command_resize (arg_len + 1);
1151 FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
1152 command_string_index += arg_len;
1158 the_printed_command[command_string_index] = '\0';
1160 #endif /* HAVE_VARARGS_H */
1162 /* Ensure that there is enough space to stuff LENGTH characters into
1163 THE_PRINTED_COMMAND. */
1165 the_printed_command_resize (length)
1168 if (the_printed_command == 0)
1170 the_printed_command_size = (length + PRINTED_COMMAND_INITIAL_SIZE - 1) & ~(PRINTED_COMMAND_INITIAL_SIZE - 1);
1171 the_printed_command = (char *)xmalloc (the_printed_command_size);
1172 command_string_index = 0;
1174 else if ((command_string_index + length) >= the_printed_command_size)
1177 new = command_string_index + length + 1;
1179 /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */
1180 new = (new + PRINTED_COMMAND_GROW_SIZE - 1) & ~(PRINTED_COMMAND_GROW_SIZE - 1);
1181 the_printed_command_size = new;
1183 the_printed_command = (char *)xrealloc (the_printed_command, the_printed_command_size);
1187 #if defined (HAVE_VPRINTF)
1188 /* ``If vprintf is available, you may assume that vfprintf and vsprintf are
1189 also available.'' */
1192 #if defined (PREFER_STDARG)
1193 xprintf (const char *format, ...)
1195 xprintf (format, va_alist)
1202 #if defined (PREFER_STDARG)
1203 va_start (args, format);
1208 vfprintf (stdout, format, args);
1215 xprintf (format, arg1, arg2, arg3, arg4, arg5)
1218 printf (format, arg1, arg2, arg3, arg4, arg5);
1221 #endif /* !HAVE_VPRINTF */