From 49bafb48f3833ae62714e31900617e9ccd6e22f7 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Wed, 3 May 2023 00:41:13 +0900 Subject: [PATCH] util: Delete argconfig value length to calcurate instead Signed-off-by: Tokunori Ikegami --- nvme.c | 8 ++++---- util/argconfig.c | 23 ++++++++++++++++++++++- util/argconfig.h | 39 +++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/nvme.c b/nvme.c index 7bb40958..ae3749ee 100644 --- a/nvme.c +++ b/nvme.c @@ -5301,10 +5301,10 @@ static int sanitize_cmd(int argc, char **argv, struct command *cmd, struct plugi }; OPT_VALS(sanact) = { - VAL_BYTE("exit-failure", 1, NVME_SANITIZE_SANACT_EXIT_FAILURE), - VAL_BYTE("start-block-erase", 7, NVME_SANITIZE_SANACT_START_BLOCK_ERASE), - VAL_BYTE("start-overwrite", 7, NVME_SANITIZE_SANACT_START_OVERWRITE), - VAL_BYTE("start-crypto-erase", 7, NVME_SANITIZE_SANACT_START_CRYPTO_ERASE), + VAL_BYTE("exit-failure", NVME_SANITIZE_SANACT_EXIT_FAILURE), + VAL_BYTE("start-block-erase", NVME_SANITIZE_SANACT_START_BLOCK_ERASE), + VAL_BYTE("start-overwrite", NVME_SANITIZE_SANACT_START_OVERWRITE), + VAL_BYTE("start-crypto-erase", NVME_SANITIZE_SANACT_START_CRYPTO_ERASE), VAL_END() }; diff --git a/util/argconfig.c b/util/argconfig.c index 1c35ba4d..7b213cc6 100644 --- a/util/argconfig.c +++ b/util/argconfig.c @@ -241,6 +241,25 @@ static int argconfig_parse_type(struct argconfig_commandline_options *s, struct return ret; } +static int argconfig_get_val_len(struct argconfig_opt_val *opt_val, const char *str) +{ + struct argconfig_opt_val *v; + int len; + int match; + + for (len = 1; len <= strlen(str); len++) { + match = 0; + for (v = opt_val; v && v->str; v++) { + if (!strncasecmp(str, v->str, len)) + match++; + } + if (match == 1) + break; + } + + return len; +} + static int argconfig_parse_val(struct argconfig_commandline_options *s, struct option *option, int index) { @@ -248,9 +267,11 @@ static int argconfig_parse_val(struct argconfig_commandline_options *s, struct o void *val = s->default_value; int len = strlen(optarg); struct argconfig_opt_val *v; + int val_len; for (v = s->opt_val; v && v->str; v++) { - if (strncasecmp(str, v->str, len > v->len ? len : v->len)) + val_len = argconfig_get_val_len(s->opt_val, v->str); + if (strncasecmp(str, v->str, len > val_len ? len : val_len)) continue; switch (v->type) { case CFG_FLAG: diff --git a/util/argconfig.h b/util/argconfig.h index fe34f54d..8ce28b7c 100644 --- a/util/argconfig.h +++ b/util/argconfig.h @@ -106,34 +106,34 @@ enum argconfig_types { #define VAL_END() { NULL } #define VAL_FLAG(s, l, v) \ - {s, l, CFG_FLAG, .val.flag = v} + {s, CFG_FLAG, .val.flag = v} -#define VAL_LONG_SUFFIX(s, l, v) \ - {s, l, CFG_LONG_SUFFIX, .val.long_suffix = v} +#define VAL_LONG_SUFFIX(s, v) \ + {s, CFG_LONG_SUFFIX, .val.long_suffix = v} -#define VAL_UINT(s, l, v) \ - {s, l, CFG_POSITIVE, v} +#define VAL_UINT(s, v) \ + {s, CFG_POSITIVE, v} -#define VAL_INT(s, l, v) \ - {s, l, CFG_INT, .val.int_val = v} +#define VAL_INT(s, v) \ + {s, CFG_INT, .val.int_val = v} -#define VAL_LONG(s, l, v) \ - {s, l, CFG_LONG, .val.long_val = v} +#define VAL_LONG(s, v) \ + {s, CFG_LONG, .val.long_val = v} -#define VAL_DOUBLE(s, l, v) \ - {s, l, CFG_DOUBLE, .val.double_val = v} +#define VAL_DOUBLE(s, v) \ + {s, CFG_DOUBLE, .val.double_val = v} -#define VAL_BYTE(s, l, v) \ - {s, l, CFG_BYTE, .val.byte = v} +#define VAL_BYTE(s, v) \ + {s, CFG_BYTE, .val.byte = v} -#define VAL_SHRT(s, l, v) \ - {s, l, CFG_SHORT, .val.short_val = v} +#define VAL_SHRT(s, v) \ + {s, CFG_SHORT, .val.short_val = v} -#define VAL_INCR(s, l, v) \ - {s, l, CFG_INCREMENT, .val.increment = v} +#define VAL_INCR(s, v) \ + {s, CFG_INCREMENT, .val.increment = v} -#define VAL_STRING(s, l, m, v) \ - {s, l, CFG_STRING, .val.string = v} +#define VAL_STRING(s, m, v) \ + {s, CFG_STRING, .val.string = v} union argconfig_val { char *string; @@ -152,7 +152,6 @@ union argconfig_val { struct argconfig_opt_val { const char *str; - const int len; enum argconfig_types type; union argconfig_val val; }; -- 2.49.0