isl_scheduler.c: directly include required header
[isl.git] / isl_arg.c
blobe23911a8f9d6fc1e43bc7f681b52d498fb82dd10
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 } else {
390 if (pos + len >= 48)
391 printf("\n%30s", "");
392 else
393 printf(" ");
395 printf("%s%s%s", default_prefix, def, default_suffix);
398 static void print_default_choice(struct isl_arg *decl, void *opt, int pos)
400 int i;
401 const char *s = "none";
402 unsigned *p;
404 p = (unsigned *)(((char *) opt) + decl->offset);
405 for (i = 0; decl->u.choice.choice[i].name; ++i)
406 if (decl->u.choice.choice[i].value == *p) {
407 s = decl->u.choice.choice[i].name;
408 break;
411 print_default(decl, s, pos);
414 static void print_choice_help(struct isl_arg *decl,
415 struct isl_prefixes *prefixes, void *opt)
417 int i;
418 int pos;
420 pos = print_arg_help(decl, prefixes, 0);
421 printf("=");
422 pos++;
424 for (i = 0; decl->u.choice.choice[i].name; ++i) {
425 if (i) {
426 printf("|");
427 pos++;
429 printf("%s", decl->u.choice.choice[i].name);
430 pos += strlen(decl->u.choice.choice[i].name);
433 pos = print_help_msg(decl, pos);
434 print_default_choice(decl, opt, pos);
436 printf("\n");
439 static void print_default_flags(struct isl_arg *decl, void *opt, int pos)
441 int i, first;
442 const char *default_prefix = "[default: ";
443 const char *default_suffix = "]";
444 int len = strlen(default_prefix) + strlen(default_suffix);
445 unsigned *p;
447 p = (unsigned *)(((char *) opt) + decl->offset);
448 for (i = 0; decl->u.flags.flags[i].name; ++i)
449 if ((*p & decl->u.flags.flags[i].mask) ==
450 decl->u.flags.flags[i].value)
451 len += strlen(decl->u.flags.flags[i].name);
453 if (!decl->help_msg) {
454 if (pos >= 29)
455 printf("\n%30s", "");
456 else
457 printf("%*s", 30 - pos, "");
458 } else {
459 if (pos + len >= 48)
460 printf("\n%30s", "");
461 else
462 printf(" ");
464 printf("%s", default_prefix);
466 for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
467 if ((*p & decl->u.flags.flags[i].mask) ==
468 decl->u.flags.flags[i].value) {
469 if (!first)
470 printf(",");
471 printf("%s", decl->u.flags.flags[i].name);
472 first = 0;
475 printf("%s", default_suffix);
478 static void print_flags_help(struct isl_arg *decl,
479 struct isl_prefixes *prefixes, void *opt)
481 int i, j;
482 int pos;
484 pos = print_arg_help(decl, prefixes, 0);
485 printf("=");
486 pos++;
488 for (i = 0; decl->u.flags.flags[i].name; ++i) {
489 if (i) {
490 printf(",");
491 pos++;
493 for (j = i;
494 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
495 ++j) {
496 if (j != i) {
497 printf("|");
498 pos++;
500 printf("%s", decl->u.flags.flags[j].name);
501 pos += strlen(decl->u.flags.flags[j].name);
503 i = j - 1;
506 pos = print_help_msg(decl, pos);
507 print_default_flags(decl, opt, pos);
509 printf("\n");
512 static void print_bool_help(struct isl_arg *decl,
513 struct isl_prefixes *prefixes, void *opt)
515 int pos;
516 unsigned *p = opt ? (unsigned *)(((char *) opt) + decl->offset) : NULL;
517 int no = p ? *p == 1 : 0;
518 pos = print_arg_help(decl, prefixes, no);
519 pos = print_help_msg(decl, pos);
520 if (decl->offset != (size_t) -1)
521 print_default(decl, no ? "yes" : "no", pos);
522 printf("\n");
525 static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
527 printf("%c<%s>", decl->long_name ? '=' : ' ', name);
528 return pos + 3 + strlen(name);
531 static void print_int_help(struct isl_arg *decl,
532 struct isl_prefixes *prefixes, void *opt)
534 int pos;
535 char val[20];
536 int *p = (int *)(((char *) opt) + decl->offset);
537 pos = print_arg_help(decl, prefixes, 0);
538 pos = print_argument_name(decl, decl->argument_name, pos);
539 pos = print_help_msg(decl, pos);
540 snprintf(val, sizeof(val), "%d", *p);
541 print_default(decl, val, pos);
542 printf("\n");
545 static void print_long_help(struct isl_arg *decl,
546 struct isl_prefixes *prefixes, void *opt)
548 int pos;
549 long *p = (long *)(((char *) opt) + decl->offset);
550 pos = print_arg_help(decl, prefixes, 0);
551 if (*p != decl->u.l.default_selected) {
552 printf("[");
553 pos++;
555 printf("=long");
556 pos += 5;
557 if (*p != decl->u.l.default_selected) {
558 printf("]");
559 pos++;
561 print_help_msg(decl, pos);
562 printf("\n");
565 static void print_ulong_help(struct isl_arg *decl,
566 struct isl_prefixes *prefixes)
568 int pos;
569 pos = print_arg_help(decl, prefixes, 0);
570 printf("=ulong");
571 pos += 6;
572 print_help_msg(decl, pos);
573 printf("\n");
576 static void print_str_help(struct isl_arg *decl,
577 struct isl_prefixes *prefixes, void *opt)
579 int pos;
580 const char *a = decl->argument_name ? decl->argument_name : "string";
581 const char **p = (const char **)(((char *) opt) + decl->offset);
582 pos = print_arg_help(decl, prefixes, 0);
583 pos = print_argument_name(decl, a, pos);
584 pos = print_help_msg(decl, pos);
585 if (*p)
586 print_default(decl, *p, pos);
587 printf("\n");
590 static void print_str_list_help(struct isl_arg *decl,
591 struct isl_prefixes *prefixes)
593 int pos;
594 const char *a = decl->argument_name ? decl->argument_name : "string";
595 pos = print_arg_help(decl, prefixes, 0);
596 pos = print_argument_name(decl, a, pos);
597 pos = print_help_msg(decl, pos);
598 printf("\n");
601 static void print_help(struct isl_arg *arg,
602 struct isl_prefixes *prefixes, void *opt)
604 int i;
605 int any = 0;
607 for (i = 0; arg[i].type != isl_arg_end; ++i) {
608 if (arg[i].flags & ISL_ARG_HIDDEN)
609 continue;
610 switch (arg[i].type) {
611 case isl_arg_flags:
612 print_flags_help(&arg[i], prefixes, opt);
613 any = 1;
614 break;
615 case isl_arg_choice:
616 print_choice_help(&arg[i], prefixes, opt);
617 any = 1;
618 break;
619 case isl_arg_bool:
620 print_bool_help(&arg[i], prefixes, opt);
621 any = 1;
622 break;
623 case isl_arg_int:
624 print_int_help(&arg[i], prefixes, opt);
625 any = 1;
626 break;
627 case isl_arg_long:
628 print_long_help(&arg[i], prefixes, opt);
629 any = 1;
630 break;
631 case isl_arg_ulong:
632 print_ulong_help(&arg[i], prefixes);
633 any = 1;
634 break;
635 case isl_arg_str:
636 print_str_help(&arg[i], prefixes, opt);
637 any = 1;
638 break;
639 case isl_arg_str_list:
640 print_str_list_help(&arg[i], prefixes);
641 any = 1;
642 break;
643 case isl_arg_alias:
644 case isl_arg_version:
645 case isl_arg_arg:
646 case isl_arg_footer:
647 case isl_arg_child:
648 case isl_arg_user:
649 case isl_arg_end:
650 break;
654 for (i = 0; arg[i].type != isl_arg_end; ++i) {
655 void *child;
656 int first;
658 if (arg[i].type != isl_arg_child)
659 continue;
660 if (arg[i].flags & ISL_ARG_HIDDEN)
661 continue;
663 if (any)
664 printf("\n");
665 if (arg[i].help_msg)
666 printf(" %s\n", arg[i].help_msg);
667 if (arg[i].offset == (size_t) -1)
668 child = opt;
669 else
670 child = *(void **)(((char *) opt) + arg[i].offset);
671 first = add_prefix(prefixes, arg[i].long_name);
672 print_help(arg[i].u.child.child->args, prefixes, child);
673 drop_prefix(prefixes, first);
674 any = 1;
678 static const char *prog_name(const char *prog)
680 const char *slash;
682 slash = strrchr(prog, '/');
683 if (slash)
684 prog = slash + 1;
685 if (strncmp(prog, "lt-", 3) == 0)
686 prog += 3;
688 return prog;
691 static int any_version(struct isl_arg *decl)
693 int i;
695 for (i = 0; decl[i].type != isl_arg_end; ++i) {
696 switch (decl[i].type) {
697 case isl_arg_version:
698 return 1;
699 case isl_arg_child:
700 if (any_version(decl[i].u.child.child->args))
701 return 1;
702 break;
703 default:
704 break;
708 return 0;
711 static void print_help_and_exit(struct isl_arg *arg, const char *prog,
712 void *opt)
714 int i;
715 struct isl_prefixes prefixes = { 0 };
717 printf("Usage: %s [OPTION...]", prog_name(prog));
719 for (i = 0; arg[i].type != isl_arg_end; ++i)
720 if (arg[i].type == isl_arg_arg)
721 printf(" %s", arg[i].argument_name);
723 printf("\n\n");
725 print_help(arg, &prefixes, opt);
726 printf("\n");
727 if (any_version(arg))
728 printf(" -V, --version\n");
729 print_bool_help(help_arg, NULL, NULL);
731 for (i = 0; arg[i].type != isl_arg_end; ++i) {
732 if (arg[i].type != isl_arg_footer)
733 continue;
734 wrap_msg(arg[i].help_msg, 0, 0);
735 printf("\n");
738 exit(0);
741 static int match_long_name(struct isl_arg *decl,
742 const char *start, const char *end)
744 do {
745 if (end - start == strlen(decl->long_name) &&
746 !strncmp(start, decl->long_name, end - start))
747 return 1;
748 } while ((++decl)->type == isl_arg_alias);
750 return 0;
753 static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
755 if (!strncmp(arg, "--", 2))
756 return arg + 2;
757 if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
758 return arg + 1;
759 return NULL;
762 static const char *skip_name(struct isl_arg *decl, const char *arg,
763 struct isl_prefixes *prefixes, int need_argument, int *has_argument)
765 const char *equal;
766 const char *name;
767 const char *end;
769 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
770 if (need_argument && !arg[2])
771 return NULL;
772 if (has_argument)
773 *has_argument = arg[2] != '\0';
774 return arg + 2;
776 if (!decl->long_name)
777 return NULL;
779 name = skip_dash_dash(decl, arg);
780 if (!name)
781 return NULL;
783 equal = strchr(name, '=');
784 if (need_argument && !equal)
785 return NULL;
787 if (has_argument)
788 *has_argument = !!equal;
789 end = equal ? equal : name + strlen(name);
791 name = skip_prefixes(name, prefixes, NULL);
793 if (!match_long_name(decl, name, end))
794 return NULL;
796 return equal ? equal + 1 : end;
799 static int parse_choice_option(struct isl_arg *decl, char **arg,
800 struct isl_prefixes *prefixes, void *opt)
802 int i;
803 int has_argument;
804 const char *choice;
806 choice = skip_name(decl, arg[0], prefixes, 0, &has_argument);
807 if (!choice)
808 return 0;
810 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
811 unsigned u = decl->u.choice.default_selected;
812 *(unsigned *)(((char *)opt) + decl->offset) = u;
813 if (decl->u.choice.set)
814 decl->u.choice.set(opt, u);
816 return 1;
819 if (!has_argument)
820 choice = arg[1];
822 for (i = 0; decl->u.choice.choice[i].name; ++i) {
823 unsigned u;
825 if (strcmp(choice, decl->u.choice.choice[i].name))
826 continue;
828 u = decl->u.choice.choice[i].value;
829 *(unsigned *)(((char *)opt) + decl->offset) = u;
830 if (decl->u.choice.set)
831 decl->u.choice.set(opt, u);
833 return has_argument ? 1 : 2;
836 return 0;
839 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
840 size_t len)
842 int i;
844 for (i = 0; decl->u.flags.flags[i].name; ++i) {
845 if (strncmp(flag, decl->u.flags.flags[i].name, len))
846 continue;
848 *val &= ~decl->u.flags.flags[i].mask;
849 *val |= decl->u.flags.flags[i].value;
851 return 1;
854 return 0;
857 static int parse_flags_option(struct isl_arg *decl, char **arg,
858 struct isl_prefixes *prefixes, void *opt)
860 int has_argument;
861 const char *flags;
862 const char *comma;
863 unsigned val;
865 flags = skip_name(decl, arg[0], prefixes, 0, &has_argument);
866 if (!flags)
867 return 0;
869 if (!has_argument && !arg[1])
870 return 0;
872 if (!has_argument)
873 flags = arg[1];
875 val = 0;
877 while ((comma = strchr(flags, ',')) != NULL) {
878 if (!set_flag(decl, &val, flags, comma - flags))
879 return 0;
880 flags = comma + 1;
882 if (!set_flag(decl, &val, flags, strlen(flags)))
883 return 0;
885 *(unsigned *)(((char *)opt) + decl->offset) = val;
887 return has_argument ? 1 : 2;
890 static int parse_bool_option(struct isl_arg *decl, char **arg,
891 struct isl_prefixes *prefixes, void *opt)
893 const char *name;
894 unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
895 int next_prefix;
897 if (skip_name(decl, arg[0], prefixes, 0, NULL)) {
898 if ((decl->flags & ISL_ARG_BOOL_ARG) && arg[1]) {
899 char *endptr;
900 int val = strtol(arg[1], &endptr, 0);
901 if (*endptr == '\0' && (val == 0 || val == 1)) {
902 if (decl->offset != (size_t) -1)
903 *p = val;
904 if (decl->u.b.set)
905 decl->u.b.set(opt, val);
906 return 2;
909 if (decl->offset != (size_t) -1)
910 *p = 1;
911 if (decl->u.b.set)
912 decl->u.b.set(opt, 1);
914 return 1;
917 if (!decl->long_name)
918 return 0;
920 name = skip_dash_dash(decl, arg[0]);
921 if (!name)
922 return 0;
924 next_prefix = 0;
925 name = skip_prefixes(name, prefixes, &next_prefix);
927 if (strncmp(name, "no-", 3))
928 return 0;
929 name += 3;
931 name = skip_prefixes(name, prefixes, &next_prefix);
933 if (match_long_name(decl, name, name + strlen(name))) {
934 if (decl->offset != (size_t) -1)
935 *p = 0;
936 if (decl->u.b.set)
937 decl->u.b.set(opt, 0);
939 return 1;
942 return 0;
945 static int parse_str_option(struct isl_arg *decl, char **arg,
946 struct isl_prefixes *prefixes, void *opt)
948 int has_argument;
949 const char *s;
950 char **p = (char **)(((char *)opt) + decl->offset);
952 s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
953 if (!s)
954 return 0;
956 if (has_argument) {
957 free(*p);
958 *p = strdup(s);
959 return 1;
962 if (arg[1]) {
963 free(*p);
964 *p = strdup(arg[1]);
965 return 2;
968 return 0;
971 static int isl_arg_str_list_append(struct isl_arg *decl, void *opt,
972 const char *s)
974 int *n = (int *)(((char *) opt) + decl->u.str_list.offset_n);
975 char **list = *(char ***)(((char *) opt) + decl->offset);
977 list = realloc(list, (*n + 1) * sizeof(char *));
978 if (!list)
979 return -1;
980 *(char ***)(((char *) opt) + decl->offset) = list;
981 list[*n] = strdup(s);
982 (*n)++;
983 return 0;
986 static int parse_str_list_option(struct isl_arg *decl, char **arg,
987 struct isl_prefixes *prefixes, void *opt)
989 int has_argument;
990 const char *s;
992 s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
993 if (!s)
994 return 0;
996 if (has_argument) {
997 isl_arg_str_list_append(decl, opt, s);
998 return 1;
1001 if (arg[1]) {
1002 isl_arg_str_list_append(decl, opt, arg[1]);
1003 return 2;
1006 return 0;
1009 static int parse_int_option(struct isl_arg *decl, char **arg,
1010 struct isl_prefixes *prefixes, void *opt)
1012 int has_argument;
1013 const char *val;
1014 char *endptr;
1015 int *p = (int *)(((char *)opt) + decl->offset);
1017 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1018 if (!val)
1019 return 0;
1021 if (has_argument) {
1022 *p = atoi(val);
1023 return 1;
1026 if (arg[1]) {
1027 int i = strtol(arg[1], &endptr, 0);
1028 if (*endptr == '\0') {
1029 *p = i;
1030 return 2;
1034 return 0;
1037 static int parse_long_option(struct isl_arg *decl, char **arg,
1038 struct isl_prefixes *prefixes, void *opt)
1040 int has_argument;
1041 const char *val;
1042 char *endptr;
1043 long *p = (long *)(((char *)opt) + decl->offset);
1045 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1046 if (!val)
1047 return 0;
1049 if (has_argument) {
1050 long l = strtol(val, NULL, 0);
1051 *p = l;
1052 if (decl->u.l.set)
1053 decl->u.l.set(opt, l);
1054 return 1;
1057 if (arg[1]) {
1058 long l = strtol(arg[1], &endptr, 0);
1059 if (*endptr == '\0') {
1060 *p = l;
1061 if (decl->u.l.set)
1062 decl->u.l.set(opt, l);
1063 return 2;
1067 if (decl->u.l.default_value != decl->u.l.default_selected) {
1068 *p = decl->u.l.default_selected;
1069 if (decl->u.l.set)
1070 decl->u.l.set(opt, decl->u.l.default_selected);
1071 return 1;
1074 return 0;
1077 static int parse_ulong_option(struct isl_arg *decl, char **arg,
1078 struct isl_prefixes *prefixes, void *opt)
1080 int has_argument;
1081 const char *val;
1082 char *endptr;
1083 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
1085 val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
1086 if (!val)
1087 return 0;
1089 if (has_argument) {
1090 *p = strtoul(val, NULL, 0);
1091 return 1;
1094 if (arg[1]) {
1095 unsigned long ul = strtoul(arg[1], &endptr, 0);
1096 if (*endptr == '\0') {
1097 *p = ul;
1098 return 2;
1102 return 0;
1105 static int parse_option(struct isl_arg *decl, char **arg,
1106 struct isl_prefixes *prefixes, void *opt);
1108 static int parse_child_option(struct isl_arg *decl, char **arg,
1109 struct isl_prefixes *prefixes, void *opt)
1111 void *child;
1112 int first, parsed;
1114 if (decl->offset == (size_t) -1)
1115 child = opt;
1116 else
1117 child = *(void **)(((char *)opt) + decl->offset);
1119 first = add_prefix(prefixes, decl->long_name);
1120 parsed = parse_option(decl->u.child.child->args, arg, prefixes, child);
1121 drop_prefix(prefixes, first);
1123 return parsed;
1126 static int parse_option(struct isl_arg *decl, char **arg,
1127 struct isl_prefixes *prefixes, void *opt)
1129 int i;
1131 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1132 int parsed = 0;
1133 switch (decl[i].type) {
1134 case isl_arg_choice:
1135 parsed = parse_choice_option(&decl[i], arg,
1136 prefixes, opt);
1137 break;
1138 case isl_arg_flags:
1139 parsed = parse_flags_option(&decl[i], arg,
1140 prefixes, opt);
1141 break;
1142 case isl_arg_int:
1143 parsed = parse_int_option(&decl[i], arg, prefixes, opt);
1144 break;
1145 case isl_arg_long:
1146 parsed = parse_long_option(&decl[i], arg,
1147 prefixes, opt);
1148 break;
1149 case isl_arg_ulong:
1150 parsed = parse_ulong_option(&decl[i], arg,
1151 prefixes, opt);
1152 break;
1153 case isl_arg_bool:
1154 parsed = parse_bool_option(&decl[i], arg,
1155 prefixes, opt);
1156 break;
1157 case isl_arg_str:
1158 parsed = parse_str_option(&decl[i], arg, prefixes, opt);
1159 break;
1160 case isl_arg_str_list:
1161 parsed = parse_str_list_option(&decl[i], arg, prefixes,
1162 opt);
1163 break;
1164 case isl_arg_child:
1165 parsed = parse_child_option(&decl[i], arg,
1166 prefixes, opt);
1167 break;
1168 case isl_arg_alias:
1169 case isl_arg_arg:
1170 case isl_arg_footer:
1171 case isl_arg_user:
1172 case isl_arg_version:
1173 case isl_arg_end:
1174 break;
1176 if (parsed)
1177 return parsed;
1180 return 0;
1183 static void print_version(struct isl_arg *decl)
1185 int i;
1187 for (i = 0; decl[i].type != isl_arg_end; ++i) {
1188 switch (decl[i].type) {
1189 case isl_arg_version:
1190 decl[i].u.version.print_version();
1191 break;
1192 case isl_arg_child:
1193 print_version(decl[i].u.child.child->args);
1194 break;
1195 default:
1196 break;
1201 static void print_version_and_exit(struct isl_arg *decl)
1203 print_version(decl);
1205 exit(0);
1208 static int drop_argument(int argc, char **argv, int drop, int n)
1210 for (; drop + n < argc; ++drop)
1211 argv[drop] = argv[drop + n];
1213 return argc - n;
1216 static int n_arg(struct isl_arg *arg)
1218 int i;
1219 int n_arg = 0;
1221 for (i = 0; arg[i].type != isl_arg_end; ++i)
1222 if (arg[i].type == isl_arg_arg)
1223 n_arg++;
1225 return n_arg;
1228 static int next_arg(struct isl_arg *arg, int a)
1230 for (++a; arg[a].type != isl_arg_end; ++a)
1231 if (arg[a].type == isl_arg_arg)
1232 return a;
1234 return -1;
1237 /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
1238 * equal to "--help" and if so call print_help_and_exit.
1240 static void check_help(struct isl_args *args, char *arg, char *prog, void *opt,
1241 unsigned flags)
1243 if (ISL_FL_ISSET(flags, ISL_ARG_SKIP_HELP))
1244 return;
1246 if (strcmp(arg, "--help") == 0)
1247 print_help_and_exit(args->args, prog, opt);
1250 int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
1251 unsigned flags)
1253 int a = -1;
1254 int skip = 0;
1255 int i;
1256 int n;
1257 struct isl_prefixes prefixes = { 0 };
1259 n = n_arg(args->args);
1261 for (i = 1; i < argc; ++i) {
1262 if ((strcmp(argv[i], "--version") == 0 ||
1263 strcmp(argv[i], "-V") == 0) && any_version(args->args))
1264 print_version_and_exit(args->args);
1267 while (argc > 1 + skip) {
1268 int parsed;
1269 if (argv[1 + skip][0] != '-') {
1270 a = next_arg(args->args, a);
1271 if (a >= 0) {
1272 char **p;
1273 p = (char **)(((char *)opt)+args->args[a].offset);
1274 free(*p);
1275 *p = strdup(argv[1 + skip]);
1276 argc = drop_argument(argc, argv, 1 + skip, 1);
1277 --n;
1278 } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1279 fprintf(stderr, "%s: extra argument: %s\n",
1280 prog_name(argv[0]), argv[1 + skip]);
1281 exit(-1);
1282 } else
1283 ++skip;
1284 continue;
1286 check_help(args, argv[1 + skip], argv[0], opt, flags);
1287 parsed = parse_option(args->args, &argv[1 + skip],
1288 &prefixes, opt);
1289 if (parsed)
1290 argc = drop_argument(argc, argv, 1 + skip, parsed);
1291 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
1292 fprintf(stderr, "%s: unrecognized option: %s\n",
1293 prog_name(argv[0]), argv[1 + skip]);
1294 exit(-1);
1295 } else
1296 ++skip;
1299 if (n > 0) {
1300 fprintf(stderr, "%s: expecting %d more argument(s)\n",
1301 prog_name(argv[0]), n);
1302 exit(-1);
1305 return argc;