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;
34 * // Read code from stdin.
35 * code = grab_file(NULL, NULL);
37 * defs = cdump_extract(NULL, code, &problems);
39 * errx(1, "Parsing stdin: %s", problems);
41 * for (i = 1; i < argc; i++) {
42 * struct cdump_type *t = strmap_get(&defs->structs, argv[i]);
44 * errx(1, "Could not find struct %s", argv[i]);
46 * printf("void print_struct_%s(const struct %s *s)\n"
47 * "{\n", argv[i], argv[i]);
48 * for (j = 0; j < tal_count(t->u.members); j++) {
49 * const struct cdump_member *m = t->u.members + j;
50 * switch (m->type->kind) {
54 * // Too hard for this simple example.
55 * printf("\tprintf(\"%%s:???\\n\", \"%s\");\n",
59 * print_as("%i", m->name);
62 * print_as("%p", m->name);
65 * if (!strcmp(m->type->name, "int"))
66 * print_as("%i", m->name);
67 * else if (!strcmp(m->type->name, "long int"))
68 * print_as("%li", m->name);
69 * else if (!strcmp(m->type->name, "unsigned int"))
70 * print_as("%u", m->name);
80 int main(int argc, char *argv[])
82 /* Expect exactly one argument */
86 if (strcmp(argv[1], "depends") == 0) {
88 printf("ccan/tal/str\n");
89 printf("ccan/strmap\n");