From 82617852a4d3c89b09124eddedcc2c1859b9d50e Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Sun, 12 May 2024 12:04:54 +0200 Subject: [PATCH] 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. --- tools/lvm.c | 32 ++++++++++++++------------------ tools/lvm2cmdline.h | 4 ++-- tools/lvmcmdline.c | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/tools/lvm.c b/tools/lvm.c index 2d7f1a516..116b707b2 100644 --- a/tools/lvm.c +++ b/tools/lvm.c @@ -50,9 +50,9 @@ static char *_list_cmds(const char *text, int state) len = strlen(text); } - while (i < _cmdline->num_command_names) - if (!strncmp(text, _cmdline->command_names[i++].name, len)) - return strdup(_cmdline->command_names[i - 1].name); + for (;i < _cmdline->num_command_names;++i) + if (!strncmp(text, _cmdline->command_names[i].name, len)) + return strdup(_cmdline->command_names[i].name); return NULL; } @@ -62,8 +62,8 @@ static char *_list_args(const char *text, int state) { static int match_no = 0; static size_t len = 0; - static struct command_name *cname; - static struct command_name_args *cna; + static const struct command_name *cname; + static const struct command_name_args *cna; /* Initialise if this is a new completion attempt */ if (!state) { @@ -102,16 +102,14 @@ static char *_list_args(const char *text, int state) /* Short form arguments */ if (len < 3) { - while (match_no < cna->num_args) { + for (;match_no < cna->num_args; ++match_no) { char s[3]; - char c; - if (!(c = (_cmdline->opt_names + - cna->valid_args[match_no++])->short_opt)) - continue; - - sprintf(s, "-%c", c); - if (!strncmp(text, s, len)) - return strdup(s); + char c = (_cmdline->opt_names + cna->valid_args[match_no])->short_opt; + if (c) { + sprintf(s, "-%c", c); + if (!strncmp(text, s, len)) + return strdup(s); + } } } @@ -119,10 +117,8 @@ static char *_list_args(const char *text, int state) if (match_no < cna->num_args) match_no = cna->num_args; - while (match_no - cna->num_args < cna->num_args) { - const char *l; - l = (_cmdline->opt_names + - cna->valid_args[match_no++ - cna->num_args])->long_opt; + for (;match_no - cna->num_args < cna->num_args; ++match_no) { + const char *l = (_cmdline->opt_names + cna->valid_args[match_no - cna->num_args])->long_opt; if (*(l + 2) && !strncmp(text, l, len)) return strdup(l); } diff --git a/tools/lvm2cmdline.h b/tools/lvm2cmdline.h index 15df29ff6..d89f8845b 100644 --- a/tools/lvm2cmdline.h +++ b/tools/lvm2cmdline.h @@ -22,8 +22,8 @@ struct cmdline_context { const struct opt_name *opt_names; struct command *commands; int num_commands; - struct command_name *command_names; - struct command_name_args *command_names_args; + const struct command_name *command_names; + const struct command_name_args *command_names_args; int num_command_names; }; diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c index da69de6e7..40d9806c3 100644 --- a/tools/lvmcmdline.c +++ b/tools/lvmcmdline.c @@ -2692,7 +2692,7 @@ static void _display_help(void) log_error(" "); 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); }