[Tool] Enhance return code for openfpga shell

This commit is contained in:
tangxifan 2021-01-24 14:48:27 -07:00
parent 8cac3291cb
commit fd0e73a9bb
3 changed files with 44 additions and 19 deletions

View File

@ -148,6 +148,10 @@ class Shell {
const bool& batch_mode = false); const bool& batch_mode = false);
/* Print all the commands by their classes. This is actually the help desk */ /* Print all the commands by their classes. This is actually the help desk */
void print_commands() const; 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 */ /* Quit the shell */
void exit() const; void exit() const;
private: /* Private executors */ private: /* Private executors */

View File

@ -401,7 +401,7 @@ void Shell<T>::print_commands() const {
} }
template <class T> template <class T>
void Shell<T>::exit() const { int Shell<T>::exit_code() const {
/* Check all the command status, if we see fatal errors or minor errors, we drop an error code */ /* Check all the command status, if we see fatal errors or minor errors, we drop an error code */
int exit_code = 0; int exit_code = 0;
for (const int& status : command_status_) { for (const int& status : command_status_) {
@ -412,23 +412,41 @@ void Shell<T>::exit() const {
} }
} }
return exit_code;
}
template <class T>
int Shell<T>::execution_errors() const {
/* Show error message if we detect any errors */ /* Show error message if we detect any errors */
int num_err = 0; int num_err = 0;
if (0 != exit_code) {
VTR_LOG("\n"); for (const ShellCommandId& cmd : commands()) {
for (const ShellCommandId& cmd : commands()) { if (command_status_[cmd] == CMD_EXEC_FATAL_ERROR) {
if (command_status_[cmd] == CMD_EXEC_FATAL_ERROR) { VTR_LOG_ERROR("Command '%s' execution has fatal errors\n",
VTR_LOG_ERROR("Command '%s' execution has fatal errors\n", commands_[cmd].name().c_str());
commands_[cmd].name().c_str()); num_err++;
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++;
}
} }
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 <class T>
void Shell<T>::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", VTR_LOG("\nFinish execution with %d errors\n",
@ -440,7 +458,7 @@ void Shell<T>::exit() const {
VTR_LOG("\nThank you for using %s!\n", VTR_LOG("\nThank you for using %s!\n",
name().c_str()); name().c_str());
std::exit(exit_code); std::exit(shell_exit_code);
} }
/************************************************************************ /************************************************************************

View File

@ -99,18 +99,21 @@ int main(int argc, char** argv) {
if (true == start_cmd_context.option_enable(start_cmd, opt_interactive)) { if (true == start_cmd_context.option_enable(start_cmd, opt_interactive)) {
shell.run_interactive_mode(openfpga_context); shell.run_interactive_mode(openfpga_context);
return 0; return shell.exit_code();
} }
if (true == start_cmd_context.option_enable(start_cmd, opt_script_mode)) { 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(), shell.run_script_mode(start_cmd_context.option_value(start_cmd, opt_script_mode).c_str(),
openfpga_context, openfpga_context,
start_cmd_context.option_enable(start_cmd, opt_batch_exec)); 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 */ /* Reach here there is something wrong, show the help desk */
openfpga::print_command_options(start_cmd); 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;
} }