From patchwork Sun Oct 14 12:51:26 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 191347 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 47E792C0095 for ; Mon, 15 Oct 2012 00:07:50 +1100 (EST) Received: from localhost ([::1]:33040 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TNNfx-0004qo-Im for incoming@patchwork.ozlabs.org; Sun, 14 Oct 2012 08:52:13 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36073) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TNNfk-0004gd-JM for qemu-devel@nongnu.org; Sun, 14 Oct 2012 08:52:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TNNfj-0003XW-BL for qemu-devel@nongnu.org; Sun, 14 Oct 2012 08:52:00 -0400 Received: from mail-pa0-f45.google.com ([209.85.220.45]:44927) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TNNfj-0003XR-4a for qemu-devel@nongnu.org; Sun, 14 Oct 2012 08:51:59 -0400 Received: by mail-pa0-f45.google.com with SMTP id fb10so3999127pad.4 for ; Sun, 14 Oct 2012 05:51:58 -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=ydKgvce6T022I3KbSm56PTQBtHYxGuabFC9LoVaEuIFUUe0f/Y5kQ2OKDvhP34sFUt 2FMIVfVdsRQaRFh3jdzk0i0G0UrA/e4JEttkWmUvATCmL+kDJ0PXVEvv4il7L84Gbx0T Ug06mg4QHTPhxihY8zMy2lZt4v+uMWvIvpLfCtxw4eB5utefwh42OwAKo6ZaUjpsuqgE RKc3qqZK/eNS9Xn8+0xtKRWFNqe40qQNgj8bVCFIibtqx+fOVP+XzTsvrVmZLjmQ/+dJ a+Ar33p4w4E0QM9HzaCLhwVOgcy3NobYPHTek3naZxPSBAPFIai+wD3oSyrdbkekFXNi Wqiw== Received: by 10.66.85.227 with SMTP id k3mr25066992paz.79.1350219118147; Sun, 14 Oct 2012 05:51:58 -0700 (PDT) Received: from localhost.localdomain ([202.108.130.138]) by mx.google.com with ESMTPS id nt7sm7447649pbb.33.2012.10.14.05.51.55 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 14 Oct 2012 05:51:57 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Sun, 14 Oct 2012 20:51:26 +0800 Message-Id: <1350219095-3378-2-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1350219095-3378-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1350219095-3378-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.220.45 Cc: kwolf@redhat.com, Dong Xu Wang , Luiz Capitulino Subject: [Qemu-devel] [PATCH V3 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); }