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_str.h>
22 #include <ecoli_node_option.h>
23 #include <ecoli_node_or.h>
24 #include <ecoli_node_many.h>
25 #include <ecoli_node_seq.h>
27 EC_LOG_TYPE_REGISTER(node_seq);
31 struct ec_node **table;
36 ec_node_seq_parse(const struct ec_node *gen_node,
37 struct ec_parse *state,
38 const struct ec_strvec *strvec)
40 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
41 struct ec_strvec *childvec = NULL;
46 for (i = 0; i < node->len; i++) {
47 childvec = ec_strvec_ndup(strvec, len,
48 ec_strvec_len(strvec) - len);
52 ret = ec_node_parse_child(node->table[i], state, childvec);
56 ec_strvec_free(childvec);
59 if (ret == EC_PARSE_NOMATCH) {
60 ec_parse_free_children(state);
61 return EC_PARSE_NOMATCH;
70 ec_strvec_free(childvec);
75 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
77 const struct ec_strvec *strvec)
79 struct ec_parse *parse = ec_comp_get_state(comp);
80 struct ec_strvec *childvec = NULL;
88 * Example of completion for a sequence node = [n1,n2] and an
91 * result = complete(n1, [a,b,c,d]) +
92 * complete(n2, [b,c,d]) if n1 matches [a] +
93 * complete(n2, [c,d]) if n1 matches [a,b] +
94 * complete(n2, [d]) if n1 matches [a,b,c] +
95 * complete(n2, []) if n1 matches [a,b,c,d]
98 /* first, try to complete with the first node of the table */
99 ret = ec_node_complete_child(table[0], comp, strvec);
103 /* then, if the first node of the table matches the beginning of the
104 * strvec, try to complete the rest */
105 for (i = 0; i < ec_strvec_len(strvec); i++) {
106 childvec = ec_strvec_ndup(strvec, 0, i);
107 if (childvec == NULL)
110 ret = ec_node_parse_child(table[0], parse, childvec);
114 ec_strvec_free(childvec);
117 if ((unsigned int)ret != i) {
118 if (ret != EC_PARSE_NOMATCH)
119 ec_parse_del_last_child(parse);
123 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
124 if (childvec == NULL) {
125 ec_parse_del_last_child(parse);
129 ret = __ec_node_seq_complete(&table[1],
132 ec_parse_del_last_child(parse);
133 ec_strvec_free(childvec);
143 ec_strvec_free(childvec);
148 ec_node_seq_complete(const struct ec_node *gen_node,
149 struct ec_comp *comp,
150 const struct ec_strvec *strvec)
152 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
154 return __ec_node_seq_complete(node->table, node->len, comp,
158 static void ec_node_seq_free_priv(struct ec_node *gen_node)
160 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
163 for (i = 0; i < node->len; i++)
164 ec_node_free(node->table[i]);
165 ec_free(node->table);
168 static const struct ec_config_schema ec_node_seq_subschema[] = {
170 .desc = "A child node which is part of the sequence.",
171 .type = EC_CONFIG_TYPE_NODE,
175 static const struct ec_config_schema ec_node_seq_schema[] = {
178 .desc = "The list of children nodes, to be parsed in sequence.",
179 .type = EC_CONFIG_TYPE_LIST,
180 .subschema = ec_node_seq_subschema,
181 .subschema_len = EC_COUNT_OF(ec_node_seq_subschema),
185 static int ec_node_seq_set_config(struct ec_node *gen_node,
186 const struct ec_config *config)
188 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
189 const struct ec_config *children = NULL, *child;
190 struct ec_node **table = NULL;
193 children = ec_config_dict_get(config, "children");
194 if (children == NULL) {
200 TAILQ_FOREACH(child, &children->list, next)
203 table = ec_malloc(n * sizeof(*table));
208 TAILQ_FOREACH(child, &children->list, next) {
209 table[n] = ec_node_clone(child->node);
213 for (i = 0; i < node->len; i++)
214 ec_node_free(node->table[i]);
215 ec_free(node->table);
223 for (i = 0; i < n; i++)
224 ec_node_free(table[i]);
231 ec_node_seq_get_children_count(const struct ec_node *gen_node)
233 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
238 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i,
239 struct ec_node **child, unsigned int *refs)
241 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
246 *child = node->table[i];
247 /* each child node is referenced twice: once in the config and
248 * once in the node->table[] */
253 static struct ec_node_type ec_node_seq_type = {
255 .schema = ec_node_seq_schema,
256 .schema_len = EC_COUNT_OF(ec_node_seq_schema),
257 .set_config = ec_node_seq_set_config,
258 .parse = ec_node_seq_parse,
259 .complete = ec_node_seq_complete,
260 .size = sizeof(struct ec_node_seq),
261 .free_priv = ec_node_seq_free_priv,
262 .get_children_count = ec_node_seq_get_children_count,
263 .get_child = ec_node_seq_get_child,
266 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
268 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
270 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
271 const struct ec_config *cur_config = NULL;
272 struct ec_config *config = NULL, *children;
275 assert(node != NULL);
277 /* XXX factorize this code in a helper */
279 if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
282 cur_config = ec_node_get_config(gen_node);
283 if (cur_config == NULL)
284 config = ec_config_dict();
286 config = ec_config_dup(cur_config);
290 children = ec_config_dict_get(config, "children");
291 if (children == NULL) {
292 children = ec_config_list();
293 if (children == NULL)
296 if (ec_config_dict_set(config, "children", children) < 0)
297 goto fail; /* children list is freed on error */
300 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
305 ret = ec_node_set_config(gen_node, config);
306 config = NULL; /* freed */
313 ec_config_free(config);
318 struct ec_node *__ec_node_seq(const char *id, ...)
320 struct ec_config *config = NULL, *children = NULL;
321 struct ec_node *gen_node = NULL;
322 struct ec_node *child;
327 child = va_arg(ap, struct ec_node *);
329 gen_node = __ec_node(&ec_node_seq_type, id);
330 if (gen_node == NULL)
331 goto fail_free_children;
333 config = ec_config_dict();
335 goto fail_free_children;
337 children = ec_config_list();
338 if (children == NULL)
339 goto fail_free_children;
341 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
343 goto fail_free_children;
345 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
347 goto fail_free_children;
351 if (ec_config_dict_set(config, "children", children) < 0) {
352 children = NULL; /* freed */
357 ret = ec_node_set_config(gen_node, config);
358 config = NULL; /* freed */
367 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
370 ec_node_free(gen_node); /* will also free added children */
371 ec_config_free(children);
372 ec_config_free(config);
378 /* LCOV_EXCL_START */
379 static int ec_node_seq_testcase(void)
381 struct ec_node *node = NULL;
384 node = EC_NODE_SEQ(EC_NO_ID,
385 ec_node_str(EC_NO_ID, "foo"),
386 ec_node_str(EC_NO_ID, "bar")
389 EC_LOG(EC_LOG_ERR, "cannot create node\n");
392 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
393 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
394 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
395 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
396 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
397 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
398 testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
400 testres |= (ec_node_seq_add(node, ec_node_str(EC_NO_ID, "grr")) < 0);
401 testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "grr");
405 /* test completion */
406 node = EC_NODE_SEQ(EC_NO_ID,
407 ec_node_str(EC_NO_ID, "foo"),
408 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
409 ec_node_str(EC_NO_ID, "bar")
412 EC_LOG(EC_LOG_ERR, "cannot create node\n");
415 testres |= EC_TEST_CHECK_COMPLETE(node,
417 "foo", EC_NODE_ENDLIST);
418 testres |= EC_TEST_CHECK_COMPLETE(node,
419 "f", EC_NODE_ENDLIST,
420 "foo", EC_NODE_ENDLIST);
421 testres |= EC_TEST_CHECK_COMPLETE(node,
422 "foo", EC_NODE_ENDLIST,
423 "foo", EC_NODE_ENDLIST);
424 testres |= EC_TEST_CHECK_COMPLETE(node,
425 "foo", "", EC_NODE_ENDLIST,
426 "bar", "toto", EC_NODE_ENDLIST);
427 testres |= EC_TEST_CHECK_COMPLETE(node,
428 "foo", "t", EC_NODE_ENDLIST,
429 "toto", EC_NODE_ENDLIST);
430 testres |= EC_TEST_CHECK_COMPLETE(node,
431 "foo", "b", EC_NODE_ENDLIST,
432 "bar", EC_NODE_ENDLIST);
433 testres |= EC_TEST_CHECK_COMPLETE(node,
434 "foo", "bar", EC_NODE_ENDLIST,
435 "bar", EC_NODE_ENDLIST);
436 testres |= EC_TEST_CHECK_COMPLETE(node,
437 "x", EC_NODE_ENDLIST,
439 testres |= EC_TEST_CHECK_COMPLETE(node,
440 "foobarx", EC_NODE_ENDLIST,
448 static struct ec_test ec_node_seq_test = {
450 .test = ec_node_seq_testcase,
453 EC_TEST_REGISTER(ec_node_seq_test);