rework completion iterators
[protos/libecoli.git] / src / ecoli_complete.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <errno.h>
10
11 #include <ecoli_malloc.h>
12 #include <ecoli_string.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_dict.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>
23
24 EC_LOG_TYPE_REGISTER(comp);
25
26 struct ec_comp_item {
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_dict *attrs;
35 };
36
37 TAILQ_HEAD(ec_comp_item_list, ec_comp_item);
38
39 struct ec_comp_group {
40         /* XXX counts ? */
41         TAILQ_ENTRY(ec_comp_group) next;
42         const struct ec_node *node;
43         struct ec_comp_item_list items;
44         struct ec_pnode *state;
45         struct ec_dict *attrs;
46 };
47
48 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
49
50 struct ec_comp {
51         unsigned count;
52         unsigned count_full;
53         unsigned count_partial;
54         unsigned count_unknown;
55         struct ec_pnode *cur_state;
56         struct ec_comp_group *cur_group;
57         struct ec_comp_group_list groups;
58         struct ec_dict *attrs;
59 };
60
61 struct ec_comp *ec_comp(struct ec_pnode *state)
62 {
63         struct ec_comp *comp = NULL;
64
65         comp = ec_calloc(1, sizeof(*comp));
66         if (comp == NULL)
67                 goto fail;
68
69         comp->attrs = ec_dict();
70         if (comp->attrs == NULL)
71                 goto fail;
72
73         TAILQ_INIT(&comp->groups);
74
75         comp->cur_state = state;
76
77         return comp;
78
79  fail:
80         if (comp != NULL)
81                 ec_dict_free(comp->attrs);
82         ec_free(comp);
83
84         return NULL;
85 }
86
87 struct ec_pnode *ec_comp_get_state(const struct ec_comp *comp)
88 {
89         return comp->cur_state;
90 }
91
92 struct ec_comp_group *ec_comp_get_group(const struct ec_comp *comp)
93 {
94         return comp->cur_group;
95 }
96
97 struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp)
98 {
99         return comp->attrs;
100 }
101
102 int
103 ec_complete_child(const struct ec_node *node,
104                 struct ec_comp *comp,
105                 const struct ec_strvec *strvec)
106 {
107         struct ec_pnode *child_state, *cur_state;
108         struct ec_comp_group *cur_group;
109         int ret;
110
111         //XXX call ec_complete_unknown() instead, as
112         //described in API doc.
113         if (ec_node_type(node)->complete == NULL) {
114                 errno = ENOTSUP;
115                 return -1;
116         }
117
118         /* save previous parse state, prepare child state */
119         cur_state = comp->cur_state;
120         child_state = ec_pnode(node);
121         if (child_state == NULL)
122                 return -1;
123
124         if (cur_state != NULL)
125                 ec_pnode_link_child(cur_state, child_state);
126         comp->cur_state = child_state;
127         cur_group = comp->cur_group;
128         comp->cur_group = NULL;
129
130         /* fill the comp struct with items */
131         ret = ec_node_type(node)->complete(node, comp, strvec);
132
133         /* restore parent parse state */
134         if (cur_state != NULL) {
135                 ec_pnode_unlink_child(cur_state, child_state);
136                 assert(!ec_pnode_has_child(child_state));
137         }
138         ec_pnode_free(child_state);
139         comp->cur_state = cur_state;
140         comp->cur_group = cur_group;
141
142         if (ret < 0)
143                 return -1;
144
145         return 0;
146 }
147
148 struct ec_comp *ec_complete_strvec(const struct ec_node *node,
149         const struct ec_strvec *strvec)
150 {
151         struct ec_comp *comp = NULL;
152         int ret;
153
154         comp = ec_comp(NULL);
155         if (comp == NULL)
156                 goto fail;
157
158         ret = ec_complete_child(node, comp, strvec);
159         if (ret < 0)
160                 goto fail;
161
162         return comp;
163
164 fail:
165         ec_comp_free(comp);
166         return NULL;
167 }
168
169 struct ec_comp *ec_complete(const struct ec_node *node,
170         const char *str)
171 {
172         struct ec_strvec *strvec = NULL;
173         struct ec_comp *comp;
174
175         errno = ENOMEM;
176         strvec = ec_strvec();
177         if (strvec == NULL)
178                 goto fail;
179
180         if (ec_strvec_add(strvec, str) < 0)
181                 goto fail;
182
183         comp = ec_complete_strvec(node, strvec);
184         if (comp == NULL)
185                 goto fail;
186
187         ec_strvec_free(strvec);
188         return comp;
189
190  fail:
191         ec_strvec_free(strvec);
192         return NULL;
193 }
194
195 static struct ec_comp_group *
196 ec_comp_group(const struct ec_node *node, struct ec_pnode *parse)
197 {
198         struct ec_comp_group *grp = NULL;
199
200         grp = ec_calloc(1, sizeof(*grp));
201         if (grp == NULL)
202                 return NULL;
203
204         grp->attrs = ec_dict();
205         if (grp->attrs == NULL)
206                 goto fail;
207
208         grp->state = ec_pnode_dup(parse);
209         if (grp->state == NULL)
210                 goto fail;
211
212         grp->node = node;
213         TAILQ_INIT(&grp->items);
214
215         return grp;
216
217 fail:
218         if (grp != NULL) {
219                 ec_pnode_free(grp->state);
220                 ec_dict_free(grp->attrs);
221         }
222         ec_free(grp);
223         return NULL;
224 }
225
226 static struct ec_comp_item *
227 ec_comp_item(enum ec_comp_type type,
228         const char *start, const char *full)
229 {
230         struct ec_comp_item *item = NULL;
231         struct ec_dict *attrs = NULL;
232         char *comp_cp = NULL, *start_cp = NULL;
233         char *full_cp = NULL, *display_cp = NULL;
234
235         if (type == EC_COMP_UNKNOWN && full != NULL) {
236                 errno = EINVAL;
237                 return NULL;
238         }
239         if (type != EC_COMP_UNKNOWN && full == NULL) {
240                 errno = EINVAL;
241                 return NULL;
242         }
243
244         item = ec_calloc(1, sizeof(*item));
245         if (item == NULL)
246                 goto fail;
247
248         attrs = ec_dict();
249         if (attrs == NULL)
250                 goto fail;
251
252         if (start != NULL) {
253                 start_cp = ec_strdup(start);
254                 if (start_cp == NULL)
255                         goto fail;
256
257                 if (ec_str_startswith(full, start)) {
258                         comp_cp = ec_strdup(&full[strlen(start)]);
259                         if (comp_cp == NULL)
260                                 goto fail;
261                 }
262         }
263         if (full != NULL) {
264                 full_cp = ec_strdup(full);
265                 if (full_cp == NULL)
266                         goto fail;
267                 display_cp = ec_strdup(full);
268                 if (display_cp == NULL)
269                         goto fail;
270         }
271
272         item->type = type;
273         item->start = start_cp;
274         item->full = full_cp;
275         item->completion = comp_cp;
276         item->display = display_cp;
277         item->attrs = attrs;
278
279         return item;
280
281 fail:
282         ec_dict_free(attrs);
283         ec_free(comp_cp);
284         ec_free(start_cp);
285         ec_free(full_cp);
286         ec_free(display_cp);
287         ec_free(item);
288
289         return NULL;
290 }
291
292 int ec_comp_item_set_display(struct ec_comp_item *item,
293                                 const char *display)
294 {
295         char *display_copy = NULL;
296
297         if (item == NULL || display == NULL ||
298                         item->type == EC_COMP_UNKNOWN) {
299                 errno = EINVAL;
300                 return -1;
301         }
302
303         display_copy = ec_strdup(display);
304         if (display_copy == NULL)
305                 goto fail;
306
307         ec_free(item->display);
308         item->display = display_copy;
309
310         return 0;
311
312 fail:
313         ec_free(display_copy);
314         return -1;
315 }
316
317 int
318 ec_comp_item_set_completion(struct ec_comp_item *item,
319                                 const char *completion)
320 {
321         char *completion_copy = NULL;
322
323         if (item == NULL || completion == NULL ||
324                         item->type == EC_COMP_UNKNOWN) {
325                 errno = EINVAL;
326                 return -1;
327         }
328
329         completion_copy = ec_strdup(completion);
330         if (completion_copy == NULL)
331                 goto fail;
332
333         ec_free(item->completion);
334         item->completion = completion_copy;
335
336         return 0;
337
338 fail:
339         ec_free(completion_copy);
340         return -1;
341 }
342
343 int
344 ec_comp_item_set_str(struct ec_comp_item *item,
345                         const char *str)
346 {
347         char *str_copy = NULL;
348
349         if (item == NULL || str == NULL ||
350                         item->type == EC_COMP_UNKNOWN) {
351                 errno = EINVAL;
352                 return -1;
353         }
354
355         str_copy = ec_strdup(str);
356         if (str_copy == NULL)
357                 goto fail;
358
359         ec_free(item->full);
360         item->full = str_copy;
361
362         return 0;
363
364 fail:
365         ec_free(str_copy);
366         return -1;
367 }
368
369 static int
370 ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
371                 struct ec_comp_item *item)
372 {
373         if (comp == NULL || item == NULL) {
374                 errno = EINVAL;
375                 return -1;
376         }
377
378         switch (item->type) {
379         case EC_COMP_UNKNOWN:
380                 comp->count_unknown++;
381                 break;
382         case EC_COMP_FULL:
383                 comp->count_full++;
384                 break;
385         case EC_COMP_PARTIAL:
386                 comp->count_partial++;
387                 break;
388         default:
389                 errno = EINVAL;
390                 return -1;
391         }
392
393         if (comp->cur_group == NULL) {
394                 struct ec_comp_group *grp;
395
396                 grp = ec_comp_group(node, comp->cur_state);
397                 if (grp == NULL)
398                         return -1;
399                 TAILQ_INSERT_TAIL(&comp->groups, grp, next);
400                 comp->cur_group = grp;
401         }
402
403         comp->count++;
404         TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next);
405         item->grp = comp->cur_group;
406
407         return 0;
408 }
409
410 const char *
411 ec_comp_item_get_str(const struct ec_comp_item *item)
412 {
413         return item->full;
414 }
415
416 const char *
417 ec_comp_item_get_display(const struct ec_comp_item *item)
418 {
419         return item->display;
420 }
421
422 const char *
423 ec_comp_item_get_completion(const struct ec_comp_item *item)
424 {
425         return item->completion;
426 }
427
428 enum ec_comp_type
429 ec_comp_item_get_type(const struct ec_comp_item *item)
430 {
431         return item->type;
432 }
433
434 const struct ec_comp_group *
435 ec_comp_item_get_grp(const struct ec_comp_item *item)
436 {
437         return item->grp;
438 }
439
440 const struct ec_node *
441 ec_comp_item_get_node(const struct ec_comp_item *item)
442 {
443         return ec_comp_item_get_grp(item)->node;
444 }
445
446 static void
447 ec_comp_item_free(struct ec_comp_item *item)
448 {
449         if (item == NULL)
450                 return;
451
452         ec_free(item->full);
453         ec_free(item->start);
454         ec_free(item->completion);
455         ec_free(item->display);
456         ec_dict_free(item->attrs);
457         ec_free(item);
458 }
459
460 int ec_comp_add_item(struct ec_comp *comp,
461                         const struct ec_node *node,
462                         struct ec_comp_item **p_item,
463                         enum ec_comp_type type,
464                         const char *start, const char *full)
465 {
466         struct ec_comp_item *item = NULL;
467         int ret;
468
469         item = ec_comp_item(type, start, full);
470         if (item == NULL)
471                 return -1;
472
473         ret = ec_comp_item_add(comp, node, item);
474         if (ret < 0)
475                 goto fail;
476
477         if (p_item != NULL)
478                 *p_item = item;
479
480         return 0;
481
482 fail:
483         ec_comp_item_free(item);
484
485         return -1;
486 }
487
488 /* XXX move in helpers + rename ? */
489 /* return a completion item of type "unknown" */
490 int
491 ec_complete_unknown(const struct ec_node *gen_node,
492                         struct ec_comp *comp,
493                         const struct ec_strvec *strvec)
494 {
495         int ret;
496
497         if (ec_strvec_len(strvec) != 1)
498                 return 0;
499
500         ret = ec_comp_add_item(comp, gen_node, NULL,
501                                 EC_COMP_UNKNOWN, NULL, NULL);
502         if (ret < 0)
503                 return ret;
504
505         return 0;
506 }
507
508 static void ec_comp_group_free(struct ec_comp_group *grp)
509 {
510         struct ec_comp_item *item;
511
512         if (grp == NULL)
513                 return;
514
515         while (!TAILQ_EMPTY(&grp->items)) {
516                 item = TAILQ_FIRST(&grp->items);
517                 TAILQ_REMOVE(&grp->items, item, next);
518                 ec_comp_item_free(item);
519         }
520         ec_pnode_free(ec_pnode_get_root(grp->state));
521         ec_dict_free(grp->attrs);
522         ec_free(grp);
523 }
524
525 const struct ec_node *
526 ec_comp_group_get_node(const struct ec_comp_group *grp)
527 {
528         return grp->node;
529 }
530
531 const struct ec_pnode *
532 ec_comp_group_get_state(const struct ec_comp_group *grp)
533 {
534         return grp->state;
535 }
536
537 const struct ec_dict *
538 ec_comp_group_get_attrs(const struct ec_comp_group *grp)
539 {
540         return grp->attrs;
541 }
542
543 void ec_comp_free(struct ec_comp *comp)
544 {
545         struct ec_comp_group *grp;
546
547         if (comp == NULL)
548                 return;
549
550         while (!TAILQ_EMPTY(&comp->groups)) {
551                 grp = TAILQ_FIRST(&comp->groups);
552                 TAILQ_REMOVE(&comp->groups, grp, next);
553                 ec_comp_group_free(grp);
554         }
555         ec_dict_free(comp->attrs);
556         ec_free(comp);
557 }
558
559 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
560 {
561         struct ec_comp_group *grp;
562         struct ec_comp_item *item;
563
564         if (comp == NULL || comp->count == 0) {
565                 fprintf(out, "no completion\n");
566                 return;
567         }
568
569         fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n",
570                 comp->count, comp->count_full,
571                 comp->count_partial,  comp->count_unknown);
572
573         TAILQ_FOREACH(grp, &comp->groups, next) {
574                 fprintf(out, "node=%p, node_type=%s\n",
575                         grp->node, ec_node_type(grp->node)->name);
576                 TAILQ_FOREACH(item, &grp->items, next) {
577                         const char *typestr;
578
579                         switch (item->type) {
580                         case EC_COMP_UNKNOWN: typestr = "unknown"; break;
581                         case EC_COMP_FULL: typestr = "full"; break;
582                         case EC_COMP_PARTIAL: typestr = "partial"; break;
583                         default: typestr = "unknown"; break;
584                         }
585
586                         fprintf(out, "  type=%s str=<%s> comp=<%s> disp=<%s>\n",
587                                 typestr, item->full, item->completion,
588                                 item->display);
589                 }
590         }
591 }
592
593 int ec_comp_merge(struct ec_comp *to,
594                 struct ec_comp *from)
595 {
596         struct ec_comp_group *grp;
597
598         while (!TAILQ_EMPTY(&from->groups)) {
599                 grp = TAILQ_FIRST(&from->groups);
600                 TAILQ_REMOVE(&from->groups, grp, next);
601                 TAILQ_INSERT_TAIL(&to->groups, grp, next);
602         }
603         to->count += from->count;
604         to->count_full += from->count_full;
605         to->count_partial += from->count_partial;
606         to->count_unknown += from->count_unknown;
607
608         ec_comp_free(from);
609         return 0;
610 }
611
612 unsigned int ec_comp_count(
613         const struct ec_comp *comp,
614         enum ec_comp_type type)
615 {
616         unsigned int count = 0;
617
618         if (comp == NULL)
619                 return count;
620
621         if (type & EC_COMP_FULL)
622                 count += comp->count_full;
623         if (type & EC_COMP_PARTIAL)
624                 count += comp->count_partial;
625         if (type & EC_COMP_UNKNOWN)
626                 count += comp->count_unknown;
627
628         return count;
629 }
630
631 static struct ec_comp_item *
632 __ec_comp_iter_next(const struct ec_comp *comp, struct ec_comp_item *item,
633                 enum ec_comp_type type)
634 {
635         struct ec_comp_group *cur_grp;
636         struct ec_comp_item *cur_match;
637
638         /* first call */
639         if (item == NULL) {
640                 TAILQ_FOREACH(cur_grp, &comp->groups, next) {
641                         TAILQ_FOREACH(cur_match, &cur_grp->items, next) {
642                                 if (cur_match->type & type)
643                                         return cur_match;
644                         }
645                 }
646                 return NULL;
647         }
648
649         cur_grp = item->grp;
650         cur_match = TAILQ_NEXT(item, next);
651         while (cur_match != NULL) {
652                 if (cur_match->type & type)
653                         return cur_match;
654                 cur_match = TAILQ_NEXT(cur_match, next);
655         }
656         cur_grp = TAILQ_NEXT(cur_grp, next);
657         while (cur_grp != NULL) {
658                 TAILQ_FOREACH(cur_match, &cur_grp->items, next) {
659                         if (cur_match->type & type)
660                                 return cur_match;
661                 }
662         }
663
664         return NULL;
665 }
666
667 struct ec_comp_item *
668 ec_comp_iter_next(struct ec_comp_item *item, enum ec_comp_type type)
669 {
670         if (item == NULL)
671                 return NULL;
672         return __ec_comp_iter_next(item->grp->comp, item, type);
673 }
674
675
676 struct ec_comp_item *
677 ec_comp_iter_first(const struct ec_comp *comp, enum ec_comp_type type)
678 {
679         return __ec_comp_iter_next(comp, NULL, type);
680 }
681
682 /* LCOV_EXCL_START */
683 static int ec_comp_testcase(void)
684 {
685         struct ec_node *node = NULL;
686         struct ec_comp *c = NULL;
687         struct ec_comp_item *item;
688         FILE *f = NULL;
689         char *buf = NULL;
690         size_t buflen = 0;
691         int testres = 0;
692
693         node = ec_node_sh_lex(EC_NO_ID,
694                         EC_NODE_OR(EC_NO_ID,
695                                 ec_node_str("id_x", "xx"),
696                                 ec_node_str("id_y", "yy")));
697         if (node == NULL)
698                 goto fail;
699
700         c = ec_complete(node, "xcdscds");
701         testres |= EC_TEST_CHECK(
702                 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
703                 "complete count should is not 0\n");
704         ec_comp_free(c);
705
706         c = ec_complete(node, "x");
707         testres |= EC_TEST_CHECK(
708                 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
709                 "complete count should is not 1\n");
710         ec_comp_free(c);
711
712         c = ec_complete(node, "");
713         testres |= EC_TEST_CHECK(
714                 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
715                 "complete count should is not 2\n");
716
717         f = open_memstream(&buf, &buflen);
718         if (f == NULL)
719                 goto fail;
720         ec_comp_dump(f, NULL);
721         fclose(f);
722         f = NULL;
723
724         testres |= EC_TEST_CHECK(
725                 strstr(buf, "no completion"), "bad dump\n");
726         free(buf);
727         buf = NULL;
728
729         f = open_memstream(&buf, &buflen);
730         if (f == NULL)
731                 goto fail;
732         ec_comp_dump(f, c);
733         fclose(f);
734         f = NULL;
735
736         testres |= EC_TEST_CHECK(
737                 strstr(buf, "comp=<xx>"), "bad dump\n");
738         testres |= EC_TEST_CHECK(
739                 strstr(buf, "comp=<yy>"), "bad dump\n");
740         free(buf);
741         buf = NULL;
742
743         item = ec_comp_iter_first(c, EC_COMP_ALL);
744         if (item == NULL)
745                 goto fail;
746
747         testres |= EC_TEST_CHECK(
748                 !strcmp(ec_comp_item_get_display(item), "xx"),
749                 "bad item display\n");
750         testres |= EC_TEST_CHECK(
751                 ec_comp_item_get_type(item) == EC_COMP_FULL,
752                 "bad item type\n");
753         testres |= EC_TEST_CHECK(
754                 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
755                 "bad item node\n");
756
757         item = ec_comp_iter_next(item, EC_COMP_ALL);
758         if (item == NULL)
759                 goto fail;
760
761         testres |= EC_TEST_CHECK(
762                 !strcmp(ec_comp_item_get_display(item), "yy"),
763                 "bad item display\n");
764         testres |= EC_TEST_CHECK(
765                 ec_comp_item_get_type(item) == EC_COMP_FULL,
766                 "bad item type\n");
767         testres |= EC_TEST_CHECK(
768                 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
769                 "bad item node\n");
770
771         item = ec_comp_iter_next(item, EC_COMP_ALL);
772         testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
773
774         ec_comp_free(c);
775         ec_node_free(node);
776
777         return testres;
778
779 fail:
780         ec_comp_free(c);
781         ec_node_free(node);
782         if (f != NULL)
783                 fclose(f);
784         free(buf);
785
786         return -1;
787 }
788 /* LCOV_EXCL_STOP */
789
790 static struct ec_test ec_comp_test = {
791         .name = "comp",
792         .test = ec_comp_testcase,
793 };
794
795 EC_TEST_REGISTER(ec_comp_test);