Replaced commandOptions member with commandArguments in the GDB Monitor command base class

Updated the `eeprom fill` monitor command to take the fill value from the third command argument.
Also updated help text
This commit is contained in:
Nav
2024-08-24 20:11:30 +01:00
parent 34ca7d4289
commit 34b26c3d06
5 changed files with 56 additions and 94 deletions

View File

@@ -25,7 +25,7 @@ namespace DebugServer::Gdb::CommandPackets
this->command = std::string{decodedCommand.begin(), decodedCommand.end()};
this->command.erase(this->command.find_last_not_of(" ") + 1);
this->commandOptions = this->extractCommandOptions(this->command);
this->commandArguments = Monitor::extractCommandArguments(this->command);
}
void Monitor::handle(
@@ -38,61 +38,39 @@ namespace DebugServer::Gdb::CommandPackets
debugSession.connection.writePacket(EmptyResponsePacket{});
}
std::map<std::string, std::optional<std::string>> Monitor::extractCommandOptions(const std::string& command) {
auto output = std::map<std::string, std::optional<std::string>>{};
std::vector<std::string> Monitor::extractCommandArguments(const std::string& command) {
auto output = std::vector<std::string>{};
for (std::string::size_type cmdIndex = 1; cmdIndex < command.size(); ++cmdIndex) {
const auto cmdChar = command.at(cmdIndex);
auto quoteEnabled = false;
auto argument = std::string{};
if (cmdChar == '-') {
if (command.at(cmdIndex - 1) != '-') {
continue;
const auto commit = [&output, &argument] () {
output.emplace_back(std::move(argument));
argument.clear();
};
for (auto i = std::string::size_type{0}; i < command.size(); ++i) {
const auto cmdChar = command.at(i);
if (cmdChar == '"') {
if (quoteEnabled) {
commit();
}
auto option = std::string{};
auto optionValue = std::optional<std::string>{};
bool quoted = false;
auto optIndex = std::string::size_type{0};
for (optIndex = cmdIndex + 1; optIndex < command.size(); ++optIndex) {
const auto optChar = command.at(optIndex);
if (!option.empty() && ((!quoted && optChar == ' ') || (quoted && optChar == '"'))) {
output.emplace(option, optionValue);
option.clear();
optionValue.reset();
quoted = false;
cmdIndex = optIndex;
break;
}
if (optionValue.has_value()) {
if (optChar == '"' && !quoted && optionValue->empty()) {
quoted = true;
continue;
}
optionValue->push_back(optChar);
continue;
}
if (optChar == '=') {
optionValue = std::string{};
continue;
}
option.push_back(optChar);
continue;
}
if (!option.empty()) {
output.emplace(option, optionValue);
cmdIndex = optIndex;
}
quoteEnabled = !quoteEnabled;
continue;
}
if (!quoteEnabled && cmdChar == ' ') {
commit();
continue;
}
argument.push_back(cmdChar);
}
if (!argument.empty()) {
commit();
}
return output;

View File

@@ -2,8 +2,7 @@
#include <cstdint>
#include <string>
#include <map>
#include <optional>
#include <vector>
#include "CommandPacket.hpp"
@@ -20,14 +19,7 @@ namespace DebugServer::Gdb::CommandPackets
*/
std::string command;
/**
* A mapping of any command options included in this->command. A command option must begin with "--" and
* can optionally have a value.
*
* The key of this map is the option name. The map value is the option value, or std::nullopt if no value was
* provided.
*/
std::map<std::string, std::optional<std::string>> commandOptions;
std::vector<std::string> commandArguments;
explicit Monitor(const RawPacket& rawPacket);
@@ -39,12 +31,6 @@ namespace DebugServer::Gdb::CommandPackets
) override;
private:
/**
* Extracts command options from a command string.
*
* @param command
* @return
*/
std::map<std::string, std::optional<std::string>> extractCommandOptions(const std::string& command);
static std::vector<std::string> extractCommandArguments(const std::string& command);
};
}