qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org
Subject: [PULL 09/35] readline: Extract readline_add_completion_of() from monitor
Date: Fri,  3 Feb 2023 09:45:23 +0100	[thread overview]
Message-ID: <20230203084549.2622302-10-armbru@redhat.com> (raw)
In-Reply-To: <20230203084549.2622302-1-armbru@redhat.com>

monitor/misc.h has static add_completion_option().  It's useful
elsewhere in the monitor.  Since it's not monitor-specific, move it to
util/readline.c renamed to readline_add_completion_of(), and put it to
use.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20230124121946.1139465-7-armbru@redhat.com>
---
 include/qemu/readline.h |  2 +
 monitor/hmp.c           | 12 +++---
 monitor/misc.c          | 85 +++++++++++++----------------------------
 util/readline.c         |  8 ++++
 4 files changed, 41 insertions(+), 66 deletions(-)

diff --git a/include/qemu/readline.h b/include/qemu/readline.h
index 622aa4564f..b05e4782da 100644
--- a/include/qemu/readline.h
+++ b/include/qemu/readline.h
@@ -44,6 +44,8 @@ typedef struct ReadLineState {
 } ReadLineState;
 
 void readline_add_completion(ReadLineState *rs, const char *str);
+void readline_add_completion_of(ReadLineState *rs,
+                                const char *pfx, const char *str);
 void readline_set_completion_index(ReadLineState *rs, int completion_index);
 
 const char *readline_get_history(ReadLineState *rs, unsigned int index);
diff --git a/monitor/hmp.c b/monitor/hmp.c
index 65641a6e56..893e100581 100644
--- a/monitor/hmp.c
+++ b/monitor/hmp.c
@@ -1188,8 +1188,8 @@ static void cmd_completion(MonitorHMP *mon, const char *name, const char *list)
         }
         memcpy(cmd, pstart, len);
         cmd[len] = '\0';
-        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
-            readline_add_completion(mon->rs, cmd);
+        if (name[0] == '\0') {
+            readline_add_completion_of(mon->rs, name, cmd);
         }
         if (*p == '\0') {
             break;
@@ -1269,7 +1269,7 @@ static void monitor_find_completion_by_table(MonitorHMP *mon,
 {
     const char *cmdname;
     int i;
-    const char *ptype, *old_ptype, *str, *name;
+    const char *ptype, *old_ptype, *str;
     const HMPCommand *cmd;
     BlockBackend *blk = NULL;
 
@@ -1334,10 +1334,8 @@ static void monitor_find_completion_by_table(MonitorHMP *mon,
             /* block device name completion */
             readline_set_completion_index(mon->rs, strlen(str));
             while ((blk = blk_next(blk)) != NULL) {
-                name = blk_name(blk);
-                if (str[0] == '\0' ||
-                    !strncmp(name, str, strlen(str))) {
-                    readline_add_completion(mon->rs, name);
+                if (str[0] == '\0') {
+                    readline_add_completion_of(mon->rs, str, blk_name(blk));
                 }
             }
             break;
diff --git a/monitor/misc.c b/monitor/misc.c
index d58a81c452..9da52939b2 100644
--- a/monitor/misc.c
+++ b/monitor/misc.c
@@ -1350,14 +1350,6 @@ int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
     return ret;
 }
 
-static void add_completion_option(ReadLineState *rs, const char *str,
-                                  const char *option)
-{
-    if (!strncmp(option, str, strlen(str))) {
-        readline_add_completion(rs, option);
-    }
-}
-
 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
@@ -1369,7 +1361,7 @@ void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
     len = strlen(str);
     readline_set_completion_index(rs, len);
     for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
-        add_completion_option(rs, str, NetClientDriver_str(i));
+        readline_add_completion_of(rs, str, NetClientDriver_str(i));
     }
 }
 
@@ -1386,14 +1378,12 @@ void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
     readline_set_completion_index(rs, len);
     list = elt = object_class_get_list(TYPE_DEVICE, false);
     while (elt) {
-        const char *name;
         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
                                              TYPE_DEVICE);
-        name = object_class_get_name(OBJECT_CLASS(dc));
 
-        if (dc->user_creatable
-            && !strncmp(name, str, len)) {
-            readline_add_completion(rs, name);
+        if (dc->user_creatable) {
+            readline_add_completion_of(rs, str,
+                                object_class_get_name(OBJECT_CLASS(dc)));
         }
         elt = elt->next;
     }
@@ -1416,8 +1406,8 @@ void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
         const char *name;
 
         name = object_class_get_name(OBJECT_CLASS(elt->data));
-        if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
-            readline_add_completion(rs, name);
+        if (strcmp(name, TYPE_USER_CREATABLE)) {
+            readline_add_completion_of(rs, str, name);
         }
         elt = elt->next;
     }
@@ -1450,7 +1440,7 @@ static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
 }
 
 static void peripheral_device_del_completion(ReadLineState *rs,
-                                             const char *str, size_t len)
+                                             const char *str)
 {
     Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
     GSList *list, *item;
@@ -1463,8 +1453,8 @@ static void peripheral_device_del_completion(ReadLineState *rs,
     for (item = list; item; item = g_slist_next(item)) {
         DeviceState *dev = item->data;
 
-        if (dev->id && !strncmp(str, dev->id, len)) {
-            readline_add_completion(rs, dev->id);
+        if (dev->id) {
+            readline_add_completion_of(rs, str, dev->id);
         }
     }
 
@@ -1473,15 +1463,12 @@ static void peripheral_device_del_completion(ReadLineState *rs,
 
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
-    size_t len;
-
     if (nb_args != 2) {
         return;
     }
 
-    len = strlen(str);
-    readline_set_completion_index(rs, len);
-    peripheral_device_del_completion(rs, str, len);
+    readline_set_completion_index(rs, strlen(str));
+    peripheral_device_del_completion(rs, str);
 }
 
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
@@ -1499,9 +1486,8 @@ void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
     while (list) {
         ObjectPropertyInfo *info = list->value;
 
-        if (!strncmp(info->type, "child<", 5)
-            && !strncmp(info->name, str, len)) {
-            readline_add_completion(rs, info->name);
+        if (!strncmp(info->type, "child<", 5)) {
+            readline_add_completion_of(rs, str, info->name);
         }
         list = list->next;
     }
@@ -1521,14 +1507,11 @@ void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
                                              NET_CLIENT_DRIVER_NONE,
                                              MAX_QUEUE_NUM);
         for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
-            const char *name = ncs[i]->name;
-            if (!strncmp(str, name, len)) {
-                readline_add_completion(rs, name);
-            }
+            readline_add_completion_of(rs, str, ncs[i]->name);
         }
     } else if (nb_args == 3) {
-        add_completion_option(rs, str, "on");
-        add_completion_option(rs, str, "off");
+        readline_add_completion_of(rs, str, "on");
+        readline_add_completion_of(rs, str, "off");
     }
 }
 
