Define custom node types with specialized parsing and completion.
Demonstrates creating a custom grammar node type (bool_tuple) with its own parsing and completion logic. The bool_tuple node parses and completes tuples of booleans like "(true,false,true)" and converts them to an integer where each boolean becomes a bit (e.g. "(true,false,true)" = 101 binary = 5).
extension> convert (true,false,true)
Integer value for (true,false,true) is 5
extension> convert (false,true)
Integer value for (false,true) is 1
extension> exit
Exit !
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ecoli.h>
#define ID_BOOL_TUPLE "id_bool_tuple"
static bool done;
static int convert_cb(
const struct ec_pnode *parse)
{
const char *bool_tuple;
const char *next_false;
const char *next_true;
unsigned int val = 0;
const char *t;
t = bool_tuple;
while (1) {
next_true = strstr(t, "true");
next_false = strstr(t, "false");
if (next_true && (next_false == NULL || next_false > next_true)) {
val = (val << 1) | 1;
t = next_true + 1;
} else if (next_false && (next_true == NULL || next_true > next_false)) {
val = val << 1;
t = next_false + 1;
} else {
break;
}
}
printf("Integer value for %s is %u\n", bool_tuple, val);
return 0;
}
static int exit_cb(
const struct ec_pnode *parse)
{
(void)parse;
printf("Exit !\n");
done = true;
return 0;
}
static int check_exit(void *opaque)
{
(void)opaque;
return done;
}
static struct ec_node *create_commands(
void)
{
struct ec_node *cmdlist = NULL, *cmd = NULL;
int ret;
if (cmdlist == NULL)
goto fail;
);
if (cmd == NULL)
goto fail;
goto fail;
< 0)
goto fail;
"A tuple of booleans. Example: \"(true,false,true)\""
)
< 0)
goto fail;
cmd = NULL;
if (ret < 0)
goto fail;
goto fail;
goto fail;
cmd = NULL;
if (ret < 0)
goto fail;
if (cmdlist == NULL)
goto fail;
return cmdlist;
fail:
fprintf(stderr, "cannot initialize nodes\n");
return NULL;
}
int main(void)
{
fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
return 1;
}
node = create_commands();
if (node == NULL) {
fprintf(stderr, "failed to create commands: %s\n", strerror(errno));
goto fail;
}
editline =
ec_editline(
"extension-editline", stdin, stdout, stderr, 0);
if (editline == NULL) {
fprintf(stderr, "Failed to initialize editline\n");
goto fail;
}
fprintf(stderr, "Failed to set prompt\n");
goto fail;
}
goto fail;
return 0;
fail:
return 1;
}
int ec_editline_set_prompt(struct ec_editline *editline, const char *prompt)
struct ec_editline * ec_editline(const char *prog, FILE *f_in, FILE *f_out, FILE *f_err, enum ec_editline_init_flags flags)
void ec_editline_free(struct ec_editline *editline)
int ec_editline_interact(struct ec_editline *editline, ec_editline_check_exit_cb_t check_exit_cb, void *opaque)
int ec_editline_set_node(struct ec_editline *editline, const struct ec_node *node)
int ec_interact_set_help(struct ec_node *node, const char *help)
int ec_interact_set_callback(struct ec_node *node, ec_interact_command_cb_t cb)
int ec_node_or_add(struct ec_node *node, struct ec_node *child)
#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_node * ec_node_find(struct ec_node *node, const char *id)
void ec_node_free(struct ec_node *node)
const struct ec_strvec * ec_pnode_get_strvec(const struct ec_pnode *pnode)
const struct ec_pnode * ec_pnode_find(const struct ec_pnode *root, const char *id)
struct ec_pnode * ec_pnode(const struct ec_node *node)
const char * ec_strvec_val(const struct ec_strvec *strvec, size_t idx)