Skip to content

Commit

Permalink
hmp: introduce cmd_available
Browse files Browse the repository at this point in the history
Combine the RUN_STATE_PRECONFIG and cmd_can_preconfig checks
into a single function, to avoid repeating the same expression
(or its negation after applying DeMorgan's rule) over and
over again.

Reviewed-by: Igor Mammedov <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini committed Dec 10, 2020
1 parent ee55686 commit 4cd2927
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions monitor/hmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,19 @@ static bool cmd_can_preconfig(const HMPCommand *cmd)
return strchr(cmd->flags, 'p');
}

static bool cmd_available(const HMPCommand *cmd)
{
return !runstate_check(RUN_STATE_PRECONFIG) || cmd_can_preconfig(cmd);
}

static void help_cmd_dump_one(Monitor *mon,
const HMPCommand *cmd,
char **prefix_args,
int prefix_args_nb)
{
int i;

if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
if (!cmd_available(cmd)) {
return;
}

Expand Down Expand Up @@ -248,8 +253,7 @@ static void help_cmd_dump(Monitor *mon, const HMPCommand *cmds,
/* Find one entry to dump */
for (cmd = cmds; cmd->name != NULL; cmd++) {
if (hmp_compare_cmd(args[arg_index], cmd->name) &&
((!runstate_check(RUN_STATE_PRECONFIG) ||
cmd_can_preconfig(cmd)))) {
cmd_available(cmd)) {
if (cmd->sub_table) {
/* continue with next arg */
help_cmd_dump(mon, cmd->sub_table,
Expand Down Expand Up @@ -653,7 +657,7 @@ static const HMPCommand *monitor_parse_command(MonitorHMP *hmp_mon,
(int)(p - cmdp_start), cmdp_start);
return NULL;
}
if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
if (!cmd_available(cmd)) {
monitor_printf(mon, "Command '%.*s' not available with -preconfig "
"until after exit_preconfig.\n",
(int)(p - cmdp_start), cmdp_start);
Expand Down Expand Up @@ -1225,17 +1229,15 @@ static void monitor_find_completion_by_table(MonitorHMP *mon,
}
readline_set_completion_index(mon->rs, strlen(cmdname));
for (cmd = cmd_table; cmd->name != NULL; cmd++) {
if (!runstate_check(RUN_STATE_PRECONFIG) ||
cmd_can_preconfig(cmd)) {
if (cmd_available(cmd)) {
cmd_completion(mon, cmdname, cmd->name);
}
}
} else {
/* find the command */
for (cmd = cmd_table; cmd->name != NULL; cmd++) {
if (hmp_compare_cmd(args[0], cmd->name) &&
(!runstate_check(RUN_STATE_PRECONFIG) ||
cmd_can_preconfig(cmd))) {
cmd_available(cmd)) {
break;
}
}
Expand Down

0 comments on commit 4cd2927

Please sign in to comment.