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