9a21d6304f211826347d5e89711302f8bad72ba7
[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->id);
139         ec_free(node->desc);
140         ec_keyval_free(node->attrs);
141         ec_config_free(node->config);
142         ec_free(node);
143 }
144
145 struct ec_node *ec_node_clone(struct ec_node *node)
146 {
147         if (node != NULL)
148                 node->refcnt++;
149         return node;
150 }
151
152 size_t ec_node_get_children_count(const struct ec_node *node)
153 {
154         if (node->type->get_children_count == NULL)
155                 return 0;
156         return node->type->get_children_count(node);
157 }
158
159 struct ec_node *
160 ec_node_get_child(const struct ec_node *node, size_t i)
161 {
162         if (node->type->get_child == NULL)
163                 return NULL;
164         return node->type->get_child(node, i);
165 }
166
167 int
168 ec_node_set_config(struct ec_node *node, struct ec_config *config)
169 {
170         if (node->type->schema == NULL) {
171                 errno = EINVAL;
172                 goto fail;
173         }
174         if (ec_config_validate(config, node->type->schema,
175                                 node->type->schema_len) < 0)
176                 goto fail;
177         if (node->type->set_config != NULL) {
178                 if (node->type->set_config(node, config) < 0)
179                         goto fail;
180         }
181
182         ec_config_free(node->config);
183         node->config = config;
184
185         return 0;
186
187 fail:
188         ec_config_free(config);
189         return -1;
190 }
191
192 const struct ec_config *ec_node_get_config(struct ec_node *node)
193 {
194         return node->config;
195 }
196
197 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
198 {
199         struct ec_node *child, *ret;
200         const char *node_id = ec_node_id(node);
201         size_t i, n;
202
203         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
204                 return node;
205
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);
210                 if (ret != NULL)
211                         return ret;
212         }
213
214         return NULL;
215 }
216
217 const struct ec_node_type *ec_node_type(const struct ec_node *node)
218 {
219         return node->type;
220 }
221
222 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
223 {
224         return node->attrs;
225 }
226
227 const char *ec_node_id(const struct ec_node *node)
228 {
229         return node->id;
230 }
231
232 static void __ec_node_dump(FILE *out,
233         const struct ec_node *node, size_t indent)
234 {
235         const char *id, *typename;
236         struct ec_node *child;
237         size_t i, n;
238
239         id = ec_node_id(node);
240         typename = node->type->name;
241
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);
248         }
249 }
250
251 void ec_node_dump(FILE *out, const struct ec_node *node)
252 {
253         fprintf(out, "------------------- node dump:\n");
254
255         if (node == NULL) {
256                 fprintf(out, "node is NULL\n");
257                 return;
258         }
259
260         __ec_node_dump(out, node, 0);
261 }
262
263 const char *ec_node_desc(const struct ec_node *node)
264 {
265         if (node->type->desc != NULL)
266                 return node->type->desc(node);
267
268         return node->desc;
269 }
270
271 int ec_node_check_type(const struct ec_node *node,
272                 const struct ec_node_type *type)
273 {
274         if (strcmp(node->type->name, type->name)) {
275                 errno = EINVAL;
276                 return -1;
277         }
278
279         return 0;
280 }
281
282 /* LCOV_EXCL_START */
283 static int ec_node_testcase(void)
284 {
285         struct ec_node *node = NULL;
286         const struct ec_node *child;
287         const struct ec_node_type *type;
288         FILE *f = NULL;
289         char *buf = NULL;
290         size_t buflen = 0;
291         int testres = 0;
292         int ret;
293
294         node = EC_NODE_SEQ(EC_NO_ID,
295                         ec_node_str("id_x", "x"),
296                         ec_node_str("id_y", "y"));
297         if (node == NULL)
298                 goto fail;
299
300         ec_node_clone(node);
301         ec_node_free(node);
302
303         f = open_memstream(&buf, &buflen);
304         if (f == NULL)
305                 goto fail;
306         ec_node_dump(f, node);
307         ec_node_type_dump(f);
308         ec_node_dump(f, NULL);
309         fclose(f);
310         f = NULL;
311
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,
317                         "type=str id=id_y"),
318                 "bad dump\n");
319         free(buf);
320         buf = NULL;
321
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>"),
326                 "bad child 0");
327
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"),
335                 "bad child 0");
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"),
340                 "bad child 1");
341         child = ec_node_get_child(node, 2);
342         testres |= EC_TEST_CHECK(child == NULL,
343                 "child 2 should be NULL");
344
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"),
350                 "bad child id_x");
351         child = ec_node_find(node, "id_dezdex");
352         testres |= EC_TEST_CHECK(child == NULL,
353                 "child with wrong id should be NULL");
354
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");
358
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");
367
368         ec_node_free(node);
369         node = NULL;
370
371         node = ec_node("deznuindez", EC_NO_ID);
372         testres |= EC_TEST_CHECK(node == NULL,
373                         "should not be able to create node\n");
374
375         return testres;
376
377 fail:
378         ec_node_free(node);
379         if (f != NULL)
380                 fclose(f);
381         free(buf);
382
383         assert(errno != 0);
384         return -1;
385 }
386 /* LCOV_EXCL_STOP */
387
388 static struct ec_test ec_node_test = {
389         .name = "node",
390         .test = ec_node_testcase,
391 };
392
393 EC_TEST_REGISTER(ec_node_test);