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_parsed.h>
18 #include <ecoli_completed.h>
20 struct ec_completed_item {
21 TAILQ_ENTRY(ec_completed_item) next;
22 enum ec_completed_type type;
23 const struct ec_node *node;
24 struct ec_completed_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_completed *ec_completed(struct ec_parsed *state)
34 struct ec_completed *completed = NULL;
36 completed = ec_calloc(1, sizeof(*completed));
37 if (completed == NULL)
40 completed->attrs = ec_keyval();
41 if (completed->attrs == NULL)
44 TAILQ_INIT(&completed->groups);
46 completed->cur_state = state;
51 if (completed != NULL)
52 ec_keyval_free(completed->attrs);
58 struct ec_parsed *ec_completed_get_state(struct ec_completed *completed)
60 return completed->cur_state;
64 ec_node_complete_child(const struct ec_node *node,
65 struct ec_completed *completed,
66 const struct ec_strvec *strvec)
68 struct ec_parsed *child_state, *cur_state;
69 struct ec_completed_group *cur_group;
72 if (node->type->complete == NULL)
75 /* save previous parse state, prepare child state */
76 cur_state = completed->cur_state;
77 child_state = ec_parsed(node);
78 if (child_state == NULL)
81 if (cur_state != NULL)
82 ec_parsed_add_child(cur_state, child_state);
83 completed->cur_state = child_state;
84 cur_group = completed->cur_group;
85 completed->cur_group = NULL;
87 /* fill the completed struct with items */
88 ret = node->type->complete(node, completed, strvec);
90 /* restore parent parse state */
91 if (cur_state != NULL) {
92 ec_parsed_del_child(cur_state, child_state);
93 assert(!ec_parsed_has_child(child_state));
95 ec_parsed_free(child_state);
96 completed->cur_state = cur_state;
97 completed->cur_group = cur_group;
105 struct ec_completed *ec_node_complete_strvec(const struct ec_node *node,
106 const struct ec_strvec *strvec)
108 struct ec_completed *completed = NULL;
111 completed = ec_completed(NULL);
112 if (completed == NULL)
115 ret = ec_node_complete_child(node, completed, strvec);
122 ec_completed_free(completed);
126 struct ec_completed *ec_node_complete(const struct ec_node *node,
129 struct ec_strvec *strvec = NULL;
130 struct ec_completed *completed;
133 strvec = ec_strvec();
137 if (ec_strvec_add(strvec, str) < 0)
140 completed = ec_node_complete_strvec(node, strvec);
141 if (completed == NULL)
144 ec_strvec_free(strvec);
148 ec_strvec_free(strvec);
152 static struct ec_completed_group *
153 ec_completed_group(const struct ec_node *node, struct ec_parsed *parsed)
155 struct ec_completed_group *grp = NULL;
157 grp = ec_calloc(1, sizeof(*grp));
161 grp->attrs = ec_keyval();
162 if (grp->attrs == NULL)
165 grp->state = ec_parsed_dup(parsed);
166 if (grp->state == NULL)
170 TAILQ_INIT(&grp->items);
176 ec_parsed_free(grp->state);
177 ec_keyval_free(grp->attrs);
183 static struct ec_completed_item *
184 ec_completed_item(const struct ec_node *node, enum ec_completed_type type,
185 const char *start, const char *full)
187 struct ec_completed_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_completed_item_set_display(struct ec_completed_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_completed_item_set_completion(struct ec_completed_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_completed_item_set_str(struct ec_completed_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_completed_item_add(struct ec_completed *completed,
328 struct ec_completed_item *item)
330 if (completed == NULL || item == NULL || item->node == NULL)
333 switch (item->type) {
334 case EC_COMP_UNKNOWN:
335 completed->count_unknown++;
338 completed->count_full++;
340 case EC_COMP_PARTIAL:
341 completed->count_partial++;
347 if (completed->cur_group == NULL) {
348 struct ec_completed_group *grp;
350 grp = ec_completed_group(item->node, completed->cur_state);
353 TAILQ_INSERT_TAIL(&completed->groups, grp, next);
354 completed->cur_group = grp;
358 TAILQ_INSERT_TAIL(&completed->cur_group->items, item, next);
359 item->grp = completed->cur_group;
365 ec_completed_item_get_str(const struct ec_completed_item *item)
371 ec_completed_item_get_display(const struct ec_completed_item *item)
373 return item->display;
377 ec_completed_item_get_completion(const struct ec_completed_item *item)
379 return item->completion;
382 enum ec_completed_type
383 ec_completed_item_get_type(const struct ec_completed_item *item)
388 const struct ec_node *
389 ec_completed_item_get_node(const struct ec_completed_item *item)
394 const struct ec_completed_group *
395 ec_completed_item_get_grp(const struct ec_completed_item *item)
401 ec_completed_item_free(struct ec_completed_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_completed_add_item(struct ec_completed *completed,
415 const struct ec_node *node,
416 struct ec_completed_item **p_item,
417 enum ec_completed_type type,
418 const char *start, const char *full)
420 struct ec_completed_item *item = NULL;
423 item = ec_completed_item(node, type, start, full);
427 ret = ec_completed_item_add(completed, item);
437 ec_completed_item_free(item);
442 /* default completion function: return a no-match element */
444 ec_node_default_complete(const struct ec_node *gen_node, // XXX rename in nomatch
445 struct ec_completed *completed,
446 const struct ec_strvec *strvec)
450 if (ec_strvec_len(strvec) != 1)
453 ret = ec_completed_add_item(completed, gen_node, NULL,
454 EC_COMP_UNKNOWN, NULL, NULL);
461 static void ec_completed_group_free(struct ec_completed_group *grp)
463 struct ec_completed_item *item;
468 while (!TAILQ_EMPTY(&grp->items)) {
469 item = TAILQ_FIRST(&grp->items);
470 TAILQ_REMOVE(&grp->items, item, next);
471 ec_completed_item_free(item);
473 ec_parsed_free(ec_parsed_get_root(grp->state));
474 ec_keyval_free(grp->attrs);
478 void ec_completed_free(struct ec_completed *completed)
480 struct ec_completed_group *grp;
482 if (completed == NULL)
485 while (!TAILQ_EMPTY(&completed->groups)) {
486 grp = TAILQ_FIRST(&completed->groups);
487 TAILQ_REMOVE(&completed->groups, grp, next);
488 ec_completed_group_free(grp);
490 ec_keyval_free(completed->attrs);
494 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
496 struct ec_completed_group *grp;
497 struct ec_completed_item *item;
499 if (completed == NULL || completed->count == 0) {
500 fprintf(out, "no completion\n");
504 fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
505 completed->count, completed->count_full,
506 completed->count_partial, completed->count_unknown);
508 TAILQ_FOREACH(grp, &completed->groups, next) {
509 fprintf(out, "node=%p, node_type=%s\n",
510 grp->node, grp->node->type->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_completed_merge(struct ec_completed *to,
529 struct ec_completed *from)
531 struct ec_completed_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;
543 ec_completed_free(from);
547 unsigned int ec_completed_count(
548 const struct ec_completed *completed,
549 enum ec_completed_type type)
551 unsigned int count = 0;
553 if (completed == NULL)
556 if (type & EC_COMP_FULL)
557 count += completed->count_full;
558 if (type & EC_COMP_PARTIAL)
559 count += completed->count_partial;
560 if (type & EC_COMP_UNKNOWN)
561 count += completed->count_unknown;
566 struct ec_completed_iter *
567 ec_completed_iter(struct ec_completed *completed,
568 enum ec_completed_type type)
570 struct ec_completed_iter *iter;
572 iter = ec_calloc(1, sizeof(*iter));
576 iter->completed = completed;
578 iter->cur_node = NULL;
579 iter->cur_match = NULL;
584 struct ec_completed_item *ec_completed_iter_next(
585 struct ec_completed_iter *iter)
587 struct ec_completed *completed;
588 struct ec_completed_group *cur_node;
589 struct ec_completed_item *cur_match;
593 completed = iter->completed;
594 if (completed == NULL)
597 cur_node = iter->cur_node;
598 cur_match = iter->cur_match;
601 if (cur_node == NULL) {
602 TAILQ_FOREACH(cur_node, &completed->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_completed_iter_free(struct ec_completed_iter *iter)