1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-03-10 16:58:47 +03:00

command: use command_id_to_enum

Instead of storing command_id as string, direcly
translate string to enum index and use 'command_enum()'
to get string when needed for printing.

This way we can easily detect error in the structure
while parsing it - and we can later avoid separate
'translation' loop.
This commit is contained in:
Zdenek Kabelac 2024-05-12 16:00:48 +02:00
parent 5840f90e82
commit bebbb1e66a
9 changed files with 29 additions and 31 deletions

View File

@ -284,10 +284,10 @@ err:
/* "foo" string to foo_CMD int */
int command_id_to_enum(const char *str)
unsigned command_id_to_enum(const char *str)
{
int i;
int first = 1, last = CMD_COUNT - 1, middle;
unsigned first = 1, last = CMD_COUNT - 1, middle;
while (first <= last) {
middle = first + (last - first) / 2;
@ -303,6 +303,11 @@ int command_id_to_enum(const char *str)
return CMD_NONE;
}
const char *command_enum(unsigned command_enum)
{
return cmd_names[command_enum].name;
}
/* "lv_is_prop" to is_prop_LVP */
static int _lvp_name_to_enum(struct command *cmd, const char *str)
@ -1397,14 +1402,9 @@ int define_commands(struct cmd_context *cmdtool, const char *run_name)
}
if (cmd && _is_id_line(line_argv[0])) {
#ifdef MAN_PAGE_GENERATOR
free((void*)cmd->command_id);
#endif
cmd->command_id = dm_pool_strdup(cmdtool->libmem, line_argv[1]);
if (!cmd->command_id) {
/* FIXME */
stack;
cmd->command_enum = command_id_to_enum(line_argv[1]);
if (cmd->command_enum == CMD_NONE) {
cmd->cmd_flags |= CMD_FLAG_PARSE_ERROR;
return 0;
}
continue;

View File

@ -185,7 +185,6 @@ struct cmd_rule {
struct command {
const char *name;
const char *desc; /* specific command description from command-lines.in */
const char *command_id; /* ID string in command-lines.in */
uint16_t command_enum; /* <command_id>_CMD */
uint16_t command_index; /* position in commands[] */
@ -273,7 +272,8 @@ struct lv_type {
int define_commands(struct cmd_context *cmdtool, const char *run_name);
int command_id_to_enum(const char *str);
unsigned command_id_to_enum(const char *str);
const char *command_enum(unsigned command_enum);
void print_usage(struct command *cmd, int longhelp, int desc_first);
void print_usage_common_cmd(const struct command_name *cname, struct command *cmd);
void print_usage_common_lvm(const struct command_name *cname, struct command *cmd);

View File

@ -1919,6 +1919,6 @@ int lvchange_persistent_cmd(struct cmd_context *cmd, int argc, char **argv)
int lvchange(struct cmd_context *cmd, int argc, char **argv)
{
log_error(INTERNAL_ERROR "Missing function for command definition %d:%s.",
cmd->command->command_index, cmd->command->command_id);
cmd->command->command_index, command_enum(cmd->command->command_enum));
return ECMD_FAILED;
}

View File

@ -6562,6 +6562,6 @@ int lvconvert_integrity_cmd(struct cmd_context *cmd, int argc, char **argv)
int lvconvert(struct cmd_context *cmd, int argc, char **argv)
{
log_error(INTERNAL_ERROR "Missing function for command definition %d:%s.",
cmd->command->command_index, cmd->command->command_id);
cmd->command->command_index, command_enum(cmd->command->command_enum));
return ECMD_FAILED;
}

View File

@ -1357,14 +1357,6 @@ int lvm_register_commands(struct cmd_context *cmd, const char *run_name)
for (i = 0; i < COMMAND_COUNT; i++) {
commands_idx[i] = &commands[i];
commands[i].command_index = i;
commands[i].command_enum = command_id_to_enum(commands[i].command_id);
if (!commands[i].command_enum) {
log_error(INTERNAL_ERROR "Failed to find command id %s.", commands[i].command_id);
_cmdline.commands = NULL;
_cmdline.num_commands = 0;
return 0;
}
/* new style */
commands[i].functions = _find_command_id_function(commands[i].command_enum);
@ -2001,9 +1993,9 @@ out:
}
log_debug("Recognised command %s (id %d / enum %d).",
commands[best_i].command_id, best_i, commands[best_i].command_enum);
command_enum(commands[best_i].command_enum), best_i, commands[best_i].command_enum);
log_command(cmd->cmd_line, commands[best_i].name, commands[best_i].command_id);
log_command(cmd->cmd_line, commands[best_i].name, command_enum(commands[best_i].command_enum));
return &commands[best_i];
}
@ -2052,7 +2044,7 @@ static int _usage(const char *name, int longhelp, int skip_notes)
log_very_verbose("Command definition index %d enum %d id %s",
_cmdline.commands[i].command_index,
_cmdline.commands[i].command_enum,
_cmdline.commands[i].command_id);
command_enum(_cmdline.commands[i].command_enum));
print_usage(&_cmdline.commands[i], 1, 1);
cmd = &_cmdline.commands[i];

