-/*
- * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
*/
#include <stdio.h>
TAILQ_HEAD(ec_parsed_list, ec_parsed);
-/* XXX idea for parse: maintain a "cursor" ?
-struct ec_parsed {
- struct ec_parsed_tree *root;
- stuct ec_parsed_tree *cursor;
-};
-*/
-
struct ec_parsed {
TAILQ_ENTRY(ec_parsed) next;
struct ec_parsed_list children;
struct ec_keyval *attrs;
};
-static int __ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
+static int __ec_node_parse_child(const struct ec_node *node,
+ struct ec_parsed *state,
bool is_root, const struct ec_strvec *strvec)
{
struct ec_strvec *match_strvec;
- struct ec_parsed *child;
+ struct ec_parsed *child = NULL;
int ret;
- /* build the node if required */
- if (node->type->build != NULL) {
- if ((node->flags & EC_NODE_F_BUILT) == 0) {
- ret = node->type->build(node);
- if (ret < 0)
- return ret;
- }
- }
- node->flags |= EC_NODE_F_BUILT;
-
if (node->type->parse == NULL)
return -ENOTSUP;
if (child == NULL)
return -ENOMEM;
- ec_parsed_add_child(state, child);
+ ec_parsed_link_child(state, child);
} else {
child = state;
}
ret = node->type->parse(node, child, strvec);
- if (ret < 0) // XXX should we free state, child?
- return ret;
-
- if (ret == EC_PARSED_NOMATCH) {
- if (!is_root) {
- ec_parsed_del_child(state, child);
- assert(TAILQ_EMPTY(&child->children));
- ec_parsed_free(child);
- }
- return ret;
- }
+ if (ret < 0 || ret == EC_PARSED_NOMATCH)
+ goto free;
match_strvec = ec_strvec_ndup(strvec, 0, ret);
- if (match_strvec == NULL)
- return -ENOMEM;
+ if (match_strvec == NULL) {
+ ret = -ENOMEM;
+ goto free;
+ }
child->strvec = match_strvec;
return ret;
+
+free:
+ if (!is_root) {
+ ec_parsed_unlink_child(state, child);
+ ec_parsed_free(child);
+ }
+ return ret;
}
-int ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
+int ec_node_parse_child(const struct ec_node *node, struct ec_parsed *state,
const struct ec_strvec *strvec)
{
assert(state != NULL);
return __ec_node_parse_child(node, state, false, strvec);
}
-struct ec_parsed *ec_node_parse_strvec(struct ec_node *node,
+struct ec_parsed *ec_node_parse_strvec(const struct ec_node *node,
const struct ec_strvec *strvec)
{
struct ec_parsed *parsed = ec_parsed(node);
return parsed;
}
-struct ec_parsed *ec_node_parse(struct ec_node *node, const char *str)
+struct ec_parsed *ec_node_parse(const struct ec_node *node, const char *str)
{
struct ec_strvec *strvec = NULL;
struct ec_parsed *parsed = NULL;
dup_child = __ec_parsed_dup(child, ref, new_ref);
if (dup_child == NULL)
goto fail;
- ec_parsed_add_child(dup, dup_child);
+ ec_parsed_link_child(dup, dup_child);
}
return dup;
return NULL;
}
-struct ec_parsed *ec_parsed_dup(struct ec_parsed *parsed) //XXX const
+struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed)
{
- struct ec_parsed *root, *dup_root, *dup = NULL;
+ const struct ec_parsed *root;
+ struct ec_parsed *dup_root, *dup = NULL;
root = ec_parsed_get_root(parsed);
dup_root = __ec_parsed_dup(root, parsed, &dup);
while (!TAILQ_EMPTY(&parsed->children)) {
child = TAILQ_FIRST(&parsed->children);
TAILQ_REMOVE(&parsed->children, child, next);
+ child->parent = NULL;
ec_parsed_free(child);
}
}
if (parsed == NULL)
return;
- // assert(parsed->parent == NULL); XXX
- // or
- // parsed = ec_parsed_get_root(parsed);
+ ec_assert_print(parsed->parent == NULL,
+ "parent not NULL in ec_parsed_free()");
ec_parsed_free_children(parsed);
ec_strvec_free(parsed->strvec);
{
struct ec_parsed *child;
const struct ec_strvec *vec;
- size_t i;
- const char *id = "none", *typename = "none";
+ const char *id, *typename = "none";
+ /* node can be null when parsing is incomplete */
if (parsed->node != NULL) {
- if (parsed->node->id != NULL)
- id = parsed->node->id;
+ id = parsed->node->id;
typename = parsed->node->type->name;
}
- /* XXX enhance */
- for (i = 0; i < indent; i++) {
- if (i % 2)
- fprintf(out, " ");
- else
- fprintf(out, "|");
- }
-
- fprintf(out, "node_type=%s id=%s vec=", typename, id);
+ fprintf(out, "%*s" "type=%s id=%s vec=",
+ (int)indent * 4, "", typename, id);
vec = ec_parsed_strvec(parsed);
ec_strvec_dump(out, vec);
TAILQ_FOREACH(child, &parsed->children, next)
- __ec_parsed_dump(out, child, indent + 2);
+ __ec_parsed_dump(out, child, indent + 1);
}
void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
{
- fprintf(out, "------------------- parsed dump:\n"); //XXX
+ fprintf(out, "------------------- parsed dump:\n");
if (parsed == NULL) {
fprintf(out, "parsed is NULL, error in parse\n");
__ec_parsed_dump(out, parsed, 0);
}
-void ec_parsed_add_child(struct ec_parsed *parsed,
+void ec_parsed_link_child(struct ec_parsed *parsed,
struct ec_parsed *child)
{
TAILQ_INSERT_TAIL(&parsed->children, child, next);
child->parent = parsed;
}
-// XXX we can remove the first arg ? parsed == child->parent ?
-void ec_parsed_del_child(struct ec_parsed *parsed, // XXX rename del in unlink?
+void ec_parsed_unlink_child(struct ec_parsed *parsed,
struct ec_parsed *child)
{
TAILQ_REMOVE(&parsed->children, child, next);
return parsed->node;
}
-void ec_parsed_del_last_child(struct ec_parsed *parsed) // rename in free
+void ec_parsed_del_last_child(struct ec_parsed *parsed)
{
struct ec_parsed *child;
child = ec_parsed_get_last_child(parsed);
- ec_parsed_del_child(parsed, child);
+ ec_parsed_unlink_child(parsed, child);
ec_parsed_free(child);
}
-struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
+struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed)
{
if (parsed == NULL)
return NULL;
return parsed;
}
-struct ec_parsed *ec_parsed_get_parent(struct ec_parsed *parsed)
+struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed)
{
if (parsed == NULL)
return NULL;