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

156 lines
4.0 KiB
C

#include "console.h"
#include <ctype.h>
#include <string.h>
LineStatus console_line(FILE *stream, char *buffer, size_t capacity)
{
size_t used = 0;
int over = 0;
int ch;
while ((ch = fgetc(stream)) != EOF && ch != '\n') {
if (used + 1 < capacity) {
buffer[used++] = (char)ch;
} else {
over = 1;
}
}
if (ferror(stream)) {
return LINE_IO;
}
if (ch == EOF && used == 0 && !over) {
return LINE_END;
}
if (used && buffer[used - 1] == '\r') {
--used;
}
buffer[used] = '\0';
return over ? LINE_LONG : LINE_TEXT;
}
int console_ignored(const char *line)
{
while (*line && isspace((unsigned char)*line)) {
++line;
}
return *line == '\0' || *line == '#';
}
static size_t tokenize(char *line, char **tokens, size_t capacity)
{
size_t count = 0;
char *cursor = line;
while (*cursor) {
while (*cursor && isspace((unsigned char)*cursor)) {
++cursor;
}
if (!*cursor) {
break;
}
if (count == capacity) {
return capacity + 1;
}
tokens[count++] = cursor;
while (*cursor && !isspace((unsigned char)*cursor)) {
++cursor;
}
if (*cursor) {
*cursor++ = '\0';
}
}
return count;
}
static int decimal(const char *token, uint64_t *result)
{
uint64_t value = 0;
if (!*token) {
return 0;
}
for (const unsigned char *p = (const unsigned char *)token; *p; ++p) {
if (*p < '0' || *p > '9') {
return 0;
}
unsigned digit = *p - '0';
if (value > (UINT64_MAX - digit) / 10u) {
return 0;
}
value = value * 10u + digit;
}
*result = value;
return 1;
}
static int bounded(const char *token, uint64_t maximum, uint64_t *result)
{
return decimal(token, result) && *result <= maximum;
}
static int parse_words(char **tokens, size_t start, Command *command)
{
for (unsigned i = 0; i < command->count; ++i) {
uint64_t value;
if (!bounded(tokens[start + i], UINT16_MAX, &value)) {
return 0;
}
command->words[i] = (uint16_t)value;
}
return 1;
}
Command console_parse(char *line)
{
Command command = {0};
char *tokens[CW_TOKEN_LIMIT];
size_t count = tokenize(line, tokens, CW_TOKEN_LIMIT);
if (count == 0 || count > CW_TOKEN_LIMIT || strlen(tokens[0]) != 1) {
return command;
}
char kind = tokens[0][0];
if (kind == 'R') {
if (count == 1) {
command.kind = CMD_RESET;
}
return command;
}
if (count < 2) {
return command;
}
uint64_t value;
if (!bounded(tokens[1], CW_SLOTS - 1, &value)) {
return command;
}
command.slot = (unsigned)value;
if (kind == 'D' && count == 3) {
if (decimal(tokens[2], &command.expected)) {
command.kind = CMD_DROP;
}
} else if (kind == 'E' && count == 3) {
if (bounded(tokens[2], UINT32_MAX, &value)) {
command.input = (uint32_t)value;
command.kind = CMD_EXECUTE;
}
} else if (kind == 'I' && count >= 4) {
if (bounded(tokens[2], CW_CAPACITY, &value) && value > 0) {
command.count = (unsigned)value;
if (count == 3 + value && parse_words(tokens, 3, &command)) {
command.kind = CMD_INSTALL;
}
}
} else if (kind == 'P' && count >= 6) {
if (!decimal(tokens[2], &command.expected)) {
return command;
}
if (!bounded(tokens[3], CW_CAPACITY - 1, &value)) {
return command;
}
command.first = (unsigned)value;
if (bounded(tokens[4], CW_CAPACITY, &value) && value > 0) {
command.count = (unsigned)value;
if (count == 5 + value && parse_words(tokens, 5, &command)) {
command.kind = CMD_PATCH;
}
}
}
return command;
}