ff524401c1e447fde59291d2da0d3839de46433b
[protos/libecoli.git] / lib / ecoli_node.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <errno.h>
34
35 #include <ecoli_malloc.h>
36 #include <ecoli_string.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_keyval.h>
39 #include <ecoli_log.h>
40 #include <ecoli_node.h>
41
42 EC_LOG_TYPE_REGISTER(node);
43
44 static struct ec_node_type_list node_type_list =
45         TAILQ_HEAD_INITIALIZER(node_type_list);
46
47 struct ec_node_type *ec_node_type_lookup(const char *name)
48 {
49         struct ec_node_type *type;
50
51         TAILQ_FOREACH(type, &node_type_list, next) {
52                 if (!strcmp(name, type->name))
53                         return type;
54         }
55
56         return NULL;
57 }
58
59 int ec_node_type_register(struct ec_node_type *type)
60 {
61         if (ec_node_type_lookup(type->name) != NULL)
62                 return -EEXIST;
63         if (type->size < sizeof(struct ec_node))
64                 return -EINVAL;
65
66         TAILQ_INSERT_TAIL(&node_type_list, type, next);
67
68         return 0;
69 }
70
71 void ec_node_type_dump(FILE *out)
72 {
73         struct ec_node_type *type;
74
75         TAILQ_FOREACH(type, &node_type_list, next)
76                 fprintf(out, "%s\n", type->name);
77 }
78
79 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
80 {
81         struct ec_node *node = NULL;
82
83         EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
84                 type->name, id);
85         if (id == NULL) {
86                 errno = EINVAL;
87                 goto fail;
88         }
89
90         node = ec_calloc(1, type->size);
91         if (node == NULL)
92                 goto fail;
93
94         node->type = type;
95         node->refcnt = 1;
96
97         node->id = ec_strdup(id);
98         if (node->id == NULL)
99                 goto fail;
100
101         if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
102                 goto fail;
103
104         node->attrs = ec_keyval();
105         if (node->attrs == NULL)
106                 goto fail;
107
108         if (type->init_priv != NULL) {
109                 if (type->init_priv(node) < 0)
110                         goto fail;
111         }
112
113         return node;
114
115  fail:
116         if (node != NULL) {
117                 ec_keyval_free(node->attrs);
118                 ec_free(node->desc);
119                 ec_free(node->id);
120         }
121         ec_free(node);
122
123         return NULL;
124 }
125
126 struct ec_node *ec_node(const char *typename, const char *id)
127 {
128         struct ec_node_type *type;
129
130         type = ec_node_type_lookup(typename);
131         if (type == NULL) {
132                 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
133                         typename);
134                 return NULL;
135         }
136
137         return __ec_node(type, id);
138 }
139
140 void ec_node_free(struct ec_node *node)
141 {
142         if (node == NULL)
143                 return;
144
145         assert(node->refcnt > 0);
146
147         if (--node->refcnt > 0)
148                 return;
149
150         if (node->type != NULL && node->type->free_priv != NULL)
151                 node->type->free_priv(node);
152         ec_free(node->children);
153         ec_free(node->id);
154         ec_free(node->desc);
155         ec_free(node->attrs);
156         ec_free(node);
157 }
158
159 size_t ec_node_get_max_parse_len(const struct ec_node *node)
160 {
161         if (node->type->get_max_parse_len == NULL)
162                 return SIZE_MAX;
163         return node->type->get_max_parse_len(node);
164 }
165
166 struct ec_node *ec_node_clone(struct ec_node *node)
167 {
168         if (node != NULL)
169                 node->refcnt++;
170         return node;
171 }
172
173 size_t ec_node_get_children_count(const struct ec_node *node)
174 {
175         return node->n_children;
176 }
177
178 struct ec_node *
179 ec_node_get_child(const struct ec_node *node, size_t i)
180 {
181         if (i >= ec_node_get_children_count(node))
182                 return NULL;
183         return node->children[i];
184 }
185
186 int ec_node_add_child(struct ec_node *node, struct ec_node *child)
187 {
188         struct ec_node **children = NULL;
189         size_t n;
190
191         if (node == NULL || child == NULL) {
192                 errno = EINVAL;
193                 goto fail;
194         }
195
196         n = node->n_children;
197         children = ec_realloc(node->children,
198                         (n + 1) * sizeof(child));
199         if (children == NULL)
200                 goto fail;
201
202         children[n] = child;
203         node->children = children;
204         node->n_children = n + 1;
205
206         return 0;
207
208 fail:
209         ec_free(children);
210         return -1;
211 }
212
213 int ec_node_del_child(struct ec_node *node, struct ec_node *child)
214 {
215         size_t i, n;
216
217         if (node == NULL || child == NULL)
218                 goto fail;
219
220         n = node->n_children;
221         for (i = 0; i < n; i++) {
222                 if (node->children[i] != child)
223                         continue;
224                 memcpy(&node->children[i], &node->children[i+1],
225                         (n - i - 1) * sizeof(child));
226                 return 0;
227         }
228
229 fail:
230         errno = EINVAL;
231         return -1;
232 }
233
234 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
235 {
236         struct ec_node *child, *ret;
237         const char *node_id = ec_node_id(node);
238         size_t i, n;
239
240         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
241                 return node;
242
243         n = node->n_children;
244         for (i = 0; i < n; i++) {
245                 child = node->children[i];
246                 ret = ec_node_find(child, id);
247                 if (ret != NULL)
248                         return ret;
249         }
250
251         return NULL;
252 }
253
254 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
255 {
256         return node->attrs;
257 }
258
259 const char *ec_node_id(const struct ec_node *node)
260 {
261         if (node->id == NULL)
262                 return "None";
263         return node->id;
264 }
265
266 static void __ec_node_dump(FILE *out,
267         const struct ec_node *node, size_t indent)
268 {
269         const char *id, *typename, *desc;
270         struct ec_node *child;
271         size_t maxlen;
272         size_t i, n;
273
274         maxlen = ec_node_get_max_parse_len(node);
275         id = ec_node_id(node);
276         typename = node->type->name;
277         desc = ec_node_desc(node);
278
279         /* XXX enhance */
280         for (i = 0; i < indent; i++) {
281                 if (i % 2)
282                         fprintf(out, " ");
283                 else
284                         fprintf(out, "|");
285         }
286
287         fprintf(out, "node %p type=%s id=%s desc=%s ",
288                 node, typename, id, desc);
289         if (maxlen == SIZE_MAX)
290                 fprintf(out, "maxlen=no\n");
291         else
292                 fprintf(out, "maxlen=%zu\n", maxlen);
293         n = node->n_children;
294         for (i = 0; i < n; i++) {
295                 child = node->children[i];
296                 __ec_node_dump(out, child, indent + 2);
297         }
298 }
299
300 void ec_node_dump(FILE *out, const struct ec_node *node)
301 {
302         fprintf(out, "------------------- node dump:\n"); //XXX
303
304         if (node == NULL) {
305                 fprintf(out, "node is NULL\n");
306                 return;
307         }
308
309         __ec_node_dump(out, node, 0);
310 }
311
312 const char *ec_node_desc(const struct ec_node *node)
313 {
314         if (node->type->desc != NULL)
315                 return node->type->desc(node);
316
317         return node->desc;
318 }
319
320 int ec_node_check_type(const struct ec_node *node,
321                 const struct ec_node_type *type)
322 {
323         if (strcmp(node->type->name, type->name)) {
324                 errno = EINVAL;
325                 return -1;
326         }
327         return 0;
328 }