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
== ISL_ARG_OFFSET_NONE
)
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
== ISL_ARG_OFFSET_NONE
)
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
== ISL_ARG_OFFSET_NONE
)
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
== ISL_ARG_OFFSET_NONE
)
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
!= ISL_ARG_OFFSET_NONE
)
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
== ISL_ARG_OFFSET_NONE
)
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 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
816 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
817 if (decl
->u
.choice
.set
)
818 decl
->u
.choice
.set(opt
, u
);
826 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
829 if (strcmp(choice
, decl
->u
.choice
.choice
[i
].name
))
832 u
= decl
->u
.choice
.choice
[i
].value
;
833 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
834 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
835 if (decl
->u
.choice
.set
)
836 decl
->u
.choice
.set(opt
, u
);
838 return has_argument
? 1 : 2;
844 static int set_flag(struct isl_arg
*decl
, unsigned *val
, const char *flag
,
849 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
850 if (strncmp(flag
, decl
->u
.flags
.flags
[i
].name
, len
))
853 *val
&= ~decl
->u
.flags
.flags
[i
].mask
;
854 *val
|= decl
->u
.flags
.flags
[i
].value
;
862 static int parse_flags_option(struct isl_arg
*decl
, char **arg
,
863 struct isl_prefixes
*prefixes
, void *opt
)
870 flags
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
874 if (!has_argument
&& !arg
[1])
882 while ((comma
= strchr(flags
, ',')) != NULL
) {
883 if (!set_flag(decl
, &val
, flags
, comma
- flags
))
887 if (!set_flag(decl
, &val
, flags
, strlen(flags
)))
890 *(unsigned *)(((char *)opt
) + decl
->offset
) = val
;
892 return has_argument
? 1 : 2;
895 static int parse_bool_option(struct isl_arg
*decl
, char **arg
,
896 struct isl_prefixes
*prefixes
, void *opt
)
899 unsigned *p
= (unsigned *)(((char *)opt
) + decl
->offset
);
902 if (skip_name(decl
, arg
[0], prefixes
, 0, NULL
)) {
903 if ((decl
->flags
& ISL_ARG_BOOL_ARG
) && arg
[1]) {
905 int val
= strtol(arg
[1], &endptr
, 0);
906 if (*endptr
== '\0' && (val
== 0 || val
== 1)) {
907 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
910 decl
->u
.b
.set(opt
, val
);
914 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
917 decl
->u
.b
.set(opt
, 1);
922 if (!decl
->long_name
)
925 name
= skip_dash_dash(decl
, arg
[0]);
930 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
932 if (strncmp(name
, "no-", 3))
936 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
938 if (match_long_name(decl
, name
, name
+ strlen(name
))) {
939 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
942 decl
->u
.b
.set(opt
, 0);
950 static int parse_str_option(struct isl_arg
*decl
, char **arg
,
951 struct isl_prefixes
*prefixes
, void *opt
)
955 char **p
= (char **)(((char *)opt
) + decl
->offset
);
957 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
976 static int isl_arg_str_list_append(struct isl_arg
*decl
, void *opt
,
979 int *n
= (int *)(((char *) opt
) + decl
->u
.str_list
.offset_n
);
980 char **list
= *(char ***)(((char *) opt
) + decl
->offset
);
982 list
= realloc(list
, (*n
+ 1) * sizeof(char *));
985 *(char ***)(((char *) opt
) + decl
->offset
) = list
;
986 list
[*n
] = strdup(s
);
991 static int parse_str_list_option(struct isl_arg
*decl
, char **arg
,
992 struct isl_prefixes
*prefixes
, void *opt
)
997 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1002 isl_arg_str_list_append(decl
, opt
, s
);
1007 isl_arg_str_list_append(decl
, opt
, arg
[1]);
1014 static int parse_int_option(struct isl_arg
*decl
, char **arg
,
1015 struct isl_prefixes
*prefixes
, void *opt
)
1020 int *p
= (int *)(((char *)opt
) + decl
->offset
);
1022 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1032 int i
= strtol(arg
[1], &endptr
, 0);
1033 if (*endptr
== '\0') {
1042 static int parse_long_option(struct isl_arg
*decl
, char **arg
,
1043 struct isl_prefixes
*prefixes
, void *opt
)
1048 long *p
= (long *)(((char *)opt
) + decl
->offset
);
1050 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1055 long l
= strtol(val
, NULL
, 0);
1058 decl
->u
.l
.set(opt
, l
);
1063 long l
= strtol(arg
[1], &endptr
, 0);
1064 if (*endptr
== '\0') {
1067 decl
->u
.l
.set(opt
, l
);
1072 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
1073 *p
= decl
->u
.l
.default_selected
;
1075 decl
->u
.l
.set(opt
, decl
->u
.l
.default_selected
);
1082 static int parse_ulong_option(struct isl_arg
*decl
, char **arg
,
1083 struct isl_prefixes
*prefixes
, void *opt
)
1088 unsigned long *p
= (unsigned long *)(((char *)opt
) + decl
->offset
);
1090 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1095 *p
= strtoul(val
, NULL
, 0);
1100 unsigned long ul
= strtoul(arg
[1], &endptr
, 0);
1101 if (*endptr
== '\0') {
1110 static int parse_option(struct isl_arg
*decl
, char **arg
,
1111 struct isl_prefixes
*prefixes
, void *opt
);
1113 static int parse_child_option(struct isl_arg
*decl
, char **arg
,
1114 struct isl_prefixes
*prefixes
, void *opt
)
1119 if (decl
->offset
== ISL_ARG_OFFSET_NONE
)
1122 child
= *(void **)(((char *)opt
) + decl
->offset
);
1124 first
= add_prefix(prefixes
, decl
->long_name
);
1125 parsed
= parse_option(decl
->u
.child
.child
->args
, arg
, prefixes
, child
);
1126 drop_prefix(prefixes
, first
);
1131 static int parse_option(struct isl_arg
*decl
, char **arg
,
1132 struct isl_prefixes
*prefixes
, void *opt
)
1136 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1138 switch (decl
[i
].type
) {
1139 case isl_arg_choice
:
1140 parsed
= parse_choice_option(&decl
[i
], arg
,
1144 parsed
= parse_flags_option(&decl
[i
], arg
,
1148 parsed
= parse_int_option(&decl
[i
], arg
, prefixes
, opt
);
1151 parsed
= parse_long_option(&decl
[i
], arg
,
1155 parsed
= parse_ulong_option(&decl
[i
], arg
,
1159 parsed
= parse_bool_option(&decl
[i
], arg
,
1163 parsed
= parse_str_option(&decl
[i
], arg
, prefixes
, opt
);
1165 case isl_arg_str_list
:
1166 parsed
= parse_str_list_option(&decl
[i
], arg
, prefixes
,
1170 parsed
= parse_child_option(&decl
[i
], arg
,
1175 case isl_arg_footer
:
1177 case isl_arg_version
:
1188 static void print_version(struct isl_arg
*decl
)
1192 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1193 switch (decl
[i
].type
) {
1194 case isl_arg_version
:
1195 decl
[i
].u
.version
.print_version();
1198 print_version(decl
[i
].u
.child
.child
->args
);
1206 static void print_version_and_exit(struct isl_arg
*decl
)
1208 print_version(decl
);
1213 static int drop_argument(int argc
, char **argv
, int drop
, int n
)
1215 for (; drop
+ n
< argc
; ++drop
)
1216 argv
[drop
] = argv
[drop
+ n
];
1221 static int n_arg(struct isl_arg
*arg
)
1226 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
1227 if (arg
[i
].type
== isl_arg_arg
)
1233 static int next_arg(struct isl_arg
*arg
, int a
)
1235 for (++a
; arg
[a
].type
!= isl_arg_end
; ++a
)
1236 if (arg
[a
].type
== isl_arg_arg
)
1242 /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
1243 * equal to "--help" and if so call print_help_and_exit.
1245 static void check_help(struct isl_args
*args
, char *arg
, char *prog
, void *opt
,
1248 if (ISL_FL_ISSET(flags
, ISL_ARG_SKIP_HELP
))
1251 if (strcmp(arg
, "--help") == 0)
1252 print_help_and_exit(args
->args
, prog
, opt
);
1255 int isl_args_parse(struct isl_args
*args
, int argc
, char **argv
, void *opt
,
1262 struct isl_prefixes prefixes
= { 0 };
1264 n
= n_arg(args
->args
);
1266 for (i
= 1; i
< argc
; ++i
) {
1267 if ((strcmp(argv
[i
], "--version") == 0 ||
1268 strcmp(argv
[i
], "-V") == 0) && any_version(args
->args
))
1269 print_version_and_exit(args
->args
);
1272 while (argc
> 1 + skip
) {
1274 if (argv
[1 + skip
][0] != '-') {
1275 a
= next_arg(args
->args
, a
);
1278 p
= (char **)(((char *)opt
)+args
->args
[a
].offset
);
1280 *p
= strdup(argv
[1 + skip
]);
1281 argc
= drop_argument(argc
, argv
, 1 + skip
, 1);
1283 } else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1284 fprintf(stderr
, "%s: extra argument: %s\n",
1285 prog_name(argv
[0]), argv
[1 + skip
]);
1291 check_help(args
, argv
[1 + skip
], argv
[0], opt
, flags
);
1292 parsed
= parse_option(args
->args
, &argv
[1 + skip
],
1295 argc
= drop_argument(argc
, argv
, 1 + skip
, parsed
);
1296 else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1297 fprintf(stderr
, "%s: unrecognized option: %s\n",
1298 prog_name(argv
[0]), argv
[1 + skip
]);
1305 fprintf(stderr
, "%s: expecting %d more argument(s)\n",
1306 prog_name(argv
[0]), n
);