add isl_map_factor_range
[isl.git] / isl_arg.c
blobfefb67d5c461a3d351dc335be6243717d5d72a27
1 /*
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
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include <isl/arg.h>
15 #include <isl/ctx.h>
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)
34 return;
35 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.b.default_value;
38 static void set_default_child(struct isl_arg *arg, void *opt)
40 void *child;
42 if (arg->offset == (size_t) -1)
43 child = opt;
44 else {
45 child = calloc(1, arg->u.child.child->options_size);
46 *(void **)(((char *)opt) + arg->offset) = child;
49 if (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)
89 int i;
91 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
92 switch (args->args[i].type) {
93 case isl_arg_choice:
94 set_default_choice(&args->args[i], opt);
95 break;
96 case isl_arg_flags:
97 set_default_flags(&args->args[i], opt);
98 break;
99 case isl_arg_bool:
100 set_default_bool(&args->args[i], opt);
101 break;
102 case isl_arg_child:
103 set_default_child(&args->args[i], opt);
104 break;
105 case isl_arg_user:
106 set_default_user(&args->args[i], opt);
107 break;
108 case isl_arg_int:
109 set_default_int(&args->args[i], opt);
110 break;
111 case isl_arg_long:
112 set_default_long(&args->args[i], opt);
113 break;
114 case isl_arg_ulong:
115 set_default_ulong(&args->args[i], opt);
116 break;
117 case isl_arg_arg:
118 case isl_arg_str:
119 set_default_str(&args->args[i], opt);
120 break;
121 case isl_arg_str_list:
122 set_default_str_list(&args->args[i], opt);
123 break;
124 case isl_arg_alias:
125 case isl_arg_footer:
126 case isl_arg_version:
127 case isl_arg_end:
128 break;
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);
139 else
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)
146 int i;
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)
151 free(list[i]);
152 free(list);
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)
163 int i;
165 for (i = 0; arg[i].type != isl_arg_end; ++i) {
166 switch (arg[i].type) {
167 case isl_arg_child:
168 free_child(&arg[i], opt);
169 break;
170 case isl_arg_arg:
171 case isl_arg_str:
172 free(*(char **)(((char *)opt) + arg[i].offset));
173 break;
174 case isl_arg_str_list:
175 free_str_list(&arg[i], opt);
176 break;
177 case isl_arg_user:
178 free_user(&arg[i], opt);
179 break;
180 case isl_arg_alias:
181 case isl_arg_bool:
182 case isl_arg_choice:
183 case isl_arg_flags:
184 case isl_arg_int:
185 case isl_arg_long:
186 case isl_arg_ulong:
187 case isl_arg_version:
188 case isl_arg_footer:
189 case isl_arg_end:
190 break;
195 void isl_args_free(struct isl_args *args, void *opt)
197 if (!opt)
198 return;
200 free_args(args->args, opt);
202 free(opt);
205 /* Data structure for collecting the prefixes of ancestor nodes.
207 * n is the number of prefixes.
208 * prefix[i] for i < n is a prefix of an ancestor.
209 * len[i] for i < n is the length of prefix[i].
211 struct isl_prefixes {
212 int n;
213 const char *prefix[10];
214 size_t len[10];
217 /* Add "prefix" to the list of prefixes and return the updated
218 * number of prefixes.
220 static int add_prefix(struct isl_prefixes *prefixes, const char *prefix)
222 int n = prefixes->n;
224 if (!prefix)
225 return n;
227 if (prefixes->n >= 10) {
228 fprintf(stderr, "too many prefixes\n");
229 exit(EXIT_FAILURE);
231 prefixes->len[prefixes->n] = strlen(prefix);
232 prefixes->prefix[prefixes->n] = prefix;
233 prefixes->n++;
235 return n;
238 /* Drop all prefixes starting at "first".
240 static void drop_prefix(struct isl_prefixes *prefixes, int first)
242 prefixes->n = first;
245 /* Print the prefixes in "prefixes".
247 static int print_prefixes(struct isl_prefixes *prefixes)
249 int i;
250 int len = 0;
252 if (!prefixes)
253 return 0;
255 for (i = 0; i < prefixes->n; ++i) {
256 printf("%s-", prefixes->prefix[i]);
257 len += strlen(prefixes->prefix[i]) + 1;
260 return len;
263 /* Check if "name" starts with one or more of the prefixes in "prefixes",
264 * starting at *first. If so, advance the pointer beyond the prefixes
265 * and return the updated pointer. Additionally, update *first to
266 * the index after the last prefix found.
268 static const char *skip_prefixes(const char *name,
269 struct isl_prefixes *prefixes, int *first)
271 int i;
273 for (i = first ? *first : 0; i < prefixes->n; ++i) {
274 size_t len = prefixes->len[i];
275 const char *prefix = prefixes->prefix[i];
276 if (strncmp(name, prefix, len) == 0 && name[len] == '-') {
277 name += len + 1;
278 if (first)
279 *first = i + 1;
283 return name;
286 static int print_arg_help(struct isl_arg *decl, struct isl_prefixes *prefixes,
287 int no)
289 int len = 0;
291 if (!decl->long_name) {
292 printf(" -%c", decl->short_name);
293 return 4;
296 if (decl->short_name) {
297 printf(" -%c, --", decl->short_name);
298 len += 8;
299 } else if (decl->flags & ISL_ARG_SINGLE_DASH) {
300 printf(" -");
301 len += 3;
302 } else {
303 printf(" --");
304 len += 8;
307 if (no) {
308 printf("no-");
309 len += 3;
311 len += print_prefixes(prefixes);
312 printf("%s", decl->long_name);
313 len += strlen(decl->long_name);
315 while ((++decl)->type == isl_arg_alias) {
316 printf(", --");
317 len += 4;
318 if (no) {
319 printf("no-");
320 len += 3;
322 printf("%s", decl->long_name);
323 len += strlen(decl->long_name);
326 return len;
329 const void *isl_memrchr(const void *s, int c, size_t n)
331 const char *p = s;
332 while (n-- > 0)
333 if (p[n] == c)
334 return p + n;
335 return NULL;
338 static int wrap_msg(const char *s, int indent, int pos)
340 int len;
341 int wrap_len = 75 - indent;
343 if (pos + 1 >= indent)
344 printf("\n%*s", indent, "");
345 else
346 printf("%*s", indent - pos, "");
348 len = strlen(s);
349 while (len > wrap_len) {
350 const char *space = isl_memrchr(s, ' ', wrap_len);
351 int l;
353 if (!space)
354 space = strchr(s + wrap_len, ' ');
355 if (!space)
356 break;
357 l = space - s;
358 printf("%.*s", l, s);
359 s = space + 1;
360 len -= l + 1;
361 printf("\n%*s", indent, "");
364 printf("%s", s);
365 return len;
368 static int print_help_msg(struct isl_arg *decl, int pos)
370 if (!decl->help_msg)
371 return pos;
373 return wrap_msg(decl->help_msg, 30, pos);
376 static void print_default(struct isl_arg *decl, const char *def, int pos)
378 const char *default_prefix = "[default: ";
379 const char *default_suffix = "]";
380 int len;
382 len = strlen(default_prefix) + strlen(def) + strlen(default_suffix);
384 if (!decl->help_msg) {
385 if (pos >= 29)
386 printf("\n%30s", "");
387 else
388 printf("%*s", 30 - pos, "");
389 pos = 0;
390 } else {
391 if (pos + len >= 48)
392 printf("\n%30s", "");
393 else
394 printf(" ");
396 printf("%s%s%s", default_prefix, def, default_suffix);
399 static void print_default_choice(struct isl_arg *decl, void *opt, int pos)
401 int i;
402 const char *s = "none";
403 unsigned *p;
405 p = (unsigned *)(((char *) opt) + decl->offset);
406 for (i = 0; decl->u.choice.choice[i].name; ++i)
407 if (decl->u.choice.choice[i].value == *p) {
408 s = decl->u.choice.choice[i].name;
409 break;
412 print_default(decl, s, pos);
415 static void print_choice_help(struct isl_arg *decl,
416 struct isl_prefixes *prefixes, void *opt)
418 int i;
419 int pos;
421 pos = print_arg_help(decl, prefixes, 0);
422 printf("=");
423 pos++;
425 for (i = 0; decl->u.choice.choice[i].name; ++i) {
426 if (i) {
427 printf("|");
428 pos++;
430 printf("%s", decl->u.choice.choice[i].name);
431 pos += strlen(decl->u.choice.choice[i].name);
434 pos = print_help_msg(decl, pos);
435 print_default_choice(decl, opt, pos);
437 printf("\n");
440 static void print_default_flags(struct isl_arg *decl, void *opt, int pos)
442 int i, first;
443 const char *default_prefix = "[default: ";
444 const char *default_suffix = "]";
445 int len = strlen(default_prefix) + strlen(default_suffix);
446 unsigned *p;
448 p = (unsigned *)(((char *) opt) + decl->offset);
449 for (i = 0; decl->u.flags.flags[i].name; ++i)
450 if ((*p & decl->u.flags.flags[i].mask) ==
451 decl->u.flags.flags[i].value)
452 len += strlen(decl->u.flags.flags[i].name);
454 if (!decl->help_msg) {
455 if (pos >= 29)
456 printf("\n%30s", "");
457 else
458 printf("%*s", 30 - pos, "");
459 pos = 0;
460 } else {
461 if (pos + len >= 48)
462 printf("\n%30s", "");
463 else
464 printf(" ");
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) {
471 if (!first)
472 printf(",");
473 printf("%s", decl->u.flags.flags[i].name);
474 first = 0;
477 printf("%s", default_suffix);
480 static void print_flags_help(struct isl_arg *decl,
481 struct isl_prefixes *prefixes, void *opt)
483 int i, j;
484 int pos;
486 pos = print_arg_help(decl, prefixes, 0);
487 printf("=");
488 pos++;
490 for (i = 0; decl->u.flags.flags[i].name; ++i) {
491 if (i) {
492 printf(",");
493 pos++;
495 for (j = i;
496 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
497 ++j) {
498 if (j != i) {
499 printf("|");
500 pos++;
502 printf("%s", decl->u.flags.flags[j].name);
503 pos += strlen(decl->u.flags.flags[j].name);
505 i = j - 1;
508 pos = print_help_msg(decl, pos);
509 print_default_flags(decl, opt, pos);
511 printf("\n");
514 static void print_bool_help(struct isl_arg *decl,
515 struct isl_prefixes *prefixes, void *opt)
517 int pos;
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);
524 printf("\n");
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)
536 int pos;
537 char val[20];
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);
544 printf("\n");
547 static void print_long_help(struct isl_arg *decl,
548 struct isl_prefixes *prefixes, void *opt)
550 int pos;
551 long *p = (long *)(((char *) opt) + decl->offset);
552 pos = print_arg_help(decl, prefixes, 0);
553 if (*p != decl->u.l.default_selected) {
554 printf("[");
555 pos++;
557 printf("=long");
558 pos += 5;
559 if (*p != decl->u.l.default_selected) {
560 printf("]");
561 pos++;
563 print_help_msg(decl, pos);
564 printf("\n");
567 static void print_ulong_help(struct isl_arg *decl,
568 struct isl_prefixes *prefixes)
570 int pos;
571 pos = print_arg_help(decl, prefixes, 0);
572 printf("=ulong");
573 pos += 6;
574 print_help_msg(decl, pos);
575 printf("\n");
578 static void print_str_help(struct isl_arg *decl,
579 struct isl_prefixes *prefixes, void *opt)
581 int pos;
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);
587 if (*p)
588 print_default(decl, *p, pos);
589 printf("\n");
592 static void print_str_list_help(struct isl_arg *decl,
593 struct isl_prefixes *prefixes)
595 int pos;
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);
600 printf("\n");
603 static void print_help(struct isl_arg *arg,
604 struct isl_prefixes *prefixes, void *opt)
606 int i;
607 int any = 0;
609 for (i = 0; arg[i].type != isl_arg_end; ++i) {
610 if (arg[i].flags & ISL_ARG_HIDDEN)
611 continue;
612 switch (arg[i].type) {
613 case isl_arg_flags:
614 print_flags_help(&arg[i], prefixes, opt);
615 any = 1;
616 break;
617 case isl_arg_choice:
618 print_choice_help(&arg[i], prefixes, opt);
619 any = 1;
620 break;
621 case isl_arg_bool:
622 print_bool_help(&arg[i], prefixes, opt);
623 any = 1;
624 break;
625 case isl_arg_int:
626 print_int_help(&arg[i], prefixes, opt);
627 any = 1;
628 break;
629 case isl_arg_long:
630 print_long_help(&arg[i], prefixes, opt);
631 any = 1;
632 break;
633 case isl_arg_ulong:
634 print_ulong_help(&arg[i], prefixes);
635 any = 1;
636 break;
637 case isl_arg_str:
638 print_str_help(&arg[i], prefixes, opt);
639 any = 1;
640 break;
641 case isl_arg_str_list:
642 print_str_list_help(&arg[i], prefixes);
643 any = 1;
644 break;
645 case isl_arg_alias:
646 case isl_arg_version:
647 case isl_arg_arg:
648 case isl_arg_footer:
649 case isl_arg_child:
650 case isl_arg_user:
651 case isl_arg_end:
652 break;
656 for (i = 0; arg[i].type != isl_arg_end; ++i) {
657 void *child;
658 int first;
660 if (arg[i].type != isl_arg_child)
661 continue;
662 if (arg[i].flags & ISL_ARG_HIDDEN)
663 continue;
665 if (any)
666 printf("\n");
667 if (arg[i].help_msg)
668 printf(" %s\n", arg[i].help_msg);
669 if (arg[i].offset == (size_t) -1)
670 child = opt;
671 else
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);
676 any = 1;
680 static const char *prog_name(const char *prog)
682 const char *slash;
684 slash = strrchr(prog, '/');
685 if (slash)
686 prog = slash + 1;
687 if (strncmp(prog, "lt-", 3) == 0)
688 prog += 3;
690 return prog;
693 static int any_version(struct isl_arg *decl)
695 int i;
697 for (i = 0; decl[i].type != isl_arg_end; ++i) {
698 switch (decl[i].type) {
699 case isl_arg_version:
700 return 1;
701 case isl_arg_child:
702 if (any_version(decl[i].u.child.child->args))
703 return 1;
704 break;
705 default:
706 break;
710 return 0;
713 static void print_help_and_exit(struct isl_arg *arg, const char *prog,
714 void *opt)
716 int i;
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);
725 printf("\n\n");
727 print_help(arg, &prefixes, opt);
728 printf("\n");
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)
735 continue;
736 wrap_msg(arg[i].help_msg, 0, 0);
737 printf("\n");
740 exit(0);
743 static int match_long_name(struct isl_arg *decl,
744 const char *start, const char *end)
746 do {
747 if (end - start == strlen(decl->long_name) &&
748 !strncmp(start, decl->long_name, end - start))
749 return 1;
750 } while ((++decl)->type == isl_arg_alias);
752 return 0;
755 static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
757 if (!strncmp(arg, "--", 2))
758 return arg + 2;
759 if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
760 return arg + 1;
761 return NULL;
764 static const char *skip_name(struct isl_arg *decl, const char *arg,
765 struct isl_prefixes *prefixes, int need_argument, int *has_argument)
767 const char *equal;
768 const char *name;
769 const char *end;
771 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
772 if (need_argument && !arg[2])
773 return NULL;
774 if (has_argument)
775 *has_argument = arg[2] != '\0';
776 return arg + 2;
778 if (!decl->long_name)
779 return NULL;
781 name = skip_dash_dash(decl, arg);
782 if (!name)
783 return NULL;
785 equal = strchr(name, '=');
786 if (need_argument && !equal)
787 return NULL;
789 if (has_argument)
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))
796 return NULL;
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)
804 int i;
805 int has_argument;
806 const char *choice;
808 choice = skip_name(decl, arg[0], prefixes, 0, &has_argument);
809 if (!choice)
810 return 0;
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);
818 return 1;
821 if (!has_argument)
822 choice = arg[1];
824 for (i = 0; decl->u.choice.choice[i].name; ++i) {
825 unsigned u;
827 if (strcmp(choice, decl->u.choice.choice[i].name))
828 continue;
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;
838 return 0;
841 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
842 size_t len)
844 int i;
846 for (i = 0; decl->u.flags.flags[i].name; ++i) {
847 if (strncmp(flag, decl->u.flags.flags[i].name, len))
848 continue;
850 *val &= ~decl->u.flags.flags[i].mask;
851 *val |= decl->u.flags.flags[i].value;
853 return 1;
856 return 0;
859 static int parse_flags_option(struct isl_arg *decl, char **arg,
860 struct isl_prefixes *prefixes, void *opt)
862 int has_argument;
863 const char *flags;
864 const char *comma;
865 unsigned val;
867 flags = skip_name(decl, arg[0], prefixes, 0, &has_argument);
868 if (!flags)
869 return 0;
871 if (!has_argument && !arg[1])
872 return 0;
874 if (!has_argument)
875 flags = arg[1];
877 val = 0;
879 while ((comma = strchr(flags, ',')) != NULL) {
880 if (!set_flag(decl, &val, flags, comma - flags))
881 return 0;
882 flags = comma + 1;
884 if (!set_flag(decl, &val, flags, strlen(flags)))
885 return 0;
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)
895 const char *name;
896 unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
897 int next_prefix;
899 if (skip_name(decl, arg[0], prefixes, 0, NULL)) {
900 if ((decl->flags & ISL_ARG_BOOL_ARG) && arg[1]) {
901 char *endptr;
902 int val = strtol(arg[1], &endptr, 0);
903 if (*endptr == '\0' && (val == 0 || val == 1)) {
904 if (decl->offset != (size_t) -1)
905 *p = val;
906 if (decl->u.b.set)
907 decl->u.b.set(opt, val);
908 return 2;
911 if (decl->offset != (size_t) -1)
912 *p = 1;
913 if (decl->u.b.set)
914 decl->u.b.set(opt, 1);
916 return 1;
919 if (!decl->long_name)
920 return 0;
922 name = skip_dash_dash(decl, arg[0]);
923 if (!name)
924 return 0;
926 next_prefix = 0;
927 name = skip_prefixes(name, prefixes, &next_prefix);
929 if (strncmp(name, "no-", 3))
930 return 0;
931 name += 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)
937 *p = 0;
938 if (decl->u.b.set)
939 decl->u.b.set(opt, 0);
941 return 1;
944 return 0;
947 static int parse_str_option(struct isl_arg *decl, char **arg,
948 struct isl_prefixes *prefixes, void *opt)
950 int has_argument;
951 const char *s;
952 char **p = (char **)(((char *)opt) + decl->offset);
954 s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
955 if (!s)
956 return 0;
958 if (has_argument) {
959 free(*p);
960 *p = strdup(s);
961 return 1;
964 if (arg[1]) {
965 free(*p);
966 *p = strdup(arg[1]);
967 return 2;
970 return 0;
973 static int isl_arg_str_list_append(struct isl_arg *decl, void *opt,
974 const char *s)
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 *));
980 if (!list)
981 return -1;
982 *(char ***)(((char *) opt) + decl->offset) = list;
983 list[*n] = strdup(s);
984 (*n)++;
985 return 0;
988 static int parse_str_list_option(struct isl_arg *decl, char **arg,
989 struct isl_prefixes *prefixes, void *opt)
991 int has_argument;
992 const char *s;
994 s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
995 if (!s)
996 return 0;
998 if (has_argument) {
999 isl_arg_str_list_append(decl, opt, s);
1000 return 1;
1003 if (arg[1]) {
1004 isl_arg_str_list_append(decl, opt, arg[1]);
1005 return 2;
1008 return 0;
1011 static int parse_int_option(struct isl_arg *decl, char **arg,
1012 struct isl_prefixes *prefixes, void *opt)
1014 int has_argument;
1015 const char *val;
1016 char *endptr;
1017 int *p = (int *)(((char *)opt) + decl->offset);
1019 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1020 if (!val)
1021 return 0;
1023 if (has_argument) {
1024 *p = atoi(val);
1025 return 1;
1028 if (arg[1]) {
1029 int i = strtol(arg[1], &endptr, 0);
1030 if (*endptr == '\0') {
1031 *p = i;
1032 return 2;
1036 return 0;
1039 static int parse_long_option(struct isl_arg *decl, char **arg,
1040 struct isl_prefixes *prefixes, void *opt)
1042 int has_argument;
1043 const char *val;
1044 char *endptr;
1045 long *p = (long *)(((char *)opt) + decl->offset);
1047 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1048 if (!val)
1049 return 0;
1051 if (has_argument) {
1052 long l = strtol(val, NULL, 0);
1053 *p = l;
1054 if (decl->u.l.set)
1055 decl->u.l.set(opt, l);
1056 return 1;
1059 if (arg[1]) {
1060 long l = strtol(arg[1], &endptr, 0);
1061 if (*endptr == '\0') {
1062 *p = l;
1063 if (decl->u.l.set)
1064 decl->u.l.set(opt, l);
1065 return 2;
1069 if (decl->u.l.default_value != decl->u.l.default_selected) {
1070 *p = decl->u.l.default_selected;
1071 if (decl->u.l.set)
1072 decl->u.l.set(opt, decl->u.l.default_selected);
1073 return 1;
1076 return 0;
1079 static int parse_ulong_option(struct isl_arg *decl, char **arg,
1080 struct isl_prefixes *prefixes, void *opt)
1082 int has_argument;
1083 const char *val;
1084 char *endptr;
1085 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
1087 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1088 if (!val)
1089 return 0;
1091 if (has_argument) {
1092 *p = strtoul(val, NULL, 0);
1093 return 1;
1096 if (arg[1]) {
1097 unsigned long ul = strtoul(arg[1], &endptr, 0);
1098 if (*endptr == '\0') {
1099 *p = ul;
1100 return 2;
1104 return 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)
1113 void *child;
1114 int first, parsed;
1116 if (decl->offset == (size_t) -1)
1117 child = opt;
1118 else
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);
1125 return parsed;
1128 static int parse_option(struct isl_arg *decl, char **arg,
1129 struct isl_prefixes *prefixes, void *opt)
1131 int i;
1133 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1134 int parsed = 0;
1135 switch (decl[i].type) {
1136 case isl_arg_choice:
1137 parsed = parse_choice_option(&decl[i], arg,
1138 prefixes, opt);
1139 break;
1140 case isl_arg_flags:
1141 parsed = parse_flags_option(&decl[i], arg,
1142 prefixes, opt);
1143 break;
1144 case isl_arg_int:
1145 parsed = parse_int_option(&decl[i], arg, prefixes, opt);
1146 break;
1147 case isl_arg_long:
1148 parsed = parse_long_option(&decl[i], arg,
1149 prefixes, opt);
1150 break;
1151 case isl_arg_ulong:
1152 parsed = parse_ulong_option(&decl[i], arg,
1153 prefixes, opt);
1154 break;
1155 case isl_arg_bool:
1156 parsed = parse_bool_option(&decl[i], arg,
1157 prefixes, opt);
1158 break;
1159 case isl_arg_str:
1160 parsed = parse_str_option(&decl[i], arg, prefixes, opt);
1161 break;
1162 case isl_arg_str_list:
1163 parsed = parse_str_list_option(&decl[i], arg, prefixes,
1164 opt);
1165 break;
1166 case isl_arg_child:
1167 parsed = parse_child_option(&decl[i], arg,
1168 prefixes, opt);
1169 break;
1170 case isl_arg_alias:
1171 case isl_arg_arg:
1172 case isl_arg_footer:
1173 case isl_arg_user:
1174 case isl_arg_version:
1175 case isl_arg_end:
1176 break;
1178 if (parsed)
1179 return parsed;
1182 return 0;
1185 static void print_version(struct isl_arg *decl)
1187 int i;
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();
1193 break;
1194 case isl_arg_child:
1195 print_version(decl[i].u.child.child->args);
1196 break;
1197 default:
1198 break;
1203 static void print_version_and_exit(struct isl_arg *decl)
1205 print_version(decl);
1207 exit(0);
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];
1215 return argc - n;
1218 static int n_arg(struct isl_arg *arg)
1220 int i;
1221 int n_arg = 0;
1223 for (i = 0; arg[i].type != isl_arg_end; ++i)
1224 if (arg[i].type == isl_arg_arg)
1225 n_arg++;
1227 return n_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)
1234 return a;
1236 return -1;
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,
1243 unsigned flags)
1245 if (ISL_FL_ISSET(flags, ISL_ARG_SKIP_HELP))
1246 return;
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,
1253 unsigned flags)
1255 int a = -1;
1256 int skip = 0;
1257 int i;
1258 int n;
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) {
1270 int parsed;
1271 if (argv[1 + skip][0] != '-') {
1272 a = next_arg(args->args, a);
1273 if (a >= 0) {
1274 char **p;
1275 p = (char **)(((char *)opt)+args->args[a].offset);
1276 free(*p);
1277 *p = strdup(argv[1 + skip]);
1278 argc = drop_argument(argc, argv, 1 + skip, 1);
1279 --n;
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]);
1283 exit(-1);
1284 } else
1285 ++skip;
1286 continue;
1288 check_help(args, argv[1 + skip], argv[0], opt, flags);
1289 parsed = parse_option(args->args, &argv[1 + skip],
1290 &prefixes, opt);
1291 if (parsed)
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]);
1296 exit(-1);
1297 } else
1298 ++skip;
1301 if (n > 0) {
1302 fprintf(stderr, "%s: expecting %d more argument(s)\n",
1303 prog_name(argv[0]), n);
1304 exit(-1);
1307 return argc;