19 #ifndef UUID_CONSOLE_H_
20 #define UUID_CONSOLE_H_
37 #include <uuid/common.h>
40 #ifndef UUID_LOG_THREAD_SAFE
41 # define UUID_LOG_THREAD_SAFE 0
44 #ifndef UUID_COMMON_STD_MUTEX_AVAILABLE
45 # define UUID_COMMON_STD_MUTEX_AVAILABLE 0
48 #if defined(DOXYGEN) || UUID_COMMON_STD_MUTEX_AVAILABLE
49 # define UUID_CONSOLE_THREAD_SAFE 1
51 # define UUID_CONSOLE_THREAD_SAFE 0
54 #if UUID_CONSOLE_THREAD_SAFE
73 #if UUID_COMMON_THREAD_SAFE && UUID_LOG_THREAD_SAFE && UUID_CONSOLE_THREAD_SAFE
132 Shell &shell,
const std::vector<std::string> ¤t_arguments,
const std::string &next_argument)>;
154 inline const std::vector<std::string> &
name()
const {
return name_; }
357 const_iterator
cbegin()
const;
369 const_iterator
cend()
const;
511 void add_command(
unsigned int context,
unsigned int flags,
530 void add_command(
unsigned int context,
unsigned int flags,
552 void add_command(
unsigned int context,
unsigned int flags,
571 void add_command(
unsigned int context,
unsigned int flags,
unsigned int not_flags,
592 void add_command(
unsigned int context,
unsigned int flags,
unsigned int not_flags,
616 void add_command(
unsigned int context,
unsigned int flags,
unsigned int not_flags,
682 Command(
unsigned int flags,
unsigned int not_flags,
724 std::multimap<size_t,const Command*>
exact;
726 std::vector<const Command*>
all;
751 static bool find_longest_common_prefix(
const std::multimap<size_t,const Command*> &commands, std::vector<std::string> &longest_name);
824 Shell(Stream &stream, std::shared_ptr<Commands> commands,
unsigned int context = 0,
unsigned int flags = 0);
899 virtual void operator<<(std::shared_ptr<uuid::log::Message> message);
1031 inline bool has_flags(
unsigned int flags,
unsigned int not_flags = 0)
const {
1153 int read() final override;
1164 int peek() final override;
1173 size_t write(uint8_t data) final override;
1182 size_t write(const uint8_t *buffer,
size_t size) final override;
1193 void flush() final override;
1195 using ::Print::
print;
1203 size_t print(const std::
string &data);
1212 size_t println(const std::
string &data);
1221 size_t printf(const
char *format, ...) ;
1230 size_t printf(const __FlashStringHelper *format, ...) ;
1239 size_t printfln(const
char *format, ...) ;
1248 size_t printfln(const __FlashStringHelper *format, ...) ;
1490 QueuedLogMessage(
unsigned long id, std::shared_ptr<uuid::log::Message> &&content);
1604 size_t vprintf(
const char *format, va_list ap);
1613 size_t vprintf(
const __FlashStringHelper *format, va_list ap);
1619 #if UUID_CONSOLE_THREAD_SAFE
1667 explicit CommandLine(std::initializer_list<
const std::vector<std::string>> arguments);
1791 return !(lhs == rhs);
size_t maximum_log_messages() const
Get the maximum number of queued log messages.
Completion complete_command(Shell &shell, const CommandLine &command_line)
Complete a partial command for a Shell if it exists in the current context and with the current flags...
size_t maximum_command_line_length() const
Get the maximum length of a command line.
Commands()=default
Construct a new container of commands for use by a Shell.
Logger instance used to make log messages.
Representation of a command line, with parameters separated by spaces and an optional trailing space.
void update()
Update the available command from the current command iterator.
Commands::AvailableCommands available_commands() const
Get the available commands in the current context and with the current flags.
virtual void end_of_transmission()
The end of transmission character has been received.
void display_prompt()
Output a prompt on the shell.
virtual void display_banner()
Output the startup banner.
Base class for a command shell.
std::list< QueuedLogMessage > log_messages_
BlockingData(blocking_function &&blocking_function)
Create Mode::DELAY shell mode data.
reference operator*() const
Dereference the current available command.
size_t print(const std::string &data)
Output a string.
Log message that has been queued.
const_iterator & operator--()
Pre-decrement the current iterator to the previous available command.
bool has_all_flags(unsigned int flags) const
Check if the current flags include all of the specified flags.
size_t maximum_arguments() const
Determine the maximum number of arguments for this command based on the length of help text for the a...
command_function function_
password_function password_function_
void enter_context(unsigned int context)
Push a new context onto the stack.
std::vector< const Command * > all
void reset()
Reset this command line's data.
std::shared_ptr< Commands > commands_
Base class of data for a shell mode.
int flags() const
Get the shell flags that must be set for this command to be available.
std::multimap< unsigned int, Command >::const_iterator command_iterator
const __FlashStringHelper * error
Mode
Current mode of the shell.
std::shared_ptr< AvailableCommand > available_command_
static constexpr size_t MAX_COMMAND_LINE_LENGTH
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs)
Compare an available commands iterator to another available commands iterator for equality.
const command_iterator end_
virtual bool exit_context()
Pop a context off the stack.
int peek() final override
Read one byte from the available input without advancing to the next one.
void delay_for(unsigned long ms, delay_function function)
Stop executing anything on this shell for a period of time.
unsigned long log_message_id_
void escape_initial_parameters()
Only escape the number of parameters that currently exist when formatting the command line for output...
delay_function delay_function_
const flash_string_vector name_
Result of a command execution operation.
static constexpr size_t MAX_LOG_MESSAGES
const Commands::Command & command_
std::vector< std::string > parameters_
void flush() final override
Does nothing.
size_t minimum_arguments() const
Determine the minimum number of arguments for this command based on the help text for the arguments t...
argument_completion_function arg_function_
void delay_until(uint64_t ms, delay_function function)
Stop executing anything on this shell until a future time is reached.
Command(unsigned int flags, unsigned int not_flags, const flash_string_vector name, const flash_string_vector arguments, command_function function, argument_completion_function arg_function)
Create a command for execution on a Shell.
size_t total_size() const
Get the total size of the command line parameters, taking into account the trailing space.
const_iterator(const Shell &shell, const command_iterator &begin, const command_iterator &command, const command_iterator &end)
Create an iterator describing an available command in a shell with the current flags.
std::function< void(Shell &shell)> delay_function
Function to handle the end of an execution delay.
virtual std::string prompt_prefix()
Get the prefix to be used at the beginning of the command prompt.
void stop()
Stop this shell from running.
bool has_any_flags(unsigned int flags) const
Check if the current flags include any of the specified flags.
static const uuid::log::Logger & logger()
Get the built-in uuid::log::Logger instance for shells.
AvailableCommand(const Command &command)
Construct a new command that is available for execution on a Shell.
Available commands in the shell's context with the current flags.
size_t printfln(const char *format,...)
Output a message followed by CRLF end of line characters.
unsigned int context() const
Get the context at the top of the stack.
Data for the Mode::PASSWORD shell mode.
void add_command(const flash_string_vector &name, command_function function)
Add a command with no arguments to the list of commands in this container.
size_t println(const std::string &data)
Output a string followed by CRLF end of line characters.
void escape_initial_parameters(size_t count)
Escape the first count parameters when formatting the command line for output.
uuid::log::Level log_level() const
Get the current log level.
const command_iterator begin_
const_iterator cbegin() const
Returns an iterator to the first command.
Level
Severity level of log messages.
blocking_function blocking_function_
friend bool operator!=(const const_iterator &lhs, const const_iterator &rhs)
Compare an available commands iterator to another available commands iterator for inequality.
AvailableCommands(const Shell &shell, const command_iterator &begin, const command_iterator &end)
Create an iterable object describing available commands in a shell with the current flags.
int available() final override
Check for available input.
std::unique_ptr< ModeData > mode_data_
pointer operator->() const
Access the current available command.
void process_password(bool completed)
Finish password entry.
const std::vector< std::string > & arguments() const
Get the help text of the command's arguments.
std::vector< const __FlashStringHelper * > flash_string_vector
Type definition for a std::vector of flash strings.
Logger handler used to process log messages.
const_iterator cend() const
Returns an iterator to the element following the last command.
Result of a command completion operation.
std::list< CommandLine > help
void check_idle_timeout()
Check idle timeout expiry.
const_iterator operator--(int)
Post-decrement the current iterator to the previous available command.
const_iterator operator++(int)
Post-increment the current iterator to the next available command.
Execution execute_command(Shell &shell, CommandLine &&command_line)
Execute a command for a Shell if it exists in the current context and with the current flags.
std::shared_ptr< const uuid::log::Message > content_
virtual void operator<<(std::shared_ptr< uuid::log::Message > message)
Add a new log message.
std::vector< std::string > & operator*()
Obtain the parameters for this command line.
Command for execution on a Shell.
std::function< bool(Shell &shell, bool stop)> blocking_function
Function to handle a blocking operation.
std::multimap< size_t, const Command * > exact
const flash_string_vector arguments_
friend bool operator!=(const CommandLine &lhs, const CommandLine &rhs)
Compare this command line to another command line for inequality.
void remove_flags(unsigned int flags)
Remove one or more flags from the current flags.
command_iterator command_
const std::vector< std::string > * operator->() const
Obtain the parameters for this command line.
const_iterator end() const
Returns an iterator to the element following the last command.
bool has_flags(unsigned int flags, unsigned int not_flags=0) const
Check if the current flags include all of the specified flags and none of the specified not_flags.
std::function< void(Shell &shell, std::vector< std::string > &arguments)> command_function
Function to handle a command.
const_iterator begin() const
Returns an iterator to the first command.
size_t vprintf(const char *format, va_list ap)
Output a message.
int read() final override
Read one byte from the available input.
void loop_one()
Perform one execution step of this shell.
Available command for execution on a Shell.
size_t printf(const char *format,...)
Output a message.
void print_all_available_commands()
Output a list of all available commands with their arguments.
QueuedLogMessage(unsigned long id, std::shared_ptr< uuid::log::Message > &&content)
Create a queued log message.
Shell(Stream &stream, std::shared_ptr< Commands > commands, unsigned int context=0, unsigned int flags=0)
Create a new Shell operating on a Stream with the given commands, default context and initial flags.
void process_command()
Try to execute a command with the current command line.
void loop_delay()
Perform one execution step in Mode::DELAY mode.
const std::vector< std::string > & name() const
Get the name of the command.
Match find_command(Shell &shell, const CommandLine &command_line)
Find commands by matching them against the command line.
void loop_blocking()
Perform one execution step in Mode::BLOCKING mode.
Container of commands for use by a Shell.
void delete_buffer_word(bool display)
Delete a word from the command line buffer.
Data for the Mode::BLOCKING shell mode.
size_t write(uint8_t data) final override
Write one byte to the output stream.
void enter_password(const __FlashStringHelper *prompt, password_function function)
Prompt for a password to be entered on this shell.
CommandLine()=default
Create an empty command line.
Iterator of available commands for execution on a Shell.
size_t escape_parameters_
const argument_completion_function & arg_function() const
Get the function to be used to perform argument completions for the command.
const __FlashStringHelper * password_prompt_
DelayData(uint64_t delay_time, delay_function &&delay_function)
Create Mode::DELAY shell mode data.
friend bool operator==(const CommandLine &lhs, const CommandLine &rhs)
Compare a command line to another command line for equality.
Result of a command find operation.
static std::set< std::shared_ptr< Shell > > & registered_shells()
Get registered running shells to be executed.
virtual std::string hostname_text()
Get the hostname to be included in the command prompt.
static constexpr bool thread_safe
Thread-safe status of the library.
static void loop_all()
Loop through all registered shell objects.
Data for the Mode::DELAY shell mode.
virtual void erase_current_line()
Output ANSI escape sequence to erase the current line.
virtual void erase_characters(size_t count)
Output ANSI escape sequence to erase characters.
void invoke_command(const std::string &line)
Invoke a command on the shell.
PasswordData(const __FlashStringHelper *password_prompt, password_function &&password_function)
Create Mode::PASSWORD shell mode data.
void output_logs()
Output queued log messages for this shell.
void add_flags(unsigned int flags)
Add one or more flags to the current flags.
void escape_all_parameters()
Escape all parameters when formatting the command line for output.
void start()
Perform startup process for this shell.
virtual void stopped()
Stop request event.
std::bidirectional_iterator_tag iterator_category
int not_flags() const
Get the shell flags that must not be set for this command to be available.
std::deque< unsigned int > context_
std::string to_string(size_t reserve=Shell::MAX_COMMAND_LINE_LENGTH) const
Format a command line from separate parameters using built-in escaping rules.
void process_completion()
Try to complete a command from the current command line.
std::vector< std::string > name_
void loop_password()
Perform one execution step in Mode::PASSWORD mode.
std::vector< std::string > arguments_
AvailableCommands available_commands(const Shell &shell) const
Get the available commands in the current context and with the current flags.
const std::vector< std::string > & operator*() const
Obtain the parameters for this command line.
std::function< const std::vector< std::string >(Shell &shell, const std::vector< std::string > ¤t_arguments, const std::string &next_argument)> argument_completion_function
Function to obtain completions for a command line.
virtual std::string prompt_suffix()
Get the prefix to be used at the end of the command prompt.
std::vector< std::string > * operator->()
Obtain the parameters for this command line.
std::multimap< unsigned int, Command > commands_
std::size_t difference_type
virtual void started()
Startup complete event.
virtual std::string context_text()
Get the text indicating the current context, to be included in the command prompt.
size_t maximum_log_messages_
void loop_normal()
Perform one execution step in Mode::NORMAL mode.
unsigned long idle_timeout() const
Get the idle timeout.
bool running() const
Determine if this shell is still running.
std::function< void(Shell &shell, bool completed, const std::string &password)> password_function
Function to handle the response to a password entry prompt.
static bool find_longest_common_prefix(const std::multimap< size_t, const Command * > &commands, std::vector< std::string > &longest_name)
Find the longest common prefix from a shortest match of commands.
size_t maximum_command_line_length_
std::multimap< size_t, const Command * > partial
const_iterator & operator++()
Pre-increment the current iterator to the next available command.
void block_with(blocking_function function)
Execute a blocking function on this shell until it returns true.