88246bdf961945b3f5a98f0be8941bc790f812d4
[protos/libecoli.git] / src / ecoli_node_str.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9
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>
19
20 EC_LOG_TYPE_REGISTER(node_str);
21
22 struct ec_node_str {
23         char *string;
24         unsigned len;
25 };
26
27 static int
28 ec_node_str_parse(const struct ec_node *node,
29                 struct ec_pnode *state,
30                 const struct ec_strvec *strvec)
31 {
32         struct ec_node_str *priv = ec_node_priv(node);
33         const char *str;
34
35         (void)state;
36
37         if (priv->string == NULL) {
38                 errno = EINVAL;
39                 return -1;
40         }
41
42         if (ec_strvec_len(strvec) == 0)
43                 return EC_PARSE_NOMATCH;
44
45         str = ec_strvec_val(strvec, 0);
46         if (strcmp(str, priv->string) != 0)
47                 return EC_PARSE_NOMATCH;
48
49         return 1;
50 }
51
52 static int
53 ec_node_str_complete(const struct ec_node *node,
54                 struct ec_comp *comp,
55                 const struct ec_strvec *strvec)
56 {
57         struct ec_node_str *priv = ec_node_priv(node);
58         const char *str;
59         size_t n = 0;
60
61         if (ec_strvec_len(strvec) != 1)
62                 return 0;
63
64         str = ec_strvec_val(strvec, 0);
65         for (n = 0; n < priv->len; n++) {
66                 if (str[n] != priv->string[n])
67                         break;
68         }
69
70         /* no completion */
71         if (str[n] != '\0')
72                 return EC_PARSE_NOMATCH;
73
74         if (ec_comp_add_item(comp, node, NULL, EC_COMP_FULL,
75                                         str, priv->string) < 0)
76                 return -1;
77
78         return 0;
79 }
80
81 static const char *ec_node_str_desc(const struct ec_node *node)
82 {
83         struct ec_node_str *priv = ec_node_priv(node);
84
85         return priv->string;
86 }
87
88 static void ec_node_str_free_priv(struct ec_node *node)
89 {
90         struct ec_node_str *priv = ec_node_priv(node);
91
92         ec_free(priv->string);
93 }
94
95 static const struct ec_config_schema ec_node_str_schema[] = {
96         {
97                 .key = "string",
98                 .desc = "The string to match.",
99                 .type = EC_CONFIG_TYPE_STRING,
100         },
101         {
102                 .type = EC_CONFIG_TYPE_NONE,
103         },
104 };
105
106 static int ec_node_str_set_config(struct ec_node *node,
107                                 const struct ec_config *config)
108 {
109         struct ec_node_str *priv = ec_node_priv(node);
110         const struct ec_config *value = NULL;
111         char *s = NULL;
112
113         value = ec_config_dict_get(config, "string");
114         if (value == NULL) {
115                 errno = EINVAL;
116                 goto fail;
117         }
118
119         s = ec_strdup(value->string);
120         if (s == NULL)
121                 goto fail;
122
123         ec_free(priv->string);
124         priv->string = s;
125         priv->len = strlen(priv->string);
126
127         return 0;
128
129 fail:
130         ec_free(s);
131         return -1;
132 }
133
134 static struct ec_node_type ec_node_str_type = {
135         .name = "str",
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,
143 };
144
145 EC_NODE_TYPE_REGISTER(ec_node_str_type);
146
147 int ec_node_str_set_str(struct ec_node *node, const char *str)
148 {
149         struct ec_config *config = NULL;
150         int ret;
151
152         if (ec_node_check_type(node, &ec_node_str_type) < 0)
153                 goto fail;
154
155         if (str == NULL) {
156                 errno = EINVAL;
157                 goto fail;
158         }
159
160         config = ec_config_dict();
161         if (config == NULL)
162                 goto fail;
163
164         ret = ec_config_dict_set(config, "string", ec_config_string(str));
165         if (ret < 0)
166                 goto fail;
167
168         ret = ec_node_set_config(node, config);
169         config = NULL;
170         if (ret < 0)
171                 goto fail;
172
173         return 0;
174
175 fail:
176         ec_config_free(config);
177         return -1;
178 }
179
180 struct ec_node *ec_node_str(const char *id, const char *str)
181 {
182         struct ec_node *node = NULL;
183
184         node = ec_node_from_type(&ec_node_str_type, id);
185         if (node == NULL)
186                 goto fail;
187
188         if (ec_node_str_set_str(node, str) < 0)
189                 goto fail;
190
191         return node;
192
193 fail:
194         ec_node_free(node);
195         return NULL;
196 }
197
198 /* LCOV_EXCL_START */
199 static int ec_node_str_testcase(void)
200 {
201         struct ec_node *node;
202         int testres = 0;
203
204         node = ec_node_str(EC_NO_ID, "foo");
205         if (node == NULL) {
206                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
207                 return -1;
208         }
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, "");
216         ec_node_free(node);
217
218         node = ec_node_str(EC_NO_ID, "Здравствуйте");
219         if (node == NULL) {
220                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
221                 return -1;
222         }
223         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
224         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
225                 "John!");
226         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
227         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
228         ec_node_free(node);
229
230         /* an empty string node always matches */
231         node = ec_node_str(EC_NO_ID, "");
232         if (node == NULL) {
233                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
234                 return -1;
235         }
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");
239         ec_node_free(node);
240
241         /* test completion */
242         node = ec_node_str(EC_NO_ID, "foo");
243         if (node == NULL) {
244                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
245                 return -1;
246         }
247         testres |= EC_TEST_CHECK_COMPLETE(node,
248                 EC_VA_END,
249                 EC_VA_END);
250         testres |= EC_TEST_CHECK_COMPLETE(node,
251                 "", EC_VA_END,
252                 "foo", EC_VA_END);
253         testres |= EC_TEST_CHECK_COMPLETE(node,
254                 "f", EC_VA_END,
255                 "foo", EC_VA_END);
256         testres |= EC_TEST_CHECK_COMPLETE(node,
257                 "foo", EC_VA_END,
258                 "foo", EC_VA_END);
259         testres |= EC_TEST_CHECK_COMPLETE(node,
260                 "x", EC_VA_END,
261                 EC_VA_END);
262         ec_node_free(node);
263
264         return testres;
265 }
266 /* LCOV_EXCL_STOP */
267
268 static struct ec_test ec_node_str_test = {
269         .name = "node_str",
270         .test = ec_node_str_testcase,
271 };
272
273 EC_TEST_REGISTER(ec_node_str_test);