1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
11 #include <ecoli_malloc.h>
12 #include <ecoli_string.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_dict.h>
15 #include <ecoli_log.h>
16 #include <ecoli_test.h>
17 #include <ecoli_node.h>
18 #include <ecoli_parse.h>
19 #include <ecoli_node_sh_lex.h>
20 #include <ecoli_node_str.h>
21 #include <ecoli_node_or.h>
22 #include <ecoli_complete.h>
24 EC_LOG_TYPE_REGISTER(comp);
27 TAILQ_ENTRY(ec_comp_item) next;
28 enum ec_comp_type type;
29 struct ec_comp_group *grp;
30 char *start; /* the initial token */
31 char *full; /* the full token after completion */
32 char *completion; /* chars that are added, NULL if not applicable */
33 char *display; /* what should be displayed by help/completers */
34 struct ec_dict *attrs;
37 TAILQ_HEAD(ec_comp_item_list, ec_comp_item);
39 struct ec_comp_group {
40 TAILQ_ENTRY(ec_comp_group) next;
41 const struct ec_node *node;
42 struct ec_comp_item_list items;
43 struct ec_parse *state;
44 struct ec_dict *attrs;
47 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
52 unsigned count_partial;
53 unsigned count_unknown;
54 struct ec_parse *cur_state;
55 struct ec_comp_group *cur_group;
56 struct ec_comp_group_list groups;
57 struct ec_dict *attrs;
60 struct ec_comp *ec_comp(struct ec_parse *state)
62 struct ec_comp *comp = NULL;
64 comp = ec_calloc(1, sizeof(*comp));
68 comp->attrs = ec_dict();
69 if (comp->attrs == NULL)
72 TAILQ_INIT(&comp->groups);
74 comp->cur_state = state;
80 ec_dict_free(comp->attrs);
86 struct ec_parse *ec_comp_get_state(const struct ec_comp *comp)
88 return comp->cur_state;
91 struct ec_comp_group *ec_comp_get_group(const struct ec_comp *comp)
93 return comp->cur_group;
96 struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp)
102 ec_node_complete_child(const struct ec_node *node,
103 struct ec_comp *comp,
104 const struct ec_strvec *strvec)
106 struct ec_parse *child_state, *cur_state;
107 struct ec_comp_group *cur_group;
110 if (ec_node_type(node)->complete == NULL) {
115 /* save previous parse state, prepare child state */
116 cur_state = comp->cur_state;
117 child_state = ec_parse(node);
118 if (child_state == NULL)
121 if (cur_state != NULL)
122 ec_parse_link_child(cur_state, child_state);
123 comp->cur_state = child_state;
124 cur_group = comp->cur_group;
125 comp->cur_group = NULL;
127 /* fill the comp struct with items */
128 ret = ec_node_type(node)->complete(node, comp, strvec);
130 /* restore parent parse state */
131 if (cur_state != NULL) {
132 ec_parse_unlink_child(cur_state, child_state);
133 assert(!ec_parse_has_child(child_state));
135 ec_parse_free(child_state);
136 comp->cur_state = cur_state;
137 comp->cur_group = cur_group;
145 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
146 const struct ec_strvec *strvec)
148 struct ec_comp *comp = NULL;
151 comp = ec_comp(NULL);
155 ret = ec_node_complete_child(node, comp, strvec);
166 struct ec_comp *ec_node_complete(const struct ec_node *node,
169 struct ec_strvec *strvec = NULL;
170 struct ec_comp *comp;
173 strvec = ec_strvec();
177 if (ec_strvec_add(strvec, str) < 0)
180 comp = ec_node_complete_strvec(node, strvec);
184 ec_strvec_free(strvec);
188 ec_strvec_free(strvec);
192 static struct ec_comp_group *
193 ec_comp_group(const struct ec_node *node, struct ec_parse *parse)
195 struct ec_comp_group *grp = NULL;
197 grp = ec_calloc(1, sizeof(*grp));
201 grp->attrs = ec_dict();
202 if (grp->attrs == NULL)
205 grp->state = ec_parse_dup(parse);
206 if (grp->state == NULL)
210 TAILQ_INIT(&grp->items);
216 ec_parse_free(grp->state);
217 ec_dict_free(grp->attrs);
223 static struct ec_comp_item *
224 ec_comp_item(enum ec_comp_type type,
225 const char *start, const char *full)
227 struct ec_comp_item *item = NULL;
228 struct ec_dict *attrs = NULL;
229 char *comp_cp = NULL, *start_cp = NULL;
230 char *full_cp = NULL, *display_cp = NULL;
232 if (type == EC_COMP_UNKNOWN && full != NULL) {
236 if (type != EC_COMP_UNKNOWN && full == NULL) {
241 item = ec_calloc(1, sizeof(*item));
250 start_cp = ec_strdup(start);
251 if (start_cp == NULL)
254 if (ec_str_startswith(full, start)) {
255 comp_cp = ec_strdup(&full[strlen(start)]);
261 full_cp = ec_strdup(full);
264 display_cp = ec_strdup(full);
265 if (display_cp == NULL)
270 item->start = start_cp;
271 item->full = full_cp;
272 item->completion = comp_cp;
273 item->display = display_cp;
289 int ec_comp_item_set_display(struct ec_comp_item *item,
292 char *display_copy = NULL;
294 if (item == NULL || display == NULL ||
295 item->type == EC_COMP_UNKNOWN) {
300 display_copy = ec_strdup(display);
301 if (display_copy == NULL)
304 ec_free(item->display);
305 item->display = display_copy;
310 ec_free(display_copy);
315 ec_comp_item_set_completion(struct ec_comp_item *item,
316 const char *completion)
318 char *completion_copy = NULL;
320 if (item == NULL || completion == NULL ||
321 item->type == EC_COMP_UNKNOWN) {
326 completion_copy = ec_strdup(completion);
327 if (completion_copy == NULL)
330 ec_free(item->completion);
331 item->completion = completion_copy;
336 ec_free(completion_copy);
341 ec_comp_item_set_str(struct ec_comp_item *item,
344 char *str_copy = NULL;
346 if (item == NULL || str == NULL ||
347 item->type == EC_COMP_UNKNOWN) {
352 str_copy = ec_strdup(str);
353 if (str_copy == NULL)
357 item->full = str_copy;
367 ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
368 struct ec_comp_item *item)
370 if (comp == NULL || item == NULL) {
375 switch (item->type) {
376 case EC_COMP_UNKNOWN:
377 comp->count_unknown++;
382 case EC_COMP_PARTIAL:
383 comp->count_partial++;
390 if (comp->cur_group == NULL) {
391 struct ec_comp_group *grp;
393 grp = ec_comp_group(node, comp->cur_state);
396 TAILQ_INSERT_TAIL(&comp->groups, grp, next);
397 comp->cur_group = grp;
401 TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next);
402 item->grp = comp->cur_group;
408 ec_comp_item_get_str(const struct ec_comp_item *item)
414 ec_comp_item_get_display(const struct ec_comp_item *item)
416 return item->display;
420 ec_comp_item_get_completion(const struct ec_comp_item *item)
422 return item->completion;
426 ec_comp_item_get_type(const struct ec_comp_item *item)
431 const struct ec_comp_group *
432 ec_comp_item_get_grp(const struct ec_comp_item *item)
437 const struct ec_node *
438 ec_comp_item_get_node(const struct ec_comp_item *item)
440 return ec_comp_item_get_grp(item)->node;
444 ec_comp_item_free(struct ec_comp_item *item)
450 ec_free(item->start);
451 ec_free(item->completion);
452 ec_free(item->display);
453 ec_dict_free(item->attrs);
457 int ec_comp_add_item(struct ec_comp *comp,
458 const struct ec_node *node,
459 struct ec_comp_item **p_item,
460 enum ec_comp_type type,
461 const char *start, const char *full)
463 struct ec_comp_item *item = NULL;
466 item = ec_comp_item(type, start, full);
470 ret = ec_comp_item_add(comp, node, item);
480 ec_comp_item_free(item);
485 /* return a completion item of type "unknown" */
487 ec_node_complete_unknown(const struct ec_node *gen_node,
488 struct ec_comp *comp,
489 const struct ec_strvec *strvec)
493 if (ec_strvec_len(strvec) != 1)
496 ret = ec_comp_add_item(comp, gen_node, NULL,
497 EC_COMP_UNKNOWN, NULL, NULL);
504 static void ec_comp_group_free(struct ec_comp_group *grp)
506 struct ec_comp_item *item;
511 while (!TAILQ_EMPTY(&grp->items)) {
512 item = TAILQ_FIRST(&grp->items);
513 TAILQ_REMOVE(&grp->items, item, next);
514 ec_comp_item_free(item);
516 ec_parse_free(ec_parse_get_root(grp->state));
517 ec_dict_free(grp->attrs);
521 const struct ec_node *
522 ec_comp_group_get_node(const struct ec_comp_group *grp)
527 const struct ec_parse *
528 ec_comp_group_get_state(const struct ec_comp_group *grp)
533 const struct ec_dict *
534 ec_comp_group_get_attrs(const struct ec_comp_group *grp)
539 void ec_comp_free(struct ec_comp *comp)
541 struct ec_comp_group *grp;
546 while (!TAILQ_EMPTY(&comp->groups)) {
547 grp = TAILQ_FIRST(&comp->groups);
548 TAILQ_REMOVE(&comp->groups, grp, next);
549 ec_comp_group_free(grp);
551 ec_dict_free(comp->attrs);
555 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
557 struct ec_comp_group *grp;
558 struct ec_comp_item *item;
560 if (comp == NULL || comp->count == 0) {
561 fprintf(out, "no completion\n");
565 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
566 comp->count, comp->count_full,
567 comp->count_partial, comp->count_unknown);
569 TAILQ_FOREACH(grp, &comp->groups, next) {
570 fprintf(out, "node=%p, node_type=%s\n",
571 grp->node, ec_node_type(grp->node)->name);
572 TAILQ_FOREACH(item, &grp->items, next) {
575 switch (item->type) {
576 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
577 case EC_COMP_FULL: typestr = "full"; break;
578 case EC_COMP_PARTIAL: typestr = "partial"; break;
579 default: typestr = "unknown"; break;
582 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
583 typestr, item->full, item->completion,
589 int ec_comp_merge(struct ec_comp *to,
590 struct ec_comp *from)
592 struct ec_comp_group *grp;
594 while (!TAILQ_EMPTY(&from->groups)) {
595 grp = TAILQ_FIRST(&from->groups);
596 TAILQ_REMOVE(&from->groups, grp, next);
597 TAILQ_INSERT_TAIL(&to->groups, grp, next);
599 to->count += from->count;
600 to->count_full += from->count_full;
601 to->count_partial += from->count_partial;
602 to->count_unknown += from->count_unknown;
608 unsigned int ec_comp_count(
609 const struct ec_comp *comp,
610 enum ec_comp_type type)
612 unsigned int count = 0;
617 if (type & EC_COMP_FULL)
618 count += comp->count_full;
619 if (type & EC_COMP_PARTIAL)
620 count += comp->count_partial;
621 if (type & EC_COMP_UNKNOWN)
622 count += comp->count_unknown;
627 struct ec_comp_iter *
628 ec_comp_iter(const struct ec_comp *comp,
629 enum ec_comp_type type)
631 struct ec_comp_iter *iter;
633 iter = ec_calloc(1, sizeof(*iter));
639 iter->cur_node = NULL;
640 iter->cur_match = NULL;
645 struct ec_comp_item *ec_comp_iter_next(
646 struct ec_comp_iter *iter)
648 const struct ec_comp *comp;
649 struct ec_comp_group *cur_node;
650 struct ec_comp_item *cur_match;
658 cur_node = iter->cur_node;
659 cur_match = iter->cur_match;
662 if (cur_node == NULL) {
663 TAILQ_FOREACH(cur_node, &comp->groups, next) {
664 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
665 if (cur_match != NULL &&
666 cur_match->type & iter->type)
672 cur_match = TAILQ_NEXT(cur_match, next);
673 if (cur_match != NULL &&
674 cur_match->type & iter->type)
676 cur_node = TAILQ_NEXT(cur_node, next);
677 while (cur_node != NULL) {
678 cur_match = TAILQ_FIRST(&cur_node->items);
679 if (cur_match != NULL &&
680 cur_match->type & iter->type)
682 cur_node = TAILQ_NEXT(cur_node, next);
688 iter->cur_node = cur_node;
689 iter->cur_match = cur_match;
691 return iter->cur_match;
694 void ec_comp_iter_free(struct ec_comp_iter *iter)
699 /* LCOV_EXCL_START */
700 static int ec_comp_testcase(void)
702 struct ec_node *node = NULL;
703 struct ec_comp *c = NULL;
704 struct ec_comp_iter *iter = NULL;
705 struct ec_comp_item *item;
711 node = ec_node_sh_lex(EC_NO_ID,
713 ec_node_str("id_x", "xx"),
714 ec_node_str("id_y", "yy")));
718 c = ec_node_complete(node, "xcdscds");
719 testres |= EC_TEST_CHECK(
720 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
721 "complete count should is not 0\n");
724 c = ec_node_complete(node, "x");
725 testres |= EC_TEST_CHECK(
726 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
727 "complete count should is not 1\n");
730 c = ec_node_complete(node, "");
731 testres |= EC_TEST_CHECK(
732 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
733 "complete count should is not 2\n");
735 f = open_memstream(&buf, &buflen);
738 ec_comp_dump(f, NULL);
742 testres |= EC_TEST_CHECK(
743 strstr(buf, "no completion"), "bad dump\n");
747 f = open_memstream(&buf, &buflen);
754 testres |= EC_TEST_CHECK(
755 strstr(buf, "comp=<xx>"), "bad dump\n");
756 testres |= EC_TEST_CHECK(
757 strstr(buf, "comp=<yy>"), "bad dump\n");
761 iter = ec_comp_iter(c, EC_COMP_ALL);
762 item = ec_comp_iter_next(iter);
766 testres |= EC_TEST_CHECK(
767 !strcmp(ec_comp_item_get_display(item), "xx"),
768 "bad item display\n");
769 testres |= EC_TEST_CHECK(
770 ec_comp_item_get_type(item) == EC_COMP_FULL,
772 testres |= EC_TEST_CHECK(
773 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
776 item = ec_comp_iter_next(iter);
780 testres |= EC_TEST_CHECK(
781 !strcmp(ec_comp_item_get_display(item), "yy"),
782 "bad item display\n");
783 testres |= EC_TEST_CHECK(
784 ec_comp_item_get_type(item) == EC_COMP_FULL,
786 testres |= EC_TEST_CHECK(
787 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
790 item = ec_comp_iter_next(iter);
791 testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
793 ec_comp_iter_free(iter);
800 ec_comp_iter_free(iter);
811 static struct ec_test ec_comp_test = {
813 .test = ec_comp_testcase,
816 EC_TEST_REGISTER(ec_comp_test);