#include "runtime.hpp" #include #include #include #include #include namespace cancelrt { namespace { bool separator(char character) { return character == ' ' || character == '\t' || character == '\r'; } std::vector tokens(const std::string& line) { std::vector result; std::size_t at = 0; while (at < line.size()) { while (at < line.size() && separator(line[at])) { ++at; } std::size_t start = at; while (at < line.size() && !separator(line[at])) { ++at; } if (at != start) { result.push_back(line.substr(start, at - start)); } } return result; } bool number(const std::string& text, std::int64_t& value) { if (text.empty()) { return false; } std::size_t at = 0; bool negative = false; if (text[at] == '+' || text[at] == '-') { negative = text[at] == '-'; ++at; } if (at == text.size()) { return false; } std::uint64_t magnitude = 0; const auto maximum = static_cast(std::numeric_limits::max()); const auto limit = maximum + (negative ? 1U : 0U); while (at < text.size()) { const auto character = text[at]; if (character < '0' || character > '9') { return false; } const auto digit = static_cast(character - '0'); if (magnitude > (limit - digit) / 10U) { return false; } magnitude = magnitude * 10U + digit; ++at; } if (negative && magnitude == maximum + 1U) { value = std::numeric_limits::min(); } else if (negative) { value = -static_cast(magnitude); } else { value = static_cast(magnitude); } return true; } int arity(const std::string& verb) { if (verb == "create") { return 3; } if (verb == "configure") { return 3; } if (verb == "write") { return 4; } if (verb == "read") { return 4; } if (verb == "inspect") { return 1; } if (verb == "run") { return 3; } if (verb == "rewind") { return 1; } if (verb == "erase") { return 1; } if (verb == "list") { return 0; } return -1; } Reply dispatch(Runtime& runtime, const std::string& verb, const std::vector& args) { if (verb == "create") { return runtime.create(args[0], args[1], args[2]); } if (verb == "configure") { return runtime.configure(args[0], args[1], args[2]); } if (verb == "write") { return runtime.write(args[0], args[1], args[2], args[3]); } if (verb == "read") { return runtime.read(args[0], args[1], args[2], args[3]); } if (verb == "inspect") { return runtime.inspect(args[0]); } if (verb == "run") { return runtime.run(args[0], args[1], args[2]); } if (verb == "rewind") { return runtime.rewind(args[0]); } if (verb == "erase") { return runtime.erase(args[0]); } return runtime.list(); } void decimal(std::ostream& out, std::int64_t value) { char buffer[21]; std::size_t position = sizeof(buffer); bool negative = value < 0; std::uint64_t magnitude; if (negative) { magnitude = static_cast(-(value + 1)); ++magnitude; } else { magnitude = static_cast(value); } do { unsigned digit = static_cast(magnitude % 10U); --position; buffer[position] = static_cast('0' + digit); magnitude /= 10U; } while (magnitude != 0); if (negative) { --position; buffer[position] = '-'; } auto length = static_cast(sizeof(buffer) - position); out.write(buffer + position, length); } void field(std::ostream& out, std::int64_t value) { out.put(' '); decimal(out, value); } bool parse_numbers(const std::vector& words, int count, std::vector& result) { std::vector parsed; parsed.reserve(static_cast(count)); for (int index = 0; index < count; ++index) { std::int64_t value = 0; const auto& token = words[static_cast(index + 1)]; if (!number(token, value)) { return false; } parsed.push_back(value); } result.swap(parsed); return true; } struct Command { std::string verb; std::vector arguments; bool valid; Command() : valid(false) { } }; Command parse_line(const std::string& line) { Command command; auto words = tokens(line); if (words.empty()) { return command; } int count = arity(words[0]); if (count < 0) { return command; } auto expected = static_cast(count + 1); if (words.size() != expected) { return command; } if (!parse_numbers(words, count, command.arguments)) { return command; } command.verb = words[0]; command.valid = true; return command; } } void print_reply(std::ostream& out, const Reply& reply) { decimal(out, static_cast(reply.code)); field(out, static_cast(reply.stop)); field(out, reply.total); field(out, reply.done); field(out, reply.produced); field(out, reply.factor); field(out, reply.bias); field(out, reply.attempts); field(out, reply.releases); field(out, reply.active); field(out, reply.sum); field(out, static_cast(reply.values.size())); for (auto value : reply.values) { field(out, value); } field(out, static_cast(reply.ids.size())); for (auto id : reply.ids) { field(out, id); } out.put('\n'); } int run_cli(std::istream& in, std::ostream& out, std::ostream& err) { (void)err; Runtime runtime; std::string line; int lines = 0; while (std::getline(in, line)) { if (lines == 4096) { out.write("LIMIT\n", 6); return 0; } ++lines; Command command = parse_line(line); if (!command.valid) { out.write("SYNTAX\n", 7); continue; } print_reply(out, dispatch(runtime, command.verb, command.arguments)); } return 0; } }