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)
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"
46 #if !defined (PRINTF_DECLARED)
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 static void cprintf
__P((char *, ...));
56 static void cprintf ();
59 static void newline (), indent (), the_printed_command_resize ();
60 static void semicolon ();
61 static void xprintf ();
63 static void make_command_string_internal ();
64 static void command_print_word_list ();
65 static void print_case_clauses ();
66 static void print_redirection_list ();
67 static void print_redirection ();
69 static void print_for_command ();
70 #if defined (SELECT_COMMAND)
71 static void print_select_command ();
73 static void print_group_command ();
74 static void print_case_command ();
75 static void print_while_command ();
76 static void print_until_command ();
77 static void print_until_or_while ();
78 static void print_if_command ();
79 static void print_function_def ();
80 #if defined (DPAREN_ARITHMETIC)
81 static void print_arith_command ();
83 #if defined (COND_COMMAND)
84 static void print_cond_node ();
85 static void print_cond_command ();
88 #define PRINTED_COMMAND_INITIAL_SIZE 64
89 #define PRINTED_COMMAND_GROW_SIZE 128
91 char *the_printed_command
= (char *)NULL
;
92 int the_printed_command_size
= 0;
93 int command_string_index
= 0;
95 /* Non-zero means the stuff being printed is inside of a function def. */
96 static int inside_function_def
;
97 static int skip_this_indent
;
98 static int was_heredoc
;
100 /* The depth of the group commands that we are currently printing. This
101 includes the group command that is a function body. */
102 static int group_command_nesting
;
104 /* Print COMMAND (a command tree) on standard output. */
106 print_command (command
)
109 command_string_index
= 0;
110 printf ("%s", make_command_string (command
));
113 /* Make a string which is the printed representation of the command
114 tree in COMMAND. We return this string. However, the string is
115 not consed, so you have to do that yourself if you want it to
118 make_command_string (command
)
121 command_string_index
= was_heredoc
= 0;
122 make_command_string_internal (command
);
123 return (the_printed_command
);
126 /* The internal function. This is the real workhorse. */
128 make_command_string_internal (command
)
135 if (skip_this_indent
)
138 indent (indentation
);
140 if (command
->flags
& CMD_WANT_SUBSHELL
)
143 if (command
->flags
& CMD_TIME_PIPELINE
)
146 if (command
->flags
& CMD_TIME_POSIX
)
150 if (command
->flags
& CMD_INVERT_RETURN
)
153 switch (command
->type
)
156 print_for_command (command
->value
.For
);
159 #if defined (SELECT_COMMAND)
161 print_select_command (command
->value
.Select
);
166 print_case_command (command
->value
.Case
);
170 print_while_command (command
->value
.While
);
174 print_until_command (command
->value
.While
);
178 print_if_command (command
->value
.If
);
181 #if defined (DPAREN_ARITHMETIC)
183 print_arith_command (command
->value
.Arith
);
187 #if defined (COND_COMMAND)
189 print_cond_command (command
->value
.Cond
);
194 print_simple_command (command
->value
.Simple
);
200 make_command_string_internal (command
->value
.Connection
->first
);
202 switch (command
->value
.Connection
->connector
)
207 char c
= command
->value
.Connection
->connector
;
209 if (c
!= '&' || command
->value
.Connection
->second
)
219 if (command
->value
.Connection
->second
)
225 if (command
->value
.Connection
->second
)
230 if (was_heredoc
== 0)
235 if (inside_function_def
)
240 if (command
->value
.Connection
->second
)
246 cprintf ("print_command: bad connector `%d'",
247 command
->value
.Connection
->connector
);
251 make_command_string_internal (command
->value
.Connection
->second
);
254 case cm_function_def
:
255 print_function_def (command
->value
.Function_def
);
259 print_group_command (command
->value
.Group
);
263 programming_error ("print_command: bad command type `%d'", command
->type
);
267 if (command
->flags
& CMD_WANT_SUBSHELL
)
270 if (command
->redirects
)
271 print_redirection_list (command
->redirects
);
276 _print_word_list (list
, separator
, pfunc
)
283 for (w
= list
; w
; w
= w
->next
)
284 (*pfunc
) ("%s%s", w
->word
->word
, w
->next
? separator
: "");
288 print_word_list (list
, separator
)
292 _print_word_list (list
, separator
, xprintf
);
295 /* A function to print the words of a simple command when set -x is on. */
297 xtrace_print_word_list (list
)
303 fprintf (stderr
, "%s", indirection_level_string ());
304 for (w
= list
; w
; w
= w
->next
)
307 if (t
== 0 || *t
== '\0')
308 fprintf (stderr
, "''%s", w
->next
? " " : "");
309 else if (contains_shell_metas (t
))
311 x
= single_quote (t
);
312 fprintf (stderr
, "%s%s", x
, w
->next
? " " : "");
316 fprintf (stderr
, "%s%s", t
, w
->next
? " " : "");
318 fprintf (stderr
, "\n");
322 command_print_word_list (list
, separator
)
326 _print_word_list (list
, separator
, cprintf
);
330 print_for_command (for_command
)
331 FOR_COM
*for_command
;
333 cprintf ("for %s in ", for_command
->name
->word
);
334 command_print_word_list (for_command
->map_list
, " ");
337 indentation
+= indentation_amount
;
338 make_command_string_internal (for_command
->action
);
340 indentation
-= indentation_amount
;
344 #if defined (SELECT_COMMAND)
346 print_select_command (select_command
)
347 SELECT_COM
*select_command
;
349 cprintf ("select %s in ", select_command
->name
->word
);
350 command_print_word_list (select_command
->map_list
, " ");
353 indentation
+= indentation_amount
;
354 make_command_string_internal (select_command
->action
);
356 indentation
-= indentation_amount
;
359 #endif /* SELECT_COMMAND */
362 print_group_command (group_command
)
363 GROUP_COM
*group_command
;
365 group_command_nesting
++;
368 if (inside_function_def
== 0)
372 /* This is a group command { ... } inside of a function
373 definition, and should be printed as a multiline group
374 command, using the current indentation. */
376 indentation
+= indentation_amount
;
379 make_command_string_internal (group_command
->command
);
381 if (inside_function_def
)
384 indentation
-= indentation_amount
;
385 indent (indentation
);
395 group_command_nesting
--;
399 print_case_command (case_command
)
400 CASE_COM
*case_command
;
402 cprintf ("case %s in ", case_command
->word
->word
);
403 if (case_command
->clauses
)
404 print_case_clauses (case_command
->clauses
);
409 print_case_clauses (clauses
)
410 PATTERN_LIST
*clauses
;
412 indentation
+= indentation_amount
;
416 command_print_word_list (clauses
->patterns
, " | ");
418 indentation
+= indentation_amount
;
419 make_command_string_internal (clauses
->action
);
420 indentation
-= indentation_amount
;
422 clauses
= clauses
->next
;
424 indentation
-= indentation_amount
;
428 print_while_command (while_command
)
429 WHILE_COM
*while_command
;
431 print_until_or_while (while_command
, "while");
435 print_until_command (while_command
)
436 WHILE_COM
*while_command
;
438 print_until_or_while (while_command
, "until");
442 print_until_or_while (while_command
, which
)
443 WHILE_COM
*while_command
;
446 cprintf ("%s ", which
);
448 make_command_string_internal (while_command
->test
);
450 cprintf (" do\n"); /* was newline ("do\n"); */
451 indentation
+= indentation_amount
;
452 make_command_string_internal (while_command
->action
);
453 indentation
-= indentation_amount
;
459 print_if_command (if_command
)
464 make_command_string_internal (if_command
->test
);
467 indentation
+= indentation_amount
;
468 make_command_string_internal (if_command
->true_case
);
469 indentation
-= indentation_amount
;
471 if (if_command
->false_case
)
475 indentation
+= indentation_amount
;
476 make_command_string_internal (if_command
->false_case
);
477 indentation
-= indentation_amount
;
483 #if defined (DPAREN_ARITHMETIC)
485 print_arith_command (arith_command
)
486 ARITH_COM
*arith_command
;
489 command_print_word_list (arith_command
->exp
, " ");
493 #if defined (COND_COMMAND)
495 print_cond_node (cond
)
498 if (cond
->flags
& CMD_INVERT_RETURN
)
501 if (cond
->type
== COND_EXPR
)
504 print_cond_node (cond
->left
);
507 else if (cond
->type
== COND_AND
)
509 print_cond_node (cond
->left
);
511 print_cond_node (cond
->right
);
513 else if (cond
->type
== COND_OR
)
515 print_cond_node (cond
->left
);
517 print_cond_node (cond
->right
);
519 else if (cond
->type
== COND_UNARY
)
521 cprintf (cond
->op
->word
);
523 print_cond_node (cond
->left
);
525 else if (cond
->type
== COND_BINARY
)
527 print_cond_node (cond
->left
);
529 cprintf (cond
->op
->word
);
531 print_cond_node (cond
->right
);
533 else if (cond
->type
== COND_TERM
)
535 cprintf (cond
->op
->word
); /* need to add quoting here */
540 print_cond_command (cond
)
544 print_cond_node (cond
);
549 debug_print_cond_command (cond
)
552 fprintf (stderr
, "DEBUG: ");
553 command_string_index
= 0;
554 print_cond_command (cond
);
555 fprintf (stderr
, "%s\n", the_printed_command
);
559 xtrace_print_cond_term (type
, invert
, op
, arg1
, arg2
)
564 command_string_index
= 0;
565 fprintf (stderr
, "%s", indirection_level_string ());
566 fprintf (stderr
, "[[ ");
568 fprintf (stderr
, "! ");
570 if (type
== COND_UNARY
)
572 fprintf (stderr
, "%s ", op
->word
);
573 fprintf (stderr
, "%s", (arg1
&& *arg1
) ? arg1
: "''");
575 else if (type
== COND_BINARY
)
577 fprintf (stderr
, "%s", (arg1
&& *arg1
) ? arg1
: "''");
578 fprintf (stderr
, " %s ", op
->word
);
579 fprintf (stderr
, "%s", (arg2
&& *arg2
) ? arg2
: "''");
582 fprintf (stderr
, " ]]\n");
584 #endif /* COND_COMMAND */
586 /* A function to print the words of an arithmetic command when set -x is on. */
588 xtrace_print_arith_cmd (list
)
593 fprintf (stderr
, "%s", indirection_level_string ());
594 fprintf (stderr
, "(( ");
595 for (w
= list
; w
; w
= w
->next
)
596 fprintf (stderr
, "%s%s", w
->word
->word
, w
->next
? " " : "");
597 fprintf (stderr
, " ))\n");
602 print_simple_command (simple_command
)
603 SIMPLE_COM
*simple_command
;
605 command_print_word_list (simple_command
->words
, " ");
607 if (simple_command
->redirects
)
610 print_redirection_list (simple_command
->redirects
);
615 print_redirection_list (redirects
)
618 REDIRECT
*heredocs
, *hdtail
, *newredir
;
620 heredocs
= (REDIRECT
*)NULL
;
626 /* Defer printing the here documents until we've printed the
627 rest of the redirections. */
628 if (redirects
->instruction
== r_reading_until
|| redirects
->instruction
== r_deblank_reading_until
)
630 newredir
= copy_redirect (redirects
);
631 newredir
->next
= (REDIRECT
*)NULL
;
634 hdtail
->next
= newredir
;
638 hdtail
= heredocs
= newredir
;
641 print_redirection (redirects
);
643 redirects
= redirects
->next
;
648 /* Now that we've printed all the other redirections (on one line),
649 print the here documents. */
653 for (hdtail
= heredocs
; hdtail
; hdtail
= hdtail
->next
)
655 print_redirection (hdtail
);
658 dispose_redirects (heredocs
);
664 print_redirection (redirect
)
667 int kill_leading
, redirector
, redir_fd
;
668 WORD_DESC
*redirectee
;
671 redirectee
= redirect
->redirectee
.filename
;
672 redirector
= redirect
->redirector
;
673 redir_fd
= redirect
->redirectee
.dest
;
675 switch (redirect
->instruction
)
677 case r_output_direction
:
679 cprintf ("%d", redirector
);
680 cprintf (">%s", redirectee
->word
);
683 case r_input_direction
:
685 cprintf ("%d", redirector
);
686 cprintf ("<%s", redirectee
->word
);
689 case r_inputa_direction
: /* Redirection created by the shell. */
695 cprintf ("%d", redirector
);
696 cprintf (">>%s", redirectee
->word
);
699 case r_deblank_reading_until
:
702 case r_reading_until
:
704 cprintf ("%d", redirector
);
705 /* If the here document delimiter is quoted, single-quote it. */
706 if (redirect
->redirectee
.filename
->flags
& W_QUOTED
)
709 x
= single_quote (redirect
->here_doc_eof
);
710 cprintf ("<<%s%s\n", kill_leading
? "-" : "", x
);
714 cprintf ("<<%s%s\n", kill_leading
? "-" : "", redirect
->here_doc_eof
);
716 redirect
->redirectee
.filename
->word
, redirect
->here_doc_eof
);
719 case r_duplicating_input
:
720 cprintf ("%d<&%d", redirector
, redir_fd
);
723 case r_duplicating_output
:
724 cprintf ("%d>&%d", redirector
, redir_fd
);
727 case r_duplicating_input_word
:
728 cprintf ("%d<&%s", redirector
, redirectee
->word
);
731 case r_duplicating_output_word
:
732 cprintf ("%d>&%s", redirector
, redirectee
->word
);
736 cprintf ("%d>&-", redirector
);
740 cprintf (">&%s", redirectee
->word
);
745 cprintf ("%d", redirector
);
746 cprintf ("<>%s", redirectee
->word
);
751 cprintf ("%d", redirector
);
752 cprintf (">|%s", redirectee
->word
);
760 inside_function_def
= 0;
765 print_function_def (func
)
768 cprintf ("function %s () \n", func
->name
->word
);
769 add_unwind_protect (reset_locals
, 0);
771 indent (indentation
);
774 inside_function_def
++;
775 indentation
+= indentation_amount
;
777 make_command_string_internal (func
->command
->type
== cm_group
778 ? func
->command
->value
.Group
->command
781 remove_unwind_protect ();
782 indentation
-= indentation_amount
;
783 inside_function_def
--;
788 /* Return the string representation of the named function.
789 NAME is the name of the function.
790 COMMAND is the function body. It should be a GROUP_COM.
791 MULTI_LINE is non-zero to pretty-print, or zero for all on one line.
794 named_function_string (name
, command
, multi_line
)
800 int old_indent
, old_amount
;
802 old_indent
= indentation
;
803 old_amount
= indentation_amount
;
804 command_string_index
= was_heredoc
= 0;
807 cprintf ("%s ", name
);
814 indentation_amount
= 0;
819 indentation
+= indentation_amount
;
822 inside_function_def
++;
824 cprintf (multi_line
? "{ \n" : "{ ");
826 make_command_string_internal (command
->type
== cm_group
827 ? command
->value
.Group
->command
830 indentation
= old_indent
;
831 indentation_amount
= old_amount
;
832 inside_function_def
--;
836 result
= the_printed_command
;
842 for (i
= 0; result
[i
]; i
++)
843 if (result
[i
] == '\n')
845 strcpy (result
+ i
, result
+ i
+ 1);
849 if (result
[2] == '\n') /* XXX -- experimental */
850 strcpy (result
+ 2, result
+ 3);
862 indent (indentation
);
863 if (string
&& *string
)
864 cprintf ("%s", string
);
867 static char *indentation_string
;
868 static int indentation_size
;
876 RESIZE_MALLOCED_BUFFER (indentation_string
, 0, amount
, indentation_size
, 16);
878 for (i
= 0; amount
> 0; amount
--)
879 indentation_string
[i
++] = ' ';
880 indentation_string
[i
] = '\0';
881 cprintf (indentation_string
);
887 if (command_string_index
> 0 && the_printed_command
[command_string_index
- 1] == '&')
892 #if !defined (USE_VARARGS)
893 /* How to make the string. */
895 cprintf (format
, arg1
, arg2
)
896 char *format
, *arg1
, *arg2
;
899 char char_arg
[2], *argp
, *args
[2];
900 int arg_len
, c
, arg_index
;
902 args
[arg_index
= 0] = arg1
;
905 arg_len
= strlen (format
);
906 the_printed_command_resize (arg_len
+ 1);
931 argp
= (char *)args
[arg_index
++];
932 arg_len
= strlen (argp
);
936 argp
= itos (pointer_to_int (args
[arg_index
]));
938 arg_len
= strlen (argp
);
943 char_arg
[0] = pointer_to_int (args
[arg_index
]);
950 programming_error ("cprintf: bad `%%' argument (%c)", c
);
955 the_printed_command_resize (arg_len
+ 1);
956 FASTCOPY (argp
, the_printed_command
+ command_string_index
, arg_len
);
957 command_string_index
+= arg_len
;
963 the_printed_command
[command_string_index
] = '\0';
966 #else /* We have support for varargs. */
968 /* How to make the string. */
970 #if defined (PREFER_STDARG)
971 cprintf (char *control
, ...)
973 cprintf (control
, va_alist
)
979 char char_arg
[2], *argp
;
980 int digit_arg
, arg_len
, c
;
983 #if defined (PREFER_STDARG)
984 va_start (args
, control
);
989 arg_len
= strlen (control
);
990 the_printed_command_resize (arg_len
+ 1);
1000 if (c
!= '%' || !*s
)
1017 argp
= va_arg (args
, char *);
1018 arg_len
= strlen (argp
);
1022 digit_arg
= va_arg (args
, int);
1023 argp
= itos (digit_arg
);
1024 arg_len
= strlen (argp
);
1029 char_arg
[0] = va_arg (args
, int);
1035 programming_error ("cprintf: bad `%%' argument (%c)", c
);
1040 if (argp
&& arg_len
)
1042 the_printed_command_resize (arg_len
+ 1);
1043 FASTCOPY (argp
, the_printed_command
+ command_string_index
, arg_len
);
1044 command_string_index
+= arg_len
;
1050 the_printed_command
[command_string_index
] = '\0';
1052 #endif /* HAVE_VARARGS_H */
1054 /* Ensure that there is enough space to stuff LENGTH characters into
1055 THE_PRINTED_COMMAND. */
1057 the_printed_command_resize (length
)
1060 if (the_printed_command
== 0)
1062 the_printed_command_size
= (length
+ PRINTED_COMMAND_INITIAL_SIZE
- 1) & ~(PRINTED_COMMAND_INITIAL_SIZE
- 1);
1063 the_printed_command
= xmalloc (the_printed_command_size
);
1064 command_string_index
= 0;
1066 else if ((command_string_index
+ length
) >= the_printed_command_size
)
1069 new = command_string_index
+ length
+ 1;
1071 /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */
1072 new = (new + PRINTED_COMMAND_GROW_SIZE
- 1) & ~(PRINTED_COMMAND_GROW_SIZE
- 1);
1074 new = new + 2 * PRINTED_COMMAND_GROW_SIZE
- 1;
1075 new -= new % PRINTED_COMMAND_GROW_SIZE
;
1077 the_printed_command_size
= new;
1078 the_printed_command
= xrealloc (the_printed_command
, the_printed_command_size
);
1082 #if defined (HAVE_VFPRINTF)
1085 #if defined (PREFER_STDARG)
1086 xprintf (const char *format
, ...)
1088 xprintf (format
, va_alist
)
1095 #if defined (PREFER_STDARG)
1096 va_start (args
, format
);
1101 vfprintf (stdout
, format
, args
);
1108 xprintf (format
, arg1
, arg2
, arg3
, arg4
, arg5
)
1111 printf (format
, arg1
, arg2
, arg3
, arg4
, arg5
);
1114 #endif /* !HAVE_VFPRINTF */