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>
40 struct ec_tk *ec_tk_new(const char *id, const struct ec_tk_ops *ops,
43 struct ec_tk *tk = NULL;
46 assert(size >= sizeof(*tk));
48 ec_log(EC_LOG_DEBUG, "create node type=%s id=%s\n", ops->typename, id);
50 tk = ec_calloc(1, size);
54 TAILQ_INIT(&tk->children);
59 tk->id = ec_strdup(id);
64 snprintf(buf, sizeof(buf), "<%s>", ops->typename);
65 tk->desc = ec_strdup(buf); // XXX ec_asprintf ?
69 tk->attrs = ec_keyval_new();
70 if (tk->attrs == NULL)
80 void ec_tk_free(struct ec_tk *tk)
85 assert(tk->refcnt > 0);
90 if (tk->ops != NULL && tk->ops->free_priv != NULL)
91 tk->ops->free_priv(tk);
98 struct ec_tk *ec_tk_clone(struct ec_tk *tk)
105 struct ec_tk *ec_tk_find(struct ec_tk *tk, const char *id)
107 struct ec_tk *child, *ret;
108 const char *tk_id = ec_tk_id(tk);
110 if (id != NULL && tk_id != NULL && !strcmp(tk_id, id))
113 TAILQ_FOREACH(child, &tk->children, next) {
114 ret = ec_tk_find(child, id);
122 struct ec_keyval *ec_tk_attrs(const struct ec_tk *tk)
127 const char *ec_tk_id(const struct ec_tk *tk)
132 struct ec_tk *ec_tk_parent(const struct ec_tk *tk)
137 struct ec_parsed_tk *ec_tk_parse(struct ec_tk *tk, const char *str)
139 struct ec_strvec *strvec = NULL;
140 struct ec_parsed_tk *parsed_tk;
143 strvec = ec_strvec_new();
147 if (ec_strvec_add(strvec, str) < 0)
150 parsed_tk = ec_tk_parse_tokens(tk, strvec);
151 if (parsed_tk == NULL)
154 ec_strvec_free(strvec);
158 ec_strvec_free(strvec);
162 struct ec_parsed_tk *ec_tk_parse_tokens(struct ec_tk *tk,
163 const struct ec_strvec *strvec)
165 struct ec_parsed_tk *parsed_tk;
168 /* build the node if required */
169 if (tk->ops->build != NULL) {
170 if ((tk->flags & EC_TK_F_BUILT) == 0) {
171 ret = tk->ops->build(tk);
178 tk->flags |= EC_TK_F_BUILT;
180 if (tk->ops->parse == NULL) {
185 parsed_tk = tk->ops->parse(tk, strvec);
191 struct ec_parsed_tk *ec_parsed_tk_new(void)
193 struct ec_parsed_tk *parsed_tk = NULL;
195 parsed_tk = ec_calloc(1, sizeof(*parsed_tk));
196 if (parsed_tk == NULL)
199 TAILQ_INIT(&parsed_tk->children);
207 void ec_parsed_tk_set_match(struct ec_parsed_tk *parsed_tk,
208 const struct ec_tk *tk, struct ec_strvec *strvec)
211 parsed_tk->strvec = strvec;
214 void ec_parsed_tk_free_children(struct ec_parsed_tk *parsed_tk)
216 struct ec_parsed_tk *child;
218 if (parsed_tk == NULL)
221 while (!TAILQ_EMPTY(&parsed_tk->children)) {
222 child = TAILQ_FIRST(&parsed_tk->children);
223 TAILQ_REMOVE(&parsed_tk->children, child, next);
224 ec_parsed_tk_free(child);
228 void ec_parsed_tk_free(struct ec_parsed_tk *parsed_tk)
230 if (parsed_tk == NULL)
233 ec_parsed_tk_free_children(parsed_tk);
234 ec_strvec_free(parsed_tk->strvec);
238 static void __ec_parsed_tk_dump(FILE *out,
239 const struct ec_parsed_tk *parsed_tk, size_t indent)
241 struct ec_parsed_tk *child;
242 const struct ec_strvec *vec;
244 const char *id = "None", *typename = "None";
247 for (i = 0; i < indent; i++)
250 if (parsed_tk->tk != NULL) {
251 if (parsed_tk->tk->id != NULL)
252 id = parsed_tk->tk->id;
253 typename = parsed_tk->tk->ops->typename;
256 fprintf(out, "tk_type=%s id=%s vec=[", typename, id);
257 vec = ec_parsed_tk_strvec(parsed_tk);
258 for (i = 0; i < ec_strvec_len(vec); i++)
259 fprintf(out, "%s<%s>",
261 ec_strvec_val(vec, i));
264 TAILQ_FOREACH(child, &parsed_tk->children, next)
265 __ec_parsed_tk_dump(out, child, indent + 2);
268 void ec_parsed_tk_dump(FILE *out, const struct ec_parsed_tk *parsed_tk)
270 if (parsed_tk == NULL) {
271 fprintf(out, "parsed_tk is NULL, error in parse\n");
274 if (!ec_parsed_tk_matches(parsed_tk)) {
275 fprintf(out, "no match\n");
279 __ec_parsed_tk_dump(out, parsed_tk, 0);
282 void ec_parsed_tk_add_child(struct ec_parsed_tk *parsed_tk,
283 struct ec_parsed_tk *child)
285 TAILQ_INSERT_TAIL(&parsed_tk->children, child, next);
288 struct ec_parsed_tk *ec_parsed_tk_find_first(struct ec_parsed_tk *parsed_tk,
291 struct ec_parsed_tk *child, *ret;
293 if (parsed_tk == NULL)
296 if (parsed_tk->tk != NULL &&
297 parsed_tk->tk->id != NULL &&
298 !strcmp(parsed_tk->tk->id, id))
301 TAILQ_FOREACH(child, &parsed_tk->children, next) {
302 ret = ec_parsed_tk_find_first(child, id);
310 const struct ec_strvec *ec_parsed_tk_strvec(
311 const struct ec_parsed_tk *parsed_tk)
313 if (parsed_tk == NULL || parsed_tk->strvec == NULL)
316 return parsed_tk->strvec;
319 /* number of parsed tokens */
320 size_t ec_parsed_tk_len(const struct ec_parsed_tk *parsed_tk)
322 if (parsed_tk == NULL || parsed_tk->strvec == NULL)
325 return ec_strvec_len(parsed_tk->strvec);
328 size_t ec_parsed_tk_matches(const struct ec_parsed_tk *parsed_tk)
330 if (parsed_tk == NULL)
333 if (parsed_tk->tk == NULL && TAILQ_EMPTY(&parsed_tk->children))
339 struct ec_completed_tk *ec_completed_tk_new(void)
341 struct ec_completed_tk *completed_tk = NULL;
343 completed_tk = ec_calloc(1, sizeof(*completed_tk));
344 if (completed_tk == NULL)
347 TAILQ_INIT(&completed_tk->elts);
348 completed_tk->count_match = 0;
353 struct ec_completed_tk_elt *ec_completed_tk_elt_new(const struct ec_tk *tk,
356 struct ec_completed_tk_elt *elt = NULL;
358 elt = ec_calloc(1, sizeof(*elt));
364 elt->add = ec_strdup(add);
365 if (elt->add == NULL) {
366 ec_completed_tk_elt_free(elt);
374 /* XXX define when to use ec_tk_complete() or tk->complete()
376 * suggestion: tk->op() is internal, user calls the function
377 * other idea: have 2 functions
379 struct ec_completed_tk *ec_tk_complete(const struct ec_tk *tk,
382 struct ec_strvec *strvec = NULL;
383 struct ec_completed_tk *completed_tk;
386 strvec = ec_strvec_new();
390 if (ec_strvec_add(strvec, str) < 0)
393 completed_tk = ec_tk_complete_tokens(tk, strvec);
394 if (completed_tk == NULL)
397 ec_strvec_free(strvec);
401 ec_strvec_free(strvec);
405 /* default completion function: return a no-match element */
406 struct ec_completed_tk *ec_tk_default_complete(const struct ec_tk *gen_tk,
407 const struct ec_strvec *strvec)
409 struct ec_completed_tk *completed_tk;
410 struct ec_completed_tk_elt *completed_tk_elt;
414 completed_tk = ec_completed_tk_new();
415 if (completed_tk == NULL)
418 completed_tk_elt = ec_completed_tk_elt_new(gen_tk, NULL);
419 if (completed_tk_elt == NULL) {
420 ec_completed_tk_free(completed_tk);
424 ec_completed_tk_add_elt(completed_tk, completed_tk_elt);
429 struct ec_completed_tk *ec_tk_complete_tokens(const struct ec_tk *tk,
430 const struct ec_strvec *strvec)
432 return tk->ops->complete(tk, strvec);
435 /* count the number of identical chars at the beginning of 2 strings */
436 static size_t strcmp_count(const char *s1, const char *s2)
440 while (s1[i] && s2[i] && s1[i] == s2[i])
446 void ec_completed_tk_add_elt(
447 struct ec_completed_tk *completed_tk, struct ec_completed_tk_elt *elt)
451 TAILQ_INSERT_TAIL(&completed_tk->elts, elt, next);
452 completed_tk->count++;
453 if (elt->add != NULL)
454 completed_tk->count_match++;
455 if (elt->add != NULL) {
456 if (completed_tk->smallest_start == NULL) {
457 completed_tk->smallest_start = ec_strdup(elt->add);
459 n = strcmp_count(elt->add,
460 completed_tk->smallest_start);
461 completed_tk->smallest_start[n] = '\0';
466 void ec_completed_tk_elt_free(struct ec_completed_tk_elt *elt)
472 void ec_completed_tk_merge(struct ec_completed_tk *completed_tk1,
473 struct ec_completed_tk *completed_tk2)
475 struct ec_completed_tk_elt *elt;
477 assert(completed_tk1 != NULL);
478 assert(completed_tk2 != NULL);
480 while (!TAILQ_EMPTY(&completed_tk2->elts)) {
481 elt = TAILQ_FIRST(&completed_tk2->elts);
482 TAILQ_REMOVE(&completed_tk2->elts, elt, next);
483 ec_completed_tk_add_elt(completed_tk1, elt);
486 ec_completed_tk_free(completed_tk2);
489 void ec_completed_tk_free(struct ec_completed_tk *completed_tk)
491 struct ec_completed_tk_elt *elt;
493 if (completed_tk == NULL)
496 while (!TAILQ_EMPTY(&completed_tk->elts)) {
497 elt = TAILQ_FIRST(&completed_tk->elts);
498 TAILQ_REMOVE(&completed_tk->elts, elt, next);
499 ec_completed_tk_elt_free(elt);
501 ec_free(completed_tk->smallest_start);
502 ec_free(completed_tk);
505 void ec_completed_tk_dump(FILE *out, const struct ec_completed_tk *completed_tk)
507 struct ec_completed_tk_elt *elt;
509 if (completed_tk == NULL || completed_tk->count == 0) {
510 fprintf(out, "no completion\n");
514 fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
515 completed_tk->count, completed_tk->count_match,
516 completed_tk->smallest_start);
518 TAILQ_FOREACH(elt, &completed_tk->elts, next) {
519 fprintf(out, "add=<%s>, tk=%p, tk_type=%s\n",
520 elt->add, elt->tk, elt->tk->ops->typename);
524 const char *ec_completed_tk_smallest_start(
525 const struct ec_completed_tk *completed_tk)
527 if (completed_tk == NULL || completed_tk->smallest_start == NULL)
530 return completed_tk->smallest_start;
533 unsigned int ec_completed_tk_count(
534 const struct ec_completed_tk *completed_tk,
535 enum ec_completed_tk_filter_flags flags)
537 unsigned int count = 0;
539 if (completed_tk == NULL)
542 if (flags & EC_MATCH)
543 count += completed_tk->count_match;
544 if (flags & EC_NO_MATCH)
545 count += (completed_tk->count - completed_tk->count_match); //XXX
550 struct ec_completed_tk_iter *
551 ec_completed_tk_iter_new(struct ec_completed_tk *completed_tk,
552 enum ec_completed_tk_filter_flags flags)
554 struct ec_completed_tk_iter *iter;
556 iter = ec_calloc(1, sizeof(*iter));
560 iter->completed_tk = completed_tk;
567 const struct ec_completed_tk_elt *ec_completed_tk_iter_next(
568 struct ec_completed_tk_iter *iter)
570 if (iter->completed_tk == NULL)
574 if (iter->cur == NULL) {
575 iter->cur = TAILQ_FIRST(&iter->completed_tk->elts);
577 iter->cur = TAILQ_NEXT(iter->cur, next);
580 if (iter->cur == NULL)
583 if (iter->cur->add == NULL &&
584 (iter->flags & EC_NO_MATCH))
587 if (iter->cur->add != NULL &&
588 (iter->flags & EC_MATCH))
591 } while (iter->cur != NULL);
596 void ec_completed_tk_iter_free(struct ec_completed_tk_iter *iter)
601 const char *ec_tk_desc(const struct ec_tk *tk)
603 if (tk->ops->desc != NULL)
604 return tk->ops->desc(tk);