All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
3.3 KiB
C++
134 lines
3.3 KiB
C++
#include "sysctl_overlay.hpp"
|
|
#include <utility>
|
|
|
|
namespace sysctloverlay {
|
|
namespace detail {
|
|
|
|
bool text_structure(const std::string &text)
|
|
{
|
|
if (text.size() > 65536) {
|
|
return false;
|
|
}
|
|
for (unsigned char byte : text) {
|
|
const bool whitespace = byte == '\t' || byte == '\n' || byte == '\r' ||
|
|
byte == '\v' || byte == '\f';
|
|
if (!whitespace && (byte < 32 || byte > 126)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|
|
namespace {
|
|
|
|
bool edge_space(unsigned char byte)
|
|
{
|
|
return byte == ' ' || byte == '\t' || byte == '\r' ||
|
|
byte == '\v' || byte == '\f';
|
|
}
|
|
|
|
std::string trim(const std::string &text)
|
|
{
|
|
std::size_t begin = 0;
|
|
std::size_t end = text.size();
|
|
while (begin < end && edge_space(text[begin])) {
|
|
++begin;
|
|
}
|
|
while (end > begin && edge_space(text[end - 1])) {
|
|
--end;
|
|
}
|
|
return text.substr(begin, end - begin);
|
|
}
|
|
|
|
bool value_text(const std::string &value)
|
|
{
|
|
if (value.empty() || value.size() > 4096) {
|
|
return false;
|
|
}
|
|
for (unsigned char byte : value) {
|
|
if (byte != '\t' && (byte < 32 || byte > 126)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool parse_line(std::string line, std::size_t row, Rule &rule, bool &present)
|
|
{
|
|
present = false;
|
|
line = trim(line.substr(0, line.find('#')));
|
|
if (line.empty()) {
|
|
return true;
|
|
}
|
|
const bool leading_minus = line.front() == '-';
|
|
if (leading_minus) {
|
|
line.erase(0, 1);
|
|
}
|
|
const auto equal = line.find('=');
|
|
const bool assignment = equal != std::string::npos;
|
|
if (!assignment && !leading_minus) {
|
|
return false;
|
|
}
|
|
const auto key = trim(line.substr(0, equal));
|
|
if (canonical_key(key, true, rule.key) != Error::none) {
|
|
return false;
|
|
}
|
|
rule.row = row;
|
|
rule.pattern = rule.key.find_first_of("*?") != std::string::npos;
|
|
if (assignment) {
|
|
rule.kind = RuleKind::assignment;
|
|
rule.value = trim(line.substr(equal + 1));
|
|
rule.ignore_error = leading_minus;
|
|
if (!value_text(rule.value)) {
|
|
return false;
|
|
}
|
|
} else {
|
|
if (rule.pattern) {
|
|
return false;
|
|
}
|
|
rule.kind = RuleKind::exclusion;
|
|
rule.value.clear();
|
|
rule.ignore_error = false;
|
|
}
|
|
present = true;
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
Error parse_text(const std::string &text, std::vector<Rule> &output)
|
|
{
|
|
output.clear();
|
|
if (!detail::text_structure(text)) {
|
|
return Error::invalid;
|
|
}
|
|
std::vector<Rule> result;
|
|
std::size_t begin = 0;
|
|
std::size_t row = 0;
|
|
for (;;) {
|
|
const auto newline = text.find('\n', begin);
|
|
const auto end = newline == std::string::npos ? text.size() : newline;
|
|
Rule rule;
|
|
bool present = false;
|
|
if (!parse_line(text.substr(begin, end - begin), row, rule, present)) {
|
|
return Error::invalid;
|
|
}
|
|
if (present) {
|
|
if (result.size() == 512) {
|
|
return Error::invalid;
|
|
}
|
|
result.push_back(std::move(rule));
|
|
}
|
|
if (newline == std::string::npos) {
|
|
break;
|
|
}
|
|
begin = newline + 1;
|
|
++row;
|
|
}
|
|
output = std::move(result);
|
|
return Error::none;
|
|
}
|
|
|
|
}
|