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 //XXX call ec_complete_unknown() instead, as
112 //described in API doc.
113 if (ec_node_type(node)->complete == NULL) {
118 /* save previous parse state, prepare child state */
119 cur_state = comp->cur_state;
120 child_state = ec_pnode(node);
121 if (child_state == NULL)
124 if (cur_state != NULL)
125 ec_pnode_link_child(cur_state, child_state);
126 comp->cur_state = child_state;
127 cur_group = comp->cur_group;
128 comp->cur_group = NULL;
130 /* fill the comp struct with items */
131 ret = ec_node_type(node)->complete(node, comp, strvec);
133 /* restore parent parse state */
134 if (cur_state != NULL) {
135 ec_pnode_unlink_child(cur_state, child_state);
136 assert(!ec_pnode_has_child(child_state));
138 ec_pnode_free(child_state);
139 comp->cur_state = cur_state;
140 comp->cur_group = cur_group;
148 struct ec_comp *ec_complete_strvec(const struct ec_node *node,
149 const struct ec_strvec *strvec)
151 struct ec_comp *comp = NULL;
154 comp = ec_comp(NULL);
158 ret = ec_complete_child(node, comp, strvec);
169 struct ec_comp *ec_complete(const struct ec_node *node,
172 struct ec_strvec *strvec = NULL;
173 struct ec_comp *comp;
176 strvec = ec_strvec();
180 if (ec_strvec_add(strvec, str) < 0)
183 comp = ec_complete_strvec(node, strvec);
187 ec_strvec_free(strvec);
191 ec_strvec_free(strvec);
195 static struct ec_comp_group *
196 ec_comp_group(const struct ec_node *node, struct ec_pnode *parse)
198 struct ec_comp_group *grp = NULL;
200 grp = ec_calloc(1, sizeof(*grp));
204 grp->attrs = ec_dict();
205 if (grp->attrs == NULL)
208 grp->state = ec_pnode_dup(parse);
209 if (grp->state == NULL)
213 TAILQ_INIT(&grp->items);
219 ec_pnode_free(grp->state);
220 ec_dict_free(grp->attrs);
226 static struct ec_comp_item *
227 ec_comp_item(enum ec_comp_type type,
228 const char *start, const char *full)
230 struct ec_comp_item *item = NULL;
231 struct ec_dict *attrs = NULL;
232 char *comp_cp = NULL, *start_cp = NULL;
233 char *full_cp = NULL, *display_cp = NULL;
235 if (type == EC_COMP_UNKNOWN && full != NULL) {
239 if (type != EC_COMP_UNKNOWN && full == NULL) {
244 item = ec_calloc(1, sizeof(*item));
253 start_cp = ec_strdup(start);
254 if (start_cp == NULL)
257 if (ec_str_startswith(full, start)) {
258 comp_cp = ec_strdup(&full[strlen(start)]);
264 full_cp = ec_strdup(full);
267 display_cp = ec_strdup(full);
268 if (display_cp == NULL)
273 item->start = start_cp;
274 item->full = full_cp;
275 item->completion = comp_cp;
276 item->display = display_cp;
292 int ec_comp_item_set_display(struct ec_comp_item *item,
295 char *display_copy = NULL;
297 if (item == NULL || display == NULL ||
298 item->type == EC_COMP_UNKNOWN) {
303 display_copy = ec_strdup(display);
304 if (display_copy == NULL)
307 ec_free(item->display);
308 item->display = display_copy;
313 ec_free(display_copy);
318 ec_comp_item_set_completion(struct ec_comp_item *item,
319 const char *completion)
321 char *completion_copy = NULL;
323 if (item == NULL || completion == NULL ||
324 item->type == EC_COMP_UNKNOWN) {
329 completion_copy = ec_strdup(completion);
330 if (completion_copy == NULL)
333 ec_free(item->completion);
334 item->completion = completion_copy;
339 ec_free(completion_copy);
344 ec_comp_item_set_str(struct ec_comp_item *item,
347 char *str_copy = NULL;
349 if (item == NULL || str == NULL ||
350 item->type == EC_COMP_UNKNOWN) {
355 str_copy = ec_strdup(str);
356 if (str_copy == NULL)
360 item->full = str_copy;
370 ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
371 struct ec_comp_item *item)
373 if (comp == NULL || item == NULL) {
378 switch (item->type) {
379 case EC_COMP_UNKNOWN:
380 comp->count_unknown++;
385 case EC_COMP_PARTIAL:
386 comp->count_partial++;
393 if (comp->cur_group == NULL) {
394 struct ec_comp_group *grp;
396 grp = ec_comp_group(node, comp->cur_state);
399 TAILQ_INSERT_TAIL(&comp->groups, grp, next);
400 comp->cur_group = grp;
404 TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next);
405 item->grp = comp->cur_group;
411 ec_comp_item_get_str(const struct ec_comp_item *item)
417 ec_comp_item_get_display(const struct ec_comp_item *item)
419 return item->display;
423 ec_comp_item_get_completion(const struct ec_comp_item *item)
425 return item->completion;
429 ec_comp_item_get_type(const struct ec_comp_item *item)
434 const struct ec_comp_group *
435 ec_comp_item_get_grp(const struct ec_comp_item *item)
440 const struct ec_node *
441 ec_comp_item_get_node(const struct ec_comp_item *item)
443 return ec_comp_item_get_grp(item)->node;
447 ec_comp_item_free(struct ec_comp_item *item)
453 ec_free(item->start);
454 ec_free(item->completion);
455 ec_free(item->display);
456 ec_dict_free(item->attrs);
460 int ec_comp_add_item(struct ec_comp *comp,
461 const struct ec_node *node,
462 struct ec_comp_item **p_item,
463 enum ec_comp_type type,
464 const char *start, const char *full)
466 struct ec_comp_item *item = NULL;
469 item = ec_comp_item(type, start, full);
473 ret = ec_comp_item_add(comp, node, item);
483 ec_comp_item_free(item);
488 /* XXX move in helpers + rename ? */
489 /* return a completion item of type "unknown" */
491 ec_complete_unknown(const struct ec_node *gen_node,
492 struct ec_comp *comp,
493 const struct ec_strvec *strvec)
497 if (ec_strvec_len(strvec) != 1)
500 ret = ec_comp_add_item(comp, gen_node, NULL,
501 EC_COMP_UNKNOWN, NULL, NULL);
508 static void ec_comp_group_free(struct ec_comp_group *grp)
510 struct ec_comp_item *item;
515 while (!TAILQ_EMPTY(&grp->items)) {
516 item = TAILQ_FIRST(&grp->items);
517 TAILQ_REMOVE(&grp->items, item, next);
518 ec_comp_item_free(item);
520 ec_pnode_free(ec_pnode_get_root(grp->state));
521 ec_dict_free(grp->attrs);
525 const struct ec_node *
526 ec_comp_group_get_node(const struct ec_comp_group *grp)
531 const struct ec_pnode *
532 ec_comp_group_get_state(const struct ec_comp_group *grp)
537 const struct ec_dict *
538 ec_comp_group_get_attrs(const struct ec_comp_group *grp)
543 void ec_comp_free(struct ec_comp *comp)
545 struct ec_comp_group *grp;
550 while (!TAILQ_EMPTY(&comp->groups)) {
551 grp = TAILQ_FIRST(&comp->groups);
552 TAILQ_REMOVE(&comp->groups, grp, next);
553 ec_comp_group_free(grp);
555 ec_dict_free(comp->attrs);
559 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
561 struct ec_comp_group *grp;
562 struct ec_comp_item *item;
564 if (comp == NULL || comp->count == 0) {
565 fprintf(out, "no completion\n");
569 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
570 comp->count, comp->count_full,
571 comp->count_partial, comp->count_unknown);
573 TAILQ_FOREACH(grp, &comp->groups, next) {
574 fprintf(out, "node=%p, node_type=%s\n",
575 grp->node, ec_node_type(grp->node)->name);
576 TAILQ_FOREACH(item, &grp->items, next) {
579 switch (item->type) {
580 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
581 case EC_COMP_FULL: typestr = "full"; break;
582 case EC_COMP_PARTIAL: typestr = "partial"; break;
583 default: typestr = "unknown"; break;
586 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
587 typestr, item->full, item->completion,
593 int ec_comp_merge(struct ec_comp *to,
594 struct ec_comp *from)
596 struct ec_comp_group *grp;
598 while (!TAILQ_EMPTY(&from->groups)) {
599 grp = TAILQ_FIRST(&from->groups);
600 TAILQ_REMOVE(&from->groups, grp, next);
601 TAILQ_INSERT_TAIL(&to->groups, grp, next);
603 to->count += from->count;
604 to->count_full += from->count_full;
605 to->count_partial += from->count_partial;
606 to->count_unknown += from->count_unknown;
612 unsigned int ec_comp_count(
613 const struct ec_comp *comp,
614 enum ec_comp_type type)
616 unsigned int count = 0;
621 if (type & EC_COMP_FULL)
622 count += comp->count_full;
623 if (type & EC_COMP_PARTIAL)
624 count += comp->count_partial;
625 if (type & EC_COMP_UNKNOWN)
626 count += comp->count_unknown;
631 struct ec_comp_iter *
632 ec_comp_iter(const struct ec_comp *comp,
633 enum ec_comp_type type)
635 struct ec_comp_iter *iter;
637 iter = ec_calloc(1, sizeof(*iter));
643 iter->cur_node = NULL;
644 iter->cur_match = NULL;
649 struct ec_comp_item *ec_comp_iter_next(
650 struct ec_comp_iter *iter)
652 const struct ec_comp *comp;
653 struct ec_comp_group *cur_node;
654 struct ec_comp_item *cur_match;
662 cur_node = iter->cur_node;
663 cur_match = iter->cur_match;
666 if (cur_node == NULL) {
667 TAILQ_FOREACH(cur_node, &comp->groups, next) {
668 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
669 if (cur_match != NULL &&
670 cur_match->type & iter->type)
676 cur_match = TAILQ_NEXT(cur_match, next);
677 if (cur_match != NULL &&
678 cur_match->type & iter->type)
680 cur_node = TAILQ_NEXT(cur_node, next);
681 while (cur_node != NULL) {
682 cur_match = TAILQ_FIRST(&cur_node->items);
683 if (cur_match != NULL &&
684 cur_match->type & iter->type)
686 cur_node = TAILQ_NEXT(cur_node, next);
692 iter->cur_node = cur_node;
693 iter->cur_match = cur_match;
695 return iter->cur_match;
698 void ec_comp_iter_free(struct ec_comp_iter *iter)
703 /* LCOV_EXCL_START */
704 static int ec_comp_testcase(void)
706 struct ec_node *node = NULL;
707 struct ec_comp *c = NULL;
708 struct ec_comp_iter *iter = NULL;
709 struct ec_comp_item *item;
715 node = ec_node_sh_lex(EC_NO_ID,
717 ec_node_str("id_x", "xx"),
718 ec_node_str("id_y", "yy")));
722 c = ec_complete(node, "xcdscds");
723 testres |= EC_TEST_CHECK(
724 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
725 "complete count should is not 0\n");
728 c = ec_complete(node, "x");
729 testres |= EC_TEST_CHECK(
730 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
731 "complete count should is not 1\n");
734 c = ec_complete(node, "");
735 testres |= EC_TEST_CHECK(
736 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
737 "complete count should is not 2\n");
739 f = open_memstream(&buf, &buflen);
742 ec_comp_dump(f, NULL);
746 testres |= EC_TEST_CHECK(
747 strstr(buf, "no completion"), "bad dump\n");
751 f = open_memstream(&buf, &buflen);
758 testres |= EC_TEST_CHECK(
759 strstr(buf, "comp=<xx>"), "bad dump\n");
760 testres |= EC_TEST_CHECK(
761 strstr(buf, "comp=<yy>"), "bad dump\n");
765 iter = ec_comp_iter(c, EC_COMP_ALL);
766 item = ec_comp_iter_next(iter);
770 testres |= EC_TEST_CHECK(
771 !strcmp(ec_comp_item_get_display(item), "xx"),
772 "bad item display\n");
773 testres |= EC_TEST_CHECK(
774 ec_comp_item_get_type(item) == EC_COMP_FULL,
776 testres |= EC_TEST_CHECK(
777 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
780 item = ec_comp_iter_next(iter);
784 testres |= EC_TEST_CHECK(
785 !strcmp(ec_comp_item_get_display(item), "yy"),
786 "bad item display\n");
787 testres |= EC_TEST_CHECK(
788 ec_comp_item_get_type(item) == EC_COMP_FULL,
790 testres |= EC_TEST_CHECK(
791 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
794 item = ec_comp_iter_next(iter);
795 testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
797 ec_comp_iter_free(iter);
804 ec_comp_iter_free(iter);
815 static struct ec_test ec_comp_test = {
817 .test = ec_comp_testcase,
820 EC_TEST_REGISTER(ec_comp_test);