From nobody Sun May 5 12:49:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1532627412529980.3910511028176; Thu, 26 Jul 2018 10:50:12 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 76FDF30832EE; Thu, 26 Jul 2018 17:50:10 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3393F6047F; Thu, 26 Jul 2018 17:50:10 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 93CCF18037ED; Thu, 26 Jul 2018 17:50:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QHo71d029332 for ; Thu, 26 Jul 2018 13:50:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1CEE31C5B0; Thu, 26 Jul 2018 17:50:07 +0000 (UTC) Received: from worklaptop.bos.redhat.com (dhcp-17-157.bos.redhat.com [10.18.17.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id 03D851C5A2; Thu, 26 Jul 2018 17:50:06 +0000 (UTC) From: Cole Robinson To: libvirt-list@redhat.com Date: Thu, 26 Jul 2018 13:49:54 -0400 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH RFC 1/2] util: Add VIR_ENUM_IMPL_LABEL X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 26 Jul 2018 17:50:11 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This allows passing in a string label describing the enum, which can be used to autogenerate error messages Signed-off-by: Cole Robinson --- src/util/virutil.c | 20 ++++++++++++++++---- src/util/virutil.h | 15 ++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/util/virutil.c b/src/util/virutil.c index a908422feb..6d23a26a74 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -444,16 +444,22 @@ virParseVersionString(const char *str, unsigned long = *version, =20 int virEnumFromString(const char *const*types, unsigned int ntypes, - const char *type) + const char *type, + const char * const label) { size_t i; if (!type) - return -1; + goto error; =20 for (i =3D 0; i < ntypes; i++) if (STREQ(types[i], type)) return i; =20 + error: + if (label) { + virReportError(VIR_ERR_INVALID_ARG, + _("Unknown '%s' value '%s'"), label, type); + } return -1; } =20 @@ -540,10 +546,16 @@ virFormatIntPretty(unsigned long long val, =20 const char *virEnumToString(const char *const*types, unsigned int ntypes, - int type) + int type, + const char * const label) { - if (type < 0 || type >=3D ntypes) + if (type < 0 || type >=3D ntypes) { + if (label) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unknown '%s' internal value %d"), label, typ= e); + } return NULL; + } =20 return types[type]; } diff --git a/src/util/virutil.h b/src/util/virutil.h index 1ba9635bd9..345c9e053d 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -76,26 +76,31 @@ char *virIndexToDiskName(int idx, const char *prefix); =20 int virEnumFromString(const char *const*types, unsigned int ntypes, - const char *type); + const char *type, + const char *errmsg); =20 const char *virEnumToString(const char *const*types, unsigned int ntypes, - int type); + int type, + const char *errmsg); =20 -# define VIR_ENUM_IMPL(name, lastVal, ...) \ +# define VIR_ENUM_IMPL_LABEL(name, label, lastVal, ...) \ static const char *const name ## TypeList[] =3D { __VA_ARGS__ }; \ verify(ARRAY_CARDINALITY(name ## TypeList) =3D=3D lastVal); \ const char *name ## TypeToString(int type) { \ return virEnumToString(name ## TypeList, \ ARRAY_CARDINALITY(name ## TypeList), \ - type); \ + type, label); \ } \ int name ## TypeFromString(const char *type) { \ return virEnumFromString(name ## TypeList, \ ARRAY_CARDINALITY(name ## TypeList), \ - type); \ + type, label); \ } =20 +# define VIR_ENUM_IMPL(name, lastVal, ...) \ + VIR_ENUM_IMPL_LABEL(name, NULL, lastVal, __VA_ARGS__) + # define VIR_ENUM_DECL(name) \ const char *name ## TypeToString(int type); \ int name ## TypeFromString(const char*type); --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun May 5 12:49:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153262741771766.90719821159712; Thu, 26 Jul 2018 10:50:17 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B25C2308214A; Thu, 26 Jul 2018 17:50:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6C40F600C5; Thu, 26 Jul 2018 17:50:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id F0B0D18037F2; Thu, 26 Jul 2018 17:50:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QHo8Eq029341 for ; Thu, 26 Jul 2018 13:50:09 -0400 Received: by smtp.corp.redhat.com (Postfix) id D9ECC1C5B0; Thu, 26 Jul 2018 17:50:08 +0000 (UTC) Received: from worklaptop.bos.redhat.com (dhcp-17-157.bos.redhat.com [10.18.17.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2653B1C5A2; Thu, 26 Jul 2018 17:50:07 +0000 (UTC) From: Cole Robinson To: libvirt-list@redhat.com Date: Thu, 26 Jul 2018 13:49:55 -0400 Message-Id: <8e2e9469e7aa25d03c95c92dec137fafc0b17f51.1532625483.git.crobinso@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH RFC 2/2] conf: Convert virDomainVirtType to VIR_ENUM_IMPL_LABEL X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 26 Jul 2018 17:50:17 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Cole Robinson --- src/conf/domain_conf.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index f94a90fbcc..eb40b7f349 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -103,7 +103,7 @@ VIR_ENUM_IMPL(virDomainTaint, VIR_DOMAIN_TAINT_LAST, "custom-dtb", "custom-ga-command"); =20 -VIR_ENUM_IMPL(virDomainVirt, VIR_DOMAIN_VIRT_LAST, +VIR_ENUM_IMPL_LABEL(virDomainVirt, "domain type", VIR_DOMAIN_VIRT_LAST, "none", "qemu", "kqemu", @@ -19141,10 +19141,7 @@ virDomainDefParseCaps(virDomainDefPtr def, goto cleanup; } if ((def->virtType =3D virDomainVirtTypeFromString(virttype)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid domain type %s"), virttype); goto cleanup; - } =20 if (!ostype) { if (def->os.bootloader) { @@ -27380,11 +27377,8 @@ virDomainDefFormatInternal(virDomainDefPtr def, VIR_DOMAIN_DEF_FORMAT_CLOCK_ADJUST, -1); =20 - if (!(type =3D virDomainVirtTypeToString(def->virtType))) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("unexpected domain type %d"), def->virtType); + if (!(type =3D virDomainVirtTypeToString(def->virtType))) goto error; - } =20 if (def->id =3D=3D -1) flags |=3D VIR_DOMAIN_DEF_FORMAT_INACTIVE; --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list