Skip to contentSkip to conversation

monitor: Completion support for various commands

PATCH In review Hani Benhabiles 2 participants 24 messages Mar 30, 2014 – Apr 11, 2014 Networking

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/17 monitor: Fix drive_del id argument type completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hmp-commands.hx +1 -1
index f3fc514..6bf4797 100644
@@ -176,7 +176,7 @@ ETEXI
 
     {
         .name       = "drive_del",
-        .args_type  = "id:s",
+        .args_type  = "id:B",
         .params     = "device",
         .help       = "remove host block device",
         .user_print = monitor_user_noop,
Signature · 1 line
1.8.3.2
#2 lore raw

PATCH v2 2/17 monitor: Add command_completion callback to mon_cmd_t.

In reply to Hani Benhabiles

Convert object_add and object_del commands to use the new callback.

---
 hmp-commands.hx |  2 ++
 hmp.h           |  3 +++
 monitor.c       | 19 +++++++++++++------
 3 files changed, 18 insertions(+), 6 deletions(-)
hmp-commands.hx +2 -0
index 6bf4797..1b382b6 100644
@@ -1254,6 +1254,7 @@ ETEXI
         .params     = "[qom-type=]type,id=str[,prop=value][,...]",
         .help       = "create QOM object",
         .mhandler.cmd = hmp_object_add,
+        .command_completion = object_add_completion,
     },
 
 STEXI
@@ -1268,6 +1269,7 @@ ETEXI
         .params     = "id",
         .help       = "destroy QOM object",
         .mhandler.cmd = hmp_object_del,
+        .command_completion = object_del_completion,
     },
 
 STEXI
hmp.h +3 -0
index ed58f0e..2f2c059 100644
@@ -15,6 +15,7 @@
 #define HMP_H
 
 #include "qemu-common.h"
+#include "qemu/readline.h"
 #include "qapi-types.h"
 #include "qapi/qmp/qdict.h"
 
@@ -92,5 +93,7 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict);
 void hmp_cpu_add(Monitor *mon, const QDict *qdict);
 void hmp_object_add(Monitor *mon, const QDict *qdict);
 void hmp_object_del(Monitor *mon, const QDict *qdict);
+void object_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +13 -6
index 342e83b..566a83f 100644
@@ -137,6 +137,7 @@ typedef struct mon_cmd_t {
      * used, and mhandler of 1st level plays the role of help function.
      */
     struct mon_cmd_t *sub_table;
+    void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
 } mon_cmd_t;
 
 /* file descriptors passed via SCM_RIGHTS */
@@ -4298,11 +4299,15 @@ static void device_add_completion(ReadLineState *rs, const char *str)
     g_slist_free(list);
 }
 
-static void object_add_completion(ReadLineState *rs, const char *str)
+void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
     size_t len;
 
+    if (nb_args != 2) {
+        return;
+    }
+
     len = strlen(str);
     readline_set_completion_index(rs, len);
     list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
@@ -4337,11 +4342,14 @@ static void device_del_completion(ReadLineState *rs, BusState *bus,
     }
 }
 
-static void object_del_completion(ReadLineState *rs, const char *str)
+void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     ObjectPropertyInfoList *list, *start;
     size_t len;
 
+    if (nb_args != 2) {
+        return;
+    }
     len = strlen(str);
     readline_set_completion_index(rs, len);
 
@@ -4395,6 +4403,9 @@ static void monitor_find_completion_by_table(Monitor *mon,
             return monitor_find_completion_by_table(mon, cmd->sub_table,
                                                     &args[1], nb_args - 1);
         }
+        if (cmd->command_completion) {
+            return cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
+        }
 
         ptype = next_arg_type(cmd->args_type);
         for(i = 0; i < nb_args - 2; i++) {
@@ -4424,8 +4435,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
         case 'O':
             if (!strcmp(cmd->name, "device_add") && nb_args == 2) {
                 device_add_completion(mon->rs, str);
-            } else if (!strcmp(cmd->name, "object_add") && nb_args == 2) {
-                object_add_completion(mon->rs, str);
             }
             break;
         case 's':
@@ -4445,8 +4454,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
                 size_t len = strlen(str);
                 readline_set_completion_index(mon->rs, len);
                 device_del_completion(mon->rs, sysbus_get_default(), str, len);
-            } else if (!strcmp(cmd->name, "object_del") && nb_args == 2) {
-                object_del_completion(mon->rs, str);
             }
             break;
         default:
Signature · 1 line
1.8.3.2
#4 lore raw

PATCH v2 3/17 monitor: Add device_add and device_del completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  2 ++
 hmp.h           |  2 ++
 monitor.c       | 34 +++++++++++++++++++++-------------
 3 files changed, 25 insertions(+), 13 deletions(-)
hmp-commands.hx +2 -0
index 1b382b6..4c4d261 100644
@@ -658,6 +658,7 @@ ETEXI
         .help       = "add device, like -device on the command line",
         .user_print = monitor_user_noop,
         .mhandler.cmd_new = do_device_add,
+        .command_completion = device_add_completion,
     },
 
 STEXI
@@ -673,6 +674,7 @@ ETEXI
         .params     = "device",
         .help       = "remove device",
         .mhandler.cmd = hmp_device_del,
+        .command_completion = device_del_completion,
     },
 
 STEXI
hmp.h +2 -0
index 2f2c059..20ef454 100644
@@ -95,5 +95,7 @@ void hmp_object_add(Monitor *mon, const QDict *qdict);
 void hmp_object_del(Monitor *mon, const QDict *qdict);
 void object_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
+void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +21 -13
index 566a83f..710ba25 100644
@@ -4278,11 +4278,15 @@ static const char *next_arg_type(const char *typestr)
     return (p != NULL ? ++p : typestr);
 }
 
-static void device_add_completion(ReadLineState *rs, const char *str)
+void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
     size_t len;
 
+    if (nb_args != 2) {
+        return;
+    }
+
     len = strlen(str);
     readline_set_completion_index(rs, len);
     list = elt = object_class_get_list(TYPE_DEVICE, false);
@@ -4323,8 +4327,8 @@ void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
     g_slist_free(list);
 }
 
