isl_basic_set_swap_vars: finalize result
[isl.git] / isl_arg.c
blob17c67ffd1a911f5e167b7c41ea3e4b2bf823e4a8
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 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 void set_default_choice(struct isl_arg *arg, void *opt)
19 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.choice.default_value;
22 static void set_default_flags(struct isl_arg *arg, void *opt)
24 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.flags.default_value;
27 static void set_default_bool(struct isl_arg *arg, void *opt)
29 *(unsigned *)(((char *)opt) + arg->offset) = arg->u.b.default_value;
32 static void set_default_child(struct isl_arg *arg, void *opt)
34 void *child = calloc(1, arg->u.child.size);
36 if (child)
37 isl_arg_set_defaults(arg->u.child.child, child);
39 *(void **)(((char *)opt) + arg->offset) = child;
42 static void set_default_user(struct isl_arg *arg, void *opt)
44 arg->u.user.init(((char *)opt) + arg->offset);
47 static void set_default_long(struct isl_arg *arg, void *opt)
49 *(long *)(((char *)opt) + arg->offset) = arg->u.l.default_value;
52 static void set_default_ulong(struct isl_arg *arg, void *opt)
54 *(unsigned long *)(((char *)opt) + arg->offset) = arg->u.ul.default_value;
57 static void set_default_str(struct isl_arg *arg, void *opt)
59 const char *str = NULL;
60 if (arg->u.str.default_value)
61 str = strdup(arg->u.str.default_value);
62 *(const char **)(((char *)opt) + arg->offset) = str;
65 void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
67 int i;
69 for (i = 0; arg[i].type != isl_arg_end; ++i) {
70 switch (arg[i].type) {
71 case isl_arg_choice:
72 set_default_choice(&arg[i], opt);
73 break;
74 case isl_arg_flags:
75 set_default_flags(&arg[i], opt);
76 break;
77 case isl_arg_bool:
78 set_default_bool(&arg[i], opt);
79 break;
80 case isl_arg_child:
81 set_default_child(&arg[i], opt);
82 break;
83 case isl_arg_user:
84 set_default_user(&arg[i], opt);
85 break;
86 case isl_arg_long:
87 set_default_long(&arg[i], opt);
88 break;
89 case isl_arg_ulong:
90 set_default_ulong(&arg[i], opt);
91 break;
92 case isl_arg_arg:
93 case isl_arg_str:
94 set_default_str(&arg[i], opt);
95 break;
96 case isl_arg_version:
97 case isl_arg_end:
98 break;
103 void isl_arg_free(struct isl_arg *arg, void *opt)
105 int i;
107 if (!opt)
108 return;
110 for (i = 0; arg[i].type != isl_arg_end; ++i) {
111 switch (arg[i].type) {
112 case isl_arg_child:
113 isl_arg_free(arg[i].u.child.child,
114 *(void **)(((char *)opt) + arg[i].offset));
115 break;
116 case isl_arg_arg:
117 case isl_arg_str:
118 free(*(char **)(((char *)opt) + arg[i].offset));
119 break;
120 case isl_arg_user:
121 if (arg[i].u.user.clear)
122 arg[i].u.user.clear(((char *)opt) + arg[i].offset);
123 break;
124 case isl_arg_bool:
125 case isl_arg_choice:
126 case isl_arg_flags:
127 case isl_arg_long:
128 case isl_arg_ulong:
129 case isl_arg_version:
130 case isl_arg_end:
131 break;
135 free(opt);
138 static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
140 int len = 0;
142 if (!decl->long_name) {
143 printf(" -%c", decl->short_name);
144 return 4;
147 if (decl->short_name)
148 printf(" -%c, --", decl->short_name);
149 else
150 printf(" --");
151 len += 8;
153 if (prefix) {
154 printf("%s-", prefix);
155 len += strlen(prefix) + 1;
157 if (no) {
158 printf("no-");
159 len += 3;
161 printf("%s", decl->long_name);
162 len += strlen(decl->long_name);
164 return len;
167 const void *isl_memrchr(const void *s, int c, size_t n)
169 const char *p = s;
170 while (n-- > 0)
171 if (p[n] == c)
172 return p + n;
173 return NULL;
176 static int print_help_msg(struct isl_arg *decl, int pos)
178 int len;
179 const char *s;
181 if (!decl->help_msg)
182 return pos;
184 if (pos >= 29)
185 printf("\n%30s", "");
186 else
187 printf("%*s", 30 - pos, "");
189 s = decl->help_msg;
190 len = strlen(s);
191 while (len > 45) {
192 const char *space = isl_memrchr(s, ' ', 45);
193 int l;
195 if (!space)
196 space = strchr(s + 45, ' ');
197 if (!space)
198 break;
199 l = space - s;
200 printf("%.*s", l, s);
201 s = space + 1;
202 len -= l + 1;
203 printf("\n%30s", "");
206 printf("%s", s);
207 return len;
210 static void print_default_choice(struct isl_arg *decl, int pos)
212 int i;
213 const char *default_prefix = "[default: ";
214 const char *default_suffix = "]";
215 const char *s = "none";
216 int len;
218 for (i = 0; decl->u.choice.choice[i].name; ++i)
219 if (decl->u.choice.choice[i].value == decl->u.choice.default_value) {
220 s = decl->u.choice.choice[i].name;
221 break;
223 len = strlen(default_prefix) + strlen(s) + strlen(default_suffix);
225 if (!decl->help_msg) {
226 if (pos >= 29)
227 printf("\n%30s", "");
228 else
229 printf("%*s", 30 - pos, "");
230 pos = 0;
231 } else {
232 if (pos + len >= 48)
233 printf("\n%30s", "");
234 else
235 printf(" ");
237 printf("%s%s%s", default_prefix, s, default_suffix);
240 static void print_choice_help(struct isl_arg *decl, const char *prefix)
242 int i;
243 int pos;
245 pos = print_arg_help(decl, prefix, 0);
246 printf("=");
247 pos++;
249 for (i = 0; decl->u.choice.choice[i].name; ++i) {
250 if (i) {
251 printf("|");
252 pos++;
254 printf("%s", decl->u.choice.choice[i].name);
255 pos += strlen(decl->u.choice.choice[i].name);
258 pos = print_help_msg(decl, pos);
259 print_default_choice(decl, pos);
261 printf("\n");
264 static void print_default_flags(struct isl_arg *decl, int pos)
266 int i, first;
267 const char *default_prefix = "[default: ";
268 const char *default_suffix = "]";
269 int len = strlen(default_prefix) + strlen(default_suffix);
271 for (i = 0; decl->u.flags.flags[i].name; ++i)
272 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
273 decl->u.flags.flags[i].value)
274 len += strlen(decl->u.flags.flags[i].name);
276 if (!decl->help_msg) {
277 if (pos >= 29)
278 printf("\n%30s", "");
279 else
280 printf("%*s", 30 - pos, "");
281 pos = 0;
282 } else {
283 if (pos + len >= 48)
284 printf("\n%30s", "");
285 else
286 printf(" ");
288 printf("%s", default_prefix);
290 for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
291 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
292 decl->u.flags.flags[i].value) {
293 if (!first)
294 printf(",");
295 printf("%s", decl->u.flags.flags[i].name);
296 first = 0;
299 printf("%s", default_suffix);
302 static void print_flags_help(struct isl_arg *decl, const char *prefix)
304 int i, j;
305 int pos;
307 pos = print_arg_help(decl, prefix, 0);
308 printf("=");
309 pos++;
311 for (i = 0; decl->u.flags.flags[i].name; ++i) {
312 if (i) {
313 printf(",");
314 pos++;
316 for (j = i;
317 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
318 ++j) {
319 if (j != i) {
320 printf("|");
321 pos++;
323 printf("%s", decl->u.flags.flags[j].name);
324 pos += strlen(decl->u.flags.flags[j].name);
326 i = j - 1;
329 pos = print_help_msg(decl, pos);
330 print_default_flags(decl, pos);
332 printf("\n");
335 static void print_bool_help(struct isl_arg *decl, const char *prefix)
337 int pos;
338 int no = decl->u.b.default_value == 1;
339 pos = print_arg_help(decl, prefix, no);
340 print_help_msg(decl, pos);
341 printf("\n");
344 static void print_long_help(struct isl_arg *decl, const char *prefix)
346 int pos;
347 pos = print_arg_help(decl, prefix, 0);
348 if (decl->u.l.default_value != decl->u.l.default_selected) {
349 printf("[");
350 pos++;
352 printf("=long");
353 pos += 5;
354 if (decl->u.l.default_value != decl->u.l.default_selected) {
355 printf("]");
356 pos++;
358 print_help_msg(decl, pos);
359 printf("\n");
362 static void print_ulong_help(struct isl_arg *decl, const char *prefix)
364 int pos;
365 pos = print_arg_help(decl, prefix, 0);
366 printf("=ulong");
367 pos += 6;
368 print_help_msg(decl, pos);
369 printf("\n");
372 static void print_str_help(struct isl_arg *decl, const char *prefix)
374 int pos;
375 const char *a = decl->argument_name ? decl->argument_name : "string";
376 pos = print_arg_help(decl, prefix, 0);
377 printf("=%s", a);
378 pos += 1 + strlen(a);
379 print_help_msg(decl, pos);
380 printf("\n");
383 static void print_help(struct isl_arg *arg, const char *prefix)
385 int i;
387 for (i = 0; arg[i].type != isl_arg_end; ++i) {
388 switch (arg[i].type) {
389 case isl_arg_flags:
390 print_flags_help(&arg[i], prefix);
391 break;
392 case isl_arg_choice:
393 print_choice_help(&arg[i], prefix);
394 break;
395 case isl_arg_bool:
396 print_bool_help(&arg[i], prefix);
397 break;
398 case isl_arg_long:
399 print_long_help(&arg[i], prefix);
400 break;
401 case isl_arg_ulong:
402 print_ulong_help(&arg[i], prefix);
403 break;
404 case isl_arg_str:
405 print_str_help(&arg[i], prefix);
406 break;
407 case isl_arg_version:
408 printf(" -V, --version\n");
409 break;
410 case isl_arg_arg:
411 case isl_arg_child:
412 case isl_arg_user:
413 case isl_arg_end:
414 break;
418 for (i = 0; arg[i].type != isl_arg_end; ++i) {
419 if (arg[i].type != isl_arg_child)
420 continue;
422 printf("\n");
423 if (arg[i].help_msg)
424 printf(" %s\n", arg[i].help_msg);
425 print_help(arg[i].u.child.child, arg[i].long_name);
429 static const char *prog_name(const char *prog)
431 const char *slash;
433 slash = strrchr(prog, '/');
434 if (slash)
435 prog = slash + 1;
436 if (strncmp(prog, "lt-", 3) == 0)
437 prog += 3;
439 return prog;
442 static void print_help_and_exit(struct isl_arg *arg, const char *prog)
444 int i;
446 printf("Usage: %s [OPTION...]", prog_name(prog));
448 for (i = 0; arg[i].type != isl_arg_end; ++i)
449 if (arg[i].type == isl_arg_arg)
450 printf(" %s", arg[i].argument_name);
452 printf("\n\n");
454 print_help(arg, NULL);
456 exit(0);
459 static const char *skip_name(struct isl_arg *decl, const char *arg,
460 const char *prefix, int need_argument, int *has_argument)
462 const char *equal;
463 const char *name;
464 const char *end;
466 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
467 if (need_argument && !arg[2])
468 return NULL;
469 if (has_argument)
470 *has_argument = arg[2] != '\0';
471 return arg + 2;
474 if (strncmp(arg, "--", 2))
475 return NULL;
477 name = arg + 2;
478 equal = strchr(name, '=');
479 if (need_argument && !equal)
480 return NULL;
482 if (has_argument)
483 *has_argument = !!equal;
484 end = equal ? equal : name + strlen(name);
486 if (prefix) {
487 size_t prefix_len = strlen(prefix);
488 if (strncmp(name, prefix, prefix_len) == 0 &&
489 name[prefix_len] == '-')
490 name += prefix_len + 1;
493 if (end - name != strlen(decl->long_name) ||
494 strncmp(name, decl->long_name, end - name))
495 return NULL;
497 return equal ? equal + 1 : end;
500 static int parse_choice_option(struct isl_arg *decl, char **arg,
501 const char *prefix, void *opt)
503 int i;
504 int has_argument;
505 const char *choice;
507 choice = skip_name(decl, arg[0], prefix, 0, &has_argument);
508 if (!choice)
509 return 0;
511 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
512 unsigned u = decl->u.choice.default_selected;
513 if (decl->u.choice.set)
514 decl->u.choice.set(opt, u);
515 else
516 *(unsigned *)(((char *)opt) + decl->offset) = u;
518 return 1;
521 if (!has_argument)
522 choice = arg[1];
524 for (i = 0; decl->u.choice.choice[i].name; ++i) {
525 unsigned u;
527 if (strcmp(choice, decl->u.choice.choice[i].name))
528 continue;
530 u = decl->u.choice.choice[i].value;
531 if (decl->u.choice.set)
532 decl->u.choice.set(opt, u);
533 else
534 *(unsigned *)(((char *)opt) + decl->offset) = u;
536 return has_argument ? 1 : 2;
539 return 0;
542 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
543 size_t len)
545 int i;
547 for (i = 0; decl->u.flags.flags[i].name; ++i) {
548 if (strncmp(flag, decl->u.flags.flags[i].name, len))
549 continue;
551 *val &= ~decl->u.flags.flags[i].mask;
552 *val |= decl->u.flags.flags[i].value;
554 return 1;
557 return 0;
560 static int parse_flags_option(struct isl_arg *decl, char **arg,
561 const char *prefix, void *opt)
563 int has_argument;
564 const char *flags;
565 const char *comma;
566 unsigned val;
568 flags = skip_name(decl, arg[0], prefix, 0, &has_argument);
569 if (!flags)
570 return 0;
572 if (!has_argument && !arg[1])
573 return 0;
575 if (!has_argument)
576 flags = arg[1];
578 val = *(unsigned *)(((char *)opt) + decl->offset);
580 while ((comma = strchr(flags, ',')) != NULL) {
581 if (!set_flag(decl, &val, flags, comma - flags))
582 return 0;
583 flags = comma + 1;
585 if (!set_flag(decl, &val, flags, strlen(flags)))
586 return 0;
588 *(unsigned *)(((char *)opt) + decl->offset) = val;
590 return has_argument ? 1 : 2;
593 static int parse_bool_option(struct isl_arg *decl, const char *arg,
594 const char *prefix, void *opt)
596 if (skip_name(decl, arg, prefix, 0, NULL)) {
597 *(unsigned *)(((char *)opt) + decl->offset) = 1;
599 return 1;
602 if (strncmp(arg, "--no-", 5))
603 return 0;
604 arg += 5;
606 if (prefix) {
607 size_t prefix_len = strlen(prefix);
608 if (strncmp(arg, prefix, prefix_len) == 0 &&
609 arg[prefix_len] == '-')
610 arg += prefix_len + 1;
613 if (!strcmp(arg, decl->long_name)) {
614 *(unsigned *)(((char *)opt) + decl->offset) = 0;
616 return 1;
619 return 0;
622 static int parse_str_option(struct isl_arg *decl, char **arg,
623 const char *prefix, void *opt)
625 int has_argument;
626 const char *s;
627 char **p = (char **)(((char *)opt) + decl->offset);
629 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
630 if (!s)
631 return 0;
633 if (has_argument) {
634 *p = strdup(s);
635 return 1;
638 if (arg[1]) {
639 *p = strdup(arg[1]);
640 return 2;
643 return 0;
646 static int parse_long_option(struct isl_arg *decl, char **arg,
647 const char *prefix, void *opt)
649 int has_argument;
650 const char *val;
651 char *endptr;
652 long *p = (long *)(((char *)opt) + decl->offset);
654 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
655 if (!val)
656 return 0;
658 if (has_argument) {
659 long l = strtol(val, NULL, 0);
660 if (decl->u.l.set)
661 decl->u.l.set(opt, l);
662 else
663 *p = l;
664 return 1;
667 if (arg[1]) {
668 long l = strtol(arg[1], &endptr, 0);
669 if (*endptr == '\0') {
670 if (decl->u.l.set)
671 decl->u.l.set(opt, l);
672 else
673 *p = l;
674 return 2;
678 if (decl->u.l.default_value != decl->u.l.default_selected) {
679 if (decl->u.l.set)
680 decl->u.l.set(opt, decl->u.l.default_selected);
681 else
682 *p = decl->u.l.default_selected;
683 return 1;
686 return 0;
689 static int parse_ulong_option(struct isl_arg *decl, char **arg,
690 const char *prefix, void *opt)
692 int has_argument;
693 const char *val;
694 char *endptr;
695 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
697 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
698 if (!val)
699 return 0;
701 if (has_argument) {
702 *p = strtoul(val, NULL, 0);
703 return 1;
706 if (arg[1]) {
707 unsigned long ul = strtoul(arg[1], &endptr, 0);
708 if (*endptr == '\0') {
709 *p = ul;
710 return 2;
714 return 0;
717 static int parse_option(struct isl_arg *decl, char **arg,
718 const char *prefix, void *opt);
720 static int parse_child_option(struct isl_arg *decl, char **arg, void *opt)
722 return parse_option(decl->u.child.child, arg, decl->long_name,
723 *(void **)(((char *)opt) + decl->offset));
726 static int parse_option(struct isl_arg *decl, char **arg,
727 const char *prefix, void *opt)
729 int i;
731 for (i = 0; decl[i].type != isl_arg_end; ++i) {
732 int parsed = 0;
733 switch (decl[i].type) {
734 case isl_arg_choice:
735 parsed = parse_choice_option(&decl[i], arg, prefix, opt);
736 break;
737 case isl_arg_flags:
738 parsed = parse_flags_option(&decl[i], arg, prefix, opt);
739 break;
740 case isl_arg_long:
741 parsed = parse_long_option(&decl[i], arg, prefix, opt);
742 break;
743 case isl_arg_ulong:
744 parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
745 break;
746 case isl_arg_bool:
747 parsed = parse_bool_option(&decl[i], *arg, prefix, opt);
748 break;
749 case isl_arg_str:
750 parsed = parse_str_option(&decl[i], arg, prefix, opt);
751 break;
752 case isl_arg_child:
753 parsed = parse_child_option(&decl[i], arg, opt);
754 break;
755 case isl_arg_arg:
756 case isl_arg_user:
757 case isl_arg_version:
758 case isl_arg_end:
759 break;
761 if (parsed)
762 return parsed;
765 return 0;
768 static int any_version(struct isl_arg *decl)
770 int i;
772 for (i = 0; decl[i].type != isl_arg_end; ++i) {
773 switch (decl[i].type) {
774 case isl_arg_version:
775 return 1;
776 case isl_arg_child:
777 if (any_version(decl[i].u.child.child))
778 return 1;
779 break;
780 default:
781 break;
785 return 0;
788 static void print_version(struct isl_arg *decl)
790 int i;
792 for (i = 0; decl[i].type != isl_arg_end; ++i) {
793 switch (decl[i].type) {
794 case isl_arg_version:
795 decl[i].u.version.print_version();
796 break;
797 case isl_arg_child:
798 print_version(decl[i].u.child.child);
799 break;
800 default:
801 break;
806 static void print_version_and_exit(struct isl_arg *decl)
808 print_version(decl);
810 exit(0);
813 static int drop_argument(int argc, char **argv, int drop, int n)
815 for (; drop < argc; ++drop)
816 argv[drop] = argv[drop + n];
818 return argc - n;
821 static int n_arg(struct isl_arg *arg)
823 int i;
824 int n_arg = 0;
826 for (i = 0; arg[i].type != isl_arg_end; ++i)
827 if (arg[i].type == isl_arg_arg)
828 n_arg++;
830 return n_arg;
833 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
834 unsigned flags)
836 int skip = 0;
837 int i;
838 int n;
840 n = n_arg(arg);
842 for (i = 1; i < argc; ++i) {
843 if (strcmp(argv[i], "--help") == 0)
844 print_help_and_exit(arg, argv[0]);
847 for (i = 1; i < argc; ++i) {
848 if ((strcmp(argv[i], "--version") == 0 ||
849 strcmp(argv[i], "-V") == 0) && any_version(arg))
850 print_version_and_exit(arg);
853 while (argc > 1 + skip) {
854 int parsed;
855 if (argv[1 + skip][0] != '-')
856 break;
857 parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
858 if (parsed)
859 argc = drop_argument(argc, argv, 1 + skip, parsed);
860 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
861 fprintf(stderr, "%s: unrecognized option: %s\n",
862 prog_name(argv[0]), argv[1 + skip]);
863 exit(-1);
864 } else
865 ++skip;
868 if (ISL_FL_ISSET(flags, ISL_ARG_ALL) ? argc != 1 + n
869 : argc < 1 + skip + n) {
870 fprintf(stderr, "%s: expecting %d arguments\n",
871 prog_name(argv[0]), n);
872 exit(-1);
875 for (i = 0; arg[i].type != isl_arg_end; ++i) {
876 const char *str;
877 if (arg[i].type != isl_arg_arg)
878 continue;
879 str = strdup(argv[1 + skip]);
880 *(const char **)(((char *)opt) + arg[i].offset) = str;
881 argc = drop_argument(argc, argv, 1 + skip, 1);
884 return argc;