del -> unlink + const macro
[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_parsed;
55 struct ec_completed;
56 struct ec_strvec;
57 struct ec_keyval;
58
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)                                          \
63         {                                                               \
64                 if (ec_node_type_register(&t) < 0)                      \
65                         fprintf(stderr,                                 \
66                                 "cannot register node type %s\n",       \
67                                 t.name);                                \
68         }
69
70 TAILQ_HEAD(ec_node_type_list, ec_node_type);
71
72 /* return 0 on success, else -errno. */
73 typedef int (*ec_node_build_t)(struct ec_node *node);
74
75 typedef int (*ec_node_parse_t)(const struct ec_node *node,
76                         struct ec_parsed *state,
77                         const struct ec_strvec *strvec);
78 typedef int (*ec_node_complete_t)(const struct ec_node *node,
79                                 struct ec_completed *completed_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 *);
84
85 /**
86  * A structure describing a node type.
87  */
88 struct ec_node_type {
89         TAILQ_ENTRY(ec_node_type) next;  /**< Next in list. */
90         const char *name;                /**< Node type name. */
91         ec_node_build_t build;           /**< (Re)build the node */
92         ec_node_parse_t parse;
93         ec_node_complete_t complete;
94         ec_node_desc_t desc;
95         size_t size;
96         ec_node_init_priv_t init_priv;
97         ec_node_free_priv_t free_priv;
98 };
99
100 /**
101  * Register a node type.
102  *
103  * @param type
104  *   A pointer to a ec_test structure describing the test
105  *   to be registered.
106  * @return
107  *   0 on success, negative value on error.
108  */
109 int ec_node_type_register(struct ec_node_type *type);
110
111 /**
112  * Lookup node type by name
113  *
114  * @param name
115  *   The name of the node type to search.
116  * @return
117  *   The node type if found, or NULL on error.
118  */
119 struct ec_node_type *ec_node_type_lookup(const char *name);
120
121 /**
122  * Dump registered log types
123  */
124 void ec_node_type_dump(FILE *out);
125
126 struct ec_node {
127         const struct ec_node_type *type;
128         char *id;
129         char *desc;
130         struct ec_keyval *attrs;
131         unsigned int refcnt;
132         struct ec_node **children;   /* array of children */
133         size_t n_children;           /* number of children in the array */
134 };
135
136 /* create a new node when the type is known, typically called from the node
137  * code */
138 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id);
139
140 /* create a new node */
141 struct ec_node *ec_node(const char *typename, const char *id);
142
143 struct ec_node *ec_node_clone(struct ec_node *node);
144 void ec_node_free(struct ec_node *node);
145
146 size_t ec_node_get_children_count(const struct ec_node *node);
147 struct ec_node *
148 ec_node_get_child(const struct ec_node *node, size_t i);
149 int ec_node_add_child(struct ec_node *node, struct ec_node *child);
150 int ec_node_del_child(struct ec_node *node, struct ec_node *child);
151
152 /* XXX add more accessors */
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);
156
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);
159
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);
163
164 #endif