/* print_command -- A way to make readable commands from a command tree. */
-/* Copyright (C) 1989-2010 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2016 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
#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"
#endif
extern int indirection_level;
+extern int posixly_correct;
static int indentation;
static int indentation_amount = 4;
#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
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';
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;
CHECK_XTRACE_FP;
- if (xtflags)
+ 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 (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);
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, 0);
+ xtrace_print_word_list (for_command->map_list, 2);
}
static void
newline ("do\n");
indentation += indentation_amount;
make_command_string_internal (arith_for_command->action);
+ PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
newline ("done");
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, 0);
+ xtrace_print_word_list (select_command->map_list, 2);
}
static void
}
make_command_string_internal (group_command->command);
+ PRINT_DEFERRED_HEREDOCS ("");
if (inside_function_def)
{
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;
cprintf (" ");
print_heredoc_header (hdtail);
}
- if (cstring[0] && (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;
}
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:
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;
}
}
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);
+ /* XXX - 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);
+ /* 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';
}