diff --git a/libopenfpga/libopenfpgashell/src/shell.h b/libopenfpga/libopenfpgashell/src/shell.h index 2f67dd610..415fc1b1c 100644 --- a/libopenfpga/libopenfpgashell/src/shell.h +++ b/libopenfpga/libopenfpgashell/src/shell.h @@ -108,7 +108,7 @@ class Shell { void set_command_execute_function(const ShellCommandId& cmd_id, std::function exec_func); void set_command_dependency(const ShellCommandId& cmd_id, - const std::vector cmd_dependency); + const std::vector& cmd_dependency); ShellCommandClassId add_command_class(const char* name); public: /* Public validators */ bool valid_command_id(const ShellCommandId& cmd_id) const; @@ -174,6 +174,9 @@ class Shell { */ vtr::vector command_execute_function_types_; + /* A flag to indicate if the command has been executed */ + vtr::vector command_status_; + /* Dependency graph for different commands, * This helps the shell interface to check if a command need other commands to be run before its execution */ diff --git a/libopenfpga/libopenfpgashell/src/shell.tpp b/libopenfpga/libopenfpgashell/src/shell.tpp index 30534cfeb..0a52fda0b 100644 --- a/libopenfpga/libopenfpgashell/src/shell.tpp +++ b/libopenfpga/libopenfpgashell/src/shell.tpp @@ -131,6 +131,7 @@ ShellCommandId Shell::add_command(const Command& cmd, const char* descr) { command_short_execute_functions_.emplace_back(); command_builtin_execute_functions_.emplace_back(); command_macro_execute_functions_.emplace_back(); + command_status_.push_back(false); /* By default, the command should be marked as never executed */ command_dependencies_.emplace_back(); /* Register the name in the name2id map */ @@ -202,7 +203,7 @@ void Shell::set_command_execute_function(const ShellCommandId& cmd_id, template void Shell::set_command_dependency(const ShellCommandId& cmd_id, - const std::vector dependent_cmds) { + const std::vector& dependent_cmds) { /* Validate the command id as well as each of the command dependency */ VTR_ASSERT(true == valid_command_id(cmd_id)); for (ShellCommandId dependent_cmd : dependent_cmds) { @@ -362,7 +363,14 @@ void Shell::execute_command(const char* cmd_line, return; } - /* TODO: Check the dependency graph to see if all the prequistics have been met */ + /* Check the dependency graph to see if all the prequistics have been met */ + for (const ShellCommandId& dep_cmd : command_dependencies_[cmd_id]) { + if (false == command_status_[dep_cmd]) { + VTR_LOG("Command '%s' is required to be executed before command '%s'!\n", + commands_[dep_cmd].name().c_str(), commands_[cmd_id].name().c_str()); + return; + } + } /* Find the command! Parse the options * Note: @@ -391,10 +399,10 @@ void Shell::execute_command(const char* cmd_line, print_command_options(commands_[cmd_id]); return; } - + /* Parse succeed. Let user to confirm selected options */ print_command_context(commands_[cmd_id], command_contexts_[cmd_id]); - + /* Execute the command depending on the type of function ! */ switch (command_execute_function_types_[cmd_id]) { case CONST_STANDARD: @@ -420,6 +428,9 @@ void Shell::execute_command(const char* cmd_line, /* Exit the shell using the exit() function inside this class! */ exit(); } + + /* Change the status of the command */ + command_status_[cmd_id] = true; } /************************************************************************ diff --git a/libopenfpga/libopenfpgashell/test/test_shell.cpp b/libopenfpga/libopenfpgashell/test/test_shell.cpp index 154986be7..649859d85 100644 --- a/libopenfpga/libopenfpgashell/test/test_shell.cpp +++ b/libopenfpga/libopenfpgashell/test/test_shell.cpp @@ -102,11 +102,13 @@ int main(int argc, char** argv) { /* Create a command of 'print' * This function will print the value of an internal variable of ShellContext + * We set a dependency to this command as it MUST be executed after 'set' */ Command shell_cmd_print("print"); ShellCommandId shell_cmd_print_id = shell.add_command(shell_cmd_print, "Print the value of internal variable 'a'"); shell.set_command_class(shell_cmd_print_id, arith_cmd_class); shell.set_command_execute_function(shell_cmd_print_id, shell_execute_print); + shell.set_command_dependency(shell_cmd_print_id, std::vector(1, shell_cmd_set_id)); /* Create a macro command of 'print_macro' * This function will print the value of an internal variable of ShellContext