1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
13 #include <ecoli_malloc.h>
14 #include <ecoli_log.h>
15 #include <ecoli_test.h>
16 #include <ecoli_strvec.h>
17 #include <ecoli_node.h>
18 #include <ecoli_config.h>
19 #include <ecoli_parse.h>
20 #include <ecoli_complete.h>
21 #include <ecoli_node_helper.h>
22 #include <ecoli_node_str.h>
23 #include <ecoli_node_option.h>
24 #include <ecoli_node_or.h>
25 #include <ecoli_node_many.h>
26 #include <ecoli_node_seq.h>
28 EC_LOG_TYPE_REGISTER(node_seq);
32 struct ec_node **table;
37 ec_node_seq_parse(const struct ec_node *gen_node,
38 struct ec_parse *state,
39 const struct ec_strvec *strvec)
41 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
42 struct ec_strvec *childvec = NULL;
47 for (i = 0; i < node->len; i++) {
48 childvec = ec_strvec_ndup(strvec, len,
49 ec_strvec_len(strvec) - len);
53 ret = ec_node_parse_child(node->table[i], state, childvec);
57 ec_strvec_free(childvec);
60 if (ret == EC_PARSE_NOMATCH) {
61 ec_parse_free_children(state);
62 return EC_PARSE_NOMATCH;
71 ec_strvec_free(childvec);
76 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
78 const struct ec_strvec *strvec)
80 struct ec_parse *parse = ec_comp_get_state(comp);
81 struct ec_strvec *childvec = NULL;
89 * Example of completion for a sequence node = [n1,n2] and an
92 * result = complete(n1, [a,b,c,d]) +
93 * complete(n2, [b,c,d]) if n1 matches [a] +
94 * complete(n2, [c,d]) if n1 matches [a,b] +
95 * complete(n2, [d]) if n1 matches [a,b,c] +
96 * complete(n2, []) if n1 matches [a,b,c,d]
99 /* first, try to complete with the first node of the table */
100 ret = ec_node_complete_child(table[0], comp, strvec);
104 /* then, if the first node of the table matches the beginning of the
105 * strvec, try to complete the rest */
106 for (i = 0; i < ec_strvec_len(strvec); i++) {
107 childvec = ec_strvec_ndup(strvec, 0, i);
108 if (childvec == NULL)
111 ret = ec_node_parse_child(table[0], parse, childvec);
115 ec_strvec_free(childvec);
118 if ((unsigned int)ret != i) {
119 if (ret != EC_PARSE_NOMATCH)
120 ec_parse_del_last_child(parse);
124 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
125 if (childvec == NULL) {
126 ec_parse_del_last_child(parse);
130 ret = __ec_node_seq_complete(&table[1],
133 ec_parse_del_last_child(parse);
134 ec_strvec_free(childvec);
144 ec_strvec_free(childvec);
149 ec_node_seq_complete(const struct ec_node *gen_node,
150 struct ec_comp *comp,
151 const struct ec_strvec *strvec)
153 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
155 return __ec_node_seq_complete(node->table, node->len, comp,
159 static void ec_node_seq_free_priv(struct ec_node *gen_node)
161 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
164 for (i = 0; i < node->len; i++)
165 ec_node_free(node->table[i]);
166 ec_free(node->table);
171 static const struct ec_config_schema ec_node_seq_subschema[] = {
173 .desc = "A child node which is part of the sequence.",
174 .type = EC_CONFIG_TYPE_NODE,
177 .type = EC_CONFIG_TYPE_NONE,
181 static const struct ec_config_schema ec_node_seq_schema[] = {
184 .desc = "The list of children nodes, to be parsed in sequence.",
185 .type = EC_CONFIG_TYPE_LIST,
186 .subschema = ec_node_seq_subschema,
189 .type = EC_CONFIG_TYPE_NONE,
193 static int ec_node_seq_set_config(struct ec_node *gen_node,
194 const struct ec_config *config)
196 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
197 struct ec_node **table = NULL;
200 table = ec_node_config_node_list_to_table(
201 ec_config_dict_get(config, "children"), &len);
205 for (i = 0; i < node->len; i++)
206 ec_node_free(node->table[i]);
207 ec_free(node->table);
214 for (i = 0; i < len; i++)
215 ec_node_free(table[i]);
221 ec_node_seq_get_children_count(const struct ec_node *gen_node)
223 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
228 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i,
229 struct ec_node **child, unsigned int *refs)
231 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
236 *child = node->table[i];
237 /* each child node is referenced twice: once in the config and
238 * once in the node->table[] */
243 static struct ec_node_type ec_node_seq_type = {
245 .schema = ec_node_seq_schema,
246 .set_config = ec_node_seq_set_config,
247 .parse = ec_node_seq_parse,
248 .complete = ec_node_seq_complete,
249 .size = sizeof(struct ec_node_seq),
250 .free_priv = ec_node_seq_free_priv,
251 .get_children_count = ec_node_seq_get_children_count,
252 .get_child = ec_node_seq_get_child,
255 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
257 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
259 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
260 const struct ec_config *cur_config = NULL;
261 struct ec_config *config = NULL, *children;
264 assert(node != NULL);
266 /* XXX factorize this code in a helper */
268 if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
271 cur_config = ec_node_get_config(gen_node);
272 if (cur_config == NULL)
273 config = ec_config_dict();
275 config = ec_config_dup(cur_config);
279 children = ec_config_dict_get(config, "children");
280 if (children == NULL) {
281 children = ec_config_list();
282 if (children == NULL)
285 if (ec_config_dict_set(config, "children", children) < 0)
286 goto fail; /* children list is freed on error */
289 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
294 ret = ec_node_set_config(gen_node, config);
295 config = NULL; /* freed */
302 ec_config_free(config);
307 struct ec_node *__ec_node_seq(const char *id, ...)
309 struct ec_config *config = NULL, *children = NULL;
310 struct ec_node *gen_node = NULL;
311 struct ec_node *child;
316 child = va_arg(ap, struct ec_node *);
318 gen_node = ec_node_from_type(&ec_node_seq_type, id);
319 if (gen_node == NULL)
320 goto fail_free_children;
322 config = ec_config_dict();
324 goto fail_free_children;
326 children = ec_config_list();
327 if (children == NULL)
328 goto fail_free_children;
330 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
332 goto fail_free_children;
334 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
336 goto fail_free_children;
340 if (ec_config_dict_set(config, "children", children) < 0) {
341 children = NULL; /* freed */
346 ret = ec_node_set_config(gen_node, config);
347 config = NULL; /* freed */
356 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
359 ec_node_free(gen_node); /* will also free added children */
360 ec_config_free(children);
361 ec_config_free(config);
367 /* LCOV_EXCL_START */
368 static int ec_node_seq_testcase(void)
370 struct ec_node *node = NULL;
373 node = EC_NODE_SEQ(EC_NO_ID,
374 ec_node_str(EC_NO_ID, "foo"),
375 ec_node_str(EC_NO_ID, "bar")
378 EC_LOG(EC_LOG_ERR, "cannot create node\n");
381 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
382 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
383 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
384 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
385 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
386 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
387 testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
389 testres |= (ec_node_seq_add(node, ec_node_str(EC_NO_ID, "grr")) < 0);
390 testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "grr");
394 /* test completion */
395 node = EC_NODE_SEQ(EC_NO_ID,
396 ec_node_str(EC_NO_ID, "foo"),
397 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
398 ec_node_str(EC_NO_ID, "bar")
401 EC_LOG(EC_LOG_ERR, "cannot create node\n");
404 testres |= EC_TEST_CHECK_COMPLETE(node,
406 "foo", EC_NODE_ENDLIST);
407 testres |= EC_TEST_CHECK_COMPLETE(node,
408 "f", EC_NODE_ENDLIST,
409 "foo", EC_NODE_ENDLIST);
410 testres |= EC_TEST_CHECK_COMPLETE(node,
411 "foo", EC_NODE_ENDLIST,
412 "foo", EC_NODE_ENDLIST);
413 testres |= EC_TEST_CHECK_COMPLETE(node,
414 "foo", "", EC_NODE_ENDLIST,
415 "bar", "toto", EC_NODE_ENDLIST);
416 testres |= EC_TEST_CHECK_COMPLETE(node,
417 "foo", "t", EC_NODE_ENDLIST,
418 "toto", EC_NODE_ENDLIST);
419 testres |= EC_TEST_CHECK_COMPLETE(node,
420 "foo", "b", EC_NODE_ENDLIST,
421 "bar", EC_NODE_ENDLIST);
422 testres |= EC_TEST_CHECK_COMPLETE(node,
423 "foo", "bar", EC_NODE_ENDLIST,
424 "bar", EC_NODE_ENDLIST);
425 testres |= EC_TEST_CHECK_COMPLETE(node,
426 "x", EC_NODE_ENDLIST,
428 testres |= EC_TEST_CHECK_COMPLETE(node,
429 "foobarx", EC_NODE_ENDLIST,
437 static struct ec_test ec_node_seq_test = {
439 .test = ec_node_seq_testcase,
442 EC_TEST_REGISTER(ec_node_seq_test);