-static void device_del_completion(ReadLineState *rs, BusState *bus,
-                                  const char *str, size_t len)
+static void device_del_bus_completion(ReadLineState *rs,  BusState *bus,
+                                      const char *str, size_t len)
 {
     BusChild *kid;
 
@@ -4337,11 +4341,24 @@ static void device_del_completion(ReadLineState *rs, BusState *bus,
         }
 
         QLIST_FOREACH(dev_child, &dev->child_bus, sibling) {
-            device_del_completion(rs, dev_child, str, len);
+            device_del_bus_completion(rs, dev_child, str, len);
         }
     }
 }
 
+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);
+    device_del_bus_completion(rs, sysbus_get_default(), str, len);
+}
+
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     ObjectPropertyInfoList *list, *start;
@@ -4432,11 +4449,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
             readline_set_completion_index(mon->rs, strlen(str));
             bdrv_iterate(block_completion_it, &mbs);
             break;
-        case 'O':
-            if (!strcmp(cmd->name, "device_add") && nb_args == 2) {
-                device_add_completion(mon->rs, str);
-            }
-            break;
         case 's':
         case 'S':
             if (!strcmp(cmd->name, "sendkey")) {
@@ -4450,10 +4462,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
             } else if (!strcmp(cmd->name, "help|?")) {
                 monitor_find_completion_by_table(mon, cmd_table,
                                                  &args[1], nb_args - 1);
-            } else if (!strcmp(cmd->name, "device_del") && nb_args == 2) {
-                size_t len = strlen(str);
-                readline_set_completion_index(mon->rs, len);
-                device_del_completion(mon->rs, sysbus_get_default(), str, len);
             }
             break;
         default:
Signature · 1 line
1.8.3.2
#0 lore raw

v2 cover monitor: Completion support for various commands

This patch series adds a new callback to mon_cmd_t which will make adding completion support for more commands cleaner.

It then adds full or partial arguments completion for multiple hmp commands.

Changes since v1:
* Splitting patch 1/7 to 1/17, 2/17 and 3/17.
* Changed command_completion's first argument from Monitor to ReadLineState.
* Added new commands completions (10/17 to 17/17)

Hani Benhabiles (17):
monitor: Fix drive_del id argument type completion.
monitor: Add command_completion callback to mon_cmd_t.
monitor: Add device_add and device_del completion.
monitor: Add chardev-remove id argument completion.
monitor: Add chardev-add backend argument completion.
monitor: Add cpu index argument completion.
monitor: Add set_link arguments completion.
monitor: Add netdev_add type argument completion.
monitor: Add netdev_del id argument completion.
monitor: Add ringbuf_write and ringbuf_read argument completion.
monitor: Add watchdog_action argument completion.
monitor: Add migrate_set_capability completion.
monitor: Add host_net_add device argument completion.
readline: Make completion strings always unique.
monitor: Add host_net_remove arguments completion.
monitor: Add mouse_set index argument completion.
monitor: Add delvm and loadvm argument completion.

 hmp-commands.hx       |  25 +++-
 hmp.h                 |  23 +++
 include/sysemu/char.h |   3 +-
 monitor.c             | 397 +++++++++++++++++++++++++++++++++++++++++++++++---
 net/net.c             |   2 +-
 qemu-char.c           |   2 +-
 util/readline.c       |   6 +
 7 files changed, 432 insertions(+), 26 deletions(-)
Signature · 1 line
1.8.3.2
#6 lore raw

PATCH v2 4/17 monitor: Add chardev-remove id argument completion.

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 23 +++++++++++++++++++++++
 3 files changed, 25 insertions(+)
hmp-commands.hx +1 -0
index 4c4d261..4f0f053 100644
@@ -1630,6 +1630,7 @@ ETEXI
         .params     = "id",
         .help       = "remove chardev",
         .mhandler.cmd = hmp_chardev_remove,
+        .command_completion = chardev_remove_completion,
     },
 
 STEXI
hmp.h +1 -0
index 20ef454..42b4d6b 100644
@@ -97,5 +97,6 @@ void object_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
+void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +23 -0
index 710ba25..040da9b 100644
@@ -4346,6 +4346,29 @@ static void device_del_bus_completion(ReadLineState *rs,  BusState *bus,
     }
 }
 
+void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    ChardevInfoList *list, *start;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+
+    start = list = qmp_query_chardev(NULL);
+    while (list) {
+        ChardevInfo *chr = list->value;
+
+        if (!strncmp(chr->label, str, len)) {
+            readline_add_completion(rs, chr->label);
+        }
+        list = list->next;
+    }
+    qapi_free_ChardevInfoList(start);
+}
+
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
Signature · 1 line
1.8.3.2
#8 lore raw

PATCH v2 5/17 monitor: Add chardev-add backend argument completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 23 +++++++++++++++++++++++
 3 files changed, 25 insertions(+)
hmp-commands.hx +1 -0
index 4f0f053..a411d4f 100644
@@ -1614,6 +1614,7 @@ ETEXI
         .params     = "args",
         .help       = "add chardev",
         .mhandler.cmd = hmp_chardev_add,
+        .command_completion = chardev_add_completion,
     },
 
 STEXI
hmp.h +1 -0
index 42b4d6b..6de5d1a 100644
@@ -98,5 +98,6 @@ void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
+void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +23 -0
index 040da9b..27caa74 100644
@@ -4278,6 +4278,29 @@ static const char *next_arg_type(const char *typestr)
     return (p != NULL ? ++p : typestr);
 }
 
+void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    ChardevBackendInfoList *list, *start;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+
+    start = list = qmp_query_chardev_backends(NULL);
+    while (list) {
+        const char *chr_name = list->value->name;
+
+        if (!strncmp(chr_name, str, len)) {
+            readline_add_completion(rs, chr_name);
+        }
+        list = list->next;
+    }
+    qapi_free_ChardevBackendInfoList(start);
+}
+
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
Signature · 1 line
1.8.3.2
#9 lore raw

PATCH v2 6/17 monitor: Add cpu index argument completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 24 ++++++++++++++++++++++++
 3 files changed, 26 insertions(+)
