c7a6a623008fa67d81d50525e73519c919f57a9d
[protos/libecoli.git] / lib / ecoli_completed.h
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /**
29  * API for generating completions item on a node.
30  *
31  * This file provide helpers to list and manipulate the possible
32  * completions for a given input.
33  *
34  * XXX completed vs item
35  */
36
37 #ifndef ECOLI_COMPLETED_
38 #define ECOLI_COMPLETED_
39
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <stdio.h>
43
44 struct ec_node;
45
46 enum ec_completed_type {
47         EC_COMP_UNKNOWN = 0x1,
48         EC_COMP_FULL = 0x2,
49         EC_COMP_PARTIAL = 0x4,
50         EC_COMP_ALL = 0x7,
51 };
52
53 struct ec_completed_item;
54
55 TAILQ_HEAD(ec_completed_item_list, ec_completed_item);
56
57 struct ec_completed_group {
58         TAILQ_ENTRY(ec_completed_group) next;
59         const struct ec_node *node;
60         struct ec_completed_item_list items;
61         struct ec_parsed *state;
62         struct ec_keyval *attrs;
63 };
64
65 TAILQ_HEAD(ec_completed_group_list, ec_completed_group);
66
67 struct ec_completed {
68         unsigned count;
69         unsigned count_full;
70         unsigned count_partial;
71         unsigned count_unknown;
72         struct ec_parsed *cur_state;
73         struct ec_completed_group *cur_group;
74         struct ec_completed_group_list groups;
75         struct ec_keyval *attrs;
76 };
77
78 /*
79  * return a completed object filled with items
80  * return NULL on error (nomem?)
81  */
82 struct ec_completed *ec_node_complete(const struct ec_node *node,
83         const char *str);
84 struct ec_completed *ec_node_complete_strvec(const struct ec_node *node,
85         const struct ec_strvec *strvec);
86
87 /* internal: used by nodes */
88 int ec_node_complete_child(const struct ec_node *node,
89                         struct ec_completed *completed,
90                         const struct ec_strvec *strvec);
91
92 /**
93  * Create a completion object (list of completion items).
94  *
95  *
96  */
97 struct ec_completed *ec_completed(struct ec_parsed *state);
98
99 /**
100  * Free a completion object and all its items.
101  *
102  *
103  */
104 void ec_completed_free(struct ec_completed *completed);
105
106 /**
107  *
108  *
109  *
110  */
111 void ec_completed_dump(FILE *out,
112         const struct ec_completed *completed);
113
114 /**
115  * Merge items contained in 'from' into 'to'
116  *
117  * The 'from' completed struct is freed.
118  */
119 int ec_completed_merge(struct ec_completed *to,
120                 struct ec_completed *from);
121
122 struct ec_parsed *ec_completed_get_state(struct ec_completed *completed);
123
124 /* shortcut for ec_completed_item() + ec_completed_item_add() */
125 int ec_completed_add_item(struct ec_completed *completed,
126                         const struct ec_node *node,
127                         struct ec_completed_item **p_item,
128                         enum ec_completed_type type,
129                         const char *start, const char *full);
130
131 /**
132  *
133  */
134 int ec_completed_item_set_str(struct ec_completed_item *item,
135                         const char *str);
136
137 /**
138  * Get the string value of a completion item.
139  *
140  *
141  */
142 const char *
143 ec_completed_item_get_str(const struct ec_completed_item *item);
144
145 /**
146  * Get the display string value of a completion item.
147  *
148  *
149  */
150 const char *
151 ec_completed_item_get_display(const struct ec_completed_item *item);
152
153 /**
154  * Get the completion string value of a completion item.
155  *
156  *
157  */
158 const char *
159 ec_completed_item_get_completion(const struct ec_completed_item *item);
160
161 /**
162  * Get the group of a completion item.
163  *
164  *
165  */
166 const struct ec_completed_group *
167 ec_completed_item_get_grp(const struct ec_completed_item *item);
168
169 /**
170  * Get the type of a completion item.
171  *
172  *
173  */
174 enum ec_completed_type
175 ec_completed_item_get_type(const struct ec_completed_item *item);
176
177 /**
178  * Get the node associated to a completion item.
179  *
180  *
181  */
182 const struct ec_node *
183 ec_completed_item_get_node(const struct ec_completed_item *item);
184
185 /**
186  * Set the display value of an item.
187  *
188  *
189  */
190 int ec_completed_item_set_display(struct ec_completed_item *item,
191                                 const char *display);
192
193 /**
194  * Set the completion value of an item.
195  *
196  *
197  */
198 int ec_completed_item_set_completion(struct ec_completed_item *item,
199                                 const char *completion);
200
201 /**
202  *
203  *
204  *
205  */
206 int
207 ec_node_default_complete(const struct ec_node *gen_node,
208                         struct ec_completed *completed,
209                         const struct ec_strvec *strvec);
210
211 /**
212  *
213  *
214  *
215  */
216 unsigned int ec_completed_count(
217         const struct ec_completed *completed,
218         enum ec_completed_type flags);
219
220 /**
221  *
222  *
223  *
224  */
225 struct ec_completed_iter {
226         enum ec_completed_type type;
227         struct ec_completed *completed;
228         struct ec_completed_group *cur_node;
229         struct ec_completed_item *cur_match;
230 };
231
232 /**
233  *
234  *
235  *
236  */
237 struct ec_completed_iter *
238 ec_completed_iter(struct ec_completed *completed,
239         enum ec_completed_type type);
240
241 /**
242  *
243  *
244  *
245  */
246 struct ec_completed_item *ec_completed_iter_next(
247         struct ec_completed_iter *iter);
248
249 /**
250  *
251  *
252  *
253  */
254 void ec_completed_iter_free(struct ec_completed_iter *iter);
255
256
257 #endif