2 * Copyright (c) 2017, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
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>
45 #include "ecoli_node_dynamic.h"
47 EC_LOG_TYPE_REGISTER(node_dynamic);
49 struct ec_node_dynamic {
51 ec_node_dynamic_build_t build;
56 ec_node_dynamic_parse(const struct ec_node *gen_node,
57 struct ec_parsed *parsed,
58 const struct ec_strvec *strvec)
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;
66 child = node->build(parsed, node->opaque);
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,
76 child = NULL; /* already freed */
80 return ec_node_parse_child(child, parsed, strvec);
88 ec_node_dynamic_complete(const struct ec_node *gen_node,
89 struct ec_completed *completed,
90 const struct ec_strvec *strvec)
92 struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
93 struct ec_parsed *parsed;
94 struct ec_node *child = NULL;
95 void (*node_free)(struct ec_node *) = ec_node_free;
99 parsed = ec_completed_get_state(completed);
100 child = node->build(parsed, node->opaque);
104 /* add the node pointer in the attributes, so it will be freed
105 * when parsed is freed */
106 snprintf(key, sizeof(key), "_dyn_%p", child);
107 ret = ec_keyval_set(completed->attrs, key, child,
110 child = NULL; /* already freed */
114 return ec_node_complete_child(child, completed, strvec);
121 static struct ec_node_type ec_node_dynamic_type = {
123 .parse = ec_node_dynamic_parse,
124 .complete = ec_node_dynamic_complete,
125 .size = sizeof(struct ec_node_dynamic),
129 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
131 struct ec_node *gen_node = NULL;
132 struct ec_node_dynamic *node;
139 gen_node = __ec_node(&ec_node_dynamic_type, id);
140 if (gen_node == NULL)
143 node = (struct ec_node_dynamic *)gen_node;
145 node->opaque = opaque;
150 ec_node_free(gen_node);
155 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
157 static struct ec_node *
158 build_counter(struct ec_parsed *parsed, void *opaque)
160 const struct ec_node *node;
161 struct ec_parsed *iter;
162 unsigned int count = 0;
166 for (iter = ec_parsed_get_root(parsed); iter != NULL;
167 iter = ec_parsed_iter_next(iter)) {
168 node = ec_parsed_get_node(iter);
169 if (node->id && !strcmp(node->id, "my-id"))
172 snprintf(buf, sizeof(buf), "count-%u", count);
174 return ec_node_str("my-id", buf);
177 /* LCOV_EXCL_START */
178 static int ec_node_dynamic_testcase(void)
180 struct ec_node *node;
183 node = ec_node_many(EC_NO_ID,
184 ec_node_dynamic(EC_NO_ID, build_counter, NULL),
187 EC_LOG(EC_LOG_ERR, "cannot create node\n");
190 ret |= EC_TEST_CHECK_PARSE(node, 1, "count-0");
191 ret |= EC_TEST_CHECK_PARSE(node, 3, "count-0", "count-1", "count-2");
192 ret |= EC_TEST_CHECK_PARSE(node, 1, "count-0", "count-0");
194 /* test completion */
196 ret |= EC_TEST_CHECK_COMPLETE(node,
197 "c", EC_NODE_ENDLIST,
198 "count-0", EC_NODE_ENDLIST);
199 ret |= EC_TEST_CHECK_COMPLETE(node,
200 "count-0", "", EC_NODE_ENDLIST,
201 "count-1", EC_NODE_ENDLIST,
209 static struct ec_test ec_node_dynamic_test = {
210 .name = "node_dynamic",
211 .test = ec_node_dynamic_testcase,
214 EC_TEST_REGISTER(ec_node_dynamic_test);