2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
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>
41 EC_LOG_TYPE_REGISTER(node);
43 static struct ec_node_type_list node_type_list =
44 TAILQ_HEAD_INITIALIZER(node_type_list);
46 struct ec_node_type *ec_node_type_lookup(const char *name)
48 struct ec_node_type *type;
50 TAILQ_FOREACH(type, &node_type_list, next) {
51 if (!strcmp(name, type->name))
58 int ec_node_type_register(struct ec_node_type *type)
60 if (ec_node_type_lookup(type->name) != NULL)
62 if (type->size < sizeof(struct ec_node))
65 TAILQ_INSERT_TAIL(&node_type_list, type, next);
70 void ec_node_type_dump(FILE *out)
72 struct ec_node_type *type;
74 TAILQ_FOREACH(type, &node_type_list, next)
75 fprintf(out, "%s\n", type->name);
78 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
80 struct ec_node *node = NULL;
83 EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
90 node = ec_calloc(1, type->size);
94 TAILQ_INIT(&node->children);
99 node->id = ec_strdup(id);
100 if (node->id == NULL)
104 snprintf(buf, sizeof(buf), "<%s>", type->name);
105 node->desc = ec_strdup(buf); // XXX ec_asprintf ?
106 if (node->desc == NULL)
109 node->attrs = ec_keyval();
110 if (node->attrs == NULL)
120 struct ec_node *ec_node(const char *typename, const char *id)
122 struct ec_node_type *type;
124 type = ec_node_type_lookup(typename);
126 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
131 return __ec_node(type, id);
134 void ec_node_free(struct ec_node *node)
139 assert(node->refcnt > 0);
141 if (--node->refcnt > 0)
144 if (node->type != NULL && node->type->free_priv != NULL)
145 node->type->free_priv(node);
148 ec_free(node->attrs);
152 size_t ec_node_get_max_parse_len(const struct ec_node *node)
154 if (node->type->get_max_parse_len == NULL)
156 return node->type->get_max_parse_len(node);
159 struct ec_node *ec_node_clone(struct ec_node *node)
166 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
168 struct ec_node *child, *ret;
169 const char *node_id = ec_node_id(node);
171 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
174 TAILQ_FOREACH(child, &node->children, next) {
175 ret = ec_node_find(child, id);
183 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
188 const char *ec_node_id(const struct ec_node *node)
190 if (node->id == NULL)
195 struct ec_node *ec_node_parent(const struct ec_node *node)
200 static void __ec_node_dump(FILE *out,
201 const struct ec_node *node, size_t indent)
203 const char *id, *typename, *desc;
204 struct ec_node *child;
208 maxlen = ec_node_get_max_parse_len(node);
209 id = ec_node_id(node);
210 typename = node->type->name;
211 desc = ec_node_desc(node);
214 for (i = 0; i < indent; i++) {
221 fprintf(out, "node %p type=%s id=%s desc=%s ",
222 node, typename, id, desc);
223 if (maxlen == SIZE_MAX)
224 fprintf(out, "maxlen=no\n");
226 fprintf(out, "maxlen=%zu\n", maxlen);
227 TAILQ_FOREACH(child, &node->children, next)
228 __ec_node_dump(out, child, indent + 2);
231 void ec_node_dump(FILE *out, const struct ec_node *node)
233 fprintf(out, "------------------- node dump:\n"); //XXX
236 fprintf(out, "node is NULL\n");
240 __ec_node_dump(out, node, 0);
243 const char *ec_node_desc(const struct ec_node *node)
245 if (node->type->desc != NULL)
246 return node->type->desc(node);
251 int ec_node_check_type(const struct ec_node *node,
252 const struct ec_node_type *type)
254 if (strcmp(node->type->name, type->name))