185 lines
3.4 KiB
C++
185 lines
3.4 KiB
C++
#include "echoion/protocol/Command.hpp"
|
|
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
|
|
namespace Echoion::Protocol
|
|
{
|
|
|
|
std::string tsEscape(const std::string& str)
|
|
{
|
|
std::string out;
|
|
out.reserve(str.size());
|
|
for (char c : str) {
|
|
switch (c) {
|
|
case '\\':
|
|
out += "\\\\";
|
|
break;
|
|
case '/':
|
|
out += "\\/";
|
|
break;
|
|
case ' ':
|
|
out += "\\s";
|
|
break;
|
|
case '|':
|
|
out += "\\p";
|
|
break;
|
|
case '\f':
|
|
out += "\\f";
|
|
break;
|
|
case '\n':
|
|
out += "\\n";
|
|
break;
|
|
case '\r':
|
|
out += "\\r";
|
|
break;
|
|
case '\t':
|
|
out += "\\t";
|
|
break;
|
|
case '\v':
|
|
out += "\\v";
|
|
break;
|
|
default:
|
|
out.push_back(c);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string tsUnescape(const std::string& str)
|
|
{
|
|
std::string out;
|
|
out.reserve(str.size());
|
|
for (size_t i = 0; i < str.size(); ++i) {
|
|
if (str[i] == '\\') {
|
|
if (++i >= str.size()) {
|
|
throw std::runtime_error("invalid escape sequence");
|
|
}
|
|
switch (str[i]) {
|
|
case '\\':
|
|
out.push_back('\\');
|
|
break;
|
|
case '/':
|
|
out.push_back('/');
|
|
break;
|
|
case 's':
|
|
out.push_back(' ');
|
|
break;
|
|
case 'p':
|
|
out.push_back('|');
|
|
break;
|
|
case 'f':
|
|
out.push_back('\f');
|
|
break;
|
|
case 'n':
|
|
out.push_back('\n');
|
|
break;
|
|
case 'r':
|
|
out.push_back('\r');
|
|
break;
|
|
case 't':
|
|
out.push_back('\t');
|
|
break;
|
|
case 'v':
|
|
out.push_back('\v');
|
|
break;
|
|
default:
|
|
throw std::runtime_error(std::string("unknown escape: \\") +
|
|
str[i]);
|
|
}
|
|
} else {
|
|
out.push_back(str[i]);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string buildCommand(const std::string& name,
|
|
const CommandParams& params)
|
|
{
|
|
std::string cmd = tsEscape(name);
|
|
for (const auto& [key, value] : params) {
|
|
cmd += ' ';
|
|
cmd += key;
|
|
cmd += '=';
|
|
cmd += tsEscape(value);
|
|
}
|
|
return cmd;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
CommandParams parseGroup(const std::string& part)
|
|
{
|
|
CommandParams params;
|
|
std::istringstream iss(part);
|
|
std::string token;
|
|
while (std::getline(iss, token, ' ')) {
|
|
if (token.empty()) {
|
|
continue;
|
|
}
|
|
auto eq = token.find('=');
|
|
if (eq != std::string::npos) {
|
|
params[token.substr(0, eq)] =
|
|
tsUnescape(token.substr(eq + 1));
|
|
} else {
|
|
params[token] = "";
|
|
}
|
|
}
|
|
return params;
|
|
}
|
|
|
|
std::string trim(const std::string& s)
|
|
{
|
|
size_t a = s.find_first_not_of(" \t\r\n");
|
|
if (a == std::string::npos) {
|
|
return "";
|
|
}
|
|
size_t b = s.find_last_not_of(" \t\r\n");
|
|
return s.substr(a, b - a + 1);
|
|
}
|
|
} // namespace
|
|
|
|
ParsedCommand parseCommand(const std::string& raw)
|
|
{
|
|
// Split on '|' into groups.
|
|
std::vector<std::string> parts;
|
|
{
|
|
std::string cur;
|
|
for (char c : raw) {
|
|
if (c == '|') {
|
|
parts.push_back(cur);
|
|
cur.clear();
|
|
} else {
|
|
cur.push_back(c);
|
|
}
|
|
}
|
|
parts.push_back(cur);
|
|
}
|
|
|
|
ParsedCommand out;
|
|
std::string first = trim(parts[0]);
|
|
|
|
// The command name is the first whitespace-delimited token.
|
|
std::string body;
|
|
auto sp = first.find(' ');
|
|
if (sp == std::string::npos) {
|
|
out.name = tsUnescape(first);
|
|
body = "";
|
|
} else {
|
|
out.name = tsUnescape(first.substr(0, sp));
|
|
body = first.substr(sp + 1);
|
|
}
|
|
out.params = parseGroup(body);
|
|
|
|
if (parts.size() > 1) {
|
|
out.groups.push_back(out.params);
|
|
for (size_t g = 1; g < parts.size(); ++g) {
|
|
out.groups.push_back(parseGroup(trim(parts[g])));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace Echoion::Protocol
|