From: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, Dong Xu Wang <wdongxu@linux.vnet.ibm.com>,
wdongxu@cn.ibm.com, armbru@redhat.com, stefanha@redhat.com
Subject: [Qemu-devel] [PATCH V16 2/7] avoid duplication of default value in QemuOpts
Date: Tue, 18 Jun 2013 17:31:54 +0800 [thread overview]
Message-ID: <1371547919-15654-3-git-send-email-wdongxu@linux.vnet.ibm.com> (raw)
In-Reply-To: <1371547919-15654-1-git-send-email-wdongxu@linux.vnet.ibm.com>
This patch moves the default value entirely to QemuOptDesc.
When getting the value of an option that hasn't been set, and
QemuOptDesc has a default value, return that. Else, behave as
before.
Example: qemu_opt_get_number(opts, "foo", 42)
If "foo" has been set in opts, return its value.
Else, if opt's QemuOptDesc has a default value for "foo", return
that.
Else, return 42.
Note that the last argument is useless when QemuOptDesc has a
default value. Ugly. If it bothers us, we could assert.
Example: qemu_opt_get(opts, "bar")
If "bar" has been set in opts, return its value.
Else, if opt's QemuOptDesc has a default value for "bar", return
that.
Else, return NULL.
Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
v13->v14:
1) change code style.
2) assert errors.
util/qemu-option.c | 66 ++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 49 insertions(+), 17 deletions(-)
diff --git a/util/qemu-option.c b/util/qemu-option.c
index 84d8c8b..bd2acdc 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -525,9 +525,31 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
return NULL;
}
+static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
+ const char *name)
+{
+ int i;
+
+ for (i = 0; desc[i].name != NULL; i++) {
+ if (strcmp(desc[i].name, name) == 0) {
+ return &desc[i];
+ }
+ }
+
+ return NULL;
+}
+
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;
}
@@ -546,9 +568,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;
}
@@ -556,9 +586,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;
}
@@ -566,9 +604,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;
}
@@ -609,20 +655,6 @@ static bool opts_accepts_any(const QemuOpts *opts)
return opts->list->desc[0].name == NULL;
}
-static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
- const char *name)
-{
- int i;
-
- for (i = 0; desc[i].name != NULL; i++) {
- if (strcmp(desc[i].name, name) == 0) {
- return &desc[i];
- }
- }
-
- return NULL;
-}
-
static void opt_set(QemuOpts *opts, const char *name, const char *value,
bool prepend, Error **errp)
{
--
1.7.11.7
next prev parent reply other threads:[~2013-06-18 9:33 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-18 9:31 [Qemu-devel] [PATCH V16 0/7] replace QEMUOptionParameter with QemuOpts parser Dong Xu Wang
2013-06-18 9:31 ` [Qemu-devel] [PATCH V16 1/7] add def_value_str in QemuOptDesc struct and rewrite qemu_opts_print Dong Xu Wang
2013-06-18 9:31 ` Dong Xu Wang [this message]
2013-06-18 9:31 ` [Qemu-devel] [PATCH V16 3/7] Create four QemuOptsList related functions Dong Xu Wang
2013-07-10 16:07 ` Eric Blake
2013-06-18 9:31 ` [Qemu-devel] [PATCH V16 4/7] Create some QemuOpts functons Dong Xu Wang
2013-07-10 16:51 ` Eric Blake
2013-07-10 19:06 ` Eric Blake
2013-06-18 9:31 ` [Qemu-devel] [PATCH V16 5/7] Use QemuOpts support in block layer Dong Xu Wang
2013-07-10 17:56 ` Eric Blake
2013-07-10 19:47 ` Eric Blake
2013-06-18 9:31 ` [Qemu-devel] [PATCH V16 6/7] query-command-line-options outputs def_value_str Dong Xu Wang
2013-07-10 19:56 ` Eric Blake
2013-06-18 9:31 ` [Qemu-devel] [PATCH V16 7/7] remove QEMUOptionParameter related functions and struct Dong Xu Wang
2013-07-10 19:57 ` Eric Blake
2013-07-04 12:52 ` [Qemu-devel] [PATCH V16 0/7] replace QEMUOptionParameter with QemuOpts parser Stefan Hajnoczi
2013-07-09 20:41 ` Eric Blake
2013-07-10 19:49 ` Eric Blake
2013-07-15 7:38 ` Dong Xu Wang
2013-07-09 22:05 ` Andreas Färber
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1371547919-15654-3-git-send-email-wdongxu@linux.vnet.ibm.com \
--to=wdongxu@linux.vnet.ibm.com \
--cc=armbru@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
--cc=wdongxu@cn.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).