From 3ca518255e7be2af5fe4a6b2a02e33dd232729d9 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 2 May 2015 18:22:20 -0700 Subject: [PATCH] Treat comments ending in backslashes as not continuing onto the next line Fixes #1255 --- reader.cpp | 25 +++++++++++++++++++++++-- tests/generic.expect | 6 ++++++ tests/test1.in | 5 +++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/reader.cpp b/reader.cpp index 2700da0fd..516a099e2 100644 --- a/reader.cpp +++ b/reader.cpp @@ -3065,6 +3065,18 @@ static wchar_t unescaped_quote(const wcstring &str, size_t pos) return result; } +/* Returns true if the last token is a comment. */ +static bool text_ends_in_comment(const wcstring &text) +{ + token_type last_type = TOK_NONE; + tokenizer_t tok(text.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SHOW_COMMENTS | TOK_SQUASH_ERRORS); + while (tok_has_next(&tok)) + { + last_type = tok_last_type(&tok); + tok_next(&tok); + } + return last_type == TOK_COMMENT; +} const wchar_t *reader_readline(int nchars) { @@ -3561,10 +3573,19 @@ const wchar_t *reader_readline(int nchars) /* We only execute the command line */ editable_line_t *el = &data->command_line; - /* Allow backslash-escaped newlines, but only if the following character is whitespace, or we're at the end of the text (see issue #613) */ + /* Allow backslash-escaped newlines, but only if the following character is whitespace, or we're at the end of the text (see issue #613) and not in a comment (#1255). */ if (is_backslashed(el->text, el->position)) { - if (el->position >= el->size() || iswspace(el->text.at(el->position))) + bool continue_on_next_line = false; + if (el->position >= el->size()) + { + continue_on_next_line = ! text_ends_in_comment(el->text); + } + else + { + continue_on_next_line = iswspace(el->text.at(el->position)); + } + if (continue_on_next_line) { insert_char(el, '\n'); break; diff --git a/tests/generic.expect b/tests/generic.expect index 7a5b64070..33d260a71 100644 --- a/tests/generic.expect +++ b/tests/generic.expect @@ -33,3 +33,9 @@ send_line $hist_command expect_prompt -re {echo .history.*} {} unmatched { puts stderr "Couldn't find expected output $hist_command" } + +# Backslashes at end of comments (#1255) +# This backslash should NOT cause the line to continue +send_line "echo -n #comment\\" +expect_prompt + diff --git a/tests/test1.in b/tests/test1.in index b9db902ae..cfea247a3 100644 --- a/tests/test1.in +++ b/tests/test1.in @@ -155,6 +155,11 @@ echo before comment \ # comment after comment +# Backslashes are part of comments and do not join lines (#1255) +# This should execute false, not echo it +echo -n # comment\ +false + function always_fails if true return 1