get config
[protos/libecoli.git] / lib / ecoli_node.h
index d4f65f3..63178f7 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>
  */
 
 /**
 #define EC_NODE_ENDLIST ((void *)1)
 
 struct ec_node;
-struct ec_parsed;
-struct ec_completed;
+struct ec_parse;
+struct ec_comp;
 struct ec_strvec;
 struct ec_keyval;
+struct ec_config;
+struct ec_config_schema;
 
-#define EC_NODE_TYPE_REGISTER(t)                                               \
+#define EC_NODE_TYPE_REGISTER(t)                                       \
        static void ec_node_init_##t(void);                             \
        static void __attribute__((constructor, used))                  \
        ec_node_init_##t(void)                                          \
        {                                                               \
                if (ec_node_type_register(&t) < 0)                      \
-                       fprintf(stderr, "cannot register %s\n", t.name); \
+                       fprintf(stderr,                                 \
+                               "cannot register node type %s\n",       \
+                               t.name);                                \
        }
 
 TAILQ_HEAD(ec_node_type_list, ec_node_type);
 
-/* return 0 on success, else -errno. */
-typedef int (*ec_node_build_t)(struct ec_node *node);
-
+typedef int (*ec_node_set_config_t)(struct ec_node *node,
+                               const struct ec_config *config);
 typedef int (*ec_node_parse_t)(const struct ec_node *node,
-                       struct ec_parsed *state,
+                       struct ec_parse *state,
                        const struct ec_strvec *strvec);
 typedef int (*ec_node_complete_t)(const struct ec_node *node,
-                               struct ec_completed *completed_state,
+                               struct ec_comp *comp_state,
                                const struct ec_strvec *strvec);
-typedef size_t (*ec_node_get_max_parse_len_t)(const struct ec_node *node);
 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
 typedef int (*ec_node_init_priv_t)(struct ec_node *);
 typedef void (*ec_node_free_priv_t)(struct ec_node *);
@@ -110,10 +89,12 @@ typedef void (*ec_node_free_priv_t)(struct ec_node *);
 struct ec_node_type {
        TAILQ_ENTRY(ec_node_type) next;  /**< Next in list. */
        const char *name;                /**< Node type name. */
-       ec_node_build_t build;           /**< (Re)build the node */
+       /** Generic configuration schema. */
+       const struct ec_config_schema *schema;
+       size_t schema_len;               /**< Number of elts in schema array. */
+       ec_node_set_config_t set_config; /* validate/ack a config change */
        ec_node_parse_t parse;
        ec_node_complete_t complete;
-       ec_node_get_max_parse_len_t get_max_parse_len;
        ec_node_desc_t desc;
        size_t size;
        ec_node_init_priv_t init_priv;
@@ -139,50 +120,52 @@ int ec_node_type_register(struct ec_node_type *type);
  * @return
  *   The node type if found, or NULL on error.
  */
-struct ec_node_type *ec_node_type_lookup(const char *name);
+const struct ec_node_type *ec_node_type_lookup(const char *name);
 
 /**
  * Dump registered log types
  */
 void ec_node_type_dump(FILE *out);
 
-TAILQ_HEAD(ec_node_list, ec_node);
-
 struct ec_node {
        const struct ec_node_type *type;
+       struct ec_config *config;    /**< Generic configuration. */
        char *id;
        char *desc;
        struct ec_keyval *attrs;
-       /* XXX ensure parent and child are properly set in all nodes */
-       struct ec_node *parent;
        unsigned int refcnt;
-#define EC_NODE_F_BUILT 0x0001 /** set if configuration is built */
-       unsigned int flags;
-
-       TAILQ_ENTRY(ec_node) next;
-       struct ec_node_list children;
+       struct ec_node **children;   /* array of children */
+       size_t n_children;           /* number of children in the array */
 };
 
 /* create a new node when the type is known, typically called from the node
  * code */
 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
 
-/* create a_new node node */
+/* create a new node */
 struct ec_node *ec_node(const char *typename, const char *id);
 
 struct ec_node *ec_node_clone(struct ec_node *node);
 void ec_node_free(struct ec_node *node);
 
-/**
- * Get the max len of strvec that can be parsed by this node
- *
- * If there is no maximum, return SIZE_MAX.
- */
-size_t ec_node_get_max_parse_len(const struct ec_node *node);
+/* set configuration of a node
+ * after a call to this function, the config is
+ * owned by the node and must not be used by the caller
+ * on error, the config is freed. */
+int ec_node_set_config(struct ec_node *node, struct ec_config *config);
+
+/* get the current node configuration. Return NULL if no configuration. */
+const struct ec_config *ec_node_get_config(struct ec_node *node);
+
+size_t ec_node_get_children_count(const struct ec_node *node);
+struct ec_node *
+ec_node_get_child(const struct ec_node *node, size_t i);
+int ec_node_add_child(struct ec_node *node, struct ec_node *child);
+int ec_node_del_child(struct ec_node *node, struct ec_node *child);
 
 /* XXX add more accessors */
+const struct ec_node_type *ec_node_type(const struct ec_node *node);
 struct ec_keyval *ec_node_attrs(const struct ec_node *node);
-struct ec_node *ec_node_parent(const struct ec_node *node);
 const char *ec_node_id(const struct ec_node *node);
 const char *ec_node_desc(const struct ec_node *node);