1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
6 * @defgroup complete Complete
9 * @brief Complete string input using a grammar tree
11 * This file provide helpers to list and manipulate the possible
12 * completions for a given input.
19 #ifndef ECOLI_COMPLETE_
20 #define ECOLI_COMPLETE_
22 #include <sys/queue.h>
23 #include <sys/types.h>
28 enum ec_comp_type { /* XXX should be a define */
29 EC_COMP_UNKNOWN = 0x1,
31 EC_COMP_PARTIAL = 0x4,
37 TAILQ_HEAD(ec_comp_item_list, ec_comp_item);
39 struct ec_comp_group {
40 TAILQ_ENTRY(ec_comp_group) next;
41 const struct ec_node *node;
42 struct ec_comp_item_list items;
43 struct ec_parse *state;
44 struct ec_dict *attrs;
47 TAILQ_HEAD(ec_comp_group_list, ec_comp_group);
52 unsigned count_partial;
53 unsigned count_unknown;
54 struct ec_parse *cur_state;
55 struct ec_comp_group *cur_group;
56 struct ec_comp_group_list groups;
57 struct ec_dict *attrs;
61 * return a comp object filled with items
62 * return NULL on error (nomem?)
64 struct ec_comp *ec_node_complete(const struct ec_node *node,
66 struct ec_comp *ec_node_complete_strvec(const struct ec_node *node,
67 const struct ec_strvec *strvec);
69 /* internal: used by nodes */
70 int ec_node_complete_child(const struct ec_node *node,
72 const struct ec_strvec *strvec);
75 * Create a completion object (list of completion items).
79 struct ec_comp *ec_comp(struct ec_parse *state);
82 * Free a completion object and all its items.
86 void ec_comp_free(struct ec_comp *comp);
93 void ec_comp_dump(FILE *out,
94 const struct ec_comp *comp);
97 * Merge items contained in 'from' into 'to'
99 * The 'from' comp struct is freed.
101 int ec_comp_merge(struct ec_comp *to,
102 struct ec_comp *from);
104 struct ec_parse *ec_comp_get_state(struct ec_comp *comp);
106 /* shortcut for ec_comp_item() + ec_comp_item_add() */
107 int ec_comp_add_item(struct ec_comp *comp,
108 const struct ec_node *node,
109 struct ec_comp_item **p_item,
110 enum ec_comp_type type,
111 const char *start, const char *full);
116 int ec_comp_item_set_str(struct ec_comp_item *item,
120 * Get the string value of a completion item.
125 ec_comp_item_get_str(const struct ec_comp_item *item);
128 * Get the display string value of a completion item.
133 ec_comp_item_get_display(const struct ec_comp_item *item);
136 * Get the completion string value of a completion item.
141 ec_comp_item_get_completion(const struct ec_comp_item *item);
144 * Get the group of a completion item.
148 const struct ec_comp_group *
149 ec_comp_item_get_grp(const struct ec_comp_item *item);
152 * Get the type of a completion item.
157 ec_comp_item_get_type(const struct ec_comp_item *item);
160 * Get the node associated to a completion item.
164 const struct ec_node *
165 ec_comp_item_get_node(const struct ec_comp_item *item);
168 * Set the display value of an item.
172 int ec_comp_item_set_display(struct ec_comp_item *item,
173 const char *display);
176 * Set the completion value of an item.
180 int ec_comp_item_set_completion(struct ec_comp_item *item,
181 const char *completion);
189 ec_node_complete_unknown(const struct ec_node *gen_node,
190 struct ec_comp *comp,
191 const struct ec_strvec *strvec);
198 unsigned int ec_comp_count(
199 const struct ec_comp *comp,
200 enum ec_comp_type flags);
207 struct ec_comp_iter {
208 enum ec_comp_type type;
209 const struct ec_comp *comp;
210 struct ec_comp_group *cur_node;
211 struct ec_comp_item *cur_match;
219 struct ec_comp_iter *
220 ec_comp_iter(const struct ec_comp *comp,
221 enum ec_comp_type type);
228 struct ec_comp_item *ec_comp_iter_next(
229 struct ec_comp_iter *iter);
236 void ec_comp_iter_free(struct ec_comp_iter *iter);