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)
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 static void cprintf __P((char *, ...));
52 static void newline (), indent (), the_printed_command_resize ();
53 static void semicolon ();
54 static void xprintf ();
56 static void make_command_string_internal ();
57 static void command_print_word_list ();
58 static void print_case_clauses ();
59 static void print_redirection_list ();
60 static void print_redirection ();
62 static void print_for_command ();
63 #if defined (SELECT_COMMAND)
64 static void print_select_command ();
66 static void print_group_command ();
67 static void print_case_command ();
68 static void print_while_command ();
69 static void print_until_command ();
70 static void print_until_or_while ();
71 static void print_if_command ();
72 static void print_function_def ();
74 #define PRINTED_COMMAND_GROW_SIZE 1024
76 char *the_printed_command = (char *)NULL;
77 int the_printed_command_size = 0;
78 int command_string_index = 0;
80 /* Non-zero means the stuff being printed is inside of a function def. */
81 static int inside_function_def;
82 static int skip_this_indent;
84 /* The depth of the group commands that we are currently printing. This
85 includes the group command that is a function body. */
86 static int group_command_nesting;
88 /* Print COMMAND (a command tree) on standard output. */
90 print_command (command)
93 command_string_index = 0;
94 printf ("%s", make_command_string (command));
97 /* Make a string which is the printed representation of the command
98 tree in COMMAND. We return this string. However, the string is
99 not consed, so you have to do that yourself if you want it to
102 make_command_string (command)
105 command_string_index = 0;
106 make_command_string_internal (command);
107 return (the_printed_command);
110 /* The internal function. This is the real workhorse. */
112 make_command_string_internal (command)
119 if (skip_this_indent)
122 indent (indentation);
124 if (command->flags & CMD_WANT_SUBSHELL)
127 if (command->flags & CMD_TIME_PIPELINE)
130 if (command->flags & CMD_INVERT_RETURN)
133 switch (command->type)
136 print_for_command (command->value.For);
139 #if defined (SELECT_COMMAND)
141 print_select_command (command->value.Select);
146 print_case_command (command->value.Case);
150 print_while_command (command->value.While);
154 print_until_command (command->value.While);
158 print_if_command (command->value.If);
162 print_simple_command (command->value.Simple);
168 make_command_string_internal (command->value.Connection->first);
170 switch (command->value.Connection->connector)
175 char c = command->value.Connection->connector;
177 if (c != '&' || command->value.Connection->second)
187 if (command->value.Connection->second)
193 if (command->value.Connection->second)
200 if (inside_function_def)
205 if (command->value.Connection->second)
211 cprintf ("print_command: bad connector `%d'",
212 command->value.Connection->connector);
216 make_command_string_internal (command->value.Connection->second);
219 case cm_function_def:
220 print_function_def (command->value.Function_def);
224 print_group_command (command->value.Group);
228 programming_error ("print_command: bad command type `%d'", command->type);
232 if (command->flags & CMD_WANT_SUBSHELL)
235 if (command->redirects)
236 print_redirection_list (command->redirects);
241 _print_word_list (list, separator, pfunc)
248 for (w = list; w; w = w->next)
249 (*pfunc) ("%s%s", w->word->word, w->next ? separator : "");
253 print_word_list (list, separator)
257 _print_word_list (list, separator, xprintf);
260 /* A function to print the words of a simple command when set -x is on. */
262 xtrace_print_word_list (list)
268 fprintf (stderr, "%s", indirection_level_string ());
269 for (w = list; w; w = w->next)
272 if (t == 0 || *t == '\0')
273 fprintf (stderr, "''%s", w->next ? " " : "");
274 else if (contains_shell_metas (t))
275 fprintf (stderr, "'%s'%s", t, w->next ? " " : "");
277 fprintf (stderr, "%s%s", t, w->next ? " " : "");
279 fprintf (stderr, "\n");
283 command_print_word_list (list, separator)
287 _print_word_list (list, separator, cprintf);
291 print_for_command (for_command)
292 FOR_COM *for_command;
294 cprintf ("for %s in ", for_command->name->word);
295 command_print_word_list (for_command->map_list, " ");
298 indentation += indentation_amount;
299 make_command_string_internal (for_command->action);
301 indentation -= indentation_amount;
305 #if defined (SELECT_COMMAND)
307 print_select_command (select_command)
308 SELECT_COM *select_command;
310 cprintf ("select %s in ", select_command->name->word);
311 command_print_word_list (select_command->map_list, " ");
314 indentation += indentation_amount;
315 make_command_string_internal (select_command->action);
317 indentation -= indentation_amount;
320 #endif /* SELECT_COMMAND */
323 print_group_command (group_command)
324 GROUP_COM *group_command;
326 group_command_nesting++;
329 if (inside_function_def == 0)
333 /* This is a group command { ... } inside of a function
334 definition, and should be printed as a multiline group
335 command, using the current indentation. */
337 indentation += indentation_amount;
340 make_command_string_internal (group_command->command);
342 if (inside_function_def)
345 indentation -= indentation_amount;
346 indent (indentation);
356 group_command_nesting--;
360 print_case_command (case_command)
361 CASE_COM *case_command;
363 cprintf ("case %s in ", case_command->word->word);
364 if (case_command->clauses)
365 print_case_clauses (case_command->clauses);
370 print_case_clauses (clauses)
371 PATTERN_LIST *clauses;
373 indentation += indentation_amount;
377 command_print_word_list (clauses->patterns, " | ");
379 indentation += indentation_amount;
380 make_command_string_internal (clauses->action);
381 indentation -= indentation_amount;
383 clauses = clauses->next;
385 indentation -= indentation_amount;
389 print_while_command (while_command)
390 WHILE_COM *while_command;
392 print_until_or_while (while_command, "while");
396 print_until_command (while_command)
397 WHILE_COM *while_command;
399 print_until_or_while (while_command, "until");
403 print_until_or_while (while_command, which)
404 WHILE_COM *while_command;
407 cprintf ("%s ", which);
409 make_command_string_internal (while_command->test);
411 cprintf (" do\n"); /* was newline ("do\n"); */
412 indentation += indentation_amount;
413 make_command_string_internal (while_command->action);
414 indentation -= indentation_amount;
420 print_if_command (if_command)
425 make_command_string_internal (if_command->test);
428 indentation += indentation_amount;
429 make_command_string_internal (if_command->true_case);
430 indentation -= indentation_amount;
432 if (if_command->false_case)
436 indentation += indentation_amount;
437 make_command_string_internal (if_command->false_case);
438 indentation -= indentation_amount;
445 print_simple_command (simple_command)
446 SIMPLE_COM *simple_command;
448 command_print_word_list (simple_command->words, " ");
450 if (simple_command->redirects)
453 print_redirection_list (simple_command->redirects);
458 print_redirection_list (redirects)
463 print_redirection (redirects);
464 redirects = redirects->next;
471 print_redirection (redirect)
474 int kill_leading = 0;
475 int redirector = redirect->redirector;
476 WORD_DESC *redirectee = redirect->redirectee.filename;
477 int redir_fd = redirect->redirectee.dest;
479 switch (redirect->instruction)
481 case r_output_direction:
483 cprintf ("%d", redirector);
484 cprintf (">%s", redirectee->word);
487 case r_input_direction:
489 cprintf ("%d", redirector);
490 cprintf ("<%s", redirectee->word);
493 case r_inputa_direction: /* Redirection created by the shell. */
499 cprintf ("%d", redirector);
500 cprintf (">>%s", redirectee->word);
503 case r_deblank_reading_until:
506 case r_reading_until:
508 cprintf ("%d", redirector);
509 /* If the here document delimiter is quoted, single-quote it. */
510 if (redirect->redirectee.filename->flags & W_QUOTED)
513 x = single_quote (redirect->here_doc_eof);
514 cprintf ("<<%s%s\n", kill_leading? "-" : "", x);
518 cprintf ("<<%s%s\n", kill_leading? "-" : "", redirect->here_doc_eof);
520 redirect->redirectee.filename->word, redirect->here_doc_eof);
523 case r_duplicating_input:
524 cprintf ("%d<&%d", redirector, redir_fd);
527 case r_duplicating_output:
528 cprintf ("%d>&%d", redirector, redir_fd);
531 case r_duplicating_input_word:
532 cprintf ("%d<&%s", redirector, redirectee->word);
535 case r_duplicating_output_word:
536 cprintf ("%d>&%s", redirector, redirectee->word);
540 cprintf ("%d>&-", redirector);
544 cprintf (">&%s", redirectee->word);
549 cprintf ("%d", redirector);
550 cprintf ("<>%s", redirectee->word);
555 cprintf ("%d", redirector);
556 cprintf (">|%s", redirectee->word);
564 inside_function_def = 0;
569 print_function_def (func)
572 cprintf ("function %s () \n", func->name->word);
573 add_unwind_protect (reset_locals, 0);
575 indent (indentation);
578 inside_function_def++;
579 indentation += indentation_amount;
581 make_command_string_internal (func->command->type == cm_group
582 ? func->command->value.Group->command
585 remove_unwind_protect ();
586 indentation -= indentation_amount;
587 inside_function_def--;
592 /* Return the string representation of the named function.
593 NAME is the name of the function.
594 COMMAND is the function body. It should be a GROUP_COM.
595 MULTI_LINE is non-zero to pretty-print, or zero for all on one line.
598 named_function_string (name, command, multi_line)
604 int old_indent, old_amount;
606 old_indent = indentation;
607 old_amount = indentation_amount;
608 command_string_index = 0;
611 cprintf ("%s ", name);
618 indentation_amount = 0;
623 indentation += indentation_amount;
626 inside_function_def++;
628 cprintf (multi_line ? "{ \n" : "{ ");
630 make_command_string_internal (command->type == cm_group
631 ? command->value.Group->command
634 indentation = old_indent;
635 indentation_amount = old_amount;
636 inside_function_def--;
640 result = the_printed_command;
646 for (i = 0; result[i]; i++)
647 if (result[i] == '\n')
649 strcpy (result + i, result + i + 1);
653 if (result[2] == '\n') /* XXX -- experimental */
654 strcpy (result + 2, result + 3);
666 indent (indentation);
667 if (string && *string)
668 cprintf ("%s", string);
671 static char *indentation_string;
672 static int indentation_size;
680 RESIZE_MALLOCED_BUFFER (indentation_string, 0, amount, indentation_size, 16);
682 for (i = 0; amount > 0; amount--)
683 indentation_string[i++] = ' ';
684 indentation_string[i] = '\0';
685 cprintf (indentation_string);
691 if (command_string_index > 0 && the_printed_command[command_string_index - 1] == '&')
696 #if !defined (USE_VARARGS)
697 /* How to make the string. */
699 cprintf (format, arg1, arg2)
700 char *format, *arg1, *arg2;
703 char char_arg[2], *argp, *args[2];
704 int arg_len, c, arg_index;
706 args[arg_index = 0] = arg1;
709 arg_len = strlen (format);
710 the_printed_command_resize (arg_len + 1);
735 argp = (char *)args[arg_index++];
736 arg_len = strlen (argp);
740 argp = itos (pointer_to_int (args[arg_index]));
742 arg_len = strlen (argp);
747 char_arg[0] = pointer_to_int (args[arg_index]);
754 programming_error ("cprintf: bad `%%' argument (%c)", c);
759 the_printed_command_resize (arg_len + 1);
760 FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
761 command_string_index += arg_len;
767 the_printed_command[command_string_index] = '\0';
770 #else /* We have support for varargs. */
772 /* How to make the string. */
774 #if defined (PREFER_STDARG)
775 cprintf (char *control, ...)
777 cprintf (control, va_alist)
783 char char_arg[2], *argp;
784 int digit_arg, arg_len, c;
787 #if defined (PREFER_STDARG)
788 va_start (args, control);
793 arg_len = strlen (control);
794 the_printed_command_resize (arg_len + 1);
821 argp = va_arg (args, char *);
822 arg_len = strlen (argp);
826 digit_arg = va_arg (args, int);
827 argp = itos (digit_arg);
828 arg_len = strlen (argp);
833 char_arg[0] = va_arg (args, int);
839 programming_error ("cprintf: bad `%%' argument (%c)", c);
846 the_printed_command_resize (arg_len + 1);
847 FASTCOPY (argp, the_printed_command + command_string_index, arg_len);
848 command_string_index += arg_len;
854 the_printed_command[command_string_index] = '\0';
856 #endif /* HAVE_VARARGS_H */
858 /* Ensure that there is enough space to stuff LENGTH characters into
859 THE_PRINTED_COMMAND. */
861 the_printed_command_resize (length)
864 if (!the_printed_command)
866 the_printed_command_size = length + 1;
867 the_printed_command = xmalloc (the_printed_command_size);
868 command_string_index = 0;
870 else if ((command_string_index + length) >= the_printed_command_size)
873 new = command_string_index + length + 1;
874 new = new + 2 * PRINTED_COMMAND_GROW_SIZE - 1;
875 new -= new % PRINTED_COMMAND_GROW_SIZE;
876 the_printed_command_size = new;
877 the_printed_command = xrealloc (the_printed_command, the_printed_command_size);
881 #if defined (HAVE_VFPRINTF)
891 format = va_arg (args, char *);
892 vfprintf (stdout, format, args);
899 xprintf (format, arg1, arg2, arg3, arg4, arg5)
902 printf (format, arg1, arg2, arg3, arg4, arg5);
905 #endif /* !HAVE_VFPRINTF */