1 | /*
|
---|
2 | * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
|
---|
3 | * (Royal Institute of Technology, Stockholm, Sweden).
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | *
|
---|
13 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | *
|
---|
17 | * 3. Neither the name of the Institute nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <config.h>
|
---|
35 |
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <string.h>
|
---|
39 | #include "roken.h"
|
---|
40 | #include "getarg.h"
|
---|
41 |
|
---|
42 | #define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
|
---|
43 |
|
---|
44 | static size_t
|
---|
45 | print_arg (char *string,
|
---|
46 | size_t len,
|
---|
47 | int mdoc,
|
---|
48 | int longp,
|
---|
49 | struct getargs *arg,
|
---|
50 | char *(i18n)(const char *))
|
---|
51 | {
|
---|
52 | const char *s;
|
---|
53 |
|
---|
54 | *string = '\0';
|
---|
55 |
|
---|
56 | if (ISFLAG(*arg) || (!longp && arg->type == arg_counter))
|
---|
57 | return 0;
|
---|
58 |
|
---|
59 | if(mdoc){
|
---|
60 | if(longp)
|
---|
61 | strlcat(string, "= Ns", len);
|
---|
62 | strlcat(string, " Ar ", len);
|
---|
63 | } else {
|
---|
64 | if (longp)
|
---|
65 | strlcat (string, "=", len);
|
---|
66 | else
|
---|
67 | strlcat (string, " ", len);
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (arg->arg_help)
|
---|
71 | s = (*i18n)(arg->arg_help);
|
---|
72 | else if (arg->type == arg_integer || arg->type == arg_counter)
|
---|
73 | s = "integer";
|
---|
74 | else if (arg->type == arg_string)
|
---|
75 | s = "string";
|
---|
76 | else if (arg->type == arg_strings)
|
---|
77 | s = "strings";
|
---|
78 | else if (arg->type == arg_double)
|
---|
79 | s = "float";
|
---|
80 | else
|
---|
81 | s = "<undefined>";
|
---|
82 |
|
---|
83 | strlcat(string, s, len);
|
---|
84 | return 1 + strlen(s);
|
---|
85 | }
|
---|
86 |
|
---|
87 | static void
|
---|
88 | mandoc_template(struct getargs *args,
|
---|
89 | size_t num_args,
|
---|
90 | const char *progname,
|
---|
91 | const char *extra_string,
|
---|
92 | char *(i18n)(const char *))
|
---|
93 | {
|
---|
94 | size_t i;
|
---|
95 | char timestr[64], cmd[64];
|
---|
96 | char buf[128];
|
---|
97 | const char *p;
|
---|
98 | time_t t;
|
---|
99 |
|
---|
100 | printf(".\\\" Things to fix:\n");
|
---|
101 | printf(".\\\" * correct section, and operating system\n");
|
---|
102 | printf(".\\\" * remove Op from mandatory flags\n");
|
---|
103 | printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
|
---|
104 | printf(".\\\"\n");
|
---|
105 | t = time(NULL);
|
---|
106 | strftime(timestr, sizeof(timestr), "%B %e, %Y", localtime(&t));
|
---|
107 | printf(".Dd %s\n", timestr);
|
---|
108 | p = strrchr(progname, '/');
|
---|
109 | if(p) p++; else p = progname;
|
---|
110 | strlcpy(cmd, p, sizeof(cmd));
|
---|
111 | strupr(cmd);
|
---|
112 |
|
---|
113 | printf(".Dt %s SECTION\n", cmd);
|
---|
114 | printf(".Os OPERATING_SYSTEM\n");
|
---|
115 | printf(".Sh NAME\n");
|
---|
116 | printf(".Nm %s\n", p);
|
---|
117 | printf(".Nd\n");
|
---|
118 | printf("in search of a description\n");
|
---|
119 | printf(".Sh SYNOPSIS\n");
|
---|
120 | printf(".Nm\n");
|
---|
121 | for(i = 0; i < num_args; i++){
|
---|
122 | /* we seem to hit a limit on number of arguments if doing
|
---|
123 | short and long flags with arguments -- split on two lines */
|
---|
124 | if(ISFLAG(args[i]) ||
|
---|
125 | args[i].short_name == 0 || args[i].long_name == NULL) {
|
---|
126 | printf(".Op ");
|
---|
127 |
|
---|
128 | if(args[i].short_name) {
|
---|
129 | print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
|
---|
130 | printf("Fl %c%s", args[i].short_name, buf);
|
---|
131 | if(args[i].long_name)
|
---|
132 | printf(" | ");
|
---|
133 | }
|
---|
134 | if(args[i].long_name) {
|
---|
135 | print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
|
---|
136 | printf("Fl -%s%s%s",
|
---|
137 | args[i].type == arg_negative_flag ? "no-" : "",
|
---|
138 | args[i].long_name, buf);
|
---|
139 | }
|
---|
140 | printf("\n");
|
---|
141 | } else {
|
---|
142 | print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
|
---|
143 | printf(".Oo Fl %c%s \\*(Ba Xo\n", args[i].short_name, buf);
|
---|
144 | print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
|
---|
145 | printf(".Fl -%s%s\n.Xc\n.Oc\n", args[i].long_name, buf);
|
---|
146 | }
|
---|
147 | /*
|
---|
148 | if(args[i].type == arg_strings)
|
---|
149 | fprintf (stderr, "...");
|
---|
150 | */
|
---|
151 | }
|
---|
152 | if (extra_string && *extra_string)
|
---|
153 | printf (".Ar %s\n", extra_string);
|
---|
154 | printf(".Sh DESCRIPTION\n");
|
---|
155 | printf("Supported options:\n");
|
---|
156 | printf(".Bl -tag -width Ds\n");
|
---|
157 | for(i = 0; i < num_args; i++){
|
---|
158 | printf(".It Xo\n");
|
---|
159 | if(args[i].short_name){
|
---|
160 | printf(".Fl %c", args[i].short_name);
|
---|
161 | print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
|
---|
162 | printf("%s", buf);
|
---|
163 | if(args[i].long_name)
|
---|
164 | printf(" ,");
|
---|
165 | printf("\n");
|
---|
166 | }
|
---|
167 | if(args[i].long_name){
|
---|
168 | printf(".Fl -%s%s",
|
---|
169 | args[i].type == arg_negative_flag ? "no-" : "",
|
---|
170 | args[i].long_name);
|
---|
171 | print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
|
---|
172 | printf("%s\n", buf);
|
---|
173 | }
|
---|
174 | printf(".Xc\n");
|
---|
175 | if(args[i].help)
|
---|
176 | printf("%s\n", args[i].help);
|
---|
177 | /*
|
---|
178 | if(args[i].type == arg_strings)
|
---|
179 | fprintf (stderr, "...");
|
---|
180 | */
|
---|
181 | }
|
---|
182 | printf(".El\n");
|
---|
183 | printf(".\\\".Sh ENVIRONMENT\n");
|
---|
184 | printf(".\\\".Sh FILES\n");
|
---|
185 | printf(".\\\".Sh EXAMPLES\n");
|
---|
186 | printf(".\\\".Sh DIAGNOSTICS\n");
|
---|
187 | printf(".\\\".Sh SEE ALSO\n");
|
---|
188 | printf(".\\\".Sh STANDARDS\n");
|
---|
189 | printf(".\\\".Sh HISTORY\n");
|
---|
190 | printf(".\\\".Sh AUTHORS\n");
|
---|
191 | printf(".\\\".Sh BUGS\n");
|
---|
192 | }
|
---|
193 |
|
---|
194 | static int
|
---|
195 | check_column(FILE *f, int col, int len, int columns)
|
---|
196 | {
|
---|
197 | if(col + len > columns) {
|
---|
198 | fprintf(f, "\n");
|
---|
199 | col = fprintf(f, " ");
|
---|
200 | }
|
---|
201 | return col;
|
---|
202 | }
|
---|
203 |
|
---|
204 | static char *
|
---|
205 | builtin_i18n(const char *str)
|
---|
206 | {
|
---|
207 | return rk_UNCONST(str);
|
---|
208 | }
|
---|
209 |
|
---|
210 | ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
|
---|
211 | arg_printusage (struct getargs *args,
|
---|
212 | size_t num_args,
|
---|
213 | const char *progname,
|
---|
214 | const char *extra_string)
|
---|
215 | {
|
---|
216 | arg_printusage_i18n(args, num_args, "Usage",
|
---|
217 | progname, extra_string, builtin_i18n);
|
---|
218 | }
|
---|
219 |
|
---|
220 | ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
|
---|
221 | arg_printusage_i18n (struct getargs *args,
|
---|
222 | size_t num_args,
|
---|
223 | const char *usage,
|
---|
224 | const char *progname,
|
---|
225 | const char *extra_string,
|
---|
226 | char *(*i18n)(const char *))
|
---|
227 | {
|
---|
228 | size_t i, max_len = 0;
|
---|
229 | char buf[128];
|
---|
230 | int col = 0, columns;
|
---|
231 | struct winsize ws;
|
---|
232 |
|
---|
233 | if (progname == NULL)
|
---|
234 | progname = getprogname();
|
---|
235 |
|
---|
236 | if (i18n == NULL)
|
---|
237 | i18n = builtin_i18n;
|
---|
238 |
|
---|
239 | if(getenv("GETARGMANDOC")){
|
---|
240 | mandoc_template(args, num_args, progname, extra_string, i18n);
|
---|
241 | return;
|
---|
242 | }
|
---|
243 | if(get_window_size(2, &ws) == 0)
|
---|
244 | columns = ws.ws_col;
|
---|
245 | else
|
---|
246 | columns = 80;
|
---|
247 | col = 0;
|
---|
248 | col += fprintf (stderr, "%s: %s", usage, progname);
|
---|
249 | buf[0] = '\0';
|
---|
250 | for (i = 0; i < num_args; ++i) {
|
---|
251 | if(args[i].short_name && ISFLAG(args[i])) {
|
---|
252 | char s[2];
|
---|
253 | if(buf[0] == '\0')
|
---|
254 | strlcpy(buf, "[-", sizeof(buf));
|
---|
255 | s[0] = args[i].short_name;
|
---|
256 | s[1] = '\0';
|
---|
257 | strlcat(buf, s, sizeof(buf));
|
---|
258 | }
|
---|
259 | }
|
---|
260 | if(buf[0] != '\0') {
|
---|
261 | strlcat(buf, "]", sizeof(buf));
|
---|
262 | col = check_column(stderr, col, strlen(buf) + 1, columns);
|
---|
263 | col += fprintf(stderr, " %s", buf);
|
---|
264 | }
|
---|
265 |
|
---|
266 | for (i = 0; i < num_args; ++i) {
|
---|
267 | size_t len = 0;
|
---|
268 |
|
---|
269 | if (args[i].long_name) {
|
---|
270 | buf[0] = '\0';
|
---|
271 | strlcat(buf, "[--", sizeof(buf));
|
---|
272 | len += 2;
|
---|
273 | if(args[i].type == arg_negative_flag) {
|
---|
274 | strlcat(buf, "no-", sizeof(buf));
|
---|
275 | len += 3;
|
---|
276 | }
|
---|
277 | strlcat(buf, args[i].long_name, sizeof(buf));
|
---|
278 | len += strlen(args[i].long_name);
|
---|
279 | len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
---|
280 | 0, 1, &args[i], i18n);
|
---|
281 | strlcat(buf, "]", sizeof(buf));
|
---|
282 | if(args[i].type == arg_strings)
|
---|
283 | strlcat(buf, "...", sizeof(buf));
|
---|
284 | col = check_column(stderr, col, strlen(buf) + 1, columns);
|
---|
285 | col += fprintf(stderr, " %s", buf);
|
---|
286 | }
|
---|
287 | if (args[i].short_name && !ISFLAG(args[i])) {
|
---|
288 | snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
|
---|
289 | len += 2;
|
---|
290 | len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
---|
291 | 0, 0, &args[i], i18n);
|
---|
292 | strlcat(buf, "]", sizeof(buf));
|
---|
293 | if(args[i].type == arg_strings)
|
---|
294 | strlcat(buf, "...", sizeof(buf));
|
---|
295 | col = check_column(stderr, col, strlen(buf) + 1, columns);
|
---|
296 | col += fprintf(stderr, " %s", buf);
|
---|
297 | }
|
---|
298 | if (args[i].long_name && args[i].short_name)
|
---|
299 | len += 2; /* ", " */
|
---|
300 | max_len = max(max_len, len);
|
---|
301 | }
|
---|
302 | if (extra_string) {
|
---|
303 | check_column(stderr, col, strlen(extra_string) + 1, columns);
|
---|
304 | fprintf (stderr, " %s\n", extra_string);
|
---|
305 | } else
|
---|
306 | fprintf (stderr, "\n");
|
---|
307 | for (i = 0; i < num_args; ++i) {
|
---|
308 | if (args[i].help) {
|
---|
309 | size_t count = 0;
|
---|
310 |
|
---|
311 | if (args[i].short_name) {
|
---|
312 | count += fprintf (stderr, "-%c", args[i].short_name);
|
---|
313 | print_arg (buf, sizeof(buf), 0, 0, &args[i], i18n);
|
---|
314 | count += fprintf(stderr, "%s", buf);
|
---|
315 | }
|
---|
316 | if (args[i].short_name && args[i].long_name)
|
---|
317 | count += fprintf (stderr, ", ");
|
---|
318 | if (args[i].long_name) {
|
---|
319 | count += fprintf (stderr, "--");
|
---|
320 | if (args[i].type == arg_negative_flag)
|
---|
321 | count += fprintf (stderr, "no-");
|
---|
322 | count += fprintf (stderr, "%s", args[i].long_name);
|
---|
323 | print_arg (buf, sizeof(buf), 0, 1, &args[i], i18n);
|
---|
324 | count += fprintf(stderr, "%s", buf);
|
---|
325 | }
|
---|
326 | while(count++ <= max_len)
|
---|
327 | putc (' ', stderr);
|
---|
328 | fprintf (stderr, "%s\n", (*i18n)(args[i].help));
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | static int
|
---|
334 | add_string(getarg_strings *s, char *value)
|
---|
335 | {
|
---|
336 | char **strings;
|
---|
337 |
|
---|
338 | strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings));
|
---|
339 | if (strings == NULL) {
|
---|
340 | free(s->strings);
|
---|
341 | s->strings = NULL;
|
---|
342 | s->num_strings = 0;
|
---|
343 | return ENOMEM;
|
---|
344 | }
|
---|
345 | s->strings = strings;
|
---|
346 | s->strings[s->num_strings] = value;
|
---|
347 | s->num_strings++;
|
---|
348 | return 0;
|
---|
349 | }
|
---|
350 |
|
---|
351 | static int
|
---|
352 | arg_match_long(struct getargs *args, size_t num_args,
|
---|
353 | char *argv, int argc, char **rargv, int *goptind)
|
---|
354 | {
|
---|
355 | int i;
|
---|
356 | char *goptarg = NULL;
|
---|
357 | int negate = 0;
|
---|
358 | int partial_match = 0;
|
---|
359 | struct getargs *partial = NULL;
|
---|
360 | struct getargs *current = NULL;
|
---|
361 | int argv_len;
|
---|
362 | char *p;
|
---|
363 | int p_len;
|
---|
364 |
|
---|
365 | argv_len = strlen(argv);
|
---|
366 | p = strchr (argv, '=');
|
---|
367 | if (p != NULL)
|
---|
368 | argv_len = p - argv;
|
---|
369 |
|
---|
370 | for (i = 0; i < num_args; ++i) {
|
---|
371 | if(args[i].long_name) {
|
---|
372 | int len = strlen(args[i].long_name);
|
---|
373 | p = argv;
|
---|
374 | p_len = argv_len;
|
---|
375 | negate = 0;
|
---|
376 |
|
---|
377 | for (;;) {
|
---|
378 | if (strncmp (args[i].long_name, p, p_len) == 0) {
|
---|
379 | if(p_len == len)
|
---|
380 | current = &args[i];
|
---|
381 | else {
|
---|
382 | ++partial_match;
|
---|
383 | partial = &args[i];
|
---|
384 | }
|
---|
385 | goptarg = p + p_len;
|
---|
386 | } else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) {
|
---|
387 | negate = !negate;
|
---|
388 | p += 3;
|
---|
389 | p_len -= 3;
|
---|
390 | continue;
|
---|
391 | }
|
---|
392 | break;
|
---|
393 | }
|
---|
394 | if (current)
|
---|
395 | break;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | if (current == NULL) {
|
---|
399 | if (partial_match == 1)
|
---|
400 | current = partial;
|
---|
401 | else
|
---|
402 | return ARG_ERR_NO_MATCH;
|
---|
403 | }
|
---|
404 |
|
---|
405 | if(*goptarg == '\0'
|
---|
406 | && !ISFLAG(*current)
|
---|
407 | && current->type != arg_collect
|
---|
408 | && current->type != arg_counter)
|
---|
409 | return ARG_ERR_NO_MATCH;
|
---|
410 | switch(current->type){
|
---|
411 | case arg_integer:
|
---|
412 | {
|
---|
413 | int tmp;
|
---|
414 | if(sscanf(goptarg + 1, "%d", &tmp) != 1)
|
---|
415 | return ARG_ERR_BAD_ARG;
|
---|
416 | *(int*)current->value = tmp;
|
---|
417 | return 0;
|
---|
418 | }
|
---|
419 | case arg_string:
|
---|
420 | {
|
---|
421 | *(char**)current->value = goptarg + 1;
|
---|
422 | return 0;
|
---|
423 | }
|
---|
424 | case arg_strings:
|
---|
425 | {
|
---|
426 | return add_string((getarg_strings*)current->value, goptarg + 1);
|
---|
427 | }
|
---|
428 | case arg_flag:
|
---|
429 | case arg_negative_flag:
|
---|
430 | {
|
---|
431 | int *flag = current->value;
|
---|
432 | if(*goptarg == '\0' ||
|
---|
433 | strcmp(goptarg + 1, "yes") == 0 ||
|
---|
434 | strcmp(goptarg + 1, "true") == 0){
|
---|
435 | *flag = !negate;
|
---|
436 | return 0;
|
---|
437 | } else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) {
|
---|
438 | *flag = rk_random() & 1;
|
---|
439 | } else {
|
---|
440 | *flag = negate;
|
---|
441 | return 0;
|
---|
442 | }
|
---|
443 | return ARG_ERR_BAD_ARG;
|
---|
444 | }
|
---|
445 | case arg_counter :
|
---|
446 | {
|
---|
447 | int val;
|
---|
448 |
|
---|
449 | if (*goptarg == '\0')
|
---|
450 | val = 1;
|
---|
451 | else if(sscanf(goptarg + 1, "%d", &val) != 1)
|
---|
452 | return ARG_ERR_BAD_ARG;
|
---|
453 | *(int *)current->value += val;
|
---|
454 | return 0;
|
---|
455 | }
|
---|
456 | case arg_double:
|
---|
457 | {
|
---|
458 | double tmp;
|
---|
459 | if(sscanf(goptarg + 1, "%lf", &tmp) != 1)
|
---|
460 | return ARG_ERR_BAD_ARG;
|
---|
461 | *(double*)current->value = tmp;
|
---|
462 | return 0;
|
---|
463 | }
|
---|
464 | case arg_collect:{
|
---|
465 | struct getarg_collect_info *c = current->value;
|
---|
466 | int o = argv - rargv[*goptind];
|
---|
467 | return (*c->func)(FALSE, argc, rargv, goptind, &o, c->data);
|
---|
468 | }
|
---|
469 |
|
---|
470 | default:
|
---|
471 | abort ();
|
---|
472 | UNREACHABLE(return 0);
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | static int
|
---|
477 | arg_match_short (struct getargs *args, size_t num_args,
|
---|
478 | char *argv, int argc, char **rargv, int *goptind)
|
---|
479 | {
|
---|
480 | int j, k;
|
---|
481 |
|
---|
482 | for(j = 1; j > 0 && j < strlen(rargv[*goptind]); j++) {
|
---|
483 | for(k = 0; k < num_args; k++) {
|
---|
484 | char *goptarg;
|
---|
485 |
|
---|
486 | if(args[k].short_name == 0)
|
---|
487 | continue;
|
---|
488 | if(argv[j] == args[k].short_name) {
|
---|
489 | if(args[k].type == arg_flag) {
|
---|
490 | *(int*)args[k].value = 1;
|
---|
491 | break;
|
---|
492 | }
|
---|
493 | if(args[k].type == arg_negative_flag) {
|
---|
494 | *(int*)args[k].value = 0;
|
---|
495 | break;
|
---|
496 | }
|
---|
497 | if(args[k].type == arg_counter) {
|
---|
498 | ++*(int *)args[k].value;
|
---|
499 | break;
|
---|
500 | }
|
---|
501 | if(args[k].type == arg_collect) {
|
---|
502 | struct getarg_collect_info *c = args[k].value;
|
---|
503 |
|
---|
504 | if((*c->func)(TRUE, argc, rargv, goptind, &j, c->data))
|
---|
505 | return ARG_ERR_BAD_ARG;
|
---|
506 | break;
|
---|
507 | }
|
---|
508 |
|
---|
509 | if(argv[j + 1])
|
---|
510 | goptarg = &argv[j + 1];
|
---|
511 | else {
|
---|
512 | ++*goptind;
|
---|
513 | goptarg = rargv[*goptind];
|
---|
514 | }
|
---|
515 | if(goptarg == NULL) {
|
---|
516 | --*goptind;
|
---|
517 | return ARG_ERR_NO_ARG;
|
---|
518 | }
|
---|
519 | if(args[k].type == arg_integer) {
|
---|
520 | int tmp;
|
---|
521 | if(sscanf(goptarg, "%d", &tmp) != 1)
|
---|
522 | return ARG_ERR_BAD_ARG;
|
---|
523 | *(int*)args[k].value = tmp;
|
---|
524 | return 0;
|
---|
525 | } else if(args[k].type == arg_string) {
|
---|
526 | *(char**)args[k].value = goptarg;
|
---|
527 | return 0;
|
---|
528 | } else if(args[k].type == arg_strings) {
|
---|
529 | return add_string((getarg_strings*)args[k].value, goptarg);
|
---|
530 | } else if(args[k].type == arg_double) {
|
---|
531 | double tmp;
|
---|
532 | if(sscanf(goptarg, "%lf", &tmp) != 1)
|
---|
533 | return ARG_ERR_BAD_ARG;
|
---|
534 | *(double*)args[k].value = tmp;
|
---|
535 | return 0;
|
---|
536 | }
|
---|
537 | return ARG_ERR_BAD_ARG;
|
---|
538 | }
|
---|
539 | }
|
---|
540 | if (k == num_args)
|
---|
541 | return ARG_ERR_NO_MATCH;
|
---|
542 | }
|
---|
543 | return 0;
|
---|
544 | }
|
---|
545 |
|
---|
546 | ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
|
---|
547 | getarg(struct getargs *args, size_t num_args,
|
---|
548 | int argc, char **argv, int *goptind)
|
---|
549 | {
|
---|
550 | int i;
|
---|
551 | int ret = 0;
|
---|
552 |
|
---|
553 | rk_random_init();
|
---|
554 | (*goptind)++;
|
---|
555 | for(i = *goptind; i < argc; i++) {
|
---|
556 | if(argv[i][0] != '-')
|
---|
557 | break;
|
---|
558 | if(argv[i][1] == '-'){
|
---|
559 | if(argv[i][2] == 0){
|
---|
560 | i++;
|
---|
561 | break;
|
---|
562 | }
|
---|
563 | ret = arg_match_long (args, num_args, argv[i] + 2,
|
---|
564 | argc, argv, &i);
|
---|
565 | } else {
|
---|
566 | ret = arg_match_short (args, num_args, argv[i],
|
---|
567 | argc, argv, &i);
|
---|
568 | }
|
---|
569 | if(ret)
|
---|
570 | break;
|
---|
571 | }
|
---|
572 | *goptind = i;
|
---|
573 | return ret;
|
---|
574 | }
|
---|
575 |
|
---|
576 | ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
|
---|
577 | free_getarg_strings (getarg_strings *s)
|
---|
578 | {
|
---|
579 | free (s->strings);
|
---|
580 | }
|
---|
581 |
|
---|
582 | #if TEST
|
---|
583 | int foo_flag = 2;
|
---|
584 | int flag1 = 0;
|
---|
585 | int flag2 = 0;
|
---|
586 | int bar_int;
|
---|
587 | char *baz_string;
|
---|
588 |
|
---|
589 | struct getargs args[] = {
|
---|
590 | { NULL, '1', arg_flag, &flag1, "one", NULL },
|
---|
591 | { NULL, '2', arg_flag, &flag2, "two", NULL },
|
---|
592 | { "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL },
|
---|
593 | { "bar", 'b', arg_integer, &bar_int, "bar", "seconds"},
|
---|
594 | { "baz", 'x', arg_string, &baz_string, "baz", "name" },
|
---|
595 | };
|
---|
596 |
|
---|
597 | int main(int argc, char **argv)
|
---|
598 | {
|
---|
599 | int goptind = 0;
|
---|
600 | while(getarg(args, 5, argc, argv, &goptind))
|
---|
601 | printf("Bad arg: %s\n", argv[goptind]);
|
---|
602 | printf("flag1 = %d\n", flag1);
|
---|
603 | printf("flag2 = %d\n", flag2);
|
---|
604 | printf("foo_flag = %d\n", foo_flag);
|
---|
605 | printf("bar_int = %d\n", bar_int);
|
---|
606 | printf("baz_flag = %s\n", baz_string);
|
---|
607 | arg_printusage (args, 5, argv[0], "nothing here");
|
---|
608 | }
|
---|
609 | #endif
|
---|