vsh: Rework vshReadlineCommandGenerator()

Firstly, move variable declarations into the inner most block
they are used. Secondly, use for() loop instead of while so that
we don't have to advance loop counter explicitly on 'continue'.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Michal Privoznik 2021-01-26 17:22:00 +01:00
parent 9b005b1967
commit 72636ed86d

View File

@ -2566,37 +2566,30 @@ vshTreePrint(vshControl *ctl, vshTreeLookup lookup, void *opaque,
static char ** static char **
vshReadlineCommandGenerator(const char *text G_GNUC_UNUSED) vshReadlineCommandGenerator(const char *text G_GNUC_UNUSED)
{ {
size_t grp_list_index = 0, cmd_list_index = 0; size_t grp_list_index = 0;
const char *name;
const vshCmdGrp *grp; const vshCmdGrp *grp;
const vshCmdDef *cmds;
size_t ret_size = 0; size_t ret_size = 0;
g_auto(GStrv) ret = NULL; g_auto(GStrv) ret = NULL;
grp = cmdGroups; grp = cmdGroups;
/* Return the next name which partially matches from the for (grp_list_index = 0; grp[grp_list_index].name; grp_list_index++) {
* command list. const vshCmdDef *cmds = grp[grp_list_index].commands;
*/ size_t cmd_list_index;
while (grp[grp_list_index].name) {
cmds = grp[grp_list_index].commands;
if (cmds[cmd_list_index].name) { for (cmd_list_index = 0; cmds[cmd_list_index].name; cmd_list_index++) {
while ((name = cmds[cmd_list_index].name)) { const char *name = cmds[cmd_list_index].name;
if (cmds[cmd_list_index++].flags & VSH_CMD_FLAG_ALIAS)
continue;
if (VIR_REALLOC_N(ret, ret_size + 2) < 0) if (cmds[cmd_list_index].flags & VSH_CMD_FLAG_ALIAS)
return NULL; continue;
ret[ret_size] = g_strdup(name); if (VIR_REALLOC_N(ret, ret_size + 2) < 0)
ret_size++; return NULL;
/* Terminate the string list properly. */
ret[ret_size] = NULL; ret[ret_size] = g_strdup(name);
} ret_size++;
} else { /* Terminate the string list properly. */
cmd_list_index = 0; ret[ret_size] = NULL;
grp_list_index++;
} }
} }