sort init list
[protos/libecoli.git] / lib / ecoli_parsed.c
index f7ca38f..035ec13 100644 (file)
@@ -1,28 +1,5 @@
-/*
- * 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;
@@ -58,28 +28,19 @@ struct ec_parsed {
        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 (!is_root) {
-               child = ec_parsed();
+               child = ec_parsed(node);
                if (child == NULL)
                        return -ENOMEM;
 
@@ -87,40 +48,39 @@ static int __ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
        } else {
                child = state;
        }
-       ec_parsed_set_node(child, node);
        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_del_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();
+       struct ec_parsed *parsed = ec_parsed(node);
        int ret;
 
        if (parsed == NULL)
@@ -135,7 +95,7 @@ struct ec_parsed *ec_node_parse_strvec(struct ec_node *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;
@@ -161,7 +121,7 @@ struct ec_parsed *ec_node_parse(struct ec_node *node, const char *str)
        return NULL;
 }
 
-struct ec_parsed *ec_parsed(void)
+struct ec_parsed *ec_parsed(const struct ec_node *node)
 {
        struct ec_parsed *parsed = NULL;
 
@@ -171,6 +131,7 @@ struct ec_parsed *ec_parsed(void)
 
        TAILQ_INIT(&parsed->children);
 
+       parsed->node = node;
        parsed->attrs = ec_keyval();
        if (parsed->attrs == NULL)
                goto fail;
@@ -196,7 +157,7 @@ __ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref,
        if (root == NULL)
                return NULL;
 
-       dup = ec_parsed();
+       dup = ec_parsed(root->node);
        if (dup == NULL)
                return NULL;
 
@@ -208,7 +169,6 @@ __ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref,
                goto fail;
        ec_keyval_free(dup->attrs);
        dup->attrs = attrs;
-       dup->node = root->node;
 
        if (root->strvec != NULL) {
                dup->strvec = ec_strvec_dup(root->strvec);
@@ -253,6 +213,7 @@ void ec_parsed_free_children(struct ec_parsed *parsed)
        while (!TAILQ_EMPTY(&parsed->children)) {
                child = TAILQ_FIRST(&parsed->children);
                TAILQ_REMOVE(&parsed->children, child, next);
+               child->parent = NULL;
                ec_parsed_free(child);
        }
 }
@@ -262,9 +223,8 @@ void ec_parsed_free(struct ec_parsed *parsed)
        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);
@@ -277,24 +237,16 @@ static void __ec_parsed_dump(FILE *out,
 {
        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);
 
@@ -304,7 +256,7 @@ static void __ec_parsed_dump(FILE *out,
 
 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");
@@ -365,11 +317,6 @@ const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed)
        return parsed->node;
 }
 
-void ec_parsed_set_node(struct ec_parsed *parsed, const struct ec_node *node)
-{
-       parsed->node = node;
-}
-
 void ec_parsed_del_last_child(struct ec_parsed *parsed) // rename in free
 {
        struct ec_parsed *child;
@@ -398,6 +345,24 @@ struct ec_parsed *ec_parsed_get_parent(struct ec_parsed *parsed)
        return parsed->parent;
 }
 
+struct ec_parsed *ec_parsed_iter_next(struct ec_parsed *parsed)
+{
+       struct ec_parsed *child, *parent, *next;
+
+       child = TAILQ_FIRST(&parsed->children);
+       if (child != NULL)
+               return child;
+       parent = parsed->parent;
+       while (parent != NULL) {
+               next = TAILQ_NEXT(parsed, next);
+               if (next != NULL)
+                       return next;
+               parsed = parent;
+               parent = parsed->parent;
+       }
+       return NULL;
+}
+
 struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
        const char *id)
 {
@@ -420,6 +385,15 @@ struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
        return NULL;
 }
 
+struct ec_keyval *
+ec_parsed_get_attrs(struct ec_parsed *parsed)
+{
+       if (parsed == NULL)
+               return NULL;
+
+       return parsed->attrs;
+}
+
 const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed)
 {
        if (parsed == NULL || parsed->strvec == NULL)