7c73b65b67b676149e60d6f7097df2ad6ef50bdb
[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 *root, *iter;
139         unsigned int count = 0;
140         char buf[32];
141
142         (void)opaque;
143         root = ec_parse_get_root(parse);
144         for (iter = root; iter != NULL;
145              iter = EC_PARSE_ITER_NEXT(root, iter, 1)) {
146                 node = ec_parse_get_node(iter);
147                 if (node->id && !strcmp(node->id, "my-id"))
148                         count++;
149         }
150         snprintf(buf, sizeof(buf), "count-%u", count);
151
152         return ec_node_str("my-id", buf);
153 }
154
155 /* LCOV_EXCL_START */
156 static int ec_node_dynamic_testcase(void)
157 {
158         struct ec_node *node;
159         int testres = 0;
160
161         node = ec_node_many(EC_NO_ID,
162                         ec_node_dynamic(EC_NO_ID, build_counter, NULL),
163                         1, 3);
164         if (node == NULL) {
165                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
166                 return -1;
167         }
168         testres |= EC_TEST_CHECK_PARSE(node, 1, "count-0");
169         testres |= EC_TEST_CHECK_PARSE(node, 3, "count-0", "count-1", "count-2");
170         testres |= EC_TEST_CHECK_PARSE(node, 1, "count-0", "count-0");
171
172         /* test completion */
173
174         testres |= EC_TEST_CHECK_COMPLETE(node,
175                 "c", EC_NODE_ENDLIST,
176                 "count-0", EC_NODE_ENDLIST);
177         testres |= EC_TEST_CHECK_COMPLETE(node,
178                 "count-0", "", EC_NODE_ENDLIST,
179                 "count-1", EC_NODE_ENDLIST,
180                 "count-1");
181         ec_node_free(node);
182
183         return testres;
184 }
185 /* LCOV_EXCL_STOP */
186
187 static struct ec_test ec_node_dynamic_test = {
188         .name = "node_dynamic",
189         .test = ec_node_dynamic_testcase,
190 };
191
192 EC_TEST_REGISTER(ec_node_dynamic_test);