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.
31 #include <sys/queue.h>
32 #include <sys/types.h>
35 #define EC_NODE_ENDLIST ((void *)1)
43 /* return 0 on success, else -errno. */
44 typedef int (*ec_node_build_t)(struct ec_node *node);
46 typedef struct ec_parsed *(*ec_node_parse_t)(const struct ec_node *node,
47 const struct ec_strvec *strvec);
48 typedef struct ec_completed *(*ec_node_complete_t)(const struct ec_node *node,
49 const struct ec_strvec *strvec);
50 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
51 typedef void (*ec_node_init_priv_t)(struct ec_node *);
52 typedef void (*ec_node_free_priv_t)(struct ec_node *);
54 #define EC_NODE_TYPE_REGISTER(t) \
55 static void ec_node_init_##t(void); \
56 static void __attribute__((constructor, used)) \
57 ec_node_init_##t(void) \
59 if (ec_node_type_register(&t) < 0) \
60 fprintf(stderr, "cannot register %s\n", t.name); \
63 TAILQ_HEAD(ec_node_type_list, ec_node_type);
66 * A structure describing a node type.
69 TAILQ_ENTRY(ec_node_type) next; /**< Next in list. */
70 const char *name; /**< Node type name. */
71 ec_node_build_t build; /* (re)build the node, called by generic parse */
72 ec_node_parse_t parse;
73 ec_node_complete_t complete;
76 ec_node_init_priv_t init_priv;
77 ec_node_free_priv_t free_priv;
81 * Register a node type.
84 * A pointer to a ec_test structure describing the test
87 * 0 on success, negative value on error.
89 int ec_node_type_register(struct ec_node_type *type);
92 * Lookup node type by name
95 * The name of the node type to search.
97 * The node type if found, or NULL on error.
99 struct ec_node_type *ec_node_type_lookup(const char *name);
102 * Dump registered log types
104 void ec_node_type_dump(FILE *out);
106 TAILQ_HEAD(ec_node_list, ec_node);
109 const struct ec_node_type *type;
112 struct ec_keyval *attrs;
113 /* XXX ensure parent and child are properly set in all nodes */
114 struct ec_node *parent;
116 #define EC_NODE_F_BUILT 0x0001 /** set if configuration is built */
119 TAILQ_ENTRY(ec_node) next;
120 struct ec_node_list children;
123 /* create a new node when the type is known, typically called from the node
125 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
127 /* create a_new node node */
128 struct ec_node *ec_node(const char *typename, const char *id);
130 struct ec_node *ec_node_clone(struct ec_node *node);
131 void ec_node_free(struct ec_node *node);
133 /* XXX add more accessors */
134 struct ec_keyval *ec_node_attrs(const struct ec_node *node);
135 struct ec_node *ec_node_parent(const struct ec_node *node);
136 const char *ec_node_id(const struct ec_node *node);
137 const char *ec_node_desc(const struct ec_node *node);
139 void ec_node_dump(FILE *out, const struct ec_node *node);
140 struct ec_node *ec_node_find(struct ec_node *node, const char *id);