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);
169 static const struct ec_config_schema ec_node_seq_subschema[] = {
171 .desc = "A child node which is part of the sequence.",
172 .type = EC_CONFIG_TYPE_NODE,
175 .type = EC_CONFIG_TYPE_NONE,
179 static const struct ec_config_schema ec_node_seq_schema[] = {
182 .desc = "The list of children nodes, to be parsed in sequence.",
183 .type = EC_CONFIG_TYPE_LIST,
184 .subschema = ec_node_seq_subschema,
187 .type = EC_CONFIG_TYPE_NONE,
191 static int ec_node_seq_set_config(struct ec_node *gen_node,
192 const struct ec_config *config)
194 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
195 struct ec_node **table = NULL;
198 table = ec_node_config_node_list_to_table(
199 ec_config_dict_get(config, "children"), &len);
203 for (i = 0; i < node->len; i++)
204 ec_node_free(node->table[i]);
205 ec_free(node->table);
212 for (i = 0; i < len; i++)
213 ec_node_free(table[i]);
219 ec_node_seq_get_children_count(const struct ec_node *gen_node)
221 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
226 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i,
227 struct ec_node **child, unsigned int *refs)
229 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
234 *child = node->table[i];
235 /* each child node is referenced twice: once in the config and
236 * once in the node->table[] */
241 static struct ec_node_type ec_node_seq_type = {
243 .schema = ec_node_seq_schema,
244 .set_config = ec_node_seq_set_config,
245 .parse = ec_node_seq_parse,
246 .complete = ec_node_seq_complete,
247 .size = sizeof(struct ec_node_seq),
248 .free_priv = ec_node_seq_free_priv,
249 .get_children_count = ec_node_seq_get_children_count,
250 .get_child = ec_node_seq_get_child,
253 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
255 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
257 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
258 const struct ec_config *cur_config = NULL;
259 struct ec_config *config = NULL, *children;
262 assert(node != NULL);
264 /* XXX factorize this code in a helper */
266 if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
269 cur_config = ec_node_get_config(gen_node);
270 if (cur_config == NULL)
271 config = ec_config_dict();
273 config = ec_config_dup(cur_config);
277 children = ec_config_dict_get(config, "children");
278 if (children == NULL) {
279 children = ec_config_list();
280 if (children == NULL)
283 if (ec_config_dict_set(config, "children", children) < 0)
284 goto fail; /* children list is freed on error */
287 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
292 ret = ec_node_set_config(gen_node, config);
293 config = NULL; /* freed */
300 ec_config_free(config);
305 struct ec_node *__ec_node_seq(const char *id, ...)
307 struct ec_config *config = NULL, *children = NULL;
308 struct ec_node *gen_node = NULL;
309 struct ec_node *child;
314 child = va_arg(ap, struct ec_node *);
316 gen_node = ec_node_from_type(&ec_node_seq_type, id);
317 if (gen_node == NULL)
318 goto fail_free_children;
320 config = ec_config_dict();
322 goto fail_free_children;
324 children = ec_config_list();
325 if (children == NULL)
326 goto fail_free_children;
328 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
330 goto fail_free_children;
332 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
334 goto fail_free_children;
338 if (ec_config_dict_set(config, "children", children) < 0) {
339 children = NULL; /* freed */
344 ret = ec_node_set_config(gen_node, config);
345 config = NULL; /* freed */
354 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
357 ec_node_free(gen_node); /* will also free added children */
358 ec_config_free(children);
359 ec_config_free(config);
365 /* LCOV_EXCL_START */
366 static int ec_node_seq_testcase(void)
368 struct ec_node *node = NULL;
371 node = EC_NODE_SEQ(EC_NO_ID,
372 ec_node_str(EC_NO_ID, "foo"),
373 ec_node_str(EC_NO_ID, "bar")
376 EC_LOG(EC_LOG_ERR, "cannot create node\n");
379 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
380 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
381 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
382 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
383 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
384 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
385 testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
387 testres |= (ec_node_seq_add(node, ec_node_str(EC_NO_ID, "grr")) < 0);
388 testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "grr");
392 /* test completion */
393 node = EC_NODE_SEQ(EC_NO_ID,
394 ec_node_str(EC_NO_ID, "foo"),
395 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
396 ec_node_str(EC_NO_ID, "bar")
399 EC_LOG(EC_LOG_ERR, "cannot create node\n");
402 testres |= EC_TEST_CHECK_COMPLETE(node,
404 "foo", EC_NODE_ENDLIST);
405 testres |= EC_TEST_CHECK_COMPLETE(node,
406 "f", EC_NODE_ENDLIST,
407 "foo", EC_NODE_ENDLIST);
408 testres |= EC_TEST_CHECK_COMPLETE(node,
409 "foo", EC_NODE_ENDLIST,
410 "foo", EC_NODE_ENDLIST);
411 testres |= EC_TEST_CHECK_COMPLETE(node,
412 "foo", "", EC_NODE_ENDLIST,
413 "bar", "toto", EC_NODE_ENDLIST);
414 testres |= EC_TEST_CHECK_COMPLETE(node,
415 "foo", "t", EC_NODE_ENDLIST,
416 "toto", EC_NODE_ENDLIST);
417 testres |= EC_TEST_CHECK_COMPLETE(node,
418 "foo", "b", EC_NODE_ENDLIST,
419 "bar", EC_NODE_ENDLIST);
420 testres |= EC_TEST_CHECK_COMPLETE(node,
421 "foo", "bar", EC_NODE_ENDLIST,
422 "bar", EC_NODE_ENDLIST);
423 testres |= EC_TEST_CHECK_COMPLETE(node,
424 "x", EC_NODE_ENDLIST,
426 testres |= EC_TEST_CHECK_COMPLETE(node,
427 "foobarx", EC_NODE_ENDLIST,
435 static struct ec_test ec_node_seq_test = {
437 .test = ec_node_seq_testcase,
440 EC_TEST_REGISTER(ec_node_seq_test);