1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
10 #include <ecoli_log.h>
11 #include <ecoli_malloc.h>
12 #include <ecoli_test.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_node.h>
15 #include <ecoli_config.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_str.h>
20 EC_LOG_TYPE_REGISTER(node_str);
28 ec_node_str_parse(const struct ec_node *node,
29 struct ec_parse *state,
30 const struct ec_strvec *strvec)
32 struct ec_node_str *priv = ec_node_priv(node);
37 if (priv->string == NULL) {
42 if (ec_strvec_len(strvec) == 0)
43 return EC_PARSE_NOMATCH;
45 str = ec_strvec_val(strvec, 0);
46 if (strcmp(str, priv->string) != 0)
47 return EC_PARSE_NOMATCH;
53 ec_node_str_complete(const struct ec_node *node,
55 const struct ec_strvec *strvec)
57 struct ec_node_str *priv = ec_node_priv(node);
61 if (ec_strvec_len(strvec) != 1)
64 str = ec_strvec_val(strvec, 0);
65 for (n = 0; n < priv->len; n++) {
66 if (str[n] != priv->string[n])
72 return EC_PARSE_NOMATCH;
74 if (ec_comp_add_item(comp, node, NULL, EC_COMP_FULL,
75 str, priv->string) < 0)
81 static const char *ec_node_str_desc(const struct ec_node *node)
83 struct ec_node_str *priv = ec_node_priv(node);
88 static void ec_node_str_free_priv(struct ec_node *node)
90 struct ec_node_str *priv = ec_node_priv(node);
92 ec_free(priv->string);
95 static const struct ec_config_schema ec_node_str_schema[] = {
98 .desc = "The string to match.",
99 .type = EC_CONFIG_TYPE_STRING,
102 .type = EC_CONFIG_TYPE_NONE,
106 static int ec_node_str_set_config(struct ec_node *node,
107 const struct ec_config *config)
109 struct ec_node_str *priv = ec_node_priv(node);
110 const struct ec_config *value = NULL;
113 value = ec_config_dict_get(config, "string");
119 s = ec_strdup(value->string);
123 ec_free(priv->string);
125 priv->len = strlen(priv->string);
134 static struct ec_node_type ec_node_str_type = {
136 .schema = ec_node_str_schema,
137 .set_config = ec_node_str_set_config,
138 .parse = ec_node_str_parse,
139 .complete = ec_node_str_complete,
140 .desc = ec_node_str_desc,
141 .size = sizeof(struct ec_node_str),
142 .free_priv = ec_node_str_free_priv,
145 EC_NODE_TYPE_REGISTER(ec_node_str_type);
147 int ec_node_str_set_str(struct ec_node *node, const char *str)
149 struct ec_config *config = NULL;
152 if (ec_node_check_type(node, &ec_node_str_type) < 0)
160 config = ec_config_dict();
164 ret = ec_config_dict_set(config, "string", ec_config_string(str));
168 ret = ec_node_set_config(node, config);
176 ec_config_free(config);
180 struct ec_node *ec_node_str(const char *id, const char *str)
182 struct ec_node *node = NULL;
184 node = ec_node_from_type(&ec_node_str_type, id);
188 if (ec_node_str_set_str(node, str) < 0)
198 /* LCOV_EXCL_START */
199 static int ec_node_str_testcase(void)
201 struct ec_node *node;
204 node = ec_node_str(EC_NO_ID, "foo");
206 EC_LOG(EC_LOG_ERR, "cannot create node\n");
209 testres |= EC_TEST_CHECK(!strcmp(ec_node_desc(node), "foo"),
210 "Invalid node description.");
211 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
212 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
213 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
214 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
215 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
218 node = ec_node_str(EC_NO_ID, "Здравствуйте");
220 EC_LOG(EC_LOG_ERR, "cannot create node\n");
223 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
224 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
226 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
227 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
230 /* an empty string node always matches */
231 node = ec_node_str(EC_NO_ID, "");
233 EC_LOG(EC_LOG_ERR, "cannot create node\n");
236 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
237 testres |= EC_TEST_CHECK_PARSE(node, 1, "", "foo");
238 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
241 /* test completion */
242 node = ec_node_str(EC_NO_ID, "foo");
244 EC_LOG(EC_LOG_ERR, "cannot create node\n");
247 testres |= EC_TEST_CHECK_COMPLETE(node,
250 testres |= EC_TEST_CHECK_COMPLETE(node,
252 "foo", EC_NODE_ENDLIST);
253 testres |= EC_TEST_CHECK_COMPLETE(node,
254 "f", EC_NODE_ENDLIST,
255 "foo", EC_NODE_ENDLIST);
256 testres |= EC_TEST_CHECK_COMPLETE(node,
257 "foo", EC_NODE_ENDLIST,
258 "foo", EC_NODE_ENDLIST);
259 testres |= EC_TEST_CHECK_COMPLETE(node,
260 "x", EC_NODE_ENDLIST,
268 static struct ec_test ec_node_str_test = {
270 .test = ec_node_str_testcase,
273 EC_TEST_REGISTER(ec_node_str_test);