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.
68 lines
1.2 KiB
68 lines
1.2 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <console.h>
|
|
#include <shell.h>
|
|
|
|
void cmd_exec(struct cmd *cmds, FILE *out, const char **argv, size_t argc)
|
|
{
|
|
struct cmd *cmd;
|
|
|
|
if (argc < 1)
|
|
return;
|
|
|
|
for (cmd = cmds; cmd->cmd; ++cmd) {
|
|
if (strcmp(cmd->cmd, argv[0]) == 0) {
|
|
cmd->exec(out, argv + 1, argc - 1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void cmd_parse(struct cmd *cmds, FILE *out, const char *line)
|
|
{
|
|
char **argv;
|
|
size_t argc;
|
|
|
|
if (!(argv = parse_args(line, &argc)))
|
|
return;
|
|
|
|
cmd_exec(cmds, out, (const char **)argv, argc);
|
|
free(argv);
|
|
}
|
|
|
|
int shell_init(struct shell *shell, struct cmd *cmds,
|
|
struct usart_console *con, const char *prompt)
|
|
{
|
|
if (!shell || !con)
|
|
return -1;
|
|
|
|
memset(shell->line, '\0', sizeof shell->line);
|
|
shell->cmds = cmds;
|
|
shell->con = con;
|
|
shell->fp = console_to_fp(con);
|
|
shell->prompt = prompt;
|
|
|
|
fprintf(shell->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;
|
|
|
|
cmd_parse(shell->cmds, shell->fp, shell->line);
|
|
fprintf(shell->fp, "%s ", shell->prompt ? shell->prompt : "#");
|
|
*shell->line = '\0';
|
|
|
|
return 0;
|
|
}
|
|
|