hide internal check_help
[platform/upstream/isl.git] / isl_arg.c
index ce0dcce..f2025ee 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
@@ -78,6 +78,12 @@ static void set_default_str(struct isl_arg *arg, void *opt)
        *(const char **)(((char *)opt) + arg->offset) = str;
 }
 
+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;
@@ -112,6 +118,9 @@ void isl_args_set_defaults(struct isl_args *args, void *opt)
                case isl_arg_str:
                        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:
@@ -121,6 +130,17 @@ void isl_args_set_defaults(struct isl_args *args, 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);
+
+       for (i = 0; i < n; ++i)
+               free(list[i]);
+       free(list);
+}
+
 static void free_args(struct isl_arg *arg, void *opt)
 {
        int i;
@@ -138,6 +158,9 @@ static void free_args(struct isl_arg *arg, void *opt)
                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);
@@ -470,6 +493,16 @@ static void print_str_help(struct isl_arg *decl, const char *prefix, void *opt)
        printf("\n");
 }
 
+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;
@@ -507,6 +540,10 @@ static void print_help(struct isl_arg *arg, const char *prefix, void *opt)
                        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:
@@ -742,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))
@@ -848,6 +885,44 @@ 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)
 {
@@ -993,6 +1068,10 @@ static int parse_option(struct isl_arg *decl, char **arg,
                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, prefix, opt);
                        break;
@@ -1065,6 +1144,23 @@ static int next_arg(struct isl_arg *arg, int a)
        return -1;
 }
 
+/* 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 i;
+
+       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(args->args, argv[0], opt);
+       }
+}
+
 int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
        unsigned flags)
 {
@@ -1075,10 +1171,7 @@ int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
 
        n = n_arg(args->args);
 
-       for (i = 1; i < argc; ++i) {
-               if (strcmp(argv[i], "--help") == 0)
-                       print_help_and_exit(args->args, argv[0], opt);
-       }
+       check_help(args, argc, argv, opt, flags);
 
        for (i = 1; i < argc; ++i) {
                if ((strcmp(argv[i], "--version") == 0 ||