b710a40a2c5709c80fe3e880c6f963837d9082c6
[protos/libecoli.git] / lib / ecoli_node.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <errno.h>
11
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>
20
21 #include <ecoli_node_str.h>
22 #include <ecoli_node_seq.h>
23
24 EC_LOG_TYPE_REGISTER(node);
25
26 static struct ec_node_type_list node_type_list =
27         TAILQ_HEAD_INITIALIZER(node_type_list);
28
29 const struct ec_node_type *
30 ec_node_type_lookup(const char *name)
31 {
32         struct ec_node_type *type;
33
34         TAILQ_FOREACH(type, &node_type_list, next) {
35                 if (!strcmp(name, type->name))
36                         return type;
37         }
38
39         errno = ENOENT;
40         return NULL;
41 }
42
43 int ec_node_type_register(struct ec_node_type *type)
44 {
45         EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
46
47         if (ec_node_type_lookup(type->name) != NULL) {
48                 errno = EEXIST;
49                 return -1;
50         }
51
52         TAILQ_INSERT_TAIL(&node_type_list, type, next);
53
54         return 0;
55 }
56
57 void ec_node_type_dump(FILE *out)
58 {
59         struct ec_node_type *type;
60
61         TAILQ_FOREACH(type, &node_type_list, next)
62                 fprintf(out, "%s\n", type->name);
63 }
64
65 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
66 {
67         struct ec_node *node = NULL;
68
69         EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
70                 type->name, id);
71         if (id == NULL) {
72                 errno = EINVAL;
73                 goto fail;
74         }
75
76         node = ec_calloc(1, type->size);
77         if (node == NULL)
78                 goto fail;
79
80         node->type = type;
81         node->refcnt = 1;
82
83         node->id = ec_strdup(id);
84         if (node->id == NULL)
85                 goto fail;
86
87         if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
88                 goto fail;
89
90         node->attrs = ec_keyval();
91         if (node->attrs == NULL)
92                 goto fail;
93
94         if (type->init_priv != NULL) {
95                 if (type->init_priv(node) < 0)
96                         goto fail;
97         }
98
99         return node;
100
101  fail:
102         if (node != NULL) {
103                 ec_keyval_free(node->attrs);
104                 ec_free(node->desc);
105                 ec_free(node->id);
106         }
107         ec_free(node);
108
109         return NULL;
110 }
111
112 struct ec_node *ec_node(const char *typename, const char *id)
113 {
114         const struct ec_node_type *type;
115
116         type = ec_node_type_lookup(typename);
117         if (type == NULL) {
118                 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
119                         typename);
120                 return NULL;
121         }
122
123         return __ec_node(type, id);
124 }
125
126 void ec_node_free(struct ec_node *node)
127 {
128         if (node == NULL)
129                 return;
130
131         assert(node->refcnt > 0);
132
133         if (--node->refcnt > 0)
134                 return;
135
136         if (node->type != NULL && node->type->free_priv != NULL)
137                 node->type->free_priv(node);
138         ec_free(node->children);
139         ec_free(node->id);
140         ec_free(node->desc);
141         ec_keyval_free(node->attrs);
142         ec_config_free(node->config);
143         ec_free(node);
144 }
145
146 struct ec_node *ec_node_clone(struct ec_node *node)
147 {
148         if (node != NULL)
149                 node->refcnt++;
150         return node;
151 }
152
153 size_t ec_node_get_children_count(const struct ec_node *node)
154 {
155         return node->n_children;
156 }
157
158 struct ec_node *
159 ec_node_get_child(const struct ec_node *node, size_t i)
160 {
161         if (i >= ec_node_get_children_count(node))
162                 return NULL;
163         return node->children[i];
164 }
165
166 int ec_node_add_child(struct ec_node *node, struct ec_node *child)
167 {
168         struct ec_node **children = NULL;
169         size_t n;
170
171         if (node == NULL || child == NULL) {
172                 errno = EINVAL;
173                 goto fail;
174         }
175
176         n = node->n_children;
177         children = ec_realloc(node->children,
178                         (n + 1) * sizeof(child));
179         if (children == NULL)
180                 goto fail;
181
182         children[n] = child;
183         node->children = children;
184         node->n_children = n + 1;
185
186         return 0;
187
188 fail:
189         ec_free(children);
190         assert(errno != 0);
191         return -1;
192 }
193
194 int
195 ec_node_set_config(struct ec_node *node, struct ec_config *config)
196 {
197         if (node->type->schema == NULL) {
198                 errno = EINVAL;
199                 goto fail;
200         }
201         if (ec_config_validate(config, node->type->schema,
202                                 node->type->schema_len) < 0)
203                 goto fail;
204         if (node->type->set_config != NULL) {
205                 if (node->type->set_config(node, config) < 0)
206                         goto fail;
207         }
208
209         ec_config_free(node->config);
210         node->config = config;
211
212         return 0;
213
214 fail:
215         return -1;
216 }
217
218 #if 0 /* later */
219 int ec_node_del_child(struct ec_node *node, struct ec_node *child)
220 {
221         size_t i, n;
222
223         if (node == NULL || child == NULL)
224                 goto fail;
225
226         n = node->n_children;
227         for (i = 0; i < n; i++) {
228                 if (node->children[i] != child)
229                         continue;
230                 memcpy(&node->children[i], &node->children[i+1],
231                         (n - i - 1) * sizeof(child));
232                 return 0;
233         }
234
235 fail:
236         errno = EINVAL;
237         return -1;
238 }
239 #endif
240
241 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
242 {
243         struct ec_node *child, *ret;
244         const char *node_id = ec_node_id(node);
245         size_t i, n;
246
247         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
248                 return node;
249
250         n = node->n_children;
251         for (i = 0; i < n; i++) {
252                 child = node->children[i];
253                 ret = ec_node_find(child, id);
254                 if (ret != NULL)
255                         return ret;
256         }
257
258         return NULL;
259 }
260
261 const struct ec_node_type *ec_node_type(const struct ec_node *node)
262 {
263         return node->type;
264 }
265
266 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
267 {
268         return node->attrs;
269 }
270
271 const char *ec_node_id(const struct ec_node *node)
272 {
273         return node->id;
274 }
275
276 static void __ec_node_dump(FILE *out,
277         const struct ec_node *node, size_t indent)
278 {
279         const char *id, *typename;
280         struct ec_node *child;
281         size_t i, n;
282
283         id = ec_node_id(node);
284         typename = node->type->name;
285
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);
292         }
293 }
294
295 void ec_node_dump(FILE *out, const struct ec_node *node)
296 {
297         fprintf(out, "------------------- node dump:\n");
298
299         if (node == NULL) {
300                 fprintf(out, "node is NULL\n");
301                 return;
302         }
303
304         __ec_node_dump(out, node, 0);
305 }
306
307 const char *ec_node_desc(const struct ec_node *node)
308 {
309         if (node->type->desc != NULL)
310                 return node->type->desc(node);
311
312         return node->desc;
313 }
314
315 int ec_node_check_type(const struct ec_node *node,
316                 const struct ec_node_type *type)
317 {
318         if (strcmp(node->type->name, type->name)) {
319                 errno = EINVAL;
320                 return -1;
321         }
322
323         return 0;
324 }
325
326 /* LCOV_EXCL_START */
327 static int ec_node_testcase(void)
328 {
329         struct ec_node *node = NULL;
330         const struct ec_node *child;
331         const struct ec_node_type *type;
332         FILE *f = NULL;
333         char *buf = NULL;
334         size_t buflen = 0;
335         int testres = 0;
336         int ret;
337
338         node = EC_NODE_SEQ(EC_NO_ID,
339                         ec_node_str("id_x", "x"),
340                         ec_node_str("id_y", "y"));
341         if (node == NULL)
342                 goto fail;
343
344         ec_node_clone(node);
345         ec_node_free(node);
346
347         f = open_memstream(&buf, &buflen);
348         if (f == NULL)
349                 goto fail;
350         ec_node_dump(f, node);
351         ec_node_type_dump(f);
352         ec_node_dump(f, NULL);
353         fclose(f);
354         f = NULL;
355
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,
361                         "type=str id=id_y"),
362                 "bad dump\n");
363         free(buf);
364         buf = NULL;
365
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>"),
370                 "bad child 0");
371
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"),
379                 "bad child 0");
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"),
384                 "bad child 1");
385         child = ec_node_get_child(node, 2);
386         testres |= EC_TEST_CHECK(child == NULL,
387                 "child 2 should be NULL");
388
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"),
394                 "bad child id_x");
395         child = ec_node_find(node, "id_dezdex");
396         testres |= EC_TEST_CHECK(child == NULL,
397                 "child with wrong id should be NULL");
398
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");
402
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");
411
412         ec_node_free(node);
413         node = NULL;
414
415         node = ec_node("deznuindez", EC_NO_ID);
416         testres |= EC_TEST_CHECK(node == NULL,
417                         "should not be able to create node\n");
418
419         return testres;
420
421 fail:
422         ec_node_free(node);
423         if (f != NULL)
424                 fclose(f);
425         free(buf);
426
427         assert(errno != 0);
428         return -1;
429 }
430 /* LCOV_EXCL_STOP */
431
432 static struct ec_test ec_node_test = {
433         .name = "node",
434         .test = ec_node_testcase,
435 };
436
437 EC_TEST_REGISTER(ec_node_test);