add meson support
[protos/libecoli.git] / src / 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 #include <ecoli_node_or.h>
24 #include <ecoli_node_int.h>
25
26 EC_LOG_TYPE_REGISTER(node);
27
28 static struct ec_node_type_list node_type_list =
29         TAILQ_HEAD_INITIALIZER(node_type_list);
30
31 const struct ec_node_type *
32 ec_node_type_lookup(const char *name)
33 {
34         struct ec_node_type *type;
35
36         TAILQ_FOREACH(type, &node_type_list, next) {
37                 if (!strcmp(name, type->name))
38                         return type;
39         }
40
41         errno = ENOENT;
42         return NULL;
43 }
44
45 int ec_node_type_register(struct ec_node_type *type)
46 {
47         EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
48
49         if (ec_node_type_lookup(type->name) != NULL) {
50                 errno = EEXIST;
51                 return -1;
52         }
53
54         TAILQ_INSERT_TAIL(&node_type_list, type, next);
55
56         return 0;
57 }
58
59 void ec_node_type_dump(FILE *out)
60 {
61         struct ec_node_type *type;
62
63         TAILQ_FOREACH(type, &node_type_list, next)
64                 fprintf(out, "%s\n", type->name);
65 }
66
67 struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *id)
68 {
69         struct ec_node *node = NULL;
70
71         EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
72                 type->name, id);
73         if (id == NULL) {
74                 errno = EINVAL;
75                 goto fail;
76         }
77
78         node = ec_calloc(1, type->size);
79         if (node == NULL)
80                 goto fail;
81
82         node->type = type;
83         node->refcnt = 1;
84
85         // XXX check that id matches [_a-zA-Z][:-_0-9a-zA-Z]*
86         node->id = ec_strdup(id);
87         if (node->id == NULL)
88                 goto fail;
89
90         if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
91                 goto fail;
92
93         node->attrs = ec_keyval();
94         if (node->attrs == NULL)
95                 goto fail;
96
97         if (type->init_priv != NULL) {
98                 if (type->init_priv(node) < 0)
99                         goto fail;
100         }
101
102         return node;
103
104  fail:
105         if (node != NULL) {
106                 ec_keyval_free(node->attrs);
107                 ec_free(node->desc);
108                 ec_free(node->id);
109         }
110         ec_free(node);
111
112         return NULL;
113 }
114
115 const struct ec_config_schema *
116 ec_node_type_schema(const struct ec_node_type *type)
117 {
118         return type->schema;
119 }
120
121 const char *
122 ec_node_type_name(const struct ec_node_type *type)
123 {
124         return type->name;
125 }
126
127 struct ec_node *ec_node(const char *typename, const char *id)
128 {
129         const struct ec_node_type *type;
130
131         type = ec_node_type_lookup(typename);
132         if (type == NULL) {
133                 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
134                         typename);
135                 return NULL;
136         }
137
138         return ec_node_from_type(type, id);
139 }
140
141 static void count_references(struct ec_node *node, unsigned int refs)
142 {
143         struct ec_node *child;
144         size_t i, n;
145         int ret;
146
147         if (node->free.state == EC_NODE_FREE_STATE_TRAVERSED) {
148                 node->free.refcnt += refs;
149                 return;
150         }
151         node->free.refcnt = refs;
152         node->free.state = EC_NODE_FREE_STATE_TRAVERSED;
153         n = ec_node_get_children_count(node);
154         for (i = 0; i < n; i++) {
155                 ret = ec_node_get_child(node, i, &child, &refs);
156                 assert(ret == 0);
157                 count_references(child, refs);
158         }
159 }
160
161 static void mark_freeable(struct ec_node *node, enum ec_node_free_state mark)
162 {
163         struct ec_node *child;
164         unsigned int refs;
165         size_t i, n;
166         int ret;
167
168         if (mark == node->free.state)
169                 return;
170
171         if (node->refcnt > node->free.refcnt)
172                 mark = EC_NODE_FREE_STATE_NOT_FREEABLE;
173         assert(node->refcnt >= node->free.refcnt);
174         node->free.state = mark;
175
176         n = ec_node_get_children_count(node);
177         for (i = 0; i < n; i++) {
178                 ret = ec_node_get_child(node, i, &child, &refs);
179                 assert(ret == 0);
180                 mark_freeable(child, mark);
181         }
182 }
183
184 static void reset_mark(struct ec_node *node)
185 {
186         struct ec_node *child;
187         unsigned int refs;
188         size_t i, n;
189         int ret;
190
191         if (node->free.state == EC_NODE_FREE_STATE_NONE)
192                 return;
193
194         node->free.state = EC_NODE_FREE_STATE_NONE;
195         node->free.refcnt = 0;
196
197         n = ec_node_get_children_count(node);
198         for (i = 0; i < n; i++) {
199                 ret = ec_node_get_child(node, i, &child, &refs);
200                 assert(ret == 0);
201                 reset_mark(child);
202         }
203 }
204
205 /* free a node, taking care of loops in the node graph */
206 void ec_node_free(struct ec_node *node)
207 {
208         size_t n;
209
210         if (node == NULL)
211                 return;
212
213         assert(node->refcnt > 0);
214
215         if (node->free.state == EC_NODE_FREE_STATE_NONE &&
216                         node->refcnt != 1) {
217
218                 /* Traverse the node tree starting from this node, and for each
219                  * node, count the number of reachable references. Then, all
220                  * nodes whose reachable references == total reference are
221                  * marked as freeable, and other are marked as unfreeable. Any
222                  * node reachable from an unfreeable node is also marked as
223                  * unfreeable. */
224                 if (node->free.state == EC_NODE_FREE_STATE_NONE) {
225                         count_references(node, 1);
226                         mark_freeable(node, EC_NODE_FREE_STATE_FREEABLE);
227                 }
228         }
229
230         if (node->free.state == EC_NODE_FREE_STATE_NOT_FREEABLE) {
231                 node->refcnt--;
232                 reset_mark(node);
233                 return;
234         }
235
236         if (node->free.state != EC_NODE_FREE_STATE_FREEING) {
237                 node->free.state = EC_NODE_FREE_STATE_FREEING;
238
239                 /* children will be freed by config_free() and free_priv() */
240                 ec_config_free(node->config);
241                 node->config = NULL;
242                 n = ec_node_get_children_count(node);
243                 assert(n == 0 || node->type->free_priv != NULL);
244                 if (node->type->free_priv != NULL)
245                         node->type->free_priv(node);
246                 ec_free(node->id);
247                 ec_free(node->desc);
248                 ec_keyval_free(node->attrs);
249         }
250
251         node->refcnt--;
252         if (node->refcnt != 0)
253                 return;
254
255         node->free.state = EC_NODE_FREE_STATE_NONE;
256         node->free.refcnt = 0;
257
258         ec_free(node);
259 }
260
261 struct ec_node *ec_node_clone(struct ec_node *node)
262 {
263         if (node != NULL)
264                 node->refcnt++;
265         return node;
266 }
267
268 size_t ec_node_get_children_count(const struct ec_node *node)
269 {
270         if (node->type->get_children_count == NULL)
271                 return 0;
272         return node->type->get_children_count(node);
273 }
274
275 int
276 ec_node_get_child(const struct ec_node *node, size_t i,
277         struct ec_node **child, unsigned int *refs)
278 {
279         *child = NULL;
280         *refs = 0;
281         if (node->type->get_child == NULL)
282                 return -1;
283         return node->type->get_child(node, i, child, refs);
284 }
285
286 int
287 ec_node_set_config(struct ec_node *node, struct ec_config *config)
288 {
289         if (node->type->schema == NULL) {
290                 errno = EINVAL;
291                 goto fail;
292         }
293         if (ec_config_validate(config, node->type->schema) < 0)
294                 goto fail;
295         if (node->type->set_config != NULL) {
296                 if (node->type->set_config(node, config) < 0)
297                         goto fail;
298         }
299
300         ec_config_free(node->config);
301         node->config = config;
302
303         return 0;
304
305 fail:
306         ec_config_free(config);
307         return -1;
308 }
309
310 const struct ec_config *ec_node_get_config(struct ec_node *node)
311 {
312         return node->config;
313 }
314
315 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
316 {
317         struct ec_node *child, *retnode;
318         const char *node_id = ec_node_id(node);
319         unsigned int refs;
320         size_t i, n;
321         int ret;
322
323         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
324                 return node;
325
326         n = ec_node_get_children_count(node);
327         for (i = 0; i < n; i++) {
328                 ret = ec_node_get_child(node, i, &child, &refs);
329                 assert(ret == 0);
330                 retnode = ec_node_find(child, id);
331                 if (retnode != NULL)
332                         return retnode;
333         }
334
335         return NULL;
336 }
337
338 const struct ec_node_type *ec_node_type(const struct ec_node *node)
339 {
340         return node->type;
341 }
342
343 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
344 {
345         return node->attrs;
346 }
347
348 const char *ec_node_id(const struct ec_node *node)
349 {
350         return node->id;
351 }
352
353 static void __ec_node_dump(FILE *out,
354         const struct ec_node *node, size_t indent, struct ec_keyval *dict)
355 {
356         const char *id, *typename;
357         struct ec_node *child;
358         unsigned int refs;
359         char buf[32];
360         size_t i, n;
361         int ret;
362
363         id = ec_node_id(node);
364         typename = node->type->name;
365
366         snprintf(buf, sizeof(buf), "%p", node);
367         if (ec_keyval_has_key(dict, buf)) {
368                 fprintf(out, "%*s" "type=%s id=%s %p... (loop)\n",
369                         (int)indent * 4, "", typename, id, node);
370                 return;
371         }
372
373         ec_keyval_set(dict, buf, NULL, NULL);
374         fprintf(out, "%*s" "type=%s id=%s %p refs=%u free_state=%d free_refs=%d\n",
375                 (int)indent * 4, "", typename, id, node, node->refcnt,
376                 node->free.state, node->free.refcnt);
377
378         n = ec_node_get_children_count(node);
379         for (i = 0; i < n; i++) {
380                 ret = ec_node_get_child(node, i, &child, &refs);
381                 assert(ret == 0);
382                 __ec_node_dump(out, child, indent + 1, dict);
383         }
384 }
385
386 /* XXX this is too much debug-oriented, we should have a parameter or 2 funcs */
387 void ec_node_dump(FILE *out, const struct ec_node *node)
388 {
389         struct ec_keyval *dict = NULL;
390
391         fprintf(out, "------------------- node dump:\n");
392
393         if (node == NULL) {
394                 fprintf(out, "node is NULL\n");
395                 return;
396         }
397
398         dict = ec_keyval();
399         if (dict == NULL)
400                 goto fail;
401
402         __ec_node_dump(out, node, 0, dict);
403
404         ec_keyval_free(dict);
405         return;
406
407 fail:
408         ec_keyval_free(dict);
409         EC_LOG(EC_LOG_ERR, "failed to dump node\n");
410 }
411
412 const char *ec_node_desc(const struct ec_node *node)
413 {
414         if (node->type->desc != NULL)
415                 return node->type->desc(node);
416
417         return node->desc;
418 }
419
420 int ec_node_check_type(const struct ec_node *node,
421                 const struct ec_node_type *type)
422 {
423         if (strcmp(node->type->name, type->name)) {
424                 errno = EINVAL;
425                 return -1;
426         }
427
428         return 0;
429 }
430
431 /* LCOV_EXCL_START */
432 static int ec_node_testcase(void)
433 {
434         struct ec_node *node = NULL, *expr = NULL;
435         struct ec_node *expr2 = NULL, *val = NULL, *op = NULL, *seq = NULL;
436         const struct ec_node_type *type;
437         struct ec_node *child;
438         unsigned int refs;
439         FILE *f = NULL;
440         char *buf = NULL;
441         size_t buflen = 0;
442         int testres = 0;
443         int ret;
444
445         node = EC_NODE_SEQ(EC_NO_ID,
446                         ec_node_str("id_x", "x"),
447                         ec_node_str("id_y", "y"));
448         if (node == NULL)
449                 goto fail;
450
451         ec_node_clone(node);
452         ec_node_free(node);
453
454         f = open_memstream(&buf, &buflen);
455         if (f == NULL)
456                 goto fail;
457         ec_node_dump(f, node);
458         ec_node_type_dump(f);
459         ec_node_dump(f, NULL);
460         fclose(f);
461         f = NULL;
462
463         testres |= EC_TEST_CHECK(
464                 strstr(buf, "type=seq id=no-id"), "bad dump\n");
465         testres |= EC_TEST_CHECK(
466                 strstr(buf, "type=str id=id_x") &&
467                 strstr(strstr(buf, "type=str id=id_x") + 1,
468                         "type=str id=id_y"),
469                 "bad dump\n");
470         free(buf);
471         buf = NULL;
472
473         testres |= EC_TEST_CHECK(
474                 !strcmp(ec_node_type(node)->name, "seq") &&
475                 !strcmp(ec_node_id(node), EC_NO_ID) &&
476                 !strcmp(ec_node_desc(node), "<seq>"),
477                 "bad child 0");
478
479         testres |= EC_TEST_CHECK(
480                 ec_node_get_children_count(node) == 2,
481                 "bad children count\n");
482         ret = ec_node_get_child(node, 0, &child, &refs);
483         testres |= EC_TEST_CHECK(ret == 0 &&
484                 child != NULL &&
485                 !strcmp(ec_node_type(child)->name, "str") &&
486                 !strcmp(ec_node_id(child), "id_x"),
487                 "bad child 0");
488         ret = ec_node_get_child(node, 1, &child, &refs);
489         testres |= EC_TEST_CHECK(ret == 0 &&
490                 child != NULL &&
491                 !strcmp(ec_node_type(child)->name, "str") &&
492                 !strcmp(ec_node_id(child), "id_y"),
493                 "bad child 1");
494         ret = ec_node_get_child(node, 2, &child, &refs);
495         testres |= EC_TEST_CHECK(ret != 0,
496                 "ret should be != 0");
497         testres |= EC_TEST_CHECK(child == NULL,
498                 "child 2 should be NULL");
499
500         child = ec_node_find(node, "id_x");
501         testres |= EC_TEST_CHECK(child != NULL &&
502                 !strcmp(ec_node_type(child)->name, "str") &&
503                 !strcmp(ec_node_id(child), "id_x") &&
504                 !strcmp(ec_node_desc(child), "x"),
505                 "bad child id_x");
506         child = ec_node_find(node, "id_dezdex");
507         testres |= EC_TEST_CHECK(child == NULL,
508                 "child with wrong id should be NULL");
509
510         ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
511         testres |= EC_TEST_CHECK(ret == 0,
512                 "cannot set node attribute\n");
513
514         type = ec_node_type_lookup("seq");
515         testres |= EC_TEST_CHECK(type != NULL &&
516                 ec_node_check_type(node, type) == 0,
517                 "cannot get seq node type");
518         type = ec_node_type_lookup("str");
519         testres |= EC_TEST_CHECK(type != NULL &&
520                 ec_node_check_type(node, type) < 0,
521                 "node type should not be str");
522
523         ec_node_free(node);
524         node = NULL;
525
526         node = ec_node("deznuindez", EC_NO_ID);
527         testres |= EC_TEST_CHECK(node == NULL,
528                         "should not be able to create node\n");
529
530         /* test loop */
531         expr = ec_node("or", EC_NO_ID);
532         val = ec_node_int(EC_NO_ID, 0, 10, 0);
533         op = ec_node_str(EC_NO_ID, "!");
534         seq = EC_NODE_SEQ(EC_NO_ID,
535                         op,
536                         ec_node_clone(expr));
537         op = NULL;
538         if (expr == NULL || val == NULL || seq == NULL)
539                 goto fail;
540         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
541                 goto fail;
542         ec_node_free(seq);
543         seq = NULL;
544         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
545                 goto fail;
546         ec_node_free(val);
547         val = NULL;
548
549         testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
550         testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
551         testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
552
553         ec_node_free(expr);
554         expr = NULL;
555
556         /* same loop test, but keep some refs (released later) */
557         expr = ec_node("or", EC_NO_ID);
558         ec_node_clone(expr);
559         expr2 = expr;
560         val = ec_node_int(EC_NO_ID, 0, 10, 0);
561         op = ec_node_str(EC_NO_ID, "!");
562         seq = EC_NODE_SEQ(EC_NO_ID,
563                         op,
564                         ec_node_clone(expr));
565         op = NULL;
566         if (expr == NULL || val == NULL || seq == NULL)
567                 goto fail;
568         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
569                 goto fail;
570         ec_node_free(seq);
571         seq = NULL;
572         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
573                 goto fail;
574
575         testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
576         testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
577         testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
578
579         ec_node_free(expr2);
580         expr2 = NULL;
581         ec_node_free(val);
582         val = NULL;
583         ec_node_free(expr);
584         expr = NULL;
585
586         return testres;
587
588 fail:
589         ec_node_free(expr);
590         ec_node_free(expr2);
591         ec_node_free(val);
592         ec_node_free(seq);
593         ec_node_free(node);
594         if (f != NULL)
595                 fclose(f);
596         free(buf);
597
598         assert(errno != 0);
599         return -1;
600 }
601 /* LCOV_EXCL_STOP */
602
603 static struct ec_test ec_node_test = {
604         .name = "node",
605         .test = ec_node_testcase,
606 };
607
608 EC_TEST_REGISTER(ec_node_test);