From nobody Mon Feb 9 11:32:14 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 1488317487220545.8332021717117; Tue, 28 Feb 2017 13:31:27 -0800 (PST) Received: from localhost ([::1]:37068 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cipMi-0001jq-Pz for importer@patchew.org; Tue, 28 Feb 2017 16:31:25 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36336) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cipIu-0007aU-6I for qemu-devel@nongnu.org; Tue, 28 Feb 2017 16:27:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cipIs-0000zI-AN for qemu-devel@nongnu.org; Tue, 28 Feb 2017 16:27:28 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52840) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cipIk-0000ri-OV; Tue, 28 Feb 2017 16:27:18 -0500 Received: from smtp.corp.redhat.com (int-mx16.intmail.prod.int.phx2.redhat.com [10.5.11.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CD0403B74F; Tue, 28 Feb 2017 21:27:18 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-55.ams2.redhat.com [10.36.116.55]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 47D1A30665; Tue, 28 Feb 2017 21:27:18 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 6FB6C1138616; Tue, 28 Feb 2017 22:27:10 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 28 Feb 2017 22:27:04 +0100 Message-Id: <1488317230-26248-19-git-send-email-armbru@redhat.com> In-Reply-To: <1488317230-26248-1-git-send-email-armbru@redhat.com> References: <1488317230-26248-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.74 on 10.5.11.28 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 28 Feb 2017 21:27:18 +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 v2 18/24] qapi: New parse_qapi_name() 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: kwolf@redhat.com, pkrempa@redhat.com, mdroth@linux.vnet.ibm.com, qemu-block@nongnu.org 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" Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- include/qapi/util.h | 2 ++ qapi/qapi-util.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test-qapi-util.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/include/qapi/util.h b/include/qapi/util.h index 7ad26c0..7436ed8 100644 --- a/include/qapi/util.h +++ b/include/qapi/util.h @@ -14,4 +14,6 @@ int qapi_enum_parse(const char * const lookup[], const char *buf, int max, int def, Error **errp); =20 +int parse_qapi_name(const char *name, bool complete); + #endif diff --git a/qapi/qapi-util.c b/qapi/qapi-util.c index 818730a..e28dbd0 100644 --- a/qapi/qapi-util.c +++ b/qapi/qapi-util.c @@ -33,3 +33,50 @@ int qapi_enum_parse(const char * const lookup[], const c= har *buf, error_setg(errp, "invalid parameter value: %s", buf); return def; } + +/* + * Parse a valid QAPI name from @str. + * A valid name consists of letters, digits, hyphen and underscore. + * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN + * may contain only letters, digits, hyphen and period. + * The special exception for enumeration names is not implemented. + * See docs/qapi-code-gen.txt for more on QAPI naming rules. + * Keep this consistent with scripts/qapi.py! + * If @complete, the parse fails unless it consumes @str completely. + * Return its length on success, -1 on failure. + */ +int parse_qapi_name(const char *str, bool complete) +{ + const char *p =3D str; + + if (*p =3D=3D '_') { /* Downstream __RFQDN_ */ + p++; + if (*p !=3D '_') { + return -1; + } + while (*++p) { + if (!qemu_isalnum(*p) && *p !=3D '-' && *p !=3D '.') { + break; + } + } + + if (*p !=3D '_') { + return -1; + } + p++; + } + + if (!qemu_isalpha(*p)) { + return -1; + } + while (*++p) { + if (!qemu_isalnum(*p) && *p !=3D '-' && *p !=3D '_') { + break; + } + } + + if (complete && *p) { + return -1; + } + return p - str; +} diff --git a/tests/test-qapi-util.c b/tests/test-qapi-util.c index 39db8bf..e869757 100644 --- a/tests/test-qapi-util.c +++ b/tests/test-qapi-util.c @@ -42,10 +42,44 @@ static void test_qapi_enum_parse(void) g_assert_cmpint(ret, =3D=3D, QTYPE__MAX - 1); } =20 +static void test_parse_qapi_name(void) +{ + int ret; + + /* Must start with a letter */ + ret =3D parse_qapi_name("a", true); + g_assert(ret =3D=3D 1); + ret =3D parse_qapi_name("a$", false); + g_assert(ret =3D=3D 1); + ret =3D parse_qapi_name("", false); + g_assert(ret =3D=3D -1); + ret =3D parse_qapi_name("1", false); + g_assert(ret =3D=3D -1); + + /* Only letters, digits, hyphen, underscore */ + ret =3D parse_qapi_name("A-Za-z0-9_", true); + g_assert(ret =3D=3D 10); + ret =3D parse_qapi_name("A-Za-z0-9_$", false); + g_assert(ret =3D=3D 10); + ret =3D parse_qapi_name("A-Za-z0-9_$", true); + g_assert(ret =3D=3D -1); + + /* __RFQDN_ */ + ret =3D parse_qapi_name("__com.redhat_supports", true); + g_assert(ret =3D=3D 21); + ret =3D parse_qapi_name("_com.example_", false); + g_assert(ret =3D=3D -1); + ret =3D parse_qapi_name("__com.example", false); + g_assert(ret =3D=3D -1); + ret =3D parse_qapi_name("__com.example_", false); + g_assert(ret =3D=3D -1); +} + int main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); g_test_add_func("/qapi/util/qapi_enum_parse", test_qapi_enum_parse); + g_test_add_func("/qapi/util/parse_qapi_name", test_parse_qapi_name); g_test_run(); return 0; } --=20 2.7.4