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(struct isl_arg *decl, const char *def, int pos)
213 const char *default_prefix = "[default: ";
214 const char *default_suffix = "]";
217 len = strlen(default_prefix) + strlen(def) + strlen(default_suffix);
219 if (!decl->help_msg) {
221 printf("\n%30s", "");
223 printf("%*s", 30 - pos, "");
227 printf("\n%30s", "");
231 printf("%s%s%s", default_prefix, def, default_suffix);
234 static void print_default_choice(struct isl_arg *decl, int pos)
237 const char *s = "none";
239 for (i = 0; decl->u.choice.choice[i].name; ++i)
240 if (decl->u.choice.choice[i].value == decl->u.choice.default_value) {
241 s = decl->u.choice.choice[i].name;
245 print_default(decl, s, pos);
248 static void print_choice_help(struct isl_arg *decl, const char *prefix)
253 pos = print_arg_help(decl, prefix, 0);
257 for (i = 0; decl->u.choice.choice[i].name; ++i) {
262 printf("%s", decl->u.choice.choice[i].name);
263 pos += strlen(decl->u.choice.choice[i].name);
266 pos = print_help_msg(decl, pos);
267 print_default_choice(decl, pos);
272 static void print_default_flags(struct isl_arg *decl, int pos)
275 const char *default_prefix = "[default: ";
276 const char *default_suffix = "]";
277 int len = strlen(default_prefix) + strlen(default_suffix);
279 for (i = 0; decl->u.flags.flags[i].name; ++i)
280 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
281 decl->u.flags.flags[i].value)
282 len += strlen(decl->u.flags.flags[i].name);
284 if (!decl->help_msg) {
286 printf("\n%30s", "");
288 printf("%*s", 30 - pos, "");
292 printf("\n%30s", "");
296 printf("%s", default_prefix);
298 for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
299 if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
300 decl->u.flags.flags[i].value) {
303 printf("%s", decl->u.flags.flags[i].name);
307 printf("%s", default_suffix);
310 static void print_flags_help(struct isl_arg *decl, const char *prefix)
315 pos = print_arg_help(decl, prefix, 0);
319 for (i = 0; decl->u.flags.flags[i].name; ++i) {
325 decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
331 printf("%s", decl->u.flags.flags[j].name);
332 pos += strlen(decl->u.flags.flags[j].name);
337 pos = print_help_msg(decl, pos);
338 print_default_flags(decl, pos);
343 static void print_bool_help(struct isl_arg *decl, const char *prefix)
346 int no = decl->u.b.default_value == 1;
347 pos = print_arg_help(decl, prefix, no);
348 pos = print_help_msg(decl, pos);
349 print_default(decl, no ? "yes" : "no", pos);
353 static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
355 printf("%c<%s>", decl->long_name ? '=' : ' ', name);
356 return pos + 3 + strlen(name);
359 static void print_long_help(struct isl_arg *decl, const char *prefix)
362 pos = print_arg_help(decl, prefix, 0);
363 if (decl->u.l.default_value != decl->u.l.default_selected) {
369 if (decl->u.l.default_value != decl->u.l.default_selected) {
373 print_help_msg(decl, pos);
377 static void print_ulong_help(struct isl_arg *decl, const char *prefix)
380 pos = print_arg_help(decl, prefix, 0);
383 print_help_msg(decl, pos);
387 static void print_str_help(struct isl_arg *decl, const char *prefix)
390 const char *a = decl->argument_name ? decl->argument_name : "string";
391 pos = print_arg_help(decl, prefix, 0);
392 pos = print_argument_name(decl, a, pos);
393 pos = print_help_msg(decl, pos);
394 if (decl->u.str.default_value)
395 print_default(decl, decl->u.str.default_value, pos);
399 static void print_help(struct isl_arg *arg, const char *prefix)
403 for (i = 0; arg[i].type != isl_arg_end; ++i) {
404 switch (arg[i].type) {
406 print_flags_help(&arg[i], prefix);
409 print_choice_help(&arg[i], prefix);
412 print_bool_help(&arg[i], prefix);
415 print_long_help(&arg[i], prefix);
418 print_ulong_help(&arg[i], prefix);
421 print_str_help(&arg[i], prefix);
423 case isl_arg_version:
432 for (i = 0; arg[i].type != isl_arg_end; ++i) {
433 if (arg[i].type != isl_arg_child)
438 printf(" %s\n", arg[i].help_msg);
439 print_help(arg[i].u.child.child, arg[i].long_name);
443 static const char *prog_name(const char *prog)
447 slash = strrchr(prog, '/');
450 if (strncmp(prog, "lt-", 3) == 0)
456 static int any_version(struct isl_arg *decl)
460 for (i = 0; decl[i].type != isl_arg_end; ++i) {
461 switch (decl[i].type) {
462 case isl_arg_version:
465 if (any_version(decl[i].u.child.child))
476 static void print_help_and_exit(struct isl_arg *arg, const char *prog)
480 printf("Usage: %s [OPTION...]", prog_name(prog));
482 for (i = 0; arg[i].type != isl_arg_end; ++i)
483 if (arg[i].type == isl_arg_arg)
484 printf(" %s", arg[i].argument_name);
488 print_help(arg, NULL);
489 if (any_version(arg)) {
491 printf(" -V, --version\n");
497 static const char *skip_name(struct isl_arg *decl, const char *arg,
498 const char *prefix, int need_argument, int *has_argument)
504 if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
505 if (need_argument && !arg[2])
508 *has_argument = arg[2] != '\0';
511 if (!decl->long_name)
514 if (strncmp(arg, "--", 2))
518 equal = strchr(name, '=');
519 if (need_argument && !equal)
523 *has_argument = !!equal;
524 end = equal ? equal : name + strlen(name);
527 size_t prefix_len = strlen(prefix);
528 if (strncmp(name, prefix, prefix_len) == 0 &&
529 name[prefix_len] == '-')
530 name += prefix_len + 1;
533 if (end - name != strlen(decl->long_name) ||
534 strncmp(name, decl->long_name, end - name))
537 return equal ? equal + 1 : end;
540 static int parse_choice_option(struct isl_arg *decl, char **arg,
541 const char *prefix, void *opt)
547 choice = skip_name(decl, arg[0], prefix, 0, &has_argument);
551 if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
552 unsigned u = decl->u.choice.default_selected;
553 if (decl->u.choice.set)
554 decl->u.choice.set(opt, u);
556 *(unsigned *)(((char *)opt) + decl->offset) = u;
564 for (i = 0; decl->u.choice.choice[i].name; ++i) {
567 if (strcmp(choice, decl->u.choice.choice[i].name))
570 u = decl->u.choice.choice[i].value;
571 if (decl->u.choice.set)
572 decl->u.choice.set(opt, u);
574 *(unsigned *)(((char *)opt) + decl->offset) = u;
576 return has_argument ? 1 : 2;
582 static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
587 for (i = 0; decl->u.flags.flags[i].name; ++i) {
588 if (strncmp(flag, decl->u.flags.flags[i].name, len))
591 *val &= ~decl->u.flags.flags[i].mask;
592 *val |= decl->u.flags.flags[i].value;
600 static int parse_flags_option(struct isl_arg *decl, char **arg,
601 const char *prefix, void *opt)
608 flags = skip_name(decl, arg[0], prefix, 0, &has_argument);
612 if (!has_argument && !arg[1])
618 val = *(unsigned *)(((char *)opt) + decl->offset);
620 while ((comma = strchr(flags, ',')) != NULL) {
621 if (!set_flag(decl, &val, flags, comma - flags))
625 if (!set_flag(decl, &val, flags, strlen(flags)))
628 *(unsigned *)(((char *)opt) + decl->offset) = val;
630 return has_argument ? 1 : 2;
633 static int parse_bool_option(struct isl_arg *decl, const char *arg,
634 const char *prefix, void *opt)
636 if (skip_name(decl, arg, prefix, 0, NULL)) {
637 *(unsigned *)(((char *)opt) + decl->offset) = 1;
642 if (!decl->long_name)
645 if (strncmp(arg, "--", 2))
650 size_t prefix_len = strlen(prefix);
651 if (strncmp(arg, prefix, prefix_len) == 0 &&
652 arg[prefix_len] == '-') {
653 arg += prefix_len + 1;
658 if (strncmp(arg, "no-", 3))
663 size_t prefix_len = strlen(prefix);
664 if (strncmp(arg, prefix, prefix_len) == 0 &&
665 arg[prefix_len] == '-')
666 arg += prefix_len + 1;
669 if (!strcmp(arg, decl->long_name)) {
670 *(unsigned *)(((char *)opt) + decl->offset) = 0;
678 static int parse_str_option(struct isl_arg *decl, char **arg,
679 const char *prefix, void *opt)
683 char **p = (char **)(((char *)opt) + decl->offset);
685 s = skip_name(decl, arg[0], prefix, 0, &has_argument);
702 static int parse_long_option(struct isl_arg *decl, char **arg,
703 const char *prefix, void *opt)
708 long *p = (long *)(((char *)opt) + decl->offset);
710 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
715 long l = strtol(val, NULL, 0);
717 decl->u.l.set(opt, l);
724 long l = strtol(arg[1], &endptr, 0);
725 if (*endptr == '\0') {
727 decl->u.l.set(opt, l);
734 if (decl->u.l.default_value != decl->u.l.default_selected) {
736 decl->u.l.set(opt, decl->u.l.default_selected);
738 *p = decl->u.l.default_selected;
745 static int parse_ulong_option(struct isl_arg *decl, char **arg,
746 const char *prefix, void *opt)
751 unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
753 val = skip_name(decl, arg[0], prefix, 0, &has_argument);
758 *p = strtoul(val, NULL, 0);
763 unsigned long ul = strtoul(arg[1], &endptr, 0);
764 if (*endptr == '\0') {
773 static int parse_option(struct isl_arg *decl, char **arg,
774 const char *prefix, void *opt);
776 static int parse_child_option(struct isl_arg *decl, char **arg, void *opt)
778 return parse_option(decl->u.child.child, arg, decl->long_name,
779 *(void **)(((char *)opt) + decl->offset));
782 static int parse_option(struct isl_arg *decl, char **arg,
783 const char *prefix, void *opt)
787 for (i = 0; decl[i].type != isl_arg_end; ++i) {
789 switch (decl[i].type) {
791 parsed = parse_choice_option(&decl[i], arg, prefix, opt);
794 parsed = parse_flags_option(&decl[i], arg, prefix, opt);
797 parsed = parse_long_option(&decl[i], arg, prefix, opt);
800 parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
803 parsed = parse_bool_option(&decl[i], *arg, prefix, opt);
806 parsed = parse_str_option(&decl[i], arg, prefix, opt);
809 parsed = parse_child_option(&decl[i], arg, opt);
813 case isl_arg_version:
824 static void print_version(struct isl_arg *decl)
828 for (i = 0; decl[i].type != isl_arg_end; ++i) {
829 switch (decl[i].type) {
830 case isl_arg_version:
831 decl[i].u.version.print_version();
834 print_version(decl[i].u.child.child);
842 static void print_version_and_exit(struct isl_arg *decl)
849 static int drop_argument(int argc, char **argv, int drop, int n)
851 for (; drop < argc; ++drop)
852 argv[drop] = argv[drop + n];
857 static int n_arg(struct isl_arg *arg)
862 for (i = 0; arg[i].type != isl_arg_end; ++i)
863 if (arg[i].type == isl_arg_arg)
869 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
878 for (i = 1; i < argc; ++i) {
879 if (strcmp(argv[i], "--help") == 0)
880 print_help_and_exit(arg, argv[0]);
883 for (i = 1; i < argc; ++i) {
884 if ((strcmp(argv[i], "--version") == 0 ||
885 strcmp(argv[i], "-V") == 0) && any_version(arg))
886 print_version_and_exit(arg);
889 while (argc > 1 + skip) {
891 if (argv[1 + skip][0] != '-')
893 parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
895 argc = drop_argument(argc, argv, 1 + skip, parsed);
896 else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
897 fprintf(stderr, "%s: unrecognized option: %s\n",
898 prog_name(argv[0]), argv[1 + skip]);
904 if (ISL_FL_ISSET(flags, ISL_ARG_ALL) ? argc != 1 + n
905 : argc < 1 + skip + n) {
906 fprintf(stderr, "%s: expecting %d arguments\n",
907 prog_name(argv[0]), n);
911 for (i = 0; arg[i].type != isl_arg_end; ++i) {
913 if (arg[i].type != isl_arg_arg)
915 str = strdup(argv[1 + skip]);
916 *(const char **)(((char *)opt) + arg[i].offset) = str;
917 argc = drop_argument(argc, argv, 1 + skip, 1);