]> git.ozlabs.org Git - ccan/blob - ccan/cdump/tools/cdump-enumstr.c
structeq: fix case where we mark padding as unknown.
[ccan] / ccan / cdump / tools / cdump-enumstr.c
1 #include <ccan/cdump/cdump.h>
2 #include <ccan/tal/grab_file/grab_file.h>
3 #include <ccan/err/err.h>
4
5 static bool dump_map(const char *name, struct cdump_type *t, void *unused)
6 {
7         size_t i;
8
9         printf("struct {\n"
10                "        enum %s v;\n"
11                "        const char *name;\n"
12                "} enum_%s_names[] = {\n", name, name);
13
14         for (i = 0; i < tal_count(t->u.enum_vals); i++)
15                 printf("        { %s, \"%s\" },\n",
16                        t->u.enum_vals[i].name,
17                        t->u.enum_vals[i].name);
18         printf("        { 0, NULL } };\n");
19         return true;
20 }
21
22 int main(int argc, char *argv[])
23 {
24         char *code, *problems;
25         struct cdump_definitions *defs;
26
27         if (argc < 2)
28                 errx(1, "Usage: cdump-enumstr <filename> [<enums>...]");
29
30         code = grab_file(NULL, streq(argv[1], "-") ? NULL : argv[1]);
31         if (!code)
32                 err(1, "Reading %s", argv[1]);
33
34         defs = cdump_extract(code, code, &problems);
35         if (!defs)
36                 errx(1, "Parsing %s:\n%s", argv[1], problems);
37
38         if (argc == 2)
39                 strmap_iterate(&defs->enums, dump_map, NULL);
40         else {
41                 unsigned int i;
42                 struct cdump_type *t;
43
44                 for (i = 2; i < argc; i++) {
45                         t = strmap_get(&defs->enums, argv[i]);
46                         if (!t)
47                                 errx(1, "Enum %s not found", argv[i]);
48                         dump_map(argv[i], t, NULL);
49                 }
50         }
51         return 0;
52 }