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->items);
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 /* count the number of identical chars at the beginning of 2 strings */
147 static size_t strcmp_count(const char *s1, const char *s2)
151 while (s1[i] && s2[i] && s1[i] == s2[i])
157 static struct ec_completed_item *
158 ec_completed_item(enum ec_completed_type type, struct ec_parsed *state,
159 const struct ec_node *node, const char *add)
161 struct ec_completed_item *item = NULL;
163 item = ec_calloc(1, sizeof(*item));
167 /* XXX can state be NULL? */
173 for (p = state, len = 0; p != NULL;
174 p = ec_parsed_get_parent(p), len++)
177 item->path = ec_calloc(len, sizeof(*item->path));
178 if (item->path == NULL)
183 /* write path in array */
184 for (p = state, len = 0; p != NULL;
185 p = ec_parsed_get_parent(p), len++)
186 item->path[len] = p->node;
192 item->add = ec_strdup(add);
193 if (item->add == NULL)
204 ec_completed_item_free(item);
209 static int ec_completed_add_item(struct ec_completed *completed,
210 struct ec_completed_item *item)
214 if (item->add != NULL) {
215 if (completed->smallest_start == NULL) {
216 completed->smallest_start = ec_strdup(item->add);
217 if (completed->smallest_start == NULL)
220 n = strcmp_count(item->add,
221 completed->smallest_start);
222 completed->smallest_start[n] = '\0';
224 completed->count_match++;
226 TAILQ_INSERT_TAIL(&completed->items, item, next);
232 int ec_completed_add_match(struct ec_completed *completed,
233 struct ec_parsed *state,
234 const struct ec_node *node, const char *add)
236 struct ec_completed_item *item;
239 item = ec_completed_item(EC_MATCH, state, node, add);
243 ret = ec_completed_add_item(completed, item);
245 ec_completed_item_free(item);
252 int ec_completed_add_no_match(struct ec_completed *completed,
253 struct ec_parsed *state, const struct ec_node *node)
255 struct ec_completed_item *item;
258 item = ec_completed_item(EC_NO_MATCH, state, node, NULL);
262 ret = ec_completed_add_item(completed, item);
264 ec_completed_item_free(item);
271 void ec_completed_item_free(struct ec_completed_item *item)
278 /* default completion function: return a no-match element */
279 struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
280 struct ec_parsed *state,
281 const struct ec_strvec *strvec)
283 struct ec_completed *completed;
288 completed = ec_completed();
289 if (completed == NULL)
292 if (ec_strvec_len(strvec) != 1)
295 if (ec_completed_add_no_match(completed, state, gen_node) < 0) {
296 ec_completed_free(completed);
303 void ec_completed_merge(struct ec_completed *completed1,
304 struct ec_completed *completed2)
306 struct ec_completed_item *item;
308 assert(completed1 != NULL);
309 assert(completed2 != NULL);
311 while (!TAILQ_EMPTY(&completed2->items)) {
312 item = TAILQ_FIRST(&completed2->items);
313 TAILQ_REMOVE(&completed2->items, item, next);
314 ec_completed_add_item(completed1, item);
317 ec_completed_free(completed2);
320 void ec_completed_free(struct ec_completed *completed)
322 struct ec_completed_item *item;
324 if (completed == NULL)
327 while (!TAILQ_EMPTY(&completed->items)) {
328 item = TAILQ_FIRST(&completed->items);
329 TAILQ_REMOVE(&completed->items, item, next);
330 ec_completed_item_free(item);
332 ec_free(completed->smallest_start);
336 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
338 struct ec_completed_item *item;
340 if (completed == NULL || completed->count == 0) {
341 fprintf(out, "no completion\n");
345 fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
346 completed->count, completed->count_match,
347 completed->smallest_start);
349 TAILQ_FOREACH(item, &completed->items, next) {
350 fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
351 item->add, item->node, item->node->type->name);
355 const char *ec_completed_smallest_start(
356 const struct ec_completed *completed)
358 if (completed == NULL || completed->smallest_start == NULL)
361 return completed->smallest_start;
364 unsigned int ec_completed_count(
365 const struct ec_completed *completed,
366 enum ec_completed_type type)
368 unsigned int count = 0;
370 if (completed == NULL)
374 count += completed->count_match;
375 if (type & EC_NO_MATCH)
376 count += (completed->count - completed->count_match); //XXX
381 struct ec_completed_iter *
382 ec_completed_iter(struct ec_completed *completed,
383 enum ec_completed_type type)
385 struct ec_completed_iter *iter;
387 iter = ec_calloc(1, sizeof(*iter));
391 iter->completed = completed;
398 const struct ec_completed_item *ec_completed_iter_next(
399 struct ec_completed_iter *iter)
401 if (iter->completed == NULL)
405 if (iter->cur == NULL)
406 iter->cur = TAILQ_FIRST(&iter->completed->items);
408 iter->cur = TAILQ_NEXT(iter->cur, next);
410 if (iter->cur == NULL)
413 if (iter->cur->add == NULL &&
414 (iter->type & EC_NO_MATCH))
417 if (iter->cur->add != NULL &&
418 (iter->type & EC_MATCH))
421 } while (iter->cur != NULL);
426 void ec_completed_iter_free(struct ec_completed_iter *iter)