19 #include <uuid/console.h>
30 #include <uuid/common.h>
33 #if !defined(__cpp_lib_make_unique) && !defined(DOXYGEN)
36 template<
typename _Tp,
typename... _Args>
37 inline unique_ptr<_Tp> make_unique(_Args&&... __args) {
38 return unique_ptr<_Tp>(
new _Tp(std::forward<_Args>(__args)...));
49 Shell::Shell(Stream &stream, std::shared_ptr<Commands> commands,
unsigned int context,
unsigned int flags)
50 : stream_(stream), commands_(std::move(commands)), flags_(flags) {
76 blocking_data->
stop_ =
true;
99 return commands_->available_commands(*
this);
130 const int input =
stream_.read();
137 const unsigned char c = input;
203 if (c >=
'\x20' && c <=
'\x7E') {
223 : password_prompt_(password_prompt), password_function_(std::move(
password_function)) {
228 const int input =
stream_.read();
235 const unsigned char c = input;
282 if (c >=
'\x20' && c <=
'\x7E') {
301 : delay_time_(delay_time), delay_function_(std::move(
delay_function)) {
314 function_copy(*
this);
337 if (blocking_data->blocking_function_(*
this, blocking_data->stop_)) {
338 bool stop_pending = blocking_data->
stop_;
358 mode_data_ = std::make_unique<Shell::PasswordData>(prompt, std::move(
function));
369 mode_data_ = std::make_unique<Shell::DelayData>(ms, std::move(
function));
376 mode_data_ = std::make_unique<Shell::BlockingData>(std::move(
function));
383 if (pos == std::string::npos) {
414 if (!command_line->empty()) {
416 auto execution =
commands_->execute_command(*
this, std::move(command_line));
418 if (execution.error !=
nullptr) {
422 println(F(
"No commands configured"));
435 if (!command_line->empty() &&
commands_) {
436 auto completion =
commands_->complete_command(*
this, command_line);
437 bool redisplay =
false;
439 if (!completion.help.empty()) {
443 for (
auto &help : completion.help) {
450 if (!completion.replacement->empty()) {
size_t maximum_command_line_length() const
Get the maximum length of a command line.
Representation of a command line, with parameters separated by spaces and an optional trailing space.
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.
BlockingData(blocking_function &&blocking_function)
Create Mode::DELAY shell mode data.
size_t print(const std::string &data)
Output a string.
password_function password_function_
void enter_context(unsigned int context)
Push a new context onto the stack.
uint64_t get_uptime_ms()
Get the current uptime as a 64-bit milliseconds value.
std::shared_ptr< Commands > commands_
virtual bool exit_context()
Pop a context off the stack.
void delay_for(unsigned long ms, delay_function function)
Stop executing anything on this shell for a period of time.
delay_function delay_function_
void delay_until(uint64_t ms, delay_function function)
Stop executing anything on this shell until a future time is reached.
std::function< void(Shell &shell)> delay_function
Function to handle the end of an execution delay.
void stop()
Stop this shell from running.
Available commands in the shell's context with the current flags.
unsigned int context() const
Get the context at the top of the stack.
Data for the Mode::PASSWORD shell mode.
size_t println(const std::string &data)
Output a string followed by CRLF end of line characters.
std::unique_ptr< ModeData > mode_data_
void process_password(bool completed)
Finish password entry.
static void register_handler(Handler *handler, Level level)
Register a log handler.
void check_idle_timeout()
Check idle timeout expiry.
std::function< bool(Shell &shell, bool stop)> blocking_function
Function to handle a blocking operation.
void loop_one()
Perform one execution step of this 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.
void process_command()
Try to execute a command with the current command line.
void loop_delay()
Perform one execution step in Mode::DELAY mode.
void loop_blocking()
Perform one execution step in Mode::BLOCKING mode.
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.
DelayData(uint64_t delay_time, delay_function &&delay_function)
Create Mode::DELAY shell mode data.
static std::set< std::shared_ptr< Shell > > & registered_shells()
Get registered running shells to be executed.
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 start()
Perform startup process for this shell.
virtual void stopped()
Stop request event.
std::deque< unsigned int > context_
void process_completion()
Try to complete a command from the current command line.
void loop_password()
Perform one execution step in Mode::PASSWORD mode.
virtual void started()
Startup complete event.
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.
size_t maximum_command_line_length_
void block_with(blocking_function function)
Execute a blocking function on this shell until it returns true.