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)
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)
struct ec_node * ec_node_int(const char *id, int64_t min, int64_t max, unsigned int base)
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_clone(struct ec_node *node)
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)