From patchwork Tue Jun 18 09:31:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 252195 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 26C392C029B for ; Tue, 18 Jun 2013 19:34:19 +1000 (EST) Received: from localhost ([::1]:55703 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UosIq-0007Iw-VK for incoming@patchwork.ozlabs.org; Tue, 18 Jun 2013 05:34:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51510) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UosIF-0007FB-Vm for qemu-devel@nongnu.org; Tue, 18 Jun 2013 05:33:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UosIE-0002BN-9S for qemu-devel@nongnu.org; Tue, 18 Jun 2013 05:33:39 -0400 Received: from mail-ie0-x236.google.com ([2607:f8b0:4001:c03::236]:45781) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UosIE-0002BJ-1I for qemu-devel@nongnu.org; Tue, 18 Jun 2013 05:33:38 -0400 Received: by mail-ie0-f182.google.com with SMTP id s9so9437110iec.13 for ; Tue, 18 Jun 2013 02:33:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=dhENicXXAZGYChczNfVeQWnpMMF9tijLX8QyIViv05M=; b=HGA7/xvml++NtumrYW8Yq+inQBElxrluXcM+Ejiqy2SAd02otG7Y4ZDrmRXsLT3o9Z PI630OY1r6YEPH+cnrNlB0LCfLZRAGIpo5vDSXxl4lzhOLkS7jVcOY2NVVD+OpG4fCxR i814lLnpk/HoVeW4nz2nrvspRcVFlUG8A4XLtYtYvYQRCYi/EOqHNbXDkN8xcBtv31+0 TXPhcQGsazyH/g/iM51kLyq0y7iLXtAHiksTQRkrMMuePPi7QDWuT1HLLP33jW+w7TEs GZ4OAuxzPgOaiEEJ0HWS7gBz2vTn7yB9hlbs1D2t2+5Ay8aiFpUohsHEHxpRkjfUtuOJ OUhA== X-Received: by 10.43.151.67 with SMTP id kr3mr906208icc.44.1371548017523; Tue, 18 Jun 2013 02:33:37 -0700 (PDT) Received: from 11.wdongxu.kvm58 ([202.108.130.153]) by mx.google.com with ESMTPSA id bc6sm373016igb.1.2013.06.18.02.33.34 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 18 Jun 2013 02:33:36 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Tue, 18 Jun 2013 17:31:56 +0800 Message-Id: <1371547919-15654-5-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1371547919-15654-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1371547919-15654-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4001:c03::236 Cc: kwolf@redhat.com, Dong Xu Wang , wdongxu@cn.ibm.com, armbru@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH V16 4/7] Create some QemuOpts functons X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org These functions will be used in next commit. qemu_opt_get_(*)_del functions are used to make sure we have the same behaviors as before: in block layer, after parsing a parameter value, parameter list will delete it to avoid parsing it twice. Signed-off-by: Dong Xu Wang Reviewed-by: Eric Blake --- v13->v14: 1) rewrite commit message. 2) use def_value_str in qemu_opt_get_FOO_del() and qemu_opt_get_del(). 3) delete redundant qemu_opt_del(opt). include/qemu/option.h | 11 ++++- util/qemu-option.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 8 deletions(-) diff --git a/include/qemu/option.h b/include/qemu/option.h index c7a5c14..d63e447 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -108,6 +108,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 @@ -121,13 +122,18 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name); */ bool qemu_opt_has_help_opt(QemuOpts *opts); bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval); +bool qemu_opt_get_bool_del(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); +uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, + uint64_t defval); int qemu_opt_set(QemuOpts *opts, const char *name, const char *value); +int qemu_opt_replace_set(QemuOpts *opts, const char *name, const char *value); void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value, Error **errp); int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val); int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val); +int qemu_opt_replace_set_number(QemuOpts *opts, const char *name, int64_t val); typedef int (*qemu_opt_loopfunc)(const char *name, const char *value, void *opaque); int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, int abort_on_failure); @@ -144,7 +150,10 @@ const char *qemu_opts_id(QemuOpts *opts); void qemu_opts_del(QemuOpts *opts); void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp); int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname); -QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, int permit_abbrev); +int qemu_opts_do_parse_replace(QemuOpts *opts, const char *params, + const char *firstname); +QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, + int permit_abbrev); void qemu_opts_set_defaults(QemuOptsList *list, const char *params, int permit_abbrev); QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict, diff --git a/util/qemu-option.c b/util/qemu-option.c index 5dd62e8..544aca6 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -33,6 +33,8 @@ #include "qapi/qmp/qerror.h" #include "qemu/option_int.h" +static void qemu_opt_del(QemuOpt *opt); + /* * Extracts the name of an option from the parameter string (p points at the * first byte of the option name) @@ -553,6 +555,24 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name) return opt ? opt->str : NULL; } +const char *qemu_opt_get_del(QemuOpts *opts, const char *name) +{ + QemuOpt *opt = qemu_opt_find(opts, name); + const QemuOptDesc *desc; + const char *str = NULL; + + if (!opt) { + desc = find_desc_by_name(opts->list->desc, name); + if (desc && desc->def_value_str) { + str = g_strdup(desc->def_value_str); + } + } else { + str = g_strdup(opt->str); + qemu_opt_del(opt); + } + return str; +} + bool qemu_opt_has_help_opt(QemuOpts *opts) { QemuOpt *opt; @@ -583,6 +603,27 @@ 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) +{ + QemuOpt *opt = qemu_opt_find(opts, name); + const QemuOptDesc *desc; + Error *local_err = NULL; + bool ret = defval; + + 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, &ret, &local_err); + assert(!local_err); + } + return ret; + } + assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); + ret = opt->value.boolean; + qemu_opt_del(opt); + return ret; +} + uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) { QemuOpt *opt = qemu_opt_find(opts, name); @@ -619,6 +660,28 @@ 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) +{ + QemuOpt *opt = qemu_opt_find(opts, name); + const QemuOptDesc *desc; + Error *local_err = NULL; + uint64_t ret = defval; + + 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, &ret, &local_err); + assert(!local_err); + } + return ret; + } + assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); + ret = opt->value.uint; + qemu_opt_del(opt); + return ret; +} + static void qemu_opt_parse(QemuOpt *opt, Error **errp) { if (opt->desc == NULL) @@ -656,7 +719,7 @@ static bool opts_accepts_any(const QemuOpts *opts) } static void opt_set(QemuOpts *opts, const char *name, const char *value, - bool prepend, Error **errp) + bool prepend, bool replace, Error **errp) { QemuOpt *opt; const QemuOptDesc *desc; @@ -668,6 +731,11 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value, return; } + opt = qemu_opt_find(opts, name); + if (replace && opt) { + qemu_opt_del(opt); + } + opt = g_malloc0(sizeof(*opt)); opt->name = g_strdup(name); opt->opts = opts; @@ -689,7 +757,25 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) { Error *local_err = NULL; - opt_set(opts, name, value, false, &local_err); + opt_set(opts, name, value, false, false, &local_err); + if (error_is_set(&local_err)) { + qerror_report_err(local_err); + error_free(local_err); + return -1; + } + + return 0; +} + +/* + * If name exists, the function will delete the opt first and then add a new + * one. + */ +int qemu_opt_replace_set(QemuOpts *opts, const char *name, const char *value) +{ + Error *local_err = NULL; + + opt_set(opts, name, value, false, true, &local_err); if (error_is_set(&local_err)) { qerror_report_err(local_err); error_free(local_err); @@ -702,7 +788,7 @@ 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, Error **errp) { - opt_set(opts, name, value, false, errp); + opt_set(opts, name, value, false, false, errp); } int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) @@ -749,6 +835,16 @@ int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val) return 0; } +int qemu_opt_replace_set_number(QemuOpts *opts, const char *name, int64_t val) +{ + QemuOpt *opt = qemu_opt_find(opts, name); + + if (opt) { + qemu_opt_del(opt); + } + return qemu_opt_set_number(opts, name, val); +} + int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, int abort_on_failure) { @@ -926,7 +1022,7 @@ int qemu_opts_print(QemuOpts *opts) } static int opts_do_parse(QemuOpts *opts, const char *params, - const char *firstname, bool prepend) + const char *firstname, bool prepend, bool replace) { char option[128], value[1024]; const char *p,*pe,*pc; @@ -962,7 +1058,7 @@ static int opts_do_parse(QemuOpts *opts, const char *params, } if (strcmp(option, "id") != 0) { /* store and parse */ - opt_set(opts, option, value, prepend, &local_err); + opt_set(opts, option, value, prepend, replace, &local_err); if (error_is_set(&local_err)) { qerror_report_err(local_err); error_free(local_err); @@ -978,7 +1074,13 @@ static int opts_do_parse(QemuOpts *opts, const char *params, int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) { - return opts_do_parse(opts, params, firstname, false); + return opts_do_parse(opts, params, firstname, false, false); +} + +int qemu_opts_do_parse_replace(QemuOpts *opts, const char *params, + const char *firstname) +{ + return opts_do_parse(opts, params, firstname, false, true); } static QemuOpts *opts_parse(QemuOptsList *list, const char *params, @@ -1017,7 +1119,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params, return NULL; } - if (opts_do_parse(opts, params, firstname, defaults) != 0) { + if (opts_do_parse(opts, params, firstname, defaults, false) != 0) { qemu_opts_del(opts); return NULL; }