1 /* print_command -- A way to make readable commands from a command tree. */
3 /* Copyright (C) 1989-2016 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 */
48 #include "builtins/common.h"
51 extern int printf
__P((const char *, ...)); /* Yuck. Double yuck. */
54 extern int indirection_level
;
55 extern int posixly_correct
;
57 static int indentation
;
58 static int indentation_amount
= 4;
60 #if defined (PREFER_STDARG)
61 typedef void PFUNC
__P((const char *, ...));
63 static void cprintf
__P((const char *, ...)) __attribute__((__format__ (printf
, 1, 2)));
64 static void xprintf
__P((const char *, ...)) __attribute__((__format__ (printf
, 1, 2)));
66 #define PFUNC VFunction
67 static void cprintf ();
68 static void xprintf ();
71 static void reset_locals
__P((void));
72 static void newline
__P((char *));
73 static void indent
__P((int));
74 static void semicolon
__P((void));
75 static void the_printed_command_resize
__P((int));
77 static void make_command_string_internal
__P((COMMAND
*));
78 static void _print_word_list
__P((WORD_LIST
*, char *, PFUNC
*));
79 static void command_print_word_list
__P((WORD_LIST
*, char *));
80 static void print_case_clauses
__P((PATTERN_LIST
*));
81 static void print_redirection_list
__P((REDIRECT
*));
82 static void print_redirection
__P((REDIRECT
*));
83 static void print_heredoc_header
__P((REDIRECT
*));
84 static void print_heredoc_body
__P((REDIRECT
*));
85 static void print_heredocs
__P((REDIRECT
*));
86 static void print_deferred_heredocs
__P((const char *));
88 static void print_for_command
__P((FOR_COM
*));
89 #if defined (ARITH_FOR_COMMAND)
90 static void print_arith_for_command
__P((ARITH_FOR_COM
*));
92 #if defined (SELECT_COMMAND)
93 static void print_select_command
__P((SELECT_COM
*));
95 static void print_group_command
__P((GROUP_COM
*));
96 static void print_case_command
__P((CASE_COM
*));
97 static void print_while_command
__P((WHILE_COM
*));
98 static void print_until_command
__P((WHILE_COM
*));
99 static void print_until_or_while
__P((WHILE_COM
*, char *));
100 static void print_if_command
__P((IF_COM
*));
101 #if defined (COND_COMMAND)
102 static void print_cond_node
__P((COND_COM
*));
104 static void print_function_def
__P((FUNCTION_DEF
*));
106 #define PRINTED_COMMAND_INITIAL_SIZE 64
107 #define PRINTED_COMMAND_GROW_SIZE 128
109 char *the_printed_command
= (char *)NULL
;
110 int the_printed_command_size
= 0;
111 int command_string_index
= 0;
116 #define CHECK_XTRACE_FP xtrace_fp = (xtrace_fp ? xtrace_fp : stderr)
118 /* shell expansion characters: used in print_redirection_list */
119 #define EXPCHAR(c) ((c) == '{' || (c) == '~' || (c) == '$' || (c) == '`')
121 #define PRINT_DEFERRED_HEREDOCS(x) \
123 if (deferred_heredocs) \
124 print_deferred_heredocs (x); \
127 /* Non-zero means the stuff being printed is inside of a function def. */
128 static int inside_function_def
;
129 static int skip_this_indent
;
130 static int was_heredoc
;
131 static int printing_connection
;
132 static REDIRECT
*deferred_heredocs
;
134 /* The depth of the group commands that we are currently printing. This
135 includes the group command that is a function body. */
136 static int group_command_nesting
;
138 /* A buffer to indicate the indirection level (PS4) when set -x is enabled. */
139 static char *indirection_string
= 0;
140 static int indirection_stringsiz
= 0;
142 /* Print COMMAND (a command tree) on standard output. */
144 print_command (command
)
147 command_string_index
= 0;
148 printf ("%s", make_command_string (command
));
151 /* Make a string which is the printed representation of the command
152 tree in COMMAND. We return this string. However, the string is
153 not consed, so you have to do that yourself if you want it to
156 make_command_string (command
)
159 command_string_index
= was_heredoc
= 0;
160 deferred_heredocs
= 0;
161 make_command_string_internal (command
);
162 return (the_printed_command
);
165 /* The internal function. This is the real workhorse. */
167 make_command_string_internal (command
)
176 if (skip_this_indent
)
179 indent (indentation
);
181 if (command
->flags
& CMD_TIME_PIPELINE
)
184 if (command
->flags
& CMD_TIME_POSIX
)
188 if (command
->flags
& CMD_INVERT_RETURN
)
191 switch (command
->type
)
194 print_for_command (command
->value
.For
);
197 #if defined (ARITH_FOR_COMMAND)
199 print_arith_for_command (command
->value
.ArithFor
);
203 #if defined (SELECT_COMMAND)
205 print_select_command (command
->value
.Select
);
210 print_case_command (command
->value
.Case
);
214 print_while_command (command
->value
.While
);
218 print_until_command (command
->value
.While
);
222 print_if_command (command
->value
.If
);
225 #if defined (DPAREN_ARITHMETIC)
227 print_arith_command (command
->value
.Arith
->exp
);
231 #if defined (COND_COMMAND)
233 print_cond_command (command
->value
.Cond
);
238 print_simple_command (command
->value
.Simple
);
244 printing_connection
++;
245 make_command_string_internal (command
->value
.Connection
->first
);
247 switch (command
->value
.Connection
->connector
)
252 char c
= command
->value
.Connection
->connector
;
258 print_deferred_heredocs (s
);
260 if (c
!= '&' || command
->value
.Connection
->second
)
269 print_deferred_heredocs (" && ");
270 if (command
->value
.Connection
->second
)
275 print_deferred_heredocs (" || ");
276 if (command
->value
.Connection
->second
)
281 if (deferred_heredocs
== 0)
283 if (was_heredoc
== 0)
289 print_deferred_heredocs (inside_function_def
? "" : ";");
291 if (inside_function_def
)
296 if (command
->value
.Connection
->second
)
302 cprintf (_("print_command: bad connector `%d'"),
303 command
->value
.Connection
->connector
);
307 make_command_string_internal (command
->value
.Connection
->second
);
308 PRINT_DEFERRED_HEREDOCS ("");
309 printing_connection
--;
312 case cm_function_def
:
313 print_function_def (command
->value
.Function_def
);
317 print_group_command (command
->value
.Group
);
323 make_command_string_internal (command
->value
.Subshell
->command
);
324 PRINT_DEFERRED_HEREDOCS ("");
329 cprintf ("coproc %s ", command
->value
.Coproc
->name
);
331 make_command_string_internal (command
->value
.Coproc
->command
);
335 command_error ("print_command", CMDERR_BADTYPE
, command
->type
, 0);
340 if (command
->redirects
)
343 print_redirection_list (command
->redirects
);
349 _print_word_list (list
, separator
, pfunc
)
356 for (w
= list
; w
; w
= w
->next
)
357 (*pfunc
) ("%s%s", w
->word
->word
, w
->next
? separator
: "");
361 print_word_list (list
, separator
)
365 _print_word_list (list
, separator
, xprintf
);
373 if (fd
>= 0 && sh_validfd (fd
) == 0)
375 internal_error (_("xtrace_set: %d: invalid file descriptor"), fd
);
380 internal_error (_("xtrace_set: NULL file pointer"));
383 if (fd
>= 0 && fileno (fp
) != fd
)
384 internal_warning (_("xtrace fd (%d) != fileno xtrace fp (%d)"), fd
, fileno (fp
));
393 xtrace_set (-1, stderr
);
399 if (xtrace_fd
>= 0 && xtrace_fp
)
404 else if (xtrace_fd
>= 0)
419 /* Return a string denoting what our indirection level is. */
422 indirection_level_string ()
426 char ps4_firstc
[MB_LEN_MAX
+1];
427 int ps4_firstc_len
, ps4_len
, ineed
, old
;
429 ps4
= get_string_value ("PS4");
430 if (indirection_string
== 0)
431 indirection_string
= xmalloc (indirection_stringsiz
= 100);
432 indirection_string
[0] = '\0';
434 if (ps4
== 0 || *ps4
== '\0')
435 return (indirection_string
);
437 old
= change_flag ('x', FLAG_OFF
);
438 ps4
= decode_prompt_string (ps4
);
440 change_flag ('x', FLAG_ON
);
442 if (ps4
== 0 || *ps4
== '\0')
443 return (indirection_string
);
445 #if defined (HANDLE_MULTIBYTE)
446 ps4_len
= strnlen (ps4
, MB_CUR_MAX
);
447 ps4_firstc_len
= MBLEN (ps4
, ps4_len
);
448 if (ps4_firstc_len
== 1 || ps4_firstc_len
== 0 || ps4_firstc_len
< 0)
450 ps4_firstc
[0] = ps4
[0];
451 ps4_firstc
[ps4_firstc_len
= 1] = '\0';
454 memcpy (ps4_firstc
, ps4
, ps4_firstc_len
);
456 ps4_firstc
[0] = ps4
[0];
457 ps4_firstc
[ps4_firstc_len
= 1] = '\0';
460 /* Dynamically resize indirection_string so we have room for everything
461 and we don't have to truncate ps4 */
462 ineed
= (ps4_firstc_len
* indirection_level
) + strlen (ps4
);
463 if (ineed
> indirection_stringsiz
- 1)
465 indirection_stringsiz
= ineed
+ 1;
466 indirection_string
= xrealloc (indirection_string
, indirection_stringsiz
);
469 for (i
= j
= 0; ps4_firstc
[0] && j
< indirection_level
&& i
< indirection_stringsiz
- 1; i
+= ps4_firstc_len
, j
++)
471 if (ps4_firstc_len
== 1)
472 indirection_string
[i
] = ps4_firstc
[0];
474 memcpy (indirection_string
+i
, ps4_firstc
, ps4_firstc_len
);
477 for (j
= ps4_firstc_len
; *ps4
&& ps4
[j
] && i
< indirection_stringsiz
- 1; i
++, j
++)
478 indirection_string
[i
] = ps4
[j
];
480 indirection_string
[i
] = '\0';
482 return (indirection_string
);
486 xtrace_print_assignment (name
, value
, assign_list
, xflags
)
488 int assign_list
, xflags
;
495 fprintf (xtrace_fp
, "%s", indirection_level_string ());
497 /* VALUE should not be NULL when this is called. */
498 if (*value
== '\0' || assign_list
)
500 else if (sh_contains_shell_metas (value
))
501 nval
= sh_single_quote (value
);
502 else if (ansic_shouldquote (value
))
503 nval
= ansic_quote (value
, 0, (int *)0);
508 fprintf (xtrace_fp
, "%s=(%s)\n", name
, nval
);
510 fprintf (xtrace_fp
, "%s=%s\n", name
, nval
);
518 /* A function to print the words of a simple command when set -x is on. Also
519 used to print the word list in a for or select command header; in that case,
520 we suppress quoting the words because they haven't been expanded yet.
521 XTFLAGS&1 means to print $PS4; XTFLAGS&2 means to suppress quoting the
524 xtrace_print_word_list (list
, xtflags
)
534 fprintf (xtrace_fp
, "%s", indirection_level_string ());
536 for (w
= list
; w
; w
= w
->next
)
539 if (t
== 0 || *t
== '\0')
540 fprintf (xtrace_fp
, "''%s", w
->next
? " " : "");
541 else if (xtflags
& 2)
542 fprintf (xtrace_fp
, "%s%s", t
, w
->next
? " " : "");
543 else if (sh_contains_shell_metas (t
))
545 x
= sh_single_quote (t
);
546 fprintf (xtrace_fp
, "%s%s", x
, w
->next
? " " : "");
549 else if (ansic_shouldquote (t
))
551 x
= ansic_quote (t
, 0, (int *)0);
552 fprintf (xtrace_fp
, "%s%s", x
, w
->next
? " " : "");
556 fprintf (xtrace_fp
, "%s%s", t
, w
->next
? " " : "");
558 fprintf (xtrace_fp
, "\n");
563 command_print_word_list (list
, separator
)
567 _print_word_list (list
, separator
, cprintf
);
571 print_for_command_head (for_command
)
572 FOR_COM
*for_command
;
574 cprintf ("for %s in ", for_command
->name
->word
);
575 command_print_word_list (for_command
->map_list
, " ");
579 xtrace_print_for_command_head (for_command
)
580 FOR_COM
*for_command
;
583 fprintf (xtrace_fp
, "%s", indirection_level_string ());
584 fprintf (xtrace_fp
, "for %s in ", for_command
->name
->word
);
585 xtrace_print_word_list (for_command
->map_list
, 2);
589 print_for_command (for_command
)
590 FOR_COM
*for_command
;
592 print_for_command_head (for_command
);
596 indentation
+= indentation_amount
;
597 make_command_string_internal (for_command
->action
);
598 PRINT_DEFERRED_HEREDOCS ("");
600 indentation
-= indentation_amount
;
605 #if defined (ARITH_FOR_COMMAND)
607 print_arith_for_command (arith_for_command
)
608 ARITH_FOR_COM
*arith_for_command
;
611 command_print_word_list (arith_for_command
->init
, " ");
613 command_print_word_list (arith_for_command
->test
, " ");
615 command_print_word_list (arith_for_command
->step
, " ");
618 indentation
+= indentation_amount
;
619 make_command_string_internal (arith_for_command
->action
);
620 PRINT_DEFERRED_HEREDOCS ("");
622 indentation
-= indentation_amount
;
625 #endif /* ARITH_FOR_COMMAND */
627 #if defined (SELECT_COMMAND)
629 print_select_command_head (select_command
)
630 SELECT_COM
*select_command
;
632 cprintf ("select %s in ", select_command
->name
->word
);
633 command_print_word_list (select_command
->map_list
, " ");
637 xtrace_print_select_command_head (select_command
)
638 SELECT_COM
*select_command
;
641 fprintf (xtrace_fp
, "%s", indirection_level_string ());
642 fprintf (xtrace_fp
, "select %s in ", select_command
->name
->word
);
643 xtrace_print_word_list (select_command
->map_list
, 2);
647 print_select_command (select_command
)
648 SELECT_COM
*select_command
;
650 print_select_command_head (select_command
);
654 indentation
+= indentation_amount
;
655 make_command_string_internal (select_command
->action
);
656 PRINT_DEFERRED_HEREDOCS ("");
658 indentation
-= indentation_amount
;
661 #endif /* SELECT_COMMAND */
664 print_group_command (group_command
)
665 GROUP_COM
*group_command
;
667 group_command_nesting
++;
670 if (inside_function_def
== 0)
674 /* This is a group command { ... } inside of a function
675 definition, and should be printed as a multiline group
676 command, using the current indentation. */
678 indentation
+= indentation_amount
;
681 make_command_string_internal (group_command
->command
);
682 PRINT_DEFERRED_HEREDOCS ("");
684 if (inside_function_def
)
687 indentation
-= indentation_amount
;
688 indent (indentation
);
698 group_command_nesting
--;
702 print_case_command_head (case_command
)
703 CASE_COM
*case_command
;
705 cprintf ("case %s in ", case_command
->word
->word
);
709 xtrace_print_case_command_head (case_command
)
710 CASE_COM
*case_command
;
713 fprintf (xtrace_fp
, "%s", indirection_level_string ());
714 fprintf (xtrace_fp
, "case %s in\n", case_command
->word
->word
);
718 print_case_command (case_command
)
719 CASE_COM
*case_command
;
721 print_case_command_head (case_command
);
723 if (case_command
->clauses
)
724 print_case_clauses (case_command
->clauses
);
729 print_case_clauses (clauses
)
730 PATTERN_LIST
*clauses
;
732 indentation
+= indentation_amount
;
736 command_print_word_list (clauses
->patterns
, " | ");
738 indentation
+= indentation_amount
;
739 make_command_string_internal (clauses
->action
);
740 indentation
-= indentation_amount
;
741 PRINT_DEFERRED_HEREDOCS ("");
742 if (clauses
->flags
& CASEPAT_FALLTHROUGH
)
744 else if (clauses
->flags
& CASEPAT_TESTNEXT
)
748 clauses
= clauses
->next
;
750 indentation
-= indentation_amount
;
754 print_while_command (while_command
)
755 WHILE_COM
*while_command
;
757 print_until_or_while (while_command
, "while");
761 print_until_command (while_command
)
762 WHILE_COM
*while_command
;
764 print_until_or_while (while_command
, "until");
768 print_until_or_while (while_command
, which
)
769 WHILE_COM
*while_command
;
772 cprintf ("%s ", which
);
774 make_command_string_internal (while_command
->test
);
775 PRINT_DEFERRED_HEREDOCS ("");
777 cprintf (" do\n"); /* was newline ("do\n"); */
778 indentation
+= indentation_amount
;
779 make_command_string_internal (while_command
->action
);
780 PRINT_DEFERRED_HEREDOCS ("");
781 indentation
-= indentation_amount
;
787 print_if_command (if_command
)
792 make_command_string_internal (if_command
->test
);
795 indentation
+= indentation_amount
;
796 make_command_string_internal (if_command
->true_case
);
797 PRINT_DEFERRED_HEREDOCS ("");
798 indentation
-= indentation_amount
;
800 if (if_command
->false_case
)
804 indentation
+= indentation_amount
;
805 make_command_string_internal (if_command
->false_case
);
806 PRINT_DEFERRED_HEREDOCS ("");
807 indentation
-= indentation_amount
;
813 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
815 print_arith_command (arith_cmd_list
)
816 WORD_LIST
*arith_cmd_list
;
819 command_print_word_list (arith_cmd_list
, " ");
824 #if defined (COND_COMMAND)
826 print_cond_node (cond
)
829 if (cond
->flags
& CMD_INVERT_RETURN
)
832 if (cond
->type
== COND_EXPR
)
835 print_cond_node (cond
->left
);
838 else if (cond
->type
== COND_AND
)
840 print_cond_node (cond
->left
);
842 print_cond_node (cond
->right
);
844 else if (cond
->type
== COND_OR
)
846 print_cond_node (cond
->left
);
848 print_cond_node (cond
->right
);
850 else if (cond
->type
== COND_UNARY
)
852 cprintf ("%s", cond
->op
->word
);
854 print_cond_node (cond
->left
);
856 else if (cond
->type
== COND_BINARY
)
858 print_cond_node (cond
->left
);
860 cprintf ("%s", cond
->op
->word
);
862 print_cond_node (cond
->right
);
864 else if (cond
->type
== COND_TERM
)
866 cprintf ("%s", cond
->op
->word
); /* need to add quoting here */
871 print_cond_command (cond
)
875 print_cond_node (cond
);
881 debug_print_word_list (s
, list
, sep
)
889 fprintf (stderr
, "%s: ", s
);
890 for (w
= list
; w
; w
= w
->next
)
891 fprintf (stderr
, "%s%s", w
->word
->word
, w
->next
? sep
: "");
892 fprintf (stderr
, "\n");
896 debug_print_cond_command (cond
)
899 fprintf (stderr
, "DEBUG: ");
900 command_string_index
= 0;
901 print_cond_command (cond
);
902 fprintf (stderr
, "%s\n", the_printed_command
);
907 xtrace_print_cond_term (type
, invert
, op
, arg1
, arg2
)
913 command_string_index
= 0;
914 fprintf (xtrace_fp
, "%s", indirection_level_string ());
915 fprintf (xtrace_fp
, "[[ ");
917 fprintf (xtrace_fp
, "! ");
919 if (type
== COND_UNARY
)
921 fprintf (xtrace_fp
, "%s ", op
->word
);
922 fprintf (xtrace_fp
, "%s", (arg1
&& *arg1
) ? arg1
: "''");
924 else if (type
== COND_BINARY
)
926 fprintf (xtrace_fp
, "%s", (arg1
&& *arg1
) ? arg1
: "''");
927 fprintf (xtrace_fp
, " %s ", op
->word
);
928 fprintf (xtrace_fp
, "%s", (arg2
&& *arg2
) ? arg2
: "''");
931 fprintf (xtrace_fp
, " ]]\n");
935 #endif /* COND_COMMAND */
937 #if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
938 /* A function to print the words of an arithmetic command when set -x is on. */
940 xtrace_print_arith_cmd (list
)
946 fprintf (xtrace_fp
, "%s", indirection_level_string ());
947 fprintf (xtrace_fp
, "(( ");
948 for (w
= list
; w
; w
= w
->next
)
949 fprintf (xtrace_fp
, "%s%s", w
->word
->word
, w
->next
? " " : "");
950 fprintf (xtrace_fp
, " ))\n");
957 print_simple_command (simple_command
)
958 SIMPLE_COM
*simple_command
;
960 command_print_word_list (simple_command
->words
, " ");
962 if (simple_command
->redirects
)
965 print_redirection_list (simple_command
->redirects
);
970 print_heredocs (heredocs
)
976 for (hdtail
= heredocs
; hdtail
; hdtail
= hdtail
->next
)
978 print_redirection (hdtail
);
984 /* Print heredocs that are attached to the command before the connector
985 represented by CSTRING. The parsing semantics require us to print the
986 here-doc delimiters, then the connector (CSTRING), then the here-doc
987 bodies. We don't print the connector if it's a `;', but we use it to
988 note not to print an extra space after the last heredoc body and
991 print_deferred_heredocs (cstring
)
996 for (hdtail
= deferred_heredocs
; hdtail
; hdtail
= hdtail
->next
)
999 print_heredoc_header (hdtail
);
1001 if (cstring
&& cstring
[0] && (cstring
[0] != ';' || cstring
[1]))
1002 cprintf ("%s", cstring
);
1003 if (deferred_heredocs
)
1005 for (hdtail
= deferred_heredocs
; hdtail
; hdtail
= hdtail
->next
)
1007 print_heredoc_body (hdtail
);
1010 if (deferred_heredocs
)
1012 if (cstring
&& cstring
[0] && (cstring
[0] != ';' || cstring
[1]))
1013 cprintf (" "); /* make sure there's at least one space */
1014 dispose_redirects (deferred_heredocs
);
1017 deferred_heredocs
= (REDIRECT
*)NULL
;
1021 print_redirection_list (redirects
)
1022 REDIRECT
*redirects
;
1024 REDIRECT
*heredocs
, *hdtail
, *newredir
;
1027 heredocs
= (REDIRECT
*)NULL
;
1033 /* Defer printing the here documents until we've printed the
1034 rest of the redirections. */
1035 if (redirects
->instruction
== r_reading_until
|| redirects
->instruction
== r_deblank_reading_until
)
1037 newredir
= copy_redirect (redirects
);
1038 newredir
->next
= (REDIRECT
*)NULL
;
1041 hdtail
->next
= newredir
;
1045 hdtail
= heredocs
= newredir
;
1047 else if (redirects
->instruction
== r_duplicating_output_word
&& (redirects
->flags
& REDIR_VARASSIGN
) == 0 && redirects
->redirector
.dest
== 1)
1049 /* Temporarily translate it as the execution code does. */
1050 rw
= redirects
->redirectee
.filename
->word
;
1051 if (rw
&& *rw
!= '-' && DIGIT (*rw
) == 0 && EXPCHAR (*rw
) == 0)
1052 redirects
->instruction
= r_err_and_out
;
1053 print_redirection (redirects
);
1054 redirects
->instruction
= r_duplicating_output_word
;
1057 print_redirection (redirects
);
1059 redirects
= redirects
->next
;
1064 /* Now that we've printed all the other redirections (on one line),
1065 print the here documents. */
1066 if (heredocs
&& printing_connection
)
1067 deferred_heredocs
= heredocs
;
1070 print_heredocs (heredocs
);
1071 dispose_redirects (heredocs
);
1076 print_heredoc_header (redirect
)
1082 kill_leading
= redirect
->instruction
== r_deblank_reading_until
;
1084 /* Here doc header */
1085 if (redirect
->rflags
& REDIR_VARASSIGN
)
1086 cprintf ("{%s}", redirect
->redirector
.filename
->word
);
1087 else if (redirect
->redirector
.dest
!= 0)
1088 cprintf ("%d", redirect
->redirector
.dest
);
1090 /* If the here document delimiter is quoted, single-quote it. */
1091 if (redirect
->redirectee
.filename
->flags
& W_QUOTED
)
1093 x
= sh_single_quote (redirect
->here_doc_eof
);
1094 cprintf ("<<%s%s", kill_leading
? "-" : "", x
);
1098 cprintf ("<<%s%s", kill_leading
? "-" : "", redirect
->here_doc_eof
);
1102 print_heredoc_body (redirect
)
1106 cprintf ("%s%s", redirect
->redirectee
.filename
->word
, redirect
->here_doc_eof
);
1110 print_redirection (redirect
)
1113 int redirector
, redir_fd
;
1114 WORD_DESC
*redirectee
, *redir_word
;
1116 redirectee
= redirect
->redirectee
.filename
;
1117 redir_fd
= redirect
->redirectee
.dest
;
1119 redir_word
= redirect
->redirector
.filename
;
1120 redirector
= redirect
->redirector
.dest
;
1122 switch (redirect
->instruction
)
1124 case r_input_direction
:
1125 if (redirect
->rflags
& REDIR_VARASSIGN
)
1126 cprintf ("{%s}", redir_word
->word
);
1127 else if (redirector
!= 0)
1128 cprintf ("%d", redirector
);
1129 cprintf ("< %s", redirectee
->word
);
1132 case r_output_direction
:
1133 if (redirect
->rflags
& REDIR_VARASSIGN
)
1134 cprintf ("{%s}", redir_word
->word
);
1135 else if (redirector
!= 1)
1136 cprintf ("%d", redirector
);
1137 cprintf ("> %s", redirectee
->word
);
1140 case r_inputa_direction
: /* Redirection created by the shell. */
1144 case r_output_force
:
1145 if (redirect
->rflags
& REDIR_VARASSIGN
)
1146 cprintf ("{%s}", redir_word
->word
);
1147 else if (redirector
!= 1)
1148 cprintf ("%d", redirector
);
1149 cprintf (">| %s", redirectee
->word
);
1152 case r_appending_to
:
1153 if (redirect
->rflags
& REDIR_VARASSIGN
)
1154 cprintf ("{%s}", redir_word
->word
);
1155 else if (redirector
!= 1)
1156 cprintf ("%d", redirector
);
1157 cprintf (">> %s", redirectee
->word
);
1160 case r_input_output
:
1161 if (redirect
->rflags
& REDIR_VARASSIGN
)
1162 cprintf ("{%s}", redir_word
->word
);
1163 else if (redirector
!= 1)
1164 cprintf ("%d", redirector
);
1165 cprintf ("<> %s", redirectee
->word
);
1168 case r_deblank_reading_until
:
1169 case r_reading_until
:
1170 print_heredoc_header (redirect
);
1172 print_heredoc_body (redirect
);
1175 case r_reading_string
:
1176 if (redirect
->rflags
& REDIR_VARASSIGN
)
1177 cprintf ("{%s}", redir_word
->word
);
1178 else if (redirector
!= 0)
1179 cprintf ("%d", redirector
);
1181 /* Don't need to check whether or not to requote, since original quotes
1182 are still intact. The only thing that has happened is that $'...'
1183 has been replaced with 'expanded ...'. */
1184 if (ansic_shouldquote (redirect
->redirectee
.filename
->word
))
1187 x
= ansic_quote (redirect
->redirectee
.filename
->word
, 0, (int *)0);
1188 cprintf ("<<< %s", x
);
1193 cprintf ("<<< %s", redirect
->redirectee
.filename
->word
);
1196 case r_duplicating_input
:
1197 if (redirect
->rflags
& REDIR_VARASSIGN
)
1198 cprintf ("{%s}<&%d", redir_word
->word
, redir_fd
);
1200 cprintf ("%d<&%d", redirector
, redir_fd
);
1203 case r_duplicating_output
:
1204 if (redirect
->rflags
& REDIR_VARASSIGN
)
1205 cprintf ("{%s}>&%d", redir_word
->word
, redir_fd
);
1207 cprintf ("%d>&%d", redirector
, redir_fd
);
1210 case r_duplicating_input_word
:
1211 if (redirect
->rflags
& REDIR_VARASSIGN
)
1212 cprintf ("{%s}<&%s", redir_word
->word
, redirectee
->word
);
1214 cprintf ("%d<&%s", redirector
, redirectee
->word
);
1217 case r_duplicating_output_word
:
1218 if (redirect
->rflags
& REDIR_VARASSIGN
)
1219 cprintf ("{%s}>&%s", redir_word
->word
, redirectee
->word
);
1221 cprintf ("%d>&%s", redirector
, redirectee
->word
);
1225 if (redirect
->rflags
& REDIR_VARASSIGN
)
1226 cprintf ("{%s}<&%d-", redir_word
->word
, redir_fd
);
1228 cprintf ("%d<&%d-", redirector
, redir_fd
);
1232 if (redirect
->rflags
& REDIR_VARASSIGN
)
1233 cprintf ("{%s}>&%d-", redir_word
->word
, redir_fd
);
1235 cprintf ("%d>&%d-", redirector
, redir_fd
);
1238 case r_move_input_word
:
1239 if (redirect
->rflags
& REDIR_VARASSIGN
)
1240 cprintf ("{%s}<&%s-", redir_word
->word
, redirectee
->word
);
1242 cprintf ("%d<&%s-", redirector
, redirectee
->word
);
1245 case r_move_output_word
:
1246 if (redirect
->rflags
& REDIR_VARASSIGN
)
1247 cprintf ("{%s}>&%s-", redir_word
->word
, redirectee
->word
);
1249 cprintf ("%d>&%s-", redirector
, redirectee
->word
);
1253 if (redirect
->rflags
& REDIR_VARASSIGN
)
1254 cprintf ("{%s}>&-", redir_word
->word
);
1256 cprintf ("%d>&-", redirector
);
1260 cprintf ("&> %s", redirectee
->word
);
1263 case r_append_err_and_out
:
1264 cprintf ("&>> %s", redirectee
->word
);
1272 inside_function_def
= 0;
1274 printing_connection
= 0;
1275 deferred_heredocs
= 0;
1279 print_function_def (func
)
1283 REDIRECT
*func_redirects
;
1285 func_redirects
= NULL
;
1286 /* When in posix mode, print functions as posix specifies them. */
1287 if (posixly_correct
== 0)
1288 cprintf ("function %s () \n", func
->name
->word
);
1290 cprintf ("%s () \n", func
->name
->word
);
1291 add_unwind_protect (reset_locals
, 0);
1293 indent (indentation
);
1296 inside_function_def
++;
1297 indentation
+= indentation_amount
;
1299 cmdcopy
= copy_command (func
->command
);
1300 if (cmdcopy
->type
== cm_group
)
1302 func_redirects
= cmdcopy
->redirects
;
1303 cmdcopy
->redirects
= (REDIRECT
*)NULL
;
1305 make_command_string_internal (cmdcopy
->type
== cm_group
1306 ? cmdcopy
->value
.Group
->command
1308 /* XXX - PRINT_DEFERRED_HEREDOCS (""); ? */
1310 remove_unwind_protect ();
1311 indentation
-= indentation_amount
;
1312 inside_function_def
--;
1317 print_redirection_list (func_redirects
);
1318 cmdcopy
->redirects
= func_redirects
;
1323 dispose_command (cmdcopy
);
1326 /* Return the string representation of the named function.
1327 NAME is the name of the function.
1328 COMMAND is the function body. It should be a GROUP_COM.
1329 flags&FUNC_MULTILINE is non-zero to pretty-print, or zero for all on one line.
1330 flags&FUNC_EXTERNAL means convert from internal to external form
1333 named_function_string (name
, command
, flags
)
1339 int old_indent
, old_amount
;
1341 REDIRECT
*func_redirects
;
1343 old_indent
= indentation
;
1344 old_amount
= indentation_amount
;
1345 command_string_index
= was_heredoc
= 0;
1346 deferred_heredocs
= 0;
1350 if (find_reserved_word (name
) >= 0)
1351 cprintf ("function ");
1352 cprintf ("%s ", name
);
1357 if ((flags
& FUNC_MULTILINE
) == 0)
1360 indentation_amount
= 0;
1365 indentation
+= indentation_amount
;
1368 inside_function_def
++;
1370 cprintf ((flags
& FUNC_MULTILINE
) ? "{ \n" : "{ ");
1372 cmdcopy
= copy_command (command
);
1373 /* Take any redirections specified in the function definition (which should
1374 apply to the function as a whole) and save them for printing later. */
1375 func_redirects
= (REDIRECT
*)NULL
;
1376 if (cmdcopy
->type
== cm_group
)
1378 func_redirects
= cmdcopy
->redirects
;
1379 cmdcopy
->redirects
= (REDIRECT
*)NULL
;
1381 make_command_string_internal (cmdcopy
->type
== cm_group
1382 ? cmdcopy
->value
.Group
->command
1384 /* XXX - PRINT_DEFERRED_HEREDOCS (""); ? */
1386 indentation
= old_indent
;
1387 indentation_amount
= old_amount
;
1388 inside_function_def
--;
1393 print_redirection_list (func_redirects
);
1394 cmdcopy
->redirects
= func_redirects
;
1399 result
= the_printed_command
;
1401 if ((flags
& FUNC_MULTILINE
) == 0)
1405 for (i
= 0; result
[i
]; i
++)
1406 if (result
[i
] == '\n')
1408 strcpy (result
+ i
, result
+ i
+ 1);
1412 if (result
[2] == '\n') /* XXX -- experimental */
1413 memmove (result
+ 2, result
+ 3, strlen (result
) - 2);
1418 dispose_command (cmdcopy
);
1420 if (flags
& FUNC_EXTERNAL
)
1421 result
= remove_quoted_escapes (result
);
1431 indent (indentation
);
1432 if (string
&& *string
)
1433 cprintf ("%s", string
);
1436 static char *indentation_string
;
1437 static int indentation_size
;
1445 RESIZE_MALLOCED_BUFFER (indentation_string
, 0, amount
, indentation_size
, 16);
1447 for (i
= 0; amount
> 0; amount
--)
1448 indentation_string
[i
++] = ' ';
1449 indentation_string
[i
] = '\0';
1450 cprintf ("%s", indentation_string
);
1456 if (command_string_index
> 0 &&
1457 (the_printed_command
[command_string_index
- 1] == '&' ||
1458 the_printed_command
[command_string_index
- 1] == '\n'))
1463 /* How to make the string. */
1465 #if defined (PREFER_STDARG)
1466 cprintf (const char *control
, ...)
1468 cprintf (control
, va_alist
)
1469 const char *control
;
1473 register const char *s
;
1474 char char_arg
[2], *argp
, intbuf
[INT_STRLEN_BOUND (int) + 1];
1475 int digit_arg
, arg_len
, c
;
1478 SH_VA_START (args
, control
);
1480 arg_len
= strlen (control
);
1481 the_printed_command_resize (arg_len
+ 1);
1488 argp
= (char *)NULL
;
1489 if (c
!= '%' || !*s
)
1507 argp
= va_arg (args
, char *);
1508 arg_len
= strlen (argp
);
1512 /* Represent an out-of-range file descriptor with an out-of-range
1513 integer value. We can do this because the only use of `%d' in
1514 the calls to cprintf is to output a file descriptor number for
1516 digit_arg
= va_arg (args
, int);
1519 sprintf (intbuf
, "%u", (unsigned)-1);
1523 argp
= inttostr (digit_arg
, intbuf
, sizeof (intbuf
));
1524 arg_len
= strlen (argp
);
1528 char_arg
[0] = va_arg (args
, int);
1534 programming_error (_("cprintf: `%c': invalid format character"), c
);
1539 if (argp
&& arg_len
)
1541 the_printed_command_resize (arg_len
+ 1);
1542 FASTCOPY (argp
, the_printed_command
+ command_string_index
, arg_len
);
1543 command_string_index
+= arg_len
;
1549 the_printed_command
[command_string_index
] = '\0';
1552 /* Ensure that there is enough space to stuff LENGTH characters into
1553 THE_PRINTED_COMMAND. */
1555 the_printed_command_resize (length
)
1558 if (the_printed_command
== 0)
1560 the_printed_command_size
= (length
+ PRINTED_COMMAND_INITIAL_SIZE
- 1) & ~(PRINTED_COMMAND_INITIAL_SIZE
- 1);
1561 the_printed_command
= (char *)xmalloc (the_printed_command_size
);
1562 command_string_index
= 0;
1564 else if ((command_string_index
+ length
) >= the_printed_command_size
)
1567 new = command_string_index
+ length
+ 1;
1569 /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */
1570 new = (new + PRINTED_COMMAND_GROW_SIZE
- 1) & ~(PRINTED_COMMAND_GROW_SIZE
- 1);
1571 the_printed_command_size
= new;
1573 the_printed_command
= (char *)xrealloc (the_printed_command
, the_printed_command_size
);
1577 #if defined (HAVE_VPRINTF)
1578 /* ``If vprintf is available, you may assume that vfprintf and vsprintf are
1579 also available.'' */
1582 #if defined (PREFER_STDARG)
1583 xprintf (const char *format
, ...)
1585 xprintf (format
, va_alist
)
1592 SH_VA_START (args
, format
);
1594 vfprintf (stdout
, format
, args
);
1601 xprintf (format
, arg1
, arg2
, arg3
, arg4
, arg5
)
1604 printf (format
, arg1
, arg2
, arg3
, arg4
, arg5
);
1607 #endif /* !HAVE_VPRINTF */