My Project
console.h
1 /*
2  * uuid-console - Microcontroller console shell
3  * Copyright 2019,2021-2022 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef UUID_CONSOLE_H_
20 #define UUID_CONSOLE_H_
21 
22 #include <Arduino.h>
23 
24 #include <cstdarg>
25 #include <cstdint>
26 #include <deque>
27 #include <functional>
28 #include <iterator>
29 #include <limits>
30 #include <list>
31 #include <memory>
32 #include <map>
33 #include <set>
34 #include <string>
35 #include <vector>
36 
37 #include <uuid/common.h>
38 #include <uuid/log.h>
39 
40 #ifndef UUID_LOG_THREAD_SAFE
41 # define UUID_LOG_THREAD_SAFE 0
42 #endif
43 
44 #ifndef UUID_COMMON_STD_MUTEX_AVAILABLE
45 # define UUID_COMMON_STD_MUTEX_AVAILABLE 0
46 #endif
47 
48 #if defined(DOXYGEN) || UUID_COMMON_STD_MUTEX_AVAILABLE
49 # define UUID_CONSOLE_THREAD_SAFE 1
50 #else
51 # define UUID_CONSOLE_THREAD_SAFE 0
52 #endif
53 
54 #if UUID_CONSOLE_THREAD_SAFE
55 # include <mutex>
56 #endif
57 
58 namespace uuid {
59 
66 namespace console {
67 
73 #if UUID_COMMON_THREAD_SAFE && UUID_LOG_THREAD_SAFE && UUID_CONSOLE_THREAD_SAFE
74 static constexpr bool thread_safe = true;
75 #else
76 static constexpr bool thread_safe = false;
77 #endif
78 
79 class Commands;
80 class CommandLine;
81 class Shell;
82 
90 class Commands {
91 private:
92  class Command;
93 
94 public:
95  struct Completion;
96 
102  struct Execution {
103  const __FlashStringHelper *error;
104  };
105 
113  using command_function = std::function<void(Shell &shell, std::vector<std::string> &arguments)>;
131  using argument_completion_function = std::function<const std::vector<std::string>(
132  Shell &shell, const std::vector<std::string> &current_arguments, const std::string &next_argument)>;
133 
140  public:
146  AvailableCommand(const Command &command);
147 
154  inline const std::vector<std::string> &name() const { return name_; }
155 
162  inline const std::vector<std::string> &arguments() const { return arguments_; }
163 
170  inline const command_function &function() const { return command_.function_; };
171 
179 
186  inline int flags() const { return command_.flags_; }
187 
194  inline int not_flags() const { return command_.not_flags_; }
195 
196  private:
198  std::vector<std::string> name_;
199  std::vector<std::string> arguments_;
200  };
201 
211  public:
212  using command_iterator = std::multimap<unsigned int,Command>::const_iterator;
220  public:
221  using iterator_category = std::bidirectional_iterator_tag;
222  using difference_type = std::size_t;
224  using pointer = const value_type*;
225  using reference = const value_type&;
240  const_iterator(const Shell &shell, const command_iterator &begin,
241  const command_iterator &command, const command_iterator &end);
242 
251  inline reference operator*() const { return *available_command_; }
260  inline pointer operator->() const { return available_command_.get(); }
261 
277  inline const_iterator operator++(int) { auto tmp = *this; ++(*this); return tmp; }
278 
294  inline const_iterator operator--(int) { auto tmp = *this; --(*this); return tmp; }
295 
305  friend inline bool operator==(const const_iterator& lhs, const const_iterator& rhs) { return lhs.command_ == rhs.command_; }
315  friend inline bool operator!=(const const_iterator& lhs, const const_iterator& rhs) { return lhs.command_ != rhs.command_; }
316 
317  private:
323  void update();
324 
325  const Shell &shell_;
329  std::shared_ptr<AvailableCommand> available_command_;
330  };
331 
344  AvailableCommands(const Shell &shell, const command_iterator &begin, const command_iterator &end);
345 
351  inline const_iterator begin() const { return cbegin(); }
357  const_iterator cbegin() const;
363  inline const_iterator end() const { return cend(); }
369  const_iterator cend() const;
370 
371  private:
372  const Shell &shell_;
375  };
376 
384  Commands() = default;
385  ~Commands() = default;
386 
400  void add_command(const flash_string_vector &name, command_function function);
417  void add_command(const flash_string_vector &name, const flash_string_vector &arguments,
418  command_function function);
437  void add_command(const flash_string_vector &name, const flash_string_vector &arguments,
438  command_function function, argument_completion_function arg_function);
439 
454  void add_command(unsigned int context, const flash_string_vector &name,
455  command_function function);
473  void add_command(unsigned int context, const flash_string_vector &name,
474  const flash_string_vector &arguments, command_function function);
494  void add_command(unsigned int context, const flash_string_vector &name,
495  const flash_string_vector &arguments, command_function function,
496  argument_completion_function arg_function);
511  void add_command(unsigned int context, unsigned int flags,
512  const flash_string_vector &name, command_function function);
530  void add_command(unsigned int context, unsigned int flags,
531  const flash_string_vector &name, const flash_string_vector &arguments,
532  command_function function);
552  void add_command(unsigned int context, unsigned int flags,
553  const flash_string_vector &name, const flash_string_vector &arguments,
554  command_function function, argument_completion_function arg_function);
571  void add_command(unsigned int context, unsigned int flags, unsigned int not_flags,
572  const flash_string_vector &name, command_function function);
592  void add_command(unsigned int context, unsigned int flags, unsigned int not_flags,
593  const flash_string_vector &name, const flash_string_vector &arguments,
594  command_function function);
616  void add_command(unsigned int context, unsigned int flags, unsigned int not_flags,
617  const flash_string_vector &name, const flash_string_vector &arguments,
618  command_function function, argument_completion_function arg_function);
619 
630  Execution execute_command(Shell &shell, CommandLine &&command_line);
631 
642  Completion complete_command(Shell &shell, const CommandLine &command_line);
643 
655  AvailableCommands available_commands(const Shell &shell) const;
656 
657 private:
662  class Command {
663  public:
682  Command(unsigned int flags, unsigned int not_flags,
683  const flash_string_vector name, const flash_string_vector arguments,
684  command_function function, argument_completion_function arg_function);
685  ~Command();
686 
695  size_t minimum_arguments() const;
703  inline size_t maximum_arguments() const { return arguments_.size(); }
704 
705  unsigned int flags_;
706  unsigned int not_flags_;
712  private:
713  Command(const Command&) = delete;
714  Command& operator=(const Command&) = delete;
715  };
716 
723  struct Match {
724  std::multimap<size_t,const Command*> exact;
725  std::multimap<size_t,const Command*> partial;
726  std::vector<const Command*> all;
727  };
728 
738  Match find_command(Shell &shell, const CommandLine &command_line);
739 
751  static bool find_longest_common_prefix(const std::multimap<size_t,const Command*> &commands, std::vector<std::string> &longest_name);
752 
760  static std::string find_longest_common_prefix(const std::vector<std::string> &arguments);
761 
762  std::multimap<unsigned int,Command> commands_;
763 };
764 
772 class Shell: public std::enable_shared_from_this<Shell>, public uuid::log::Handler, public ::Stream {
773 public:
774  static constexpr size_t MAX_COMMAND_LINE_LENGTH = 80;
775  static constexpr size_t MAX_LOG_MESSAGES = 20;
788  using password_function = std::function<void(Shell &shell, bool completed, const std::string &password)>;
795  using delay_function = std::function<void(Shell &shell)>;
806  using blocking_function = std::function<bool(Shell &shell, bool stop)>;
807 
824  Shell(Stream &stream, std::shared_ptr<Commands> commands, unsigned int context = 0, unsigned int flags = 0);
825 
826  ~Shell() = default;
827 
836  static void loop_all();
837 
851  void start();
860  void loop_one();
868  bool running() const;
880  void stop();
881 
888  static const uuid::log::Logger& logger();
899  virtual void operator<<(std::shared_ptr<uuid::log::Message> message);
909  uuid::log::Level log_level() const;
920  void log_level(uuid::log::Level level);
921 
928  size_t maximum_command_line_length() const;
937  void maximum_command_line_length(size_t length);
944  size_t maximum_log_messages() const;
953  void maximum_log_messages(size_t count);
960  unsigned long idle_timeout() const;
969  void idle_timeout(unsigned long timeout);
970 
979  inline unsigned int context() const {
980  if (!context_.empty()) {
981  return context_.back();
982  } else {
983  return 0;
984  }
985  }
994  inline void enter_context(unsigned int context) {
995  context_.emplace_back(context);
996  }
1006  virtual bool exit_context();
1007 
1017  inline void add_flags(unsigned int flags) { flags_ |= flags; }
1031  inline bool has_flags(unsigned int flags, unsigned int not_flags = 0) const {
1032  return has_all_flags(flags) && !has_any_flags(not_flags);
1033  }
1045  inline bool has_all_flags(unsigned int flags) const { return (flags_ & flags) == flags; }
1057  inline bool has_any_flags(unsigned int flags) const { return (flags_ & flags) != 0; }
1067  inline void remove_flags(unsigned int flags) { flags_ &= ~flags; }
1068 
1084  void enter_password(const __FlashStringHelper *prompt, password_function function);
1085 
1099  void delay_for(unsigned long ms, delay_function function);
1100 
1118  void delay_until(uint64_t ms, delay_function function);
1119 
1132  void block_with(blocking_function function);
1133 
1143  int available() final override;
1153  int read() final override;
1164  int peek() final override;
1165 
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;
1194 
1195  using ::Print::print;
1203  size_t print(const std::string &data);
1204  using ::Print::println;
1212  size_t println(const std::string &data);
1221  size_t printf(const char *format, ...) /* __attribute__((format(printf, 2, 3))) */;
1230  size_t printf(const __FlashStringHelper *format, ...) /* __attribute__((format(printf, 2, 3))) */;
1239  size_t printfln(const char *format, ...) /* __attribute__((format (printf, 2, 3))) */;
1248  size_t printfln(const __FlashStringHelper *format, ...) /* __attribute__((format(printf, 2, 3))) */;
1249 
1260  Commands::AvailableCommands available_commands() const;
1267 
1268 protected:
1274  virtual void erase_current_line();
1281  virtual void erase_characters(size_t count);
1282 
1289  virtual void started();
1296  virtual void display_banner();
1306  virtual std::string hostname_text();
1317  virtual std::string context_text();
1328  virtual std::string prompt_prefix();
1338  virtual std::string prompt_suffix();
1351  virtual void end_of_transmission();
1358  virtual void stopped();
1359 
1372  void invoke_command(const std::string &line);
1373 
1374 private:
1380  enum class Mode : uint8_t {
1381  NORMAL,
1382  PASSWORD,
1383  DELAY,
1384  BLOCKING,
1385  };
1386 
1392  class ModeData {
1393  public:
1394  virtual ~ModeData() = default;
1395 
1396  protected:
1397  ModeData() = default;
1398  };
1399 
1405  class PasswordData: public ModeData {
1406  public:
1418  PasswordData(const __FlashStringHelper *password_prompt, password_function &&password_function);
1419  ~PasswordData() override = default;
1420 
1421  const __FlashStringHelper *password_prompt_;
1423  };
1424 
1430  class DelayData: public ModeData {
1431  public:
1442  DelayData(uint64_t delay_time, delay_function &&delay_function);
1443  ~DelayData() override = default;
1444 
1445  uint64_t delay_time_;
1447  };
1448 
1454  class BlockingData: public ModeData {
1455  public:
1465  ~BlockingData() override = default;
1466 
1468  bool consume_line_feed_ = true;
1469  bool stop_ = false;
1470  };
1471 
1482  public:
1490  QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> &&content);
1491  ~QueuedLogMessage() = default;
1492 
1493  unsigned long id_;
1494  std::shared_ptr<const uuid::log::Message> content_;
1495  };
1496 
1497  Shell(const Shell&) = delete;
1498  Shell& operator=(const Shell&) = delete;
1499 
1506  static std::set<std::shared_ptr<Shell>>& registered_shells();
1507 
1515  void loop_normal();
1523  void loop_password();
1531  void loop_delay();
1539  void loop_blocking();
1540 
1550  void display_prompt();
1556  void output_logs();
1562  void process_command();
1568  void process_completion();
1578  void process_password(bool completed);
1579 
1585  void check_idle_timeout();
1586 
1594  void delete_buffer_word(bool display);
1595 
1604  size_t vprintf(const char *format, va_list ap);
1613  size_t vprintf(const __FlashStringHelper *format, va_list ap);
1614 
1615  Stream &stream_;
1616  std::shared_ptr<Commands> commands_;
1617  std::deque<unsigned int> context_;
1618  unsigned int flags_ = 0;
1619 #if UUID_CONSOLE_THREAD_SAFE
1620  mutable std::mutex mutex_;
1621 #endif
1622  unsigned long log_message_id_ = 0;
1623  std::list<QueuedLogMessage> log_messages_;
1625  std::string line_buffer_;
1627  unsigned char previous_ = 0;
1629  std::unique_ptr<ModeData> mode_data_ = nullptr;
1630  bool stopped_ = false;
1631  bool prompt_displayed_ = false;
1632  uint64_t idle_time_ = 0;
1633  uint64_t idle_timeout_ = 0;
1634 };
1635 
1643 public:
1649  CommandLine() = default;
1650 
1658  explicit CommandLine(const std::string &line);
1659 
1667  explicit CommandLine(std::initializer_list<const std::vector<std::string>> arguments);
1668 
1669  ~CommandLine() = default;
1670 
1671 #ifdef UNIT_TEST
1672  CommandLine(CommandLine&&) = default;
1673  CommandLine& operator=(CommandLine&&) = default;
1674  CommandLine(const CommandLine&) __attribute__((deprecated)) = default;
1675  CommandLine& operator=(const CommandLine&) __attribute__((deprecated)) = default;
1676 #endif
1677 
1688  std::string to_string(size_t reserve = Shell::MAX_COMMAND_LINE_LENGTH) const;
1689 
1698  inline size_t total_size() const { return parameters_.size() + (trailing_space ? 1 : 0); }
1706  void reset();
1707 
1716  inline void escape_all_parameters() { escape_parameters_ = std::numeric_limits<size_t>::max(); }
1740  inline void escape_initial_parameters(size_t count) { escape_parameters_ = count; }
1741 
1748  inline std::vector<std::string>& operator*() { return parameters_; }
1755  inline const std::vector<std::string>& operator*() const { return parameters_; }
1762  inline std::vector<std::string>* operator->() { return &parameters_; }
1769  inline const std::vector<std::string>* operator->() const { return &parameters_; }
1770 
1779  friend inline bool operator==(const CommandLine& lhs, const CommandLine& rhs) {
1780  return (lhs.trailing_space == rhs.trailing_space) && (lhs.parameters_ == rhs.parameters_);
1781  }
1790  friend inline bool operator!=(const CommandLine& lhs, const CommandLine& rhs) {
1791  return !(lhs == rhs);
1792  }
1793 
1794  bool trailing_space = false;
1796 private:
1797  std::vector<std::string> parameters_;
1798  size_t escape_parameters_ = std::numeric_limits<size_t>::max();
1799 };
1800 
1809  std::list<CommandLine> help;
1811 };
1812 
1813 } // namespace console
1814 
1815 } // namespace uuid
1816 
1817 #endif
uuid::console::Shell::maximum_log_messages
size_t maximum_log_messages() const
Get the maximum number of queued log messages.
Definition: shell_log.cpp:75
uuid::console::Commands::complete_command
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...
Definition: commands.cpp:230
uuid::console::Shell::maximum_command_line_length
size_t maximum_command_line_length() const
Get the maximum length of a command line.
Definition: shell.cpp:398
uuid::console::Commands::Commands
Commands()=default
Construct a new container of commands for use by a Shell.
uuid::log::Logger
Logger instance used to make log messages.
Definition: log.h:347
uuid::console::CommandLine
Representation of a command line, with parameters separated by spaces and an optional trailing space.
Definition: console.h:1642
uuid::console::Commands::AvailableCommands::const_iterator::update
void update()
Update the available command from the current command iterator.
Definition: commands_iterable.cpp:69
uuid::console::Shell::available_commands
Commands::AvailableCommands available_commands() const
Get the available commands in the current context and with the current flags.
Definition: shell.cpp:98
uuid::console::Shell::end_of_transmission
virtual void end_of_transmission()
The end of transmission character has been received.
Definition: shell_prompt.cpp:47
uuid::console::Shell::display_prompt
void display_prompt()
Output a prompt on the shell.
Definition: shell_prompt.cpp:54
uuid::console::Shell::display_banner
virtual void display_banner()
Output the startup banner.
Definition: shell_prompt.cpp:27
uuid::console::Shell
Base class for a command shell.
Definition: console.h:772
uuid::console::Shell::log_messages_
std::list< QueuedLogMessage > log_messages_
Definition: console.h:1623
uuid::console::Shell::BlockingData::BlockingData
BlockingData(blocking_function &&blocking_function)
Create Mode::DELAY shell mode data.
Definition: shell.cpp:324
uuid::console::Shell::idle_timeout_
uint64_t idle_timeout_
Definition: console.h:1633
uuid::console::Commands::AvailableCommands::const_iterator::operator*
reference operator*() const
Dereference the current available command.
Definition: console.h:251
uuid::console::Shell::print
size_t print(const std::string &data)
Output a string.
Definition: shell_print.cpp:30
uuid::console::Shell::QueuedLogMessage
Log message that has been queued.
Definition: console.h:1481
uuid::console::Commands::AvailableCommands::const_iterator::operator--
const_iterator & operator--()
Pre-decrement the current iterator to the previous available command.
Definition: commands_iterable.cpp:86
uuid::console::Shell::has_all_flags
bool has_all_flags(unsigned int flags) const
Check if the current flags include all of the specified flags.
Definition: console.h:1045
uuid::console::Commands::Command::maximum_arguments
size_t maximum_arguments() const
Determine the maximum number of arguments for this command based on the length of help text for the a...
Definition: console.h:703
uuid::console::Commands::Command::function_
command_function function_
Definition: console.h:709
uuid::console::Shell::PasswordData::password_function_
password_function password_function_
Definition: console.h:1422
uuid::console::Shell::enter_context
void enter_context(unsigned int context)
Push a new context onto the stack.
Definition: console.h:994
uuid::console::Commands::Match::all
std::vector< const Command * > all
Definition: console.h:726
uuid::console::Commands::Completion::replacement
CommandLine replacement
Definition: console.h:1810
uuid::console::CommandLine::reset
void reset()
Reset this command line's data.
Definition: command_line.cpp:158
uuid::console::Shell::commands_
std::shared_ptr< Commands > commands_
Definition: console.h:1616
uuid::console::Shell::ModeData
Base class of data for a shell mode.
Definition: console.h:1392
uuid::console::Commands::AvailableCommand::flags
int flags() const
Get the shell flags that must be set for this command to be available.
Definition: console.h:186
uuid::console::Commands::AvailableCommands::command_iterator
std::multimap< unsigned int, Command >::const_iterator command_iterator
Definition: console.h:212
uuid::console::Commands::Execution::error
const __FlashStringHelper * error
Definition: console.h:103
uuid::console::Shell::Mode
Mode
Current mode of the shell.
Definition: console.h:1380
uuid::console::Commands::Command::flags_
unsigned int flags_
Definition: console.h:705
uuid::console::Commands::AvailableCommands::const_iterator::available_command_
std::shared_ptr< AvailableCommand > available_command_
Definition: console.h:329
uuid::console::Shell::MAX_COMMAND_LINE_LENGTH
static constexpr size_t MAX_COMMAND_LINE_LENGTH
Definition: console.h:774
uuid::console::Commands::AvailableCommands::const_iterator::operator==
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs)
Compare an available commands iterator to another available commands iterator for equality.
Definition: console.h:305
uuid::console::Commands::AvailableCommands::const_iterator::end_
const command_iterator end_
Definition: console.h:328
uuid::console::Shell::exit_context
virtual bool exit_context()
Pop a context off the stack.
Definition: shell.cpp:89
uuid::console::Shell::peek
int peek() final override
Read one byte from the available input without advancing to the next one.
Definition: shell_stream.cpp:90
uuid::console::Shell::delay_for
void delay_for(unsigned long ms, delay_function function)
Stop executing anything on this shell for a period of time.
Definition: shell.cpp:362
uuid::console::Shell::log_message_id_
unsigned long log_message_id_
Definition: console.h:1622
uuid::console::CommandLine::escape_initial_parameters
void escape_initial_parameters()
Only escape the number of parameters that currently exist when formatting the command line for output...
Definition: console.h:1728
uuid::console::Shell::DelayData::delay_function_
delay_function delay_function_
Definition: console.h:1446
uuid::console::CommandLine::trailing_space
bool trailing_space
Definition: console.h:1794
uuid::console::Commands::Command::name_
const flash_string_vector name_
Definition: console.h:707
uuid::console::Commands::Execution
Result of a command execution operation.
Definition: console.h:102
uuid::console::Shell::MAX_LOG_MESSAGES
static constexpr size_t MAX_LOG_MESSAGES
Definition: console.h:775
uuid::console::Shell::BlockingData::stop_
bool stop_
Definition: console.h:1469
uuid::console::Commands::AvailableCommand::command_
const Commands::Command & command_
Definition: console.h:197
uuid::console::CommandLine::parameters_
std::vector< std::string > parameters_
Definition: console.h:1797
uuid::console::Shell::flush
void flush() final override
Does nothing.
Definition: shell_stream.cpp:125
uuid::console::Commands::Command::minimum_arguments
size_t minimum_arguments() const
Determine the minimum number of arguments for this command based on the help text for the arguments t...
Definition: commands.cpp:526
uuid::console::Commands::Command::arg_function_
argument_completion_function arg_function_
Definition: console.h:710
uuid::console::Shell::delay_until
void delay_until(uint64_t ms, delay_function function)
Stop executing anything on this shell until a future time is reached.
Definition: shell.cpp:366
uuid::console::Commands::Command::Command
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.
Definition: commands.cpp:514
uuid::console::CommandLine::total_size
size_t total_size() const
Get the total size of the command line parameters, taking into account the trailing space.
Definition: console.h:1698
uuid::console::Commands::AvailableCommands::const_iterator::const_iterator
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.
Definition: commands_iterable.cpp:62
uuid::console::Shell::delay_function
std::function< void(Shell &shell)> delay_function
Function to handle the end of an execution delay.
Definition: console.h:795
uuid::console::Shell::prompt_prefix
virtual std::string prompt_prefix()
Get the prefix to be used at the beginning of the command prompt.
Definition: shell_prompt.cpp:39
uuid::console::Shell::stop
void stop()
Stop this shell from running.
Definition: shell.cpp:72
uuid::console::Shell::has_any_flags
bool has_any_flags(unsigned int flags) const
Check if the current flags include any of the specified flags.
Definition: console.h:1057
uuid::console::Shell::previous_
unsigned char previous_
Definition: console.h:1627
uuid::console::Shell::stopped_
bool stopped_
Definition: console.h:1630
uuid::console::Shell::logger
static const uuid::log::Logger & logger()
Get the built-in uuid::log::Logger instance for shells.
Definition: shell_log.cpp:44
uuid::console::Commands::AvailableCommand::AvailableCommand
AvailableCommand(const Command &command)
Construct a new command that is available for execution on a Shell.
Definition: commands_iterable.cpp:33
uuid::console::Commands::AvailableCommands
Available commands in the shell's context with the current flags.
Definition: console.h:210
uuid::console::Shell::printfln
size_t printfln(const char *format,...)
Output a message followed by CRLF end of line characters.
Definition: shell_print.cpp:64
uuid::console::Shell::context
unsigned int context() const
Get the context at the top of the stack.
Definition: console.h:979
uuid::console::Shell::PasswordData
Data for the Mode::PASSWORD shell mode.
Definition: console.h:1405
uuid::console::Commands::add_command
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.
Definition: commands.cpp:46
uuid::console::Shell::println
size_t println(const std::string &data)
Output a string followed by CRLF end of line characters.
Definition: shell_print.cpp:38
uuid::console::Shell::line_buffer_
std::string line_buffer_
Definition: console.h:1625
uuid::console::CommandLine::escape_initial_parameters
void escape_initial_parameters(size_t count)
Escape the first count parameters when formatting the command line for output.
Definition: console.h:1740
uuid::console::Shell::log_level
uuid::log::Level log_level() const
Get the current log level.
Definition: shell_log.cpp:67
uuid::console::Commands::AvailableCommands::const_iterator::begin_
const command_iterator begin_
Definition: console.h:326
uuid::console::Commands::AvailableCommands::cbegin
const_iterator cbegin() const
Returns an iterator to the first command.
Definition: commands_iterable.cpp:54
uuid::log::Level
Level
Severity level of log messages.
Definition: log.h:84
uuid::console::Shell::BlockingData::blocking_function_
blocking_function blocking_function_
Definition: console.h:1467
uuid::console::Commands::AvailableCommands::const_iterator::operator!=
friend bool operator!=(const const_iterator &lhs, const const_iterator &rhs)
Compare an available commands iterator to another available commands iterator for inequality.
Definition: console.h:315
uuid::console::Shell::idle_time_
uint64_t idle_time_
Definition: console.h:1632
uuid::console::Commands::AvailableCommands::AvailableCommands
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.
Definition: commands_iterable.cpp:45
uuid::console::Shell::available
int available() final override
Check for available input.
Definition: shell_stream.cpp:27
uuid::console::Shell::mode_data_
std::unique_ptr< ModeData > mode_data_
Definition: console.h:1629
uuid::console::Commands::AvailableCommands::const_iterator::operator->
pointer operator->() const
Access the current available command.
Definition: console.h:260
uuid::console::Shell::process_password
void process_password(bool completed)
Finish password entry.
Definition: shell.cpp:468
uuid::console::Commands::AvailableCommand::arguments
const std::vector< std::string > & arguments() const
Get the help text of the command's arguments.
Definition: console.h:162
uuid::flash_string_vector
std::vector< const __FlashStringHelper * > flash_string_vector
Type definition for a std::vector of flash strings.
Definition: common.h:99
uuid::log::Handler
Logger handler used to process log messages.
Definition: log.h:290
uuid::console::Commands::AvailableCommands::cend
const_iterator cend() const
Returns an iterator to the element following the last command.
Definition: commands_iterable.cpp:58
uuid::console::Commands::Completion
Result of a command completion operation.
Definition: console.h:1808
uuid::console::Commands::Completion::help
std::list< CommandLine > help
Definition: console.h:1809
uuid::console::Shell::check_idle_timeout
void check_idle_timeout()
Check idle timeout expiry.
Definition: shell.cpp:506
uuid::console::Shell::BlockingData::consume_line_feed_
bool consume_line_feed_
Definition: console.h:1468
uuid::console::Commands::AvailableCommands::const_iterator::operator--
const_iterator operator--(int)
Post-decrement the current iterator to the previous available command.
Definition: console.h:294
uuid::console::Commands::AvailableCommands::const_iterator::operator++
const_iterator operator++(int)
Post-increment the current iterator to the next available command.
Definition: console.h:277
uuid::console::Commands::execute_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.
Definition: commands.cpp:111
uuid::console::Shell::QueuedLogMessage::content_
std::shared_ptr< const uuid::log::Message > content_
Definition: console.h:1494
uuid::console::Shell::operator<<
virtual void operator<<(std::shared_ptr< uuid::log::Message > message)
Add a new log message.
Definition: shell_log.cpp:55
uuid::console::CommandLine::operator*
std::vector< std::string > & operator*()
Obtain the parameters for this command line.
Definition: console.h:1748
uuid::console::Commands::Command
Command for execution on a Shell.
Definition: console.h:662
uuid::console::Shell::blocking_function
std::function< bool(Shell &shell, bool stop)> blocking_function
Function to handle a blocking operation.
Definition: console.h:806
uuid::console::Commands::Match::exact
std::multimap< size_t, const Command * > exact
Definition: console.h:724
uuid::console::Commands::Command::arguments_
const flash_string_vector arguments_
Definition: console.h:708
uuid::console::CommandLine::operator!=
friend bool operator!=(const CommandLine &lhs, const CommandLine &rhs)
Compare this command line to another command line for inequality.
Definition: console.h:1790
uuid::console::Shell::remove_flags
void remove_flags(unsigned int flags)
Remove one or more flags from the current flags.
Definition: console.h:1067
uuid::console::Commands::AvailableCommands::const_iterator::command_
command_iterator command_
Definition: console.h:327
uuid::console::CommandLine::operator->
const std::vector< std::string > * operator->() const
Obtain the parameters for this command line.
Definition: console.h:1769
uuid::console::Commands::AvailableCommands::end
const_iterator end() const
Returns an iterator to the element following the last command.
Definition: console.h:363
uuid::console::Shell::has_flags
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.
Definition: console.h:1031
uuid::console::Commands::command_function
std::function< void(Shell &shell, std::vector< std::string > &arguments)> command_function
Function to handle a command.
Definition: console.h:113
uuid::console::Commands::AvailableCommands::begin
const_iterator begin() const
Returns an iterator to the first command.
Definition: console.h:351
uuid::console::Shell::vprintf
size_t vprintf(const char *format, va_list ap)
Output a message.
Definition: shell_print.cpp:86
uuid::console::Shell::read
int read() final override
Read one byte from the available input.
Definition: shell_stream.cpp:62
uuid::console::Shell::loop_one
void loop_one()
Perform one execution step of this shell.
Definition: shell.cpp:102
uuid::console::Commands::AvailableCommand
Available command for execution on a Shell.
Definition: console.h:139
uuid::console::Shell::printf
size_t printf(const char *format,...)
Output a message.
Definition: shell_print.cpp:44
uuid::console::Shell::print_all_available_commands
void print_all_available_commands()
Output a list of all available commands with their arguments.
Definition: shell_print.cpp:122
uuid::console::Commands::AvailableCommands::end_
command_iterator end_
Definition: console.h:374
uuid::console::Shell::QueuedLogMessage::QueuedLogMessage
QueuedLogMessage(unsigned long id, std::shared_ptr< uuid::log::Message > &&content)
Create a queued log message.
Definition: shell_log.cpp:50
uuid::console::Shell::Shell
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.
Definition: shell.cpp:49
uuid::console::Shell::process_command
void process_command()
Try to execute a command with the current command line.
Definition: shell.cpp:407
uuid::console::Shell::loop_delay
void loop_delay()
Perform one execution step in Mode::DELAY mode.
Definition: shell.cpp:305
uuid::console::Commands::AvailableCommand::name
const std::vector< std::string > & name() const
Get the name of the command.
Definition: console.h:154
uuid::console::Shell::stream_
Stream & stream_
Definition: console.h:1615
uuid::console::Shell::DelayData::delay_time_
uint64_t delay_time_
Definition: console.h:1445
uuid::console::Commands::find_command
Match find_command(Shell &shell, const CommandLine &command_line)
Find commands by matching them against the command line.
Definition: commands.cpp:456
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28
uuid::console::Commands::AvailableCommands::shell_
const Shell & shell_
Definition: console.h:372
uuid::console::Shell::flags_
unsigned int flags_
Definition: console.h:1618
uuid::console::Shell::loop_blocking
void loop_blocking()
Perform one execution step in Mode::BLOCKING mode.
Definition: shell.cpp:329
uuid::console::Commands
Container of commands for use by a Shell.
Definition: console.h:90
uuid::console::Commands::AvailableCommands::const_iterator::shell_
const Shell & shell_
Definition: console.h:325
uuid::console::Shell::delete_buffer_word
void delete_buffer_word(bool display)
Delete a word from the command line buffer.
Definition: shell.cpp:380
uuid::console::Shell::BlockingData
Data for the Mode::BLOCKING shell mode.
Definition: console.h:1454
uuid::console::Shell::write
size_t write(uint8_t data) final override
Write one byte to the output stream.
Definition: shell_stream.cpp:116
uuid::console::Shell::enter_password
void enter_password(const __FlashStringHelper *prompt, password_function function)
Prompt for a password to be entered on this shell.
Definition: shell.cpp:355
uuid::console::CommandLine::CommandLine
CommandLine()=default
Create an empty command line.
uuid::console::Commands::AvailableCommands::const_iterator
Iterator of available commands for execution on a Shell.
Definition: console.h:219
uuid::console::Shell::QueuedLogMessage::id_
unsigned long id_
Definition: console.h:1493
uuid::console::CommandLine::escape_parameters_
size_t escape_parameters_
Definition: console.h:1798
uuid::console::Commands::AvailableCommand::arg_function
const argument_completion_function & arg_function() const
Get the function to be used to perform argument completions for the command.
Definition: console.h:178
uuid::console::Shell::PasswordData::password_prompt_
const __FlashStringHelper * password_prompt_
Definition: console.h:1421
uuid::console::Shell::DelayData::DelayData
DelayData(uint64_t delay_time, delay_function &&delay_function)
Create Mode::DELAY shell mode data.
Definition: shell.cpp:300
uuid::console::CommandLine::operator==
friend bool operator==(const CommandLine &lhs, const CommandLine &rhs)
Compare a command line to another command line for equality.
Definition: console.h:1779
uuid::console::Commands::Match
Result of a command find operation.
Definition: console.h:723
uuid::console::Shell::registered_shells
static std::set< std::shared_ptr< Shell > > & registered_shells()
Get registered running shells to be executed.
Definition: shell_loop_all.cpp:28
uuid::console::Shell::hostname_text
virtual std::string hostname_text()
Get the hostname to be included in the command prompt.
Definition: shell_prompt.cpp:31
uuid::console::thread_safe
static constexpr bool thread_safe
Thread-safe status of the library.
Definition: console.h:76
uuid::console::Commands::Command::not_flags_
unsigned int not_flags_
Definition: console.h:706
uuid::console::Shell::loop_all
static void loop_all()
Loop through all registered shell objects.
Definition: shell_loop_all.cpp:34
uuid::console::Shell::DelayData
Data for the Mode::DELAY shell mode.
Definition: console.h:1430
uuid::console::Shell::erase_current_line
virtual void erase_current_line()
Output ANSI escape sequence to erase the current line.
Definition: shell_print.cpp:131
uuid::console::Shell::erase_characters
virtual void erase_characters(size_t count)
Output ANSI escape sequence to erase characters.
Definition: shell_print.cpp:135
uuid::console::Shell::invoke_command
void invoke_command(const std::string &line)
Invoke a command on the shell.
Definition: shell.cpp:485
uuid::console::Shell::PasswordData::PasswordData
PasswordData(const __FlashStringHelper *password_prompt, password_function &&password_function)
Create Mode::PASSWORD shell mode data.
Definition: shell.cpp:222
uuid::console::Shell::output_logs
void output_logs()
Output queued log messages for this shell.
Definition: shell_log.cpp:94
uuid::console::Shell::add_flags
void add_flags(unsigned int flags)
Add one or more flags to the current flags.
Definition: console.h:1017
uuid::console::CommandLine::escape_all_parameters
void escape_all_parameters()
Escape all parameters when formatting the command line for output.
Definition: console.h:1716
uuid::console::Shell::start
void start()
Perform startup process for this shell.
Definition: shell.cpp:54
uuid::console::Shell::stopped
virtual void stopped()
Stop request event.
Definition: shell.cpp:85
uuid::console::Commands::AvailableCommands::const_iterator::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition: console.h:221
uuid::console::Commands::AvailableCommand::not_flags
int not_flags() const
Get the shell flags that must not be set for this command to be available.
Definition: console.h:194
uuid::console::Shell::context_
std::deque< unsigned int > context_
Definition: console.h:1617
uuid::console::CommandLine::to_string
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.
Definition: command_line.cpp:113
uuid::console::Shell::process_completion
void process_completion()
Try to complete a command from the current command line.
Definition: shell.cpp:432
uuid::console::Commands::AvailableCommand::name_
std::vector< std::string > name_
Definition: console.h:198
uuid::console::Shell::loop_password
void loop_password()
Perform one execution step in Mode::PASSWORD mode.
Definition: shell.cpp:227
uuid::console::Commands::AvailableCommand::arguments_
std::vector< std::string > arguments_
Definition: console.h:199
uuid::console::Commands::available_commands
AvailableCommands available_commands(const Shell &shell) const
Get the available commands in the current context and with the current flags.
Definition: commands_iterable.cpp:28
uuid::console::CommandLine::operator*
const std::vector< std::string > & operator*() const
Obtain the parameters for this command line.
Definition: console.h:1755
uuid::console::Shell::prompt_displayed_
bool prompt_displayed_
Definition: console.h:1631
uuid::console::Commands::argument_completion_function
std::function< const std::vector< std::string >(Shell &shell, const std::vector< std::string > &current_arguments, const std::string &next_argument)> argument_completion_function
Function to obtain completions for a command line.
Definition: console.h:132
uuid::console::Commands::AvailableCommands::begin_
command_iterator begin_
Definition: console.h:373
uuid::console::Shell::prompt_suffix
virtual std::string prompt_suffix()
Get the prefix to be used at the end of the command prompt.
Definition: shell_prompt.cpp:43
uuid::console::CommandLine::operator->
std::vector< std::string > * operator->()
Obtain the parameters for this command line.
Definition: console.h:1762
uuid::console::Commands::commands_
std::multimap< unsigned int, Command > commands_
Definition: console.h:762
uuid::console::Shell::mode_
Mode mode_
Definition: console.h:1628
uuid::console::Commands::AvailableCommands::const_iterator::difference_type
std::size_t difference_type
Definition: console.h:222
uuid::console::Shell::started
virtual void started()
Startup complete event.
Definition: shell.cpp:64
uuid::console::Shell::context_text
virtual std::string context_text()
Get the text indicating the current context, to be included in the command prompt.
Definition: shell_prompt.cpp:35
uuid::console::Shell::maximum_log_messages_
size_t maximum_log_messages_
Definition: console.h:1624
uuid::console::Shell::loop_normal
void loop_normal()
Perform one execution step in Mode::NORMAL mode.
Definition: shell.cpp:129
uuid::console::Shell::idle_timeout
unsigned long idle_timeout() const
Get the idle timeout.
Definition: shell.cpp:498
uuid::console::Shell::Mode::NORMAL
@ NORMAL
uuid::console::Shell::running
bool running() const
Determine if this shell is still running.
Definition: shell.cpp:68
uuid::console::Shell::password_function
std::function< void(Shell &shell, bool completed, const std::string &password)> password_function
Function to handle the response to a password entry prompt.
Definition: console.h:788
uuid::console::Commands::find_longest_common_prefix
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.
Definition: commands.cpp:145
uuid::console::Shell::maximum_command_line_length_
size_t maximum_command_line_length_
Definition: console.h:1626
uuid::console::Commands::Match::partial
std::multimap< size_t, const Command * > partial
Definition: console.h:725
uuid::console::Commands::AvailableCommands::const_iterator::operator++
const_iterator & operator++()
Pre-increment the current iterator to the next available command.
Definition: commands_iterable.cpp:77
uuid::console::Shell::block_with
void block_with(blocking_function function)
Execute a blocking function on this shell until it returns true.
Definition: shell.cpp:373
uuid::console::Shell::mutex_
std::mutex mutex_
Definition: console.h:1620