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

lvmcmdline: use strcut as const

Constity members in cmdline_context, would be nice, to replace
this static struct with couple function calls.

Also replace some 'while' loops with for loops, so code
is more readable.
This commit is contained in:
Zdenek Kabelac 2024-05-12 12:04:54 +02:00
parent f854e3a722
commit 82617852a4
3 changed files with 17 additions and 21 deletions

View File

@ -50,9 +50,9 @@ static char *_list_cmds(const char *text, int state)
len = strlen(text); len = strlen(text);
} }
while (i < _cmdline->num_command_names) for (;i < _cmdline->num_command_names;++i)
if (!strncmp(text, _cmdline->command_names[i++].name, len)) if (!strncmp(text, _cmdline->command_names[i].name, len))
return strdup(_cmdline->command_names[i - 1].name); return strdup(_cmdline->command_names[i].name);
return NULL; return NULL;
} }
@ -62,8 +62,8 @@ static char *_list_args(const char *text, int state)
{ {
static int match_no = 0; static int match_no = 0;
static size_t len = 0; static size_t len = 0;
static struct command_name *cname; static const struct command_name *cname;
static struct command_name_args *cna; static const struct command_name_args *cna;
/* Initialise if this is a new completion attempt */ /* Initialise if this is a new completion attempt */
if (!state) { if (!state) {
@ -102,27 +102,23 @@ static char *_list_args(const char *text, int state)
/* Short form arguments */ /* Short form arguments */
if (len < 3) { if (len < 3) {
while (match_no < cna->num_args) { for (;match_no < cna->num_args; ++match_no) {
char s[3]; char s[3];
char c; char c = (_cmdline->opt_names + cna->valid_args[match_no])->short_opt;
if (!(c = (_cmdline->opt_names + if (c) {
cna->valid_args[match_no++])->short_opt))
continue;
sprintf(s, "-%c", c); sprintf(s, "-%c", c);
if (!strncmp(text, s, len)) if (!strncmp(text, s, len))
return strdup(s); return strdup(s);
} }
} }
}
/* Long form arguments */ /* Long form arguments */
if (match_no < cna->num_args) if (match_no < cna->num_args)
match_no = cna->num_args; match_no = cna->num_args;
while (match_no - cna->num_args < cna->num_args) { for (;match_no - cna->num_args < cna->num_args; ++match_no) {
const char *l; const char *l = (_cmdline->opt_names + cna->valid_args[match_no - cna->num_args])->long_opt;
l = (_cmdline->opt_names +
cna->valid_args[match_no++ - cna->num_args])->long_opt;
if (*(l + 2) && !strncmp(text, l, len)) if (*(l + 2) && !strncmp(text, l, len))
return strdup(l); return strdup(l);
} }

View File

@ -22,8 +22,8 @@ struct cmdline_context {
const struct opt_name *opt_names; const struct opt_name *opt_names;
struct command *commands; struct command *commands;
int num_commands; int num_commands;
struct command_name *command_names; const struct command_name *command_names;
struct command_name_args *command_names_args; const struct command_name_args *command_names_args;
int num_command_names; int num_command_names;
}; };

View File

@ -2692,7 +2692,7 @@ static void _display_help(void)
log_error(" "); log_error(" ");
for (i = 0; i < _cmdline.num_command_names; i++) { for (i = 0; i < _cmdline.num_command_names; i++) {
struct command_name *cname = _cmdline.command_names + i; const struct command_name *cname = _cmdline.command_names + i;
log_error("%-16.16s%s", cname->name, cname->desc); log_error("%-16.16s%s", cname->name, cname->desc);
} }