Patchwork [v3] Add tab-completion for device_add.

login
register
mail settings
Submitter Andrzej Zaborowski
Date Feb. 20, 2012, 2:39 a.m.
Message ID <1329705554-22225-1-git-send-email-andrew.zaborowski@intel.com>
Download mbox | patch
Permalink /patch/142153/
State New
Headers show

Comments

Andrzej Zaborowski - Feb. 20, 2012, 2:39 a.m.
From: Andrzej Zaborowski <balrogg@gmail.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.
v3: use QOM.
---
 monitor.c |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)

Patch

diff --git a/monitor.c b/monitor.c
index aadbdcb..28f5b9a 100644
--- a/monitor.c
+++ b/monitor.c
@@ -3917,6 +3917,27 @@  static void block_completion_it(void *opaque, BlockDriverState *bs)
     }
 }
 
+static void class_completion_it(ObjectClass *klass, void *opaque)
+{
+    const char *input = opaque;
+    const char *name = object_class_get_name(klass);
+
+    if (input[0] == '\0' || !strncmp(name, input, strlen(input))) {
+        readline_add_completion(cur_mon->rs, name);
+    }
+}
+
+static int driver_prop_completion_it(Property *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;
+}
+
 /* NOTE: this parser is an approximate form of the real command parser */
 static void parse_cmdline(const char *cmdline,
                          int *pnb_args, char **args)
@@ -3957,6 +3978,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
@@ -3995,6 +4017,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') {
@@ -4040,9 +4063,44 @@  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);
+                    ObjectClass *klass = object_class_by_name(driver);
+                    DeviceClass *info;
+                    Property *prop;
+
+                    g_free(driver);
+                    if (!klass) {
+                        break;
+                    }
+
+                    info = DEVICE_CLASS(klass);
+                    for (prop = info->props; prop && prop->name; prop++) {
+                        driver_prop_completion_it(prop, sep + 1);
+                    }
+                    if (info->bus_info) {
+                        for (prop = info->bus_info->props;
+                                prop && prop->name; prop++) {
+                            driver_prop_completion_it(prop, sep + 1);
+                        }
+                    }
+                    break;
+                }
+
+                object_class_foreach(class_completion_it, TYPE_DEVICE, 0,
+                                (void *) str);
+            }
+            break;
         default:
             break;
         }
+        g_free(pkey);
     }
 
 cleanup: