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
Representation of a command line, with parameters separated by spaces and an optional trailing space.
Definition: console.h:1642
std::vector< std::string > parameters_
Definition: console.h:1797
const std::vector< std::string > * operator->() const
Obtain the parameters for this command line.
Definition: console.h:1769
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 escape_all_parameters()
Escape all parameters when formatting the command line for output.
Definition: console.h:1716
void escape_initial_parameters()
Only escape the number of parameters that currently exist when formatting the command line for output...
Definition: console.h:1728
std::vector< std::string > * operator->()
Obtain the parameters for this command line.
Definition: console.h:1762
size_t total_size() const
Get the total size of the command line parameters, taking into account the trailing space.
Definition: console.h:1698
friend bool operator==(const CommandLine &lhs, const CommandLine &rhs)
Compare a command line to another command line for equality.
Definition: console.h:1779
void reset()
Reset this command line's data.
std::vector< std::string > & operator*()
Obtain the parameters for this command line.
Definition: console.h:1748
CommandLine()=default
Create an empty command line.
void escape_initial_parameters(size_t count)
Escape the first count parameters when formatting the command line for output.
Definition: console.h:1740
friend bool operator!=(const CommandLine &lhs, const CommandLine &rhs)
Compare this command line to another command line for inequality.
Definition: console.h:1790
const std::vector< std::string > & operator*() const
Obtain the parameters for this command line.
Definition: console.h:1755
Available command for execution on a Shell.
Definition: console.h:139
const std::vector< std::string > & arguments() const
Get the help text of the command's arguments.
Definition: console.h:162
AvailableCommand(const Command &command)
Construct a new command that is available for execution on a Shell.
std::vector< std::string > name_
Definition: console.h:198
int not_flags() const
Get the shell flags that must not be set for this command to be available.
Definition: console.h:194
const std::vector< std::string > & name() const
Get the name of the command.
Definition: console.h:154
int flags() const
Get the shell flags that must be set for this command to be available.
Definition: console.h:186
const argument_completion_function & arg_function() const
Get the function to be used to perform argument completions for the command.
Definition: console.h:178
std::vector< std::string > arguments_
Definition: console.h:199
const Commands::Command & command_
Definition: console.h:197
Iterator of available commands for execution on a Shell.
Definition: console.h:219
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
std::bidirectional_iterator_tag iterator_category
Definition: console.h:221
const_iterator & operator--()
Pre-decrement the current iterator to the previous available command.
const_iterator operator--(int)
Post-decrement the current iterator to the previous available command.
Definition: console.h:294
const_iterator operator++(int)
Post-increment the current iterator to the next available command.
Definition: console.h:277
const_iterator & operator++()
Pre-increment the current iterator to the next available command.
pointer operator->() const
Access the current available command.
Definition: console.h:260
void update()
Update the available command from the current command iterator.
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
reference operator*() const
Dereference the current available command.
Definition: console.h:251
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::shared_ptr< AvailableCommand > available_command_
Definition: console.h:329
Available commands in the shell's context with the current flags.
Definition: console.h:210
const_iterator cbegin() const
Returns an iterator to the first command.
const_iterator begin() const
Returns an iterator to the first command.
Definition: console.h:351
const_iterator cend() const
Returns an iterator to the element following the last command.
std::multimap< unsigned int, Command >::const_iterator command_iterator
Definition: console.h:212
const_iterator end() const
Returns an iterator to the element following the last command.
Definition: console.h:363
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.
Command for execution on a Shell.
Definition: console.h:662
command_function function_
Definition: console.h:709
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
argument_completion_function arg_function_
Definition: console.h:710
const flash_string_vector name_
Definition: console.h:707
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
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
const flash_string_vector arguments_
Definition: console.h:708
Container of commands for use by a Shell.
Definition: console.h:90
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
std::multimap< unsigned int, Command > commands_
Definition: console.h:762
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
Match find_command(Shell &shell, const CommandLine &command_line)
Find commands by matching them against the command line.
Definition: commands.cpp:456
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
AvailableCommands available_commands(const Shell &shell) const
Get the available commands in the current context and with the current flags.
std::function< void(Shell &shell, std::vector< std::string > &arguments)> command_function
Function to handle a command.
Definition: console.h:113
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
Commands()=default
Construct a new container of commands for use by a Shell.
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
Data for the Mode::BLOCKING shell mode.
Definition: console.h:1454
BlockingData(blocking_function &&blocking_function)
Create Mode::DELAY shell mode data.
Definition: shell.cpp:324
blocking_function blocking_function_
Definition: console.h:1467
Data for the Mode::DELAY shell mode.
Definition: console.h:1430
DelayData(uint64_t delay_time, delay_function &&delay_function)
Create Mode::DELAY shell mode data.
Definition: shell.cpp:300
delay_function delay_function_
Definition: console.h:1446
Base class of data for a shell mode.
Definition: console.h:1392
Data for the Mode::PASSWORD shell mode.
Definition: console.h:1405
password_function password_function_
Definition: console.h:1422
const __FlashStringHelper * password_prompt_
Definition: console.h:1421
PasswordData(const __FlashStringHelper *password_prompt, password_function &&password_function)
Create Mode::PASSWORD shell mode data.
Definition: shell.cpp:222
Log message that has been queued.
Definition: console.h:1481
QueuedLogMessage(unsigned long id, std::shared_ptr< uuid::log::Message > &&content)
Create a queued log message.
Definition: shell_log.cpp:50
std::shared_ptr< const uuid::log::Message > content_
Definition: console.h:1494
Base class for a command shell.
Definition: console.h:772
void delay_for(unsigned long ms, delay_function function)
Stop executing anything on this shell for a period of time.
Definition: shell.cpp:362
virtual void operator<<(std::shared_ptr< uuid::log::Message > message)
Add a new log message.
Definition: shell_log.cpp:55
size_t println(const std::string &data)
Output a string followed by CRLF end of line characters.
Definition: shell_print.cpp:38
size_t maximum_command_line_length_
Definition: console.h:1626
size_t maximum_command_line_length() const
Get the maximum length of a command line.
Definition: shell.cpp:398
size_t printf(const char *format,...)
Output a message.
Definition: shell_print.cpp:44
void loop_password()
Perform one execution step in Mode::PASSWORD mode.
Definition: shell.cpp:227
int read() final override
Read one byte from the available input.
virtual void end_of_transmission()
The end of transmission character has been received.
bool has_all_flags(unsigned int flags) const
Check if the current flags include all of the specified flags.
Definition: console.h:1045
virtual bool exit_context()
Pop a context off the stack.
Definition: shell.cpp:89
void loop_one()
Perform one execution step of this shell.
Definition: shell.cpp:102
void start()
Perform startup process for this shell.
Definition: shell.cpp:54
unsigned char previous_
Definition: console.h:1627
void output_logs()
Output queued log messages for this shell.
Definition: shell_log.cpp:94
size_t print(const std::string &data)
Output a string.
Definition: shell_print.cpp:30
virtual void stopped()
Stop request event.
Definition: shell.cpp:85
void block_with(blocking_function function)
Execute a blocking function on this shell until it returns true.
Definition: shell.cpp:373
virtual std::string prompt_prefix()
Get the prefix to be used at the beginning of the command prompt.
void loop_delay()
Perform one execution step in Mode::DELAY mode.
Definition: shell.cpp:305
std::mutex mutex_
Definition: console.h:1620
bool has_any_flags(unsigned int flags) const
Check if the current flags include any of the specified flags.
Definition: console.h:1057
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
unsigned int context() const
Get the context at the top of the stack.
Definition: console.h:979
static constexpr size_t MAX_COMMAND_LINE_LENGTH
Definition: console.h:774
unsigned int flags_
Definition: console.h:1618
unsigned long log_message_id_
Definition: console.h:1622
virtual std::string hostname_text()
Get the hostname to be included in the command prompt.
void enter_context(unsigned int context)
Push a new context onto the stack.
Definition: console.h:994
std::string line_buffer_
Definition: console.h:1625
uint64_t idle_timeout_
Definition: console.h:1633
void flush() final override
Does nothing.
uuid::log::Level log_level() const
Get the current log level.
Definition: shell_log.cpp:67
void loop_blocking()
Perform one execution step in Mode::BLOCKING mode.
Definition: shell.cpp:329
bool running() const
Determine if this shell is still running.
Definition: shell.cpp:68
int available() final override
Check for available input.
void enter_password(const __FlashStringHelper *prompt, password_function function)
Prompt for a password to be entered on this shell.
Definition: shell.cpp:355
Commands::AvailableCommands available_commands() const
Get the available commands in the current context and with the current flags.
Definition: shell.cpp:98
void loop_normal()
Perform one execution step in Mode::NORMAL mode.
Definition: shell.cpp:129
void invoke_command(const std::string &line)
Invoke a command on the shell.
Definition: shell.cpp:485
virtual void started()
Startup complete event.
Definition: shell.cpp:64
void remove_flags(unsigned int flags)
Remove one or more flags from the current flags.
Definition: console.h:1067
std::function< void(Shell &shell)> delay_function
Function to handle the end of an execution delay.
Definition: console.h:795
static constexpr size_t MAX_LOG_MESSAGES
Definition: console.h:775
virtual void erase_characters(size_t count)
Output ANSI escape sequence to erase characters.
void print_all_available_commands()
Output a list of all available commands with their arguments.
void process_command()
Try to execute a command with the current command line.
Definition: shell.cpp:407
void process_password(bool completed)
Finish password entry.
Definition: shell.cpp:468
virtual std::string prompt_suffix()
Get the prefix to be used at the end of the command prompt.
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
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
Mode
Current mode of the shell.
Definition: console.h:1380
void process_completion()
Try to complete a command from the current command line.
Definition: shell.cpp:432
virtual void display_banner()
Output the startup banner.
size_t write(uint8_t data) final override
Write one byte to the output stream.
size_t vprintf(const char *format, va_list ap)
Output a message.
Definition: shell_print.cpp:86
size_t maximum_log_messages() const
Get the maximum number of queued log messages.
Definition: shell_log.cpp:75
virtual void erase_current_line()
Output ANSI escape sequence to erase the current line.
size_t maximum_log_messages_
Definition: console.h:1624
virtual std::string context_text()
Get the text indicating the current context, to be included in the command prompt.
void display_prompt()
Output a prompt on the shell.
void delete_buffer_word(bool display)
Delete a word from the command line buffer.
Definition: shell.cpp:380
static const uuid::log::Logger & logger()
Get the built-in uuid::log::Logger instance for shells.
Definition: shell_log.cpp:44
int peek() final override
Read one byte from the available input without advancing to the next one.
void add_flags(unsigned int flags)
Add one or more flags to the current flags.
Definition: console.h:1017
void check_idle_timeout()
Check idle timeout expiry.
Definition: shell.cpp:506
std::unique_ptr< ModeData > mode_data_
Definition: console.h:1629
std::list< QueuedLogMessage > log_messages_
Definition: console.h:1623
uint64_t idle_time_
Definition: console.h:1632
std::shared_ptr< Commands > commands_
Definition: console.h:1616
std::function< bool(Shell &shell, bool stop)> blocking_function
Function to handle a blocking operation.
Definition: console.h:806
static std::set< std::shared_ptr< Shell > > & registered_shells()
Get registered running shells to be executed.
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
std::deque< unsigned int > context_
Definition: console.h:1617
size_t printfln(const char *format,...)
Output a message followed by CRLF end of line characters.
Definition: shell_print.cpp:64
void stop()
Stop this shell from running.
Definition: shell.cpp:72
unsigned long idle_timeout() const
Get the idle timeout.
Definition: shell.cpp:498
static void loop_all()
Loop through all registered shell objects.
Logger handler used to process log messages.
Definition: log.h:290
Logger instance used to make log messages.
Definition: log.h:347
static constexpr bool thread_safe
Thread-safe status of the library.
Definition: console.h:76
Level
Severity level of log messages.
Definition: log.h:84
Common utilities.
std::vector< const __FlashStringHelper * > flash_string_vector
Type definition for a std::vector of flash strings.
Definition: common.h:99
Result of a command completion operation.
Definition: console.h:1808
std::list< CommandLine > help
Definition: console.h:1809
Result of a command execution operation.
Definition: console.h:102
const __FlashStringHelper * error
Definition: console.h:103
Result of a command find operation.
Definition: console.h:723
std::multimap< size_t, const Command * > partial
Definition: console.h:725
std::vector< const Command * > all
Definition: console.h:726
std::multimap< size_t, const Command * > exact
Definition: console.h:724