From 93918a19903cb4d0fd87f79102b7a31885e1b620 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Sat, 18 May 2024 01:38:18 +0200 Subject: [PATCH] cleanup: use ARG_LONG_OPT Just like with _VAL strings, also _ARG strings do not need to be present - as we can easily check for LONG opt version just by adding attribute. With attribute ARG_LONG_OPT string arg name[] becomes unused and can be safely removed. Also within _find_command_id_function() we do not need to handle 'command_enum == CMD_NONE' as separate case and just use single loop. --- tools/args.h | 6 +++--- tools/command.c | 6 +++--- tools/command.h | 5 ++--- tools/lvmcmdline.c | 10 ++++------ tools/man-generator.c | 1 + tools/tools.h | 1 + 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/tools/args.h b/tools/args.h index ed0fb9620..e456a572b 100644 --- a/tools/args.h +++ b/tools/args.h @@ -137,7 +137,7 @@ arg(bootloaderareasize_ARG, '\0', "bootloaderareasize", sizemb_VAL, 0, 0, "To see the bootloader area start and size of\n" "an existing PV use pvs -o +pv_ba_start,pv_ba_size.\n") -arg(cache_long_ARG, '\0', "cache", 0, 0, 0, +arg(cache_long_ARG, '\0', "cache", 0, ARG_LONG_OPT, 0, "#pvscan\n" "Scan one or more devices and record that they are online.\n" "#vgscan\n" @@ -301,7 +301,7 @@ arg(errorwhenfull_ARG, '\0', "errorwhenfull", bool_VAL, 0, 0, "(Also see dm-thin-pool kernel module option no_space_timeout.)\n" "See \\fBlvmthin\\fP(7) for more information.\n") -arg(force_long_ARG, '\0', "force", 0, ARG_COUNTABLE, 0, +arg(force_long_ARG, '\0', "force", 0, ARG_COUNTABLE | ARG_LONG_OPT, 0, "Force metadata restore even with thin pool LVs.\n" "Use with extreme caution. Most changes to thin metadata\n" "cannot be reverted.\n" @@ -837,7 +837,7 @@ arg(showunsupported_ARG, '\0', "showunsupported", 0, 0, 0, arg(startpoll_ARG, '\0', "startpoll", 0, 0, 0, "Start polling an LV to continue processing a conversion.\n") -arg(stripes_long_ARG, '\0', "stripes", number_VAL, 0, 0, +arg(stripes_long_ARG, '\0', "stripes", number_VAL, ARG_LONG_OPT, 0, "Specifies the number of stripes in a striped LV. This is the number of\n" "PVs (devices) that a striped LV is spread across. Data that\n" "appears sequential in the LV is spread across multiple devices in units of\n" diff --git a/tools/command.c b/tools/command.c index 55a774698..8fe9326bf 100644 --- a/tools/command.c +++ b/tools/command.c @@ -47,7 +47,7 @@ static const struct val_name val_names[VAL_COUNT + 1] = { /* create table of option names, e.g. --foo, and corresponding enum from args.h */ static const struct opt_name opt_names[ARG_COUNT + 1] = { -#define arg(a, b, c, d, e, f, g) { # a, b, a, "--" c, d, e, f, g }, +#define arg(a, b, c, d, e, f, g) { g, "--" c, b, a, d, e, f }, #include "args.h" #undef arg }; @@ -256,7 +256,7 @@ static int _opt_str_to_num(struct command *cmd, const char *str) * check left & right side for possible match */ for (i = middle;;) { - if ((!p && !strstr(opt_names_alpha[i]->name, "_long_ARG")) || + if ((!p && !(opt_names_alpha[i]->flags & ARG_LONG_OPT)) || (p && !opt_names_alpha[i]->short_opt)) return opt_names_alpha[i]->opt_enum; /* Found */ /* Check if there is something on the 'left-side' */ @@ -268,7 +268,7 @@ static int _opt_str_to_num(struct command *cmd, const char *str) for (i = middle + 1; i <= last; ++i) { if (strcmp(opt_names_alpha[i]->long_opt, long_name)) break; - if ((!p && !strstr(opt_names_alpha[i]->name, "_long_ARG")) || + if ((!p && !(opt_names_alpha[i]->flags & ARG_LONG_OPT)) || (p && !opt_names_alpha[i]->short_opt)) return opt_names_alpha[i]->opt_enum; /* Found */ } diff --git a/tools/command.h b/tools/command.h index 4d3ab8274..4159bf53c 100644 --- a/tools/command.h +++ b/tools/command.h @@ -226,14 +226,13 @@ struct command { /* see global opt_names[] */ struct opt_name { - const char name[27]; /* "foo_ARG" */ + const char *desc; + const char long_opt[27];/* --foo */ const char short_opt; /* -f */ uint16_t opt_enum; /* foo_ARG */ - const char long_opt[28]; /* --foo */ uint16_t val_enum; /* xyz_VAL when --foo takes a val like "--foo xyz" */ uint16_t flags; uint16_t prio; - const char *desc; }; /* see global val_names[] */ diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c index efd473cfe..aa15ee805 100644 --- a/tools/lvmcmdline.c +++ b/tools/lvmcmdline.c @@ -71,6 +71,7 @@ static struct cmdline_context _cmdline; * For now, any command id not included here uses the old command fn. */ static const struct command_function _command_functions[CMD_COUNT] = { + { CMD_NONE, NULL }, { lvmconfig_general_CMD, lvmconfig }, { lvchange_properties_CMD, lvchange_properties_cmd }, { lvchange_resync_CMD, lvchange_resync_cmd }, @@ -1303,15 +1304,12 @@ static void _set_valid_args_for_command_name(int ci) static command_fn _find_command_id_function(int command_enum) { - int i; + unsigned i; - if (!command_enum) - return NULL; - - for (i = 0; i < CMD_COUNT; i++) { + for (i = 0; i < CMD_COUNT; i++) if (_command_functions[i].command_enum == command_enum) return _command_functions[i].fn; - } + return NULL; } diff --git a/tools/man-generator.c b/tools/man-generator.c index a0758c228..3b07fd03e 100644 --- a/tools/man-generator.c +++ b/tools/man-generator.c @@ -60,6 +60,7 @@ static void *dm_pool_alloc(void *p, size_t size) #define ARG_COUNTABLE 0x00000001 #define ARG_GROUPABLE 0x00000002 #define ARG_NONINTERACTIVE 0x00000004 +#define ARG_LONG_OPT 0x00000008 struct arg_values; /* needed to include vals.h */ diff --git a/tools/tools.h b/tools/tools.h index 58708c695..fbf5af93b 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -61,6 +61,7 @@ #define ARG_COUNTABLE 0x00000001 /* E.g. -vvvv */ #define ARG_GROUPABLE 0x00000002 /* E.g. --addtag */ #define ARG_NONINTERACTIVE 0x00000004 /* only for use in noninteractive mode */ +#define ARG_LONG_OPT 0x00000008 /* arg has long format option */ struct arg_values { char *value;