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 *pstate,
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);
58 const struct ec_comp_item *item;
62 if (ec_strvec_len(strvec) != 1)
65 /* XXX startswith ? */
66 str = ec_strvec_val(strvec, 0);
67 for (n = 0; n < priv->len; n++) {
68 if (str[n] != priv->string[n])
74 return EC_PARSE_NOMATCH;
76 item = ec_comp_add_item(comp, node, EC_COMP_FULL, str, priv->string);
83 static char *ec_node_str_desc(const struct ec_node *node)
85 struct ec_node_str *priv = ec_node_priv(node);
87 return ec_strdup(priv->string);
90 static void ec_node_str_free_priv(struct ec_node *node)
92 struct ec_node_str *priv = ec_node_priv(node);
94 ec_free(priv->string);
97 static const struct ec_config_schema ec_node_str_schema[] = {
100 .desc = "The string to match.",
101 .type = EC_CONFIG_TYPE_STRING,
104 .type = EC_CONFIG_TYPE_NONE,
108 static int ec_node_str_set_config(struct ec_node *node,
109 const struct ec_config *config)
111 struct ec_node_str *priv = ec_node_priv(node);
112 const struct ec_config *value = NULL;
115 value = ec_config_dict_get(config, "string");
121 s = ec_strdup(value->string);
125 ec_free(priv->string);
127 priv->len = strlen(priv->string);
136 static struct ec_node_type ec_node_str_type = {
138 .schema = ec_node_str_schema,
139 .set_config = ec_node_str_set_config,
140 .parse = ec_node_str_parse,
141 .complete = ec_node_str_complete,
142 .desc = ec_node_str_desc,
143 .size = sizeof(struct ec_node_str),
144 .free_priv = ec_node_str_free_priv,
147 EC_NODE_TYPE_REGISTER(ec_node_str_type);
149 int ec_node_str_set_str(struct ec_node *node, const char *str)
151 struct ec_config *config = NULL;
154 if (ec_node_check_type(node, &ec_node_str_type) < 0)
162 config = ec_config_dict();
166 ret = ec_config_dict_set(config, "string", ec_config_string(str));
170 ret = ec_node_set_config(node, config);
178 ec_config_free(config);
182 struct ec_node *ec_node_str(const char *id, const char *str)
184 struct ec_node *node = NULL;
186 node = ec_node_from_type(&ec_node_str_type, id);
190 if (ec_node_str_set_str(node, str) < 0)
200 /* LCOV_EXCL_START */
201 static int ec_node_str_testcase(void)
203 struct ec_node *node;
207 node = ec_node_str(EC_NO_ID, "foo");
209 EC_LOG(EC_LOG_ERR, "cannot create node\n");
212 desc = ec_node_desc(node);
213 testres |= EC_TEST_CHECK(!strcmp(desc, "foo"),
214 "Invalid node description.");
217 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
218 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
219 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
220 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
221 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
224 node = ec_node_str(EC_NO_ID, "Здравствуйте");
226 EC_LOG(EC_LOG_ERR, "cannot create node\n");
229 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
230 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
232 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
233 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
236 /* an empty string node always matches */
237 node = ec_node_str(EC_NO_ID, "");
239 EC_LOG(EC_LOG_ERR, "cannot create node\n");
242 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
243 testres |= EC_TEST_CHECK_PARSE(node, 1, "", "foo");
244 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
247 /* test completion */
248 node = ec_node_str(EC_NO_ID, "foo");
250 EC_LOG(EC_LOG_ERR, "cannot create node\n");
253 testres |= EC_TEST_CHECK_COMPLETE(node,
256 testres |= EC_TEST_CHECK_COMPLETE(node,
259 testres |= EC_TEST_CHECK_COMPLETE(node,
262 testres |= EC_TEST_CHECK_COMPLETE(node,
265 testres |= EC_TEST_CHECK_COMPLETE(node,
274 static struct ec_test ec_node_str_test = {
276 .test = ec_node_str_testcase,
279 EC_TEST_REGISTER(ec_node_str_test);