isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_arg.c
index 5f1950f..3e2188b 100644 (file)
--- a/isl_arg.c
+++ b/isl_arg.c
@@ -1,7 +1,7 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
@@ -16,7 +16,6 @@
 
 static struct isl_arg help_arg[] = {
 ISL_ARG_PHANTOM_BOOL('h', "help", NULL, "print this help, then exit")
-ISL_ARG_END
 };
 
 static void set_default_choice(struct isl_arg *arg, void *opt)
@@ -38,12 +37,17 @@ static void set_default_bool(struct isl_arg *arg, void *opt)
 
 static void set_default_child(struct isl_arg *arg, void *opt)
 {
-       void *child = calloc(1, arg->u.child.size);
+       void *child;
 
-       if (child)
-               isl_arg_set_defaults(arg->u.child.child, child);
+       if (arg->offset == (size_t) -1)
+               child = opt;
+       else {
+               child = calloc(1, arg->u.child.child->options_size);
+               *(void **)(((char *)opt) + arg->offset) = child;
+       }
 
-       *(void **)(((char *)opt) + arg->offset) = child;
+       if (child)
+               isl_args_set_defaults(arg->u.child.child, child);
 }
 
 static void set_default_user(struct isl_arg *arg, void *opt)
@@ -51,6 +55,11 @@ static void set_default_user(struct isl_arg *arg, void *opt)
        arg->u.user.init(((char *)opt) + arg->offset);
 }
 
+static void set_default_int(struct isl_arg *arg, void *opt)
+{
+       *(int *)(((char *)opt) + arg->offset) = arg->u.i.default_value;
+}
+
 static void set_default_long(struct isl_arg *arg, void *opt)
 {
        *(long *)(((char *)opt) + arg->offset) = arg->u.l.default_value;
@@ -69,37 +78,51 @@ static void set_default_str(struct isl_arg *arg, void *opt)
        *(const char **)(((char *)opt) + arg->offset) = str;
 }
 
