From: Ian Campbell Subject: Re: [PATCH 2/9] libxl: disks: new xlu_disk_parse function Date: Fri, 3 Jun 2011 15:06:38 +0100 Message-ID: <1307109998.775.398.camel@zakaz.uk.xensource.com> References: <1307037346-31251-1-git-send-email-ian.jackson@eu.citrix.com> <1307037346-31251-2-git-send-email-ian.jackson@eu.citrix.com> <1307037346-31251-3-git-send-email-ian.jackson@eu.citrix.com> <1307095702.775.343.camel@zakaz.uk.xensource.com> <19944.49437.818111.279525@mariner.uk.xensource.com> <1307102620.775.373.camel@zakaz.uk.xensource.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-Jd17YZ0T5FGP/4BcNbzc" Return-path: In-Reply-To: <1307102620.775.373.camel@zakaz.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Ian Jackson Cc: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org --=-Jd17YZ0T5FGP/4BcNbzc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit On Fri, 2011-06-03 at 13:03 +0100, Ian Campbell wrote: > On Fri, 2011-06-03 at 12:10 +0100, Ian Jackson wrote: > > This is a bit mad, isn't it ? I mean, we could have a > > non-autogenerated "lookup string from table" function and only > > autogenerate the tables. I think that's a nicer interface for > > callers, too - for example, they can print a list of options if they > > want to, etc. > > Yes, I agree. I ended up providing the table and a lookup function (a wrapper around a common private implementation). I even tested this version ;-) (skanky testprog generator attached, patch not for application, avoid contact with eyes, etc) 8<-------------------------------------------------------------------- # HG changeset patch # User Ian Campbell # Date 1307109900 -3600 # Node ID d033fc8122931d13e4921b9a299f4c90f1146757 # Parent bd110eb1481a650db3406bf2da80c76b1bfd7e7d libxl: autogenerate to_string and from_string functions for Enumerations. The generated strings are the lower case enum value names, with underscores. Accepted string for parsing are the same but are case insensitive. We provide a table of strings->value for each Enumeration as well as a convenience function to perform a lookup. Signed-off-by: Ian Campbell diff -r bd110eb1481a -r d033fc812293 tools/libxl/gentypes.py --- a/tools/libxl/gentypes.py Fri Jun 03 10:22:22 2011 +0100 +++ b/tools/libxl/gentypes.py Fri Jun 03 15:05:00 2011 +0100 @@ -113,6 +113,43 @@ def libxl_C_type_destroy(ty, v, indent = s = indent + s return s.replace("\n", "\n%s" % indent).rstrip(indent) +def libxl_C_enum_to_string(ty, e, indent = " "): + s = "" + s += "switch(%s) {\n" % e + for v in ty.values: + s += " case %s:\n" % (v.name) + s += " return \"%s\";\n" % (v.valuename.lower()) + s += " default:\n " + s += " return NULL;\n" + s += "}\n" + + if s != "": + s = indent + s + return s.replace("\n", "\n%s" % indent).rstrip(indent) + +def libxl_C_enum_strings(ty, indent=""): + s = "" + s += "libxl_enum_string_table %s_string_table[] = {\n" % (ty.typename) + for v in ty.values: + s += " { .s = \"%s\", .v = %s },\n" % (v.valuename.lower(), v.name) + s += " { NULL, -1 },\n" + s += "};\n" + s += "\n" + + if s != "": + s = indent + s + return s.replace("\n", "\n%s" % indent).rstrip(indent) + +def libxl_C_enum_from_string(ty, str, e, indent = " "): + s = "" + s += "return libxl__enum_from_string(%s_string_table,\n" % ty.typename + s += " %s, (int *)%s);\n" % (str, e) + + if s != "": + s = indent + s + return s.replace("\n", "\n%s" % indent).rstrip(indent) + + if __name__ == '__main__': if len(sys.argv) < 4: print >>sys.stderr, "Usage: gentypes.py
" @@ -142,6 +179,10 @@ if __name__ == '__main__': f.write(libxl_C_type_define(ty) + ";\n") if ty.destructor_fn is not None: f.write("void %s(%s *p);\n" % (ty.destructor_fn, ty.typename)) + if isinstance(ty, libxltypes.Enumeration): + f.write("const char *%s_to_string(%s e);\n" % (ty.typename, ty.typename)) + f.write("int %s_from_string(const char *s, %s *e);\n" % (ty.typename, ty.typename)) + f.write("extern libxl_enum_string_table %s_string_table[];\n" % (ty.typename)) f.write("\n") f.write("""#endif /* __LIBXL_TYPES_H */\n""") @@ -165,6 +206,7 @@ if __name__ == '__main__': #include #include "libxl.h" +#include "libxl_internal.h" #define LIBXL_DTOR_POISON 0xa5 @@ -177,4 +219,21 @@ if __name__ == '__main__': f.write(" memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n") f.write("}\n") f.write("\n") + + for ty in [t for t in types if isinstance(t,libxltypes.Enumeration)]: + f.write("const char *%s_to_string(%s e)\n" % (ty.typename, ty.typename)) + f.write("{\n") + f.write(libxl_C_enum_to_string(ty, "e")) + f.write("}\n") + f.write("\n") + + f.write(libxl_C_enum_strings(ty)) + + f.write("int %s_from_string(const char *s, %s *e)\n" % (ty.typename, ty.typename)) + f.write("{\n") + f.write(libxl_C_enum_from_string(ty, "s", "e")) + f.write("}\n") + f.write("\n") + + f.close() diff -r bd110eb1481a -r d033fc812293 tools/libxl/libxl.h --- a/tools/libxl/libxl.h Fri Jun 03 10:22:22 2011 +0100 +++ b/tools/libxl/libxl.h Fri Jun 03 15:05:00 2011 +0100 @@ -185,6 +185,17 @@ void libxl_cpuid_destroy(libxl_cpuid_pol typedef uint32_t libxl_domid; +/* + * Formatting Enumerations. + * + * Each enumeration type libxl_E declares an associated lookup table + * libxl_E_string_table and a lookup function libxl_E_from_string. + */ +typedef struct { + const char *s; + int v; +} libxl_enum_string_table; + #include "_libxl_types.h" typedef struct libxl__ctx libxl_ctx; diff -r bd110eb1481a -r d033fc812293 tools/libxl/libxl_internal.h --- a/tools/libxl/libxl_internal.h Fri Jun 03 10:22:22 2011 +0100 +++ b/tools/libxl/libxl_internal.h Fri Jun 03 15:05:00 2011 +0100 @@ -323,6 +323,8 @@ _hidden char *libxl__abs_path(libxl__gc _hidden char *libxl__domid_to_name(libxl__gc *gc, uint32_t domid); _hidden char *libxl__cpupoolid_to_name(libxl__gc *gc, uint32_t poolid); +_hidden int libxl__enum_from_string(const libxl_enum_string_table *t, + const char *s, int *e); /* holds the CPUID response for a single CPUID leaf * input contains the value of the EAX and ECX register, diff -r bd110eb1481a -r d033fc812293 tools/libxl/libxl_utils.c --- a/tools/libxl/libxl_utils.c Fri Jun 03 10:22:22 2011 +0100 +++ b/tools/libxl/libxl_utils.c Fri Jun 03 15:05:00 2011 +0100 @@ -639,3 +639,17 @@ int libxl_get_max_cpus(libxl_ctx *ctx) { return xc_get_max_cpus(ctx->xch); } + +int libxl__enum_from_string(const libxl_enum_string_table *t, + const char *s, int *e) +{ + if (!t) return ERROR_INVAL; + + for( ; t->s; t++) { + if (!strcasecmp(t->s, s)) { + *e = t->v; + return 0; + } + } + return ERROR_FAIL; +} --=-Jd17YZ0T5FGP/4BcNbzc Content-Disposition: attachment; filename="enumtest.patch" Content-Type: text/x-patch; name="enumtest.patch"; charset="UTF-8" Content-Transfer-Encoding: 7bit # HG changeset patch # User Ian Campbell # Date 1307109705 -3600 # Node ID b433d9d9716fa8deccb565bc822703817768d588 # Parent 03f8d29478ef61137d318ccc3d37d938828d2bcd imported patch libxl-debug-test-enum-strings.patch diff -r 03f8d29478ef -r b433d9d9716f tools/libxl/Makefile --- a/tools/libxl/Makefile Fri Jun 03 14:59:31 2011 +0100 +++ b/tools/libxl/Makefile Fri Jun 03 15:01:45 2011 +0100 @@ -44,12 +44,17 @@ AUTOSRCS= libxlu_cfg_y.c libxlu_cfg_l.c LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o $(LIBXLU_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h -CLIENTS = xl +CLIENTS = xl testenum XL_OBJS = xl.o xl_cmdimpl.o xl_cmdtable.o $(XL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h $(XL_OBJS): CFLAGS += $(CFLAGS_libxenlight) +testenum.o: CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenlight) +testenum.c: libxl.idl gentest.py + $(PYTHON) gentest.py libxl.idl testenum.c.new + mv testenum.c.new testenum.c + .PHONY: all all: $(CLIENTS) libxenlight.so libxenlight.a libxlutil.so libxlutil.a \ $(AUTOSRCS) $(AUTOINCS) @@ -106,9 +111,12 @@ libxlutil.so.$(XLUMAJOR).$(XLUMINOR): $( libxlutil.a: $(LIBXLU_OBJS) $(AR) rcs libxlutil.a $^ -$(CLIENTS): $(XL_OBJS) libxlutil.so libxenlight.so +xl: $(XL_OBJS) libxlutil.so libxenlight.so $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl) +testenum: testenum.o libxlutil.so libxenlight.so + $(CC) $(LDFLAGS) -o $@ testenum.o libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl) + .PHONY: install install: all $(INSTALL_DIR) $(DESTDIR)$(SBINDIR) diff -r 03f8d29478ef -r b433d9d9716f tools/libxl/gentest.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/libxl/gentest.py Fri Jun 03 15:01:45 2011 +0100 @@ -0,0 +1,63 @@ +#!/usr/bin/python + +import sys +import re +import random + +import libxltypes +def randomize_char(c): + if random.random() < 0.5: + return str.lower(c) + else: + return str.upper(c) + +def randomize_case(s): + r = [randomize_char(c) for c in s] + return "".join(r) + +if __name__ == '__main__': + if len(sys.argv) < 3: + print >>sys.stderr, "Usage: gentest.py " + sys.exit(1) + + random.seed() + + idl = sys.argv[1] + (_,types) = libxltypes.parse(idl) + + impl = sys.argv[2] + f = open(impl, "w") + f.write(""" +#include +#include \"libxl.h\" + +int main(int argc, char **argv) +{ +""") + + for ty in [t for t in types if isinstance(t,libxltypes.Enumeration)]: + f.write(" %s %s_val;\n" % (ty.typename, ty.typename)) + f.write(" int rc;\n") + f.write("\n") + + for ty in [t for t in types if isinstance(t,libxltypes.Enumeration)]: + f.write(" printf(\"%s -- to string:\\n\");\n" % (ty.typename)) + for v in ty.values: + f.write(" printf(\"\\t%s = %%d = \\\"%%s\\\"\\n\", %s, %s_to_string(%s));\n" %\ + (v.valuename, v.name, ty.typename, v.name)) + f.write("\n") + + f.write(" printf(\"%s -- from string:\\n\");\n" % (ty.typename)) + for v in [v.valuename for v in ty.values] + ["AN INVALID VALUE"]: + n = randomize_case(v) + f.write(" %s_val = -1;\n" % (ty.typename)) + f.write(" rc = %s_from_string(\"%s\", &%s_val);\n" %\ + (ty.typename, n, ty.typename)) + + f.write(" printf(\"\\t%s = \\\"%%s\\\" = %%d (rc %%d)\\n\", \"%s\", %s_val, rc);\n" %\ + (v, n, ty.typename)) + f.write("\n") + + f.write("""return 0; +} +""") --=-Jd17YZ0T5FGP/4BcNbzc Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=-Jd17YZ0T5FGP/4BcNbzc--