From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 2/6] gdb: move display of completion results into completion_result class
Date: Fri, 29 Mar 2024 11:42:28 +0000 [thread overview]
Message-ID: <cc27bda4f7cbf9f8fdc564758e0c1c5b35ff1594.1711712401.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1711712401.git.aburgess@redhat.com>
When using the 'complete' command to complete file names we have some
problems. The suggested completion should include any required
escaping, so if there is a filename '/tmp/aa"bb' (without the single
quotes), then this should be displayed in the completion output like:
(gdb) complete file /tmp/aa
file /tmp/aa\"bb
This doesn't currently happen. We already have some code in
filename_completer (completer.c) that handles adding the trailing '/'
character if the completion result is a directory, so we might be
tempted to add any needed escaping in that function, however if we do
then we run into a problem.
If there are multiple completion results from a 'complete' command,
then in complete_command (cli/cli-cmds.c) we sort the completion
results prior to printing them.
If those results have already had the escaping added then the sort
will be done on the text including the escape characters. This
means that results from the 'complete' command will appear in a
different order than readline would present them; readline sorts the
results and then adds and required escaping.
I think that the 'complete' command should behave the same as
readline, sort the entries and then add the escaping. This means that
we need to sort before adding the escaping.
There is a second problem when using the 'complete' command with file
names, that is trailing quote characters. The addition of a trailing
quote character is a bit complex due (I think) to the structure of
readline itself.
Adding the quote character currently occurs in two places in GDB. In
completion_tracker::build_completion_result (completer.c) we add the
trailing quote in the case where we only have a single result, and in
complete_command (cli/cli-cmds.c) we add the trailing quote character
if we have more than one result.
With these two cases we ensure that the 'complete' command always adds
a trailing quote (when necessary) to the results it displays.
However, I think that for filename completion we don't always want a
trailing quote. Consider if we have a file '/tmp/xxx/foo.c' (without
the quotes), and then we do:
(gdb) complete file /tmp/xx
What should the result of this be? Well, if we use TAB completion
like this:
(gdb) file /tmp/xx<TAB>
(gdb) file /tmp/xxx/
then readline completes the directory 'xxx/', but doesn't try to
complete the filename within the xxx/ directory until we press <TAB>
again. So I think that the results of the 'complete' command should
share this behaviour:
(gdb) complete file /tmp/xx
file /tmp/xxx/
And this is what we get right now, but what if the user adds some
opening quotes? Right now we get this:
(gdb) complete file "/tmp/xx
file "/tmp/xxx/
Which I think is correct, there's no trailing quote added. This is
because in completion_tracker::build_completion_result the
completion_tracker object doesn't know that the double quote is the
quote_char(). However, this causes a problem, if we do this:
(gdb) complete file "/tmp/xxx/f
file "/tmp/xxx/foo.c
We're still missing the trailing quote, even though in this case, when
we've expanded a complete filename, adding the trailing quote would be
the correct thing to do. The cause is, of course, the same, the
completion_tracker is unaware of what the quote character is, and so
doesn't add the quote when needed.
However, the problem gets worse, if we create an additional file
'/tmp/xxa/bar.c', notice that this is in a different directory. So
now when we do this:
(gdb) complete file "/tmp/xx
file "/tmp/xxa/"
file "/tmp/xxx/"
Now the trailing quote has been added even though we haven't completed
a full filename. This is because in complete_command (cli/cli-cmds.c)
we do know what the quote character is, and the trailing quote is
always added.
There are multiple problems here, but at least part of the problem
will be solved by making the result printing in complete_command
smarter. My plan is to allow different completion functions to modify
how the 'complete' results are printed.
This commit doesn't solve any of the problems listed above. Instead
this commit is just a refactor. I've moved the result printing logic
out of complete_command and created a new function
completion_result::print_matches. And that's it. Nothing has
functionally changed yet, that will come in later commits when the
print_matches function is made smarter.
There should be no user visible changes after this commit.
---
gdb/cli/cli-cmds.c | 26 +-------------------------
gdb/completer.c | 33 +++++++++++++++++++++++++++++++++
gdb/completer.h | 13 +++++++++++++
3 files changed, 47 insertions(+), 25 deletions(-)
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index df11f956245..0b621f65917 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -424,31 +424,7 @@ complete_command (const char *arg, int from_tty)
{
std::string arg_prefix (arg, word - arg);
- if (result.number_matches == 1)
- printf_unfiltered ("%s%s\n", arg_prefix.c_str (), result.match_list[0]);
- else
- {
- result.sort_match_list ();
-
- for (size_t i = 0; i < result.number_matches; i++)
- {
- printf_unfiltered ("%s%s",
- arg_prefix.c_str (),
- result.match_list[i + 1]);
- if (quote_char)
- printf_unfiltered ("%c", quote_char);
- printf_unfiltered ("\n");
- }
- }
-
- if (result.number_matches == max_completions)
- {
- /* ARG_PREFIX and WORD are included in the output so that emacs
- will include the message in the output. */
- printf_unfiltered (_("%s%s %s\n"),
- arg_prefix.c_str (), word,
- get_max_completions_reached_message ());
- }
+ result.print_matches (arg_prefix, word, quote_char);
}
}
diff --git a/gdb/completer.c b/gdb/completer.c
index 4cda5f3a383..9b4041da01a 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -2443,6 +2443,39 @@ completion_result::reset_match_list ()
}
}
+/* See completer.h */
+
+void
+completion_result::print_matches (const std::string &prefix,
+ const char *word, int quote_char)
+{
+ if (this->number_matches == 1)
+ printf_unfiltered ("%s%s\n", prefix.c_str (), this->match_list[0]);
+ else
+ {
+ this->sort_match_list ();
+
+ for (size_t i = 0; i < this->number_matches; i++)
+ {
+ printf_unfiltered ("%s%s", prefix.c_str (),
+ this->match_list[i + 1]);
+ if (quote_char)
+ printf_unfiltered ("%c", quote_char);
+ printf_unfiltered ("\n");
+ }
+ }
+
+ if (this->number_matches == max_completions)
+ {
+ /* PREFIX and WORD are included in the output so that emacs will
+ include the message in the output. */
+ printf_unfiltered (_("%s%s %s\n"),
+ prefix.c_str (), word,
+ get_max_completions_reached_message ());
+ }
+
+}
+
/* Helper for gdb_rl_attempted_completion_function, which does most of
the work. This is called by readline to build the match list array
and to determine the lowest common denominator. The real matches
diff --git a/gdb/completer.h b/gdb/completer.h
index 98a12f3907c..4419c8f6d30 100644
--- a/gdb/completer.h
+++ b/gdb/completer.h
@@ -268,6 +268,19 @@ struct completion_result
/* Sort the match list. */
void sort_match_list ();
+ /* Called to display all matches (used by the 'complete' command).
+ PREFIX is everything before the completion word. WORD is the word
+ being completed, this is only used if we reach the maximum number of
+ completions, otherwise, each line of output consists of PREFIX
+ followed by one of the possible completion words.
+
+ The QUOTE_CHAR is appended after each possible completion word and
+ should be the quote character that appears before the completion word,
+ or the null-character if there is no quote before the completion
+ word. */
+ void print_matches (const std::string &prefix, const char *word,
+ int quote_char);
+
private:
/* Destroy the match list array and its contents. */
void reset_match_list ();
--
2.25.4
next prev parent reply other threads:[~2024-03-29 11:43 UTC|newest]
Thread overview: 104+ 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 ` Andrew Burgess [this message]
2024-03-29 12:14 ` [PATCH 2/6] gdb: move display of completion results into completion_result class 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 ` [PATCHv2 7/8] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-04-20 9:10 ` [PATCHv2 8/8] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess
2024-06-05 13:36 ` [PATCHv3 0/7] Further filename completion improvements Andrew Burgess
2024-06-05 13:36 ` [PATCHv3 1/7] gdb: split apart two different types of filename completion Andrew Burgess
2024-06-06 16:18 ` Tom Tromey
2024-06-17 13:05 ` Andrew Burgess
2024-06-05 13:36 ` [PATCHv3 2/7] gdb: improve escaping when completing filenames Andrew Burgess
2024-06-05 13:36 ` [PATCHv3 3/7] gdb: move display of completion results into completion_result class Andrew Burgess
2024-06-06 16:19 ` Tom Tromey
2024-06-05 13:36 ` [PATCHv3 4/7] gdb: simplify completion_result::print_matches Andrew Burgess
2024-06-05 13:36 ` [PATCHv3 5/7] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-06-06 16:14 ` Tom Tromey
2024-06-17 13:29 ` Andrew Burgess
2024-06-05 13:36 ` [PATCHv3 6/7] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-06-05 13:36 ` [PATCHv3 7/7] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess
2024-06-06 16:24 ` [PATCHv3 0/7] Further filename completion improvements Tom Tromey
2024-07-04 14:20 ` [PATCHv4 00/14] " Andrew Burgess
2024-07-04 14:20 ` [PATCHv4 01/14] gdb: split apart two different types of filename completion Andrew Burgess
2024-07-04 14:20 ` [PATCHv4 02/14] gdb: deprecated filename_completer and associated functions Andrew Burgess
2024-07-04 14:20 ` [PATCHv4 03/14] gdb: improve escaping when completing filenames Andrew Burgess
2024-07-04 14:20 ` [PATCHv4 04/14] gdb: move display of completion results into completion_result class Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 05/14] gdb: simplify completion_result::print_matches Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 06/14] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 07/14] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 08/14] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 09/14] gdb: implement readline rl_directory_rewrite_hook callback Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 10/14] gdb: new extract_single_filename_arg helper function Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 11/14] gdb: extend completion of quoted filenames to work in brkchars phase Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 12/14] gdb: add remove-symbol-file command completion Andrew Burgess
2024-07-04 15:38 ` Eli Zaretskii
2024-07-04 14:21 ` [PATCHv4 13/14] gdb: allow quoted filenames for commands that have custom completion Andrew Burgess
2024-07-04 15:42 ` Eli Zaretskii
2024-08-20 17:18 ` Andrew Burgess
2024-07-04 14:21 ` [PATCHv4 14/14] gdb: 'target ...' commands now expect quoted/escaped filenames Andrew Burgess
2024-07-04 15:34 ` Eli Zaretskii
2024-08-20 17:10 ` [PATCHv5 00/14] Further filename completion improvements Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 01/14] gdb: split apart two different types of filename completion Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 02/14] gdb: deprecated filename_completer and associated functions Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 03/14] gdb: improve escaping when completing filenames Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 04/14] gdb: move display of completion results into completion_result class Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 05/14] gdb: simplify completion_result::print_matches Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 06/14] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 07/14] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 08/14] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 09/14] gdb: implement readline rl_directory_rewrite_hook callback Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 10/14] gdb: new extract_single_filename_arg helper function Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 11/14] gdb: extend completion of quoted filenames to work in brkchars phase Andrew Burgess
2024-08-20 17:10 ` [PATCHv5 12/14] gdb: add remove-symbol-file command completion Andrew Burgess
2024-08-20 18:46 ` Eli Zaretskii
2024-08-20 17:10 ` [PATCHv5 13/14] gdb: allow quoted filenames for commands that have custom completion Andrew Burgess
2024-08-20 18:47 ` Eli Zaretskii
2024-08-20 17:10 ` [PATCHv5 14/14] gdb: 'target ...' commands now expect quoted/escaped filenames Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 00/14] Further filename completion improvements Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 01/14] gdb: split apart two different types of filename completion Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 02/14] gdb: deprecated filename_completer and associated functions Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 03/14] gdb: improve escaping when completing filenames Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 04/14] gdb: move display of completion results into completion_result class Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 05/14] gdb: simplify completion_result::print_matches Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 06/14] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 07/14] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 08/14] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 09/14] gdb: implement readline rl_directory_rewrite_hook callback Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 10/14] gdb: new extract_single_filename_arg helper function Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 11/14] gdb: extend completion of quoted filenames to work in brkchars phase Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 12/14] gdb: add remove-symbol-file command completion Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 13/14] gdb: allow quoted filenames for commands that have custom completion Andrew Burgess
2024-08-30 11:33 ` [PATCHv6 14/14] gdb: 'target ...' commands now expect quoted/escaped filenames Andrew Burgess
2024-09-07 19:57 ` [PATCHv6 00/14] Further filename completion improvements 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=cc27bda4f7cbf9f8fdc564758e0c1c5b35ff1594.1711712401.git.aburgess@redhat.com \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
/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