2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
17 static struct isl_arg help_arg[] = {
18 ISL_ARG_PHANTOM_BOOL('h', "help", NULL, "print this help, then exit")
22 static void set_default_choice(struct isl_arg *arg, void *opt)
24 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.choice.default_value;
27 static void set_default_flags(struct isl_arg *arg, void *opt)
29 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.flags.default_value;
32 static void set_default_bool(struct isl_arg *arg, void *opt)
34 if (arg->offset == (size_t) -1)
36 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.b.default_value;
39 static void set_default_child(struct isl_arg *arg, void *opt)
41 void *child = calloc(1, arg->u.child.size);
44 isl_arg_set_defaults(arg->u.child.child, child);
46 *(void **)(((char *)opt) + arg->offset) = child;
49 static void set_default_user(struct isl_arg *arg, void *opt)
51 arg->u.user.init(((char *)opt) + arg->offset);
54 static void set_default_long(struct isl_arg *arg, void *opt)
56 *(long *)(((char *)opt) + arg->offset) = arg->u.l.default_value;
59 static void set_default_ulong(struct isl_arg *arg, void *opt)
61 *(unsigned long *)(((char *)opt) + arg->offset) = arg->u.ul.default_value;
64 static void set_default_str(struct isl_arg *arg, void *opt)
66 const char *str = NULL;
67 if (arg->u.str.default_value)
68 str = strdup(arg->u.str.default_value);
69 *(const char **)(((char *)opt) + arg->offset) = str;
72 void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
76 for (i = 0; arg[i].type != isl_arg_end; ++i) {
77 switch (arg[i].type) {
79 set_default_choice(&arg[i], opt);
82 set_default_flags(&arg[i], opt);
85 set_default_bool(&arg[i], opt);
88 set_default_child(&arg[i], opt);
91 set_default_user(&arg[i], opt);
94 set_default_long(&arg[i], opt);
97 set_default_ulong(&arg[i], opt);
101 set_default_str(&arg[i], opt);
104 case isl_arg_version:
111 void isl_arg_free(struct isl_arg *arg, void *opt)
118 for (i = 0; arg[i].type != isl_arg_end; ++i) {
119 switch (arg[i].type) {
121 isl_arg_free(arg[i].u.child.child,
122 *(void **)(((char *)opt) + arg[i].offset));
126 free(*(char **)(((char *)opt) + arg[i].offset));
129 if (arg[i].u.user.clear)
130 arg[i].u.user.clear(((char *)opt) + arg[i].offset);
138 case isl_arg_version:
147 static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
151 if (!decl->long_name) {
152 printf(" -%c", decl->short_name);
156 if (decl->short_name) {
157 printf(" -%c, --", decl->short_name);
159 } else if (decl->flags & ISL_ARG_SINGLE_DASH) {
168 printf("%s-", prefix);
169 len += strlen(prefix) + 1;
175 printf("%s", decl->long_name);
176 len += strlen(decl->long_name);
178 while ((++decl)->type == isl_arg_alias) {
185 printf("%s", decl->long_name);
186 len += strlen(decl->long_name);
192 const void *isl_memrchr(const void *s, int c, size_t n)
201 static int print_help_msg(struct isl_arg *decl, int pos)
210 printf("\n%30s", "");
212 printf("%*s", 30 - pos, "");
217 const char *space = isl_memrchr(s, ' ', 45);
221 space = strchr(s + 45, ' ');
225 printf("%.*s", l, s);
228 printf("\n%30s", "");
235 static void print_default(struct isl_arg *decl, const char *def, int pos)
238 const char *default_prefix = "[default: ";
239 const char *default_suffix = "]";
242 len = strlen(default_prefix) + strlen(def) + strlen(default_suffix);
244 if (!decl->help_msg) {
246 printf("\n%30s", "");
248 printf("%*s", 30 - pos, "");
252 printf("\n%30s", "");
256 printf("%s%s%s", default_prefix, def, default_suffix);
259 static void print_default_choice(struct isl_arg *decl, int pos)
262 const char *s = "none";
264 for (i = 0; decl->u.choice.choice[i].name; ++i)
265 if (decl->u.choice.choice[i].value == decl->u.choice.default_value) {
266 s = decl->u.choice.choice[i].name;
270 print_default(decl, s, pos);
273 static void print_choice_help(struct isl_arg *decl, const char *prefix)
278 pos = print_arg_help(decl, prefix, 0);
282 for (i = 0; decl->u.choice.choice[i].name; ++i) {
287 printf("%s", decl->u.choice.choice[i].name);
288 pos += strlen(decl->u.choice.choice[i].name);
291 pos = print_help_msg(decl, pos);
292 print_default_choice(decl, pos);
297 static void print_default_flags(struct isl_arg *decl, int pos)
300 const char *default_prefix = "[default: ";
301 const char *default_suffix = "]";
302 int len = strlen(default_prefix) + strlen(default_suffix);
304 for (i = 0; decl->u.flags.flags[i].name; ++i)
305 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
306 decl->u.flags.flags[i].value)
307 len += strlen(decl->u.flags.flags[i].name);
309 if (!decl->help_msg) {
311 printf("\n%30s", "");
313 printf("%*s", 30 - pos, "");
317 printf("\n%30s", "");
321 printf("%s", default_prefix);
323 for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
324 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
325 decl->u.flags.flags[i].value) {
328 printf("%s", decl->u.flags.flags[i].name);
332 printf("%s", default_suffix);
335 static void print_flags_help(struct isl_arg *decl, const char *prefix)
340 pos = print_arg_help(decl, prefix, 0);
344 for (i = 0; decl->u.flags.flags[i].name; ++i) {
350 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
356 printf("%s", decl->u.flags.flags[j].name);
357 pos += strlen(decl->u.flags.flags[j].name);
362 pos = print_help_msg(decl, pos);
363 print_default_flags(decl, pos);
368 static void print_bool_help(struct isl_arg *decl, const char *prefix)
371 int no = decl->u.b.default_value == 1;
372 pos = print_arg_help(decl, prefix, no);
373 pos = print_help_msg(decl, pos);
374 if (decl->offset != (size_t) -1)
375 print_default(decl, no ? "yes" : "no", pos);
379 static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
381 printf("%c<%s>", decl->long_name ? '=' : ' ', name);
382 return pos + 3 + strlen(name);
385 static void print_long_help(struct isl_arg *decl, const char *prefix)
388 pos = print_arg_help(decl, prefix, 0);
389 if (decl->u.l.default_value != decl->u.l.default_selected) {
395 if (decl->u.l.default_value != decl->u.l.default_selected) {
399 print_help_msg(decl, pos);
403 static void print_ulong_help(struct isl_arg *decl, const char *prefix)
406 pos = print_arg_help(decl, prefix, 0);
409 print_help_msg(decl, pos);
413 static void print_str_help(struct isl_arg *decl, const char *prefix)
416 const char *a = decl->argument_name ? decl->argument_name : "string";
417 pos = print_arg_help(decl, prefix, 0);
418 pos = print_argument_name(decl, a, pos);
419 pos = print_help_msg(decl, pos);
420 if (decl->u.str.default_value)
421 print_default(decl, decl->u.str.default_value, pos);
425 static void print_help(struct isl_arg *arg, const char *prefix)
429 for (i = 0; arg[i].type != isl_arg_end; ++i) {
430 if (arg[i].flags & ISL_ARG_HIDDEN)
432 switch (arg[i].type) {
434 print_flags_help(&arg[i], prefix);
437 print_choice_help(&arg[i], prefix);
440 print_bool_help(&arg[i], prefix);
443 print_long_help(&arg[i], prefix);
446 print_ulong_help(&arg[i], prefix);
449 print_str_help(&arg[i], prefix);
452 case isl_arg_version:
461 for (i = 0; arg[i].type != isl_arg_end; ++i) {
462 if (arg[i].type != isl_arg_child)
464 if (arg[i].flags & ISL_ARG_HIDDEN)
469 printf(" %s\n", arg[i].help_msg);
470 print_help(arg[i].u.child.child, arg[i].long_name);
474 static const char *prog_name(const char *prog)
478 slash = strrchr(prog, '/');
481 if (strncmp(prog, "lt-", 3) == 0)
487 static int any_version(struct isl_arg *decl)
491 for (i = 0; decl[i].type != isl_arg_end; ++i) {
492 switch (decl[i].type) {
493 case isl_arg_version:
496 if (any_version(decl[i].u.child.child))
507 static void print_help_and_exit(struct isl_arg *arg, const char *prog)
511 printf("Usage: %s [OPTION...]", prog_name(prog));
513 for (i = 0; arg[i].type != isl_arg_end; ++i)
514 if (arg[i].type == isl_arg_arg)
515 printf(" %s", arg[i].argument_name);
519 print_help(arg, NULL);
521 if (any_version(arg))
522 printf(" -V, --version\n");
523 print_bool_help(help_arg, NULL);
528 static int match_long_name(struct isl_arg *decl,
529 const char *start, const char *end)
532 if (end - start == strlen(decl->long_name) &&
533 !strncmp(start, decl->long_name, end - start))
535 } while ((++decl)->type == isl_arg_alias);
540 static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
542 if (!strncmp(arg, "--", 2))
544 if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
549 static const char *skip_name(struct isl_arg *decl, const char *arg,
550 const char *prefix, int need_argument, int *has_argument)
556 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
557 if (need_argument && !arg[2])
560 *has_argument = arg[2] != '\0';
563 if (!decl->long_name)
566 name = skip_dash_dash(decl, arg);
570 equal = strchr(name, '=');
571 if (need_argument && !equal)
575 *has_argument = !!equal;
576 end = equal ? equal : name + strlen(name);
579 size_t prefix_len = strlen(prefix);
580 if (strncmp(name, prefix, prefix_len) == 0 &&
581 name[prefix_len] == '-')
582 name += prefix_len + 1;
585 if (!match_long_name(decl, name, end))
588 return equal ? equal + 1 : end;
591 static int parse_choice_option(struct isl_arg *decl, char **arg,
592 const char *prefix, void *opt)
598 choice = skip_name(decl, arg[0], prefix, 0, &has_argument);
602 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
603 unsigned u = decl->u.choice.default_selected;
604 if (decl->u.choice.set)
605 decl->u.choice.set(opt, u);
607 *(unsigned *)(((char *)opt) + decl->offset) = u;
615 for (i = 0; decl->u.choice.choice[i].name; ++i) {
618 if (strcmp(choice, decl->u.choice.choice[i].name))
621 u = decl->u.choice.choice[i].value;
622 if (decl->u.choice.set)
623 decl->u.choice.set(opt, u);
625 *(unsigned *)(((char *)opt) + decl->offset) = u;
627 return has_argument ? 1 : 2;
633 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
638 for (i = 0; decl->u.flags.flags[i].name; ++i) {
639 if (strncmp(flag, decl->u.flags.flags[i].name, len))
642 *val &= ~decl->u.flags.flags[i].mask;
643 *val |= decl->u.flags.flags[i].value;
651 static int parse_flags_option(struct isl_arg *decl, char **arg,
652 const char *prefix, void *opt)
659 flags = skip_name(decl, arg[0], prefix, 0, &has_argument);
663 if (!has_argument && !arg[1])
669 val = *(unsigned *)(((char *)opt) + decl->offset);
671 while ((comma = strchr(flags, ',')) != NULL) {
672 if (!set_flag(decl, &val, flags, comma - flags))
676 if (!set_flag(decl, &val, flags, strlen(flags)))
679 *(unsigned *)(((char *)opt) + decl->offset) = val;
681 return has_argument ? 1 : 2;
684 static int parse_bool_option(struct isl_arg *decl, char **arg,
685 const char *prefix, void *opt)
688 unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
690 if (skip_name(decl, arg[0], prefix, 0, NULL)) {
691 if ((decl->flags && ISL_ARG_BOOL_ARG) && arg[1]) {
693 int val = strtol(arg[1], &endptr, 0);
694 if (*endptr == '\0' && (val == 0 || val == 1)) {
696 decl->u.b.set(opt, val);
697 else if (decl->offset != (size_t) -1)
703 decl->u.b.set(opt, 1);
704 else if (decl->offset != (size_t) -1)
710 if (!decl->long_name)
713 name = skip_dash_dash(decl, arg[0]);
718 size_t prefix_len = strlen(prefix);
719 if (strncmp(name, prefix, prefix_len) == 0 &&
720 name[prefix_len] == '-') {
721 name += prefix_len + 1;
726 if (strncmp(name, "no-", 3))
731 size_t prefix_len = strlen(prefix);
732 if (strncmp(name, prefix, prefix_len) == 0 &&
733 name[prefix_len] == '-')
734 name += prefix_len + 1;
737 if (match_long_name(decl, name, name + strlen(name))) {
739 decl->u.b.set(opt, 0);
740 else if (decl->offset != (size_t) -1)
749 static int parse_str_option(struct isl_arg *decl, char **arg,
750 const char *prefix, void *opt)
754 char **p = (char **)(((char *)opt) + decl->offset);
756 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
775 static int parse_long_option(struct isl_arg *decl, char **arg,
776 const char *prefix, void *opt)
781 long *p = (long *)(((char *)opt) + decl->offset);
783 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
788 long l = strtol(val, NULL, 0);
790 decl->u.l.set(opt, l);
797 long l = strtol(arg[1], &endptr, 0);
798 if (*endptr == '\0') {
800 decl->u.l.set(opt, l);
807 if (decl->u.l.default_value != decl->u.l.default_selected) {
809 decl->u.l.set(opt, decl->u.l.default_selected);
811 *p = decl->u.l.default_selected;
818 static int parse_ulong_option(struct isl_arg *decl, char **arg,
819 const char *prefix, void *opt)
824 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
826 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
831 *p = strtoul(val, NULL, 0);
836 unsigned long ul = strtoul(arg[1], &endptr, 0);
837 if (*endptr == '\0') {
846 static int parse_option(struct isl_arg *decl, char **arg,
847 const char *prefix, void *opt);
849 static int parse_child_option(struct isl_arg *decl, char **arg, void *opt)
851 return parse_option(decl->u.child.child, arg, decl->long_name,
852 *(void **)(((char *)opt) + decl->offset));
855 static int parse_option(struct isl_arg *decl, char **arg,
856 const char *prefix, void *opt)
860 for (i = 0; decl[i].type != isl_arg_end; ++i) {
862 switch (decl[i].type) {
864 parsed = parse_choice_option(&decl[i], arg, prefix, opt);
867 parsed = parse_flags_option(&decl[i], arg, prefix, opt);
870 parsed = parse_long_option(&decl[i], arg, prefix, opt);
873 parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
876 parsed = parse_bool_option(&decl[i], arg, prefix, opt);
879 parsed = parse_str_option(&decl[i], arg, prefix, opt);
882 parsed = parse_child_option(&decl[i], arg, opt);
887 case isl_arg_version:
898 static void print_version(struct isl_arg *decl)
902 for (i = 0; decl[i].type != isl_arg_end; ++i) {
903 switch (decl[i].type) {
904 case isl_arg_version:
905 decl[i].u.version.print_version();
908 print_version(decl[i].u.child.child);
916 static void print_version_and_exit(struct isl_arg *decl)
923 static int drop_argument(int argc, char **argv, int drop, int n)
925 for (; drop < argc; ++drop)
926 argv[drop] = argv[drop + n];
931 static int n_arg(struct isl_arg *arg)
936 for (i = 0; arg[i].type != isl_arg_end; ++i)
937 if (arg[i].type == isl_arg_arg)
943 static int next_arg(struct isl_arg *arg, int a)
945 for (++a; arg[a].type != isl_arg_end; ++a)
946 if (arg[a].type == isl_arg_arg)
952 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
962 for (i = 1; i < argc; ++i) {
963 if (strcmp(argv[i], "--help") == 0)
964 print_help_and_exit(arg, argv[0]);
967 for (i = 1; i < argc; ++i) {
968 if ((strcmp(argv[i], "--version") == 0 ||
969 strcmp(argv[i], "-V") == 0) && any_version(arg))
970 print_version_and_exit(arg);
973 while (argc > 1 + skip) {
975 if (argv[1 + skip][0] != '-') {
976 a = next_arg(arg, a);
979 p = (char **)(((char *)opt)+arg[a].offset);
981 *p = strdup(argv[1 + skip]);
982 argc = drop_argument(argc, argv, 1 + skip, 1);
984 } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
985 fprintf(stderr, "%s: extra argument: %s\n",
986 prog_name(argv[0]), argv[1 + skip]);
992 parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
994 argc = drop_argument(argc, argv, 1 + skip, parsed);
995 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
996 fprintf(stderr, "%s: unrecognized option: %s\n",
997 prog_name(argv[0]), argv[1 + skip]);
1004 fprintf(stderr, "%s: expecting %d more argument(s)\n",
1005 prog_name(argv[0]), n);