aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-05-02 18:22:20 -0700
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-05-02 18:22:20 -0700
commit3ca518255e7be2af5fe4a6b2a02e33dd232729d9 (patch)
tree0c48abaadd3f9e820d785f2740c006482ae42975
parentc59119e0b77d08fcaab7a620920e565baa06aee7 (diff)
Treat comments ending in backslashes as not continuing onto the next line
Fixes #1255
-rw-r--r--reader.cpp25
-rw-r--r--tests/generic.expect6
-rw-r--r--tests/test1.in5
3 files changed, 34 insertions, 2 deletions
diff --git a/reader.cpp b/reader.cpp
index 2700da0f..516a099e 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 7a5b6407..33d260a7 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 b9db902a..cfea247a 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