hmp-commands.hx +1 -0
index a411d4f..813c0fb 100644
@@ -690,6 +690,7 @@ ETEXI
         .params     = "index",
         .help       = "set the default CPU",
         .mhandler.cmd = hmp_cpu,
+        .command_completion = cpu_completion,
     },
 
 STEXI
hmp.h +1 -0
index 6de5d1a..8701b16 100644
@@ -99,5 +99,6 @@ void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void cpu_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +24 -0
index 27caa74..29d4f37 100644
@@ -4429,6 +4429,30 @@ void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_ObjectPropertyInfoList(start);
 }
 
+void cpu_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    CpuInfoList *cpu_list, *start;
+    size_t len;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+
+    start = cpu_list = qmp_query_cpus(NULL);
+    while (cpu_list) {
+        char name[16];
+        snprintf(name, sizeof(name), "%" PRId64, cpu_list->value->CPU);
+
+        if (!strncmp(str, name, len)) {
+            readline_add_completion(rs, name);
+        }
+        cpu_list = cpu_list->next;
+    }
+    qapi_free_CpuInfoList(start);
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.8.3.2
#10 lore raw

PATCH v2 7/17 monitor: Add set_link arguments completion.

In reply to Hani Benhabiles

Make it possible to query all net clients without specifying an ID when calling qemu_find_net_clients_except().

This also adds the add_completion_option() function which is to be used for other commands completions as well.

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 34 ++++++++++++++++++++++++++++++++++
 net/net.c       |  2 +-
 4 files changed, 37 insertions(+), 1 deletion(-)
hmp-commands.hx +1 -0
index 813c0fb..fbd303a 100644
@@ -1332,6 +1332,7 @@ ETEXI
         .params     = "name on|off",
         .help       = "change the link status of a network adapter",
         .mhandler.cmd = hmp_set_link,
+        .command_completion = set_link_completion,
     },
 
 STEXI
hmp.h +1 -0
index 8701b16..33a883d 100644
@@ -100,5 +100,6 @@ void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void cpu_completion(ReadLineState *rs, int nb_args, const char *str);
+void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +34 -0
index 29d4f37..009f269 100644
@@ -4278,6 +4278,17 @@ static const char *next_arg_type(const char *typestr)
     return (p != NULL ? ++p : typestr);
 }
 
+static void add_completion_option(ReadLineState *rs, const char *str,
+                                  const char *option)
+{
+    if (!str || !option) {
+        return;
+    }
+    if (!strncmp(option, str, strlen(str))) {
+        readline_add_completion(rs, option);
+    }
+}
+
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
@@ -4453,6 +4464,29 @@ void cpu_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_CpuInfoList(start);
 }
 
+void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    if (nb_args == 2) {
+        NetClientState *ncs[255];
+        int count, i;
+        count = qemu_find_net_clients_except(NULL, ncs,
+                                             NET_CLIENT_OPTIONS_KIND_NONE, 255);
+        for (i = 0; i < count; i++) {
+            const char *name = ncs[i]->name;
+            if (!strncmp(str, name, len)) {
+                readline_add_completion(rs, name);
+            }
+        }
+    } else if (nb_args == 3) {
+        add_completion_option(rs, str, "on");
+        add_completion_option(rs, str, "off");
+    }
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
net/net.c +1 -1
index e3ef1e4..86dd051 100644
@@ -633,7 +633,7 @@ int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
         if (nc->info->type == type) {
             continue;
         }
-        if (!strcmp(nc->name, id)) {
+        if (!id || !strcmp(nc->name, id)) {
             if (ret < max) {
                 ncs[ret] = nc;
             }
Signature · 1 line
1.8.3.2
#12 lore raw

PATCH v2 8/17 monitor: Add netdev_add type argument completion.

In reply to Hani Benhabiles

Also update the command's documentation.

---
 hmp-commands.hx |  3 ++-
 hmp.h           |  1 +
 monitor.c       | 18 ++++++++++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
hmp-commands.hx +2 -1
index fbd303a..b009561 100644
@@ -1226,9 +1226,10 @@ ETEXI
     {
         .name       = "netdev_add",
         .args_type  = "netdev:O",
-        .params     = "[user|tap|socket|hubport|netmap],id=str[,prop=value][,...]",
+        .params     = "[user|tap|socket|vde|bridge|hubport|netmap],id=str[,prop=value][,...]",
         .help       = "add host network device",
         .mhandler.cmd = hmp_netdev_add,
+        .command_completion = netdev_add_completion,
     },
 
 STEXI
hmp.h +1 -0
index 33a883d..9dab1a3 100644
@@ -101,5 +101,6 @@ void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void cpu_completion(ReadLineState *rs, int nb_args, const char *str);
 void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
+void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +18 -0
index 009f269..5293370 100644
@@ -4312,6 +4312,24 @@ void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_ChardevBackendInfoList(start);
 }
 
+void netdev_add_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);
+    add_completion_option(rs, str, "user");
+    add_completion_option(rs, str, "tap");
+    add_completion_option(rs, str, "socket");
+    add_completion_option(rs, str, "vde");
+    add_completion_option(rs, str, "bridge");
+    add_completion_option(rs, str, "hubport");
+    add_completion_option(rs, str, "netmap");
+}
+
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
Signature · 1 line
1.8.3.2
#14 lore raw

PATCH v2 9/17 monitor: Add netdev_del id argument completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+)
hmp-commands.hx +1 -0
index b009561..d252ffc 100644
@@ -1244,6 +1244,7 @@ ETEXI
         .params     = "id",
         .help       = "remove host network device",
         .mhandler.cmd = hmp_netdev_del,
+        .command_completion = netdev_del_completion,
     },
 
 STEXI
hmp.h +1 -0
index 9dab1a3..c98722b 100644
@@ -102,5 +102,6 @@ void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void cpu_completion(ReadLineState *rs, int nb_args, const char *str);
 void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +26 -0
index 5293370..d1b0d6c 100644
@@ -4505,6 +4505,32 @@ void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
     }
 }
 
+void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    int len, count, i;
+    NetClientState *ncs[255];
+
+    if (nb_args != 2) {
+        return;
+    }
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
+                                         255);
+    for (i = 0; i < count; i++) {
+        QemuOpts *opts;
+        const char *name = ncs[i]->name;
+        if (strncmp(str, name, len)) {
+            continue;
+        }
+        opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
+        if (opts) {
+            readline_add_completion(rs, name);
+        }
+    }
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.8.3.2
#15 lore raw

