2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
34 #include <ecoli_malloc.h>
35 #include <ecoli_strvec.h>
36 #include <ecoli_keyval.h>
37 #include <ecoli_log.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
42 struct ec_completed *ec_completed(void)
44 struct ec_completed *completed = NULL;
46 completed = ec_calloc(1, sizeof(*completed));
47 if (completed == NULL)
50 TAILQ_INIT(&completed->elts);
51 completed->count_match = 0;
57 ec_node_complete_child(struct ec_node *node,
58 struct ec_parsed *state,
59 const struct ec_strvec *strvec)
61 struct ec_completed *completed;
62 struct ec_parsed *child;
65 /* build the node if required */
66 if (node->type->build != NULL) {
67 if ((node->flags & EC_NODE_F_BUILT) == 0) {
68 ret = node->type->build(node);
75 node->flags |= EC_NODE_F_BUILT;
77 if (node->type->complete == NULL) {
87 ec_parsed_add_child(state, child);
88 completed = node->type->complete(node, child, strvec);
91 printf("----------------------------------------------------------\n");
92 ec_node_dump(stdout, node);
93 ec_strvec_dump(stdout, strvec);
94 ec_completed_dump(stdout, completed);
95 ec_parsed_dump(stdout, state);
98 ec_parsed_del_child(state, child);
99 assert(TAILQ_EMPTY(&child->children));
100 ec_parsed_free(child);
105 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
106 const struct ec_strvec *strvec)
108 struct ec_parsed *state = ec_parsed();
109 struct ec_completed *completed;
114 completed = ec_node_complete_child(node, state, strvec);
115 ec_parsed_free(state);
120 struct ec_completed *ec_node_complete(struct ec_node *node,
123 struct ec_strvec *strvec = NULL;
124 struct ec_completed *completed;
127 strvec = ec_strvec();
131 if (ec_strvec_add(strvec, str) < 0)
134 completed = ec_node_complete_strvec(node, strvec);
135 if (completed == NULL)
138 ec_strvec_free(strvec);
142 ec_strvec_free(strvec);
146 /* default completion function: return a no-match element */
147 struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
148 struct ec_parsed *state,
149 const struct ec_strvec *strvec)
151 struct ec_completed *completed;
156 completed = ec_completed();
157 if (completed == NULL)
160 if (ec_strvec_len(strvec) != 1)
163 if (ec_completed_add_elt(completed, state, gen_node, NULL) < 0) {
164 ec_completed_free(completed);
171 /* count the number of identical chars at the beginning of 2 strings */
172 static size_t strcmp_count(const char *s1, const char *s2)
176 while (s1[i] && s2[i] && s1[i] == s2[i])
182 static struct ec_completed_elt *
183 ec_completed_elt(struct ec_parsed *parsed,
184 const struct ec_node *node, const char *add)
186 struct ec_completed_elt *elt = NULL;
188 elt = ec_calloc(1, sizeof(*elt));
192 if (parsed != NULL) {
197 for (p = parsed, len = 0; p != NULL;
198 p = ec_parsed_get_parent(p), len++)
201 elt->path = ec_calloc(len, sizeof(*elt->path));
202 if (elt->path == NULL)
207 /* write path in array */
208 for (p = parsed, len = 0; p != NULL;
209 p = ec_parsed_get_parent(p), len++)
210 elt->path[len] = p->node;
215 elt->add = ec_strdup(add);
216 if (elt->add == NULL)
227 ec_completed_elt_free(elt);
232 static int __ec_completed_add_elt(struct ec_completed *completed,
233 struct ec_completed_elt *elt)
237 TAILQ_INSERT_TAIL(&completed->elts, elt, next);
239 if (elt->add != NULL) {
240 completed->count_match++;
241 if (completed->smallest_start == NULL) {
242 completed->smallest_start = ec_strdup(elt->add);
244 n = strcmp_count(elt->add,
245 completed->smallest_start);
246 completed->smallest_start[n] = '\0';
253 int ec_completed_add_elt(struct ec_completed *completed,
254 struct ec_parsed *parsed,
255 const struct ec_node *node, const char *add)
257 struct ec_completed_elt *elt;
259 elt = ec_completed_elt(parsed, node, add);
263 return __ec_completed_add_elt(completed, elt);
266 void ec_completed_elt_free(struct ec_completed_elt *elt)
273 void ec_completed_merge(struct ec_completed *completed1,
274 struct ec_completed *completed2)
276 struct ec_completed_elt *elt;
278 assert(completed1 != NULL);
279 assert(completed2 != NULL);
281 while (!TAILQ_EMPTY(&completed2->elts)) {
282 elt = TAILQ_FIRST(&completed2->elts);
283 TAILQ_REMOVE(&completed2->elts, elt, next);
284 __ec_completed_add_elt(completed1, elt);
287 ec_completed_free(completed2);
290 void ec_completed_free(struct ec_completed *completed)
292 struct ec_completed_elt *elt;
294 if (completed == NULL)
297 while (!TAILQ_EMPTY(&completed->elts)) {
298 elt = TAILQ_FIRST(&completed->elts);
299 TAILQ_REMOVE(&completed->elts, elt, next);
300 ec_completed_elt_free(elt);
302 ec_free(completed->smallest_start);
306 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
308 struct ec_completed_elt *elt;
310 if (completed == NULL || completed->count == 0) {
311 fprintf(out, "no completion\n");
315 fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
316 completed->count, completed->count_match,
317 completed->smallest_start);
319 TAILQ_FOREACH(elt, &completed->elts, next) {
320 fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
321 elt->add, elt->node, elt->node->type->name);
325 const char *ec_completed_smallest_start(
326 const struct ec_completed *completed)
328 if (completed == NULL || completed->smallest_start == NULL)
331 return completed->smallest_start;
334 unsigned int ec_completed_count(
335 const struct ec_completed *completed,
336 enum ec_completed_filter_flags flags)
338 unsigned int count = 0;
340 if (completed == NULL)
343 if (flags & EC_MATCH)
344 count += completed->count_match;
345 if (flags & EC_NO_MATCH)
346 count += (completed->count - completed->count_match); //XXX
351 struct ec_completed_iter *
352 ec_completed_iter(struct ec_completed *completed,
353 enum ec_completed_filter_flags flags)
355 struct ec_completed_iter *iter;
357 iter = ec_calloc(1, sizeof(*iter));
361 iter->completed = completed;
368 const struct ec_completed_elt *ec_completed_iter_next(
369 struct ec_completed_iter *iter)
371 if (iter->completed == NULL)
375 if (iter->cur == NULL)
376 iter->cur = TAILQ_FIRST(&iter->completed->elts);
378 iter->cur = TAILQ_NEXT(iter->cur, next);
380 if (iter->cur == NULL)
383 if (iter->cur->add == NULL &&
384 (iter->flags & EC_NO_MATCH))
387 if (iter->cur->add != NULL &&
388 (iter->flags & EC_MATCH))
391 } while (iter->cur != NULL);
396 void ec_completed_iter_free(struct ec_completed_iter *iter)