1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_str.h>
19 #include <ecoli_test.h>
20 #include <ecoli_config.h>
21 #include <ecoli_node_option.h>
23 EC_LOG_TYPE_REGISTER(node_option);
25 struct ec_node_option {
27 struct ec_node *child;
31 ec_node_option_parse(const struct ec_node *gen_node,
32 struct ec_parse *state,
33 const struct ec_strvec *strvec)
35 struct ec_node_option *node = (struct ec_node_option *)gen_node;
38 ret = ec_node_parse_child(node->child, state, strvec);
42 if (ret == EC_PARSE_NOMATCH)
49 ec_node_option_complete(const struct ec_node *gen_node,
51 const struct ec_strvec *strvec)
53 struct ec_node_option *node = (struct ec_node_option *)gen_node;
55 return ec_node_complete_child(node->child, comp, strvec);
58 static void ec_node_option_free_priv(struct ec_node *gen_node)
60 struct ec_node_option *node = (struct ec_node_option *)gen_node;
62 ec_node_free(node->child);
66 ec_node_option_get_children_count(const struct ec_node *gen_node)
68 struct ec_node_option *node = (struct ec_node_option *)gen_node;
76 ec_node_option_get_child(const struct ec_node *gen_node, size_t i,
77 struct ec_node **child, unsigned int *refs)
79 struct ec_node_option *node = (struct ec_node_option *)gen_node;
89 static const struct ec_config_schema ec_node_option_schema[] = {
92 .desc = "The child node.",
93 .type = EC_CONFIG_TYPE_NODE,
96 .type = EC_CONFIG_TYPE_NONE,
100 static int ec_node_option_set_config(struct ec_node *gen_node,
101 const struct ec_config *config)
103 struct ec_node_option *node = (struct ec_node_option *)gen_node;
104 const struct ec_config *child;
106 child = ec_config_dict_get(config, "child");
109 if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
114 if (node->child != NULL)
115 ec_node_free(node->child);
116 node->child = ec_node_clone(child->node);
124 static struct ec_node_type ec_node_option_type = {
126 .schema = ec_node_option_schema,
127 .set_config = ec_node_option_set_config,
128 .parse = ec_node_option_parse,
129 .complete = ec_node_option_complete,
130 .size = sizeof(struct ec_node_option),
131 .free_priv = ec_node_option_free_priv,
132 .get_children_count = ec_node_option_get_children_count,
133 .get_child = ec_node_option_get_child,
136 EC_NODE_TYPE_REGISTER(ec_node_option_type);
139 ec_node_option_set_child(struct ec_node *gen_node, struct ec_node *child)
141 const struct ec_config *cur_config = NULL;
142 struct ec_config *config = NULL;
145 if (ec_node_check_type(gen_node, &ec_node_option_type) < 0)
148 cur_config = ec_node_get_config(gen_node);
149 if (cur_config == NULL)
150 config = ec_config_dict();
152 config = ec_config_dup(cur_config);
156 if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
157 child = NULL; /* freed */
160 child = NULL; /* freed */
162 ret = ec_node_set_config(gen_node, config);
163 config = NULL; /* freed */
170 ec_config_free(config);
175 struct ec_node *ec_node_option(const char *id, struct ec_node *child)
177 struct ec_node *gen_node = NULL;
182 gen_node = ec_node_from_type(&ec_node_option_type, id);
183 if (gen_node == NULL)
186 ec_node_option_set_child(gen_node, child);
196 /* LCOV_EXCL_START */
197 static int ec_node_option_testcase(void)
199 struct ec_node *node;
202 node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
204 EC_LOG(EC_LOG_ERR, "cannot create node\n");
207 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
208 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
209 testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
210 testres |= EC_TEST_CHECK_PARSE(node, 0);
213 /* test completion */
214 node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
216 EC_LOG(EC_LOG_ERR, "cannot create node\n");
219 testres |= EC_TEST_CHECK_COMPLETE(node,
221 "foo", EC_NODE_ENDLIST);
222 testres |= EC_TEST_CHECK_COMPLETE(node,
223 "f", EC_NODE_ENDLIST,
224 "foo", EC_NODE_ENDLIST);
225 testres |= EC_TEST_CHECK_COMPLETE(node,
226 "b", EC_NODE_ENDLIST,
234 static struct ec_test ec_node_option_test = {
235 .name = "node_option",
236 .test = ec_node_option_testcase,
239 EC_TEST_REGISTER(ec_node_option_test);