Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Marco Barisione via Gdb-patches <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Subject: [PATCH v2 1/5] gdb: add lookup_cmd_exact to simplify a common pattern
Date: Mon, 25 Jan 2021 11:26:45 +0000	[thread overview]
Message-ID: <20210125112649.56362-2-mbarisione@undo.io> (raw)
In-Reply-To: <20210125112649.56362-1-mbarisione@undo.io>

In code dealing with commands, there's a pattern repeated a few times of
calling lookup_cmd with some speficic arguments and then using strcmp
on the returned command to check for an exact match.
As a later patch would add a few more similar lines of code, this patch
adds a new lookup_cmd_exact function which simplify this use case.

gdb/ChangeLog:

	* cli/cli-decode.c (lookup_cmd_exact): Add.
	* cli/cli-script.c (do_define_command): Use lookup_cmd_exact.
	(define_prefix_command): Ditto.
	* command.h: Add lookup_cmd_exact.
---
 gdb/cli/cli-decode.c | 15 +++++++++++++++
 gdb/cli/cli-script.c | 23 ++++++-----------------
 gdb/command.h        | 19 +++++++++++++++++++
 3 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 99bd4c6d2cd..f48a9add4d4 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -1875,6 +1875,21 @@ lookup_cmd (const char **line, struct cmd_list_element *list,
   return 0;
 }
 
