add meson support
[protos/libecoli.git] / src / ecoli_node_dynamic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9
10 #include <ecoli_log.h>
11 #include <ecoli_malloc.h>
12 #include <ecoli_test.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_keyval.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_string.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_many.h>
21
22 #include "ecoli_node_dynamic.h"
23
24 EC_LOG_TYPE_REGISTER(node_dynamic);
25
26 struct ec_node_dynamic {
27         struct ec_node gen;
28         ec_node_dynamic_build_t build;
29         void *opaque;
30 };
31
32 static int
33 ec_node_dynamic_parse(const struct ec_node *gen_node,
34                 struct ec_parse *parse,
35                 const struct ec_strvec *strvec)
36 {
37         struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
38         struct ec_node *child = NULL;
39         void (*node_free)(struct ec_node *) = ec_node_free;
40         char key[64];
41         int ret = -1;
42
43         child = node->build(parse, node->opaque);
44         if (child == NULL)
45                 goto fail;
46
47         /* add the node pointer in the attributes, so it will be freed
48          * when parse is freed */
49         snprintf(key, sizeof(key), "_dyn_%p", child);
50         ret = ec_keyval_set(ec_parse_get_attrs(parse), key, child,
51                         (void *)node_free);
52         if (ret < 0) {
53                 child = NULL; /* already freed */
54                 goto fail;
55         }
56
57         return ec_node_parse_child(child, parse, strvec);
58
59 fail:
60         ec_node_free(child);
61         return ret;
62 }
63
64 static int
65 ec_node_dynamic_complete(const struct ec_node *gen_node,
66                 struct ec_comp *comp,
67                 const struct ec_strvec *strvec)
68 {
69         struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
70         struct ec_parse *parse;
71         struct ec_node *child = NULL;
72         void (*node_free)(struct ec_node *) = ec_node_free;
73         char key[64];
74         int ret = -1;
75
76         parse = ec_comp_get_state(comp);
77         child = node->build(parse, node->opaque);
78         if (child == NULL)
79                 goto fail;
80
81         /* add the node pointer in the attributes, so it will be freed
82          * when parse is freed */
83         snprintf(key, sizeof(key), "_dyn_%p", child);
84         ret = ec_keyval_set(comp->attrs, key, child,
85                         (void *)node_free);
86         if (ret < 0) {
87                 child = NULL; /* already freed */
88                 goto fail;
89         }
90
91         return ec_node_complete_child(child, comp, strvec);
92
93 fail:
94         ec_node_free(child);
95         return ret;
96 }
97
98 static struct ec_node_type ec_node_dynamic_type = {
99         .name = "dynamic",
100         .parse = ec_node_dynamic_parse,
101         .complete = ec_node_dynamic_complete,
102         .size = sizeof(struct ec_node_dynamic),
103 };
104
105 struct ec_node *
106 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
107 {
108         struct ec_node *gen_node = NULL;
109         struct ec_node_dynamic *node;
110
111         if (build == NULL) {
112                 errno = EINVAL;
113                 goto fail;
114         }
115
116         gen_node = ec_node_from_type(&ec_node_dynamic_type, id);
117         if (gen_node == NULL)
118                 goto fail;
119
120         node = (struct ec_node_dynamic *)gen_node;
121         node->build = build;
122         node->opaque = opaque;
123
124         return gen_node;
125
126 fail:
127         ec_node_free(gen_node);
128         return NULL;
129
130 }
131
132 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
133
134 static struct ec_node *
135 build_counter(struct ec_parse *parse, void *opaque)
136 {
137         const struct ec_node *node;
138         struct ec_parse *iter;
139         unsigned int count = 0;
140         char buf[32];
141
142         (void)opaque;
143         for (iter = ec_parse_get_root(parse); iter != NULL;
144              iter = ec_parse_iter_next(iter)) {
145                 node = ec_parse_get_node(iter);
146                 if (node->id && !strcmp(node->id, "my-id"))
147                         count++;
148         }
149         snprintf(buf, sizeof(buf), "count-%u", count);
150
151         return ec_node_str("my-id", buf);
152 }
153
154 /* LCOV_EXCL_START */
155 static int ec_node_dynamic_testcase(void)
156 {
157         struct ec_node *node;
158         int testres = 0;
159
160         node = ec_node_many(EC_NO_ID,
161                         ec_node_dynamic(EC_NO_ID, build_counter, NULL),
162                         1, 3);
163         if (node == NULL) {
164                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
165                 return -1;
166         }
167         testres |= EC_TEST_CHECK_PARSE(node, 1, "count-0");
168         testres |= EC_TEST_CHECK_PARSE(node, 3, "count-0", "count-1", "count-2");
169         testres |= EC_TEST_CHECK_PARSE(node, 1, "count-0", "count-0");
170
171         /* test completion */
172
173         testres |= EC_TEST_CHECK_COMPLETE(node,
174                 "c", EC_NODE_ENDLIST,
175                 "count-0", EC_NODE_ENDLIST);
176         testres |= EC_TEST_CHECK_COMPLETE(node,
177                 "count-0", "", EC_NODE_ENDLIST,
178                 "count-1", EC_NODE_ENDLIST,
179                 "count-1");
180         ec_node_free(node);
181
182         return testres;
183 }
184 /* LCOV_EXCL_STOP */
185
186 static struct ec_test ec_node_dynamic_test = {
187         .name = "node_dynamic",
188         .test = ec_node_dynamic_testcase,
189 };
190
191 EC_TEST_REGISTER(ec_node_dynamic_test);