PATCH v2 10/17 monitor: Add ringbuf_write and ringbuf_read argument completion.

In reply to Hani Benhabiles

Export chr_is_ringbuf() function. Also remove left-over function prototypes while at it.

---
 hmp-commands.hx       |  2 ++
 hmp.h                 |  2 ++
 include/sysemu/char.h |  3 +--
 monitor.c             | 39 +++++++++++++++++++++++++++++++++++++++
 qemu-char.c           |  2 +-
 5 files changed, 45 insertions(+), 3 deletions(-)
hmp-commands.hx +2 -0
index d252ffc..300be4e 100644
@@ -852,6 +852,7 @@ ETEXI
         .params     = "device data",
         .help       = "Write to a ring buffer character device",
         .mhandler.cmd = hmp_ringbuf_write,
+        .command_completion = ringbuf_write_completion,
     },
 
 STEXI
@@ -868,6 +869,7 @@ ETEXI
         .params     = "device size",
         .help       = "Read from a ring buffer character device",
         .mhandler.cmd = hmp_ringbuf_read,
+        .command_completion = ringbuf_write_completion,
     },
 
 STEXI
hmp.h +2 -0
index c98722b..d7d0770 100644
@@ -103,5 +103,7 @@ void cpu_completion(ReadLineState *rs, int nb_args, const char *str);
 void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str);
+void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str);
+void ringbuf_read_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
include/sysemu/char.h +1 -2
index b81a6ff..7f5eeb3 100644
@@ -286,9 +286,8 @@ void qemu_chr_add_handlers(CharDriverState *s,
 void qemu_chr_be_generic_open(CharDriverState *s);
 void qemu_chr_accept_input(CharDriverState *s);
 int qemu_chr_add_client(CharDriverState *s, int fd);
-void qemu_chr_info_print(Monitor *mon, const QObject *ret_data);
-void qemu_chr_info(Monitor *mon, QObject **ret_data);
 CharDriverState *qemu_chr_find(const char *name);
+bool chr_is_ringbuf(const CharDriverState *chr);
 
 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
 
monitor.c +39 -0
index d1b0d6c..839f8f1 100644
@@ -4421,6 +4421,45 @@ void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_ChardevInfoList(start);
 }
 
+static void ringbuf_completion(ReadLineState *rs, const char *str)
+{
+    size_t len;
+    ChardevInfoList *list, *start;
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+
+    start = list = qmp_query_chardev(NULL);
+    while (list) {
+        ChardevInfo *chr_info = list->value;
+
+        if (!strncmp(chr_info->label, str, len)) {
+            CharDriverState *chr = qemu_chr_find(chr_info->label);
+            if (chr && chr_is_ringbuf(chr)) {
+                readline_add_completion(rs, chr_info->label);
+            }
+        }
+        list = list->next;
+    }
+    qapi_free_ChardevInfoList(start);
+}
+
+void ringbuf_read_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    if (nb_args != 2) {
+        return;
+    }
+    ringbuf_completion(rs, str);
+}
+
+void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    if (nb_args != 2) {
+        return;
+    }
+    ringbuf_completion(rs, str);
+}
+
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
qemu-char.c +1 -1
index 54ed244..34c8f08 100644
@@ -2849,7 +2849,7 @@ fail:
     return NULL;
 }
 
-static bool chr_is_ringbuf(const CharDriverState *chr)
+bool chr_is_ringbuf(const CharDriverState *chr)
 {
     return chr->chr_write == ringbuf_chr_write;
 }
Signature · 1 line
1.8.3.2
#16 lore raw

PATCH v2 11/17 monitor: Add watchdog_action argument completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  1 +
 hmp.h           |  2 ++
 monitor.c       | 14 ++++++++++++++
 3 files changed, 17 insertions(+)
hmp-commands.hx +1 -0
index 300be4e..669a845 100644
@@ -1351,6 +1351,7 @@ ETEXI
         .params     = "[reset|shutdown|poweroff|pause|debug|none]",
         .help       = "change watchdog action",
         .mhandler.cmd = do_watchdog_action,
+        .command_completion = watchdog_action_completion,
     },
 
 STEXI
hmp.h +2 -0
index d7d0770..eca50b4 100644
@@ -105,5 +105,7 @@ void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str);
 void ringbuf_read_completion(ReadLineState *rs, int nb_args, const char *str);
+void watchdog_action_completion(ReadLineState *rs, int nb_args,
+                                const char *str);
 
 #endif
monitor.c +14 -0
index 839f8f1..cb1fb7c 100644
@@ -4570,6 +4570,20 @@ void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
     }
 }
 
+void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    if (nb_args != 2) {
+        return;
+    }
+    readline_set_completion_index(rs, strlen(str));
+    add_completion_option(rs, str, "reset");
+    add_completion_option(rs, str, "shutdown");
+    add_completion_option(rs, str, "poweroff");
+    add_completion_option(rs, str, "pause");
+    add_completion_option(rs, str, "debug");
+    add_completion_option(rs, str, "none");
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.8.3.2
#17 lore raw

PATCH v2 12/17 monitor: Add migrate_set_capability completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  1 +
 hmp.h           |  2 ++
 monitor.c       | 21 +++++++++++++++++++++
 3 files changed, 24 insertions(+)
hmp-commands.hx +1 -0
index 669a845..8c674ba 100644
@@ -975,6 +975,7 @@ ETEXI
         .params     = "capability state",
         .help       = "Enable/Disable the usage of a capability for migration",
         .mhandler.cmd = hmp_migrate_set_capability,
+        .command_completion = migrate_set_capability_completion,
     },
 
 STEXI
hmp.h +2 -0
index eca50b4..3570530 100644
@@ -107,5 +107,7 @@ void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str);
 void ringbuf_read_completion(ReadLineState *rs, int nb_args, const char *str);
 void watchdog_action_completion(ReadLineState *rs, int nb_args,
                                 const char *str);
+void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
+                                       const char *str);
 
 #endif
