isl_ast_build_set_loop_bounds: extract out update of pending and generated
[isl.git] / isl_arg.c
bloba5211574a5038bfdd60cac9dfd367566402c6d50
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 if (arg->offset == (size_t) -1)
24 return;
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)
36 return;
37 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.b.default_value;
40 static void set_default_child(struct isl_arg *arg, void *opt)
42 void *child;
44 if (arg->offset == (size_t) -1)
45 child = opt;
46 else {
47 child = calloc(1, arg->u.child.child->options_size);
48 *(void **)(((char *)opt) + arg->offset) = child;
51 if (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)
91 int i;
93 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
94 switch (args->args[i].type) {
95 case isl_arg_choice:
96 set_default_choice(&args->args[i], opt);
97 break;
98 case isl_arg_flags:
99 set_default_flags(&args->args[i], opt);
100 break;
101 case isl_arg_bool:
102 set_default_bool(&args->args[i], opt);
103 break;
104 case isl_arg_child:
105 set_default_child(&args->args[i], opt);
106 break;
107 case isl_arg_user:
108 set_default_user(&args->args[i], opt);
109 break;
110 case isl_arg_int:
111 set_default_int(&args->args[i], opt);
112 break;
113 case isl_arg_long:
114 set_default_long(&args->args[i], opt);
115 break;
116 case isl_arg_ulong:
117 set_default_ulong(&args->args[i], opt);
118 break;
119 case isl_arg_arg:
120 case isl_arg_str:
121 set_default_str(&args->args[i], opt);
122 break;
123 case isl_arg_str_list:
124 set_default_str_list(&args->args[i], opt);
125 break;
126 case isl_arg_alias:
127 case isl_arg_footer:
128 case isl_arg_version:
129 case isl_arg_end:
130 break;
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);
141 else
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)
148 int i;
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)
153 free(list[i]);
154 free(list);
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)
165 int i;
167 for (i = 0; arg[i].type != isl_arg_end; ++i) {
168 switch (arg[i].type) {
169 case isl_arg_child:
170 free_child(&arg[i], opt);
171 break;
172 case isl_arg_arg:
173 case isl_arg_str:
174 free(*(char **)(((char *)opt) + arg[i].offset));
175 break;
176 case isl_arg_str_list:
177 free_str_list(&arg[i], opt);
178 break;
179 case isl_arg_user:
180 free_user(&arg[i], opt);
181 break;
182 case isl_arg_alias:
183 case isl_arg_bool:
184 case isl_arg_choice:
185 case isl_arg_flags:
186 case isl_arg_int:
187 case isl_arg_long:
188 case isl_arg_ulong:
189 case isl_arg_version:
190 case isl_arg_footer:
191 case isl_arg_end:
192 break;
197 void isl_args_free(struct isl_args *args, void *opt)
199 if (!opt)
200 return;
202 free_args(args->args, opt);
204 free(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 {
214 int n;
215 const char *prefix[10];
216 size_t len[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)
224 int n = prefixes->n;
226 if (!prefix)
227 return n;
229 if (prefixes->n >= 10) {
230 fprintf(stderr, "too many prefixes\n");
231 exit(EXIT_FAILURE);
233 prefixes->len[prefixes->n] = strlen(prefix);
234 prefixes->prefix[prefixes->n] = prefix;
235 prefixes->n++;
237 return n;
240 /* Drop all prefixes starting at "first".
242 static void drop_prefix(struct isl_prefixes *prefixes, int first)
244 prefixes->n = first;
247 /* Print the prefixes in "prefixes".
249 static int print_prefixes(struct isl_prefixes *prefixes)
251 int i;
252 int len = 0;
254 if (!prefixes)
255 return 0;
257 for (i = 0; i < prefixes->n; ++i) {
258 printf("%s-", prefixes->prefix[i]);
259 len += strlen(prefixes->prefix[i]) + 1;
262 return len;
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)
273 int i;
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] == '-') {
279 name += len + 1;
280 if (first)
281 *first = i + 1;
285 return name;
288 static int print_arg_help(struct isl_arg *decl, struct isl_prefixes *prefixes,
289 int no)
291 int len = 0;
293 if (!decl->long_name) {
294 printf(" -%c", decl->short_name);
295 return 4;
298 if (decl->short_name) {
299 printf(" -%c, --", decl->short_name);
300 len += 8;
301 } else if (decl->flags & ISL_ARG_SINGLE_DASH) {
302 printf(" -");
303 len += 3;
304 } else {
305 printf(" --");
306 len += 8;
309 if (no) {
310 printf("no-");
311 len += 3;
313 len += print_prefixes(prefixes);
314 printf("%s", decl->long_name);
315 len += strlen(decl->long_name);
317 while ((++decl)->type == isl_arg_alias) {
318 printf(", --");
319 len += 4;
320 if (no) {
321 printf("no-");
322 len += 3;
324 printf("%s", decl->long_name);
325 len += strlen(decl->long_name);
328 return len;
331 const void *isl_memrchr(const void *s, int c, size_t n)
333 const char *p = s;
334 while (n-- > 0)
335 if (p[n] == c)
336 return p + n;
337 return NULL;
340 static int wrap_msg(const char *s, int indent, int pos)
342 int len;
343 int wrap_len = 75 - indent;
345 if (pos + 1 >= indent)
346 printf("\n%*s", indent, "");
347 else
348 printf("%*s", indent - pos, "");
350 len = strlen(s);
351 while (len > wrap_len) {
352 const char *space = isl_memrchr(s, ' ', wrap_len);
353 int l;
355 if (!space)
356 space = strchr(s + wrap_len, ' ');
357 if (!space)
358 break;
359 l = space - s;
360 printf("%.*s", l, s);
361 s = space + 1;
362 len -= l + 1;
363 printf("\n%*s", indent, "");
366 printf("%s", s);
367 return len;
370 static int print_help_msg(struct isl_arg *decl, int pos)
372 if (!decl->help_msg)
373 return 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 = "]";
382 int len;
384 len = strlen(default_prefix) + strlen(def) + strlen(default_suffix);
386 if (!decl->help_msg) {
387 if (pos >= 29)
388 printf("\n%30s", "");
389 else
390 printf("%*s", 30 - pos, "");
391 } else {
392 if (pos + len >= 48)
393 printf("\n%30s", "");
394 else
395 printf(" ");
397 printf("%s%s%s", default_prefix, def, default_suffix);
400 static void print_default_choice(struct isl_arg *decl, void *opt, int pos)
402 int i;
403 const char *s = "none";
404 unsigned *p;
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;
410 break;
413 print_default(decl, s, pos);
416 static void print_choice_help(struct isl_arg *decl,
417 struct isl_prefixes *prefixes, void *opt)
419 int i;
420 int pos;
422 pos = print_arg_help(decl, prefixes, 0);
423 printf("=");
424 pos++;
426 for (i = 0; decl->u.choice.choice[i].name; ++i) {
427 if (i) {
428 printf("|");
429 pos++;
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);
438 printf("\n");
441 static void print_default_flags(struct isl_arg *decl, void *opt, int pos)
443 int i, first;
444 const char *default_prefix = "[default: ";
445 const char *default_suffix = "]";
446 int len = strlen(default_prefix) + strlen(default_suffix);
447 unsigned *p;
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) {
456 if (pos >= 29)
457 printf("\n%30s", "");
458 else
459 printf("%*s", 30 - pos, "");
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 if (decl->offset != (size_t) -1)
815 *(unsigned *)(((char *)opt) + decl->offset) = u;
816 if (decl->u.choice.set)
817 decl->u.choice.set(opt, u);
819 return 1;
822 if (!has_argument)
823 choice = arg[1];
825 for (i = 0; decl->u.choice.choice[i].name; ++i) {
826 unsigned u;
828 if (strcmp(choice, decl->u.choice.choice[i].name))
829 continue;
831 u = decl->u.choice.choice[i].value;
832 if (decl->offset != (size_t) -1)
833 *(unsigned *)(((char *)opt) + decl->offset) = u;
834 if (decl->u.choice.set)
835 decl->u.choice.set(opt, u);
837 return has_argument ? 1 : 2;
840 return 0;
843 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
844 size_t len)
846 int i;
848 for (i = 0; decl->u.flags.flags[i].name; ++i) {
849 if (strncmp(flag, decl->u.flags.flags[i].name, len))
850 continue;
852 *val &= ~decl->u.flags.flags[i].mask;
853 *val |= decl->u.flags.flags[i].value;
855 return 1;
858 return 0;
861 static int parse_flags_option(struct isl_arg *decl, char **arg,
862 struct isl_prefixes *prefixes, void *opt)
864 int has_argument;
865 const char *flags;
866 const char *comma;
867 unsigned val;
869 flags = skip_name(decl, arg[0], prefixes, 0, &has_argument);
870 if (!flags)
871 return 0;
873 if (!has_argument && !arg[1])
874 return 0;
876 if (!has_argument)
877 flags = arg[1];
879 val = 0;
881 while ((comma = strchr(flags, ',')) != NULL) {
882 if (!set_flag(decl, &val, flags, comma - flags))
883 return 0;
884 flags = comma + 1;
886 if (!set_flag(decl, &val, flags, strlen(flags)))
887 return 0;
889 *(unsigned *)(((char *)opt) + decl->offset) = val;
891 return has_argument ? 1 : 2;
894 static int parse_bool_option(struct isl_arg *decl, char **arg,
895 struct isl_prefixes *prefixes, void *opt)
897 const char *name;
898 unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
899 int next_prefix;
901 if (skip_name(decl, arg[0], prefixes, 0, NULL)) {
902 if ((decl->flags & ISL_ARG_BOOL_ARG) && arg[1]) {
903 char *endptr;
904 int val = strtol(arg[1], &endptr, 0);
905 if (*endptr == '\0' && (val == 0 || val == 1)) {
906 if (decl->offset != (size_t) -1)
907 *p = val;
908 if (decl->u.b.set)
909 decl->u.b.set(opt, val);
910 return 2;
913 if (decl->offset != (size_t) -1)
914 *p = 1;
915 if (decl->u.b.set)
916 decl->u.b.set(opt, 1);
918 return 1;
921 if (!decl->long_name)
922 return 0;
924 name = skip_dash_dash(decl, arg[0]);
925 if (!name)
926 return 0;
928 next_prefix = 0;
929 name = skip_prefixes(name, prefixes, &next_prefix);
931 if (strncmp(name, "no-", 3))
932 return 0;
933 name += 3;
935 name = skip_prefixes(name, prefixes, &next_prefix);
937 if (match_long_name(decl, name, name + strlen(name))) {
938 if (decl->offset != (size_t) -1)
939 *p = 0;
940 if (decl->u.b.set)
941 decl->u.b.set(opt, 0);
943 return 1;
946 return 0;
949 static int parse_str_option(struct isl_arg *decl, char **arg,
950 struct isl_prefixes *prefixes, void *opt)
952 int has_argument;
953 const char *s;
954 char **p = (char **)(((char *)opt) + decl->offset);
956 s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
957 if (!s)
958 return 0;
960 if (has_argument) {
961 free(*p);
962 *p = strdup(s);
963 return 1;
966 if (arg[1]) {
967 free(*p);
968 *p = strdup(arg[1]);
969 return 2;
972 return 0;
975 static int isl_arg_str_list_append(struct isl_arg *decl, void *opt,
976 const char *s)
978 int *n = (int *)(((char *) opt) + decl->u.str_list.offset_n);
979 char **list = *(char ***)(((char *) opt) + decl->offset);
981 list = realloc(list, (*n + 1) * sizeof(char *));
982 if (!list)
983 return -1;
984 *(char ***)(((char *) opt) + decl->offset) = list;
985 list[*n] = strdup(s);
986 (*n)++;
987 return 0;
990 static int parse_str_list_option(struct isl_arg *decl, char **arg,
991 struct isl_prefixes *prefixes, void *opt)
993 int has_argument;
994 const char *s;
996 s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
997 if (!s)
998 return 0;
1000 if (has_argument) {
1001 isl_arg_str_list_append(decl, opt, s);
1002 return 1;
1005 if (arg[1]) {
1006 isl_arg_str_list_append(decl, opt, arg[1]);
1007 return 2;
1010 return 0;
1013 static int parse_int_option(struct isl_arg *decl, char **arg,
1014 struct isl_prefixes *prefixes, void *opt)
1016 int has_argument;
1017 const char *val;
1018 char *endptr;
1019 int *p = (int *)(((char *)opt) + decl->offset);
1021 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1022 if (!val)
1023 return 0;
1025 if (has_argument) {
1026 *p = atoi(val);
1027 return 1;
1030 if (arg[1]) {
1031 int i = strtol(arg[1], &endptr, 0);
1032 if (*endptr == '\0') {
1033 *p = i;
1034 return 2;
1038 return 0;
1041 static int parse_long_option(struct isl_arg *decl, char **arg,
1042 struct isl_prefixes *prefixes, void *opt)
1044 int has_argument;
1045 const char *val;
1046 char *endptr;
1047 long *p = (long *)(((char *)opt) + decl->offset);
1049 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1050 if (!val)
1051 return 0;
1053 if (has_argument) {
1054 long l = strtol(val, NULL, 0);
1055 *p = l;
1056 if (decl->u.l.set)
1057 decl->u.l.set(opt, l);
1058 return 1;
1061 if (arg[1]) {
1062 long l = strtol(arg[1], &endptr, 0);
1063 if (*endptr == '\0') {
1064 *p = l;
1065 if (decl->u.l.set)
1066 decl->u.l.set(opt, l);
1067 return 2;
1071 if (decl->u.l.default_value != decl->u.l.default_selected) {
1072 *p = decl->u.l.default_selected;
1073 if (decl->u.l.set)
1074 decl->u.l.set(opt, decl->u.l.default_selected);
1075 return 1;
1078 return 0;
1081 static int parse_ulong_option(struct isl_arg *decl, char **arg,
1082 struct isl_prefixes *prefixes, void *opt)
1084 int has_argument;
1085 const char *val;
1086 char *endptr;
1087 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
1089 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1090 if (!val)
1091 return 0;
1093 if (has_argument) {
1094 *p = strtoul(val, NULL, 0);
1095 return 1;
1098 if (arg[1]) {
1099 unsigned long ul = strtoul(arg[1], &endptr, 0);
1100 if (*endptr == '\0') {
1101 *p = ul;
1102 return 2;
1106 return 0;
1109 static int parse_option(struct isl_arg *decl, char **arg,
1110 struct isl_prefixes *prefixes, void *opt);
1112 static int parse_child_option(struct isl_arg *decl, char **arg,
1113 struct isl_prefixes *prefixes, void *opt)
1115 void *child;
1116 int first, parsed;
1118 if (decl->offset == (size_t) -1)
1119 child = opt;
1120 else
1121 child = *(void **)(((char *)opt) + decl->offset);
1123 first = add_prefix(prefixes, decl->long_name);
1124 parsed = parse_option(decl->u.child.child->args, arg, prefixes, child);
1125 drop_prefix(prefixes, first);
1127 return parsed;
1130 static int parse_option(struct isl_arg *decl, char **arg,
1131 struct isl_prefixes *prefixes, void *opt)
1133 int i;
1135 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1136 int parsed = 0;
1137 switch (decl[i].type) {
1138 case isl_arg_choice:
1139 parsed = parse_choice_option(&decl[i], arg,
1140 prefixes, opt);
1141 break;
1142 case isl_arg_flags:
1143 parsed = parse_flags_option(&decl[i], arg,
1144 prefixes, opt);
1145 break;
1146 case isl_arg_int:
1147 parsed = parse_int_option(&decl[i], arg, prefixes, opt);
1148 break;
1149 case isl_arg_long:
1150 parsed = parse_long_option(&decl[i], arg,
1151 prefixes, opt);
1152 break;
1153 case isl_arg_ulong:
1154 parsed = parse_ulong_option(&decl[i], arg,
1155 prefixes, opt);
1156 break;
1157 case isl_arg_bool:
1158 parsed = parse_bool_option(&decl[i], arg,
1159 prefixes, opt);
1160 break;
1161 case isl_arg_str:
1162 parsed = parse_str_option(&decl[i], arg, prefixes, opt);
1163 break;
1164 case isl_arg_str_list:
1165 parsed = parse_str_list_option(&decl[i], arg, prefixes,
1166 opt);
1167 break;
1168 case isl_arg_child:
1169 parsed = parse_child_option(&decl[i], arg,
1170 prefixes, opt);
1171 break;
1172 case isl_arg_alias:
1173 case isl_arg_arg:
1174 case isl_arg_footer:
1175 case isl_arg_user:
1176 case isl_arg_version:
1177 case isl_arg_end:
1178 break;
1180 if (parsed)
1181 return parsed;
1184 return 0;
1187 static void print_version(struct isl_arg *decl)
1189 int i;
1191 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1192 switch (decl[i].type) {
1193 case isl_arg_version:
1194 decl[i].u.version.print_version();
1195 break;
1196 case isl_arg_child:
1197 print_version(decl[i].u.child.child->args);
1198 break;
1199 default:
1200 break;
1205 static void print_version_and_exit(struct isl_arg *decl)
1207 print_version(decl);
1209 exit(0);
1212 static int drop_argument(int argc, char **argv, int drop, int n)
1214 for (; drop + n < argc; ++drop)
1215 argv[drop] = argv[drop + n];
1217 return argc - n;
1220 static int n_arg(struct isl_arg *arg)
1222 int i;
1223 int n_arg = 0;
1225 for (i = 0; arg[i].type != isl_arg_end; ++i)
1226 if (arg[i].type == isl_arg_arg)
1227 n_arg++;
1229 return n_arg;
1232 static int next_arg(struct isl_arg *arg, int a)
1234 for (++a; arg[a].type != isl_arg_end; ++a)
1235 if (arg[a].type == isl_arg_arg)
1236 return a;
1238 return -1;
1241 /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
1242 * equal to "--help" and if so call print_help_and_exit.
1244 static void check_help(struct isl_args *args, char *arg, char *prog, void *opt,
1245 unsigned flags)
1247 if (ISL_FL_ISSET(flags, ISL_ARG_SKIP_HELP))
1248 return;
1250 if (strcmp(arg, "--help") == 0)
1251 print_help_and_exit(args->args, prog, opt);
1254 int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
1255 unsigned flags)
1257 int a = -1;
1258 int skip = 0;
1259 int i;
1260 int n;
1261 struct isl_prefixes prefixes = { 0 };
1263 n = n_arg(args->args);
1265 for (i = 1; i < argc; ++i) {
1266 if ((strcmp(argv[i], "--version") == 0 ||
1267 strcmp(argv[i], "-V") == 0) && any_version(args->args))
1268 print_version_and_exit(args->args);
1271 while (argc > 1 + skip) {
1272 int parsed;
1273 if (argv[1 + skip][0] != '-') {
1274 a = next_arg(args->args, a);
1275 if (a >= 0) {
1276 char **p;
1277 p = (char **)(((char *)opt)+args->args[a].offset);
1278 free(*p);
1279 *p = strdup(argv[1 + skip]);
1280 argc = drop_argument(argc, argv, 1 + skip, 1);
1281 --n;
1282 } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1283 fprintf(stderr, "%s: extra argument: %s\n",
1284 prog_name(argv[0]), argv[1 + skip]);
1285 exit(-1);
1286 } else
1287 ++skip;
1288 continue;
1290 check_help(args, argv[1 + skip], argv[0], opt, flags);
1291 parsed = parse_option(args->args, &argv[1 + skip],
1292 &prefixes, opt);
1293 if (parsed)
1294 argc = drop_argument(argc, argv, 1 + skip, parsed);
1295 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1296 fprintf(stderr, "%s: unrecognized option: %s\n",
1297 prog_name(argv[0]), argv[1 + skip]);
1298 exit(-1);
1299 } else
1300 ++skip;
1303 if (n > 0) {
1304 fprintf(stderr, "%s: expecting %d more argument(s)\n",
1305 prog_name(argv[0]), n);
1306 exit(-1);
1309 return argc;