api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_node_once.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_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_parse.h>
18 #include <ecoli_complete.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_or.h>
21 #include <ecoli_node_many.h>
22 #include <ecoli_config.h>
23 #include <ecoli_node_once.h>
24
25 EC_LOG_TYPE_REGISTER(node_once);
26
27 struct ec_node_once {
28         struct ec_node *child;
29 };
30
31 static unsigned int
32 count_node(struct ec_pnode *parse, const struct ec_node *node)
33 {
34         struct ec_pnode *child;
35         unsigned int count = 0;
36
37         if (parse == NULL)
38                 return 0;
39
40         if (ec_pnode_get_node(parse) == node)
41                 count++;
42
43         EC_PNODE_FOREACH_CHILD(child, parse)
44                 count += count_node(child, node);
45
46         return count;
47 }
48
49 static int
50 ec_node_once_parse(const struct ec_node *node,
51                 struct ec_pnode *state,
52                 const struct ec_strvec *strvec)
53 {
54         struct ec_node_once *priv = ec_node_priv(node);
55         unsigned int count;
56
57         /* count the number of occurences of the node: if already parsed,
58          * do not match
59          */
60         count = count_node(ec_pnode_get_root(state), priv->child);
61         if (count > 0)
62                 return EC_PARSE_NOMATCH;
63
64         return ec_parse_child(priv->child, state, strvec);
65 }
66
67 static int
68 ec_node_once_complete(const struct ec_node *node,
69                 struct ec_comp *comp,
70                 const struct ec_strvec *strvec)
71 {
72         struct ec_node_once *priv = ec_node_priv(node);
73         struct ec_pnode *parse = ec_comp_get_cur_pstate(comp);
74         unsigned int count;
75         int ret;
76
77         /* count the number of occurences of the node: if already parsed,
78          * do not match
79          */
80         count = count_node(ec_pnode_get_root(parse), priv->child);
81         if (count > 0)
82                 return 0;
83
84         ret = ec_complete_child(priv->child, comp, strvec);
85         if (ret < 0)
86                 return ret;
87
88         return 0;
89 }
90
91 static void ec_node_once_free_priv(struct ec_node *node)
92 {
93         struct ec_node_once *priv = ec_node_priv(node);
94
95         ec_node_free(priv->child);
96 }
97
98 static size_t
99 ec_node_once_get_children_count(const struct ec_node *node)
100 {
101         struct ec_node_once *priv = ec_node_priv(node);
102
103         if (priv->child)
104                 return 1;
105         return 0;
106 }
107
108 static int
109 ec_node_once_get_child(const struct ec_node *node, size_t i,
110                 struct ec_node **child, unsigned int *refs)
111 {
112         struct ec_node_once *priv = ec_node_priv(node);
113
114         if (i >= 1)
115                 return -1;
116
117         *child = priv->child;
118         *refs = 2;
119         return 0;
120 }
121
122 static const struct ec_config_schema ec_node_once_schema[] = {
123         {
124                 .key = "child",
125                 .desc = "The child node.",
126                 .type = EC_CONFIG_TYPE_NODE,
127         },
128         {
129                 .type = EC_CONFIG_TYPE_NONE,
130         },
131 };
132
133 static int ec_node_once_set_config(struct ec_node *node,
134                                 const struct ec_config *config)
135 {
136         struct ec_node_once *priv = ec_node_priv(node);
137         const struct ec_config *child;
138
139         child = ec_config_dict_get(config, "child");
140         if (child == NULL)
141                 goto fail;
142         if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
143                 errno = EINVAL;
144                 goto fail;
145         }
146
147         if (priv->child != NULL)
148                 ec_node_free(priv->child);
149         priv->child = ec_node_clone(child->node);
150
151         return 0;
152
153 fail:
154         return -1;
155 }
156
157 static struct ec_node_type ec_node_once_type = {
158         .name = "once",
159         .schema = ec_node_once_schema,
160         .set_config = ec_node_once_set_config,
161         .parse = ec_node_once_parse,
162         .complete = ec_node_once_complete,
163         .size = sizeof(struct ec_node_once),
164         .free_priv = ec_node_once_free_priv,
165         .get_children_count = ec_node_once_get_children_count,
166         .get_child = ec_node_once_get_child,
167 };
168
169 EC_NODE_TYPE_REGISTER(ec_node_once_type);
170
171 int
172 ec_node_once_set_child(struct ec_node *node, struct ec_node *child)
173 {
174         const struct ec_config *cur_config = NULL;
175         struct ec_config *config = NULL;
176         int ret;
177
178         if (ec_node_check_type(node, &ec_node_once_type) < 0)
179                 goto fail;
180
181         cur_config = ec_node_get_config(node);
182         if (cur_config == NULL)
183                 config = ec_config_dict();
184         else
185                 config = ec_config_dup(cur_config);
186         if (config == NULL)
187                 goto fail;
188
189         if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
190                 child = NULL; /* freed */
191                 goto fail;
192         }
193         child = NULL; /* freed */
194
195         ret = ec_node_set_config(node, config);
196         config = NULL; /* freed */
197         if (ret < 0)
198                 goto fail;
199
200         return 0;
201
202 fail:
203         ec_config_free(config);
204         ec_node_free(child);
205         return -1;
206 }
207
208 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
209 {
210         struct ec_node *node = NULL;
211
212         if (child == NULL)
213                 return NULL;
214
215         node = ec_node_from_type(&ec_node_once_type, id);
216         if (node == NULL)
217                 goto fail;
218
219         ec_node_once_set_child(node, child);
220         child = NULL;
221
222         return node;
223
224 fail:
225         ec_node_free(child);
226         return NULL;
227 }
228
229 /* LCOV_EXCL_START */
230 static int ec_node_once_testcase(void)
231 {
232         struct ec_node *node;
233         int testres = 0;
234
235         node = ec_node_many(EC_NO_ID,
236                         EC_NODE_OR(EC_NO_ID,
237                                 ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
238                                 ec_node_str(EC_NO_ID, "bar")
239                                 ), 0, 0
240                 );
241         if (node == NULL) {
242                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
243                 return -1;
244         }
245         testres |= EC_TEST_CHECK_PARSE(node, 0);
246         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
247         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
248         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
249         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "bar");
250         testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "bar");
251         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo", "foo");
252         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
253         testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
254
255         testres |= EC_TEST_CHECK_COMPLETE(node,
256                 "", EC_VA_END,
257                 "foo", "bar", EC_VA_END);
258         testres |= EC_TEST_CHECK_COMPLETE(node,
259                 "f", EC_VA_END,
260                 "foo", EC_VA_END);
261         testres |= EC_TEST_CHECK_COMPLETE(node,
262                 "b", EC_VA_END,
263                 "bar", EC_VA_END);
264         testres |= EC_TEST_CHECK_COMPLETE(node,
265                 "foo", "", EC_VA_END,
266                 "bar", EC_VA_END);
267         testres |= EC_TEST_CHECK_COMPLETE(node,
268                 "bar", "", EC_VA_END,
269                 "foo", "bar", EC_VA_END);
270         ec_node_free(node);
271
272         return testres;
273 }
274 /* LCOV_EXCL_STOP */
275
276 static struct ec_test ec_node_once_test = {
277         .name = "node_once",
278         .test = ec_node_once_testcase,
279 };
280
281 EC_TEST_REGISTER(ec_node_once_test);