2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <ecoli_malloc.h>
35 #include <ecoli_string.h>
36 #include <ecoli_strvec.h>
37 #include <ecoli_keyval.h>
38 #include <ecoli_log.h>
39 #include <ecoli_node.h>
40 #include <ecoli_parsed.h>
41 #include <ecoli_completed.h>
43 struct ec_completed_item {
44 TAILQ_ENTRY(ec_completed_item) next;
45 enum ec_completed_type type;
46 const struct ec_node *node;
47 struct ec_completed_group *grp;
48 char *start; /* the initial token */
49 char *full; /* the full token after completion */
50 char *completion; /* chars that are added, NULL if not applicable */
51 char *display; /* what should be displayed by help/completers */
52 struct ec_keyval *attrs;
55 struct ec_completed *ec_completed(struct ec_parsed *state)
57 struct ec_completed *completed = NULL;
59 completed = ec_calloc(1, sizeof(*completed));
60 if (completed == NULL)
63 TAILQ_INIT(&completed->groups);
65 completed->cur_state = state;
75 struct ec_parsed *ec_completed_get_state(struct ec_completed *completed)
77 return completed->cur_state;
81 ec_node_complete_child(struct ec_node *node, struct ec_completed *completed,
82 const struct ec_strvec *strvec)
84 struct ec_parsed *child_state, *cur_state;
85 struct ec_completed_group *cur_group;
88 /* build the node if required */
89 if (node->type->build != NULL) {
90 if ((node->flags & EC_NODE_F_BUILT) == 0) {
91 ret = node->type->build(node);
96 node->flags |= EC_NODE_F_BUILT;
98 if (node->type->complete == NULL)
101 /* save previous parse state, prepare child state */
102 cur_state = completed->cur_state;
103 child_state = ec_parsed();
104 if (child_state == NULL)
107 if (cur_state != NULL)
108 ec_parsed_add_child(cur_state, child_state);
109 ec_parsed_set_node(child_state, node);
110 completed->cur_state = child_state;
111 cur_group = completed->cur_group;
112 completed->cur_group = NULL;
114 /* fill the completed struct with items */
115 ret = node->type->complete(node, completed, strvec);
117 /* restore parent parse state */
118 if (cur_state != NULL) {
119 ec_parsed_del_child(cur_state, child_state);
120 assert(!ec_parsed_has_child(child_state));
122 ec_parsed_free(child_state);
123 completed->cur_state = cur_state;
124 completed->cur_group = cur_group;
130 printf("----------------------------------------------------------\n");
131 ec_node_dump(stdout, node);
132 ec_strvec_dump(stdout, strvec);
133 ec_completed_dump(stdout, completed);
139 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
140 const struct ec_strvec *strvec)
142 struct ec_completed *completed = NULL;
145 completed = ec_completed(NULL);
146 if (completed == NULL)
149 ret = ec_node_complete_child(node, completed, strvec);
156 ec_completed_free(completed);
160 struct ec_completed *ec_node_complete(struct ec_node *node,
163 struct ec_strvec *strvec = NULL;
164 struct ec_completed *completed;
167 strvec = ec_strvec();
171 if (ec_strvec_add(strvec, str) < 0)
174 completed = ec_node_complete_strvec(node, strvec);
175 if (completed == NULL)
178 ec_strvec_free(strvec);
182 ec_strvec_free(strvec);
186 static struct ec_completed_group *
187 ec_completed_group(const struct ec_node *node, struct ec_parsed *parsed)
189 struct ec_completed_group *grp = NULL;
191 grp = ec_calloc(1, sizeof(*grp));
195 grp->attrs = ec_keyval();
196 if (grp->attrs == NULL)
199 grp->state = ec_parsed_dup(parsed);
200 if (grp->state == NULL)
204 TAILQ_INIT(&grp->items);
210 ec_parsed_free(grp->state);
211 ec_keyval_free(grp->attrs);
217 static struct ec_completed_item *
218 ec_completed_item(const struct ec_node *node, enum ec_completed_type type,
219 const char *start, const char *full)
221 struct ec_completed_item *item = NULL;
222 struct ec_keyval *attrs = NULL;
223 char *comp_cp = NULL, *start_cp = NULL;
224 char *full_cp = NULL, *display_cp = NULL;
226 if (type == EC_COMP_UNKNOWN && full != NULL) {
230 if (type != EC_COMP_UNKNOWN && full == NULL) {
235 item = ec_calloc(1, sizeof(*item));
244 start_cp = ec_strdup(start);
245 if (start_cp == NULL)
248 if (ec_str_startswith(full, start)) {
249 comp_cp = ec_strdup(&full[strlen(start)]);
255 full_cp = ec_strdup(full);
258 display_cp = ec_strdup(full);
259 if (display_cp == NULL)
265 item->start = start_cp;
266 item->full = full_cp;
267 item->completion = comp_cp;
268 item->display = display_cp;
274 ec_keyval_free(item->attrs);
284 int ec_completed_item_set_display(struct ec_completed_item *item,
287 char *display_copy = NULL;
290 if (item == NULL || display == NULL ||
291 item->type == EC_COMP_UNKNOWN)
294 display_copy = ec_strdup(display);
295 if (display_copy == NULL)
298 ec_free(item->display);
299 item->display = display_copy;
304 ec_free(display_copy);
309 ec_completed_item_set_completion(struct ec_completed_item *item,
310 const char *completion)
312 char *completion_copy = NULL;
315 if (item == NULL || completion == NULL ||
316 item->type == EC_COMP_UNKNOWN)
320 completion_copy = ec_strdup(completion);
321 if (completion_copy == NULL)
324 ec_free(item->completion);
325 item->completion = completion_copy;
330 ec_free(completion_copy);
335 ec_completed_item_set_str(struct ec_completed_item *item,
338 char *str_copy = NULL;
341 if (item == NULL || str == NULL ||
342 item->type == EC_COMP_UNKNOWN)
346 str_copy = ec_strdup(str);
347 if (str_copy == NULL)
351 item->full = str_copy;
361 ec_completed_item_add(struct ec_completed *completed,
362 struct ec_completed_item *item)
364 if (completed == NULL || item == NULL || item->node == NULL)
367 switch (item->type) {
368 case EC_COMP_UNKNOWN:
371 case EC_COMP_PARTIAL:
372 completed->count_match++; //XXX
378 if (completed->cur_group == NULL) {
379 struct ec_completed_group *grp;
381 grp = ec_completed_group(item->node, completed->cur_state);
384 TAILQ_INSERT_TAIL(&completed->groups, grp, next);
385 completed->cur_group = grp;
389 TAILQ_INSERT_TAIL(&completed->cur_group->items, item, next);
390 item->grp = completed->cur_group;
396 ec_completed_item_get_str(const struct ec_completed_item *item)
402 ec_completed_item_get_display(const struct ec_completed_item *item)
404 return item->display;
408 ec_completed_item_get_completion(const struct ec_completed_item *item)
410 return item->completion;
413 enum ec_completed_type
414 ec_completed_item_get_type(const struct ec_completed_item *item)
419 const struct ec_node *
420 ec_completed_item_get_node(const struct ec_completed_item *item)
425 const struct ec_completed_group *
426 ec_completed_item_get_grp(const struct ec_completed_item *item)
432 ec_completed_item_free(struct ec_completed_item *item)
438 ec_free(item->start);
439 ec_free(item->completion);
440 ec_free(item->display);
441 ec_keyval_free(item->attrs);
445 int ec_completed_add_item(struct ec_completed *completed,
446 const struct ec_node *node,
447 struct ec_completed_item **p_item,
448 enum ec_completed_type type,
449 const char *start, const char *full)
451 struct ec_completed_item *item = NULL;
454 item = ec_completed_item(node, type, start, full);
458 ret = ec_completed_item_add(completed, item);
468 ec_completed_item_free(item);
473 /* default completion function: return a no-match element */
475 ec_node_default_complete(const struct ec_node *gen_node, // XXX rename in nomatch
476 struct ec_completed *completed,
477 const struct ec_strvec *strvec)
481 if (ec_strvec_len(strvec) != 1)
484 ret = ec_completed_add_item(completed, gen_node, NULL,
485 EC_COMP_UNKNOWN, NULL, NULL);
492 static void ec_completed_group_free(struct ec_completed_group *grp)
494 struct ec_completed_item *item;
499 while (!TAILQ_EMPTY(&grp->items)) {
500 item = TAILQ_FIRST(&grp->items);
501 TAILQ_REMOVE(&grp->items, item, next);
502 ec_completed_item_free(item);
504 ec_parsed_free(ec_parsed_get_root(grp->state));
505 ec_keyval_free(grp->attrs);
509 void ec_completed_free(struct ec_completed *completed)
511 struct ec_completed_group *grp;
513 if (completed == NULL)
516 while (!TAILQ_EMPTY(&completed->groups)) {
517 grp = TAILQ_FIRST(&completed->groups);
518 TAILQ_REMOVE(&completed->groups, grp, next);
519 ec_completed_group_free(grp);
524 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
526 struct ec_completed_group *grp;
527 struct ec_completed_item *item;
529 if (completed == NULL || completed->count == 0) {
530 fprintf(out, "no completion\n");
534 fprintf(out, "completion: count=%u match=%u\n",
535 completed->count, completed->count_match);
537 TAILQ_FOREACH(grp, &completed->groups, next) {
538 fprintf(out, "node=%p, node_type=%s\n",
539 grp->node, grp->node->type->name);
540 TAILQ_FOREACH(item, &grp->items, next) {
543 switch (item->type) {
544 case EC_COMP_UNKNOWN: typestr = "unknown"; break;
545 case EC_COMP_FULL: typestr = "full"; break;
546 case EC_COMP_PARTIAL: typestr = "partial"; break;
547 default: typestr = "unknown"; break;
550 fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n",
551 typestr, item->full, item->completion,
557 int ec_completed_merge(struct ec_completed *to,
558 struct ec_completed *from)
560 struct ec_completed_group *grp;
562 while (!TAILQ_EMPTY(&from->groups)) {
563 grp = TAILQ_FIRST(&from->groups);
564 TAILQ_REMOVE(&from->groups, grp, next);
565 TAILQ_INSERT_TAIL(&to->groups, grp, next);
567 to->count += from->count;
568 to->count_match += from->count_match;
570 ec_completed_free(from);
574 unsigned int ec_completed_count(
575 const struct ec_completed *completed,
576 enum ec_completed_type type)
578 unsigned int count = 0;
580 if (completed == NULL)
583 if (type & EC_COMP_FULL)
584 count += completed->count_match;
585 if (type & EC_COMP_UNKNOWN)
586 count += (completed->count - completed->count_match); //XXX
591 struct ec_completed_iter *
592 ec_completed_iter(struct ec_completed *completed,
593 enum ec_completed_type type)
595 struct ec_completed_iter *iter;
597 iter = ec_calloc(1, sizeof(*iter));
601 iter->completed = completed;
603 iter->cur_node = NULL;
604 iter->cur_match = NULL;
609 struct ec_completed_item *ec_completed_iter_next(
610 struct ec_completed_iter *iter)
612 struct ec_completed *completed = iter->completed;
613 struct ec_completed_group *cur_node;
614 struct ec_completed_item *cur_match;
616 if (completed == NULL)
619 cur_node = iter->cur_node;
620 cur_match = iter->cur_match;
623 if (cur_node == NULL) {
624 TAILQ_FOREACH(cur_node, &completed->groups, next) {
625 TAILQ_FOREACH(cur_match, &cur_node->items, next) {
626 if (cur_match != NULL &&
627 cur_match->type & iter->type)
633 cur_match = TAILQ_NEXT(cur_match, next);
634 if (cur_match != NULL &&
635 cur_match->type & iter->type)
637 cur_node = TAILQ_NEXT(cur_node, next);
638 while (cur_node != NULL) {
639 cur_match = TAILQ_FIRST(&cur_node->items);
640 if (cur_match != NULL &&
641 cur_match->type & iter->type)
643 cur_node = TAILQ_NEXT(cur_node, next);
649 iter->cur_node = cur_node;
650 iter->cur_match = cur_match;
652 return iter->cur_match;
655 void ec_completed_iter_free(struct ec_completed_iter *iter)