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_string.h>
36 #include <ecoli_strvec.h>
37 #include <ecoli_keyval.h>
38 #include <ecoli_log.h>
39 #include <ecoli_node.h>
40 #include <ecoli_parsed.h>
41 #include <ecoli_completed.h>
43 struct ec_completed_item {
44 TAILQ_ENTRY(ec_completed_item) next;
45 enum ec_completed_type type;
46 const struct ec_node *node;
47 struct ec_completed_group *grp;
48 char *start; /* the initial token */
49 char *full; /* the full token after completion */
50 char *completion; /* chars that are added, NULL if not applicable */
51 char *display; /* what should be displayed by help/completers */
52 struct ec_keyval *attrs;
55 struct ec_completed *ec_completed(struct ec_parsed *state)
57 struct ec_completed *completed = NULL;
59 completed = ec_calloc(1, sizeof(*completed));
60 if (completed == NULL)
63 completed->attrs = ec_keyval();
64 if (completed->attrs == NULL)
67 TAILQ_INIT(&completed->groups);
69 completed->cur_state = state;
74 if (completed != NULL)
75 ec_keyval_free(completed->attrs);
81 struct ec_parsed *ec_completed_get_state(struct ec_completed *completed)
83 return completed->cur_state;
87 ec_node_complete_child(const struct ec_node *node,
88 struct ec_completed *completed,
89 const struct ec_strvec *strvec)
91 struct ec_parsed *child_state, *cur_state;
92 struct ec_completed_group *cur_group;
95 if (node->type->complete == NULL)
98 /* save previous parse state, prepare child state */
99 cur_state = completed->cur_state;
100 child_state = ec_parsed(node);
101 if (child_state == NULL)
104 if (cur_state != NULL)
105 ec_parsed_add_child(cur_state, child_state);
106 completed->cur_state = child_state;
107 cur_group = completed->cur_group;
108 completed->cur_group = NULL;
110 /* fill the completed struct with items */
111 ret = node->type->complete(node, completed, strvec);
113 /* restore parent parse state */
114 if (cur_state != NULL) {
115 ec_parsed_del_child(cur_state, child_state);
116 assert(!ec_parsed_has_child(child_state));
118 ec_parsed_free(child_state);
119 completed->cur_state = cur_state;
120 completed->cur_group = cur_group;
128 struct ec_completed *ec_node_complete_strvec(const struct ec_node *node,
129 const struct ec_strvec *strvec)
131 struct ec_completed *completed = NULL;
134 completed = ec_completed(NULL);
135 if (completed == NULL)
138 ret = ec_node_complete_child(node, completed, strvec);
145 ec_completed_free(completed);
149 struct ec_completed *ec_node_complete(const struct ec_node *node,
152 struct ec_strvec *strvec = NULL;
153 struct ec_completed *completed;
156 strvec = ec_strvec();
160 if (ec_strvec_add(strvec, str) < 0)
163 completed = ec_node_complete_strvec(node, strvec);
164 if (completed == NULL)
167 ec_strvec_free(strvec);
171 ec_strvec_free(strvec);
175 static struct ec_completed_group *
176 ec_completed_group(const struct ec_node *node, struct ec_parsed *parsed)
178 struct ec_completed_group *grp = NULL;
180 grp = ec_calloc(1, sizeof(*grp));
184 grp->attrs = ec_keyval();
185 if (grp->attrs == NULL)
188 grp->state = ec_parsed_dup(parsed);
189 if (grp->state == NULL)
193 TAILQ_INIT(&grp->items);
199 ec_parsed_free(grp->state);
200 ec_keyval_free(grp->attrs);
206 static struct ec_completed_item *
207 ec_completed_item(const struct ec_node *node, enum ec_completed_type type,
208 const char *start, const char *full)
210 struct ec_completed_item *item = NULL;
211 struct ec_keyval *attrs = NULL;
212 char *comp_cp = NULL, *start_cp = NULL;
213 char *full_cp = NULL, *display_cp = NULL;
215 if (type == EC_COMP_UNKNOWN && full != NULL) {
219 if (type != EC_COMP_UNKNOWN && full == NULL) {
224 item = ec_calloc(1, sizeof(*item));
233 start_cp = ec_strdup(start);
234 if (start_cp == NULL)
237 if (ec_str_startswith(full, start)) {
238 comp_cp = ec_strdup(&full[strlen(start)]);
244 full_cp = ec_strdup(full);
247 display_cp = ec_strdup(full);
248 if (display_cp == NULL)
254 item->start = start_cp;
255 item->full = full_cp;
256 item->completion = comp_cp;
257 item->display = display_cp;
263 ec_keyval_free(attrs);
273 int ec_completed_item_set_display(struct ec_completed_item *item,
276 char *display_copy = NULL;
279 if (item == NULL || display == NULL ||
280 item->type == EC_COMP_UNKNOWN)
283 display_copy = ec_strdup(display);
284 if (display_copy == NULL)
287 ec_free(item->display);
288 item->display = display_copy;
293 ec_free(display_copy);
298 ec_completed_item_set_completion(struct ec_completed_item *item,
299 const char *completion)
301 char *completion_copy = NULL;
304 if (item == NULL || completion == NULL ||
305 item->type == EC_COMP_UNKNOWN)
309 completion_copy = ec_strdup(completion);
310 if (completion_copy == NULL)
313 ec_free(item->completion);
314 item->completion = completion_copy;
319 ec_free(completion_copy);
324 ec_completed_item_set_str(struct ec_completed_item *item,
327 char *str_copy = NULL;
330 if (item == NULL || str == NULL ||
331 item->type == EC_COMP_UNKNOWN)
335 str_copy = ec_strdup(str);
336 if (str_copy == NULL)
340 item->full = str_copy;
350 ec_completed_item_add(struct ec_completed *completed,
351 struct ec_completed_item *item)
353 if (completed == NULL || item == NULL || item->node == NULL)
356 switch (item->type) {
357 case EC_COMP_UNKNOWN:
360 case EC_COMP_PARTIAL:
361 completed->count_match++; //XXX
367 if (completed->cur_group == NULL) {
368 struct ec_completed_group *grp;
370 grp = ec_completed_group(item->node, completed->cur_state);
373 TAILQ_INSERT_TAIL(&completed->groups, grp, next);
374 completed->cur_group = grp;
378 TAILQ_INSERT_TAIL(&completed->cur_group->items, item, next);
379 item->grp = completed->cur_group;
385 ec_completed_item_get_str(const struct ec_completed_item *item)
391 ec_completed_item_get_display(const struct ec_completed_item *item)
393 return item->display;
397 ec_completed_item_get_completion(const struct ec_completed_item *item)
399 return item->completion;
402 enum ec_completed_type
403 ec_completed_item_get_type(const struct ec_completed_item *item)
408 const struct ec_node *
409 ec_completed_item_get_node(const struct ec_completed_item *item)
414 const struct ec_completed_group *
415 ec_completed_item_get_grp(const struct ec_completed_item *item)
421 ec_completed_item_free(struct ec_completed_item *item)
427 ec_free(item->start);
428 ec_free(item->completion);
429 ec_free(item->display);
430 ec_keyval_free(item->attrs);
434 int ec_completed_add_item(struct ec_completed *completed,
435 const struct ec_node *node,
436 struct ec_completed_item **p_item,
437 enum ec_completed_type type,
438 const char *start, const char *full)
440 struct ec_completed_item *item = NULL;
443 item = ec_completed_item(node, type, start, full);
447 ret = ec_completed_item_add(completed, item);
457 ec_completed_item_free(item);
462 /* default completion function: return a no-match element */
464 ec_node_default_complete(const struct ec_node *gen_node, // XXX rename in nomatch
465 struct ec_completed *completed,
466 const struct ec_strvec *strvec)
470 if (ec_strvec_len(strvec) != 1)
473 ret = ec_completed_add_item(completed, gen_node, NULL,
474 EC_COMP_UNKNOWN, NULL, NULL);
481 static void ec_completed_group_free(struct ec_completed_group *grp)
483 struct ec_completed_item *item;
488 while (!TAILQ_EMPTY(&grp->items)) {
489 item = TAILQ_FIRST(&grp->items);
490 TAILQ_REMOVE(&grp->items, item, next);
491 ec_completed_item_free(item);
493 ec_parsed_free(ec_parsed_get_root(grp->state));
494 ec_keyval_free(grp->attrs);
498 void ec_completed_free(struct ec_completed *completed)
500 struct ec_completed_group *grp;
502 if (completed == NULL)
505 while (!TAILQ_EMPTY(&completed->groups)) {
506 grp = TAILQ_FIRST(&completed->groups);
507 TAILQ_REMOVE(&completed->groups, grp, next);
508 ec_completed_group_free(grp);
510 ec_keyval_free(completed->attrs);
514 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
516 struct ec_completed_group *grp;
517 struct ec_completed_item *item;
519 if (completed == NULL || completed->count == 0) {
520 fprintf(out, "no completion\n");
524 fprintf(out, "completion: count=%u match=%u\n",
525 completed->count, completed->count_match);
527 TAILQ_FOREACH(grp, &completed->groups, next) {
528 fprintf(out, "node=%p, node_type=%s\n",
529 grp->node, grp->node->type->name);
530 TAILQ_FOREACH(item, &grp->items, next) {
533 switch (item->type) {
534 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
535 case EC_COMP_FULL: typestr = "full"; break;
536 case EC_COMP_PARTIAL: typestr = "partial"; break;
537 default: typestr = "unknown"; break;
540 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
541 typestr, item->full, item->completion,
547 int ec_completed_merge(struct ec_completed *to,
548 struct ec_completed *from)
550 struct ec_completed_group *grp;
552 while (!TAILQ_EMPTY(&from->groups)) {
553 grp = TAILQ_FIRST(&from->groups);
554 TAILQ_REMOVE(&from->groups, grp, next);
555 TAILQ_INSERT_TAIL(&to->groups, grp, next);
557 to->count += from->count;
558 to->count_match += from->count_match;
560 ec_completed_free(from);
564 unsigned int ec_completed_count(
565 const struct ec_completed *completed,
566 enum ec_completed_type type)
568 unsigned int count = 0;
570 if (completed == NULL)
573 if (type & EC_COMP_FULL)
574 count += completed->count_match;
575 if (type & EC_COMP_UNKNOWN)
576 count += (completed->count - completed->count_match); //XXX
581 struct ec_completed_iter *
582 ec_completed_iter(struct ec_completed *completed,
583 enum ec_completed_type type)
585 struct ec_completed_iter *iter;
587 iter = ec_calloc(1, sizeof(*iter));
591 iter->completed = completed;
593 iter->cur_node = NULL;
594 iter->cur_match = NULL;
599 struct ec_completed_item *ec_completed_iter_next(
600 struct ec_completed_iter *iter)
602 struct ec_completed *completed;
603 struct ec_completed_group *cur_node;
604 struct ec_completed_item *cur_match;
608 completed = iter->completed;
609 if (completed == NULL)
612 cur_node = iter->cur_node;
613 cur_match = iter->cur_match;
616 if (cur_node == NULL) {
617 TAILQ_FOREACH(cur_node, &completed->groups, next) {
618 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
619 if (cur_match != NULL &&
620 cur_match->type & iter->type)
626 cur_match = TAILQ_NEXT(cur_match, next);
627 if (cur_match != NULL &&
628 cur_match->type & iter->type)
630 cur_node = TAILQ_NEXT(cur_node, next);
631 while (cur_node != NULL) {
632 cur_match = TAILQ_FIRST(&cur_node->items);
633 if (cur_match != NULL &&
634 cur_match->type & iter->type)
636 cur_node = TAILQ_NEXT(cur_node, next);
642 iter->cur_node = cur_node;
643 iter->cur_match = cur_match;
645 return iter->cur_match;
648 void ec_completed_iter_free(struct ec_completed_iter *iter)