change to namespace openfpga and bug fixed to avoid easy crash due to wrong options

This commit is contained in:
tangxifan 2020-01-22 16:49:32 -07:00
parent f8c5c1a117
commit bb8ef9614f
14 changed files with 78 additions and 40 deletions

View File

@ -4,8 +4,8 @@
#include "vtr_assert.h"
#include "command.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* Public constructors
@ -169,4 +169,4 @@ bool Command::valid_option_id(const CommandOptionId& option_id) const {
return ( size_t(option_id) < option_ids_.size() ) && ( option_id == option_ids_[option_id] );
}
} /* End namespace minshell */
} /* End namespace openfpga */

View File

@ -23,8 +23,8 @@
#include "vtr_range.h"
#include "command_fwd.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* Supported date types of value which is followed by a command-line option
@ -139,6 +139,6 @@ class Command {
std::map<std::string, CommandOptionId> option_short_name2ids_;
};
} /* End namespace minshell */
} /* End namespace openfpga */
#endif

View File

@ -4,8 +4,8 @@
#include "vtr_assert.h"
#include "command_context.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* Public constructor
@ -66,4 +66,4 @@ void CommandContext::set_option_value(const Command& command,
option_values_[option_id] = value;
}
} /* End namespace minshell */
} /* End namespace openfpga */

View File

@ -6,8 +6,8 @@
#include "command_fwd.h"
#include "command.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* Data structure to stores parsing results for a command
@ -54,6 +54,6 @@ class CommandContext {
vtr::vector<CommandOptionId, std::string> option_values_;
};
} /* End namespace minshell */
} /* End namespace openfpga */
#endif

View File

@ -6,18 +6,18 @@
#include "vtr_log.h"
#include "command_echo.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* Print all the options that are defined in an object Command
* This function is mainly used to create help desk for a command
********************************************************************/
void print_command_options(const Command& cmd) {
VTR_LOG("Command '%s' usage:\n",
cmd.name().c_str());
VTR_LOG("Command '%s' usage:\n%lu options available\n",
cmd.name().c_str(), cmd.options().size());
for (const CommandOptionId& opt : cmd.options()) {
VTR_LOG("--%s, -%s : %s\n",
VTR_LOG("\t--%s, -%s : %s\n",
cmd.option_name(opt).c_str(),
cmd.option_short_name(opt).c_str(),
cmd.option_description(opt).c_str());

View File

@ -11,14 +11,14 @@
* Function declaration
*******************************************************************/
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
void print_command_options(const Command& cmd);
void print_command_context(const Command& cmd,
const CommandContext& cmd_context);
} /* End namespace minshell */
} /* End namespace openfpga */
#endif

View File

@ -3,8 +3,8 @@
#include "vtr_strong_id.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* A strong id for the options used by a command
@ -13,6 +13,6 @@ struct command_option_id_tag;
typedef vtr::StrongId<command_option_id_tag> CommandOptionId;
} /* End namespace minshell */
} /* End namespace openfpga */
#endif

View File

@ -10,8 +10,8 @@
#include "vtr_log.h"
#include "command_parser.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/********************************************************************
* Try to find an option in the command and update the CommandContext if needed
@ -25,6 +25,7 @@ CommandOptionId parse_option(const std::string& argv,
if (CommandOptionId::INVALID() == option_id) {
VTR_LOG("Detect unknown option '--%s'!\n",
argv.c_str());
return CommandOptionId::INVALID();
}
/* Found, update the CommandContext */
cmd_context.set_option(cmd, option_id, true);
@ -45,6 +46,7 @@ CommandOptionId parse_short_option(const std::string& argv,
if (CommandOptionId::INVALID() == option_id) {
VTR_LOG("Detect unknown short option '-%s'!\n",
argv.c_str());
return CommandOptionId::INVALID();
}
/* Found, update the CommandContext */
cmd_context.set_option(cmd, option_id, true);
@ -160,4 +162,4 @@ bool parse_command(const std::vector<std::string>& argv,
return true;
}
} /* End namespace minshell */
} /* End namespace openfpga */

