X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fcdump%2F_info;fp=ccan%2Fcdump%2F_info;h=02ba19b5b3fb0d0b9d4ebe8a009195c6895e930e;hp=0000000000000000000000000000000000000000;hb=158691ae36fe1da78ec54dbcc0006f603398dae2;hpb=17aa322abd7b785330ab49e348144be940b3d1d8 diff --git a/ccan/cdump/_info b/ccan/cdump/_info new file mode 100644 index 00000000..02ba19b5 --- /dev/null +++ b/ccan/cdump/_info @@ -0,0 +1,94 @@ +#include "config.h" +#include +#include + +/** + * cdump - routines to parse simple C structures. + * + * This code is designed to produce data structures summarizing C code. + * It only operates on simple, well-formed C code (eg. specific headers + * which you want to autogenerate from), but it should be fairly easy to + * enhance if desired. + * + * Author: Rusty Russell + * License: BSD-MIT + * + * Example: + * // Creates a simple print function for a structure. + * #include + * #include + * #include + * + * static void print_as(const char *fmt, const char *member_name) + * { + * printf("\tprintf(\"%%s:%s\\n\", \"%s\", s->%s);\n", + * fmt, member_name, member_name); + * } + * + * int main(int argc, char *argv[]) + * { + * char *code, *problems; + * struct cdump_definitions *defs; + * int i, j; + * + * // Read code from stdin. + * code = grab_file(NULL, NULL); + * + * defs = cdump_extract(NULL, code, &problems); + * if (!defs) + * errx(1, "Parsing stdin: %s", problems); + * + * for (i = 1; i < argc; i++) { + * struct cdump_type *t = strmap_get(&defs->structs, argv[i]); + * if (!t) + * errx(1, "Could not find struct %s", argv[i]); + * + * printf("void print_struct_%s(const struct %s *s)\n" + * "{\n", argv[i], argv[i]); + * for (j = 0; j < tal_count(t->u.members); j++) { + * const struct cdump_member *m = t->u.members + j; + * switch (m->type->kind) { + * case CDUMP_STRUCT: + * case CDUMP_UNION: + * case CDUMP_ARRAY: + * // Too hard for this simple example. + * printf("\tprintf(\"%%s:???\\n\", \"%s\");\n", + * m->name); + * break; + * case CDUMP_ENUM: + * print_as("%i", m->name); + * break; + * case CDUMP_POINTER: + * print_as("%p", m->name); + * break; + * case CDUMP_UNKNOWN: + * if (!strcmp(m->type->name, "int")) + * print_as("%i", m->name); + * else if (!strcmp(m->type->name, "long int")) + * print_as("%li", m->name); + * else if (!strcmp(m->type->name, "unsigned int")) + * print_as("%u", m->name); + * // etc... + * break; + * } + * } + * printf("}\n"); + * } + * return 0; + * } + */ +int main(int argc, char *argv[]) +{ + /* Expect exactly one argument */ + if (argc != 2) + return 1; + + if (strcmp(argv[1], "depends") == 0) { + printf("ccan/tal\n"); + printf("ccan/tal/str\n"); + printf("ccan/strmap\n"); + return 0; + } + + return 1; +}