shell: return exit codes to the user console
This commit is contained in:
parent
2942340b21
commit
4aa807a7b3
3 changed files with 16 additions and 5 deletions
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <macros.h>
|
||||
|
||||
struct console;
|
||||
|
||||
struct cmd {
|
||||
|
@ -13,8 +15,11 @@ struct shell {
|
|||
struct cmd *cmds;
|
||||
const char *prompt;
|
||||
struct console *con;
|
||||
unsigned flags;
|
||||
};
|
||||
|
||||
#define SHELL_SHOW_EXIT_CODE BIT(0)
|
||||
|
||||
size_t count_args(const char *line);
|
||||
char **parse_args(const char *line, size_t *argc);
|
||||
|
||||
|
@ -23,7 +28,7 @@ int cmd_exec(struct cmd *cmds, struct console *con, size_t argc,
|
|||
int cmd_parse(struct cmd *cmds, struct console *con, const char *line);
|
||||
|
||||
int shell_init(struct shell *shell, struct cmd *cmds,
|
||||
struct console *con, const char *prompt);
|
||||
struct console *con, const char *prompt, unsigned flags);
|
||||
int shell_parse(struct shell *shell);
|
||||
|
||||
int do_flash_cmd(struct console *con, size_t argc, const char **argv);
|
||||
|
|
|
@ -64,8 +64,9 @@ int main(void)
|
|||
fprintf(user_con->fp, "TBM-dev (built on " __DATE__ ")\n");
|
||||
fprintf(admin_con->fp, "TBM-dev (built on " __DATE__ ")\n");
|
||||
|
||||
shell_init(&user_shell, user_cmds, user_con, "tbm $");
|
||||
shell_init(&admin_shell, admin_cmds, admin_con, "tbm #");
|
||||
shell_init(&user_shell, user_cmds, user_con, "tbm $",
|
||||
SHELL_SHOW_EXIT_CODE);
|
||||
shell_init(&admin_shell, admin_cmds, admin_con, "tbm #", 0);
|
||||
|
||||
while (1) {
|
||||
shell_parse(&user_shell);
|
||||
|
|
|
@ -38,7 +38,7 @@ int cmd_parse(struct cmd *cmds, struct console *con, const char *line)
|
|||
}
|
||||
|
||||
int shell_init(struct shell *shell, struct cmd *cmds,
|
||||
struct console *con, const char *prompt)
|
||||
struct console *con, const char *prompt, unsigned flags)
|
||||
{
|
||||
if (!shell || !con)
|
||||
return -1;
|
||||
|
@ -47,6 +47,7 @@ int shell_init(struct shell *shell, struct cmd *cmds,
|
|||
shell->cmds = cmds;
|
||||
shell->con = con;
|
||||
shell->prompt = prompt;
|
||||
shell->flags = flags;
|
||||
|
||||
fprintf(shell->con->fp, "%s ", prompt ? prompt : "#");
|
||||
|
||||
|
@ -64,7 +65,11 @@ int shell_parse(struct shell *shell)
|
|||
0)
|
||||
return ret;
|
||||
|
||||
cmd_parse(shell->cmds, shell->con, shell->line);
|
||||
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';
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue