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_keyval.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_keyval *attrs;
37 struct ec_comp *ec_comp(struct ec_parse *state)
39 struct ec_comp *comp = NULL;
41 comp = ec_calloc(1, sizeof(*comp));
45 comp->attrs = ec_keyval();
46 if (comp->attrs == NULL)
49 TAILQ_INIT(&comp->groups);
51 comp->cur_state = state;
57 ec_keyval_free(comp->attrs);
63 struct ec_parse *ec_comp_get_state(struct ec_comp *comp)
65 return comp->cur_state;
69 ec_node_complete_child(const struct ec_node *node,
71 const struct ec_strvec *strvec)
73 struct ec_parse *child_state, *cur_state;
74 struct ec_comp_group *cur_group;
77 if (ec_node_type(node)->complete == NULL) {
82 /* save previous parse state, prepare child state */
83 cur_state = comp->cur_state;
84 child_state = ec_parse(node);
85 if (child_state == NULL)
88 if (cur_state != NULL)
89 ec_parse_link_child(cur_state, child_state);
90 comp->cur_state = child_state;
91 cur_group = comp->cur_group;
92 comp->cur_group = NULL;
94 /* fill the comp struct with items */
95 ret = ec_node_type(node)->complete(node, comp, strvec);
97 /* restore parent parse state */
98 if (cur_state != NULL) {
99 ec_parse_unlink_child(cur_state, child_state);
100 assert(!ec_parse_has_child(child_state));
102 ec_parse_free(child_state);
103 comp->cur_state = cur_state;
104 comp->cur_group = cur_group;
112 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
113 const struct ec_strvec *strvec)
115 struct ec_comp *comp = NULL;
118 comp = ec_comp(NULL);
122 ret = ec_node_complete_child(node, comp, strvec);
133 struct ec_comp *ec_node_complete(const struct ec_node *node,
136 struct ec_strvec *strvec = NULL;
137 struct ec_comp *comp;
140 strvec = ec_strvec();
144 if (ec_strvec_add(strvec, str) < 0)
147 comp = ec_node_complete_strvec(node, strvec);
151 ec_strvec_free(strvec);
155 ec_strvec_free(strvec);
159 static struct ec_comp_group *
160 ec_comp_group(const struct ec_node *node, struct ec_parse *parse)
162 struct ec_comp_group *grp = NULL;
164 grp = ec_calloc(1, sizeof(*grp));
168 grp->attrs = ec_keyval();
169 if (grp->attrs == NULL)
172 grp->state = ec_parse_dup(parse);
173 if (grp->state == NULL)
177 TAILQ_INIT(&grp->items);
183 ec_parse_free(grp->state);
184 ec_keyval_free(grp->attrs);
190 static struct ec_comp_item *
191 ec_comp_item(enum ec_comp_type type,
192 const char *start, const char *full)
194 struct ec_comp_item *item = NULL;
195 struct ec_keyval *attrs = NULL;
196 char *comp_cp = NULL, *start_cp = NULL;
197 char *full_cp = NULL, *display_cp = NULL;
199 if (type == EC_COMP_UNKNOWN && full != NULL) {
203 if (type != EC_COMP_UNKNOWN && full == NULL) {
208 item = ec_calloc(1, sizeof(*item));
217 start_cp = ec_strdup(start);
218 if (start_cp == NULL)
221 if (ec_str_startswith(full, start)) {
222 comp_cp = ec_strdup(&full[strlen(start)]);
228 full_cp = ec_strdup(full);
231 display_cp = ec_strdup(full);
232 if (display_cp == NULL)
237 item->start = start_cp;
238 item->full = full_cp;
239 item->completion = comp_cp;
240 item->display = display_cp;
246 ec_keyval_free(attrs);
256 int ec_comp_item_set_display(struct ec_comp_item *item,
259 char *display_copy = NULL;
261 if (item == NULL || display == NULL ||
262 item->type == EC_COMP_UNKNOWN) {
267 display_copy = ec_strdup(display);
268 if (display_copy == NULL)
271 ec_free(item->display);
272 item->display = display_copy;
277 ec_free(display_copy);
282 ec_comp_item_set_completion(struct ec_comp_item *item,
283 const char *completion)
285 char *completion_copy = NULL;
287 if (item == NULL || completion == NULL ||
288 item->type == EC_COMP_UNKNOWN) {
293 completion_copy = ec_strdup(completion);
294 if (completion_copy == NULL)
297 ec_free(item->completion);
298 item->completion = completion_copy;
303 ec_free(completion_copy);
308 ec_comp_item_set_str(struct ec_comp_item *item,
311 char *str_copy = NULL;
313 if (item == NULL || str == NULL ||
314 item->type == EC_COMP_UNKNOWN) {
319 str_copy = ec_strdup(str);
320 if (str_copy == NULL)
324 item->full = str_copy;
334 ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
335 struct ec_comp_item *item)
337 if (comp == NULL || item == NULL) {
342 switch (item->type) {
343 case EC_COMP_UNKNOWN:
344 comp->count_unknown++;
349 case EC_COMP_PARTIAL:
350 comp->count_partial++;
357 if (comp->cur_group == NULL) {
358 struct ec_comp_group *grp;
360 grp = ec_comp_group(node, comp->cur_state);
363 TAILQ_INSERT_TAIL(&comp->groups, grp, next);
364 comp->cur_group = grp;
368 TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next);
369 item->grp = comp->cur_group;
375 ec_comp_item_get_str(const struct ec_comp_item *item)
381 ec_comp_item_get_display(const struct ec_comp_item *item)
383 return item->display;
387 ec_comp_item_get_completion(const struct ec_comp_item *item)
389 return item->completion;
393 ec_comp_item_get_type(const struct ec_comp_item *item)
398 const struct ec_comp_group *
399 ec_comp_item_get_grp(const struct ec_comp_item *item)
404 const struct ec_node *
405 ec_comp_item_get_node(const struct ec_comp_item *item)
407 return ec_comp_item_get_grp(item)->node;
411 ec_comp_item_free(struct ec_comp_item *item)
417 ec_free(item->start);
418 ec_free(item->completion);
419 ec_free(item->display);
420 ec_keyval_free(item->attrs);
424 int ec_comp_add_item(struct ec_comp *comp,
425 const struct ec_node *node,
426 struct ec_comp_item **p_item,
427 enum ec_comp_type type,
428 const char *start, const char *full)
430 struct ec_comp_item *item = NULL;
433 item = ec_comp_item(type, start, full);
437 ret = ec_comp_item_add(comp, node, item);
447 ec_comp_item_free(item);
452 /* return a completion item of type "unknown" */
454 ec_node_complete_unknown(const struct ec_node *gen_node,
455 struct ec_comp *comp,
456 const struct ec_strvec *strvec)
460 if (ec_strvec_len(strvec) != 1)
463 ret = ec_comp_add_item(comp, gen_node, NULL,
464 EC_COMP_UNKNOWN, NULL, NULL);
471 static void ec_comp_group_free(struct ec_comp_group *grp)
473 struct ec_comp_item *item;
478 while (!TAILQ_EMPTY(&grp->items)) {
479 item = TAILQ_FIRST(&grp->items);
480 TAILQ_REMOVE(&grp->items, item, next);
481 ec_comp_item_free(item);
483 ec_parse_free(ec_parse_get_root(grp->state));
484 ec_keyval_free(grp->attrs);
488 void ec_comp_free(struct ec_comp *comp)
490 struct ec_comp_group *grp;
495 while (!TAILQ_EMPTY(&comp->groups)) {
496 grp = TAILQ_FIRST(&comp->groups);
497 TAILQ_REMOVE(&comp->groups, grp, next);
498 ec_comp_group_free(grp);
500 ec_keyval_free(comp->attrs);
504 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
506 struct ec_comp_group *grp;
507 struct ec_comp_item *item;
509 if (comp == NULL || comp->count == 0) {
510 fprintf(out, "no completion\n");
514 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
515 comp->count, comp->count_full,
516 comp->count_partial, comp->count_unknown);
518 TAILQ_FOREACH(grp, &comp->groups, next) {
519 fprintf(out, "node=%p, node_type=%s\n",
520 grp->node, ec_node_type(grp->node)->name);
521 TAILQ_FOREACH(item, &grp->items, next) {
524 switch (item->type) {
525 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
526 case EC_COMP_FULL: typestr = "full"; break;
527 case EC_COMP_PARTIAL: typestr = "partial"; break;
528 default: typestr = "unknown"; break;
531 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
532 typestr, item->full, item->completion,
538 int ec_comp_merge(struct ec_comp *to,
539 struct ec_comp *from)
541 struct ec_comp_group *grp;
543 while (!TAILQ_EMPTY(&from->groups)) {
544 grp = TAILQ_FIRST(&from->groups);
545 TAILQ_REMOVE(&from->groups, grp, next);
546 TAILQ_INSERT_TAIL(&to->groups, grp, next);
548 to->count += from->count;
549 to->count_full += from->count_full;
550 to->count_partial += from->count_partial;
551 to->count_unknown += from->count_unknown;
557 unsigned int ec_comp_count(
558 const struct ec_comp *comp,
559 enum ec_comp_type type)
561 unsigned int count = 0;
566 if (type & EC_COMP_FULL)
567 count += comp->count_full;
568 if (type & EC_COMP_PARTIAL)
569 count += comp->count_partial;
570 if (type & EC_COMP_UNKNOWN)
571 count += comp->count_unknown;
576 struct ec_comp_iter *
577 ec_comp_iter(struct ec_comp *comp,
578 enum ec_comp_type type)
580 struct ec_comp_iter *iter;
582 iter = ec_calloc(1, sizeof(*iter));
588 iter->cur_node = NULL;
589 iter->cur_match = NULL;
594 struct ec_comp_item *ec_comp_iter_next(
595 struct ec_comp_iter *iter)
597 struct ec_comp *comp;
598 struct ec_comp_group *cur_node;
599 struct ec_comp_item *cur_match;
607 cur_node = iter->cur_node;
608 cur_match = iter->cur_match;
611 if (cur_node == NULL) {
612 TAILQ_FOREACH(cur_node, &comp->groups, next) {
613 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
614 if (cur_match != NULL &&
615 cur_match->type & iter->type)
621 cur_match = TAILQ_NEXT(cur_match, next);
622 if (cur_match != NULL &&
623 cur_match->type & iter->type)
625 cur_node = TAILQ_NEXT(cur_node, next);
626 while (cur_node != NULL) {
627 cur_match = TAILQ_FIRST(&cur_node->items);
628 if (cur_match != NULL &&
629 cur_match->type & iter->type)
631 cur_node = TAILQ_NEXT(cur_node, next);
637 iter->cur_node = cur_node;
638 iter->cur_match = cur_match;
640 return iter->cur_match;
643 void ec_comp_iter_free(struct ec_comp_iter *iter)
648 /* LCOV_EXCL_START */
649 static int ec_comp_testcase(void)
651 struct ec_node *node = NULL;
652 struct ec_comp *c = NULL;
653 struct ec_comp_iter *iter = NULL;
654 struct ec_comp_item *item;
660 node = ec_node_sh_lex(EC_NO_ID,
662 ec_node_str("id_x", "xx"),
663 ec_node_str("id_y", "yy")));
667 c = ec_node_complete(node, "xcdscds");
668 testres |= EC_TEST_CHECK(
669 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
670 "complete count should is not 0\n");
673 c = ec_node_complete(node, "x");
674 testres |= EC_TEST_CHECK(
675 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
676 "complete count should is not 1\n");
679 c = ec_node_complete(node, "");
680 testres |= EC_TEST_CHECK(
681 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
682 "complete count should is not 2\n");
684 f = open_memstream(&buf, &buflen);
687 ec_comp_dump(f, NULL);
691 testres |= EC_TEST_CHECK(
692 strstr(buf, "no completion"), "bad dump\n");
696 f = open_memstream(&buf, &buflen);
703 testres |= EC_TEST_CHECK(
704 strstr(buf, "comp=<xx>"), "bad dump\n");
705 testres |= EC_TEST_CHECK(
706 strstr(buf, "comp=<yy>"), "bad dump\n");
710 iter = ec_comp_iter(c, EC_COMP_ALL);
711 item = ec_comp_iter_next(iter);
715 testres |= EC_TEST_CHECK(
716 !strcmp(ec_comp_item_get_display(item), "xx"),
717 "bad item display\n");
718 testres |= EC_TEST_CHECK(
719 ec_comp_item_get_type(item) == EC_COMP_FULL,
721 testres |= EC_TEST_CHECK(
722 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
725 item = ec_comp_iter_next(iter);
729 testres |= EC_TEST_CHECK(
730 !strcmp(ec_comp_item_get_display(item), "yy"),
731 "bad item display\n");
732 testres |= EC_TEST_CHECK(
733 ec_comp_item_get_type(item) == EC_COMP_FULL,
735 testres |= EC_TEST_CHECK(
736 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
739 item = ec_comp_iter_next(iter);
740 testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
742 ec_comp_iter_free(iter);
749 ec_comp_iter_free(iter);
760 static struct ec_test ec_comp_test = {
762 .test = ec_comp_testcase,
765 EC_TEST_REGISTER(ec_comp_test);