aboutsummaryrefslogtreecommitdiff
path: root/gdb/cli
diff options
context:
space:
mode:
authorLancelot SIX <lsix@lancelotsix.com>2021-10-11 23:42:33 +0100
committerLancelot SIX <lsix@lancelotsix.com>2021-11-14 13:50:30 +0000
commitcc81bc2dfbc0248ec0b00cea776f351bf8d4b235 (patch)
treebfc9980ff376eb6e99a9554cc7ee65421f4848f0 /gdb/cli
parentb431e7a3fe8bcd47e38e5b5a6720272861449ed5 (diff)
downloadgdb-cc81bc2dfbc0248ec0b00cea776f351bf8d4b235.zip
gdb-cc81bc2dfbc0248ec0b00cea776f351bf8d4b235.tar.gz
gdb-cc81bc2dfbc0248ec0b00cea776f351bf8d4b235.tar.bz2
[PR gdb/16238] Add completer for the show user command
The 'show user' command (which shows the definition of non-python/scheme user defined commands) is currently missing a completer. This is mentioned in PR 16238. Having one can improve the user experience. In this commit I propose an implementation for such completer as well as the associated tests. Tested on x86_64 GNU/Linux. All feedbacks are welcome. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16238
Diffstat (limited to 'gdb/cli')
-rw-r--r--gdb/cli/cli-cmds.c72
1 files changed, 71 insertions, 1 deletions
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 2cf614c..138a146 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -1631,6 +1631,75 @@ show_user (const char *args, int from_tty)
}
}
+/* Return true if COMMAND or any of its sub-commands is a user defined command.
+ This is a helper function for show_user_completer. */
+
+static bool
+has_user_subcmd (struct cmd_list_element *command)
+{
+ if (cli_user_command_p (command))
+ return true;
+
+ /* Alias command can yield false positive. Ignore them as the targeted
+ command should be reachable anyway. */
+ if (command->is_alias ())
+ return false;
+
+ if (command->is_prefix ())
+ for (struct cmd_list_element *subcommand = *command->subcommands;
+ subcommand != nullptr;
+ subcommand = subcommand->next)
+ if (has_user_subcmd (subcommand))
+ return true;
+
+ return false;
+}
+
+/* Implement completer for the 'show user' command. */
+
+static void
+show_user_completer (cmd_list_element *,
+ completion_tracker &tracker, const char *text,
+ const char *word)
+{
+ struct cmd_list_element *cmd_group = cmdlist;
+
+ /* TEXT can contain a chain of commands and subcommands. Follow the
+ commands chain until we reach the point where the user wants a
+ completion. */
+ while (word > text)
+ {
+ const char *curr_cmd = text;
+ const char *after = skip_to_space (text);
+ const size_t curr_cmd_len = after - text;
+ text = skip_spaces (after);
+
+ for (struct cmd_list_element *c = cmd_group; c != nullptr; c = c->next)
+ {
+ if (strlen (c->name) == curr_cmd_len
+ && strncmp (c->name, curr_cmd, curr_cmd_len) == 0)
+ {
+ if (c->subcommands == nullptr)
+ /* We arrived after a command with no child, so nothing more
+ to complete. */
+ return;
+
+ cmd_group = *c->subcommands;
+ break;
+ }
+ }
+ }
+
+ const int wordlen = strlen (word);
+ for (struct cmd_list_element *c = cmd_group; c != nullptr; c = c->next)
+ if (has_user_subcmd (c))
+ {
+ if (strncmp (c->name, word, wordlen) == 0)
+ tracker.add_completion
+ (gdb::unique_xmalloc_ptr<char> (xstrdup (c->name)));
+ }
+}
+
/* Search through names of commands and documentations for a certain
regular expression. */
@@ -2593,10 +2662,11 @@ you must type \"disassemble 'foo.c'::bar\" and not \"disassemble foo.c:bar\"."))
c = add_com ("make", class_support, make_command, _("\
Run the ``make'' program using the rest of the line as arguments."));
set_cmd_completer (c, filename_completer);
- add_cmd ("user", no_class, show_user, _("\
+ c = add_cmd ("user", no_class, show_user, _("\
Show definitions of non-python/scheme user defined commands.\n\
Argument is the name of the user defined command.\n\
With no argument, show definitions of all user defined commands."), &showlist);
+ set_cmd_completer (c, show_user_completer);
add_com ("apropos", class_support, apropos_command, _("\
Search for commands matching a REGEXP.\n\
Usage: apropos [-v] REGEXP\n\