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 void ec_node_free(struct ec_node *node)
131 assert(node->refcnt > 0);
133 if (--node->refcnt > 0)
136 if (node->type != NULL && node->type->free_priv != NULL)
137 node->type->free_priv(node);
140 ec_keyval_free(node->attrs);
141 ec_config_free(node->config);
145 struct ec_node *ec_node_clone(struct ec_node *node)
152 size_t ec_node_get_children_count(const struct ec_node *node)
154 if (node->type->get_children_count == NULL)
156 return node->type->get_children_count(node);
160 ec_node_get_child(const struct ec_node *node, size_t i)
162 if (node->type->get_child == NULL)
164 return node->type->get_child(node, i);
168 ec_node_set_config(struct ec_node *node, struct ec_config *config)
170 if (node->type->schema == NULL) {
174 if (ec_config_validate(config, node->type->schema,
175 node->type->schema_len) < 0)
177 if (node->type->set_config != NULL) {
178 if (node->type->set_config(node, config) < 0)
182 ec_config_free(node->config);
183 node->config = config;
188 ec_config_free(config);
192 const struct ec_config *ec_node_get_config(struct ec_node *node)
197 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
199 struct ec_node *child, *ret;
200 const char *node_id = ec_node_id(node);
203 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
206 n = ec_node_get_children_count(node);
207 for (i = 0; i < n; i++) {
208 child = ec_node_get_child(node, i);
209 ret = ec_node_find(child, id);
217 const struct ec_node_type *ec_node_type(const struct ec_node *node)
222 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
227 const char *ec_node_id(const struct ec_node *node)
232 static void __ec_node_dump(FILE *out,
233 const struct ec_node *node, size_t indent)
235 const char *id, *typename;
236 struct ec_node *child;
239 id = ec_node_id(node);
240 typename = node->type->name;
242 fprintf(out, "%*s" "type=%s id=%s %p\n",
243 (int)indent * 4, "", typename, id, node);
244 n = ec_node_get_children_count(node);
245 for (i = 0; i < n; i++) {
246 child = ec_node_get_child(node, i);
247 __ec_node_dump(out, child, indent + 1);
251 void ec_node_dump(FILE *out, const struct ec_node *node)
253 fprintf(out, "------------------- node dump:\n");
256 fprintf(out, "node is NULL\n");
260 __ec_node_dump(out, node, 0);
263 const char *ec_node_desc(const struct ec_node *node)
265 if (node->type->desc != NULL)
266 return node->type->desc(node);
271 int ec_node_check_type(const struct ec_node *node,
272 const struct ec_node_type *type)
274 if (strcmp(node->type->name, type->name)) {
282 /* LCOV_EXCL_START */
283 static int ec_node_testcase(void)
285 struct ec_node *node = NULL;
286 const struct ec_node *child;
287 const struct ec_node_type *type;
294 node = EC_NODE_SEQ(EC_NO_ID,
295 ec_node_str("id_x", "x"),
296 ec_node_str("id_y", "y"));
303 f = open_memstream(&buf, &buflen);
306 ec_node_dump(f, node);
307 ec_node_type_dump(f);
308 ec_node_dump(f, NULL);
312 testres |= EC_TEST_CHECK(
313 strstr(buf, "type=seq id=no-id"), "bad dump\n");
314 testres |= EC_TEST_CHECK(
315 strstr(buf, "type=str id=id_x") &&
316 strstr(strstr(buf, "type=str id=id_x") + 1,
322 testres |= EC_TEST_CHECK(
323 !strcmp(ec_node_type(node)->name, "seq") &&
324 !strcmp(ec_node_id(node), EC_NO_ID) &&
325 !strcmp(ec_node_desc(node), "<seq>"),
328 testres |= EC_TEST_CHECK(
329 ec_node_get_children_count(node) == 2,
330 "bad children count\n");
331 child = ec_node_get_child(node, 0);
332 testres |= EC_TEST_CHECK(child != NULL &&
333 !strcmp(ec_node_type(child)->name, "str") &&
334 !strcmp(ec_node_id(child), "id_x"),
336 child = ec_node_get_child(node, 1);
337 testres |= EC_TEST_CHECK(child != NULL &&
338 !strcmp(ec_node_type(child)->name, "str") &&
339 !strcmp(ec_node_id(child), "id_y"),
341 child = ec_node_get_child(node, 2);
342 testres |= EC_TEST_CHECK(child == NULL,
343 "child 2 should be NULL");
345 child = ec_node_find(node, "id_x");
346 testres |= EC_TEST_CHECK(child != NULL &&
347 !strcmp(ec_node_type(child)->name, "str") &&
348 !strcmp(ec_node_id(child), "id_x") &&
349 !strcmp(ec_node_desc(child), "x"),
351 child = ec_node_find(node, "id_dezdex");
352 testres |= EC_TEST_CHECK(child == NULL,
353 "child with wrong id should be NULL");
355 ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
356 testres |= EC_TEST_CHECK(ret == 0,
357 "cannot set node attribute\n");
359 type = ec_node_type_lookup("seq");
360 testres |= EC_TEST_CHECK(type != NULL &&
361 ec_node_check_type(node, type) == 0,
362 "cannot get seq node type");
363 type = ec_node_type_lookup("str");
364 testres |= EC_TEST_CHECK(type != NULL &&
365 ec_node_check_type(node, type) < 0,
366 "node type should not be str");
371 node = ec_node("deznuindez", EC_NO_ID);
372 testres |= EC_TEST_CHECK(node == NULL,
373 "should not be able to create node\n");
388 static struct ec_test ec_node_test = {
390 .test = ec_node_testcase,
393 EC_TEST_REGISTER(ec_node_test);