[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH] Add tab-completion for device_add.
From: |
Andrzej Zaborowski |
Subject: |
[Qemu-devel] [PATCH] Add tab-completion for device_add. |
Date: |
Sun, 8 Jan 2012 16:28:54 +0100 |
Signed-off-by: Andrzej Zaborowski <address@hidden>
---
There are other ways to do this, but adding an API for querying
available qdev drivers was the one that made most sense to me.
---
hw/qdev.c | 38 ++++++++++++++++++++++++++++++++++++++
hw/qdev.h | 7 +++++++
monitor.c | 41 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 86 insertions(+), 0 deletions(-)
diff --git a/hw/qdev.c b/hw/qdev.c
index d0cf66d..ba97312 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -1535,3 +1535,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, opaque);
+ }
+
+ return ret;
+}
diff --git a/hw/qdev.h b/hw/qdev.h
index 2abb767..b72a31a 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)(Property *info, 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..3eab307 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(Property *prop, 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:
--
1.7.4.4