-
Notifications
You must be signed in to change notification settings - Fork 258
/
cmdline.mini.c
333 lines (274 loc) · 8.01 KB
/
cmdline.mini.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* -*- mode: c; c-basic-offset: 2 -*- */
/*
* Copyright (C) 2007-2012 David Bird (Coova Technologies) <support@coova.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: David Bird <david@coova.com>
*/
/*
* This is a simple replacement for the use of the gengetopt generated cmdline.c
* which has more features (and is a lot larger) than what we really need. This
* file include "cmdline.mini.gen" which is generated by "cmdline.mini.sh".
*/
#include "chilli.h"
#include "cmdline.h"
#include <getopt.h>
#ifndef _debug_
#define _debug_ 0
#endif
#define OPT_string 0
#define OPT_integer 1
#define OPT_long 2
#define OPT_flag 3
struct opt_def_t {
int opt_type;
const char *opt_name;
char multi_opt;
ssize_t offset_arg;
ssize_t offset_orig;
ssize_t offset_given;
};
static int OPT_generic(struct gengetopt_args_info *, int, void *, char);
#include "cmdline.mini.gen"
static int opts_cnt = sizeof(opts) / sizeof(struct opt_def_t);
static int OPT_generic(struct gengetopt_args_info *args_info, int o,
void *d, char is_cmdline) {
struct opt_def_t * opt = &opts[o];
#if(_debug_)
printf("%s opts[%d] %s [%s] offset %d\n", __FUNCTION__, o,
opt->opt_name, d ? (char *)d : "(null)",
opt->offset_arg);
#endif
unsigned int * given = (unsigned int *)(((char *)args_info) + opt->offset_given);
if (opt->offset_orig > 0) {
void **p = (void **) (((void *)args_info) + opt->offset_orig);
if (is_cmdline) {
*p = d;
} else {
if (*p && !opt->multi_opt) {
#if(_debug_)
fprintf(stderr, "Skipping option %s defined on command line\n",
opt->opt_name);
#endif
return 0;
}
}
}
switch(opt->opt_type) {
case OPT_integer:
if (opt->offset_arg > 0) {
#if(_debug_)
if (opt->multi_opt) fprintf(stderr, "NOT SUPPORTED %s %s %d\n", __FUNCTION__, __FILE__, __LINE__);
#endif
*((int *)(((char *)args_info) + opt->offset_arg)) = atoi((char *)d);
}
*given = 1;
break;
case OPT_flag:
if (opt->offset_arg > 0) {
#if(_debug_)
if (opt->multi_opt) fprintf(stderr, "NOT SUPPORTED %s %s %d\n", __FUNCTION__, __FILE__, __LINE__);
#endif
*((int *)(((char *)args_info) + opt->offset_arg)) = 1;
}
*given = 1;
break;
case OPT_string:
if (opt->offset_arg > 0) {
if (d) {
if (opt->multi_opt) {
char ***p = (char ***) (((char *)args_info) + opt->offset_arg);
char **lp = *p;
int idx = *given;
*given = *given + 1;
if (lp) {
lp = (char **)realloc(lp, *given * sizeof(char *));
} else {
lp = (char **)malloc(*given * sizeof(char *));
}
lp[idx] = strdup((char *)d);
*p = lp;
} else {
char **p = (char **) (((char *)args_info) + opt->offset_arg);
if (*p) free(*p);
*p = strdup((char *)d);
*given = 1;
}
}
}
break;
case OPT_long:
if (opt->offset_arg > 0) {
#if(_debug_)
if (opt->multi_opt) fprintf(stderr, "NOT SUPPORTED %s %s %d\n", __FUNCTION__, __FILE__, __LINE__);
printf("long offset %d\n", opt->offset_arg);
#endif
*((long *)(((char *)args_info) + opt->offset_arg)) = (long) atol((char *)d);
break;
}
}
return 0;
}
int mini_cmdline_file(char *filename, struct gengetopt_args_info *args_info,
int b, int b2, int b3) {
FILE *file = fopen(filename, "r");
char linebuf[2048];
int line_num = 0;
char *str_index;
size_t next_token;
char delimiter;
char *fopt, *farg;
int result = 0;
int i;
if (!file) {
syslog(LOG_ERR, "%s: Could not open file %s", strerror(errno), filename);
return -1;
}
while ((fgets(linebuf, sizeof(linebuf), file)) != 0) {
++line_num;
next_token = strspn(linebuf, " \t\r\n");
str_index = linebuf + next_token;
if (str_index[0] == '\0' || str_index[0] == '#')
continue;
fopt = str_index;
next_token = strcspn(fopt, " \t\r\n=");
if (fopt[next_token] == '\0') {
farg = 0;
goto noarg;
}
fopt[next_token++] = '\0';
next_token += strspn(fopt + next_token, " \t\r\n");
if (fopt[next_token] == '=') {
next_token++;
next_token += strspn(fopt + next_token, " \t\r\n");
}
str_index += next_token;
farg = str_index;
if (farg[0] == '\"' || farg[0] == '\'') {
str_index = strchr(++farg, str_index[0]);
if (!str_index) {
fprintf(stderr,
"%s:%s:%d: unterminated string in configuration file\n",
PACKAGE, filename, line_num);
result = -1;
break;
}
} else {
next_token = strcspn(farg, " \t\r\n#\'\"");
str_index += next_token;
}
delimiter = *str_index, *str_index++ = '\0';
if (delimiter != '\0' && delimiter != '#') {
str_index += strspn(str_index, " \t\r\n");
if (*str_index != '\0' && *str_index != '#') {
fprintf(stderr,
"%s:%s:%d: malformed string in configuration file\n",
PACKAGE, filename, line_num);
result = -1;
break;
}
}
noarg:
if (!strcmp(fopt,"include")) {
if (farg && *farg) {
result = mini_cmdline_file(farg, args_info, 0, 0, 0);
} else {
fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
CMDLINE_PARSER_PACKAGE, filename, line_num);
}
continue;
}
#if(_debug_)
fprintf(stderr,"%s = %s\n", fopt, farg);
#endif
for (i=0; i < opts_cnt; i++) {
struct opt_def_t * opt = &opts[i];
if (!strcmp(opt->opt_name, fopt)) {
OPT_generic(args_info,i,farg,0);
break;
}
}
if (i == opts_cnt) {
fprintf(stderr,"invalid option %s\n", fopt);
result = -1;
}
}
return result;
}
int mini_cmdline_free(struct gengetopt_args_info *args_info) {
int i;
for (i=0; i < opts_cnt; i++) {
struct opt_def_t * opt = &opts[i];
switch (opt->opt_type) {
case OPT_string:
if (opt->offset_arg) {
if (opt->multi_opt) {
char ***v = (char ***) (((char *)args_info) + opt->offset_arg);
unsigned int * given = (unsigned int *)(((char *)args_info) + opt->offset_given);
int cnt = *given;
int j;
if (*v && cnt) {
char **lp = *v;
for (j=0; j<cnt; j++) {
free(lp[j]);
}
free(*v);
}
} else {
char **v = (char **) (((char *)args_info) + opt->offset_arg);
if (*v) free(*v);
}
}
break;
}
}
return 0;
}
int mini_cmdline_parser2(int argc, char **argv,
struct gengetopt_args_info *args_info,
int b, int b2, int b3) {
int ret = 0;
int c;
optarg = 0;
optind = 0;
opterr = 0;
optopt = '?';
getopt_DEFAULTS
while(1) {
int option_index = 0;
c = getopt_long(argc, argv, getopt_OPTIONS, long_options, &option_index);
if (c == -1) break;
switch(c) {
getopt_CHECKS
case 0:
OPT_generic(args_info, option_index, optarg, 1);
break;
case '?':
fprintf(stderr,"invalid option %s\n", argv[optind-1]);
ret = -1;
break;
default:
break;
}
}
if (optind < argc) {
fprintf(stderr, "non-option ARGV-elements: ");
while (optind < argc)
fprintf(stderr, "%s ", argv[optind++]);
fprintf(stderr, "\n");
ret = -1;
}
return ret;
}