Source code for the Trusted Boot Module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tbm-mcu/source/shell/cmd.c

78 lines
1.4 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <console.h>
#include <shell.h>
int cmd_exec(struct cmd *cmds, struct console *con, size_t argc,
const char **argv)
{
struct cmd *cmd;
if (argc < 1)
return -1;
for (cmd = cmds; cmd->cmd; ++cmd) {
if (strcmp(cmd->cmd, argv[0]) == 0) {
return cmd->exec(con, argc - 1, argv + 1);
}
}
return -1;
}
int cmd_parse(struct cmd *cmds, struct console *con, const char *line)
{
char **argv;
size_t argc;
int ret;
if (!(argv = parse_args(line, &argc)))
return -1;
ret = cmd_exec(cmds, con, argc, (const char **)argv);
free(argv);
return ret;
}
int shell_init(struct shell *shell, struct cmd *cmds,
struct console *con, const char *prompt, unsigned flags)
{
if (!shell || !con)
return -1;
memset(shell->line, '\0', sizeof shell->line);
shell->cmds = cmds;
shell->con = con;
shell->prompt = prompt;
shell->flags = flags;
fprintf(shell->con->fp, "%s ", prompt ? prompt : "#");
return 0;
}
int shell_parse(struct shell *shell)
{
int ret;
if (!shell)
return -1;
if ((ret = console_getline(shell->con, shell->line, sizeof shell->line)) <
0)
return ret;
ret = cmd_parse(shell->cmds, shell->con, shell->line);
if (shell->flags & SHELL_SHOW_EXIT_CODE)
fprintf(shell->con->fp, "\004%d\n\004", ret);
fprintf(shell->con->fp, "%s ", shell->prompt ? shell->prompt : "#");
*shell->line = '\0';
return 0;
}