* the redirection expression. For example:
* echo \2>a
* writes the character 2 into file a"
- * I am not sure we do it right (and not sure we care)
+ * We are getting it right by setting ->o_quoted on any \<char>
*
* A -1 return means no valid number was found,
* the caller should use the appropriate default for this redirection.
}
}
+/* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs
+ * and load them all. There should be exactly heredoc_cnt of them.
+ */
static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input)
{
struct pipe *pi = ctx->list_head;
if (redir->rd_type == REDIRECT_HEREDOC) {
char *p;
- if (heredoc_cnt <= 0)
+ if (heredoc_cnt <= 0) {
+ syntax("heredoc BUG 1");
return 1; /* error */
+ }
redir->rd_type = REDIRECT_HEREDOC2;
/* redir->dup is (ab)used to indicate <<- */
p = fetch_till_str(&ctx->as_string, input,
redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS);
- if (!p)
- return 1; /* unexpected EOF */
+ if (!p) {
+ syntax("unexpected EOF in here document");
+ return 1;
+ }
free(redir->rd_filename);
redir->rd_filename = p;
heredoc_cnt--;
pi = pi->next;
}
/* Should be 0. If it isn't, it's a parse error */
+ if (heredoc_cnt)
+ syntax("heredoc BUG 2");
return heredoc_cnt;
}
if (ch != EOF)
nommu_addchr(as_string, ch);
if (ch == dquote_end) { /* may be only '"' or EOF */
- dest->o_quoted = 1;
if (dest->o_assignment == NOT_ASSIGNMENT)
dest->o_escape ^= 1;
debug_printf_parse("parse_stream_dquoted return 0\n");
return 0;
}
+ /* note: can't move it above ch == dquote_end check! */
if (ch == EOF) {
syntax("unterminated \"");
debug_printf_parse("parse_stream_dquoted return 1: unterminated \"\n");
redir_type redir_style;
if (is_in_dquote) {
+ /* dest.o_quoted = 1; - already is (see below) */
if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) {
goto parse_error;
}
done_pipe(&ctx, PIPE_SEQ);
debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt);
if (heredoc_cnt) {
- if (fetch_heredocs(heredoc_cnt, &ctx, input))
+ if (fetch_heredocs(heredoc_cnt, &ctx, input)) {
goto parse_error;
+ }
heredoc_cnt = 0;
}
dest.o_assignment = MAYBE_ASSIGNMENT;
i_getch(input);
/* note: we do not add it to &ctx.as_string */
}
-//TODO: go back one char?
nommu_addchr(&ctx.as_string, '\n');
} else {
o_addQchr(&dest, ch);
}
o_addchr(&dest, '\\');
ch = i_getch(input);
- o_addchr(&dest, ch);
nommu_addchr(&ctx.as_string, ch);
+ o_addchr(&dest, ch);
+ /* Example: echo Hello \2>file
+ * we need to know that word 2 is quoted */
+ dest.o_quoted = 1;
break;
case '$':
if (handle_dollar(&ctx.as_string, &dest, input) != 0) {