From patchwork Tue Sep 28 13:14:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522547 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2635AC433F5 for ; Tue, 28 Sep 2021 13:14:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0AFCC611BD for ; Tue, 28 Sep 2021 13:14:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240642AbhI1NQT (ORCPT ); Tue, 28 Sep 2021 09:16:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239811AbhI1NQR (ORCPT ); Tue, 28 Sep 2021 09:16:17 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DE19C061575 for ; Tue, 28 Sep 2021 06:14:38 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id x20so13320185wrg.10 for ; Tue, 28 Sep 2021 06:14:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=98NBot6U2akdiJwAVAWdVbKOgyUwozDN0iJD/t1K61I=; b=g2HYio2KbgNaq63ICc4I9y5i3rw6yN5VBjnSoZGrz7s1JU5UXmSVsPF0sLLfp36iHU Yg7M9vK87kpteAhiSXv9QzP8Ekqocs/cZFhdtFT4hlpeAcQVmbW4Xlrbiyb//2DGbrbp CEWciw7P24ft2iUXyLGL0IOTQqDZy7xMIfzk+po96cJkfIsZ+3ksS9U8KBSynG+pOZJk S2rjqhckZqAbSq7lDKYXLoRIXNtS7BVjPKAYvCkFTnz+BS2lZ313+qZw2cu4uPYl5wgI m7FOyr1HSEhIbbKz6ALG3cq9FglpURiBWG6DoHQTslo4YcHzMDkV90xpQXiIHnaQ27gZ LLRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=98NBot6U2akdiJwAVAWdVbKOgyUwozDN0iJD/t1K61I=; b=2/sbSzirJ6fF0gBbuX7BGwy7biPLCoNPvxrwjY8zry/Pt385W29/F5WISKadDRjYVq 5PhC2aVrxzO0kNKlSO5TVWO7afZN8o5C4DVdCqud9E9C971+gUbrW7Ifgihekb89R96R tdMj7oy7tesRPCGJhPvQNKFDVt/ViAIXbeNr//5zbaGjFsKSUcoTV5YGHJifbltDHpwg h1uC/yPFODsO8a9mEIsf4Q814ZxO8Q/Un4poQlvan5W1vdUWK2LW5gUaAZGGEOg9oZ5j Fbas2L71MgkUpYR/kljGdcQS3AtyK5flNTiBrRKFHOLLbQswc1ZL1LC1b6decCVKZfNG tqFw== X-Gm-Message-State: AOAM533+O1SNyszI+5sTfHrsF67oPqTmPmJRPTdD7KV4FIqExwxq9yGA /+5V4XkhA84h9O4asVrYOKjjtM1X8iqM4A== X-Google-Smtp-Source: ABdhPJyydUmScVgLnR6r1P2/RZu3yLKrvyNK0CQULdAOnIh25nvgNvPQj5jHsUF0VUHdGvBPCl7a2Q== X-Received: by 2002:a5d:4fd2:: with SMTP id h18mr5255750wrw.218.1632834876591; Tue, 28 Sep 2021 06:14:36 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:35 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 01/10] parse-options.h: move PARSE_OPT_SHELL_EVAL between enums Date: Tue, 28 Sep 2021 15:14:22 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a bad landmine of a bug which has been with us ever since PARSE_OPT_SHELL_EVAL was added in 47e9cd28f8a (parseopt: wrap rev-parse --parseopt usage for eval consumption, 2010-06-12). It's an argument to parse_options() and should therefore be in "enum parse_opt_flags", but it was added to the per-option "enum parse_opt_option_flags" by mistake. Therefore as soon as we'd have an enum member in the former that reached its value of "1 << 8" we'd run into a seemingly bizarre bug where that new option would turn on the unrelated PARSE_OPT_SHELL_EVAL in "git rev-parse --parseopt" by proxy. I manually checked that no other enum members suffered from such overlap, by setting the values to non-overlapping values, and making the relevant codepaths BUG() out if the given value was above/below the expected (excluding flags=0 in the case of "enum parse_opt_flags"). Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parse-options.h b/parse-options.h index 39d90882548..3a3176ae65c 100644 --- a/parse-options.h +++ b/parse-options.h @@ -33,6 +33,7 @@ enum parse_opt_flags { PARSE_OPT_KEEP_UNKNOWN = 1 << 3, PARSE_OPT_NO_INTERNAL_HELP = 1 << 4, PARSE_OPT_ONE_SHOT = 1 << 5, + PARSE_OPT_SHELL_EVAL = 1 << 6, }; enum parse_opt_option_flags { @@ -44,7 +45,6 @@ enum parse_opt_option_flags { PARSE_OPT_NODASH = 1 << 5, PARSE_OPT_LITERAL_ARGHELP = 1 << 6, PARSE_OPT_FROM_ALIAS = 1 << 7, - PARSE_OPT_SHELL_EVAL = 1 << 8, PARSE_OPT_NOCOMPLETE = 1 << 9, PARSE_OPT_COMP_ARG = 1 << 10, PARSE_OPT_CMDMODE = 1 << 11, From patchwork Tue Sep 28 13:14:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522549 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E611C433EF for ; Tue, 28 Sep 2021 13:14:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9CB1600CD for ; Tue, 28 Sep 2021 13:14:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240697AbhI1NQU (ORCPT ); Tue, 28 Sep 2021 09:16:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240172AbhI1NQS (ORCPT ); Tue, 28 Sep 2021 09:16:18 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 271E6C061575 for ; Tue, 28 Sep 2021 06:14:39 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id v17so14575435wrv.9 for ; Tue, 28 Sep 2021 06:14:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8TbVUPd8V76NN87Pt7XBq451ixBgEJFvkD0QzmoIPuY=; b=i+X06OR5HxxeqJ2RwJfov6NTlEC8GBvswV2KCNABA+ET8AMt4xMLravy2Zjieaox0b ZWRfLNCrB1jNg7uh1aagTDfSJgT5smEFPm6lsdrzUca1Mc4b2XS6ebbdH4FtoTc4/7VD amdi+BfCQg5sNmRPyA1+/o4kP2cr4oqrZUFuY0kw7cVOc0/zM5li0Q4vs1J+OvBJhrCH 6YD7fMPhacD2RPBwfDQeOHO5P+pU7u3RBSIf9NPupThkd1LuskEl3aun7a69Gfh/wTFo qrpuqlqo0TKFalgIln/YvsaXDIuULL1RGT40oRd9M6RJRXwhuYxbscz/u9yf3o6CzHYL G//A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8TbVUPd8V76NN87Pt7XBq451ixBgEJFvkD0QzmoIPuY=; b=WjbMs4jaV/NAuYAHqNV8IFV7k4jVSpNkJ16zv9LehNq8AnxC1JU2WogcY/QO9BGA4u ljPbXfhpbavRxCbH8lRQftIKiwHs6IauJHPt/AOCf5nOf4v6v8zuYmZ8lVs2Z6TN1V0N j1eLyjR+AI68N/XUjyUSgRoxW4l19954tDxJpQo+es7unW/McIgmiyettERJejqu0lrx kGvDelmS1AhIvTiqXWcFYW//TGc2eZGWqz5deGaHuIcr9Ui0+IYRfpkJ8S1Q+garnOvo oGg8jo8aLe+dq41N5KagaHn2bHawahKiiHz9plRSyI3Zx4GKdufVmnlHTdsJ6oMUf2+/ rxEw== X-Gm-Message-State: AOAM531ROSAVJ7p7MUa6uRdLPheNX+QUxjRSD8RxS41kWOK38hnaZKnx dsDupRSCtnOe4cUyMO5rBPU3TxSLAuwODQ== X-Google-Smtp-Source: ABdhPJwYc0paMY7flo10c/VqHjefpqbrRwvX6/hENRyYba0H5nz/XU2i1AIOS0p47DMSFOAbo7yI/A== X-Received: by 2002:a5d:6481:: with SMTP id o1mr6561414wri.140.1632834877468; Tue, 28 Sep 2021 06:14:37 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:36 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 02/10] parse-options.[ch]: consistently use "enum parse_opt_flags" Date: Tue, 28 Sep 2021 15:14:23 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Use the "enum parse_opt_flags" instead of an "int flags" as arguments to the various functions in parse-options.c. This will help to catch cases where we're not handling cases in switch statements, and generally make it obvious which "flags" we're referring to in this case. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 13 ++++++++----- parse-options.h | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/parse-options.c b/parse-options.c index 55c5821b08d..9dce8b7f8a8 100644 --- a/parse-options.c +++ b/parse-options.c @@ -481,7 +481,8 @@ static void parse_options_check(const struct option *opts) static void parse_options_start_1(struct parse_opt_ctx_t *ctx, int argc, const char **argv, const char *prefix, - const struct option *options, int flags) + const struct option *options, + enum parse_opt_flags flags) { ctx->argc = argc; ctx->argv = argv; @@ -506,7 +507,8 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx, void parse_options_start(struct parse_opt_ctx_t *ctx, int argc, const char **argv, const char *prefix, - const struct option *options, int flags) + const struct option *options, + enum parse_opt_flags flags) { memset(ctx, 0, sizeof(*ctx)); parse_options_start_1(ctx, argc, argv, prefix, options, flags); @@ -838,8 +840,9 @@ int parse_options_end(struct parse_opt_ctx_t *ctx) } int parse_options(int argc, const char **argv, const char *prefix, - const struct option *options, const char * const usagestr[], - int flags) + const struct option *options, + const char * const usagestr[], + enum parse_opt_flags flags) { struct parse_opt_ctx_t ctx; struct option *real_options; @@ -861,7 +864,7 @@ int parse_options(int argc, const char **argv, const char *prefix, case PARSE_OPT_NON_OPTION: case PARSE_OPT_DONE: break; - default: /* PARSE_OPT_UNKNOWN */ + case PARSE_OPT_UNKNOWN: if (ctx.argv[0][1] == '-') { error(_("unknown option `%s'"), ctx.argv[0] + 2); } else if (isascii(*ctx.opt)) { diff --git a/parse-options.h b/parse-options.h index 3a3176ae65c..fb5aafd4f7b 100644 --- a/parse-options.h +++ b/parse-options.h @@ -213,7 +213,8 @@ struct option { */ int parse_options(int argc, const char **argv, const char *prefix, const struct option *options, - const char * const usagestr[], int flags); + const char * const usagestr[], + enum parse_opt_flags flags); NORETURN void usage_with_options(const char * const *usagestr, const struct option *options); @@ -270,7 +271,8 @@ struct parse_opt_ctx_t { void parse_options_start(struct parse_opt_ctx_t *ctx, int argc, const char **argv, const char *prefix, - const struct option *options, int flags); + const struct option *options, + enum parse_opt_flags flags); int parse_options_step(struct parse_opt_ctx_t *ctx, const struct option *options, From patchwork Tue Sep 28 13:14:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522551 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34D1AC433F5 for ; Tue, 28 Sep 2021 13:14:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1CC7161139 for ; Tue, 28 Sep 2021 13:14:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240712AbhI1NQV (ORCPT ); Tue, 28 Sep 2021 09:16:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240688AbhI1NQU (ORCPT ); Tue, 28 Sep 2021 09:16:20 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90F64C061575 for ; Tue, 28 Sep 2021 06:14:40 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id w29so57864287wra.8 for ; Tue, 28 Sep 2021 06:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sqSstnTP+KtUctKZ7pL2cKWlUwUbOzaNkbDJ6RsPu2k=; b=kjtSzBoXLDRnL7D+YJKntP1SPvH6QzfgicNUia7jVLlbOO9eMi/KSipVpQILB1UlEB ZapsO2dg0b5B15c1s/S1PiaIg8kHrB7l23vfM7GnR00a/hrxeEzOBrZrnMWz480Uu0pR 9c1CisaKTFCx53JTblCENPQ/WYN9Ct9Qey914TMo2/ATmBqfNQHgWRsEv52CcoPhp1u3 xwt1e3+e+q2nFTPvZCSvHUi4QvlQ763/tt7x/X8oXsCoxqXVTFfjUNK2okwoaI7Lb4vR wkQTUrj+fgSNTdKpCWjwGV6dEQ25OWXwLQkZ2bHJtuK8Xzjjh4MSDPd177WaKWMqvjzd rPBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sqSstnTP+KtUctKZ7pL2cKWlUwUbOzaNkbDJ6RsPu2k=; b=QXjtfMN05N6vR+5IWL515DQK2/Y+Hbp3znGtIlnh5OtbSxNwKQKMbO5/tt2VKsp8wy D/w650zJu+IUzOqweUn3BKYLGZyr899jUXuYRISvnIj5NUJanHGmZWHQOLBZX8QJmrFF 5oMcSwfvCMULd5pA0XGeI/Q/6h5mfk3+YNAIkLVFMRcrFdCO1jcmRNTJ40vy5oq8qC08 iN99ygvt8fUb1EhtdSK9A2pbnbDth8slup3DcVHWydBs91kCXFaU3SDfPmBKmS1Ay1VI zy8DJb7TeReSOxN0ZzPhDp9mh/uFZRmQ5j3TQy/PBOriQ9xEA28WhhUkEMVLN57cx/0Z qjIw== X-Gm-Message-State: AOAM532I1+/8WHK5vGpIH5fqfgIfoZoNWc5p5cfHls2ZxuAPCXenr+PC l8TU7MaEri3fYTU18kI42ADJ5+C7303xsQ== X-Google-Smtp-Source: ABdhPJwPjkQfe7g6WQ4Eq92K9xtYTaUqIrDtOQL+3XDlgxQvXxVxyzGGdTf/2zOCygrWZrzXar5DmA== X-Received: by 2002:adf:f387:: with SMTP id m7mr6219793wro.53.1632834878828; Tue, 28 Sep 2021 06:14:38 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:37 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 03/10] parse-options.[ch]: consistently use "enum parse_opt_result" Date: Tue, 28 Sep 2021 15:14:24 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Use the "enum parse_opt_result" instead of an "int flags" as the return value of the applicable functions in parse-options.c. This will help catch future bugs, such as the missing "case" arms in the two existing users of the API in "blame.c" and "shortlog.c". A third caller in 309be813c9b (update-index: migrate to parse-options API, 2010-12-01) was already checking for these. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/blame.c | 3 +++ builtin/shortlog.c | 3 +++ parse-options.c | 15 ++++++++------- parse-options.h | 15 ++++++++------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 641523ff9af..9273fb222d5 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -917,6 +917,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix) PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); for (;;) { switch (parse_options_step(&ctx, options, blame_opt_usage)) { + case PARSE_OPT_NON_OPTION: + case PARSE_OPT_UNKNOWN: + break; case PARSE_OPT_HELP: case PARSE_OPT_ERROR: exit(129); diff --git a/builtin/shortlog.c b/builtin/shortlog.c index 3e7ab1ca821..e7f7af5de3f 100644 --- a/builtin/shortlog.c +++ b/builtin/shortlog.c @@ -374,6 +374,9 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix) for (;;) { switch (parse_options_step(&ctx, options, shortlog_usage)) { + case PARSE_OPT_NON_OPTION: + case PARSE_OPT_UNKNOWN: + break; case PARSE_OPT_HELP: case PARSE_OPT_ERROR: exit(129); diff --git a/parse-options.c b/parse-options.c index 9dce8b7f8a8..799cd884f2b 100644 --- a/parse-options.c +++ b/parse-options.c @@ -703,9 +703,9 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *, const char * const *, const struct option *, int, int); -int parse_options_step(struct parse_opt_ctx_t *ctx, - const struct option *options, - const char * const usagestr[]) +enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, + const struct option *options, + const char * const usagestr[]) { int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); @@ -839,10 +839,11 @@ int parse_options_end(struct parse_opt_ctx_t *ctx) return ctx->cpidx + ctx->argc; } -int parse_options(int argc, const char **argv, const char *prefix, - const struct option *options, - const char * const usagestr[], - enum parse_opt_flags flags) +enum parse_opt_result parse_options(int argc, const char **argv, + const char *prefix, + const struct option *options, + const char * const usagestr[], + enum parse_opt_flags flags) { struct parse_opt_ctx_t ctx; struct option *real_options; diff --git a/parse-options.h b/parse-options.h index fb5aafd4f7b..d931300f4d6 100644 --- a/parse-options.h +++ b/parse-options.h @@ -211,10 +211,11 @@ struct option { * untouched and parse_options() returns the number of options * processed. */ -int parse_options(int argc, const char **argv, const char *prefix, - const struct option *options, - const char * const usagestr[], - enum parse_opt_flags flags); +enum parse_opt_result parse_options(int argc, const char **argv, + const char *prefix, + const struct option *options, + const char * const usagestr[], + enum parse_opt_flags flags); NORETURN void usage_with_options(const char * const *usagestr, const struct option *options); @@ -274,9 +275,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx, const struct option *options, enum parse_opt_flags flags); -int parse_options_step(struct parse_opt_ctx_t *ctx, - const struct option *options, - const char * const usagestr[]); +enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, + const struct option *options, + const char * const usagestr[]); int parse_options_end(struct parse_opt_ctx_t *ctx); From patchwork Tue Sep 28 13:14:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59476C433EF for ; Tue, 28 Sep 2021 13:14:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DB7D61139 for ; Tue, 28 Sep 2021 13:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240729AbhI1NQX (ORCPT ); Tue, 28 Sep 2021 09:16:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240702AbhI1NQU (ORCPT ); Tue, 28 Sep 2021 09:16:20 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B7BDC061575 for ; Tue, 28 Sep 2021 06:14:41 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id l18-20020a05600c4f1200b002f8cf606262so2249944wmq.1 for ; Tue, 28 Sep 2021 06:14:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lYsVGCBZyb5BeYgYOZ3qFR9LXWcBnJzeMIMOD9Z6XWs=; b=AkyktUnFtwwSmzvPxp3H56YKIks8TBKShpb3RAZYNBdBjLlhJgyFSuZuVtua0oFS3V iO4n7WNLDWQANQhIchQNjejt75tlnhMgzyOCm5x+QUCHzIHR/LB2Gooh+C9eykvwQrWX CL3pONwn1o+d1NXBnrq7zWmVypjfL0FlzQQI6g5eRQbdpVsupzEM4qPzHbp29DHMP5rr 7Ro2qOdg0EX+zP1ycNIZR11DVLtDE571aOpzcaqksUVgh6OagS6ikOVpuj8WwFiJS6Bn q1DUmv7A8fxLOIx+77Qud4COioLhXoKdellbs3NKCw9FmUQLUOe1FQjqA570E/9ashej MQtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lYsVGCBZyb5BeYgYOZ3qFR9LXWcBnJzeMIMOD9Z6XWs=; b=Ha08hxhgjvC2UPdJiz3ie4SBk97pRAdDR0CvLJQDg4vZm4oALHY76V62alBtE51xRm waEkpTK8EQ8CfxF8ptYQwpJpRG9IX5BzWn7SM3V7jrphYUziQRREbOBf2LkrpnUzwI14 qHHWzViBcw6JrnVuZ79yDggVSeLCVMY4CylBWmJcKlZRBZkZGWesSj/QMZV3T+Q9iQI2 xzmmj/luXJeVLoaMquO2ezBh7rOP47z7qiZcnxMltlSo7BRrAkhP9ihWxJnUntxG6VT8 IRrGLLNMEC4tPRSpR3BJkKjZ8rEGJC1pvTPveFQr/ahctzryDbn1ent3+2ZGqH7GbH5F Abpg== X-Gm-Message-State: AOAM530NJ+uTrBcgpDvpa4W1OIt/RtvYJYxb/BgSTsSleX/VrCTG6bGg ul4lADEyJYMT6BWfoKDbrkbrVDhquMsQpQ== X-Google-Smtp-Source: ABdhPJx3Kh1e3UYCakzaXXOTMDkxHOS+rgxQ9FHkrtcFSej+b3JDQTORw8PPQvCbyKEv1S62A8TLdg== X-Received: by 2002:a7b:cb96:: with SMTP id m22mr2005732wmi.127.1632834879726; Tue, 28 Sep 2021 06:14:39 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:39 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 04/10] parse-options.c: use exhaustive "case" arms for "enum parse_opt_type" Date: Tue, 28 Sep 2021 15:14:25 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code in get_value(), parse_options_check() etc. to do away with the "default" case in favor of exhaustively checking the relevant fields. The added "return -1" is needed for the GCC version commented on inline, my local clang 11.0.1-2 does not require it. Let's add it for now to appease GCC. The added "special types" etc. comments correspond to the relevant comments and ordering on the "enum parse_opt_type". Let's try to keep the same order and commentary as there where possible for clarity. This doesn't reach that end-state, and due to the different handling of options it's probably not worth it to get there, but let's match its ordering where it's easy to do so. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 43 ++++++++++++++++++++++++++++++++++++++----- parse-options.h | 2 +- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/parse-options.c b/parse-options.c index 799cd884f2b..733cbfa8821 100644 --- a/parse-options.c +++ b/parse-options.c @@ -219,9 +219,14 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, optname(opt, flags)); return 0; - default: + /* special types */ + case OPTION_END: + case OPTION_GROUP: + case OPTION_NUMBER: + case OPTION_ALIAS: BUG("opt->type %d should not happen", opt->type); } + return -1; /* gcc 10.2.1-6's -Werror=return-type */ } static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p, @@ -468,8 +473,15 @@ static void parse_options_check(const struct option *opts) BUG("OPT_ALIAS() should not remain at this point. " "Are you using parse_options_step() directly?\n" "That case is not supported yet."); - default: - ; /* ok. (usually accepts an argument) */ + + case OPTION_BITOP: + case OPTION_END: + case OPTION_FILENAME: + case OPTION_GROUP: + case OPTION_INTEGER: + case OPTION_MAGNITUDE: + case OPTION_STRING: + break; } if (opts->argh && strcspn(opts->argh, " _") != strlen(opts->argh)) @@ -543,7 +555,15 @@ static void show_negated_gitcomp(const struct option *opts, int show_all, case OPTION_SET_INT: has_unset_form = 1; break; - default: + /* special types */ + case OPTION_END: + case OPTION_GROUP: + case OPTION_NUMBER: + case OPTION_ALIAS: + /* options with no arguments */ + case OPTION_BITOP: + /* options with arguments (usually) */ + case OPTION_LOWLEVEL_CALLBACK: break; } if (!has_unset_form) @@ -593,7 +613,20 @@ static int show_gitcomp(const struct option *opts, int show_all) break; suffix = "="; break; - default: + /* special types */ + case OPTION_END: + case OPTION_NUMBER: + case OPTION_ALIAS: + + /* options with no arguments */ + case OPTION_BIT: + case OPTION_NEGBIT: + case OPTION_BITOP: + case OPTION_COUNTUP: + case OPTION_SET_INT: + + /* options with arguments (usually) */ + case OPTION_LOWLEVEL_CALLBACK: break; } if (opts->flags & PARSE_OPT_COMP_ARG) diff --git a/parse-options.h b/parse-options.h index d931300f4d6..a1c7c86ad30 100644 --- a/parse-options.h +++ b/parse-options.h @@ -264,7 +264,7 @@ struct parse_opt_ctx_t { const char **out; int argc, cpidx, total; const char *opt; - int flags; + enum parse_opt_flags flags; const char *prefix; const char **alias_groups; /* must be in groups of 3 elements! */ struct option *updated_options; From patchwork Tue Sep 28 13:14:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522555 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 665EDC4332F for ; Tue, 28 Sep 2021 13:14:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F76F61139 for ; Tue, 28 Sep 2021 13:14:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240744AbhI1NQY (ORCPT ); Tue, 28 Sep 2021 09:16:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240711AbhI1NQV (ORCPT ); Tue, 28 Sep 2021 09:16:21 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33B13C061604 for ; Tue, 28 Sep 2021 06:14:42 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id t18so58045904wrb.0 for ; Tue, 28 Sep 2021 06:14:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5mlWwAd9DdLnQL5m8pL8Kr/zuypj9LecLwbSXk/gHB0=; b=Ze/7XgJiBuNMTuZqIzc/y9nDo3dkwEqdh4Jx9y0ER0iH9nzSDM79LHcx16BPU6LiIF lVFAT0ijTLvJks2oJIPpJuBOIuuVhKEax7A+flES1BgqD11XDLg7w+BZlHseyBzbYS1C rD0lAoLf5HdyLmBnOvaqBa0vNF+r4HlVmZfRmF5aesHBzH3YOPRQlw27MZDWztHkIc2G n9gYcsGUVp/79BcmHvSIcf28zDh/WGsZK1fws0EUgcB/OqT676aduCy/vlYau3TiSM0d GyJV1DvdrKVM4ST8PlXL9mIOzyo/onW7Eu0wl13lzPxyXPDPHxEFYS2gPtXipmTJjfjp HSNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5mlWwAd9DdLnQL5m8pL8Kr/zuypj9LecLwbSXk/gHB0=; b=Yp90INgB9pf1bBuUP7e/MDnSVnr6qFgAtTkT3Mn5IIGUXOI3FtAkTWO6MgrgX+aKmq ZK+J4myCJOO25zwlCGeH681AdmnVuhP5ojynZEEQ4fC/OSZAOerajdmOShEAeNgVLekg LlDhTdmYtrjY967K4YxFRxiZ2wxmpTsPgpb1QR4KYrs0TdkHDnWlB12wNJU0nLqdMQ1P a84DME0FRLalygFJfIZaclAsG8jFYAeJJVX/C5I6Pd7OKTwry76gKetunkvfTV/PYVF8 erxDaJ4Q2nQB7KwsLmgteulheuWlRDNSBZ1MjPKR7NNCpYtSokx954RQNr4ju8YglcJn Aw0g== X-Gm-Message-State: AOAM532khZImhsqCkizi0GlAOlicEEVoy7hV9191IKVt0Zp1xbgl9eDi 4H5rCn+ryI9HNmYc18urWBbZ6pHNDF0fFw== X-Google-Smtp-Source: ABdhPJymSEoV9husUV9cHbsQY75025aGmYUf54Sb1wsS3UAtfSkdJSIA+Xz8splF4sYhYkYJUpPtmg== X-Received: by 2002:a5d:4212:: with SMTP id n18mr1748374wrq.162.1632834880566; Tue, 28 Sep 2021 06:14:40 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:39 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 05/10] parse-options.h: make the "flags" in "struct option" an enum Date: Tue, 28 Sep 2021 15:14:26 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the "flags" members of "struct option" to refer to their corresponding "enum" defined earlier in the file. The benefit of changing this to an enum isn't as great as with some "enum parse_opt_type" as we'll always check this as a bitfield, so we can't rely on the compiler checking "case" arms for us. But let's do it for consistency with the rest of the file. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parse-options.h b/parse-options.h index a1c7c86ad30..74b66ba6e93 100644 --- a/parse-options.h +++ b/parse-options.h @@ -134,7 +134,7 @@ struct option { const char *argh; const char *help; - int flags; + enum parse_opt_option_flags flags; parse_opt_cb *callback; intptr_t defval; parse_opt_ll_cb *ll_callback; From patchwork Tue Sep 28 13:14:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522559 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C634FC433EF for ; Tue, 28 Sep 2021 13:14:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B072C61139 for ; Tue, 28 Sep 2021 13:14:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240742AbhI1NQ2 (ORCPT ); Tue, 28 Sep 2021 09:16:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240688AbhI1NQW (ORCPT ); Tue, 28 Sep 2021 09:16:22 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA7F8C061575 for ; Tue, 28 Sep 2021 06:14:42 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id b192so3466566wmb.2 for ; Tue, 28 Sep 2021 06:14:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uFnRPV4NL/Qy/7LGTomKuAfgVNv7KKElg2mHDO8c+kY=; b=O2nEFahmjrJeVQ+yeTEaJu1mZYZcmOmi1OaAz6YELBQ3S57hO/Q4WWK6DAh+OY2BVO +130lsv260/4Ri2LKkcqwgIJ8cYAQ2ZmWWzvaETP9w4BiULHS8AWFPSIKDs8AF1sW34C R6hS0MLyzgjB+R/sTSlpUD7Oh23dSpgVJSHckH2pZJVVn3PZ5j4UK9twMOeTaXWRlNvv l+1fr6+zExd4KwlfxP6oqMcfQ8k56TKWvdP16o9Tms+ZwVUYCWIvMrSXCh6UQewo0S/M xncIRBg0eeZyaa/99ID/5VmlqKjmn8VO7OVeGKJtNYukTClwzmBfzNMOzCB9ocneWOQy eH5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uFnRPV4NL/Qy/7LGTomKuAfgVNv7KKElg2mHDO8c+kY=; b=c2Qx33lZiaShJPseJeg9FKhuGWfkd+P9RJIei55Mi58pWxFJcw4etZ4/1b3UvOSUTT UpwB/3qofaz8OgvL/MPBUg37M3hgzQD9irc96KXV6oyj6PU4rR89exXLh0FayrQJ9WDW +uXEJUQXgjwR4CdsPwWns7BlTVKG6nfb3zNeVK7v8RlIFvRng7AWdPKjStmET25GBEKC a5+OtwLfSzCvhKWQlFXPp+7aJQf07mahwNIvDdZqfYeltfx0UVvxnto2Ef2Htd9RpiCV TG8eptWjIt/m4bDc49JkBj4M//MNMXANr3BjEl+X874Jha3wg5thfHVEaO0qqeN25pVv YBsw== X-Gm-Message-State: AOAM5325mMQ0UU/gBb+/YADzBGEI2qd534l4fW5JQhzGlDMMVgapmGbQ dD5jn4X5bhWP7cH32AhxDm4tTgIJ0BjwTQ== X-Google-Smtp-Source: ABdhPJwAaVF06w8k7vZSur8MVwcSzSXwJNHadwWAG62dkP6JLIPnICUweNMvBDt7fPet72wY5vS+uQ== X-Received: by 2002:a7b:c112:: with SMTP id w18mr4657237wmi.86.1632834881304; Tue, 28 Sep 2021 06:14:41 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:40 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 06/10] parse-options.c: move optname() earlier in the file Date: Tue, 28 Sep 2021 15:14:27 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In preparation for making "optname" a static function move it above its first user in parse-options.c. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/parse-options.c b/parse-options.c index 733cbfa8821..238a283db5d 100644 --- a/parse-options.c +++ b/parse-options.c @@ -22,6 +22,21 @@ int optbug(const struct option *opt, const char *reason) return error("BUG: switch '%c' %s", opt->short_name, reason); } +const char *optname(const struct option *opt, int flags) +{ + static struct strbuf sb = STRBUF_INIT; + + strbuf_reset(&sb); + if (flags & OPT_SHORT) + strbuf_addf(&sb, "switch `%c'", opt->short_name); + else if (flags & OPT_UNSET) + strbuf_addf(&sb, "option `no-%s'", opt->long_name); + else + strbuf_addf(&sb, "option `%s'", opt->long_name); + + return sb.buf; +} + static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, const struct option *opt, int flags, const char **arg) @@ -1037,18 +1052,3 @@ void NORETURN usage_msg_opt(const char *msg, fprintf(stderr, "fatal: %s\n\n", msg); usage_with_options(usagestr, options); } - -const char *optname(const struct option *opt, int flags) -{ - static struct strbuf sb = STRBUF_INIT; - - strbuf_reset(&sb); - if (flags & OPT_SHORT) - strbuf_addf(&sb, "switch `%c'", opt->short_name); - else if (flags & OPT_UNSET) - strbuf_addf(&sb, "option `no-%s'", opt->long_name); - else - strbuf_addf(&sb, "option `%s'", opt->long_name); - - return sb.buf; -} From patchwork Tue Sep 28 13:14:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522557 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86EACC433EF for ; Tue, 28 Sep 2021 13:14:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 71B63600CD for ; Tue, 28 Sep 2021 13:14:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240747AbhI1NQZ (ORCPT ); Tue, 28 Sep 2021 09:16:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240718AbhI1NQX (ORCPT ); Tue, 28 Sep 2021 09:16:23 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6E22C061740 for ; Tue, 28 Sep 2021 06:14:43 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id f78-20020a1c1f51000000b0030cdb3d6079so2733568wmf.3 for ; Tue, 28 Sep 2021 06:14:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9+AKfWqP5PTcsFh1Y92T7q30jGoiSXplv/SwQO0S6Pw=; b=SCcEMBuDMQl7SBHycjpjwpJmpOLH1hnBcPjIFolr8u2ZWBA+GMg+4w7tqSZpFuYxIj Ycir9DqeowZBY8qEarnU4ImZrfZS6a3mU5QCQapnhiTJCqbo7gwn4pkLQXqkP2NYRpls ozjRx6+GyKEdfrVm+k9LuNJHYyDPzjqnA4Tb9j9if0LTSD3znF5guWZrgbvC1xj+2NmC ayid1KFqNVhrBmCi19YR5UsY+Cu/UIIhE9oVFMZ/h+DBIMJil5mFRsM0vMhJVlwvO7Ll 4VTl0+pn1HAcaQ9zQVUC8aJBtFI8Pzwqd+LvWEgvAHFdcWzg96ZRE3OjbTTjwKjngK8S ILhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9+AKfWqP5PTcsFh1Y92T7q30jGoiSXplv/SwQO0S6Pw=; b=ilNXAxd8eqzxxcDeAQ1Svuic8PXne+u5Sa/oHpBck4HFTX8qWbrx+kVx1gxsTWubhP xGjVdYp2Ns6qoSS6LIyp96TqxtnKfXWl4xtNvd5Qbv29h1iKT1VmMpBJ50J88ZfU7iz1 RJfasBSj4tN2/KOaQiZnfqvYL/wdf7Rux6Q1dXZ+zZcb8PFPwU1XAOcBNxglsDUk/zUL r6fshc6LDwjn8QFKGgnNpT3UNAGtnJutSadshmgZms5puwH8gBWIIMnnVwjRVcdFWVGM OJVWf6YNzjsawcnCWdgY2QEsx+d4cxIwrmsNB6Tc5RrT/z+9wJbne4Ar23D1LWFtnA2Y ZMBA== X-Gm-Message-State: AOAM533VVQd7BmaeupJt3s4bmg1D7qtSSdSwnbkV7XEsyfL42UexT2O6 nUD0AZNF9qBnPy8Fkp2nAhqnguaitxV+hA== X-Google-Smtp-Source: ABdhPJz7PC9I1BeADIWruqqG5bCRj9T3ZMk8kTtUMk2q6TJ2rfZNftDdwkRt5pdQI2Jd4yIabcsqQA== X-Received: by 2002:a05:600c:198c:: with SMTP id t12mr4751128wmq.128.1632834882057; Tue, 28 Sep 2021 06:14:42 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:41 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 07/10] commit-graph: stop using optname() Date: Tue, 28 Sep 2021 15:14:28 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Stop using optname() in builtin/commit-graph.c to emit an error with the --max-new-filters option. This changes code added in 809e0327f57 (builtin/commit-graph.c: introduce '--max-new-filters=', 2020-09-18). See 9440b831ad5 (parse-options: replace opterror() with optname(), 2018-11-10) for why using optname() like this is considered bad, i.e. it's assembling human-readable output piecemeal, and the "option `X'" at the start can't be translated. It didn't matter in this case, but this code was also buggy in its use of "opt->flags" to optname(), that function expects flags, but not *those* flags. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/commit-graph.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c index 0386f5c7755..36552db89fe 100644 --- a/builtin/commit-graph.c +++ b/builtin/commit-graph.c @@ -172,8 +172,7 @@ static int write_option_max_new_filters(const struct option *opt, const char *s; *to = strtol(arg, (char **)&s, 10); if (*s) - return error(_("%s expects a numerical value"), - optname(opt, opt->flags)); + return error(_("option `max-new-filters' expects a numerical value")); } return 0; } From patchwork Tue Sep 28 13:14:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1B1CC433FE for ; Tue, 28 Sep 2021 13:14:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C94A961131 for ; Tue, 28 Sep 2021 13:14:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240764AbhI1NQa (ORCPT ); Tue, 28 Sep 2021 09:16:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240732AbhI1NQX (ORCPT ); Tue, 28 Sep 2021 09:16:23 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CF39C061604 for ; Tue, 28 Sep 2021 06:14:44 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id d26so409520wrb.6 for ; Tue, 28 Sep 2021 06:14:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8Y/K37CLslmBOGjQtg0CREEHlDXNbAwZD8skEKjGK6M=; b=kwSfRM7f1qiJ8dTw9mCNZEWny8lqSVQ0aELwJuYtWasvLn1w4O90cD1dI5G8RodurL R9KDcby6QEL1sPZv/P3iOEXU0xWK3NdPocuadxtzXYMbKx2/XvngTB3M+/09obJIwObM 5yFZMiv+odMW71cqKR2NYU+yRufmgMCg0hX2NQGdwyd33EdgNg5rKvXn62OdYtpMg9Rp 9sgzQHemAH/YO+eHUfYVEDgZjrJr8O0OdLRUIIHNUBaCW3isfQ7dyoQdYlFOpJTRMcmv jpTGwPPqLKuZT85tXJEYvUoZ5ZM872gGz5Uj4WztpMwCoD/OHGDDzYs/fnb/+4VwfshB 6qcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8Y/K37CLslmBOGjQtg0CREEHlDXNbAwZD8skEKjGK6M=; b=xCvSX2rCUoV4yya9ICcq3fNACuJwuluTUuZl7QJ3gPvAMFT7MNoJpdHwX2aO/P+Vo7 JL2eyXv1a5+QAeCpZv2tm0pvmcFqUwNK4GNmw5Lqjr7VG6sNba/nwvvX8QZMn99l4gmx 4Xf6bScxJ9rn3uJkFb27rho0AEzex9JcYSlV0FI5edlnYogGCJWh4zLpfH7DMNlidGU+ Wju5PvKpW0h6HM07CNxOgfpjmL8wRqpyOAbMdXr3fe00LkR2IPtZJcWeZniq4rBlxvwj zMkGrBrtlh1EpsLhiIdaGP4zn40ePZfHHuINPQ5mc0HIgeKD4YesuWB+bBxMsbB49YQo N9DQ== X-Gm-Message-State: AOAM532KXjgXbS5WMsWPwGt/WNpcGgzBrNjsP6EC8uLjDPwzY1Hierwj IFXE/p9t1F4XKdZJ2QVQq8ZRZIb8xRE01Q== X-Google-Smtp-Source: ABdhPJyADK4qmemre8OEiFNnknpJdH0CreV6/7N1HNTiAM8cTEYDBNDePkwTAg9AZ+DK8KxKPC4QVw== X-Received: by 2002:adf:f748:: with SMTP id z8mr3038844wrp.82.1632834882782; Tue, 28 Sep 2021 06:14:42 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:42 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 08/10] parse-options.[ch]: make opt{bug,name}() "static" Date: Tue, 28 Sep 2021 15:14:29 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change these two functions to "static", the last user of "optname()" outside of parse-options.c itself went away in the preceding commit, for the reasons noted in 9440b831ad5 (parse-options: replace opterror() with optname(), 2018-11-10) we shouldn't be adding any more users of it. The "optbug()" function was never used outside of parse-options.c, but was made non-static in 1f275b7c4ca (parse-options: export opterr, optbug, 2011-08-11). I think the only external user of optname() was the commit-graph.c caller added in 09e0327f57 (builtin/commit-graph.c: introduce '--max-new-filters=', 2020-09-18). Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 4 ++-- parse-options.h | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/parse-options.c b/parse-options.c index 238a283db5d..57e95846e83 100644 --- a/parse-options.c +++ b/parse-options.c @@ -11,7 +11,7 @@ static int disallow_abbreviated_options; #define OPT_SHORT 1 #define OPT_UNSET 2 -int optbug(const struct option *opt, const char *reason) +static int optbug(const struct option *opt, const char *reason) { if (opt->long_name) { if (opt->short_name) @@ -22,7 +22,7 @@ int optbug(const struct option *opt, const char *reason) return error("BUG: switch '%c' %s", opt->short_name, reason); } -const char *optname(const struct option *opt, int flags) +static const char *optname(const struct option *opt, int flags) { static struct strbuf sb = STRBUF_INIT; diff --git a/parse-options.h b/parse-options.h index 74b66ba6e93..dd79c9c566f 100644 --- a/parse-options.h +++ b/parse-options.h @@ -224,9 +224,6 @@ NORETURN void usage_msg_opt(const char *msg, const char * const *usagestr, const struct option *options); -int optbug(const struct option *opt, const char *reason); -const char *optname(const struct option *opt, int flags); - /* * Use these assertions for callbacks that expect to be called with NONEG and * NOARG respectively, and do not otherwise handle the "unset" and "arg" From patchwork Tue Sep 28 13:14:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26272C433F5 for ; Tue, 28 Sep 2021 13:14:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1281F61131 for ; Tue, 28 Sep 2021 13:14:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239811AbhI1NQb (ORCPT ); Tue, 28 Sep 2021 09:16:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240724AbhI1NQY (ORCPT ); Tue, 28 Sep 2021 09:16:24 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AE54C061604 for ; Tue, 28 Sep 2021 06:14:45 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id a131so3474303wmd.1 for ; Tue, 28 Sep 2021 06:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ywvtN6dkRVDSG7f9k+PKUlwxClMBGIgk0+5HPeBmoEs=; b=SdS8bvfAkCkQaJ7JWsX3LiY44WGH2qD59EvBz7sKh3QIF/WmSpBxlMmTaUmPgoIfnO KWR1bu8VTE5zFeFmlGQSeEiBGxzPrkfSOSQ0fhtyOvCSlREz4VOJqsRpSeyTtytGzPic lgfI9hebhEsyfALy4OF7XE/Y49w8YfoBiIRfjJo12opIICZmMpE9hLq6mb+/3747wqNd g7bC/fPYuro88EGlLmdaaGjHxLgFt6AN/c6hGF1bFGn+Sx5aublzcYNugaCTIBQDaiiz Y8x0KfZr2jSQ953zORJKimN6aNCfLc+n60jL1rVVlZXH8/dB62v1gu3jCLFjlb9ypOWf 84Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ywvtN6dkRVDSG7f9k+PKUlwxClMBGIgk0+5HPeBmoEs=; b=DoQ9hyR/FNVz+K2V+LgSIA366R6ZfelI4YdteFxBFyGBAuoIF/8+chluOyLSyObucE cw87dnatTBbtlueSwZK1qO4uR9bCbe+Rl/LfjUZ1MDKd+GFKWraJW7wybodx9TfqOorB zM/UG0TaVpd+YKGaOucg5sl6pcRSsX9T+E8Lwge2LnXFQayxQqdJfz49EpiFc7JrudZz SqN1eEua2DwBECovKXh2fc5TLfo+zO9wo6bUsjK6YNpKFIPNbG/TkzZlAwR4jN5OGkhV fP+54NUd+pk6fjZSVJ9BqUUO7b7amyZn/LzPtUFy999udNsMFRCHI0CLHqJ+6v9RghBH ycIg== X-Gm-Message-State: AOAM533kA7rf+N/P4Bku/ehkmrB0iXxsBtnsHoJvphMQxnnh6fG+pjMn tbcTe+5aNeq/K2ri5D929j5TlldJbqUwfw== X-Google-Smtp-Source: ABdhPJzln4/QQApJRLHMVE2dg+y5dG2SZmhPlH7WHM6ydb5Nt+4T5mr2gWidsc/IaK+KrnxKHKeQgQ== X-Received: by 2002:a7b:c219:: with SMTP id x25mr4653704wmi.125.1632834883604; Tue, 28 Sep 2021 06:14:43 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:43 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 09/10] parse-options tests: test optname() output Date: Tue, 28 Sep 2021 15:14:30 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org There were no tests for checking the specific output that we'll generate in optname(), let's add some. That output was added back in 4a59fd13122 (Add a simple option parser., 2007-10-15). Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t0040-parse-options.sh | 42 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index da310ed29b1..d6f391a497b 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -168,9 +168,45 @@ test_expect_success 'long options' ' ' test_expect_success 'missing required value' ' - test_expect_code 129 test-tool parse-options -s && - test_expect_code 129 test-tool parse-options --string && - test_expect_code 129 test-tool parse-options --file + cat >expect <<-\EOF && + error: switch `s'\'' requires a value + EOF + test_expect_code 129 test-tool parse-options -s 2>actual && + test_cmp expect actual && + + cat >expect <<-\EOF && + error: option `string'\'' requires a value + EOF + test_expect_code 129 test-tool parse-options --string 2>actual && + test_cmp expect actual && + + cat >expect <<-\EOF && + error: option `file'\'' requires a value + EOF + test_expect_code 129 test-tool parse-options --file 2>actual && + test_cmp expect actual +' + +test_expect_success 'superfluous value provided: boolean' ' + cat >expect <<-\EOF && + error: option `yes'\'' takes no value + EOF + test_expect_code 129 test-tool parse-options --yes=hi 2>actual && + test_cmp expect actual && + + cat >expect <<-\EOF && + error: option `no-yes'\'' takes no value + EOF + test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual && + test_cmp expect actual +' + +test_expect_success 'superfluous value provided: cmdmode' ' + cat >expect <<-\EOF && + error: option `mode1'\'' takes no value + EOF + test_expect_code 129 test-tool parse-options --mode1=hi 2>actual && + test_cmp expect actual ' cat >expect <<\EOF From patchwork Tue Sep 28 13:14:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12522565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E8BEC433FE for ; Tue, 28 Sep 2021 13:14:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5433F61131 for ; Tue, 28 Sep 2021 13:14:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240811AbhI1NQc (ORCPT ); Tue, 28 Sep 2021 09:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240711AbhI1NQZ (ORCPT ); Tue, 28 Sep 2021 09:16:25 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E8CAC061575 for ; Tue, 28 Sep 2021 06:14:46 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id t18so58046508wrb.0 for ; Tue, 28 Sep 2021 06:14:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=y2ga8NrhoHT62xcigXV6kP7xXqqvMgVdPXbBGem337I=; b=SJc2xfk82cG49oQt2MncZbeaJaAsBkZJOiXNFxqp20frEPaIwZ1ESEHNf9JNnQNM2V HnUvOIYFK0SWIlBwqanllewfZ/Q5/yEA4PoPOOnMy+QLa4n+yBZ4Yg39eLIa7145DtKg so0v2ODbwkADErvak4O9sUbE1IE72Yn4L8dyPIXNgtdvrYUGJsVeOrQPOgdvDw5RclUx bNDHFwHDufFG/9O9nOX0iECUOb2wPTzxSHZ5G5ogfDRSE2qZOXh0oll7ewZgTSYSjkMJ 1/VXMFOJvm848+zxFmBQXVwZvIArb4l/aiNzfTt7HdHUtOVTZQFpKF6XYJjhhW3EeBhM QL7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=y2ga8NrhoHT62xcigXV6kP7xXqqvMgVdPXbBGem337I=; b=JMGCSbPtkT3yGjWKhUTnF2EbR0BI5pXN/cUnuMMn2nsq00+27dqaC8k7WdQ/BuRLE8 4hlk7rY+CPz0XTxBTXT3jTyain+6aMeerze4Bn4EzXlNUk5Z18jzcMMfVHMr1lnR7MOq iD6fDbC4wtUhGq/+xjyDQX9DQEuI4HAbr4FWDme+4TqGrs7a8+4mjSSHkyCQkmbsHRUE iUlFfGFu4iw3ggNGtBb2LeA5tJE4xPk6btO/w4ueL98Ggr/cipNbFTWjcA+1hXhP+SPB 8uiDuA265eNwwq8r25klRrF1Tu8b/xEfSzmUoO4V8lUiZ9YwO3ruNZqBFyyHDjLzKo9V 7pNw== X-Gm-Message-State: AOAM532E2DM3ygCrR8zHfjVvSe2x6JotlYJxPfdoUL2hVXuXPBW3u3Zg ORfcZpOqqyv8AbvjAuIRBb5QN9mk5YVOuA== X-Google-Smtp-Source: ABdhPJyyQ8uD8zvIuDK/YIm9SMMTllRrV7SpYQvZWlHzpnq0NNRvz5V9JcvIblsGw3cwtYWROEDb7Q== X-Received: by 2002:a05:6000:8d:: with SMTP id m13mr6430827wrx.109.1632834884355; Tue, 28 Sep 2021 06:14:44 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l21sm2617981wmh.31.2021.09.28.06.14.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:14:43 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 10/10] parse-options: change OPT_{SHORT,UNSET} to an enum Date: Tue, 28 Sep 2021 15:14:31 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1339.g53eae12fb46 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the comparisons against OPT_SHORT and OPT_UNSET to an enum which keeps track of how a given option got parsed. The case of "0" was an implicit OPT_LONG, so let's add an explicit label for it. Due to the xor in 0f1930c5875 (parse-options: allow positivation of options starting, with no-, 2012-02-25) the code already relied on this being set back to 0. To avoid refactoring the logic involved in that let's just start the enum at "0" instead of the usual "1<<0" (1), but BUG() out if we don't have one of our expected flags. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/parse-options.c b/parse-options.c index 57e95846e83..1eb3b51f753 100644 --- a/parse-options.c +++ b/parse-options.c @@ -8,8 +8,11 @@ static int disallow_abbreviated_options; -#define OPT_SHORT 1 -#define OPT_UNSET 2 +enum opt_parsed { + OPT_LONG = 0, + OPT_SHORT = 1<<0, + OPT_UNSET = 1<<1, +}; static int optbug(const struct option *opt, const char *reason) { @@ -22,7 +25,7 @@ static int optbug(const struct option *opt, const char *reason) return error("BUG: switch '%c' %s", opt->short_name, reason); } -static const char *optname(const struct option *opt, int flags) +static const char *optname(const struct option *opt, enum opt_parsed flags) { static struct strbuf sb = STRBUF_INIT; @@ -31,15 +34,17 @@ static const char *optname(const struct option *opt, int flags) strbuf_addf(&sb, "switch `%c'", opt->short_name); else if (flags & OPT_UNSET) strbuf_addf(&sb, "option `no-%s'", opt->long_name); - else + else if (flags == OPT_LONG) strbuf_addf(&sb, "option `%s'", opt->long_name); + else + BUG("optname() got unknown flags %d", flags); return sb.buf; } static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, const struct option *opt, - int flags, const char **arg) + enum opt_parsed flags, const char **arg) { if (p->opt) { *arg = p->opt; @@ -65,7 +70,7 @@ static void fix_filename(const char *prefix, const char **file) static enum parse_opt_result opt_command_mode_error( const struct option *opt, const struct option *all_opts, - int flags) + enum opt_parsed flags) { const struct option *that; struct strbuf that_name = STRBUF_INIT; @@ -97,7 +102,7 @@ static enum parse_opt_result opt_command_mode_error( static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, const struct option *opt, const struct option *all_opts, - int flags) + enum opt_parsed flags) { const char *s, *arg; const int unset = flags & OPT_UNSET; @@ -318,11 +323,11 @@ static enum parse_opt_result parse_long_opt( const struct option *all_opts = options; const char *arg_end = strchrnul(arg, '='); const struct option *abbrev_option = NULL, *ambiguous_option = NULL; - int abbrev_flags = 0, ambiguous_flags = 0; + enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG; for (; options->type != OPTION_END; options++) { const char *rest, *long_name = options->long_name; - int flags = 0, opt_flags = 0; + enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG; if (!long_name) continue;