standardize return values + errno
[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_parse.h>
16 #include <ecoli_complete.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_parse *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_PARSE_NOMATCH;
39
40         str = ec_strvec_val(strvec, 0);
41         if (strcmp(str, node->string) != 0)
42                 return EC_PARSE_NOMATCH;
43
44         return 1;
45 }
46
47 static int
48 ec_node_str_complete(const struct ec_node *gen_node,
49                 struct ec_comp *comp,
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_PARSE_NOMATCH;
68
69         if (ec_comp_add_item(comp, 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         char *s = NULL;
105         int ret;
106
107         ret = ec_node_check_type(gen_node, &ec_node_str_type);
108         if (ret < 0)
109                 return ret;
110
111         if (str == NULL) {
112                 errno = EINVAL;
113                 return -1;
114         }
115
116         s = ec_strdup(str);
117         if (s == NULL)
118                 return -1;
119
120         ec_free(node->string);
121         node->string = s;
122         node->len = strlen(node->string);
123
124         return 0;
125 }
126
127 struct ec_node *ec_node_str(const char *id, const char *str)
128 {
129         struct ec_node *gen_node = NULL;
130
131         gen_node = __ec_node(&ec_node_str_type, id);
132         if (gen_node == NULL)
133                 goto fail;
134
135         if (ec_node_str_set_str(gen_node, str) < 0)
136                 goto fail;
137
138         return gen_node;
139
140 fail:
141         ec_node_free(gen_node);
142         return NULL;
143 }
144
145 /* LCOV_EXCL_START */
146 static int ec_node_str_testcase(void)
147 {
148         struct ec_node *node;
149         int testres = 0;
150
151         node = ec_node_str(EC_NO_ID, "foo");
152         if (node == NULL) {
153                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
154                 return -1;
155         }
156         testres |= EC_TEST_CHECK(!strcmp(ec_node_desc(node), "foo"),
157                 "Invalid node description.");
158         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
159         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
160         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
161         testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
162         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
163         ec_node_free(node);
164
165         node = ec_node_str(EC_NO_ID, "Здравствуйте");
166         if (node == NULL) {
167                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
168                 return -1;
169         }
170         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
171         testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
172                 "John!");
173         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
174         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
175         ec_node_free(node);
176
177         /* an empty string node always matches */
178         node = ec_node_str(EC_NO_ID, "");
179         if (node == NULL) {
180                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
181                 return -1;
182         }
183         testres |= EC_TEST_CHECK_PARSE(node, 1, "");
184         testres |= EC_TEST_CHECK_PARSE(node, 1, "", "foo");
185         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
186         ec_node_free(node);
187
188         /* test completion */
189         node = ec_node_str(EC_NO_ID, "foo");
190         if (node == NULL) {
191                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
192                 return -1;
193         }
194         testres |= EC_TEST_CHECK_COMPLETE(node,
195                 EC_NODE_ENDLIST,
196                 EC_NODE_ENDLIST);
197         testres |= EC_TEST_CHECK_COMPLETE(node,
198                 "", EC_NODE_ENDLIST,
199                 "foo", EC_NODE_ENDLIST);
200         testres |= EC_TEST_CHECK_COMPLETE(node,
201                 "f", EC_NODE_ENDLIST,
202                 "foo", EC_NODE_ENDLIST);
203         testres |= EC_TEST_CHECK_COMPLETE(node,
204                 "foo", EC_NODE_ENDLIST,
205                 "foo", EC_NODE_ENDLIST);
206         testres |= EC_TEST_CHECK_COMPLETE(node,
207                 "x", EC_NODE_ENDLIST,
208                 EC_NODE_ENDLIST);
209         ec_node_free(node);
210
211         return testres;
212 }
213 /* LCOV_EXCL_STOP */
214
215 static struct ec_test ec_node_str_test = {
216         .name = "node_str",
217         .test = ec_node_str_testcase,
218 };
219
220 EC_TEST_REGISTER(ec_node_str_test);