39#include <readline/history.h>
40#include <readline/readline.h>
46static char *my_completion_entry(
const char *s,
int state)
49 static struct ec_comp_item *item;
51 const char *item_str, *item_display;
57 rl_completion_suppress_quote = 1;
58 rl_completer_quote_characters =
"\"'";
64 line = strdup(rl_line_buffer);
67 line[rl_point] =
'\0';
90 rl_completion_suppress_append = 0;
92 rl_completion_suppress_append = 1;
95 return strdup(item_str);
96 }
else if (rl_completion_type ==
'?') {
99 return strdup(item_display);
102 return strdup(item_str);
105static char **my_attempted_completion(
const char *text,
int start,
int end)
111 rl_attempted_completion_over = 1;
113 return rl_completion_matches(text, my_completion_entry);
117static char *get_node_help(
const struct ec_comp_item *item)
119 const struct ec_comp_group *grp;
123 const char *node_help = NULL;
124 char *node_desc = NULL;
130 if (node_help == NULL)
132 if (node_desc == NULL)
136 if (node_help == NULL)
138 if (node_desc == NULL)
141 if (asprintf(&help,
"%-20s %s", node_desc, node_help) < 0)
149static int show_help(
int ignore,
int invoking_key)
151 const struct ec_comp_group *grp, *prev_grp = NULL;
152 struct ec_comp_item *item = NULL;
165 line = strdup(rl_line_buffer);
177 line[rl_point] =
'\0';
185 helps = calloc(match + 1,
sizeof(
char *));
189 helps[1] =
"<return>";
202 tmp = realloc(helps, (count + match + 2) *
sizeof(
char *));
206 helps[count + match + 1] = get_node_help(item);
211 rl_get_screen_size(NULL, &cols);
212 rl_display_match_list(helps, count + match, cols);
213 rl_forced_update_display();
224 free(helps[count + match + 1]);
231static int create_commands(
void)
233 struct ec_node *cmdlist = NULL, *cmd = NULL;
265 "good morning name [count]",
276 "how many times to greet (0-10)",
321 if (commands == NULL)
327 fprintf(stderr,
"cannot initialize nodes\n");
338 fprintf(stderr,
"cannot init ecoli: %s\n", strerror(errno));
342 if (create_commands() < 0)
345 rl_bind_key(
'?', show_help);
346 rl_attempted_completion_function = my_attempted_completion;
349 line = readline(
"> ");
const struct ec_comp_group * ec_comp_item_get_grp(const struct ec_comp_item *item)
struct ec_comp_item * ec_comp_iter_next(struct ec_comp_item *item, enum ec_comp_type type)
const struct ec_pnode * ec_comp_group_get_pstate(const struct ec_comp_group *grp)
struct ec_comp * ec_comp(void)
struct ec_comp * ec_complete(const struct ec_node *node, const char *str)
struct ec_comp_item * ec_comp_iter_first(const struct ec_comp *comp, enum ec_comp_type type)
const char * ec_comp_item_get_display(const struct ec_comp_item *item)
size_t ec_comp_count(const struct ec_comp *comp, enum ec_comp_type type)
void ec_comp_free(struct ec_comp *comp)
const char * ec_comp_item_get_str(const struct ec_comp_item *item)
#define EC_COMP_FOREACH(item, comp, type)
enum ec_comp_type ec_comp_item_get_type(const struct ec_comp_item *item)
void * ec_dict_get(const struct ec_dict *dict, const char *key)
int ec_dict_set(struct ec_dict *dict, const char *key, void *val, ec_dict_elt_free_t free_cb)
#define EC_NODE_CMD(args...)
struct ec_node * ec_node_int(const char *id, int64_t min, int64_t max, unsigned int base)
struct ec_node * ec_node_many(const char *id, struct ec_node *child, unsigned int min, unsigned int max)
struct ec_node * ec_node_once(const char *id, struct ec_node *child)
struct ec_node * ec_node_option(const char *id, struct ec_node *node)
int ec_node_or_add(struct ec_node *node, struct ec_node *child)
#define EC_NODE_OR(args...)
#define EC_NODE_SEQ(args...)
struct ec_node * ec_node_sh_lex(const char *id, struct ec_node *child)
struct ec_node * ec_node_str(const char *id, const char *str)
struct ec_node * ec_node(const char *typename, const char *id)
struct ec_dict * ec_node_attrs(const struct ec_node *node)
struct ec_node * ec_node_find(struct ec_node *node, const char *id)
char * ec_node_desc(const struct ec_node *node)
void ec_node_free(struct ec_node *node)
bool ec_pnode_matches(const struct ec_pnode *pnode)
struct ec_pnode * ec_pnode_get_parent(const struct ec_pnode *pnode)
struct ec_pnode * ec_parse(const struct ec_node *node, const char *str)
void ec_pnode_dump(FILE *out, const struct ec_pnode *pnode)
void ec_pnode_free(struct ec_pnode *pnode)
const struct ec_node * ec_pnode_get_node(const struct ec_pnode *pnode)
struct ec_pnode * ec_pnode(const struct ec_node *node)