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 {
26 struct ec_node *child;
30 ec_node_option_parse(const struct ec_node *node,
31 struct ec_pnode *state,
32 const struct ec_strvec *strvec)
34 struct ec_node_option *priv = ec_node_priv(node);
37 ret = ec_parse_child(priv->child, state, strvec);
41 if (ret == EC_PARSE_NOMATCH)
48 ec_node_option_complete(const struct ec_node *node,
50 const struct ec_strvec *strvec)
52 struct ec_node_option *priv = ec_node_priv(node);
54 return ec_complete_child(priv->child, comp, strvec);
57 static void ec_node_option_free_priv(struct ec_node *node)
59 struct ec_node_option *priv = ec_node_priv(node);
61 ec_node_free(priv->child);
65 ec_node_option_get_children_count(const struct ec_node *node)
67 struct ec_node_option *priv = ec_node_priv(node);
75 ec_node_option_get_child(const struct ec_node *node, size_t i,
76 struct ec_node **child, unsigned int *refs)
78 struct ec_node_option *priv = ec_node_priv(node);
88 static const struct ec_config_schema ec_node_option_schema[] = {
91 .desc = "The child node.",
92 .type = EC_CONFIG_TYPE_NODE,
95 .type = EC_CONFIG_TYPE_NONE,
99 static int ec_node_option_set_config(struct ec_node *node,
100 const struct ec_config *config)
102 struct ec_node_option *priv = ec_node_priv(node);
103 const struct ec_config *child;
105 child = ec_config_dict_get(config, "child");
108 if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
113 if (priv->child != NULL)
114 ec_node_free(priv->child);
115 priv->child = ec_node_clone(child->node);
123 static struct ec_node_type ec_node_option_type = {
125 .schema = ec_node_option_schema,
126 .set_config = ec_node_option_set_config,
127 .parse = ec_node_option_parse,
128 .complete = ec_node_option_complete,
129 .size = sizeof(struct ec_node_option),
130 .free_priv = ec_node_option_free_priv,
131 .get_children_count = ec_node_option_get_children_count,
132 .get_child = ec_node_option_get_child,
135 EC_NODE_TYPE_REGISTER(ec_node_option_type);
138 ec_node_option_set_child(struct ec_node *node, struct ec_node *child)
140 const struct ec_config *cur_config = NULL;
141 struct ec_config *config = NULL;
144 if (ec_node_check_type(node, &ec_node_option_type) < 0)
147 cur_config = ec_node_get_config(node);
148 if (cur_config == NULL)
149 config = ec_config_dict();
151 config = ec_config_dup(cur_config);
155 if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
156 child = NULL; /* freed */
159 child = NULL; /* freed */
161 ret = ec_node_set_config(node, config);
162 config = NULL; /* freed */
169 ec_config_free(config);
174 struct ec_node *ec_node_option(const char *id, struct ec_node *child)
176 struct ec_node *node = NULL;
181 node = ec_node_from_type(&ec_node_option_type, id);
185 ec_node_option_set_child(node, child);
195 /* LCOV_EXCL_START */
196 static int ec_node_option_testcase(void)
198 struct ec_node *node;
201 node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
203 EC_LOG(EC_LOG_ERR, "cannot create node\n");
206 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
207 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
208 testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
209 testres |= EC_TEST_CHECK_PARSE(node, 0);
212 /* test completion */
213 node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
215 EC_LOG(EC_LOG_ERR, "cannot create node\n");
218 testres |= EC_TEST_CHECK_COMPLETE(node,
221 testres |= EC_TEST_CHECK_COMPLETE(node,
224 testres |= EC_TEST_CHECK_COMPLETE(node,
233 static struct ec_test ec_node_option_test = {
234 .name = "node_option",
235 .test = ec_node_option_testcase,
238 EC_TEST_REGISTER(ec_node_option_test);