api doc and minor changes
[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 *pstate,
30                 const struct ec_strvec *strvec)
31 {
32         struct ec_node_str *priv = ec_node_priv(node);
33         const char *str;
34
35         (void)pstate;
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 struct ec_comp_item *item;
59         const char *str;
60         size_t n = 0;
61
62         if (ec_strvec_len(strvec) != 1)
63                 return 0;
64
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])
69                         break;
70         }
71
72         /* no completion */
73         if (str[n] != '\0')
74                 return EC_PARSE_NOMATCH;
75
76         item = ec_comp_add_item(comp, node, EC_COMP_FULL, str, priv->string);
77         if (item == NULL)
78                 return -1;
79
80         return 0;
81 }
82
83 static char *ec_node_str_desc(const struct ec_node *node)
84 {
85         struct ec_node_str *priv = ec_node_priv(node);
86
87         return ec_strdup(priv->string);
88 }
89
90 static void ec_node_str_free_priv(struct ec_node *node)
91 {
92         struct ec_node_str *priv = ec_node_priv(node);
93
94         ec_free(priv->string);
95 }
96
97 static const struct ec_config_schema ec_node_str_schema[] = {
98         {
99                 .key = "string",
100                 .desc = "The string to match.",
101                 .type = EC_CONFIG_TYPE_STRING,
102         },
103         {
104                 .type = EC_CONFIG_TYPE_NONE,
105         },
106 };
107
108 static int ec_node_str_set_config(struct ec_node *node,
109                                 const struct ec_config *config)
110 {
111         struct ec_node_str *priv = ec_node_priv(node);
112         const struct ec_config *value = NULL;
113         char *s = NULL;
114
115         value = ec_config_dict_get(config, "string");
116         if (value == NULL) {
117                 errno = EINVAL;
118                 goto fail;
119         }
120
121         s = ec_strdup(value->string);
122         if (s == NULL)
123                 goto fail;
124
125         ec_free(priv->string);
126         priv->string = s;
127         priv->len = strlen(priv->string);
128
129         return 0;
130
131 fail:
132         ec_free(s);
133         return -1;
134 }
135
136 static struct ec_node_type ec_node_str_type = {
137         .name = "str",
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,
145 };
146
147 EC_NODE_TYPE_REGISTER(ec_node_str_type);
148
149 int ec_node_str_set_str(struct ec_node *node, const char *str)
150 {
151         struct ec_config *config = NULL;
152         int ret;
153
154         if (ec_node_check_type(node, &ec_node_str_type) < 0)
155                 goto fail;
156
157         if (str == NULL) {
158                 errno = EINVAL;
159                 goto fail;
160         }
161
162         config = ec_config_dict();
163         if (config == NULL)
164                 goto fail;
165
166         ret = ec_config_dict_set(config, "string", ec_config_string(str));
167         if (ret < 0)
168                 goto fail;
169
170         ret = ec_node_set_config(node, config);
171         config = NULL;
172         if (ret < 0)
173                 goto fail;
174
175         return 0;
176
177 fail:
178         ec_config_free(config);
179         return -1;
180 }
181
182 struct ec_node *ec_node_str(const char *id, const char *str)
183 {
184         struct ec_node *node = NULL;
185
186         node = ec_node_from_type(&ec_node_str_type, id);
187         if (node == NULL)
188                 goto fail;
189
190         if (ec_node_str_set_str(node, str) < 0)
191                 goto fail;
192
193         return node;
194
195 fail:
196         ec_node_free(node);
197         return NULL;
198 }
199
200 /* LCOV_EXCL_START */
201 static int ec_node_str_testcase(void)
202 {
203         struct ec_node *node;
204         int testres = 0;
205         char *desc = NULL;
206
207         node = ec_node_str(EC_NO_ID, "foo");
208         if (node == NULL) {
209                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
210                 return -1;
211         }
212         desc = ec_node_desc(node);
213         testres |= EC_TEST_CHECK(!strcmp(desc, "foo"),
214                 "Invalid node description.");
215         ec_free(desc);
216         desc = NULL;
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, "");
222         ec_node_free(node);
223
224         node = ec_node_str(EC_NO_ID, "Здравствуйте");
225         if (node == NULL) {
226                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
227                 return -1;
228         }
229         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
230         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
231                 "John!");
232         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
233         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
234         ec_node_free(node);
235
236         /* an empty string node always matches */
237         node = ec_node_str(EC_NO_ID, "");
238         if (node == NULL) {
239                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
240                 return -1;
241         }
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");
245         ec_node_free(node);
246
247         /* test completion */
248         node = ec_node_str(EC_NO_ID, "foo");
249         if (node == NULL) {
250                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
251                 return -1;
252         }
253         testres |= EC_TEST_CHECK_COMPLETE(node,
254                 EC_VA_END,
255                 EC_VA_END);
256         testres |= EC_TEST_CHECK_COMPLETE(node,
257                 "", EC_VA_END,
258                 "foo", EC_VA_END);
259         testres |= EC_TEST_CHECK_COMPLETE(node,
260                 "f", EC_VA_END,
261                 "foo", EC_VA_END);
262         testres |= EC_TEST_CHECK_COMPLETE(node,
263                 "foo", EC_VA_END,
264                 "foo", EC_VA_END);
265         testres |= EC_TEST_CHECK_COMPLETE(node,
266                 "x", EC_VA_END,
267                 EC_VA_END);
268         ec_node_free(node);
269
270         return testres;
271 }
272 /* LCOV_EXCL_STOP */
273
274 static struct ec_test ec_node_str_test = {
275         .name = "node_str",
276         .test = ec_node_str_testcase,
277 };
278
279 EC_TEST_REGISTER(ec_node_str_test);