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)
42 /* return 0 on success, else -errno. */
43 typedef int (*ec_node_build_t)(struct ec_node *node);
45 typedef struct ec_parsed *(*ec_node_parse_t)(const struct ec_node *node,
46 const struct ec_strvec *strvec);
47 typedef struct ec_completed *(*ec_node_complete_t)(const struct ec_node *node,
48 const struct ec_strvec *strvec);
49 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
50 typedef void (*ec_node_init_priv_t)(struct ec_node *);
51 typedef void (*ec_node_free_priv_t)(struct ec_node *);
53 #define EC_NODE_TYPE_REGISTER(t) \
54 static void ec_node_init_##t(void); \
55 static void __attribute__((constructor, used)) \
56 ec_node_init_##t(void) \
58 if (ec_node_type_register(&t) < 0) \
59 fprintf(stderr, "cannot register %s\n", t.name); \
62 TAILQ_HEAD(ec_node_type_list, ec_node_type);
65 * A structure describing a node type.
68 TAILQ_ENTRY(ec_node_type) next; /**< Next in list. */
69 const char *name; /**< Node type name. */
70 ec_node_build_t build; /* (re)build the node, called by generic parse */
71 ec_node_parse_t parse;
72 ec_node_complete_t complete;
75 ec_node_init_priv_t init_priv;
76 ec_node_free_priv_t free_priv;
80 * Register a node type.
83 * A pointer to a ec_test structure describing the test
86 * 0 on success, negative value on error.
88 int ec_node_type_register(struct ec_node_type *type);
91 * Lookup node type by name
94 * The name of the node type to search.
96 * The node type if found, or NULL on error.
98 struct ec_node_type *ec_node_type_lookup(const char *name);
101 * Dump registered log types
103 void ec_node_type_dump(FILE *out);
105 TAILQ_HEAD(ec_node_list, ec_node);
108 const struct ec_node_type *type;
111 struct ec_keyval *attrs;
112 /* XXX ensure parent and child are properly set in all nodes */
113 struct ec_node *parent;
115 #define EC_NODE_F_BUILT 0x0001 /** set if configuration is built */
118 TAILQ_ENTRY(ec_node) next;
119 struct ec_node_list children;
122 /* create a new node when the type is known, typically called from the node
124 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
126 /* create a_new node node */
127 struct ec_node *ec_node(const char *typename, const char *id);
129 void ec_node_free(struct ec_node *node);
131 /* XXX add more accessors */
132 struct ec_keyval *ec_node_attrs(const struct ec_node *node);
133 struct ec_node *ec_node_parent(const struct ec_node *node);
134 const char *ec_node_id(const struct ec_node *node);
135 const char *ec_node_desc(const struct ec_node *node);
137 void ec_node_dump(FILE *out, const struct ec_node *node);
138 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
140 /* XXX split this file ? */
142 TAILQ_HEAD(ec_parsed_list, ec_parsed);
145 node == NULL + empty children list means "no match"
148 TAILQ_ENTRY(ec_parsed) next;
149 struct ec_parsed_list children;
150 const struct ec_node *node;
151 struct ec_strvec *strvec;
154 struct ec_parsed *ec_parsed(void);
155 void ec_parsed_free(struct ec_parsed *parsed);
156 struct ec_node *ec_node_clone(struct ec_node *node);
157 void ec_parsed_free_children(struct ec_parsed *parsed);
159 const struct ec_strvec *ec_parsed_strvec(
160 const struct ec_parsed *parsed);
162 void ec_parsed_set_match(struct ec_parsed *parsed,
163 const struct ec_node *node, struct ec_strvec *strvec);
165 /* XXX we could use a cache to store possible completions or match: the
166 * cache would be per-node, and would be reset for each call to parse()
168 /* a NULL return value is an error, with errno set
169 ENOTSUP: no ->parse() operation
171 struct ec_parsed *ec_node_parse(struct ec_node *node, const char *str);
173 /* mostly internal to nodes */
174 /* XXX it should not reset cache
175 * ... not sure... it is used by tests */
176 struct ec_parsed *ec_node_parse_strvec(struct ec_node *node,
177 const struct ec_strvec *strvec);
179 void ec_parsed_add_child(struct ec_parsed *parsed,
180 struct ec_parsed *child);
181 void ec_parsed_del_child(struct ec_parsed *parsed,
182 struct ec_parsed *child);
183 void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed);
185 struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
188 const char *ec_parsed_to_string(const struct ec_parsed *parsed);
189 size_t ec_parsed_len(const struct ec_parsed *parsed);
190 size_t ec_parsed_matches(const struct ec_parsed *parsed);
192 struct ec_completed_elt {
193 TAILQ_ENTRY(ec_completed_elt) next;
194 const struct ec_node *node;
198 TAILQ_HEAD(ec_completed_elt_list, ec_completed_elt);
201 struct ec_completed {
202 struct ec_completed_elt_list elts;
204 unsigned count_match;
205 char *smallest_start;
209 * return a completed object filled with elts
210 * return NULL on error (nomem?)
212 struct ec_completed *ec_node_complete(struct ec_node *node,
214 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
215 const struct ec_strvec *strvec);
216 struct ec_completed *ec_completed(void);
217 struct ec_completed_elt *ec_completed_elt(const struct ec_node *node,
219 void ec_completed_add_elt(struct ec_completed *completed,
220 struct ec_completed_elt *elt);
221 void ec_completed_elt_free(struct ec_completed_elt *elt);
222 void ec_completed_merge(struct ec_completed *completed1,
223 struct ec_completed *completed2);
224 void ec_completed_free(struct ec_completed *completed);
225 void ec_completed_dump(FILE *out,
226 const struct ec_completed *completed);
227 struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
228 const struct ec_strvec *strvec);
230 /* cannot return NULL */
231 const char *ec_completed_smallest_start(
232 const struct ec_completed *completed);
234 enum ec_completed_filter_flags {
239 unsigned int ec_completed_count(
240 const struct ec_completed *completed,
241 enum ec_completed_filter_flags flags);
243 struct ec_completed_iter {
244 enum ec_completed_filter_flags flags;
245 const struct ec_completed *completed;
246 const struct ec_completed_elt *cur;
249 struct ec_completed_iter *
250 ec_completed_iter(struct ec_completed *completed,
251 enum ec_completed_filter_flags flags);
253 const struct ec_completed_elt *ec_completed_iter_next(
254 struct ec_completed_iter *iter);
256 void ec_completed_iter_free(struct ec_completed_iter *iter);