From patchwork Fri Mar 29 11:42:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 87812 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5B0D93858C55 for ; Fri, 29 Mar 2024 11:43:12 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id F1BE63858D28 for ; Fri, 29 Mar 2024 11:42:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1BE63858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F1BE63858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711712562; cv=none; b=vqTAJEfvyc7pTDTBiKC1c8J2K1t46F6HKCC0k7uEYALIhTlQMKGBdhGmsDcrFijJzfi+KUENl99egVYJR65pA7pE+pHBBMfiL1KiYrZBiYTWKJRwL3UNQMlMid7jUjvckd9yMutkQkyo4BYhdiLG/5ly+sYMELE//cUmbUFnVog= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711712562; c=relaxed/simple; bh=sXsBC3Uz2JBl1Qru4bsWgSENlDwIFOMK76JfM3Rh2TU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=xynEL84GGKo8FR2ECsu950v4v7X6pxpSC3Mbk0o91TOpGJNtfWkyLYESvldLSRhtHswxiAOztJX983TO4cjBHyL+L19E3kXpkzJi2SxBYrgQC33bGKg1ztppxgsYINUN5GvClS26GQL8fCbas9I1LDxrQf28RYJilPsmPZAfX0A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711712559; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=74UtgUnjzOsWpAVWoqjkzLOBBp6kPCNwfk2+nQX/soE=; b=ZPz8fAYrvpKSevzn8Rt4oZf23eHbWGd18nxFWEnas8AsfWNrhaJVkUEo5Tjbzn6KeQnKpz Evv80fnybmQzaJAo5CBmHuKnVGosNOKBjzaiUiHQ4IQ6g18UsEWEUhQGBeCG1pjWIxtFst tBoATyXgRC/lKRk11OSFGeibvrdK70c= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-577-MzhCx5uNOm-Qggbq2NS1fg-1; Fri, 29 Mar 2024 07:42:37 -0400 X-MC-Unique: MzhCx5uNOm-Qggbq2NS1fg-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a4dfdbdaf06so103077066b.3 for ; Fri, 29 Mar 2024 04:42:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711712555; x=1712317355; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=74UtgUnjzOsWpAVWoqjkzLOBBp6kPCNwfk2+nQX/soE=; b=gUodiMVUCLerwpUu/JX6YV76eGsVQ3QePukpfPM0SyLnArZDnjHh44FFlnOLVFg8nA FLBzQpV0D6Vv+Nks4TD3q4P+vljsdfv9aaWc3lLHr3su5KbGmzSQAv3z780J5HFhb/6a 9A9S6BzJtN9EDwH171hAgk2J9C95tYWt8QEHYqcevcnn6cEPftgBWkjQYegdzS8yrchf ao0kh+p72nzbmHbJenBzoARWoJIPA39WvuRX60qVDzVTeO+sAP35Wiq6yO278VCWep1d Lt8ALFsE9QjcKTwR4RVe9rNKPZowP3Z22Scair2x57wZQ+e6FYAPx5nEtcx0ys5u0Mpq 0gbg== X-Gm-Message-State: AOJu0YyxKXDxlryg7l5+4Fi3Vtt1DumoXIEhZW6qQO1kStaYEDKbyYU8 bV5lnm8aE9v8jbT8xgNfQ+W+OE6lH1zK4X+5UusGU4JCTqCt5bb0An6CmFsAt3zxiJmCVfx8dzK 01rehIO2HtO4NS3wg3l9Y7txbz14fVn+56WdfumekQlRBOx3xb6XLrHQHuCXYlKsnTx3z0RJQ+X mapXvd18vOiRQE0DKLR8iXeJ/lOOG2RI0saBlRis1GiM4= X-Received: by 2002:a05:6402:268f:b0:56b:f54a:6eee with SMTP id w15-20020a056402268f00b0056bf54a6eeemr1587968edd.0.1711712555381; Fri, 29 Mar 2024 04:42:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3vI9kLWx22cozrScc+aVTJB38OlxVt9uvGpbtSnjwm5dzBx+8uNMyPCCtok7tBZxP7vcIyA== X-Received: by 2002:a05:6402:268f:b0:56b:f54a:6eee with SMTP id w15-20020a056402268f00b0056bf54a6eeemr1587946edd.0.1711712554769; Fri, 29 Mar 2024 04:42:34 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id cf2-20020a0564020b8200b0056b0af78d80sm1945221edb.34.2024.03.29.04.42.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 04:42:34 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 1/6] gdb: improve escaping when completing filenames Date: Fri, 29 Mar 2024 11:42:27 +0000 Message-Id: <0543026b57b831d545d99e325d6e6c3ed9c4c968.1711712401.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This commit is a step towards improving filename quoting when completing filenames. I've struggled a bit trying to split this series into chunks. There's a lot of dependencies between different parts of the completion system, and trying to get this working correctly is pretty messy. This first step is really about implementing 3 readline hooks: rl_char_is_quoted_p - Is a particular character quoted within readline's input buffer? rl_filename_dequoting_function - Remove quoting characters from a filename. rl_filename_quoting_function - Add quoting characters to a filename. See 'info readline' for more details. There's still a bunch of stuff that doesn't work after this commit, mostly around the 'complete' command which of course doesn't go through readline, so doesn't benefit from all of these new functions yet, I'll add some of this in a later commit. Tab completion is now slightly improved though, it is possible to tab-complete a filename that includes a double or single quote, either in an unquoted string or within a string surrounded by single or double quotes, backslash escaping is used when necessary. There are some additional tests to cover the new functionality. --- gdb/completer.c | 163 +++++++++++++++++- .../gdb.base/filename-completion.exp | 34 ++++ 2 files changed, 194 insertions(+), 3 deletions(-) diff --git a/gdb/completer.c b/gdb/completer.c index 8e34e30f46b..4cda5f3a383 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -204,6 +204,153 @@ noop_completer (struct cmd_list_element *ignore, { } +/* Return 1 if the character at EINDEX in STRING is quoted (there is an + unclosed quoted string), or if the character at EINDEX is quoted by a + backslash. */ + +static int +gdb_completer_file_name_char_is_quoted (char *string, int eindex) +{ + for (int i = 0; i <= eindex && string[i] != '\0'; ) + { + char c = string[i]; + + if (c == '\\') + { + /* The backslash itself is not quoted. */ + if (i >= eindex) + return 0; + ++i; + /* But the next character is. */ + if (i >= eindex) + return 1; + if (string[i] == '\0') + return 0; + ++i; + continue; + } + else if (strchr (rl_completer_quote_characters, c) != nullptr) + { + /* This assumes that extract_string_maybe_quoted can handle a + string quoted with character C. Currently this is true as the + only characters we put in rl_completer_quote_characters are + single and/or double quotes, both of which + extract_string_maybe_quoted can handle. */ + const char *tmp = &string[i]; + (void) extract_string_maybe_quoted (&tmp); + i = tmp - string; + + /* Consider any character within the string we just skipped over + as quoted, though this might not be completely correct; the + opening and closing quotes are not themselves quoted. But so + far this doesn't seem to have caused any issues. */ + if (i >= eindex) + return 1; + } + else + ++i; + } + + return 0; +} + +/* Removing character escaping from FILENAME. QUOTE_CHAR is the quote + character around FILENAME or the null-character if there is no quoting + around FILENAME. */ + +static char * +gdb_completer_file_name_dequote (char *filename, int quote_char) +{ + std::string tmp; + + if (quote_char == '\'') + { + /* There is no backslash escaping within a single quoted string. In + this case we can just return the input string. */ + tmp = filename; + } + else if (quote_char == '"') + { + /* Remove escaping from a double quoted string. */ + for (const char *input = filename; + *input != '\0'; + ++input) + { + if (input[0] == '\\' + && input[1] != '\0' + && strchr ("\"\\", input[1]) != nullptr) + ++input; + tmp += *input; + } + } + else + { + /* Remove escaping from an unquoted string. */ + for (const char *input = filename; + *input != '\0'; + ++input) + { + /* We allow anything to be escaped in an unquoted string. */ + if (*input == '\\') + { + ++input; + if (*input == '\0') + break; + } + + tmp += *input; + } + } + + return strdup (tmp.c_str ()); +} + +/* Apply character escaping to the file name in TEXT. QUOTE_PTR points to + the quote character surrounding TEXT, or points to the null-character if + there are no quotes around TEXT. MATCH_TYPE will be one of the readline + constants SINGLE_MATCH or MULTI_MATCH depending on if there is one or + many completions. */ + +static char * +gdb_completer_file_name_quote (char *text, int match_type, char *quote_ptr) +{ + std::string str; + + if (*quote_ptr == '\'') + { + /* There is no backslash escaping permitted within a single quoted + string, so in this case we can just return the input sting. */ + str = text; + } + else if (*quote_ptr == '"') + { + /* Add escaping for a double quoted filename. */ + for (const char *input = text; + *input != '\0'; + ++input) + { + if (strchr ("\"\\", *input) != nullptr) + str += '\\'; + str += *input; + } + } + else + { + /* Add escaping for an unquoted filename. */ + for (const char *input = text; + *input != '\0'; + ++input) + { + if (strchr (" \t\n\\\"'", *input) + != nullptr) + str += '\\'; + str += *input; + } + } + + return strdup (str.c_str ()); +} + /* Complete on filenames. */ void @@ -211,6 +358,7 @@ filename_completer (struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word) { + rl_char_is_quoted_p = gdb_completer_file_name_char_is_quoted; rl_completer_quote_characters = gdb_completer_file_name_quote_characters; int subsequent_name = 0; @@ -262,6 +410,7 @@ filename_completer_handle_brkchars (struct cmd_list_element *ignore, (gdb_completer_file_name_break_characters); rl_completer_quote_characters = gdb_completer_file_name_quote_characters; + rl_char_is_quoted_p = gdb_completer_file_name_char_is_quoted; } /* Find the bounds of the current word for completion purposes, and @@ -1261,6 +1410,7 @@ complete_line_internal_1 (completion_tracker &tracker, completing file names then we can switch to the file name quote character set (i.e., both single- and double-quotes). */ rl_completer_quote_characters = gdb_completer_expression_quote_characters; + rl_char_is_quoted_p = nullptr; /* Decide whether to complete on a list of gdb commands or on symbols. */ @@ -2153,9 +2303,11 @@ completion_tracker::build_completion_result (const char *text, /* Build replacement word, based on the LCD. */ recompute_lowest_common_denominator (); - match_list[0] - = expand_preserving_ws (text, end - start, - m_lowest_common_denominator); + if (rl_filename_completion_desired) + match_list[0] = xstrdup (m_lowest_common_denominator); + else + match_list[0] + = expand_preserving_ws (text, end - start, m_lowest_common_denominator); if (m_lowest_common_denominator_unique) { @@ -3018,6 +3170,11 @@ _initialize_completer () rl_attempted_completion_function = gdb_rl_attempted_completion_function; set_rl_completer_word_break_characters (default_word_break_characters ()); + /* Setup readline globals relating to filename completion. */ + rl_filename_quote_characters = " \t\n\\\"'"; + rl_filename_dequoting_function = gdb_completer_file_name_dequote; + rl_filename_quoting_function = gdb_completer_file_name_quote; + add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class, &max_completions, _("\ Set maximum number of completion candidates."), _("\ diff --git a/gdb/testsuite/gdb.base/filename-completion.exp b/gdb/testsuite/gdb.base/filename-completion.exp index b700977cec5..f23e8671f40 100644 --- a/gdb/testsuite/gdb.base/filename-completion.exp +++ b/gdb/testsuite/gdb.base/filename-completion.exp @@ -40,6 +40,9 @@ proc setup_directory_tree {} { remote_exec host "touch \"${root}/aaa/aa bb\"" remote_exec host "touch \"${root}/aaa/aa cc\"" + remote_exec host "touch \"${root}/bb1/aa\\\"bb\"" + remote_exec host "touch \"${root}/bb1/aa'bb\"" + return $root } @@ -89,6 +92,37 @@ proc run_tests { root } { "aa cc" } "" "${qc}" false \ "expand filenames containing spaces" + + test_gdb_complete_multiple "file ${qc}${root}/bb1/" \ + "a" "a" { + "aa\"bb" + "aa'bb" + } "" "${qc}" false \ + "expand filenames containing quotes" + } else { + set sp "\\ " + + test_gdb_complete_tab_multiple "file ${qc}${root}/aaa/a" \ + "a${sp}" { + "aa bb" + "aa cc" + } false \ + "expand filenames containing spaces" + + test_gdb_complete_tab_multiple "file ${qc}${root}/bb1/a" \ + "a" { + "aa\"bb" + "aa'bb" + } false \ + "expand filenames containing quotes" + + test_gdb_complete_tab_unique "file ${qc}${root}/bb1/aa\\\"" \ + "file ${qc}${root}/bb1/aa\\\\\"bb${qc}" " " \ + "expand unique filename containing double quotes" + + test_gdb_complete_tab_unique "file ${qc}${root}/bb1/aa\\'" \ + "file ${qc}${root}/bb1/aa\\\\'bb${qc}" " " \ + "expand unique filename containing single quote" } } }