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")
23 static void set_default_choice(struct isl_arg
*arg
, void *opt
)
25 if (arg
->offset
== ISL_ARG_OFFSET_NONE
)
27 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.choice
.default_value
;
30 static void set_default_flags(struct isl_arg
*arg
, void *opt
)
32 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.flags
.default_value
;
35 static void set_default_bool(struct isl_arg
*arg
, void *opt
)
37 if (arg
->offset
== ISL_ARG_OFFSET_NONE
)
39 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.b
.default_value
;
42 static void set_default_child(struct isl_arg
*arg
, void *opt
)
46 if (arg
->offset
== ISL_ARG_OFFSET_NONE
)
49 child
= calloc(1, arg
->u
.child
.child
->options_size
);
50 *(void **)(((char *)opt
) + arg
->offset
) = child
;
54 isl_args_set_defaults(arg
->u
.child
.child
, child
);
57 static void set_default_user(struct isl_arg
*arg
, void *opt
)
59 arg
->u
.user
.init(((char *)opt
) + arg
->offset
);
62 static void set_default_int(struct isl_arg
*arg
, void *opt
)
64 *(int *)(((char *)opt
) + arg
->offset
) = arg
->u
.i
.default_value
;
67 static void set_default_long(struct isl_arg
*arg
, void *opt
)
69 *(long *)(((char *)opt
) + arg
->offset
) = arg
->u
.l
.default_value
;
72 static void set_default_ulong(struct isl_arg
*arg
, void *opt
)
74 *(unsigned long *)(((char *)opt
) + arg
->offset
) = arg
->u
.ul
.default_value
;
77 static void set_default_str(struct isl_arg
*arg
, void *opt
)
79 const char *str
= NULL
;
80 if (arg
->u
.str
.default_value
)
81 str
= strdup(arg
->u
.str
.default_value
);
82 *(const char **)(((char *)opt
) + arg
->offset
) = str
;
85 static void set_default_str_list(struct isl_arg
*arg
, void *opt
)
87 *(const char ***)(((char *) opt
) + arg
->offset
) = NULL
;
88 *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
) = 0;
91 void isl_args_set_defaults(struct isl_args
*args
, void *opt
)
95 for (i
= 0; args
->args
[i
].type
!= isl_arg_end
; ++i
) {
96 switch (args
->args
[i
].type
) {
98 set_default_choice(&args
->args
[i
], opt
);
101 set_default_flags(&args
->args
[i
], opt
);
104 set_default_bool(&args
->args
[i
], opt
);
107 set_default_child(&args
->args
[i
], opt
);
110 set_default_user(&args
->args
[i
], opt
);
113 set_default_int(&args
->args
[i
], opt
);
116 set_default_long(&args
->args
[i
], opt
);
119 set_default_ulong(&args
->args
[i
], opt
);
123 set_default_str(&args
->args
[i
], opt
);
125 case isl_arg_str_list
:
126 set_default_str_list(&args
->args
[i
], opt
);
130 case isl_arg_version
:
137 static void free_args(struct isl_arg
*arg
, void *opt
);
139 static void free_child(struct isl_arg
*arg
, void *opt
)
141 if (arg
->offset
== ISL_ARG_OFFSET_NONE
)
142 free_args(arg
->u
.child
.child
->args
, opt
);
144 isl_args_free(arg
->u
.child
.child
,
145 *(void **)(((char *)opt
) + arg
->offset
));
148 static void free_str_list(struct isl_arg
*arg
, void *opt
)
151 int n
= *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
);
152 char **list
= *(char ***)(((char *) opt
) + arg
->offset
);
154 for (i
= 0; i
< n
; ++i
)
159 static void free_user(struct isl_arg
*arg
, void *opt
)
161 if (arg
->u
.user
.clear
)
162 arg
->u
.user
.clear(((char *)opt
) + arg
->offset
);
165 static void free_args(struct isl_arg
*arg
, void *opt
)
169 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
170 switch (arg
[i
].type
) {
172 free_child(&arg
[i
], opt
);
176 free(*(char **)(((char *)opt
) + arg
[i
].offset
));
178 case isl_arg_str_list
:
179 free_str_list(&arg
[i
], opt
);
182 free_user(&arg
[i
], opt
);
191 case isl_arg_version
:
199 void isl_args_free(struct isl_args
*args
, void *opt
)
204 free_args(args
->args
, opt
);
209 /* Data structure for collecting the prefixes of ancestor nodes.
211 * n is the number of prefixes.
212 * prefix[i] for i < n is a prefix of an ancestor.
213 * len[i] for i < n is the length of prefix[i].
215 struct isl_prefixes
{
217 const char *prefix
[10];
221 /* Add "prefix" to the list of prefixes and return the updated
222 * number of prefixes.
224 static int add_prefix(struct isl_prefixes
*prefixes
, const char *prefix
)
231 if (prefixes
->n
>= 10) {
232 fprintf(stderr
, "too many prefixes\n");
235 prefixes
->len
[prefixes
->n
] = strlen(prefix
);
236 prefixes
->prefix
[prefixes
->n
] = prefix
;
242 /* Drop all prefixes starting at "first".
244 static void drop_prefix(struct isl_prefixes
*prefixes
, int first
)
249 /* Print the prefixes in "prefixes".
251 static int print_prefixes(struct isl_prefixes
*prefixes
)
259 for (i
= 0; i
< prefixes
->n
; ++i
) {
260 printf("%s-", prefixes
->prefix
[i
]);
261 len
+= strlen(prefixes
->prefix
[i
]) + 1;
267 /* Check if "name" starts with one or more of the prefixes in "prefixes",
268 * starting at *first. If so, advance the pointer beyond the prefixes
269 * and return the updated pointer. Additionally, update *first to
270 * the index after the last prefix found.
272 static const char *skip_prefixes(const char *name
,
273 struct isl_prefixes
*prefixes
, int *first
)
277 for (i
= first
? *first
: 0; i
< prefixes
->n
; ++i
) {
278 size_t len
= prefixes
->len
[i
];
279 const char *prefix
= prefixes
->prefix
[i
];
280 if (strncmp(name
, prefix
, len
) == 0 && name
[len
] == '-') {
290 static int print_arg_help(struct isl_arg
*decl
, struct isl_prefixes
*prefixes
,
295 if (!decl
->long_name
) {
296 printf(" -%c", decl
->short_name
);
300 if (decl
->short_name
) {
301 printf(" -%c, --", decl
->short_name
);
303 } else if (decl
->flags
& ISL_ARG_SINGLE_DASH
) {
315 len
+= print_prefixes(prefixes
);
316 printf("%s", decl
->long_name
);
317 len
+= strlen(decl
->long_name
);
319 while ((++decl
)->type
== isl_arg_alias
) {
326 printf("%s", decl
->long_name
);
327 len
+= strlen(decl
->long_name
);
333 const void *isl_memrchr(const void *s
, int c
, size_t n
)
342 static int wrap_msg(const char *s
, int indent
, int pos
)
345 int wrap_len
= 75 - indent
;
347 if (pos
+ 1 >= indent
)
348 printf("\n%*s", indent
, "");
350 printf("%*s", indent
- pos
, "");
353 while (len
> wrap_len
) {
354 const char *space
= isl_memrchr(s
, ' ', wrap_len
);
358 space
= strchr(s
+ wrap_len
, ' ');
362 printf("%.*s", l
, s
);
365 printf("\n%*s", indent
, "");
372 static int print_help_msg(struct isl_arg
*decl
, int pos
)
377 return wrap_msg(decl
->help_msg
, 30, pos
);
380 static void print_default(struct isl_arg
*decl
, const char *def
, int pos
)
382 const char *default_prefix
= "[default: ";
383 const char *default_suffix
= "]";
386 len
= strlen(default_prefix
) + strlen(def
) + strlen(default_suffix
);
388 if (!decl
->help_msg
) {
390 printf("\n%30s", "");
392 printf("%*s", 30 - pos
, "");
395 printf("\n%30s", "");
399 printf("%s%s%s", default_prefix
, def
, default_suffix
);
402 static void print_default_choice(struct isl_arg
*decl
, void *opt
, int pos
)
405 const char *s
= "none";
408 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
409 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
)
410 if (decl
->u
.choice
.choice
[i
].value
== *p
) {
411 s
= decl
->u
.choice
.choice
[i
].name
;
415 print_default(decl
, s
, pos
);
418 static void print_choice_help(struct isl_arg
*decl
,
419 struct isl_prefixes
*prefixes
, void *opt
)
424 pos
= print_arg_help(decl
, prefixes
, 0);
428 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
433 printf("%s", decl
->u
.choice
.choice
[i
].name
);
434 pos
+= strlen(decl
->u
.choice
.choice
[i
].name
);
437 pos
= print_help_msg(decl
, pos
);
438 print_default_choice(decl
, opt
, pos
);
443 static void print_default_flags(struct isl_arg
*decl
, void *opt
, int pos
)
446 const char *default_prefix
= "[default: ";
447 const char *default_suffix
= "]";
448 int len
= strlen(default_prefix
) + strlen(default_suffix
);
451 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
452 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
453 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
454 decl
->u
.flags
.flags
[i
].value
)
455 len
+= strlen(decl
->u
.flags
.flags
[i
].name
);
457 if (!decl
->help_msg
) {
459 printf("\n%30s", "");
461 printf("%*s", 30 - pos
, "");
464 printf("\n%30s", "");
468 printf("%s", default_prefix
);
470 for (first
= 1, i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
471 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
472 decl
->u
.flags
.flags
[i
].value
) {
475 printf("%s", decl
->u
.flags
.flags
[i
].name
);
479 printf("%s", default_suffix
);
482 static void print_flags_help(struct isl_arg
*decl
,
483 struct isl_prefixes
*prefixes
, void *opt
)
488 pos
= print_arg_help(decl
, prefixes
, 0);
492 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
498 decl
->u
.flags
.flags
[j
].mask
== decl
->u
.flags
.flags
[i
].mask
;
504 printf("%s", decl
->u
.flags
.flags
[j
].name
);
505 pos
+= strlen(decl
->u
.flags
.flags
[j
].name
);
510 pos
= print_help_msg(decl
, pos
);
511 print_default_flags(decl
, opt
, pos
);
516 static void print_bool_help(struct isl_arg
*decl
,
517 struct isl_prefixes
*prefixes
, void *opt
)
520 unsigned *p
= opt
? (unsigned *)(((char *) opt
) + decl
->offset
) : NULL
;
521 int no
= p
? *p
== 1 : 0;
522 pos
= print_arg_help(decl
, prefixes
, no
);
523 pos
= print_help_msg(decl
, pos
);
524 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
525 print_default(decl
, no
? "yes" : "no", pos
);
529 static int print_argument_name(struct isl_arg
*decl
, const char *name
, int pos
)
531 printf("%c<%s>", decl
->long_name
? '=' : ' ', name
);
532 return pos
+ 3 + strlen(name
);
535 static void print_int_help(struct isl_arg
*decl
,
536 struct isl_prefixes
*prefixes
, void *opt
)
540 int *p
= (int *)(((char *) opt
) + decl
->offset
);
541 pos
= print_arg_help(decl
, prefixes
, 0);
542 pos
= print_argument_name(decl
, decl
->argument_name
, pos
);
543 pos
= print_help_msg(decl
, pos
);
544 snprintf(val
, sizeof(val
), "%d", *p
);
545 print_default(decl
, val
, pos
);
549 static void print_long_help(struct isl_arg
*decl
,
550 struct isl_prefixes
*prefixes
, void *opt
)
553 long *p
= (long *)(((char *) opt
) + decl
->offset
);
554 pos
= print_arg_help(decl
, prefixes
, 0);
555 if (*p
!= decl
->u
.l
.default_selected
) {
561 if (*p
!= decl
->u
.l
.default_selected
) {
565 print_help_msg(decl
, pos
);
569 static void print_ulong_help(struct isl_arg
*decl
,
570 struct isl_prefixes
*prefixes
)
573 pos
= print_arg_help(decl
, prefixes
, 0);
576 print_help_msg(decl
, pos
);
580 static void print_str_help(struct isl_arg
*decl
,
581 struct isl_prefixes
*prefixes
, void *opt
)
584 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
585 const char **p
= (const char **)(((char *) opt
) + decl
->offset
);
586 pos
= print_arg_help(decl
, prefixes
, 0);
587 pos
= print_argument_name(decl
, a
, pos
);
588 pos
= print_help_msg(decl
, pos
);
590 print_default(decl
, *p
, pos
);
594 static void print_str_list_help(struct isl_arg
*decl
,
595 struct isl_prefixes
*prefixes
)
598 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
599 pos
= print_arg_help(decl
, prefixes
, 0);
600 pos
= print_argument_name(decl
, a
, pos
);
601 pos
= print_help_msg(decl
, pos
);
605 static void print_help(struct isl_arg
*arg
,
606 struct isl_prefixes
*prefixes
, void *opt
)
611 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
612 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
614 switch (arg
[i
].type
) {
616 print_flags_help(&arg
[i
], prefixes
, opt
);
620 print_choice_help(&arg
[i
], prefixes
, opt
);
624 print_bool_help(&arg
[i
], prefixes
, opt
);
628 print_int_help(&arg
[i
], prefixes
, opt
);
632 print_long_help(&arg
[i
], prefixes
, opt
);
636 print_ulong_help(&arg
[i
], prefixes
);
640 print_str_help(&arg
[i
], prefixes
, opt
);
643 case isl_arg_str_list
:
644 print_str_list_help(&arg
[i
], prefixes
);
648 case isl_arg_version
:
658 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
662 if (arg
[i
].type
!= isl_arg_child
)
664 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
670 printf(" %s\n", arg
[i
].help_msg
);
671 if (arg
[i
].offset
== ISL_ARG_OFFSET_NONE
)
674 child
= *(void **)(((char *) opt
) + arg
[i
].offset
);
675 first
= add_prefix(prefixes
, arg
[i
].long_name
);
676 print_help(arg
[i
].u
.child
.child
->args
, prefixes
, child
);
677 drop_prefix(prefixes
, first
);
682 static const char *prog_name(const char *prog
)
686 slash
= strrchr(prog
, '/');
689 if (strncmp(prog
, "lt-", 3) == 0)
695 static int any_version(struct isl_arg
*decl
)
699 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
700 switch (decl
[i
].type
) {
701 case isl_arg_version
:
704 if (any_version(decl
[i
].u
.child
.child
->args
))
715 static void print_help_and_exit(struct isl_arg
*arg
, const char *prog
,
719 struct isl_prefixes prefixes
= { 0 };
721 printf("Usage: %s [OPTION...]", prog_name(prog
));
723 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
724 if (arg
[i
].type
== isl_arg_arg
)
725 printf(" %s", arg
[i
].argument_name
);
729 print_help(arg
, &prefixes
, opt
);
731 if (any_version(arg
))
732 printf(" -V, --version\n");
733 print_bool_help(help_arg
, NULL
, NULL
);
735 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
736 if (arg
[i
].type
!= isl_arg_footer
)
738 wrap_msg(arg
[i
].help_msg
, 0, 0);
745 static int match_long_name(struct isl_arg
*decl
,
746 const char *start
, const char *end
)
749 if (end
- start
== strlen(decl
->long_name
) &&
750 !strncmp(start
, decl
->long_name
, end
- start
))
752 } while ((++decl
)->type
== isl_arg_alias
);
757 static const char *skip_dash_dash(struct isl_arg
*decl
, const char *arg
)
759 if (!strncmp(arg
, "--", 2))
761 if ((decl
->flags
& ISL_ARG_SINGLE_DASH
) && arg
[0] == '-')
766 static const char *skip_name(struct isl_arg
*decl
, const char *arg
,
767 struct isl_prefixes
*prefixes
, int need_argument
, int *has_argument
)
773 if (arg
[0] == '-' && arg
[1] && arg
[1] == decl
->short_name
) {
774 if (need_argument
&& !arg
[2])
777 *has_argument
= arg
[2] != '\0';
780 if (!decl
->long_name
)
783 name
= skip_dash_dash(decl
, arg
);
787 equal
= strchr(name
, '=');
788 if (need_argument
&& !equal
)
792 *has_argument
= !!equal
;
793 end
= equal
? equal
: name
+ strlen(name
);
795 name
= skip_prefixes(name
, prefixes
, NULL
);
797 if (!match_long_name(decl
, name
, end
))
800 return equal
? equal
+ 1 : end
;
803 static int parse_choice_option(struct isl_arg
*decl
, char **arg
,
804 struct isl_prefixes
*prefixes
, void *opt
)
810 choice
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
814 if (!has_argument
&& (!arg
[1] || arg
[1][0] == '-')) {
815 unsigned u
= decl
->u
.choice
.default_selected
;
816 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
817 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
818 if (decl
->u
.choice
.set
)
819 decl
->u
.choice
.set(opt
, u
);
827 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
830 if (strcmp(choice
, decl
->u
.choice
.choice
[i
].name
))
833 u
= decl
->u
.choice
.choice
[i
].value
;
834 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
835 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
836 if (decl
->u
.choice
.set
)
837 decl
->u
.choice
.set(opt
, u
);
839 return has_argument
? 1 : 2;
845 static int set_flag(struct isl_arg
*decl
, unsigned *val
, const char *flag
,
850 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
851 if (strncmp(flag
, decl
->u
.flags
.flags
[i
].name
, len
))
854 *val
&= ~decl
->u
.flags
.flags
[i
].mask
;
855 *val
|= decl
->u
.flags
.flags
[i
].value
;
863 static int parse_flags_option(struct isl_arg
*decl
, char **arg
,
864 struct isl_prefixes
*prefixes
, void *opt
)
871 flags
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
875 if (!has_argument
&& !arg
[1])
883 while ((comma
= strchr(flags
, ',')) != NULL
) {
884 if (!set_flag(decl
, &val
, flags
, comma
- flags
))
888 if (!set_flag(decl
, &val
, flags
, strlen(flags
)))
891 *(unsigned *)(((char *)opt
) + decl
->offset
) = val
;
893 return has_argument
? 1 : 2;
896 static int parse_bool_option(struct isl_arg
*decl
, char **arg
,
897 struct isl_prefixes
*prefixes
, void *opt
)
900 unsigned *p
= (unsigned *)(((char *)opt
) + decl
->offset
);
903 if (skip_name(decl
, arg
[0], prefixes
, 0, NULL
)) {
904 if ((decl
->flags
& ISL_ARG_BOOL_ARG
) && arg
[1]) {
906 int val
= strtol(arg
[1], &endptr
, 0);
907 if (*endptr
== '\0' && (val
== 0 || val
== 1)) {
908 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
911 decl
->u
.b
.set(opt
, val
);
915 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
918 decl
->u
.b
.set(opt
, 1);
923 if (!decl
->long_name
)
926 name
= skip_dash_dash(decl
, arg
[0]);
931 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
933 if (strncmp(name
, "no-", 3))
937 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
939 if (match_long_name(decl
, name
, name
+ strlen(name
))) {
940 if (decl
->offset
!= ISL_ARG_OFFSET_NONE
)
943 decl
->u
.b
.set(opt
, 0);
951 static int parse_str_option(struct isl_arg
*decl
, char **arg
,
952 struct isl_prefixes
*prefixes
, void *opt
)
956 char **p
= (char **)(((char *)opt
) + decl
->offset
);
958 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
977 static int isl_arg_str_list_append(struct isl_arg
*decl
, void *opt
,
980 int *n
= (int *)(((char *) opt
) + decl
->u
.str_list
.offset_n
);
981 char **list
= *(char ***)(((char *) opt
) + decl
->offset
);
983 list
= realloc(list
, (*n
+ 1) * sizeof(char *));
986 *(char ***)(((char *) opt
) + decl
->offset
) = list
;
987 list
[*n
] = strdup(s
);
992 static int parse_str_list_option(struct isl_arg
*decl
, char **arg
,
993 struct isl_prefixes
*prefixes
, void *opt
)
998 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1003 isl_arg_str_list_append(decl
, opt
, s
);
1008 isl_arg_str_list_append(decl
, opt
, arg
[1]);
1015 static int parse_int_option(struct isl_arg
*decl
, char **arg
,
1016 struct isl_prefixes
*prefixes
, void *opt
)
1021 int *p
= (int *)(((char *)opt
) + decl
->offset
);
1023 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1033 int i
= strtol(arg
[1], &endptr
, 0);
1034 if (*endptr
== '\0') {
1043 static int parse_long_option(struct isl_arg
*decl
, char **arg
,
1044 struct isl_prefixes
*prefixes
, void *opt
)
1049 long *p
= (long *)(((char *)opt
) + decl
->offset
);
1051 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1056 long l
= strtol(val
, NULL
, 0);
1059 decl
->u
.l
.set(opt
, l
);
1064 long l
= strtol(arg
[1], &endptr
, 0);
1065 if (*endptr
== '\0') {
1068 decl
->u
.l
.set(opt
, l
);
1073 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
1074 *p
= decl
->u
.l
.default_selected
;
1076 decl
->u
.l
.set(opt
, decl
->u
.l
.default_selected
);
1083 static int parse_ulong_option(struct isl_arg
*decl
, char **arg
,
1084 struct isl_prefixes
*prefixes
, void *opt
)
1089 unsigned long *p
= (unsigned long *)(((char *)opt
) + decl
->offset
);
1091 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1096 *p
= strtoul(val
, NULL
, 0);
1101 unsigned long ul
= strtoul(arg
[1], &endptr
, 0);
1102 if (*endptr
== '\0') {
1111 static int parse_option(struct isl_arg
*decl
, char **arg
,
1112 struct isl_prefixes
*prefixes
, void *opt
);
1114 static int parse_child_option(struct isl_arg
*decl
, char **arg
,
1115 struct isl_prefixes
*prefixes
, void *opt
)
1120 if (decl
->offset
== ISL_ARG_OFFSET_NONE
)
1123 child
= *(void **)(((char *)opt
) + decl
->offset
);
1125 first
= add_prefix(prefixes
, decl
->long_name
);
1126 parsed
= parse_option(decl
->u
.child
.child
->args
, arg
, prefixes
, child
);
1127 drop_prefix(prefixes
, first
);
1132 static int parse_option(struct isl_arg
*decl
, char **arg
,
1133 struct isl_prefixes
*prefixes
, void *opt
)
1137 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1139 switch (decl
[i
].type
) {
1140 case isl_arg_choice
:
1141 parsed
= parse_choice_option(&decl
[i
], arg
,
1145 parsed
= parse_flags_option(&decl
[i
], arg
,
1149 parsed
= parse_int_option(&decl
[i
], arg
, prefixes
, opt
);
1152 parsed
= parse_long_option(&decl
[i
], arg
,
1156 parsed
= parse_ulong_option(&decl
[i
], arg
,
1160 parsed
= parse_bool_option(&decl
[i
], arg
,
1164 parsed
= parse_str_option(&decl
[i
], arg
, prefixes
, opt
);
1166 case isl_arg_str_list
:
1167 parsed
= parse_str_list_option(&decl
[i
], arg
, prefixes
,
1171 parsed
= parse_child_option(&decl
[i
], arg
,
1176 case isl_arg_footer
:
1178 case isl_arg_version
:
1189 static void print_version(struct isl_arg
*decl
)
1193 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1194 switch (decl
[i
].type
) {
1195 case isl_arg_version
:
1196 decl
[i
].u
.version
.print_version();
1199 print_version(decl
[i
].u
.child
.child
->args
);
1207 static void print_version_and_exit(struct isl_arg
*decl
)
1209 print_version(decl
);
1214 static int drop_argument(int argc
, char **argv
, int drop
, int n
)
1216 for (; drop
+ n
< argc
; ++drop
)
1217 argv
[drop
] = argv
[drop
+ n
];
1222 static int n_arg(struct isl_arg
*arg
)
1227 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
1228 if (arg
[i
].type
== isl_arg_arg
)
1234 static int next_arg(struct isl_arg
*arg
, int a
)
1236 for (++a
; arg
[a
].type
!= isl_arg_end
; ++a
)
1237 if (arg
[a
].type
== isl_arg_arg
)
1243 /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
1244 * equal to "--help" or "-h" and if so call print_help_and_exit.
1246 static void check_help(struct isl_args
*args
, char *arg
, char *prog
, void *opt
,
1249 if (ISL_FL_ISSET(flags
, ISL_ARG_SKIP_HELP
))
1252 if (strcmp(arg
, "--help") == 0 || strcmp(arg
, "-h") == 0)
1253 print_help_and_exit(args
->args
, prog
, opt
);
1256 int isl_args_parse(struct isl_args
*args
, int argc
, char **argv
, void *opt
,
1263 struct isl_prefixes prefixes
= { 0 };
1265 n
= n_arg(args
->args
);
1267 for (i
= 1; i
< argc
; ++i
) {
1268 if ((strcmp(argv
[i
], "--version") == 0 ||
1269 strcmp(argv
[i
], "-V") == 0) && any_version(args
->args
))
1270 print_version_and_exit(args
->args
);
1273 while (argc
> 1 + skip
) {
1275 if (argv
[1 + skip
][0] != '-') {
1276 a
= next_arg(args
->args
, a
);
1279 p
= (char **)(((char *)opt
)+args
->args
[a
].offset
);
1281 *p
= strdup(argv
[1 + skip
]);
1282 argc
= drop_argument(argc
, argv
, 1 + skip
, 1);
1284 } else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1285 fprintf(stderr
, "%s: extra argument: %s\n",
1286 prog_name(argv
[0]), argv
[1 + skip
]);
1292 check_help(args
, argv
[1 + skip
], argv
[0], opt
, flags
);
1293 parsed
= parse_option(args
->args
, &argv
[1 + skip
],
1296 argc
= drop_argument(argc
, argv
, 1 + skip
, parsed
);
1297 else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1298 fprintf(stderr
, "%s: unrecognized option: %s\n",
1299 prog_name(argv
[0]), argv
[1 + skip
]);
1306 fprintf(stderr
, "%s: expecting %d more argument(s)\n",
1307 prog_name(argv
[0]), n
);