/* print_command -- A way to make readable commands from a command tree. */
-/* Copyright (C) 1989-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2011 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
#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) \
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
make_command_string_internal (command)
COMMAND *command;
{
- char s[3], *op;
+ char s[3];
if (command == 0)
cprintf ("");
}
make_command_string_internal (command->value.Connection->second);
- if (deferred_heredocs)
- print_deferred_heredocs ("");
+ PRINT_DEFERRED_HEREDOCS ("");
printing_connection--;
break;
cprintf ("( ");
skip_this_indent++;
make_command_string_internal (command->value.Subshell->command);
+ PRINT_DEFERRED_HEREDOCS ("");
cprintf (" )");
break;
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';
newline ("do\n");
indentation += indentation_amount;
make_command_string_internal (arith_for_command->action);
+ PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
newline ("done");
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)
{
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 ();
#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;
{
cprintf (" ");
print_heredoc_header (hdtail);
}
- if (cstring[0] != ';' || cstring[1])
+ if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
cprintf ("%s", cstring);
if (deferred_heredocs)
cprintf ("\n");
REDIRECT *redirects;
{
REDIRECT *heredocs, *hdtail, *newredir;
+ char *rw;
heredocs = (REDIRECT *)NULL;
hdtail = heredocs;
else
hdtail = heredocs = newredir;
}
- else if (redirects->instruction == r_duplicating_output_word && redirects->redirector.dest == 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;
}
print_redirection (redirect)
REDIRECT *redirect;
{
- int kill_leading, redirector, redir_fd;
+ int redirector, redir_fd;
WORD_DESC *redirectee, *redir_word;
- kill_leading = 0;
redirectee = redirect->redirectee.filename;
redir_fd = redirect->redirectee.dest;
cprintf ("{%s}", redir_word->word);
else if (redirector != 1)
cprintf ("%d", redirector);
- cprintf (">|%s", redirectee->word);
+ cprintf (">| %s", redirectee->word);
break;
case r_appending_to:
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;
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);
+ cprintf ("&>> %s", redirectee->word);
break;
}
}
make_command_string_internal (cmdcopy->type == cm_group
? cmdcopy->value.Group->command
: cmdcopy);
+ /* XXX - PRINT_DEFERRED_HEREDOCS (""); ? */
remove_unwind_protect ();
indentation -= indentation_amount;
make_command_string_internal (cmdcopy->type == cm_group
? cmdcopy->value.Group->command
: cmdcopy);
+ /* XXX - 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
}
}
+ va_end (args);
+
the_printed_command[command_string_index] = '\0';
}