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->nodes);
55 /* XXX on error, states are not freed ?
56 * they can be left in a bad state and should not be reused */
58 ec_node_complete_child(struct ec_node *node,
59 struct ec_completed *completed,
60 struct ec_parsed *parsed_state,
61 const struct ec_strvec *strvec)
63 struct ec_parsed *child_state = NULL;
66 /* build the node if required */
67 if (node->type->build != NULL) {
68 if ((node->flags & EC_NODE_F_BUILT) == 0) {
69 ret = node->type->build(node);
74 node->flags |= EC_NODE_F_BUILT;
76 if (node->type->complete == NULL)
79 child_state = ec_parsed();
80 if (child_state == NULL)
82 child_state->node = node;
83 ec_parsed_add_child(parsed_state, child_state);
85 ret = node->type->complete(node, completed, child_state, strvec);
90 printf("----------------------------------------------------------\n");
91 ec_node_dump(stdout, node);
92 ec_strvec_dump(stdout, strvec);
93 ec_completed_dump(stdout, completed);
94 ec_parsed_dump(stdout, parsed_state);
97 ec_parsed_del_child(parsed_state, child_state);
98 assert(TAILQ_EMPTY(&child_state->children));
99 ec_parsed_free(child_state);
104 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
105 const struct ec_strvec *strvec)
107 struct ec_parsed *parsed_state = NULL;
108 struct ec_completed *completed = NULL;
111 parsed_state = ec_parsed();
112 if (parsed_state == NULL)
115 completed = ec_completed();
116 if (completed == NULL)
119 ret = ec_node_complete_child(node, completed,
120 parsed_state, strvec);
124 ec_parsed_free(parsed_state);
129 ec_parsed_free(parsed_state);
130 ec_completed_free(completed);
134 struct ec_completed *ec_node_complete(struct ec_node *node,
137 struct ec_strvec *strvec = NULL;
138 struct ec_completed *completed;
141 strvec = ec_strvec();
145 if (ec_strvec_add(strvec, str) < 0)
148 completed = ec_node_complete_strvec(node, strvec);
149 if (completed == NULL)
152 ec_strvec_free(strvec);
156 ec_strvec_free(strvec);
160 static struct ec_completed_node *
161 ec_completed_node(const struct ec_node *node)
163 struct ec_completed_node *compnode = NULL;
165 compnode = ec_calloc(1, sizeof(*compnode));
166 if (compnode == NULL)
169 compnode->node = node;
170 TAILQ_INIT(&compnode->items);
175 struct ec_completed_item *
176 ec_completed_item(struct ec_parsed *state, const struct ec_node *node)
178 struct ec_completed_item *item = NULL;
182 item = ec_calloc(1, sizeof(*item));
187 for (p = state, len = 0; p != NULL;
188 p = ec_parsed_get_parent(p), len++)
190 /* allocate room for path */
191 item->path = ec_calloc(len, sizeof(*item->path));
192 if (item->path == NULL)
195 /* write path in array */
196 for (p = state, len = 0; p != NULL;
197 p = ec_parsed_get_parent(p), len++)
198 item->path[len] = p->node;
200 item->type = EC_NO_MATCH;
209 ec_free(item->display);
211 ec_completed_item_free(item);
217 ec_completed_item_set(struct ec_completed_item *item,
218 enum ec_completed_type type, const char *str)
220 char *str_copy = NULL;
221 char *display_copy = NULL;
226 if (item->str != NULL)
235 case EC_PARTIAL_MATCH:
245 str_copy = ec_strdup(str);
246 if (str_copy == NULL)
248 display_copy = ec_strdup(str);
249 if (display_copy == NULL)
254 item->str = str_copy;
255 item->display = display_copy;
260 ec_free(display_copy);
264 int ec_completed_item_set_display(struct ec_completed_item *item,
267 char *display_copy = NULL;
270 if (item == NULL || display == NULL ||
271 item->type == EC_NO_MATCH || item->str == NULL)
275 display_copy = ec_strdup(display);
276 if (display_copy == NULL)
279 ec_free(item->display);
280 item->display = display_copy;
285 ec_free(display_copy);
291 ec_completed_item_add(struct ec_completed *completed,
292 struct ec_completed_item *item)
294 struct ec_completed_node *compnode = NULL;
296 if (completed == NULL || item == NULL || item->node == NULL)
299 switch (item->type) {
303 case EC_PARTIAL_MATCH:
304 completed->count_match++; //XXX
310 /* find the compnode entry corresponding to this node */
311 TAILQ_FOREACH(compnode, &completed->nodes, next) {
312 if (compnode->node == item->node)
315 if (compnode == NULL) {
316 compnode = ec_completed_node(item->node);
317 if (compnode == NULL)
319 TAILQ_INSERT_TAIL(&completed->nodes, compnode, next);
323 TAILQ_INSERT_TAIL(&compnode->items, item, next);
328 void ec_completed_item_free(struct ec_completed_item *item)
334 ec_free(item->display);
339 /* default completion function: return a no-match element */
341 ec_node_default_complete(const struct ec_node *gen_node, // XXX rename in nomatch
342 struct ec_completed *completed,
343 struct ec_parsed *parsed_state,
344 const struct ec_strvec *strvec)
346 struct ec_completed_item *item = NULL;
349 if (ec_strvec_len(strvec) != 1)
352 item = ec_completed_item(parsed_state, gen_node);
355 ret = ec_completed_item_set(item, EC_NO_MATCH, NULL);
357 ec_completed_item_free(item);
360 ret = ec_completed_item_add(completed, item);
362 ec_completed_item_free(item);
369 void ec_completed_free(struct ec_completed *completed)
371 struct ec_completed_node *compnode;
372 struct ec_completed_item *item;
374 if (completed == NULL)
377 while (!TAILQ_EMPTY(&completed->nodes)) {
378 compnode = TAILQ_FIRST(&completed->nodes);
379 TAILQ_REMOVE(&completed->nodes, compnode, next);
381 while (!TAILQ_EMPTY(&compnode->items)) {
382 item = TAILQ_FIRST(&compnode->items);
383 TAILQ_REMOVE(&compnode->items, item, next);
384 ec_completed_item_free(item);
391 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
393 struct ec_completed_node *compnode;
394 struct ec_completed_item *item;
396 if (completed == NULL || completed->count == 0) {
397 fprintf(out, "no completion\n");
401 fprintf(out, "completion: count=%u match=%u\n",
402 completed->count, completed->count_match);
404 TAILQ_FOREACH(compnode, &completed->nodes, next) {
405 fprintf(out, "node=%p, node_type=%s\n",
406 compnode->node, compnode->node->type->name);
407 TAILQ_FOREACH(item, &compnode->items, next) {
410 switch (item->type) {
411 case EC_NO_MATCH: typestr = "no-match"; break;
412 case EC_MATCH: typestr = "match"; break;
413 case EC_PARTIAL_MATCH: typestr = "partial-match"; break;
414 default: typestr = "unknown"; break;
417 fprintf(out, " type=%s str=<%s> disp=<%s>\n",
418 typestr, item->str, item->display);
423 unsigned int ec_completed_count(
424 const struct ec_completed *completed,
425 enum ec_completed_type type)
427 unsigned int count = 0;
429 if (completed == NULL)
433 count += completed->count_match;
434 if (type & EC_NO_MATCH)
435 count += (completed->count - completed->count_match); //XXX
440 struct ec_completed_iter *
441 ec_completed_iter(struct ec_completed *completed,
442 enum ec_completed_type type)
444 struct ec_completed_iter *iter;
446 iter = ec_calloc(1, sizeof(*iter));
450 iter->completed = completed;
452 iter->cur_node = NULL;
453 iter->cur_match = NULL;
458 const struct ec_completed_item *ec_completed_iter_next(
459 struct ec_completed_iter *iter)
461 const struct ec_completed *completed = iter->completed;
462 const struct ec_completed_node *cur_node;
463 const struct ec_completed_item *cur_match;
465 if (completed == NULL)
468 cur_node = iter->cur_node;
469 cur_match = iter->cur_match;
472 if (cur_node == NULL) {
473 TAILQ_FOREACH(cur_node, &completed->nodes, next) {
474 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
475 if (cur_match != NULL &&
476 cur_match->type & iter->type)
482 cur_match = TAILQ_NEXT(cur_match, next);
483 if (cur_match != NULL &&
484 cur_match->type & iter->type)
486 cur_node = TAILQ_NEXT(cur_node, next);
487 while (cur_node != NULL) {
488 cur_match = TAILQ_FIRST(&cur_node->items);
489 if (cur_match != NULL &&
490 cur_match->type & iter->type)
492 cur_node = TAILQ_NEXT(cur_node, next);
498 iter->cur_node = cur_node;
499 iter->cur_match = cur_match;
501 return iter->cur_match;
504 void ec_completed_iter_free(struct ec_completed_iter *iter)