monitor.c +21 -0
index cb1fb7c..2974a66 100644
@@ -4584,6 +4584,27 @@ void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
     add_completion_option(rs, str, "none");
 }
 
+void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
+                                       const char *str)
+{
+    size_t len;
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    if (nb_args == 2) {
+        int i;
+        for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
+            const char *name = MigrationCapability_lookup[i];
+            if (!strncmp(str, name, len)) {
+                readline_add_completion(rs, name);
+            }
+        }
+    } else if (nb_args == 3) {
+        add_completion_option(rs, str, "on");
+        add_completion_option(rs, str, "off");
+    }
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.8.3.2
#18 lore raw

PATCH v2 13/17 monitor: Add host_net_add device argument completion.

In reply to Hani Benhabiles

Also fix the parameters documentation.

---
 hmp-commands.hx |  3 ++-
 hmp.h           |  1 +
 monitor.c       | 14 ++++++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
hmp-commands.hx +2 -1
index 8c674ba..9da5866 100644
@@ -1201,9 +1201,10 @@ ETEXI
     {
         .name       = "host_net_add",
         .args_type  = "device:s,opts:s?",
-        .params     = "tap|user|socket|vde|netmap|dump [options]",
+        .params     = "tap|user|socket|vde|bridge|dump [options]",
         .help       = "add host VLAN client",
         .mhandler.cmd = net_host_device_add,
+        .command_completion = host_net_add_completion,
     },
 
 STEXI
hmp.h +1 -0
index 3570530..c82fd87 100644
@@ -109,5 +109,6 @@ void watchdog_action_completion(ReadLineState *rs, int nb_args,
                                 const char *str);
 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
                                        const char *str);
+void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +14 -0
index 2974a66..4c20bc5 100644
@@ -4605,6 +4605,20 @@ void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
     }
 }
 
+void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    if (nb_args != 2) {
+        return;
+    }
+    readline_set_completion_index(rs, strlen(str));
+    add_completion_option(rs, str, "tap");
+    add_completion_option(rs, str, "user");
+    add_completion_option(rs, str, "socket");
+    add_completion_option(rs, str, "vde");
+    add_completion_option(rs, str, "dump");
+    add_completion_option(rs, str, "bridge");
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.8.3.2
#19 lore raw

PATCH v2 14/17 readline: Make completion strings always unique.

In reply to Hani Benhabiles

There is no need to clutter the user's choices with repeating the same value multiple times.

---
 util/readline.c | 6 ++++++
 1 file changed, 6 insertions(+)
util/readline.c +6 -0
index 8441be4..6aeae6a 100644
@@ -263,6 +263,12 @@ static void readline_hist_add(ReadLineState *rs, const char *cmdline)
 void readline_add_completion(ReadLineState *rs, const char *str)
 {
     if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
+        int i;
+        for (i = 0; i < rs->nb_completions; i++) {
+            if (!strcmp(rs->completions[i], str)) {
+                return;
+            }
+        }
         rs->completions[rs->nb_completions++] = g_strdup(str);
     }
 }
Signature · 1 line
1.8.3.2
#20 lore raw

PATCH v2 15/17 monitor: Add host_net_remove arguments completion.

In reply to Hani Benhabiles

Relies on readline unique completion strings patch to make the added vlan/hub completion values unique, instead of using something like a hash table.

---
 hmp-commands.hx |  1 +
 hmp.h           |  2 ++
 monitor.c       | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+)
hmp-commands.hx +1 -0
index 9da5866..b41a6d6 100644
@@ -1219,6 +1219,7 @@ ETEXI
         .params     = "vlan_id name",
         .help       = "remove host VLAN client",
         .mhandler.cmd = net_host_device_remove,
+        .command_completion = host_net_remove_completion,
     },
 
 STEXI
hmp.h +2 -0
index c82fd87..12f0364 100644
@@ -110,5 +110,7 @@ void watchdog_action_completion(ReadLineState *rs, int nb_args,
 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
                                        const char *str);
 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void host_net_remove_completion(ReadLineState *rs, int nb_args,
+                                const char *str);
 
 #endif
monitor.c +38 -0
index 4c20bc5..9781ee2 100644
@@ -4619,6 +4619,44 @@ void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
     add_completion_option(rs, str, "bridge");
 }
 
+void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    NetClientState *ncs[255];
+    int count, i, len;
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    if (nb_args == 2) {
+        count = qemu_find_net_clients_except(NULL, ncs,
+                                             NET_CLIENT_OPTIONS_KIND_NONE, 255);
+        for (i = 0; i < count; i++) {
+            int id;
+            char name[16];
+
+            if (net_hub_id_for_client(ncs[i], &id)) {
+                continue;
+            }
+            snprintf(name, sizeof(name), "%d", id);
+            if (!strncmp(str, name, len)) {
+                readline_add_completion(rs, name);
+            }
+        }
+        return;
+    } else if (nb_args == 3) {
+        count = qemu_find_net_clients_except(NULL, ncs,
+                                             NET_CLIENT_OPTIONS_KIND_NIC, 255);
+        for (i = 0; i < count; i++) {
+            const char *name;
+
+            name = ncs[i]->name;
+            if (!strncmp(str, name, len)) {
+                readline_add_completion(rs, name);
+            }
+        }
+        return;
+    }
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.8.3.2
#21 lore raw

PATCH v2 16/17 monitor: Add mouse_set index argument completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 22 ++++++++++++++++++++++
 3 files changed, 24 insertions(+)
hmp-commands.hx +1 -0
index b41a6d6..04aa059 100644
@@ -734,6 +734,7 @@ ETEXI
         .params     = "index",
         .help       = "set which mouse device receives events",
         .mhandler.cmd = do_mouse_set,
+        .command_completion = mouse_set_completion,
     },
 
 STEXI
hmp.h +1 -0
index 12f0364..6e76473 100644
@@ -112,5 +112,6 @@ void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void host_net_remove_completion(ReadLineState *rs, int nb_args,
                                 const char *str);
+void mouse_set_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +22 -0
index 9781ee2..385fb70 100644
@@ -4657,6 +4657,28 @@ void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
     }
 }
 
