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