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

289 lines
8.0 KiB
C

#include "phase.h"
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
typedef struct {
char text[TIMER_LINE_SIZE];
char *words[TIMER_WORDS];
size_t count;
} Request;
static int split_request(const char *line, Request *request)
{
char *cursor;
size_t length = strlen(line);
request->count = 0;
if (length > 255) {
return TIMER_SYNTAX;
}
memcpy(request->text, line, length + 1);
cursor = request->text;
while (*cursor != '\0') {
while (*cursor != '\0' && isspace((unsigned char)*cursor)) {
++cursor;
}
if (*cursor == '\0') {
break;
}
if (request->count == TIMER_WORDS) {
return TIMER_SYNTAX;
}
request->words[request->count++] = cursor;
while (*cursor != '\0' && !isspace((unsigned char)*cursor)) {
++cursor;
}
if (*cursor != '\0') {
*cursor++ = '\0';
}
}
return TIMER_OK;
}
static int number(const char *word, int64_t *value)
{
char *end;
long long parsed;
const char *digit = word;
if (*digit == '-' || *digit == '+') {
++digit;
}
if (*digit == '\0') {
return TIMER_SYNTAX;
}
for (const char *p = digit; *p != '\0'; ++p) {
if (*p < '0' || *p > '9') {
return TIMER_SYNTAX;
}
}
errno = 0;
parsed = strtoll(word, &end, 10);
if (errno == ERANGE) {
return TIMER_RANGE;
}
if (*end != '\0') {
return TIMER_SYNTAX;
}
*value = (int64_t)parsed;
return TIMER_OK;
}
static int read_numbers(const Request *request, size_t start,
size_t count, int64_t *values)
{
for (size_t i = 0; i < count; ++i) {
int code = number(request->words[start + i], &values[i]);
if (code != TIMER_OK) {
return code;
}
}
return TIMER_OK;
}
static void print_state(FILE *output, const TimerChannel *channel)
{
fprintf(output, "STATE %d %" PRId64 " %" PRId64 " %" PRId64
" %s %d %" PRId64 " %" PRId64 " %" PRId64 "\n",
channel->id, channel->period, channel->phase, channel->epoch,
timer_mode_name(channel->mode), channel->enabled,
channel->next, channel->fired, channel->skipped);
}
static int handle_init(TimerBank *bank, const Request *request, FILE *output)
{
int64_t now;
int code;
if (request->count != 2) {
return TIMER_SYNTAX;
}
code = number(request->words[1], &now);
if (code != TIMER_OK) {
return code;
}
if (!timer_coord_valid(now)) {
return TIMER_RANGE;
}
timer_bank_reset(bank, now);
fprintf(output, "OK INIT %" PRId64 "\n", now);
return TIMER_OK;
}
static int handle_put(TimerBank *bank, const Request *request, FILE *output)
{
int64_t args[4];
TimerMode mode;
int code;
if (request->count != 6) {
return TIMER_SYNTAX;
}
code = read_numbers(request, 1, 4, args);
if (code != TIMER_OK) {
return code;
}
if (strcmp(request->words[5], "COUNT") == 0) {
mode = TIMER_COUNT;
} else if (strcmp(request->words[5], "LATEST") == 0) {
mode = TIMER_LATEST;
} else {
return TIMER_SYNTAX;
}
if (!timer_id_valid(args[0])) {
return TIMER_RANGE;
}
code = timer_bank_put(bank, (int)args[0], args[1], args[2], args[3], mode);
if (code == TIMER_OK) {
fprintf(output, "OK PUT %d\n", (int)args[0]);
}
return code;
}
static int handle_single(TimerBank *bank, const Request *request, FILE *output)
{
int64_t id;
TimerChannel channel;
int code;
if (request->count != 2) {
return TIMER_SYNTAX;
}
code = number(request->words[1], &id);
if (code != TIMER_OK) {
return code;
}
if (!timer_id_valid(id)) {
return TIMER_RANGE;
}
if (strcmp(request->words[0], "DROP") == 0) {
code = timer_bank_drop(bank, (int)id);
if (code == TIMER_OK) {
fprintf(output, "OK DROP %d\n", (int)id);
}
} else {
code = timer_bank_peek(bank, (int)id, &channel);
if (code == TIMER_OK) {
print_state(output, &channel);
}
}
return code;
}
static int handle_pair(TimerBank *bank, const Request *request, FILE *output)
{
int64_t args[2];
int code;
if (request->count != 3) {
return TIMER_SYNTAX;
}
code = read_numbers(request, 1, 2, args);
if (code != TIMER_OK) {
return code;
}
if (!timer_id_valid(args[0])) {
return TIMER_RANGE;
}
if (strcmp(request->words[0], "SHIFT") == 0) {
code = timer_bank_shift(bank, (int)args[0], args[1]);
if (code == TIMER_OK) {
fprintf(output, "OK SHIFT %d\n", (int)args[0]);
}
} else {
if (args[1] != 0 && args[1] != 1) {
return TIMER_RANGE;
}
code = timer_bank_enable(bank, (int)args[0], (int)args[1]);
if (code == TIMER_OK) {
fprintf(output, "OK ENABLE %d %d\n", (int)args[0], (int)args[1]);
}
}
return code;
}
static int handle_tick(TimerBank *bank, const Request *request, FILE *output)
{
TimerEvent events[TIMER_CHANNELS];
int64_t target;
size_t count;
int code;
if (request->count != 2) {
return TIMER_SYNTAX;
}
code = number(request->words[1], &target);
if (code != TIMER_OK) {
return code;
}
code = timer_bank_tick(bank, target, events, &count);
if (code != TIMER_OK) {
return code;
}
fprintf(output, "BEGIN %" PRId64 " %zu\n", target, count);
for (size_t i = 0; i < count; ++i) {
const TimerEvent *event = &events[i];
fprintf(output, "EVT %d %" PRId64 " %" PRId64 " %" PRId64
" %" PRId64 " %" PRId64 "\n", event->id, event->due,
event->first, event->last, event->skipped, event->next);
}
fprintf(output, "END %" PRId64 "\n", target);
return TIMER_OK;
}
static int handle_list(const TimerBank *bank, const Request *request, FILE *output)
{
TimerChannel channels[TIMER_CHANNELS];
size_t count;
if (request->count != 1) {
return TIMER_SYNTAX;
}
count = timer_bank_list(bank, channels);
fprintf(output, "LIST %zu\n", count);
for (size_t i = 0; i < count; ++i) {
print_state(output, &channels[i]);
}
return TIMER_OK;
}
static int handle_stats(const TimerBank *bank, const Request *request, FILE *output)
{
TimerStats stats;
if (request->count != 1) {
return TIMER_SYNTAX;
}
stats = timer_bank_stats(bank);
fprintf(output, "STATS %" PRId64 " %zu %zu %" PRId64 " %" PRId64 "\n",
stats.now, stats.configured, stats.enabled, stats.fired, stats.skipped);
return TIMER_OK;
}
int timer_wire(TimerBank *bank, const char *line, FILE *output)
{
Request request;
int code = split_request(line, &request);
if (code == TIMER_OK && request.count == 0) {
return TIMER_OK;
}
if (code == TIMER_OK) {
const char *name = request.words[0];
if (strcmp(name, "INIT") == 0) {
code = handle_init(bank, &request, output);
} else if (strcmp(name, "PUT") == 0) {
code = handle_put(bank, &request, output);
} else if (strcmp(name, "PEEK") == 0 || strcmp(name, "DROP") == 0) {
code = handle_single(bank, &request, output);
} else if (strcmp(name, "ENABLE") == 0 || strcmp(name, "SHIFT") == 0) {
code = handle_pair(bank, &request, output);
} else if (strcmp(name, "TICK") == 0) {
code = handle_tick(bank, &request, output);
} else if (strcmp(name, "LIST") == 0) {
code = handle_list(bank, &request, output);
} else if (strcmp(name, "STATS") == 0) {
code = handle_stats(bank, &request, output);
} else {
code = TIMER_SYNTAX;
}
}
if (code != TIMER_OK) {
fprintf(output, "ERR %s\n", timer_error_name(code));
}
return code;
}