+void mouse_set_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    MouseInfoList *mice_list, *mouse;
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    mice_list = mouse = qmp_query_mice(NULL);
+    while (mouse) {
+        char name[16];
+        snprintf(name, sizeof(name), "%" PRId64, mouse->value->index);
+
+        if (!strncmp(str, name, len)) {
+            readline_add_completion(rs, name);
+        }
+        mouse = mouse->next;
+    }
+    qapi_free_MouseInfoList(mice_list);
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.8.3.2
#22 lore raw

PATCH v2 17/17 monitor: Add delvm and loadvm argument completion.

In reply to Hani Benhabiles
---
 hmp-commands.hx |  2 ++
 hmp.h           |  2 ++
 monitor.c       | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+)
hmp-commands.hx +2 -0
index 04aa059..b59d0e0 100644
@@ -335,6 +335,7 @@ ETEXI
         .params     = "tag|id",
         .help       = "restore a VM snapshot from its tag or id",
         .mhandler.cmd = do_loadvm,
+        .command_completion = loadvm_completion,
     },
 
 STEXI
@@ -350,6 +351,7 @@ ETEXI
         .params     = "tag|id",
         .help       = "delete a VM snapshot from its tag or id",
         .mhandler.cmd = do_delvm,
+        .command_completion = delvm_completion,
     },
 
 STEXI
hmp.h +2 -0
index 6e76473..40d1513 100644
@@ -113,5 +113,7 @@ void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void host_net_remove_completion(ReadLineState *rs, int nb_args,
                                 const char *str);
 void mouse_set_completion(ReadLineState *rs, int nb_args, const char *str);
+void delvm_completion(ReadLineState *rs, int nb_args, const char *str);
+void loadvm_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +48 -0

60 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#3 lore raw

Re: [Qemu-devel] [PATCH v2 02/17] monitor: Add command_completion callback to mon_cmd_t.

In reply to Hani Benhabiles
Quoted 95 lines · On Sun, 30 Mar 2014 11:58:24 +0100 Hani Benhabiles <[email protected]> wrote:

Convert object_add and object_del commands to use the new callback.

---
 hmp-commands.hx |  2 ++
 hmp.h           |  3 +++
 monitor.c       | 19 +++++++++++++------
 3 files changed, 18 insertions(+), 6 deletions(-)
hmp-commands.hx +2 -0
index 6bf4797..1b382b6 100644
@@ -1254,6 +1254,7 @@ ETEXI
         .params     = "[qom-type=]type,id=str[,prop=value][,...]",
         .help       = "create QOM object",
         .mhandler.cmd = hmp_object_add,
+        .command_completion = object_add_completion,
     },
 
 STEXI
@@ -1268,6 +1269,7 @@ ETEXI
         .params     = "id",
         .help       = "destroy QOM object",
         .mhandler.cmd = hmp_object_del,
+        .command_completion = object_del_completion,
     },
 
 STEXI
hmp.h +3 -0
index ed58f0e..2f2c059 100644
@@ -15,6 +15,7 @@
 #define HMP_H
 
 #include "qemu-common.h"
+#include "qemu/readline.h"
 #include "qapi-types.h"
 #include "qapi/qmp/qdict.h"
 
@@ -92,5 +93,7 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict);
 void hmp_cpu_add(Monitor *mon, const QDict *qdict);
 void hmp_object_add(Monitor *mon, const QDict *qdict);
 void hmp_object_del(Monitor *mon, const QDict *qdict);
+void object_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +10 -2
index 342e83b..566a83f 100644
@@ -137,6 +137,7 @@ typedef struct mon_cmd_t {
      * used, and mhandler of 1st level plays the role of help function.
      */
     struct mon_cmd_t *sub_table;
+    void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
 } mon_cmd_t;
 
 /* file descriptors passed via SCM_RIGHTS */
@@ -4298,11 +4299,15 @@ static void device_add_completion(ReadLineState *rs, const char *str)
     g_slist_free(list);
 }
 
-static void object_add_completion(ReadLineState *rs, const char *str)
+void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
     size_t len;
 
+    if (nb_args != 2) {
+        return;
+    }
+
     len = strlen(str);
     readline_set_completion_index(rs, len);
     list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
@@ -4337,11 +4342,14 @@ static void device_del_completion(ReadLineState *rs, BusState *bus,
     }
 }
 
-static void object_del_completion(ReadLineState *rs, const char *str)
+void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     ObjectPropertyInfoList *list, *start;
     size_t len;
 
+    if (nb_args != 2) {
+        return;
+    }
     len = strlen(str);
     readline_set_completion_index(rs, len);
 

monitor.c static void monitor_find_completion_by_table(Monitor *mon, · line 4408

Quoted 6 lines
monitor.c +3 -0
@@ -4395,6 +4403,9 @@ static void monitor_find_completion_by_table(Monitor *mon,
             return monitor_find_completion_by_table(mon, cmd->sub_table,
                                                     &args[1], nb_args - 1);
         }
+        if (cmd->command_completion) {
+            return cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
+        }

Honest question: have you checked if this plays well if the recursive call to monitor_find_completion_by_table()?

Quoted 21 lines
monitor.c +0 -4
         ptype = next_arg_type(cmd->args_type);
         for(i = 0; i < nb_args - 2; i++) {
@@ -4424,8 +4435,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
         case 'O':
             if (!strcmp(cmd->name, "device_add") && nb_args == 2) {
                 device_add_completion(mon->rs, str);
-            } else if (!strcmp(cmd->name, "object_add") && nb_args == 2) {
-                object_add_completion(mon->rs, str);
             }
             break;
         case 's':
@@ -4445,8 +4454,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
                 size_t len = strlen(str);
                 readline_set_completion_index(mon->rs, len);
                 device_del_completion(mon->rs, sysbus_get_default(), str, len);
-            } else if (!strcmp(cmd->name, "object_del") && nb_args == 2) {
-                object_del_completion(mon->rs, str);
             }
             break;
         default:
#5 lore raw

Re: [Qemu-devel] [PATCH v2 03/17] monitor: Add device_add and device_del completion.

In reply to Hani Benhabiles
Quoted 1 line · On Sun, 30 Mar 2014 11:58:25 +0100 Hani Benhabiles <[email protected]> wrote:

This is not related to this change, but device_add seems to return non-hotpluggable devices in the completion list (eg. Opteron_G5-x86_64-cpu). Might be worth fixing in this series.

One more comment below.

Quoted 60 lines
---
 hmp-commands.hx |  2 ++
 hmp.h           |  2 ++
 monitor.c       | 34 +++++++++++++++++++++-------------
 3 files changed, 25 insertions(+), 13 deletions(-)
hmp-commands.hx +2 -0
index 1b382b6..4c4d261 100644
@@ -658,6 +658,7 @@ ETEXI
         .help       = "add device, like -device on the command line",
         .user_print = monitor_user_noop,
         .mhandler.cmd_new = do_device_add,
+        .command_completion = device_add_completion,
     },
 
 STEXI
