diff mbox series

[26/29] hmp: introduce cmd_available

Message ID 20201027182144.3315885-27-pbonzini@redhat.com (mailing list archive)
State New, archived
Headers show
Series cleanup qemu_init and make sense of command line processing | expand

Commit Message

Paolo Bonzini Oct. 27, 2020, 6:21 p.m. UTC
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.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 monitor/hmp.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

Comments

Igor Mammedov Nov. 20, 2020, 3:46 p.m. UTC | #1
On Tue, 27 Oct 2020 14:21:41 -0400
Paolo Bonzini <pbonzini@redhat.com> wrote:

> 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.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Igor Mammedov <imammedo@redhat.com>

> ---
>  monitor/hmp.c | 18 ++++++++++--------
>  1 file changed, 10 insertions(+), 8 deletions(-)
> 
> diff --git a/monitor/hmp.c b/monitor/hmp.c
> index c5cd9d372b..f13ef455e2 100644
> --- a/monitor/hmp.c
> +++ b/monitor/hmp.c
> @@ -213,6 +213,11 @@ 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,
> @@ -220,7 +225,7 @@ static void help_cmd_dump_one(Monitor *mon,
>  {
>      int i;
>  
> -    if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
> +    if (!cmd_available(cmd)) {
>          return;
>      }
>  
> @@ -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,
> @@ -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);
> @@ -1225,8 +1229,7 @@ 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);
>              }
>          }
> @@ -1234,8 +1237,7 @@ static void monitor_find_completion_by_table(MonitorHMP *mon,
>          /* 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;
>              }
>          }
diff mbox series

Patch

diff --git a/monitor/hmp.c b/monitor/hmp.c
index c5cd9d372b..f13ef455e2 100644
--- a/monitor/hmp.c
+++ b/monitor/hmp.c
@@ -213,6 +213,11 @@  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,
@@ -220,7 +225,7 @@  static void help_cmd_dump_one(Monitor *mon,
 {
     int i;
 
-    if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
+    if (!cmd_available(cmd)) {
         return;
     }
 
@@ -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,
@@ -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);
@@ -1225,8 +1229,7 @@  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);
             }
         }
@@ -1234,8 +1237,7 @@  static void monitor_find_completion_by_table(MonitorHMP *mon,
         /* 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;
             }
         }