updated uuid libs

This commit is contained in:
Proddy
2023-01-05 15:05:30 +01:00
parent 6370296c53
commit 3b196fc90d
27 changed files with 2013 additions and 1411 deletions

View File

@@ -1,6 +1,6 @@
/*
* uuid-console - Microcontroller console shell
* Copyright 2019 Simon Arlott
* Copyright 2019,2022 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -12,7 +12,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a std::copy of the GNU General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@@ -25,6 +25,7 @@
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#ifndef __cpp_lib_make_unique
@@ -43,23 +44,39 @@ namespace uuid {
namespace console {
void Commands::add_command(const string_vector & name, command_function function) {
add_command(0, 0, name, string_vector{}, function, nullptr);
add_command(0, 0, 0, name, string_vector{}, function, nullptr);
}
void Commands::add_command(const string_vector & name, const string_vector & arguments, command_function function) {
add_command(0, 0, name, arguments, function, nullptr);
add_command(0, 0, 0, name, arguments, function, nullptr);
}
void Commands::add_command(const string_vector & name, const string_vector & arguments, command_function function, argument_completion_function arg_function) {
add_command(0, 0, name, arguments, function, arg_function);
add_command(0, 0, 0, name, arguments, function, arg_function);
}
void Commands::add_command(unsigned int context, const string_vector & name, command_function function) {
add_command(context, 0, 0, name, string_vector{}, function, nullptr);
}
void Commands::add_command(unsigned int context, const string_vector & name, const string_vector & arguments, command_function function) {
add_command(context, 0, 0, name, arguments, function, nullptr);
}
void Commands::add_command(unsigned int context,
const string_vector & name,
const string_vector & arguments,
command_function function,
argument_completion_function arg_function) {
add_command(context, 0, 0, name, arguments, function, arg_function);
}
void Commands::add_command(unsigned int context, unsigned int flags, const string_vector & name, command_function function) {
add_command(context, flags, name, string_vector{}, function, nullptr);
add_command(context, flags, 0, name, string_vector{}, function, nullptr);
}
void Commands::add_command(unsigned int context, unsigned int flags, const string_vector & name, const string_vector & arguments, command_function function) {
add_command(context, flags, name, arguments, function, nullptr);
add_command(context, flags, 0, name, arguments, function, nullptr);
}
void Commands::add_command(unsigned int context,
@@ -68,34 +85,30 @@ void Commands::add_command(unsigned int context,
const string_vector & arguments,
command_function function,
argument_completion_function arg_function) {
commands_.emplace(std::piecewise_construct, std::forward_as_tuple(context), std::forward_as_tuple(flags, name, arguments, function, arg_function));
add_command(context, flags, 0, name, arguments, function, arg_function);
}
// added by proddy
// note we should really iterate and free up the lambda code and any flashstrings
void Commands::remove_all_commands() {
commands_.clear();
void Commands::add_command(unsigned int context, unsigned int flags, unsigned int not_flags, const string_vector & name, command_function function) {
add_command(context, flags, not_flags, name, string_vector{}, function, nullptr);
}
// added by proddy
// note we should really iterate and free up the lambda code and any flashstrings
void Commands::remove_context_commands(unsigned int context) {
commands_.erase(context);
void Commands::add_command(unsigned int context,
unsigned int flags,
unsigned int not_flags,
const string_vector & name,
const string_vector & arguments,
command_function function) {
add_command(context, flags, not_flags, name, arguments, function, nullptr);
}
/*
auto commands = commands_.equal_range(context);
for (auto command_it = commands.first; command_it != commands.second; command_it++) {
shell.printf("Got: ");
for (auto flash_name : command_it->second.name_) {
shell.printf("%s ", read_flash_string(flash_name).c_str());
}
shell.println();
}
size_t nun = commands_.erase(context);
shell.printfln("Erased %d commands", nun);
*/
void Commands::add_command(unsigned int context,
unsigned int flags,
unsigned int not_flags,
const string_vector & name,
const string_vector & arguments,
command_function function,
argument_completion_function arg_function) {
commands_.emplace(std::piecewise_construct, std::forward_as_tuple(context), std::forward_as_tuple(flags, not_flags, name, arguments, function, arg_function));
}
Commands::Execution Commands::execute_command(Shell & shell, CommandLine && command_line) {
@@ -106,7 +119,7 @@ Commands::Execution Commands::execute_command(Shell & shell, CommandLine && comm
result.error = nullptr;
if (commands.exact.empty()) {
result.error = F("Command not found");
result.error = "Command not found";
} else if (commands.exact.count(longest->first) == 1) {
auto & command = longest->second;
std::vector<std::string> arguments;
@@ -117,16 +130,16 @@ Commands::Execution Commands::execute_command(Shell & shell, CommandLine && comm
command_line.reset();
if (commands.partial.upper_bound(longest->first) != commands.partial.end() && !arguments.empty()) {
result.error = F("Command not found");
result.error = "Command not found";
} else if (arguments.size() < command->minimum_arguments()) {
result.error = F("Not enough arguments for command");
result.error = "Not enough arguments for command";
} else if (arguments.size() > command->maximum_arguments()) {
result.error = F("Too many arguments for command");
result.error = "Too many arguments for command";
} else {
command->function_(shell, arguments);
}
} else {
result.error = F("Fatal error (multiple commands found)");
result.error = "Fatal error (multiple commands found)";
}
return result;
@@ -145,7 +158,7 @@ bool Commands::find_longest_common_prefix(const std::multimap<size_t, const Comm
for (size_t length = 0; all_match && length < shortest_match; length++) {
for (auto command_it = std::next(commands.begin()); command_it != commands.end(); command_it++) {
if ((*std::next(first.begin(), length)) != (*std::next(command_it->second->name_.begin(), length))) {
if (*std::next(first.begin(), length) != *std::next(command_it->second->name_.begin(), length)) {
all_match = false;
break;
}
@@ -158,7 +171,7 @@ bool Commands::find_longest_common_prefix(const std::multimap<size_t, const Comm
auto name_it = first.begin();
for (size_t i = 0; i < component_prefix; i++) {
longest_name.push_back(std::move((*name_it)));
longest_name.push_back(std::move(*name_it));
name_it++;
}
}
@@ -173,11 +186,17 @@ bool Commands::find_longest_common_prefix(const std::multimap<size_t, const Comm
for (auto command_it = std::next(commands.begin()); command_it != commands.end(); command_it++) {
// This relies on the null terminator character limiting the
// length before it becomes longer than any of the strings
if (pgm_read_byte(reinterpret_cast<PGM_P>(first) + length)
!= pgm_read_byte(reinterpret_cast<PGM_P>(*std::next(command_it->second->name_.begin(), component_prefix)) + length)) {
if (pgm_read_byte(first + length) != pgm_read_byte((*std::next(command_it->second->name_.begin(), component_prefix)) + length)) {
all_match = false;
break;
}
// if (pgm_read_byte(reinterpret_cast<PGM_P>(first) + length)
// != pgm_read_byte(reinterpret_cast<PGM_P>(*std::next(command_it->second->name_.begin(), component_prefix)) + length)) {
// all_match = false;
// break;
// }
}
if (all_match) {
@@ -223,12 +242,16 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
auto match = commands.partial.begin();
size_t count;
bool multiple_matches;
if (match != commands.partial.end()) {
count = commands.partial.count(match->first);
multiple_matches =
count > 1 || commands.partial.size() > count || (!commands.exact.empty() && commands.exact.rbegin()->first >= command_line.total_size());
} else if (!commands.exact.empty()) {
// Use prev() because both iterators must be forwards
match = std::prev(commands.exact.end());
count = commands.exact.count(match->first);
match = std::prev(commands.exact.end());
count = commands.exact.count(match->first);
multiple_matches = false;
} else {
return result;
}
@@ -237,20 +260,13 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
std::vector<std::string> temp_command_name;
std::multimap<size_t, const Command *>::iterator temp_command_it;
if (commands.partial.size() > 1 && (commands.exact.empty() || command_line.total_size() > commands.exact.begin()->second->name_.size())) {
// There are multiple partial matching commands, find the longest common prefix
if (multiple_matches && (commands.exact.empty() || command_line.total_size() > commands.exact.begin()->second->name_.size())) {
// There are multiple matching commands, find the longest common prefix
bool whole_components = find_longest_common_prefix(commands.partial, temp_command_name);
if (count == 1 && whole_components && temp_command_name.size() == match->first) {
// If the longest common prefix is the same as the single shortest matching command
// then there's no need for a temporary command, but add a trailing space because
// there are longer commands that matched.
temp_command_name.clear();
result.replacement.trailing_space = true;
}
// Construct a temporary command with the longest common prefix to use as the replacement
if (!temp_command_name.empty() && command_line.total_size() <= temp_command_name.size()) {
temp_command = std::make_unique<Command>(0, string_vector{}, string_vector{}, nullptr, nullptr);
temp_command = std::make_unique<Command>(0, 0, string_vector{}, string_vector{}, nullptr, nullptr);
count = 1;
match = commands.partial.end();
result.replacement.trailing_space = whole_components;
@@ -261,12 +277,12 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
}
}
if (count == 1 && !temp_command) {
if (count == 1 && !multiple_matches) {
// Construct a replacement string for a single matching command
auto & matching_command = match->second;
for (auto & name : matching_command->name_) {
result.replacement->push_back(std::move((name)));
result.replacement->push_back(name);
}
if (command_line.total_size() > result.replacement->size()
@@ -290,7 +306,8 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
}
}
auto potential_arguments = matching_command->arg_function_ ? matching_command->arg_function_(shell, arguments) : std::vector<std::string>{};
auto potential_arguments =
matching_command->arg_function_ ? matching_command->arg_function_(shell, arguments, last_argument) : std::vector<std::string>{};
// Remove arguments that can't match
if (!command_line.trailing_space) {
@@ -340,7 +357,7 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
remaining_help.escape_initial_parameters();
for (auto it = std::next(matching_command->arguments_.cbegin(), current_args_count); it != matching_command->arguments_.cend(); it++) {
remaining_help->push_back(std::move((*it)));
remaining_help->push_back(std::move(*it));
}
}
@@ -366,13 +383,13 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
// Add a space because there are more arguments for this command
result.replacement.trailing_space = true;
}
} else if (count > 1 || temp_command) {
} else if (count != 0) {
// Provide help for all of the potential commands
for (auto command_it = commands.partial.begin(); command_it != commands.partial.end(); command_it++) {
for (auto command_it = commands.all.begin(); command_it != commands.all.end(); command_it++) {
CommandLine help;
auto line_it = command_line->cbegin();
auto flash_name_it = command_it->second->name_.cbegin();
auto flash_name_it = (*command_it)->name_.cbegin();
if (temp_command) {
// Skip parts of the command name/line when the longest common prefix was used
@@ -381,14 +398,19 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
skip--;
}
// Exact match that is shorter than the replacement
if (flash_name_it + skip > (*command_it)->name_.cend()) {
continue;
}
flash_name_it += skip;
while (line_it != command_line->cend()) {
line_it++;
}
}
for (; flash_name_it != command_it->second->name_.cend(); flash_name_it++) {
std::string name = (*flash_name_it);
for (; flash_name_it != (*command_it)->name_.cend(); flash_name_it++) {
std::string name = *flash_name_it;
// Skip parts of the command name that match the command line
if (line_it != command_line->cend()) {
@@ -404,22 +426,22 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
help.escape_initial_parameters();
for (auto argument : command_it->second->arguments_) {
for (auto argument : (*command_it)->arguments_) {
// Skip parts of the command arguments that exist in the command line
if (line_it != command_line->cend()) {
line_it++;
continue;
}
help->push_back(std::move((argument)));
help->push_back(argument);
}
result.help.push_back(std::move(help));
}
}
if (count > 1 && !commands.exact.empty()) {
// Try to add a space to exact matches
if (multiple_matches && !commands.exact.empty() && result.replacement.total_size() == 0) {
// Try to add a space to exact matches if there's no other partial match replacement
auto longest = commands.exact.crbegin();
if (commands.exact.count(longest->first) == 1) {
@@ -449,7 +471,7 @@ Commands::Match Commands::find_command(Shell & shell, const CommandLine & comman
bool match = true;
bool exact = true;
if (!shell.has_flags(command.flags_)) {
if (!shell.has_flags(command.flags_, command.not_flags_)) {
continue;
}
@@ -457,7 +479,7 @@ Commands::Match Commands::find_command(Shell & shell, const CommandLine & comman
auto line_it = command_line->cbegin();
for (; name_it != command.name_.cend() && line_it != command_line->cend(); name_it++, line_it++) {
std::string name = (*name_it);
std::string name = *name_it;
size_t found = name.rfind(*line_it, 0);
if (found == std::string::npos) {
@@ -491,41 +513,21 @@ Commands::Match Commands::find_command(Shell & shell, const CommandLine & comman
} else {
commands.partial.emplace(command.name_.size(), &command);
}
commands.all.push_back(&command);
}
}
return commands;
}
void Commands::for_each_available_command(Shell & shell, apply_function f) const {
auto commands = commands_.equal_range(shell.context());
for (auto command_it = commands.first; command_it != commands.second; command_it++) {
if (shell.has_flags(command_it->second.flags_)) {
std::vector<std::string> name;
std::vector<std::string> arguments;
name.reserve(command_it->second.name_.size());
for (auto flash_name : command_it->second.name_) {
name.push_back(std::move((flash_name)));
}
arguments.reserve(command_it->second.arguments_.size());
for (auto flash_argument : command_it->second.arguments_) {
arguments.push_back(std::move((flash_argument)));
}
f(name, arguments);
}
}
}
Commands::Command::Command(unsigned int flags,
unsigned int not_flags,
const string_vector name,
const string_vector arguments,
command_function function,
argument_completion_function arg_function)
: flags_(flags)
, not_flags_(not_flags)
, name_(name)
, arguments_(arguments)
, function_(function)

View File

@@ -0,0 +1,103 @@
/*
* uuid-console - Microcontroller console shell
* Copyright 2022 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uuid/console.h>
#include <memory>
#include <utility>
namespace uuid {
namespace console {
Commands::AvailableCommands Commands::available_commands(const Shell & shell) const {
auto range = commands_.equal_range(shell.context());
return AvailableCommands(shell, range.first, range.second);
}
Commands::AvailableCommand::AvailableCommand(const Command & command)
: command_(command) {
name_.reserve(command_.name_.size());
for (auto name : command_.name_) {
name_.push_back(name);
}
arguments_.reserve(command_.arguments_.size());
for (auto argument : command_.arguments_) {
arguments_.push_back(argument);
}
}
Commands::AvailableCommands::AvailableCommands(const Shell & shell, const command_iterator & begin, const command_iterator & end)
: shell_(shell)
, begin_(begin)
, end_(end) {
// Skip over unavailable commands at the beginning
while (begin_ != end_ && !shell_.has_flags(begin_->second.flags_, begin_->second.not_flags_)) {
++begin_;
}
}
Commands::AvailableCommands::const_iterator Commands::AvailableCommands::cbegin() const {
return AvailableCommands::const_iterator(shell_, begin_, begin_, end_);
}
Commands::AvailableCommands::const_iterator Commands::AvailableCommands::cend() const {
return AvailableCommands::const_iterator(shell_, begin_, end_, end_);
}
Commands::AvailableCommands::const_iterator::const_iterator(const Shell & shell,
const command_iterator & begin,
const command_iterator & command,
const command_iterator & end)
: shell_(std::move(shell))
, begin_(begin)
, command_(command)
, end_(end) {
update();
}
void Commands::AvailableCommands::const_iterator::update() {
if (command_ != end_) {
available_command_ = std::make_shared<AvailableCommand>(command_->second);
} else {
available_command_.reset();
}
}
Commands::AvailableCommands::const_iterator & Commands::AvailableCommands::const_iterator::operator++() {
do {
++command_;
} while (command_ != end_ && !shell_.has_flags(command_->second.flags_, command_->second.not_flags_));
update();
return *this;
}
Commands::AvailableCommands::const_iterator & Commands::AvailableCommands::const_iterator::operator--() {
do {
--command_;
} while (command_ != begin_ && !shell_.has_flags(command_->second.flags_, command_->second.not_flags_));
update();
return *this;
}
} // namespace console
} // namespace uuid

View File

@@ -45,21 +45,23 @@ namespace uuid {
namespace console {
// cppcheck-suppress passedByValue
Shell::Shell(std::shared_ptr<Commands> commands, unsigned int context, unsigned int flags)
: commands_(std::move(commands))
Shell::Shell(Stream & stream, std::shared_ptr<Commands> commands, unsigned int context, unsigned int flags)
: stream_(stream)
, commands_(std::move(commands))
, flags_(flags) {
enter_context(context);
}
Shell::~Shell() {
uuid::log::Logger::unregister_handler(this);
void Shell::started() {
}
bool Shell::running() const {
return !stopped_;
}
void Shell::start() {
#ifdef EMSESP_DEBUG
uuid::log::Logger::register_handler(this, uuid::log::Level::DEBUG); // added by proddy
//uuid::log::Logger::register_handler(this, uuid::log::Level::INFO); // added by proddy
uuid::log::Logger::register_handler(this, uuid::log::Level::DEBUG); // added for EMS-ESP
#else
uuid::log::Logger::register_handler(this, uuid::log::Level::INFO);
#endif
@@ -71,18 +73,11 @@ void Shell::start() {
}
display_banner();
display_prompt();
shells_.insert(shared_from_this());
registered_shells().insert(shared_from_this());
idle_time_ = uuid::get_uptime_ms();
started();
};
void Shell::started() {
}
bool Shell::running() const {
return !stopped_;
}
void Shell::stop() {
if (mode_ == Mode::BLOCKING) {
auto * blocking_data = reinterpret_cast<Shell::BlockingData *>(mode_data_.get());
@@ -108,6 +103,10 @@ bool Shell::exit_context() {
}
}
Commands::AvailableCommands Shell::available_commands() const {
return commands_->available_commands(*this);
}
void Shell::loop_one() {
if (!running()) {
return;
@@ -144,7 +143,7 @@ void Shell::set_command_str(const char * str) {
}
void Shell::loop_normal() {
const int input = read_one_char();
const int input = stream_.read();
if (input < 0) {
check_idle_timeout();
@@ -158,8 +157,10 @@ void Shell::loop_normal() {
// Interrupt (^C)
line_buffer_.clear();
println();
cursor_ = 0;
line_no_ = 0;
cursor_ = 0;
line_no_ = 0;
// prompt_displayed_ = false;
// display_prompt();
break;
case '\x04':
@@ -304,18 +305,20 @@ void Shell::loop_normal() {
}
// common for all, display the complete line
// added for EMS-ESP
erase_current_line();
prompt_displayed_ = false;
display_prompt();
if (cursor_) {
printf(F("\033[%dD"), cursor_);
printf("\033[%dD", cursor_);
}
previous_ = c;
// This is a hack to let TelnetStream know that command
// execution is complete and that output can be flushed.
available_char();
stream_.available();
idle_time_ = uuid::get_uptime_ms();
}
@@ -326,7 +329,7 @@ Shell::PasswordData::PasswordData(const char * password_prompt, password_functio
}
void Shell::loop_password() {
const int input = read_one_char();
const int input = stream_.read();
if (input < 0) {
check_idle_timeout();
@@ -393,7 +396,7 @@ void Shell::loop_password() {
// This is a hack to let TelnetStream know that command
// execution is complete and that output can be flushed.
available_char();
stream_.available();
idle_time_ = uuid::get_uptime_ms();
}
@@ -414,9 +417,10 @@ void Shell::loop_delay() {
function_copy(*this);
// if (running()) {
// display_prompt();
// }
// TODO comment this like in v3.5? display_prompt
if (running()) {
display_prompt();
}
idle_time_ = uuid::get_uptime_ms();
}
@@ -444,9 +448,10 @@ void Shell::loop_blocking() {
stop();
}
// if (running()) {
// display_prompt();
// }
// TODO comment this like in v3.5? display_prompt
if (running()) {
display_prompt();
}
idle_time_ = uuid::get_uptime_ms();
}
@@ -513,14 +518,17 @@ void Shell::maximum_command_line_length(size_t length) {
}
void Shell::process_command() {
// added for EMS-ESP
if (line_buffer_.empty()) {
println();
return;
}
uint8_t no = line_no_ ? line_no_ : MAX_LINES;
while (--no) {
line_old_[no] = line_old_[no - 1];
}
line_no_ = 0;
line_old_[0] = line_buffer_;
while (!line_buffer_.empty()) {
@@ -542,18 +550,21 @@ void Shell::process_command() {
if (!command_line->empty()) {
if (commands_) {
auto execution = commands_->execute_command(*this, std::move(command_line));
if (execution.error != nullptr) {
println(execution.error);
}
} else {
println(F("No commands configured"));
println("No commands configured");
}
}
::yield();
}
// if (running()) {
// display_prompt();
// }
// TODO comment this like in v3.5? display_prompt
if (running()) {
display_prompt();
}
::yield();
}
void Shell::process_completion() {
@@ -561,9 +572,11 @@ void Shell::process_completion() {
if (!command_line->empty() && commands_) {
auto completion = commands_->complete_command(*this, command_line);
bool redisplay = false;
if (!completion.help.empty()) {
println();
redisplay = true;
for (auto & help : completion.help) {
std::string help_line = help.to_string(maximum_command_line_length_);
@@ -573,8 +586,18 @@ void Shell::process_completion() {
}
if (!completion.replacement->empty()) {
if (!redisplay) {
erase_current_line();
prompt_displayed_ = false;
redisplay = true;
}
line_buffer_ = completion.replacement.to_string(maximum_command_line_length_);
}
if (redisplay) {
display_prompt();
}
}
::yield();
@@ -599,9 +622,15 @@ void Shell::process_password(bool completed) {
void Shell::invoke_command(const std::string & line) {
erase_current_line();
prompt_displayed_ = false;
line_buffer_ = line;
display_prompt();
if (!line_buffer_.empty()) {
println();
prompt_displayed_ = false;
}
if (!prompt_displayed_) {
display_prompt();
}
line_buffer_ = line;
print(line_buffer_);
process_command();
}

View File

@@ -22,6 +22,9 @@
#include <algorithm>
#include <memory>
#if UUID_CONSOLE_THREAD_SAFE
#include <mutex>
#endif
#include <string>
#include <uuid/log.h>
@@ -30,8 +33,13 @@ namespace uuid {
namespace console {
static const char __pstr__logger_name[] = "shell";
const uuid::log::Logger Shell::logger_{(__pstr__logger_name), uuid::log::Facility::LPR};
static const char __pstr__logger_name[] = "shell";
const uuid::log::Logger & Shell::logger() {
static const uuid::log::Logger logger_instance{reinterpret_cast<const char *>(__pstr__logger_name), uuid::log::Facility::LPR};
return logger_instance;
}
Shell::QueuedLogMessage::QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> && content)
: id_(id)
@@ -39,6 +47,9 @@ Shell::QueuedLogMessage::QueuedLogMessage(unsigned long id, std::shared_ptr<uuid
}
void Shell::operator<<(std::shared_ptr<uuid::log::Message> message) {
#if UUID_CONSOLE_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
if (log_messages_.size() >= maximum_log_messages_) {
log_messages_.pop_front();
}
@@ -55,10 +66,18 @@ void Shell::log_level(uuid::log::Level level) {
}
size_t Shell::maximum_log_messages() const {
#if UUID_CONSOLE_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
return maximum_log_messages_;
}
void Shell::maximum_log_messages(size_t count) {
#if UUID_CONSOLE_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
maximum_log_messages_ = std::max((size_t)1, count);
while (log_messages_.size() > maximum_log_messages_) {
log_messages_.pop_front();
@@ -66,39 +85,71 @@ void Shell::maximum_log_messages(size_t count) {
}
void Shell::output_logs() {
if (!log_messages_.empty()) {
if (mode_ != Mode::DELAY) {
erase_current_line();
prompt_displayed_ = false;
}
#if UUID_CONSOLE_THREAD_SAFE
std::unique_lock<std::mutex> lock{mutex_};
#endif
while (!log_messages_.empty()) {
auto message = std::move(log_messages_.front());
log_messages_.pop_front();
if (log_messages_.empty())
return;
print(uuid::log::format_timestamp_ms(message.content_->uptime_ms, 3));
printf(F(" %c %lu: [%S] "), uuid::log::format_level_char(message.content_->level), message.id_, message.content_->name);
size_t count = std::max((size_t)1, MAX_LOG_MESSAGES);
auto message = log_messages_.front();
if ((message.content_->level == uuid::log::Level::ERR) || (message.content_->level == uuid::log::Level::WARNING)) {
print(COLOR_RED);
println(message.content_->text);
print(COLOR_RESET);
} else if (message.content_->level == uuid::log::Level::INFO) {
print(COLOR_YELLOW);
println(message.content_->text);
print(COLOR_RESET);
} else if (message.content_->level == uuid::log::Level::DEBUG) {
print(COLOR_CYAN);
println(message.content_->text);
print(COLOR_RESET);
} else {
println(message.content_->text);
}
log_messages_.pop_front();
#if UUID_CONSOLE_THREAD_SAFE
lock.unlock();
#endif
::yield();
}
display_prompt();
if (mode_ != Mode::DELAY) {
erase_current_line();
prompt_displayed_ = false;
}
while (1) {
print(uuid::log::format_timestamp_ms(message.content_->uptime_ms, 3));
printf(" %c %lu: [%s] ", uuid::log::format_level_char(message.content_->level), message.id_, message.content_->name);
if ((message.content_->level == uuid::log::Level::ERR) || (message.content_->level == uuid::log::Level::WARNING)) {
print(COLOR_RED);
println(message.content_->text);
print(COLOR_RESET);
} else if (message.content_->level == uuid::log::Level::INFO) {
print(COLOR_YELLOW);
println(message.content_->text);
print(COLOR_RESET);
} else if (message.content_->level == uuid::log::Level::DEBUG) {
print(COLOR_CYAN);
println(message.content_->text);
print(COLOR_RESET);
} else {
println(message.content_->text);
}
::yield();
count--;
if (count == 0) {
break;
}
#if UUID_CONSOLE_THREAD_SAFE
lock.lock();
#endif
if (log_messages_.empty()) {
#if UUID_CONSOLE_THREAD_SAFE
lock.unlock();
#endif
break;
}
message = log_messages_.front();
log_messages_.pop_front();
#if UUID_CONSOLE_THREAD_SAFE
lock.unlock();
#endif
}
display_prompt();
}
} // namespace console

View File

@@ -1,6 +1,6 @@
/*
* uuid-console - Microcontroller console shell
* Copyright 2019 Simon Arlott
* Copyright 2019,2021 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -25,15 +25,21 @@ namespace uuid {
namespace console {
std::set<std::shared_ptr<Shell>> Shell::shells_;
std::set<std::shared_ptr<Shell>> & Shell::registered_shells() {
static std::set<std::shared_ptr<Shell>> shells;
return shells;
}
void Shell::loop_all() {
for (auto shell = shells_.begin(); shell != shells_.end();) {
auto & shells = registered_shells();
for (auto shell = shells.begin(); shell != shells.end();) {
shell->get()->loop_one();
// This avoids copying the shared_ptr every time loop_one() is called
if (!shell->get()->running()) {
shell = shells_.erase(shell);
shell = shells.erase(shell);
} else {
shell++;
}

View File

@@ -51,16 +51,6 @@ size_t Shell::printf(const char * format, ...) {
return len;
}
size_t Shell::printf(const __FlashStringHelper * format, ...) {
va_list ap;
va_start(ap, format);
size_t len = vprintf(format, ap);
va_end(ap);
return len;
}
size_t Shell::printfln(const char * format, ...) {
va_list ap;
@@ -72,17 +62,6 @@ size_t Shell::printfln(const char * format, ...) {
return len;
}
size_t Shell::printfln(const __FlashStringHelper * format, ...) {
va_list ap;
va_start(ap, format);
size_t len = vprintf(format, ap);
va_end(ap);
len += println();
return len;
}
size_t Shell::vprintf(const char * format, va_list ap) {
size_t print_len = 0;
va_list copy_ap;
@@ -101,64 +80,26 @@ size_t Shell::vprintf(const char * format, va_list ap) {
return print_len;
}
size_t Shell::vprintf(const __FlashStringHelper * format, va_list ap) {
size_t print_len = 0;
va_list copy_ap;
va_copy(copy_ap, ap);
int format_len = ::vsnprintf_P(nullptr, 0, reinterpret_cast<PGM_P>(format), ap);
if (format_len > 0) {
std::string text(static_cast<std::string::size_type>(format_len), '\0');
::vsnprintf_P(&text[0], text.capacity() + 1, reinterpret_cast<PGM_P>(format), copy_ap);
print_len = print(text);
}
va_end(copy_ap);
return print_len;
}
// modified by proddy
void Shell::print_all_available_commands() {
/*
commands_->for_each_available_command(*this, [this](std::vector<std::string> & name, std::vector<std::string> & arguments) {
CommandLine command_line{name, arguments};
// TODO add back sorting of commands?
command_line.escape_initial_parameters(name.size());
name.clear();
arguments.clear();
for (auto & available_command : available_commands()) {
CommandLine command_line{available_command.name(), available_command.arguments()};
command_line.escape_initial_parameters(available_command.name().size());
println(command_line.to_string(maximum_command_line_length()));
});
*/
// changed by proddy - sort the help commands
std::list<std::string> sorted_cmds;
commands_->for_each_available_command(*this, [&](std::vector<std::string> & name, std::vector<std::string> & arguments) {
CommandLine command_line{name, arguments};
command_line.escape_initial_parameters(name.size());
name.clear();
arguments.clear();
sorted_cmds.push_back(command_line.to_string(maximum_command_line_length()));
});
sorted_cmds.sort();
for (auto & cl : sorted_cmds) {
// print(" ");
println(cl);
}
}
void Shell::erase_current_line() {
print(F("\033[0G\033[K"));
print("\033[0G\033[K");
}
void Shell::erase_characters(size_t count) {
print(std::string(count, '\x08'));
print(F("\033[K"));
print("\033[K");
}
} // namespace console
} // namespace uuid
} // namespace uuid

View File

@@ -1,6 +1,6 @@
/*
* uuid-console - Microcontroller console shell
* Copyright 2019 Simon Arlott
* Copyright 2019,2022 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -28,12 +28,12 @@ int Shell::available() {
if (mode_ == Mode::BLOCKING) {
auto * blocking_data = reinterpret_cast<Shell::BlockingData *>(mode_data_.get());
if (!available_char()) {
if (!stream_.available()) {
return 0;
}
if (blocking_data->consume_line_feed_) {
const int input = peek_one_char();
const int input = stream_.peek();
if (input >= 0) {
const unsigned char c = input;
@@ -42,7 +42,7 @@ int Shell::available() {
if (previous_ == '\x0D' && c == '\x0A') {
// Consume the first LF following a CR
read_one_char();
stream_.read();
previous_ = c;
return available();
}
@@ -62,7 +62,7 @@ int Shell::available() {
int Shell::read() {
if (mode_ == Mode::BLOCKING) {
auto * blocking_data = reinterpret_cast<Shell::BlockingData *>(mode_data_.get());
const int input = read_one_char();
const int input = stream_.read();
if (input >= 0) {
const unsigned char c = input;
@@ -90,7 +90,7 @@ int Shell::read() {
int Shell::peek() {
if (mode_ == Mode::BLOCKING) {
auto * blocking_data = reinterpret_cast<Shell::BlockingData *>(mode_data_.get());
const int input = peek_one_char();
const int input = stream_.peek();
if (blocking_data->consume_line_feed_) {
if (input >= 0) {
@@ -100,7 +100,7 @@ int Shell::peek() {
if (previous_ == '\x0D' && c == '\x0A') {
// Consume the first LF following a CR
read_one_char();
stream_.read();
previous_ = c;
return peek();
}
@@ -113,6 +113,15 @@ int Shell::peek() {
}
}
size_t Shell::write(uint8_t data) {
return stream_.write(data);
}
size_t Shell::write(const uint8_t * buffer, size_t size) {
return stream_.write(buffer, size);
}
void Shell::flush() {
// This is a pure virtual function in Arduino's Stream class, which
// makes no sense because that class is for input and this is an
@@ -122,4 +131,4 @@ void Shell::flush() {
} // namespace console
} // namespace uuid
} // namespace uuid

View File

@@ -1,63 +0,0 @@
/*
* uuid-console - Microcontroller console shell
* Copyright 2019 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uuid/console.h>
#include <Arduino.h>
#include <memory>
#include <string>
namespace uuid {
namespace console {
StreamConsole::StreamConsole(Stream & stream)
: Shell()
, stream_(stream) {
}
// cppcheck-suppress passedByValue
StreamConsole::StreamConsole(std::shared_ptr<Commands> commands, Stream & stream, unsigned int context, unsigned int flags)
: Shell(std::move(commands), context, flags)
, stream_(stream) {
}
size_t StreamConsole::write(uint8_t data) {
return stream_.write(data);
}
size_t StreamConsole::write(const uint8_t * buffer, size_t size) {
return stream_.write(buffer, size);
}
bool StreamConsole::available_char() {
return stream_.available() > 0;
}
int StreamConsole::read_one_char() {
return stream_.read();
}
int StreamConsole::peek_one_char() {
return stream_.peek();
}
} // namespace console
} // namespace uuid

File diff suppressed because it is too large Load Diff