add rudimentary argument parsing facility
[platform/upstream/isl.git] / include / isl_arg.h
1 #ifndef ISL_ARG_H
2 #define ISL_ARG_H
3
4 #include <stddef.h>
5
6 struct isl_arg_choice {
7         const char      *name;
8         unsigned         value;
9 };
10
11 enum isl_arg_type {
12         isl_arg_end,
13         isl_arg_choice,
14         isl_arg_bool,
15         isl_arg_child,
16 };
17
18 struct isl_arg {
19         enum isl_arg_type        type;
20         char                     short_name;
21         const char              *long_name;
22         size_t                   offset;
23         union {
24         struct {
25                 struct isl_arg_choice   *choice;
26                 unsigned                 default_value;
27         } choice;
28         struct {
29                 unsigned                 default_value;
30         } b;
31         struct {
32                 struct isl_arg          *child;
33                 size_t                   size;
34         } child;
35         } u;
36 };
37
38 #define ISL_ARG_CHOICE(st,f,s,l,c,d)    {                               \
39         .type = isl_arg_choice,                                         \
40         .short_name = s,                                                \
41         .long_name = l,                                                 \
42         .offset = offsetof(st, f),                                      \
43         .u = { .choice = { .choice = c, .default_value = d } }          \
44 },
45 #define ISL_ARG_BOOL(st,f,s,l,d)        {                               \
46         .type = isl_arg_bool,                                           \
47         .short_name = s,                                                \
48         .long_name = l,                                                 \
49         .offset = offsetof(st, f),                                      \
50         .u = { .b = { .default_value = d } }                            \
51 },
52 #define ISL_ARG_CHILD(st,f,l,c)         {                               \
53         .type = isl_arg_child,                                          \
54         .long_name = l,                                                 \
55         .offset = offsetof(st, f),                                      \
56         .u = { .child = { .child = c, .size = sizeof(*((st *)NULL)->f) } }\
57 },
58 #define ISL_ARG_END     { isl_arg_end }
59
60 void isl_arg_set_defaults(struct isl_arg *arg, void *opt);
61 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt);
62
63 #define ISL_ARG_DECL(prefix,st,arg)                                     \
64 st *prefix ## _new_with_defaults();                                     \
65 int prefix ## _parse(st *opt, int argc, char **argv);
66
67 #define ISL_ARG_DEF(prefix,st,arg)                                      \
68 st *prefix ## _new_with_defaults()                                      \
69 {                                                                       \
70         st *opt = (st *)calloc(1, sizeof(st));                          \
71         if (opt)                                                        \
72                 isl_arg_set_defaults(arg, opt);                         \
73         return opt;                                                     \
74 }                                                                       \
75                                                                         \
76 int prefix ## _parse(st *opt, int argc, char **argv)                    \
77 {                                                                       \
78         return isl_arg_parse(arg, argc, argv, opt);                     \
79 }
80
81 #endif