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
17 static struct isl_arg help_arg
[] = {
18 ISL_ARG_PHANTOM_BOOL('h', "help", NULL
, "print this help, then exit")
21 static void set_default_choice(struct isl_arg
*arg
, void *opt
)
23 if (arg
->offset
== (size_t) -1)
25 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.choice
.default_value
;
28 static void set_default_flags(struct isl_arg
*arg
, void *opt
)
30 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.flags
.default_value
;
33 static void set_default_bool(struct isl_arg
*arg
, void *opt
)
35 if (arg
->offset
== (size_t) -1)
37 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.b
.default_value
;
40 static void set_default_child(struct isl_arg
*arg
, void *opt
)
44 if (arg
->offset
== (size_t) -1)
47 child
= calloc(1, arg
->u
.child
.child
->options_size
);
48 *(void **)(((char *)opt
) + arg
->offset
) = child
;
52 isl_args_set_defaults(arg
->u
.child
.child
, child
);
55 static void set_default_user(struct isl_arg
*arg
, void *opt
)
57 arg
->u
.user
.init(((char *)opt
) + arg
->offset
);
60 static void set_default_int(struct isl_arg
*arg
, void *opt
)
62 *(int *)(((char *)opt
) + arg
->offset
) = arg
->u
.i
.default_value
;
65 static void set_default_long(struct isl_arg
*arg
, void *opt
)
67 *(long *)(((char *)opt
) + arg
->offset
) = arg
->u
.l
.default_value
;
70 static void set_default_ulong(struct isl_arg
*arg
, void *opt
)
72 *(unsigned long *)(((char *)opt
) + arg
->offset
) = arg
->u
.ul
.default_value
;
75 static void set_default_str(struct isl_arg
*arg
, void *opt
)
77 const char *str
= NULL
;
78 if (arg
->u
.str
.default_value
)
79 str
= strdup(arg
->u
.str
.default_value
);
80 *(const char **)(((char *)opt
) + arg
->offset
) = str
;
83 static void set_default_str_list(struct isl_arg
*arg
, void *opt
)
85 *(const char ***)(((char *) opt
) + arg
->offset
) = NULL
;
86 *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
) = 0;
89 void isl_args_set_defaults(struct isl_args
*args
, void *opt
)
93 for (i
= 0; args
->args
[i
].type
!= isl_arg_end
; ++i
) {
94 switch (args
->args
[i
].type
) {
96 set_default_choice(&args
->args
[i
], opt
);
99 set_default_flags(&args
->args
[i
], opt
);
102 set_default_bool(&args
->args
[i
], opt
);
105 set_default_child(&args
->args
[i
], opt
);
108 set_default_user(&args
->args
[i
], opt
);
111 set_default_int(&args
->args
[i
], opt
);
114 set_default_long(&args
->args
[i
], opt
);
117 set_default_ulong(&args
->args
[i
], opt
);
121 set_default_str(&args
->args
[i
], opt
);
123 case isl_arg_str_list
:
124 set_default_str_list(&args
->args
[i
], opt
);
128 case isl_arg_version
:
135 static void free_args(struct isl_arg
*arg
, void *opt
);
137 static void free_child(struct isl_arg
*arg
, void *opt
)
139 if (arg
->offset
== (size_t) -1)
140 free_args(arg
->u
.child
.child
->args
, opt
);
142 isl_args_free(arg
->u
.child
.child
,
143 *(void **)(((char *)opt
) + arg
->offset
));
146 static void free_str_list(struct isl_arg
*arg
, void *opt
)
149 int n
= *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
);
150 char **list
= *(char ***)(((char *) opt
) + arg
->offset
);
152 for (i
= 0; i
< n
; ++i
)
157 static void free_user(struct isl_arg
*arg
, void *opt
)
159 if (arg
->u
.user
.clear
)
160 arg
->u
.user
.clear(((char *)opt
) + arg
->offset
);
163 static void free_args(struct isl_arg
*arg
, void *opt
)
167 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
168 switch (arg
[i
].type
) {
170 free_child(&arg
[i
], opt
);
174 free(*(char **)(((char *)opt
) + arg
[i
].offset
));
176 case isl_arg_str_list
:
177 free_str_list(&arg
[i
], opt
);
180 free_user(&arg
[i
], opt
);
189 case isl_arg_version
:
197 void isl_args_free(struct isl_args
*args
, void *opt
)
202 free_args(args
->args
, opt
);
207 /* Data structure for collecting the prefixes of ancestor nodes.
209 * n is the number of prefixes.
210 * prefix[i] for i < n is a prefix of an ancestor.
211 * len[i] for i < n is the length of prefix[i].
213 struct isl_prefixes
{
215 const char *prefix
[10];
219 /* Add "prefix" to the list of prefixes and return the updated
220 * number of prefixes.
222 static int add_prefix(struct isl_prefixes
*prefixes
, const char *prefix
)
229 if (prefixes
->n
>= 10) {
230 fprintf(stderr
, "too many prefixes\n");
233 prefixes
->len
[prefixes
->n
] = strlen(prefix
);
234 prefixes
->prefix
[prefixes
->n
] = prefix
;
240 /* Drop all prefixes starting at "first".
242 static void drop_prefix(struct isl_prefixes
*prefixes
, int first
)
247 /* Print the prefixes in "prefixes".
249 static int print_prefixes(struct isl_prefixes
*prefixes
)
257 for (i
= 0; i
< prefixes
->n
; ++i
) {
258 printf("%s-", prefixes
->prefix
[i
]);
259 len
+= strlen(prefixes
->prefix
[i
]) + 1;
265 /* Check if "name" starts with one or more of the prefixes in "prefixes",
266 * starting at *first. If so, advance the pointer beyond the prefixes
267 * and return the updated pointer. Additionally, update *first to
268 * the index after the last prefix found.
270 static const char *skip_prefixes(const char *name
,
271 struct isl_prefixes
*prefixes
, int *first
)
275 for (i
= first
? *first
: 0; i
< prefixes
->n
; ++i
) {
276 size_t len
= prefixes
->len
[i
];
277 const char *prefix
= prefixes
->prefix
[i
];
278 if (strncmp(name
, prefix
, len
) == 0 && name
[len
] == '-') {
288 static int print_arg_help(struct isl_arg
*decl
, struct isl_prefixes
*prefixes
,
293 if (!decl
->long_name
) {
294 printf(" -%c", decl
->short_name
);
298 if (decl
->short_name
) {
299 printf(" -%c, --", decl
->short_name
);
301 } else if (decl
->flags
& ISL_ARG_SINGLE_DASH
) {
313 len
+= print_prefixes(prefixes
);
314 printf("%s", decl
->long_name
);
315 len
+= strlen(decl
->long_name
);
317 while ((++decl
)->type
== isl_arg_alias
) {
324 printf("%s", decl
->long_name
);
325 len
+= strlen(decl
->long_name
);
331 const void *isl_memrchr(const void *s
, int c
, size_t n
)
340 static int wrap_msg(const char *s
, int indent
, int pos
)
343 int wrap_len
= 75 - indent
;
345 if (pos
+ 1 >= indent
)
346 printf("\n%*s", indent
, "");
348 printf("%*s", indent
- pos
, "");
351 while (len
> wrap_len
) {
352 const char *space
= isl_memrchr(s
, ' ', wrap_len
);
356 space
= strchr(s
+ wrap_len
, ' ');
360 printf("%.*s", l
, s
);
363 printf("\n%*s", indent
, "");
370 static int print_help_msg(struct isl_arg
*decl
, int pos
)
375 return wrap_msg(decl
->help_msg
, 30, pos
);
378 static void print_default(struct isl_arg
*decl
, const char *def
, int pos
)
380 const char *default_prefix
= "[default: ";
381 const char *default_suffix
= "]";
384 len
= strlen(default_prefix
) + strlen(def
) + strlen(default_suffix
);
386 if (!decl
->help_msg
) {
388 printf("\n%30s", "");
390 printf("%*s", 30 - pos
, "");
393 printf("\n%30s", "");
397 printf("%s%s%s", default_prefix
, def
, default_suffix
);
400 static void print_default_choice(struct isl_arg
*decl
, void *opt
, int pos
)
403 const char *s
= "none";
406 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
407 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
)
408 if (decl
->u
.choice
.choice
[i
].value
== *p
) {
409 s
= decl
->u
.choice
.choice
[i
].name
;
413 print_default(decl
, s
, pos
);
416 static void print_choice_help(struct isl_arg
*decl
,
417 struct isl_prefixes
*prefixes
, void *opt
)
422 pos
= print_arg_help(decl
, prefixes
, 0);
426 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
431 printf("%s", decl
->u
.choice
.choice
[i
].name
);
432 pos
+= strlen(decl
->u
.choice
.choice
[i
].name
);
435 pos
= print_help_msg(decl
, pos
);
436 print_default_choice(decl
, opt
, pos
);
441 static void print_default_flags(struct isl_arg
*decl
, void *opt
, int pos
)
444 const char *default_prefix
= "[default: ";
445 const char *default_suffix
= "]";
446 int len
= strlen(default_prefix
) + strlen(default_suffix
);
449 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
450 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
451 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
452 decl
->u
.flags
.flags
[i
].value
)
453 len
+= strlen(decl
->u
.flags
.flags
[i
].name
);
455 if (!decl
->help_msg
) {
457 printf("\n%30s", "");
459 printf("%*s", 30 - pos
, "");
462 printf("\n%30s", "");
466 printf("%s", default_prefix
);
468 for (first
= 1, i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
469 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
470 decl
->u
.flags
.flags
[i
].value
) {
473 printf("%s", decl
->u
.flags
.flags
[i
].name
);
477 printf("%s", default_suffix
);
480 static void print_flags_help(struct isl_arg
*decl
,
481 struct isl_prefixes
*prefixes
, void *opt
)
486 pos
= print_arg_help(decl
, prefixes
, 0);
490 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
496 decl
->u
.flags
.flags
[j
].mask
== decl
->u
.flags
.flags
[i
].mask
;
502 printf("%s", decl
->u
.flags
.flags
[j
].name
);
503 pos
+= strlen(decl
->u
.flags
.flags
[j
].name
);
508 pos
= print_help_msg(decl
, pos
);
509 print_default_flags(decl
, opt
, pos
);
514 static void print_bool_help(struct isl_arg
*decl
,
515 struct isl_prefixes
*prefixes
, void *opt
)
518 unsigned *p
= opt
? (unsigned *)(((char *) opt
) + decl
->offset
) : NULL
;
519 int no
= p
? *p
== 1 : 0;
520 pos
= print_arg_help(decl
, prefixes
, no
);
521 pos
= print_help_msg(decl
, pos
);
522 if (decl
->offset
!= (size_t) -1)
523 print_default(decl
, no
? "yes" : "no", pos
);
527 static int print_argument_name(struct isl_arg
*decl
, const char *name
, int pos
)
529 printf("%c<%s>", decl
->long_name
? '=' : ' ', name
);
530 return pos
+ 3 + strlen(name
);
533 static void print_int_help(struct isl_arg
*decl
,
534 struct isl_prefixes
*prefixes
, void *opt
)
538 int *p
= (int *)(((char *) opt
) + decl
->offset
);
539 pos
= print_arg_help(decl
, prefixes
, 0);
540 pos
= print_argument_name(decl
, decl
->argument_name
, pos
);
541 pos
= print_help_msg(decl
, pos
);
542 snprintf(val
, sizeof(val
), "%d", *p
);
543 print_default(decl
, val
, pos
);
547 static void print_long_help(struct isl_arg
*decl
,
548 struct isl_prefixes
*prefixes
, void *opt
)
551 long *p
= (long *)(((char *) opt
) + decl
->offset
);
552 pos
= print_arg_help(decl
, prefixes
, 0);
553 if (*p
!= decl
->u
.l
.default_selected
) {
559 if (*p
!= decl
->u
.l
.default_selected
) {
563 print_help_msg(decl
, pos
);
567 static void print_ulong_help(struct isl_arg
*decl
,
568 struct isl_prefixes
*prefixes
)
571 pos
= print_arg_help(decl
, prefixes
, 0);
574 print_help_msg(decl
, pos
);
578 static void print_str_help(struct isl_arg
*decl
,
579 struct isl_prefixes
*prefixes
, void *opt
)
582 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
583 const char **p
= (const char **)(((char *) opt
) + decl
->offset
);
584 pos
= print_arg_help(decl
, prefixes
, 0);
585 pos
= print_argument_name(decl
, a
, pos
);
586 pos
= print_help_msg(decl
, pos
);
588 print_default(decl
, *p
, pos
);
592 static void print_str_list_help(struct isl_arg
*decl
,
593 struct isl_prefixes
*prefixes
)
596 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
597 pos
= print_arg_help(decl
, prefixes
, 0);
598 pos
= print_argument_name(decl
, a
, pos
);
599 pos
= print_help_msg(decl
, pos
);
603 static void print_help(struct isl_arg
*arg
,
604 struct isl_prefixes
*prefixes
, void *opt
)
609 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
610 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
612 switch (arg
[i
].type
) {
614 print_flags_help(&arg
[i
], prefixes
, opt
);
618 print_choice_help(&arg
[i
], prefixes
, opt
);
622 print_bool_help(&arg
[i
], prefixes
, opt
);
626 print_int_help(&arg
[i
], prefixes
, opt
);
630 print_long_help(&arg
[i
], prefixes
, opt
);
634 print_ulong_help(&arg
[i
], prefixes
);
638 print_str_help(&arg
[i
], prefixes
, opt
);
641 case isl_arg_str_list
:
642 print_str_list_help(&arg
[i
], prefixes
);
646 case isl_arg_version
:
656 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
660 if (arg
[i
].type
!= isl_arg_child
)
662 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
668 printf(" %s\n", arg
[i
].help_msg
);
669 if (arg
[i
].offset
== (size_t) -1)
672 child
= *(void **)(((char *) opt
) + arg
[i
].offset
);
673 first
= add_prefix(prefixes
, arg
[i
].long_name
);
674 print_help(arg
[i
].u
.child
.child
->args
, prefixes
, child
);
675 drop_prefix(prefixes
, first
);
680 static const char *prog_name(const char *prog
)
684 slash
= strrchr(prog
, '/');
687 if (strncmp(prog
, "lt-", 3) == 0)
693 static int any_version(struct isl_arg
*decl
)
697 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
698 switch (decl
[i
].type
) {
699 case isl_arg_version
:
702 if (any_version(decl
[i
].u
.child
.child
->args
))
713 static void print_help_and_exit(struct isl_arg
*arg
, const char *prog
,
717 struct isl_prefixes prefixes
= { 0 };
719 printf("Usage: %s [OPTION...]", prog_name(prog
));
721 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
722 if (arg
[i
].type
== isl_arg_arg
)
723 printf(" %s", arg
[i
].argument_name
);
727 print_help(arg
, &prefixes
, opt
);
729 if (any_version(arg
))
730 printf(" -V, --version\n");
731 print_bool_help(help_arg
, NULL
, NULL
);
733 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
734 if (arg
[i
].type
!= isl_arg_footer
)
736 wrap_msg(arg
[i
].help_msg
, 0, 0);
743 static int match_long_name(struct isl_arg
*decl
,
744 const char *start
, const char *end
)
747 if (end
- start
== strlen(decl
->long_name
) &&
748 !strncmp(start
, decl
->long_name
, end
- start
))
750 } while ((++decl
)->type
== isl_arg_alias
);
755 static const char *skip_dash_dash(struct isl_arg
*decl
, const char *arg
)
757 if (!strncmp(arg
, "--", 2))
759 if ((decl
->flags
& ISL_ARG_SINGLE_DASH
) && arg
[0] == '-')
764 static const char *skip_name(struct isl_arg
*decl
, const char *arg
,
765 struct isl_prefixes
*prefixes
, int need_argument
, int *has_argument
)
771 if (arg
[0] == '-' && arg
[1] && arg
[1] == decl
->short_name
) {
772 if (need_argument
&& !arg
[2])
775 *has_argument
= arg
[2] != '\0';
778 if (!decl
->long_name
)
781 name
= skip_dash_dash(decl
, arg
);
785 equal
= strchr(name
, '=');
786 if (need_argument
&& !equal
)
790 *has_argument
= !!equal
;
791 end
= equal
? equal
: name
+ strlen(name
);
793 name
= skip_prefixes(name
, prefixes
, NULL
);
795 if (!match_long_name(decl
, name
, end
))
798 return equal
? equal
+ 1 : end
;
801 static int parse_choice_option(struct isl_arg
*decl
, char **arg
,
802 struct isl_prefixes
*prefixes
, void *opt
)
808 choice
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
812 if (!has_argument
&& (!arg
[1] || arg
[1][0] == '-')) {
813 unsigned u
= decl
->u
.choice
.default_selected
;
814 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
815 if (decl
->u
.choice
.set
)
816 decl
->u
.choice
.set(opt
, u
);
824 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
827 if (strcmp(choice
, decl
->u
.choice
.choice
[i
].name
))
830 u
= decl
->u
.choice
.choice
[i
].value
;
831 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
832 if (decl
->u
.choice
.set
)
833 decl
->u
.choice
.set(opt
, u
);
835 return has_argument
? 1 : 2;
841 static int set_flag(struct isl_arg
*decl
, unsigned *val
, const char *flag
,
846 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
847 if (strncmp(flag
, decl
->u
.flags
.flags
[i
].name
, len
))
850 *val
&= ~decl
->u
.flags
.flags
[i
].mask
;
851 *val
|= decl
->u
.flags
.flags
[i
].value
;
859 static int parse_flags_option(struct isl_arg
*decl
, char **arg
,
860 struct isl_prefixes
*prefixes
, void *opt
)
867 flags
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
871 if (!has_argument
&& !arg
[1])
879 while ((comma
= strchr(flags
, ',')) != NULL
) {
880 if (!set_flag(decl
, &val
, flags
, comma
- flags
))
884 if (!set_flag(decl
, &val
, flags
, strlen(flags
)))
887 *(unsigned *)(((char *)opt
) + decl
->offset
) = val
;
889 return has_argument
? 1 : 2;
892 static int parse_bool_option(struct isl_arg
*decl
, char **arg
,
893 struct isl_prefixes
*prefixes
, void *opt
)
896 unsigned *p
= (unsigned *)(((char *)opt
) + decl
->offset
);
899 if (skip_name(decl
, arg
[0], prefixes
, 0, NULL
)) {
900 if ((decl
->flags
& ISL_ARG_BOOL_ARG
) && arg
[1]) {
902 int val
= strtol(arg
[1], &endptr
, 0);
903 if (*endptr
== '\0' && (val
== 0 || val
== 1)) {
904 if (decl
->offset
!= (size_t) -1)
907 decl
->u
.b
.set(opt
, val
);
911 if (decl
->offset
!= (size_t) -1)
914 decl
->u
.b
.set(opt
, 1);
919 if (!decl
->long_name
)
922 name
= skip_dash_dash(decl
, arg
[0]);
927 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
929 if (strncmp(name
, "no-", 3))
933 name
= skip_prefixes(name
, prefixes
, &next_prefix
);
935 if (match_long_name(decl
, name
, name
+ strlen(name
))) {
936 if (decl
->offset
!= (size_t) -1)
939 decl
->u
.b
.set(opt
, 0);
947 static int parse_str_option(struct isl_arg
*decl
, char **arg
,
948 struct isl_prefixes
*prefixes
, void *opt
)
952 char **p
= (char **)(((char *)opt
) + decl
->offset
);
954 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
973 static int isl_arg_str_list_append(struct isl_arg
*decl
, void *opt
,
976 int *n
= (int *)(((char *) opt
) + decl
->u
.str_list
.offset_n
);
977 char **list
= *(char ***)(((char *) opt
) + decl
->offset
);
979 list
= realloc(list
, (*n
+ 1) * sizeof(char *));
982 *(char ***)(((char *) opt
) + decl
->offset
) = list
;
983 list
[*n
] = strdup(s
);
988 static int parse_str_list_option(struct isl_arg
*decl
, char **arg
,
989 struct isl_prefixes
*prefixes
, void *opt
)
994 s
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
999 isl_arg_str_list_append(decl
, opt
, s
);
1004 isl_arg_str_list_append(decl
, opt
, arg
[1]);
1011 static int parse_int_option(struct isl_arg
*decl
, char **arg
,
1012 struct isl_prefixes
*prefixes
, void *opt
)
1017 int *p
= (int *)(((char *)opt
) + decl
->offset
);
1019 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1029 int i
= strtol(arg
[1], &endptr
, 0);
1030 if (*endptr
== '\0') {
1039 static int parse_long_option(struct isl_arg
*decl
, char **arg
,
1040 struct isl_prefixes
*prefixes
, void *opt
)
1045 long *p
= (long *)(((char *)opt
) + decl
->offset
);
1047 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1052 long l
= strtol(val
, NULL
, 0);
1055 decl
->u
.l
.set(opt
, l
);
1060 long l
= strtol(arg
[1], &endptr
, 0);
1061 if (*endptr
== '\0') {
1064 decl
->u
.l
.set(opt
, l
);
1069 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
1070 *p
= decl
->u
.l
.default_selected
;
1072 decl
->u
.l
.set(opt
, decl
->u
.l
.default_selected
);
1079 static int parse_ulong_option(struct isl_arg
*decl
, char **arg
,
1080 struct isl_prefixes
*prefixes
, void *opt
)
1085 unsigned long *p
= (unsigned long *)(((char *)opt
) + decl
->offset
);
1087 val
= skip_name(decl
, arg
[0], prefixes
, 0, &has_argument
);
1092 *p
= strtoul(val
, NULL
, 0);
1097 unsigned long ul
= strtoul(arg
[1], &endptr
, 0);
1098 if (*endptr
== '\0') {
1107 static int parse_option(struct isl_arg
*decl
, char **arg
,
1108 struct isl_prefixes
*prefixes
, void *opt
);
1110 static int parse_child_option(struct isl_arg
*decl
, char **arg
,
1111 struct isl_prefixes
*prefixes
, void *opt
)
1116 if (decl
->offset
== (size_t) -1)
1119 child
= *(void **)(((char *)opt
) + decl
->offset
);
1121 first
= add_prefix(prefixes
, decl
->long_name
);
1122 parsed
= parse_option(decl
->u
.child
.child
->args
, arg
, prefixes
, child
);
1123 drop_prefix(prefixes
, first
);
1128 static int parse_option(struct isl_arg
*decl
, char **arg
,
1129 struct isl_prefixes
*prefixes
, void *opt
)
1133 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1135 switch (decl
[i
].type
) {
1136 case isl_arg_choice
:
1137 parsed
= parse_choice_option(&decl
[i
], arg
,
1141 parsed
= parse_flags_option(&decl
[i
], arg
,
1145 parsed
= parse_int_option(&decl
[i
], arg
, prefixes
, opt
);
1148 parsed
= parse_long_option(&decl
[i
], arg
,
1152 parsed
= parse_ulong_option(&decl
[i
], arg
,
1156 parsed
= parse_bool_option(&decl
[i
], arg
,
1160 parsed
= parse_str_option(&decl
[i
], arg
, prefixes
, opt
);
1162 case isl_arg_str_list
:
1163 parsed
= parse_str_list_option(&decl
[i
], arg
, prefixes
,
1167 parsed
= parse_child_option(&decl
[i
], arg
,
1172 case isl_arg_footer
:
1174 case isl_arg_version
:
1185 static void print_version(struct isl_arg
*decl
)
1189 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1190 switch (decl
[i
].type
) {
1191 case isl_arg_version
:
1192 decl
[i
].u
.version
.print_version();
1195 print_version(decl
[i
].u
.child
.child
->args
);
1203 static void print_version_and_exit(struct isl_arg
*decl
)
1205 print_version(decl
);
1210 static int drop_argument(int argc
, char **argv
, int drop
, int n
)
1212 for (; drop
+ n
< argc
; ++drop
)
1213 argv
[drop
] = argv
[drop
+ n
];
1218 static int n_arg(struct isl_arg
*arg
)
1223 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
1224 if (arg
[i
].type
== isl_arg_arg
)
1230 static int next_arg(struct isl_arg
*arg
, int a
)
1232 for (++a
; arg
[a
].type
!= isl_arg_end
; ++a
)
1233 if (arg
[a
].type
== isl_arg_arg
)
1239 /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
1240 * equal to "--help" and if so call print_help_and_exit.
1242 static void check_help(struct isl_args
*args
, char *arg
, char *prog
, void *opt
,
1245 if (ISL_FL_ISSET(flags
, ISL_ARG_SKIP_HELP
))
1248 if (strcmp(arg
, "--help") == 0)
1249 print_help_and_exit(args
->args
, prog
, opt
);
1252 int isl_args_parse(struct isl_args
*args
, int argc
, char **argv
, void *opt
,
1259 struct isl_prefixes prefixes
= { 0 };
1261 n
= n_arg(args
->args
);
1263 for (i
= 1; i
< argc
; ++i
) {
1264 if ((strcmp(argv
[i
], "--version") == 0 ||
1265 strcmp(argv
[i
], "-V") == 0) && any_version(args
->args
))
1266 print_version_and_exit(args
->args
);
1269 while (argc
> 1 + skip
) {
1271 if (argv
[1 + skip
][0] != '-') {
1272 a
= next_arg(args
->args
, a
);
1275 p
= (char **)(((char *)opt
)+args
->args
[a
].offset
);
1277 *p
= strdup(argv
[1 + skip
]);
1278 argc
= drop_argument(argc
, argv
, 1 + skip
, 1);
1280 } else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1281 fprintf(stderr
, "%s: extra argument: %s\n",
1282 prog_name(argv
[0]), argv
[1 + skip
]);
1288 check_help(args
, argv
[1 + skip
], argv
[0], opt
, flags
);
1289 parsed
= parse_option(args
->args
, &argv
[1 + skip
],
1292 argc
= drop_argument(argc
, argv
, 1 + skip
, parsed
);
1293 else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1294 fprintf(stderr
, "%s: unrecognized option: %s\n",
1295 prog_name(argv
[0]), argv
[1 + skip
]);
1302 fprintf(stderr
, "%s: expecting %d more argument(s)\n",
1303 prog_name(argv
[0]), n
);