-void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
+static void set_default_str_list(struct isl_arg *arg, void *opt)
+{
+       *(const char ***)(((char *) opt) + arg->offset) = NULL;
+       *(int *)(((char *) opt) + arg->u.str_list.offset_n) = 0;
+}
+
+void isl_args_set_defaults(struct isl_args *args, void *opt)
 {
        int i;
 
-       for (i = 0; arg[i].type != isl_arg_end; ++i) {
-               switch (arg[i].type) {
+       for (i = 0; args->args[i].type != isl_arg_end; ++i) {
+               switch (args->args[i].type) {
                case isl_arg_choice:
-                       set_default_choice(&arg[i], opt);
+                       set_default_choice(&args->args[i], opt);
                        break;
                case isl_arg_flags:
-                       set_default_flags(&arg[i], opt);
+                       set_default_flags(&args->args[i], opt);
                        break;
                case isl_arg_bool:
-                       set_default_bool(&arg[i], opt);
+                       set_default_bool(&args->args[i], opt);
                        break;
                case isl_arg_child:
-                       set_default_child(&arg[i], opt);
+                       set_default_child(&args->args[i], opt);
                        break;
                case isl_arg_user:
-                       set_default_user(&arg[i], opt);
+                       set_default_user(&args->args[i], opt);
+                       break;
+               case isl_arg_int:
+                       set_default_int(&args->args[i], opt);
                        break;
                case isl_arg_long:
-                       set_default_long(&arg[i], opt);
+                       set_default_long(&args->args[i], opt);
                        break;
                case isl_arg_ulong:
-                       set_default_ulong(&arg[i], opt);
+                       set_default_ulong(&args->args[i], opt);
                        break;
                case isl_arg_arg:
                case isl_arg_str:
-                       set_default_str(&arg[i], opt);
+                       set_default_str(&args->args[i], opt);
+                       break;
+               case isl_arg_str_list:
+                       set_default_str_list(&args->args[i], opt);
                        break;
+               case isl_arg_alias:
+               case isl_arg_footer:
                case isl_arg_version:
                case isl_arg_end:
                        break;
@@ -107,37 +130,62 @@ void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
        }
 }
 
-void isl_arg_free(struct isl_arg *arg, void *opt)
+static void free_str_list(struct isl_arg *arg, void *opt)
 {
        int i;
+       int n = *(int *)(((char *) opt) + arg->u.str_list.offset_n);
+       char **list = *(char ***)(((char *) opt) + arg->offset);
 
-       if (!opt)
-               return;
+       for (i = 0; i < n; ++i)
+               free(list[i]);
+       free(list);
+}
+
+static void free_args(struct isl_arg *arg, void *opt)
+{
+       int i;
 
        for (i = 0; arg[i].type != isl_arg_end; ++i) {
                switch (arg[i].type) {
                case isl_arg_child:
-                       isl_arg_free(arg[i].u.child.child,
-                               *(void **)(((char *)opt) + arg[i].offset));
+                       if (arg[i].offset == (size_t) -1)
+                               free_args(arg[i].u.child.child->args, opt);
+                       else
+                               isl_args_free(arg[i].u.child.child,
+                                   *(void **)(((char *)opt) + arg[i].offset));
                        break;
                case isl_arg_arg:
                case isl_arg_str:
                        free(*(char **)(((char *)opt) + arg[i].offset));
                        break;
+               case isl_arg_str_list:
+                       free_str_list(&arg[i], opt);
+                       break;
                case isl_arg_user:
                        if (arg[i].u.user.clear)
                                arg[i].u.user.clear(((char *)opt) + arg[i].offset);
                        break;
+               case isl_arg_alias:
                case isl_arg_bool:
                case isl_arg_choice:
                case isl_arg_flags:
+               case isl_arg_int:
                case isl_arg_long:
                case isl_arg_ulong:
                case isl_arg_version:
+               case isl_arg_footer:
                case isl_arg_end:
                        break;
                }
        }
+}
+
+void isl_args_free(struct isl_args *args, void *opt)
+{
+       if (!opt)
+               return;
+
+       free_args(args->args, opt);
 
        free(opt);
 }
@@ -151,11 +199,16 @@ static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
                return 4;
        }
 
-       if (decl->short_name)
+       if (decl->short_name) {
                printf("  -%c, --", decl->short_name);
-       else
+               len += 8;
+       } else if (decl->flags & ISL_ARG_SINGLE_DASH) {
+               printf("  -");
+               len += 3;
+       } else {
                printf("      --");
-       len += 8;
+               len += 8;
+       }
 
        if (prefix) {
                printf("%s-", prefix);
@@ -168,6 +221,17 @@ static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
        printf("%s", decl->long_name);
        len += strlen(decl->long_name);
 
+       while ((++decl)->type == isl_arg_alias) {
+               printf(", --");
+               len += 4;
+               if (no) {
+                       printf("no-");
+                       len += 3;
+               }
+               printf("%s", decl->long_name);
+               len += strlen(decl->long_name);
+       }
+
        return len;
 }
 
@@ -180,43 +244,46 @@ const void *isl_memrchr(const void *s, int c, size_t n)
        return NULL;
 }
 
-static int print_help_msg(struct isl_arg *decl, int pos)
+static int wrap_msg(const char *s, int indent, int pos)
 {
        int len;
-       const char *s;
+       int wrap_len = 75 - indent;
 
-       if (!decl->help_msg)
-               return pos;
-
-       if (pos >= 29)
-               printf("\n%30s", "");
+       if (pos + 1 >= indent)
+               printf("\n%*s", indent, "");
        else
-               printf("%*s", 30 - pos, "");
+               printf("%*s", indent - pos, "");
 
-       s = decl->help_msg;
        len = strlen(s);
-       while (len > 45) {
-               const char *space = isl_memrchr(s, ' ', 45);
+       while (len > wrap_len) {
+               const char *space = isl_memrchr(s, ' ', wrap_len);
                int l;
 
                if (!space)
-                       space = strchr(s + 45, ' ');
+                       space = strchr(s + wrap_len, ' ');
                if (!space)
                        break;
                l = space - s;
                printf("%.*s", l, s);
                s = space + 1;
                len -= l + 1;
-               printf("\n%30s", "");
+               printf("\n%*s", indent, "");
        }
 
        printf("%s", s);
        return len;
 }
 
+static int print_help_msg(struct isl_arg *decl, int pos)
+{
+       if (!decl->help_msg)
+               return pos;
+
+       return wrap_msg(decl->help_msg, 30, pos);
+}
+
 static void print_default(struct isl_arg *decl, const char *def, int pos)
 {
-       int i;
        const char *default_prefix = "[default: ";
        const char *default_suffix = "]";
        int len;
@@ -238,13 +305,15 @@ static void print_default(struct isl_arg *decl, const char *def, int pos)
        printf("%s%s%s", default_prefix, def, default_suffix);
 }
 
-static void print_default_choice(struct isl_arg *decl, int pos)
+static void print_default_choice(struct isl_arg *decl, void *opt, int pos)
 {
        int i;
        const char *s = "none";
+       unsigned *p;
 
+       p = (unsigned *)(((char *) opt) + decl->offset);
        for (i = 0; decl->u.choice.choice[i].name; ++i)
-               if (decl->u.choice.choice[i].value == decl->u.choice.default_value) {
+               if (decl->u.choice.choice[i].value == *p) {
                        s = decl->u.choice.choice[i].name;
                        break;
                }
@@ -252,7 +321,8 @@ static void print_default_choice(struct isl_arg *decl, int pos)
        print_default(decl, s, pos);
 }
 
-static void print_choice_help(struct isl_arg *decl, const char *prefix)
+static void print_choice_help(struct isl_arg *decl, const char *prefix,
+       void *opt)
 {
        int i;
        int pos;
@@ -271,20 +341,22 @@ static void print_choice_help(struct isl_arg *decl, const char *prefix)
        }
 
        pos = print_help_msg(decl, pos);
-       print_default_choice(decl, pos);
+       print_default_choice(decl, opt, pos);
 
        printf("\n");
 }
 
-static void print_default_flags(struct isl_arg *decl, int pos)
+static void print_default_flags(struct isl_arg *decl, void *opt, int pos)
 {
        int i, first;
        const char *default_prefix = "[default: ";
        const char *default_suffix = "]";
        int len = strlen(default_prefix) + strlen(default_suffix);
+       unsigned *p;
 
+       p = (unsigned *)(((char *) opt) + decl->offset);
        for (i = 0; decl->u.flags.flags[i].name; ++i)
-               if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
+               if ((*p & decl->u.flags.flags[i].mask) ==
                     decl->u.flags.flags[i].value)
                        len += strlen(decl->u.flags.flags[i].name);
 
@@ -303,7 +375,7 @@ static void print_default_flags(struct isl_arg *decl, int pos)
        printf("%s", default_prefix);
 
        for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
-               if ((decl->u.flags.default_value & decl->u.flags.flags[i].mask) ==
+               if ((*p & decl->u.flags.flags[i].mask) ==
                     decl->u.flags.flags[i].value) {
                        if (!first)
                                printf(",");
@@ -314,7 +386,8 @@ static void print_default_flags(struct isl_arg *decl, int pos)
        printf("%s", default_suffix);
 }
 
-static void print_flags_help(struct isl_arg *decl, const char *prefix)
+static void print_flags_help(struct isl_arg *decl, const char *prefix,
+       void *opt)
 {
        int i, j;
        int pos;
@@ -342,15 +415,16 @@ static void print_flags_help(struct isl_arg *decl, const char *prefix)
        }
 
        pos = print_help_msg(decl, pos);
-       print_default_flags(decl, pos);
+       print_default_flags(decl, opt, pos);
 
        printf("\n");
 }
 
-static void print_bool_help(struct isl_arg *decl, const char *prefix)
+static void print_bool_help(struct isl_arg *decl, const char *prefix, void *opt)
 {
        int pos;
-       int no = decl->u.b.default_value == 1;
+       unsigned *p = opt ? (unsigned *)(((char *) opt) + decl->offset) : NULL;
+       int no = p ? *p == 1 : 0;
        pos = print_arg_help(decl, prefix, no);
        pos = print_help_msg(decl, pos);
        if (decl->offset != (size_t) -1)
@@ -364,17 +438,31 @@ static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
        return pos + 3 + strlen(name);
 }
 
-static void print_long_help(struct isl_arg *decl, const char *prefix)
+static void print_int_help(struct isl_arg *decl, const char *prefix, void *opt)
 {
        int pos;
+       char val[20];
+       int *p = (int *)(((char *) opt) + decl->offset);
        pos = print_arg_help(decl, prefix, 0);
-       if (decl->u.l.default_value != decl->u.l.default_selected) {
+       pos = print_argument_name(decl, decl->argument_name, pos);
+       pos = print_help_msg(decl, pos);
+       snprintf(val, sizeof(val), "%d", *p);
+       print_default(decl, val, pos);
+       printf("\n");
+}
+
+static void print_long_help(struct isl_arg *decl, const char *prefix, void *opt)
+{
+       int pos;
+       long *p = (long *)(((char *) opt) + decl->offset);
+       pos = print_arg_help(decl, prefix, 0);
+       if (*p != decl->u.l.default_selected) {
                printf("[");
                pos++;
        }
        printf("=long");
        pos += 5;
-       if (decl->u.l.default_value != decl->u.l.default_selected) {
+       if (*p != decl->u.l.default_selected) {
                printf("]");
                pos++;
        }
@@ -392,44 +480,74 @@ static void print_ulong_help(struct isl_arg *decl, const char *prefix)
        printf("\n");
 }
 
-static void print_str_help(struct isl_arg *decl, const char *prefix)
+static void print_str_help(struct isl_arg *decl, const char *prefix, void *opt)
 {
        int pos;
        const char *a = decl->argument_name ? decl->argument_name : "string";
+       const char **p = (const char **)(((char *) opt) + decl->offset);
        pos = print_arg_help(decl, prefix, 0);
        pos = print_argument_name(decl, a, pos);
        pos = print_help_msg(decl, pos);
-       if (decl->u.str.default_value)
-               print_default(decl, decl->u.str.default_value, pos);
+       if (*p)
+               print_default(decl, *p, pos);
        printf("\n");
 }
 
-static void print_help(struct isl_arg *arg, const char *prefix)
+static void print_str_list_help(struct isl_arg *decl, const char *prefix)
+{
+       int pos;
+       const char *a = decl->argument_name ? decl->argument_name : "string";
+       pos = print_arg_help(decl, prefix, 0);
+       pos = print_argument_name(decl, a, pos);
+       pos = print_help_msg(decl, pos);
+       printf("\n");
+}
+
+static void print_help(struct isl_arg *arg, const char *prefix, void *opt)
 {
        int i;
+       int any = 0;
 
        for (i = 0; arg[i].type != isl_arg_end; ++i) {
+               if (arg[i].flags & ISL_ARG_HIDDEN)
+                       continue;
                switch (arg[i].type) {
                case isl_arg_flags:
-                       print_flags_help(&arg[i], prefix);
+                       print_flags_help(&arg[i], prefix, opt);
+                       any = 1;
                        break;
                case isl_arg_choice:
-                       print_choice_help(&arg[i], prefix);
+                       print_choice_help(&arg[i], prefix, opt);
+                       any = 1;
                        break;
                case isl_arg_bool:
-                       print_bool_help(&arg[i], prefix);
+                       print_bool_help(&arg[i], prefix, opt);
+                       any = 1;
+                       break;
+               case isl_arg_int:
+                       print_int_help(&arg[i], prefix, opt);
+                       any = 1;
                        break;
                case isl_arg_long:
-                       print_long_help(&arg[i], prefix);
+                       print_long_help(&arg[i], prefix, opt);
+                       any = 1;
                        break;
                case isl_arg_ulong:
                        print_ulong_help(&arg[i], prefix);
+                       any = 1;
                        break;
                case isl_arg_str:
-                       print_str_help(&arg[i], prefix);
+                       print_str_help(&arg[i], prefix, opt);
+                       any = 1;
+                       break;
+               case isl_arg_str_list:
+                       print_str_list_help(&arg[i], prefix);
+                       any = 1;
                        break;
+               case isl_arg_alias:
                case isl_arg_version:
                case isl_arg_arg:
+               case isl_arg_footer:
                case isl_arg_child:
                case isl_arg_user:
                case isl_arg_end:
@@ -438,13 +556,23 @@ static void print_help(struct isl_arg *arg, const char *prefix)
        }
 
        for (i = 0; arg[i].type != isl_arg_end; ++i) {
+               void *child;
+
                if (arg[i].type != isl_arg_child)
                        continue;
+               if (arg[i].flags & ISL_ARG_HIDDEN)
+                       continue;
 
-               printf("\n");
+               if (any)
+                       printf("\n");
                if (arg[i].help_msg)
                        printf(" %s\n", arg[i].help_msg);
-               print_help(arg[i].u.child.child, arg[i].long_name);
+               if (arg[i].offset == (size_t) -1)
+                       child = opt;
+               else
+                       child = *(void **)(((char *) opt) + arg[i].offset);
+               print_help(arg[i].u.child.child->args, arg[i].long_name, child);
+               any = 1;
        }
 }
 
@@ -470,7 +598,7 @@ static int any_version(struct isl_arg *decl)
                case isl_arg_version:
                        return 1;
                case isl_arg_child:
-                       if (any_version(decl[i].u.child.child))
+                       if (any_version(decl[i].u.child.child->args))
                                return 1;
                        break;
                default:
@@ -481,7 +609,8 @@ static int any_version(struct isl_arg *decl)
        return 0;
 }
 
-static void print_help_and_exit(struct isl_arg *arg, const char *prog)
+static void print_help_and_exit(struct isl_arg *arg, const char *prog,
+       void *opt)
 {
        int i;
 
@@ -493,15 +622,43 @@ static void print_help_and_exit(struct isl_arg *arg, const char *prog)
 
        printf("\n\n");
 
-       print_help(arg, NULL);
+       print_help(arg, NULL, opt);
        printf("\n");
        if (any_version(arg))
                printf("  -V, --version\n");
-       print_bool_help(help_arg, NULL);
+       print_bool_help(help_arg, NULL, NULL);
+
+       for (i = 0; arg[i].type != isl_arg_end; ++i) {
+               if (arg[i].type != isl_arg_footer)
+                       continue;
+               wrap_msg(arg[i].help_msg, 0, 0);
+               printf("\n");
+       }
 
        exit(0);
 }
 
+static int match_long_name(struct isl_arg *decl,
+       const char *start, const char *end)
+{
+       do {
+               if (end - start == strlen(decl->long_name) &&
+                   !strncmp(start, decl->long_name, end - start))
+                       return 1;
+       } while ((++decl)->type == isl_arg_alias);
+
+       return 0;
+}
+
+static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
+{
+       if (!strncmp(arg, "--", 2))
+               return arg + 2;
+       if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
+               return arg + 1;
+       return NULL;
+}
+
 static const char *skip_name(struct isl_arg *decl, const char *arg,
        const char *prefix, int need_argument, int *has_argument)
 {
@@ -519,10 +676,10 @@ static const char *skip_name(struct isl_arg *decl, const char *arg,
        if (!decl->long_name)
                return NULL;
 
-       if (strncmp(arg, "--", 2))
+       name = skip_dash_dash(decl, arg);
+       if (!name)
                return NULL;
 
-       name = arg + 2;
        equal = strchr(name, '=');
        if (need_argument && !equal)
                return NULL;
@@ -538,8 +695,7 @@ static const char *skip_name(struct isl_arg *decl, const char *arg,
                        name += prefix_len + 1;
        }
 
-       if (end - name != strlen(decl->long_name) ||
-           strncmp(name, decl->long_name, end - name))
+       if (!match_long_name(decl, name, end))
                return NULL;
 
        return equal ? equal + 1 : end;
@@ -623,7 +779,7 @@ static int parse_flags_option(struct isl_arg *decl, char **arg,
        if (!has_argument)
                flags = arg[1];
 
-       val = *(unsigned *)(((char *)opt) + decl->offset);
+       val = 0;
 
        while ((comma = strchr(flags, ',')) != NULL) {
                if (!set_flag(decl, &val, flags, comma - flags))
@@ -638,12 +794,24 @@ static int parse_flags_option(struct isl_arg *decl, char **arg,
        return has_argument ? 1 : 2;
 }
 
-static int parse_bool_option(struct isl_arg *decl, const char *arg,
+static int parse_bool_option(struct isl_arg *decl, char **arg,
        const char *prefix, void *opt)
 {
+       const char *name;
        unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
 
-       if (skip_name(decl, arg, prefix, 0, NULL)) {
+       if (skip_name(decl, arg[0], prefix, 0, NULL)) {
+               if ((decl->flags & ISL_ARG_BOOL_ARG) && arg[1]) {
+                       char *endptr;
+                       int val = strtol(arg[1], &endptr, 0);
+                       if (*endptr == '\0' && (val == 0 || val == 1)) {
+                               if (decl->u.b.set)
+                                       decl->u.b.set(opt, val);
+                               else if (decl->offset != (size_t) -1)
+                                       *p = val;
+                               return 2;
+                       }
+               }
                if (decl->u.b.set)
                        decl->u.b.set(opt, 1);
                else if (decl->offset != (size_t) -1)
@@ -655,31 +823,31 @@ static int parse_bool_option(struct isl_arg *decl, const char *arg,
        if (!decl->long_name)
                return 0;
 
-       if (strncmp(arg, "--", 2))
+       name = skip_dash_dash(decl, arg[0]);
+       if (!name)
                return 0;
-       arg += 2;
 
        if (prefix) {
                size_t prefix_len = strlen(prefix);
-               if (strncmp(arg, prefix, prefix_len) == 0 &&
-                   arg[prefix_len] == '-') {
-                       arg += prefix_len + 1;
+               if (strncmp(name, prefix, prefix_len) == 0 &&
+                   name[prefix_len] == '-') {
+                       name += prefix_len + 1;
                        prefix = NULL;
                }
        }
 
-       if (strncmp(arg, "no-", 3))
+       if (strncmp(name, "no-", 3))
                return 0;
-       arg += 3;
+       name += 3;
 
        if (prefix) {
                size_t prefix_len = strlen(prefix);
-               if (strncmp(arg, prefix, prefix_len) == 0 &&
-                   arg[prefix_len] == '-')
-                       arg += prefix_len + 1;
+               if (strncmp(name, prefix, prefix_len) == 0 &&
+                   name[prefix_len] == '-')
+                       name += prefix_len + 1;
        }
 
-       if (!strcmp(arg, decl->long_name)) {
+       if (match_long_name(decl, name, name + strlen(name))) {
                if (decl->u.b.set)
                        decl->u.b.set(opt, 0);
                else if (decl->offset != (size_t) -1)
@@ -717,6 +885,72 @@ static int parse_str_option(struct isl_arg *decl, char **arg,
        return 0;
 }
 
+static int isl_arg_str_list_append(struct isl_arg *decl, void *opt,
+       const char *s)
+{
+       int *n = (int *)(((char *) opt) + decl->u.str_list.offset_n);
+       char **list = *(char ***)(((char *) opt) + decl->offset);
+
+       list = realloc(list, (*n + 1) * sizeof(char *));
+       if (!list)
+               return -1;
+       *(char ***)(((char *) opt) + decl->offset) = list;
+       list[*n] = strdup(s);
+       (*n)++;
+       return 0;
+}
+
+static int parse_str_list_option(struct isl_arg *decl, char **arg,
+       const char *prefix, void *opt)
+{
+       int has_argument;
+       const char *s;
+
+       s = skip_name(decl, arg[0], prefix, 0, &has_argument);
+       if (!s)
+               return 0;
+
+       if (has_argument) {
+               isl_arg_str_list_append(decl, opt, s);
+               return 1;
+       }
+
+       if (arg[1]) {
+               isl_arg_str_list_append(decl, opt, arg[1]);
+               return 2;
+       }
+
+       return 0;
+}
+
+static int parse_int_option(struct isl_arg *decl, char **arg,
+       const char *prefix, void *opt)
+{
+       int has_argument;
+       const char *val;
+       char *endptr;
+       int *p = (int *)(((char *)opt) + decl->offset);
+
+       val = skip_name(decl, arg[0], prefix, 0, &has_argument);
+       if (!val)
+               return 0;
+
+       if (has_argument) {
+               *p = atoi(val);
+               return 1;
+       }
+
+       if (arg[1]) {
+               int i = strtol(arg[1], &endptr, 0);
+               if (*endptr == '\0') {
+                       *p = i;
+                       return 2;
+               }
+       }
+
+       return 0;
+}
+
 static int parse_long_option(struct isl_arg *decl, char **arg,
        const char *prefix, void *opt)
 {
@@ -791,10 +1025,18 @@ static int parse_ulong_option(struct isl_arg *decl, char **arg,
 static int parse_option(struct isl_arg *decl, char **arg,
        const char *prefix, void *opt);
 
-static int parse_child_option(struct isl_arg *decl, char **arg, void *opt)
+static int parse_child_option(struct isl_arg *decl, char **arg,
+       const char *prefix, void *opt)
 {
-       return parse_option(decl->u.child.child, arg, decl->long_name,
-                               *(void **)(((char *)opt) + decl->offset));
+       void *child;
+
+       if (decl->offset == (size_t) -1)
+               child = opt;
+       else {
+               child = *(void **)(((char *)opt) + decl->offset);
+               prefix = decl->long_name;
+       }
+       return parse_option(decl->u.child.child->args, arg, prefix, child);
 }
 
 static int parse_option(struct isl_arg *decl, char **arg,
@@ -811,6 +1053,9 @@ static int parse_option(struct isl_arg *decl, char **arg,
                case isl_arg_flags:
                        parsed = parse_flags_option(&decl[i], arg, prefix, opt);
                        break;
+               case isl_arg_int:
+                       parsed = parse_int_option(&decl[i], arg, prefix, opt);
+                       break;
                case isl_arg_long:
                        parsed = parse_long_option(&decl[i], arg, prefix, opt);
                        break;
@@ -818,15 +1063,21 @@ static int parse_option(struct isl_arg *decl, char **arg,
                        parsed = parse_ulong_option(&decl[i], arg, prefix, opt);
                        break;
                case isl_arg_bool:
-                       parsed = parse_bool_option(&decl[i], *arg, prefix, opt);
+                       parsed = parse_bool_option(&decl[i], arg, prefix, opt);
                        break;
                case isl_arg_str:
                        parsed = parse_str_option(&decl[i], arg, prefix, opt);
                        break;
+               case isl_arg_str_list:
+                       parsed = parse_str_list_option(&decl[i], arg, prefix,
+                                                       opt);
+                       break;
                case isl_arg_child:
-                       parsed = parse_child_option(&decl[i], arg, opt);
+                       parsed = parse_child_option(&decl[i], arg, prefix, opt);
                        break;
+               case isl_arg_alias:
                case isl_arg_arg:
+               case isl_arg_footer:
                case isl_arg_user:
                case isl_arg_version:
                case isl_arg_end:
@@ -849,7 +1100,7 @@ static void print_version(struct isl_arg *decl)
                        decl[i].u.version.print_version();
                        break;
                case isl_arg_child:
-                       print_version(decl[i].u.child.child);
+                       print_version(decl[i].u.child.child->args);
                        break;
                default:
                        break;
@@ -866,7 +1117,7 @@ static void print_version_and_exit(struct isl_arg *decl)
 
 static int drop_argument(int argc, char **argv, int drop, int n)
 {
-       for (; drop < argc; ++drop)
+       for (; drop + n < argc; ++drop)
                argv[drop] = argv[drop + n];
 
        return argc - n;
@@ -893,34 +1144,48 @@ static int next_arg(struct isl_arg *arg, int a)
        return -1;
 }
 
-int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
+/* Unless ISL_ARG_SKIP_HELP is set, check if any of the arguments is
+ * equal to "--help" and if so call print_help_and_exit.
+ */
+static void check_help(struct isl_args *args, int argc, char **argv, void *opt,
        unsigned flags)
 {
-       int a = -1;
-       int skip = 0;
        int i;
-       int n;
 
-       n = n_arg(arg);
+       if (ISL_FL_ISSET(flags, ISL_ARG_SKIP_HELP))
+               return;
 
        for (i = 1; i < argc; ++i) {
                if (strcmp(argv[i], "--help") == 0)
-                       print_help_and_exit(arg, argv[0]);
+                       print_help_and_exit(args->args, argv[0], opt);
        }
+}
+
+int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
+       unsigned flags)
+{
+       int a = -1;
+       int skip = 0;
+       int i;
+       int n;
+
+       n = n_arg(args->args);
+
+       check_help(args, argc, argv, opt, flags);
 
        for (i = 1; i < argc; ++i) {
                if ((strcmp(argv[i], "--version") == 0 ||
-                    strcmp(argv[i], "-V") == 0) && any_version(arg))
-                       print_version_and_exit(arg);
+                    strcmp(argv[i], "-V") == 0) && any_version(args->args))
+                       print_version_and_exit(args->args);
        }
 
        while (argc > 1 + skip) {
                int parsed;
                if (argv[1 + skip][0] != '-') {
-                       a = next_arg(arg, a);
+                       a = next_arg(args->args, a);
                        if (a >= 0) {
                                char **p;
-                               p = (char **)(((char *)opt)+arg[a].offset);
+                               p = (char **)(((char *)opt)+args->args[a].offset);
                                free(*p);
                                *p = strdup(argv[1 + skip]);
                                argc = drop_argument(argc, argv, 1 + skip, 1);
@@ -933,7 +1198,7 @@ int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
                                ++skip;
                        continue;
                }
-               parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
+               parsed = parse_option(args->args, &argv[1 + skip], NULL, opt);
                if (parsed)
                        argc = drop_argument(argc, argv, 1 + skip, parsed);
                else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {