103577898f03a5c3fe3a9fb145c83f779f883cb6
[protos/libecoli.git] / lib / ecoli_node.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 /**
6  * Interface to manage the ecoli nodes.
7  *
8  * A node is a main structure of the ecoli library, used to define how
9  * to match and complete the input tokens. A node is a generic object
10  * that implements:
11  * - a parse(node, input) method: check if an input matches
12  * - a complete(node, input) method: return possible completions for
13  *   a given input
14  * - some other methods to initialize, free, ...
15  *
16  * One basic example is the string node (ec_node_str). A node
17  * ec_node_str("foo") will match any token list starting with "foo",
18  * for example:
19  * - ["foo"]
20  * - ["foo", "bar", ...]
21  * But will not match:
22  * - []
23  * - ["bar", ...]
24  *
25  * A node ec_node_str("foo") will complete with "foo" if the input
26  * contains one token, with the same beginning than "foo":
27  * - [""]
28  * - ["f"]
29  * - ["fo"]
30  * - ["foo"]
31  * But it will not complete:
32  * - []
33  * - ["bar"]
34  * - ["f", ""]
35  * - ["", "f"]
36  *
37  * A node can have child nodes. For instance, a sequence node
38  * ec_node_seq(ec_node_str("foo"), ec_node_str("bar")) will match
39  * ["foo", "bar"].
40  */
41
42 #ifndef ECOLI_NODE_
43 #define ECOLI_NODE_
44
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <stdio.h>
48
49 #define EC_NO_ID "no-id"
50
51 #define EC_NODE_ENDLIST ((void *)1)
52
53 struct ec_node;
54 struct ec_parse;
55 struct ec_comp;
56 struct ec_strvec;
57 struct ec_keyval;
58 struct ec_config;
59 struct ec_config_schema;
60
61 #define EC_NODE_TYPE_REGISTER(t)                                        \
62         static void ec_node_init_##t(void);                             \
63         static void __attribute__((constructor, used))                  \
64         ec_node_init_##t(void)                                          \
65         {                                                               \
66                 if (ec_node_type_register(&t) < 0)                      \
67                         fprintf(stderr,                                 \
68                                 "cannot register node type %s\n",       \
69                                 t.name);                                \
70         }
71
72 TAILQ_HEAD(ec_node_type_list, ec_node_type);
73
74 typedef int (*ec_node_set_config_t)(struct ec_node *node,
75                                 const struct ec_config *config);
76 typedef int (*ec_node_parse_t)(const struct ec_node *node,
77                         struct ec_parse *state,
78                         const struct ec_strvec *strvec);
79 typedef int (*ec_node_complete_t)(const struct ec_node *node,
80                                 struct ec_comp *comp_state,
81                                 const struct ec_strvec *strvec);
82 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
83 typedef int (*ec_node_init_priv_t)(struct ec_node *);
84 typedef void (*ec_node_free_priv_t)(struct ec_node *);
85
86 /**
87  * A structure describing a node type.
88  */
89 struct ec_node_type {
90         TAILQ_ENTRY(ec_node_type) next;  /**< Next in list. */
91         const char *name;                /**< Node type name. */
92         /** Generic configuration schema. */
93         const struct ec_config_schema *schema;
94         size_t schema_len;               /**< Number of elts in schema array. */
95         ec_node_set_config_t set_config; /* validate/ack a config change */
96         ec_node_parse_t parse;
97         ec_node_complete_t complete;
98         ec_node_desc_t desc;
99         size_t size;
100         ec_node_init_priv_t init_priv;
101         ec_node_free_priv_t free_priv;
102 };
103
104 /**
105  * Register a node type.
106  *
107  * @param type
108  *   A pointer to a ec_test structure describing the test
109  *   to be registered.
110  * @return
111  *   0 on success, negative value on error.
112  */
113 int ec_node_type_register(struct ec_node_type *type);
114
115 /**
116  * Lookup node type by name
117  *
118  * @param name
119  *   The name of the node type to search.
120  * @return
121  *   The node type if found, or NULL on error.
122  */
123 const struct ec_node_type *ec_node_type_lookup(const char *name);
124
125 /**
126  * Dump registered log types
127  */
128 void ec_node_type_dump(FILE *out);
129
130 struct ec_node {
131         const struct ec_node_type *type;
132         struct ec_config *config;    /**< Generic configuration. */
133         char *id;
134         char *desc;
135         struct ec_keyval *attrs;
136         unsigned int refcnt;
137         struct ec_node **children;   /* array of children */
138         size_t n_children;           /* number of children in the array */
139 };
140
141 /* create a new node when the type is known, typically called from the node
142  * code */
143 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
144
145 /* create a new node */
146 struct ec_node *ec_node(const char *typename, const char *id);
147
148 struct ec_node *ec_node_clone(struct ec_node *node);
149 void ec_node_free(struct ec_node *node);
150
151 /* set configuration of a node
152  * after a call to this function, the config is owned by the node and
153  * must not be used by the caller */
154 int ec_node_set_config(struct ec_node *node, struct ec_config *config);
155
156 size_t ec_node_get_children_count(const struct ec_node *node);
157 struct ec_node *
158 ec_node_get_child(const struct ec_node *node, size_t i);
159 int ec_node_add_child(struct ec_node *node, struct ec_node *child);
160 int ec_node_del_child(struct ec_node *node, struct ec_node *child);
161
162 /* XXX add more accessors */
163 const struct ec_node_type *ec_node_type(const struct ec_node *node);
164 struct ec_keyval *ec_node_attrs(const struct ec_node *node);
165 const char *ec_node_id(const struct ec_node *node);
166 const char *ec_node_desc(const struct ec_node *node);
167
168 void ec_node_dump(FILE *out, const struct ec_node *node);
169 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
170
171 /* check the type of a node */
172 int ec_node_check_type(const struct ec_node *node,
173                 const struct ec_node_type *type);
174
175 #endif