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:
358 completed->count_unknown++;
361 completed->count_full++;
363 case EC_COMP_PARTIAL:
364 completed->count_partial++;
370 if (completed->cur_group == NULL) {
371 struct ec_completed_group *grp;
373 grp = ec_completed_group(item->node, completed->cur_state);
376 TAILQ_INSERT_TAIL(&completed->groups, grp, next);
377 completed->cur_group = grp;
381 TAILQ_INSERT_TAIL(&completed->cur_group->items, item, next);
382 item->grp = completed->cur_group;
388 ec_completed_item_get_str(const struct ec_completed_item *item)
394 ec_completed_item_get_display(const struct ec_completed_item *item)
396 return item->display;
400 ec_completed_item_get_completion(const struct ec_completed_item *item)
402 return item->completion;
405 enum ec_completed_type
406 ec_completed_item_get_type(const struct ec_completed_item *item)
411 const struct ec_node *
412 ec_completed_item_get_node(const struct ec_completed_item *item)
417 const struct ec_completed_group *
418 ec_completed_item_get_grp(const struct ec_completed_item *item)
424 ec_completed_item_free(struct ec_completed_item *item)
430 ec_free(item->start);
431 ec_free(item->completion);
432 ec_free(item->display);
433 ec_keyval_free(item->attrs);
437 int ec_completed_add_item(struct ec_completed *completed,
438 const struct ec_node *node,
439 struct ec_completed_item **p_item,
440 enum ec_completed_type type,
441 const char *start, const char *full)
443 struct ec_completed_item *item = NULL;
446 item = ec_completed_item(node, type, start, full);
450 ret = ec_completed_item_add(completed, item);
460 ec_completed_item_free(item);
465 /* default completion function: return a no-match element */
467 ec_node_default_complete(const struct ec_node *gen_node, // XXX rename in nomatch
468 struct ec_completed *completed,
469 const struct ec_strvec *strvec)
473 if (ec_strvec_len(strvec) != 1)
476 ret = ec_completed_add_item(completed, gen_node, NULL,
477 EC_COMP_UNKNOWN, NULL, NULL);
484 static void ec_completed_group_free(struct ec_completed_group *grp)
486 struct ec_completed_item *item;
491 while (!TAILQ_EMPTY(&grp->items)) {
492 item = TAILQ_FIRST(&grp->items);
493 TAILQ_REMOVE(&grp->items, item, next);
494 ec_completed_item_free(item);
496 ec_parsed_free(ec_parsed_get_root(grp->state));
497 ec_keyval_free(grp->attrs);
501 void ec_completed_free(struct ec_completed *completed)
503 struct ec_completed_group *grp;
505 if (completed == NULL)
508 while (!TAILQ_EMPTY(&completed->groups)) {
509 grp = TAILQ_FIRST(&completed->groups);
510 TAILQ_REMOVE(&completed->groups, grp, next);
511 ec_completed_group_free(grp);
513 ec_keyval_free(completed->attrs);
517 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
519 struct ec_completed_group *grp;
520 struct ec_completed_item *item;
522 if (completed == NULL || completed->count == 0) {
523 fprintf(out, "no completion\n");
527 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
528 completed->count, completed->count_full,
529 completed->count_partial, completed->count_unknown);
531 TAILQ_FOREACH(grp, &completed->groups, next) {
532 fprintf(out, "node=%p, node_type=%s\n",
533 grp->node, grp->node->type->name);
534 TAILQ_FOREACH(item, &grp->items, next) {
537 switch (item->type) {
538 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
539 case EC_COMP_FULL: typestr = "full"; break;
540 case EC_COMP_PARTIAL: typestr = "partial"; break;
541 default: typestr = "unknown"; break;
544 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
545 typestr, item->full, item->completion,
551 int ec_completed_merge(struct ec_completed *to,
552 struct ec_completed *from)
554 struct ec_completed_group *grp;
556 while (!TAILQ_EMPTY(&from->groups)) {
557 grp = TAILQ_FIRST(&from->groups);
558 TAILQ_REMOVE(&from->groups, grp, next);
559 TAILQ_INSERT_TAIL(&to->groups, grp, next);
561 to->count += from->count;
562 to->count_full += from->count_full;
563 to->count_partial += from->count_partial;
564 to->count_unknown += from->count_unknown;
566 ec_completed_free(from);
570 unsigned int ec_completed_count(
571 const struct ec_completed *completed,
572 enum ec_completed_type type)
574 unsigned int count = 0;
576 if (completed == NULL)
579 if (type & EC_COMP_FULL)
580 count += completed->count_full;
581 if (type & EC_COMP_PARTIAL)
582 count += completed->count_partial;
583 if (type & EC_COMP_UNKNOWN)
584 count += completed->count_unknown;
589 struct ec_completed_iter *
590 ec_completed_iter(struct ec_completed *completed,
591 enum ec_completed_type type)
593 struct ec_completed_iter *iter;
595 iter = ec_calloc(1, sizeof(*iter));
599 iter->completed = completed;
601 iter->cur_node = NULL;
602 iter->cur_match = NULL;
607 struct ec_completed_item *ec_completed_iter_next(
608 struct ec_completed_iter *iter)
610 struct ec_completed *completed;
611 struct ec_completed_group *cur_node;
612 struct ec_completed_item *cur_match;
616 completed = iter->completed;
617 if (completed == NULL)
620 cur_node = iter->cur_node;
621 cur_match = iter->cur_match;
624 if (cur_node == NULL) {
625 TAILQ_FOREACH(cur_node, &completed->groups, next) {
626 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
627 if (cur_match != NULL &&
628 cur_match->type & iter->type)
634 cur_match = TAILQ_NEXT(cur_match, next);
635 if (cur_match != NULL &&
636 cur_match->type & iter->type)
638 cur_node = TAILQ_NEXT(cur_node, next);
639 while (cur_node != NULL) {
640 cur_match = TAILQ_FIRST(&cur_node->items);
641 if (cur_match != NULL &&
642 cur_match->type & iter->type)
644 cur_node = TAILQ_NEXT(cur_node, next);
650 iter->cur_node = cur_node;
651 iter->cur_match = cur_match;
653 return iter->cur_match;
656 void ec_completed_iter_free(struct ec_completed_iter *iter)