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")
21 static void set_default_choice(struct isl_arg *arg, void *opt)
23 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.choice.default_value;
26 static void set_default_flags(struct isl_arg *arg, void *opt)
28 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.flags.default_value;
31 static void set_default_bool(struct isl_arg *arg, void *opt)
33 if (arg->offset == (size_t) -1)
35 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.b.default_value;
38 static void set_default_child(struct isl_arg *arg, void *opt)
42 if (arg->offset == (size_t) -1)
45 child = calloc(1, arg->u.child.child->options_size);
46 *(void **)(((char *)opt) + arg->offset) = child;
50 isl_args_set_defaults(arg->u.child.child, child);
53 static void set_default_user(struct isl_arg *arg, void *opt)
55 arg->u.user.init(((char *)opt) + arg->offset);
58 static void set_default_int(struct isl_arg *arg, void *opt)
60 *(int *)(((char *)opt) + arg->offset) = arg->u.i.default_value;
63 static void set_default_long(struct isl_arg *arg, void *opt)
65 *(long *)(((char *)opt) + arg->offset) = arg->u.l.default_value;
68 static void set_default_ulong(struct isl_arg *arg, void *opt)
70 *(unsigned long *)(((char *)opt) + arg->offset) = arg->u.ul.default_value;
73 static void set_default_str(struct isl_arg *arg, void *opt)
75 const char *str = NULL;
76 if (arg->u.str.default_value)
77 str = strdup(arg->u.str.default_value);
78 *(const char **)(((char *)opt) + arg->offset) = str;
81 static void set_default_str_list(struct isl_arg *arg, void *opt)
83 *(const char ***)(((char *) opt) + arg->offset) = NULL;
84 *(int *)(((char *) opt) + arg->u.str_list.offset_n) = 0;
87 void isl_args_set_defaults(struct isl_args *args, void *opt)
91 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
92 switch (args->args[i].type) {
94 set_default_choice(&args->args[i], opt);
97 set_default_flags(&args->args[i], opt);
100 set_default_bool(&args->args[i], opt);
103 set_default_child(&args->args[i], opt);
106 set_default_user(&args->args[i], opt);
109 set_default_int(&args->args[i], opt);
112 set_default_long(&args->args[i], opt);
115 set_default_ulong(&args->args[i], opt);
119 set_default_str(&args->args[i], opt);
121 case isl_arg_str_list:
122 set_default_str_list(&args->args[i], opt);
126 case isl_arg_version:
133 static void free_str_list(struct isl_arg *arg, void *opt)
136 int n = *(int *)(((char *) opt) + arg->u.str_list.offset_n);
137 char **list = *(char ***)(((char *) opt) + arg->offset);
139 for (i = 0; i < n; ++i)
144 static void free_args(struct isl_arg *arg, void *opt)
148 for (i = 0; arg[i].type != isl_arg_end; ++i) {
149 switch (arg[i].type) {
151 if (arg[i].offset == (size_t) -1)
152 free_args(arg[i].u.child.child->args, opt);
154 isl_args_free(arg[i].u.child.child,
155 *(void **)(((char *)opt) + arg[i].offset));
159 free(*(char **)(((char *)opt) + arg[i].offset));
161 case isl_arg_str_list:
162 free_str_list(&arg[i], opt);
165 if (arg[i].u.user.clear)
166 arg[i].u.user.clear(((char *)opt) + arg[i].offset);
175 case isl_arg_version:
183 void isl_args_free(struct isl_args *args, void *opt)
188 free_args(args->args, opt);
193 static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
197 if (!decl->long_name) {
198 printf(" -%c", decl->short_name);
202 if (decl->short_name) {
203 printf(" -%c, --", decl->short_name);
205 } else if (decl->flags & ISL_ARG_SINGLE_DASH) {
214 printf("%s-", prefix);
215 len += strlen(prefix) + 1;
221 printf("%s", decl->long_name);
222 len += strlen(decl->long_name);
224 while ((++decl)->type == isl_arg_alias) {
231 printf("%s", decl->long_name);
232 len += strlen(decl->long_name);
238 const void *isl_memrchr(const void *s, int c, size_t n)
247 static int wrap_msg(const char *s, int indent, int pos)
250 int wrap_len = 75 - indent;
252 if (pos + 1 >= indent)
253 printf("\n%*s", indent, "");
255 printf("%*s", indent - pos, "");
258 while (len > wrap_len) {
259 const char *space = isl_memrchr(s, ' ', wrap_len);
263 space = strchr(s + wrap_len, ' ');
267 printf("%.*s", l, s);
270 printf("\n%*s", indent, "");
277 static int print_help_msg(struct isl_arg *decl, int pos)
282 return wrap_msg(decl->help_msg, 30, pos);
285 static void print_default(struct isl_arg *decl, const char *def, int pos)
287 const char *default_prefix = "[default: ";
288 const char *default_suffix = "]";
291 len = strlen(default_prefix) + strlen(def) + strlen(default_suffix);
293 if (!decl->help_msg) {
295 printf("\n%30s", "");
297 printf("%*s", 30 - pos, "");
301 printf("\n%30s", "");
305 printf("%s%s%s", default_prefix, def, default_suffix);
308 static void print_default_choice(struct isl_arg *decl, void *opt, int pos)
311 const char *s = "none";
314 p = (unsigned *)(((char *) opt) + decl->offset);
315 for (i = 0; decl->u.choice.choice[i].name; ++i)
316 if (decl->u.choice.choice[i].value == *p) {
317 s = decl->u.choice.choice[i].name;
321 print_default(decl, s, pos);
324 static void print_choice_help(struct isl_arg *decl, const char *prefix,
330 pos = print_arg_help(decl, prefix, 0);
334 for (i = 0; decl->u.choice.choice[i].name; ++i) {
339 printf("%s", decl->u.choice.choice[i].name);
340 pos += strlen(decl->u.choice.choice[i].name);
343 pos = print_help_msg(decl, pos);
344 print_default_choice(decl, opt, pos);
349 static void print_default_flags(struct isl_arg *decl, void *opt, int pos)
352 const char *default_prefix = "[default: ";
353 const char *default_suffix = "]";
354 int len = strlen(default_prefix) + strlen(default_suffix);
357 p = (unsigned *)(((char *) opt) + decl->offset);
358 for (i = 0; decl->u.flags.flags[i].name; ++i)
359 if ((*p & decl->u.flags.flags[i].mask) ==
360 decl->u.flags.flags[i].value)
361 len += strlen(decl->u.flags.flags[i].name);
363 if (!decl->help_msg) {
365 printf("\n%30s", "");
367 printf("%*s", 30 - pos, "");
371 printf("\n%30s", "");
375 printf("%s", default_prefix);
377 for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
378 if ((*p & decl->u.flags.flags[i].mask) ==
379 decl->u.flags.flags[i].value) {
382 printf("%s", decl->u.flags.flags[i].name);
386 printf("%s", default_suffix);
389 static void print_flags_help(struct isl_arg *decl, const char *prefix,
395 pos = print_arg_help(decl, prefix, 0);
399 for (i = 0; decl->u.flags.flags[i].name; ++i) {
405 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
411 printf("%s", decl->u.flags.flags[j].name);
412 pos += strlen(decl->u.flags.flags[j].name);
417 pos = print_help_msg(decl, pos);
418 print_default_flags(decl, opt, pos);
423 static void print_bool_help(struct isl_arg *decl, const char *prefix, void *opt)
426 unsigned *p = opt ? (unsigned *)(((char *) opt) + decl->offset) : NULL;
427 int no = p ? *p == 1 : 0;
428 pos = print_arg_help(decl, prefix, no);
429 pos = print_help_msg(decl, pos);
430 if (decl->offset != (size_t) -1)
431 print_default(decl, no ? "yes" : "no", pos);
435 static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
437 printf("%c<%s>", decl->long_name ? '=' : ' ', name);
438 return pos + 3 + strlen(name);
441 static void print_int_help(struct isl_arg *decl, const char *prefix, void *opt)
445 int *p = (int *)(((char *) opt) + decl->offset);
446 pos = print_arg_help(decl, prefix, 0);
447 pos = print_argument_name(decl, decl->argument_name, pos);
448 pos = print_help_msg(decl, pos);
449 snprintf(val, sizeof(val), "%d", *p);
450 print_default(decl, val, pos);
454 static void print_long_help(struct isl_arg *decl, const char *prefix, void *opt)
457 long *p = (long *)(((char *) opt) + decl->offset);
458 pos = print_arg_help(decl, prefix, 0);
459 if (*p != decl->u.l.default_selected) {
465 if (*p != decl->u.l.default_selected) {
469 print_help_msg(decl, pos);
473 static void print_ulong_help(struct isl_arg *decl, const char *prefix)
476 pos = print_arg_help(decl, prefix, 0);
479 print_help_msg(decl, pos);
483 static void print_str_help(struct isl_arg *decl, const char *prefix, void *opt)
486 const char *a = decl->argument_name ? decl->argument_name : "string";
487 const char **p = (const char **)(((char *) opt) + decl->offset);
488 pos = print_arg_help(decl, prefix, 0);
489 pos = print_argument_name(decl, a, pos);
490 pos = print_help_msg(decl, pos);
492 print_default(decl, *p, pos);
496 static void print_str_list_help(struct isl_arg *decl, const char *prefix)
499 const char *a = decl->argument_name ? decl->argument_name : "string";
500 pos = print_arg_help(decl, prefix, 0);
501 pos = print_argument_name(decl, a, pos);
502 pos = print_help_msg(decl, pos);
506 static void print_help(struct isl_arg *arg, const char *prefix, void *opt)
511 for (i = 0; arg[i].type != isl_arg_end; ++i) {
512 if (arg[i].flags & ISL_ARG_HIDDEN)
514 switch (arg[i].type) {
516 print_flags_help(&arg[i], prefix, opt);
520 print_choice_help(&arg[i], prefix, opt);
524 print_bool_help(&arg[i], prefix, opt);
528 print_int_help(&arg[i], prefix, opt);
532 print_long_help(&arg[i], prefix, opt);
536 print_ulong_help(&arg[i], prefix);
540 print_str_help(&arg[i], prefix, opt);
543 case isl_arg_str_list:
544 print_str_list_help(&arg[i], prefix);
548 case isl_arg_version:
558 for (i = 0; arg[i].type != isl_arg_end; ++i) {
561 if (arg[i].type != isl_arg_child)
563 if (arg[i].flags & ISL_ARG_HIDDEN)
569 printf(" %s\n", arg[i].help_msg);
570 if (arg[i].offset == (size_t) -1)
573 child = *(void **)(((char *) opt) + arg[i].offset);
574 print_help(arg[i].u.child.child->args, arg[i].long_name, child);
579 static const char *prog_name(const char *prog)
583 slash = strrchr(prog, '/');
586 if (strncmp(prog, "lt-", 3) == 0)
592 static int any_version(struct isl_arg *decl)
596 for (i = 0; decl[i].type != isl_arg_end; ++i) {
597 switch (decl[i].type) {
598 case isl_arg_version:
601 if (any_version(decl[i].u.child.child->args))
612 static void print_help_and_exit(struct isl_arg *arg, const char *prog,
617 printf("Usage: %s [OPTION...]", prog_name(prog));
619 for (i = 0; arg[i].type != isl_arg_end; ++i)
620 if (arg[i].type == isl_arg_arg)
621 printf(" %s", arg[i].argument_name);
625 print_help(arg, NULL, opt);
627 if (any_version(arg))
628 printf(" -V, --version\n");
629 print_bool_help(help_arg, NULL, NULL);
631 for (i = 0; arg[i].type != isl_arg_end; ++i) {
632 if (arg[i].type != isl_arg_footer)
634 wrap_msg(arg[i].help_msg, 0, 0);
641 static int match_long_name(struct isl_arg *decl,
642 const char *start, const char *end)
645 if (end - start == strlen(decl->long_name) &&
646 !strncmp(start, decl->long_name, end - start))
648 } while ((++decl)->type == isl_arg_alias);
653 static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
655 if (!strncmp(arg, "--", 2))
657 if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
662 static const char *skip_name(struct isl_arg *decl, const char *arg,
663 const char *prefix, int need_argument, int *has_argument)
669 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
670 if (need_argument && !arg[2])
673 *has_argument = arg[2] != '\0';
676 if (!decl->long_name)
679 name = skip_dash_dash(decl, arg);
683 equal = strchr(name, '=');
684 if (need_argument && !equal)
688 *has_argument = !!equal;
689 end = equal ? equal : name + strlen(name);
692 size_t prefix_len = strlen(prefix);
693 if (strncmp(name, prefix, prefix_len) == 0 &&
694 name[prefix_len] == '-')
695 name += prefix_len + 1;
698 if (!match_long_name(decl, name, end))
701 return equal ? equal + 1 : end;
704 static int parse_choice_option(struct isl_arg *decl, char **arg,
705 const char *prefix, void *opt)
711 choice = skip_name(decl, arg[0], prefix, 0, &has_argument);
715 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
716 unsigned u = decl->u.choice.default_selected;
717 if (decl->u.choice.set)
718 decl->u.choice.set(opt, u);
720 *(unsigned *)(((char *)opt) + decl->offset) = u;
728 for (i = 0; decl->u.choice.choice[i].name; ++i) {
731 if (strcmp(choice, decl->u.choice.choice[i].name))
734 u = decl->u.choice.choice[i].value;
735 if (decl->u.choice.set)
736 decl->u.choice.set(opt, u);
738 *(unsigned *)(((char *)opt) + decl->offset) = u;
740 return has_argument ? 1 : 2;
746 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
751 for (i = 0; decl->u.flags.flags[i].name; ++i) {
752 if (strncmp(flag, decl->u.flags.flags[i].name, len))
755 *val &= ~decl->u.flags.flags[i].mask;
756 *val |= decl->u.flags.flags[i].value;
764 static int parse_flags_option(struct isl_arg *decl, char **arg,
765 const char *prefix, void *opt)
772 flags = skip_name(decl, arg[0], prefix, 0, &has_argument);
776 if (!has_argument && !arg[1])
782 val = *(unsigned *)(((char *)opt) + decl->offset);
784 while ((comma = strchr(flags, ',')) != NULL) {
785 if (!set_flag(decl, &val, flags, comma - flags))
789 if (!set_flag(decl, &val, flags, strlen(flags)))
792 *(unsigned *)(((char *)opt) + decl->offset) = val;
794 return has_argument ? 1 : 2;
797 static int parse_bool_option(struct isl_arg *decl, char **arg,
798 const char *prefix, void *opt)
801 unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
803 if (skip_name(decl, arg[0], prefix, 0, NULL)) {
804 if ((decl->flags & ISL_ARG_BOOL_ARG) && arg[1]) {
806 int val = strtol(arg[1], &endptr, 0);
807 if (*endptr == '\0' && (val == 0 || val == 1)) {
809 decl->u.b.set(opt, val);
810 else if (decl->offset != (size_t) -1)
816 decl->u.b.set(opt, 1);
817 else if (decl->offset != (size_t) -1)
823 if (!decl->long_name)
826 name = skip_dash_dash(decl, arg[0]);
831 size_t prefix_len = strlen(prefix);
832 if (strncmp(name, prefix, prefix_len) == 0 &&
833 name[prefix_len] == '-') {
834 name += prefix_len + 1;
839 if (strncmp(name, "no-", 3))
844 size_t prefix_len = strlen(prefix);
845 if (strncmp(name, prefix, prefix_len) == 0 &&
846 name[prefix_len] == '-')
847 name += prefix_len + 1;
850 if (match_long_name(decl, name, name + strlen(name))) {
852 decl->u.b.set(opt, 0);
853 else if (decl->offset != (size_t) -1)
862 static int parse_str_option(struct isl_arg *decl, char **arg,
863 const char *prefix, void *opt)
867 char **p = (char **)(((char *)opt) + decl->offset);
869 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
888 static int isl_arg_str_list_append(struct isl_arg *decl, void *opt,
891 int *n = (int *)(((char *) opt) + decl->u.str_list.offset_n);
892 char **list = *(char ***)(((char *) opt) + decl->offset);
894 list = realloc(list, (*n + 1) * sizeof(char *));
897 *(char ***)(((char *) opt) + decl->offset) = list;
898 list[*n] = strdup(s);
903 static int parse_str_list_option(struct isl_arg *decl, char **arg,
904 const char *prefix, void *opt)
909 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
914 isl_arg_str_list_append(decl, opt, s);
919 isl_arg_str_list_append(decl, opt, arg[1]);
926 static int parse_int_option(struct isl_arg *decl, char **arg,
927 const char *prefix, void *opt)
932 int *p = (int *)(((char *)opt) + decl->offset);
934 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
944 int i = strtol(arg[1], &endptr, 0);
945 if (*endptr == '\0') {
954 static int parse_long_option(struct isl_arg *decl, char **arg,
955 const char *prefix, void *opt)
960 long *p = (long *)(((char *)opt) + decl->offset);
962 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
967 long l = strtol(val, NULL, 0);
969 decl->u.l.set(opt, l);
976 long l = strtol(arg[1], &endptr, 0);
977 if (*endptr == '\0') {
979 decl->u.l.set(opt, l);
986 if (decl->u.l.default_value != decl->u.l.default_selected) {
988 decl->u.l.set(opt, decl->u.l.default_selected);
990 *p = decl->u.l.default_selected;
997 static int parse_ulong_option(struct isl_arg *decl, char **arg,
998 const char *prefix, void *opt)
1003 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
1005 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
1010 *p = strtoul(val, NULL, 0);
1015 unsigned long ul = strtoul(arg[1], &endptr, 0);
1016 if (*endptr == '\0') {
1025 static int parse_option(struct isl_arg *decl, char **arg,
1026 const char *prefix, void *opt);
1028 static int parse_child_option(struct isl_arg *decl, char **arg,
1029 const char *prefix, void *opt)
1033 if (decl->offset == (size_t) -1)
1036 child = *(void **)(((char *)opt) + decl->offset);
1037 prefix = decl->long_name;
1039 return parse_option(decl->u.child.child->args, arg, prefix, child);
1042 static int parse_option(struct isl_arg *decl, char **arg,
1043 const char *prefix, void *opt)
1047 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1049 switch (decl[i].type) {
1050 case isl_arg_choice:
1051 parsed = parse_choice_option(&decl[i], arg, prefix, opt);
1054 parsed = parse_flags_option(&decl[i], arg, prefix, opt);
1057 parsed = parse_int_option(&decl[i], arg, prefix, opt);
1060 parsed = parse_long_option(&decl[i], arg, prefix, opt);
1063 parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
1066 parsed = parse_bool_option(&decl[i], arg, prefix, opt);
1069 parsed = parse_str_option(&decl[i], arg, prefix, opt);
1071 case isl_arg_str_list:
1072 parsed = parse_str_list_option(&decl[i], arg, prefix,
1076 parsed = parse_child_option(&decl[i], arg, prefix, opt);
1080 case isl_arg_footer:
1082 case isl_arg_version:
1093 static void print_version(struct isl_arg *decl)
1097 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1098 switch (decl[i].type) {
1099 case isl_arg_version:
1100 decl[i].u.version.print_version();
1103 print_version(decl[i].u.child.child->args);
1111 static void print_version_and_exit(struct isl_arg *decl)
1113 print_version(decl);
1118 static int drop_argument(int argc, char **argv, int drop, int n)
1120 for (; drop < argc; ++drop)
1121 argv[drop] = argv[drop + n];
1126 static int n_arg(struct isl_arg *arg)
1131 for (i = 0; arg[i].type != isl_arg_end; ++i)
1132 if (arg[i].type == isl_arg_arg)
1138 static int next_arg(struct isl_arg *arg, int a)
1140 for (++a; arg[a].type != isl_arg_end; ++a)
1141 if (arg[a].type == isl_arg_arg)
1147 int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
1155 n = n_arg(args->args);
1157 for (i = 1; i < argc; ++i) {
1158 if (strcmp(argv[i], "--help") == 0)
1159 print_help_and_exit(args->args, argv[0], opt);
1162 for (i = 1; i < argc; ++i) {
1163 if ((strcmp(argv[i], "--version") == 0 ||
1164 strcmp(argv[i], "-V") == 0) && any_version(args->args))
1165 print_version_and_exit(args->args);
1168 while (argc > 1 + skip) {
1170 if (argv[1 + skip][0] != '-') {
1171 a = next_arg(args->args, a);
1174 p = (char **)(((char *)opt)+args->args[a].offset);
1176 *p = strdup(argv[1 + skip]);
1177 argc = drop_argument(argc, argv, 1 + skip, 1);
1179 } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1180 fprintf(stderr, "%s: extra argument: %s\n",
1181 prog_name(argv[0]), argv[1 + skip]);
1187 parsed = parse_option(args->args, &argv[1 + skip], NULL, opt);
1189 argc = drop_argument(argc, argv, 1 + skip, parsed);
1190 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1191 fprintf(stderr, "%s: unrecognized option: %s\n",
1192 prog_name(argv[0]), argv[1 + skip]);
1199 fprintf(stderr, "%s: expecting %d more argument(s)\n",
1200 prog_name(argv[0]), n);