diff options
author | Jan Kiszka <jan.kiszka@siemens.com> | 2012-01-27 19:54:54 +0100 |
---|---|---|
committer | Anthony Liguori <aliguori@us.ibm.com> | 2012-02-01 14:45:01 -0600 |
commit | 4f6dd9af9cf0a0138ff7733f45568c4e20b3bad1 (patch) | |
tree | 0cdbeb4a93e759bd209434e360d1e831746da8d4 /qemu-option.c | |
parent | 4e4fa398db69e22dcad23114eb7e33b4d89b10c4 (diff) |
qemu-option: Introduce default mechanism
This adds qemu_opts_set_defaults, an interface provide default values
for a QemuOpts set. Default options are parsed from a string and then
prepended to the list of existing options, or they serve as the sole
QemuOpts set.
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'qemu-option.c')
-rw-r--r-- | qemu-option.c | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/qemu-option.c b/qemu-option.c index a303f87e1c..4626ccfe54 100644 --- a/qemu-option.c +++ b/qemu-option.c @@ -603,7 +603,8 @@ static void qemu_opt_del(QemuOpt *opt) g_free(opt); } -int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) +static int opt_set(QemuOpts *opts, const char *name, const char *value, + bool prepend) { QemuOpt *opt; const QemuOptDesc *desc = opts->list->desc; @@ -626,7 +627,11 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) opt = g_malloc0(sizeof(*opt)); opt->name = g_strdup(name); opt->opts = opts; - QTAILQ_INSERT_TAIL(&opts->head, opt, next); + if (prepend) { + QTAILQ_INSERT_HEAD(&opts->head, opt, next); + } else { + QTAILQ_INSERT_TAIL(&opts->head, opt, next); + } if (desc[i].name != NULL) { opt->desc = desc+i; } @@ -640,6 +645,11 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) return 0; } +int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) +{ + return opt_set(opts, name, value, false); +} + int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) { QemuOpt *opt; @@ -691,6 +701,9 @@ QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) QTAILQ_FOREACH(opts, &list->head, next) { if (!opts->id) { + if (!id) { + return opts; + } continue; } if (strcmp(opts->id, id) != 0) { @@ -806,7 +819,8 @@ int qemu_opts_print(QemuOpts *opts, void *dummy) return 0; } -int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) +static int opts_do_parse(QemuOpts *opts, const char *params, + const char *firstname, bool prepend) { char option[128], value[1024]; const char *p,*pe,*pc; @@ -841,7 +855,7 @@ int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname } if (strcmp(option, "id") != 0) { /* store and parse */ - if (qemu_opt_set(opts, option, value) == -1) { + if (opt_set(opts, option, value, prepend) == -1) { return -1; } } @@ -852,8 +866,13 @@ int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname return 0; } -QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, - int permit_abbrev) +int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) +{ + return opts_do_parse(opts, params, firstname, false); +} + +static QemuOpts *opts_parse(QemuOptsList *list, const char *params, + int permit_abbrev, bool defaults) { const char *firstname; char value[1024], *id = NULL; @@ -870,11 +889,19 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, get_opt_value(value, sizeof(value), p+4); id = value; } - opts = qemu_opts_create(list, id, 1); + if (defaults) { + if (!id && !QTAILQ_EMPTY(&list->head)) { + opts = qemu_opts_find(list, NULL); + } else { + opts = qemu_opts_create(list, id, 0); + } + } else { + opts = qemu_opts_create(list, id, 1); + } if (opts == NULL) return NULL; - if (qemu_opts_do_parse(opts, params, firstname) != 0) { + if (opts_do_parse(opts, params, firstname, defaults) != 0) { qemu_opts_del(opts); return NULL; } @@ -882,6 +909,21 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, return opts; } +QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, + int permit_abbrev) +{ + return opts_parse(list, params, permit_abbrev, false); +} + +void qemu_opts_set_defaults(QemuOptsList *list, const char *params, + int permit_abbrev) +{ + QemuOpts *opts; + + opts = opts_parse(list, params, permit_abbrev, true); + assert(opts); +} + static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) { char buf[32]; |