View File

@ -12,13 +12,13 @@
* Function declaration
*******************************************************************/
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
bool parse_command(const std::vector<std::string>& argv,
const Command& cmd,
CommandContext& cmd_context);
} /* End namespace minshell */
} /* End namespace openfpga */
#endif

View File

@ -12,8 +12,8 @@
#include "command_context.h"
#include "shell_fwd.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* Data structure to define a working environment of a mini shell
@ -171,7 +171,7 @@ class Shell {
vtr::vector<ShellCommandClassId, std::vector<ShellCommandId>> commands_by_classes_;
};
} /* End namespace minshell */
} /* End namespace openfpga */
/* Include the template implementation functions in the header file */
#include "shell.tpp"

View File

@ -19,8 +19,8 @@
#include "command_parser.h"
#include "command_echo.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* Public constructors
@ -380,4 +380,4 @@ bool Shell<T>::valid_command_class_id(const ShellCommandClassId& cmd_class_id) c
return ( size_t(cmd_class_id) < command_class_ids_.size() ) && ( cmd_class_id == command_class_ids_[cmd_class_id] );
}
} /* End namespace minshell */
} /* End namespace openfpga */

View File

@ -3,8 +3,8 @@
#include "vtr_strong_id.h"
/* Begin namespace minishell */
namespace minishell {
/* Begin namespace openfpga */
namespace openfpga {
/*********************************************************************
* A strong id for the options used by a command
@ -15,6 +15,6 @@ struct shell_command_class_id_tag;
typedef vtr::StrongId<shell_command_id_tag> ShellCommandId;
typedef vtr::StrongId<shell_command_class_id_tag> ShellCommandClassId;
} /* End namespace minshell */
} /* End namespace openfpga */
#endif

View File

@ -4,7 +4,7 @@
#include "command_parser.h"
#include "command_echo.h"
using namespace minishell;
using namespace openfpga;
int main(int argc, char** argv) {
/* Create a new command */

View File

@ -7,11 +7,26 @@
#include "command_echo.h"
#include "shell.h"
using namespace minishell;
using namespace openfpga;
class ShellContext {
public:
int a;
};
static
void shell_execute_set(ShellContext& context,
const Command& cmd, const CommandContext& cmd_context) {
CommandOptionId opt_id = cmd.option("value");
/* Get the value of a in the command context */
context.a = std::atoi(cmd_context.option_value(cmd, opt_id).c_str());
}
static
void shell_execute_print(ShellContext& context) {
VTR_LOG("a=%d\n", context.a);
}
int main(int argc, char** argv) {
/* Create the command to launch shell in different modes */
Command start_cmd("test_shell");
@ -61,6 +76,27 @@ int main(int argc, char** argv) {
shell.add_title(shell_title.c_str());
/* Add a new class of commands */
ShellCommandClassId arith_cmd_class = shell.add_command_class("Arithmetic");
/* Create a command of 'set' with a required option 'value' with a value
* This function sets a value to an internal variable of ShellContext
*/
Command shell_cmd_set("set");
CommandOptionId set_opt_value = shell_cmd_set.add_option("value", true, "value of variable");
shell_cmd_set.set_option_require_value(set_opt_value, OPT_STRING);
ShellCommandId shell_cmd_set_id = shell.add_command(shell_cmd_set, "Set a value to internal variable 'a'");
shell.set_command_class(shell_cmd_set_id, arith_cmd_class);
shell.set_command_execute_function(shell_cmd_set_id, shell_execute_set);
/* Create a command of 'print'
* This function will print the value of an internal variable of ShellContext
*/
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);
/* Add a new class of commands */
ShellCommandClassId basic_cmd_class = shell.add_command_class("Basic");