-
Notifications
You must be signed in to change notification settings - Fork 0
/
yopts.c.in
219 lines (171 loc) · 3.67 KB
/
yopts.c.in
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* This file is auto-generated by Yoda v${VERSION} from ${INPUTFILE}
* See ${PROJECT_URL} for details
*/
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "${HEADERFILE}"
#ifdef YDEBUG
#define dprint printf
int main(int argc, char **argv)
{
int ret;
ret = ${PROJ}yopts_parse(argc, argv);
printf("Parser returned %d (left %d(%s)... options)\n", ret, yopt_tail, argv[yopt_tail]);
return ret;
}
#else
#define dprint(...)
#endif
struct yopts ${PROJ}yopts = {
${DEFAULTS}
};
int yopt_tail;
static int yopt_err = YOPTS_PARSE_OK;
static const int yopt_nr_args = ${NR_ARGS};
static int yopt_next_arg = 0;
#define YOPTARG_SET ((char *)-1)
void ${PROJ}yopts_usage(char *prog)
{
printf("Usage:\n"
${USAGE},
prog);
}
__attribute__((weak)) void yopt_print(const char *format, ...)
{
va_list params;
va_start(params, format);
fprintf(stderr, format, params);
va_end(params);
}
__attribute__((weak)) void *yopt_realloc(void *ptr, unsigned long size)
{
return realloc(ptr, size);
}
static inline void *yopt_realloc_mem(void *ptr, unsigned long size)
{
void *ret;
ret = yopt_realloc(ptr, size);
if (!ret)
yopt_err = YOPTS_PARSE_ERR;
return ret;
}
static inline unsigned int yopt_parse_int(char *str)
{
char *aux;
unsigned int ret;
ret = strtol(str, &aux, 0);
if (*aux != '\0')
yopt_err = YOPTS_PARSE_ERR;
return ret;
}
struct yopt_set {
const char *s;
const struct option *l;
int (*assign)(int opt, char **argv);
};
static int generic_assign(int opt, char **argv);
static int yopts_parse_arg(char *arg);
static int def_assign(int opt, char **argv, bool generic)
{
if (generic) {
if (opt == 1)
return yopts_parse_arg(argv[optind - 1]);
yopt_err = YOPTS_PARSE_ERR;
return 0;
}
return generic_assign(opt, argv);
}
${YSETS}
static struct yopt_set *ycur = &generic_set;
static int yopts_fixup_choices(void)
{
yopt_err = YOPTS_PARSE_OK;
${FIX_CHOICES}
return yopt_err;
}
static int yopts_parse_arg(char *arg)
{
bool new_set = false;
switch (yopt_next_arg) {
${ASSIGN_ARGS}
}
yopt_next_arg++;
if (yopt_next_arg == yopt_nr_args)
return 1;
if (new_set)
return 2;
return 0;
}
static int yopts_check_args(void)
{
yopt_err = YOPTS_PARSE_OK;
${CHECK_ARGS}
return yopt_err;
}
static int yopts_check_reqs(void)
{
yopt_err = YOPTS_PARSE_OK;
${CHECK_REQS}
return yopt_err;
}
int ${PROJ}yopts_parse(int argc, char **argv)
{
int opt, idx, ret = YOPTS_PARSE_OK;
bool permutate = false;
if (!yopt_nr_args)
permutate = true;
while (1) {
opt = getopt_long(argc, argv, ycur->s + (permutate ? 1 : 0), ycur->l, &idx);
if (opt == -1)
break;
if ((opt == 1) && permutate) {
yopt_print("YODA: Internal error. Plz, report to developers.\n");
return YOPTS_PARSE_ERR;
}
opt = ycur->assign(opt, argv);
if (${PROJ}yopts.version)
return YOPTS_PARSE_SPECIAL;
if (${PROJ}yopts.help)
yopt_err = YOPTS_PARSE_SPECIAL;
if (yopt_err) {
if (!yopt_next_arg)
${PROJ}yopts_usage(argv[0]);
return yopt_err;
}
if (opt) {
if (opt < 0) {
ret = YOPTS_PARSE_SPECIAL;
break;
}
dprint("Switching opt array (%d)\n", opt);
/* reset getopt_long to normal (permutating) scan */
optind--;
argc -= optind;
argv += optind;
yopt_tail += optind;
optind = 0;
if (opt != 2) {
dprint("Reset arg scan to permutating\n");
permutate = true;
}
}
}
yopt_tail += optind;
if (ret == YOPTS_PARSE_OK) {
yopt_err = yopts_check_args();
if (yopt_err)
return yopt_err;
}
yopt_err = yopts_fixup_choices();
if (yopt_err)
return yopt_err;
yopt_err = yopts_check_reqs();
if (yopt_err)
return yopt_err;
return ret;
}