get max parse len
[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_completed;
40 struct ec_strvec;
41 struct ec_keyval;
42
43 #define EC_NODE_TYPE_REGISTER(t)                                                \
44         static void ec_node_init_##t(void);                             \
45         static void __attribute__((constructor, used))                  \
46         ec_node_init_##t(void)                                          \
47         {                                                               \
48                 if (ec_node_type_register(&t) < 0)                      \
49                         fprintf(stderr, "cannot register %s\n", t.name); \
50         }
51
52 TAILQ_HEAD(ec_node_type_list, ec_node_type);
53
54 /* return 0 on success, else -errno. */
55 typedef int (*ec_node_build_t)(struct ec_node *node);
56
57 typedef int (*ec_node_parse_t)(const struct ec_node *node,
58                         struct ec_parsed *state,
59                         const struct ec_strvec *strvec);
60 typedef struct ec_completed *(*ec_node_complete_t)(const struct ec_node *node,
61                                                 struct ec_parsed *state,
62                                                 const struct ec_strvec *strvec);
63 typedef size_t (*ec_node_get_max_parse_len_t)(const struct ec_node *node);
64 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
65 typedef void (*ec_node_init_priv_t)(struct ec_node *);
66 typedef void (*ec_node_free_priv_t)(struct ec_node *);
67
68 /**
69  * A structure describing a node type.
70  */
71 struct ec_node_type {
72         TAILQ_ENTRY(ec_node_type) next;  /**< Next in list. */
73         const char *name;              /**< Node type name. */
74         ec_node_build_t build; /* (re)build the node, called by generic parse */
75         ec_node_parse_t parse;
76         ec_node_complete_t complete;
77         ec_node_get_max_parse_len_t get_max_parse_len;
78         ec_node_desc_t desc;
79         size_t size;
80         ec_node_init_priv_t init_priv;
81         ec_node_free_priv_t free_priv;
82 };
83
84 /**
85  * Register a node type.
86  *
87  * @param type
88  *   A pointer to a ec_test structure describing the test
89  *   to be registered.
90  * @return
91  *   0 on success, negative value on error.
92  */
93 int ec_node_type_register(struct ec_node_type *type);
94
95 /**
96  * Lookup node type by name
97  *
98  * @param name
99  *   The name of the node type to search.
100  * @return
101  *   The node type if found, or NULL on error.
102  */
103 struct ec_node_type *ec_node_type_lookup(const char *name);
104
105 /**
106  * Dump registered log types
107  */
108 void ec_node_type_dump(FILE *out);
109
110 TAILQ_HEAD(ec_node_list, ec_node);
111
112 struct ec_node {
113         const struct ec_node_type *type;
114         char *id;
115         char *desc;
116         struct ec_keyval *attrs;
117         /* XXX ensure parent and child are properly set in all nodes */
118         struct ec_node *parent;
119         unsigned int refcnt;
120 #define EC_NODE_F_BUILT 0x0001 /** set if configuration is built */
121         unsigned int flags;
122
123         TAILQ_ENTRY(ec_node) next;
124         struct ec_node_list children;
125 };
126
127 /* create a new node when the type is known, typically called from the node
128  * code */
129 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
130
131 /* create a_new node node */
132 struct ec_node *ec_node(const char *typename, const char *id);
133
134 struct ec_node *ec_node_clone(struct ec_node *node);
135 void ec_node_free(struct ec_node *node);
136
137 /**
138  * Get the max len of strvec that can be parsed by this node
139  *
140  * If there is no maximum, return SIZE_MAX.
141  */
142 size_t ec_node_get_max_parse_len(const struct ec_node *node);
143
144 /* XXX add more accessors */
145 struct ec_keyval *ec_node_attrs(const struct ec_node *node);
146 struct ec_node *ec_node_parent(const struct ec_node *node);
147 const char *ec_node_id(const struct ec_node *node);
148 const char *ec_node_desc(const struct ec_node *node);
149
150 void ec_node_dump(FILE *out, const struct ec_node *node);
151 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
152
153 #endif