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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #if defined (HAVE_UNISTD_H)
28 #if defined (PREFER_STDARG)
31 # if defined (PREFER_VARARGS)
39 #include <y.tab.h> /* use <...> so we pick it up from the build directory */
41 #include "builtins/common.h"
43 #if !defined (PRINTF_DECLARED)
44 extern int printf __P((const char *, ...)); /* Yuck. Double yuck. */
47 static int indentation;
48 static int indentation_amount = 4;
50 #if defined (PREFER_STDARG)
51 static void cprintf __P((char *, ...));
53 static void cprintf ();
56 static void newline (), indent (), the_printed_command_resize ();
57 static void semicolon ();
58 static void xprintf ();
60 static void make_command_string_internal ();
61 static void command_print_word_list ();
62 static void print_case_clauses ();
63 static void print_redirection_list ();
64 static void print_redirection ();
66 static void print_for_command ();
67 #if defined (SELECT_COMMAND)
68 static void print_select_command ();
70 static void print_group_command ();
71 static void print_case_command ();
72 static void print_while_command ();
73 static void print_until_command ();
74 static void print_until_or_while ();
75 static void print_if_command ();
76 static void print_function_def ();
78 #define PRINTED_COMMAND_INITIAL_SIZE 64
79 #define PRINTED_COMMAND_GROW_SIZE 128
81 char *the_printed_command = (char *)NULL;
82 int the_printed_command_size = 0;
83 int command_string_index = 0;
85 /* Non-zero means the stuff being printed is inside of a function def. */
86 static int inside_function_def;
87 static int skip_this_indent;
88 static int was_heredoc;
90 /* The depth of the group commands that we are currently printing. This
91 includes the group command that is a function body. */
92 static int group_command_nesting;
94 /* Print COMMAND (a command tree) on standard output. */
96 print_command (command)
99 command_string_index = 0;
100 printf ("%s", make_command_string (command));
103 /* Make a string which is the printed representation of the command
104 tree in COMMAND. We return this string. However, the string is
105 not consed, so you have to do that yourself if you want it to
108 make_command_string (command)
111 command_string_index = was_heredoc = 0;
112 make_command_string_internal (command);
113 return (the_printed_command);
116 /* The internal function. This is the real workhorse. */
118 make_command_string_internal (command)
125 if (skip_this_indent)
128 indent (indentation);
130 if (command->flags & CMD_WANT_SUBSHELL)
133 if (command->flags & CMD_TIME_PIPELINE)
136 if (command->flags & CMD_TIME_POSIX)
140 if (command->flags & CMD_INVERT_RETURN)
143 switch (command->type)
146 print_for_command (command->value.For);
149 #if defined (SELECT_COMMAND)
151 print_select_command (command->value.Select);
156 print_case_command (command->value.Case);
160 print_while_command (command->value.While);
164 print_until_command (command->value.While);
168 print_if_command (command->value.If);
172 print_simple_command (command->value.Simple);
178 make_command_string_internal (command->value.Connection->first);
180 switch (command->value.Connection->connector)
185 char c = command->value.Connection->connector;
187 if (c != '&' || command->value.Connection->second)
197 if (command->value.Connection->second)
203 if (command->value.Connection->second)
208 if (was_heredoc == 0)
213 if (inside_function_def)
218 if (command->value.Connection->second)
224 cprintf ("print_command: bad connector `%d'",
225 command->value.Connection->connector);
229 make_command_string_internal (command->value.Connection->second);
232 case cm_function_def:
233 print_function_def (command->value.Function_def);
237 print_group_command (command->value.Group);
241 programming_error ("print_command: bad command type `%d'", command->type);
245 if (command->flags & CMD_WANT_SUBSHELL)
248 if (command->redirects)
249 print_redirection_list (command->redirects);
254 _print_word_list (list, separator, pfunc)
261 for (w = list; w; w = w->next)
262 (*pfunc) ("%s%s", w->word->word, w->next ? separator : "");
266 print_word_list (list, separator)
270 _print_word_list (list, separator, xprintf);
273 /* A function to print the words of a simple command when set -x is on. */
275 xtrace_print_word_list (list)
281 fprintf (stderr, "%s", indirection_level_string ());
282 for (w = list; w; w = w->next)
285 if (t == 0 || *t == '\0')
286 fprintf (stderr, "''%s", w->next ? " " : "");
287 else if (contains_shell_metas (t))
289 x = single_quote (t);
290 fprintf (stderr, "%s%s", x, w->next ? " " : "");
294 fprintf (stderr, "%s%s", t, w->next ? " " : "");
296 fprintf (stderr, "\n");
300 command_print_word_list (list, separator)
304 _print_word_list (list, separator, cprintf);
308 print_for_command (for_command)
309 FOR_COM *for_command;
311 cprintf ("for %s in ", for_command->name->word);
312 command_print_word_list (for_command->map_list, " ");
315 indentation += indentation_amount;
316 make_command_string_internal (for_command->action);
318 indentation -= indentation_amount;
322 #if defined (SELECT_COMMAND)
324 print_select_command (select_command)
325 SELECT_COM *select_command;
327 cprintf ("select %s in ", select_command->name->word);
328 command_print_word_list (select_command->map_list, " ");
331 indentation += indentation_amount;
332 make_command_string_internal (select_command->action);
334 indentation -= indentation_amount;
337 #endif /* SELECT_COMMAND */
340 print_group_command (group_command)
341 GROUP_COM *group_command;
343 group_command_nesting++;
346 if (inside_function_def == 0)
350 /* This is a group command { ... } inside of a function
351 definition, and should be printed as a multiline group
352 command, using the current indentation. */
354 indentation += indentation_amount;
357 make_command_string_internal (group_command->command);
359 if (inside_function_def)
362 indentation -= indentation_amount;
363 indent (indentation);
373 group_command_nesting--;
377 print_case_command (case_command)
378 CASE_COM *case_command;
380 cprintf ("case %s in ", case_command->word->word);
381 if (case_command->clauses)
382 print_case_clauses (case_command->clauses);
387 print_case_clauses (clauses)
388 PATTERN_LIST *clauses;
390 indentation += indentation_amount;
394 command_print_word_list (clauses->patterns, " | ");
396 indentation += indentation_amount;
397 make_command_string_internal (clauses->action);
398 indentation -= indentation_amount;
400 clauses = clauses->next;
402 indentation -= indentation_amount;
406 print_while_command (while_command)
407 WHILE_COM *while_command;
409 print_until_or_while (while_command, "while");
413 print_until_command (while_command)
414 WHILE_COM *while_command;
416 print_until_or_while (while_command, "until");
420 print_until_or_while (while_command, which)
421 WHILE_COM *while_command;
424 cprintf ("%s ", which);
426 make_command_string_internal (while_command->test);
428 cprintf (" do\n"); /* was newline ("do\n"); */
429 indentation += indentation_amount;
430 make_command_string_internal (while_command->action);
431 indentation -= indentation_amount;
437 print_if_command (if_command)
442 make_command_string_internal (if_command->test);
445 indentation += indentation_amount;
446 make_command_string_internal (if_command->true_case);
447 indentation -= indentation_amount;
449 if (if_command->false_case)
453 indentation += indentation_amount;
454 make_command_string_internal (if_command->false_case);
455 indentation -= indentation_amount;
462 print_simple_command (simple_command)
463 SIMPLE_COM *simple_command;
465 command_print_word_list (simple_command->words, " ");
467 if (simple_command->redirects)
470 print_redirection_list (simple_command->redirects);
475 print_redirection_list (redirects)
478 REDIRECT *heredocs, *hdtail, *newredir;
480 heredocs = (REDIRECT *)NULL;
486 /* Defer printing the here documents until we've printed the
487 rest of the redirections. */
488 if (redirects->instruction == r_reading_until || redirects->instruction == r_deblank_reading_until)
490 newredir = copy_redirect (redirects);
491 newredir->next = (REDIRECT *)NULL;
494 hdtail->next = newredir;
498 hdtail = heredocs = newredir;
501 print_redirection (redirects);
503 redirects = redirects->next;
508 /* Now that we've printed all the other redirections (on one line),
509 print the here documents. */
513 for (hdtail = heredocs; hdtail; hdtail = hdtail->next)
515 print_redirection (hdtail);
518 dispose_redirects (heredocs);
524 print_redirection (redirect)
527 int kill_leading, redirector, redir_fd;
528 WORD_DESC *redirectee;
531 redirectee = redirect->redirectee.filename;
532 redirector = redirect->redirector;
533 redir_fd = redirect->redirectee.dest;
535 switch (redirect->instruction)
537 case r_output_direction:
539 cprintf ("%d", redirector);
540 cprintf (">%s", redirectee->word);
543 case r_input_direction:
545 cprintf ("%d", redirector);
546 cprintf ("<%s", redirectee->word);
549 case r_inputa_direction: /* Redirection created by the shell. */
555 cprintf ("%d", redirector);
556 cprintf (">>%s", redirectee->word);
559 case r_deblank_reading_until:
562 case r_reading_until:
564 cprintf ("%d", redirector);
565 /* If the here document delimiter is quoted, single-quote it. */
566 if (redirect->redirectee.filename->flags & W_QUOTED)
569 x = single_quote (redirect->here_doc_eof);
570 cprintf ("<<%s%s\n", kill_leading? "-" : "", x);
574 cprintf ("<<%s%s\n", kill_leading? "-" : "", redirect->here_doc_eof);
576 redirect->redirectee.filename->word, redirect->here_doc_eof);
579 case r_duplicating_input:
580 cprintf ("%d<&%d", redirector, redir_fd);
583 case r_duplicating_output:
584 cprintf ("%d>&%d", redirector, redir_fd);
587 case r_duplicating_input_word:
588 cprintf ("%d<&%s", redirector, redirectee->word);
591 case r_duplicating_output_word:
592 cprintf ("%d>&%s", redirector, redirectee->word);
596 cprintf ("%d>&-", redirector);
600 cprintf (">&%s", redirectee->word);
605 cprintf ("%d", redirector);
606 cprintf ("<>%s", redirectee->word);
611 cprintf ("%d", redirector);
612 cprintf (">|%s", redirectee->word);
620 inside_function_def = 0;
625 print_function_def (func)
628 cprintf ("function %s () \n", func->name->word);
629 add_unwind_protect (reset_locals, 0);
631 indent (indentation);
634 inside_function_def++;
635 indentation += indentation_amount;
637 make_command_string_internal (func->command->type == cm_group
638 ? func->command->value.Group->command
641 remove_unwind_protect ();
642 indentation -= indentation_amount;
643 inside_function_def--;
648 /* Return the string representation of the named function.
649 NAME is the name of the function.
650 COMMAND is the function body. It should be a GROUP_COM.
651 MULTI_LINE is non-zero to pretty-print, or zero for all on one line.
654 named_function_string (name, command, multi_line)
660 int old_indent, old_amount;
662 old_indent = indentation;
663 old_amount = indentation_amount;
664 command_string_index = was_heredoc = 0;
667 cprintf ("%s ", name);
674 indentation_amount = 0;
679 indentation += indentation_amount;
682 inside_function_def++;
684 cprintf (multi_line ? "{ \n" : "{ ");
686 make_command_string_internal (command->type == cm_group
687 ? command->value.Group->command
690 indentation = old_indent;
691 indentation_amount = old_amount;
692 inside_function_def--;
696 result = the_printed_command;
702 for (i = 0; result[i]; i++)
703 if (result[i] == '\n')
705 strcpy (result + i, result + i + 1);
709 if (result[2] == '\n') /* XXX -- experimental */
710 strcpy (result + 2, result + 3);
722 indent (indentation);
723 if (string && *string)
724 cprintf ("%s", string);
727 static char *indentation_string;
728 static int indentation_size;
736 RESIZE_MALLOCED_BUFFER (indentation_string, 0, amount, indentation_size, 16);
738 for (i = 0; amount > 0; amount--)
739 indentation_string[i++] = ' ';
740 indentation_string[i] = '\0';
741 cprintf (indentation_string);
747 if (command_string_index > 0 && the_printed_command[command_string_index - 1] == '&')
752 #if !defined (USE_VARARGS)
753 /* How to make the string. */
755 cprintf (format, arg1, arg2)
756 char *format, *arg1, *arg2;
759 char char_arg[2], *argp, *args[2];
760 int arg_len, c, arg_index;
762 args[arg_index = 0] = arg1;
765 arg_len = strlen (format);
766 the_printed_command_resize (arg_len + 1);
791 argp = (char *)args[arg_index++];
792 arg_len = strlen (argp);
796 argp = itos (pointer_to_int (args[arg_index]));
798 arg_len = strlen (argp);
803 char_arg[0] = pointer_to_int (args[arg_index]);
810 programming_error ("cprintf: bad `%%' argument (%c)", c);
815 the_printed_command_resize (arg_len + 1);
816 FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
817 command_string_index += arg_len;
823 the_printed_command[command_string_index] = '\0';
826 #else /* We have support for varargs. */
828 /* How to make the string. */
830 #if defined (PREFER_STDARG)
831 cprintf (char *control, ...)
833 cprintf (control, va_alist)
839 char char_arg[2], *argp;
840 int digit_arg, arg_len, c;
843 #if defined (PREFER_STDARG)
844 va_start (args, control);
849 arg_len = strlen (control);
850 the_printed_command_resize (arg_len + 1);
877 argp = va_arg (args, char *);
878 arg_len = strlen (argp);
882 digit_arg = va_arg (args, int);
883 argp = itos (digit_arg);
884 arg_len = strlen (argp);
889 char_arg[0] = va_arg (args, int);
895 programming_error ("cprintf: bad `%%' argument (%c)", c);
902 the_printed_command_resize (arg_len + 1);
903 FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
904 command_string_index += arg_len;
910 the_printed_command[command_string_index] = '\0';
912 #endif /* HAVE_VARARGS_H */
914 /* Ensure that there is enough space to stuff LENGTH characters into
915 THE_PRINTED_COMMAND. */
917 the_printed_command_resize (length)
920 if (the_printed_command == 0)
922 the_printed_command_size = (length + PRINTED_COMMAND_INITIAL_SIZE - 1) & ~(PRINTED_COMMAND_INITIAL_SIZE - 1);
923 the_printed_command = xmalloc (the_printed_command_size);
924 command_string_index = 0;
926 else if ((command_string_index + length) >= the_printed_command_size)
929 new = command_string_index + length + 1;
931 /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */
932 new = (new + PRINTED_COMMAND_GROW_SIZE - 1) & ~(PRINTED_COMMAND_GROW_SIZE - 1);
934 new = new + 2 * PRINTED_COMMAND_GROW_SIZE - 1;
935 new -= new % PRINTED_COMMAND_GROW_SIZE;
937 the_printed_command_size = new;
938 the_printed_command = xrealloc (the_printed_command, the_printed_command_size);
942 #if defined (HAVE_VFPRINTF)
945 #if defined (PREFER_STDARG)
946 xprintf (const char *format, ...)
948 xprintf (format, va_alist)
955 #if defined (PREFER_STDARG)
956 va_start (args, format);
961 vfprintf (stdout, format, args);
968 xprintf (format, arg1, arg2, arg3, arg4, arg5)
971 printf (format, arg1, arg2, arg3, arg4, arg5);
974 #endif /* !HAVE_VFPRINTF */