Commit a0ab8f12 authored by Rusty Russell's avatar Rusty Russell

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

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 8e4b51ad
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 $@ $<
#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;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment