1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-04-01 18:50:41 +03:00

commands: combine duplicate arrays for opt and val

command.c and lvmcmdline.c each had a full array defining
all options and values.  This duplication was not removed
when the command.c code was merged into the run time.
This commit is contained in:
David Teigland 2017-03-07 11:47:44 -06:00
parent f48e6b2690
commit 690f604733
7 changed files with 59 additions and 88 deletions

View File

@ -167,31 +167,6 @@ enum {
#include "command.h" /* defines struct command */
#include "command-count.h" /* defines COMMAND_COUNT */
/* see opt_names[] below, also see arg_props[] in tools.h and args.h */
struct opt_name {
const char *name; /* "foo_ARG" */
int opt_enum; /* foo_ARG */
const char short_opt; /* -f */
char _padding[7];
const char *long_opt; /* --foo */
int val_enum; /* xyz_VAL when --foo takes a val like "--foo xyz" */
uint32_t flags;
uint32_t prio;
const char *desc;
};
/* see val_names[] below, also see val_props[] in tools.h and vals.h */
struct val_name {
const char *enum_name; /* "foo_VAL" */
int val_enum; /* foo_VAL */
int (*fn) (struct cmd_context *cmd, struct arg_values *av); /* foo_arg() */
const char *name; /* FooVal */
const char *usage;
};
/* see lvp_names[] below, also see lv_props[] in tools.h and lv_props.h */
struct lvp_name {

View File

@ -211,6 +211,30 @@ struct command {
int pos_count; /* temp counter used by create-command */
};
/* see global opt_names[] */
struct opt_name {
const char *name; /* "foo_ARG" */
int opt_enum; /* foo_ARG */
const char short_opt; /* -f */
char _padding[7];
const char *long_opt; /* --foo */
int val_enum; /* xyz_VAL when --foo takes a val like "--foo xyz" */
uint32_t flags;
uint32_t prio;
const char *desc;
};
/* see global val_names[] */
struct val_name {
const char *enum_name; /* "foo_VAL" */
int val_enum; /* foo_VAL */
int (*fn) (struct cmd_context *cmd, struct arg_values *av); /* foo_arg() */
const char *name; /* FooVal */
const char *usage;
};
int define_commands(char *run_name);
int command_id_to_enum(const char *str);
void print_usage(struct command *cmd, int longhelp, int desc_first);

View File

@ -97,8 +97,8 @@ static char *_list_args(const char *text, int state)
while (match_no < cname->num_args) {
char s[3];
char c;
if (!(c = (_cmdline->arg_props +
cname->valid_args[match_no++])->short_arg))
if (!(c = (_cmdline->opt_names +
cname->valid_args[match_no++])->short_opt))
continue;
sprintf(s, "-%c", c);
@ -113,8 +113,8 @@ static char *_list_args(const char *text, int state)
while (match_no - cname->num_args < cname->num_args) {
const char *l;
l = (_cmdline->arg_props +
cname->valid_args[match_no++ - cname->num_args])->long_arg;
l = (_cmdline->opt_names +
cname->valid_args[match_no++ - cname->num_args])->long_opt;
if (*(l + 2) && !strncmp(text, l, len))
return strdup(l);
}

View File

@ -19,7 +19,7 @@
struct cmd_context;
struct cmdline_context {
struct arg_props *arg_props;
struct opt_name *opt_names;
struct command *commands;
int num_commands;
struct command_name *command_names;

View File

@ -53,20 +53,12 @@ extern char *optarg;
/*
* Table of valid --option values.
*/
static struct val_props _val_props[VAL_COUNT + 1] = {
#define val(a, b, c, d) {a, b, c, d},
#include "vals.h"
#undef val
};
extern struct val_name val_names[VAL_COUNT + 1];
/*
* Table of valid --option's
*/
static struct arg_props _arg_props[ARG_COUNT + 1] = {
#define arg(a, b, c, d, e, f, g) {a, b, "", "--" c, d, e, f, g},
#include "args.h"
#undef arg
};
extern struct opt_name opt_names[ARG_COUNT + 1];
/*
* Table of LV properties
@ -277,7 +269,7 @@ unsigned grouped_arg_is_set(const struct arg_values *av, int a)
const char *arg_long_option_name(int a)
{
return _cmdline.arg_props[a].long_arg;
return _cmdline.opt_names[a].long_opt;
}
const char *arg_value(const struct cmd_context *cmd, int a)
@ -316,7 +308,7 @@ int32_t first_grouped_arg_int_value(const struct cmd_context *cmd, int a, const
int32_t arg_int_value(const struct cmd_context *cmd, int a, const int32_t def)
{
return (_cmdline.arg_props[a].flags & ARG_GROUPABLE) ?
return (_cmdline.opt_names[a].flags & ARG_GROUPABLE) ?
first_grouped_arg_int_value(cmd, a, def) : (arg_is_set(cmd, a) ? cmd->opt_arg_values[a].i_value : def);
}
@ -1088,7 +1080,7 @@ static void _set_valid_args_for_command_name(int ci)
for (i = 0; i < ARG_COUNT; i++) {
if (all_args[i]) {
opt_enum = _cmdline.arg_props[i].arg_enum;
opt_enum = _cmdline.opt_names[i].opt_enum;
command_names[ci].valid_args[num_args] = opt_enum;
num_args++;
@ -1789,12 +1781,12 @@ static void _usage_all(void)
* can't have more than 'a' long arguments.
*/
static void _add_getopt_arg(int arg_enum, char **optstrp, struct option **longoptsp)
static void _add_getopt_arg(int opt_enum, char **optstrp, struct option **longoptsp)
{
struct arg_props *a = _cmdline.arg_props + arg_enum;
struct opt_name *a = _cmdline.opt_names + opt_enum;
if (a->short_arg) {
*(*optstrp)++ = a->short_arg;
if (a->short_opt) {
*(*optstrp)++ = a->short_opt;
if (a->val_enum)
*(*optstrp)++ = ':';
@ -1802,8 +1794,8 @@ static void _add_getopt_arg(int arg_enum, char **optstrp, struct option **longop
#ifdef HAVE_GETOPTLONG
/* long_arg is "--foo", so +2 is the offset of the name after "--" */
if (*(a->long_arg + 2)) {
(*longoptsp)->name = a->long_arg + 2;
if (*(a->long_opt + 2)) {
(*longoptsp)->name = a->long_opt + 2;
(*longoptsp)->has_arg = a->val_enum ? 1 : 0;
(*longoptsp)->flag = NULL;
@ -1820,10 +1812,10 @@ static void _add_getopt_arg(int arg_enum, char **optstrp, struct option **longop
* (11 is the enum value for --cachepool, so 11+128)
*/
if (a->short_arg)
(*longoptsp)->val = a->short_arg;
if (a->short_opt)
(*longoptsp)->val = a->short_opt;
else
(*longoptsp)->val = arg_enum + 128;
(*longoptsp)->val = opt_enum + 128;
(*longoptsp)++;
}
#endif
@ -1856,14 +1848,14 @@ static int _find_arg(const char *cmd_name, int goval)
for (i = 0; i < cname->num_args; i++) {
arg_enum = cname->valid_args[i];
/* assert arg_enum == _cmdline.arg_props[arg_enum].arg_enum */
/* assert arg_enum == _cmdline.opt_names[arg_enum].arg_enum */
/* the value returned by getopt matches the ascii value of single letter option */
if (_cmdline.arg_props[arg_enum].short_arg && (goval == _cmdline.arg_props[arg_enum].short_arg))
if (_cmdline.opt_names[arg_enum].short_opt && (goval == _cmdline.opt_names[arg_enum].short_opt))
return arg_enum;
/* the value returned by getopt matches the enum value plus 128 */
if (!_cmdline.arg_props[arg_enum].short_arg && (goval == (arg_enum + 128)))
if (!_cmdline.opt_names[arg_enum].short_opt && (goval == (arg_enum + 128)))
return arg_enum;
}
@ -1874,7 +1866,7 @@ static int _process_command_line(struct cmd_context *cmd, int *argc, char ***arg
{
char str[((ARG_COUNT + 1) * 2) + 1], *ptr = str;
struct option opts[ARG_COUNT + 1], *o = opts;
struct arg_props *a;
struct opt_name *a;
struct arg_values *av;
struct arg_value_group_list *current_group = NULL;
struct command_name *cname;
@ -1893,7 +1885,7 @@ static int _process_command_line(struct cmd_context *cmd, int *argc, char ***arg
/*
* create the short-form character array (str) and the long-form option
* array (opts) to pass to the getopt_long() function. IOW we generate
* the arguments to pass to getopt_long() from the args.h/arg_props data.
* the arguments to pass to getopt_long() from the opt_names data.
*/
for (i = 0; i < cname->num_args; i++)
_add_getopt_arg(cname->valid_args[i], &ptr, &o);
@ -1917,7 +1909,7 @@ static int _process_command_line(struct cmd_context *cmd, int *argc, char ***arg
return 0;
}
a = _cmdline.arg_props + arg_enum;
a = _cmdline.opt_names + arg_enum;
av = &cmd->opt_arg_values[arg_enum];
@ -1947,10 +1939,10 @@ static int _process_command_line(struct cmd_context *cmd, int *argc, char ***arg
if (av->count && !(a->flags & ARG_COUNTABLE)) {
log_error("Option%s%c%s%s may not be repeated.",
a->short_arg ? " -" : "",
a->short_arg ? : ' ',
(a->short_arg && a->long_arg) ?
"/" : "", a->long_arg ? : "");
a->short_opt ? " -" : "",
a->short_opt ? : ' ',
(a->short_opt && a->long_opt) ?
"/" : "", a->long_opt ? : "");
return 0;
}
@ -1962,8 +1954,8 @@ static int _process_command_line(struct cmd_context *cmd, int *argc, char ***arg
av->value = optarg;
if (!_val_props[a->val_enum].fn(cmd, av)) {
log_error("Invalid argument for %s: %s", a->long_arg, optarg);
if (!val_names[a->val_enum].fn(cmd, av)) {
log_error("Invalid argument for %s: %s", a->long_opt, optarg);
return 0;
}
}
@ -1997,12 +1989,12 @@ static int _merge_synonym(struct cmd_context *cmd, int oldarg, int newarg)
if (arg_is_set(cmd, oldarg) && arg_is_set(cmd, newarg)) {
log_error("%s and %s are synonyms. Please only supply one.",
_cmdline.arg_props[oldarg].long_arg, _cmdline.arg_props[newarg].long_arg);
_cmdline.opt_names[oldarg].long_opt, _cmdline.opt_names[newarg].long_opt);
return 0;
}
/* Not groupable? */
if (!(_cmdline.arg_props[oldarg].flags & ARG_GROUPABLE)) {
if (!(_cmdline.opt_names[oldarg].flags & ARG_GROUPABLE)) {
if (arg_is_set(cmd, oldarg))
_copy_arg_values(cmd->opt_arg_values, oldarg, newarg);
return 1;
@ -3085,7 +3077,7 @@ struct cmd_context *init_lvm(unsigned set_connections, unsigned set_filters)
return_NULL;
}
_cmdline.arg_props = &_arg_props[0];
_cmdline.opt_names = &opt_names[0];
if (stored_errno()) {
destroy_toolcontext(cmd);

View File

@ -108,32 +108,12 @@ struct arg_values {
/* void *ptr; // Currently not used. */
};
/* a global table of possible --option's */
struct arg_props {
int arg_enum; /* foo_ARG from args.h */
const char short_arg;
char _padding[7];
const char *long_arg;
int val_enum; /* foo_VAL from vals.h */
uint32_t flags;
uint32_t prio;
const char *desc;
};
struct arg_value_group_list {
struct dm_list list;
struct arg_values arg_values[0];
uint32_t prio;
};
/* a global table of possible --option values */
struct val_props {
int val_enum; /* foo_VAL from vals.h */
int (*fn) (struct cmd_context *cmd, struct arg_values *av);
const char *name;
const char *usage;
};
/* a global table of possible LV properties */
struct lv_props {
int lvp_enum; /* is_foo_LVP from lv_props.h */

View File

@ -63,7 +63,7 @@
* then also be added to the usage string for the val type here.
* It would be nice if the accepted values could be defined in a
* more consistent way, and perhaps in a single place, perhaps in
* struct val_props.
* struct val_names.
*
* The usage text for an option is not always the full
* set of words accepted for an option, but may be a