@@ -673,6 +674,7 @@ ETEXI
         .params     = "device",
         .help       = "remove device",
         .mhandler.cmd = hmp_device_del,
+        .command_completion = device_del_completion,
     },
 
 STEXI
hmp.h +2 -0
index 2f2c059..20ef454 100644
@@ -95,5 +95,7 @@ void hmp_object_add(Monitor *mon, const QDict *qdict);
 void hmp_object_del(Monitor *mon, const QDict *qdict);
 void object_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
+void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +5 -1
index 566a83f..710ba25 100644
@@ -4278,11 +4278,15 @@ static const char *next_arg_type(const char *typestr)
     return (p != NULL ? ++p : typestr);
 }
 
-static void device_add_completion(ReadLineState *rs, const char *str)
+void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
     size_t len;
 
+    if (nb_args != 2) {
+        return;
+    }
+
     len = strlen(str);
     readline_set_completion_index(rs, len);
     list = elt = object_class_get_list(TYPE_DEVICE, false);

monitor.c void object_add_completion(ReadLineState *rs, int nb_args, const char *str) · line 4332

Quoted 8 lines
monitor.c +2 -2
@@ -4323,8 +4327,8 @@ void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
     g_slist_free(list);
 }
 
-static void device_del_completion(ReadLineState *rs, BusState *bus,
-                                  const char *str, size_t len)
+static void device_del_bus_completion(ReadLineState *rs,  BusState *bus,
+                                      const char *str, size_t len)
 {

You don't have to respin just because of this, but it's not clear to me why you're renaming that function.

Quoted 51 lines
monitor.c +14 -10
     BusChild *kid;
 
@@ -4337,11 +4341,24 @@ static void device_del_completion(ReadLineState *rs, BusState *bus,
         }
 
         QLIST_FOREACH(dev_child, &dev->child_bus, sibling) {
-            device_del_completion(rs, dev_child, str, len);
+            device_del_bus_completion(rs, dev_child, str, len);
         }
     }
 }
 
+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);
+    device_del_bus_completion(rs, sysbus_get_default(), str, len);
+}
+
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     ObjectPropertyInfoList *list, *start;
@@ -4432,11 +4449,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
             readline_set_completion_index(mon->rs, strlen(str));
             bdrv_iterate(block_completion_it, &mbs);
             break;
-        case 'O':
-            if (!strcmp(cmd->name, "device_add") && nb_args == 2) {
-                device_add_completion(mon->rs, str);
-            }
-            break;
         case 's':
         case 'S':
             if (!strcmp(cmd->name, "sendkey")) {
@@ -4450,10 +4462,6 @@ static void monitor_find_completion_by_table(Monitor *mon,
             } else if (!strcmp(cmd->name, "help|?")) {
                 monitor_find_completion_by_table(mon, cmd_table,
                                                  &args[1], nb_args - 1);
-            } else if (!strcmp(cmd->name, "device_del") && nb_args == 2) {
-                size_t len = strlen(str);
-                readline_set_completion_index(mon->rs, len);
-                device_del_completion(mon->rs, sysbus_get_default(), str, len);
             }
             break;
         default:
#7 lore raw

Re: [Qemu-devel] [PATCH v2 04/17] monitor: Add chardev-remove id argument completion.

In reply to Hani Benhabiles
Quoted 1 line · On Sun, 30 Mar 2014 11:58:26 +0100 Hani Benhabiles <[email protected]> wrote:

Gerd, can you review this one and the patch (04/18). Both look good to me, btw.

Quoted 63 lines
---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 23 +++++++++++++++++++++++
 3 files changed, 25 insertions(+)
hmp-commands.hx +1 -0
index 4c4d261..4f0f053 100644
@@ -1630,6 +1630,7 @@ ETEXI
         .params     = "id",
         .help       = "remove chardev",
         .mhandler.cmd = hmp_chardev_remove,
+        .command_completion = chardev_remove_completion,
     },
 
 STEXI
hmp.h +1 -0
index 20ef454..42b4d6b 100644
@@ -97,5 +97,6 @@ void object_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
+void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +23 -0
index 710ba25..040da9b 100644
@@ -4346,6 +4346,29 @@ static void device_del_bus_completion(ReadLineState *rs,  BusState *bus,
     }
 }
 
+void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    ChardevInfoList *list, *start;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+
+    start = list = qmp_query_chardev(NULL);
+    while (list) {
+        ChardevInfo *chr = list->value;
+
+        if (!strncmp(chr->label, str, len)) {
+            readline_add_completion(rs, chr->label);
+        }
+        list = list->next;
+    }
+    qapi_free_ChardevInfoList(start);
+}
+
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
#11 lore raw

Re: [Qemu-devel] [PATCH v2 07/17] monitor: Add set_link arguments completion.

In reply to Hani Benhabiles
Quoted 76 lines · On Sun, 30 Mar 2014 11:58:29 +0100 Hani Benhabiles <[email protected]> wrote:

Make it possible to query all net clients without specifying an ID when calling qemu_find_net_clients_except().

This also adds the add_completion_option() function which is to be used for other commands completions as well.

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 34 ++++++++++++++++++++++++++++++++++
 net/net.c       |  2 +-
 4 files changed, 37 insertions(+), 1 deletion(-)
hmp-commands.hx +1 -0
index 813c0fb..fbd303a 100644
@@ -1332,6 +1332,7 @@ ETEXI
         .params     = "name on|off",
         .help       = "change the link status of a network adapter",
         .mhandler.cmd = hmp_set_link,
