[lib] now openfpga shell supports 'source <script>' command

This commit is contained in:
tangxifan 2022-12-31 22:22:30 -08:00
parent 7c95e6d7ef
commit 6e3ea51a06
2 changed files with 23 additions and 2 deletions

View File

@ -181,7 +181,7 @@ class Shell {
/* 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
*/
int execute_command(const char* cmd_line, T& common_context);
int execute_command(const char* cmd_line, T& common_context, const bool& batch_mode = false);
private: /* Internal data */
/* Name of the shell, this will appear in the interactive mode */
@ -259,6 +259,9 @@ class Shell {
/* Timer */
std::clock_t time_start_;
/* Constants */
std::string COMMAND_NAME_SOURCE_;
};
} /* End namespace openfpga */

View File

@ -29,6 +29,7 @@ template<class T>
Shell<T>::Shell() {
name_ = std::string("shell_no_name");
time_start_ = 0;
COMMAND_NAME_SOURCE_ = "source";
}
/************************************************************************
@ -117,6 +118,12 @@ void Shell<T>::add_title(const char* title) {
/* Add a command with it description */
template<class T>
ShellCommandId Shell<T>::add_command(const Command& cmd, const char* descr) {
/* Not allow to add a command whose name conflicts with built-in ones */
if (cmd.name() == COMMAND_NAME_SOURCE_) {
VTR_LOG_WARN("Not allow to overwrite a built-in command: '%s'!\n", COMMAND_NAME_SOURCE_.c_str());
return ShellCommandId::INVALID();
}
/* Ensure that the name is unique in the command list */
std::map<std::string, ShellCommandId>::const_iterator name_it = command_name2ids_.find(std::string(cmd.name()));
if (name_it != command_name2ids_.end()) {
@ -476,11 +483,22 @@ void Shell<T>::exit(const int& init_err) const {
***********************************************************************/
template <class T>
int Shell<T>::execute_command(const char* cmd_line,
T& common_context) {
T& common_context,
const bool& batch_mode) {
/* Tokenize the line */
openfpga::StringToken tokenizer(cmd_line);
std::vector<std::string> tokens = tokenizer.split(" ");
/* "source" command has the highest priority */
if (tokens[0] == COMMAND_NAME_SOURCE_) {
/* Expect only two tokens, second is the script name */
if (2 != tokens.size()) {
VTR_LOG("source command only accepts 1 argument!\n");
return CMD_EXEC_FATAL_ERROR;
}
run_script_mode(tokens[1].c_str(), common_context, batch_mode);
}
/* Find if the command name is valid */
ShellCommandId cmd_id = command(tokens[0]);
if (ShellCommandId::INVALID() == cmd_id) {