From patchwork Fri Jul 13 20:44:15 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 170964 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 73D8A2C0314 for ; Sat, 14 Jul 2012 06:54:49 +1000 (EST) Received: from localhost ([::1]:48408 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Spmj7-0004eR-Ot for incoming@patchwork.ozlabs.org; Fri, 13 Jul 2012 16:44:37 -0400 Received: from eggs.gnu.org ([208.118.235.92]:46874) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SpmiU-0002lX-7r for qemu-devel@nongnu.org; Fri, 13 Jul 2012 16:43:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SpmiT-0001rX-Ai for qemu-devel@nongnu.org; Fri, 13 Jul 2012 16:43:58 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38738) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SpmiT-0001rP-1k for qemu-devel@nongnu.org; Fri, 13 Jul 2012 16:43:57 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q6DKhu0B001506 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 13 Jul 2012 16:43:56 -0400 Received: from localhost ([10.3.113.3]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q6DKhtTj017348; Fri, 13 Jul 2012 16:43:55 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Fri, 13 Jul 2012 17:44:15 -0300 Message-Id: <1342212261-19903-3-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1342212261-19903-1-git-send-email-lcapitulino@redhat.com> References: <1342212261-19903-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: jan.kiszka@siemens.com, aliguori@us.ibm.com, afaerber@suse.de, armbru@redhat.com Subject: [Qemu-devel] [PATCH 2/8] 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. --- qemu-option.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/qemu-option.c b/qemu-option.c index 2cb2835..d67e10f 100644 --- a/qemu-option.c +++ b/qemu-option.c @@ -612,26 +612,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)); @@ -642,9 +652,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); }