1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
12 #include <ecoli_assert.h>
13 #include <ecoli_malloc.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_keyval.h>
16 #include <ecoli_log.h>
17 #include <ecoli_test.h>
18 #include <ecoli_node.h>
19 #include <ecoli_node_sh_lex.h>
20 #include <ecoli_node_str.h>
21 #include <ecoli_node_seq.h>
22 #include <ecoli_parse.h>
24 EC_LOG_TYPE_REGISTER(parse);
26 TAILQ_HEAD(ec_parse_list, ec_parse);
29 TAILQ_ENTRY(ec_parse) next;
30 struct ec_parse_list children;
31 struct ec_parse *parent;
32 const struct ec_node *node;
33 struct ec_strvec *strvec;
34 struct ec_keyval *attrs;
37 static int __ec_node_parse_child(const struct ec_node *node,
38 struct ec_parse *state,
39 bool is_root, const struct ec_strvec *strvec)
41 struct ec_strvec *match_strvec;
42 struct ec_parse *child = NULL;
45 if (ec_node_type(node)->parse == NULL) {
51 child = ec_parse(node);
55 ec_parse_link_child(state, child);
59 ret = ec_node_type(node)->parse(node, child, strvec);
63 if (ret == EC_PARSE_NOMATCH) {
65 ec_parse_unlink_child(state, child);
71 match_strvec = ec_strvec_ndup(strvec, 0, ret);
72 if (match_strvec == NULL)
75 child->strvec = match_strvec;
81 ec_parse_unlink_child(state, child);
87 int ec_node_parse_child(const struct ec_node *node, struct ec_parse *state,
88 const struct ec_strvec *strvec)
90 assert(state != NULL);
91 return __ec_node_parse_child(node, state, false, strvec);
94 struct ec_parse *ec_node_parse_strvec(const struct ec_node *node,
95 const struct ec_strvec *strvec)
97 struct ec_parse *parse = ec_parse(node);
103 ret = __ec_node_parse_child(node, parse, true, strvec);
105 ec_parse_free(parse);
112 struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str)
114 struct ec_strvec *strvec = NULL;
115 struct ec_parse *parse = NULL;
118 strvec = ec_strvec();
122 if (ec_strvec_add(strvec, str) < 0)
125 parse = ec_node_parse_strvec(node, strvec);
129 ec_strvec_free(strvec);
133 ec_strvec_free(strvec);
134 ec_parse_free(parse);
138 struct ec_parse *ec_parse(const struct ec_node *node)
140 struct ec_parse *parse = NULL;
142 parse = ec_calloc(1, sizeof(*parse));
146 TAILQ_INIT(&parse->children);
149 parse->attrs = ec_keyval();
150 if (parse->attrs == NULL)
157 ec_keyval_free(parse->attrs);
163 static struct ec_parse *
164 __ec_parse_dup(const struct ec_parse *root, const struct ec_parse *ref,
165 struct ec_parse **new_ref)
167 struct ec_parse *dup = NULL;
168 struct ec_parse *child, *dup_child;
169 struct ec_keyval *attrs = NULL;
174 dup = ec_parse(root->node);
181 attrs = ec_keyval_dup(root->attrs);
184 ec_keyval_free(dup->attrs);
187 if (root->strvec != NULL) {
188 dup->strvec = ec_strvec_dup(root->strvec);
189 if (dup->strvec == NULL)
193 TAILQ_FOREACH(child, &root->children, next) {
194 dup_child = __ec_parse_dup(child, ref, new_ref);
195 if (dup_child == NULL)
197 ec_parse_link_child(dup, dup_child);
207 struct ec_parse *ec_parse_dup(const struct ec_parse *parse)
209 const struct ec_parse *root;
210 struct ec_parse *dup_root, *dup = NULL;
212 root = ec_parse_get_root(parse);
213 dup_root = __ec_parse_dup(root, parse, &dup);
214 if (dup_root == NULL)
221 void ec_parse_free_children(struct ec_parse *parse)
223 struct ec_parse *child;
228 while (!TAILQ_EMPTY(&parse->children)) {
229 child = TAILQ_FIRST(&parse->children);
230 TAILQ_REMOVE(&parse->children, child, next);
231 child->parent = NULL;
232 ec_parse_free(child);
236 void ec_parse_free(struct ec_parse *parse)
241 ec_assert_print(parse->parent == NULL,
242 "parent not NULL in ec_parse_free()");
244 ec_parse_free_children(parse);
245 ec_strvec_free(parse->strvec);
246 ec_keyval_free(parse->attrs);
250 static void __ec_parse_dump(FILE *out,
251 const struct ec_parse *parse, size_t indent)
253 struct ec_parse *child;
254 const struct ec_strvec *vec;
255 const char *id = "none", *typename = "none";
257 /* node can be null when parsing is incomplete */
258 if (parse->node != NULL) {
259 id = parse->node->id;
260 typename = ec_node_type(parse->node)->name;
263 fprintf(out, "%*s" "type=%s id=%s vec=",
264 (int)indent * 4, "", typename, id);
265 vec = ec_parse_strvec(parse);
266 ec_strvec_dump(out, vec);
268 TAILQ_FOREACH(child, &parse->children, next)
269 __ec_parse_dump(out, child, indent + 1);
272 void ec_parse_dump(FILE *out, const struct ec_parse *parse)
274 fprintf(out, "------------------- parse dump:\n");
277 fprintf(out, "parse is NULL\n");
281 /* only exist if it does not match (strvec == NULL) and if it
282 * does not have children: an incomplete parse, like those
283 * generated by complete() don't match but have children that
285 if (!ec_parse_matches(parse) && TAILQ_EMPTY(&parse->children)) {
286 fprintf(out, "no match\n");
290 __ec_parse_dump(out, parse, 0);
293 void ec_parse_link_child(struct ec_parse *parse,
294 struct ec_parse *child)
296 TAILQ_INSERT_TAIL(&parse->children, child, next);
297 child->parent = parse;
300 void ec_parse_unlink_child(struct ec_parse *parse,
301 struct ec_parse *child)
303 TAILQ_REMOVE(&parse->children, child, next);
304 child->parent = NULL;
308 ec_parse_get_first_child(const struct ec_parse *parse)
310 return TAILQ_FIRST(&parse->children);
314 ec_parse_get_last_child(const struct ec_parse *parse)
316 return TAILQ_LAST(&parse->children, ec_parse_list);
319 struct ec_parse *ec_parse_get_next(const struct ec_parse *parse)
321 return TAILQ_NEXT(parse, next);
324 bool ec_parse_has_child(const struct ec_parse *parse)
326 return !TAILQ_EMPTY(&parse->children);
329 const struct ec_node *ec_parse_get_node(const struct ec_parse *parse)
334 void ec_parse_del_last_child(struct ec_parse *parse)
336 struct ec_parse *child;
338 child = ec_parse_get_last_child(parse);
339 ec_parse_unlink_child(parse, child);
340 ec_parse_free(child);
343 struct ec_parse *__ec_parse_get_root(struct ec_parse *parse)
348 while (parse->parent != NULL)
349 parse = parse->parent;
354 struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse)
359 return parse->parent;
362 struct ec_parse *ec_parse_iter_next(struct ec_parse *parse)
364 struct ec_parse *child, *parent, *next;
366 child = TAILQ_FIRST(&parse->children);
369 parent = parse->parent;
370 while (parent != NULL) {
371 next = TAILQ_NEXT(parse, next);
375 parent = parse->parent;
380 struct ec_parse *ec_parse_find_first(struct ec_parse *parse,
383 struct ec_parse *iter;
388 for (iter = parse; iter != NULL; iter = ec_parse_iter_next(iter)) {
389 if (iter->node != NULL &&
390 iter->node->id != NULL &&
391 !strcmp(iter->node->id, id))
399 ec_parse_get_attrs(struct ec_parse *parse)
407 const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse)
409 if (parse == NULL || parse->strvec == NULL)
412 return parse->strvec;
415 /* number of strings in the parse vector */
416 size_t ec_parse_len(const struct ec_parse *parse)
418 if (parse == NULL || parse->strvec == NULL)
421 return ec_strvec_len(parse->strvec);
424 size_t ec_parse_matches(const struct ec_parse *parse)
429 if (parse->strvec == NULL)
435 /* LCOV_EXCL_START */
436 static int ec_parse_testcase(void)
438 struct ec_node *node = NULL;
439 struct ec_parse *p = NULL, *p2 = NULL;
440 const struct ec_parse *pc;
447 node = ec_node_sh_lex(EC_NO_ID,
448 EC_NODE_SEQ(EC_NO_ID,
449 ec_node_str("id_x", "x"),
450 ec_node_str("id_y", "y")));
454 p = ec_node_parse(node, "xcdscds");
455 testres |= EC_TEST_CHECK(
456 p != NULL && !ec_parse_matches(p),
457 "parse should not match\n");
459 f = open_memstream(&buf, &buflen);
466 testres |= EC_TEST_CHECK(
467 strstr(buf, "no match"), "bad dump\n");
472 p = ec_node_parse(node, "x y");
473 testres |= EC_TEST_CHECK(
474 p != NULL && ec_parse_matches(p),
475 "parse should match\n");
476 testres |= EC_TEST_CHECK(
477 ec_parse_len(p) == 1, "bad parse len\n");
479 ret = ec_keyval_set(ec_parse_get_attrs(p), "key", "val", NULL);
480 testres |= EC_TEST_CHECK(ret == 0,
481 "cannot set parse attribute\n");
483 p2 = ec_parse_dup(p);
484 testres |= EC_TEST_CHECK(
485 p2 != NULL && ec_parse_matches(p2),
486 "parse should match\n");
490 pc = ec_parse_find_first(p, "id_x");
491 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_x");
492 testres |= EC_TEST_CHECK(pc != NULL &&
493 ec_parse_get_parent(pc) != NULL &&
494 ec_parse_get_parent(ec_parse_get_parent(pc)) == p,
497 pc = ec_parse_find_first(p, "id_y");
498 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_y");
499 pc = ec_parse_find_first(p, "id_dezdezdez");
500 testres |= EC_TEST_CHECK(pc == NULL, "should not find bad id");
503 f = open_memstream(&buf, &buflen);
510 testres |= EC_TEST_CHECK(
511 strstr(buf, "type=sh_lex id=no-id") &&
512 strstr(buf, "type=seq id=no-id") &&
513 strstr(buf, "type=str id=id_x") &&
514 strstr(buf, "type=str id=id_x"),
535 static struct ec_test ec_parse_test = {
537 .test = ec_parse_testcase,
540 EC_TEST_REGISTER(ec_parse_test);