]> git.ozlabs.org Git - ccan/blob - ccan/cdump/_info
base64: fix for unsigned chars (e.g. ARM).
[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;
33  *      size_t j;
34  *
35  *      // Read code from stdin.
36  *      code = grab_file(NULL, NULL);
37  *
38  *      defs = cdump_extract(NULL, code, &problems);
39  *      if (!defs)
40  *              errx(1, "Parsing stdin: %s", problems);
41  *
42  *      for (i = 1; i < argc; i++) {
43  *              struct cdump_type *t = strmap_get(&defs->structs, argv[i]);
44  *              if (!t)
45  *                      errx(1, "Could not find struct %s", argv[i]);
46  *
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) {
52  *                      case CDUMP_STRUCT:
53  *                      case CDUMP_UNION:
54  *                      case CDUMP_ARRAY:
55  *                              // Too hard for this simple example.
56  *                              printf("\tprintf(\"%%s:???\\n\", \"%s\");\n",
57  *                                     m->name);
58  *                              break;
59  *                      case CDUMP_ENUM:
60  *                              print_as("%i", m->name);
61  *                              break;
62  *                      case CDUMP_POINTER:
63  *                              print_as("%p", m->name);
64  *                              break;
65  *                      case CDUMP_UNKNOWN:
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);
72  *                              // etc...
73  *                              break;
74  *                      }
75  *              }
76  *              printf("}\n");
77  *      }
78  *      return 0;
79  * }
80  */
81 int main(int argc, char *argv[])
82 {
83         /* Expect exactly one argument */
84         if (argc != 2)
85                 return 1;
86
87         if (strcmp(argv[1], "depends") == 0) {
88                 printf("ccan/tal\n");
89                 printf("ccan/tal/str\n");
90                 printf("ccan/strmap\n");
91                 return 0;
92         }
93
94         return 1;
95 }