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_comp *comp;
43 const struct ec_node *node;
44 struct ec_comp_item_list items;
45 struct ec_pnode *pstate;
46 struct ec_dict *attrs;
49 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
56 struct ec_pnode *cur_pstate;
57 struct ec_comp_group *cur_group;
58 struct ec_comp_group_list groups;
59 struct ec_dict *attrs;
62 struct ec_comp *ec_comp(void)
64 struct ec_comp *comp = NULL;
66 comp = ec_calloc(1, sizeof(*comp));
70 comp->attrs = ec_dict();
71 if (comp->attrs == NULL)
74 TAILQ_INIT(&comp->groups);
80 ec_dict_free(comp->attrs);
86 struct ec_pnode *ec_comp_get_cur_pstate(const struct ec_comp *comp)
88 return comp->cur_pstate;
91 struct ec_comp_group *ec_comp_get_cur_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_complete_child(const struct ec_node *node,
103 struct ec_comp *comp,
104 const struct ec_strvec *strvec)
106 struct ec_pnode *child_pstate, *cur_pstate;
107 struct ec_comp_group *cur_group;
108 ec_complete_t complete_cb;
111 /* get the complete method, falling back to ec_complete_unknown() */
112 complete_cb = ec_node_type(node)->complete;
113 if (complete_cb == NULL)
114 complete_cb = ec_complete_unknown;
116 /* save previous parse state, prepare child state */
117 cur_pstate = comp->cur_pstate;
118 child_pstate = ec_pnode(node);
119 if (child_pstate == NULL)
122 if (cur_pstate != NULL)
123 ec_pnode_link_child(cur_pstate, child_pstate);
124 comp->cur_pstate = child_pstate;
125 cur_group = comp->cur_group;
126 comp->cur_group = NULL;
128 /* fill the comp struct with items */
129 ret = complete_cb(node, comp, strvec);
131 /* restore parent parse state */
132 if (cur_pstate != NULL) {
133 ec_pnode_unlink_child(child_pstate);
134 assert(ec_pnode_get_first_child(child_pstate) == NULL);
136 ec_pnode_free(child_pstate);
137 comp->cur_pstate = cur_pstate;
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;
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_comp *comp, const struct ec_node *node,
195 struct ec_pnode *parse)
197 struct ec_comp_group *grp = NULL;
199 grp = ec_calloc(1, sizeof(*grp));
204 grp->attrs = ec_dict();
205 if (grp->attrs == NULL)
208 grp->pstate = ec_pnode_dup(parse);
209 if (grp->pstate == NULL)
213 TAILQ_INIT(&grp->items);
219 ec_pnode_free(grp->pstate);
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(comp, node, comp->cur_pstate);
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 struct ec_comp_item *ec_comp_add_item(struct ec_comp *comp,
461 const struct ec_node *node, 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);
478 ec_comp_item_free(item);
482 /* return a completion item of type "unknown" */
484 ec_complete_unknown(const struct ec_node *gen_node,
485 struct ec_comp *comp,
486 const struct ec_strvec *strvec)
488 const struct ec_comp_item *item = NULL;
490 if (ec_strvec_len(strvec) != 1)
493 item = ec_comp_add_item(comp, gen_node, EC_COMP_UNKNOWN, NULL, NULL);
500 static void ec_comp_group_free(struct ec_comp_group *grp)
502 struct ec_comp_item *item;
507 while (!TAILQ_EMPTY(&grp->items)) {
508 item = TAILQ_FIRST(&grp->items);
509 TAILQ_REMOVE(&grp->items, item, next);
510 ec_comp_item_free(item);
512 ec_pnode_free(ec_pnode_get_root(grp->pstate));
513 ec_dict_free(grp->attrs);
517 const struct ec_node *
518 ec_comp_group_get_node(const struct ec_comp_group *grp)
523 const struct ec_pnode *
524 ec_comp_group_get_pstate(const struct ec_comp_group *grp)
529 const struct ec_dict *
530 ec_comp_group_get_attrs(const struct ec_comp_group *grp)
535 void ec_comp_free(struct ec_comp *comp)
537 struct ec_comp_group *grp;
542 while (!TAILQ_EMPTY(&comp->groups)) {
543 grp = TAILQ_FIRST(&comp->groups);
544 TAILQ_REMOVE(&comp->groups, grp, next);
545 ec_comp_group_free(grp);
547 ec_dict_free(comp->attrs);
551 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
553 struct ec_comp_group *grp;
554 struct ec_comp_item *item;
556 if (comp == NULL || comp->count == 0) {
557 fprintf(out, "no completion\n");
561 fprintf(out, "completion: count=%zu full=%zu partial=%zu unknown=%zu\n",
562 comp->count, comp->count_full,
563 comp->count_partial, comp->count_unknown);
565 TAILQ_FOREACH(grp, &comp->groups, next) {
566 fprintf(out, "node=%p, node_type=%s\n",
567 grp->node, ec_node_type(grp->node)->name);
568 TAILQ_FOREACH(item, &grp->items, next) {
571 switch (item->type) {
572 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
573 case EC_COMP_FULL: typestr = "full"; break;
574 case EC_COMP_PARTIAL: typestr = "partial"; break;
575 default: typestr = "unknown"; break;
578 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
579 typestr, item->full, item->completion,
585 int ec_comp_merge(struct ec_comp *to,
586 struct ec_comp *from)
588 struct ec_comp_group *grp;
590 while (!TAILQ_EMPTY(&from->groups)) {
591 grp = TAILQ_FIRST(&from->groups);
592 TAILQ_REMOVE(&from->groups, grp, next);
593 TAILQ_INSERT_TAIL(&to->groups, grp, next);
595 to->count += from->count;
596 to->count_full += from->count_full;
597 to->count_partial += from->count_partial;
598 to->count_unknown += from->count_unknown;
604 size_t ec_comp_count(const struct ec_comp *comp, enum ec_comp_type type)
611 if (type & EC_COMP_FULL)
612 count += comp->count_full;
613 if (type & EC_COMP_PARTIAL)
614 count += comp->count_partial;
615 if (type & EC_COMP_UNKNOWN)
616 count += comp->count_unknown;
621 static struct ec_comp_item *
622 __ec_comp_iter_next(const struct ec_comp *comp, struct ec_comp_item *item,
623 enum ec_comp_type type)
625 struct ec_comp_group *cur_grp;
626 struct ec_comp_item *cur_match;
630 TAILQ_FOREACH(cur_grp, &comp->groups, next) {
631 TAILQ_FOREACH(cur_match, &cur_grp->items, next) {
632 if (cur_match->type & type)
640 cur_match = TAILQ_NEXT(item, next);
641 while (cur_match != NULL) {
642 if (cur_match->type & type)
644 cur_match = TAILQ_NEXT(cur_match, next);
646 cur_grp = TAILQ_NEXT(cur_grp, next);
647 while (cur_grp != NULL) {
648 TAILQ_FOREACH(cur_match, &cur_grp->items, next) {
649 if (cur_match->type & type)
657 struct ec_comp_item *
658 ec_comp_iter_next(struct ec_comp_item *item, enum ec_comp_type type)
662 return __ec_comp_iter_next(item->grp->comp, item, type);
666 struct ec_comp_item *
667 ec_comp_iter_first(const struct ec_comp *comp, enum ec_comp_type type)
669 return __ec_comp_iter_next(comp, NULL, type);
672 /* LCOV_EXCL_START */
673 static int ec_comp_testcase(void)
675 struct ec_node *node = NULL;
676 struct ec_comp *c = NULL;
677 struct ec_comp_item *item;
683 node = ec_node_sh_lex(EC_NO_ID,
685 ec_node_str("id_x", "xx"),
686 ec_node_str("id_y", "yy")));
690 c = ec_complete(node, "xcdscds");
691 testres |= EC_TEST_CHECK(
692 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
693 "complete count should is not 0\n");
696 c = ec_complete(node, "x");
697 testres |= EC_TEST_CHECK(
698 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
699 "complete count should is not 1\n");
702 c = ec_complete(node, "");
703 testres |= EC_TEST_CHECK(
704 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
705 "complete count should is not 2\n");
707 f = open_memstream(&buf, &buflen);
710 ec_comp_dump(f, NULL);
714 testres |= EC_TEST_CHECK(
715 strstr(buf, "no completion"), "bad dump\n");
719 f = open_memstream(&buf, &buflen);
726 testres |= EC_TEST_CHECK(
727 strstr(buf, "comp=<xx>"), "bad dump\n");
728 testres |= EC_TEST_CHECK(
729 strstr(buf, "comp=<yy>"), "bad dump\n");
733 item = ec_comp_iter_first(c, EC_COMP_ALL);
737 testres |= EC_TEST_CHECK(
738 !strcmp(ec_comp_item_get_display(item), "xx"),
739 "bad item display\n");
740 testres |= EC_TEST_CHECK(
741 ec_comp_item_get_type(item) == EC_COMP_FULL,
743 testres |= EC_TEST_CHECK(
744 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
747 item = ec_comp_iter_next(item, EC_COMP_ALL);
751 testres |= EC_TEST_CHECK(
752 !strcmp(ec_comp_item_get_display(item), "yy"),
753 "bad item display\n");
754 testres |= EC_TEST_CHECK(
755 ec_comp_item_get_type(item) == EC_COMP_FULL,
757 testres |= EC_TEST_CHECK(
758 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
761 item = ec_comp_iter_next(item, EC_COMP_ALL);
762 testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
780 static struct ec_test ec_comp_test = {
782 .test = ec_comp_testcase,
785 EC_TEST_REGISTER(ec_comp_test);