/* print_command -- A way to make readable commands from a command tree. */
-/* Copyright (C) 1989-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2020 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
#include "bashansi.h"
#include "bashintl.h"
+#define NEED_XTRACE_SET_DECL
+
#include "shell.h"
#include "flags.h"
#include <y.tab.h> /* use <...> so we pick it up from the build directory */
+#include "input.h"
#include "shmbutil.h"
#include "builtins/common.h"
#if !HAVE_DECL_PRINTF
-extern int printf __P((const char *, ...)); /* Yuck. Double yuck. */
+extern int printf PARAMS((const char *, ...)); /* Yuck. Double yuck. */
#endif
-extern int indirection_level;
-
static int indentation;
static int indentation_amount = 4;
#if defined (PREFER_STDARG)
-typedef void PFUNC __P((const char *, ...));
+typedef void PFUNC PARAMS((const char *, ...));
-static void cprintf __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
-static void xprintf __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
+static void cprintf PARAMS((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
+static void xprintf PARAMS((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
#else
#define PFUNC VFunction
static void cprintf ();
static void xprintf ();
#endif
-static void reset_locals __P((void));
-static void newline __P((char *));
-static void indent __P((int));
-static void semicolon __P((void));
-static void the_printed_command_resize __P((int));
-
-static void make_command_string_internal __P((COMMAND *));
-static void _print_word_list __P((WORD_LIST *, char *, PFUNC *));
-static void command_print_word_list __P((WORD_LIST *, char *));
-static void print_case_clauses __P((PATTERN_LIST *));
-static void print_redirection_list __P((REDIRECT *));
-static void print_redirection __P((REDIRECT *));
-static void print_heredoc_header __P((REDIRECT *));
-static void print_heredoc_body __P((REDIRECT *));
-static void print_heredocs __P((REDIRECT *));
-static void print_deferred_heredocs __P((const char *));
-
-static void print_for_command __P((FOR_COM *));
+static void reset_locals PARAMS((void));
+static void newline PARAMS((char *));
+static void indent PARAMS((int));
+static void semicolon PARAMS((void));
+static void the_printed_command_resize PARAMS((int));
+
+static void make_command_string_internal PARAMS((COMMAND *));
+static void _print_word_list PARAMS((WORD_LIST *, char *, PFUNC *));
+static void command_print_word_list PARAMS((WORD_LIST *, char *));
+static void print_case_clauses PARAMS((PATTERN_LIST *));
+static void print_redirection_list PARAMS((REDIRECT *));
+static void print_redirection PARAMS((REDIRECT *));
+static void print_heredoc_header PARAMS((REDIRECT *));
+static void print_heredoc_body PARAMS((REDIRECT *));
+static void print_heredocs PARAMS((REDIRECT *));
+static void print_heredoc_bodies PARAMS((REDIRECT *));
+static void print_deferred_heredocs PARAMS((const char *));
+
+static void print_for_command PARAMS((FOR_COM *));
#if defined (ARITH_FOR_COMMAND)
-static void print_arith_for_command __P((ARITH_FOR_COM *));
+static void print_arith_for_command PARAMS((ARITH_FOR_COM *));
#endif
#if defined (SELECT_COMMAND)
-static void print_select_command __P((SELECT_COM *));
+static void print_select_command PARAMS((SELECT_COM *));
#endif
-static void print_group_command __P((GROUP_COM *));
-static void print_case_command __P((CASE_COM *));
-static void print_while_command __P((WHILE_COM *));
-static void print_until_command __P((WHILE_COM *));
-static void print_until_or_while __P((WHILE_COM *, char *));
-static void print_if_command __P((IF_COM *));
+static void print_group_command PARAMS((GROUP_COM *));
+static void print_case_command PARAMS((CASE_COM *));
+static void print_while_command PARAMS((WHILE_COM *));
+static void print_until_command PARAMS((WHILE_COM *));
+static void print_until_or_while PARAMS((WHILE_COM *, char *));
+static void print_if_command PARAMS((IF_COM *));
#if defined (COND_COMMAND)
-static void print_cond_node __P((COND_COM *));
+static void print_cond_node PARAMS((COND_COM *));
#endif
-static void print_function_def __P((FUNCTION_DEF *));
+static void print_function_def PARAMS((FUNCTION_DEF *));
#define PRINTED_COMMAND_INITIAL_SIZE 64
#define PRINTED_COMMAND_GROW_SIZE 128
int the_printed_command_size = 0;
int command_string_index = 0;
+int xtrace_fd = -1;
+FILE *xtrace_fp = 0;
+
+#define CHECK_XTRACE_FP xtrace_fp = (xtrace_fp ? xtrace_fp : stderr)
+
+/* shell expansion characters: used in print_redirection_list */
+#define EXPCHAR(c) ((c) == '{' || (c) == '~' || (c) == '$' || (c) == '`')
+
+#define PRINT_DEFERRED_HEREDOCS(x) \
+ do { \
+ if (deferred_heredocs) \
+ print_deferred_heredocs (x); \
+ } while (0)
+
/* Non-zero means the stuff being printed is inside of a function def. */
static int inside_function_def;
static int skip_this_indent;
static int group_command_nesting;
/* A buffer to indicate the indirection level (PS4) when set -x is enabled. */
-static char indirection_string[100];
+static char *indirection_string = 0;
+static int indirection_stringsiz = 0;
/* Print COMMAND (a command tree) on standard output. */
void
break;
case ';':
-#if 0
- if (was_heredoc == 0)
- cprintf (";");
- else
- was_heredoc = 0;
-#else
if (deferred_heredocs == 0)
- cprintf (";");
+ {
+ if (was_heredoc == 0)
+ cprintf (";");
+ else
+ was_heredoc = 0;
+ }
else
- print_deferred_heredocs (";");
-#endif
+ print_deferred_heredocs (inside_function_def ? "" : ";");
if (inside_function_def)
cprintf ("\n");
}
make_command_string_internal (command->value.Connection->second);
+ PRINT_DEFERRED_HEREDOCS ("");
printing_connection--;
break;
cprintf ("( ");
skip_this_indent++;
make_command_string_internal (command->value.Subshell->command);
+ PRINT_DEFERRED_HEREDOCS ("");
cprintf (" )");
break;
_print_word_list (list, separator, xprintf);
}
+void
+xtrace_set (fd, fp)
+ int fd;
+ FILE *fp;
+{
+ if (fd >= 0 && sh_validfd (fd) == 0)
+ {
+ internal_error (_("xtrace_set: %d: invalid file descriptor"), fd);
+ return;
+ }
+ if (fp == 0)
+ {
+ internal_error (_("xtrace_set: NULL file pointer"));
+ return;
+ }
+ if (fd >= 0 && fileno (fp) != fd)
+ internal_warning (_("xtrace fd (%d) != fileno xtrace fp (%d)"), fd, fileno (fp));
+
+ xtrace_fd = fd;
+ xtrace_fp = fp;
+}
+
+void
+xtrace_init ()
+{
+ xtrace_set (-1, stderr);
+}
+
+void
+xtrace_reset ()
+{
+ if (xtrace_fd >= 0 && xtrace_fp)
+ {
+ fflush (xtrace_fp);
+ fclose (xtrace_fp);
+ }
+ else if (xtrace_fd >= 0)
+ close (xtrace_fd);
+
+ xtrace_fd = -1;
+ xtrace_fp = stderr;
+}
+
+void
+xtrace_fdchk (fd)
+ int fd;
+{
+ if (fd == xtrace_fd)
+ xtrace_reset ();
+}
+
/* Return a string denoting what our indirection level is. */
char *
register int i, j;
char *ps4;
char ps4_firstc[MB_LEN_MAX+1];
- int ps4_firstc_len, ps4_len;
+ int ps4_firstc_len, ps4_len, ineed, old;
- indirection_string[0] = '\0';
ps4 = get_string_value ("PS4");
+ if (indirection_string == 0)
+ indirection_string = xmalloc (indirection_stringsiz = 100);
+ indirection_string[0] = '\0';
if (ps4 == 0 || *ps4 == '\0')
return (indirection_string);
- change_flag ('x', FLAG_OFF);
+ old = change_flag ('x', FLAG_OFF);
ps4 = decode_prompt_string (ps4);
- change_flag ('x', FLAG_ON);
+ if (old)
+ change_flag ('x', FLAG_ON);
if (ps4 == 0 || *ps4 == '\0')
return (indirection_string);
#if defined (HANDLE_MULTIBYTE)
ps4_len = strnlen (ps4, MB_CUR_MAX);
ps4_firstc_len = MBLEN (ps4, ps4_len);
- if (ps4_firstc_len == 1 || ps4_firstc_len == 0 || MB_INVALIDCH (ps4_firstc_len))
+ if (ps4_firstc_len == 1 || ps4_firstc_len == 0 || ps4_firstc_len < 0)
{
ps4_firstc[0] = ps4[0];
ps4_firstc[ps4_firstc_len = 1] = '\0';
ps4_firstc[0] = ps4[0];
ps4_firstc[ps4_firstc_len = 1] = '\0';
#endif
-
- for (i = j = 0; ps4_firstc[0] && j < indirection_level && i < 99; i += ps4_firstc_len, j++)
+
+ /* Dynamically resize indirection_string so we have room for everything
+ and we don't have to truncate ps4 */
+ ineed = (ps4_firstc_len * indirection_level) + strlen (ps4);
+ if (ineed > indirection_stringsiz - 1)
+ {
+ indirection_stringsiz = ineed + 1;
+ indirection_string = xrealloc (indirection_string, indirection_stringsiz);
+ }
+
+ for (i = j = 0; ps4_firstc[0] && j < indirection_level && i < indirection_stringsiz - 1; i += ps4_firstc_len, j++)
{
if (ps4_firstc_len == 1)
indirection_string[i] = ps4_firstc[0];
memcpy (indirection_string+i, ps4_firstc, ps4_firstc_len);
}
- for (j = ps4_firstc_len; *ps4 && ps4[j] && i < 99; i++, j++)
+ for (j = ps4_firstc_len; *ps4 && ps4[j] && i < indirection_stringsiz - 1; i++, j++)
indirection_string[i] = ps4[j];
indirection_string[i] = '\0';
{
char *nval;
+ CHECK_XTRACE_FP;
+
if (xflags)
- fprintf (stderr, "%s", indirection_level_string ());
+ fprintf (xtrace_fp, "%s", indirection_level_string ());
/* VALUE should not be NULL when this is called. */
if (*value == '\0' || assign_list)
nval = value;
if (assign_list)
- fprintf (stderr, "%s=(%s)\n", name, nval);
+ fprintf (xtrace_fp, "%s=(%s)\n", name, nval);
else
- fprintf (stderr, "%s=%s\n", name, nval);
+ fprintf (xtrace_fp, "%s=%s\n", name, nval);
if (nval != value)
FREE (nval);
- fflush (stderr);
+ fflush (xtrace_fp);
}
-/* A function to print the words of a simple command when set -x is on. */
+/* A function to print the words of a simple command when set -x is on. Also used to
+ print the word list in a for or select command header; in that case, we suppress
+ quoting the words because they haven't been expanded yet. XTFLAGS&1 means to
+ print $PS4; XTFLAGS&2 means to suppress quoting the words in LIST. */
void
xtrace_print_word_list (list, xtflags)
WORD_LIST *list;
WORD_LIST *w;
char *t, *x;
- if (xtflags)
- fprintf (stderr, "%s", indirection_level_string ());
+ CHECK_XTRACE_FP;
+
+ if (xtflags&1)
+ fprintf (xtrace_fp, "%s", indirection_level_string ());
for (w = list; w; w = w->next)
{
t = w->word->word;
if (t == 0 || *t == '\0')
- fprintf (stderr, "''%s", w->next ? " " : "");
+ fprintf (xtrace_fp, "''%s", w->next ? " " : "");
+ else if (xtflags & 2)
+ fprintf (xtrace_fp, "%s%s", t, w->next ? " " : "");
else if (sh_contains_shell_metas (t))
{
x = sh_single_quote (t);
- fprintf (stderr, "%s%s", x, w->next ? " " : "");
+ fprintf (xtrace_fp, "%s%s", x, w->next ? " " : "");
free (x);
}
else if (ansic_shouldquote (t))
{
x = ansic_quote (t, 0, (int *)0);
- fprintf (stderr, "%s%s", x, w->next ? " " : "");
+ fprintf (xtrace_fp, "%s%s", x, w->next ? " " : "");
free (x);
}
else
- fprintf (stderr, "%s%s", t, w->next ? " " : "");
+ fprintf (xtrace_fp, "%s%s", t, w->next ? " " : "");
}
- fprintf (stderr, "\n");
+ fprintf (xtrace_fp, "\n");
+ fflush (xtrace_fp);
}
static void
xtrace_print_for_command_head (for_command)
FOR_COM *for_command;
{
- fprintf (stderr, "%s", indirection_level_string ());
- fprintf (stderr, "for %s in ", for_command->name->word);
- xtrace_print_word_list (for_command->map_list, 0);
+ CHECK_XTRACE_FP;
+ fprintf (xtrace_fp, "%s", indirection_level_string ());
+ fprintf (xtrace_fp, "for %s in ", for_command->name->word);
+ xtrace_print_word_list (for_command->map_list, 2);
}
static void
FOR_COM *for_command;
{
print_for_command_head (for_command);
-
cprintf (";");
newline ("do\n");
+
indentation += indentation_amount;
make_command_string_internal (for_command->action);
+ PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
+
newline ("done");
}
newline ("do\n");
indentation += indentation_amount;
make_command_string_internal (arith_for_command->action);
+ PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
newline ("done");
xtrace_print_select_command_head (select_command)
SELECT_COM *select_command;
{
- fprintf (stderr, "%s", indirection_level_string ());
- fprintf (stderr, "select %s in ", select_command->name->word);
- xtrace_print_word_list (select_command->map_list, 0);
+ CHECK_XTRACE_FP;
+ fprintf (xtrace_fp, "%s", indirection_level_string ());
+ fprintf (xtrace_fp, "select %s in ", select_command->name->word);
+ xtrace_print_word_list (select_command->map_list, 2);
}
static void
newline ("do\n");
indentation += indentation_amount;
make_command_string_internal (select_command->action);
+ PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
newline ("done");
}
make_command_string_internal (group_command->command);
+ PRINT_DEFERRED_HEREDOCS ("");
if (inside_function_def)
{
xtrace_print_case_command_head (case_command)
CASE_COM *case_command;
{
- fprintf (stderr, "%s", indirection_level_string ());
- fprintf (stderr, "case %s in\n", case_command->word->word);
+ CHECK_XTRACE_FP;
+ fprintf (xtrace_fp, "%s", indirection_level_string ());
+ fprintf (xtrace_fp, "case %s in\n", case_command->word->word);
}
static void
indentation += indentation_amount;
make_command_string_internal (clauses->action);
indentation -= indentation_amount;
+ PRINT_DEFERRED_HEREDOCS ("");
if (clauses->flags & CASEPAT_FALLTHROUGH)
newline (";&");
else if (clauses->flags & CASEPAT_TESTNEXT)
cprintf ("%s ", which);
skip_this_indent++;
make_command_string_internal (while_command->test);
+ PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
cprintf (" do\n"); /* was newline ("do\n"); */
indentation += indentation_amount;
make_command_string_internal (while_command->action);
+ PRINT_DEFERRED_HEREDOCS ("");
indentation -= indentation_amount;
semicolon ();
newline ("done");
cprintf (" then\n");
indentation += indentation_amount;
make_command_string_internal (if_command->true_case);
+ PRINT_DEFERRED_HEREDOCS ("");
indentation -= indentation_amount;
if (if_command->false_case)
newline ("else\n");
indentation += indentation_amount;
make_command_string_internal (if_command->false_case);
+ PRINT_DEFERRED_HEREDOCS ("");
indentation -= indentation_amount;
}
semicolon ();
newline ("fi");
}
-#if defined (DPAREN_ARITHMETIC)
+#if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
void
print_arith_command (arith_cmd_list)
WORD_LIST *arith_cmd_list;
}
#ifdef DEBUG
+void
+debug_print_word_list (s, list, sep)
+ char *s;
+ WORD_LIST *list;
+ char *sep;
+{
+ WORD_LIST *w;
+
+ if (s)
+ fprintf (stderr, "%s: ", s);
+ for (w = list; w; w = w->next)
+ fprintf (stderr, "%s%s", w->word->word, w->next ? sep : "");
+ fprintf (stderr, "\n");
+}
+
void
debug_print_cond_command (cond)
COND_COM *cond;
WORD_DESC *op;
char *arg1, *arg2;
{
+ CHECK_XTRACE_FP;
command_string_index = 0;
- fprintf (stderr, "%s", indirection_level_string ());
- fprintf (stderr, "[[ ");
+ fprintf (xtrace_fp, "%s", indirection_level_string ());
+ fprintf (xtrace_fp, "[[ ");
if (invert)
- fprintf (stderr, "! ");
+ fprintf (xtrace_fp, "! ");
if (type == COND_UNARY)
{
- fprintf (stderr, "%s ", op->word);
- fprintf (stderr, "%s", (arg1 && *arg1) ? arg1 : "''");
+ fprintf (xtrace_fp, "%s ", op->word);
+ fprintf (xtrace_fp, "%s", (arg1 && *arg1) ? arg1 : "''");
}
else if (type == COND_BINARY)
{
- fprintf (stderr, "%s", (arg1 && *arg1) ? arg1 : "''");
- fprintf (stderr, " %s ", op->word);
- fprintf (stderr, "%s", (arg2 && *arg2) ? arg2 : "''");
+ fprintf (xtrace_fp, "%s", (arg1 && *arg1) ? arg1 : "''");
+ fprintf (xtrace_fp, " %s ", op->word);
+ fprintf (xtrace_fp, "%s", (arg2 && *arg2) ? arg2 : "''");
}
- fprintf (stderr, " ]]\n");
+ fprintf (xtrace_fp, " ]]\n");
+
+ fflush (xtrace_fp);
}
#endif /* COND_COMMAND */
{
WORD_LIST *w;
- fprintf (stderr, "%s", indirection_level_string ());
- fprintf (stderr, "(( ");
+ CHECK_XTRACE_FP;
+ fprintf (xtrace_fp, "%s", indirection_level_string ());
+ fprintf (xtrace_fp, "(( ");
for (w = list; w; w = w->next)
- fprintf (stderr, "%s%s", w->word->word, w->next ? " " : "");
- fprintf (stderr, " ))\n");
+ fprintf (xtrace_fp, "%s%s", w->word->word, w->next ? " " : "");
+ fprintf (xtrace_fp, " ))\n");
+
+ fflush (xtrace_fp);
}
#endif
was_heredoc = 1;
}
+static void
+print_heredoc_bodies (heredocs)
+ REDIRECT *heredocs;
+{
+ REDIRECT *hdtail;
+
+ cprintf ("\n");
+ for (hdtail = heredocs; hdtail; hdtail = hdtail->next)
+ {
+ print_heredoc_body (hdtail);
+ cprintf ("\n");
+ }
+ was_heredoc = 1;
+}
+
/* Print heredocs that are attached to the command before the connector
represented by CSTRING. The parsing semantics require us to print the
here-doc delimiters, then the connector (CSTRING), then the here-doc
- bodies. We don't print the connector if it's a `;', but we use it to
- note not to print an extra space after the last heredoc body and
- newline. */
+ bodies. We print the here-doc delimiters in print_redirection_list
+ and print the connector and the bodies here. We don't print the connector
+ if it's a `;', but we use it to note not to print an extra space after the
+ last heredoc body and newline. */
static void
print_deferred_heredocs (cstring)
const char *cstring;
{
- REDIRECT *hdtail;
-
- for (hdtail = deferred_heredocs; hdtail; hdtail = hdtail->next)
- {
- cprintf (" ");
- print_heredoc_header (hdtail);
- }
- if (cstring[0] != ';' || cstring[1])
+ /* We now print the heredoc headers in print_redirection_list */
+ if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
cprintf ("%s", cstring);
- if (deferred_heredocs)
- cprintf ("\n");
- for (hdtail = deferred_heredocs; hdtail; hdtail = hdtail->next)
- {
- print_heredoc_body (hdtail);
- cprintf ("\n");
- }
if (deferred_heredocs)
{
- if (cstring[0] != ';' || cstring[1])
+ print_heredoc_bodies (deferred_heredocs);
+ if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
cprintf (" "); /* make sure there's at least one space */
dispose_redirects (deferred_heredocs);
+ was_heredoc = 1;
}
deferred_heredocs = (REDIRECT *)NULL;
}
REDIRECT *redirects;
{
REDIRECT *heredocs, *hdtail, *newredir;
+ char *rw;
heredocs = (REDIRECT *)NULL;
hdtail = heredocs;
was_heredoc = 0;
while (redirects)
{
- /* Defer printing the here documents until we've printed the
- rest of the redirections. */
+ /* Defer printing the here document bodiess until we've printed the rest of the
+ redirections, but print the headers in the order they're given. */
if (redirects->instruction == r_reading_until || redirects->instruction == r_deblank_reading_until)
{
newredir = copy_redirect (redirects);
newredir->next = (REDIRECT *)NULL;
+
+ print_heredoc_header (newredir);
+
if (heredocs)
{
hdtail->next = newredir;
else
hdtail = heredocs = newredir;
}
- else if (redirects->instruction == r_duplicating_output_word && redirects->redirector == 1)
+ else if (redirects->instruction == r_duplicating_output_word && (redirects->flags & REDIR_VARASSIGN) == 0 && redirects->redirector.dest == 1)
{
/* Temporarily translate it as the execution code does. */
- redirects->instruction = r_err_and_out;
+ rw = redirects->redirectee.filename->word;
+ if (rw && *rw != '-' && DIGIT (*rw) == 0 && EXPCHAR (*rw) == 0)
+ redirects->instruction = r_err_and_out;
print_redirection (redirects);
redirects->instruction = r_duplicating_output_word;
}
}
/* Now that we've printed all the other redirections (on one line),
- print the here documents. */
+ print the here documents. If we're printing a connection, we wait until
+ we print the connector symbol, then we print the here document bodies */
if (heredocs && printing_connection)
deferred_heredocs = heredocs;
else if (heredocs)
{
- print_heredocs (heredocs);
+ print_heredoc_bodies (heredocs);
dispose_redirects (heredocs);
}
}
kill_leading = redirect->instruction == r_deblank_reading_until;
/* Here doc header */
- if (redirect->redirector != 0)
- cprintf ("%d", redirect->redirector);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}", redirect->redirector.filename->word);
+ else if (redirect->redirector.dest != 0)
+ cprintf ("%d", redirect->redirector.dest);
/* If the here document delimiter is quoted, single-quote it. */
if (redirect->redirectee.filename->flags & W_QUOTED)
print_redirection (redirect)
REDIRECT *redirect;
{
- int kill_leading, redirector, redir_fd;
- WORD_DESC *redirectee;
+ int redirector, redir_fd;
+ WORD_DESC *redirectee, *redir_word;
- kill_leading = 0;
redirectee = redirect->redirectee.filename;
- redirector = redirect->redirector;
redir_fd = redirect->redirectee.dest;
+ redir_word = redirect->redirector.filename;
+ redirector = redirect->redirector.dest;
+
switch (redirect->instruction)
{
- case r_output_direction:
- if (redirector != 1)
+ case r_input_direction:
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}", redir_word->word);
+ else if (redirector != 0)
cprintf ("%d", redirector);
- cprintf ("> %s", redirectee->word);
+ cprintf ("< %s", redirectee->word);
break;
- case r_input_direction:
- if (redirector != 0)
+ case r_output_direction:
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}", redir_word->word);
+ else if (redirector != 1)
cprintf ("%d", redirector);
- cprintf ("< %s", redirectee->word);
+ cprintf ("> %s", redirectee->word);
break;
case r_inputa_direction: /* Redirection created by the shell. */
cprintf ("&");
break;
+ case r_output_force:
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}", redir_word->word);
+ else if (redirector != 1)
+ cprintf ("%d", redirector);
+ cprintf (">| %s", redirectee->word);
+ break;
+
case r_appending_to:
- if (redirector != 1)
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}", redir_word->word);
+ else if (redirector != 1)
cprintf ("%d", redirector);
cprintf (">> %s", redirectee->word);
break;
+ case r_input_output:
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}", redir_word->word);
+ else if (redirector != 1)
+ cprintf ("%d", redirector);
+ cprintf ("<> %s", redirectee->word);
+ break;
+
case r_deblank_reading_until:
case r_reading_until:
print_heredoc_header (redirect);
break;
case r_reading_string:
- if (redirector != 0)
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}", redir_word->word);
+ else if (redirector != 0)
cprintf ("%d", redirector);
+#if 0
+ /* Don't need to check whether or not to requote, since original quotes
+ are still intact. The only thing that has happened is that $'...'
+ has been replaced with 'expanded ...'. */
if (ansic_shouldquote (redirect->redirectee.filename->word))
{
char *x;
free (x);
}
else
+#endif
cprintf ("<<< %s", redirect->redirectee.filename->word);
break;
case r_duplicating_input:
- cprintf ("%d<&%d", redirector, redir_fd);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}<&%d", redir_word->word, redir_fd);
+ else
+ cprintf ("%d<&%d", redirector, redir_fd);
break;
case r_duplicating_output:
- cprintf ("%d>&%d", redirector, redir_fd);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}>&%d", redir_word->word, redir_fd);
+ else
+ cprintf ("%d>&%d", redirector, redir_fd);
break;
case r_duplicating_input_word:
- cprintf ("%d<&%s", redirector, redirectee->word);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}<&%s", redir_word->word, redirectee->word);
+ else
+ cprintf ("%d<&%s", redirector, redirectee->word);
break;
case r_duplicating_output_word:
- cprintf ("%d>&%s", redirector, redirectee->word);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}>&%s", redir_word->word, redirectee->word);
+ else
+ cprintf ("%d>&%s", redirector, redirectee->word);
break;
case r_move_input:
- cprintf ("%d<&%d-", redirector, redir_fd);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}<&%d-", redir_word->word, redir_fd);
+ else
+ cprintf ("%d<&%d-", redirector, redir_fd);
break;
case r_move_output:
- cprintf ("%d>&%d-", redirector, redir_fd);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}>&%d-", redir_word->word, redir_fd);
+ else
+ cprintf ("%d>&%d-", redirector, redir_fd);
break;
case r_move_input_word:
- cprintf ("%d<&%s-", redirector, redirectee->word);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}<&%s-", redir_word->word, redirectee->word);
+ else
+ cprintf ("%d<&%s-", redirector, redirectee->word);
break;
case r_move_output_word:
- cprintf ("%d>&%s-", redirector, redirectee->word);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}>&%s-", redir_word->word, redirectee->word);
+ else
+ cprintf ("%d>&%s-", redirector, redirectee->word);
break;
case r_close_this:
- cprintf ("%d>&-", redirector);
+ if (redirect->rflags & REDIR_VARASSIGN)
+ cprintf ("{%s}>&-", redir_word->word);
+ else
+ cprintf ("%d>&-", redirector);
break;
case r_err_and_out:
- cprintf ("&>%s", redirectee->word);
+ cprintf ("&> %s", redirectee->word);
break;
case r_append_err_and_out:
- cprintf ("&>>%s", redirectee->word);
- break;
-
- case r_input_output:
- if (redirector != 1)
- cprintf ("%d", redirector);
- cprintf ("<> %s", redirectee->word);
- break;
-
- case r_output_force:
- if (redirector != 1)
- cprintf ("%d", redirector);
- cprintf (">|%s", redirectee->word);
+ cprintf ("&>> %s", redirectee->word);
break;
}
}
REDIRECT *func_redirects;
func_redirects = NULL;
- cprintf ("function %s () \n", func->name->word);
+ /* When in posix mode, print functions as posix specifies them. */
+ if (posixly_correct == 0)
+ cprintf ("function %s () \n", func->name->word);
+ else
+ cprintf ("%s () \n", func->name->word);
add_unwind_protect (reset_locals, 0);
indent (indentation);
make_command_string_internal (cmdcopy->type == cm_group
? cmdcopy->value.Group->command
: cmdcopy);
+ PRINT_DEFERRED_HEREDOCS ("");
remove_unwind_protect ();
indentation -= indentation_amount;
deferred_heredocs = 0;
if (name && *name)
- cprintf ("%s ", name);
+ {
+ if (find_reserved_word (name) >= 0)
+ cprintf ("function ");
+ cprintf ("%s ", name);
+ }
cprintf ("() ");
make_command_string_internal (cmdcopy->type == cm_group
? cmdcopy->value.Group->command
: cmdcopy);
+ PRINT_DEFERRED_HEREDOCS ("");
indentation = old_indent;
indentation_amount = old_amount;
}
#else
if (result[2] == '\n') /* XXX -- experimental */
- strcpy (result + 2, result + 3);
+ memmove (result + 2, result + 3, strlen (result) - 2);
#endif
}
for (i = 0; amount > 0; amount--)
indentation_string[i++] = ' ';
indentation_string[i] = '\0';
- cprintf (indentation_string);
+ cprintf ("%s", indentation_string);
}
static void
{
if (command_string_index > 0 &&
(the_printed_command[command_string_index - 1] == '&' ||
- the_printed_command[command_string_index - 1] == '\n'))
+ the_printed_command[command_string_index - 1] == '\n'))
return;
cprintf (";");
}
#endif
{
register const char *s;
- char char_arg[2], *argp, intbuf[INT_STRLEN_BOUND (int) + 1];
+ char char_arg[2], *argp, intbuf[INT_STRLEN_BOUND (unsigned int) + 1];
int digit_arg, arg_len, c;
va_list args;
digit_arg = va_arg (args, int);
if (digit_arg < 0)
{
- sprintf (intbuf, "%u", (unsigned)-1);
+ sprintf (intbuf, "%u", (unsigned int)-1);
argp = intbuf;
}
else
- argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
+ argp = inttostr (digit_arg, intbuf, sizeof (intbuf));
arg_len = strlen (argp);
break;
}
}
+ va_end (args);
+
the_printed_command[command_string_index] = '\0';
}