Basic interactive CLI with commands, help, and completion.
Basic interactive CLI with commands, help, and completion.Demonstrates building a grammar using sequence, or, and option nodes, setting help text and callbacks on commands, and using the editline integration for interactive input with TAB completion.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ecoli.h>
#define ID_NAME "id_name"
#define ID_JOHN "id_john"
#define ID_COUNT "id_count"
static bool done;
static int hello_cb(
const struct ec_pnode *parse)
{
const char *count;
const char *name;
printf("you say hello to %s", name);
if (count)
printf(" %s times", count);
printf("\n");
return 0;
}
static int bye_cb(
const struct ec_pnode *parse)
{
const char *name;
printf("you say bye to %s\n", name);
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;
ID_NAME,
);
if (names == NULL)
goto fail;
);
if (cmd == NULL)
goto fail;
goto fail;
goto fail;
goto fail;
goto fail;
goto fail;
cmd = NULL;
if (ret < 0)
goto fail;
goto fail;
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(
"simple-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)
Set editline prompt.
struct ec_editline * ec_editline(const char *prog, FILE *f_in, FILE *f_out, FILE *f_err, enum ec_editline_init_flags flags)
Create an editline instance with default behavior.
void ec_editline_free(struct ec_editline *editline)
Free an editline structure allocated with ec_editline().
int ec_editline_interact(struct ec_editline *editline, ec_editline_check_exit_cb_t check_exit_cb, void *opaque)
Interact with the user until exit.
int ec_editline_set_node(struct ec_editline *editline, const struct ec_node *node)
Attach an ecoli node to the editline structure.
int ec_init(void)
Initialize ecoli library.
int ec_interact_set_help(struct ec_node *node, const char *help)
Set help on a grammar node.
int ec_interact_set_callback(struct ec_node *node, ec_interact_command_cb_t cb)
Set callback function on a grammar node.
struct ec_node * ec_node_int(const char *id, int64_t min, int64_t max, unsigned int base)
Create a signed integer node.
struct ec_node * ec_node_option(const char *id, struct ec_node *node)
Create an option node that makes its child optional.
int ec_node_or_add(struct ec_node *node, struct ec_node *child)
Add a child to an "or" node.
#define EC_NODE_OR(args...)
Create a new "or" node from an arbitrary list of child nodes.
#define EC_NODE_SEQ(args...)
Create a sequence node from a list of child nodes.
struct ec_node * ec_node_sh_lex(const char *id, struct ec_node *child)
Create a shell lexer node.
struct ec_node * ec_node_str(const char *id, const char *str)
Create a string node that matches a specific string.
struct ec_node * ec_node_clone(struct ec_node *node)
Clone a grammar node.
struct ec_node * ec_node(const char *typename, const char *id)
Create a new node from its type name.
struct ec_node * ec_node_find(struct ec_node *node, const char *id)
Find a node from its identifier string.
#define EC_NO_ID
Node has no identifier.
void ec_node_free(struct ec_node *node)
Decrement node reference counter and free the node if it is the last reference.
const struct ec_strvec * ec_pnode_get_strvec(const struct ec_pnode *pnode)
Get the string vector associated with a parsing node.
const struct ec_pnode * ec_pnode_find(const struct ec_pnode *root, const char *id)
Find a node from its identifier.
struct ec_pnode * ec_pnode(const struct ec_node *node)
Create an empty parsing tree.
const char * ec_strvec_val(const struct ec_strvec *strvec, size_t idx)
Get a string element from a vector.