1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
12 #include <ecoli_malloc.h>
13 #include <ecoli_string.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_keyval.h>
16 #include <ecoli_log.h>
17 #include <ecoli_config.h>
18 #include <ecoli_test.h>
19 #include <ecoli_node.h>
21 #include <ecoli_node_str.h>
22 #include <ecoli_node_seq.h>
24 EC_LOG_TYPE_REGISTER(node);
26 static struct ec_node_type_list node_type_list =
27 TAILQ_HEAD_INITIALIZER(node_type_list);
29 const struct ec_node_type *
30 ec_node_type_lookup(const char *name)
32 struct ec_node_type *type;
34 TAILQ_FOREACH(type, &node_type_list, next) {
35 if (!strcmp(name, type->name))
43 int ec_node_type_register(struct ec_node_type *type)
45 EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
47 if (ec_node_type_lookup(type->name) != NULL) {
52 TAILQ_INSERT_TAIL(&node_type_list, type, next);
57 void ec_node_type_dump(FILE *out)
59 struct ec_node_type *type;
61 TAILQ_FOREACH(type, &node_type_list, next)
62 fprintf(out, "%s\n", type->name);
65 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
67 struct ec_node *node = NULL;
69 EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
76 node = ec_calloc(1, type->size);
83 node->id = ec_strdup(id);
87 if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
90 node->attrs = ec_keyval();
91 if (node->attrs == NULL)
94 if (type->init_priv != NULL) {
95 if (type->init_priv(node) < 0)
103 ec_keyval_free(node->attrs);
112 struct ec_node *ec_node(const char *typename, const char *id)
114 const struct ec_node_type *type;
116 type = ec_node_type_lookup(typename);
118 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
123 return __ec_node(type, id);
126 static void traverse_tree1(const struct ec_node *node)
128 const struct ec_node *child;
131 if (node->free.state == 1) {
135 node->free.refcnt = 1;
136 node->free.state = 1; // traversed
137 n = node->n_children;
138 for (i = 0; i < n; i++) {
139 child = node->children[i];
140 traverse_tree1(child);
144 void ec_node_free(struct ec_node *node)
146 struct ec_node *child;
147 bool free_it = false;
153 assert(node->refcnt > 0);
156 if (node->free.state == 0)
157 traverse_tree1(node);
160 if (node->free.state == 2)
163 node->free.state = 2; // beeing freed
164 if (node->refcnt < node->free.refcnt) {
165 for (i = 0; i < n; i++) {
166 child = node->children[i];
171 if (node->refcnt > 0) {
172 node->free.refcnt = 0;
173 node->free.state = 0;
177 if (node->type != NULL && node->type->free_priv != NULL)
178 node->type->free_priv(node);
181 ec_keyval_free(node->attrs);
182 ec_config_free(node->config);
186 struct ec_node *ec_node_clone(struct ec_node *node)
193 size_t ec_node_get_children_count(const struct ec_node *node)
195 if (node->type->get_children_count == NULL)
197 return node->type->get_children_count(node);
201 ec_node_get_child(const struct ec_node *node, size_t i)
203 if (node->type->get_child == NULL)
205 return node->type->get_child(node, i);
209 ec_node_set_config(struct ec_node *node, struct ec_config *config)
211 if (node->type->schema == NULL) {
215 if (ec_config_validate(config, node->type->schema,
216 node->type->schema_len) < 0)
218 if (node->type->set_config != NULL) {
219 if (node->type->set_config(node, config) < 0)
223 ec_config_free(node->config);
224 node->config = config;
229 ec_config_free(config);
233 const struct ec_config *ec_node_get_config(struct ec_node *node)
238 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
240 struct ec_node *child, *ret;
241 const char *node_id = ec_node_id(node);
244 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
247 n = ec_node_get_children_count(node);
248 for (i = 0; i < n; i++) {
249 child = ec_node_get_child(node, i);
250 ret = ec_node_find(child, id);
258 const struct ec_node_type *ec_node_type(const struct ec_node *node)
263 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
268 const char *ec_node_id(const struct ec_node *node)
273 static void __ec_node_dump(FILE *out,
274 const struct ec_node *node, size_t indent)
276 const char *id, *typename;
277 struct ec_node *child;
280 id = ec_node_id(node);
281 typename = node->type->name;
283 fprintf(out, "%*s" "type=%s id=%s %p free=(%d,%d)\n",
284 (int)indent * 4, "", typename, id, node,
285 node->free.state, node->free.refcnt);
286 n = ec_node_get_children_count(node);
287 for (i = 0; i < n; i++) {
288 child = ec_node_get_child(node, i);
289 __ec_node_dump(out, child, indent + 1);
293 void ec_node_dump(FILE *out, const struct ec_node *node)
295 fprintf(out, "------------------- node dump:\n");
298 fprintf(out, "node is NULL\n");
302 __ec_node_dump(out, node, 0);
305 const char *ec_node_desc(const struct ec_node *node)
307 if (node->type->desc != NULL)
308 return node->type->desc(node);
313 int ec_node_check_type(const struct ec_node *node,
314 const struct ec_node_type *type)
316 if (strcmp(node->type->name, type->name)) {
324 /* LCOV_EXCL_START */
325 static int ec_node_testcase(void)
327 struct ec_node *node = NULL;
328 const struct ec_node *child;
329 const struct ec_node_type *type;
336 node = EC_NODE_SEQ(EC_NO_ID,
337 ec_node_str("id_x", "x"),
338 ec_node_str("id_y", "y"));
345 f = open_memstream(&buf, &buflen);
348 ec_node_dump(f, node);
349 ec_node_type_dump(f);
350 ec_node_dump(f, NULL);
354 testres |= EC_TEST_CHECK(
355 strstr(buf, "type=seq id=no-id"), "bad dump\n");
356 testres |= EC_TEST_CHECK(
357 strstr(buf, "type=str id=id_x") &&
358 strstr(strstr(buf, "type=str id=id_x") + 1,
364 testres |= EC_TEST_CHECK(
365 !strcmp(ec_node_type(node)->name, "seq") &&
366 !strcmp(ec_node_id(node), EC_NO_ID) &&
367 !strcmp(ec_node_desc(node), "<seq>"),
370 testres |= EC_TEST_CHECK(
371 ec_node_get_children_count(node) == 2,
372 "bad children count\n");
373 child = ec_node_get_child(node, 0);
374 testres |= EC_TEST_CHECK(child != NULL &&
375 !strcmp(ec_node_type(child)->name, "str") &&
376 !strcmp(ec_node_id(child), "id_x"),
378 child = ec_node_get_child(node, 1);
379 testres |= EC_TEST_CHECK(child != NULL &&
380 !strcmp(ec_node_type(child)->name, "str") &&
381 !strcmp(ec_node_id(child), "id_y"),
383 child = ec_node_get_child(node, 2);
384 testres |= EC_TEST_CHECK(child == NULL,
385 "child 2 should be NULL");
387 child = ec_node_find(node, "id_x");
388 testres |= EC_TEST_CHECK(child != NULL &&
389 !strcmp(ec_node_type(child)->name, "str") &&
390 !strcmp(ec_node_id(child), "id_x") &&
391 !strcmp(ec_node_desc(child), "x"),
393 child = ec_node_find(node, "id_dezdex");
394 testres |= EC_TEST_CHECK(child == NULL,
395 "child with wrong id should be NULL");
397 ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
398 testres |= EC_TEST_CHECK(ret == 0,
399 "cannot set node attribute\n");
401 type = ec_node_type_lookup("seq");
402 testres |= EC_TEST_CHECK(type != NULL &&
403 ec_node_check_type(node, type) == 0,
404 "cannot get seq node type");
405 type = ec_node_type_lookup("str");
406 testres |= EC_TEST_CHECK(type != NULL &&
407 ec_node_check_type(node, type) < 0,
408 "node type should not be str");
413 node = ec_node("deznuindez", EC_NO_ID);
414 testres |= EC_TEST_CHECK(node == NULL,
415 "should not be able to create node\n");
430 static struct ec_test ec_node_test = {
432 .test = ec_node_testcase,
435 EC_TEST_REGISTER(ec_node_test);