+        .command_completion = set_link_completion,
     },
 
 STEXI
hmp.h +1 -0
index 8701b16..33a883d 100644
@@ -100,5 +100,6 @@ void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void cpu_completion(ReadLineState *rs, int nb_args, const char *str);
+void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +24 -0
index 29d4f37..009f269 100644
@@ -4278,6 +4278,17 @@ static const char *next_arg_type(const char *typestr)
     return (p != NULL ? ++p : typestr);
 }
 
+static void add_completion_option(ReadLineState *rs, const char *str,
+                                  const char *option)
+{
+    if (!str || !option) {
+        return;
+    }
+    if (!strncmp(option, str, strlen(str))) {
+        readline_add_completion(rs, option);
+    }
+}
+
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
@@ -4453,6 +4464,29 @@ void cpu_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_CpuInfoList(start);
 }
 
+void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    if (nb_args == 2) {
+        NetClientState *ncs[255];
+        int count, i;
+        count = qemu_find_net_clients_except(NULL, ncs,
+                                             NET_CLIENT_OPTIONS_KIND_NONE, 255);
+        for (i = 0; i < count; i++) {
+            const char *name = ncs[i]->name;

monitor.c void cpu_completion(ReadLineState *rs, int nb_args, const char *str) · line 4489

Quoted 10 lines
monitor.c +10 -0
+            if (!strncmp(str, name, len)) {
+                readline_add_completion(rs, name);
+            }
+        }
+    } else if (nb_args == 3) {
+        add_completion_option(rs, str, "on");
+        add_completion_option(rs, str, "off");
+    }
+}
+

This prints both, the backend and frontend. Shouldn't it print only one of them?

Quoted 16 lines
monitor.c +0 -0
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
net/net.c +1 -1
index e3ef1e4..86dd051 100644
@@ -633,7 +633,7 @@ int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
         if (nc->info->type == type) {
             continue;
         }
-        if (!strcmp(nc->name, id)) {
+        if (!id || !strcmp(nc->name, id)) {
             if (ret < max) {
                 ncs[ret] = nc;
             }
#13 lore raw

Re: [Qemu-devel] [PATCH v2 08/17] monitor: Add netdev_add type argument completion.

In reply to Hani Benhabiles
Quoted 50 lines · On Sun, 30 Mar 2014 11:58:30 +0100 Hani Benhabiles <[email protected]> wrote:

Also update the command's documentation.

---
 hmp-commands.hx |  3 ++-
 hmp.h           |  1 +
 monitor.c       | 18 ++++++++++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
hmp-commands.hx +2 -1
index fbd303a..b009561 100644
@@ -1226,9 +1226,10 @@ ETEXI
     {
         .name       = "netdev_add",
         .args_type  = "netdev:O",
-        .params     = "[user|tap|socket|hubport|netmap],id=str[,prop=value][,...]",
+        .params     = "[user|tap|socket|vde|bridge|hubport|netmap],id=str[,prop=value][,...]",
         .help       = "add host network device",
         .mhandler.cmd = hmp_netdev_add,
+        .command_completion = netdev_add_completion,
     },
 
 STEXI
hmp.h +1 -0
index 33a883d..9dab1a3 100644
@@ -101,5 +101,6 @@ void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void cpu_completion(ReadLineState *rs, int nb_args, const char *str);
 void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
+void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +6 -0
index 009f269..5293370 100644
@@ -4312,6 +4312,24 @@ void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_ChardevBackendInfoList(start);
 }
 
+void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+
+    if (nb_args != 2) {
+        return;

monitor.c void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str) · line 4330

Quoted 10 lines
monitor.c +10 -0
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    add_completion_option(rs, str, "user");
+    add_completion_option(rs, str, "tap");
+    add_completion_option(rs, str, "socket");
+    add_completion_option(rs, str, "vde");
+    add_completion_option(rs, str, "bridge");
+    add_completion_option(rs, str, "hubport");
+    add_completion_option(rs, str, "netmap");

Having hardcoded values doesn't look good to me at all. I think we need a way to query those, and then we add completion.

Quoted 5 lines
monitor.c +2 -0
+}
+
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
#23 lore raw
In reply to Hani Benhabiles
Quoted 4 lines · On Sun, 30 Mar 2014 11:58:22 +0100 Hani Benhabiles <[email protected]> wrote:

This patch series adds a new callback to mon_cmd_t which will make adding completion support for more commands cleaner.

It then adds full or partial arguments completion for multiple hmp commands.

I was half-way through this series when something occurred to me: what about merging only the existing completions first? I think that that can be merged faster because it won't require other people's reviews and discussions on new completions won't hold the entire series.

If you agree, please repost it and I'll try to review it quicker.

Quoted 35 lines

Changes since v1:
* Splitting patch 1/7 to 1/17, 2/17 and 3/17.
* Changed command_completion's first argument from Monitor to ReadLineState.
* Added new commands completions (10/17 to 17/17)

Hani Benhabiles (17):
monitor: Fix drive_del id argument type completion.
monitor: Add command_completion callback to mon_cmd_t.
monitor: Add device_add and device_del completion.
monitor: Add chardev-remove id argument completion.
monitor: Add chardev-add backend argument completion.
monitor: Add cpu index argument completion.
monitor: Add set_link arguments completion.
monitor: Add netdev_add type argument completion.
monitor: Add netdev_del id argument completion.
monitor: Add ringbuf_write and ringbuf_read argument completion.
monitor: Add watchdog_action argument completion.
monitor: Add migrate_set_capability completion.
monitor: Add host_net_add device argument completion.
readline: Make completion strings always unique.
monitor: Add host_net_remove arguments completion.
monitor: Add mouse_set index argument completion.
monitor: Add delvm and loadvm argument completion.

 hmp-commands.hx       |  25 +++-
 hmp.h                 |  23 +++
 include/sysemu/char.h |   3 +-
 monitor.c             | 397 +++++++++++++++++++++++++++++++++++++++++++++++---
 net/net.c             |   2 +-
 qemu-char.c           |   2 +-
 util/readline.c       |   6 +
 7 files changed, 432 insertions(+), 26 deletions(-)