From a2dfd87928f4711223274688e34b876d33986853 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 25 Jun 2021 22:40:43 -0700 Subject: [PATCH] Simplify parse_util_get_parameter_info We no longer use any part of the "parameter info" except its quote type. Just return the quote type directly. --- src/parse_util.cpp | 45 ++++++--------------------------------------- src/parse_util.h | 8 ++------ src/reader.cpp | 4 ++-- 3 files changed, 10 insertions(+), 47 deletions(-) diff --git a/src/parse_util.cpp b/src/parse_util.cpp index c93307f2c..b21a36266 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -489,7 +489,7 @@ wcstring parse_util_unescape_wildcards(const wcstring &str) { static wchar_t get_quote(const wcstring &cmd_str, size_t len) { size_t i = 0; wchar_t res = 0; - const wchar_t *const cmd = cmd_str.c_str(); + const wchar_t *cmd = cmd_str.c_str(); while (true) { if (!cmd[i]) break; @@ -501,7 +501,6 @@ static wchar_t get_quote(const wcstring &cmd_str, size_t len) { } else { if (cmd[i] == L'\'' || cmd[i] == L'\"') { const wchar_t *end = quote_end(&cmd[i], cmd[i]); - // std::fwprintf( stderr, L"Jump %d\n", end-cmd ); if ((end == nullptr) || (!*end) || (end > cmd + len)) { res = cmd[i]; break; @@ -515,47 +514,15 @@ static wchar_t get_quote(const wcstring &cmd_str, size_t len) { return res; } -void parse_util_get_parameter_info(const wcstring &cmd, const size_t pos, wchar_t *quote, - size_t *offset, token_type_t *out_type) { - size_t prev_pos = 0; - wchar_t last_quote = L'\0'; - +wchar_t parse_util_get_quote_type(const wcstring &cmd, size_t pos) { tokenizer_t tok(cmd.c_str(), TOK_ACCEPT_UNFINISHED); while (auto token = tok.next()) { - if (token->offset > pos) break; - - if (token->type == token_type_t::string) - last_quote = get_quote(tok.text_of(*token), pos - token->offset); - - if (out_type != nullptr) *out_type = token->type; - - prev_pos = token->offset; - } - - wchar_t *cmd_tmp = wcsdup(cmd.c_str()); - cmd_tmp[pos] = 0; - size_t cmdlen = pos; - bool finished = cmdlen != 0; - if (finished) { - finished = (quote == nullptr); - if (finished && std::wcschr(L" \t\n\r", cmd_tmp[cmdlen - 1])) { - finished = cmdlen > 1 && cmd_tmp[cmdlen - 2] == L'\\'; + if (token->type == token_type_t::string && + token->location_in_or_at_end_of_source_range(pos)) { + return get_quote(tok.text_of(*token), pos - token->offset); } } - - if (quote) *quote = last_quote; - - if (offset != nullptr) { - if (finished) { - while ((cmd_tmp[prev_pos] != 0) && (std::wcschr(L";|", cmd_tmp[prev_pos]) != nullptr)) - prev_pos++; - *offset = prev_pos; - } else { - *offset = pos; - } - } - - free(cmd_tmp); + return L'\0'; } wcstring parse_util_escape_string_with_quote(const wcstring &cmd, wchar_t quote, bool no_tilde) { diff --git a/src/parse_util.h b/src/parse_util.h index 21ed6dab2..d446075e1 100644 --- a/src/parse_util.h +++ b/src/parse_util.h @@ -100,12 +100,8 @@ bool parse_util_argument_is_help(const wcstring &s); /// /// \param cmd The command to be analyzed /// \param pos An index in the string which is inside the parameter -/// \param quote If not NULL, store the type of quote this parameter has, can be either ', " or \\0, -/// meaning the string is not quoted. -/// \param offset If not NULL, get_param will store the offset to the beginning of the parameter. -/// \param out_type If not NULL, get_param will store the token type. -void parse_util_get_parameter_info(const wcstring &cmd, const size_t pos, wchar_t *quote, - size_t *offset, token_type_t *out_type); +/// \return the type of quote used by the parameter: either ' or " or \0. +wchar_t parse_util_get_quote_type(const wcstring &cmd, size_t pos); /// Attempts to escape the string 'cmd' using the given quote type, as determined by the quote /// character. The quote can be a single quote or double quote, or L'\0' to indicate no quoting (and diff --git a/src/reader.cpp b/src/reader.cpp index 0a170153a..c3df51997 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1592,9 +1592,9 @@ wcstring completion_apply_to_command_line(const wcstring &val, complete_flags_t // Find the last quote in the token to complete. By parsing only the string inside any // command substitution, we prevent the tokenizer from treating the entire command // substitution as one token. - parse_util_get_parameter_info( + quote = parse_util_get_quote_type( command_line.substr(cmdsub_offset, (cmdsub_end - cmdsub_begin)), - cursor_pos - cmdsub_offset, "e, nullptr, nullptr); + cursor_pos - cmdsub_offset); // If the token is reported as unquoted, but ends with a (unescaped) quote, and we can // modify the command line, then delete the trailing quote so that we can insert within