From 3ae80a192fad7282ef5569e65ae5e0129e79a3f9 Mon Sep 17 00:00:00 2001 From: tangxifan Date: Mon, 20 Jan 2020 19:42:43 -0700 Subject: [PATCH] add command echo functionality for mini shell --- libopenfpga/libminishell/src/command_echo.cpp | 53 +++++++++++++++++++ libopenfpga/libminishell/src/command_echo.h | 24 +++++++++ libopenfpga/libminishell/src/command_parser.h | 7 +++ 3 files changed, 84 insertions(+) create mode 100644 libopenfpga/libminishell/src/command_echo.cpp create mode 100644 libopenfpga/libminishell/src/command_echo.h diff --git a/libopenfpga/libminishell/src/command_echo.cpp b/libopenfpga/libminishell/src/command_echo.cpp new file mode 100644 index 000000000..23b07c58c --- /dev/null +++ b/libopenfpga/libminishell/src/command_echo.cpp @@ -0,0 +1,53 @@ +/********************************************************************* + * This file includes functions that output the content of + * object Command and CommandContext + ********************************************************************/ +#include "vtr_assert.h" +#include "vtr_log.h" +#include "command_echo.h" + +/* Begin namespace minishell */ +namespace minishell { + +/********************************************************************* + * 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:", + cmd.name().c_str()); + for (const CommandOptionId& opt : cmd.options()) { + VTR_LOG("%s, %s : %s", + cmd.option_name(opt).c_str(), + cmd.option_short_name(opt).c_str(), + cmd.option_description(opt)); + } +} + +/********************************************************************* + * Print all the options that have been parsed to an object of CommandContext + * This function is mainly used to validate what options have been enabled + * for users' confirmation + ********************************************************************/ +void print_command_context(const Command& cmd, + const CommandContext& cmd_context) { + VTR_LOG("Confirm selected options when call command '%s':", + cmd.name().c_str()); + for (const CommandOptionId& opt : cmd.options()) { + if (false == cmd.option_require_value(opt)) { + VTR_LOG("%s, %s : %s", + cmd.option_name(opt).c_str(), + cmd.option_short_name(opt).c_str(), + cmd_context.option_enable(cmd, opt) ? "on" : "off"); + } else { + VTR_ASSERT_SAFE (true == cmd.option_require_value(opt)); + VTR_LOG("%s, %s : %s", + cmd.option_name(opt).c_str(), + cmd.option_short_name(opt).c_str(), + cmd_context.option_value(cmd, opt).c_str()); + } + } +} + + +} /* End namespace minshell */ diff --git a/libopenfpga/libminishell/src/command_echo.h b/libopenfpga/libminishell/src/command_echo.h new file mode 100644 index 000000000..eb3923100 --- /dev/null +++ b/libopenfpga/libminishell/src/command_echo.h @@ -0,0 +1,24 @@ +#ifndef COMMAND_ECHO_H +#define COMMAND_ECHO_H + +/******************************************************************** + * Include header files that are required by function declaration + *******************************************************************/ +#include "command.h" +#include "command_context.h" + +/******************************************************************** + * Function declaration + *******************************************************************/ + +/* Begin namespace minishell */ +namespace minishell { + +void print_command_options(const Command& cmd); + +void print_command_context(const Command& cmd, + const CommandContext& cmd_context); + +} /* End namespace minshell */ + +#endif diff --git a/libopenfpga/libminishell/src/command_parser.h b/libopenfpga/libminishell/src/command_parser.h index 3ffb76423..6aee4291b 100644 --- a/libopenfpga/libminishell/src/command_parser.h +++ b/libopenfpga/libminishell/src/command_parser.h @@ -1,10 +1,17 @@ #ifndef COMMAND_PARSER_H #define COMMAND_PARSER_H +/******************************************************************** + * Include header files that are required by function declaration + *******************************************************************/ #include #include "command.h" #include "command_context.h" +/******************************************************************** + * Function declaration + *******************************************************************/ + /* Begin namespace minishell */ namespace minishell {