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);
138 ec_free(node->children);
141 ec_keyval_free(node->attrs);
142 ec_config_free(node->config);
146 struct ec_node *ec_node_clone(struct ec_node *node)
153 size_t ec_node_get_children_count(const struct ec_node *node)
155 return node->n_children;
159 ec_node_get_child(const struct ec_node *node, size_t i)
161 if (i >= ec_node_get_children_count(node))
163 return node->children[i];
166 int ec_node_add_child(struct ec_node *node, struct ec_node *child)
168 struct ec_node **children = NULL;
171 if (node == NULL || child == NULL) {
176 n = node->n_children;
177 children = ec_realloc(node->children,
178 (n + 1) * sizeof(child));
179 if (children == NULL)
183 node->children = children;
184 node->n_children = n + 1;
195 ec_node_set_config(struct ec_node *node, struct ec_config *config)
197 if (node->type->schema == NULL) {
201 if (ec_config_validate(config, node->type->schema,
202 node->type->schema_len) < 0)
204 if (node->type->set_config != NULL) {
205 if (node->type->set_config(node, config) < 0)
209 ec_config_free(node->config);
210 node->config = config;
219 int ec_node_del_child(struct ec_node *node, struct ec_node *child)
223 if (node == NULL || child == NULL)
226 n = node->n_children;
227 for (i = 0; i < n; i++) {
228 if (node->children[i] != child)
230 memcpy(&node->children[i], &node->children[i+1],
231 (n - i - 1) * sizeof(child));
241 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
243 struct ec_node *child, *ret;
244 const char *node_id = ec_node_id(node);
247 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
250 n = node->n_children;
251 for (i = 0; i < n; i++) {
252 child = node->children[i];
253 ret = ec_node_find(child, id);
261 const struct ec_node_type *ec_node_type(const struct ec_node *node)
266 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
271 const char *ec_node_id(const struct ec_node *node)
276 static void __ec_node_dump(FILE *out,
277 const struct ec_node *node, size_t indent)
279 const char *id, *typename;
280 struct ec_node *child;
283 id = ec_node_id(node);
284 typename = node->type->name;
286 fprintf(out, "%*s" "type=%s id=%s %p\n",
287 (int)indent * 4, "", typename, id, node);
288 n = node->n_children;
289 for (i = 0; i < n; i++) {
290 child = node->children[i];
291 __ec_node_dump(out, child, indent + 1);
295 void ec_node_dump(FILE *out, const struct ec_node *node)
297 fprintf(out, "------------------- node dump:\n");
300 fprintf(out, "node is NULL\n");
304 __ec_node_dump(out, node, 0);
307 const char *ec_node_desc(const struct ec_node *node)
309 if (node->type->desc != NULL)
310 return node->type->desc(node);
315 int ec_node_check_type(const struct ec_node *node,
316 const struct ec_node_type *type)
318 if (strcmp(node->type->name, type->name)) {
326 /* LCOV_EXCL_START */
327 static int ec_node_testcase(void)
329 struct ec_node *node = NULL;
330 const struct ec_node *child;
331 const struct ec_node_type *type;
338 node = EC_NODE_SEQ(EC_NO_ID,
339 ec_node_str("id_x", "x"),
340 ec_node_str("id_y", "y"));
347 f = open_memstream(&buf, &buflen);
350 ec_node_dump(f, node);
351 ec_node_type_dump(f);
352 ec_node_dump(f, NULL);
356 testres |= EC_TEST_CHECK(
357 strstr(buf, "type=seq id=no-id"), "bad dump\n");
358 testres |= EC_TEST_CHECK(
359 strstr(buf, "type=str id=id_x") &&
360 strstr(strstr(buf, "type=str id=id_x") + 1,
366 testres |= EC_TEST_CHECK(
367 !strcmp(ec_node_type(node)->name, "seq") &&
368 !strcmp(ec_node_id(node), EC_NO_ID) &&
369 !strcmp(ec_node_desc(node), "<seq>"),
372 testres |= EC_TEST_CHECK(
373 ec_node_get_children_count(node) == 2,
374 "bad children count\n");
375 child = ec_node_get_child(node, 0);
376 testres |= EC_TEST_CHECK(child != NULL &&
377 !strcmp(ec_node_type(child)->name, "str") &&
378 !strcmp(ec_node_id(child), "id_x"),
380 child = ec_node_get_child(node, 1);
381 testres |= EC_TEST_CHECK(child != NULL &&
382 !strcmp(ec_node_type(child)->name, "str") &&
383 !strcmp(ec_node_id(child), "id_y"),
385 child = ec_node_get_child(node, 2);
386 testres |= EC_TEST_CHECK(child == NULL,
387 "child 2 should be NULL");
389 child = ec_node_find(node, "id_x");
390 testres |= EC_TEST_CHECK(child != NULL &&
391 !strcmp(ec_node_type(child)->name, "str") &&
392 !strcmp(ec_node_id(child), "id_x") &&
393 !strcmp(ec_node_desc(child), "x"),
395 child = ec_node_find(node, "id_dezdex");
396 testres |= EC_TEST_CHECK(child == NULL,
397 "child with wrong id should be NULL");
399 ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
400 testres |= EC_TEST_CHECK(ret == 0,
401 "cannot set node attribute\n");
403 type = ec_node_type_lookup("seq");
404 testres |= EC_TEST_CHECK(type != NULL &&
405 ec_node_check_type(node, type) == 0,
406 "cannot get seq node type");
407 type = ec_node_type_lookup("str");
408 testres |= EC_TEST_CHECK(type != NULL &&
409 ec_node_check_type(node, type) < 0,
410 "node type should not be str");
415 node = ec_node("deznuindez", EC_NO_ID);
416 testres |= EC_TEST_CHECK(node == NULL,
417 "should not be able to create node\n");
432 static struct ec_test ec_node_test = {
434 .test = ec_node_testcase,
437 EC_TEST_REGISTER(ec_node_test);