View File

@ -418,7 +418,7 @@ retry:
int lvresize(struct cmd_context *cmd, int argc, char **argv)
{
log_error(INTERNAL_ERROR "Missing function for command definition %d:%s.",
cmd->command->command_index, cmd->command->command_id);
cmd->command->command_index, command_enum(cmd->command->command_enum));
return ECMD_FAILED;
}

View File

@ -1590,7 +1590,9 @@ static int _compare_cmds(struct command *cmd1, struct command *cmd2, int *all_re
/* compare opt_list_1 and opt_list_2 */
if (!_compare_opt_lists(opt_list_1, opt_count_1, opt_list_2, opt_count_2, NULL, NULL)) {
log_error("Repeated commands %s %s", cmd1->command_id, cmd2->command_id);
log_error("Repeated commands %s %s",
command_enum(cmd1->command_enum),
command_enum(cmd2->command_enum));
log_error("cmd1: %s", cmd1->desc);
log_error("cmd2: %s", cmd2->desc);
_print_opt_list("cmd1 options: ", opt_list_1, opt_count_1);
@ -1614,7 +1616,9 @@ static int _compare_cmds(struct command *cmd1, struct command *cmd2, int *all_re
opt_list_2[opt_count_2] = cmd2->optional_opt_args[i].opt;
if (!_compare_opt_lists(opt_list_1, opt_count_1, opt_list_2, opt_count_2+1, NULL, NULL)) {
log_error("Repeated commands %s %s", cmd1->command_id, cmd2->command_id);
log_error("Repeated commands %s %s",
command_enum(cmd1->command_enum),
command_enum(cmd2->command_enum));
log_error("cmd1: %s", cmd1->desc);
log_error("cmd2: %s", cmd2->desc);
log_error("Included cmd2 OO: %s", opt_names[cmd2->optional_opt_args[i].opt].long_opt);
@ -1650,7 +1654,9 @@ static int _compare_cmds(struct command *cmd1, struct command *cmd2, int *all_re
opt_list_2[opt_count_2] = cmd2->optional_opt_args[j].opt;
if (!_compare_opt_lists(opt_list_1, opt_count_1+1, opt_list_2, opt_count_2+1, cmd1_type_str, cmd2_type_str)) {
log_error("Repeated commands %s %s", cmd1->command_id, cmd2->command_id);
log_error("Repeated commands %s %s",
command_enum(cmd1->command_enum),
command_enum(cmd2->command_enum));
log_error("cmd1: %s", cmd1->desc);
log_error("cmd2: %s", cmd2->desc);
log_error("Included cmd1 OO: %s and cmd2 OO: %s",

View File

@ -1779,7 +1779,7 @@ int pvscan_cache_cmd(struct cmd_context *cmd, int argc, char **argv)
int pvscan(struct cmd_context *cmd, int argc, char **argv)
{
log_error(INTERNAL_ERROR "Missing function for command definition %d:%s.",
cmd->command->command_index, cmd->command->command_id);
cmd->command->command_index, command_enum(cmd->command->command_enum));
return ECMD_FAILED;
}

View File

@ -3006,7 +3006,7 @@ static int _check_lv_types(struct cmd_context *cmd, struct logical_volume *lv, i
if (!val_bit_is_set(cmd->command->required_pos_args[pos-1].def.val_bits, lv_VAL)) {
log_error(INTERNAL_ERROR "Command %d:%s arg position %d does not permit an LV (%llx)",
cmd->command->command_index, cmd->command->command_id,
cmd->command->command_index, command_enum(cmd->command->command_enum),
pos, (unsigned long long)cmd->command->required_pos_args[pos-1].def.val_bits);
return 0;
}