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