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)
80 /* save previous parse state, prepare child state */
81 cur_state = comp->cur_state;
82 child_state = ec_parse(node);
83 if (child_state == NULL)
86 if (cur_state != NULL)
87 ec_parse_link_child(cur_state, child_state);
88 comp->cur_state = child_state;
89 cur_group = comp->cur_group;
90 comp->cur_group = NULL;
92 /* fill the comp struct with items */
93 ret = ec_node_type(node)->complete(node, comp, strvec);
95 /* restore parent parse state */
96 if (cur_state != NULL) {
97 ec_parse_unlink_child(cur_state, child_state);
98 assert(!ec_parse_has_child(child_state));
100 ec_parse_free(child_state);
101 comp->cur_state = cur_state;
102 comp->cur_group = cur_group;
110 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
111 const struct ec_strvec *strvec)
113 struct ec_comp *comp = NULL;
116 comp = ec_comp(NULL);
120 ret = ec_node_complete_child(node, comp, strvec);
131 struct ec_comp *ec_node_complete(const struct ec_node *node,
134 struct ec_strvec *strvec = NULL;
135 struct ec_comp *comp;
138 strvec = ec_strvec();
142 if (ec_strvec_add(strvec, str) < 0)
145 comp = ec_node_complete_strvec(node, strvec);
149 ec_strvec_free(strvec);
153 ec_strvec_free(strvec);
157 static struct ec_comp_group *
158 ec_comp_group(const struct ec_node *node, struct ec_parse *parse)
160 struct ec_comp_group *grp = NULL;
162 grp = ec_calloc(1, sizeof(*grp));
166 grp->attrs = ec_keyval();
167 if (grp->attrs == NULL)
170 grp->state = ec_parse_dup(parse);
171 if (grp->state == NULL)
175 TAILQ_INIT(&grp->items);
181 ec_parse_free(grp->state);
182 ec_keyval_free(grp->attrs);
188 static struct ec_comp_item *
189 ec_comp_item(enum ec_comp_type type,
190 const char *start, const char *full)
192 struct ec_comp_item *item = NULL;
193 struct ec_keyval *attrs = NULL;
194 char *comp_cp = NULL, *start_cp = NULL;
195 char *full_cp = NULL, *display_cp = NULL;
197 if (type == EC_COMP_UNKNOWN && full != NULL) {
201 if (type != EC_COMP_UNKNOWN && full == NULL) {
206 item = ec_calloc(1, sizeof(*item));
215 start_cp = ec_strdup(start);
216 if (start_cp == NULL)
219 if (ec_str_startswith(full, start)) {
220 comp_cp = ec_strdup(&full[strlen(start)]);
226 full_cp = ec_strdup(full);
229 display_cp = ec_strdup(full);
230 if (display_cp == NULL)
235 item->start = start_cp;
236 item->full = full_cp;
237 item->completion = comp_cp;
238 item->display = display_cp;
244 ec_keyval_free(attrs);
254 int ec_comp_item_set_display(struct ec_comp_item *item,
257 char *display_copy = NULL;
260 if (item == NULL || display == NULL ||
261 item->type == EC_COMP_UNKNOWN)
264 display_copy = ec_strdup(display);
265 if (display_copy == NULL)
268 ec_free(item->display);
269 item->display = display_copy;
274 ec_free(display_copy);
279 ec_comp_item_set_completion(struct ec_comp_item *item,
280 const char *completion)
282 char *completion_copy = NULL;
285 if (item == NULL || completion == NULL ||
286 item->type == EC_COMP_UNKNOWN)
290 completion_copy = ec_strdup(completion);
291 if (completion_copy == NULL)
294 ec_free(item->completion);
295 item->completion = completion_copy;
300 ec_free(completion_copy);
305 ec_comp_item_set_str(struct ec_comp_item *item,
308 char *str_copy = NULL;
311 if (item == NULL || str == NULL ||
312 item->type == EC_COMP_UNKNOWN)
316 str_copy = ec_strdup(str);
317 if (str_copy == NULL)
321 item->full = str_copy;
331 ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
332 struct ec_comp_item *item)
334 if (comp == NULL || item == NULL)
337 switch (item->type) {
338 case EC_COMP_UNKNOWN:
339 comp->count_unknown++;
344 case EC_COMP_PARTIAL:
345 comp->count_partial++;
351 if (comp->cur_group == NULL) {
352 struct ec_comp_group *grp;
354 grp = ec_comp_group(node, comp->cur_state);
357 TAILQ_INSERT_TAIL(&comp->groups, grp, next);
358 comp->cur_group = grp;
362 TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next);
363 item->grp = comp->cur_group;
369 ec_comp_item_get_str(const struct ec_comp_item *item)
375 ec_comp_item_get_display(const struct ec_comp_item *item)
377 return item->display;
381 ec_comp_item_get_completion(const struct ec_comp_item *item)
383 return item->completion;
387 ec_comp_item_get_type(const struct ec_comp_item *item)
392 const struct ec_comp_group *
393 ec_comp_item_get_grp(const struct ec_comp_item *item)
398 const struct ec_node *
399 ec_comp_item_get_node(const struct ec_comp_item *item)
401 return ec_comp_item_get_grp(item)->node;
405 ec_comp_item_free(struct ec_comp_item *item)
411 ec_free(item->start);
412 ec_free(item->completion);
413 ec_free(item->display);
414 ec_keyval_free(item->attrs);
418 int ec_comp_add_item(struct ec_comp *comp,
419 const struct ec_node *node,
420 struct ec_comp_item **p_item,
421 enum ec_comp_type type,
422 const char *start, const char *full)
424 struct ec_comp_item *item = NULL;
427 item = ec_comp_item(type, start, full);
431 ret = ec_comp_item_add(comp, node, item);
441 ec_comp_item_free(item);
446 /* return a completion item of type "unknown" */
448 ec_node_complete_unknown(const struct ec_node *gen_node,
449 struct ec_comp *comp,
450 const struct ec_strvec *strvec)
454 if (ec_strvec_len(strvec) != 1)
457 ret = ec_comp_add_item(comp, gen_node, NULL,
458 EC_COMP_UNKNOWN, NULL, NULL);
465 static void ec_comp_group_free(struct ec_comp_group *grp)
467 struct ec_comp_item *item;
472 while (!TAILQ_EMPTY(&grp->items)) {
473 item = TAILQ_FIRST(&grp->items);
474 TAILQ_REMOVE(&grp->items, item, next);
475 ec_comp_item_free(item);
477 ec_parse_free(ec_parse_get_root(grp->state));
478 ec_keyval_free(grp->attrs);
482 void ec_comp_free(struct ec_comp *comp)
484 struct ec_comp_group *grp;
489 while (!TAILQ_EMPTY(&comp->groups)) {
490 grp = TAILQ_FIRST(&comp->groups);
491 TAILQ_REMOVE(&comp->groups, grp, next);
492 ec_comp_group_free(grp);
494 ec_keyval_free(comp->attrs);
498 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
500 struct ec_comp_group *grp;
501 struct ec_comp_item *item;
503 if (comp == NULL || comp->count == 0) {
504 fprintf(out, "no completion\n");
508 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
509 comp->count, comp->count_full,
510 comp->count_partial, comp->count_unknown);
512 TAILQ_FOREACH(grp, &comp->groups, next) {
513 fprintf(out, "node=%p, node_type=%s\n",
514 grp->node, ec_node_type(grp->node)->name);
515 TAILQ_FOREACH(item, &grp->items, next) {
518 switch (item->type) {
519 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
520 case EC_COMP_FULL: typestr = "full"; break;
521 case EC_COMP_PARTIAL: typestr = "partial"; break;
522 default: typestr = "unknown"; break;
525 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
526 typestr, item->full, item->completion,
532 int ec_comp_merge(struct ec_comp *to,
533 struct ec_comp *from)
535 struct ec_comp_group *grp;
537 while (!TAILQ_EMPTY(&from->groups)) {
538 grp = TAILQ_FIRST(&from->groups);
539 TAILQ_REMOVE(&from->groups, grp, next);
540 TAILQ_INSERT_TAIL(&to->groups, grp, next);
542 to->count += from->count;
543 to->count_full += from->count_full;
544 to->count_partial += from->count_partial;
545 to->count_unknown += from->count_unknown;
551 unsigned int ec_comp_count(
552 const struct ec_comp *comp,
553 enum ec_comp_type type)
555 unsigned int count = 0;
560 if (type & EC_COMP_FULL)
561 count += comp->count_full;
562 if (type & EC_COMP_PARTIAL)
563 count += comp->count_partial;
564 if (type & EC_COMP_UNKNOWN)
565 count += comp->count_unknown;
570 struct ec_comp_iter *
571 ec_comp_iter(struct ec_comp *comp,
572 enum ec_comp_type type)
574 struct ec_comp_iter *iter;
576 iter = ec_calloc(1, sizeof(*iter));
582 iter->cur_node = NULL;
583 iter->cur_match = NULL;
588 struct ec_comp_item *ec_comp_iter_next(
589 struct ec_comp_iter *iter)
591 struct ec_comp *comp;
592 struct ec_comp_group *cur_node;
593 struct ec_comp_item *cur_match;
601 cur_node = iter->cur_node;
602 cur_match = iter->cur_match;
605 if (cur_node == NULL) {
606 TAILQ_FOREACH(cur_node, &comp->groups, next) {
607 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
608 if (cur_match != NULL &&
609 cur_match->type & iter->type)
615 cur_match = TAILQ_NEXT(cur_match, next);
616 if (cur_match != NULL &&
617 cur_match->type & iter->type)
619 cur_node = TAILQ_NEXT(cur_node, next);
620 while (cur_node != NULL) {
621 cur_match = TAILQ_FIRST(&cur_node->items);
622 if (cur_match != NULL &&
623 cur_match->type & iter->type)
625 cur_node = TAILQ_NEXT(cur_node, next);
631 iter->cur_node = cur_node;
632 iter->cur_match = cur_match;
634 return iter->cur_match;
637 void ec_comp_iter_free(struct ec_comp_iter *iter)
642 /* LCOV_EXCL_START */
643 static int ec_comp_testcase(void)
645 struct ec_node *node = NULL;
646 struct ec_comp *c = NULL;
647 struct ec_comp_iter *iter = NULL;
648 struct ec_comp_item *item;
654 node = ec_node_sh_lex(EC_NO_ID,
656 ec_node_str("id_x", "xx"),
657 ec_node_str("id_y", "yy")));
661 c = ec_node_complete(node, "xcdscds");
662 testres |= EC_TEST_CHECK(
663 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
664 "complete count should is not 0\n");
667 c = ec_node_complete(node, "x");
668 testres |= EC_TEST_CHECK(
669 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
670 "complete count should is not 1\n");
673 c = ec_node_complete(node, "");
674 testres |= EC_TEST_CHECK(
675 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
676 "complete count should is not 2\n");
678 f = open_memstream(&buf, &buflen);
685 /* testres |= EC_TEST_CHECK( */
686 /* strstr(buf, "no match"), "bad dump\n"); */
690 iter = ec_comp_iter(c, EC_COMP_ALL);
691 item = ec_comp_iter_next(iter);
695 testres |= EC_TEST_CHECK(
696 !strcmp(ec_comp_item_get_display(item), "xx"),
697 "bad item display\n");
698 testres |= EC_TEST_CHECK(
699 ec_comp_item_get_type(item) == EC_COMP_FULL,
701 testres |= EC_TEST_CHECK(
702 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
705 item = ec_comp_iter_next(iter);
709 testres |= EC_TEST_CHECK(
710 !strcmp(ec_comp_item_get_display(item), "yy"),
711 "bad item display\n");
712 testres |= EC_TEST_CHECK(
713 ec_comp_item_get_type(item) == EC_COMP_FULL,
715 testres |= EC_TEST_CHECK(
716 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
719 item = ec_comp_iter_next(iter);
720 testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
722 ec_comp_iter_free(iter);
729 ec_comp_iter_free(iter);
740 static struct ec_test ec_comp_test = {
742 .test = ec_comp_testcase,
745 EC_TEST_REGISTER(ec_comp_test);