save
[protos/libecoli.git] / lib / ecoli_node.h
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #ifndef ECOLI_NODE_
29 #define ECOLI_NODE_
30
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <stdio.h>
34
35 #define EC_NODE_ENDLIST ((void *)1)
36
37 struct ec_node;
38 struct ec_parsed;
39 struct ec_strvec;
40 struct ec_keyval;
41
42 /* return 0 on success, else -errno. */
43 typedef int (*ec_node_build_t)(struct ec_node *node);
44
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 *);
52
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)                                          \
57         {                                                               \
58                 if (ec_node_type_register(&t) < 0)                      \
59                         fprintf(stderr, "cannot register %s\n", t.name); \
60         }
61
62 TAILQ_HEAD(ec_node_type_list, ec_node_type);
63
64 /**
65  * A structure describing a node type.
66  */
67 struct ec_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;
73         ec_node_desc_t desc;
74         size_t size;
75         ec_node_init_priv_t init_priv;
76         ec_node_free_priv_t free_priv;
77 };
78
79 /**
80  * Register a node type.
81  *
82  * @param type
83  *   A pointer to a ec_test structure describing the test
84  *   to be registered.
85  * @return
86  *   0 on success, negative value on error.
87  */
88 int ec_node_type_register(struct ec_node_type *type);
89
90 /**
91  * Lookup node type by name
92  *
93  * @param name
94  *   The name of the node type to search.
95  * @return
96  *   The node type if found, or NULL on error.
97  */
98 struct ec_node_type *ec_node_type_lookup(const char *name);
99
100 /**
101  * Dump registered log types
102  */
103 void ec_node_type_dump(FILE *out);
104
105 TAILQ_HEAD(ec_node_list, ec_node);
106
107 struct ec_node {
108         const struct ec_node_type *type;
109         char *id;
110         char *desc;
111         struct ec_keyval *attrs;
112         /* XXX ensure parent and child are properly set in all nodes */
113         struct ec_node *parent;
114         unsigned int refcnt;
115 #define EC_NODE_F_BUILT 0x0001 /** set if configuration is built */
116         unsigned int flags;
117
118         TAILQ_ENTRY(ec_node) next;
119         struct ec_node_list children;
120 };
121
122 /* create a new node when the type is known, typically called from the node
123  * code */
124 struct ec_node *__ec_node_new(const struct ec_node_type *type, const char *id);
125
126 /* create a_new node node */
127 struct ec_node *ec_node_new(const char *typename, const char *id);
128
129 void ec_node_free(struct ec_node *node);
130
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);
136
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);
139
140 /* XXX split this file ? */
141
142 TAILQ_HEAD(ec_parsed_list, ec_parsed);
143
144 /*
145   node == NULL + empty children list means "no match"
146 */
147 struct ec_parsed {
148         TAILQ_ENTRY(ec_parsed) next;
149         struct ec_parsed_list children;
150         const struct ec_node *node;
151         struct ec_strvec *strvec;
152 };
153
154 struct ec_parsed *ec_parsed_new(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);
158
159 const struct ec_strvec *ec_parsed_strvec(
160         const struct ec_parsed *parsed);
161
162 void ec_parsed_set_match(struct ec_parsed *parsed,
163         const struct ec_node *node, struct ec_strvec *strvec);
164
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()
167  * or complete() ? */
168 /* a NULL return value is an error, with errno set
169   ENOTSUP: no ->parse() operation
170 */
171 struct ec_parsed *ec_node_parse(struct ec_node *node, const char *str);
172
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);
178
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);
184
185 struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
186         const char *id);
187
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);
191
192 struct ec_completed_elt {
193         TAILQ_ENTRY(ec_completed_elt) next;
194         const struct ec_node *node;
195         char *add;
196 };
197
198 TAILQ_HEAD(ec_completed_elt_list, ec_completed_elt);
199
200
201 struct ec_completed {
202         struct ec_completed_elt_list elts;
203         unsigned count;
204         unsigned count_match;
205         char *smallest_start;
206 };
207
208 /*
209  * return a completed object filled with elts
210  * return NULL on error (nomem?)
211  */
212 struct ec_completed *ec_node_complete(struct ec_node *node,
213         const char *str);
214 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
215         const struct ec_strvec *strvec);
216 struct ec_completed *ec_completed_new(void);
217 struct ec_completed_elt *ec_completed_elt_new(const struct ec_node *node,
218         const char *add);
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);
229
230 /* cannot return NULL */
231 const char *ec_completed_smallest_start(
232         const struct ec_completed *completed);
233
234 enum ec_completed_filter_flags {
235         EC_MATCH = 1,
236         EC_NO_MATCH = 2,
237 };
238
239 unsigned int ec_completed_count(
240         const struct ec_completed *completed,
241         enum ec_completed_filter_flags flags);
242
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;
247 };
248
249 struct ec_completed_iter *
250 ec_completed_iter_new(struct ec_completed *completed,
251         enum ec_completed_filter_flags flags);
252
253 const struct ec_completed_elt *ec_completed_iter_next(
254         struct ec_completed_iter *iter);
255
256 void ec_completed_iter_free(struct ec_completed_iter *iter);
257
258
259 #endif