From patchwork Thu Dec 6 06:47:18 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 204147 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 603212C0189 for ; Thu, 6 Dec 2012 17:48:05 +1100 (EST) Received: from localhost ([::1]:53572 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TgVFb-00063r-71 for incoming@patchwork.ozlabs.org; Thu, 06 Dec 2012 01:48:03 -0500 Received: from eggs.gnu.org ([208.118.235.92]:59462) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TgVFP-00063f-JK for qemu-devel@nongnu.org; Thu, 06 Dec 2012 01:47:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TgVFL-0005Ij-29 for qemu-devel@nongnu.org; Thu, 06 Dec 2012 01:47:51 -0500 Received: from mail-ie0-f177.google.com ([209.85.223.177]:47360) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TgVFK-0005Ie-TF for qemu-devel@nongnu.org; Thu, 06 Dec 2012 01:47:46 -0500 Received: by mail-ie0-f177.google.com with SMTP id k13so9524959iea.36 for ; Wed, 05 Dec 2012 22:47:46 -0800 (PST) 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=CmwBGKXBZi9lVIKJWa3M4XeXuUU1MVg1VzNAqJvCP4teMmAgC0J4HB4iASlOxf/N7X 0z59PfvoX7SgNeuVMbpZIpCyJcbcbnC44JsOwIWKWC0On9Pxiblt6mytmcBnMPHRXeRM 9xxwfsL/QRLFdZlXndZuXzR825OFSq5EcTNnmtBeAvDVE8lKgLbiy8UWCVAk+OcLLb3r B/8z/siLORVtF9rKfv4Ugvtn/CjWqM90iJf0qEPSQJ2rQXyAKxcsAKTauFEuXdfm01Bp 41H9V07cuWz/3oHCwEbsVNqpXimNZAKZTOOD3fDraTUBDZAIfZPtt8GUi2KkL+B3juyU d5Jw== Received: by 10.50.42.170 with SMTP id p10mr4778704igl.47.1354776466326; Wed, 05 Dec 2012 22:47:46 -0800 (PST) Received: from localhost.localdomain ([202.108.130.153]) by mx.google.com with ESMTPS id kp4sm6494017igc.1.2012.12.05.22.47.43 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 05 Dec 2012 22:47:45 -0800 (PST) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Thu, 6 Dec 2012 14:47:18 +0800 Message-Id: <1354776447-12041-2-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1354776447-12041-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1354776447-12041-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.223.177 Cc: kwolf@redhat.com, Dong Xu Wang , Luiz Capitulino Subject: [Qemu-devel] [PATCH V7 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); }