cdump: add useful tool to generate enum name string table.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 11 Nov 2014 05:25:54 +0000 (15:55 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 11 Nov 2014 05:29:24 +0000 (15:59 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/cdump/tools/.gitignore [new file with mode: 0644]
ccan/cdump/tools/Makefile [new file with mode: 0644]
ccan/cdump/tools/cdump-enumstr.c [new file with mode: 0644]

diff --git a/ccan/cdump/tools/.gitignore b/ccan/cdump/tools/.gitignore
new file mode 100644 (file)
index 0000000..67baa86
--- /dev/null
@@ -0,0 +1 @@
+cdump-enumstr
diff --git a/ccan/cdump/tools/Makefile b/ccan/cdump/tools/Makefile
new file mode 100644 (file)
index 0000000..3b30d66
--- /dev/null
@@ -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 (file)
index 0000000..f0b70ec
--- /dev/null
@@ -0,0 +1,52 @@
+#include <ccan/cdump/cdump.h>
+#include <ccan/tal/grab_file/grab_file.h>
+#include <ccan/err/err.h>
+
+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 <filename> [<enums>...]");
+
+       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;
+}