1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
6 * Interface to manage the ecoli nodes.
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
11 * - a parse(node, input) method: check if an input matches
12 * - a complete(node, input) method: return possible completions for
14 * - some other methods to initialize, free, ...
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",
20 * - ["foo", "bar", ...]
25 * A node ec_node_str("foo") will complete with "foo" if the input
26 * contains one token, with the same beginning than "foo":
31 * But it will not complete:
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
45 #include <sys/queue.h>
46 #include <sys/types.h>
49 #define EC_NO_ID "no-id"
51 #define EC_NODE_ENDLIST ((void *)1)
59 struct ec_config_schema;
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) \
66 if (ec_node_type_register(&t) < 0) \
68 "cannot register node type %s\n", \
72 TAILQ_HEAD(ec_node_type_list, ec_node_type);
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 *);
87 * A structure describing a 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;
100 ec_node_init_priv_t init_priv;
101 ec_node_free_priv_t free_priv;
105 * Register a node type.
108 * A pointer to a ec_test structure describing the test
111 * 0 on success, negative value on error.
113 int ec_node_type_register(struct ec_node_type *type);
116 * Lookup node type by name
119 * The name of the node type to search.
121 * The node type if found, or NULL on error.
123 const struct ec_node_type *ec_node_type_lookup(const char *name);
126 * Dump registered log types
128 void ec_node_type_dump(FILE *out);
131 const struct ec_node_type *type;
132 struct ec_config *config; /**< Generic configuration. */
135 struct ec_keyval *attrs;
137 struct ec_node **children; /* array of children */
138 size_t n_children; /* number of children in the array */
141 /* create a new node when the type is known, typically called from the node
143 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
145 /* create a new node */
146 struct ec_node *ec_node(const char *typename, const char *id);
148 struct ec_node *ec_node_clone(struct ec_node *node);
149 void ec_node_free(struct ec_node *node);
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);
156 size_t ec_node_get_children_count(const struct ec_node *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);
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);
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);
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);