amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
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>
2026-10-27 16:14:57 +08:00

202 lines
5.2 KiB
C

#include "vector.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
struct console {
struct vt_job job;
struct vt_book book;
struct vt_device device;
unsigned char payload[VT_SEGMENTS][VT_SEGMENT_BYTES];
struct vt_span spans[VT_SEGMENTS];
FILE *output;
};
static int numeric_word(const char *word, int allow_negative)
{
if (word == NULL) {
return 0;
}
if (*word == '+' || (allow_negative && *word == '-')) {
word++;
}
if (*word == '\0') {
return 0;
}
while (*word != '\0') {
if (*word < '0' || *word > '9') {
return 0;
}
word++;
}
return 1;
}
static int parse_size(const char *word, size_t *result)
{
char *end;
unsigned long value;
if (!numeric_word(word, 0)) {
return 0;
}
errno = 0;
value = strtoul(word, &end, 10);
if (errno || *end != '\0') {
return 0;
}
*result = (size_t)value;
return (unsigned long)*result == value;
}
static int parse_action(const char *word, long *result)
{
char *end;
if (!numeric_word(word, 1)) {
return 0;
}
errno = 0;
*result = strtol(word, &end, 10);
return !errno && *end == '\0';
}
static int digit(char value)
{
if (value >= '0' && value <= '9') {
return value - '0';
}
if (value >= 'a' && value <= 'f') {
return value - 'a' + 10;
}
if (value >= 'A' && value <= 'F') {
return value - 'A' + 10;
}
return -1;
}
static int decode(const char *word, unsigned char *data, size_t *length)
{
size_t index;
size_t count = strlen(word);
if (strcmp(word, "-") == 0) {
*length = 0;
return 1;
}
if (count % 2 || count / 2 > VT_SEGMENT_BYTES) {
return 0;
}
for (index = 0; index < count / 2; index++) {
int high = digit(word[index * 2]);
int low = digit(word[index * 2 + 1]);
if (high < 0 || low < 0) {
return 0;
}
data[index] = (unsigned char)(high * 16 + low);
}
*length = count / 2;
return 1;
}
static void reset(struct console *console)
{
memset(&console->job, 0, sizeof(console->job));
vt_book_reset(&console->book);
vt_device_reset(&console->device, console->output);
}
static int open_job(struct console *console, char **words, size_t count)
{
size_t index;
size_t wanted;
unsigned char fresh[VT_SEGMENTS][VT_SEGMENT_BYTES];
size_t lengths[VT_SEGMENTS];
if (count < 2 || !parse_size(words[1], &wanted) || wanted > VT_SEGMENTS ||
count != wanted + 2) {
return -EINVAL;
}
for (index = 0; index < wanted; index++) {
if (!decode(words[index + 2], fresh[index], &lengths[index])) {
return -EINVAL;
}
}
for (index = 0; index < wanted; index++) {
memcpy(console->payload[index], fresh[index], lengths[index]);
console->spans[index].data = console->payload[index];
console->spans[index].length = lengths[index];
}
return vt_job_open(&console->job, console->spans, wanted);
}
static int set_script(struct console *console, char **words, size_t count)
{
long values[VT_SCRIPT];
size_t wanted;
size_t index;
if (count < 2 || !parse_size(words[1], &wanted) || wanted > VT_SCRIPT ||
count != wanted + 2) {
return -EINVAL;
}
for (index = 0; index < wanted; index++) {
if (!parse_action(words[index + 2], &values[index])) {
return -EINVAL;
}
}
return vt_device_script(&console->device, values, wanted);
}
static long execute(struct console *console, char **words, size_t count)
{
if (count == 1 && strcmp(words[0], "R") == 0) {
reset(console);
return 0;
}
if (count != 0 && strcmp(words[0], "O") == 0) {
return open_job(console, words, count);
}
if (count != 0 && strcmp(words[0], "P") == 0) {
return set_script(console, words, count);
}
if (count == 3 && strcmp(words[0], "S") == 0) {
size_t quota;
size_t width;
if (!parse_size(words[1], &quota) || !parse_size(words[2], &width)) {
return -EINVAL;
}
return vt_submit(&console->job, &console->book, quota, width,
vt_device_send, &console->device);
}
if (count == 1 && strcmp(words[0], "Q") == 0) {
return 0;
}
return -EINVAL;
}
int vt_console(FILE *input, FILE *output)
{
struct console console;
char line[8193];
size_t step = 0;
memset(&console, 0, sizeof(console));
console.output = output;
reset(&console);
while (fgets(line, sizeof(line), input) != NULL) {
char *words[VT_SCRIPT + 4];
size_t count = 0;
char *word = strtok(line, " \t\r\n");
long result;
while (word != NULL && count < VT_SCRIPT + 4) {
words[count++] = word;
word = strtok(NULL, " \t\r\n");
}
step++;
if (word != NULL) {
result = -EINVAL;
} else {
result = execute(&console, words, count);
}
fprintf(output, "RESULT %zu %ld\n", step, result);
vt_print_state(output, &console.job, &console.book, &console.device);
}
return ferror(input) || ferror(output) ? 2 : 0;
}