add meson support
[protos/libecoli.git] / src / ecoli_node_option.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_str.h>
19 #include <ecoli_test.h>
20 #include <ecoli_config.h>
21 #include <ecoli_node_option.h>
22
23 EC_LOG_TYPE_REGISTER(node_option);
24
25 struct ec_node_option {
26         struct ec_node gen;
27         struct ec_node *child;
28 };
29
30 static int
31 ec_node_option_parse(const struct ec_node *gen_node,
32                 struct ec_parse *state,
33                 const struct ec_strvec *strvec)
34 {
35         struct ec_node_option *node = (struct ec_node_option *)gen_node;
36         int ret;
37
38         ret = ec_node_parse_child(node->child, state, strvec);
39         if (ret < 0)
40                 return ret;
41
42         if (ret == EC_PARSE_NOMATCH)
43                 return 0;
44
45         return ret;
46 }
47
48 static int
49 ec_node_option_complete(const struct ec_node *gen_node,
50                         struct ec_comp *comp,
51                         const struct ec_strvec *strvec)
52 {
53         struct ec_node_option *node = (struct ec_node_option *)gen_node;
54
55         return ec_node_complete_child(node->child, comp, strvec);
56 }
57
58 static void ec_node_option_free_priv(struct ec_node *gen_node)
59 {
60         struct ec_node_option *node = (struct ec_node_option *)gen_node;
61
62         ec_node_free(node->child);
63 }
64
65 static size_t
66 ec_node_option_get_children_count(const struct ec_node *gen_node)
67 {
68         struct ec_node_option *node = (struct ec_node_option *)gen_node;
69
70         if (node->child)
71                 return 1;
72         return 0;
73 }
74
75 static int
76 ec_node_option_get_child(const struct ec_node *gen_node, size_t i,
77         struct ec_node **child, unsigned int *refs)
78 {
79         struct ec_node_option *node = (struct ec_node_option *)gen_node;
80
81         if (i >= 1)
82                 return -1;
83
84         *child = node->child;
85         *refs = 2;
86         return 0;
87 }
88
89 static const struct ec_config_schema ec_node_option_schema[] = {
90         {
91                 .key = "child",
92                 .desc = "The child node.",
93                 .type = EC_CONFIG_TYPE_NODE,
94         },
95         {
96                 .type = EC_CONFIG_TYPE_NONE,
97         },
98 };
99
100 static int ec_node_option_set_config(struct ec_node *gen_node,
101                                 const struct ec_config *config)
102 {
103         struct ec_node_option *node = (struct ec_node_option *)gen_node;
104         const struct ec_config *child;
105
106         child = ec_config_dict_get(config, "child");
107         if (child == NULL)
108                 goto fail;
109         if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
110                 errno = EINVAL;
111                 goto fail;
112         }
113
114         if (node->child != NULL)
115                 ec_node_free(node->child);
116         node->child = ec_node_clone(child->node);
117
118         return 0;
119
120 fail:
121         return -1;
122 }
123
124 static struct ec_node_type ec_node_option_type = {
125         .name = "option",
126         .schema = ec_node_option_schema,
127         .set_config = ec_node_option_set_config,
128         .parse = ec_node_option_parse,
129         .complete = ec_node_option_complete,
130         .size = sizeof(struct ec_node_option),
131         .free_priv = ec_node_option_free_priv,
132         .get_children_count = ec_node_option_get_children_count,
133         .get_child = ec_node_option_get_child,
134 };
135
136 EC_NODE_TYPE_REGISTER(ec_node_option_type);
137
138 int
139 ec_node_option_set_child(struct ec_node *gen_node, struct ec_node *child)
140 {
141         const struct ec_config *cur_config = NULL;
142         struct ec_config *config = NULL;
143         int ret;
144
145         if (ec_node_check_type(gen_node, &ec_node_option_type) < 0)
146                 goto fail;
147
148         cur_config = ec_node_get_config(gen_node);
149         if (cur_config == NULL)
150                 config = ec_config_dict();
151         else
152                 config = ec_config_dup(cur_config);
153         if (config == NULL)
154                 goto fail;
155
156         if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
157                 child = NULL; /* freed */
158                 goto fail;
159         }
160         child = NULL; /* freed */
161
162         ret = ec_node_set_config(gen_node, config);
163         config = NULL; /* freed */
164         if (ret < 0)
165                 goto fail;
166
167         return 0;
168
169 fail:
170         ec_config_free(config);
171         ec_node_free(child);
172         return -1;
173 }
174
175 struct ec_node *ec_node_option(const char *id, struct ec_node *child)
176 {
177         struct ec_node *gen_node = NULL;
178
179         if (child == NULL)
180                 goto fail;
181
182         gen_node = ec_node_from_type(&ec_node_option_type, id);
183         if (gen_node == NULL)
184                 goto fail;
185
186         ec_node_option_set_child(gen_node, child);
187         child = NULL;
188
189         return gen_node;
190
191 fail:
192         ec_node_free(child);
193         return NULL;
194 }
195
196 /* LCOV_EXCL_START */
197 static int ec_node_option_testcase(void)
198 {
199         struct ec_node *node;
200         int testres = 0;
201
202         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
203         if (node == NULL) {
204                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
205                 return -1;
206         }
207         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
208         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
209         testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
210         testres |= EC_TEST_CHECK_PARSE(node, 0);
211         ec_node_free(node);
212
213         /* test completion */
214         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
215         if (node == NULL) {
216                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
217                 return -1;
218         }
219         testres |= EC_TEST_CHECK_COMPLETE(node,
220                 "", EC_NODE_ENDLIST,
221                 "foo", EC_NODE_ENDLIST);
222         testres |= EC_TEST_CHECK_COMPLETE(node,
223                 "f", EC_NODE_ENDLIST,
224                 "foo", EC_NODE_ENDLIST);
225         testres |= EC_TEST_CHECK_COMPLETE(node,
226                 "b", EC_NODE_ENDLIST,
227                 EC_NODE_ENDLIST);
228         ec_node_free(node);
229
230         return testres;
231 }
232 /* LCOV_EXCL_STOP */
233
234 static struct ec_test ec_node_option_test = {
235         .name = "node_option",
236         .test = ec_node_option_testcase,
237 };
238
239 EC_TEST_REGISTER(ec_node_option_test);