6 * cdump - routines to parse simple C structures.
8 * This code is designed to produce data structures summarizing C code.
9 * It only operates on simple, well-formed C code (eg. specific headers
10 * which you want to autogenerate from), but it should be fairly easy to
13 * Author: Rusty Russell <rusty@rustcorp.com.au>
17 * // Creates a simple print function for a structure.
18 * #include <ccan/cdump/cdump.h>
19 * #include <ccan/tal/grab_file/grab_file.h>
20 * #include <ccan/err/err.h>
22 * static void print_as(const char *fmt, const char *member_name)
24 * printf("\tprintf(\"%%s:%s\\n\", \"%s\", s->%s);\n",
25 * fmt, member_name, member_name);
28 * int main(int argc, char *argv[])
30 * char *code, *problems;
31 * struct cdump_definitions *defs;
35 * // Read code from stdin.
36 * code = grab_file(NULL, NULL);
38 * defs = cdump_extract(NULL, code, &problems);
40 * errx(1, "Parsing stdin: %s", problems);
42 * for (i = 1; i < argc; i++) {
43 * struct cdump_type *t = strmap_get(&defs->structs, argv[i]);
45 * errx(1, "Could not find struct %s", argv[i]);
47 * printf("void print_struct_%s(const struct %s *s)\n"
48 * "{\n", argv[i], argv[i]);
49 * for (j = 0; j < tal_count(t->u.members); j++) {
50 * const struct cdump_member *m = t->u.members + j;
51 * switch (m->type->kind) {
55 * // Too hard for this simple example.
56 * printf("\tprintf(\"%%s:???\\n\", \"%s\");\n",
60 * print_as("%i", m->name);
63 * print_as("%p", m->name);
66 * if (!strcmp(m->type->name, "int"))
67 * print_as("%i", m->name);
68 * else if (!strcmp(m->type->name, "long int"))
69 * print_as("%li", m->name);
70 * else if (!strcmp(m->type->name, "unsigned int"))
71 * print_as("%u", m->name);
81 int main(int argc, char *argv[])
83 /* Expect exactly one argument */
87 if (strcmp(argv[1], "depends") == 0) {
89 printf("ccan/tal/str\n");
90 printf("ccan/strmap\n");