]> git.ozlabs.org Git - ccan/blob - ccan/cdump/_info
cdump: new module.
[ccan] / ccan / cdump / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * cdump - routines to parse simple C structures.
7  *
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
11  * enhance if desired.
12  *
13  * Author: Rusty Russell <rusty@rustcorp.com.au>
14  * License: BSD-MIT
15  *
16  * Example:
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>
21  *
22  * static void print_as(const char *fmt, const char *member_name)
23  * {
24  *      printf("\tprintf(\"%%s:%s\\n\", \"%s\", s->%s);\n",
25  *             fmt, member_name, member_name);
26  * }
27  *
28  * int main(int argc, char *argv[])
29  * {
30  *      char *code, *problems;
31  *      struct cdump_definitions *defs;
32  *      int i, j;
33  *
34  *      // Read code from stdin.
35  *      code = grab_file(NULL, NULL);
36  *
37  *      defs = cdump_extract(NULL, code, &problems);
38  *      if (!defs)
39  *              errx(1, "Parsing stdin: %s", problems);
40  *
41  *      for (i = 1; i < argc; i++) {
42  *              struct cdump_type *t = strmap_get(&defs->structs, argv[i]);
43  *              if (!t)
44  *                      errx(1, "Could not find struct %s", argv[i]);
45  *
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) {
51  *                      case CDUMP_STRUCT:
52  *                      case CDUMP_UNION:
53  *                      case CDUMP_ARRAY:
54  *                              // Too hard for this simple example.
55  *                              printf("\tprintf(\"%%s:???\\n\", \"%s\");\n",
56  *                                     m->name);
57  *                              break;
58  *                      case CDUMP_ENUM:
59  *                              print_as("%i", m->name);
60  *                              break;
61  *                      case CDUMP_POINTER:
62  *                              print_as("%p", m->name);
63  *                              break;
64  *                      case CDUMP_UNKNOWN:
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);
71  *                              // etc...
72  *                              break;
73  *                      }
74  *              }
75  *              printf("}\n");
76  *      }
77  *      return 0;
78  * }
79  */
80 int main(int argc, char *argv[])
81 {
82         /* Expect exactly one argument */
83         if (argc != 2)
84                 return 1;
85
86         if (strcmp(argv[1], "depends") == 0) {
87                 printf("ccan/tal\n");
88                 printf("ccan/tal/str\n");
89                 printf("ccan/strmap\n");
90                 return 0;
91         }
92
93         return 1;
94 }