@@ -1546,12 +1529,8 @@ void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
     count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
                                          MAX_QUEUE_NUM);
     for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
-        const char *name = ncs[i]->name;
-        if (strncmp(str, name, len)) {
-            continue;
-        }
         if (ncs[i]->is_netdev) {
-            readline_add_completion(rs, name);
+            readline_add_completion_of(rs, str, ncs[i]->name);
         }
     }
 }
@@ -1590,8 +1569,8 @@ void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
         }
         g_free(pattern);
     } else if (nb_args == 3) {
-        add_completion_option(rs, str, "on");
-        add_completion_option(rs, str, "off");
+        readline_add_completion_of(rs, str, "on");
+        readline_add_completion_of(rs, str, "off");
     }
 }
 
@@ -1604,7 +1583,7 @@ void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
     }
     readline_set_completion_index(rs, strlen(str));
     for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
-        add_completion_option(rs, str, WatchdogAction_str(i));
+        readline_add_completion_of(rs, str, WatchdogAction_str(i));
     }
 }
 
@@ -1618,14 +1597,11 @@ void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
     if (nb_args == 2) {
         int i;
         for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
-            const char *name = MigrationCapability_str(i);
-            if (!strncmp(str, name, len)) {
-                readline_add_completion(rs, name);
-            }
+            readline_add_completion_of(rs, str, MigrationCapability_str(i));
         }
     } else if (nb_args == 3) {
-        add_completion_option(rs, str, "on");
-        add_completion_option(rs, str, "off");
+        readline_add_completion_of(rs, str, "on");
+        readline_add_completion_of(rs, str, "off");
     }
 }
 
@@ -1639,10 +1615,7 @@ void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
     if (nb_args == 2) {
         int i;
         for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
-            const char *name = MigrationParameter_str(i);
-            if (!strncmp(str, name, len)) {
-                readline_add_completion(rs, name);
-            }
+            readline_add_completion_of(rs, str, MigrationParameter_str(i));
         }
     }
 }
@@ -1672,14 +1645,8 @@ static void vm_completion(ReadLineState *rs, const char *str)
 
         snapshot = snapshots;
         while (snapshot) {
-            char *completion = snapshot->value->name;
-            if (!strncmp(str, completion, len)) {
-                readline_add_completion(rs, completion);
-            }
-            completion = snapshot->value->id;
-            if (!strncmp(str, completion, len)) {
-                readline_add_completion(rs, completion);
-            }
+            readline_add_completion_of(rs, str, snapshot->value->name);
+            readline_add_completion_of(rs, str, snapshot->value->id);
             snapshot = snapshot->next;
         }
         qapi_free_SnapshotInfoList(snapshots);
diff --git a/util/readline.c b/util/readline.c
index f1ac6e4769..494a3d924e 100644
--- a/util/readline.c
+++ b/util/readline.c
@@ -286,6 +286,14 @@ void readline_add_completion(ReadLineState *rs, const char *str)
     }
 }
 
