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