From aec592b27e9d6774af3b1a27a05f48ffccb36d20 Mon Sep 17 00:00:00 2001 From: tangxifan Date: Sun, 8 Jan 2023 22:34:35 -0800 Subject: [PATCH] [lib] now shell forbids to call any hidden commands from interactive mode or script mode --- libs/libopenfpgashell/src/shell.h | 3 ++- libs/libopenfpgashell/src/shell.tpp | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libs/libopenfpgashell/src/shell.h b/libs/libopenfpgashell/src/shell.h index d209f38b4..3086a47e8 100644 --- a/libs/libopenfpgashell/src/shell.h +++ b/libs/libopenfpgashell/src/shell.h @@ -206,8 +206,9 @@ class Shell { void exit(const int& init_err = 0) const; /* Execute a command, the command line is the user's input to launch a command * The common_context is the data structure to exchange data between commands + * Optionally, hidden commands may be forbidden to call. User can allow them to be called under different situations */ - int execute_command(const char* cmd_line, T& common_context); + int execute_command(const char* cmd_line, T& common_context, const bool& allow_hidden_command = true); private: /* Internal data */ /* Name of the shell, this will appear in the interactive mode */ diff --git a/libs/libopenfpgashell/src/shell.tpp b/libs/libopenfpgashell/src/shell.tpp index eb4ec02c3..6abd44775 100644 --- a/libs/libopenfpgashell/src/shell.tpp +++ b/libs/libopenfpgashell/src/shell.tpp @@ -286,7 +286,8 @@ void Shell::run_interactive_mode(T& context, const bool& quiet_mode) { * Add to history */ if (strlen(cmd_line) > 0) { - execute_command((const char*)cmd_line, context); + /* Do not allow any hidden command to be directly called by users */ + execute_command((const char*)cmd_line, context, false); add_history(cmd_line); } @@ -378,7 +379,8 @@ void Shell::run_script_mode(const char* script_file_name, /* Process the command only when the full command line in ended */ if (!cmd_line.empty()) { VTR_LOG("\nCommand line to execute: %s\n", cmd_line.c_str()); - int status = execute_command(cmd_line.c_str(), context); + /* Do not allow any hidden command to be directly called by users */ + int status = execute_command(cmd_line.c_str(), context, false); /* Empty the line ready to start a new line */ cmd_line.clear(); @@ -507,7 +509,7 @@ void Shell::exit(const int& init_err) const { ***********************************************************************/ template int Shell::execute_command(const char* cmd_line, - T& common_context) { + T& common_context, const bool& allow_hidden_command) { openfpga::StringToken tokenizer(cmd_line); tokenizer.add_delim(' '); /* Do not split the string in each quote "", as they should be a piece */ @@ -537,6 +539,14 @@ int Shell::execute_command(const char* cmd_line, tokens[0].c_str()); return CMD_EXEC_FATAL_ERROR; } + /* Do not allow hidden commands if specified */ + if (!allow_hidden_command) { + if (command_hidden_[cmd_id]) { + VTR_LOG("Try to call a command '%s' which is not defined!\n", + tokens[0].c_str()); + return CMD_EXEC_FATAL_ERROR; + } + } /* Check the dependency graph to see if all the prequistics have been met */ for (const ShellCommandId& dep_cmd : command_dependencies_[cmd_id]) {