diff mbox

[v19,01/25] add def_value_str to QemuOptDesc

Message ID 1390227608-7225-2-git-send-email-cyliu@suse.com
State New
Headers show

Commit Message

Chunyan Liu Jan. 20, 2014, 2:19 p.m. UTC
Add def_value_str (default value) to QemuOptDesc, to replace function of the
default value in QEMUOptionParameter. And improved related functions.
 
Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
Signed-off-by: Chunyan Liu <cyliu@suse.com>
---
 include/qemu/option.h |    3 +-
 util/qemu-option.c    |   76 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 68 insertions(+), 11 deletions(-)

Comments

Kevin Wolf Jan. 22, 2014, 1:25 p.m. UTC | #1
Am 20.01.2014 um 15:19 hat Chunyan Liu geschrieben:
> Add def_value_str (default value) to QemuOptDesc, to replace function of the
> default value in QEMUOptionParameter. And improved related functions.
>  
> Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
> Signed-off-by: Chunyan Liu <cyliu@suse.com>

It would be worth mentioning that qemu_opts_print() is unused, so
changing the prototype and behaviour is fine.

> -int qemu_opts_print(QemuOpts *opts, void *dummy)
> +void qemu_opts_print(QemuOpts *opts)
>  {
>      QemuOpt *opt;
> +    QemuOptDesc *desc = opts->list->desc;
>  
> -    fprintf(stderr, "%s: %s:", opts->list->name,
> -            opts->id ? opts->id : "<noid>");
> -    QTAILQ_FOREACH(opt, &opts->head, next) {
> -        fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
> +    if (desc[0].name == NULL) {

I think 'if (opts_accepts_any(opts))' would be more readable.

> +        QTAILQ_FOREACH(opt, &opts->head, next) {
> +            printf("%s=\"%s\" ", opt->name, opt->str);
> +        }
> +        return;
> +    }
> +    for (; desc && desc->name; desc++) {
> +        const char *value = desc->def_value_str;
> +        QemuOpt *opt;
> +
> +        opt = qemu_opt_find(opts, desc->name);
> +        if (opt) {
> +            value = opt->str;
> +        }
> +
> +        if (!value) {
> +            continue;
> +        }
> +
> +        if (desc->type == QEMU_OPT_STRING) {
> +            printf("%s='%s' ", desc->name, value);
> +        } else if (desc->type == QEMU_OPT_SIZE && opt) {
> +            printf("%s=%" PRIu64 " ", desc->name, opt->value.uint);

This is so that a value like '64k' gets expanded to '65536'? Perhaps add
a comment?

> +        } else {
> +            printf("%s=%s ", desc->name, value);
> +        }
>      }
> -    fprintf(stderr, "\n");
> -    return 0;
>  }

Kevin
diff mbox

Patch

diff --git a/include/qemu/option.h b/include/qemu/option.h
index 3ea871a..2c5b03f 100644
--- a/include/qemu/option.h
+++ b/include/qemu/option.h
@@ -97,6 +97,7 @@  typedef struct QemuOptDesc {
     const char *name;
     enum QemuOptType type;
     const char *help;
+    const char *def_value_str;
 } QemuOptDesc;
 
 struct QemuOptsList {
@@ -154,7 +155,7 @@  QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);
 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp);
 
 typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
-int qemu_opts_print(QemuOpts *opts, void *dummy);
+void qemu_opts_print(QemuOpts *opts);
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure);
 
diff --git a/util/qemu-option.c b/util/qemu-option.c
index 668e5d9..fd84f95 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -33,6 +33,9 @@ 
 #include "qapi/qmp/qerror.h"
 #include "qemu/option_int.h"
 
+static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
+                                            const char *name);
+
 /*
  * Extracts the name of an option from the parameter string (p points at the
  * first byte of the option name)
@@ -507,6 +510,14 @@  static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
 const char *qemu_opt_get(QemuOpts *opts, const char *name)
 {
     QemuOpt *opt = qemu_opt_find(opts, name);
+    const QemuOptDesc *desc;
+
+    if (!opt) {
+        desc = find_desc_by_name(opts->list->desc, name);
+        if (desc && desc->def_value_str) {
+            return desc->def_value_str;
+        }
+    }
     return opt ? opt->str : NULL;
 }
 
@@ -525,9 +536,17 @@  bool qemu_opt_has_help_opt(QemuOpts *opts)
 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
 {
     QemuOpt *opt = qemu_opt_find(opts, name);
+    const QemuOptDesc *desc;
+    Error *local_err = NULL;
 
-    if (opt == NULL)
+    if (opt == NULL) {
+        desc = find_desc_by_name(opts->list->desc, name);
+        if (desc && desc->def_value_str) {
+            parse_option_bool(name, desc->def_value_str, &defval, &local_err);
+            assert(!local_err);
+        }
         return defval;
+    }
     assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
     return opt->value.boolean;
 }
@@ -535,9 +554,17 @@  bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
 {
     QemuOpt *opt = qemu_opt_find(opts, name);
+    const QemuOptDesc *desc;
+    Error *local_err = NULL;
 
-    if (opt == NULL)
+    if (opt == NULL) {
+        desc = find_desc_by_name(opts->list->desc, name);
+        if (desc && desc->def_value_str) {
+            parse_option_number(name, desc->def_value_str, &defval, &local_err);
+            assert(!local_err);
+        }
         return defval;
+    }
     assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
     return opt->value.uint;
 }
@@ -545,9 +572,17 @@  uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
 {
     QemuOpt *opt = qemu_opt_find(opts, name);
+    const QemuOptDesc *desc;
+    Error *local_err = NULL;
 
-    if (opt == NULL)
+    if (opt == NULL) {
+        desc = find_desc_by_name(opts->list->desc, name);
+        if (desc && desc->def_value_str) {
+            parse_option_size(name, desc->def_value_str, &defval, &local_err);
+            assert(!local_err);
+        }
         return defval;
+    }
     assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
     return opt->value.uint;
 }
@@ -846,17 +881,38 @@  void qemu_opts_del(QemuOpts *opts)
     g_free(opts);
 }
 
-int qemu_opts_print(QemuOpts *opts, void *dummy)
+void qemu_opts_print(QemuOpts *opts)
 {
     QemuOpt *opt;
+    QemuOptDesc *desc = opts->list->desc;
 
-    fprintf(stderr, "%s: %s:", opts->list->name,
-            opts->id ? opts->id : "<noid>");
-    QTAILQ_FOREACH(opt, &opts->head, next) {
-        fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
+    if (desc[0].name == NULL) {
+        QTAILQ_FOREACH(opt, &opts->head, next) {
+            printf("%s=\"%s\" ", opt->name, opt->str);
+        }
+        return;
+    }
+    for (; desc && desc->name; desc++) {
+        const char *value = desc->def_value_str;
+        QemuOpt *opt;
+
+        opt = qemu_opt_find(opts, desc->name);
+        if (opt) {
+            value = opt->str;
+        }
+
+        if (!value) {
+            continue;
+        }
+
+        if (desc->type == QEMU_OPT_STRING) {
+            printf("%s='%s' ", desc->name, value);
+        } else if (desc->type == QEMU_OPT_SIZE && opt) {
+            printf("%s=%" PRIu64 " ", desc->name, opt->value.uint);
+        } else {
+            printf("%s=%s ", desc->name, value);
+        }
     }
-    fprintf(stderr, "\n");
-    return 0;
 }
 
 static int opts_do_parse(QemuOpts *opts, const char *params,