From patchwork Wed Oct 14 08:39:27 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 35928 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0D4C1B6F2B for ; Wed, 14 Oct 2009 19:44:10 +1100 (EST) Received: from localhost ([127.0.0.1]:36272 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MxzSp-0008Sn-4J for incoming@patchwork.ozlabs.org; Wed, 14 Oct 2009 04:44:07 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MxzOW-0006e8-6y for qemu-devel@nongnu.org; Wed, 14 Oct 2009 04:39:40 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MxzOR-0006cK-6d for qemu-devel@nongnu.org; Wed, 14 Oct 2009 04:39:39 -0400 Received: from [199.232.76.173] (port=40701 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MxzOQ-0006cA-Uv for qemu-devel@nongnu.org; Wed, 14 Oct 2009 04:39:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60981) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MxzOQ-000491-FG for qemu-devel@nongnu.org; Wed, 14 Oct 2009 04:39:34 -0400 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n9E8dXew014324 for ; Wed, 14 Oct 2009 04:39:33 -0400 Received: from zweiblum.home.kraxel.org (vpn1-5-6.ams2.redhat.com [10.36.5.6]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id n9E8dVhA018130; Wed, 14 Oct 2009 04:39:32 -0400 Received: by zweiblum.home.kraxel.org (Postfix, from userid 500) id D6EF5700FC; Wed, 14 Oct 2009 10:39:28 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Wed, 14 Oct 2009 10:39:27 +0200 Message-Id: <1255509568-10635-4-git-send-email-kraxel@redhat.com> In-Reply-To: <1255509568-10635-1-git-send-email-kraxel@redhat.com> References: <1255509568-10635-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH 3/4] QemuOpts: parse config from file. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add functions to parse QemuOpts from a git-style config file. Signed-off-by: Gerd Hoffmann --- qemu-config.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-config.h | 1 + 2 files changed, 51 insertions(+), 0 deletions(-) diff --git a/qemu-config.c b/qemu-config.c index fa236e9..1b24ec8 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -277,3 +277,53 @@ void qemu_config_write(FILE *fp) qemu_opts_foreach(data.list, config_write_opts, &data, 0); } } + +int qemu_config_parse(FILE *fp) +{ + char line[1024], group[64], id[64], arg[64], value[1024]; + QemuOptsList *list = NULL; + QemuOpts *opts = NULL; + + while (fgets(line, sizeof(line), fp) != NULL) { + if (line[0] == '\n') { + /* skip empty lines */ + continue; + } + if (line[0] == '#') { + /* comment */ + continue; + } + if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) { + /* group with id */ + list = find_list(group); + if (list == NULL) + return -1; + opts = qemu_opts_create(list, id, 1); + continue; + } + if (sscanf(line, "[%63[^]]]", group) == 1) { + /* group without id */ + list = find_list(group); + if (list == NULL) + return -1; + opts = qemu_opts_create(list, NULL, 0); + continue; + } + if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { + /* arg = value */ + if (opts == NULL) { + fprintf(stderr, "no group defined\n"); + return -1; + } + if (qemu_opt_set(opts, arg, value) != 0) { + fprintf(stderr, "failed to set \"%s\" for %s\n", + arg, group); + return -1; + } + continue; + } + fprintf(stderr, "parse error: %s\n", line); + return -1; + } + return 0; +} diff --git a/qemu-config.h b/qemu-config.h index 33fce7e..0448ab4 100644 --- a/qemu-config.h +++ b/qemu-config.h @@ -10,5 +10,6 @@ extern QemuOptsList qemu_rtc_opts; int qemu_set_option(const char *str); void qemu_config_write(FILE *fp); +int qemu_config_parse(FILE *fp); #endif /* QEMU_CONFIG_H */