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
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);
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)
69 for (i = 0; arg[i].type != isl_arg_end; ++i) {
70 switch (arg[i].type) {
72 set_default_choice(&arg[i], opt);
75 set_default_flags(&arg[i], opt);
78 set_default_bool(&arg[i], opt);
81 set_default_child(&arg[i], opt);
84 set_default_user(&arg[i], opt);
87 set_default_long(&arg[i], opt);
90 set_default_ulong(&arg[i], opt);
94 set_default_str(&arg[i], opt);
103 void isl_arg_free(struct isl_arg *arg, void *opt)
110 for (i = 0; arg[i].type != isl_arg_end; ++i) {
111 switch (arg[i].type) {
113 isl_arg_free(arg[i].u.child.child,
114 *(void **)(((char *)opt) + arg[i].offset));
118 free(*(char **)(((char *)opt) + arg[i].offset));
121 if (arg[i].u.user.clear)
122 arg[i].u.user.clear(((char *)opt) + arg[i].offset);
129 case isl_arg_version:
138 static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
142 if (!decl->long_name) {
143 printf(" -%c", decl->short_name);
147 if (decl->short_name)
148 printf(" -%c, --", decl->short_name);
154 printf("%s-", prefix);
155 len += strlen(prefix) + 1;
161 printf("%s", decl->long_name);
162 len += strlen(decl->long_name);
167 const void *isl_memrchr(const void *s, int c, size_t n)
176 static int print_help_msg(struct isl_arg *decl, int pos)
185 printf("\n%30s", "");
187 printf("%*s", 30 - pos, "");
192 const char *space = isl_memrchr(s, ' ', 45);
196 space = strchr(s + 45, ' ');
200 printf("%.*s", l, s);
203 printf("\n%30s", "");
210 static void print_default_choice(struct isl_arg *decl, int pos)
213 const char *default_prefix = "[default: ";
214 const char *default_suffix = "]";
215 const char *s = "none";
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;
223 len = strlen(default_prefix) + strlen(s) + strlen(default_suffix);
225 if (!decl->help_msg) {
227 printf("\n%30s", "");
229 printf("%*s", 30 - pos, "");
233 printf("\n%30s", "");
237 printf("%s%s%s", default_prefix, s, default_suffix);
240 static void print_choice_help(struct isl_arg *decl, const char *prefix)
245 pos = print_arg_help(decl, prefix, 0);
249 for (i = 0; decl->u.choice.choice[i].name; ++i) {
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);
264 static void print_default_flags(struct isl_arg *decl, int pos)
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) {
278 printf("\n%30s", "");
280 printf("%*s", 30 - pos, "");
284 printf("\n%30s", "");
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) {
295 printf("%s", decl->u.flags.flags[i].name);
299 printf("%s", default_suffix);
302 static void print_flags_help(struct isl_arg *decl, const char *prefix)
307 pos = print_arg_help(decl, prefix, 0);
311 for (i = 0; decl->u.flags.flags[i].name; ++i) {
317 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
323 printf("%s", decl->u.flags.flags[j].name);
324 pos += strlen(decl->u.flags.flags[j].name);
329 pos = print_help_msg(decl, pos);
330 print_default_flags(decl, pos);
335 static void print_bool_help(struct isl_arg *decl, const char *prefix)
338 int no = decl->u.b.default_value == 1;
339 pos = print_arg_help(decl, prefix, no);
340 print_help_msg(decl, pos);
344 static void print_long_help(struct isl_arg *decl, const char *prefix)
347 pos = print_arg_help(decl, prefix, 0);
348 if (decl->u.l.default_value != decl->u.l.default_selected) {
354 if (decl->u.l.default_value != decl->u.l.default_selected) {
358 print_help_msg(decl, pos);
362 static void print_ulong_help(struct isl_arg *decl, const char *prefix)
365 pos = print_arg_help(decl, prefix, 0);
368 print_help_msg(decl, pos);
372 static void print_str_help(struct isl_arg *decl, const char *prefix)
375 const char *a = decl->argument_name ? decl->argument_name : "string";
376 pos = print_arg_help(decl, prefix, 0);
378 pos += 1 + strlen(a);
379 print_help_msg(decl, pos);
383 static void print_help(struct isl_arg *arg, const char *prefix)
387 for (i = 0; arg[i].type != isl_arg_end; ++i) {
388 switch (arg[i].type) {
390 print_flags_help(&arg[i], prefix);
393 print_choice_help(&arg[i], prefix);
396 print_bool_help(&arg[i], prefix);
399 print_long_help(&arg[i], prefix);
402 print_ulong_help(&arg[i], prefix);
405 print_str_help(&arg[i], prefix);
407 case isl_arg_version:
408 printf(" -V, --version\n");
418 for (i = 0; arg[i].type != isl_arg_end; ++i) {
419 if (arg[i].type != isl_arg_child)
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)
433 slash = strrchr(prog, '/');
436 if (strncmp(prog, "lt-", 3) == 0)
442 static void print_help_and_exit(struct isl_arg *arg, const char *prog)
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);
454 print_help(arg, NULL);
459 static const char *skip_name(struct isl_arg *decl, const char *arg,
460 const char *prefix, int need_argument, int *has_argument)
466 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
467 if (need_argument && !arg[2])
470 *has_argument = arg[2] != '\0';
474 if (strncmp(arg, "--", 2))
478 equal = strchr(name, '=');
479 if (need_argument && !equal)
483 *has_argument = !!equal;
484 end = equal ? equal : name + strlen(name);
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))
497 return equal ? equal + 1 : end;
500 static int parse_choice_option(struct isl_arg *decl, char **arg,
501 const char *prefix, void *opt)
507 choice = skip_name(decl, arg[0], prefix, 0, &has_argument);
511 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
512 *(unsigned *)(((char *)opt) + decl->offset) =
513 decl->u.choice.default_selected;
521 for (i = 0; decl->u.choice.choice[i].name; ++i) {
522 if (strcmp(choice, decl->u.choice.choice[i].name))
525 *(unsigned *)(((char *)opt) + decl->offset) =
526 decl->u.choice.choice[i].value;
528 return has_argument ? 1 : 2;
534 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
539 for (i = 0; decl->u.flags.flags[i].name; ++i) {
540 if (strncmp(flag, decl->u.flags.flags[i].name, len))
543 *val &= ~decl->u.flags.flags[i].mask;
544 *val |= decl->u.flags.flags[i].value;
552 static int parse_flags_option(struct isl_arg *decl, char **arg,
553 const char *prefix, void *opt)
560 flags = skip_name(decl, arg[0], prefix, 0, &has_argument);
564 if (!has_argument && !arg[1])
570 val = *(unsigned *)(((char *)opt) + decl->offset);
572 while ((comma = strchr(flags, ',')) != NULL) {
573 if (!set_flag(decl, &val, flags, comma - flags))
577 if (!set_flag(decl, &val, flags, strlen(flags)))
580 *(unsigned *)(((char *)opt) + decl->offset) = val;
582 return has_argument ? 1 : 2;
585 static int parse_bool_option(struct isl_arg *decl, const char *arg,
586 const char *prefix, void *opt)
588 if (skip_name(decl, arg, prefix, 0, NULL)) {
589 *(unsigned *)(((char *)opt) + decl->offset) = 1;
594 if (strncmp(arg, "--no-", 5))
599 size_t prefix_len = strlen(prefix);
600 if (strncmp(arg, prefix, prefix_len) == 0 &&
601 arg[prefix_len] == '-')
602 arg += prefix_len + 1;
605 if (!strcmp(arg, decl->long_name)) {
606 *(unsigned *)(((char *)opt) + decl->offset) = 0;
614 static int parse_str_option(struct isl_arg *decl, char **arg,
615 const char *prefix, void *opt)
619 char **p = (char **)(((char *)opt) + decl->offset);
621 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
638 static int parse_long_option(struct isl_arg *decl, char **arg,
639 const char *prefix, void *opt)
644 long *p = (long *)(((char *)opt) + decl->offset);
646 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
651 long l = strtol(val, NULL, 0);
653 decl->u.l.set(opt, l);
660 long l = strtol(arg[1], &endptr, 0);
661 if (*endptr == '\0') {
663 decl->u.l.set(opt, l);
670 if (decl->u.l.default_value != decl->u.l.default_selected) {
672 decl->u.l.set(opt, decl->u.l.default_selected);
674 *p = decl->u.l.default_selected;
681 static int parse_ulong_option(struct isl_arg *decl, char **arg,
682 const char *prefix, void *opt)
687 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
689 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
694 *p = strtoul(val, NULL, 0);
699 unsigned long ul = strtoul(arg[1], &endptr, 0);
700 if (*endptr == '\0') {
709 static int parse_option(struct isl_arg *decl, char **arg,
710 const char *prefix, void *opt);
712 static int parse_child_option(struct isl_arg *decl, char **arg, void *opt)
714 return parse_option(decl->u.child.child, arg, decl->long_name,
715 *(void **)(((char *)opt) + decl->offset));
718 static int parse_option(struct isl_arg *decl, char **arg,
719 const char *prefix, void *opt)
723 for (i = 0; decl[i].type != isl_arg_end; ++i) {
725 switch (decl[i].type) {
727 parsed = parse_choice_option(&decl[i], arg, prefix, opt);
730 parsed = parse_flags_option(&decl[i], arg, prefix, opt);
733 parsed = parse_long_option(&decl[i], arg, prefix, opt);
736 parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
739 parsed = parse_bool_option(&decl[i], *arg, prefix, opt);
742 parsed = parse_str_option(&decl[i], arg, prefix, opt);
745 parsed = parse_child_option(&decl[i], arg, opt);
749 case isl_arg_version:
760 static int any_version(struct isl_arg *decl)
764 for (i = 0; decl[i].type != isl_arg_end; ++i) {
765 switch (decl[i].type) {
766 case isl_arg_version:
769 if (any_version(decl[i].u.child.child))
780 static void print_version(struct isl_arg *decl)
784 for (i = 0; decl[i].type != isl_arg_end; ++i) {
785 switch (decl[i].type) {
786 case isl_arg_version:
787 decl[i].u.version.print_version();
790 print_version(decl[i].u.child.child);
798 static void print_version_and_exit(struct isl_arg *decl)
805 static int drop_argument(int argc, char **argv, int drop, int n)
807 for (; drop < argc; ++drop)
808 argv[drop] = argv[drop + n];
813 static int n_arg(struct isl_arg *arg)
818 for (i = 0; arg[i].type != isl_arg_end; ++i)
819 if (arg[i].type == isl_arg_arg)
825 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
834 for (i = 1; i < argc; ++i) {
835 if (strcmp(argv[i], "--help") == 0)
836 print_help_and_exit(arg, argv[0]);
839 for (i = 1; i < argc; ++i) {
840 if ((strcmp(argv[i], "--version") == 0 ||
841 strcmp(argv[i], "-V") == 0) && any_version(arg))
842 print_version_and_exit(arg);
845 while (argc > 1 + skip) {
847 if (argv[1 + skip][0] != '-')
849 parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
851 argc = drop_argument(argc, argv, 1 + skip, parsed);
852 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
853 fprintf(stderr, "%s: unrecognized option: %s\n",
854 prog_name(argv[0]), argv[1 + skip]);
860 if (ISL_FL_ISSET(flags, ISL_ARG_ALL) ? argc != 1 + n
861 : argc < 1 + skip + n) {
862 fprintf(stderr, "%s: expecting %d arguments\n",
863 prog_name(argv[0]), n);
867 for (i = 0; arg[i].type != isl_arg_end; ++i) {
869 if (arg[i].type != isl_arg_arg)
871 str = strdup(argv[1 + skip]);
872 *(const char **)(((char *)opt) + arg[i].offset) = str;
873 argc = drop_argument(argc, argv, 1 + skip, 1);