isl_arg_parse: support ISL_ARG_BOOL_ARG flag
[platform/upstream/isl.git] / include / isl / arg.h
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #ifndef ISL_ARG_H
11 #define ISL_ARG_H
12
13 #include <stddef.h>
14 #include <stdlib.h>
15
16 #if defined(__cplusplus)
17 extern "C" {
18 #endif
19
20 struct isl_arg_choice {
21         const char      *name;
22         unsigned         value;
23 };
24
25 struct isl_arg_flags {
26         const char      *name;
27         unsigned         mask;
28         unsigned         value;
29 };
30
31 enum isl_arg_type {
32         isl_arg_end,
33         isl_arg_alias,
34         isl_arg_arg,
35         isl_arg_bool,
36         isl_arg_child,
37         isl_arg_choice,
38         isl_arg_flags,
39         isl_arg_user,
40         isl_arg_long,
41         isl_arg_ulong,
42         isl_arg_str,
43         isl_arg_version
44 };
45
46 struct isl_arg {
47         enum isl_arg_type        type;
48         char                     short_name;
49         const char              *long_name;
50         const char              *argument_name;
51         size_t                   offset;
52         const char              *help_msg;
53 #define ISL_ARG_SINGLE_DASH     (1 << 0)
54 #define ISL_ARG_BOOL_ARG        (1 << 1)
55         unsigned                 flags;
56         union {
57         struct {
58                 struct isl_arg_choice   *choice;
59                 unsigned                 default_value;
60                 unsigned                 default_selected;
61                 int (*set)(void *opt, unsigned val);
62         } choice;
63         struct {
64                 struct isl_arg_flags    *flags;
65                 unsigned                 default_value;
66         } flags;
67         struct {
68                 unsigned                 default_value;
69                 int (*set)(void *opt, unsigned val);
70         } b;
71         struct {
72                 long                    default_value;
73                 long                    default_selected;
74                 int (*set)(void *opt, long val);
75         } l;
76         struct {
77                 unsigned long           default_value;
78         } ul;
79         struct {
80                 const char              *default_value;
81         } str;
82         struct {
83                 struct isl_arg          *child;
84                 size_t                   size;
85         } child;
86         struct {
87                 void (*print_version)(void);
88         } version;
89         struct {
90                 int (*init)(void*);
91                 void (*clear)(void*);
92         } user;
93         } u;
94 };
95
96 #define ISL_ARG_ALIAS(l)        {                                       \
97         .type = isl_arg_alias,                                          \
98         .long_name = l,                                                 \
99 },
100 #define ISL_ARG_ARG(st,f,a,d)   {                                       \
101         .type = isl_arg_arg,                                            \
102         .argument_name = a,                                             \
103         .offset = offsetof(st, f),                                      \
104         .u = { .str = { .default_value = d } }                          \
105 },
106 #define ISL_ARG_CHOICE(st,f,s,l,c,d,h)  {                               \
107         .type = isl_arg_choice,                                         \
108         .short_name = s,                                                \
109         .long_name = l,                                                 \
110         .offset = offsetof(st, f),                                      \
111         .help_msg = h,                                                  \
112         .u = { .choice = { .choice = c, .default_value = d,             \
113                             .default_selected = d, .set = NULL } }      \
114 },
115 #define ISL_ARG_OPT_CHOICE(st,f,s,l,c,d,ds,h)   {                       \
116         .type = isl_arg_choice,                                         \
117         .short_name = s,                                                \
118         .long_name = l,                                                 \
119         .offset = offsetof(st, f),                                      \
120         .help_msg = h,                                                  \
121         .u = { .choice = { .choice = c, .default_value = d,             \
122                             .default_selected = ds, .set = NULL } }     \
123 },
124 #define ISL_ARG_USER_OPT_CHOICE(st,f,s,l,c,setter,d,ds,h)       {       \
125         .type = isl_arg_choice,                                         \
126         .short_name = s,                                                \
127         .long_name = l,                                                 \
128         .offset = offsetof(st, f),                                      \
129         .help_msg = h,                                                  \
130         .u = { .choice = { .choice = c, .default_value = d,             \
131                             .default_selected = ds, .set = setter } }   \
132 },
133 #define _ISL_ARG_BOOL_F(o,s,l,setter,d,h,fl)    {                       \
134         .type = isl_arg_bool,                                           \
135         .short_name = s,                                                \
136         .long_name = l,                                                 \
137         .offset = o,                                                    \
138         .help_msg = h,                                                  \
139         .flags = fl,                                                    \
140         .u = { .b = { .default_value = d, .set = setter } }             \
141 },
142 #define ISL_ARG_BOOL_F(st,f,s,l,d,h,fl)                                 \
143         _ISL_ARG_BOOL_F(offsetof(st, f),s,l,NULL,d,h,fl)
144 #define ISL_ARG_BOOL(st,f,s,l,d,h)                                      \
145         ISL_ARG_BOOL_F(st,f,s,l,d,h,0)
146 #define ISL_ARG_PHANTOM_BOOL_F(s,l,setter,h,fl)                         \
147         _ISL_ARG_BOOL_F(-1,s,l,setter,0,h,fl)
148 #define ISL_ARG_PHANTOM_BOOL(s,l,setter,h)                              \
149         ISL_ARG_PHANTOM_BOOL_F(s,l,setter,h,0)
150 #define ISL_ARG_LONG(st,f,s,lo,d,h)     {                               \
151         .type = isl_arg_long,                                           \
152         .short_name = s,                                                \
153         .long_name = lo,                                                \
154         .offset = offsetof(st, f),                                      \
155         .help_msg = h,                                                  \
156         .u = { .l = { .default_value = d, .default_selected = d,        \
157                       .set = NULL } }                                   \
158 },
159 #define ISL_ARG_USER_LONG(st,f,s,lo,setter,d,h) {                       \
160         .type = isl_arg_long,                                           \
161         .short_name = s,                                                \
162         .long_name = lo,                                                \
163         .offset = offsetof(st, f),                                      \
164         .help_msg = h,                                                  \
165         .u = { .l = { .default_value = d, .default_selected = d,        \
166                       .set = setter } }                                 \
167 },
168 #define ISL_ARG_OPT_LONG(st,f,s,lo,d,ds,h)      {                       \
169         .type = isl_arg_long,                                           \
170         .short_name = s,                                                \
171         .long_name = lo,                                                \
172         .offset = offsetof(st, f),                                      \
173         .help_msg = h,                                                  \
174         .u = { .l = { .default_value = d, .default_selected = ds,       \
175                       .set = NULL } }                                   \
176 },
177 #define ISL_ARG_ULONG(st,f,s,l,d,h)     {                               \
178         .type = isl_arg_ulong,                                          \
179         .short_name = s,                                                \
180         .long_name = l,                                                 \
181         .offset = offsetof(st, f),                                      \
182         .help_msg = h,                                                  \
183         .u = { .ul = { .default_value = d } }                           \
184 },
185 #define ISL_ARG_STR(st,f,s,l,a,d,h)     {                               \
186         .type = isl_arg_str,                                            \
187         .short_name = s,                                                \
188         .long_name = l,                                                 \
189         .argument_name = a,                                             \
190         .offset = offsetof(st, f),                                      \
191         .help_msg = h,                                                  \
192         .u = { .str = { .default_value = d } }                          \
193 },
194 #define ISL_ARG_CHILD(st,f,l,c,h)       {                               \
195         .type = isl_arg_child,                                          \
196         .long_name = l,                                                 \
197         .offset = offsetof(st, f),                                      \
198         .help_msg = h,                                                  \
199         .u = { .child = { .child = c, .size = sizeof(*((st *)NULL)->f) } }\
200 },
201 #define ISL_ARG_FLAGS(st,f,s,l,c,d,h)   {                               \
202         .type = isl_arg_flags,                                          \
203         .short_name = s,                                                \
204         .long_name = l,                                                 \
205         .offset = offsetof(st, f),                                      \
206         .help_msg = h,                                                  \
207         .u = { .flags = { .flags = c, .default_value = d } }            \
208 },
209 #define ISL_ARG_USER(st,f,i,c) {                                        \
210         .type = isl_arg_user,                                           \
211         .offset = offsetof(st, f),                                      \
212         .u = { .user = { .init = i, .clear = c} }                       \
213 },
214 #define ISL_ARG_VERSION(print) {                                        \
215         .type = isl_arg_version,                                        \
216         .u = { .version = { .print_version = print } }                  \
217 },
218 #define ISL_ARG_END     { isl_arg_end }
219
220 #define ISL_ARG_ALL     (1 << 0)
221
222 void isl_arg_set_defaults(struct isl_arg *arg, void *opt);
223 void isl_arg_free(struct isl_arg *arg, void *opt);
224 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
225         unsigned flags);
226
227 #define ISL_ARG_DECL(prefix,st,arg)                                     \
228 extern struct isl_arg arg[];                                            \
229 st *prefix ## _new_with_defaults();                                     \
230 void prefix ## _free(st *opt);                                          \
231 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags);
232
233 #define ISL_ARG_DEF(prefix,st,arg)                                      \
234 st *prefix ## _new_with_defaults()                                      \
235 {                                                                       \
236         st *opt = (st *)calloc(1, sizeof(st));                          \
237         if (opt)                                                        \
238                 isl_arg_set_defaults(arg, opt);                         \
239         return opt;                                                     \
240 }                                                                       \
241                                                                         \
242 void prefix ## _free(st *opt)                                           \
243 {                                                                       \
244         isl_arg_free(arg, opt);                                         \
245 }                                                                       \
246                                                                         \
247 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags)    \
248 {                                                                       \
249         return isl_arg_parse(arg, argc, argv, opt, flags);              \
250 }
251
252 #if defined(__cplusplus)
253 }
254 #endif
255
256 #endif