add ec_node_bypass
[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  * API for generating completions item on a node.
7  *
8  * This file provide helpers to list and manipulate the possible
9  * completions for a given input.
10  *
11  * XXX comp vs item
12  */
13
14 #ifndef ECOLI_COMPLETE_
15 #define ECOLI_COMPLETE_
16
17 #include <sys/queue.h>
18 #include <sys/types.h>
19 #include <stdio.h>
20
21 struct ec_node;
22
23 enum ec_comp_type { /* XXX should be a define */
24         EC_COMP_UNKNOWN = 0x1,
25         EC_COMP_FULL = 0x2,
26         EC_COMP_PARTIAL = 0x4,
27         EC_COMP_ALL = 0x7,
28 };
29
30 struct ec_comp_item;
31
32 TAILQ_HEAD(ec_comp_item_list, ec_comp_item);
33
34 struct ec_comp_group {
35         TAILQ_ENTRY(ec_comp_group) next;
36         const struct ec_node *node;
37         struct ec_comp_item_list items;
38         struct ec_parse *state;
39         struct ec_keyval *attrs;
40 };
41
42 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
43
44 struct ec_comp {
45         unsigned count;
46         unsigned count_full;
47         unsigned count_partial;
48         unsigned count_unknown;
49         struct ec_parse *cur_state;
50         struct ec_comp_group *cur_group;
51         struct ec_comp_group_list groups;
52         struct ec_keyval *attrs;
53 };
54
55 /*
56  * return a comp object filled with items
57  * return NULL on error (nomem?)
58  */
59 struct ec_comp *ec_node_complete(const struct ec_node *node,
60         const char *str);
61 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
62         const struct ec_strvec *strvec);
63
64 /* internal: used by nodes */
65 int ec_node_complete_child(const struct ec_node *node,
66                         struct ec_comp *comp,
67                         const struct ec_strvec *strvec);
68
69 /**
70  * Create a completion object (list of completion items).
71  *
72  *
73  */
74 struct ec_comp *ec_comp(struct ec_parse *state);
75
76 /**
77  * Free a completion object and all its items.
78  *
79  *
80  */
81 void ec_comp_free(struct ec_comp *comp);
82
83 /**
84  *
85  *
86  *
87  */
88 void ec_comp_dump(FILE *out,
89         const struct ec_comp *comp);
90
91 /**
92  * Merge items contained in 'from' into 'to'
93  *
94  * The 'from' comp struct is freed.
95  */
96 int ec_comp_merge(struct ec_comp *to,
97                 struct ec_comp *from);
98
99 struct ec_parse *ec_comp_get_state(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  *
180  *
181  *
182  */
183 int
184 ec_node_complete_unknown(const struct ec_node *gen_node,
185                         struct ec_comp *comp,
186                         const struct ec_strvec *strvec);
187
188 /**
189  *
190  *
191  *
192  */
193 unsigned int ec_comp_count(
194         const struct ec_comp *comp,
195         enum ec_comp_type flags);
196
197 /**
198  *
199  *
200  *
201  */
202 struct ec_comp_iter {
203         enum ec_comp_type type;
204         const struct ec_comp *comp;
205         struct ec_comp_group *cur_node;
206         struct ec_comp_item *cur_match;
207 };
208
209 /**
210  *
211  *
212  *
213  */
214 struct ec_comp_iter *
215 ec_comp_iter(const struct ec_comp *comp,
216         enum ec_comp_type type);
217
218 /**
219  *
220  *
221  *
222  */
223 struct ec_comp_item *ec_comp_iter_next(
224         struct ec_comp_iter *iter);
225
226 /**
227  *
228  *
229  *
230  */
231 void ec_comp_iter_free(struct ec_comp_iter *iter);
232
233
234 #endif