public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>,
	Lancelot SIX <lsix@lancelotsix.com>, Eli Zaretskii <eliz@gnu.org>
Subject: [PATCHv2 7/8] gdb: apply escaping to filenames in 'complete' results
Date: Sat, 20 Apr 2024 10:10:07 +0100	[thread overview]
Message-ID: <b0f0bb9de5eb082449b50b2f06e3916a02f8e50a.1713603416.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1713603415.git.aburgess@redhat.com>

Building on the mechanism added in the previous commit(s), this commit
applies escaping to filenames in the 'complete' command output.
Consider a file: /tmp/xxx/aa"bb -- that is a filename that contains a
double quote, currently the 'complete' command output looks like this:

  (gdb) complete file /tmp/xxx/a
  file /tmp/xxx/aa"bb

Notice that the double quote in the output is not escaped.  If we
passed this same output back to GDB then the double quote will be
treated as the start of a string.

After this commit then the output looks like this:

  (gdb) complete file /tmp/xxx/a
  file /tmp/xxx/aa\"bb

The double quote is now escaped.  If we feed this output back to GDB
then GDB will treat this as a single filename that contains a double
quote, exactly what we want.

To achieve this I've done a little refactoring, splitting out the core
of gdb_completer_file_name_quote, and then added a new call from the
filename_match_formatter function.

There are updates to the tests to cover this new functionality.
---
 gdb/completer.c                               |  98 ++++++++++++++---
 .../gdb.base/filename-completion.exp          | 100 +++++++++++-------
 2 files changed, 144 insertions(+), 54 deletions(-)

diff --git a/gdb/completer.c b/gdb/completer.c
index ee016cdc7f7..9c14e6a10ce 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -309,25 +309,24 @@ gdb_completer_file_name_dequote (char *filename, int quote_char)
   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.  */
+/* Apply character escaping to the filename in TEXT and return a newly
+   allocated buffer containing the possibly updated filename.
+
+   QUOTE_CHAR is the quote character surrounding TEXT, or the
+   null-character if there are no quotes around TEXT.  */
 
 static char *