+/* See command.h.  */
+
+struct cmd_list_element *
+lookup_cmd_exact (const char *name,
+		  struct cmd_list_element *list,
+		  bool ignore_help_classes)
+{
+  const char *tem = name;
+  struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
+					     ignore_help_classes);
+  if (cmd != nullptr && strcmp (name, cmd->name) != 0)
+    cmd = nullptr;
+  return cmd;
+}
+
 /* We are here presumably because an alias or command in TEXT is
    deprecated and a warning message should be generated.  This
    function decodes TEXT and potentially generates a warning message
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 9d0dd7796e0..0544f3efb1b 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -1391,7 +1391,7 @@ do_define_command (const char *comname, int from_tty,
       CMD_POST_HOOK
     };
   struct cmd_list_element *c, *newc, *hookc = 0, **list;
-  const char *tem, *comfull;
+  const char *comfull;
   int  hook_type      = CMD_NO_HOOK;
   int  hook_name_size = 0;
    
@@ -1403,11 +1403,7 @@ do_define_command (const char *comname, int from_tty,
   comfull = comname;
   list = validate_comname (&comname);
 
-  /* Look it up, and verify that we got an exact match.  */
-  tem = comname;
-  c = lookup_cmd (&tem, *list, "", NULL, -1, 1);
-  if (c && strcmp (comname, c->name) != 0)
-    c = 0;
+  c = lookup_cmd_exact (comname, *list);
 
   if (c && commands == nullptr)
     {
@@ -1448,11 +1444,9 @@ do_define_command (const char *comname, int from_tty,
 
   if (hook_type != CMD_NO_HOOK)
     {
-      /* Look up cmd it hooks, and verify that we got an exact match.  */
-      tem = comname + hook_name_size;
-      hookc = lookup_cmd (&tem, *list, "", NULL, -1, 0);
-      if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
-	hookc = 0;
+      /* Look up cmd it hooks.  */
+      hookc = lookup_cmd_exact (comname + hook_name_size, *list,
+				/* ignore_help_classes = */ false);
       if (!hookc && commands == nullptr)
 	{
 	  warning (_("Your new `%s' command does not "
@@ -1593,17 +1587,12 @@ static void
 define_prefix_command (const char *comname, int from_tty)
 {
   struct cmd_list_element *c, **list;
-  const char *tem;
   const char *comfull;
 
   comfull = comname;
   list = validate_comname (&comname);
 
-  /* Look it up, and verify that we got an exact match.  */
-  tem = comname;
-  c = lookup_cmd (&tem, *list, "", NULL, -1, 1);
-  if (c != nullptr && strcmp (comname, c->name) != 0)
-    c = nullptr;
+  c = lookup_cmd_exact (comname, *list);
 
   if (c != nullptr && c->theclass != class_user)
     error (_("Command \"%s\" is built-in."), comfull);
diff --git a/gdb/command.h b/gdb/command.h
index 79e5017ff7a..827a19637a2 100644
--- a/gdb/command.h
+++ b/gdb/command.h
@@ -326,6 +326,25 @@ extern struct cmd_list_element *lookup_cmd_1
 	 struct cmd_list_element **result_list, std::string *default_args,
 	 int ignore_help_classes, bool lookup_for_completion_p = false);
 
+/* Look up the command called NAME in the command list LIST.
+
+   Unlike LOOKUP_CMD, partial matches are ignored and only exact matches
+   on NAME are considered.
+
+   LIST is a chain of struct cmd_list_element's.
+
+   If IGNORE_HELP_CLASSES is true (the default), ignore any command list
+   elements which are actually help classes rather than commands (i.e.
+   the function field of the struct cmd_list_element is null).
+
+   If found, return the struct cmd_list_element for that command,
+   otherwise return NULLPTR.  */
+
+extern struct cmd_list_element *lookup_cmd_exact
+			(const char *name,
+			 struct cmd_list_element *list,
+			 bool ignore_help_classes = true);
+
 extern struct cmd_list_element *deprecate_cmd (struct cmd_list_element *,
 					       const char * );
 
-- 
2.28.0


  reply	other threads:[~2021-01-25 11:27 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-08 10:07 [PATCH 0/4] Add support for command renaming Marco Barisione via Gdb-patches
2021-01-08 10:07 ` [PATCH 1/4] gdb: add lookup_cmd_exact to simplify a common pattern Marco Barisione via Gdb-patches
2021-01-10  0:06   ` Lancelot SIX via Gdb-patches
2021-01-17 10:47     ` Marco Barisione via Gdb-patches
2021-01-17 19:02       ` Lancelot SIX via Gdb-patches
2021-01-25 11:33         ` Luis Machado via Gdb-patches
2021-01-08 10:07 ` [PATCH 2/4] gdb: prevent prefix commands from being hooks Marco Barisione via Gdb-patches
2021-01-08 10:07 ` [PATCH 3/4] gdb: update the docs for add_cmd and do_add_cmd to match reality Marco Barisione via Gdb-patches
2021-01-08 10:07 ` [PATCH 4/4] gdb: Add support for renaming commands Marco Barisione via Gdb-patches
2021-01-08 10:30   ` Eli Zaretskii via Gdb-patches
2021-01-25 11:26 ` [PATCH v2 0/5] Add support for command renaming Marco Barisione via Gdb-patches
2021-01-25 11:26   ` Marco Barisione via Gdb-patches [this message]
2021-03-08 18:58     ` [PATCH v2 1/5] gdb: add lookup_cmd_exact to simplify a common pattern Simon Marchi
2021-05-07 14:47       ` Marco Barisione via Gdb-patches
2021-01-25 11:26   ` [PATCH v2 2/5] gdb: prevent prefix commands from being hooks Marco Barisione via Gdb-patches
2021-03-08 21:32     ` Simon Marchi via Gdb-patches
2021-03-09  9:42       ` Marco Barisione via Gdb-patches
2021-03-16  3:17         ` Simon Marchi via Gdb-patches
2021-05-07 14:59           ` Marco Barisione via Gdb-patches
2021-05-07 19:30             ` Simon Marchi via Gdb-patches
2021-05-07 20:11               ` Marco Barisione via Gdb-patches
2021-05-14 20:38       ` [PATCH v3 " Marco Barisione via Gdb-patches
2021-01-25 11:26   ` [PATCH v2 3/5] gdb: update the docs for add_cmd and do_add_cmd to match reality Marco Barisione via Gdb-patches
2021-03-08 22:52     ` Simon Marchi via Gdb-patches
2021-03-08 23:10       ` Simon Marchi via Gdb-patches
2021-05-14 20:39       ` [PATCH v3 3/5] gdb: move declarations and docs for cli-decode.c to cli-decode.h Marco Barisione via Gdb-patches
2021-01-25 11:26   ` [PATCH v2 4/5] gdb: generate the prefix name for prefix commands on demand Marco Barisione via Gdb-patches
2021-03-08 23:25     ` Simon Marchi via Gdb-patches
2021-03-16 17:00       ` Simon Marchi via Gdb-patches
2021-05-12 11:10       ` Marco Barisione via Gdb-patches
2021-01-25 11:26   ` [PATCH v2 5/5] gdb: Add support for renaming commands Marco Barisione via Gdb-patches
2021-03-23 18:45     ` Simon Marchi via Gdb-patches
2021-05-14 20:41       ` [PATCH v3 5/5] gdb: add " Marco Barisione via Gdb-patches
2021-02-08 17:53   ` [PING] [PATCH v2 0/5] Add support for command renaming Marco Barisione via Gdb-patches
2021-02-15  8:27     ` [PING2] " Marco Barisione via Gdb-patches
2021-02-22  8:28       ` [PING 3] " Marco Barisione via Gdb-patches
2021-03-01  8:32         ` [PING 4] " Marco Barisione via Gdb-patches
2021-03-08  9:23           ` [PING 5] " Marco Barisione via Gdb-patches

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=20210125112649.56362-2-mbarisione@undo.io \
    --to=gdb-patches@sourceware.org \
    --cc=mbarisione@undo.io \
    /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