c57eb679d6f8bb9754021c26b97aa289c1d1e6d6
[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 struct ec_comp_group;
37 struct ec_comp;
38
39 /*
40  * return a comp object filled with items
41  * return NULL on error (nomem?)
42  */
43 struct ec_comp *ec_node_complete(const struct ec_node *node,
44         const char *str);
45 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
46         const struct ec_strvec *strvec);
47
48 /* internal: used by nodes */
49 int ec_node_complete_child(const struct ec_node *node,
50                         struct ec_comp *comp,
51                         const struct ec_strvec *strvec);
52
53 /**
54  * Create a completion object (list of completion items).
55  *
56  *
57  */
58 struct ec_comp *ec_comp(struct ec_parse *state);
59
60 /**
61  * Free a completion object and all its items.
62  *
63  *
64  */
65 void ec_comp_free(struct ec_comp *comp);
66
67 /**
68  *
69  *
70  *
71  */
72 void ec_comp_dump(FILE *out,
73         const struct ec_comp *comp);
74
75 /**
76  * Merge items contained in 'from' into 'to'
77  *
78  * The 'from' comp struct is freed.
79  */
80 int ec_comp_merge(struct ec_comp *to,
81                 struct ec_comp *from);
82
83 /**
84  * Get current completion state.
85  *
86  */
87 struct ec_parse *ec_comp_get_state(const struct ec_comp *comp);
88
89 /**
90  * Get current completion group.
91  *
92  */
93 struct ec_comp_group *ec_comp_get_group(const struct ec_comp *comp);
94
95 /**
96  * Get completion group attributes.
97  *
98  */
99 struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp);
100
101 /* shortcut for ec_comp_item() + ec_comp_item_add() */
102 int ec_comp_add_item(struct ec_comp *comp,
103                         const struct ec_node *node,
104                         struct ec_comp_item **p_item,
105                         enum ec_comp_type type,
106                         const char *start, const char *full);
107
108 /**
109  *
110  */
111 int ec_comp_item_set_str(struct ec_comp_item *item,
112                         const char *str);
113
114 /**
115  * Get the string value of a completion item.
116  *
117  *
118  */
119 const char *
120 ec_comp_item_get_str(const struct ec_comp_item *item);
121
122 /**
123  * Get the display string value of a completion item.
124  *
125  *
126  */
127 const char *
128 ec_comp_item_get_display(const struct ec_comp_item *item);
129
130 /**
131  * Get the completion string value of a completion item.
132  *
133  *
134  */
135 const char *
136 ec_comp_item_get_completion(const struct ec_comp_item *item);
137
138 /**
139  * Get the group of a completion item.
140  *
141  *
142  */
143 const struct ec_comp_group *
144 ec_comp_item_get_grp(const struct ec_comp_item *item);
145
146 /**
147  * Get the type of a completion item.
148  *
149  *
150  */
151 enum ec_comp_type
152 ec_comp_item_get_type(const struct ec_comp_item *item);
153
154 /**
155  * Get the node associated to a completion item.
156  *
157  *
158  */
159 const struct ec_node *
160 ec_comp_item_get_node(const struct ec_comp_item *item);
161
162 /**
163  * Set the display value of an item.
164  *
165  *
166  */
167 int ec_comp_item_set_display(struct ec_comp_item *item,
168                                 const char *display);
169
170 /**
171  * Set the completion value of an item.
172  *
173  *
174  */
175 int ec_comp_item_set_completion(struct ec_comp_item *item,
176                                 const char *completion);
177
178 /**
179  * Get the completion group node.
180  *
181  *
182  */
183 const struct ec_node *
184 ec_comp_group_get_node(const struct ec_comp_group *grp);
185
186 /**
187  * Get the completion group parsed state.
188  *
189  *
190  */
191 const struct ec_parse *
192 ec_comp_group_get_state(const struct ec_comp_group *grp);
193
194 /**
195  * Get the completion group attributes.
196  *
197  *
198  */
199 const struct ec_dict *
200 ec_comp_group_get_attrs(const struct ec_comp_group *grp);
201
202
203 /**
204  *
205  *
206  *
207  */
208 int
209 ec_node_complete_unknown(const struct ec_node *gen_node,
210                         struct ec_comp *comp,
211                         const struct ec_strvec *strvec);
212
213 /**
214  *
215  *
216  *
217  */
218 unsigned int ec_comp_count(
219         const struct ec_comp *comp,
220         enum ec_comp_type flags);
221
222 /**
223  *
224  *
225  *
226  */
227 struct ec_comp_iter {
228         enum ec_comp_type type;
229         const struct ec_comp *comp;
230         struct ec_comp_group *cur_node;
231         struct ec_comp_item *cur_match;
232 };
233
234 /**
235  *
236  *
237  *
238  */
239 struct ec_comp_iter *
240 ec_comp_iter(const struct ec_comp *comp,
241         enum ec_comp_type type);
242
243 /**
244  *
245  *
246  *
247  */
248 struct ec_comp_item *ec_comp_iter_next(
249         struct ec_comp_iter *iter);
250
251 /**
252  *
253  *
254  *
255  */
256 void ec_comp_iter_free(struct ec_comp_iter *iter);
257
258
259 #endif