vxBoot/src/cli/entry.c

72 lines
1.5 KiB
C

#include "vxBoot/cli.h"
#include "vxBoot/builtin.h"
#include "vxBoot/terminal.h"
#include <string.h>
//TODO: better API for the command-line parser
/* internal builtin list */
struct {
const char *name;
int (*f)(int argc, char **argv);
} cmd_list[] = {
{.name = "ls", &ls_main},
{.name = "ld", &ld_main},
{.name = "hw", &hw_main},
{.name = "help", &help_main},
{.name = "log", &log_main},
{.name = NULL, NULL}
};
/* try to find the appropriate command */
static int (*check_cmd(char *cmd))(int, char**)
{
for (int i = 0; cmd_list[i].name != NULL; ++i) {
if (strcmp(cmd, cmd_list[i].name) != 0)
continue;
if (cmd_list[i].f == NULL)
terminal_write("command exist but not implemented\n");
return (cmd_list[i].f);
}
return (NULL);
}
void cli_main(void)
{
int (*builtin)(int, char**);
const char *usrline;
char buff[128];
char **argv;
int argc;
int ret;
/* CLI loop */
ret = 0;
while (1) {
/* get user command */
usrline = (ret != 0) ? "/[%d]>" : "/>";
terminal_write(usrline, ret);
ret = 0;
if (terminal_read(buff, 128) <= 1)
continue;
/* parse and try to find the command */
if (cli_parser_strtotab(&argc, &argv, buff) != 0) {
terminal_write("error when processing \"%s\"", buff);
ret = 255;
continue;
}
builtin = check_cmd(argv[0]);
if (builtin == NULL) {
terminal_write("command \"%s\" not found\n", argv[0]);
ret = 127;
continue;
}
/* execute the command and free'd allocated memories */
ret = builtin(argc, argv);
cli_parser_strtotab_quit(&argc, &argv);
}
}