hide ec_node structure
[protos/libecoli.git] / include / ecoli_complete.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 /**
6  * @defgroup complete Complete
7  * @{
8  *
9  * @brief Complete string input using a grammar tree
10  *
11  * This file provide helpers to list and manipulate the possible
12  * completions for a given input.
13  *
14  * XXX comp vs item
15  *
16  * @}
17  */
18
19 #ifndef ECOLI_COMPLETE_
20 #define ECOLI_COMPLETE_
21
22 #include <sys/queue.h>
23 #include <sys/types.h>
24 #include <stdio.h>
25
26 struct ec_node;
27
28 enum ec_comp_type { /* XXX should be a define */
29         EC_COMP_UNKNOWN = 0x1,
30         EC_COMP_FULL = 0x2,
31         EC_COMP_PARTIAL = 0x4,
32         EC_COMP_ALL = 0x7,
33 };
34
35 struct ec_comp_item;
36
37 TAILQ_HEAD(ec_comp_item_list, ec_comp_item);
38
39 struct ec_comp_group {
40         TAILQ_ENTRY(ec_comp_group) next;
41         const struct ec_node *node;
42         struct ec_comp_item_list items;
43         struct ec_parse *state;
44         struct ec_dict *attrs;
45 };
46
47 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
48
49 struct ec_comp {
50         unsigned count;
51         unsigned count_full;
52         unsigned count_partial;
53         unsigned count_unknown;
54         struct ec_parse *cur_state;
55         struct ec_comp_group *cur_group;
56         struct ec_comp_group_list groups;
57         struct ec_dict *attrs;
58 };
59
60 /*
61  * return a comp object filled with items
62  * return NULL on error (nomem?)
63  */
64 struct ec_comp *ec_node_complete(const struct ec_node *node,
65         const char *str);
66 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
67         const struct ec_strvec *strvec);
68
69 /* internal: used by nodes */
70 int ec_node_complete_child(const struct ec_node *node,
71                         struct ec_comp *comp,
72                         const struct ec_strvec *strvec);
73
74 /**
75  * Create a completion object (list of completion items).
76  *
77  *
78  */
79 struct ec_comp *ec_comp(struct ec_parse *state);
80
81 /**
82  * Free a completion object and all its items.
83  *
84  *
85  */
86 void ec_comp_free(struct ec_comp *comp);
87
88 /**
89  *
90  *
91  *
92  */
93 void ec_comp_dump(FILE *out,
94         const struct ec_comp *comp);
95
96 /**
97  * Merge items contained in 'from' into 'to'
98  *
99  * The 'from' comp struct is freed.
100  */
101 int ec_comp_merge(struct ec_comp *to,
102                 struct ec_comp *from);
103
104 struct ec_parse *ec_comp_get_state(struct ec_comp *comp);
105
106 /* shortcut for ec_comp_item() + ec_comp_item_add() */
107 int ec_comp_add_item(struct ec_comp *comp,
108                         const struct ec_node *node,
109                         struct ec_comp_item **p_item,
110                         enum ec_comp_type type,
111                         const char *start, const char *full);
112
113 /**
114  *
115  */
116 int ec_comp_item_set_str(struct ec_comp_item *item,
117                         const char *str);
118
119 /**
120  * Get the string value of a completion item.
121  *
122  *
123  */
124 const char *
125 ec_comp_item_get_str(const struct ec_comp_item *item);
126
127 /**
128  * Get the display string value of a completion item.
129  *
130  *
131  */
132 const char *
133 ec_comp_item_get_display(const struct ec_comp_item *item);
134
135 /**
136  * Get the completion string value of a completion item.
137  *
138  *
139  */
140 const char *
141 ec_comp_item_get_completion(const struct ec_comp_item *item);
142
143 /**
144  * Get the group of a completion item.
145  *
146  *
147  */
148 const struct ec_comp_group *
149 ec_comp_item_get_grp(const struct ec_comp_item *item);
150
151 /**
152  * Get the type of a completion item.
153  *
154  *
155  */
156 enum ec_comp_type
157 ec_comp_item_get_type(const struct ec_comp_item *item);
158
159 /**
160  * Get the node associated to a completion item.
161  *
162  *
163  */
164 const struct ec_node *
165 ec_comp_item_get_node(const struct ec_comp_item *item);
166
167 /**
168  * Set the display value of an item.
169  *
170  *
171  */
172 int ec_comp_item_set_display(struct ec_comp_item *item,
173                                 const char *display);
174
175 /**
176  * Set the completion value of an item.
177  *
178  *
179  */
180 int ec_comp_item_set_completion(struct ec_comp_item *item,
181                                 const char *completion);
182
183 /**
184  *
185  *
186  *
187  */
188 int
189 ec_node_complete_unknown(const struct ec_node *gen_node,
190                         struct ec_comp *comp,
191                         const struct ec_strvec *strvec);
192
193 /**
194  *
195  *
196  *
197  */
198 unsigned int ec_comp_count(
199         const struct ec_comp *comp,
200         enum ec_comp_type flags);
201
202 /**
203  *
204  *
205  *
206  */
207 struct ec_comp_iter {
208         enum ec_comp_type type;
209         const struct ec_comp *comp;
210         struct ec_comp_group *cur_node;
211         struct ec_comp_item *cur_match;
212 };
213
214 /**
215  *
216  *
217  *
218  */
219 struct ec_comp_iter *
220 ec_comp_iter(const struct ec_comp *comp,
221         enum ec_comp_type type);
222
223 /**
224  *
225  *
226  *
227  */
228 struct ec_comp_item *ec_comp_iter_next(
229         struct ec_comp_iter *iter);
230
231 /**
232  *
233  *
234  *
235  */
236 void ec_comp_iter_free(struct ec_comp_iter *iter);
237
238
239 #endif