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)
43 if (arg->offset == (size_t) -1)
46 child = calloc(1, arg->u.child.size);
47 *(void **)(((char *)opt) + arg->offset) = child;
51 isl_arg_set_defaults(arg->u.child.child, child);
54 static void set_default_user(struct isl_arg *arg, void *opt)
56 arg->u.user.init(((char *)opt) + arg->offset);
59 static void set_default_int(struct isl_arg *arg, void *opt)
61 *(int *)(((char *)opt) + arg->offset) = arg->u.i.default_value;
64 static void set_default_long(struct isl_arg *arg, void *opt)
66 *(long *)(((char *)opt) + arg->offset) = arg->u.l.default_value;
69 static void set_default_ulong(struct isl_arg *arg, void *opt)
71 *(unsigned long *)(((char *)opt) + arg->offset) = arg->u.ul.default_value;
74 static void set_default_str(struct isl_arg *arg, void *opt)
76 const char *str = NULL;
77 if (arg->u.str.default_value)
78 str = strdup(arg->u.str.default_value);
79 *(const char **)(((char *)opt) + arg->offset) = str;
82 void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
86 for (i = 0; arg[i].type != isl_arg_end; ++i) {
87 switch (arg[i].type) {
89 set_default_choice(&arg[i], opt);
92 set_default_flags(&arg[i], opt);
95 set_default_bool(&arg[i], opt);
98 set_default_child(&arg[i], opt);
101 set_default_user(&arg[i], opt);
104 set_default_int(&arg[i], opt);
107 set_default_long(&arg[i], opt);
110 set_default_ulong(&arg[i], opt);
114 set_default_str(&arg[i], opt);
118 case isl_arg_version:
125 static void free_args(struct isl_arg *arg, void *opt)
129 for (i = 0; arg[i].type != isl_arg_end; ++i) {
130 switch (arg[i].type) {
132 if (arg[i].offset == (size_t) -1)
133 free_args(arg[i].u.child.child, opt);
135 isl_arg_free(arg[i].u.child.child,
136 *(void **)(((char *)opt) + arg[i].offset));
140 free(*(char **)(((char *)opt) + arg[i].offset));
143 if (arg[i].u.user.clear)
144 arg[i].u.user.clear(((char *)opt) + arg[i].offset);
153 case isl_arg_version:
161 void isl_arg_free(struct isl_arg *arg, void *opt)
171 static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
175 if (!decl->long_name) {
176 printf(" -%c", decl->short_name);
180 if (decl->short_name) {
181 printf(" -%c, --", decl->short_name);
183 } else if (decl->flags & ISL_ARG_SINGLE_DASH) {
192 printf("%s-", prefix);
193 len += strlen(prefix) + 1;
199 printf("%s", decl->long_name);
200 len += strlen(decl->long_name);
202 while ((++decl)->type == isl_arg_alias) {
209 printf("%s", decl->long_name);
210 len += strlen(decl->long_name);
216 const void *isl_memrchr(const void *s, int c, size_t n)
225 static int wrap_msg(const char *s, int indent, int pos)
228 int wrap_len = 75 - indent;
230 if (pos + 1 >= indent)
231 printf("\n%*s", indent, "");
233 printf("%*s", indent - pos, "");
236 while (len > wrap_len) {
237 const char *space = isl_memrchr(s, ' ', wrap_len);
241 space = strchr(s + wrap_len, ' ');
245 printf("%.*s", l, s);
248 printf("\n%*s", indent, "");
255 static int print_help_msg(struct isl_arg *decl, int pos)
260 return wrap_msg(decl->help_msg, 30, pos);
263 static void print_default(struct isl_arg *decl, const char *def, int pos)
265 const char *default_prefix = "[default: ";
266 const char *default_suffix = "]";
269 len = strlen(default_prefix) + strlen(def) + strlen(default_suffix);
271 if (!decl->help_msg) {
273 printf("\n%30s", "");
275 printf("%*s", 30 - pos, "");
279 printf("\n%30s", "");
283 printf("%s%s%s", default_prefix, def, default_suffix);
286 static void print_default_choice(struct isl_arg *decl, void *opt, int pos)
289 const char *s = "none";
292 p = (unsigned *)(((char *) opt) + decl->offset);
293 for (i = 0; decl->u.choice.choice[i].name; ++i)
294 if (decl->u.choice.choice[i].value == *p) {
295 s = decl->u.choice.choice[i].name;
299 print_default(decl, s, pos);
302 static void print_choice_help(struct isl_arg *decl, const char *prefix,
308 pos = print_arg_help(decl, prefix, 0);
312 for (i = 0; decl->u.choice.choice[i].name; ++i) {
317 printf("%s", decl->u.choice.choice[i].name);
318 pos += strlen(decl->u.choice.choice[i].name);
321 pos = print_help_msg(decl, pos);
322 print_default_choice(decl, opt, pos);
327 static void print_default_flags(struct isl_arg *decl, void *opt, int pos)
330 const char *default_prefix = "[default: ";
331 const char *default_suffix = "]";
332 int len = strlen(default_prefix) + strlen(default_suffix);
335 p = (unsigned *)(((char *) opt) + decl->offset);
336 for (i = 0; decl->u.flags.flags[i].name; ++i)
337 if ((*p & decl->u.flags.flags[i].mask) ==
338 decl->u.flags.flags[i].value)
339 len += strlen(decl->u.flags.flags[i].name);
341 if (!decl->help_msg) {
343 printf("\n%30s", "");
345 printf("%*s", 30 - pos, "");
349 printf("\n%30s", "");
353 printf("%s", default_prefix);
355 for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
356 if ((*p & decl->u.flags.flags[i].mask) ==
357 decl->u.flags.flags[i].value) {
360 printf("%s", decl->u.flags.flags[i].name);
364 printf("%s", default_suffix);
367 static void print_flags_help(struct isl_arg *decl, const char *prefix,
373 pos = print_arg_help(decl, prefix, 0);
377 for (i = 0; decl->u.flags.flags[i].name; ++i) {
383 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
389 printf("%s", decl->u.flags.flags[j].name);
390 pos += strlen(decl->u.flags.flags[j].name);
395 pos = print_help_msg(decl, pos);
396 print_default_flags(decl, opt, pos);
401 static void print_bool_help(struct isl_arg *decl, const char *prefix, void *opt)
404 unsigned *p = opt ? (unsigned *)(((char *) opt) + decl->offset) : NULL;
405 int no = p ? *p == 1 : 0;
406 pos = print_arg_help(decl, prefix, no);
407 pos = print_help_msg(decl, pos);
408 if (decl->offset != (size_t) -1)
409 print_default(decl, no ? "yes" : "no", pos);
413 static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
415 printf("%c<%s>", decl->long_name ? '=' : ' ', name);
416 return pos + 3 + strlen(name);
419 static void print_int_help(struct isl_arg *decl, const char *prefix, void *opt)
423 int *p = (int *)(((char *) opt) + decl->offset);
424 pos = print_arg_help(decl, prefix, 0);
425 pos = print_argument_name(decl, decl->argument_name, pos);
426 pos = print_help_msg(decl, pos);
427 snprintf(val, sizeof(val), "%d", *p);
428 print_default(decl, val, pos);
432 static void print_long_help(struct isl_arg *decl, const char *prefix, void *opt)
435 long *p = (long *)(((char *) opt) + decl->offset);
436 pos = print_arg_help(decl, prefix, 0);
437 if (*p != decl->u.l.default_selected) {
443 if (*p != decl->u.l.default_selected) {
447 print_help_msg(decl, pos);
451 static void print_ulong_help(struct isl_arg *decl, const char *prefix)
454 pos = print_arg_help(decl, prefix, 0);
457 print_help_msg(decl, pos);
461 static void print_str_help(struct isl_arg *decl, const char *prefix, void *opt)
464 const char *a = decl->argument_name ? decl->argument_name : "string";
465 const char **p = (const char **)(((char *) opt) + decl->offset);
466 pos = print_arg_help(decl, prefix, 0);
467 pos = print_argument_name(decl, a, pos);
468 pos = print_help_msg(decl, pos);
470 print_default(decl, *p, pos);
474 static void print_help(struct isl_arg *arg, const char *prefix, void *opt)
479 for (i = 0; arg[i].type != isl_arg_end; ++i) {
480 if (arg[i].flags & ISL_ARG_HIDDEN)
482 switch (arg[i].type) {
484 print_flags_help(&arg[i], prefix, opt);
488 print_choice_help(&arg[i], prefix, opt);
492 print_bool_help(&arg[i], prefix, opt);
496 print_int_help(&arg[i], prefix, opt);
500 print_long_help(&arg[i], prefix, opt);
504 print_ulong_help(&arg[i], prefix);
508 print_str_help(&arg[i], prefix, opt);
512 case isl_arg_version:
522 for (i = 0; arg[i].type != isl_arg_end; ++i) {
525 if (arg[i].type != isl_arg_child)
527 if (arg[i].flags & ISL_ARG_HIDDEN)
533 printf(" %s\n", arg[i].help_msg);
534 if (arg[i].offset == (size_t) -1)
537 child = *(void **)(((char *) opt) + arg[i].offset);
538 print_help(arg[i].u.child.child, arg[i].long_name, child);
543 static const char *prog_name(const char *prog)
547 slash = strrchr(prog, '/');
550 if (strncmp(prog, "lt-", 3) == 0)
556 static int any_version(struct isl_arg *decl)
560 for (i = 0; decl[i].type != isl_arg_end; ++i) {
561 switch (decl[i].type) {
562 case isl_arg_version:
565 if (any_version(decl[i].u.child.child))
576 static void print_help_and_exit(struct isl_arg *arg, const char *prog,
581 printf("Usage: %s [OPTION...]", prog_name(prog));
583 for (i = 0; arg[i].type != isl_arg_end; ++i)
584 if (arg[i].type == isl_arg_arg)
585 printf(" %s", arg[i].argument_name);
589 print_help(arg, NULL, opt);
591 if (any_version(arg))
592 printf(" -V, --version\n");
593 print_bool_help(help_arg, NULL, NULL);
595 for (i = 0; arg[i].type != isl_arg_end; ++i) {
596 if (arg[i].type != isl_arg_footer)
598 wrap_msg(arg[i].help_msg, 0, 0);
605 static int match_long_name(struct isl_arg *decl,
606 const char *start, const char *end)
609 if (end - start == strlen(decl->long_name) &&
610 !strncmp(start, decl->long_name, end - start))
612 } while ((++decl)->type == isl_arg_alias);
617 static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
619 if (!strncmp(arg, "--", 2))
621 if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
626 static const char *skip_name(struct isl_arg *decl, const char *arg,
627 const char *prefix, int need_argument, int *has_argument)
633 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
634 if (need_argument && !arg[2])
637 *has_argument = arg[2] != '\0';
640 if (!decl->long_name)
643 name = skip_dash_dash(decl, arg);
647 equal = strchr(name, '=');
648 if (need_argument && !equal)
652 *has_argument = !!equal;
653 end = equal ? equal : name + strlen(name);
656 size_t prefix_len = strlen(prefix);
657 if (strncmp(name, prefix, prefix_len) == 0 &&
658 name[prefix_len] == '-')
659 name += prefix_len + 1;
662 if (!match_long_name(decl, name, end))
665 return equal ? equal + 1 : end;
668 static int parse_choice_option(struct isl_arg *decl, char **arg,
669 const char *prefix, void *opt)
675 choice = skip_name(decl, arg[0], prefix, 0, &has_argument);
679 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
680 unsigned u = decl->u.choice.default_selected;
681 if (decl->u.choice.set)
682 decl->u.choice.set(opt, u);
684 *(unsigned *)(((char *)opt) + decl->offset) = u;
692 for (i = 0; decl->u.choice.choice[i].name; ++i) {
695 if (strcmp(choice, decl->u.choice.choice[i].name))
698 u = decl->u.choice.choice[i].value;
699 if (decl->u.choice.set)
700 decl->u.choice.set(opt, u);
702 *(unsigned *)(((char *)opt) + decl->offset) = u;
704 return has_argument ? 1 : 2;
710 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
715 for (i = 0; decl->u.flags.flags[i].name; ++i) {
716 if (strncmp(flag, decl->u.flags.flags[i].name, len))
719 *val &= ~decl->u.flags.flags[i].mask;
720 *val |= decl->u.flags.flags[i].value;
728 static int parse_flags_option(struct isl_arg *decl, char **arg,
729 const char *prefix, void *opt)
736 flags = skip_name(decl, arg[0], prefix, 0, &has_argument);
740 if (!has_argument && !arg[1])
746 val = *(unsigned *)(((char *)opt) + decl->offset);
748 while ((comma = strchr(flags, ',')) != NULL) {
749 if (!set_flag(decl, &val, flags, comma - flags))
753 if (!set_flag(decl, &val, flags, strlen(flags)))
756 *(unsigned *)(((char *)opt) + decl->offset) = val;
758 return has_argument ? 1 : 2;
761 static int parse_bool_option(struct isl_arg *decl, char **arg,
762 const char *prefix, void *opt)
765 unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
767 if (skip_name(decl, arg[0], prefix, 0, NULL)) {
768 if ((decl->flags & ISL_ARG_BOOL_ARG) && arg[1]) {
770 int val = strtol(arg[1], &endptr, 0);
771 if (*endptr == '\0' && (val == 0 || val == 1)) {
773 decl->u.b.set(opt, val);
774 else if (decl->offset != (size_t) -1)
780 decl->u.b.set(opt, 1);
781 else if (decl->offset != (size_t) -1)
787 if (!decl->long_name)
790 name = skip_dash_dash(decl, arg[0]);
795 size_t prefix_len = strlen(prefix);
796 if (strncmp(name, prefix, prefix_len) == 0 &&
797 name[prefix_len] == '-') {
798 name += prefix_len + 1;
803 if (strncmp(name, "no-", 3))
808 size_t prefix_len = strlen(prefix);
809 if (strncmp(name, prefix, prefix_len) == 0 &&
810 name[prefix_len] == '-')
811 name += prefix_len + 1;
814 if (match_long_name(decl, name, name + strlen(name))) {
816 decl->u.b.set(opt, 0);
817 else if (decl->offset != (size_t) -1)
826 static int parse_str_option(struct isl_arg *decl, char **arg,
827 const char *prefix, void *opt)
831 char **p = (char **)(((char *)opt) + decl->offset);
833 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
852 static int parse_int_option(struct isl_arg *decl, char **arg,
853 const char *prefix, void *opt)
858 int *p = (int *)(((char *)opt) + decl->offset);
860 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
870 int i = strtol(arg[1], &endptr, 0);
871 if (*endptr == '\0') {
880 static int parse_long_option(struct isl_arg *decl, char **arg,
881 const char *prefix, void *opt)
886 long *p = (long *)(((char *)opt) + decl->offset);
888 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
893 long l = strtol(val, NULL, 0);
895 decl->u.l.set(opt, l);
902 long l = strtol(arg[1], &endptr, 0);
903 if (*endptr == '\0') {
905 decl->u.l.set(opt, l);
912 if (decl->u.l.default_value != decl->u.l.default_selected) {
914 decl->u.l.set(opt, decl->u.l.default_selected);
916 *p = decl->u.l.default_selected;
923 static int parse_ulong_option(struct isl_arg *decl, char **arg,
924 const char *prefix, void *opt)
929 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
931 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
936 *p = strtoul(val, NULL, 0);
941 unsigned long ul = strtoul(arg[1], &endptr, 0);
942 if (*endptr == '\0') {
951 static int parse_option(struct isl_arg *decl, char **arg,
952 const char *prefix, void *opt);
954 static int parse_child_option(struct isl_arg *decl, char **arg,
955 const char *prefix, void *opt)
959 if (decl->offset == (size_t) -1)
962 child = *(void **)(((char *)opt) + decl->offset);
963 prefix = decl->long_name;
965 return parse_option(decl->u.child.child, arg, prefix, child);
968 static int parse_option(struct isl_arg *decl, char **arg,
969 const char *prefix, void *opt)
973 for (i = 0; decl[i].type != isl_arg_end; ++i) {
975 switch (decl[i].type) {
977 parsed = parse_choice_option(&decl[i], arg, prefix, opt);
980 parsed = parse_flags_option(&decl[i], arg, prefix, opt);
983 parsed = parse_int_option(&decl[i], arg, prefix, opt);
986 parsed = parse_long_option(&decl[i], arg, prefix, opt);
989 parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
992 parsed = parse_bool_option(&decl[i], arg, prefix, opt);
995 parsed = parse_str_option(&decl[i], arg, prefix, opt);
998 parsed = parse_child_option(&decl[i], arg, prefix, opt);
1002 case isl_arg_footer:
1004 case isl_arg_version:
1015 static void print_version(struct isl_arg *decl)
1019 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1020 switch (decl[i].type) {
1021 case isl_arg_version:
1022 decl[i].u.version.print_version();
1025 print_version(decl[i].u.child.child);
1033 static void print_version_and_exit(struct isl_arg *decl)
1035 print_version(decl);
1040 static int drop_argument(int argc, char **argv, int drop, int n)
1042 for (; drop < argc; ++drop)
1043 argv[drop] = argv[drop + n];
1048 static int n_arg(struct isl_arg *arg)
1053 for (i = 0; arg[i].type != isl_arg_end; ++i)
1054 if (arg[i].type == isl_arg_arg)
1060 static int next_arg(struct isl_arg *arg, int a)
1062 for (++a; arg[a].type != isl_arg_end; ++a)
1063 if (arg[a].type == isl_arg_arg)
1069 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
1079 for (i = 1; i < argc; ++i) {
1080 if (strcmp(argv[i], "--help") == 0)
1081 print_help_and_exit(arg, argv[0], opt);
1084 for (i = 1; i < argc; ++i) {
1085 if ((strcmp(argv[i], "--version") == 0 ||
1086 strcmp(argv[i], "-V") == 0) && any_version(arg))
1087 print_version_and_exit(arg);
1090 while (argc > 1 + skip) {
1092 if (argv[1 + skip][0] != '-') {
1093 a = next_arg(arg, a);
1096 p = (char **)(((char *)opt)+arg[a].offset);
1098 *p = strdup(argv[1 + skip]);
1099 argc = drop_argument(argc, argv, 1 + skip, 1);
1101 } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1102 fprintf(stderr, "%s: extra argument: %s\n",
1103 prog_name(argv[0]), argv[1 + skip]);
1109 parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
1111 argc = drop_argument(argc, argv, 1 + skip, parsed);
1112 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1113 fprintf(stderr, "%s: unrecognized option: %s\n",
1114 prog_name(argv[0]), argv[1 + skip]);
1121 fprintf(stderr, "%s: expecting %d more argument(s)\n",
1122 prog_name(argv[0]), n);