From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 4NaPIgerDmAyfgAAWB0awg (envelope-from ) for ; Mon, 25 Jan 2021 06:27:03 -0500 Received: by simark.ca (Postfix, from userid 112) id 8C15F1EF80; Mon, 25 Jan 2021 06:27:03 -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 3381F1E940 for ; Mon, 25 Jan 2021 06:27:03 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9DFF4394848E; Mon, 25 Jan 2021 11:27:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9DFF4394848E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1611574022; bh=rez3arn5e3m3rc58z7avUVc2SKlnDpenLoC2K/7oPto=; 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=wXkzImkAGZe9FSwpQzfEFTHhUeIbUCun/zYCCFzJUfqPQ2SlqKxSAMNRilo5LIz/O bqXPANDS9fOCYzOvmGofOy9G7lH9ovrmVNnqqYepBmVB/Un70ip6MNE08j97raMK9W G/rDdqGmSqqLH9NuUWjlbTj2E6wkhhxlcOR4qqio= Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id A1313394742F for ; Mon, 25 Jan 2021 11:26:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A1313394742F Received: by mail-wr1-x429.google.com with SMTP id m1so11258606wrq.12 for ; Mon, 25 Jan 2021 03:26:59 -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=rez3arn5e3m3rc58z7avUVc2SKlnDpenLoC2K/7oPto=; b=WbYiYCpLSjBjdLzwSvYuJJZ0+Yzv90cgdn2L8EtupBg+OCrN/U8FeyIFyyou522iLv gC9Fsy4In8JvEFSOqaK2jF1ABBSLDxNWO2/ekJPhthzaSX75JKmj4Vk2p7dzhWP0YFjT nyQPAPJ1Lanii/N6SX3H38jTODjU+VDmJ72gsndpT1T7eXdh3rcVpn1XIt4DtneBFv6o 0k4t61jO6sLOi2qXcY7gpU5IwdteB6mPTC6AWHUaTS5yCS0RIit2UuEGgkBCvi5rmgiB GCEjjlB/pEDs0HTGiwi4hKZWwRorZ2ojrfDbY5pvDjxtdYWAmBJCjykaZgWCjFVpM4aI 61Yw== X-Gm-Message-State: AOAM531ml3PXU0U4OBymcQdmSw90eA46ZL+OmmEyDiMtpqtY9obhj/Xw C77NDFdY0F6mFQHuS7ohpBLjIcmJ4eDouFL5g8c+rLaM/TDira/G6Cx+sdPiQuEuJdFM2vU7lz/ bgyKwSiDE3H89HOO/ftdi++ZKl3/oe10mfwTpWAGllx4sbUb0bm638cFgK+Aep57tjzZkyT5F23 0s X-Google-Smtp-Source: ABdhPJxVbTJIY4MC/sYWG1WoJl242qVlFUxLMA9lnAceFt8IwqmrUeOPQ3KWYGNAEkpPWVGKL7gUPA== X-Received: by 2002:adf:dd92:: with SMTP id x18mr369472wrl.311.1611574018271; Mon, 25 Jan 2021 03:26:58 -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 q2sm20648220wma.6.2021.01.25.03.26.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jan 2021 03:26:57 -0800 (PST) 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 Message-Id: <20210125112649.56362-2-mbarisione@undo.io> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210125112649.56362-1-mbarisione@undo.io> References: <20210108100706.96190-1-mbarisione@undo.io> <20210125112649.56362-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..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