isl_arg_parse: avoid duplicate newline in help output
[platform/upstream/isl.git] / isl_arg.c
index 0c175fc..1f2e63b 100644 (file)
--- a/isl_arg.c
+++ b/isl_arg.c
 #include <isl/arg.h>
 #include <isl/ctx.h>
 
+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)
 {
        *(unsigned *)(((char *)opt) + arg->offset) = arg->u.choice.default_value;
@@ -26,17 +31,24 @@ static void set_default_flags(struct isl_arg *arg, void *opt)
 
 static void set_default_bool(struct isl_arg *arg, void *opt)
 {
+       if (arg->offset == (size_t) -1)
+               return;
        *(unsigned *)(((char *)opt) + arg->offset) = arg->u.b.default_value;
 }
 
 static void set_default_child(struct isl_arg *arg, void *opt)
 {
-       void *child = calloc(1, arg->u.child.size);
+       void *child;
+
+       if (arg->offset == (size_t) -1)
+               child = opt;
+       else {
+               child = calloc(1, arg->u.child.size);
+               *(void **)(((char *)opt) + arg->offset) = child;
+       }
 
        if (child)
                isl_arg_set_defaults(arg->u.child.child, child);
-
-       *(void **)(((char *)opt) + arg->offset) = child;
 }
 
 static void set_default_user(struct isl_arg *arg, void *opt)
@@ -44,6 +56,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;
@@ -83,6 +100,9 @@ void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
                case isl_arg_user:
                        set_default_user(&arg[i], opt);
                        break;
+               case isl_arg_int:
+                       set_default_int(&arg[i], opt);
+                       break;
                case isl_arg_long:
                        set_default_long(&arg[i], opt);
                        break;
@@ -93,6 +113,7 @@ void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
                case isl_arg_str:
                        set_default_str(&arg[i], opt);
                        break;
+               case isl_arg_alias:
                case isl_arg_version:
                case isl_arg_end:
                        break;
@@ -100,18 +121,18 @@ void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
        }
 }
 
-void isl_arg_free(struct isl_arg *arg, void *opt)
+static void free_args(struct isl_arg *arg, void *opt)
 {
        int i;
 
-       if (!opt)
-               return;
-
        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, opt);
+                       else
+                               isl_arg_free(arg[i].u.child.child,
+                                   *(void **)(((char *)opt) + arg[i].offset));
                        break;
                case isl_arg_arg:
                case isl_arg_str:
@@ -121,9 +142,11 @@ void isl_arg_free(struct isl_arg *arg, void *opt)
                        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:
@@ -131,6 +154,14 @@ void isl_arg_free(struct isl_arg *arg, void *opt)
                        break;
                }
        }
+}
+
+void isl_arg_free(struct isl_arg *arg, void *opt)
+{
+       if (!opt)
+               return;
+
+       free_args(arg, opt);
 
        free(opt);
 }
@@ -144,11 +175,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);
@@ -161,6 +197,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;
 }
 
@@ -346,7 +393,8 @@ static void print_bool_help(struct isl_arg *decl, const char *prefix)
        int no = decl->u.b.default_value == 1;
        pos = print_arg_help(decl, prefix, no);
        pos = print_help_msg(decl, pos);
-       print_default(decl, no ? "yes" : "no", pos);
+       if (decl->offset != (size_t) -1)
+               print_default(decl, no ? "yes" : "no", pos);
        printf("\n");
 }
 
@@ -356,6 +404,18 @@ static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
        return pos + 3 + strlen(name);
 }
 
+static void print_int_help(struct isl_arg *decl, const char *prefix)
+{
+       int pos;
+       char val[20];
+       pos = print_arg_help(decl, prefix, 0);
+       pos = print_argument_name(decl, decl->argument_name, pos);
+       pos = print_help_msg(decl, pos);
+       snprintf(val, sizeof(val), "%d", decl->u.i.default_value);
+       print_default(decl, val, pos);
+       printf("\n");
+}
+
 static void print_long_help(struct isl_arg *decl, const char *prefix)
 {
        int pos;
@@ -399,27 +459,41 @@ static void print_str_help(struct isl_arg *decl, const char *prefix)
 static void print_help(struct isl_arg *arg, const char *prefix)
 {
        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);
+                       any = 1;
                        break;
                case isl_arg_choice:
                        print_choice_help(&arg[i], prefix);
+                       any = 1;
                        break;
                case isl_arg_bool:
                        print_bool_help(&arg[i], prefix);
+                       any = 1;
+                       break;
+               case isl_arg_int:
+                       print_int_help(&arg[i], prefix);
+                       any = 1;
                        break;
                case isl_arg_long:
                        print_long_help(&arg[i], prefix);
+                       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);
+                       any = 1;
                        break;
