api documentation for ec_parse
[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_dict.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         ec_node_dynamic_build_t build;
28         void *opaque;
29 };
30
31 static int
32 ec_node_dynamic_parse(const struct ec_node *node,
33                 struct ec_pnode *parse,
34                 const struct ec_strvec *strvec)
35 {
36         struct ec_node_dynamic *priv = ec_node_priv(node);
37         struct ec_node *child = NULL;
38         void (*node_free)(struct ec_node *) = ec_node_free;
39         char key[64];
40         int ret = -1;
41
42         child = priv->build(parse, priv->opaque);
43         if (child == NULL)
44                 goto fail;
45
46         /* add the node pointer in the attributes, so it will be freed
47          * when parse is freed */
48         snprintf(key, sizeof(key), "_dyn_%p", child);
49         ret = ec_dict_set(ec_pnode_get_attrs(parse), key, child,
50                         (void *)node_free);
51         if (ret < 0) {
52                 child = NULL; /* already freed */
53                 goto fail;
54         }
55
56         return ec_parse_child(child, parse, strvec);
57
58 fail:
59         ec_node_free(child);
60         return ret;
61 }
62
63 static int
64 ec_node_dynamic_complete(const struct ec_node *node,
65                 struct ec_comp *comp,
66                 const struct ec_strvec *strvec)
67 {
68         struct ec_node_dynamic *priv = ec_node_priv(node);
69         struct ec_pnode *parse;
70         struct ec_node *child = NULL;
71         void (*node_free)(struct ec_node *) = ec_node_free;
72         char key[64];
73         int ret = -1;
74
75         parse = ec_comp_get_cur_pstate(comp);
76         child = priv->build(parse, priv->opaque);
77         if (child == NULL)
78                 goto fail;
79
80         /* add the node pointer in the attributes, so it will be freed
81          * when parse is freed */
82         snprintf(key, sizeof(key), "_dyn_%p", child);
83         ret = ec_dict_set(ec_comp_get_attrs(comp), key, child,
84                         (void *)node_free);
85         if (ret < 0) {
86                 child = NULL; /* already freed */
87                 goto fail;
88         }
89
90         return ec_complete_child(child, comp, strvec);
91
92 fail:
93         ec_node_free(child);
94         return ret;
95 }
96
97 static struct ec_node_type ec_node_dynamic_type = {
98         .name = "dynamic",
99         .parse = ec_node_dynamic_parse,
100         .complete = ec_node_dynamic_complete,
101         .size = sizeof(struct ec_node_dynamic),
102 };
103
104 struct ec_node *
105 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
106 {
107         struct ec_node *node = NULL;
108         struct ec_node_dynamic *priv;
109
110         if (build == NULL) {
111                 errno = EINVAL;
112                 goto fail;
113         }
114
115         node = ec_node_from_type(&ec_node_dynamic_type, id);
116         if (node == NULL)
117                 goto fail;
118
119         priv = ec_node_priv(node);
120         priv->build = build;
121         priv->opaque = opaque;
122
123         return node;
124
125 fail:
126         ec_node_free(node);
127         return NULL;
128
129 }
130
131 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
132
133 static struct ec_node *
134 build_counter(struct ec_pnode *parse, void *opaque)
135 {
136         const struct ec_node *node;
137         struct ec_pnode *root, *iter;
138         unsigned int count = 0;
139         char buf[32];
140
141         (void)opaque;
142         root = ec_pnode_get_root(parse);
143         for (iter = root; iter != NULL;
144              iter = EC_PNODE_ITER_NEXT(root, iter, 1)) {
145                 node = ec_pnode_get_node(iter);
146                 if (!strcmp(ec_node_id(node), "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_VA_END,
175                 "count-0", EC_VA_END);
176         testres |= EC_TEST_CHECK_COMPLETE(node,
177                 "count-0", "", EC_VA_END,
178                 "count-1", EC_VA_END,
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);