738b43d3f5877a5a4069bdc6f661895224b135b5
[protos/libecoli.git] / lib / ecoli_node_once.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 <assert.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_parse.h>
18 #include <ecoli_complete.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_or.h>
21 #include <ecoli_node_many.h>
22 #include <ecoli_node_once.h>
23
24 EC_LOG_TYPE_REGISTER(node_once);
25
26 struct ec_node_once {
27         struct ec_node gen;
28         struct ec_node *child;
29 };
30
31 static unsigned int
32 count_node(struct ec_parse *parse, const struct ec_node *node)
33 {
34         struct ec_parse *child;
35         unsigned int count = 0;
36
37         if (parse == NULL)
38                 return 0;
39
40         if (ec_parse_get_node(parse) == node)
41                 count++;
42
43         EC_PARSE_FOREACH_CHILD(child, parse)
44                 count += count_node(child, node);
45
46         return count;
47 }
48
49 static int
50 ec_node_once_parse(const struct ec_node *gen_node,
51                 struct ec_parse *state,
52                 const struct ec_strvec *strvec)
53 {
54         struct ec_node_once *node = (struct ec_node_once *)gen_node;
55         unsigned int count;
56
57         /* count the number of occurences of the node: if already parsed,
58          * do not match
59          */
60         count = count_node(ec_parse_get_root(state), node->child);
61         if (count > 0)
62                 return EC_PARSE_NOMATCH;
63
64         return ec_node_parse_child(node->child, state, strvec);
65 }
66
67 static int
68 ec_node_once_complete(const struct ec_node *gen_node,
69                 struct ec_comp *comp,
70                 const struct ec_strvec *strvec)
71 {
72         struct ec_node_once *node = (struct ec_node_once *)gen_node;
73         struct ec_parse *parse = ec_comp_get_state(comp);
74         unsigned int count;
75         int ret;
76
77         /* count the number of occurences of the node: if already parsed,
78          * do not match
79          */
80         count = count_node(ec_parse_get_root(parse), node->child);
81         if (count > 0)
82                 return 0;
83
84         ret = ec_node_complete_child(node->child, comp, strvec);
85         if (ret < 0)
86                 return ret;
87
88         return 0;
89 }
90
91 static size_t
92 ec_node_once_get_children_count(const struct ec_node *gen_node)
93 {
94         struct ec_node_once *node = (struct ec_node_once *)gen_node;
95
96         if (node->child)
97                 return 1;
98         return 0;
99 }
100
101 static struct ec_node *
102 ec_node_once_get_child(const struct ec_node *gen_node, size_t i)
103 {
104         struct ec_node_once *node = (struct ec_node_once *)gen_node;
105
106         if (i >= 1)
107                 return NULL;
108
109         return node->child;
110 }
111
112 static struct ec_node_type ec_node_once_type = {
113         .name = "once",
114         .parse = ec_node_once_parse,
115         .complete = ec_node_once_complete,
116         .size = sizeof(struct ec_node_once),
117         .get_children_count = ec_node_once_get_children_count,
118         .get_child = ec_node_once_get_child,
119 };
120
121 EC_NODE_TYPE_REGISTER(ec_node_once_type);
122
123 int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child)
124 {
125         struct ec_node_once *node = (struct ec_node_once *)gen_node;
126
127         if (gen_node == NULL || child == NULL) {
128                 errno = EINVAL;
129                 goto fail;
130         }
131
132         if (ec_node_check_type(gen_node, &ec_node_once_type) < 0)
133                 goto fail;
134
135         node->child = child;
136
137         return 0;
138
139 fail:
140         ec_node_free(child);
141         return -1;
142 }
143
144 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
145 {
146         struct ec_node *gen_node = NULL;
147
148         if (child == NULL)
149                 return NULL;
150
151         gen_node = __ec_node(&ec_node_once_type, id);
152         if (gen_node == NULL)
153                 goto fail;
154
155         ec_node_once_set(gen_node, child);
156         child = NULL;
157
158         return gen_node;
159
160 fail:
161         ec_node_free(child);
162         return NULL;
163 }
164
165 /* LCOV_EXCL_START */
166 static int ec_node_once_testcase(void)
167 {
168         struct ec_node *node;
169         int testres = 0;
170
171         node = ec_node_many(EC_NO_ID,
172                         EC_NODE_OR(EC_NO_ID,
173                                 ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
174                                 ec_node_str(EC_NO_ID, "bar")
175                                 ), 0, 0
176                 );
177         if (node == NULL) {
178                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
179                 return -1;
180         }
181         testres |= EC_TEST_CHECK_PARSE(node, 0);
182         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
183         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
184         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
185         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "bar");
186         testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "bar");
187         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo", "foo");
188         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
189         testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
190
191         testres |= EC_TEST_CHECK_COMPLETE(node,
192                 "", EC_NODE_ENDLIST,
193                 "foo", "bar", EC_NODE_ENDLIST);
194         testres |= EC_TEST_CHECK_COMPLETE(node,
195                 "f", EC_NODE_ENDLIST,
196                 "foo", EC_NODE_ENDLIST);
197         testres |= EC_TEST_CHECK_COMPLETE(node,
198                 "b", EC_NODE_ENDLIST,
199                 "bar", EC_NODE_ENDLIST);
200         testres |= EC_TEST_CHECK_COMPLETE(node,
201                 "foo", "", EC_NODE_ENDLIST,
202                 "bar", EC_NODE_ENDLIST);
203         testres |= EC_TEST_CHECK_COMPLETE(node,
204                 "bar", "", EC_NODE_ENDLIST,
205                 "foo", "bar", EC_NODE_ENDLIST);
206         ec_node_free(node);
207
208         return testres;
209 }
210 /* LCOV_EXCL_STOP */
211
212 static struct ec_test ec_node_once_test = {
213         .name = "node_once",
214         .test = ec_node_once_testcase,
215 };
216
217 EC_TEST_REGISTER(ec_node_once_test);