+void readline_add_completion_of(ReadLineState *rs,
+                                const char *pfx, const char *str)
+{
+    if (!strncmp(str, pfx, strlen(pfx))) {
+        readline_add_completion(rs, str);
+    }
+}
+
 void readline_set_completion_index(ReadLineState *rs, int index)
 {
     rs->completion_index = index;
-- 
2.39.0



  parent reply	other threads:[~2023-02-03  8:48 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-03  8:45 [PULL 00/35] Monitor patches for 2023-02-03 Markus Armbruster
2023-02-03  8:45 ` [PULL 01/35] MAINTAINERS: Cover userfaultfd Markus Armbruster
2023-02-03  8:45 ` [PULL 02/35] MAINTAINERS: Cover include/sysemu/accel-blocker.h Markus Armbruster
2023-02-03  8:45 ` [PULL 03/35] MAINTAINERS: Cover tpm.c again Markus Armbruster
2023-02-03  8:45 ` [PULL 04/35] monitor: Drop unnecessary includes Markus Armbruster
2023-02-03  8:45 ` [PULL 05/35] audio: Move HMP commands from monitor/ to audio/ Markus Armbruster
2023-02-03  8:45 ` [PULL 06/35] char: Move HMP commands from monitor/ to chardev/ Markus Armbruster
2023-02-03  8:45 ` [PULL 07/35] char: Factor out qmp_add_client() parts and move " Markus Armbruster
2023-02-03  8:45 ` [PULL 08/35] hmp: Drop redundant argument check from add_completion_option() Markus Armbruster
2023-02-03  8:45 ` Markus Armbruster [this message]
2023-02-08  8:44   ` [PULL 09/35] readline: Extract readline_add_completion_of() from monitor Philippe Mathieu-Daudé
2023-02-08 10:53     ` Markus Armbruster
2023-02-08 11:06       ` Philippe Mathieu-Daudé
2023-02-03  8:45 ` [PULL 10/35] hmp: Rename help_cmd() to hmp_help_cmd(), move declaration to hmp.h Markus Armbruster
2023-02-03  8:45 ` [PULL 11/35] trace: Move HMP commands from monitor/ to trace/ Markus Armbruster
2023-02-03  8:45 ` [PULL 12/35] machine: Move QMP commands from monitor/ to hw/core/ Markus Armbruster
2023-02-03  8:45 ` [PULL 13/35] machine: Move HMP " Markus Armbruster
2023-02-03  8:45 ` [PULL 14/35] qom: Move HMP commands from monitor/ to qom/ Markus Armbruster
2023-02-03  8:45 ` [PULL 15/35] block: Factor out hmp_change_medium(), and move to block/monitor/ Markus Armbruster
2023-02-03  8:45 ` [PULL 16/35] rocker: Move HMP commands from monitor to hw/net/rocker/ Markus Armbruster
2023-02-03  8:45 ` [PULL 17/35] hmp: Rewrite strlist_from_comma_list() as hmp_split_at_comma() Markus Armbruster
2023-02-03  8:45 ` [PULL 18/35] net: Move HMP commands from monitor to net/ Markus Armbruster
2023-02-03  8:45 ` [PULL 19/35] net: Move hmp_info_network() to net-hmp-cmds.c Markus Armbruster
2023-02-03  8:45 ` [PULL 20/35] migration: Move HMP commands from monitor/ to migration/ Markus Armbruster
2023-02-03  8:45 ` [PULL 21/35] migration: Move the QMP command " Markus Armbruster
2023-02-03  8:45 ` [PULL 22/35] virtio: Move HMP commands from monitor/ to hw/virtio/ Markus Armbruster
2023-02-03  8:45 ` [PULL 23/35] tpm: Move HMP commands from monitor/ to softmmu/ Markus Armbruster
2023-02-03  8:45 ` [PULL 24/35] runstate: " Markus Armbruster
2023-02-03  8:45 ` [PULL 25/35] stats: Move QMP commands from monitor/ to stats/ Markus Armbruster
2023-02-03  8:45 ` [PULL 26/35] stats: Move HMP " Markus Armbruster
2023-02-03  8:45 ` [PULL 27/35] acpi: Move the QMP command from monitor/ to hw/acpi/ Markus Armbruster
2023-02-03  8:45 ` [PULL 28/35] qdev: Move HMP command completion from monitor to softmmu/ Markus Armbruster
2023-02-03  8:45 ` [PULL 29/35] monitor: Split file descriptor passing stuff off misc.c Markus Armbruster
2023-02-03  8:45 ` [PULL 30/35] monitor: Move monitor_putc() next to monitor_puts & external linkage Markus Armbruster
2023-02-03  8:45 ` [PULL 31/35] monitor: Move target-dependent HMP commands to hmp-cmds-target.c Markus Armbruster
2023-02-03  8:45 ` [PULL 32/35] monitor: Move remaining HMP commands from misc.c to hmp-cmds.c Markus Armbruster
2023-02-03  8:45 ` [PULL 33/35] monitor: Move remaining QMP stuff from misc.c to qmp-cmds.c Markus Armbruster
2023-02-03  8:45 ` [PULL 34/35] monitor: Loosen coupling between misc.c and monitor.c slightly Markus Armbruster
2023-02-03  8:45 ` [PULL 35/35] monitor: Rename misc.c to hmp-target.c Markus Armbruster
2023-02-03 15:32 ` [PULL 00/35] Monitor patches for 2023-02-03 Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230203084549.2622302-10-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).