get max parse len
[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_strvec.h>
37 #include <ecoli_keyval.h>
38 #include <ecoli_log.h>
39 #include <ecoli_node.h>
40
41 static struct ec_node_type_list node_type_list =
42         TAILQ_HEAD_INITIALIZER(node_type_list);
43
44 struct ec_node_type *ec_node_type_lookup(const char *name)
45 {
46         struct ec_node_type *type;
47
48         TAILQ_FOREACH(type, &node_type_list, next) {
49                 if (!strcmp(name, type->name))
50                         return type;
51         }
52
53         return NULL;
54 }
55
56 int ec_node_type_register(struct ec_node_type *type)
57 {
58         if (ec_node_type_lookup(type->name) != NULL)
59                 return -EEXIST;
60         if (type->size < sizeof(struct ec_node))
61                 return -EINVAL;
62
63         TAILQ_INSERT_TAIL(&node_type_list, type, next);
64
65         return 0;
66 }
67
68 void ec_node_type_dump(FILE *out)
69 {
70         struct ec_node_type *type;
71
72         TAILQ_FOREACH(type, &node_type_list, next)
73                 fprintf(out, "%s\n", type->name);
74 }
75
76 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
77 {
78         struct ec_node *node = NULL;
79         char buf[256]; // XXX
80
81         ec_log(EC_LOG_DEBUG, "create node type=%s id=%s\n",
82                 type->name, id);
83
84         node = ec_calloc(1, type->size);
85         if (node == NULL)
86                 goto fail;
87
88         TAILQ_INIT(&node->children);
89         node->type = type;
90         node->refcnt = 1;
91
92         if (id != NULL) {
93                 node->id = ec_strdup(id);
94                 if (node->id == NULL)
95                         goto fail;
96         }
97
98         snprintf(buf, sizeof(buf), "<%s>", type->name);
99         node->desc = ec_strdup(buf); // XXX ec_asprintf ?
100         if (node->desc == NULL)
101                 goto fail;
102
103         node->attrs = ec_keyval();
104         if (node->attrs == NULL)
105                 goto fail;
106
107         return node;
108
109  fail:
110         ec_node_free(node);
111         return NULL;
112 }
113
114 struct ec_node *ec_node(const char *typename, const char *id)
115 {
116         struct ec_node_type *type;
117
118         type = ec_node_type_lookup(typename);
119         if (type == NULL) {
120                 ec_log(EC_LOG_ERR, "type=%s does not exist\n", typename);
121                 return NULL;
122         }
123
124         return __ec_node(type, id);
125 }
126
127 void ec_node_free(struct ec_node *node)
128 {
129         if (node == NULL)
130                 return;
131
132         assert(node->refcnt > 0);
133
134         if (--node->refcnt > 0)
135                 return;
136
137         if (node->type != NULL && node->type->free_priv != NULL)
138                 node->type->free_priv(node);
139         ec_free(node->id);
140         ec_free(node->desc);
141         ec_free(node->attrs);
142         ec_free(node);
143 }
144
145 size_t ec_node_get_max_parse_len(const struct ec_node *node)
146 {
147         if (node->type->get_max_parse_len == NULL)
148                 return SIZE_MAX;
149         return node->type->get_max_parse_len(node);
150 }
151
152 struct ec_node *ec_node_clone(struct ec_node *node)
153 {
154         if (node != NULL)
155                 node->refcnt++;
156         return node;
157 }
158
159 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
160 {
161         struct ec_node *child, *ret;
162         const char *node_id = ec_node_id(node);
163
164         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
165                 return node;
166
167         TAILQ_FOREACH(child, &node->children, next) {
168                 ret = ec_node_find(child, id);
169                 if (ret != NULL)
170                         return ret;
171         }
172
173         return NULL;
174 }
175
176 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
177 {
178         return node->attrs;
179 }
180
181 const char *ec_node_id(const struct ec_node *node)
182 {
183         if (node->id == NULL)
184                 return "None";
185         return node->id;
186 }
187
188 struct ec_node *ec_node_parent(const struct ec_node *node)
189 {
190         return node->parent;
191 }
192
193 static void __ec_node_dump(FILE *out,
194         const struct ec_node *node, size_t indent)
195 {
196         const char *id, *typename, *desc;
197         struct ec_node *child;
198         size_t maxlen;
199         size_t i;
200
201         maxlen = ec_node_get_max_parse_len(node);
202         id = ec_node_id(node);
203         typename = node->type->name;
204         desc = ec_node_desc(node);
205
206         /* XXX enhance */
207         for (i = 0; i < indent; i++) {
208                 if (i % 2)
209                         fprintf(out, " ");
210                 else
211                         fprintf(out, "|");
212         }
213
214         fprintf(out, "node %p type=%s id=%s desc=%s ",
215                 node, typename, id, desc);
216         if (maxlen == SIZE_MAX)
217                 fprintf(out, "maxlen=no\n");
218         else
219                 fprintf(out, "maxlen=%zu\n", maxlen);
220         TAILQ_FOREACH(child, &node->children, next)
221                 __ec_node_dump(out, child, indent + 2);
222 }
223
224 void ec_node_dump(FILE *out, const struct ec_node *node)
225 {
226         fprintf(out, "------------------- node dump:\n"); //XXX
227
228         if (node == NULL) {
229                 fprintf(out, "node is NULL\n");
230                 return;
231         }
232
233         __ec_node_dump(out, node, 0);
234 }
235
236 const char *ec_node_desc(const struct ec_node *node)
237 {
238         if (node->type->desc != NULL)
239                 return node->type->desc(node);
240
241         return node->desc;
242 }