2020-01-21 23:59:53 -06:00
|
|
|
/********************************************************************
|
|
|
|
* Test the shell interface by pre-defining simple commands
|
|
|
|
* like exit() and help()
|
|
|
|
*******************************************************************/
|
|
|
|
#include "command_echo.h"
|
2022-10-06 19:08:50 -05:00
|
|
|
#include "command_parser.h"
|
2020-01-21 23:59:53 -06:00
|
|
|
#include "shell.h"
|
2022-10-06 19:08:50 -05:00
|
|
|
#include "vtr_log.h"
|
2020-01-21 23:59:53 -06:00
|
|
|
|
2020-01-22 17:49:32 -06:00
|
|
|
using namespace openfpga;
|
2020-01-21 23:59:53 -06:00
|
|
|
|
|
|
|
class ShellContext {
|
2022-10-06 19:08:50 -05:00
|
|
|
public:
|
|
|
|
int a;
|
2020-01-21 23:59:53 -06:00
|
|
|
};
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
static int shell_execute_set(ShellContext& context, const Command& cmd,
|
|
|
|
const CommandContext& cmd_context) {
|
2020-01-22 17:49:32 -06:00
|
|
|
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());
|
2020-04-08 17:18:05 -05:00
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
return CMD_EXEC_SUCCESS;
|
2020-01-22 17:49:32 -06:00
|
|
|
}
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
static int shell_execute_print(ShellContext& context) {
|
2020-01-22 17:49:32 -06:00
|
|
|
VTR_LOG("a=%d\n", context.a);
|
2020-04-08 17:18:05 -05:00
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
return CMD_EXEC_SUCCESS;
|
2020-01-22 17:49:32 -06:00
|
|
|
}
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
static int shell_execute_print_macro(int argc, char** argv) {
|
2020-01-22 20:30:36 -06:00
|
|
|
VTR_LOG("Number of arguments: %d\n", argc);
|
|
|
|
VTR_LOG("Detailed arguments:\n");
|
|
|
|
for (int iarg = 0; iarg < argc; ++iarg) {
|
|
|
|
VTR_LOG("\t[%d]: %s\n", iarg, argv[iarg]);
|
|
|
|
}
|
2020-01-22 21:20:10 -06:00
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
return CMD_EXEC_SUCCESS;
|
2020-01-22 20:30:36 -06:00
|
|
|
}
|
|
|
|
|
2020-01-21 23:59:53 -06:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
/* Create the command to launch shell in different modes */
|
|
|
|
Command start_cmd("test_shell");
|
|
|
|
/* Add two options:
|
2022-10-06 19:08:50 -05:00
|
|
|
* '--interactive', -i': launch the interactive mode
|
|
|
|
* '--file', -f': launch the script mode
|
2020-01-21 23:59:53 -06:00
|
|
|
*/
|
2022-10-06 19:08:50 -05:00
|
|
|
CommandOptionId opt_interactive = start_cmd.add_option(
|
|
|
|
"interactive", false, "Launch the shell in interactive mode");
|
2020-01-21 23:59:53 -06:00
|
|
|
start_cmd.set_option_short_name(opt_interactive, "i");
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
CommandOptionId opt_script_mode =
|
|
|
|
start_cmd.add_option("file", false, "Launch the shell in script mode");
|
2020-01-21 23:59:53 -06:00
|
|
|
start_cmd.set_option_require_value(opt_script_mode, OPT_STRING);
|
|
|
|
start_cmd.set_option_short_name(opt_script_mode, "f");
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
CommandOptionId opt_help = start_cmd.add_option("help", false, "Help desk");
|
2020-01-21 23:59:53 -06:00
|
|
|
start_cmd.set_option_short_name(opt_help, "h");
|
|
|
|
|
|
|
|
/* Create a shell object
|
|
|
|
* Add two commands, which are
|
|
|
|
* 1. help
|
|
|
|
* 2. exit
|
|
|
|
*/
|
2022-11-23 19:06:27 -06:00
|
|
|
Shell<ShellContext> shell;
|
|
|
|
shell.set_name("test_shell");
|
2020-01-22 17:05:14 -06:00
|
|
|
std::string shell_title;
|
2020-01-21 23:59:53 -06:00
|
|
|
|
2020-01-22 17:05:14 -06:00
|
|
|
shell_title += std::string("The MIT License\n");
|
|
|
|
shell_title += std::string("\n");
|
2022-10-06 19:08:50 -05:00
|
|
|
shell_title +=
|
|
|
|
std::string("Copyright (c) 2018 LNIS - The University of Utah\n");
|
2020-01-22 17:05:14 -06:00
|
|
|
shell_title += std::string("\n");
|
2022-10-06 19:08:50 -05:00
|
|
|
shell_title += std::string(
|
|
|
|
"Permission is hereby granted, free of charge, to any person obtaining a "
|
|
|
|
"copy\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"of this software and associated documentation files (the \"Software\"), "
|
|
|
|
"to deal\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"in the Software without restriction, including without limitation the "
|
|
|
|
"rights\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"to use, copy, modify, merge, publish, distribute, sublicense, and/or "
|
|
|
|
"sell\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"copies of the Software, and to permit persons to whom the Software is\n");
|
|
|
|
shell_title +=
|
|
|
|
std::string("furnished to do so, subject to the following conditions:\n");
|
2020-01-22 17:05:14 -06:00
|
|
|
shell_title += std::string("\n");
|
2022-10-06 19:08:50 -05:00
|
|
|
shell_title += std::string(
|
|
|
|
"The above copyright notice and this permission notice shall be included "
|
|
|
|
"in\n");
|
|
|
|
shell_title +=
|
|
|
|
std::string("all copies or substantial portions of the Software.\n");
|
2020-01-22 17:05:14 -06:00
|
|
|
shell_title += std::string("\n");
|
2022-10-06 19:08:50 -05:00
|
|
|
shell_title += std::string(
|
|
|
|
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS "
|
|
|
|
"OR\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
|
|
|
|
"MERCHANTABILITY,\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL "
|
|
|
|
"THE\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING "
|
|
|
|
"FROM,\n");
|
|
|
|
shell_title += std::string(
|
|
|
|
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS "
|
|
|
|
"IN\n");
|
2020-01-22 17:05:14 -06:00
|
|
|
shell_title += std::string("THE SOFTWARE.\n");
|
|
|
|
|
|
|
|
shell.add_title(shell_title.c_str());
|
|
|
|
|
2020-01-22 17:49:32 -06:00
|
|
|
/* Add a new class of commands */
|
|
|
|
ShellCommandClassId arith_cmd_class = shell.add_command_class("Arithmetic");
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
/* Create a command of 'set' with a required option 'value' with a value
|
|
|
|
* This function sets a value to an internal variable of ShellContext
|
2020-01-22 17:49:32 -06:00
|
|
|
*/
|
|
|
|
Command shell_cmd_set("set");
|
2022-10-06 19:08:50 -05:00
|
|
|
CommandOptionId set_opt_value =
|
|
|
|
shell_cmd_set.add_option("value", true, "value of variable");
|
2020-01-22 17:49:32 -06:00
|
|
|
shell_cmd_set.set_option_require_value(set_opt_value, OPT_STRING);
|
2022-10-06 19:08:50 -05:00
|
|
|
ShellCommandId shell_cmd_set_id =
|
|
|
|
shell.add_command(shell_cmd_set, "Set a value to internal variable 'a'");
|
2020-01-22 17:49:32 -06:00
|
|
|
shell.set_command_class(shell_cmd_set_id, arith_cmd_class);
|
|
|
|
shell.set_command_execute_function(shell_cmd_set_id, shell_execute_set);
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
/* Create a command of 'print'
|
|
|
|
* This function will print the value of an internal variable of ShellContext
|
2020-01-24 17:46:39 -06:00
|
|
|
* We set a dependency to this command as it MUST be executed after 'set'
|
2020-01-22 17:49:32 -06:00
|
|
|
*/
|
|
|
|
Command shell_cmd_print("print");
|
2022-10-06 19:08:50 -05:00
|
|
|
ShellCommandId shell_cmd_print_id = shell.add_command(
|
|
|
|
shell_cmd_print, "Print the value of internal variable 'a'");
|
2020-01-22 17:49:32 -06:00
|
|
|
shell.set_command_class(shell_cmd_print_id, arith_cmd_class);
|
|
|
|
shell.set_command_execute_function(shell_cmd_print_id, shell_execute_print);
|
2022-10-06 19:08:50 -05:00
|
|
|
shell.set_command_dependency(
|
|
|
|
shell_cmd_print_id, std::vector<ShellCommandId>(1, shell_cmd_set_id));
|
2020-01-22 17:49:32 -06:00
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
/* Create a macro command of 'print_macro'
|
|
|
|
* This function will print the value of an internal variable of ShellContext
|
2020-01-22 20:30:36 -06:00
|
|
|
*/
|
|
|
|
Command shell_cmd_print_macro("print_macro");
|
2022-10-06 19:08:50 -05:00
|
|
|
ShellCommandId shell_cmd_print_macro_id = shell.add_command(
|
|
|
|
shell_cmd_print_macro, "A macro function to print arguments");
|
2020-01-22 20:30:36 -06:00
|
|
|
shell.set_command_class(shell_cmd_print_macro_id, arith_cmd_class);
|
2022-10-06 19:08:50 -05:00
|
|
|
shell.set_command_execute_function(shell_cmd_print_macro_id,
|
|
|
|
shell_execute_print_macro);
|
2020-01-22 20:30:36 -06:00
|
|
|
|
2020-01-22 17:05:14 -06:00
|
|
|
/* Add a new class of commands */
|
|
|
|
ShellCommandClassId basic_cmd_class = shell.add_command_class("Basic");
|
2020-01-21 23:59:53 -06:00
|
|
|
|
|
|
|
Command shell_cmd_exit("exit");
|
2022-10-06 19:08:50 -05:00
|
|
|
ShellCommandId shell_cmd_exit_id =
|
|
|
|
shell.add_command(shell_cmd_exit, "Exit the shell");
|
2020-01-22 17:05:14 -06:00
|
|
|
shell.set_command_class(shell_cmd_exit_id, basic_cmd_class);
|
2022-10-06 19:08:50 -05:00
|
|
|
shell.set_command_execute_function(shell_cmd_exit_id,
|
|
|
|
[shell]() { shell.exit(); });
|
2020-01-22 17:05:14 -06:00
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
/* Note: help must be the last to add because the linking to execute function
|
|
|
|
* will do a snapshot on the shell */
|
2020-01-22 17:05:14 -06:00
|
|
|
Command shell_cmd_help("help");
|
2022-10-06 19:08:50 -05:00
|
|
|
ShellCommandId shell_cmd_help_id =
|
|
|
|
shell.add_command(shell_cmd_help, "Launch help desk");
|
2020-01-22 17:05:14 -06:00
|
|
|
shell.set_command_class(shell_cmd_help_id, basic_cmd_class);
|
2022-10-06 19:08:50 -05:00
|
|
|
shell.set_command_execute_function(shell_cmd_help_id,
|
|
|
|
[shell]() { shell.print_commands(); });
|
2020-01-21 23:59:53 -06:00
|
|
|
|
|
|
|
/* Create the data base for the shell */
|
|
|
|
ShellContext shell_context;
|
|
|
|
|
2022-10-06 19:08:50 -05:00
|
|
|
/* Parse the option, to avoid issues, we use the command name to replace the
|
|
|
|
* argv[0] */
|
|
|
|
std::vector<std::string> cmd_opts;
|
2020-01-21 23:59:53 -06:00
|
|
|
cmd_opts.push_back(start_cmd.name());
|
|
|
|
for (int iarg = 1; iarg < argc; ++iarg) {
|
|
|
|
cmd_opts.push_back(std::string(argv[iarg]));
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandContext start_cmd_context(start_cmd);
|
|
|
|
if (false == parse_command(cmd_opts, start_cmd, start_cmd_context)) {
|
|
|
|
/* Parse fail: Echo the command */
|
|
|
|
print_command_options(start_cmd);
|
|
|
|
} else {
|
2022-10-06 19:08:50 -05:00
|
|
|
/* Parse succeed. Start a shell */
|
2020-01-21 23:59:53 -06:00
|
|
|
if (true == start_cmd_context.option_enable(start_cmd, opt_interactive)) {
|
|
|
|
shell.run_interactive_mode(shell_context);
|
|
|
|
return 0;
|
2022-10-06 19:08:50 -05:00
|
|
|
}
|
2020-01-21 23:59:53 -06:00
|
|
|
|
|
|
|
if (true == start_cmd_context.option_enable(start_cmd, opt_script_mode)) {
|
2022-10-06 19:08:50 -05:00
|
|
|
shell.run_script_mode(
|
|
|
|
start_cmd_context.option_value(start_cmd, opt_script_mode).c_str(),
|
|
|
|
shell_context);
|
2020-01-21 23:59:53 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2020-01-22 17:05:14 -06:00
|
|
|
/* Reach here there is something wrong, show the help desk */
|
|
|
|
print_command_options(start_cmd);
|
2020-01-21 23:59:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|