+               case isl_arg_alias:
                case isl_arg_version:
                case isl_arg_arg:
                case isl_arg_child:
@@ -432,11 +506,15 @@ static void print_help(struct isl_arg *arg, const char *prefix)
        for (i = 0; arg[i].type != isl_arg_end; ++i) {
                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);
+               any = 1;
        }
 }
 
@@ -486,14 +564,35 @@ static void print_help_and_exit(struct isl_arg *arg, const char *prog)
        printf("\n\n");
 
        print_help(arg, NULL);
-       if (any_version(arg)) {
-               printf("\n");
+       printf("\n");
+       if (any_version(arg))
                printf("  -V, --version\n");
-       }
+       print_bool_help(help_arg, NULL);
 
        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)
 {
@@ -511,10 +610,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;
@@ -530,8 +629,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;
@@ -630,11 +728,28 @@ 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)
 {
-       if (skip_name(decl, arg, prefix, 0, NULL)) {
-               *(unsigned *)(((char *)opt) + decl->offset) = 1;
+       const char *name;
+       unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
+
+       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)
+                       *p = 1;
 
                return 1;
        }
@@ -642,32 +757,35 @@ 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)) {
-               *(unsigned *)(((char *)opt) + decl->offset) = 0;
+       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)
+                       *p = 0;
 
                return 1;
        }
@@ -687,11 +805,13 @@ static int parse_str_option(struct isl_arg *decl, char **arg,
                return 0;
 
        if (has_argument) {
+               free(*p);
                *p = strdup(s);
                return 1;
        }
 
        if (arg[1]) {
+               free(*p);
                *p = strdup(arg[1]);
                return 2;
        }
@@ -699,6 +819,34 @@ static int parse_str_option(struct isl_arg *decl, char **arg,
        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)
 {
@@ -773,10 +921,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, arg, prefix, child);
 }
 
 static int parse_option(struct isl_arg *decl, char **arg,
@@ -793,6 +949,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;
@@ -800,14 +959,15 @@ 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_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_user:
                case isl_arg_version:
@@ -866,9 +1026,19 @@ static int n_arg(struct isl_arg *arg)
        return n_arg;
 }
 
+static int next_arg(struct isl_arg *arg, int a)
+{
+       for (++a; arg[a].type != isl_arg_end; ++a)
+               if (arg[a].type == isl_arg_arg)
+                       return a;
+
+       return -1;
+}
+
 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
        unsigned flags)
 {
+       int a = -1;
        int skip = 0;
        int i;
        int n;
@@ -888,8 +1058,23 @@ int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
 
        while (argc > 1 + skip) {
                int parsed;
-               if (argv[1 + skip][0] != '-')
-                       break;
+               if (argv[1 + skip][0] != '-') {
+                       a = next_arg(arg, a);
+                       if (a >= 0) {
+                               char **p;
+                               p = (char **)(((char *)opt)+arg[a].offset);
+                               free(*p);
+                               *p = strdup(argv[1 + skip]);
+                               argc = drop_argument(argc, argv, 1 + skip, 1);
+                               --n;
+                       } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
+                               fprintf(stderr, "%s: extra argument: %s\n",
+                                           prog_name(argv[0]), argv[1 + skip]);
+                               exit(-1);
+                       } else
+                               ++skip;
+                       continue;
+               }
                parsed = parse_option(arg, &argv[1 + skip], NULL, opt);
                if (parsed)
                        argc = drop_argument(argc, argv, 1 + skip, parsed);
@@ -901,21 +1086,11 @@ int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
                        ++skip;
        }
 
-       if (ISL_FL_ISSET(flags, ISL_ARG_ALL) ? argc != 1 + n
-                                            : argc < 1 + skip + n) {
-               fprintf(stderr, "%s: expecting %d arguments\n",
+       if (n > 0) {
+               fprintf(stderr, "%s: expecting %d more argument(s)\n",
                                prog_name(argv[0]), n);
                exit(-1);
        }
 
-       for (i = 0; arg[i].type != isl_arg_end; ++i) {
-               const char *str;
-               if (arg[i].type != isl_arg_arg)
-                       continue;
-               str = strdup(argv[1 + skip]);
-               *(const char **)(((char *)opt) + arg[i].offset) = str;
-               argc = drop_argument(argc, argv, 1 + skip, 1);
-       }
-
        return argc;
 }