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,
78 return ec_node_parse_child(child, parsed, strvec);
86 ec_node_dynamic_complete(const struct ec_node *gen_node,
87 struct ec_completed *completed,
88 const struct ec_strvec *strvec)
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;
97 parsed = ec_completed_get_state(completed);
98 child = node->build(parsed, node->opaque);
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,
110 return ec_node_complete_child(child, completed, strvec);
117 static struct ec_node_type ec_node_dynamic_type = {
119 .parse = ec_node_dynamic_parse,
120 .complete = ec_node_dynamic_complete,
121 .size = sizeof(struct ec_node_dynamic),
125 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
127 struct ec_node *gen_node = NULL;
128 struct ec_node_dynamic *node;
135 gen_node = __ec_node(&ec_node_dynamic_type, id);
136 if (gen_node == NULL)
139 node = (struct ec_node_dynamic *)gen_node;
141 node->opaque = opaque;
146 ec_node_free(gen_node);
151 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
153 static struct ec_node *
154 build_counter(struct ec_parsed *parsed, void *opaque)
156 const struct ec_node *node;
157 struct ec_parsed *iter;
158 unsigned int count = 0;
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"))
168 snprintf(buf, sizeof(buf), "count-%u", count);
170 return ec_node_str("my-id", buf);
173 /* LCOV_EXCL_START */
174 static int ec_node_dynamic_testcase(void)
176 struct ec_node *node;
179 node = ec_node_many(EC_NO_ID,
180 ec_node_dynamic(EC_NO_ID, build_counter, NULL),
183 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
190 /* test completion */
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,
205 static struct ec_test ec_node_dynamic_test = {
206 .name = "node_dynamic",
207 .test = ec_node_dynamic_testcase,
210 EC_TEST_REGISTER(ec_node_dynamic_test);