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_pnode *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 /* XXX startswith ? */
65 str = ec_strvec_val(strvec, 0);
66 for (n = 0; n < priv->len; n++) {
67 if (str[n] != priv->string[n])
73 return EC_PARSE_NOMATCH;
75 if (ec_comp_add_item(comp, node, NULL, EC_COMP_FULL,
76 str, priv->string) < 0)
82 static const char *ec_node_str_desc(const struct ec_node *node)
84 struct ec_node_str *priv = ec_node_priv(node);
89 static void ec_node_str_free_priv(struct ec_node *node)
91 struct ec_node_str *priv = ec_node_priv(node);
93 ec_free(priv->string);
96 static const struct ec_config_schema ec_node_str_schema[] = {
99 .desc = "The string to match.",
100 .type = EC_CONFIG_TYPE_STRING,
103 .type = EC_CONFIG_TYPE_NONE,
107 static int ec_node_str_set_config(struct ec_node *node,
108 const struct ec_config *config)
110 struct ec_node_str *priv = ec_node_priv(node);
111 const struct ec_config *value = NULL;
114 value = ec_config_dict_get(config, "string");
120 s = ec_strdup(value->string);
124 ec_free(priv->string);
126 priv->len = strlen(priv->string);
135 static struct ec_node_type ec_node_str_type = {
137 .schema = ec_node_str_schema,
138 .set_config = ec_node_str_set_config,
139 .parse = ec_node_str_parse,
140 .complete = ec_node_str_complete,
141 .desc = ec_node_str_desc,
142 .size = sizeof(struct ec_node_str),
143 .free_priv = ec_node_str_free_priv,
146 EC_NODE_TYPE_REGISTER(ec_node_str_type);
148 int ec_node_str_set_str(struct ec_node *node, const char *str)
150 struct ec_config *config = NULL;
153 if (ec_node_check_type(node, &ec_node_str_type) < 0)
161 config = ec_config_dict();
165 ret = ec_config_dict_set(config, "string", ec_config_string(str));
169 ret = ec_node_set_config(node, config);
177 ec_config_free(config);
181 struct ec_node *ec_node_str(const char *id, const char *str)
183 struct ec_node *node = NULL;
185 node = ec_node_from_type(&ec_node_str_type, id);
189 if (ec_node_str_set_str(node, str) < 0)
199 /* LCOV_EXCL_START */
200 static int ec_node_str_testcase(void)
202 struct ec_node *node;
205 node = ec_node_str(EC_NO_ID, "foo");
207 EC_LOG(EC_LOG_ERR, "cannot create node\n");
210 testres |= EC_TEST_CHECK(!strcmp(ec_node_desc(node), "foo"),
211 "Invalid node description.");
212 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
213 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
214 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
215 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
216 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
219 node = ec_node_str(EC_NO_ID, "Здравствуйте");
221 EC_LOG(EC_LOG_ERR, "cannot create node\n");
224 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
225 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
227 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
228 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
231 /* an empty string node always matches */
232 node = ec_node_str(EC_NO_ID, "");
234 EC_LOG(EC_LOG_ERR, "cannot create node\n");
237 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
238 testres |= EC_TEST_CHECK_PARSE(node, 1, "", "foo");
239 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
242 /* test completion */
243 node = ec_node_str(EC_NO_ID, "foo");
245 EC_LOG(EC_LOG_ERR, "cannot create node\n");
248 testres |= EC_TEST_CHECK_COMPLETE(node,
251 testres |= EC_TEST_CHECK_COMPLETE(node,
254 testres |= EC_TEST_CHECK_COMPLETE(node,
257 testres |= EC_TEST_CHECK_COMPLETE(node,
260 testres |= EC_TEST_CHECK_COMPLETE(node,
269 static struct ec_test ec_node_str_test = {
271 .test = ec_node_str_testcase,
274 EC_TEST_REGISTER(ec_node_str_test);