From patchwork Thu Oct 25 12:57:19 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 194194 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 659EE2C00AE for ; Fri, 26 Oct 2012 01:43:07 +1100 (EST) Received: from localhost ([::1]:41273 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TRN1G-0007gJ-Cu for incoming@patchwork.ozlabs.org; Thu, 25 Oct 2012 08:58:42 -0400 Received: from eggs.gnu.org ([208.118.235.92]:44208) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TRN0i-0006OW-Va for qemu-devel@nongnu.org; Thu, 25 Oct 2012 08:58:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TRN0Z-0005Io-Ps for qemu-devel@nongnu.org; Thu, 25 Oct 2012 08:58:08 -0400 Received: from mail-oa0-f45.google.com ([209.85.219.45]:55027) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TRN0Z-0005GV-LF for qemu-devel@nongnu.org; Thu, 25 Oct 2012 08:57:59 -0400 Received: by mail-oa0-f45.google.com with SMTP id i18so1476486oag.4 for ; Thu, 25 Oct 2012 05:57:59 -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=bRClk8zJ5hcfmE021m5xmk32HNpfgx8Gl0MNGBu7q8g=; b=n1utnMoIj7UrGmVa7dGEdMfnPfSDbprVpBYHr6PznKdYOG/PXVmaQz0iiJ5rQJAgAg JYMKw8P011KfRg4ENS7HcVil3YdL2SSVZPVruOAsBR0/SeTSn3eBroArCbBfk1p1+MF0 8VOTWQiuiQlOUkyG+VlYsWLmV0ylZlHlItMYnkrGh5RBjHLcwy6l5eDKjp++/RqHVV+H lmoCPEoH+/toIzPITK0ZjOnQgbbozGiVRkvLcm/NX2bHyDT9QthsFzNG2i4n7lzWvLDK brAud2CBi22gt+3qkGTQDTHu4icerPL3aHiVd0X54DPq4WOuVMwzYFkbosaasVHceTTz BbuQ== Received: by 10.60.4.227 with SMTP id n3mr16241991oen.136.1351169879303; Thu, 25 Oct 2012 05:57:59 -0700 (PDT) Received: from localhost.localdomain ([202.108.130.138]) by mx.google.com with ESMTPS id k9sm6019404oeg.6.2012.10.25.05.57.55 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 25 Oct 2012 05:57:58 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Thu, 25 Oct 2012 20:57:19 +0800 Message-Id: <1351169848-28223-2-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1351169848-28223-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1351169848-28223-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 , Luiz Capitulino Subject: [Qemu-devel] [PATCH V4 01/10] 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 27891e7..375daaa 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); }