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 #define EC_NODE_TYPE_REGISTER(t) \
60 static void ec_node_init_##t(void); \
61 static void __attribute__((constructor, used)) \
62 ec_node_init_##t(void) \
64 if (ec_node_type_register(&t) < 0) \
66 "cannot register node type %s\n", \
70 TAILQ_HEAD(ec_node_type_list, ec_node_type);
72 /* return 0 on success, else -errno. */
73 typedef int (*ec_node_build_t)(struct ec_node *node);
75 typedef int (*ec_node_parse_t)(const struct ec_node *node,
76 struct ec_parse *state,
77 const struct ec_strvec *strvec);
78 typedef int (*ec_node_complete_t)(const struct ec_node *node,
79 struct ec_comp *comp_state,
80 const struct ec_strvec *strvec);
81 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
82 typedef int (*ec_node_init_priv_t)(struct ec_node *);
83 typedef void (*ec_node_free_priv_t)(struct ec_node *);
86 * A structure describing a node type.
89 TAILQ_ENTRY(ec_node_type) next; /**< Next in list. */
90 const char *name; /**< Node type name. */
91 ec_node_parse_t parse;
92 ec_node_complete_t complete;
95 ec_node_init_priv_t init_priv;
96 ec_node_free_priv_t free_priv;
100 * Register a node type.
103 * A pointer to a ec_test structure describing the test
106 * 0 on success, negative value on error.
108 int ec_node_type_register(struct ec_node_type *type);
111 * Lookup node type by name
114 * The name of the node type to search.
116 * The node type if found, or NULL on error.
118 const struct ec_node_type *ec_node_type_lookup(const char *name);
121 * Dump registered log types
123 void ec_node_type_dump(FILE *out);
126 const struct ec_node_type *type;
129 struct ec_keyval *attrs;
131 struct ec_node **children; /* array of children */
132 size_t n_children; /* number of children in the array */
135 /* create a new node when the type is known, typically called from the node
137 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
139 /* create a new node */
140 struct ec_node *ec_node(const char *typename, const char *id);
142 struct ec_node *ec_node_clone(struct ec_node *node);
143 void ec_node_free(struct ec_node *node);
145 size_t ec_node_get_children_count(const struct ec_node *node);
147 ec_node_get_child(const struct ec_node *node, size_t i);
148 int ec_node_add_child(struct ec_node *node, struct ec_node *child);
149 int ec_node_del_child(struct ec_node *node, struct ec_node *child);
151 /* XXX add more accessors */
152 const struct ec_node_type *ec_node_type(const struct ec_node *node);
153 struct ec_keyval *ec_node_attrs(const struct ec_node *node);
154 const char *ec_node_id(const struct ec_node *node);
155 const char *ec_node_desc(const struct ec_node *node);
157 void ec_node_dump(FILE *out, const struct ec_node *node);
158 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
160 /* check the type of a node */
161 int ec_node_check_type(const struct ec_node *node,
162 const struct ec_node_type *type);