]> git.ozlabs.org Git - ccan/blob - ccan/autodata/_info
ccanlint: enhance and streamline "output" testing lines.
[ccan] / ccan / autodata / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * autodata - stash pointers in your binary for automatic registration
7  *
8  * This code allows declarations in your source which you can gather
9  * together at runtime to form tables.  This is often used in place of
10  * having a central registration function or table.
11  *
12  * Note that this technique does not work in general for shared libaries,
13  * only for code compiled into a binary.
14  *
15  * License: BSD-MIT
16  *
17  * Example:
18  * // Distributed commandline option registration (note: ccan/opt is better!)
19  * #include <ccan/autodata/autodata.h>
20  * #include <stdio.h>
21  * #include <unistd.h>
22  * #include <stdbool.h>
23  * #include <err.h>
24  *
25  * static bool verbose = false;
26  *
27  * // This would normally be in a header, so any C file can use it.
28  * struct option {
29  *      char c;
30  *      bool takes_arg;
31  *      bool (*cb)(char *optarg);
32  * };
33  * AUTODATA_TYPE(options, struct option);
34  * #define REGISTER_OPTION(optstruct) \
35  *              AUTODATA(options, (optstruct))
36  *
37  * // Now a few examples (could be anywhere in source)
38  * static bool verbose_cb(char *unused)
39  * {
40  *      verbose = true;
41  *      return true;
42  * }
43  * static struct option dash_v = { 'v', false, verbose_cb };
44  * REGISTER_OPTION(&dash_v);
45  *
46  * static bool chdir_cb(char *dir)
47  * {
48  *      if (verbose)
49  *              printf("chdir to %s. ", dir);
50  *      if (chdir(dir) != 0)
51  *              return false;
52  *      return true;
53  * }
54  * static struct option dash_C = { 'C', true, chdir_cb };
55  * REGISTER_OPTION(&dash_C);
56  *
57  * int main(int argc, char *argv[])
58  * {
59  *      struct option **opts;
60  *      size_t i, num;
61  *      int o;
62  *      char *optstring, *p;
63  *
64  *      // Gather together all the registered options.
65  *      opts = autodata_get(options, &num);
66  *
67  *      // Make pretty string for getopt().
68  *      p = optstring = malloc(num * 2 + 1);
69  *      for (i = 0; i < num; i++) {
70  *              *(p++) = opts[i]->c;
71  *              if (opts[i]->takes_arg)
72  *                      *(p++) = ':';
73  *      }
74  *      *p = '\0';
75  *
76  *      while ((o = getopt(argc, argv, optstring)) != -1) {
77  *              if (o == '?')
78  *                      exit(1);
79  *              // Call callback in matching option.
80  *              for (i = 0; i < num; i++) {
81  *                      if (opts[i]->c == o) {
82  *                              if (!opts[i]->cb(optarg))
83  *                                      err(1, "parsing -%c", o);
84  *                              break;
85  *                      }
86  *              }
87  *      }
88  *      // free up gathered option table.
89  *      autodata_free(opts);
90  *
91  *      if (verbose)
92  *              printf("verbose mode on\n");
93  *      return 0;
94  * }
95  * // Given "-v" outputs "verbose mode on\n"
96  * // Given "-v -C /" outputs "chdir to /. verbose mode on\n"
97  */
98 int main(int argc, char *argv[])
99 {
100         /* Expect exactly one argument */
101         if (argc != 2)
102                 return 1;
103
104         if (strcmp(argv[1], "depends") == 0) {
105                 printf("ccan/compiler\n");
106                 printf("ccan/ptr_valid\n");
107                 return 0;
108         }
109
110         return 1;
111 }