-gdb_completer_file_name_quote (char *text, int match_type ATTRIBUTE_UNUSED,
-			       char *quote_ptr)
+gdb_completer_file_name_quote_1 (const char *text, char quote_char)
 {
   std::string str;
 
-  if (*quote_ptr == '\'')
+  if (quote_char == '\'')
     {
       /* 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 == '"')
+  else if (quote_char == '"')
     {
       /* Add escaping for a double quoted filename.  */
       for (const char *input = text;
@@ -341,7 +340,7 @@ gdb_completer_file_name_quote (char *text, int match_type ATTRIBUTE_UNUSED,
     }
   else
     {
-      gdb_assert (*quote_ptr == '\0');
+      gdb_assert (quote_char == '\0');
 
       /* Add escaping for an unquoted filename.  */
       for (const char *input = text;
@@ -358,6 +357,19 @@ gdb_completer_file_name_quote (char *text, int match_type ATTRIBUTE_UNUSED,
   return strdup (str.c_str ());
 }
 
+/* Apply character escaping to the filename 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 ATTRIBUTE_UNUSED,
+			       char *quote_ptr)
+{
+  return gdb_completer_file_name_quote_1 (text, *quote_ptr);
+}
+
 /* The function is used to update the completion word MATCH before
    displaying it to the user in the 'complete' command output.  This
    function is only used for formatting filename or directory names.
@@ -366,12 +378,28 @@ gdb_completer_file_name_quote (char *text, int match_type ATTRIBUTE_UNUSED,
    in which case a trailing "/" (forward-slash) is added, otherwise
    QUOTE_CHAR is added as a trailing quote.
 
+   When ADD_ESCAPES is true any special characters (e.g. whitespace,
+   quotes) will be escaped with a backslash.  See
+   gdb_completer_file_name_quote_1 for full details on escaping.  When
+   ADD_ESCAPES is false then no escaping will be added and MATCH (with the
+   correct trailing character) will be used unmodified.
+
    Return the updated completion word as a string.  */
 
 static std::string
-filename_match_formatter (const char *match, char quote_char)
+filename_match_formatter_1 (const char *match, char quote_char,
+			    bool add_escapes)
 {
-  std::string result (match);
+  std::string result;
+  if (add_escapes)
+    {
+      gdb::unique_xmalloc_ptr<char> quoted_match
+	(gdb_completer_file_name_quote_1 (match, quote_char));
+      result = quoted_match.get ();
+    }
+  else
+    result = match;
+
   if (std::filesystem::is_directory (gdb_tilde_expand (match)))
     result += "/";
   else
@@ -380,16 +408,52 @@ filename_match_formatter (const char *match, char quote_char)
   return result;
 }
 
+/* The formatting function used to format the results of a 'complete'
+   command when the result is a filename, but the filename should not have
+   any escape characters added.  Most commands that accept a filename don't
+   expect the filename to be quoted or to contain escape characters.
+
+   See filename_match_formatter_1 for more argument details.  */
+
+static std::string
+filename_unquoted_match_formatter (const char *match, char quote_char)
+{
+  return filename_match_formatter_1 (match, quote_char, false);
+}
+
+/* The formatting function used to format the results of a 'complete'
+   command when the result is a filename, and the filename should have any
+   special character (e.g. whitespace, quotes) within it escaped with a
+   backslash.  A limited number of commands accept this style of filename
+   argument.
+
+   See filename_match_formatter_1 for more argument details.  */
+
+static std::string
+filename_maybe_quoted_match_formatter (const char *match, char quote_char)
+{
+  return filename_match_formatter_1 (match, quote_char, true);
+}
+
 /* Generate filename completions of WORD, storing the completions into
    TRACKER.  This is used for generating completions for commands that
    only accept unquoted filenames as well as for commands that accept
-   quoted and escaped filenames.  */
+   quoted and escaped filenames.
+
+   When QUOTE_MATCHES is true TRACKER will be given a match formatter
+   function which will add escape characters (if needed) in the results.
+   When QUOTE_MATCHES is false the match formatter provided will not add
+   any escaping to the results.  */
 
 static void
 filename_completer_generate_completions (completion_tracker &tracker,
-					 const char *word)
+					 const char *word,
+					 bool quote_matches)
 {
-  tracker.set_match_format_func (filename_match_formatter);
+  if (quote_matches)
+    tracker.set_match_format_func (filename_maybe_quoted_match_formatter);
+  else
+    tracker.set_match_format_func (filename_unquoted_match_formatter);
 
   int subsequent_name = 0;
   while (1)
@@ -423,7 +487,7 @@ filename_maybe_quoted_completer (struct cmd_list_element *ignore,
 {
   rl_char_is_quoted_p = gdb_completer_file_name_char_is_quoted;
   rl_completer_quote_characters = gdb_completer_file_name_quote_characters;
-  filename_completer_generate_completions (tracker, word);
+  filename_completer_generate_completions (tracker, word, true);
 }
 
 /* The corresponding completer_handle_brkchars implementation.  */
@@ -449,7 +513,7 @@ filename_completer_handle_brkchars
 {
   gdb_assert (word == nullptr);
   tracker.set_use_custom_word_point (true);
-  filename_completer_generate_completions (tracker, text);
+  filename_completer_generate_completions (tracker, text, false);
 }
 
 /* Find the bounds of the current word for completion purposes, and
diff --git a/gdb/testsuite/gdb.base/filename-completion.exp b/gdb/testsuite/gdb.base/filename-completion.exp
index 0467d5c425e..3ded82431c8 100644
--- a/gdb/testsuite/gdb.base/filename-completion.exp
+++ b/gdb/testsuite/gdb.base/filename-completion.exp
@@ -82,10 +82,22 @@ proc test_gdb_complete_filename_multiple {
 	    $add_completed_line $completion_list $max_completions $testname
     }
 
-    if { $start_quote_char eq "" && $end_quote_char ne "" } {
+    if { $start_quote_char eq "" } {
 	set updated_completion_list {}
 
 	foreach entry $completion_list {
+	    # If ENTRY is quoted with double quotes, then any double
+	    # quotes within the entry need to be escaped.
+	    if { $end_quote_char eq "\"" } {
+		regsub -all "\"" $entry "\\\"" entry
+	    }
+
+	    if { $end_quote_char eq "" } {
+		regsub -all " " $entry "\\ " entry
+		regsub -all "\"" $entry "\\\"" entry
+		regsub -all "'" $entry "\\'" entry
+	    }
+
 	    if {[string range $entry end end] ne "/"} {
 		set entry $entry$end_quote_char
 	    }
@@ -147,47 +159,61 @@ proc run_quoting_and_escaping_tests { root } {
 		} "" "${qc}" false \
 		"expand mixed directory and file names"
 
-	    # GDB does not currently escape word break characters
-	    # (e.g. white space) correctly in unquoted filenames.
 	    if { $qc ne "" } {
 		set sp " "
-
-		test_gdb_complete_filename_multiple "file ${qc}${root}/aaa/" \
-		    "a" "a${sp}" {
-			"aa bb"
-			"aa cc"
-		    } "" "${qc}" false \
-		    "expand filenames containing spaces"
-
-		test_gdb_complete_filename_multiple "file ${qc}${root}/bb1/" \
-		    "a" "a" {
-			"aa\"bb"
-			"aa'bb"
-		    } "" "${qc}" false \
-		    "expand filenames containing quotes"
 	    } else {
 		set sp "\\ "
+	    }
+
+	    if { $qc eq "'" } {
+		set dq "\""
+		set dq_re "\""
+	    } else {
+		set dq "\\\""
+		set dq_re "\\\\\""
+	    }
+
+	    test_gdb_complete_filename_multiple "file ${qc}${root}/bb2/" \
+		"d" "ir${sp}" {
+		    "dir 1/"
+		    "dir 2/"
+		} "" "${qc}" false \
+		"expand multiple directory names containing spaces"
 
-		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}" " " \
+	    test_gdb_complete_filename_multiple "file ${qc}${root}/aaa/" \
+		"a" "a${sp}" {
+		    "aa bb"
+		    "aa cc"
+		} "" "${qc}" false \
+		"expand filenames containing spaces"
+
+	    test_gdb_complete_filename_multiple "file ${qc}${root}/bb1/" \
+		"a" "a" {
+		    "aa\"bb"
+		    "aa'bb"
+		} "" "${qc}" false \
+		"expand filenames containing quotes"
+
+	    test_gdb_complete_tab_unique "file ${qc}${root}/bb1/aa${dq}" \
+		"file ${qc}${root}/bb1/aa${dq_re}bb${qc}" " " \
+		"expand unique filename containing double quotes"
+
+	    # It is not possible to include a single quote character
+	    # within a single quoted string.  However, GDB does not do
+	    # anything smart if a user tries to do this.  Avoid testing
+	    # this case.  Maybe in the future we'll figure a way to avoid
+	    # this situation.
+	    if { $qc ne "'" } {
+		if { $qc eq "" } {
+		    set sq "\\'"
+		    set sq_re "\\\\'"
+		} else {
+		    set sq "'"
+		    set sq_re "'"
+		}
+
+		test_gdb_complete_tab_unique "file ${qc}${root}/bb1/aa${sq}" \
+		    "file ${qc}${root}/bb1/aa${sq_re}bb${qc}" " " \
 		    "expand unique filename containing single quote"
 	    }
 	}
-- 
2.25.4


  parent reply	other threads:[~2024-04-20  9:10 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-29 11:42 [PATCH 0/6] Further filename completion improvements Andrew Burgess
2024-03-29 11:42 ` [PATCH 1/6] gdb: improve escaping when completing filenames Andrew Burgess
2024-03-30 23:48   ` Lancelot SIX
2024-03-29 11:42 ` [PATCH 2/6] gdb: move display of completion results into completion_result class Andrew Burgess
2024-03-29 12:14   ` Eli Zaretskii
2024-03-30 23:30     ` Lancelot SIX
2024-03-31  5:49       ` Eli Zaretskii
2024-04-12 17:24         ` Andrew Burgess
2024-04-12 18:42           ` Eli Zaretskii
2024-04-12 22:20             ` Andrew Burgess
2024-04-13  6:36               ` Eli Zaretskii
2024-04-13  9:09                 ` Andrew Burgess
2024-04-13  9:46                   ` Eli Zaretskii
2024-04-12 17:31       ` Andrew Burgess
2024-03-29 11:42 ` [PATCH 3/6] gdb: simplify completion_result::print_matches Andrew Burgess
2024-03-30 23:48   ` Lancelot SIX
2024-03-29 11:42 ` [PATCH 4/6] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-03-30 23:49   ` Lancelot SIX
2024-03-31  5:55     ` Eli Zaretskii
2024-04-12 17:42       ` Andrew Burgess
2024-04-12 18:44         ` Eli Zaretskii
2024-04-12 22:29           ` Andrew Burgess
2024-04-13  6:39             ` Eli Zaretskii
2024-03-29 11:42 ` [PATCH 5/6] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-03-29 11:42 ` [PATCH 6/6] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess
2024-04-20  9:10 ` [PATCHv2 0/8] Further filename completion improvements Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 1/8] gdb/doc: document how filename arguments are formatted Andrew Burgess
2024-04-20  9:44     ` Eli Zaretskii
2024-04-27 10:01       ` Andrew Burgess
2024-04-27 10:06         ` Eli Zaretskii
2024-04-29  9:10           ` Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 2/8] gdb: split apart two different types of filename completion Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 3/8] gdb: improve escaping when completing filenames Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 4/8] gdb: move display of completion results into completion_result class Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 5/8] gdb: simplify completion_result::print_matches Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 6/8] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-04-20  9:10   ` Andrew Burgess [this message]
2024-04-20  9:10   ` [PATCHv2 8/8] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b0f0bb9de5eb082449b50b2f06e3916a02f8e50a.1713603416.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=lsix@lancelotsix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).