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)
337 void ec_parse_del_last_child(struct ec_parse *parse)
339 struct ec_parse *child;
341 child = ec_parse_get_last_child(parse);
342 ec_parse_unlink_child(parse, child);
343 ec_parse_free(child);
346 struct ec_parse *__ec_parse_get_root(struct ec_parse *parse)
351 while (parse->parent != NULL)
352 parse = parse->parent;
357 struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse)
362 return parse->parent;
365 struct ec_parse *ec_parse_iter_next(struct ec_parse *parse)
367 struct ec_parse *child, *parent, *next;
369 child = TAILQ_FIRST(&parse->children);
372 parent = parse->parent;
373 while (parent != NULL) {
374 next = TAILQ_NEXT(parse, next);
378 parent = parse->parent;
383 struct ec_parse *ec_parse_find_first(struct ec_parse *parse,
386 struct ec_parse *iter;
391 for (iter = parse; iter != NULL; iter = ec_parse_iter_next(iter)) {
392 if (iter->node != NULL &&
393 iter->node->id != NULL &&
394 !strcmp(iter->node->id, id))
402 ec_parse_get_attrs(struct ec_parse *parse)
410 const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse)
412 if (parse == NULL || parse->strvec == NULL)
415 return parse->strvec;
418 /* number of strings in the parse vector */
419 size_t ec_parse_len(const struct ec_parse *parse)
421 if (parse == NULL || parse->strvec == NULL)
424 return ec_strvec_len(parse->strvec);
427 size_t ec_parse_matches(const struct ec_parse *parse)
432 if (parse->strvec == NULL)
438 /* LCOV_EXCL_START */
439 static int ec_parse_testcase(void)
441 struct ec_node *node = NULL;
442 struct ec_parse *p = NULL, *p2 = NULL;
443 const struct ec_parse *pc;
450 node = ec_node_sh_lex(EC_NO_ID,
451 EC_NODE_SEQ(EC_NO_ID,
452 ec_node_str("id_x", "x"),
453 ec_node_str("id_y", "y")));
457 p = ec_node_parse(node, "xcdscds");
458 testres |= EC_TEST_CHECK(
459 p != NULL && !ec_parse_matches(p),
460 "parse should not match\n");
462 f = open_memstream(&buf, &buflen);
469 testres |= EC_TEST_CHECK(
470 strstr(buf, "no match"), "bad dump\n");
475 p = ec_node_parse(node, "x y");
476 testres |= EC_TEST_CHECK(
477 p != NULL && ec_parse_matches(p),
478 "parse should match\n");
479 testres |= EC_TEST_CHECK(
480 ec_parse_len(p) == 1, "bad parse len\n");
482 ret = ec_keyval_set(ec_parse_get_attrs(p), "key", "val", NULL);
483 testres |= EC_TEST_CHECK(ret == 0,
484 "cannot set parse attribute\n");
486 p2 = ec_parse_dup(p);
487 testres |= EC_TEST_CHECK(
488 p2 != NULL && ec_parse_matches(p2),
489 "parse should match\n");
493 pc = ec_parse_find_first(p, "id_x");
494 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_x");
495 testres |= EC_TEST_CHECK(pc != NULL &&
496 ec_parse_get_parent(pc) != NULL &&
497 ec_parse_get_parent(ec_parse_get_parent(pc)) == p,
500 pc = ec_parse_find_first(p, "id_y");
501 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_y");
502 pc = ec_parse_find_first(p, "id_dezdezdez");
503 testres |= EC_TEST_CHECK(pc == NULL, "should not find bad id");
506 f = open_memstream(&buf, &buflen);
513 testres |= EC_TEST_CHECK(
514 strstr(buf, "type=sh_lex id=no-id") &&
515 strstr(buf, "type=seq id=no-id") &&
516 strstr(buf, "type=str id=id_x") &&
517 strstr(buf, "type=str id=id_x"),
538 static struct ec_test ec_parse_test = {
540 .test = ec_parse_testcase,
543 EC_TEST_REGISTER(ec_parse_test);