From patchwork Thu Sep 27 05:14:20 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 187273 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 74D352C0094 for ; Thu, 27 Sep 2012 15:14:28 +1000 (EST) Received: from localhost ([::1]:33643 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TH6Qc-0007EI-DX for incoming@patchwork.ozlabs.org; Thu, 27 Sep 2012 01:14:26 -0400 Received: from eggs.gnu.org ([208.118.235.92]:55177) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TH6QO-00076C-9G for qemu-devel@nongnu.org; Thu, 27 Sep 2012 01:14:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TH6QN-0003Y4-5I for qemu-devel@nongnu.org; Thu, 27 Sep 2012 01:14:12 -0400 Received: from mail-oa0-f45.google.com ([209.85.219.45]:37197) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TH6QN-0003X6-0K for qemu-devel@nongnu.org; Thu, 27 Sep 2012 01:14:11 -0400 Received: by mail-oa0-f45.google.com with SMTP id i18so1324797oag.4 for ; Wed, 26 Sep 2012 22:14:10 -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=TnM9z0V90Ywt0nXOSMKn5bjOY3TEX/vIEiYnjKdXtfI=; b=weYd96q7j/08RZQMZut649FtK3e+KXLom7hodZx4Ky4us4Sii+OJ0RfdQjdZoZasEq SIJzif6cHlTbKnmR0q77RspUK+KDvlAqeTLxbRwmhW6U+K1JbwOwdXgpc2MzV+M7TsnO mGeeMLs/IKgOwEjCHE4ShjG/BLq3w0T2wL2XvwoKoDPFMRt/H6livJ76zwxsO88q5qmh kXAzNF6dQBhfauZmoEjYLDhGv20FHaRSF5gXAF95Vu3aHuw6XDfF3BUsVJTrZpiEcY3D DoPAFx8ZWQvkjCYJy0b5qu3PVF8L10GsApAWaWMovRX5zpfJXx5ucLiXliC6oTge4h5K bbow== Received: by 10.60.170.110 with SMTP id al14mr2161357oec.120.1348722850812; Wed, 26 Sep 2012 22:14:10 -0700 (PDT) Received: from localhost.localdomain ([202.108.130.138]) by mx.google.com with ESMTPS id q6sm3833954oec.7.2012.09.26.22.14.08 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 26 Sep 2012 22:14:10 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Thu, 27 Sep 2012 13:14:20 +0800 Message-Id: <1348722865-20564-3-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1348722865-20564-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1348722865-20564-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.219.45 Cc: kwolf@redhat.com, Dong Xu Wang , armbru@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [RFC v2 2/7] qemu-option: opt_set(): split it up into more functions 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 The new functions are opts_accepts_any() and find_desc_by_name(), which are also going to be used by qemu_opts_validate() (see next commit). This also makes opt_set() slightly more readable. Signed-off-by: Luiz Capitulino Signed-off-by: Dong Xu Wang --- qemu-option.c | 40 ++++++++++++++++++++++++---------------- 1 files changed, 24 insertions(+), 16 deletions(-) diff --git a/qemu-option.c b/qemu-option.c index 0b81e77..d8b0988 100644 --- a/qemu-option.c +++ b/qemu-option.c @@ -602,26 +602,36 @@ static void qemu_opt_del(QemuOpt *opt) g_free(opt); } -static void opt_set(QemuOpts *opts, const char *name, const char *value, - bool prepend, Error **errp) +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) { - QemuOpt *opt; - const QemuOptDesc *desc = opts->list->desc; - Error *local_err = NULL; int i; for (i = 0; desc[i].name != NULL; i++) { if (strcmp(desc[i].name, name) == 0) { - break; + return &desc[i]; } } - if (desc[i].name == NULL) { - if (i == 0) { - /* empty list -> allow any */; - } else { - error_set(errp, QERR_INVALID_PARAMETER, name); - return; - } + + return NULL; +} + +static void opt_set(QemuOpts *opts, const char *name, const char *value, + bool prepend, Error **errp) +{ + QemuOpt *opt; + const QemuOptDesc *desc; + Error *local_err = NULL; + + desc = find_desc_by_name(opts->list->desc, name); + if (!desc && !opts_accepts_any(opts)) { + error_set(errp, QERR_INVALID_PARAMETER, name); + return; } opt = g_malloc0(sizeof(*opt)); @@ -632,9 +642,7 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value, } else { QTAILQ_INSERT_TAIL(&opts->head, opt, next); } - if (desc[i].name != NULL) { - opt->desc = desc+i; - } + opt->desc = desc; if (value) { opt->str = g_strdup(value); }