isl_map_project_out: fix error return value
[isl.git] / include / isl_arg.h
blobc7bbc4108c3a3c4b3114d2d7f54299d2ae18a8ba
1 /*
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
8 */
10 #ifndef ISL_ARG_H
11 #define ISL_ARG_H
13 #include <stddef.h>
14 #include <stdlib.h>
16 #if defined(__cplusplus)
17 extern "C" {
18 #endif
20 struct isl_arg_choice {
21 const char *name;
22 unsigned value;
25 struct isl_arg_flags {
26 const char *name;
27 unsigned mask;
28 unsigned value;
31 enum isl_arg_type {
32 isl_arg_end,
33 isl_arg_arg,
34 isl_arg_bool,
35 isl_arg_child,
36 isl_arg_choice,
37 isl_arg_flags,
38 isl_arg_user,
39 isl_arg_long,
40 isl_arg_ulong,
41 isl_arg_str,
42 isl_arg_version
45 struct isl_arg {
46 enum isl_arg_type type;
47 char short_name;
48 const char *long_name;
49 const char *argument_name;
50 size_t offset;
51 const char *help_msg;
52 union {
53 struct {
54 struct isl_arg_choice *choice;
55 unsigned default_value;
56 unsigned default_selected;
57 } choice;
58 struct {
59 struct isl_arg_flags *flags;
60 unsigned default_value;
61 } flags;
62 struct {
63 unsigned default_value;
64 } b;
65 struct {
66 long default_value;
67 long default_selected;
68 int (*set)(void *opt, long val);
69 } l;
70 struct {
71 unsigned long default_value;
72 } ul;
73 struct {
74 const char *default_value;
75 } str;
76 struct {
77 struct isl_arg *child;
78 size_t size;
79 } child;
80 struct {
81 void (*print_version)(void);
82 } version;
83 struct {
84 int (*init)(void*);
85 void (*clear)(void*);
86 } user;
87 } u;
90 #define ISL_ARG_ARG(st,f,a,d) { \
91 .type = isl_arg_arg, \
92 .argument_name = a, \
93 .offset = offsetof(st, f), \
94 .u = { .str = { .default_value = d } } \
96 #define ISL_ARG_CHOICE(st,f,s,l,c,d,h) { \
97 .type = isl_arg_choice, \
98 .short_name = s, \
99 .long_name = l, \
100 .offset = offsetof(st, f), \
101 .help_msg = h, \
102 .u = { .choice = { .choice = c, .default_value = d, \
103 .default_selected = d } } \
105 #define ISL_ARG_OPT_CHOICE(st,f,s,l,c,d,ds,h) { \
106 .type = isl_arg_choice, \
107 .short_name = s, \
108 .long_name = l, \
109 .offset = offsetof(st, f), \
110 .help_msg = h, \
111 .u = { .choice = { .choice = c, .default_value = d, \
112 .default_selected = ds } } \
114 #define ISL_ARG_BOOL(st,f,s,l,d,h) { \
115 .type = isl_arg_bool, \
116 .short_name = s, \
117 .long_name = l, \
118 .offset = offsetof(st, f), \
119 .help_msg = h, \
120 .u = { .b = { .default_value = d } } \
122 #define ISL_ARG_LONG(st,f,s,lo,d,h) { \
123 .type = isl_arg_long, \
124 .short_name = s, \
125 .long_name = lo, \
126 .offset = offsetof(st, f), \
127 .help_msg = h, \
128 .u = { .l = { .default_value = d, .default_selected = d, \
129 .set = NULL } } \
131 #define ISL_ARG_USER_LONG(st,f,s,lo,setter,d,h) { \
132 .type = isl_arg_long, \
133 .short_name = s, \
134 .long_name = lo, \
135 .offset = offsetof(st, f), \
136 .help_msg = h, \
137 .u = { .l = { .default_value = d, .default_selected = d, \
138 .set = setter } } \
140 #define ISL_ARG_OPT_LONG(st,f,s,lo,d,ds,h) { \
141 .type = isl_arg_long, \
142 .short_name = s, \
143 .long_name = lo, \
144 .offset = offsetof(st, f), \
145 .help_msg = h, \
146 .u = { .l = { .default_value = d, .default_selected = ds, \
147 .set = NULL } } \
149 #define ISL_ARG_ULONG(st,f,s,l,d,h) { \
150 .type = isl_arg_ulong, \
151 .short_name = s, \
152 .long_name = l, \
153 .offset = offsetof(st, f), \
154 .help_msg = h, \
155 .u = { .ul = { .default_value = d } } \
157 #define ISL_ARG_STR(st,f,s,l,a,d,h) { \
158 .type = isl_arg_str, \
159 .short_name = s, \
160 .long_name = l, \
161 .argument_name = a, \
162 .offset = offsetof(st, f), \
163 .help_msg = h, \
164 .u = { .str = { .default_value = d } } \
166 #define ISL_ARG_CHILD(st,f,l,c,h) { \
167 .type = isl_arg_child, \
168 .long_name = l, \
169 .offset = offsetof(st, f), \
170 .help_msg = h, \
171 .u = { .child = { .child = c, .size = sizeof(*((st *)NULL)->f) } }\
173 #define ISL_ARG_FLAGS(st,f,s,l,c,d,h) { \
174 .type = isl_arg_flags, \
175 .short_name = s, \
176 .long_name = l, \
177 .offset = offsetof(st, f), \
178 .help_msg = h, \
179 .u = { .flags = { .flags = c, .default_value = d } } \
181 #define ISL_ARG_USER(st,f,i,c) { \
182 .type = isl_arg_user, \
183 .offset = offsetof(st, f), \
184 .u = { .user = { .init = i, .clear = c} } \
186 #define ISL_ARG_VERSION(print) { \
187 .type = isl_arg_version, \
188 .u = { .version = { .print_version = print } } \
190 #define ISL_ARG_END { isl_arg_end }
192 #define ISL_ARG_ALL (1 << 0)
194 void isl_arg_set_defaults(struct isl_arg *arg, void *opt);
195 void isl_arg_free(struct isl_arg *arg, void *opt);
196 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
197 unsigned flags);
199 #define ISL_ARG_DECL(prefix,st,arg) \
200 extern struct isl_arg arg[]; \
201 st *prefix ## _new_with_defaults(); \
202 void prefix ## _free(st *opt); \
203 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags);
205 #define ISL_ARG_DEF(prefix,st,arg) \
206 st *prefix ## _new_with_defaults() \
208 st *opt = (st *)calloc(1, sizeof(st)); \
209 if (opt) \
210 isl_arg_set_defaults(arg, opt); \
211 return opt; \
214 void prefix ## _free(st *opt) \
216 isl_arg_free(arg, opt); \
219 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags) \
221 return isl_arg_parse(arg, argc, argv, opt, flags); \
224 #if defined(__cplusplus)
226 #endif
228 #endif