add command dependency in shell execution

This commit is contained in:
tangxifan 2020-01-24 16:46:39 -07:00
parent d1725de606
commit b641ae15d3
3 changed files with 21 additions and 5 deletions

View File

@ -108,7 +108,7 @@ class Shell {
void set_command_execute_function(const ShellCommandId& cmd_id,
std::function<int(int, char**)> exec_func);
void set_command_dependency(const ShellCommandId& cmd_id,
const std::vector<ShellCommandId> cmd_dependency);
const std::vector<ShellCommandId>& 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<ShellCommandId, e_exec_func_type> command_execute_function_types_;
/* A flag to indicate if the command has been executed */
vtr::vector<ShellCommandId, bool> 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
*/

View File

@ -131,6 +131,7 @@ ShellCommandId Shell<T>::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<T>::set_command_execute_function(const ShellCommandId& cmd_id,
template<class T>
void Shell<T>::set_command_dependency(const ShellCommandId& cmd_id,
const std::vector<ShellCommandId> dependent_cmds) {
const std::vector<ShellCommandId>& 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<T>::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<T>::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<T>::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;
}
/************************************************************************

View File

@ -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<ShellCommandId>(1, shell_cmd_set_id));
/* Create a macro command of 'print_macro'
* This function will print the value of an internal variable of ShellContext