From nobody Mon Feb 9 03:13:16 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1493757187123421.5329057805577; Tue, 2 May 2017 13:33:07 -0700 (PDT) Received: from localhost ([::1]:33560 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5eTp-0005iw-Kv for importer@patchew.org; Tue, 02 May 2017 16:33:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49984) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5eSC-0004iK-5u for qemu-devel@nongnu.org; Tue, 02 May 2017 16:31:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5eSA-000526-PP for qemu-devel@nongnu.org; Tue, 02 May 2017 16:31:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50832) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d5eSA-00051N-G3 for qemu-devel@nongnu.org; Tue, 02 May 2017 16:31:22 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 70B477B2C3; Tue, 2 May 2017 20:31:21 +0000 (UTC) Received: from localhost (ovpn-116-8.gru2.redhat.com [10.97.116.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id F09C21714F; Tue, 2 May 2017 20:31:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 70B477B2C3 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 70B477B2C3 From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Tue, 2 May 2017 17:31:13 -0300 Message-Id: <20170502203115.22233-3-ehabkost@redhat.com> In-Reply-To: <20170502203115.22233-1-ehabkost@redhat.com> References: <20170502203115.22233-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 02 May 2017 20:31:21 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/4] string-input-visitor: Support alternate types X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , Alexander Graf , Markus Armbruster , Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When parsing alternates from a string, there are some limitations in what we can do, but it is a valid use case in some situations. We can support booleans, integer types, and enums. This will be used to support 'feature=3Dforce' in -cpu options, while keeping 'feature=3Don|off|true|false' represented as boolean values. Signed-off-by: Eduardo Habkost --- qapi/string-input-visitor.c | 71 ++++++++++++++++++++++---- tests/test-string-input-visitor.c | 89 +++++++++++++++++++++++++++++= ++++ tests/qapi-schema/qapi-schema-test.json | 8 +++ 3 files changed, 158 insertions(+), 10 deletions(-) diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index c089491c24..bf8f58748b 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -19,6 +19,7 @@ #include "qemu/option.h" #include "qemu/queue.h" #include "qemu/range.h" +#include "qemu/bitops.h" =20 =20 struct StringInputVisitor @@ -278,21 +279,34 @@ static void parse_type_size(Visitor *v, const char *n= ame, uint64_t *obj, *obj =3D val; } =20 +static int try_parse_bool(const char *s, bool *result) +{ + if (!strcasecmp(s, "on") || + !strcasecmp(s, "yes") || + !strcasecmp(s, "true")) { + if (result) { + *result =3D true; + } + return 0; + } + if (!strcasecmp(s, "off") || + !strcasecmp(s, "no") || + !strcasecmp(s, "false")) { + if (result) { + *result =3D false; + } + return 0; + } + + return -1; +} + static void parse_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) { StringInputVisitor *siv =3D to_siv(v); =20 - if (!strcasecmp(siv->string, "on") || - !strcasecmp(siv->string, "yes") || - !strcasecmp(siv->string, "true")) { - *obj =3D true; - return; - } - if (!strcasecmp(siv->string, "off") || - !strcasecmp(siv->string, "no") || - !strcasecmp(siv->string, "false")) { - *obj =3D false; + if (try_parse_bool(siv->string, obj) =3D=3D 0) { return; } =20 @@ -326,6 +340,42 @@ static void parse_type_number(Visitor *v, const char *= name, double *obj, *obj =3D val; } =20 +/* Support for alternates on string-input-visitor is limited, because + * the input string doesn't have any type information. + * + * Supported alternate member types: + * 1) enums + * 2) integer types + * 3) booleans (but only if the there's no enum variant + * containing "on", "off", "true", or "false" as members) + * + * UNSUPPORTED alternate member types: + * 1) strings + * 2) complex types + */ +static void start_alternate(Visitor *v, const char *name, + GenericAlternate **obj, size_t size, + unsigned long supported_qtypes, Error **errp) +{ + StringInputVisitor *siv =3D to_siv(v); + QType t =3D QTYPE_QSTRING; + + if (supported_qtypes & BIT(QTYPE_QBOOL)) { + if (try_parse_bool(siv->string, NULL) =3D=3D 0) { + t =3D QTYPE_QBOOL; + } + } + + if (supported_qtypes & BIT(QTYPE_QINT)) { + if (parse_str(siv, name, NULL) =3D=3D 0) { + t =3D QTYPE_QINT; + } + } + + *obj =3D g_malloc0(size); + (*obj)->type =3D t; +} + static void string_input_free(Visitor *v) { StringInputVisitor *siv =3D to_siv(v); @@ -353,6 +403,7 @@ Visitor *string_input_visitor_new(const char *str) v->visitor.next_list =3D next_list; v->visitor.check_list =3D check_list; v->visitor.end_list =3D end_list; + v->visitor.start_alternate =3D start_alternate; v->visitor.free =3D string_input_free; =20 v->string =3D str; diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 79313a7f7a..1e867d62c9 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -290,6 +290,91 @@ static void test_visitor_in_enum(TestInputVisitorData = *data, } } =20 +static void test_visitor_in_alt_bool_enum(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltBoolEnum *be =3D NULL; + + v =3D visitor_input_test_init(data, "true"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + g_assert(!err); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QBOOL); + g_assert(be->u.b); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "off"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + g_assert(!err); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QBOOL); + g_assert(!be->u.b); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "value2"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + g_assert(!err); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QSTRING); + g_assert_cmpint(be->u.e, =3D=3D, ENUM_ONE_VALUE2); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "value100"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "10"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltBoolEnum(be); +} + +static void test_visitor_in_alt_enum_int(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltOnOffInt *be =3D NULL; + + v =3D visitor_input_test_init(data, "on"); + visit_type_AltOnOffInt(v, NULL, &be, &err); + g_assert(!err); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QSTRING); + g_assert_cmpint(be->u.o, =3D=3D, TEST_ON_OFF_AUTO_ON); + qapi_free_AltOnOffInt(be); + + v =3D visitor_input_test_init(data, "off"); + visit_type_AltOnOffInt(v, NULL, &be, &err); + g_assert(!err); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QSTRING); + g_assert_cmpint(be->u.o, =3D=3D, TEST_ON_OFF_AUTO_OFF); + qapi_free_AltOnOffInt(be); + + v =3D visitor_input_test_init(data, "auto"); + visit_type_AltOnOffInt(v, NULL, &be, &err); + g_assert(!err); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QSTRING); + g_assert_cmpint(be->u.o, =3D=3D, TEST_ON_OFF_AUTO_AUTO); + qapi_free_AltOnOffInt(be); + + v =3D visitor_input_test_init(data, "-12345"); + visit_type_AltOnOffInt(v, NULL, &be, &err); + g_assert(!err); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QINT); + g_assert_cmpint(be->u.i, =3D=3D, -12345); + qapi_free_AltOnOffInt(be); + + v =3D visitor_input_test_init(data, "true"); + visit_type_AltOnOffInt(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltOnOffInt(be); + + v =3D visitor_input_test_init(data, "value2"); + visit_type_AltOnOffInt(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltOnOffInt(be); +} + /* Try to crash the visitors */ static void test_visitor_in_fuzz(TestInputVisitorData *data, const void *unused) @@ -366,6 +451,10 @@ int main(int argc, char **argv) &in_visitor_data, test_visitor_in_string); input_visitor_test_add("/string-visitor/input/enum", &in_visitor_data, test_visitor_in_enum); + input_visitor_test_add("/string-visitor/input/alternate/bool_enum", + &in_visitor_data, test_visitor_in_alt_bool_enu= m); + input_visitor_test_add("/string-visitor/input/alternate/enum_int", + &in_visitor_data, test_visitor_in_alt_enum_int= ); input_visitor_test_add("/string-visitor/input/fuzz", &in_visitor_data, test_visitor_in_fuzz); =20 diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qa= pi-schema-test.json index 842ea3c5e3..231c8952e8 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -106,6 +106,14 @@ { 'alternate': 'AltIntNum', 'data': { 'i': 'int', 'n': 'number' } } { 'alternate': 'AltNumInt', 'data': { 'n': 'number', 'i': 'int' } } =20 + +# for testing string-input-visitor handling of alternates: +{ 'enum': 'TestOnOffAuto', + 'data': [ 'on', 'off', 'auto' ] } + +{ 'alternate': 'AltBoolEnum', 'data': { 'b': 'bool', 'e': 'EnumOne' } } +{ 'alternate': 'AltOnOffInt', 'data': { 'o': 'TestOnOffAuto', 'i': 'int' }= } + # for testing native lists { 'union': 'UserDefNativeListUnion', 'data': { 'integer': ['int'], --=20 2.11.0.259.g40922b1