Use "Fl Fl" for long options.
[heimdal.git] / lib / roken / getarg.c
blob9f2f608f69e73b75eef804b8d664d0ecaa4a942a
1 /*
2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <config.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "roken.h"
40 #include "getarg.h"
42 #define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
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 *))
52 const char *s;
54 *string = '\0';
56 if (ISFLAG(*arg) || (!longp && arg->type == arg_counter))
57 return 0;
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);
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>";
83 strlcat(string, s, len);
84 return 1 + strlen(s);
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 *))
94 size_t i;
95 char timestr[64], cmd[64];
96 char buf[128];
97 const char *p;
98 time_t t;
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);
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 ");
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(" | ");
134 if(args[i].long_name) {
135 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
136 printf("Fl Fl %s%s%s",
137 args[i].type == arg_negative_flag ? "no-" : "",
138 args[i].long_name, buf);
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 Fl %s%s\n.Xc\n.Oc\n", args[i].long_name, buf);
148 if(args[i].type == arg_strings)
149 fprintf (stderr, "...");
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");
167 if(args[i].long_name){
168 printf(".Fl 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);
174 printf(".Xc\n");
175 if(args[i].help)
176 printf("%s\n", args[i].help);
178 if(args[i].type == arg_strings)
179 fprintf (stderr, "...");
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");
194 static int
195 check_column(FILE *f, int col, int len, int columns)
197 if(col + len > columns) {
198 fprintf(f, "\n");
199 col = fprintf(f, " ");
201 return col;
204 static char *
205 builtin_i18n(const char *str)
207 return rk_UNCONST(str);
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)
216 arg_printusage_i18n(args, num_args, "Usage",
217 progname, extra_string, builtin_i18n);
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 *))
228 size_t i, max_len = 0;
229 char buf[128];
230 int col = 0, columns;
232 if (progname == NULL)
233 progname = getprogname();
235 if (i18n == NULL)
236 i18n = builtin_i18n;
238 if(getenv("GETARGMANDOC")){
239 mandoc_template(args, num_args, progname, extra_string, i18n);
240 return;
242 if(get_window_size(2, NULL, &columns) == -1)
243 columns = 80;
244 col = 0;
245 col += fprintf (stderr, "%s: %s", usage, progname);
246 buf[0] = '\0';
247 for (i = 0; i < num_args; ++i) {
248 if(args[i].short_name && ISFLAG(args[i])) {
249 char s[2];
250 if(buf[0] == '\0')
251 strlcpy(buf, "[-", sizeof(buf));
252 s[0] = args[i].short_name;
253 s[1] = '\0';
254 strlcat(buf, s, sizeof(buf));
257 if(buf[0] != '\0') {
258 strlcat(buf, "]", sizeof(buf));
259 col = check_column(stderr, col, strlen(buf) + 1, columns);
260 col += fprintf(stderr, " %s", buf);
263 for (i = 0; i < num_args; ++i) {
264 size_t len = 0;
266 if (args[i].long_name) {
267 buf[0] = '\0';
268 strlcat(buf, "[--", sizeof(buf));
269 len += 2;
270 if(args[i].type == arg_negative_flag) {
271 strlcat(buf, "no-", sizeof(buf));
272 len += 3;
274 strlcat(buf, args[i].long_name, sizeof(buf));
275 len += strlen(args[i].long_name);
276 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
277 0, 1, &args[i], i18n);
278 strlcat(buf, "]", sizeof(buf));
279 if(args[i].type == arg_strings)
280 strlcat(buf, "...", sizeof(buf));
281 col = check_column(stderr, col, strlen(buf) + 1, columns);
282 col += fprintf(stderr, " %s", buf);
284 if (args[i].short_name && !ISFLAG(args[i])) {
285 snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
286 len += 2;
287 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
288 0, 0, &args[i], i18n);
289 strlcat(buf, "]", sizeof(buf));
290 if(args[i].type == arg_strings)
291 strlcat(buf, "...", sizeof(buf));
292 col = check_column(stderr, col, strlen(buf) + 1, columns);
293 col += fprintf(stderr, " %s", buf);
295 if (args[i].long_name && args[i].short_name)
296 len += 2; /* ", " */
297 max_len = max(max_len, len);
299 if (extra_string) {
300 check_column(stderr, col, strlen(extra_string) + 1, columns);
301 fprintf (stderr, " %s\n", extra_string);
302 } else
303 fprintf (stderr, "\n");
304 for (i = 0; i < num_args; ++i) {
305 if (args[i].help) {
306 size_t count = 0;
308 if (args[i].short_name) {
309 count += fprintf (stderr, "-%c", args[i].short_name);
310 print_arg (buf, sizeof(buf), 0, 0, &args[i], i18n);
311 count += fprintf(stderr, "%s", buf);
313 if (args[i].short_name && args[i].long_name)
314 count += fprintf (stderr, ", ");
315 if (args[i].long_name) {
316 count += fprintf (stderr, "--");
317 if (args[i].type == arg_negative_flag)
318 count += fprintf (stderr, "no-");
319 count += fprintf (stderr, "%s", args[i].long_name);
320 print_arg (buf, sizeof(buf), 0, 1, &args[i], i18n);
321 count += fprintf(stderr, "%s", buf);
323 while(count++ <= max_len)
324 putc (' ', stderr);
325 fprintf (stderr, "%s\n", (*i18n)(args[i].help));
330 static int
331 add_string(getarg_strings *s, char *value)
333 char **strings;
335 strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings));
336 if (strings == NULL) {
337 free(s->strings);
338 s->strings = NULL;
339 s->num_strings = 0;
340 return ENOMEM;
342 s->strings = strings;
343 s->strings[s->num_strings] = value;
344 s->num_strings++;
345 return 0;
348 static int
349 arg_match_long(struct getargs *args, size_t num_args,
350 char *argv, int argc, char **rargv, int *goptind)
352 size_t i;
353 char *goptarg = NULL;
354 int negate = 0;
355 int partial_match = 0;
356 struct getargs *partial = NULL;
357 struct getargs *current = NULL;
358 int argv_len;
359 char *p;
360 int p_len;
362 argv_len = strlen(argv);
363 p = strchr (argv, '=');
364 if (p != NULL)
365 argv_len = p - argv;
367 for (i = 0; i < num_args; ++i) {
368 if(args[i].long_name) {
369 int len = strlen(args[i].long_name);
370 p = argv;
371 p_len = argv_len;
372 negate = 0;
374 for (;;) {
375 if (strncmp (args[i].long_name, p, p_len) == 0) {
376 if(p_len == len)
377 current = &args[i];
378 else {
379 ++partial_match;
380 partial = &args[i];
382 goptarg = p + p_len;
383 } else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) {
384 negate = !negate;
385 p += 3;
386 p_len -= 3;
387 continue;
389 break;
391 if (current)
392 break;
395 if (current == NULL) {
396 if (partial_match == 1)
397 current = partial;
398 else
399 return ARG_ERR_NO_MATCH;
402 if(*goptarg == '\0'
403 && !ISFLAG(*current)
404 && current->type != arg_collect
405 && current->type != arg_counter)
406 return ARG_ERR_NO_MATCH;
407 switch(current->type){
408 case arg_integer:
410 int tmp;
411 if(sscanf(goptarg + 1, "%d", &tmp) != 1)
412 return ARG_ERR_BAD_ARG;
413 *(int*)current->value = tmp;
414 return 0;
416 case arg_string:
418 *(char**)current->value = goptarg + 1;
419 return 0;
421 case arg_strings:
423 return add_string((getarg_strings*)current->value, goptarg + 1);
425 case arg_flag:
426 case arg_negative_flag:
428 int *flag = current->value;
429 if(*goptarg == '\0' ||
430 strcmp(goptarg + 1, "yes") == 0 ||
431 strcmp(goptarg + 1, "true") == 0){
432 *flag = !negate;
433 return 0;
434 } else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) {
435 *flag = rk_random() & 1;
436 } else {
437 *flag = negate;
438 return 0;
440 return ARG_ERR_BAD_ARG;
442 case arg_counter :
444 int val;
446 if (*goptarg == '\0')
447 val = 1;
448 else if(sscanf(goptarg + 1, "%d", &val) != 1)
449 return ARG_ERR_BAD_ARG;
450 *(int *)current->value += val;
451 return 0;
453 case arg_double:
455 double tmp;
456 if(sscanf(goptarg + 1, "%lf", &tmp) != 1)
457 return ARG_ERR_BAD_ARG;
458 *(double*)current->value = tmp;
459 return 0;
461 case arg_collect:{
462 struct getarg_collect_info *c = current->value;
463 int o = argv - rargv[*goptind];
464 return (*c->func)(FALSE, argc, rargv, goptind, &o, c->data);
467 default:
468 abort ();
469 UNREACHABLE(return 0);
473 static int
474 arg_match_short (struct getargs *args, size_t num_args,
475 char *argv, int argc, char **rargv, int *goptind)
477 size_t j, k;
479 for(j = 1; j > 0 && j < strlen(rargv[*goptind]); j++) {
480 for(k = 0; k < num_args; k++) {
481 char *goptarg;
483 if(args[k].short_name == 0)
484 continue;
485 if(argv[j] == args[k].short_name) {
486 if(args[k].type == arg_flag) {
487 *(int*)args[k].value = 1;
488 break;
490 if(args[k].type == arg_negative_flag) {
491 *(int*)args[k].value = 0;
492 break;
494 if(args[k].type == arg_counter) {
495 ++*(int *)args[k].value;
496 break;
498 if(args[k].type == arg_collect) {
499 struct getarg_collect_info *c = args[k].value;
500 int a = (int)j;
502 if((*c->func)(TRUE, argc, rargv, goptind, &a, c->data))
503 return ARG_ERR_BAD_ARG;
504 j = a;
505 break;
508 if(argv[j + 1])
509 goptarg = &argv[j + 1];
510 else {
511 ++*goptind;
512 goptarg = rargv[*goptind];
514 if(goptarg == NULL) {
515 --*goptind;
516 return ARG_ERR_NO_ARG;
518 if(args[k].type == arg_integer) {
519 int tmp;
520 if(sscanf(goptarg, "%d", &tmp) != 1)
521 return ARG_ERR_BAD_ARG;
522 *(int*)args[k].value = tmp;
523 return 0;
524 } else if(args[k].type == arg_string) {
525 *(char**)args[k].value = goptarg;
526 return 0;
527 } else if(args[k].type == arg_strings) {
528 return add_string((getarg_strings*)args[k].value, goptarg);
529 } else if(args[k].type == arg_double) {
530 double tmp;
531 if(sscanf(goptarg, "%lf", &tmp) != 1)
532 return ARG_ERR_BAD_ARG;
533 *(double*)args[k].value = tmp;
534 return 0;
536 return ARG_ERR_BAD_ARG;
539 if (k == num_args)
540 return ARG_ERR_NO_MATCH;
542 return 0;
545 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
546 getarg(struct getargs *args, size_t num_args,
547 int argc, char **argv, int *goptind)
549 int i;
550 int ret = 0;
552 rk_random_init();
553 (*goptind)++;
554 for(i = *goptind; i < argc; i++) {
555 if(argv[i][0] != '-')
556 break;
557 if(argv[i][1] == '-'){
558 if(argv[i][2] == 0){
559 i++;
560 break;
562 ret = arg_match_long (args, num_args, argv[i] + 2,
563 argc, argv, &i);
564 } else {
565 ret = arg_match_short (args, num_args, argv[i],
566 argc, argv, &i);
568 if(ret)
569 break;
571 *goptind = i;
572 return ret;
575 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
576 free_getarg_strings (getarg_strings *s)
578 free (s->strings);
581 #if TEST
582 int foo_flag = 2;
583 int flag1 = 0;
584 int flag2 = 0;
585 int bar_int;
586 char *baz_string;
588 struct getargs args[] = {
589 { NULL, '1', arg_flag, &flag1, "one", NULL },
590 { NULL, '2', arg_flag, &flag2, "two", NULL },
591 { "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL },
592 { "bar", 'b', arg_integer, &bar_int, "bar", "seconds"},
593 { "baz", 'x', arg_string, &baz_string, "baz", "name" },
596 int main(int argc, char **argv)
598 int goptind = 0;
599 while(getarg(args, 5, argc, argv, &goptind))
600 printf("Bad arg: %s\n", argv[goptind]);
601 printf("flag1 = %d\n", flag1);
602 printf("flag2 = %d\n", flag2);
603 printf("foo_flag = %d\n", foo_flag);
604 printf("bar_int = %d\n", bar_int);
605 printf("baz_flag = %s\n", baz_string);
606 arg_printusage (args, 5, argv[0], "nothing here");
608 #endif