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 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.choice
.default_value
;
26 static void set_default_flags(struct isl_arg
*arg
, void *opt
)
28 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.flags
.default_value
;
31 static void set_default_bool(struct isl_arg
*arg
, void *opt
)
33 if (arg
->offset
== (size_t) -1)
35 *(unsigned *)(((char *)opt
) + arg
->offset
) = arg
->u
.b
.default_value
;
38 static void set_default_child(struct isl_arg
*arg
, void *opt
)
42 if (arg
->offset
== (size_t) -1)
45 child
= calloc(1, arg
->u
.child
.child
->options_size
);
46 *(void **)(((char *)opt
) + arg
->offset
) = child
;
50 isl_args_set_defaults(arg
->u
.child
.child
, child
);
53 static void set_default_user(struct isl_arg
*arg
, void *opt
)
55 arg
->u
.user
.init(((char *)opt
) + arg
->offset
);
58 static void set_default_int(struct isl_arg
*arg
, void *opt
)
60 *(int *)(((char *)opt
) + arg
->offset
) = arg
->u
.i
.default_value
;
63 static void set_default_long(struct isl_arg
*arg
, void *opt
)
65 *(long *)(((char *)opt
) + arg
->offset
) = arg
->u
.l
.default_value
;
68 static void set_default_ulong(struct isl_arg
*arg
, void *opt
)
70 *(unsigned long *)(((char *)opt
) + arg
->offset
) = arg
->u
.ul
.default_value
;
73 static void set_default_str(struct isl_arg
*arg
, void *opt
)
75 const char *str
= NULL
;
76 if (arg
->u
.str
.default_value
)
77 str
= strdup(arg
->u
.str
.default_value
);
78 *(const char **)(((char *)opt
) + arg
->offset
) = str
;
81 static void set_default_str_list(struct isl_arg
*arg
, void *opt
)
83 *(const char ***)(((char *) opt
) + arg
->offset
) = NULL
;
84 *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
) = 0;
87 void isl_args_set_defaults(struct isl_args
*args
, void *opt
)
91 for (i
= 0; args
->args
[i
].type
!= isl_arg_end
; ++i
) {
92 switch (args
->args
[i
].type
) {
94 set_default_choice(&args
->args
[i
], opt
);
97 set_default_flags(&args
->args
[i
], opt
);
100 set_default_bool(&args
->args
[i
], opt
);
103 set_default_child(&args
->args
[i
], opt
);
106 set_default_user(&args
->args
[i
], opt
);
109 set_default_int(&args
->args
[i
], opt
);
112 set_default_long(&args
->args
[i
], opt
);
115 set_default_ulong(&args
->args
[i
], opt
);
119 set_default_str(&args
->args
[i
], opt
);
121 case isl_arg_str_list
:
122 set_default_str_list(&args
->args
[i
], opt
);
126 case isl_arg_version
:
133 static void free_args(struct isl_arg
*arg
, void *opt
);
135 static void free_child(struct isl_arg
*arg
, void *opt
)
137 if (arg
->offset
== (size_t) -1)
138 free_args(arg
->u
.child
.child
->args
, opt
);
140 isl_args_free(arg
->u
.child
.child
,
141 *(void **)(((char *)opt
) + arg
->offset
));
144 static void free_str_list(struct isl_arg
*arg
, void *opt
)
147 int n
= *(int *)(((char *) opt
) + arg
->u
.str_list
.offset_n
);
148 char **list
= *(char ***)(((char *) opt
) + arg
->offset
);
150 for (i
= 0; i
< n
; ++i
)
155 static void free_user(struct isl_arg
*arg
, void *opt
)
157 if (arg
->u
.user
.clear
)
158 arg
->u
.user
.clear(((char *)opt
) + arg
->offset
);
161 static void free_args(struct isl_arg
*arg
, void *opt
)
165 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
166 switch (arg
[i
].type
) {
168 free_child(&arg
[i
], opt
);
172 free(*(char **)(((char *)opt
) + arg
[i
].offset
));
174 case isl_arg_str_list
:
175 free_str_list(&arg
[i
], opt
);
178 free_user(&arg
[i
], opt
);
187 case isl_arg_version
:
195 void isl_args_free(struct isl_args
*args
, void *opt
)
200 free_args(args
->args
, opt
);
205 static int print_arg_help(struct isl_arg
*decl
, const char *prefix
, int no
)
209 if (!decl
->long_name
) {
210 printf(" -%c", decl
->short_name
);
214 if (decl
->short_name
) {
215 printf(" -%c, --", decl
->short_name
);
217 } else if (decl
->flags
& ISL_ARG_SINGLE_DASH
) {
226 printf("%s-", prefix
);
227 len
+= strlen(prefix
) + 1;
233 printf("%s", decl
->long_name
);
234 len
+= strlen(decl
->long_name
);
236 while ((++decl
)->type
== isl_arg_alias
) {
243 printf("%s", decl
->long_name
);
244 len
+= strlen(decl
->long_name
);
250 const void *isl_memrchr(const void *s
, int c
, size_t n
)
259 static int wrap_msg(const char *s
, int indent
, int pos
)
262 int wrap_len
= 75 - indent
;
264 if (pos
+ 1 >= indent
)
265 printf("\n%*s", indent
, "");
267 printf("%*s", indent
- pos
, "");
270 while (len
> wrap_len
) {
271 const char *space
= isl_memrchr(s
, ' ', wrap_len
);
275 space
= strchr(s
+ wrap_len
, ' ');
279 printf("%.*s", l
, s
);
282 printf("\n%*s", indent
, "");
289 static int print_help_msg(struct isl_arg
*decl
, int pos
)
294 return wrap_msg(decl
->help_msg
, 30, pos
);
297 static void print_default(struct isl_arg
*decl
, const char *def
, int pos
)
299 const char *default_prefix
= "[default: ";
300 const char *default_suffix
= "]";
303 len
= strlen(default_prefix
) + strlen(def
) + strlen(default_suffix
);
305 if (!decl
->help_msg
) {
307 printf("\n%30s", "");
309 printf("%*s", 30 - pos
, "");
313 printf("\n%30s", "");
317 printf("%s%s%s", default_prefix
, def
, default_suffix
);
320 static void print_default_choice(struct isl_arg
*decl
, void *opt
, int pos
)
323 const char *s
= "none";
326 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
327 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
)
328 if (decl
->u
.choice
.choice
[i
].value
== *p
) {
329 s
= decl
->u
.choice
.choice
[i
].name
;
333 print_default(decl
, s
, pos
);
336 static void print_choice_help(struct isl_arg
*decl
, const char *prefix
,
342 pos
= print_arg_help(decl
, prefix
, 0);
346 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
351 printf("%s", decl
->u
.choice
.choice
[i
].name
);
352 pos
+= strlen(decl
->u
.choice
.choice
[i
].name
);
355 pos
= print_help_msg(decl
, pos
);
356 print_default_choice(decl
, opt
, pos
);
361 static void print_default_flags(struct isl_arg
*decl
, void *opt
, int pos
)
364 const char *default_prefix
= "[default: ";
365 const char *default_suffix
= "]";
366 int len
= strlen(default_prefix
) + strlen(default_suffix
);
369 p
= (unsigned *)(((char *) opt
) + decl
->offset
);
370 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
371 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
372 decl
->u
.flags
.flags
[i
].value
)
373 len
+= strlen(decl
->u
.flags
.flags
[i
].name
);
375 if (!decl
->help_msg
) {
377 printf("\n%30s", "");
379 printf("%*s", 30 - pos
, "");
383 printf("\n%30s", "");
387 printf("%s", default_prefix
);
389 for (first
= 1, i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
)
390 if ((*p
& decl
->u
.flags
.flags
[i
].mask
) ==
391 decl
->u
.flags
.flags
[i
].value
) {
394 printf("%s", decl
->u
.flags
.flags
[i
].name
);
398 printf("%s", default_suffix
);
401 static void print_flags_help(struct isl_arg
*decl
, const char *prefix
,
407 pos
= print_arg_help(decl
, prefix
, 0);
411 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
417 decl
->u
.flags
.flags
[j
].mask
== decl
->u
.flags
.flags
[i
].mask
;
423 printf("%s", decl
->u
.flags
.flags
[j
].name
);
424 pos
+= strlen(decl
->u
.flags
.flags
[j
].name
);
429 pos
= print_help_msg(decl
, pos
);
430 print_default_flags(decl
, opt
, pos
);
435 static void print_bool_help(struct isl_arg
*decl
, const char *prefix
, void *opt
)
438 unsigned *p
= opt
? (unsigned *)(((char *) opt
) + decl
->offset
) : NULL
;
439 int no
= p
? *p
== 1 : 0;
440 pos
= print_arg_help(decl
, prefix
, no
);
441 pos
= print_help_msg(decl
, pos
);
442 if (decl
->offset
!= (size_t) -1)
443 print_default(decl
, no
? "yes" : "no", pos
);
447 static int print_argument_name(struct isl_arg
*decl
, const char *name
, int pos
)
449 printf("%c<%s>", decl
->long_name
? '=' : ' ', name
);
450 return pos
+ 3 + strlen(name
);
453 static void print_int_help(struct isl_arg
*decl
, const char *prefix
, void *opt
)
457 int *p
= (int *)(((char *) opt
) + decl
->offset
);
458 pos
= print_arg_help(decl
, prefix
, 0);
459 pos
= print_argument_name(decl
, decl
->argument_name
, pos
);
460 pos
= print_help_msg(decl
, pos
);
461 snprintf(val
, sizeof(val
), "%d", *p
);
462 print_default(decl
, val
, pos
);
466 static void print_long_help(struct isl_arg
*decl
, const char *prefix
, void *opt
)
469 long *p
= (long *)(((char *) opt
) + decl
->offset
);
470 pos
= print_arg_help(decl
, prefix
, 0);
471 if (*p
!= decl
->u
.l
.default_selected
) {
477 if (*p
!= decl
->u
.l
.default_selected
) {
481 print_help_msg(decl
, pos
);
485 static void print_ulong_help(struct isl_arg
*decl
, const char *prefix
)
488 pos
= print_arg_help(decl
, prefix
, 0);
491 print_help_msg(decl
, pos
);
495 static void print_str_help(struct isl_arg
*decl
, const char *prefix
, void *opt
)
498 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
499 const char **p
= (const char **)(((char *) opt
) + decl
->offset
);
500 pos
= print_arg_help(decl
, prefix
, 0);
501 pos
= print_argument_name(decl
, a
, pos
);
502 pos
= print_help_msg(decl
, pos
);
504 print_default(decl
, *p
, pos
);
508 static void print_str_list_help(struct isl_arg
*decl
, const char *prefix
)
511 const char *a
= decl
->argument_name
? decl
->argument_name
: "string";
512 pos
= print_arg_help(decl
, prefix
, 0);
513 pos
= print_argument_name(decl
, a
, pos
);
514 pos
= print_help_msg(decl
, pos
);
518 static void print_help(struct isl_arg
*arg
, const char *prefix
, void *opt
)
523 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
524 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
526 switch (arg
[i
].type
) {
528 print_flags_help(&arg
[i
], prefix
, opt
);
532 print_choice_help(&arg
[i
], prefix
, opt
);
536 print_bool_help(&arg
[i
], prefix
, opt
);
540 print_int_help(&arg
[i
], prefix
, opt
);
544 print_long_help(&arg
[i
], prefix
, opt
);
548 print_ulong_help(&arg
[i
], prefix
);
552 print_str_help(&arg
[i
], prefix
, opt
);
555 case isl_arg_str_list
:
556 print_str_list_help(&arg
[i
], prefix
);
560 case isl_arg_version
:
570 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
573 if (arg
[i
].type
!= isl_arg_child
)
575 if (arg
[i
].flags
& ISL_ARG_HIDDEN
)
581 printf(" %s\n", arg
[i
].help_msg
);
582 if (arg
[i
].offset
== (size_t) -1)
585 child
= *(void **)(((char *) opt
) + arg
[i
].offset
);
586 print_help(arg
[i
].u
.child
.child
->args
, arg
[i
].long_name
, child
);
591 static const char *prog_name(const char *prog
)
595 slash
= strrchr(prog
, '/');
598 if (strncmp(prog
, "lt-", 3) == 0)
604 static int any_version(struct isl_arg
*decl
)
608 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
609 switch (decl
[i
].type
) {
610 case isl_arg_version
:
613 if (any_version(decl
[i
].u
.child
.child
->args
))
624 static void print_help_and_exit(struct isl_arg
*arg
, const char *prog
,
629 printf("Usage: %s [OPTION...]", prog_name(prog
));
631 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
632 if (arg
[i
].type
== isl_arg_arg
)
633 printf(" %s", arg
[i
].argument_name
);
637 print_help(arg
, NULL
, opt
);
639 if (any_version(arg
))
640 printf(" -V, --version\n");
641 print_bool_help(help_arg
, NULL
, NULL
);
643 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
) {
644 if (arg
[i
].type
!= isl_arg_footer
)
646 wrap_msg(arg
[i
].help_msg
, 0, 0);
653 static int match_long_name(struct isl_arg
*decl
,
654 const char *start
, const char *end
)
657 if (end
- start
== strlen(decl
->long_name
) &&
658 !strncmp(start
, decl
->long_name
, end
- start
))
660 } while ((++decl
)->type
== isl_arg_alias
);
665 static const char *skip_dash_dash(struct isl_arg
*decl
, const char *arg
)
667 if (!strncmp(arg
, "--", 2))
669 if ((decl
->flags
& ISL_ARG_SINGLE_DASH
) && arg
[0] == '-')
674 static const char *skip_name(struct isl_arg
*decl
, const char *arg
,
675 const char *prefix
, int need_argument
, int *has_argument
)
681 if (arg
[0] == '-' && arg
[1] && arg
[1] == decl
->short_name
) {
682 if (need_argument
&& !arg
[2])
685 *has_argument
= arg
[2] != '\0';
688 if (!decl
->long_name
)
691 name
= skip_dash_dash(decl
, arg
);
695 equal
= strchr(name
, '=');
696 if (need_argument
&& !equal
)
700 *has_argument
= !!equal
;
701 end
= equal
? equal
: name
+ strlen(name
);
704 size_t prefix_len
= strlen(prefix
);
705 if (strncmp(name
, prefix
, prefix_len
) == 0 &&
706 name
[prefix_len
] == '-')
707 name
+= prefix_len
+ 1;
710 if (!match_long_name(decl
, name
, end
))
713 return equal
? equal
+ 1 : end
;
716 static int parse_choice_option(struct isl_arg
*decl
, char **arg
,
717 const char *prefix
, void *opt
)
723 choice
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
727 if (!has_argument
&& (!arg
[1] || arg
[1][0] == '-')) {
728 unsigned u
= decl
->u
.choice
.default_selected
;
729 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
730 if (decl
->u
.choice
.set
)
731 decl
->u
.choice
.set(opt
, u
);
739 for (i
= 0; decl
->u
.choice
.choice
[i
].name
; ++i
) {
742 if (strcmp(choice
, decl
->u
.choice
.choice
[i
].name
))
745 u
= decl
->u
.choice
.choice
[i
].value
;
746 *(unsigned *)(((char *)opt
) + decl
->offset
) = u
;
747 if (decl
->u
.choice
.set
)
748 decl
->u
.choice
.set(opt
, u
);
750 return has_argument
? 1 : 2;
756 static int set_flag(struct isl_arg
*decl
, unsigned *val
, const char *flag
,
761 for (i
= 0; decl
->u
.flags
.flags
[i
].name
; ++i
) {
762 if (strncmp(flag
, decl
->u
.flags
.flags
[i
].name
, len
))
765 *val
&= ~decl
->u
.flags
.flags
[i
].mask
;
766 *val
|= decl
->u
.flags
.flags
[i
].value
;
774 static int parse_flags_option(struct isl_arg
*decl
, char **arg
,
775 const char *prefix
, void *opt
)
782 flags
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
786 if (!has_argument
&& !arg
[1])
794 while ((comma
= strchr(flags
, ',')) != NULL
) {
795 if (!set_flag(decl
, &val
, flags
, comma
- flags
))
799 if (!set_flag(decl
, &val
, flags
, strlen(flags
)))
802 *(unsigned *)(((char *)opt
) + decl
->offset
) = val
;
804 return has_argument
? 1 : 2;
807 static int parse_bool_option(struct isl_arg
*decl
, char **arg
,
808 const char *prefix
, void *opt
)
811 unsigned *p
= (unsigned *)(((char *)opt
) + decl
->offset
);
813 if (skip_name(decl
, arg
[0], prefix
, 0, NULL
)) {
814 if ((decl
->flags
& ISL_ARG_BOOL_ARG
) && arg
[1]) {
816 int val
= strtol(arg
[1], &endptr
, 0);
817 if (*endptr
== '\0' && (val
== 0 || val
== 1)) {
818 if (decl
->offset
!= (size_t) -1)
821 decl
->u
.b
.set(opt
, val
);
825 if (decl
->offset
!= (size_t) -1)
828 decl
->u
.b
.set(opt
, 1);
833 if (!decl
->long_name
)
836 name
= skip_dash_dash(decl
, arg
[0]);
841 size_t prefix_len
= strlen(prefix
);
842 if (strncmp(name
, prefix
, prefix_len
) == 0 &&
843 name
[prefix_len
] == '-') {
844 name
+= prefix_len
+ 1;
849 if (strncmp(name
, "no-", 3))
854 size_t prefix_len
= strlen(prefix
);
855 if (strncmp(name
, prefix
, prefix_len
) == 0 &&
856 name
[prefix_len
] == '-')
857 name
+= prefix_len
+ 1;
860 if (match_long_name(decl
, name
, name
+ strlen(name
))) {
861 if (decl
->offset
!= (size_t) -1)
864 decl
->u
.b
.set(opt
, 0);
872 static int parse_str_option(struct isl_arg
*decl
, char **arg
,
873 const char *prefix
, void *opt
)
877 char **p
= (char **)(((char *)opt
) + decl
->offset
);
879 s
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
898 static int isl_arg_str_list_append(struct isl_arg
*decl
, void *opt
,
901 int *n
= (int *)(((char *) opt
) + decl
->u
.str_list
.offset_n
);
902 char **list
= *(char ***)(((char *) opt
) + decl
->offset
);
904 list
= realloc(list
, (*n
+ 1) * sizeof(char *));
907 *(char ***)(((char *) opt
) + decl
->offset
) = list
;
908 list
[*n
] = strdup(s
);
913 static int parse_str_list_option(struct isl_arg
*decl
, char **arg
,
914 const char *prefix
, void *opt
)
919 s
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
924 isl_arg_str_list_append(decl
, opt
, s
);
929 isl_arg_str_list_append(decl
, opt
, arg
[1]);
936 static int parse_int_option(struct isl_arg
*decl
, char **arg
,
937 const char *prefix
, void *opt
)
942 int *p
= (int *)(((char *)opt
) + decl
->offset
);
944 val
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
954 int i
= strtol(arg
[1], &endptr
, 0);
955 if (*endptr
== '\0') {
964 static int parse_long_option(struct isl_arg
*decl
, char **arg
,
965 const char *prefix
, void *opt
)
970 long *p
= (long *)(((char *)opt
) + decl
->offset
);
972 val
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
977 long l
= strtol(val
, NULL
, 0);
980 decl
->u
.l
.set(opt
, l
);
985 long l
= strtol(arg
[1], &endptr
, 0);
986 if (*endptr
== '\0') {
989 decl
->u
.l
.set(opt
, l
);
994 if (decl
->u
.l
.default_value
!= decl
->u
.l
.default_selected
) {
995 *p
= decl
->u
.l
.default_selected
;
997 decl
->u
.l
.set(opt
, decl
->u
.l
.default_selected
);
1004 static int parse_ulong_option(struct isl_arg
*decl
, char **arg
,
1005 const char *prefix
, void *opt
)
1010 unsigned long *p
= (unsigned long *)(((char *)opt
) + decl
->offset
);
1012 val
= skip_name(decl
, arg
[0], prefix
, 0, &has_argument
);
1017 *p
= strtoul(val
, NULL
, 0);
1022 unsigned long ul
= strtoul(arg
[1], &endptr
, 0);
1023 if (*endptr
== '\0') {
1032 static int parse_option(struct isl_arg
*decl
, char **arg
,
1033 const char *prefix
, void *opt
);
1035 static int parse_child_option(struct isl_arg
*decl
, char **arg
,
1036 const char *prefix
, void *opt
)
1040 if (decl
->offset
== (size_t) -1)
1043 child
= *(void **)(((char *)opt
) + decl
->offset
);
1044 prefix
= decl
->long_name
;
1046 return parse_option(decl
->u
.child
.child
->args
, arg
, prefix
, child
);
1049 static int parse_option(struct isl_arg
*decl
, char **arg
,
1050 const char *prefix
, void *opt
)
1054 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1056 switch (decl
[i
].type
) {
1057 case isl_arg_choice
:
1058 parsed
= parse_choice_option(&decl
[i
], arg
, prefix
, opt
);
1061 parsed
= parse_flags_option(&decl
[i
], arg
, prefix
, opt
);
1064 parsed
= parse_int_option(&decl
[i
], arg
, prefix
, opt
);
1067 parsed
= parse_long_option(&decl
[i
], arg
, prefix
, opt
);
1070 parsed
= parse_ulong_option(&decl
[i
], arg
, prefix
, opt
);
1073 parsed
= parse_bool_option(&decl
[i
], arg
, prefix
, opt
);
1076 parsed
= parse_str_option(&decl
[i
], arg
, prefix
, opt
);
1078 case isl_arg_str_list
:
1079 parsed
= parse_str_list_option(&decl
[i
], arg
, prefix
,
1083 parsed
= parse_child_option(&decl
[i
], arg
, prefix
, opt
);
1087 case isl_arg_footer
:
1089 case isl_arg_version
:
1100 static void print_version(struct isl_arg
*decl
)
1104 for (i
= 0; decl
[i
].type
!= isl_arg_end
; ++i
) {
1105 switch (decl
[i
].type
) {
1106 case isl_arg_version
:
1107 decl
[i
].u
.version
.print_version();
1110 print_version(decl
[i
].u
.child
.child
->args
);
1118 static void print_version_and_exit(struct isl_arg
*decl
)
1120 print_version(decl
);
1125 static int drop_argument(int argc
, char **argv
, int drop
, int n
)
1127 for (; drop
+ n
< argc
; ++drop
)
1128 argv
[drop
] = argv
[drop
+ n
];
1133 static int n_arg(struct isl_arg
*arg
)
1138 for (i
= 0; arg
[i
].type
!= isl_arg_end
; ++i
)
1139 if (arg
[i
].type
== isl_arg_arg
)
1145 static int next_arg(struct isl_arg
*arg
, int a
)
1147 for (++a
; arg
[a
].type
!= isl_arg_end
; ++a
)
1148 if (arg
[a
].type
== isl_arg_arg
)
1154 /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
1155 * equal to "--help" and if so call print_help_and_exit.
1157 static void check_help(struct isl_args
*args
, char *arg
, char *prog
, void *opt
,
1160 if (ISL_FL_ISSET(flags
, ISL_ARG_SKIP_HELP
))
1163 if (strcmp(arg
, "--help") == 0)
1164 print_help_and_exit(args
->args
, prog
, opt
);
1167 int isl_args_parse(struct isl_args
*args
, int argc
, char **argv
, void *opt
,
1175 n
= n_arg(args
->args
);
1177 for (i
= 1; i
< argc
; ++i
) {
1178 if ((strcmp(argv
[i
], "--version") == 0 ||
1179 strcmp(argv
[i
], "-V") == 0) && any_version(args
->args
))
1180 print_version_and_exit(args
->args
);
1183 while (argc
> 1 + skip
) {
1185 if (argv
[1 + skip
][0] != '-') {
1186 a
= next_arg(args
->args
, a
);
1189 p
= (char **)(((char *)opt
)+args
->args
[a
].offset
);
1191 *p
= strdup(argv
[1 + skip
]);
1192 argc
= drop_argument(argc
, argv
, 1 + skip
, 1);
1194 } else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1195 fprintf(stderr
, "%s: extra argument: %s\n",
1196 prog_name(argv
[0]), argv
[1 + skip
]);
1202 check_help(args
, argv
[1 + skip
], argv
[0], opt
, flags
);
1203 parsed
= parse_option(args
->args
, &argv
[1 + skip
], NULL
, opt
);
1205 argc
= drop_argument(argc
, argv
, 1 + skip
, parsed
);
1206 else if (ISL_FL_ISSET(flags
, ISL_ARG_ALL
)) {
1207 fprintf(stderr
, "%s: unrecognized option: %s\n",
1208 prog_name(argv
[0]), argv
[1 + skip
]);
1215 fprintf(stderr
, "%s: expecting %d more argument(s)\n",
1216 prog_name(argv
[0]), n
);