save
[protos/libecoli.git] / lib / ecoli_completed.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <errno.h>
33
34 #include <ecoli_malloc.h>
35 #include <ecoli_strvec.h>
36 #include <ecoli_keyval.h>
37 #include <ecoli_log.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41
42 struct ec_completed *ec_completed(void)
43 {
44         struct ec_completed *completed = NULL;
45
46         completed = ec_calloc(1, sizeof(*completed));
47         if (completed == NULL)
48                 return NULL;
49
50         TAILQ_INIT(&completed->elts);
51         completed->count_match = 0;
52
53         return completed;
54 }
55
56 struct ec_completed *
57 ec_node_complete_child(struct ec_node *node,
58                 struct ec_parsed *state,
59                 const struct ec_strvec *strvec)
60 {
61         struct ec_completed *completed;
62         struct ec_parsed *child;
63         int ret;
64
65         /* build the node if required */
66         if (node->type->build != NULL) {
67                 if ((node->flags & EC_NODE_F_BUILT) == 0) {
68                         ret = node->type->build(node);
69                         if (ret < 0) {
70                                 errno = -ret;
71                                 return NULL;
72                         }
73                 }
74         }
75         node->flags |= EC_NODE_F_BUILT;
76
77         if (node->type->complete == NULL) {
78                 errno = ENOTSUP;
79                 return NULL;
80         }
81
82         child = ec_parsed();
83         if (child == NULL)
84                 return NULL;
85
86         child->node = node;
87         ec_parsed_add_child(state, child);
88         completed = node->type->complete(node, child, strvec);
89
90 #if 0 // XXX dump
91         printf("----------------------------------------------------------\n");
92         ec_node_dump(stdout, node);
93         ec_strvec_dump(stdout, strvec);
94         ec_completed_dump(stdout, completed);
95         ec_parsed_dump(stdout, state);
96 #endif
97
98         ec_parsed_del_child(state, child);
99         assert(TAILQ_EMPTY(&child->children));
100         ec_parsed_free(child);
101
102         return completed;
103 }
104
105 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
106         const struct ec_strvec *strvec)
107 {
108         struct ec_parsed *state = ec_parsed();
109         struct ec_completed *completed;
110
111         if (state == NULL)
112                 return NULL;
113
114         completed = ec_node_complete_child(node, state, strvec);
115         ec_parsed_free(state);
116
117         return completed;
118 }
119
120 struct ec_completed *ec_node_complete(struct ec_node *node,
121         const char *str)
122 {
123         struct ec_strvec *strvec = NULL;
124         struct ec_completed *completed;
125
126         errno = ENOMEM;
127         strvec = ec_strvec();
128         if (strvec == NULL)
129                 goto fail;
130
131         if (ec_strvec_add(strvec, str) < 0)
132                 goto fail;
133
134         completed = ec_node_complete_strvec(node, strvec);
135         if (completed == NULL)
136                 goto fail;
137
138         ec_strvec_free(strvec);
139         return completed;
140
141  fail:
142         ec_strvec_free(strvec);
143         return NULL;
144 }
145
146 /* default completion function: return a no-match element */
147 struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
148                                         struct ec_parsed *state,
149                                         const struct ec_strvec *strvec)
150 {
151         struct ec_completed *completed;
152
153         (void)strvec;
154         (void)state;
155
156         completed = ec_completed();
157         if (completed == NULL)
158                 return NULL;
159
160         if (ec_strvec_len(strvec) != 1)
161                 return completed;
162
163         if (ec_completed_add_elt(completed, state, gen_node, NULL) < 0) {
164                 ec_completed_free(completed);
165                 return NULL;
166         }
167
168         return completed;
169 }
170
171 /* count the number of identical chars at the beginning of 2 strings */
172 static size_t strcmp_count(const char *s1, const char *s2)
173 {
174         size_t i = 0;
175
176         while (s1[i] && s2[i] && s1[i] == s2[i])
177                 i++;
178
179         return i;
180 }
181
182 static struct ec_completed_elt *
183 ec_completed_elt(struct ec_parsed *parsed,
184                 const struct ec_node *node, const char *add)
185 {
186         struct ec_completed_elt *elt = NULL;
187
188         elt = ec_calloc(1, sizeof(*elt));
189         if (elt == NULL)
190                 return NULL;
191
192         if (parsed != NULL) {
193                 struct ec_parsed *p;
194                 size_t len;
195
196                 /* get path len */
197                 for (p = parsed, len = 0; p != NULL;
198                      p = ec_parsed_get_parent(p), len++)
199                         ;
200
201                 elt->path = ec_calloc(len, sizeof(*elt->path));
202                 if (elt->path == NULL)
203                         goto fail;
204
205                 elt->pathlen = len;
206
207                 /* write path in array */
208                 for (p = parsed, len = 0; p != NULL;
209                      p = ec_parsed_get_parent(p), len++)
210                         elt->path[len] = p->node;
211         }
212
213         elt->node = node;
214         if (add != NULL) {
215                 elt->add = ec_strdup(add);
216                 if (elt->add == NULL)
217                         goto fail;
218         }
219
220         return elt;
221
222 fail:
223         if (elt != NULL) {
224                 ec_free(elt->path);
225                 ec_free(elt->add);
226         }
227         ec_completed_elt_free(elt);
228
229         return NULL;
230 }
231
232 static int __ec_completed_add_elt(struct ec_completed *completed,
233                                 struct ec_completed_elt *elt)
234 {
235         size_t n;
236
237         TAILQ_INSERT_TAIL(&completed->elts, elt, next);
238         completed->count++;
239         if (elt->add != NULL) {
240                 completed->count_match++;
241                 if (completed->smallest_start == NULL) {
242                         completed->smallest_start = ec_strdup(elt->add);
243                 } else {
244                         n = strcmp_count(elt->add,
245                                 completed->smallest_start);
246                         completed->smallest_start[n] = '\0';
247                 }
248         }
249
250         return 0;
251 }
252
253 int ec_completed_add_elt(struct ec_completed *completed,
254                         struct ec_parsed *parsed,
255                         const struct ec_node *node, const char *add)
256 {
257         struct ec_completed_elt *elt;
258
259         elt = ec_completed_elt(parsed, node, add);
260         if (elt == NULL)
261                 return -ENOMEM;
262
263         return __ec_completed_add_elt(completed, elt);
264 }
265
266 void ec_completed_elt_free(struct ec_completed_elt *elt)
267 {
268         ec_free(elt->add);
269         ec_free(elt->path);
270         ec_free(elt);
271 }
272
273 void ec_completed_merge(struct ec_completed *completed1,
274         struct ec_completed *completed2)
275 {
276         struct ec_completed_elt *elt;
277
278         assert(completed1 != NULL);
279         assert(completed2 != NULL);
280
281         while (!TAILQ_EMPTY(&completed2->elts)) {
282                 elt = TAILQ_FIRST(&completed2->elts);
283                 TAILQ_REMOVE(&completed2->elts, elt, next);
284                 __ec_completed_add_elt(completed1, elt);
285         }
286
287         ec_completed_free(completed2);
288 }
289
290 void ec_completed_free(struct ec_completed *completed)
291 {
292         struct ec_completed_elt *elt;
293
294         if (completed == NULL)
295                 return;
296
297         while (!TAILQ_EMPTY(&completed->elts)) {
298                 elt = TAILQ_FIRST(&completed->elts);
299                 TAILQ_REMOVE(&completed->elts, elt, next);
300                 ec_completed_elt_free(elt);
301         }
302         ec_free(completed->smallest_start);
303         ec_free(completed);
304 }
305
306 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
307 {
308         struct ec_completed_elt *elt;
309
310         if (completed == NULL || completed->count == 0) {
311                 fprintf(out, "no completion\n");
312                 return;
313         }
314
315         fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
316                 completed->count, completed->count_match,
317                 completed->smallest_start);
318
319         TAILQ_FOREACH(elt, &completed->elts, next) {
320                 fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
321                         elt->add, elt->node, elt->node->type->name);
322         }
323 }
324
325 const char *ec_completed_smallest_start(
326         const struct ec_completed *completed)
327 {
328         if (completed == NULL || completed->smallest_start == NULL)
329                 return "";
330
331         return completed->smallest_start;
332 }
333
334 unsigned int ec_completed_count(
335         const struct ec_completed *completed,
336         enum ec_completed_filter_flags flags)
337 {
338         unsigned int count = 0;
339
340         if (completed == NULL)
341                 return count;
342
343         if (flags & EC_MATCH)
344                 count += completed->count_match;
345         if (flags & EC_NO_MATCH)
346                 count += (completed->count - completed->count_match); //XXX
347
348         return count;
349 }
350
351 struct ec_completed_iter *
352 ec_completed_iter(struct ec_completed *completed,
353         enum ec_completed_filter_flags flags)
354 {
355         struct ec_completed_iter *iter;
356
357         iter = ec_calloc(1, sizeof(*iter));
358         if (iter == NULL)
359                 return NULL;
360
361         iter->completed = completed;
362         iter->flags = flags;
363         iter->cur = NULL;
364
365         return iter;
366 }
367
368 const struct ec_completed_elt *ec_completed_iter_next(
369         struct ec_completed_iter *iter)
370 {
371         if (iter->completed == NULL)
372                 return NULL;
373
374         do {
375                 if (iter->cur == NULL)
376                         iter->cur = TAILQ_FIRST(&iter->completed->elts);
377                 else
378                         iter->cur = TAILQ_NEXT(iter->cur, next);
379
380                 if (iter->cur == NULL)
381                         break;
382
383                 if (iter->cur->add == NULL &&
384                                 (iter->flags & EC_NO_MATCH))
385                         break;
386
387                 if (iter->cur->add != NULL &&
388                                 (iter->flags & EC_MATCH))
389                         break;
390
391         } while (iter->cur != NULL);
392
393         return iter->cur;
394 }
395
396 void ec_completed_iter_free(struct ec_completed_iter *iter)
397 {
398         ec_free(iter);
399 }