1 /* Copyright (C) 2004 MySQL AB
2    Copyright (C) 2004-2017 Alexey Kopytov <akopytov@gmail.com>
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef OPTIONS_H
20 #define OPTIONS_H
21 
22 #include <stdio.h>
23 #include <stdbool.h>
24 
25 #include "sb_list.h"
26 
27 /* Helper option declaration macros */
28 #define SB_OPT(n, d, v, t)                   \
29   { .name = (n),                             \
30     .desc = (d),                             \
31     .type = SB_ARG_TYPE_##t,                 \
32     .value = (v) }
33 
34 #define SB_OPT_END { .type = SB_ARG_TYPE_NULL }
35 
36 /* Option types definition */
37 
38 typedef enum
39 {
40   SB_ARG_TYPE_NULL,
41   SB_ARG_TYPE_BOOL,
42   SB_ARG_TYPE_INT,
43   SB_ARG_TYPE_SIZE,
44   SB_ARG_TYPE_DOUBLE,
45   SB_ARG_TYPE_STRING,
46   SB_ARG_TYPE_LIST,
47   SB_ARG_TYPE_FILE,
48   SB_ARG_TYPE_MAX
49 } sb_arg_type_t;
50 
51 /* Option validation function */
52 typedef bool sb_opt_validate_t(const char *, const char *);
53 
54 /* Test option definition */
55 typedef struct
56 {
57   const char         *name;
58   const char         *desc;
59   const char         *value;
60   sb_arg_type_t      type;
61   sb_opt_validate_t  *validate;
62 } sb_arg_t;
63 
64 typedef struct
65 {
66   char            *data;
67   char            ignore;
68 
69   sb_list_item_t  listitem;
70 } value_t;
71 
72 typedef struct
73 {
74   char            *name;
75   sb_arg_type_t   type;
76   sb_list_t       values;
77   char            ignore;
78 
79   sb_opt_validate_t *validate;
80 
81   sb_list_item_t  listitem;
82 } option_t;
83 
84 /* Initilize options library */
85 int sb_options_init(void);
86 
87 /* Release resource allocated by the options library */
88 int sb_options_done(void);
89 
90 /* Register set of command line arguments */
91 int sb_register_arg_set(sb_arg_t *set);
92 
93 /* Set value 'value' of type 'type' for option 'name' */
94 option_t *set_option(const char *name, const char *value, sb_arg_type_t type);
95 
96 /* Find option specified by 'name' */
97 option_t *sb_find_option(const char *name);
98 
99 /* Print list of options specified by 'opts' */
100 void sb_print_options(sb_arg_t *opts);
101 
102 int sb_get_value_flag(const char *name);
103 
104 int sb_get_value_int(const char *name);
105 
106 unsigned long long sb_get_value_size(const char *name);
107 
108 double sb_get_value_double(const char *name);
109 
110 char *sb_get_value_string(const char *name);
111 
112 sb_list_t *sb_get_value_list(const char *name);
113 
114 char *sb_print_value_size(char *buf, unsigned int buflen, double value);
115 
116 int sb_opt_to_flag(option_t *);
117 
118 int sb_opt_to_int(option_t *);
119 
120 unsigned long long sb_opt_to_size(option_t *);
121 
122 double sb_opt_to_double(option_t *);
123 
124 char *sb_opt_to_string(option_t *);
125 
126 sb_list_t *sb_opt_to_list(option_t *);
127 
128 bool sb_opt_copy(const char *to, const char *from);
129 
130 sb_list_item_t *sb_options_enum_start(void);
131 
132 sb_list_item_t *sb_options_enum_next(sb_list_item_t *, option_t **);
133 
134 value_t *new_value(void);
135 
136 option_t *new_option(void);
137 
138 void free_values(sb_list_t *);
139 
140 void free_options(sb_list_t *);
141 
142 value_t *add_value(sb_list_t *, const char *);
143 
144 value_t *find_value(sb_list_t *, const char *);
145 
146 option_t *add_option(sb_list_t *, const char *);
147 
148 option_t *find_option(sb_list_t *, const char *);
149 
150 int remove_value(sb_list_t *, char *);
151 
152 int remove_option(sb_list_t *, char *);
153 
154 sb_list_t *read_config(FILE *, sb_list_t *);
155 
156 int write_config(FILE *, sb_list_t *);
157 
158 #endif /* OPTIONS_H */
159 
160