2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
16 #include <isl_config.h>
18 static struct isl_arg help_arg
[] = {
19 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 if (arg
->offset
== (size_t) -1)
26 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.choice
.default_value
;
29 static void set_default_flags(struct isl_arg
*arg
, void *opt
)
31 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.flags
.default_value
;
34 static void set_default_bool(struct isl_arg
*arg
, void *opt
)
36 if (arg
->offset
== (size_t) -1)
38 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.b
.default_value
;
41 static void set_default_child(struct isl_arg
*arg
, void *opt
)
45 if (arg
->offset
== (size_t) -1)
48 child
= calloc(1, arg
->u
.child
.child
->options_size
);
49 *(void **)(((char *)opt
) + arg
->offset
) = child
;
53 isl_args_set_defaults(arg
->u
.child
.child
, child
);
56 static void set_default_user(struct isl_arg
*arg
, void *opt
)
58 arg
->u
.user
.init(((char *)opt
) + arg
->offset
);
61 static void set_default_int(struct isl_arg
*arg
, void *opt
)
63 *(int *)(((char *)opt
) + arg
->offset
) = arg
->u
.i
.default_value
;
66 static void set_default_long(struct isl_arg
*arg
, void *opt
)
68 *(long *)(((char *)opt
) + arg
->offset
) = arg
->u
.l
.default_value
;
71 static void set_default_ulong(struct isl_arg
*arg
, void *opt
)
73 *(unsigned long *)(((char *)opt
) + arg
->offset
) = arg
->u
.ul
.default_value
;
76 static void set_default_str(struct isl_arg
*arg
, void *opt
)
78 const char *str
= NULL
;
79 if (arg
->u
.str
.default_value
)
80 str
= strdup(arg
->u
.str
.default_value
);
81 *(const char **)(((char *)opt
) + arg
->offset
) = str
;
84 static void set_default_str_list(struct isl_arg
*arg
, void *opt
)
86 *(const char ***)(((char *) opt
) + arg
->offset
) = NULL
;
87 *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
) = 0;
90 void isl_args_set_defaults(struct isl_args
*args
, void *opt
)
94 for (i
= 0; args
->args
[i
].type
!= isl_arg_end
; ++i
) {
95 switch (args
->args
[i
].type
) {
97 set_default_choice(&args
->args
[i
], opt
);
100 set_default_flags(&args
->args
[i
], opt
);
103 set_default_bool(&args
->args
[i
], opt
);
106 set_default_child(&args
->args
[i
], opt
);
109 set_default_user(&args
->args
[i
], opt
);
112 set_default_int(&args
->args
[i
], opt
);
115 set_default_long(&args
->args
[i
], opt
);
118 set_default_ulong(&args
->args
[i
], opt
);
122 set_default_str(&args
->args
[i
], opt
);
124 case isl_arg_str_list
:
125 set_default_str_list(&args
->args
[i
], opt
);
129 case isl_arg_version
:
136 static void free_args(struct isl_arg
*arg
, void *opt
);
138 static void free_child(struct isl_arg
*arg
, void *opt
)
140 if (arg
->offset
== (size_t) -1)
141 free_args(arg
->u
.child
.child
->args
, opt
);
143 isl_args_free(arg
->u
.child
.child
,
144 *(void **)(((char *)opt
) + arg
->offset
));
147 static void free_str_list(struct isl_arg
*arg
, void *opt
)
150 int n
= *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
);
151 char **list
= *(char ***)(((char *) opt
) + arg
->offset
);
153 for (i
= 0; i
< n
; ++i
)
158 static void free_user(struct isl_arg
*arg
, void *opt
)
160 if (arg
->u
.user
.clear
)
161 arg
->u
.user
.clear(((char *)opt
) + arg
->offset
);
164 static void free_args(struct isl_arg
*arg
, void *opt
)
168 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
169 switch (arg
[i
].type
) {
171 free_child(&arg
[i
], opt
);
175 free(*(char **)(((char *)opt
) + arg
[i
].offset
));
177 case isl_arg_str_list
:
178 free_str_list(&arg
[i
], opt
);
181 free_user(&arg
[i
], opt
);
190 case isl_arg_version
:
198 void isl_args_free(struct isl_args
*args
, void *opt
)
203 free_args(args
->args
, opt
);
208 /* Data structure for collecting the prefixes of ancestor nodes.
210 * n is the number of prefixes.
211 * prefix[i] for i < n is a prefix of an ancestor.
212 * len[i] for i < n is the length of prefix[i].
214 struct isl_prefixes
{
216 const char *prefix
[10];
220 /* Add "prefix" to the list of prefixes and return the updated
221 * number of prefixes.
223 static int add_prefix(struct isl_prefixes
*prefixes
, const char *prefix
)
230 if (prefixes
->n
>= 10) {
231 fprintf(stderr
, "too many prefixes\n");
234 prefixes
->len
[prefixes
->n
] = strlen(prefix
);
235 prefixes
->prefix
[prefixes
->n
] = prefix
;
241 /* Drop all prefixes starting at "first".
243 static void drop_prefix(struct isl_prefixes
*prefixes
, int first
)
248 /* Print the prefixes in "prefixes".
250 static int print_prefixes(struct isl_prefixes
*prefixes
)
258 for (i
= 0; i
< prefixes
->n
; ++i
) {
259 printf("%s-", prefixes
->prefix
[i
]);
260 len
+= strlen(prefixes
->prefix
[i
]) + 1;
266 /* Check if "name" starts with one or more of the prefixes in "prefixes",
267 * starting at *first. If so, advance the pointer beyond the prefixes
268 * and return the updated pointer. Additionally, update *first to
269 * the index after the last prefix found.
271 static const char *skip_prefixes(const char *name
,
272 struct isl_prefixes
*prefixes
, int *first
)
276 for (i
= first
? *first
: 0; i
< prefixes
->n
; ++i
) {
277 size_t len
= prefixes
->len
[i
];
278 const char *prefix
= prefixes
->prefix
[i
];
279 if (strncmp(name
, prefix
, len
) == 0 && name
[len
] == '-') {
289 static int print_arg_help(struct isl_arg
*decl
, struct isl_prefixes
*prefixes
,
294 if (!decl
->long_name
) {
295 printf(" -%c", decl
->short_name
);
299 if (decl
->short_name
) {
300 printf(" -%c, --", decl
->short_name
);
302 } else if (decl
->flags
& ISL_ARG_SINGLE_DASH
) {
314 len
+= print_prefixes(prefixes
);
315 printf("%s", decl
->long_name
);
316 len
+= strlen(decl
->long_name
);
318 while ((++decl
)->type
== isl_arg_alias
) {
325 printf("%s", decl
->long_name
);
326 len
+= strlen(decl
->long_name
);
332 const void *isl_memrchr(const void *s
, int c
, size_t n
)
341 static int wrap_msg(const char *s
, int indent
, int pos
)
344 int wrap_len
= 75 - indent
;
346 if (pos
+ 1 >= indent
)
347 printf("\n%*s", indent
, "");
349 printf("%*s", indent
- pos
, "");
352 while (len
> wrap_len
) {
353 const char *space
= isl_memrchr(s
, ' ', wrap_len
);
357 space
= strchr(s
+ wrap_len
, ' ');
361 printf("%.*s", l
, s
);
364 printf("\n%*s", indent
, "");
371 static int print_help_msg(struct isl_arg
*decl
, int pos
)
376 return wrap_msg(decl
->help_msg
, 30, pos
);
379 static void print_default(struct isl_arg
*decl
, const char *def
, int pos
)
381 const char *default_prefix
= "[default: ";
382 const char *default_suffix
= "]";
385 len
= strlen(default_prefix
) + strlen(def
) + strlen(default_suffix
);
387 if (!decl
->help_msg
) {
389 printf("\n%30s", "");
391 printf("%*s", 30 - pos
, "");
394 printf("\n%30s", "");
398 printf("%s%s%s", default_prefix
, def
, default_suffix
);
401 static void print_default_choice(struct isl_arg
*decl
, void *opt
, int pos
)
404 const char *s
= "none";
407 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
408 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
)
409 if (decl
->u
.choice
.choice
[i
].value
== *p
) {
410 s
= decl
->u
.choice
.choice
[i
].name
;
414 print_default(decl
, s
, pos
);
417 static void print_choice_help(struct isl_arg
*decl
,
418 struct isl_prefixes
*prefixes
, void *opt
)
423 pos
= print_arg_help(decl
, prefixes
, 0);
427 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
432 printf("%s", decl
->u
.choice
.choice
[i
].name
);
433 pos
+= strlen(decl
->u
.choice
.choice
[i
].name
);
436 pos
= print_help_msg(decl
, pos
);
437 print_default_choice(decl
, opt
, pos
);
442 static void print_default_flags(struct isl_arg
*decl
, void *opt
, int pos
)
445 const char *default_prefix
= "[default: ";
446 const char *default_suffix
= "]";
447 int len
= strlen(default_prefix
) + strlen(default_suffix
);
450 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
451 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
452 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
453 decl
->u
.flags
.flags
[i
].value
)
454 len
+= strlen(decl
->u
.flags
.flags
[i
].name
);
456 if (!decl
->help_msg
) {
458 printf("\n%30s", "");
460 printf("%*s", 30 - pos
, "");
463 printf("\n%30s", "");
467 printf("%s", default_prefix
);
469 for (first
= 1, i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
470 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
471 decl
->u
.flags
.flags
[i
].value
) {
474 printf("%s", decl
->u
.flags
.flags
[i
].name
);
478 printf("%s", default_suffix
);
481 static void print_flags_help(struct isl_arg
*decl
,
482 struct isl_prefixes
*prefixes
, void *opt
)
487 pos
= print_arg_help(decl
, prefixes
, 0);
491 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
497 decl
->u
.flags
.flags
[j
].mask
== decl
->u
.flags
.flags
[i
].mask
;
503 printf("%s", decl
->u
.flags
.flags
[j
].name
);
504 pos
+= strlen(decl
->u
.flags
.flags
[j
].name
);
509 pos
= print_help_msg(decl
, pos
);
510 print_default_flags(decl
, opt
, pos
);
515 static void print_bool_help(struct isl_arg
*decl
,
516 struct isl_prefixes
*prefixes
, void *opt
)
519 unsigned *p
= opt
? (unsigned *)(((char *) opt
) + decl
->offset
) : NULL
;
520 int no
= p
? *p
== 1 : 0;
521 pos
= print_arg_help(decl
, prefixes
, no
);
522 pos
= print_help_msg(decl
, pos
);
523 if (decl
->offset
!= (size_t) -1)
524 print_default(decl
, no
? "yes" : "no", pos
);
528 static int print_argument_name(struct isl_arg
*decl
, const char *name
, int pos
)
530 printf("%c<%s>", decl
->long_name
? '=' : ' ', name
);
531 return pos
+ 3 + strlen(name
);
534 static void print_int_help(struct isl_arg
*decl
,
535 struct isl_prefixes
*prefixes
, void *opt
)
539 int *p
= (int *)(((char *) opt
) + decl
->offset
);
540 pos
= print_arg_help(decl
, prefixes
, 0);
541 pos
= print_argument_name(decl
, decl
->argument_name
, pos
);
542 pos
= print_help_msg(decl
, pos
);
543 snprintf(val
, sizeof(val
), "%d", *p
);
544 print_default(decl
, val
, pos
);
548 static void print_long_help(struct isl_arg
*decl
,
549 struct isl_prefixes
*prefixes
, void *opt
)
552 long *p
= (long *)(((char *) opt
) + decl
->offset
);
553 pos
= print_arg_help(decl
, prefixes
, 0);
554 if (*p
!= decl
->u
.l
.default_selected
) {
560 if (*p
!= decl
->u
.l
.default_selected
) {
564 print_help_msg(decl
, pos
);
568 static void print_ulong_help(struct isl_arg
*decl
,
569 struct isl_prefixes
*prefixes
)
572 pos
= print_arg_help(decl
, prefixes
, 0);
575 print_help_msg(decl
, pos
);
579 static void print_str_help(struct isl_arg
*decl
,
580 struct isl_prefixes
*prefixes
, void *opt
)
583 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
584 const char **p
= (const char **)(((char *) opt
) + decl
->offset
);
585 pos
= print_arg_help(decl
, prefixes
, 0);
586 pos
= print_argument_name(decl
, a
, pos
);
587 pos
= print_help_msg(decl
, pos
);
589 print_default(decl
, *p
, pos
);
593 static void print_str_list_help(struct isl_arg
*decl
,
594 struct isl_prefixes
*prefixes
)
597 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
598 pos
= print_arg_help(decl
, prefixes
, 0);
599 pos
= print_argument_name(decl
, a
, pos
);
600 pos
= print_help_msg(decl
, pos
);
604 static void print_help(struct isl_arg
*arg
,
605 struct isl_prefixes
*prefixes
, void *opt
)
610 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
611 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
613 switch (arg
[i
].type
) {
615 print_flags_help(&arg
[i
], prefixes
, opt
);
619 print_choice_help(&arg
[i
], prefixes
, opt
);
623 print_bool_help(&arg
[i
], prefixes
, opt
);
627 print_int_help(&arg
[i
], prefixes
, opt
);
631 print_long_help(&arg
[i
], prefixes
, opt
);
635 print_ulong_help(&arg
[i
], prefixes
);
639 print_str_help(&arg
[i
], prefixes
, opt
);
642 case isl_arg_str_list
:
643 print_str_list_help(&arg
[i
], prefixes
);
647 case isl_arg_version
:
657 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
661 if (arg
[i
].type
!= isl_arg_child
)
663 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
669 printf(" %s\n", arg
[i
].help_msg
);
670 if (arg
[i
].offset
== (size_t) -1)
673 child
= *(void **)(((char *) opt
) + arg
[i
].offset
);
674 first
= add_prefix(prefixes
, arg
[i
].long_name
);
675 print_help(arg
[i
].u
.child
.child
->args
, prefixes
, child
);
676 drop_prefix(prefixes
, first
);
681 static const char *prog_name(const char *prog
)
685 slash
= strrchr(prog
, '/');
688 if (strncmp(prog
, "lt-", 3) == 0)
694 static int any_version(struct isl_arg
*decl
)
698 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
699 switch (decl
[i
].type
) {
700 case isl_arg_version
:
703 if (any_version(decl
[i
].u
.child
.child
->args
))
714 static void print_help_and_exit(struct isl_arg
*arg
, const char *prog
,
718 struct isl_prefixes prefixes
= { 0 };
720 printf("Usage: %s [OPTION...]", prog_name(prog
));
722 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
723 if (arg
[i
].type
== isl_arg_arg
)
724 printf(" %s", arg
[i
].argument_name
);
728 print_help(arg
, &prefixes
, opt
);
730 if (any_version(arg
))
731 printf(" -V, --version\n");
732 print_bool_help(help_arg
, NULL
, NULL
);
734 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
735 if (arg
[i
].type
!= isl_arg_footer
)
737 wrap_msg(arg
[i
].help_msg
, 0, 0);
744 static int match_long_name(struct isl_arg
*decl
,
745 const char *start
, const char *end
)
748 if (end
- start
== strlen(decl
->long_name
) &&
749 !strncmp(start
, decl
->long_name
, end
- start
))
751 } while ((++decl
)->type
== isl_arg_alias
);
756 static const char *skip_dash_dash(struct isl_arg
*decl
, const char *arg
)
758 if (!strncmp(arg
, "--", 2))
760 if ((decl
->flags
& ISL_ARG_SINGLE_DASH
) && arg
[0] == '-')
765 static const char *skip_name(struct isl_arg
*decl
, const char *arg
,
766 struct isl_prefixes
*prefixes
, int need_argument
, int *has_argument
)
772 if (arg
[0] == '-' && arg
[1] && arg
[1] == decl
->short_name
) {
773 if (need_argument
&& !arg
[2])
776 *has_argument
= arg
[2] != '\0';
779 if (!decl
->long_name
)
782 name
= skip_dash_dash(decl
, arg
);
786 equal
= strchr(name
, '=');
787 if (need_argument
&& !equal
)
791 *has_argument
= !!equal
;
792 end
= equal
? equal
: name
+ strlen(name
);
794 name
= skip_prefixes(name
, prefixes
, NULL
);
796 if (!match_long_name(decl
, name
, end
))
799 return equal
? equal
+ 1 : end
;
802 static int parse_choice_option(struct isl_arg
*decl
, char **arg
,
803 struct isl_prefixes
*prefixes
, void *opt
)
809 choice
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
813 if (!has_argument
&& (!arg
[1] || arg
[1][0] == '-')) {
814 unsigned u
= decl
->u
.choice
.default_selected
;
815 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
816 if (decl
->u
.choice
.set
)
817 decl
->u
.choice
.set(opt
, u
);
825 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
828 if (strcmp(choice
, decl
->u
.choice
.choice
[i
].name
))
831 u
= decl
->u
.choice
.choice
[i
].value
;
832 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
833 if (decl
->u
.choice
.set
)
834 decl
->u
.choice
.set(opt
, u
);
836 return has_argument
? 1 : 2;
842 static int set_flag(struct isl_arg
*decl
, unsigned *val
, const char *flag
,
847 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
848 if (strncmp(flag
, decl
->u
.flags
.flags
[i
].name
, len
))
851 *val
&= ~decl
->u
.flags
.flags
[i
].mask
;
852 *val
|= decl
->u
.flags
.flags
[i
].value
;
860 static int parse_flags_option(struct isl_arg
*decl
, char **arg
,
861 struct isl_prefixes
*prefixes
, void *opt
)
868 flags
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
872 if (!has_argument
&& !arg
[1])
880 while ((comma
= strchr(flags
, ',')) != NULL
) {
881 if (!set_flag(decl
, &val
, flags
, comma
- flags
))
885 if (!set_flag(decl
, &val
, flags
, strlen(flags
)))
888 *(unsigned *)(((char *)opt
) + decl
->offset
) = val
;
890 return has_argument
? 1 : 2;
893 static int parse_bool_option(struct isl_arg
*decl
, char **arg
,
894 struct isl_prefixes
*prefixes
, void *opt
)
897 unsigned *p
= (unsigned *)(((char *)opt
) + decl
->offset
);
900 if (skip_name(decl
, arg
[0], prefixes
, 0, NULL
)) {
901 if ((decl
->flags
& ISL_ARG_BOOL_ARG
) && arg
[1]) {
903 int val
= strtol(arg
[1], &endptr
, 0);
904 if (*endptr
== '\0' && (val
== 0 || val
== 1)) {
905 if (decl
->offset
!= (size_t) -1)
908 decl
->u
.b
.set(opt
, val
);
912 if (decl
->offset
!= (size_t) -1)
915 decl
->u
.b
.set(opt
, 1);
920 if (!decl
->long_name
)
923 name
= skip_dash_dash(decl
, arg
[0]);
928 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
930 if (strncmp(name
, "no-", 3))
934 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
936 if (match_long_name(decl
, name
, name
+ strlen(name
))) {
937 if (decl
->offset
!= (size_t) -1)
940 decl
->u
.b
.set(opt
, 0);
948 static int parse_str_option(struct isl_arg
*decl
, char **arg
,
949 struct isl_prefixes
*prefixes
, void *opt
)
953 char **p
= (char **)(((char *)opt
) + decl
->offset
);
955 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
974 static int isl_arg_str_list_append(struct isl_arg
*decl
, void *opt
,
977 int *n
= (int *)(((char *) opt
) + decl
->u
.str_list
.offset_n
);
978 char **list
= *(char ***)(((char *) opt
) + decl
->offset
);
980 list
= realloc(list
, (*n
+ 1) * sizeof(char *));
983 *(char ***)(((char *) opt
) + decl
->offset
) = list
;
984 list
[*n
] = strdup(s
);
989 static int parse_str_list_option(struct isl_arg
*decl
, char **arg
,
990 struct isl_prefixes
*prefixes
, void *opt
)
995 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1000 isl_arg_str_list_append(decl
, opt
, s
);
1005 isl_arg_str_list_append(decl
, opt
, arg
[1]);
1012 static int parse_int_option(struct isl_arg
*decl
, char **arg
,
1013 struct isl_prefixes
*prefixes
, void *opt
)
1018 int *p
= (int *)(((char *)opt
) + decl
->offset
);
1020 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1030 int i
= strtol(arg
[1], &endptr
, 0);
1031 if (*endptr
== '\0') {
1040 static int parse_long_option(struct isl_arg
*decl
, char **arg
,
1041 struct isl_prefixes
*prefixes
, void *opt
)
1046 long *p
= (long *)(((char *)opt
) + decl
->offset
);
1048 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1053 long l
= strtol(val
, NULL
, 0);
1056 decl
->u
.l
.set(opt
, l
);
1061 long l
= strtol(arg
[1], &endptr
, 0);
1062 if (*endptr
== '\0') {
1065 decl
->u
.l
.set(opt
, l
);
1070 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
1071 *p
= decl
->u
.l
.default_selected
;
1073 decl
->u
.l
.set(opt
, decl
->u
.l
.default_selected
);
1080 static int parse_ulong_option(struct isl_arg
*decl
, char **arg
,
1081 struct isl_prefixes
*prefixes
, void *opt
)
1086 unsigned long *p
= (unsigned long *)(((char *)opt
) + decl
->offset
);
1088 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1093 *p
= strtoul(val
, NULL
, 0);
1098 unsigned long ul
= strtoul(arg
[1], &endptr
, 0);
1099 if (*endptr
== '\0') {
1108 static int parse_option(struct isl_arg
*decl
, char **arg
,
1109 struct isl_prefixes
*prefixes
, void *opt
);
1111 static int parse_child_option(struct isl_arg
*decl
, char **arg
,
1112 struct isl_prefixes
*prefixes
, void *opt
)
1117 if (decl
->offset
== (size_t) -1)
1120 child
= *(void **)(((char *)opt
) + decl
->offset
);
1122 first
= add_prefix(prefixes
, decl
->long_name
);
1123 parsed
= parse_option(decl
->u
.child
.child
->args
, arg
, prefixes
, child
);
1124 drop_prefix(prefixes
, first
);
1129 static int parse_option(struct isl_arg
*decl
, char **arg
,
1130 struct isl_prefixes
*prefixes
, void *opt
)
1134 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1136 switch (decl
[i
].type
) {
1137 case isl_arg_choice
:
1138 parsed
= parse_choice_option(&decl
[i
], arg
,
1142 parsed
= parse_flags_option(&decl
[i
], arg
,
1146 parsed
= parse_int_option(&decl
[i
], arg
, prefixes
, opt
);
1149 parsed
= parse_long_option(&decl
[i
], arg
,
1153 parsed
= parse_ulong_option(&decl
[i
], arg
,
1157 parsed
= parse_bool_option(&decl
[i
], arg
,
1161 parsed
= parse_str_option(&decl
[i
], arg
, prefixes
, opt
);
1163 case isl_arg_str_list
:
1164 parsed
= parse_str_list_option(&decl
[i
], arg
, prefixes
,
1168 parsed
= parse_child_option(&decl
[i
], arg
,
1173 case isl_arg_footer
:
1175 case isl_arg_version
:
1186 static void print_version(struct isl_arg
*decl
)
1190 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1191 switch (decl
[i
].type
) {
1192 case isl_arg_version
:
1193 decl
[i
].u
.version
.print_version();
1196 print_version(decl
[i
].u
.child
.child
->args
);
1204 static void print_version_and_exit(struct isl_arg
*decl
)
1206 print_version(decl
);
1211 static int drop_argument(int argc
, char **argv
, int drop
, int n
)
1213 for (; drop
+ n
< argc
; ++drop
)
1214 argv
[drop
] = argv
[drop
+ n
];
1219 static int n_arg(struct isl_arg
*arg
)
1224 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
1225 if (arg
[i
].type
== isl_arg_arg
)
1231 static int next_arg(struct isl_arg
*arg
, int a
)
1233 for (++a
; arg
[a
].type
!= isl_arg_end
; ++a
)
1234 if (arg
[a
].type
== isl_arg_arg
)
1240 /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
1241 * equal to "--help" and if so call print_help_and_exit.
1243 static void check_help(struct isl_args
*args
, char *arg
, char *prog
, void *opt
,
1246 if (ISL_FL_ISSET(flags
, ISL_ARG_SKIP_HELP
))
1249 if (strcmp(arg
, "--help") == 0)
1250 print_help_and_exit(args
->args
, prog
, opt
);
1253 int isl_args_parse(struct isl_args
*args
, int argc
, char **argv
, void *opt
,
1260 struct isl_prefixes prefixes
= { 0 };
1262 n
= n_arg(args
->args
);
1264 for (i
= 1; i
< argc
; ++i
) {
1265 if ((strcmp(argv
[i
], "--version") == 0 ||
1266 strcmp(argv
[i
], "-V") == 0) && any_version(args
->args
))
1267 print_version_and_exit(args
->args
);
1270 while (argc
> 1 + skip
) {
1272 if (argv
[1 + skip
][0] != '-') {
1273 a
= next_arg(args
->args
, a
);
1276 p
= (char **)(((char *)opt
)+args
->args
[a
].offset
);
1278 *p
= strdup(argv
[1 + skip
]);
1279 argc
= drop_argument(argc
, argv
, 1 + skip
, 1);
1281 } else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1282 fprintf(stderr
, "%s: extra argument: %s\n",
1283 prog_name(argv
[0]), argv
[1 + skip
]);
1289 check_help(args
, argv
[1 + skip
], argv
[0], opt
, flags
);
1290 parsed
= parse_option(args
->args
, &argv
[1 + skip
],
1293 argc
= drop_argument(argc
, argv
, 1 + skip
, parsed
);
1294 else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1295 fprintf(stderr
, "%s: unrecognized option: %s\n",
1296 prog_name(argv
[0]), argv
[1 + skip
]);
1303 fprintf(stderr
, "%s: expecting %d more argument(s)\n",
1304 prog_name(argv
[0]), n
);