From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47060) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WDTNe-0001np-4C for qemu-devel@nongnu.org; Wed, 12 Feb 2014 01:33:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WDTNV-0000pv-EU for qemu-devel@nongnu.org; Wed, 12 Feb 2014 01:33:10 -0500 Received: from victor.provo.novell.com ([137.65.250.26]:41246) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WDTNV-0000pd-49 for qemu-devel@nongnu.org; Wed, 12 Feb 2014 01:33:01 -0500 From: Chunyan Liu Date: Wed, 12 Feb 2014 14:33:04 +0800 Message-Id: <1392186806-10418-5-git-send-email-cyliu@suse.com> In-Reply-To: <1392186806-10418-1-git-send-email-cyliu@suse.com> References: <1392186806-10418-1-git-send-email-cyliu@suse.com> Subject: [Qemu-devel] [PATCH v20 04/26] add some QemuOpts functions for replace work List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, Dong Xu Wang , Chunyan Liu , stefanha@redhat.com Add some qemu_opt functions to replace the same functionality of QEMUOptionParameter handling. Signed-off-by: Dong Xu Wang Signed-off-by: Chunyan Liu --- include/qemu/option.h | 9 +++ util/qemu-option.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 0 deletions(-) diff --git a/include/qemu/option.h b/include/qemu/option.h index 2c5b03f..3957604 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -109,6 +109,7 @@ struct QemuOptsList { }; const char *qemu_opt_get(QemuOpts *opts, const char *name); +const char *qemu_opt_get_del(QemuOpts *opts, const char *name); /** * qemu_opt_has_help_opt: * @opts: options to search for a help request @@ -124,6 +125,11 @@ bool qemu_opt_has_help_opt(QemuOpts *opts); 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); uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval); +bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval); +uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name, + uint64_t defval); +uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, + uint64_t defval); int qemu_opt_unset(QemuOpts *opts, const char *name); int qemu_opt_set(QemuOpts *opts, const char *name, const char *value); void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value, @@ -159,4 +165,7 @@ void qemu_opts_print(QemuOpts *opts); int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, int abort_on_failure); +QemuOptsList *qemu_opts_append(QemuOptsList *dst, QemuOptsList *list); +void qemu_opts_free(QemuOptsList *list); +void qemu_opts_print_help(QemuOptsList *list); #endif diff --git a/util/qemu-option.c b/util/qemu-option.c index ea6793a..21699d0 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -379,6 +379,74 @@ QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, return dest; } +static size_t count_opts_list(QemuOptsList *list) +{ + QemuOptDesc *desc = NULL; + size_t num_opts = 0; + + if (!list) { + return 0; + } + + desc = list->desc; + while (desc && desc->name) { + num_opts++; + desc++; + } + + return num_opts; +} + +/* Create a new QemuOptsList with a desc of the merge of the first + * and second. It will allocate space for one new QemuOptsList plus + * enough space for QemuOptDesc in first and second QemuOptsList. + * First argument's QemuOptDesc members take precedence over second's. + * The result's name and implied_opt_name are not copied from them. + * Both merge_lists should not be set. Both lists can be NULL. + */ +QemuOptsList *qemu_opts_append(QemuOptsList *dst, + QemuOptsList *list) +{ + size_t num_opts, num_dst_opts; + QemuOptsList *tmp; + QemuOptDesc *desc; + + if (!dst && !list) { + return NULL; + } + + num_opts = count_opts_list(dst); + num_opts += count_opts_list(list); + tmp = g_malloc0(sizeof(QemuOptsList) + + (num_opts + 1) * sizeof(QemuOptDesc)); + QTAILQ_INIT(&tmp->head); + num_dst_opts = 0; + + /* copy dst->desc to new list */ + if (dst) { + desc = dst->desc; + while (desc && desc->name) { + tmp->desc[num_dst_opts++] = *desc; + tmp->desc[num_dst_opts].name = NULL; + desc++; + } + } + + /* add list->desc to new list */ + if (list) { + desc = list->desc; + while (desc && desc->name) { + if (find_desc_by_name(tmp->desc, desc->name) == NULL) { + tmp->desc[num_dst_opts++] = *desc; + tmp->desc[num_dst_opts].name = NULL; + } + desc++; + } + } + + return tmp; +} + /* * Parses a parameter string (param) into an option list (dest). * @@ -529,6 +597,18 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name) return opt ? opt->str : NULL; } +static void qemu_opt_del(QemuOpt *opt); + +const char *qemu_opt_get_del(QemuOpts *opts, const char *name) +{ + const char *str = qemu_opt_get(opts, name); + QemuOpt *opt = qemu_opt_find(opts, name); + if (opt) { + qemu_opt_del(opt); + } + return str; +} + bool qemu_opt_has_help_opt(QemuOpts *opts) { QemuOpt *opt; @@ -565,6 +645,16 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) return opt->value.boolean; } +bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval) +{ + bool ret = qemu_opt_get_bool(opts, name, defval); + QemuOpt *opt = qemu_opt_find(opts, name); + if (opt) { + qemu_opt_del(opt); + } + return ret; +} + uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) { QemuOpt *opt; @@ -589,6 +679,18 @@ uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) return opt->value.uint; } +uint64_t qemu_opt_get_number_del(QemuOpts *opts, + const char *name, + uint64_t defval) +{ + uint64_t ret = qemu_opt_get_number(opts, name, defval); + QemuOpt *opt = qemu_opt_find(opts, name); + if (opt) { + qemu_opt_del(opt); + } + return ret; +} + uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) { QemuOpt *opt; @@ -612,6 +714,17 @@ uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) return opt->value.uint; } +uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, + uint64_t defval) +{ + uint64_t ret = qemu_opt_get_size(opts, name, defval); + QemuOpt *opt = qemu_opt_find(opts, name); + if (opt) { + qemu_opt_del(opt); + } + return ret; +} + static void qemu_opt_parse(QemuOpt *opt, Error **errp) { if (opt->desc == NULL) @@ -1265,3 +1378,24 @@ int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, loc_pop(&loc); return rc; } + +/* free a QemuOptsList, can accept NULL as arguments */ +void qemu_opts_free(QemuOptsList *list) +{ + if (!list) { + return; + } + + g_free(list); +} + +void qemu_opts_print_help(QemuOptsList *list) +{ + int i; + printf("Supported options:\n"); + for (i = 0; list && list->desc[i].name; i++) { + printf("%-16s %s\n", list->desc[i].name, + list->desc[i].help ? + list->desc[i].help : ""); + } +} -- 1.6.0.2