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_node.h>
17 #include <ecoli_parse.h>
18 #include <ecoli_complete.h>
21 TAILQ_ENTRY(ec_comp_item) next;
22 enum ec_comp_type type;
23 const struct ec_node *node;
24 struct ec_comp_group *grp;
25 char *start; /* the initial token */
26 char *full; /* the full token after completion */
27 char *completion; /* chars that are added, NULL if not applicable */
28 char *display; /* what should be displayed by help/completers */
29 struct ec_keyval *attrs;
32 struct ec_comp *ec_comp(struct ec_parse *state)
34 struct ec_comp *comp = NULL;
36 comp = ec_calloc(1, sizeof(*comp));
40 comp->attrs = ec_keyval();
41 if (comp->attrs == NULL)
44 TAILQ_INIT(&comp->groups);
46 comp->cur_state = state;
52 ec_keyval_free(comp->attrs);
58 struct ec_parse *ec_comp_get_state(struct ec_comp *comp)
60 return comp->cur_state;
64 ec_node_complete_child(const struct ec_node *node,
66 const struct ec_strvec *strvec)
68 struct ec_parse *child_state, *cur_state;
69 struct ec_comp_group *cur_group;
72 if (ec_node_type(node)->complete == NULL)
75 /* save previous parse state, prepare child state */
76 cur_state = comp->cur_state;
77 child_state = ec_parse(node);
78 if (child_state == NULL)
81 if (cur_state != NULL)
82 ec_parse_link_child(cur_state, child_state);
83 comp->cur_state = child_state;
84 cur_group = comp->cur_group;
85 comp->cur_group = NULL;
87 /* fill the comp struct with items */
88 ret = ec_node_type(node)->complete(node, comp, strvec);
90 /* restore parent parse state */
91 if (cur_state != NULL) {
92 ec_parse_unlink_child(cur_state, child_state);
93 assert(!ec_parse_has_child(child_state));
95 ec_parse_free(child_state);
96 comp->cur_state = cur_state;
97 comp->cur_group = cur_group;
105 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
106 const struct ec_strvec *strvec)
108 struct ec_comp *comp = NULL;
111 comp = ec_comp(NULL);
115 ret = ec_node_complete_child(node, comp, strvec);
126 struct ec_comp *ec_node_complete(const struct ec_node *node,
129 struct ec_strvec *strvec = NULL;
130 struct ec_comp *comp;
133 strvec = ec_strvec();
137 if (ec_strvec_add(strvec, str) < 0)
140 comp = ec_node_complete_strvec(node, strvec);
144 ec_strvec_free(strvec);
148 ec_strvec_free(strvec);
152 static struct ec_comp_group *
153 ec_comp_group(const struct ec_node *node, struct ec_parse *parse)
155 struct ec_comp_group *grp = NULL;
157 grp = ec_calloc(1, sizeof(*grp));
161 grp->attrs = ec_keyval();
162 if (grp->attrs == NULL)
165 grp->state = ec_parse_dup(parse);
166 if (grp->state == NULL)
170 TAILQ_INIT(&grp->items);
176 ec_parse_free(grp->state);
177 ec_keyval_free(grp->attrs);
183 static struct ec_comp_item *
184 ec_comp_item(const struct ec_node *node, enum ec_comp_type type,
185 const char *start, const char *full)
187 struct ec_comp_item *item = NULL;
188 struct ec_keyval *attrs = NULL;
189 char *comp_cp = NULL, *start_cp = NULL;
190 char *full_cp = NULL, *display_cp = NULL;
192 if (type == EC_COMP_UNKNOWN && full != NULL) {
196 if (type != EC_COMP_UNKNOWN && full == NULL) {
201 item = ec_calloc(1, sizeof(*item));
210 start_cp = ec_strdup(start);
211 if (start_cp == NULL)
214 if (ec_str_startswith(full, start)) {
215 comp_cp = ec_strdup(&full[strlen(start)]);
221 full_cp = ec_strdup(full);
224 display_cp = ec_strdup(full);
225 if (display_cp == NULL)
231 item->start = start_cp;
232 item->full = full_cp;
233 item->completion = comp_cp;
234 item->display = display_cp;
240 ec_keyval_free(attrs);
250 int ec_comp_item_set_display(struct ec_comp_item *item,
253 char *display_copy = NULL;
256 if (item == NULL || display == NULL ||
257 item->type == EC_COMP_UNKNOWN)
260 display_copy = ec_strdup(display);
261 if (display_copy == NULL)
264 ec_free(item->display);
265 item->display = display_copy;
270 ec_free(display_copy);
275 ec_comp_item_set_completion(struct ec_comp_item *item,
276 const char *completion)
278 char *completion_copy = NULL;
281 if (item == NULL || completion == NULL ||
282 item->type == EC_COMP_UNKNOWN)
286 completion_copy = ec_strdup(completion);
287 if (completion_copy == NULL)
290 ec_free(item->completion);
291 item->completion = completion_copy;
296 ec_free(completion_copy);
301 ec_comp_item_set_str(struct ec_comp_item *item,
304 char *str_copy = NULL;
307 if (item == NULL || str == NULL ||
308 item->type == EC_COMP_UNKNOWN)
312 str_copy = ec_strdup(str);
313 if (str_copy == NULL)
317 item->full = str_copy;
327 ec_comp_item_add(struct ec_comp *comp,
328 struct ec_comp_item *item)
330 if (comp == NULL || item == NULL || item->node == NULL)
333 switch (item->type) {
334 case EC_COMP_UNKNOWN:
335 comp->count_unknown++;
340 case EC_COMP_PARTIAL:
341 comp->count_partial++;
347 if (comp->cur_group == NULL) {
348 struct ec_comp_group *grp;
350 grp = ec_comp_group(item->node, comp->cur_state);
353 TAILQ_INSERT_TAIL(&comp->groups, grp, next);
354 comp->cur_group = grp;
358 TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next);
359 item->grp = comp->cur_group;
365 ec_comp_item_get_str(const struct ec_comp_item *item)
371 ec_comp_item_get_display(const struct ec_comp_item *item)
373 return item->display;
377 ec_comp_item_get_completion(const struct ec_comp_item *item)
379 return item->completion;
383 ec_comp_item_get_type(const struct ec_comp_item *item)
388 const struct ec_node *
389 ec_comp_item_get_node(const struct ec_comp_item *item)
394 const struct ec_comp_group *
395 ec_comp_item_get_grp(const struct ec_comp_item *item)
401 ec_comp_item_free(struct ec_comp_item *item)
407 ec_free(item->start);
408 ec_free(item->completion);
409 ec_free(item->display);
410 ec_keyval_free(item->attrs);
414 int ec_comp_add_item(struct ec_comp *comp,
415 const struct ec_node *node,
416 struct ec_comp_item **p_item,
417 enum ec_comp_type type,
418 const char *start, const char *full)
420 struct ec_comp_item *item = NULL;
423 item = ec_comp_item(node, type, start, full);
427 ret = ec_comp_item_add(comp, item);
437 ec_comp_item_free(item);
442 /* return a completion item of type "unknown" */
444 ec_node_complete_unknown(const struct ec_node *gen_node,
445 struct ec_comp *comp,
446 const struct ec_strvec *strvec)
450 if (ec_strvec_len(strvec) != 1)
453 ret = ec_comp_add_item(comp, gen_node, NULL,
454 EC_COMP_UNKNOWN, NULL, NULL);
461 static void ec_comp_group_free(struct ec_comp_group *grp)
463 struct ec_comp_item *item;
468 while (!TAILQ_EMPTY(&grp->items)) {
469 item = TAILQ_FIRST(&grp->items);
470 TAILQ_REMOVE(&grp->items, item, next);
471 ec_comp_item_free(item);
473 ec_parse_free(ec_parse_get_root(grp->state));
474 ec_keyval_free(grp->attrs);
478 void ec_comp_free(struct ec_comp *comp)
480 struct ec_comp_group *grp;
485 while (!TAILQ_EMPTY(&comp->groups)) {
486 grp = TAILQ_FIRST(&comp->groups);
487 TAILQ_REMOVE(&comp->groups, grp, next);
488 ec_comp_group_free(grp);
490 ec_keyval_free(comp->attrs);
494 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
496 struct ec_comp_group *grp;
497 struct ec_comp_item *item;
499 if (comp == NULL || comp->count == 0) {
500 fprintf(out, "no completion\n");
504 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
505 comp->count, comp->count_full,
506 comp->count_partial, comp->count_unknown);
508 TAILQ_FOREACH(grp, &comp->groups, next) {
509 fprintf(out, "node=%p, node_type=%s\n",
510 grp->node, ec_node_type(grp->node)->name);
511 TAILQ_FOREACH(item, &grp->items, next) {
514 switch (item->type) {
515 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
516 case EC_COMP_FULL: typestr = "full"; break;
517 case EC_COMP_PARTIAL: typestr = "partial"; break;
518 default: typestr = "unknown"; break;
521 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
522 typestr, item->full, item->completion,
528 int ec_comp_merge(struct ec_comp *to,
529 struct ec_comp *from)
531 struct ec_comp_group *grp;
533 while (!TAILQ_EMPTY(&from->groups)) {
534 grp = TAILQ_FIRST(&from->groups);
535 TAILQ_REMOVE(&from->groups, grp, next);
536 TAILQ_INSERT_TAIL(&to->groups, grp, next);
538 to->count += from->count;
539 to->count_full += from->count_full;
540 to->count_partial += from->count_partial;
541 to->count_unknown += from->count_unknown;
547 unsigned int ec_comp_count(
548 const struct ec_comp *comp,
549 enum ec_comp_type type)
551 unsigned int count = 0;
556 if (type & EC_COMP_FULL)
557 count += comp->count_full;
558 if (type & EC_COMP_PARTIAL)
559 count += comp->count_partial;
560 if (type & EC_COMP_UNKNOWN)
561 count += comp->count_unknown;
566 struct ec_comp_iter *
567 ec_comp_iter(struct ec_comp *comp,
568 enum ec_comp_type type)
570 struct ec_comp_iter *iter;
572 iter = ec_calloc(1, sizeof(*iter));
578 iter->cur_node = NULL;
579 iter->cur_match = NULL;
584 struct ec_comp_item *ec_comp_iter_next(
585 struct ec_comp_iter *iter)
587 struct ec_comp *comp;
588 struct ec_comp_group *cur_node;
589 struct ec_comp_item *cur_match;
597 cur_node = iter->cur_node;
598 cur_match = iter->cur_match;
601 if (cur_node == NULL) {
602 TAILQ_FOREACH(cur_node, &comp->groups, next) {
603 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
604 if (cur_match != NULL &&
605 cur_match->type & iter->type)
611 cur_match = TAILQ_NEXT(cur_match, next);
612 if (cur_match != NULL &&
613 cur_match->type & iter->type)
615 cur_node = TAILQ_NEXT(cur_node, next);
616 while (cur_node != NULL) {
617 cur_match = TAILQ_FIRST(&cur_node->items);
618 if (cur_match != NULL &&
619 cur_match->type & iter->type)
621 cur_node = TAILQ_NEXT(cur_node, next);
627 iter->cur_node = cur_node;
628 iter->cur_match = cur_match;
630 return iter->cur_match;
633 void ec_comp_iter_free(struct ec_comp_iter *iter)