From 617a3851426434bc22d82fe7574ba8f0455c0dcd Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 13 Apr 2024 05:55:51 +0800 Subject: vim-patch:9.1.0312: heredocs are not supported for :commands Problem: heredocs are not supported for :commands (balki) Solution: Add heredoc support (Yegappan Lakshmanan) fixes: vim/vim#14491 closes: vim/vim#14528 https://github.com/vim/vim/commit/e74cad3321ce1dcefc1fc64f617511275b6cd930 Co-authored-by: Yegappan Lakshmanan --- src/nvim/charset.c | 16 ++++++++++- src/nvim/eval/vars.c | 66 ++++++++++++++++++++++++++++++++----------- test/old/testdir/test_let.vim | 14 +++++++++ 3 files changed, 78 insertions(+), 18 deletions(-) diff --git a/src/nvim/charset.c b/src/nvim/charset.c index 2e6f24b2d5..59932b2eb0 100644 --- a/src/nvim/charset.c +++ b/src/nvim/charset.c @@ -1045,13 +1045,27 @@ char *skiptowhite(const char *p) return (char *)p; } +/// Skip over text until ' ' or '\t' or newline or NUL +/// +/// @param[in] p Text to skip over. +/// +/// @return Pointer to the next whitespace or newline or NUL character. +char *skiptowhite_or_nl(const char *p) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE +{ + while (*p != ' ' && *p != '\t' && *p != NL && *p != NUL) { + p++; + } + return (char *)p; +} + /// skiptowhite_esc: Like skiptowhite(), but also skip escaped chars /// /// @param p /// /// @return Pointer to the next whitespace character. char *skiptowhite_esc(const char *p) - FUNC_ATTR_PURE + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE { while (*p != ' ' && *p != '\t' && *p != NUL) { if (((*p == '\\') || (*p == Ctrl_V)) && (*(p + 1) != NUL)) { diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index 65e648b625..70ecb76deb 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -62,8 +62,8 @@ static const char e_double_semicolon_in_list_of_variables[] static const char *e_lock_unlock = N_("E940: Cannot lock or unlock variable %s"); static const char e_setting_v_str_to_value_with_wrong_type[] = N_("E963: Setting v:%s to value with wrong type"); -static const char e_cannot_use_heredoc_here[] - = N_("E991: Cannot use =<< here"); +static const char e_missing_end_marker_str[] = N_("E990: Missing end marker '%s'"); +static const char e_cannot_use_heredoc_here[] = N_("E991: Cannot use =<< here"); /// Evaluate one Vim expression {expr} in string "p" and append the /// resulting string to "gap". "p" points to the opening "{". @@ -180,7 +180,7 @@ list_T *heredoc_get(exarg_T *eap, char *cmd, bool script_get) char *text_indent = NULL; char dot[] = "."; - if (eap->ea_getline == NULL) { + if (eap->ea_getline == NULL && vim_strchr(cmd, '\n') == NULL) { emsg(_(e_cannot_use_heredoc_here)); return NULL; } @@ -216,11 +216,18 @@ list_T *heredoc_get(exarg_T *eap, char *cmd, bool script_get) break; } + const char comment_char = '"'; + bool heredoc_in_string = false; + char *line_arg = NULL; // The marker is the next word. - if (*cmd != NUL && *cmd != '"') { + if (*cmd != NUL && *cmd != comment_char) { marker = skipwhite(cmd); - char *p = skiptowhite(marker); - if (*skipwhite(p) != NUL && *skipwhite(p) != '"') { + char *p = skiptowhite_or_nl(marker); + if (*p == NL) { + // heredoc in a string + line_arg = p + 1; + heredoc_in_string = true; + } else if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char) { semsg(_(e_trailing_arg), p); return NULL; } @@ -246,13 +253,34 @@ list_T *heredoc_get(exarg_T *eap, char *cmd, bool script_get) int mi = 0; int ti = 0; - xfree(theline); - theline = eap->ea_getline(NUL, eap->cookie, 0, false); - if (theline == NULL) { - if (!script_get) { - semsg(_("E990: Missing end marker '%s'"), marker); + if (heredoc_in_string) { + // heredoc in a string separated by newlines. Get the next line + // from the string. + + if (*line_arg == NUL) { + if (!script_get) { + semsg(_(e_missing_end_marker_str), marker); + } + break; + } + + theline = line_arg; + char *next_line = vim_strchr(theline, '\n'); + if (next_line == NULL) { + line_arg += strlen(line_arg); + } else { + *next_line = NUL; + line_arg = next_line + 1; + } + } else { + xfree(theline); + theline = eap->ea_getline(NUL, eap->cookie, 0, false); + if (theline == NULL) { + if (!script_get) { + semsg(_(e_missing_end_marker_str), marker); + } + break; } - break; } // with "trim": skip the indent matching the :let line to find the @@ -298,13 +326,17 @@ list_T *heredoc_get(exarg_T *eap, char *cmd, bool script_get) eval_failed = true; continue; } - xfree(theline); - theline = str; + tv_list_append_allocated_string(l, str); + } else { + tv_list_append_string(l, str, -1); } - - tv_list_append_string(l, str, -1); } - xfree(theline); + if (heredoc_in_string) { + // Next command follows the heredoc in the string. + eap->nextcmd = line_arg; + } else { + xfree(theline); + } xfree(text_indent); if (eval_failed) { diff --git a/test/old/testdir/test_let.vim b/test/old/testdir/test_let.vim index 655c177385..56b880f3b0 100644 --- a/test/old/testdir/test_let.vim +++ b/test/old/testdir/test_let.vim @@ -713,6 +713,20 @@ END LINES call CheckScriptFailure(lines, 'E15:') + " Test for using heredoc in a single string using execute() + call assert_equal(["['one', 'two']"], + \ execute("let x =<< trim END\n one\n two\nEND\necho x")->split("\n")) + call assert_equal(["[' one', ' two']"], + \ execute("let x =<< END\n one\n two\nEND\necho x")->split("\n")) + let cmd = 'execute("let x =<< END\n one\n two\necho x")' + call assert_fails(cmd, "E990: Missing end marker 'END'") + let cmd = 'execute("let x =<<\n one\n two\necho x")' + call assert_fails(cmd, "E990: Missing end marker ''") + let cmd = 'execute("let x =<< trim\n one\n two\necho x")' + call assert_fails(cmd, "E221: Marker cannot start with lower case letter") + let cmd = 'execute("let x =<< eval END\n one\n two{y}\nEND\necho x")' + call assert_fails(cmd, 'E121: Undefined variable: y') + " skipped heredoc if 0 let msg =<< trim eval END -- cgit From e81fe387d6291e5643a97a61e6d05b48aaeab2a1 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 14 Apr 2024 05:03:49 +0800 Subject: vim-patch:9.1.0313: Crash when using heredoc with comment in command block Problem: Crash when using heredoc with comment in command block. Solution: Handle a newline more like the end of the line, fix coverity warning (zeertzjq). closes: vim/vim#14535 https://github.com/vim/vim/commit/1f5175d9af3d3f37e19f23e0e6f84caec47390f2 --- src/nvim/charset.c | 14 -------------- src/nvim/eval/vars.c | 19 ++++++++++--------- test/old/testdir/test_let.vim | 29 ++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/nvim/charset.c b/src/nvim/charset.c index 59932b2eb0..c611d4cfd6 100644 --- a/src/nvim/charset.c +++ b/src/nvim/charset.c @@ -1045,20 +1045,6 @@ char *skiptowhite(const char *p) return (char *)p; } -/// Skip over text until ' ' or '\t' or newline or NUL -/// -/// @param[in] p Text to skip over. -/// -/// @return Pointer to the next whitespace or newline or NUL character. -char *skiptowhite_or_nl(const char *p) - FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE -{ - while (*p != ' ' && *p != '\t' && *p != NL && *p != NUL) { - p++; - } - return (char *)p; -} - /// skiptowhite_esc: Like skiptowhite(), but also skip escaped chars /// /// @param p diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index 70ecb76deb..37199cd95d 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -179,8 +179,15 @@ list_T *heredoc_get(exarg_T *eap, char *cmd, bool script_get) int text_indent_len = 0; char *text_indent = NULL; char dot[] = "."; + bool heredoc_in_string = false; + char *line_arg = NULL; + char *nl_ptr = vim_strchr(cmd, '\n'); - if (eap->ea_getline == NULL && vim_strchr(cmd, '\n') == NULL) { + if (nl_ptr != NULL) { + heredoc_in_string = true; + line_arg = nl_ptr + 1; + *nl_ptr = NUL; + } else if (eap->ea_getline == NULL) { emsg(_(e_cannot_use_heredoc_here)); return NULL; } @@ -217,17 +224,11 @@ list_T *heredoc_get(exarg_T *eap, char *cmd, bool script_get) } const char comment_char = '"'; - bool heredoc_in_string = false; - char *line_arg = NULL; // The marker is the next word. if (*cmd != NUL && *cmd != comment_char) { marker = skipwhite(cmd); - char *p = skiptowhite_or_nl(marker); - if (*p == NL) { - // heredoc in a string - line_arg = p + 1; - heredoc_in_string = true; - } else if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char) { + char *p = skiptowhite(marker); + if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char) { semsg(_(e_trailing_arg), p); return NULL; } diff --git a/test/old/testdir/test_let.vim b/test/old/testdir/test_let.vim index 56b880f3b0..da4334833b 100644 --- a/test/old/testdir/test_let.vim +++ b/test/old/testdir/test_let.vim @@ -542,6 +542,13 @@ END XX call assert_equal(['Line1'], var1) + let var1 =<< trim XX " comment + Line1 + Line2 + Line3 + XX + call assert_equal(['Line1', ' Line2', 'Line3'], var1) + " ignore "endfunc" let var1 =<< END something @@ -714,15 +721,27 @@ END call CheckScriptFailure(lines, 'E15:') " Test for using heredoc in a single string using execute() - call assert_equal(["['one', 'two']"], - \ execute("let x =<< trim END\n one\n two\nEND\necho x")->split("\n")) - call assert_equal(["[' one', ' two']"], - \ execute("let x =<< END\n one\n two\nEND\necho x")->split("\n")) + call assert_equal("\n['one', 'two']", + \ execute("let x =<< trim END\n one\n two\nEND\necho x")) + call assert_equal("\n['one', ' two']", + \ execute("let x =<< trim END\n one\n two\nEND\necho x")) + call assert_equal("\n['one', 'two']", + \ execute(" let x =<< trim END\n one\n two\n END\necho x")) + call assert_equal("\n['one', ' two']", + \ execute(" let x =<< trim END\n one\n two\n END\necho x")) + call assert_equal("\n[' one', ' two']", + \ execute("let x =<< END\n one\n two\nEND\necho x")) + call assert_equal("\n['one', 'two']", + \ execute("let x =<< END\none\ntwo\nEND\necho x")) + call assert_equal("\n['one', 'two']", + \ execute("let x =<< END \" comment\none\ntwo\nEND\necho x")) let cmd = 'execute("let x =<< END\n one\n two\necho x")' call assert_fails(cmd, "E990: Missing end marker 'END'") let cmd = 'execute("let x =<<\n one\n two\necho x")' - call assert_fails(cmd, "E990: Missing end marker ''") + call assert_fails(cmd, "E172: Missing marker") let cmd = 'execute("let x =<< trim\n one\n two\necho x")' + call assert_fails(cmd, "E172: Missing marker") + let cmd = 'execute("let x =<< end\n one\n two\nend\necho x")' call assert_fails(cmd, "E221: Marker cannot start with lower case letter") let cmd = 'execute("let x =<< eval END\n one\n two{y}\nEND\necho x")' call assert_fails(cmd, 'E121: Undefined variable: y') -- cgit