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, int pos)
289 const char *s = "none";
291 for (i = 0; decl->u.choice.choice[i].name; ++i)
292 if (decl->u.choice.choice[i].value == decl->u.choice.default_value) {
293 s = decl->u.choice.choice[i].name;
297 print_default(decl, s, pos);
300 static void print_choice_help(struct isl_arg *decl, const char *prefix)
305 pos = print_arg_help(decl, prefix, 0);
309 for (i = 0; decl->u.choice.choice[i].name; ++i) {
314 printf("%s", decl->u.choice.choice[i].name);
315 pos += strlen(decl->u.choice.choice[i].name);
318 pos = print_help_msg(decl, pos);
319 print_default_choice(decl, pos);
324 static void print_default_flags(struct isl_arg *decl, int pos)
327 const char *default_prefix = "[default: ";
328 const char *default_suffix = "]";
329 int len = strlen(default_prefix) + strlen(default_suffix);
331 for (i = 0; decl->u.flags.flags[i].name; ++i)
332 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
333 decl->u.flags.flags[i].value)
334 len += strlen(decl->u.flags.flags[i].name);
336 if (!decl->help_msg) {
338 printf("\n%30s", "");
340 printf("%*s", 30 - pos, "");
344 printf("\n%30s", "");
348 printf("%s", default_prefix);
350 for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
351 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
352 decl->u.flags.flags[i].value) {
355 printf("%s", decl->u.flags.flags[i].name);
359 printf("%s", default_suffix);
362 static void print_flags_help(struct isl_arg *decl, const char *prefix)
367 pos = print_arg_help(decl, prefix, 0);
371 for (i = 0; decl->u.flags.flags[i].name; ++i) {
377 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
383 printf("%s", decl->u.flags.flags[j].name);
384 pos += strlen(decl->u.flags.flags[j].name);
389 pos = print_help_msg(decl, pos);
390 print_default_flags(decl, pos);
395 static void print_bool_help(struct isl_arg *decl, const char *prefix)
398 int no = decl->u.b.default_value == 1;
399 pos = print_arg_help(decl, prefix, no);
400 pos = print_help_msg(decl, pos);
401 if (decl->offset != (size_t) -1)
402 print_default(decl, no ? "yes" : "no", pos);
406 static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
408 printf("%c<%s>", decl->long_name ? '=' : ' ', name);
409 return pos + 3 + strlen(name);
412 static void print_int_help(struct isl_arg *decl, const char *prefix)
416 pos = print_arg_help(decl, prefix, 0);
417 pos = print_argument_name(decl, decl->argument_name, pos);
418 pos = print_help_msg(decl, pos);
419 snprintf(val, sizeof(val), "%d", decl->u.i.default_value);
420 print_default(decl, val, pos);
424 static void print_long_help(struct isl_arg *decl, const char *prefix)
427 pos = print_arg_help(decl, prefix, 0);
428 if (decl->u.l.default_value != decl->u.l.default_selected) {
434 if (decl->u.l.default_value != decl->u.l.default_selected) {
438 print_help_msg(decl, pos);
442 static void print_ulong_help(struct isl_arg *decl, const char *prefix)
445 pos = print_arg_help(decl, prefix, 0);
448 print_help_msg(decl, pos);
452 static void print_str_help(struct isl_arg *decl, const char *prefix)
455 const char *a = decl->argument_name ? decl->argument_name : "string";
456 pos = print_arg_help(decl, prefix, 0);
457 pos = print_argument_name(decl, a, pos);
458 pos = print_help_msg(decl, pos);
459 if (decl->u.str.default_value)
460 print_default(decl, decl->u.str.default_value, pos);
464 static void print_help(struct isl_arg *arg, const char *prefix)
469 for (i = 0; arg[i].type != isl_arg_end; ++i) {
470 if (arg[i].flags & ISL_ARG_HIDDEN)
472 switch (arg[i].type) {
474 print_flags_help(&arg[i], prefix);
478 print_choice_help(&arg[i], prefix);
482 print_bool_help(&arg[i], prefix);
486 print_int_help(&arg[i], prefix);
490 print_long_help(&arg[i], prefix);
494 print_ulong_help(&arg[i], prefix);
498 print_str_help(&arg[i], prefix);
502 case isl_arg_version:
512 for (i = 0; arg[i].type != isl_arg_end; ++i) {
513 if (arg[i].type != isl_arg_child)
515 if (arg[i].flags & ISL_ARG_HIDDEN)
521 printf(" %s\n", arg[i].help_msg);
522 print_help(arg[i].u.child.child, arg[i].long_name);
527 static const char *prog_name(const char *prog)
531 slash = strrchr(prog, '/');
534 if (strncmp(prog, "lt-", 3) == 0)
540 static int any_version(struct isl_arg *decl)
544 for (i = 0; decl[i].type != isl_arg_end; ++i) {
545 switch (decl[i].type) {
546 case isl_arg_version:
549 if (any_version(decl[i].u.child.child))
560 static void print_help_and_exit(struct isl_arg *arg, const char *prog)
564 printf("Usage: %s [OPTION...]", prog_name(prog));
566 for (i = 0; arg[i].type != isl_arg_end; ++i)
567 if (arg[i].type == isl_arg_arg)
568 printf(" %s", arg[i].argument_name);
572 print_help(arg, NULL);
574 if (any_version(arg))
575 printf(" -V, --version\n");
576 print_bool_help(help_arg, NULL);
578 for (i = 0; arg[i].type != isl_arg_end; ++i) {
579 if (arg[i].type != isl_arg_footer)
581 wrap_msg(arg[i].help_msg, 0, 0);
588 static int match_long_name(struct isl_arg *decl,
589 const char *start, const char *end)
592 if (end - start == strlen(decl->long_name) &&
593 !strncmp(start, decl->long_name, end - start))
595 } while ((++decl)->type == isl_arg_alias);
600 static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
602 if (!strncmp(arg, "--", 2))
604 if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
609 static const char *skip_name(struct isl_arg *decl, const char *arg,
610 const char *prefix, int need_argument, int *has_argument)
616 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
617 if (need_argument && !arg[2])
620 *has_argument = arg[2] != '\0';
623 if (!decl->long_name)
626 name = skip_dash_dash(decl, arg);
630 equal = strchr(name, '=');
631 if (need_argument && !equal)
635 *has_argument = !!equal;
636 end = equal ? equal : name + strlen(name);
639 size_t prefix_len = strlen(prefix);
640 if (strncmp(name, prefix, prefix_len) == 0 &&
641 name[prefix_len] == '-')
642 name += prefix_len + 1;
645 if (!match_long_name(decl, name, end))
648 return equal ? equal + 1 : end;
651 static int parse_choice_option(struct isl_arg *decl, char **arg,
652 const char *prefix, void *opt)
658 choice = skip_name(decl, arg[0], prefix, 0, &has_argument);
662 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
663 unsigned u = decl->u.choice.default_selected;
664 if (decl->u.choice.set)
665 decl->u.choice.set(opt, u);
667 *(unsigned *)(((char *)opt) + decl->offset) = u;
675 for (i = 0; decl->u.choice.choice[i].name; ++i) {
678 if (strcmp(choice, decl->u.choice.choice[i].name))
681 u = decl->u.choice.choice[i].value;
682 if (decl->u.choice.set)
683 decl->u.choice.set(opt, u);
685 *(unsigned *)(((char *)opt) + decl->offset) = u;
687 return has_argument ? 1 : 2;
693 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
698 for (i = 0; decl->u.flags.flags[i].name; ++i) {
699 if (strncmp(flag, decl->u.flags.flags[i].name, len))
702 *val &= ~decl->u.flags.flags[i].mask;
703 *val |= decl->u.flags.flags[i].value;
711 static int parse_flags_option(struct isl_arg *decl, char **arg,
712 const char *prefix, void *opt)
719 flags = skip_name(decl, arg[0], prefix, 0, &has_argument);
723 if (!has_argument && !arg[1])
729 val = *(unsigned *)(((char *)opt) + decl->offset);
731 while ((comma = strchr(flags, ',')) != NULL) {
732 if (!set_flag(decl, &val, flags, comma - flags))
736 if (!set_flag(decl, &val, flags, strlen(flags)))
739 *(unsigned *)(((char *)opt) + decl->offset) = val;
741 return has_argument ? 1 : 2;
744 static int parse_bool_option(struct isl_arg *decl, char **arg,
745 const char *prefix, void *opt)
748 unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
750 if (skip_name(decl, arg[0], prefix, 0, NULL)) {
751 if ((decl->flags && ISL_ARG_BOOL_ARG) && arg[1]) {
753 int val = strtol(arg[1], &endptr, 0);
754 if (*endptr == '\0' && (val == 0 || val == 1)) {
756 decl->u.b.set(opt, val);
757 else if (decl->offset != (size_t) -1)
763 decl->u.b.set(opt, 1);
764 else if (decl->offset != (size_t) -1)
770 if (!decl->long_name)
773 name = skip_dash_dash(decl, arg[0]);
778 size_t prefix_len = strlen(prefix);
779 if (strncmp(name, prefix, prefix_len) == 0 &&
780 name[prefix_len] == '-') {
781 name += prefix_len + 1;
786 if (strncmp(name, "no-", 3))
791 size_t prefix_len = strlen(prefix);
792 if (strncmp(name, prefix, prefix_len) == 0 &&
793 name[prefix_len] == '-')
794 name += prefix_len + 1;
797 if (match_long_name(decl, name, name + strlen(name))) {
799 decl->u.b.set(opt, 0);
800 else if (decl->offset != (size_t) -1)
809 static int parse_str_option(struct isl_arg *decl, char **arg,
810 const char *prefix, void *opt)
814 char **p = (char **)(((char *)opt) + decl->offset);
816 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
835 static int parse_int_option(struct isl_arg *decl, char **arg,
836 const char *prefix, void *opt)
841 int *p = (int *)(((char *)opt) + decl->offset);
843 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
853 int i = strtol(arg[1], &endptr, 0);
854 if (*endptr == '\0') {
863 static int parse_long_option(struct isl_arg *decl, char **arg,
864 const char *prefix, void *opt)
869 long *p = (long *)(((char *)opt) + decl->offset);
871 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
876 long l = strtol(val, NULL, 0);
878 decl->u.l.set(opt, l);
885 long l = strtol(arg[1], &endptr, 0);
886 if (*endptr == '\0') {
888 decl->u.l.set(opt, l);
895 if (decl->u.l.default_value != decl->u.l.default_selected) {
897 decl->u.l.set(opt, decl->u.l.default_selected);
899 *p = decl->u.l.default_selected;
906 static int parse_ulong_option(struct isl_arg *decl, char **arg,
907 const char *prefix, void *opt)
912 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
914 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
919 *p = strtoul(val, NULL, 0);
924 unsigned long ul = strtoul(arg[1], &endptr, 0);
925 if (*endptr == '\0') {
934 static int parse_option(struct isl_arg *decl, char **arg,
935 const char *prefix, void *opt);
937 static int parse_child_option(struct isl_arg *decl, char **arg,
938 const char *prefix, void *opt)
942 if (decl->offset == (size_t) -1)
945 child = *(void **)(((char *)opt) + decl->offset);
946 prefix = decl->long_name;
948 return parse_option(decl->u.child.child, arg, prefix, child);
951 static int parse_option(struct isl_arg *decl, char **arg,
952 const char *prefix, void *opt)
956 for (i = 0; decl[i].type != isl_arg_end; ++i) {
958 switch (decl[i].type) {
960 parsed = parse_choice_option(&decl[i], arg, prefix, opt);
963 parsed = parse_flags_option(&decl[i], arg, prefix, opt);
966 parsed = parse_int_option(&decl[i], arg, prefix, opt);
969 parsed = parse_long_option(&decl[i], arg, prefix, opt);
972 parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
975 parsed = parse_bool_option(&decl[i], arg, prefix, opt);
978 parsed = parse_str_option(&decl[i], arg, prefix, opt);
981 parsed = parse_child_option(&decl[i], arg, prefix, opt);
987 case isl_arg_version:
998 static void print_version(struct isl_arg *decl)
1002 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1003 switch (decl[i].type) {
1004 case isl_arg_version:
1005 decl[i].u.version.print_version();
1008 print_version(decl[i].u.child.child);
1016 static void print_version_and_exit(struct isl_arg *decl)
1018 print_version(decl);
1023 static int drop_argument(int argc, char **argv, int drop, int n)
1025 for (; drop < argc; ++drop)
1026 argv[drop] = argv[drop + n];
1031 static int n_arg(struct isl_arg *arg)
1036 for (i = 0; arg[i].type != isl_arg_end; ++i)
1037 if (arg[i].type == isl_arg_arg)
1043 static int next_arg(struct isl_arg *arg, int a)
1045 for (++a; arg[a].type != isl_arg_end; ++a)
1046 if (arg[a].type == isl_arg_arg)
1052 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
1062 for (i = 1; i < argc; ++i) {
1063 if (strcmp(argv[i], "--help") == 0)
1064 print_help_and_exit(arg, argv[0]);
1067 for (i = 1; i < argc; ++i) {
1068 if ((strcmp(argv[i], "--version") == 0 ||
1069 strcmp(argv[i], "-V") == 0) && any_version(arg))
1070 print_version_and_exit(arg);
1073 while (argc > 1 + skip) {
1075 if (argv[1 + skip][0] != '-') {
1076 a = next_arg(arg, a);
1079 p = (char **)(((char *)opt)+arg[a].offset);
1081 *p = strdup(argv[1 + skip]);
1082 argc = drop_argument(argc, argv, 1 + skip, 1);
1084 } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1085 fprintf(stderr, "%s: extra argument: %s\n",
1086 prog_name(argv[0]), argv[1 + skip]);
1092 parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
1094 argc = drop_argument(argc, argv, 1 + skip, parsed);
1095 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1096 fprintf(stderr, "%s: unrecognized option: %s\n",
1097 prog_name(argv[0]), argv[1 + skip]);
1104 fprintf(stderr, "%s: expecting %d more argument(s)\n",
1105 prog_name(argv[0]), n);