Patchwork [v2] Add tab-completion for device_add.

login
register
mail settings
Submitter andrzej zaborowski
Date Jan. 16, 2012, 3:57 p.m.
Message ID <1326729445-15546-1-git-send-email-balrogg@gmail.com>
Download mbox | patch
Permalink /patch/136385/
State New
Headers show

Comments

andrzej zaborowski - Jan. 16, 2012, 3:57 p.m.
From: Andrzej Zaborowski <andrew.zaborowski@intel.com>

Signed-off-by: Andrzej Zaborowski <andrew.zaborowski@intel.com>
---
v2: pass only char *prop_name as qdev_driver_prop_foreach_func parameter,
  Anthony noted Property was soon going away.
---
 hw/qdev.c |   38 ++++++++++++++++++++++++++++++++++++++
 hw/qdev.h |    7 +++++++
 monitor.c |   41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 0 deletions(-)

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index e59f345..d36be35 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -1568,3 +1568,41 @@  void qdev_machine_init(void)
     qdev_get_peripheral_anon();
     qdev_get_peripheral();
 }
+
+int qdev_driver_foreach(qdev_driver_foreach_func func, void *opaque)
+{
+    DeviceInfo *info;
+    int ret = 0;
+
+    for (info = device_info_list; info != NULL; info = info->next) {
+        ret |= (*func)(info, opaque);
+    }
+
+    return ret;
+}
+
+int qdev_driver_prop_foreach(qdev_driver_prop_foreach_func func,
+                const char *driver, void *opaque)
+{
+    DeviceInfo *info;
+    Property *prop;
+    int ret = 0;
+
+    info = qdev_find_info(NULL, driver);
+    if (!info) {
+        return -1;
+    }
+
+    for (prop = info->props; prop && prop->name; prop++) {
+        /*
+         * TODO Properties without a parser are just for dirty hacks.
+         * See comment in qdev_device_help.
+         */
+        if (!prop->info->parse) {
+            continue;           /* no way to set it, don't show */
+        }
+        ret |= (*func)(prop->name, opaque);
+    }
+
+    return ret;
+}
diff --git a/hw/qdev.h b/hw/qdev.h
index 2abb767..aa2455b 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -624,4 +624,11 @@  char *qdev_get_type(DeviceState *dev, Error **errp);
  */
 void qdev_machine_init(void);
 
+/* Driver querying */
+typedef int (*qdev_driver_foreach_func)(DeviceInfo *info, void *opaque);
+int qdev_driver_foreach(qdev_driver_foreach_func func, void *opaque);
+typedef int (*qdev_driver_prop_foreach_func)(const char *prop, void *opaque);
+int qdev_driver_prop_foreach(qdev_driver_prop_foreach_func func,
+                const char *driver, void *opaque);
+
 #endif
diff --git a/monitor.c b/monitor.c
index 7334401..9a9beb5 100644
--- a/monitor.c
+++ b/monitor.c
@@ -4069,6 +4069,28 @@  static void block_completion_it(void *opaque, BlockDriverState *bs)
     }
 }
 
+static int driver_completion_it(DeviceInfo *info, void *opaque)
+{
+    const char *input = opaque;
+
+    if (input[0] == '\0' || !strncmp(info->name, input, strlen(input))) {
+        readline_add_completion(cur_mon->rs, info->name);
+    }
+
+    return 0;
+}
+
+static int driver_prop_completion_it(const char *prop_name, void *opaque)
+{
+    const char *input = opaque;
+
+    if (input[0] == '\0' || !strncmp(prop_name, input, strlen(input))) {
+        readline_add_completion(cur_mon->rs, prop_name);
+    }
+
+    return 0;
+}
+
 /* NOTE: this parser is an approximate form of the real command parser */
 static void parse_cmdline(const char *cmdline,
                          int *pnb_args, char **args)
@@ -4109,6 +4131,7 @@  static void monitor_find_completion(const char *cmdline)
     const char *ptype, *str;
     const mon_cmd_t *cmd;
     const KeyDef *key;
+    char *pkey;
 
     parse_cmdline(cmdline, &nb_args, args);
 #ifdef DEBUG_COMPLETION
@@ -4147,6 +4170,7 @@  static void monitor_find_completion(const char *cmdline)
             goto cleanup;
         }
 
+        key_get_info(cmd->args_type, &pkey);
         ptype = next_arg_type(cmd->args_type);
         for(i = 0; i < nb_args - 2; i++) {
             if (*ptype != '\0') {
@@ -4192,9 +4216,26 @@  static void monitor_find_completion(const char *cmdline)
                 }
             }
             break;
+        case 'O':
+            if (!strcmp(pkey, "device")) {
+                char *sep = strrchr(str, ',');
+
+                readline_set_completion_index(cur_mon->rs,
+                                strlen(sep ? sep + 1 : str));
+                if (sep) {
+                    char *driver = g_strndup(str, strchr(str, ',') - str);
+                    qdev_driver_prop_foreach(driver_prop_completion_it,
+                                    driver, (void *) (sep + 1));
+                    g_free(driver);
+                } else {
+                    qdev_driver_foreach(driver_completion_it, (void *) str);
+                }
+            }
+            break;
         default:
             break;
         }
+        g_free(pkey);
     }
 
 cleanup: