amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

130 lines
3.1 KiB
C

#include "input.h"
#include <string.h>
#include <stddef.h>
static int horizontal(char value)
{
return value == ' ' || value == '\t';
}
static size_t words(char *line, char **tokens, size_t capacity)
{
size_t count = 0;
char *cursor = line;
while (*cursor != '\0') {
while (horizontal(*cursor)) {
++cursor;
}
if (*cursor == '\0') {
break;
}
if (count == capacity) {
return capacity + 1;
}
tokens[count++] = cursor;
while (*cursor != '\0' && !horizontal(*cursor)) {
++cursor;
}
if (*cursor != '\0') {
*cursor++ = '\0';
}
}
return count;
}
static int decimal(const char *text, uint64_t *value)
{
uint64_t result = 0;
if (*text == '+') {
++text;
}
if (*text == '\0') {
return 0;
}
while (*text != '\0') {
if (*text < '0' || *text > '9') {
return 0;
}
unsigned digit = (unsigned)(*text++ - '0');
if (result > (UINT64_MAX - digit) / 10) {
return 0;
}
result = result * 10 + digit;
}
*value = result;
return 1;
}
const char *command_name(int opcode)
{
const char *names[] = {"INVALID", "RESET", "RESERVE", "COMMIT", "CANCEL", "PEEK", "STATS", "LIST", "NULL"};
if (opcode < 1 || opcode > 8) {
return names[0];
}
return names[opcode];
}
static int parse(char *line, Command *out)
{
char *tokens[5];
size_t count = words(line, tokens, 5);
if (count < 2 || count > 4) {
return -1;
}
int opcode = 0;
for (int i = 1; i <= 8; ++i) {
if (strcmp(tokens[0], command_name(i)) == 0) {
opcode = i;
break;
}
}
size_t expected = 3;
if (opcode == 1 || opcode == 2) {
expected = 4;
}
if (opcode == 6) {
expected = 2;
}
if (opcode == 0 || count != expected) {
return -1;
}
uint64_t numbers[3] = {0};
for (size_t i = 1; i < count; ++i) {
if (!decimal(tokens[i], &numbers[i - 1])) {
return -1;
}
}
if (numbers[0] > 3 || (opcode == 7 && numbers[1] > 8) || (opcode == 8 && numbers[1] > 9)) {
return -1;
}
out->opcode = opcode;
out->book = (unsigned)numbers[0];
out->a = numbers[1];
out->b = numbers[2];
return 1;
}
int command_read(FILE *stream, Command *out, unsigned *line_number)
{
char line[4097];
while (fgets(line, sizeof(line), stream) != NULL) {
++*line_number;
size_t length = strlen(line);
if (length > 4095 || (length > 0 && line[length - 1] != '\n' && !feof(stream))) {
return -1;
}
while (length > 0 && (line[length - 1] == '\n' || line[length - 1] == '\r')) {
line[--length] = '\0';
}
char *cursor = line;
while (horizontal(*cursor)) {
++cursor;
}
if (*cursor == '\0' || *cursor == '#') {
continue;
}
return parse(cursor, out);
}
return ferror(stream) ? -1 : 0;
}