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);
48 if (childvec == NULL) {
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 struct ec_node_type ec_node_seq_type = {
171 .parse = ec_node_seq_parse,
172 .complete = ec_node_seq_complete,
173 .size = sizeof(struct ec_node_seq),
174 .free_priv = ec_node_seq_free_priv,
177 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
179 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
181 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
182 struct ec_node **table;
184 assert(node != NULL);
191 if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
194 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
200 if (ec_node_add_child(gen_node, child) < 0)
203 table[node->len] = child;
213 struct ec_node *__ec_node_seq(const char *id, ...)
215 struct ec_node *gen_node = NULL;
216 struct ec_node_seq *node = NULL;
217 struct ec_node *child;
223 gen_node = __ec_node(&ec_node_seq_type, id);
224 node = (struct ec_node_seq *)gen_node;
228 for (child = va_arg(ap, struct ec_node *);
229 child != EC_NODE_ENDLIST;
230 child = va_arg(ap, struct ec_node *)) {
232 /* on error, don't quit the loop to avoid leaks */
233 if (fail == 1 || child == NULL ||
234 ec_node_seq_add(&node->gen, child) < 0) {
247 ec_node_free(gen_node); /* will also free children */
252 /* LCOV_EXCL_START */
253 static int ec_node_seq_testcase(void)
255 struct ec_node *node;
258 node = EC_NODE_SEQ(EC_NO_ID,
259 ec_node_str(EC_NO_ID, "foo"),
260 ec_node_str(EC_NO_ID, "bar")
263 EC_LOG(EC_LOG_ERR, "cannot create node\n");
266 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
267 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
268 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
269 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
270 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
271 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
272 testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
275 /* test completion */
276 node = EC_NODE_SEQ(EC_NO_ID,
277 ec_node_str(EC_NO_ID, "foo"),
278 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
279 ec_node_str(EC_NO_ID, "bar")
282 EC_LOG(EC_LOG_ERR, "cannot create node\n");
285 testres |= EC_TEST_CHECK_COMPLETE(node,
287 "foo", EC_NODE_ENDLIST);
288 testres |= EC_TEST_CHECK_COMPLETE(node,
289 "f", EC_NODE_ENDLIST,
290 "foo", EC_NODE_ENDLIST);
291 testres |= EC_TEST_CHECK_COMPLETE(node,
292 "foo", EC_NODE_ENDLIST,
293 "foo", EC_NODE_ENDLIST);
294 testres |= EC_TEST_CHECK_COMPLETE(node,
295 "foo", "", EC_NODE_ENDLIST,
296 "bar", "toto", EC_NODE_ENDLIST);
297 testres |= EC_TEST_CHECK_COMPLETE(node,
298 "foo", "t", EC_NODE_ENDLIST,
299 "toto", EC_NODE_ENDLIST);
300 testres |= EC_TEST_CHECK_COMPLETE(node,
301 "foo", "b", EC_NODE_ENDLIST,
302 "bar", EC_NODE_ENDLIST);
303 testres |= EC_TEST_CHECK_COMPLETE(node,
304 "foo", "bar", EC_NODE_ENDLIST,
305 "bar", EC_NODE_ENDLIST);
306 testres |= EC_TEST_CHECK_COMPLETE(node,
307 "x", EC_NODE_ENDLIST,
309 testres |= EC_TEST_CHECK_COMPLETE(node,
310 "foobarx", EC_NODE_ENDLIST,
318 static struct ec_test ec_node_seq_test = {
320 .test = ec_node_seq_testcase,
323 EC_TEST_REGISTER(ec_node_seq_test);