From: Rusty Russell Date: Tue, 11 Nov 2014 05:25:54 +0000 (+1030) Subject: cdump: add useful tool to generate enum name string table. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=a0ab8f12ebd40760544a25e521e06c931f19fa5c cdump: add useful tool to generate enum name string table. Signed-off-by: Rusty Russell --- diff --git a/ccan/cdump/tools/.gitignore b/ccan/cdump/tools/.gitignore new file mode 100644 index 00000000..67baa867 --- /dev/null +++ b/ccan/cdump/tools/.gitignore @@ -0,0 +1 @@ +cdump-enumstr diff --git a/ccan/cdump/tools/Makefile b/ccan/cdump/tools/Makefile new file mode 100644 index 00000000..3b30d661 --- /dev/null +++ b/ccan/cdump/tools/Makefile @@ -0,0 +1,27 @@ +CCAN_OBJS := ccan-tal.o ccan-tal-str.o ccan-tal-grab_file.o ccan-cdump.o ccan-take.o ccan-list.o ccan-read_write_all.o ccan-strmap.o ccan-noerr.o +CCANDIR:=../../.. +CFLAGS := -I$(CCANDIR) -Wall + +cdump-enumstr: cdump-enumstr.o $(CCAN_OBJS) + +clean: + $(RM) cdump-enumstr.o $(CCAN_OBJS) + +ccan-tal.o: $(CCANDIR)/ccan/tal/tal.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-strmap.o: $(CCANDIR)/ccan/strmap/strmap.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-noerr.o: $(CCANDIR)/ccan/noerr/noerr.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-cdump.o: $(CCANDIR)/ccan/cdump/cdump.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-tal-str.o: $(CCANDIR)/ccan/tal/str/str.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-tal-grab_file.o: $(CCANDIR)/ccan/tal/grab_file/grab_file.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-take.o: $(CCANDIR)/ccan/take/take.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-list.o: $(CCANDIR)/ccan/list/list.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-read_write_all.o: $(CCANDIR)/ccan/read_write_all/read_write_all.c + $(CC) $(CFLAGS) -c -o $@ $< diff --git a/ccan/cdump/tools/cdump-enumstr.c b/ccan/cdump/tools/cdump-enumstr.c new file mode 100644 index 00000000..f0b70ecd --- /dev/null +++ b/ccan/cdump/tools/cdump-enumstr.c @@ -0,0 +1,52 @@ +#include +#include +#include + +static bool dump_map(const char *name, struct cdump_type *t, void *unused) +{ + size_t i; + + printf("struct {\n" + " enum %s v;\n" + " const char *name;\n" + "} enum_%s_names[] = {\n", name, name); + + for (i = 0; i < tal_count(t->u.enum_vals); i++) + printf(" { %s, \"%s\" },\n", + t->u.enum_vals[i].name, + t->u.enum_vals[i].name); + printf(" { 0, NULL } };\n"); + return true; +} + +int main(int argc, char *argv[]) +{ + char *code, *problems; + struct cdump_definitions *defs; + + if (argc < 2) + errx(1, "Usage: cdump-enumstr [...]"); + + code = grab_file(NULL, streq(argv[1], "-") ? NULL : argv[1]); + if (!code) + err(1, "Reading %s", argv[1]); + + defs = cdump_extract(code, code, &problems); + if (!defs) + errx(1, "Parsing %s:\n%s", argv[1], problems); + + if (argc == 2) + strmap_iterate(&defs->enums, dump_map, NULL); + else { + unsigned int i; + struct cdump_type *t; + + for (i = 2; i < argc; i++) { + t = strmap_get(&defs->enums, argv[i]); + if (!t) + errx(1, "Enum %s not found", argv[i]); + dump_map(argv[i], t, NULL); + } + } + return 0; +}