537de572c2875b4bee4cb046d9879106f68b2fd3
[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_comp *comp;
43         const struct ec_node *node;
44         struct ec_comp_item_list items;
45         struct ec_pnode *pstate;
46         struct ec_dict *attrs;
47 };
48
49 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
50
51 struct ec_comp {
52         size_t count;
53         size_t count_full;
54         size_t count_partial;
55         size_t count_unknown;
56         struct ec_pnode *cur_pstate;
57         struct ec_comp_group *cur_group;
58         struct ec_comp_group_list groups;
59         struct ec_dict *attrs;
60 };
61
62 struct ec_comp *ec_comp(void)
63 {
64         struct ec_comp *comp = NULL;
65
66         comp = ec_calloc(1, sizeof(*comp));
67         if (comp == NULL)
68                 goto fail;
69
70         comp->attrs = ec_dict();
71         if (comp->attrs == NULL)
72                 goto fail;
73
74         TAILQ_INIT(&comp->groups);
75
76         return comp;
77
78  fail:
79         if (comp != NULL)
80                 ec_dict_free(comp->attrs);
81         ec_free(comp);
82
83         return NULL;
84 }
85
86 struct ec_pnode *ec_comp_get_cur_pstate(const struct ec_comp *comp)
87 {
88         return comp->cur_pstate;
89 }
90
91 struct ec_comp_group *ec_comp_get_cur_group(const struct ec_comp *comp)
92 {
93         return comp->cur_group;
94 }
95
96 struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp)
97 {
98         return comp->attrs;
99 }
100
101 int
102 ec_complete_child(const struct ec_node *node,
103                 struct ec_comp *comp,
104                 const struct ec_strvec *strvec)
105 {
106         struct ec_pnode *child_pstate, *cur_pstate;
107         struct ec_comp_group *cur_group;
108         ec_complete_t complete_cb;
109         int ret;
110
111         /* get the complete method, falling back to ec_complete_unknown() */
112         complete_cb = ec_node_type(node)->complete;
113         if (complete_cb == NULL)
114                 complete_cb = ec_complete_unknown;
115
116         /* save previous parse state, prepare child state */
117         cur_pstate = comp->cur_pstate;
118         child_pstate = ec_pnode(node);
119         if (child_pstate == NULL)
120                 return -1;
121
122         if (cur_pstate != NULL)
123                 ec_pnode_link_child(cur_pstate, child_pstate);
124         comp->cur_pstate = child_pstate;
125         cur_group = comp->cur_group;
126         comp->cur_group = NULL;
127
128         /* fill the comp struct with items */
129         ret = complete_cb(node, comp, strvec);
130
131         /* restore parent parse state */
132         if (cur_pstate != NULL) {
133                 ec_pnode_unlink_child(cur_pstate, child_pstate);
134                 assert(!ec_pnode_has_child(child_pstate));
135         }
136         ec_pnode_free(child_pstate);
137         comp->cur_pstate = cur_pstate;
138         comp->cur_group = cur_group;
139
140         if (ret < 0)
141                 return -1;
142
143         return 0;
144 }
145
146 struct ec_comp *ec_complete_strvec(const struct ec_node *node,
147         const struct ec_strvec *strvec)
148 {
149         struct ec_comp *comp = NULL;
150         int ret;
151
152         comp = ec_comp();
153         if (comp == NULL)
154                 goto fail;
155
156         ret = ec_complete_child(node, comp, strvec);
157         if (ret < 0)
158                 goto fail;
159
160         return comp;
161
162 fail:
163         ec_comp_free(comp);
164         return NULL;
165 }
166
167 struct ec_comp *ec_complete(const struct ec_node *node,
168         const char *str)
169 {
170         struct ec_strvec *strvec = NULL;
171         struct ec_comp *comp;
172
173         errno = ENOMEM;
174         strvec = ec_strvec();
175         if (strvec == NULL)
176                 goto fail;
177
178         if (ec_strvec_add(strvec, str) < 0)
179                 goto fail;
180
181         comp = ec_complete_strvec(node, strvec);
182         if (comp == NULL)
183                 goto fail;
184
185         ec_strvec_free(strvec);
186         return comp;
187
188  fail:
189         ec_strvec_free(strvec);
190         return NULL;
191 }
192
193 static struct ec_comp_group *
194 ec_comp_group(const struct ec_comp *comp, const struct ec_node *node,
195         struct ec_pnode *parse)
196 {
197         struct ec_comp_group *grp = NULL;
198
199         grp = ec_calloc(1, sizeof(*grp));
200         if (grp == NULL)
201                 return NULL;
202
203         grp->comp = comp;
204         grp->attrs = ec_dict();
205         if (grp->attrs == NULL)
206                 goto fail;
207
208         grp->pstate = ec_pnode_dup(parse);
209         if (grp->pstate == 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->pstate);
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(comp, node, comp->cur_pstate);
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 struct ec_comp_item *ec_comp_add_item(struct ec_comp *comp,
461                 const struct ec_node *node, enum ec_comp_type type,
462                 const char *start, const char *full)
463 {
464         struct ec_comp_item *item = NULL;
465         int ret;
466
467         item = ec_comp_item(type, start, full);
468         if (item == NULL)
469                 return NULL;
470
471         ret = ec_comp_item_add(comp, node, item);
472         if (ret < 0)
473                 goto fail;
474
475         return item;
476
477 fail:
478         ec_comp_item_free(item);
479         return NULL;
480 }
481
482 /* return a completion item of type "unknown" */
483 int
484 ec_complete_unknown(const struct ec_node *gen_node,
485                         struct ec_comp *comp,
486                         const struct ec_strvec *strvec)
487 {
488         const struct ec_comp_item *item = NULL;
489
490         if (ec_strvec_len(strvec) != 1)
491                 return 0;
492
493         item = ec_comp_add_item(comp, gen_node, EC_COMP_UNKNOWN, NULL, NULL);
494         if (item == NULL)
495                 return -1;
496
497         return 0;
498 }
499
500 static void ec_comp_group_free(struct ec_comp_group *grp)
501 {
502         struct ec_comp_item *item;
503
504         if (grp == NULL)
505                 return;
506
507         while (!TAILQ_EMPTY(&grp->items)) {
508                 item = TAILQ_FIRST(&grp->items);
509                 TAILQ_REMOVE(&grp->items, item, next);
510                 ec_comp_item_free(item);
511         }
512         ec_pnode_free(ec_pnode_get_root(grp->pstate));
513         ec_dict_free(grp->attrs);
514         ec_free(grp);
515 }
516
517 const struct ec_node *
518 ec_comp_group_get_node(const struct ec_comp_group *grp)
519 {
520         return grp->node;
521 }
522
523 const struct ec_pnode *
524 ec_comp_group_get_pstate(const struct ec_comp_group *grp)
525 {
526         return grp->pstate;
527 }
528
529 const struct ec_dict *
530 ec_comp_group_get_attrs(const struct ec_comp_group *grp)
531 {
532         return grp->attrs;
533 }
534
535 void ec_comp_free(struct ec_comp *comp)
536 {
537         struct ec_comp_group *grp;
538
539         if (comp == NULL)
540                 return;
541
542         while (!TAILQ_EMPTY(&comp->groups)) {
543                 grp = TAILQ_FIRST(&comp->groups);
544                 TAILQ_REMOVE(&comp->groups, grp, next);
545                 ec_comp_group_free(grp);
546         }
547         ec_dict_free(comp->attrs);
548         ec_free(comp);
549 }
550
551 void ec_comp_dump(FILE *out, const struct ec_comp *comp)
552 {
553         struct ec_comp_group *grp;
554         struct ec_comp_item *item;
555
556         if (comp == NULL || comp->count == 0) {
557                 fprintf(out, "no completion\n");
558                 return;
559         }
560
561         fprintf(out, "completion: count=%zu full=%zu partial=%zu unknown=%zu\n",
562                 comp->count, comp->count_full,
563                 comp->count_partial,  comp->count_unknown);
564
565         TAILQ_FOREACH(grp, &comp->groups, next) {
566                 fprintf(out, "node=%p, node_type=%s\n",
567                         grp->node, ec_node_type(grp->node)->name);
568                 TAILQ_FOREACH(item, &grp->items, next) {
569                         const char *typestr;
570
571                         switch (item->type) {
572                         case EC_COMP_UNKNOWN: typestr = "unknown"; break;
573                         case EC_COMP_FULL: typestr = "full"; break;
574                         case EC_COMP_PARTIAL: typestr = "partial"; break;
575                         default: typestr = "unknown"; break;
576                         }
577
578                         fprintf(out, "  type=%s str=<%s> comp=<%s> disp=<%s>\n",
579                                 typestr, item->full, item->completion,
580                                 item->display);
581                 }
582         }
583 }
584
585 int ec_comp_merge(struct ec_comp *to,
586                 struct ec_comp *from)
587 {
588         struct ec_comp_group *grp;
589
590         while (!TAILQ_EMPTY(&from->groups)) {
591                 grp = TAILQ_FIRST(&from->groups);
592                 TAILQ_REMOVE(&from->groups, grp, next);
593                 TAILQ_INSERT_TAIL(&to->groups, grp, next);
594         }
595         to->count += from->count;
596         to->count_full += from->count_full;
597         to->count_partial += from->count_partial;
598         to->count_unknown += from->count_unknown;
599
600         ec_comp_free(from);
601         return 0;
602 }
603
604 size_t ec_comp_count(const struct ec_comp *comp, enum ec_comp_type type)
605 {
606         size_t count = 0;
607
608         if (comp == NULL)
609                 return count;
610
611         if (type & EC_COMP_FULL)
612                 count += comp->count_full;
613         if (type & EC_COMP_PARTIAL)
614                 count += comp->count_partial;
615         if (type & EC_COMP_UNKNOWN)
616                 count += comp->count_unknown;
617
618         return count;
619 }
620
621 static struct ec_comp_item *
622 __ec_comp_iter_next(const struct ec_comp *comp, struct ec_comp_item *item,
623                 enum ec_comp_type type)
624 {
625         struct ec_comp_group *cur_grp;
626         struct ec_comp_item *cur_match;
627
628         /* first call */
629         if (item == NULL) {
630                 TAILQ_FOREACH(cur_grp, &comp->groups, next) {
631                         TAILQ_FOREACH(cur_match, &cur_grp->items, next) {
632                                 if (cur_match->type & type)
633                                         return cur_match;
634                         }
635                 }
636                 return NULL;
637         }
638
639         cur_grp = item->grp;
640         cur_match = TAILQ_NEXT(item, next);
641         while (cur_match != NULL) {
642                 if (cur_match->type & type)
643                         return cur_match;
644                 cur_match = TAILQ_NEXT(cur_match, next);
645         }
646         cur_grp = TAILQ_NEXT(cur_grp, next);
647         while (cur_grp != NULL) {
648                 TAILQ_FOREACH(cur_match, &cur_grp->items, next) {
649                         if (cur_match->type & type)
650                                 return cur_match;
651                 }
652         }
653
654         return NULL;
655 }
656
657 struct ec_comp_item *
658 ec_comp_iter_next(struct ec_comp_item *item, enum ec_comp_type type)
659 {
660         if (item == NULL)
661                 return NULL;
662         return __ec_comp_iter_next(item->grp->comp, item, type);
663 }
664
665
666 struct ec_comp_item *
667 ec_comp_iter_first(const struct ec_comp *comp, enum ec_comp_type type)
668 {
669         return __ec_comp_iter_next(comp, NULL, type);
670 }
671
672 /* LCOV_EXCL_START */
673 static int ec_comp_testcase(void)
674 {
675         struct ec_node *node = NULL;
676         struct ec_comp *c = NULL;
677         struct ec_comp_item *item;
678         FILE *f = NULL;
679         char *buf = NULL;
680         size_t buflen = 0;
681         int testres = 0;
682
683         node = ec_node_sh_lex(EC_NO_ID,
684                         EC_NODE_OR(EC_NO_ID,
685                                 ec_node_str("id_x", "xx"),
686                                 ec_node_str("id_y", "yy")));
687         if (node == NULL)
688                 goto fail;
689
690         c = ec_complete(node, "xcdscds");
691         testres |= EC_TEST_CHECK(
692                 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
693                 "complete count should is not 0\n");
694         ec_comp_free(c);
695
696         c = ec_complete(node, "x");
697         testres |= EC_TEST_CHECK(
698                 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
699                 "complete count should is not 1\n");
700         ec_comp_free(c);
701
702         c = ec_complete(node, "");
703         testres |= EC_TEST_CHECK(
704                 c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
705                 "complete count should is not 2\n");
706
707         f = open_memstream(&buf, &buflen);
708         if (f == NULL)
709                 goto fail;
710         ec_comp_dump(f, NULL);
711         fclose(f);
712         f = NULL;
713
714         testres |= EC_TEST_CHECK(
715                 strstr(buf, "no completion"), "bad dump\n");
716         free(buf);
717         buf = NULL;
718
719         f = open_memstream(&buf, &buflen);
720         if (f == NULL)
721                 goto fail;
722         ec_comp_dump(f, c);
723         fclose(f);
724         f = NULL;
725
726         testres |= EC_TEST_CHECK(
727                 strstr(buf, "comp=<xx>"), "bad dump\n");
728         testres |= EC_TEST_CHECK(
729                 strstr(buf, "comp=<yy>"), "bad dump\n");
730         free(buf);
731         buf = NULL;
732
733         item = ec_comp_iter_first(c, EC_COMP_ALL);
734         if (item == NULL)
735                 goto fail;
736
737         testres |= EC_TEST_CHECK(
738                 !strcmp(ec_comp_item_get_display(item), "xx"),
739                 "bad item display\n");
740         testres |= EC_TEST_CHECK(
741                 ec_comp_item_get_type(item) == EC_COMP_FULL,
742                 "bad item type\n");
743         testres |= EC_TEST_CHECK(
744                 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
745                 "bad item node\n");
746
747         item = ec_comp_iter_next(item, EC_COMP_ALL);
748         if (item == NULL)
749                 goto fail;
750
751         testres |= EC_TEST_CHECK(
752                 !strcmp(ec_comp_item_get_display(item), "yy"),
753                 "bad item display\n");
754         testres |= EC_TEST_CHECK(
755                 ec_comp_item_get_type(item) == EC_COMP_FULL,
756                 "bad item type\n");
757         testres |= EC_TEST_CHECK(
758                 !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
759                 "bad item node\n");
760
761         item = ec_comp_iter_next(item, EC_COMP_ALL);
762         testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
763
764         ec_comp_free(c);
765         ec_node_free(node);
766
767         return testres;
768
769 fail:
770         ec_comp_free(c);
771         ec_node_free(node);
772         if (f != NULL)
773                 fclose(f);
774         free(buf);
775
776         return -1;
777 }
778 /* LCOV_EXCL_STOP */
779
780 static struct ec_test ec_comp_test = {
781         .name = "comp",
782         .test = ec_comp_testcase,
783 };
784
785 EC_TEST_REGISTER(ec_comp_test);