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