standardize copyright
[protos/libecoli.git] / lib / 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_parsed.h>
16 #include <ecoli_completed.h>
17 #include <ecoli_node_str.h>
18
19 EC_LOG_TYPE_REGISTER(node_str);
20
21 struct ec_node_str {
22         struct ec_node gen;
23         char *string;
24         unsigned len;
25 };
26
27 static int
28 ec_node_str_parse(const struct ec_node *gen_node,
29                 struct ec_parsed *state,
30                 const struct ec_strvec *strvec)
31 {
32         struct ec_node_str *node = (struct ec_node_str *)gen_node;
33         const char *str;
34
35         (void)state;
36
37         if (ec_strvec_len(strvec) == 0)
38                 return EC_PARSED_NOMATCH;
39
40         str = ec_strvec_val(strvec, 0);
41         if (strcmp(str, node->string) != 0)
42                 return EC_PARSED_NOMATCH;
43
44         return 1;
45 }
46
47 static int
48 ec_node_str_complete(const struct ec_node *gen_node,
49                 struct ec_completed *completed,
50                 const struct ec_strvec *strvec)
51 {
52         struct ec_node_str *node = (struct ec_node_str *)gen_node;
53         const char *str;
54         size_t n = 0;
55
56         if (ec_strvec_len(strvec) != 1)
57                 return 0;
58
59         str = ec_strvec_val(strvec, 0);
60         for (n = 0; n < node->len; n++) {
61                 if (str[n] != node->string[n])
62                         break;
63         }
64
65         /* no completion */
66         if (str[n] != '\0')
67                 return EC_PARSED_NOMATCH;
68
69         if (ec_completed_add_item(completed, gen_node, NULL, EC_COMP_FULL,
70                                         str, node->string) < 0)
71                 return -1;
72
73         return 0;
74 }
75
76 static const char *ec_node_str_desc(const struct ec_node *gen_node)
77 {
78         struct ec_node_str *node = (struct ec_node_str *)gen_node;
79
80         return node->string;
81 }
82
83 static void ec_node_str_free_priv(struct ec_node *gen_node)
84 {
85         struct ec_node_str *node = (struct ec_node_str *)gen_node;
86
87         ec_free(node->string);
88 }
89
90 static struct ec_node_type ec_node_str_type = {
91         .name = "str",
92         .parse = ec_node_str_parse,
93         .complete = ec_node_str_complete,
94         .desc = ec_node_str_desc,
95         .size = sizeof(struct ec_node_str),
96         .free_priv = ec_node_str_free_priv,
97 };
98
99 EC_NODE_TYPE_REGISTER(ec_node_str_type);
100
101 int ec_node_str_set_str(struct ec_node *gen_node, const char *str)
102 {
103         struct ec_node_str *node = (struct ec_node_str *)gen_node;
104         int ret;
105
106         ret = ec_node_check_type(gen_node, &ec_node_str_type);
107         if (ret < 0)
108                 return ret;
109
110         if (str == NULL)
111                 return -EINVAL;
112         ec_free(node->string);
113         node->string = ec_strdup(str);
114         if (node->string == NULL)
115                 return -ENOMEM;
116
117         node->len = strlen(node->string);
118
119         return 0;
120 }
121
122 struct ec_node *ec_node_str(const char *id, const char *str)
123 {
124         struct ec_node *gen_node = NULL;
125
126         gen_node = __ec_node(&ec_node_str_type, id);
127         if (gen_node == NULL)
128                 goto fail;
129
130         if (ec_node_str_set_str(gen_node, str) < 0)
131                 goto fail;
132
133         return gen_node;
134
135 fail:
136         ec_node_free(gen_node);
137         return NULL;
138 }
139
140 /* LCOV_EXCL_START */
141 static int ec_node_str_testcase(void)
142 {
143         struct ec_node *node;
144         int testres = 0;
145
146         node = ec_node_str(EC_NO_ID, "foo");
147         if (node == NULL) {
148                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
149                 return -1;
150         }
151         testres |= EC_TEST_CHECK(!strcmp(ec_node_desc(node), "foo"),
152                 "Invalid node description.");
153         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
154         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
155         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
156         testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
157         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
158         ec_node_free(node);
159
160         node = ec_node_str(EC_NO_ID, "Здравствуйте");
161         if (node == NULL) {
162                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
163                 return -1;
164         }
165         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
166         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
167                 "John!");
168         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
169         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
170         ec_node_free(node);
171
172         /* an empty string node always matches */
173         node = ec_node_str(EC_NO_ID, "");
174         if (node == NULL) {
175                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
176                 return -1;
177         }
178         testres |= EC_TEST_CHECK_PARSE(node, 1, "");
179         testres |= EC_TEST_CHECK_PARSE(node, 1, "", "foo");
180         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
181         ec_node_free(node);
182
183         /* test completion */
184         node = ec_node_str(EC_NO_ID, "foo");
185         if (node == NULL) {
186                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
187                 return -1;
188         }
189         testres |= EC_TEST_CHECK_COMPLETE(node,
190                 EC_NODE_ENDLIST,
191                 EC_NODE_ENDLIST);
192         testres |= EC_TEST_CHECK_COMPLETE(node,
193                 "", EC_NODE_ENDLIST,
194                 "foo", EC_NODE_ENDLIST);
195         testres |= EC_TEST_CHECK_COMPLETE(node,
196                 "f", EC_NODE_ENDLIST,
197                 "foo", EC_NODE_ENDLIST);
198         testres |= EC_TEST_CHECK_COMPLETE(node,
199                 "foo", EC_NODE_ENDLIST,
200                 "foo", EC_NODE_ENDLIST);
201         testres |= EC_TEST_CHECK_COMPLETE(node,
202                 "x", EC_NODE_ENDLIST,
203                 EC_NODE_ENDLIST);
204         ec_node_free(node);
205
206         return testres;
207 }
208 /* LCOV_EXCL_STOP */
209
210 static struct ec_test ec_node_str_test = {
211         .name = "node_str",
212         .test = ec_node_str_testcase,
213 };
214
215 EC_TEST_REGISTER(ec_node_str_test);