Browse Source

cdump: add useful tool to generate enum name string table.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 11 years ago
parent
commit
a0ab8f12eb
3 changed files with 80 additions and 0 deletions
  1. 1 0
      ccan/cdump/tools/.gitignore
  2. 27 0
      ccan/cdump/tools/Makefile
  3. 52 0
      ccan/cdump/tools/cdump-enumstr.c

+ 1 - 0
ccan/cdump/tools/.gitignore

@@ -0,0 +1 @@
+cdump-enumstr

+ 27 - 0
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 $@ $<

+ 52 - 0
ccan/cdump/tools/cdump-enumstr.c

@@ -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;
+}