0b0e641c759d58203cd0ebc9ebbf6c730b68bbab
[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->nodes);
51         TAILQ_INIT(&completed->matches);
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 /* count the number of identical chars at the beginning of 2 strings */
147 static size_t strcmp_count(const char *s1, const char *s2)
148 {
149         size_t i = 0;
150
151         while (s1[i] && s2[i] && s1[i] == s2[i])
152                 i++;
153
154         return i;
155 }
156
157 static struct ec_completed_item *
158 ec_completed_item(enum ec_completed_type type, struct ec_parsed *state,
159                 const struct ec_node *node, const char *add)
160 {
161         struct ec_completed_item *item = NULL;
162
163         item = ec_calloc(1, sizeof(*item));
164         if (item == NULL)
165                 return NULL;
166
167         /* XXX can state be NULL? */
168         if (state != NULL) {
169                 struct ec_parsed *p;
170                 size_t len;
171
172                 /* get path len */
173                 for (p = state, len = 0; p != NULL;
174                      p = ec_parsed_get_parent(p), len++)
175                         ;
176
177                 item->path = ec_calloc(len, sizeof(*item->path));
178                 if (item->path == NULL)
179                         goto fail;
180
181                 item->pathlen = len;
182
183                 /* write path in array */
184                 for (p = state, len = 0; p != NULL;
185                      p = ec_parsed_get_parent(p), len++)
186                         item->path[len] = p->node;
187         }
188
189         item->type = type;
190         item->node = node;
191         if (add != NULL) {
192                 item->add = ec_strdup(add);
193                 if (item->add == NULL)
194                         goto fail;
195         }
196
197         return item;
198
199 fail:
200         if (item != NULL) {
201                 ec_free(item->path);
202                 ec_free(item->add);
203         }
204         ec_completed_item_free(item);
205
206         return NULL;
207 }
208
209 static int ec_completed_add_item(struct ec_completed *completed,
210                                 struct ec_completed_item *item)
211 {
212         size_t n;
213
214         if (item->add != NULL) {
215                 if (completed->smallest_start == NULL) {
216                         completed->smallest_start = ec_strdup(item->add);
217                         if (completed->smallest_start == NULL)
218                                 return -ENOMEM;
219                 } else {
220                         n = strcmp_count(item->add,
221                                 completed->smallest_start);
222                         completed->smallest_start[n] = '\0';
223                 }
224                 completed->count_match++;
225         }
226
227         TAILQ_INSERT_TAIL(&completed->matches, item, next);
228         completed->count++;
229
230         return 0;
231 }
232
233 int ec_completed_add_match(struct ec_completed *completed,
234                         struct ec_parsed *state,
235                         const struct ec_node *node, const char *add)
236 {
237         struct ec_completed_item *item;
238         int ret;
239
240         item = ec_completed_item(EC_MATCH, state, node, add);
241         if (item == NULL)
242                 return -ENOMEM;
243
244         ret = ec_completed_add_item(completed, item);
245         if (ret < 0) {
246                 ec_completed_item_free(item);
247                 return ret;
248         }
249
250         return 0;
251 }
252
253 int ec_completed_add_no_match(struct ec_completed *completed,
254                         struct ec_parsed *state, const struct ec_node *node)
255 {
256         struct ec_completed_item *item;
257         int ret;
258
259         item = ec_completed_item(EC_NO_MATCH, state, node, NULL);
260         if (item == NULL)
261                 return -ENOMEM;
262
263         ret = ec_completed_add_item(completed, item);
264         if (ret < 0) {
265                 ec_completed_item_free(item);
266                 return ret;
267         }
268
269         return 0;
270 }
271
272 void ec_completed_item_free(struct ec_completed_item *item)
273 {
274         ec_free(item->add);
275         ec_free(item->path);
276         ec_free(item);
277 }
278
279 /* default completion function: return a no-match element */
280 struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
281                                         struct ec_parsed *state,
282                                         const struct ec_strvec *strvec)
283 {
284         struct ec_completed *completed;
285
286         (void)strvec;
287         (void)state;
288
289         completed = ec_completed();
290         if (completed == NULL)
291                 return NULL;
292
293         if (ec_strvec_len(strvec) != 1)
294                 return completed;
295
296         if (ec_completed_add_no_match(completed, state, gen_node) < 0) {
297                 ec_completed_free(completed);
298                 return NULL;
299         }
300
301         return completed;
302 }
303
304 void ec_completed_merge(struct ec_completed *completed1,
305         struct ec_completed *completed2)
306 {
307         struct ec_completed_item *item;
308
309         assert(completed1 != NULL);
310         assert(completed2 != NULL);
311
312         while (!TAILQ_EMPTY(&completed2->matches)) {
313                 item = TAILQ_FIRST(&completed2->matches);
314                 TAILQ_REMOVE(&completed2->matches, item, next);
315                 ec_completed_add_item(completed1, item);
316         }
317
318         ec_completed_free(completed2);
319 }
320
321 void ec_completed_free(struct ec_completed *completed)
322 {
323         struct ec_completed_item *item;
324
325         if (completed == NULL)
326                 return;
327
328         while (!TAILQ_EMPTY(&completed->matches)) {
329                 item = TAILQ_FIRST(&completed->matches);
330                 TAILQ_REMOVE(&completed->matches, item, next);
331                 ec_completed_item_free(item);
332         }
333         ec_free(completed->smallest_start);
334         ec_free(completed);
335 }
336
337 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
338 {
339         struct ec_completed_item *item;
340
341         if (completed == NULL || completed->count == 0) {
342                 fprintf(out, "no completion\n");
343                 return;
344         }
345
346         fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
347                 completed->count, completed->count_match,
348                 completed->smallest_start);
349
350         TAILQ_FOREACH(item, &completed->matches, next) {
351                 fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
352                         item->add, item->node, item->node->type->name);
353         }
354 }
355
356 const char *ec_completed_smallest_start(
357         const struct ec_completed *completed)
358 {
359         if (completed == NULL || completed->smallest_start == NULL)
360                 return "";
361
362         return completed->smallest_start;
363 }
364
365 unsigned int ec_completed_count(
366         const struct ec_completed *completed,
367         enum ec_completed_type type)
368 {
369         unsigned int count = 0;
370
371         if (completed == NULL)
372                 return count;
373
374         if (type & EC_MATCH)
375                 count += completed->count_match;
376         if (type & EC_NO_MATCH)
377                 count += (completed->count - completed->count_match); //XXX
378
379         return count;
380 }
381
382 struct ec_completed_iter *
383 ec_completed_iter(struct ec_completed *completed,
384         enum ec_completed_type type)
385 {
386         struct ec_completed_iter *iter;
387
388         iter = ec_calloc(1, sizeof(*iter));
389         if (iter == NULL)
390                 return NULL;
391
392         iter->completed = completed;
393         iter->type = type;
394         iter->cur_item = NULL;
395
396         return iter;
397 }
398
399 const struct ec_completed_item *ec_completed_iter_next(
400         struct ec_completed_iter *iter)
401 {
402         const struct ec_completed *completed = iter->completed;
403
404         if (completed == NULL)
405                 return NULL;
406
407         do {
408                 if (iter->cur_item == NULL)
409                         iter->cur_item = TAILQ_FIRST(&completed->matches);
410                 else
411                         iter->cur_item = TAILQ_NEXT(iter->cur_item, next);
412
413                 if (iter->cur_item == NULL)
414                         break;
415
416                 if (iter->cur_item->add == NULL &&
417                                 (iter->type & EC_NO_MATCH))
418                         break;
419
420                 if (iter->cur_item->add != NULL &&
421                                 (iter->type & EC_MATCH))
422                         break;
423
424         } while (iter->cur_item != NULL);
425
426         return iter->cur_item;
427 }
428
429 void ec_completed_iter_free(struct ec_completed_iter *iter)
430 {
431         ec_free(iter);
432 }