-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_handler.h
164 lines (134 loc) · 3.76 KB
/
cmd_handler.h
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Stage 1
*
* Define function prototypes.
*/
#undef PROGRAM
#define PROGRAM(_name, _version, _short_desc, _long_desc, _usage)
#undef COMMAND
#define COMMAND(name, callback, short_desc, long_desc, usage, deprecated, options, action) \
static int callback(int argc, char **argv, struct command *command);
#undef COMMAND_LIST
#define COMMAND_LIST(args...) args
#undef OPTION_LIMIT
#define OPTION_LIMIT(type, var, short_opt, long_opt, desc, meta, value, limit, deprecated)
#undef OPTIONS
#define OPTIONS(args...) args
#include CMD_INCLUDE(CMD_INC_FILE)
/*
* Stage 2
*
* Define command structures.
*/
#undef PROGRAM
#define PROGRAM(_name, _version, _short_desc, _long_desc, _usage)
#undef OPTIONS
#define OPTIONS(args...) { args {.type = TYPE_END} }
#undef OPTION_LIMIT
#define OPTION_LIMIT(_type, var, _short_option, _long_option, _desc, _meta, value, _limit, _deprecated) \
{ \
.type = TYPE_ ## _type, \
.long_option = _long_option, \
.short_option = _short_option, \
.desc = _desc, \
.meta = _meta, \
.limit = _limit, \
.deprecated = _deprecated, \
},
#undef COMMAND
#define COMMAND(_name, _callback, _short_desc, _long_desc, _usage, _deprecated, _options, action) \
static struct command _callback ## _cmd = { \
.name = _name, \
.short_desc = _short_desc, \
.long_desc = _long_desc, \
.usage = _usage, \
.callback = &_callback, \
.deprecated = _deprecated, \
.options = _options, \
};
#undef COMMAND_LIST
#define COMMAND_LIST(args...) args
#include CMD_INCLUDE(CMD_INC_FILE)
/*
* Stage 3
*
* Generate list of commands for the plugin.
*/
#undef PROGRAM
#define PROGRAM(_name, _version, _short_desc, _long_desc, _usage)
#undef OPTION_LIMIT
#define OPTION_LIMIT(type, var, short_opt, long_opt, desc, meta, value, limit, deprecated)
#undef OPTIONS
#define OPTIONS(args...) args
#undef COMMAND
#define COMMAND(name, callback, short_desc, long_desc, usage, deprecated, options, action) \
&callback ## _cmd,
#undef COMMAND_LIST
#define COMMAND_LIST(args...) \
static struct command *commands[] = { \
args \
NULL \
};
#include CMD_INCLUDE(CMD_INC_FILE)
/*
* Stage 4
*
* Define callback.
*/
#undef PROGRAM
#define PROGRAM(_name, _version, _short_desc, _long_desc, _usage)
#undef OPTION_LIMIT
#define OPTION_LIMIT(type, var, short_opt, long_opt, desc, meta, _value, limit, deprecated) \
type var = (type)_value; \
command->options[option_index++].value = (void*)&var;
#undef OPTIONS
#define OPTIONS(args...) args
#undef ACTION
#define ACTION(args...) args
#undef COMMAND
#define COMMAND(name, callback, short_desc, long_desc, usage, deprecated, options, action) \
static int callback(int argc, char **argv, struct command *command) \
{ \
int ret, option_index = 0; \
options \
ret = parse_args(argc, argv, command); \
if (!ret) { \
action \
} \
return ret; \
}
#undef COMMAND_LIST
#define COMMAND_LIST(args...) args
#include CMD_INCLUDE(CMD_INC_FILE)
/*
* Stage 5
*
* Define and execute the program.
*/
#undef PROGRAM
#define PROGRAM(_name, _version, _short_desc, _long_desc, _usage) \
static struct program program = { \
.version = _version, \
.command = { \
.name = _name, \
.short_desc = _short_desc, \
.long_desc = _long_desc, \
.usage = _usage, \
.subcommands = commands, \
}, \
}; \
int main(int argc, char **argv) \
{ \
if (parse_args(argc, argv, &program.command)) \
exit(errno); \
return 0; \
}
#undef COMMAND
#define COMMAND(name, callback, short_desc, long_desc, usage, deprecated, options, action)
#undef COMMAND_LIST
#define COMMAND_LIST(args...)
#undef OPTION_LIMIT
#define OPTION_LIMIT(type, var, short_opt, long_opt, desc, meta, value, limit, deprecated)
#undef OPTIONS
#define OPTIONS(args...)
#include CMD_INCLUDE(CMD_INC_FILE)