From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id eDenK+gu+F+1JwAAWB0awg (envelope-from ) for ; Fri, 08 Jan 2021 05:07:36 -0500 Received: by simark.ca (Postfix, from userid 112) id AFCC51E99A; Fri, 8 Jan 2021 05:07:36 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 19DCF1E4F4; Fri, 8 Jan 2021 05:07:36 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8BAB3973009; Fri, 8 Jan 2021 10:07:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A8BAB3973009 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1610100455; bh=EG9RdGFaiing7Dm3CIVtIbGY3aEzEH9DhJUp0exxMbQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Q+pFL+rL3R/yCVahgw40jGQGi3CvV07c3FNbUHPccyEdgRSXNkNvohAVvQtz3j1Of +6EzMKWlU/321rz5EUF8vCaKUb4FAuzaI+kQ0gnJnYfnUHCa2zaPrESzN7Tp3Bq8kv C8dD6IXBghhmCuX9QAv4BPCt32CpqSqEM6vjekAU= Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id B3B2D3858025 for ; Fri, 8 Jan 2021 10:07:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B3B2D3858025 Received: by mail-wr1-x42d.google.com with SMTP id y17so8389660wrr.10 for ; Fri, 08 Jan 2021 02:07:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EG9RdGFaiing7Dm3CIVtIbGY3aEzEH9DhJUp0exxMbQ=; b=ZqsRcVa25jSsXT/Qbjlhph+cABmq5X82etbbiQFKPhTMNbR8eVP0sDj5PFEIhbpLBk l4loEj8DHj59LRr093z82Fgo+owc3pwrG9KLG7GID6stuA1y0Vm57wNDGYjldCxwrmVy Qc3nfjPslxvSJhr7bQvGsrEFhHfnaPT5irwnIld4B9wZPWHK0omu7neCHJbgj403pAjZ 3m0vjUTYEwsJ8rticrMeaerF6VGa0TwVdjEUwncJqqORkYHysyulupcJmv+oTiHrtU3N CIkywmhsJQDUDXx90pl4u7iIbQncXXWOsyEYsMKAAf+TdV1TUj0cb2IWx5XN7rYUF5Mi 2KZw== X-Gm-Message-State: AOAM530kyy4kbEBkl7F+8/Njx+z2VI6JlRoftf/bseum342I55KjSP3K QlFVQ4Jr8rkvU1z/op0iGLi2shaoiveYl58br4lD4sgLVdVmrLPIf3D2PXNtELhGlPyrnS8GmgF 8UoEUsKkWQ6LDBAPT3AbdL0K6/xkz4+WIcMTELu8iblc0g3XtLRZiOjr/rC5TPiu+qFLA0M73cM x/ X-Google-Smtp-Source: ABdhPJxwgG01q50szRD1LJr0bv08LQIV/bC3lQ6wsOEER8r8Ja8x0MODR26IJV76GtQ60zBy8cNxNQ== X-Received: by 2002:a5d:6909:: with SMTP id t9mr2772781wru.327.1610100451307; Fri, 08 Jan 2021 02:07:31 -0800 (PST) Received: from focaccia.undoers.io (cpc159317-cmbg20-2-0-cust151.5-4.cable.virginm.net. [81.111.29.152]) by smtp.gmail.com with ESMTPSA id s205sm11579143wmf.46.2021.01.08.02.07.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jan 2021 02:07:30 -0800 (PST) To: gdb-patches@sourceware.org Subject: [PATCH 1/4] gdb: add lookup_cmd_exact to simplify a common pattern Date: Fri, 8 Jan 2021 10:07:03 +0000 Message-Id: <20210108100706.96190-2-mbarisione@undo.io> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210108100706.96190-1-mbarisione@undo.io> References: <20210108100706.96190-1-mbarisione@undo.io> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Marco Barisione via Gdb-patches Reply-To: Marco Barisione Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" 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..87785107e3d 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 && 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