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_parse.h>
19 #include <ecoli_complete.h>
20 #include <ecoli_node_str.h>
21 #include <ecoli_node_option.h>
22 #include <ecoli_node_or.h>
23 #include <ecoli_node_many.h>
24 #include <ecoli_node_seq.h>
26 EC_LOG_TYPE_REGISTER(node_seq);
30 struct ec_node **table;
35 ec_node_seq_parse(const struct ec_node *gen_node,
36 struct ec_parse *state,
37 const struct ec_strvec *strvec)
39 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
40 struct ec_strvec *childvec = NULL;
45 for (i = 0; i < node->len; i++) {
46 childvec = ec_strvec_ndup(strvec, len,
47 ec_strvec_len(strvec) - len);
51 ret = ec_node_parse_child(node->table[i], state, childvec);
55 ec_strvec_free(childvec);
58 if (ret == EC_PARSE_NOMATCH) {
59 ec_parse_free_children(state);
60 return EC_PARSE_NOMATCH;
69 ec_strvec_free(childvec);
74 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
76 const struct ec_strvec *strvec)
78 struct ec_parse *parse = ec_comp_get_state(comp);
79 struct ec_strvec *childvec = NULL;
87 * Example of completion for a sequence node = [n1,n2] and an
90 * result = complete(n1, [a,b,c,d]) +
91 * complete(n2, [b,c,d]) if n1 matches [a] +
92 * complete(n2, [c,d]) if n1 matches [a,b] +
93 * complete(n2, [d]) if n1 matches [a,b,c] +
94 * complete(n2, []) if n1 matches [a,b,c,d]
97 /* first, try to complete with the first node of the table */
98 ret = ec_node_complete_child(table[0], comp, strvec);
102 /* then, if the first node of the table matches the beginning of the
103 * strvec, try to complete the rest */
104 for (i = 0; i < ec_strvec_len(strvec); i++) {
105 childvec = ec_strvec_ndup(strvec, 0, i);
106 if (childvec == NULL)
109 ret = ec_node_parse_child(table[0], parse, childvec);
113 ec_strvec_free(childvec);
116 if ((unsigned int)ret != i) {
117 if (ret != EC_PARSE_NOMATCH)
118 ec_parse_del_last_child(parse);
122 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
123 if (childvec == NULL) {
124 ec_parse_del_last_child(parse);
128 ret = __ec_node_seq_complete(&table[1],
131 ec_parse_del_last_child(parse);
132 ec_strvec_free(childvec);
142 ec_strvec_free(childvec);
147 ec_node_seq_complete(const struct ec_node *gen_node,
148 struct ec_comp *comp,
149 const struct ec_strvec *strvec)
151 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
153 return __ec_node_seq_complete(node->table, node->len, comp,
157 static void ec_node_seq_free_priv(struct ec_node *gen_node)
159 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
162 for (i = 0; i < node->len; i++)
163 ec_node_free(node->table[i]);
164 ec_free(node->table);
168 ec_node_seq_get_children_count(const struct ec_node *gen_node)
170 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
174 static struct ec_node *
175 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i)
177 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
182 return node->table[i];
185 static struct ec_node_type ec_node_seq_type = {
187 .parse = ec_node_seq_parse,
188 .complete = ec_node_seq_complete,
189 .size = sizeof(struct ec_node_seq),
190 .free_priv = ec_node_seq_free_priv,
191 .get_children_count = ec_node_seq_get_children_count,
192 .get_child = ec_node_seq_get_child,
195 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
197 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
199 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
200 struct ec_node **table;
202 assert(node != NULL);
209 if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
212 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
217 table[node->len] = child;
227 struct ec_node *__ec_node_seq(const char *id, ...)
229 struct ec_node *gen_node = NULL;
230 struct ec_node_seq *node = NULL;
231 struct ec_node *child;
237 gen_node = __ec_node(&ec_node_seq_type, id);
238 node = (struct ec_node_seq *)gen_node;
242 for (child = va_arg(ap, struct ec_node *);
243 child != EC_NODE_ENDLIST;
244 child = va_arg(ap, struct ec_node *)) {
246 /* on error, don't quit the loop to avoid leaks */
247 if (fail == 1 || child == NULL ||
248 ec_node_seq_add(&node->gen, child) < 0) {
261 ec_node_free(gen_node); /* will also free children */
266 /* LCOV_EXCL_START */
267 static int ec_node_seq_testcase(void)
269 struct ec_node *node;
272 node = EC_NODE_SEQ(EC_NO_ID,
273 ec_node_str(EC_NO_ID, "foo"),
274 ec_node_str(EC_NO_ID, "bar")
277 EC_LOG(EC_LOG_ERR, "cannot create node\n");
280 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
281 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
282 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
283 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
284 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
285 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
286 testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
289 /* test completion */
290 node = EC_NODE_SEQ(EC_NO_ID,
291 ec_node_str(EC_NO_ID, "foo"),
292 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
293 ec_node_str(EC_NO_ID, "bar")
296 EC_LOG(EC_LOG_ERR, "cannot create node\n");
299 testres |= EC_TEST_CHECK_COMPLETE(node,
301 "foo", EC_NODE_ENDLIST);
302 testres |= EC_TEST_CHECK_COMPLETE(node,
303 "f", EC_NODE_ENDLIST,
304 "foo", EC_NODE_ENDLIST);
305 testres |= EC_TEST_CHECK_COMPLETE(node,
306 "foo", EC_NODE_ENDLIST,
307 "foo", EC_NODE_ENDLIST);
308 testres |= EC_TEST_CHECK_COMPLETE(node,
309 "foo", "", EC_NODE_ENDLIST,
310 "bar", "toto", EC_NODE_ENDLIST);
311 testres |= EC_TEST_CHECK_COMPLETE(node,
312 "foo", "t", EC_NODE_ENDLIST,
313 "toto", EC_NODE_ENDLIST);
314 testres |= EC_TEST_CHECK_COMPLETE(node,
315 "foo", "b", EC_NODE_ENDLIST,
316 "bar", EC_NODE_ENDLIST);
317 testres |= EC_TEST_CHECK_COMPLETE(node,
318 "foo", "bar", EC_NODE_ENDLIST,
319 "bar", EC_NODE_ENDLIST);
320 testres |= EC_TEST_CHECK_COMPLETE(node,
321 "x", EC_NODE_ENDLIST,
323 testres |= EC_TEST_CHECK_COMPLETE(node,
324 "foobarx", EC_NODE_ENDLIST,
332 static struct ec_test ec_node_seq_test = {
334 .test = ec_node_seq_testcase,
337 EC_TEST_REGISTER(ec_node_seq_test);