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_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>
42 struct ec_completed *ec_completed(void)
44 struct ec_completed *completed = NULL;
46 completed = ec_calloc(1, sizeof(*completed));
47 if (completed == NULL)
50 TAILQ_INIT(&completed->elts);
51 completed->count_match = 0;
56 struct ec_completed_elt *ec_completed_elt(const struct ec_node *node,
59 struct ec_completed_elt *elt = NULL;
61 elt = ec_calloc(1, sizeof(*elt));
67 elt->add = ec_strdup(add);
68 if (elt->add == NULL) {
69 ec_completed_elt_free(elt);
77 /* XXX define when to use ec_node_complete() or node->complete()
79 * suggestion: node->op() is internal, user calls the function
80 * other idea: have 2 functions
82 struct ec_completed *ec_node_complete(struct ec_node *node,
85 struct ec_strvec *strvec = NULL;
86 struct ec_completed *completed;
93 if (ec_strvec_add(strvec, str) < 0)
96 completed = ec_node_complete_strvec(node, strvec);
97 if (completed == NULL)
100 ec_strvec_free(strvec);
104 ec_strvec_free(strvec);
108 /* default completion function: return a no-match element */
109 struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
110 const struct ec_strvec *strvec)
112 struct ec_completed *completed;
113 struct ec_completed_elt *completed_elt;
117 completed = ec_completed();
118 if (completed == NULL)
121 completed_elt = ec_completed_elt(gen_node, NULL);
122 if (completed_elt == NULL) {
123 ec_completed_free(completed);
127 ec_completed_add_elt(completed, completed_elt);
132 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
133 const struct ec_strvec *strvec)
137 /* build the node if required */
138 if (node->type->build != NULL) {
139 if ((node->flags & EC_NODE_F_BUILT) == 0) {
140 ret = node->type->build(node);
147 node->flags |= EC_NODE_F_BUILT;
149 if (node->type->complete == NULL) {
154 return node->type->complete(node, strvec);
157 /* count the number of identical chars at the beginning of 2 strings */
158 static size_t strcmp_count(const char *s1, const char *s2)
162 while (s1[i] && s2[i] && s1[i] == s2[i])
168 void ec_completed_add_elt(
169 struct ec_completed *completed, struct ec_completed_elt *elt)
173 TAILQ_INSERT_TAIL(&completed->elts, elt, next);
175 if (elt->add != NULL)
176 completed->count_match++;
177 if (elt->add != NULL) {
178 if (completed->smallest_start == NULL) {
179 completed->smallest_start = ec_strdup(elt->add);
181 n = strcmp_count(elt->add,
182 completed->smallest_start);
183 completed->smallest_start[n] = '\0';
188 void ec_completed_elt_free(struct ec_completed_elt *elt)
194 void ec_completed_merge(struct ec_completed *completed1,
195 struct ec_completed *completed2)
197 struct ec_completed_elt *elt;
199 assert(completed1 != NULL);
200 assert(completed2 != NULL);
202 while (!TAILQ_EMPTY(&completed2->elts)) {
203 elt = TAILQ_FIRST(&completed2->elts);
204 TAILQ_REMOVE(&completed2->elts, elt, next);
205 ec_completed_add_elt(completed1, elt);
208 ec_completed_free(completed2);
211 void ec_completed_free(struct ec_completed *completed)
213 struct ec_completed_elt *elt;
215 if (completed == NULL)
218 while (!TAILQ_EMPTY(&completed->elts)) {
219 elt = TAILQ_FIRST(&completed->elts);
220 TAILQ_REMOVE(&completed->elts, elt, next);
221 ec_completed_elt_free(elt);
223 ec_free(completed->smallest_start);
227 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
229 struct ec_completed_elt *elt;
231 if (completed == NULL || completed->count == 0) {
232 fprintf(out, "no completion\n");
236 fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
237 completed->count, completed->count_match,
238 completed->smallest_start);
240 TAILQ_FOREACH(elt, &completed->elts, next) {
241 fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
242 elt->add, elt->node, elt->node->type->name);
246 const char *ec_completed_smallest_start(
247 const struct ec_completed *completed)
249 if (completed == NULL || completed->smallest_start == NULL)
252 return completed->smallest_start;
255 unsigned int ec_completed_count(
256 const struct ec_completed *completed,
257 enum ec_completed_filter_flags flags)
259 unsigned int count = 0;
261 if (completed == NULL)
264 if (flags & EC_MATCH)
265 count += completed->count_match;
266 if (flags & EC_NO_MATCH)
267 count += (completed->count - completed->count_match); //XXX
272 struct ec_completed_iter *
273 ec_completed_iter(struct ec_completed *completed,
274 enum ec_completed_filter_flags flags)
276 struct ec_completed_iter *iter;
278 iter = ec_calloc(1, sizeof(*iter));
282 iter->completed = completed;
289 const struct ec_completed_elt *ec_completed_iter_next(
290 struct ec_completed_iter *iter)
292 if (iter->completed == NULL)
296 if (iter->cur == NULL) {
297 iter->cur = TAILQ_FIRST(&iter->completed->elts);
299 iter->cur = TAILQ_NEXT(iter->cur, next);
302 if (iter->cur == NULL)
305 if (iter->cur->add == NULL &&
306 (iter->flags & EC_NO_MATCH))
309 if (iter->cur->add != NULL &&
310 (iter->flags & EC_MATCH))
313 } while (iter->cur != NULL);
318 void ec_completed_iter_free(struct ec_completed_iter *iter)