From 4aa807a7b3c0aa8791e8c481dbaf6b0088d90a55 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Thu, 27 Jul 2017 12:15:24 +0200 Subject: [PATCH] shell: return exit codes to the user console --- include/shell.h | 7 ++++++- source/main.c | 5 +++-- source/shell/cmd.c | 9 +++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/shell.h b/include/shell.h index a146ca0..7499d28 100644 --- a/include/shell.h +++ b/include/shell.h @@ -1,5 +1,7 @@ #pragma once +#include + 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); diff --git a/source/main.c b/source/main.c index 3e51f2f..a650b83 100644 --- a/source/main.c +++ b/source/main.c @@ -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); diff --git a/source/shell/cmd.c b/source/shell/cmd.c index 652239e..e0d8472 100644 --- a/source/shell/cmd.c +++ b/source/shell/cmd.c @@ -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';