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
)
266 const char *default_prefix
= "[default: ";
267 const char *default_suffix
= "]";
270 len
= strlen(default_prefix
) + strlen(def
) + strlen(default_suffix
);
272 if (!decl
->help_msg
) {
274 printf("\n%30s", "");
276 printf("%*s", 30 - pos
, "");
280 printf("\n%30s", "");
284 printf("%s%s%s", default_prefix
, def
, default_suffix
);
287 static void print_default_choice(struct isl_arg
*decl
, int pos
)
290 const char *s
= "none";
292 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
)
293 if (decl
->u
.choice
.choice
[i
].value
== decl
->u
.choice
.default_value
) {
294 s
= decl
->u
.choice
.choice
[i
].name
;
298 print_default(decl
, s
, pos
);
301 static void print_choice_help(struct isl_arg
*decl
, const char *prefix
)
306 pos
= print_arg_help(decl
, prefix
, 0);
310 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
315 printf("%s", decl
->u
.choice
.choice
[i
].name
);
316 pos
+= strlen(decl
->u
.choice
.choice
[i
].name
);
319 pos
= print_help_msg(decl
, pos
);
320 print_default_choice(decl
, pos
);
325 static void print_default_flags(struct isl_arg
*decl
, int pos
)
328 const char *default_prefix
= "[default: ";
329 const char *default_suffix
= "]";
330 int len
= strlen(default_prefix
) + strlen(default_suffix
);
332 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
333 if ((decl
->u
.flags
.default_value
& decl
->u
.flags
.flags
[i
].mask
) ==
334 decl
->u
.flags
.flags
[i
].value
)
335 len
+= strlen(decl
->u
.flags
.flags
[i
].name
);
337 if (!decl
->help_msg
) {
339 printf("\n%30s", "");
341 printf("%*s", 30 - pos
, "");
345 printf("\n%30s", "");
349 printf("%s", default_prefix
);
351 for (first
= 1, i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
352 if ((decl
->u
.flags
.default_value
& decl
->u
.flags
.flags
[i
].mask
) ==
353 decl
->u
.flags
.flags
[i
].value
) {
356 printf("%s", decl
->u
.flags
.flags
[i
].name
);
360 printf("%s", default_suffix
);
363 static void print_flags_help(struct isl_arg
*decl
, const char *prefix
)
368 pos
= print_arg_help(decl
, prefix
, 0);
372 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
378 decl
->u
.flags
.flags
[j
].mask
== decl
->u
.flags
.flags
[i
].mask
;
384 printf("%s", decl
->u
.flags
.flags
[j
].name
);
385 pos
+= strlen(decl
->u
.flags
.flags
[j
].name
);
390 pos
= print_help_msg(decl
, pos
);
391 print_default_flags(decl
, pos
);
396 static void print_bool_help(struct isl_arg
*decl
, const char *prefix
)
399 int no
= decl
->u
.b
.default_value
== 1;
400 pos
= print_arg_help(decl
, prefix
, no
);
401 pos
= print_help_msg(decl
, pos
);
402 if (decl
->offset
!= (size_t) -1)
403 print_default(decl
, no
? "yes" : "no", pos
);
407 static int print_argument_name(struct isl_arg
*decl
, const char *name
, int pos
)
409 printf("%c<%s>", decl
->long_name
? '=' : ' ', name
);
410 return pos
+ 3 + strlen(name
);
413 static void print_int_help(struct isl_arg
*decl
, const char *prefix
)
417 pos
= print_arg_help(decl
, prefix
, 0);
418 pos
= print_argument_name(decl
, decl
->argument_name
, pos
);
419 pos
= print_help_msg(decl
, pos
);
420 snprintf(val
, sizeof(val
), "%d", decl
->u
.i
.default_value
);
421 print_default(decl
, val
, pos
);
425 static void print_long_help(struct isl_arg
*decl
, const char *prefix
)
428 pos
= print_arg_help(decl
, prefix
, 0);
429 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
435 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
439 print_help_msg(decl
, pos
);
443 static void print_ulong_help(struct isl_arg
*decl
, const char *prefix
)
446 pos
= print_arg_help(decl
, prefix
, 0);
449 print_help_msg(decl
, pos
);
453 static void print_str_help(struct isl_arg
*decl
, const char *prefix
)
456 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
457 pos
= print_arg_help(decl
, prefix
, 0);
458 pos
= print_argument_name(decl
, a
, pos
);
459 pos
= print_help_msg(decl
, pos
);
460 if (decl
->u
.str
.default_value
)
461 print_default(decl
, decl
->u
.str
.default_value
, pos
);
465 static void print_help(struct isl_arg
*arg
, const char *prefix
)
470 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
471 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
473 switch (arg
[i
].type
) {
475 print_flags_help(&arg
[i
], prefix
);
479 print_choice_help(&arg
[i
], prefix
);
483 print_bool_help(&arg
[i
], prefix
);
487 print_int_help(&arg
[i
], prefix
);
491 print_long_help(&arg
[i
], prefix
);
495 print_ulong_help(&arg
[i
], prefix
);
499 print_str_help(&arg
[i
], prefix
);
503 case isl_arg_version
:
513 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
514 if (arg
[i
].type
!= isl_arg_child
)
516 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
522 printf(" %s\n", arg
[i
].help_msg
);
523 print_help(arg
[i
].u
.child
.child
, arg
[i
].long_name
);
528 static const char *prog_name(const char *prog
)
532 slash
= strrchr(prog
, '/');
535 if (strncmp(prog
, "lt-", 3) == 0)
541 static int any_version(struct isl_arg
*decl
)
545 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
546 switch (decl
[i
].type
) {
547 case isl_arg_version
:
550 if (any_version(decl
[i
].u
.child
.child
))
561 static void print_help_and_exit(struct isl_arg
*arg
, const char *prog
)
565 printf("Usage: %s [OPTION...]", prog_name(prog
));
567 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
568 if (arg
[i
].type
== isl_arg_arg
)
569 printf(" %s", arg
[i
].argument_name
);
573 print_help(arg
, NULL
);
575 if (any_version(arg
))
576 printf(" -V, --version\n");
577 print_bool_help(help_arg
, NULL
);
579 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
580 if (arg
[i
].type
!= isl_arg_footer
)
582 wrap_msg(arg
[i
].help_msg
, 0, 0);
589 static int match_long_name(struct isl_arg
*decl
,
590 const char *start
, const char *end
)
593 if (end
- start
== strlen(decl
->long_name
) &&
594 !strncmp(start
, decl
->long_name
, end
- start
))
596 } while ((++decl
)->type
== isl_arg_alias
);
601 static const char *skip_dash_dash(struct isl_arg
*decl
, const char *arg
)
603 if (!strncmp(arg
, "--", 2))
605 if ((decl
->flags
& ISL_ARG_SINGLE_DASH
) && arg
[0] == '-')
610 static const char *skip_name(struct isl_arg
*decl
, const char *arg
,
611 const char *prefix
, int need_argument
, int *has_argument
)
617 if (arg
[0] == '-' && arg
[1] && arg
[1] == decl
->short_name
) {
618 if (need_argument
&& !arg
[2])
621 *has_argument
= arg
[2] != '\0';
624 if (!decl
->long_name
)
627 name
= skip_dash_dash(decl
, arg
);
631 equal
= strchr(name
, '=');
632 if (need_argument
&& !equal
)
636 *has_argument
= !!equal
;
637 end
= equal
? equal
: name
+ strlen(name
);
640 size_t prefix_len
= strlen(prefix
);
641 if (strncmp(name
, prefix
, prefix_len
) == 0 &&
642 name
[prefix_len
] == '-')
643 name
+= prefix_len
+ 1;
646 if (!match_long_name(decl
, name
, end
))
649 return equal
? equal
+ 1 : end
;
652 static int parse_choice_option(struct isl_arg
*decl
, char **arg
,
653 const char *prefix
, void *opt
)
659 choice
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
663 if (!has_argument
&& (!arg
[1] || arg
[1][0] == '-')) {
664 unsigned u
= decl
->u
.choice
.default_selected
;
665 if (decl
->u
.choice
.set
)
666 decl
->u
.choice
.set(opt
, u
);
668 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
676 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
679 if (strcmp(choice
, decl
->u
.choice
.choice
[i
].name
))
682 u
= decl
->u
.choice
.choice
[i
].value
;
683 if (decl
->u
.choice
.set
)
684 decl
->u
.choice
.set(opt
, u
);
686 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
688 return has_argument
? 1 : 2;
694 static int set_flag(struct isl_arg
*decl
, unsigned *val
, const char *flag
,
699 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
700 if (strncmp(flag
, decl
->u
.flags
.flags
[i
].name
, len
))
703 *val
&= ~decl
->u
.flags
.flags
[i
].mask
;
704 *val
|= decl
->u
.flags
.flags
[i
].value
;
712 static int parse_flags_option(struct isl_arg
*decl
, char **arg
,
713 const char *prefix
, void *opt
)
720 flags
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
724 if (!has_argument
&& !arg
[1])
730 val
= *(unsigned *)(((char *)opt
) + decl
->offset
);
732 while ((comma
= strchr(flags
, ',')) != NULL
) {
733 if (!set_flag(decl
, &val
, flags
, comma
- flags
))
737 if (!set_flag(decl
, &val
, flags
, strlen(flags
)))
740 *(unsigned *)(((char *)opt
) + decl
->offset
) = val
;
742 return has_argument
? 1 : 2;
745 static int parse_bool_option(struct isl_arg
*decl
, char **arg
,
746 const char *prefix
, void *opt
)
749 unsigned *p
= (unsigned *)(((char *)opt
) + decl
->offset
);
751 if (skip_name(decl
, arg
[0], prefix
, 0, NULL
)) {
752 if ((decl
->flags
&& ISL_ARG_BOOL_ARG
) && arg
[1]) {
754 int val
= strtol(arg
[1], &endptr
, 0);
755 if (*endptr
== '\0' && (val
== 0 || val
== 1)) {
757 decl
->u
.b
.set(opt
, val
);
758 else if (decl
->offset
!= (size_t) -1)
764 decl
->u
.b
.set(opt
, 1);
765 else if (decl
->offset
!= (size_t) -1)
771 if (!decl
->long_name
)
774 name
= skip_dash_dash(decl
, arg
[0]);
779 size_t prefix_len
= strlen(prefix
);
780 if (strncmp(name
, prefix
, prefix_len
) == 0 &&
781 name
[prefix_len
] == '-') {
782 name
+= prefix_len
+ 1;
787 if (strncmp(name
, "no-", 3))
792 size_t prefix_len
= strlen(prefix
);
793 if (strncmp(name
, prefix
, prefix_len
) == 0 &&
794 name
[prefix_len
] == '-')
795 name
+= prefix_len
+ 1;
798 if (match_long_name(decl
, name
, name
+ strlen(name
))) {
800 decl
->u
.b
.set(opt
, 0);
801 else if (decl
->offset
!= (size_t) -1)
810 static int parse_str_option(struct isl_arg
*decl
, char **arg
,
811 const char *prefix
, void *opt
)
815 char **p
= (char **)(((char *)opt
) + decl
->offset
);
817 s
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
836 static int parse_int_option(struct isl_arg
*decl
, char **arg
,
837 const char *prefix
, void *opt
)
842 int *p
= (int *)(((char *)opt
) + decl
->offset
);
844 val
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
854 int i
= strtol(arg
[1], &endptr
, 0);
855 if (*endptr
== '\0') {
864 static int parse_long_option(struct isl_arg
*decl
, char **arg
,
865 const char *prefix
, void *opt
)
870 long *p
= (long *)(((char *)opt
) + decl
->offset
);
872 val
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
877 long l
= strtol(val
, NULL
, 0);
879 decl
->u
.l
.set(opt
, l
);
886 long l
= strtol(arg
[1], &endptr
, 0);
887 if (*endptr
== '\0') {
889 decl
->u
.l
.set(opt
, l
);
896 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
898 decl
->u
.l
.set(opt
, decl
->u
.l
.default_selected
);
900 *p
= decl
->u
.l
.default_selected
;
907 static int parse_ulong_option(struct isl_arg
*decl
, char **arg
,
908 const char *prefix
, void *opt
)
913 unsigned long *p
= (unsigned long *)(((char *)opt
) + decl
->offset
);
915 val
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
920 *p
= strtoul(val
, NULL
, 0);
925 unsigned long ul
= strtoul(arg
[1], &endptr
, 0);
926 if (*endptr
== '\0') {
935 static int parse_option(struct isl_arg
*decl
, char **arg
,
936 const char *prefix
, void *opt
);
938 static int parse_child_option(struct isl_arg
*decl
, char **arg
,
939 const char *prefix
, void *opt
)
943 if (decl
->offset
== (size_t) -1)
946 child
= *(void **)(((char *)opt
) + decl
->offset
);
947 prefix
= decl
->long_name
;
949 return parse_option(decl
->u
.child
.child
, arg
, prefix
, child
);
952 static int parse_option(struct isl_arg
*decl
, char **arg
,
953 const char *prefix
, void *opt
)
957 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
959 switch (decl
[i
].type
) {
961 parsed
= parse_choice_option(&decl
[i
], arg
, prefix
, opt
);
964 parsed
= parse_flags_option(&decl
[i
], arg
, prefix
, opt
);
967 parsed
= parse_int_option(&decl
[i
], arg
, prefix
, opt
);
970 parsed
= parse_long_option(&decl
[i
], arg
, prefix
, opt
);
973 parsed
= parse_ulong_option(&decl
[i
], arg
, prefix
, opt
);
976 parsed
= parse_bool_option(&decl
[i
], arg
, prefix
, opt
);
979 parsed
= parse_str_option(&decl
[i
], arg
, prefix
, opt
);
982 parsed
= parse_child_option(&decl
[i
], arg
, prefix
, opt
);
988 case isl_arg_version
:
999 static void print_version(struct isl_arg
*decl
)
1003 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1004 switch (decl
[i
].type
) {
1005 case isl_arg_version
:
1006 decl
[i
].u
.version
.print_version();
1009 print_version(decl
[i
].u
.child
.child
);
1017 static void print_version_and_exit(struct isl_arg
*decl
)
1019 print_version(decl
);
1024 static int drop_argument(int argc
, char **argv
, int drop
, int n
)
1026 for (; drop
< argc
; ++drop
)
1027 argv
[drop
] = argv
[drop
+ n
];
1032 static int n_arg(struct isl_arg
*arg
)
1037 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
1038 if (arg
[i
].type
== isl_arg_arg
)
1044 static int next_arg(struct isl_arg
*arg
, int a
)
1046 for (++a
; arg
[a
].type
!= isl_arg_end
; ++a
)
1047 if (arg
[a
].type
== isl_arg_arg
)
1053 int isl_arg_parse(struct isl_arg
*arg
, int argc
, char **argv
, void *opt
,
1063 for (i
= 1; i
< argc
; ++i
) {
1064 if (strcmp(argv
[i
], "--help") == 0)
1065 print_help_and_exit(arg
, argv
[0]);
1068 for (i
= 1; i
< argc
; ++i
) {
1069 if ((strcmp(argv
[i
], "--version") == 0 ||
1070 strcmp(argv
[i
], "-V") == 0) && any_version(arg
))
1071 print_version_and_exit(arg
);
1074 while (argc
> 1 + skip
) {
1076 if (argv
[1 + skip
][0] != '-') {
1077 a
= next_arg(arg
, a
);
1080 p
= (char **)(((char *)opt
)+arg
[a
].offset
);
1082 *p
= strdup(argv
[1 + skip
]);
1083 argc
= drop_argument(argc
, argv
, 1 + skip
, 1);
1085 } else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1086 fprintf(stderr
, "%s: extra argument: %s\n",
1087 prog_name(argv
[0]), argv
[1 + skip
]);
1093 parsed
= parse_option(arg
, &argv
[1 + skip
], NULL
, opt
);
1095 argc
= drop_argument(argc
, argv
, 1 + skip
, parsed
);
1096 else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1097 fprintf(stderr
, "%s: unrecognized option: %s\n",
1098 prog_name(argv
[0]), argv
[1 + skip
]);
1105 fprintf(stderr
, "%s: expecting %d more argument(s)\n",
1106 prog_name(argv
[0]), n
);