spdx tags for c and h files
[protos/libecoli.git] / lib / ecoli_node_once.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 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_parsed.h>
18 #include <ecoli_completed.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_parsed *parsed, const struct ec_node *node)
33 {
34         struct ec_parsed *child;
35         unsigned int count = 0;
36
37         if (parsed == NULL)
38                 return 0;
39
40         if (ec_parsed_get_node(parsed) == node)
41                 count++;
42
43         EC_PARSED_FOREACH_CHILD(child, parsed)
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_parsed *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_parsed_get_root(state), node->child);
61         if (count > 0)
62                 return EC_PARSED_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_completed *completed,
70                 const struct ec_strvec *strvec)
71 {
72         struct ec_node_once *node = (struct ec_node_once *)gen_node;
73         struct ec_parsed *parsed = ec_completed_get_state(completed);
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_parsed_get_root(parsed), node->child);
81         if (count > 0)
82                 return 0;
83
84         ret = ec_node_complete_child(node->child, completed, strvec);
85         if (ret < 0)
86                 return ret;
87
88         return 0;
89 }
90
91 static void ec_node_once_free_priv(struct ec_node *gen_node)
92 {
93         struct ec_node_once *node = (struct ec_node_once *)gen_node;
94
95         ec_node_free(node->child);
96 }
97
98 static struct ec_node_type ec_node_once_type = {
99         .name = "once",
100         .parse = ec_node_once_parse,
101         .complete = ec_node_once_complete,
102         .size = sizeof(struct ec_node_once),
103         .free_priv = ec_node_once_free_priv,
104 };
105
106 EC_NODE_TYPE_REGISTER(ec_node_once_type);
107
108 int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child)
109 {
110         struct ec_node_once *node = (struct ec_node_once *)gen_node;
111
112         if (gen_node == NULL || child == NULL) {
113                 errno = EINVAL;
114                 goto fail;
115         }
116
117         if (ec_node_check_type(gen_node, &ec_node_once_type) < 0)
118                 goto fail;
119
120         if (ec_node_add_child(gen_node, child) < 0)
121                 goto fail;
122
123         node->child = child;
124
125         return 0;
126
127 fail:
128         ec_node_free(child);
129         return -1;
130 }
131
132 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
133 {
134         struct ec_node *gen_node = NULL;
135
136         if (child == NULL)
137                 return NULL;
138
139         gen_node = __ec_node(&ec_node_once_type, id);
140         if (gen_node == NULL)
141                 goto fail;
142
143         ec_node_once_set(gen_node, child);
144         child = NULL;
145
146         return gen_node;
147
148 fail:
149         ec_node_free(child);
150         return NULL;
151 }
152
153 /* LCOV_EXCL_START */
154 static int ec_node_once_testcase(void)
155 {
156         struct ec_node *node;
157         int testres = 0;
158
159         node = ec_node_many(EC_NO_ID,
160                         EC_NODE_OR(EC_NO_ID,
161                                 ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
162                                 ec_node_str(EC_NO_ID, "bar")
163                                 ), 0, 0
164                 );
165         if (node == NULL) {
166                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
167                 return -1;
168         }
169         testres |= EC_TEST_CHECK_PARSE(node, 0);
170         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
171         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
172         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
173         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "bar");
174         testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "bar");
175         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo", "foo");
176         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
177         testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
178
179         testres |= EC_TEST_CHECK_COMPLETE(node,
180                 "", EC_NODE_ENDLIST,
181                 "foo", "bar", EC_NODE_ENDLIST);
182         testres |= EC_TEST_CHECK_COMPLETE(node,
183                 "f", EC_NODE_ENDLIST,
184                 "foo", EC_NODE_ENDLIST);
185         testres |= EC_TEST_CHECK_COMPLETE(node,
186                 "b", EC_NODE_ENDLIST,
187                 "bar", EC_NODE_ENDLIST);
188         testres |= EC_TEST_CHECK_COMPLETE(node,
189                 "foo", "", EC_NODE_ENDLIST,
190                 "bar", EC_NODE_ENDLIST);
191         testres |= EC_TEST_CHECK_COMPLETE(node,
192                 "bar", "", EC_NODE_ENDLIST,
193                 "foo", "bar", EC_NODE_ENDLIST);
194         ec_node_free(node);
195
196         return testres;
197 }
198 /* LCOV_EXCL_STOP */
199
200 static struct ec_test ec_node_once_test = {
201         .name = "node_once",
202         .test = ec_node_once_testcase,
203 };
204
205 EC_TEST_REGISTER(ec_node_once_test);