1 /* print_command -- A way to make readable commands from a command tree. */
3 /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #if defined (HAVE_UNISTD_H)
27 # include <sys/types.h>
32 #if defined (PREFER_STDARG)
43 #include <y.tab.h> /* use <...> so we pick it up from the build directory */
47 #include "builtins/common.h"
50 extern int printf
__P((const char *, ...)); /* Yuck. Double yuck. */
53 extern int indirection_level
;
55 static int indentation
;
56 static int indentation_amount
= 4;
58 #if defined (PREFER_STDARG)
59 typedef void PFUNC
__P((const char *, ...));
61 static void cprintf
__P((const char *, ...)) __attribute__((__format__ (printf
, 1, 2)));
62 static void xprintf
__P((const char *, ...)) __attribute__((__format__ (printf
, 1, 2)));
64 #define PFUNC VFunction
65 static void cprintf ();
66 static void xprintf ();
69 static void reset_locals
__P((void));
70 static void newline
__P((char *));
71 static void indent
__P((int));
72 static void semicolon
__P((void));
73 static void the_printed_command_resize
__P((int));
75 static void make_command_string_internal
__P((COMMAND
*));
76 static void _print_word_list
__P((WORD_LIST
*, char *, PFUNC
*));
77 static void command_print_word_list
__P((WORD_LIST
*, char *));
78 static void print_case_clauses
__P((PATTERN_LIST
*));
79 static void print_redirection_list
__P((REDIRECT
*));
80 static void print_redirection
__P((REDIRECT
*));
81 static void print_heredoc_header
__P((REDIRECT
*));
82 static void print_heredoc_body
__P((REDIRECT
*));
83 static void print_heredocs
__P((REDIRECT
*));
84 static void print_deferred_heredocs
__P((const char *));
86 static void print_for_command
__P((FOR_COM
*));
87 #if defined (ARITH_FOR_COMMAND)
88 static void print_arith_for_command
__P((ARITH_FOR_COM
*));
90 #if defined (SELECT_COMMAND)
91 static void print_select_command
__P((SELECT_COM
*));
93 static void print_group_command
__P((GROUP_COM
*));
94 static void print_case_command
__P((CASE_COM
*));
95 static void print_while_command
__P((WHILE_COM
*));
96 static void print_until_command
__P((WHILE_COM
*));
97 static void print_until_or_while
__P((WHILE_COM
*, char *));
98 static void print_if_command
__P((IF_COM
*));
99 #if defined (COND_COMMAND)
100 static void print_cond_node
__P((COND_COM
*));
102 static void print_function_def
__P((FUNCTION_DEF
*));
104 #define PRINTED_COMMAND_INITIAL_SIZE 64
105 #define PRINTED_COMMAND_GROW_SIZE 128
107 char *the_printed_command
= (char *)NULL
;
108 int the_printed_command_size
= 0;
109 int command_string_index
= 0;
111 /* Non-zero means the stuff being printed is inside of a function def. */
112 static int inside_function_def
;
113 static int skip_this_indent
;
114 static int was_heredoc
;
115 static int printing_connection
;
116 static REDIRECT
*deferred_heredocs
;
118 /* The depth of the group commands that we are currently printing. This
119 includes the group command that is a function body. */
120 static int group_command_nesting
;
122 /* A buffer to indicate the indirection level (PS4) when set -x is enabled. */
123 static char indirection_string
[100];
125 /* Print COMMAND (a command tree) on standard output. */
127 print_command (command
)
130 command_string_index
= 0;
131 printf ("%s", make_command_string (command
));
134 /* Make a string which is the printed representation of the command
135 tree in COMMAND. We return this string. However, the string is
136 not consed, so you have to do that yourself if you want it to
139 make_command_string (command
)
142 command_string_index
= was_heredoc
= 0;
143 deferred_heredocs
= 0;
144 make_command_string_internal (command
);
145 return (the_printed_command
);
148 /* The internal function. This is the real workhorse. */
150 make_command_string_internal (command
)
159 if (skip_this_indent
)
162 indent (indentation
);
164 if (command
->flags
& CMD_TIME_PIPELINE
)
167 if (command
->flags
& CMD_TIME_POSIX
)
171 if (command
->flags
& CMD_INVERT_RETURN
)
174 switch (command
->type
)
177 print_for_command (command
->value
.For
);
180 #if defined (ARITH_FOR_COMMAND)
182 print_arith_for_command (command
->value
.ArithFor
);
186 #if defined (SELECT_COMMAND)
188 print_select_command (command
->value
.Select
);
193 print_case_command (command
->value
.Case
);
197 print_while_command (command
->value
.While
);
201 print_until_command (command
->value
.While
);
205 print_if_command (command
->value
.If
);
208 #if defined (DPAREN_ARITHMETIC)
210 print_arith_command (command
->value
.Arith
->exp
);
214 #if defined (COND_COMMAND)
216 print_cond_command (command
->value
.Cond
);
221 print_simple_command (command
->value
.Simple
);
227 printing_connection
++;
228 make_command_string_internal (command
->value
.Connection
->first
);
230 switch (command
->value
.Connection
->connector
)
235 char c
= command
->value
.Connection
->connector
;
241 print_deferred_heredocs (s
);
243 if (c
!= '&' || command
->value
.Connection
->second
)
252 print_deferred_heredocs (" && ");
253 if (command
->value
.Connection
->second
)
258 print_deferred_heredocs (" || ");
259 if (command
->value
.Connection
->second
)
265 if (was_heredoc
== 0)
270 if (deferred_heredocs
== 0)
273 print_deferred_heredocs (";");
276 if (inside_function_def
)
281 if (command
->value
.Connection
->second
)
287 cprintf (_("print_command: bad connector `%d'"),
288 command
->value
.Connection
->connector
);
292 make_command_string_internal (command
->value
.Connection
->second
);
293 printing_connection
--;
296 case cm_function_def
:
297 print_function_def (command
->value
.Function_def
);
301 print_group_command (command
->value
.Group
);
307 make_command_string_internal (command
->value
.Subshell
->command
);
312 cprintf ("coproc %s ", command
->value
.Coproc
->name
);
314 make_command_string_internal (command
->value
.Coproc
->command
);
318 command_error ("print_command", CMDERR_BADTYPE
, command
->type
, 0);
323 if (command
->redirects
)
326 print_redirection_list (command
->redirects
);
332 _print_word_list (list
, separator
, pfunc
)
339 for (w
= list
; w
; w
= w
->next
)
340 (*pfunc
) ("%s%s", w
->word
->word
, w
->next
? separator
: "");
344 print_word_list (list
, separator
)
348 _print_word_list (list
, separator
, xprintf
);
351 /* Return a string denoting what our indirection level is. */
354 indirection_level_string ()
358 char ps4_firstc
[MB_LEN_MAX
+1];
359 int ps4_firstc_len
, ps4_len
;
361 indirection_string
[0] = '\0';
362 ps4
= get_string_value ("PS4");
364 if (ps4
== 0 || *ps4
== '\0')
365 return (indirection_string
);
367 change_flag ('x', FLAG_OFF
);
368 ps4
= decode_prompt_string (ps4
);
369 change_flag ('x', FLAG_ON
);
371 if (ps4
== 0 || *ps4
== '\0')
372 return (indirection_string
);
374 #if defined (HANDLE_MULTIBYTE)
375 ps4_len
= strnlen (ps4
, MB_CUR_MAX
);
376 ps4_firstc_len
= MBLEN (ps4
, ps4_len
);
377 if (ps4_firstc_len
== 1 || ps4_firstc_len
== 0 || MB_INVALIDCH (ps4_firstc_len
))
379 ps4_firstc
[0] = ps4
[0];
380 ps4_firstc
[ps4_firstc_len
= 1] = '\0';
383 memcpy (ps4_firstc
, ps4
, ps4_firstc_len
);
385 ps4_firstc
[0] = ps4
[0];
386 ps4_firstc
[ps4_firstc_len
= 1] = '\0';
389 for (i
= j
= 0; ps4_firstc
[0] && j
< indirection_level
&& i
< 99; i
+= ps4_firstc_len
, j
++)
391 if (ps4_firstc_len
== 1)
392 indirection_string
[i
] = ps4_firstc
[0];
394 memcpy (indirection_string
+i
, ps4_firstc
, ps4_firstc_len
);
397 for (j
= ps4_firstc_len
; *ps4
&& ps4
[j
] && i
< 99; i
++, j
++)
398 indirection_string
[i
] = ps4
[j
];
400 indirection_string
[i
] = '\0';
402 return (indirection_string
);
406 xtrace_print_assignment (name
, value
, assign_list
, xflags
)
408 int assign_list
, xflags
;
413 fprintf (stderr
, "%s", indirection_level_string ());
415 /* VALUE should not be NULL when this is called. */
416 if (*value
== '\0' || assign_list
)
418 else if (sh_contains_shell_metas (value
))
419 nval
= sh_single_quote (value
);
420 else if (ansic_shouldquote (value
))
421 nval
= ansic_quote (value
, 0, (int *)0);
426 fprintf (stderr
, "%s=(%s)\n", name
, nval
);
428 fprintf (stderr
, "%s=%s\n", name
, nval
);
436 /* A function to print the words of a simple command when set -x is on. */
438 xtrace_print_word_list (list
, xtflags
)
446 fprintf (stderr
, "%s", indirection_level_string ());
448 for (w
= list
; w
; w
= w
->next
)
451 if (t
== 0 || *t
== '\0')
452 fprintf (stderr
, "''%s", w
->next
? " " : "");
453 else if (sh_contains_shell_metas (t
))
455 x
= sh_single_quote (t
);
456 fprintf (stderr
, "%s%s", x
, w
->next
? " " : "");
459 else if (ansic_shouldquote (t
))
461 x
= ansic_quote (t
, 0, (int *)0);
462 fprintf (stderr
, "%s%s", x
, w
->next
? " " : "");
466 fprintf (stderr
, "%s%s", t
, w
->next
? " " : "");
468 fprintf (stderr
, "\n");
472 command_print_word_list (list
, separator
)
476 _print_word_list (list
, separator
, cprintf
);
480 print_for_command_head (for_command
)
481 FOR_COM
*for_command
;
483 cprintf ("for %s in ", for_command
->name
->word
);
484 command_print_word_list (for_command
->map_list
, " ");
488 xtrace_print_for_command_head (for_command
)
489 FOR_COM
*for_command
;
491 fprintf (stderr
, "%s", indirection_level_string ());
492 fprintf (stderr
, "for %s in ", for_command
->name
->word
);
493 xtrace_print_word_list (for_command
->map_list
, 0);
497 print_for_command (for_command
)
498 FOR_COM
*for_command
;
500 print_for_command_head (for_command
);
504 indentation
+= indentation_amount
;
505 make_command_string_internal (for_command
->action
);
507 indentation
-= indentation_amount
;
511 #if defined (ARITH_FOR_COMMAND)
513 print_arith_for_command (arith_for_command
)
514 ARITH_FOR_COM
*arith_for_command
;
517 command_print_word_list (arith_for_command
->init
, " ");
519 command_print_word_list (arith_for_command
->test
, " ");
521 command_print_word_list (arith_for_command
->step
, " ");
524 indentation
+= indentation_amount
;
525 make_command_string_internal (arith_for_command
->action
);
527 indentation
-= indentation_amount
;
530 #endif /* ARITH_FOR_COMMAND */
532 #if defined (SELECT_COMMAND)
534 print_select_command_head (select_command
)
535 SELECT_COM
*select_command
;
537 cprintf ("select %s in ", select_command
->name
->word
);
538 command_print_word_list (select_command
->map_list
, " ");
542 xtrace_print_select_command_head (select_command
)
543 SELECT_COM
*select_command
;
545 fprintf (stderr
, "%s", indirection_level_string ());
546 fprintf (stderr
, "select %s in ", select_command
->name
->word
);
547 xtrace_print_word_list (select_command
->map_list
, 0);
551 print_select_command (select_command
)
552 SELECT_COM
*select_command
;
554 print_select_command_head (select_command
);
558 indentation
+= indentation_amount
;
559 make_command_string_internal (select_command
->action
);
561 indentation
-= indentation_amount
;
564 #endif /* SELECT_COMMAND */
567 print_group_command (group_command
)
568 GROUP_COM
*group_command
;
570 group_command_nesting
++;
573 if (inside_function_def
== 0)
577 /* This is a group command { ... } inside of a function
578 definition, and should be printed as a multiline group
579 command, using the current indentation. */
581 indentation
+= indentation_amount
;
584 make_command_string_internal (group_command
->command
);
586 if (inside_function_def
)
589 indentation
-= indentation_amount
;
590 indent (indentation
);
600 group_command_nesting
--;
604 print_case_command_head (case_command
)
605 CASE_COM
*case_command
;
607 cprintf ("case %s in ", case_command
->word
->word
);
611 xtrace_print_case_command_head (case_command
)
612 CASE_COM
*case_command
;
614 fprintf (stderr
, "%s", indirection_level_string ());
615 fprintf (stderr
, "case %s in\n", case_command
->word
->word
);
619 print_case_command (case_command
)
620 CASE_COM
*case_command
;
622 print_case_command_head (case_command
);
624 if (case_command
->clauses
)
625 print_case_clauses (case_command
->clauses
);
630 print_case_clauses (clauses
)
631 PATTERN_LIST
*clauses
;
633 indentation
+= indentation_amount
;
637 command_print_word_list (clauses
->patterns
, " | ");
639 indentation
+= indentation_amount
;
640 make_command_string_internal (clauses
->action
);
641 indentation
-= indentation_amount
;
642 if (clauses
->flags
& CASEPAT_FALLTHROUGH
)
644 else if (clauses
->flags
& CASEPAT_TESTNEXT
)
648 clauses
= clauses
->next
;
650 indentation
-= indentation_amount
;
654 print_while_command (while_command
)
655 WHILE_COM
*while_command
;
657 print_until_or_while (while_command
, "while");
661 print_until_command (while_command
)
662 WHILE_COM
*while_command
;
664 print_until_or_while (while_command
, "until");
668 print_until_or_while (while_command
, which
)
669 WHILE_COM
*while_command
;
672 cprintf ("%s ", which
);
674 make_command_string_internal (while_command
->test
);
676 cprintf (" do\n"); /* was newline ("do\n"); */
677 indentation
+= indentation_amount
;
678 make_command_string_internal (while_command
->action
);
679 indentation
-= indentation_amount
;
685 print_if_command (if_command
)
690 make_command_string_internal (if_command
->test
);
693 indentation
+= indentation_amount
;
694 make_command_string_internal (if_command
->true_case
);
695 indentation
-= indentation_amount
;
697 if (if_command
->false_case
)
701 indentation
+= indentation_amount
;
702 make_command_string_internal (if_command
->false_case
);
703 indentation
-= indentation_amount
;
709 #if defined (DPAREN_ARITHMETIC)
711 print_arith_command (arith_cmd_list
)
712 WORD_LIST
*arith_cmd_list
;
715 command_print_word_list (arith_cmd_list
, " ");
720 #if defined (COND_COMMAND)
722 print_cond_node (cond
)
725 if (cond
->flags
& CMD_INVERT_RETURN
)
728 if (cond
->type
== COND_EXPR
)
731 print_cond_node (cond
->left
);
734 else if (cond
->type
== COND_AND
)
736 print_cond_node (cond
->left
);
738 print_cond_node (cond
->right
);
740 else if (cond
->type
== COND_OR
)
742 print_cond_node (cond
->left
);
744 print_cond_node (cond
->right
);
746 else if (cond
->type
== COND_UNARY
)
748 cprintf ("%s", cond
->op
->word
);
750 print_cond_node (cond
->left
);
752 else if (cond
->type
== COND_BINARY
)
754 print_cond_node (cond
->left
);
756 cprintf ("%s", cond
->op
->word
);
758 print_cond_node (cond
->right
);
760 else if (cond
->type
== COND_TERM
)
762 cprintf ("%s", cond
->op
->word
); /* need to add quoting here */
767 print_cond_command (cond
)
771 print_cond_node (cond
);
777 debug_print_cond_command (cond
)
780 fprintf (stderr
, "DEBUG: ");
781 command_string_index
= 0;
782 print_cond_command (cond
);
783 fprintf (stderr
, "%s\n", the_printed_command
);
788 xtrace_print_cond_term (type
, invert
, op
, arg1
, arg2
)
793 command_string_index
= 0;
794 fprintf (stderr
, "%s", indirection_level_string ());
795 fprintf (stderr
, "[[ ");
797 fprintf (stderr
, "! ");
799 if (type
== COND_UNARY
)
801 fprintf (stderr
, "%s ", op
->word
);
802 fprintf (stderr
, "%s", (arg1
&& *arg1
) ? arg1
: "''");
804 else if (type
== COND_BINARY
)
806 fprintf (stderr
, "%s", (arg1
&& *arg1
) ? arg1
: "''");
807 fprintf (stderr
, " %s ", op
->word
);
808 fprintf (stderr
, "%s", (arg2
&& *arg2
) ? arg2
: "''");
811 fprintf (stderr
, " ]]\n");
813 #endif /* COND_COMMAND */
815 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
816 /* A function to print the words of an arithmetic command when set -x is on. */
818 xtrace_print_arith_cmd (list
)
823 fprintf (stderr
, "%s", indirection_level_string ());
824 fprintf (stderr
, "(( ");
825 for (w
= list
; w
; w
= w
->next
)
826 fprintf (stderr
, "%s%s", w
->word
->word
, w
->next
? " " : "");
827 fprintf (stderr
, " ))\n");
832 print_simple_command (simple_command
)
833 SIMPLE_COM
*simple_command
;
835 command_print_word_list (simple_command
->words
, " ");
837 if (simple_command
->redirects
)
840 print_redirection_list (simple_command
->redirects
);
845 print_heredocs (heredocs
)
851 for (hdtail
= heredocs
; hdtail
; hdtail
= hdtail
->next
)
853 print_redirection (hdtail
);
859 /* Print heredocs that are attached to the command before the connector
860 represented by CSTRING. The parsing semantics require us to print the
861 here-doc delimiters, then the connector (CSTRING), then the here-doc
862 bodies. We don't print the connector if it's a `;', but we use it to
863 note not to print an extra space after the last heredoc body and
866 print_deferred_heredocs (cstring
)
871 for (hdtail
= deferred_heredocs
; hdtail
; hdtail
= hdtail
->next
)
874 print_heredoc_header (hdtail
);
876 if (cstring
[0] != ';' || cstring
[1])
877 cprintf ("%s", cstring
);
878 if (deferred_heredocs
)
880 for (hdtail
= deferred_heredocs
; hdtail
; hdtail
= hdtail
->next
)
882 print_heredoc_body (hdtail
);
885 if (deferred_heredocs
)
887 if (cstring
[0] != ';' || cstring
[1])
888 cprintf (" "); /* make sure there's at least one space */
889 dispose_redirects (deferred_heredocs
);
891 deferred_heredocs
= (REDIRECT
*)NULL
;
895 print_redirection_list (redirects
)
898 REDIRECT
*heredocs
, *hdtail
, *newredir
;
900 heredocs
= (REDIRECT
*)NULL
;
906 /* Defer printing the here documents until we've printed the
907 rest of the redirections. */
908 if (redirects
->instruction
== r_reading_until
|| redirects
->instruction
== r_deblank_reading_until
)
910 newredir
= copy_redirect (redirects
);
911 newredir
->next
= (REDIRECT
*)NULL
;
914 hdtail
->next
= newredir
;
918 hdtail
= heredocs
= newredir
;
920 else if (redirects
->instruction
== r_duplicating_output_word
&& redirects
->redirector
== 1)
922 /* Temporarily translate it as the execution code does. */
923 redirects
->instruction
= r_err_and_out
;
924 print_redirection (redirects
);
925 redirects
->instruction
= r_duplicating_output_word
;
928 print_redirection (redirects
);
930 redirects
= redirects
->next
;
935 /* Now that we've printed all the other redirections (on one line),
936 print the here documents. */
937 if (heredocs
&& printing_connection
)
938 deferred_heredocs
= heredocs
;
941 print_heredocs (heredocs
);
942 dispose_redirects (heredocs
);
947 print_heredoc_header (redirect
)
953 kill_leading
= redirect
->instruction
== r_deblank_reading_until
;
955 /* Here doc header */
956 if (redirect
->redirector
!= 0)
957 cprintf ("%d", redirect
->redirector
);
959 /* If the here document delimiter is quoted, single-quote it. */
960 if (redirect
->redirectee
.filename
->flags
& W_QUOTED
)
962 x
= sh_single_quote (redirect
->here_doc_eof
);
963 cprintf ("<<%s%s", kill_leading
? "-" : "", x
);
967 cprintf ("<<%s%s", kill_leading
? "-" : "", redirect
->here_doc_eof
);
971 print_heredoc_body (redirect
)
975 cprintf ("%s%s", redirect
->redirectee
.filename
->word
, redirect
->here_doc_eof
);
979 print_redirection (redirect
)
982 int kill_leading
, redirector
, redir_fd
;
983 WORD_DESC
*redirectee
;
986 redirectee
= redirect
->redirectee
.filename
;
987 redirector
= redirect
->redirector
;
988 redir_fd
= redirect
->redirectee
.dest
;
990 switch (redirect
->instruction
)
992 case r_output_direction
:
994 cprintf ("%d", redirector
);
995 cprintf ("> %s", redirectee
->word
);
998 case r_input_direction
:
1000 cprintf ("%d", redirector
);
1001 cprintf ("< %s", redirectee
->word
);
1004 case r_inputa_direction
: /* Redirection created by the shell. */
1008 case r_appending_to
:
1009 if (redirector
!= 1)
1010 cprintf ("%d", redirector
);
1011 cprintf (">> %s", redirectee
->word
);
1014 case r_deblank_reading_until
:
1015 case r_reading_until
:
1016 print_heredoc_header (redirect
);
1018 print_heredoc_body (redirect
);
1021 case r_reading_string
:
1022 if (redirector
!= 0)
1023 cprintf ("%d", redirector
);
1024 if (ansic_shouldquote (redirect
->redirectee
.filename
->word
))
1027 x
= ansic_quote (redirect
->redirectee
.filename
->word
, 0, (int *)0);
1028 cprintf ("<<< %s", x
);
1032 cprintf ("<<< %s", redirect
->redirectee
.filename
->word
);
1035 case r_duplicating_input
:
1036 cprintf ("%d<&%d", redirector
, redir_fd
);
1039 case r_duplicating_output
:
1040 cprintf ("%d>&%d", redirector
, redir_fd
);
1043 case r_duplicating_input_word
:
1044 cprintf ("%d<&%s", redirector
, redirectee
->word
);
1047 case r_duplicating_output_word
:
1048 cprintf ("%d>&%s", redirector
, redirectee
->word
);
1052 cprintf ("%d<&%d-", redirector
, redir_fd
);
1056 cprintf ("%d>&%d-", redirector
, redir_fd
);
1059 case r_move_input_word
:
1060 cprintf ("%d<&%s-", redirector
, redirectee
->word
);
1063 case r_move_output_word
:
1064 cprintf ("%d>&%s-", redirector
, redirectee
->word
);
1068 cprintf ("%d>&-", redirector
);
1072 cprintf ("&>%s", redirectee
->word
);
1075 case r_append_err_and_out
:
1076 cprintf ("&>>%s", redirectee
->word
);
1079 case r_input_output
:
1080 if (redirector
!= 1)
1081 cprintf ("%d", redirector
);
1082 cprintf ("<> %s", redirectee
->word
);
1085 case r_output_force
:
1086 if (redirector
!= 1)
1087 cprintf ("%d", redirector
);
1088 cprintf (">|%s", redirectee
->word
);
1096 inside_function_def
= 0;
1098 printing_connection
= 0;
1099 deferred_heredocs
= 0;
1103 print_function_def (func
)
1107 REDIRECT
*func_redirects
;
1109 func_redirects
= NULL
;
1110 cprintf ("function %s () \n", func
->name
->word
);
1111 add_unwind_protect (reset_locals
, 0);
1113 indent (indentation
);
1116 inside_function_def
++;
1117 indentation
+= indentation_amount
;
1119 cmdcopy
= copy_command (func
->command
);
1120 if (cmdcopy
->type
== cm_group
)
1122 func_redirects
= cmdcopy
->redirects
;
1123 cmdcopy
->redirects
= (REDIRECT
*)NULL
;
1125 make_command_string_internal (cmdcopy
->type
== cm_group
1126 ? cmdcopy
->value
.Group
->command
1129 remove_unwind_protect ();
1130 indentation
-= indentation_amount
;
1131 inside_function_def
--;
1136 print_redirection_list (func_redirects
);
1137 cmdcopy
->redirects
= func_redirects
;
1142 dispose_command (cmdcopy
);
1145 /* Return the string representation of the named function.
1146 NAME is the name of the function.
1147 COMMAND is the function body. It should be a GROUP_COM.
1148 flags&FUNC_MULTILINE is non-zero to pretty-print, or zero for all on one line.
1149 flags&FUNC_EXTERNAL means convert from internal to external form
1152 named_function_string (name
, command
, flags
)
1158 int old_indent
, old_amount
;
1160 REDIRECT
*func_redirects
;
1162 old_indent
= indentation
;
1163 old_amount
= indentation_amount
;
1164 command_string_index
= was_heredoc
= 0;
1165 deferred_heredocs
= 0;
1168 cprintf ("%s ", name
);
1172 if ((flags
& FUNC_MULTILINE
) == 0)
1175 indentation_amount
= 0;
1180 indentation
+= indentation_amount
;
1183 inside_function_def
++;
1185 cprintf ((flags
& FUNC_MULTILINE
) ? "{ \n" : "{ ");
1187 cmdcopy
= copy_command (command
);
1188 /* Take any redirections specified in the function definition (which should
1189 apply to the function as a whole) and save them for printing later. */
1190 func_redirects
= (REDIRECT
*)NULL
;
1191 if (cmdcopy
->type
== cm_group
)
1193 func_redirects
= cmdcopy
->redirects
;
1194 cmdcopy
->redirects
= (REDIRECT
*)NULL
;
1196 make_command_string_internal (cmdcopy
->type
== cm_group
1197 ? cmdcopy
->value
.Group
->command
1200 indentation
= old_indent
;
1201 indentation_amount
= old_amount
;
1202 inside_function_def
--;
1207 print_redirection_list (func_redirects
);
1208 cmdcopy
->redirects
= func_redirects
;
1213 result
= the_printed_command
;
1215 if ((flags
& FUNC_MULTILINE
) == 0)
1219 for (i
= 0; result
[i
]; i
++)
1220 if (result
[i
] == '\n')
1222 strcpy (result
+ i
, result
+ i
+ 1);
1226 if (result
[2] == '\n') /* XXX -- experimental */
1227 strcpy (result
+ 2, result
+ 3);
1231 dispose_command (cmdcopy
);
1233 if (flags
& FUNC_EXTERNAL
)
1234 result
= remove_quoted_escapes (result
);
1244 indent (indentation
);
1245 if (string
&& *string
)
1246 cprintf ("%s", string
);
1249 static char *indentation_string
;
1250 static int indentation_size
;
1258 RESIZE_MALLOCED_BUFFER (indentation_string
, 0, amount
, indentation_size
, 16);
1260 for (i
= 0; amount
> 0; amount
--)
1261 indentation_string
[i
++] = ' ';
1262 indentation_string
[i
] = '\0';
1263 cprintf (indentation_string
);
1269 if (command_string_index
> 0 &&
1270 (the_printed_command
[command_string_index
- 1] == '&' ||
1271 the_printed_command
[command_string_index
- 1] == '\n'))
1276 /* How to make the string. */
1278 #if defined (PREFER_STDARG)
1279 cprintf (const char *control
, ...)
1281 cprintf (control
, va_alist
)
1282 const char *control
;
1286 register const char *s
;
1287 char char_arg
[2], *argp
, intbuf
[INT_STRLEN_BOUND (int) + 1];
1288 int digit_arg
, arg_len
, c
;
1291 SH_VA_START (args
, control
);
1293 arg_len
= strlen (control
);
1294 the_printed_command_resize (arg_len
+ 1);
1301 argp
= (char *)NULL
;
1302 if (c
!= '%' || !*s
)
1320 argp
= va_arg (args
, char *);
1321 arg_len
= strlen (argp
);
1325 /* Represent an out-of-range file descriptor with an out-of-range
1326 integer value. We can do this because the only use of `%d' in
1327 the calls to cprintf is to output a file descriptor number for
1329 digit_arg
= va_arg (args
, int);
1332 sprintf (intbuf
, "%u", (unsigned)-1);
1336 argp
= inttostr (digit_arg
, intbuf
, sizeof (intbuf
));
1337 arg_len
= strlen (argp
);
1341 char_arg
[0] = va_arg (args
, int);
1347 programming_error (_("cprintf: `%c': invalid format character"), c
);
1352 if (argp
&& arg_len
)
1354 the_printed_command_resize (arg_len
+ 1);
1355 FASTCOPY (argp
, the_printed_command
+ command_string_index
, arg_len
);
1356 command_string_index
+= arg_len
;
1360 the_printed_command
[command_string_index
] = '\0';
1363 /* Ensure that there is enough space to stuff LENGTH characters into
1364 THE_PRINTED_COMMAND. */
1366 the_printed_command_resize (length
)
1369 if (the_printed_command
== 0)
1371 the_printed_command_size
= (length
+ PRINTED_COMMAND_INITIAL_SIZE
- 1) & ~(PRINTED_COMMAND_INITIAL_SIZE
- 1);
1372 the_printed_command
= (char *)xmalloc (the_printed_command_size
);
1373 command_string_index
= 0;
1375 else if ((command_string_index
+ length
) >= the_printed_command_size
)
1378 new = command_string_index
+ length
+ 1;
1380 /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */
1381 new = (new + PRINTED_COMMAND_GROW_SIZE
- 1) & ~(PRINTED_COMMAND_GROW_SIZE
- 1);
1382 the_printed_command_size
= new;
1384 the_printed_command
= (char *)xrealloc (the_printed_command
, the_printed_command_size
);
1388 #if defined (HAVE_VPRINTF)
1389 /* ``If vprintf is available, you may assume that vfprintf and vsprintf are
1390 also available.'' */
1393 #if defined (PREFER_STDARG)
1394 xprintf (const char *format
, ...)
1396 xprintf (format
, va_alist
)
1403 SH_VA_START (args
, format
);
1405 vfprintf (stdout
, format
, args
);
1412 xprintf (format
, arg1
, arg2
, arg3
, arg4
, arg5
)
1415 printf (format
, arg1
, arg2
, arg3
, arg4
, arg5
);
1418 #endif /* !HAVE_VPRINTF */