save
[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         TAILQ_INIT(&node->children);
95         node->type = type;
96         node->refcnt = 1;
97
98         node->id = ec_strdup(id);
99         if (node->id == NULL)
100                 goto fail;
101
102         if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
103                 goto fail;
104
105         node->attrs = ec_keyval();
106         if (node->attrs == NULL)
107                 goto fail;
108
109         if (type->init_priv != NULL) {
110                 if (type->init_priv(node) < 0)
111                         goto fail;
112         }
113
114         return node;
115
116  fail:
117         if (node != NULL) {
118                 ec_keyval_free(node->attrs);
119                 ec_free(node->desc);
120                 ec_free(node->id);
121         }
122         ec_free(node);
123
124         return NULL;
125 }
126
127 struct ec_node *ec_node(const char *typename, const char *id)
128 {
129         struct ec_node_type *type;
130
131         type = ec_node_type_lookup(typename);
132         if (type == NULL) {
133                 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
134                         typename);
135                 return NULL;
136         }
137
138         return __ec_node(type, id);
139 }
140
141 void ec_node_free(struct ec_node *node)
142 {
143         if (node == NULL)
144                 return;
145
146         assert(node->refcnt > 0);
147
148         if (--node->refcnt > 0)
149                 return;
150
151         if (node->type != NULL && node->type->free_priv != NULL)
152                 node->type->free_priv(node);
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 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
174 {
175         struct ec_node *child, *ret;
176         const char *node_id = ec_node_id(node);
177
178         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
179                 return node;
180
181         TAILQ_FOREACH(child, &node->children, next) {
182                 ret = ec_node_find(child, id);
183                 if (ret != NULL)
184                         return ret;
185         }
186
187         return NULL;
188 }
189
190 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
191 {
192         return node->attrs;
193 }
194
195 const char *ec_node_id(const struct ec_node *node)
196 {
197         if (node->id == NULL)
198                 return "None";
199         return node->id;
200 }
201
202 static void __ec_node_dump(FILE *out,
203         const struct ec_node *node, size_t indent)
204 {
205         const char *id, *typename, *desc;
206         struct ec_node *child;
207         size_t maxlen;
208         size_t i;
209
210         maxlen = ec_node_get_max_parse_len(node);
211         id = ec_node_id(node);
212         typename = node->type->name;
213         desc = ec_node_desc(node);
214
215         /* XXX enhance */
216         for (i = 0; i < indent; i++) {
217                 if (i % 2)
218                         fprintf(out, " ");
219                 else
220                         fprintf(out, "|");
221         }
222
223         fprintf(out, "node %p type=%s id=%s desc=%s ",
224                 node, typename, id, desc);
225         if (maxlen == SIZE_MAX)
226                 fprintf(out, "maxlen=no\n");
227         else
228                 fprintf(out, "maxlen=%zu\n", maxlen);
229         TAILQ_FOREACH(child, &node->children, next)
230                 __ec_node_dump(out, child, indent + 2);
231 }
232
233 void ec_node_dump(FILE *out, const struct ec_node *node)
234 {
235         fprintf(out, "------------------- node dump:\n"); //XXX
236
237         if (node == NULL) {
238                 fprintf(out, "node is NULL\n");
239                 return;
240         }
241
242         __ec_node_dump(out, node, 0);
243 }
244
245 const char *ec_node_desc(const struct ec_node *node)
246 {
247         if (node->type->desc != NULL)
248                 return node->type->desc(node);
249
250         return node->desc;
251 }
252
253 int ec_node_check_type(const struct ec_node *node,
254                 const struct ec_node_type *type)
255 {
256         if (strcmp(node->type->name, type->name)) {
257                 errno = EINVAL;
258                 return -1;
259         }
260         return 0;
261 }