isl_arg_parse: support ISL_ARG_SINGLE_DASH 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         unsigned                 flags;
55         union {
56         struct {
57                 struct isl_arg_choice   *choice;
58                 unsigned                 default_value;
59                 unsigned                 default_selected;
60                 int (*set)(void *opt, unsigned val);
61         } choice;
62         struct {
63                 struct isl_arg_flags    *flags;
64                 unsigned                 default_value;
65         } flags;
66         struct {
67                 unsigned                 default_value;
68                 int (*set)(void *opt, unsigned val);
69         } b;
70         struct {
71                 long                    default_value;
72                 long                    default_selected;
73                 int (*set)(void *opt, long val);
74         } l;
75         struct {
76                 unsigned long           default_value;
77         } ul;
78         struct {
79                 const char              *default_value;
80         } str;
81         struct {
82                 struct isl_arg          *child;
83                 size_t                   size;
84         } child;
85         struct {
86                 void (*print_version)(void);
87         } version;
88         struct {
89                 int (*init)(void*);
90                 void (*clear)(void*);
91         } user;
92         } u;
93 };
94
95 #define ISL_ARG_ALIAS(l)        {                                       \
96         .type = isl_arg_alias,                                          \
97         .long_name = l,                                                 \
98 },
99 #define ISL_ARG_ARG(st,f,a,d)   {                                       \
100         .type = isl_arg_arg,                                            \
101         .argument_name = a,                                             \
102         .offset = offsetof(st, f),                                      \
103         .u = { .str = { .default_value = d } }                          \
104 },
105 #define ISL_ARG_CHOICE(st,f,s,l,c,d,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 = d, .set = NULL } }      \
113 },
114 #define ISL_ARG_OPT_CHOICE(st,f,s,l,c,d,ds,h)   {                       \
115         .type = isl_arg_choice,                                         \
116         .short_name = s,                                                \
117         .long_name = l,                                                 \
118         .offset = offsetof(st, f),                                      \
119         .help_msg = h,                                                  \
120         .u = { .choice = { .choice = c, .default_value = d,             \
121                             .default_selected = ds, .set = NULL } }     \
122 },
123 #define ISL_ARG_USER_OPT_CHOICE(st,f,s,l,c,setter,d,ds,h)       {       \
124         .type = isl_arg_choice,                                         \
125         .short_name = s,                                                \
126         .long_name = l,                                                 \
127         .offset = offsetof(st, f),                                      \
128         .help_msg = h,                                                  \
129         .u = { .choice = { .choice = c, .default_value = d,             \
130                             .default_selected = ds, .set = setter } }   \
131 },
132 #define _ISL_ARG_BOOL_F(o,s,l,setter,d,h,fl)    {                       \
133         .type = isl_arg_bool,                                           \
134         .short_name = s,                                                \
135         .long_name = l,                                                 \
136         .offset = o,                                                    \
137         .help_msg = h,                                                  \
138         .flags = fl,                                                    \
139         .u = { .b = { .default_value = d, .set = setter } }             \
140 },
141 #define ISL_ARG_BOOL_F(st,f,s,l,d,h,fl)                                 \
142         _ISL_ARG_BOOL_F(offsetof(st, f),s,l,NULL,d,h,fl)
143 #define ISL_ARG_BOOL(st,f,s,l,d,h)                                      \
144         ISL_ARG_BOOL_F(st,f,s,l,d,h,0)
145 #define ISL_ARG_PHANTOM_BOOL_F(s,l,setter,h,fl)                         \
146         _ISL_ARG_BOOL_F(-1,s,l,setter,0,h,fl)
147 #define ISL_ARG_PHANTOM_BOOL(s,l,setter,h)                              \
148         ISL_ARG_PHANTOM_BOOL_F(s,l,setter,h,0)
149 #define ISL_ARG_LONG(st,f,s,lo,d,h)     {                               \
150         .type = isl_arg_long,                                           \
151         .short_name = s,                                                \
152         .long_name = lo,                                                \
153         .offset = offsetof(st, f),                                      \
154         .help_msg = h,                                                  \
155         .u = { .l = { .default_value = d, .default_selected = d,        \
156                       .set = NULL } }                                   \
157 },
158 #define ISL_ARG_USER_LONG(st,f,s,lo,setter,d,h) {                       \
159         .type = isl_arg_long,                                           \
160         .short_name = s,                                                \
161         .long_name = lo,                                                \
162         .offset = offsetof(st, f),                                      \
163         .help_msg = h,                                                  \
164         .u = { .l = { .default_value = d, .default_selected = d,        \
165                       .set = setter } }                                 \
166 },
167 #define ISL_ARG_OPT_LONG(st,f,s,lo,d,ds,h)      {                       \
168         .type = isl_arg_long,                                           \
169         .short_name = s,                                                \
170         .long_name = lo,                                                \
171         .offset = offsetof(st, f),                                      \
172         .help_msg = h,                                                  \
173         .u = { .l = { .default_value = d, .default_selected = ds,       \
174                       .set = NULL } }                                   \
175 },
176 #define ISL_ARG_ULONG(st,f,s,l,d,h)     {                               \
177         .type = isl_arg_ulong,                                          \
178         .short_name = s,                                                \
179         .long_name = l,                                                 \
180         .offset = offsetof(st, f),                                      \
181         .help_msg = h,                                                  \
182         .u = { .ul = { .default_value = d } }                           \
183 },
184 #define ISL_ARG_STR(st,f,s,l,a,d,h)     {                               \
185         .type = isl_arg_str,                                            \
186         .short_name = s,                                                \
187         .long_name = l,                                                 \
188         .argument_name = a,                                             \
189         .offset = offsetof(st, f),                                      \
190         .help_msg = h,                                                  \
191         .u = { .str = { .default_value = d } }                          \
192 },
193 #define ISL_ARG_CHILD(st,f,l,c,h)       {                               \
194         .type = isl_arg_child,                                          \
195         .long_name = l,                                                 \
196         .offset = offsetof(st, f),                                      \
197         .help_msg = h,                                                  \
198         .u = { .child = { .child = c, .size = sizeof(*((st *)NULL)->f) } }\
199 },
200 #define ISL_ARG_FLAGS(st,f,s,l,c,d,h)   {                               \
201         .type = isl_arg_flags,                                          \
202         .short_name = s,                                                \
203         .long_name = l,                                                 \
204         .offset = offsetof(st, f),                                      \
205         .help_msg = h,                                                  \
206         .u = { .flags = { .flags = c, .default_value = d } }            \
207 },
208 #define ISL_ARG_USER(st,f,i,c) {                                        \
209         .type = isl_arg_user,                                           \
210         .offset = offsetof(st, f),                                      \
211         .u = { .user = { .init = i, .clear = c} }                       \
212 },
213 #define ISL_ARG_VERSION(print) {                                        \
214         .type = isl_arg_version,                                        \
215         .u = { .version = { .print_version = print } }                  \
216 },
217 #define ISL_ARG_END     { isl_arg_end }
218
219 #define ISL_ARG_ALL     (1 << 0)
220
221 void isl_arg_set_defaults(struct isl_arg *arg, void *opt);
222 void isl_arg_free(struct isl_arg *arg, void *opt);
223 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
224         unsigned flags);
225
226 #define ISL_ARG_DECL(prefix,st,arg)                                     \
227 extern struct isl_arg arg[];                                            \
228 st *prefix ## _new_with_defaults();                                     \
229 void prefix ## _free(st *opt);                                          \
230 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags);
231
232 #define ISL_ARG_DEF(prefix,st,arg)                                      \
233 st *prefix ## _new_with_defaults()                                      \
234 {                                                                       \
235         st *opt = (st *)calloc(1, sizeof(st));                          \
236         if (opt)                                                        \
237                 isl_arg_set_defaults(arg, opt);                         \
238         return opt;                                                     \
239 }                                                                       \
240                                                                         \
241 void prefix ## _free(st *opt)                                           \
242 {                                                                       \
243         isl_arg_free(arg, opt);                                         \
244 }                                                                       \
245                                                                         \
246 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags)    \
247 {                                                                       \
248         return isl_arg_parse(arg, argc, argv, opt, flags);              \
249 }
250
251 #if defined(__cplusplus)
252 }
253 #endif
254
255 #endif