From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
8.3 KiB
C++
234 lines
8.3 KiB
C++
#include "input.hpp"
|
|
#include <climits>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
using panel::Capability;
|
|
using panel::Service;
|
|
static bool number(const std::string& word, int& value)
|
|
{
|
|
if (word.empty()) {
|
|
return false;
|
|
}
|
|
std::size_t offset = 0;
|
|
bool negative = false;
|
|
if (word[offset] == '+' || word[offset] == '-') {
|
|
negative = word[offset] == '-';
|
|
offset += 1;
|
|
}
|
|
if (offset == word.size()) {
|
|
return false;
|
|
}
|
|
unsigned long long total = 0;
|
|
unsigned long long limit = negative ? 2147483648ULL : 2147483647ULL;
|
|
for (; offset < word.size(); ++offset) {
|
|
unsigned char ch = word[offset];
|
|
if (ch < '0' || ch > '9') {
|
|
return false;
|
|
}
|
|
unsigned digit = ch - '0';
|
|
if (total > (limit - digit) / 10) {
|
|
return false;
|
|
}
|
|
total = total * 10 + digit;
|
|
}
|
|
long long signed_value = negative ? -static_cast<long long>(total) : static_cast<long long>(total);
|
|
value = static_cast<int>(signed_value);
|
|
return true;
|
|
}
|
|
static std::vector<std::string> words(const std::string& line)
|
|
{
|
|
std::vector<std::string> output;
|
|
std::size_t pos = 0;
|
|
while (pos < line.size()) {
|
|
if (line[pos] == ' ' || line[pos] == '\t') {
|
|
++pos;
|
|
continue;
|
|
}
|
|
std::size_t start = pos;
|
|
while (pos < line.size() && line[pos] != ' ' && line[pos] != '\t') {
|
|
++pos;
|
|
}
|
|
output.push_back(line.substr(start, pos - start));
|
|
}
|
|
return output;
|
|
}
|
|
static void string_json(const std::string& text)
|
|
{
|
|
std::cout << '"';
|
|
for (unsigned char ch : text) {
|
|
if (ch == '"' || ch == '\\') {
|
|
std::cout << '\\' << ch;
|
|
} else if (ch < 32 || ch >= 127) {
|
|
static const char* hex = "0123456789abcdef";
|
|
std::cout << "\\u00" << hex[ch >> 4] << hex[ch & 15];
|
|
} else {
|
|
std::cout << ch;
|
|
}
|
|
}
|
|
std::cout << '"';
|
|
}
|
|
static void capabilities(const std::vector<Capability>& caps)
|
|
{
|
|
std::cout << '[';
|
|
bool comma = false;
|
|
for (const auto& cap : caps) {
|
|
if (comma) std::cout << ',';
|
|
comma = true;
|
|
std::cout << '[' << cap.type << ',' << cap.code << ',' << cap.lower << ',' << cap.upper << ']';
|
|
}
|
|
std::cout << ']';
|
|
}
|
|
static void events(const std::vector<panel::Event>& list)
|
|
{
|
|
std::cout << '[';
|
|
bool comma = false;
|
|
for (const auto& event : list) {
|
|
if (comma) std::cout << ',';
|
|
comma = true;
|
|
std::cout << '[' << event.type << ',' << event.code << ',' << event.value << ']';
|
|
}
|
|
std::cout << ']';
|
|
}
|
|
static void observe(const Service& service, int rc, bool reading, int value,
|
|
bool norm, const std::vector<Capability>& normalized,
|
|
const std::vector<int>& help)
|
|
{
|
|
std::cout << "{\"rc\":" << rc << ",\"read\":";
|
|
if (reading) std::cout << value; else std::cout << "null";
|
|
std::cout << ",\"normalized\":";
|
|
if (norm) capabilities(normalized); else std::cout << "null";
|
|
std::cout << ",\"help\":";
|
|
if (help.empty()) {
|
|
std::cout << "null";
|
|
} else {
|
|
std::cout << '[';
|
|
for (std::size_t i = 0; i < help.size(); ++i) {
|
|
if (i) std::cout << ',';
|
|
std::cout << help[i];
|
|
}
|
|
std::cout << ']';
|
|
}
|
|
std::cout << ",\"slots\":[";
|
|
for (std::size_t i = 0; i < service.slots().size(); ++i) {
|
|
if (i) std::cout << ',';
|
|
const auto& slot = service.slots()[i];
|
|
std::cout << '[' << static_cast<int>(slot.phase) << ',' << slot.generation << ',';
|
|
string_json(slot.name);
|
|
std::cout << ',';
|
|
capabilities(slot.capabilities);
|
|
std::cout << ",[";
|
|
for (std::size_t j = 0; j < slot.values.size(); ++j) {
|
|
if (j) std::cout << ',';
|
|
std::cout << slot.values[j];
|
|
}
|
|
std::cout << "],";
|
|
events(slot.pending);
|
|
std::cout << ']';
|
|
}
|
|
const auto& c = service.counters();
|
|
std::cout << "],\"totals\":[" << c.allocations << ',' << c.releases << ','
|
|
<< c.configurations << ',' << c.registrations << ',' << c.unregistrations
|
|
<< ',' << c.reports << ',' << c.frames << ',' << c.committed_events;
|
|
std::cout << "],\"ledger\":[";
|
|
bool comma = false;
|
|
for (const auto& frame : service.ledger()) {
|
|
if (comma) std::cout << ',';
|
|
comma = true;
|
|
std::cout << '[' << frame.serial << ',' << frame.slot << ',' << frame.generation << ',';
|
|
events(frame.events);
|
|
std::cout << ']';
|
|
}
|
|
std::cout << "]}\n";
|
|
}
|
|
static bool parse_caps(const std::vector<int>& args, std::size_t begin,
|
|
std::vector<Capability>& caps)
|
|
{
|
|
if (args.size() <= begin || args[begin] < 0 || args[begin] > 40) {
|
|
return false;
|
|
}
|
|
std::size_t count = static_cast<std::size_t>(args[begin]);
|
|
if (args.size() != begin + 1 + count * 4) {
|
|
return false;
|
|
}
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
std::size_t at = begin + 1 + i * 4;
|
|
caps.push_back({args[at], args[at + 1], args[at + 2], args[at + 3]});
|
|
}
|
|
return true;
|
|
}
|
|
int main()
|
|
{
|
|
Service service;
|
|
std::string line;
|
|
while (std::getline(std::cin, line)) {
|
|
auto tokens = words(line);
|
|
int rc = -9;
|
|
bool reading = false;
|
|
bool norm = false;
|
|
int value = 0;
|
|
std::vector<Capability> normalized;
|
|
std::vector<int> help;
|
|
std::vector<int> args;
|
|
bool parsed = line.size() <= 65536 && !tokens.empty();
|
|
std::string command = tokens.empty() ? "" : tokens[0];
|
|
if (parsed && command == "A") {
|
|
int slot = 0;
|
|
if (tokens.size() == 3 && number(tokens[1], slot)) {
|
|
rc = service.allocate(slot, tokens[2]);
|
|
}
|
|
} else if (parsed && command == "H") {
|
|
int slot = 0;
|
|
if (tokens.size() == 7 && number(tokens[1], slot)) {
|
|
parsed = true;
|
|
for (std::size_t i = 3; i < tokens.size(); ++i) {
|
|
int item;
|
|
if (!number(tokens[i], item)) parsed = false;
|
|
else args.push_back(item);
|
|
}
|
|
if (parsed) {
|
|
Capability cap{args[0], args[1], args[2], args[3]};
|
|
help = {panel::valid_slot(slot), panel::valid_name(tokens[2]),
|
|
panel::capability_valid(cap), service.inspect(slot) != nullptr,
|
|
service.capability(slot, cap.type, cap.code) != nullptr};
|
|
rc = 0;
|
|
}
|
|
}
|
|
} else if (parsed) {
|
|
for (std::size_t i = 1; i < tokens.size(); ++i) {
|
|
int item;
|
|
if (!number(tokens[i], item)) parsed = false;
|
|
else args.push_back(item);
|
|
}
|
|
if (parsed) {
|
|
std::vector<Capability> caps;
|
|
if (command == "Z" && args.empty()) {
|
|
service.reset(); rc = 0;
|
|
} else if (command == "C" && args.size() >= 2 && parse_caps(args, 1, caps)) {
|
|
rc = service.replace_capabilities(args[0], caps);
|
|
} else if (command == "N" && parse_caps(args, 0, caps)) {
|
|
normalized.push_back({1,31,0,1});
|
|
norm = true;
|
|
rc = panel::normalize_capabilities(caps, normalized);
|
|
} else if (command == "B" && args.size() == 2) {
|
|
rc = service.attach(args[0], args[1]);
|
|
} else if (command == "D" && args.size() == 1) {
|
|
rc = service.detach(args[0]);
|
|
} else if (command == "X" && args.size() == 1) {
|
|
rc = service.release(args[0]);
|
|
} else if (command == "E" && args.size() == 4) {
|
|
rc = service.report(args[0], args[1], args[2], args[3]);
|
|
} else if (command == "S" && args.size() == 1) {
|
|
rc = service.sync(args[0]);
|
|
} else if (command == "R" && args.size() == 3) {
|
|
auto result = service.read(args[0], args[1], args[2]);
|
|
rc = result.rc; value = result.value; reading = true;
|
|
}
|
|
}
|
|
}
|
|
observe(service, rc, reading, value, norm, normalized, help);
|
|
}
|
|
return 0;
|
|
}
|