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 {
41 TAILQ_ENTRY(ec_comp_group) next;
42 const struct ec_node *node;
43 struct ec_comp_item_list items;
44 struct ec_pnode *state;
45 struct ec_dict *attrs;
48 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
53 unsigned count_partial;
54 unsigned count_unknown;
55 struct ec_pnode *cur_state;
56 struct ec_comp_group *cur_group;
57 struct ec_comp_group_list groups;
58 struct ec_dict *attrs;
61 struct ec_comp *ec_comp(struct ec_pnode *state)
63 struct ec_comp *comp = NULL;
65 comp = ec_calloc(1, sizeof(*comp));
69 comp->attrs = ec_dict();
70 if (comp->attrs == NULL)
73 TAILQ_INIT(&comp->groups);
75 comp->cur_state = state;
81 ec_dict_free(comp->attrs);
87 struct ec_pnode *ec_comp_get_state(const struct ec_comp *comp)
89 return comp->cur_state;
92 struct ec_comp_group *ec_comp_get_group(const struct ec_comp *comp)
94 return comp->cur_group;
97 struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp)
103 ec_complete_child(const struct ec_node *node,
104 struct ec_comp *comp,
105 const struct ec_strvec *strvec)
107 struct ec_pnode *child_state, *cur_state;
108 struct ec_comp_group *cur_group;
111 if (ec_node_type(node)->complete == NULL) {
116 /* save previous parse state, prepare child state */
117 cur_state = comp->cur_state;
118 child_state = ec_pnode(node);
119 if (child_state == NULL)
122 if (cur_state != NULL)
123 ec_pnode_link_child(cur_state, child_state);
124 comp->cur_state = child_state;
125 cur_group = comp->cur_group;
126 comp->cur_group = NULL;
128 /* fill the comp struct with items */
129 ret = ec_node_type(node)->complete(node, comp, strvec);
131 /* restore parent parse state */
132 if (cur_state != NULL) {
133 ec_pnode_unlink_child(cur_state, child_state);
134 assert(!ec_pnode_has_child(child_state));
136 ec_pnode_free(child_state);
137 comp->cur_state = cur_state;
138 comp->cur_group = cur_group;
146 struct ec_comp *ec_complete_strvec(const struct ec_node *node,
147 const struct ec_strvec *strvec)
149 struct ec_comp *comp = NULL;
152 comp = ec_comp(NULL);
156 ret = ec_complete_child(node, comp, strvec);
167 struct ec_comp *ec_complete(const struct ec_node *node,
170 struct ec_strvec *strvec = NULL;
171 struct ec_comp *comp;
174 strvec = ec_strvec();
178 if (ec_strvec_add(strvec, str) < 0)
181 comp = ec_complete_strvec(node, strvec);
185 ec_strvec_free(strvec);
189 ec_strvec_free(strvec);
193 static struct ec_comp_group *
194 ec_comp_group(const struct ec_node *node, struct ec_pnode *parse)
196 struct ec_comp_group *grp = NULL;
198 grp = ec_calloc(1, sizeof(*grp));
202 grp->attrs = ec_dict();
203 if (grp->attrs == NULL)
206 grp->state = ec_pnode_dup(parse);
207 if (grp->state == NULL)
211 TAILQ_INIT(&grp->items);
217 ec_pnode_free(grp->state);
218 ec_dict_free(grp->attrs);
224 static struct ec_comp_item *
225 ec_comp_item(enum ec_comp_type type,
226 const char *start, const char *full)
228 struct ec_comp_item *item = NULL;
229 struct ec_dict *attrs = NULL;
230 char *comp_cp = NULL, *start_cp = NULL;
231 char *full_cp = NULL, *display_cp = NULL;
233 if (type == EC_COMP_UNKNOWN && full != NULL) {
237 if (type != EC_COMP_UNKNOWN && full == NULL) {
242 item = ec_calloc(1, sizeof(*item));
251 start_cp = ec_strdup(start);
252 if (start_cp == NULL)
255 if (ec_str_startswith(full, start)) {
256 comp_cp = ec_strdup(&full[strlen(start)]);
262 full_cp = ec_strdup(full);
265 display_cp = ec_strdup(full);
266 if (display_cp == NULL)
271 item->start = start_cp;
272 item->full = full_cp;
273 item->completion = comp_cp;
274 item->display = display_cp;
290 int ec_comp_item_set_display(struct ec_comp_item *item,
293 char *display_copy = NULL;
295 if (item == NULL || display == NULL ||
296 item->type == EC_COMP_UNKNOWN) {
301 display_copy = ec_strdup(display);
302 if (display_copy == NULL)
305 ec_free(item->display);
306 item->display = display_copy;
311 ec_free(display_copy);
316 ec_comp_item_set_completion(struct ec_comp_item *item,
317 const char *completion)
319 char *completion_copy = NULL;
321 if (item == NULL || completion == NULL ||
322 item->type == EC_COMP_UNKNOWN) {
327 completion_copy = ec_strdup(completion);
328 if (completion_copy == NULL)
331 ec_free(item->completion);
332 item->completion = completion_copy;
337 ec_free(completion_copy);
342 ec_comp_item_set_str(struct ec_comp_item *item,
345 char *str_copy = NULL;
347 if (item == NULL || str == NULL ||
348 item->type == EC_COMP_UNKNOWN) {
353 str_copy = ec_strdup(str);
354 if (str_copy == NULL)
358 item->full = str_copy;
368 ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
369 struct ec_comp_item *item)
371 if (comp == NULL || item == NULL) {
376 switch (item->type) {
377 case EC_COMP_UNKNOWN:
378 comp->count_unknown++;
383 case EC_COMP_PARTIAL:
384 comp->count_partial++;
391 if (comp->cur_group == NULL) {
392 struct ec_comp_group *grp;
394 grp = ec_comp_group(node, comp->cur_state);
397 TAILQ_INSERT_TAIL(&comp->groups, grp, next);
398 comp->cur_group = grp;
402 TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next);
403 item->grp = comp->cur_group;
409 ec_comp_item_get_str(const struct ec_comp_item *item)
415 ec_comp_item_get_display(const struct ec_comp_item *item)
417 return item->display;
421 ec_comp_item_get_completion(const struct ec_comp_item *item)
423 return item->completion;
427 ec_comp_item_get_type(const struct ec_comp_item *item)
432 const struct ec_comp_group *
433 ec_comp_item_get_grp(const struct ec_comp_item *item)
438 const struct ec_node *
439 ec_comp_item_get_node(const struct ec_comp_item *item)
441 return ec_comp_item_get_grp(item)->node;
445 ec_comp_item_free(struct ec_comp_item *item)
451 ec_free(item->start);
452 ec_free(item->completion);
453 ec_free(item->display);
454 ec_dict_free(item->attrs);
458 int ec_comp_add_item(struct ec_comp *comp,
459 const struct ec_node *node,
460 struct ec_comp_item **p_item,
461 enum ec_comp_type type,
462 const char *start, const char *full)
464 struct ec_comp_item *item = NULL;
467 item = ec_comp_item(type, start, full);
471 ret = ec_comp_item_add(comp, node, item);
481 ec_comp_item_free(item);
486 /* return a completion item of type "unknown" */
488 ec_complete_unknown(const struct ec_node *gen_node,
489 struct ec_comp *comp,
490 const struct ec_strvec *strvec)
494 if (ec_strvec_len(strvec) != 1)
497 ret = ec_comp_add_item(comp, gen_node, NULL,
498 EC_COMP_UNKNOWN, NULL, NULL);
505 static void ec_comp_group_free(struct ec_comp_group *grp)
507 struct ec_comp_item *item;
512 while (!TAILQ_EMPTY(&grp->items)) {
513 item = TAILQ_FIRST(&grp->items);
514 TAILQ_REMOVE(&grp->items, item, next);
515 ec_comp_item_free(item);
517 ec_pnode_free(ec_pnode_get_root(grp->state));
518 ec_dict_free(grp->attrs);
522 const struct ec_node *
523 ec_comp_group_get_node(const struct ec_comp_group *grp)
528 const struct ec_pnode *
529 ec_comp_group_get_state(const struct ec_comp_group *grp)
534 const struct ec_dict *
535 ec_comp_group_get_attrs(const struct ec_comp_group *grp)
540 void ec_comp_free(struct ec_comp *comp)
542 struct ec_comp_group *grp;
547 while (!TAILQ_EMPTY(&comp->groups)) {
548 grp = TAILQ_FIRST(&comp->groups);
549 TAILQ_REMOVE(&comp->groups, grp, next);
550 ec_comp_group_free(grp);
552 ec_dict_free(comp->attrs);
556 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
558 struct ec_comp_group *grp;
559 struct ec_comp_item *item;
561 if (comp == NULL || comp->count == 0) {
562 fprintf(out, "no completion\n");
566 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
567 comp->count, comp->count_full,
568 comp->count_partial, comp->count_unknown);
570 TAILQ_FOREACH(grp, &comp->groups, next) {
571 fprintf(out, "node=%p, node_type=%s\n",
572 grp->node, ec_node_type(grp->node)->name);
573 TAILQ_FOREACH(item, &grp->items, next) {
576 switch (item->type) {
577 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
578 case EC_COMP_FULL: typestr = "full"; break;
579 case EC_COMP_PARTIAL: typestr = "partial"; break;
580 default: typestr = "unknown"; break;
583 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
584 typestr, item->full, item->completion,
590 int ec_comp_merge(struct ec_comp *to,
591 struct ec_comp *from)
593 struct ec_comp_group *grp;
595 while (!TAILQ_EMPTY(&from->groups)) {
596 grp = TAILQ_FIRST(&from->groups);
597 TAILQ_REMOVE(&from->groups, grp, next);
598 TAILQ_INSERT_TAIL(&to->groups, grp, next);
600 to->count += from->count;
601 to->count_full += from->count_full;
602 to->count_partial += from->count_partial;
603 to->count_unknown += from->count_unknown;
609 unsigned int ec_comp_count(
610 const struct ec_comp *comp,
611 enum ec_comp_type type)
613 unsigned int count = 0;
618 if (type & EC_COMP_FULL)
619 count += comp->count_full;
620 if (type & EC_COMP_PARTIAL)
621 count += comp->count_partial;
622 if (type & EC_COMP_UNKNOWN)
623 count += comp->count_unknown;
628 struct ec_comp_iter *
629 ec_comp_iter(const struct ec_comp *comp,
630 enum ec_comp_type type)
632 struct ec_comp_iter *iter;
634 iter = ec_calloc(1, sizeof(*iter));
640 iter->cur_node = NULL;
641 iter->cur_match = NULL;
646 struct ec_comp_item *ec_comp_iter_next(
647 struct ec_comp_iter *iter)
649 const struct ec_comp *comp;
650 struct ec_comp_group *cur_node;
651 struct ec_comp_item *cur_match;
659 cur_node = iter->cur_node;
660 cur_match = iter->cur_match;
663 if (cur_node == NULL) {
664 TAILQ_FOREACH(cur_node, &comp->groups, next) {
665 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
666 if (cur_match != NULL &&
667 cur_match->type & iter->type)
673 cur_match = TAILQ_NEXT(cur_match, next);
674 if (cur_match != NULL &&
675 cur_match->type & iter->type)
677 cur_node = TAILQ_NEXT(cur_node, next);
678 while (cur_node != NULL) {
679 cur_match = TAILQ_FIRST(&cur_node->items);
680 if (cur_match != NULL &&
681 cur_match->type & iter->type)
683 cur_node = TAILQ_NEXT(cur_node, next);
689 iter->cur_node = cur_node;
690 iter->cur_match = cur_match;
692 return iter->cur_match;
695 void ec_comp_iter_free(struct ec_comp_iter *iter)
700 /* LCOV_EXCL_START */
701 static int ec_comp_testcase(void)
703 struct ec_node *node = NULL;
704 struct ec_comp *c = NULL;
705 struct ec_comp_iter *iter = NULL;
706 struct ec_comp_item *item;
712 node = ec_node_sh_lex(EC_NO_ID,
714 ec_node_str("id_x", "xx"),
715 ec_node_str("id_y", "yy")));
719 c = ec_complete(node, "xcdscds");
720 testres |= EC_TEST_CHECK(
721 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
722 "complete count should is not 0\n");
725 c = ec_complete(node, "x");
726 testres |= EC_TEST_CHECK(
727 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
728 "complete count should is not 1\n");
731 c = ec_complete(node, "");
732 testres |= EC_TEST_CHECK(
733 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
734 "complete count should is not 2\n");
736 f = open_memstream(&buf, &buflen);
739 ec_comp_dump(f, NULL);
743 testres |= EC_TEST_CHECK(
744 strstr(buf, "no completion"), "bad dump\n");
748 f = open_memstream(&buf, &buflen);
755 testres |= EC_TEST_CHECK(
756 strstr(buf, "comp=<xx>"), "bad dump\n");
757 testres |= EC_TEST_CHECK(
758 strstr(buf, "comp=<yy>"), "bad dump\n");
762 iter = ec_comp_iter(c, EC_COMP_ALL);
763 item = ec_comp_iter_next(iter);
767 testres |= EC_TEST_CHECK(
768 !strcmp(ec_comp_item_get_display(item), "xx"),
769 "bad item display\n");
770 testres |= EC_TEST_CHECK(
771 ec_comp_item_get_type(item) == EC_COMP_FULL,
773 testres |= EC_TEST_CHECK(
774 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
777 item = ec_comp_iter_next(iter);
781 testres |= EC_TEST_CHECK(
782 !strcmp(ec_comp_item_get_display(item), "yy"),
783 "bad item display\n");
784 testres |= EC_TEST_CHECK(
785 ec_comp_item_get_type(item) == EC_COMP_FULL,
787 testres |= EC_TEST_CHECK(
788 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
791 item = ec_comp_iter_next(iter);
792 testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
794 ec_comp_iter_free(iter);
801 ec_comp_iter_free(iter);
812 static struct ec_test ec_comp_test = {
814 .test = ec_comp_testcase,
817 EC_TEST_REGISTER(ec_comp_test);