From fd0e73a9bb634d1b8ea502a12dd9fcd54f19bae0 Mon Sep 17 00:00:00 2001 From: tangxifan Date: Sun, 24 Jan 2021 14:48:27 -0700 Subject: [PATCH] [Tool] Enhance return code for openfpga shell --- libopenfpga/libopenfpgashell/src/shell.h | 4 ++ libopenfpga/libopenfpgashell/src/shell.tpp | 50 +++++++++++++++------- openfpga/src/main.cpp | 9 ++-- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/libopenfpga/libopenfpgashell/src/shell.h b/libopenfpga/libopenfpgashell/src/shell.h index 26695b819..ffcb6f1e3 100644 --- a/libopenfpga/libopenfpgashell/src/shell.h +++ b/libopenfpga/libopenfpgashell/src/shell.h @@ -148,6 +148,10 @@ class Shell { const bool& batch_mode = false); /* Print all the commands by their classes. This is actually the help desk */ void print_commands() const; + /* Find the exit code (assume quit shell now) */ + int exit_code() const; + /* Show statistics of errors during command execution */ + int execution_errors() const; /* Quit the shell */ void exit() const; private: /* Private executors */ diff --git a/libopenfpga/libopenfpgashell/src/shell.tpp b/libopenfpga/libopenfpgashell/src/shell.tpp index 6e607f016..eaa1e84c3 100644 --- a/libopenfpga/libopenfpgashell/src/shell.tpp +++ b/libopenfpga/libopenfpgashell/src/shell.tpp @@ -401,7 +401,7 @@ void Shell::print_commands() const { } template -void Shell::exit() const { +int Shell::exit_code() const { /* Check all the command status, if we see fatal errors or minor errors, we drop an error code */ int exit_code = 0; for (const int& status : command_status_) { @@ -412,23 +412,41 @@ void Shell::exit() const { } } + return exit_code; +} + +template +int Shell::execution_errors() const { /* Show error message if we detect any errors */ int num_err = 0; - if (0 != exit_code) { - VTR_LOG("\n"); - for (const ShellCommandId& cmd : commands()) { - if (command_status_[cmd] == CMD_EXEC_FATAL_ERROR) { - VTR_LOG_ERROR("Command '%s' execution has fatal errors\n", - commands_[cmd].name().c_str()); - num_err++; - } - - if (command_status_[cmd] == CMD_EXEC_MINOR_ERROR) { - VTR_LOG_ERROR("Command '%s' execution has minor errors\n", - commands_[cmd].name().c_str()); - num_err++; - } + + for (const ShellCommandId& cmd : commands()) { + if (command_status_[cmd] == CMD_EXEC_FATAL_ERROR) { + VTR_LOG_ERROR("Command '%s' execution has fatal errors\n", + commands_[cmd].name().c_str()); + num_err++; } + + if (command_status_[cmd] == CMD_EXEC_MINOR_ERROR) { + VTR_LOG_ERROR("Command '%s' execution has minor errors\n", + commands_[cmd].name().c_str()); + num_err++; + } + } + + return num_err; +} + +template +void Shell::exit() const { + /* Check all the command status, if we see fatal errors or minor errors, we drop an error code */ + int shell_exit_code = exit_code(); + + /* Show error message if we detect any errors */ + int num_err = 0; + if (0 != shell_exit_code) { + VTR_LOG("\n"); + num_err = execution_errors(); } VTR_LOG("\nFinish execution with %d errors\n", @@ -440,7 +458,7 @@ void Shell::exit() const { VTR_LOG("\nThank you for using %s!\n", name().c_str()); - std::exit(exit_code); + std::exit(shell_exit_code); } /************************************************************************ diff --git a/openfpga/src/main.cpp b/openfpga/src/main.cpp index 083d5965d..3c2372c2a 100644 --- a/openfpga/src/main.cpp +++ b/openfpga/src/main.cpp @@ -99,18 +99,21 @@ int main(int argc, char** argv) { if (true == start_cmd_context.option_enable(start_cmd, opt_interactive)) { shell.run_interactive_mode(openfpga_context); - return 0; + return shell.exit_code(); } if (true == start_cmd_context.option_enable(start_cmd, opt_script_mode)) { shell.run_script_mode(start_cmd_context.option_value(start_cmd, opt_script_mode).c_str(), openfpga_context, start_cmd_context.option_enable(start_cmd, opt_batch_exec)); - return 0; + return shell.exit_code(); } /* Reach here there is something wrong, show the help desk */ openfpga::print_command_options(start_cmd); } - return 0; + /* Reach here, it means shell execution has critical errors. + * Return a code with fatal errors + */ + return 1; }