e4cdb71483882e817ee196faf04e98484cc14391
[protos/libecoli.git] / lib / ecoli_node_dynamic.c
1 /*
2  * Copyright (c) 2017, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include <ecoli_log.h>
34 #include <ecoli_malloc.h>
35 #include <ecoli_test.h>
36 #include <ecoli_strvec.h>
37 #include <ecoli_keyval.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_string.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_many.h>
44
45 #include "ecoli_node_dynamic.h"
46
47 EC_LOG_TYPE_REGISTER(node_dynamic);
48
49 struct ec_node_dynamic {
50         struct ec_node gen;
51         ec_node_dynamic_build_t build;
52         void *opaque;
53 };
54
55 static int
56 ec_node_dynamic_parse(const struct ec_node *gen_node,
57                 struct ec_parsed *parsed,
58                 const struct ec_strvec *strvec)
59 {
60         struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
61         struct ec_node *child = NULL;
62         void (*node_free)(struct ec_node *) = ec_node_free;
63         char key[64];
64         int ret;
65
66         child = node->build(parsed, node->opaque);
67         if (child == NULL)
68                 goto fail;
69
70         /* add the node pointer in the attributes, so it will be freed
71          * when parsed is freed */
72         snprintf(key, sizeof(key), "_dyn_%p", child);
73         ret = ec_keyval_set(ec_parsed_get_attrs(parsed), key, child,
74                         (void *)node_free);
75         if (ret < 0)
76                 goto fail;
77
78         return ec_node_parse_child(child, parsed, strvec);
79
80 fail:
81         ec_node_free(child);
82         return ret;
83 }
84
85 static int
86 ec_node_dynamic_complete(const struct ec_node *gen_node,
87                 struct ec_completed *completed,
88                 const struct ec_strvec *strvec)
89 {
90         struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
91         struct ec_parsed *parsed;
92         struct ec_node *child = NULL;
93         void (*node_free)(struct ec_node *) = ec_node_free;
94         char key[64];
95         int ret;
96
97         parsed = ec_completed_get_state(completed);
98         child = node->build(parsed, node->opaque);
99         if (child == NULL)
100                 goto fail;
101
102         /* add the node pointer in the attributes, so it will be freed
103          * when parsed is freed */
104         snprintf(key, sizeof(key), "_dyn_%p", child);
105         ret = ec_keyval_set(completed->attrs, key, child,
106                         (void *)node_free);
107         if (ret < 0)
108                 goto fail;
109
110         return ec_node_complete_child(child, completed, strvec);
111
112 fail:
113         ec_node_free(child);
114         return ret;
115 }
116
117 static struct ec_node_type ec_node_dynamic_type = {
118         .name = "dynamic",
119         .parse = ec_node_dynamic_parse,
120         .complete = ec_node_dynamic_complete,
121         .size = sizeof(struct ec_node_dynamic),
122 };
123
124 struct ec_node *
125 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
126 {
127         struct ec_node *gen_node = NULL;
128         struct ec_node_dynamic *node;
129
130         if (build == NULL) {
131                 errno = EINVAL;
132                 goto fail;
133         }
134
135         gen_node = __ec_node(&ec_node_dynamic_type, id);
136         if (gen_node == NULL)
137                 goto fail;
138
139         node = (struct ec_node_dynamic *)gen_node;
140         node->build = build;
141         node->opaque = opaque;
142
143         return gen_node;
144
145 fail:
146         ec_node_free(gen_node);
147         return NULL;
148
149 }
150
151 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
152
153 static struct ec_node *
154 build_counter(struct ec_parsed *parsed, void *opaque)
155 {
156         const struct ec_node *node;
157         struct ec_parsed *iter;
158         unsigned int count = 0;
159         char buf[32];
160
161         (void)opaque;
162         for (iter = ec_parsed_get_root(parsed); iter != NULL;
163              iter = ec_parsed_iter_next(iter)) {
164                 node = ec_parsed_get_node(iter);
165                 if (node->id && !strcmp(node->id, "my-id"))
166                         count++;
167         }
168         snprintf(buf, sizeof(buf), "count-%u", count);
169
170         return ec_node_str("my-id", buf);
171 }
172
173 /* LCOV_EXCL_START */
174 static int ec_node_dynamic_testcase(void)
175 {
176         struct ec_node *node;
177         int ret = 0;
178
179         /* XXX use EC_NO_ID instead of NULL */
180         node = ec_node_many(NULL, ec_node_dynamic(NULL, build_counter, NULL),
181                         1, 3);
182         if (node == NULL) {
183                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
184                 return -1;
185         }
186         ret |= EC_TEST_CHECK_PARSE(node, 1, "count-0");
187         ret |= EC_TEST_CHECK_PARSE(node, 3, "count-0", "count-1", "count-2");
188         ret |= EC_TEST_CHECK_PARSE(node, 1, "count-0", "count-0");
189
190         /* test completion */
191
192         ret |= EC_TEST_CHECK_COMPLETE(node,
193                 "c", EC_NODE_ENDLIST,
194                 "count-0", EC_NODE_ENDLIST);
195         ret |= EC_TEST_CHECK_COMPLETE(node,
196                 "count-0", "", EC_NODE_ENDLIST,
197                 "count-1", EC_NODE_ENDLIST,
198                 "count-1");
199         ec_node_free(node);
200
201         return ret;
202 }
203 /* LCOV_EXCL_STOP */
204
205 static struct ec_test ec_node_dynamic_test = {
206         .name = "node_dynamic",
207         .test = ec_node_dynamic_testcase,
208 };
209
210 EC_TEST_REGISTER(ec_node_dynamic_test);