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

276 lines
7.3 KiB
C

#include "stack_domain.h"
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#define LINE_BYTES 16384
#define TOKEN_COUNT 6
struct token {
const unsigned char *data;
size_t length;
};
struct console_state {
struct sd_port current;
struct sd_attr slots[SD_SLOTS];
};
static int separator(unsigned char value)
{
return value == ' ' || value == '\t' || value == '\r';
}
static int tokenize(const unsigned char *line, size_t length,
struct token *tokens, size_t *count)
{
size_t position = 0;
size_t used = 0;
while (position < length) {
size_t start;
while (position < length && separator(line[position])) {
position += 1;
}
if (position == length) {
break;
}
start = position;
while (position < length && !separator(line[position])) {
position += 1;
}
if (used == TOKEN_COUNT) {
return SD_EINVAL;
}
tokens[used].data = line + start;
tokens[used].length = position - start;
used += 1;
}
*count = used;
return SD_OK;
}
enum command_kind {
COMMAND_INVALID,
COMMAND_POLICY,
COMMAND_INIT,
COMMAND_SET,
COMMAND_QUERY,
COMMAND_COPY,
COMMAND_RESET,
COMMAND_DESTROY
};
static enum command_kind identify(const struct token *token)
{
if (token->length != 1) {
return COMMAND_INVALID;
}
switch (token->data[0]) {
case 'P':
return COMMAND_POLICY;
case 'I':
return COMMAND_INIT;
case 'S':
return COMMAND_SET;
case 'Q':
return COMMAND_QUERY;
case 'C':
return COMMAND_COPY;
case 'R':
return COMMAND_RESET;
case 'D':
return COMMAND_DESTROY;
default:
return COMMAND_INVALID;
}
}
static size_t argument_count(enum command_kind command)
{
switch (command) {
case COMMAND_POLICY:
return 5;
case COMMAND_INIT:
return 2;
case COMMAND_SET:
return 3;
case COMMAND_QUERY:
return 2;
case COMMAND_COPY:
return 3;
case COMMAND_RESET:
return 2;
case COMMAND_DESTROY:
return 2;
default:
return 0;
}
}
static int number_at(const struct token *tokens, size_t index, uint64_t *out)
{
return sd_number(tokens[index].data, tokens[index].length, out);
}
static int slot_at(const struct token *tokens, size_t index, size_t *slot)
{
uint64_t parsed;
int rc = number_at(tokens, index, &parsed);
if (rc != SD_OK || parsed >= SD_SLOTS) {
return SD_EINVAL;
}
*slot = (size_t)parsed;
return SD_OK;
}
static void emit(int rc, uint64_t bytes, uint64_t revision, int explicit_size)
{
printf("{\n \"rc\": %d, \"size\": %" PRIu64
",\n \"revision\": %" PRIu64 ", \"explicit\": %d, \"extra\": true\n}\n",
rc, bytes, revision, explicit_size);
}
static void emit_slot(struct console_state *state, size_t slot, int rc)
{
uint64_t bytes = 0;
uint64_t revision = 0;
int explicit_size = 0;
sd_attr_get(&state->slots[slot], &bytes, &revision, &explicit_size);
emit(rc, bytes, revision, explicit_size);
}
static void configure(struct console_state *state, const struct token *tokens,
size_t count)
{
uint64_t minimum;
uint64_t quantum;
uint64_t ceiling;
uint64_t default_bytes = 0;
uint64_t bytes = 0;
int has_default = 1;
int rc;
if (count != 5 || number_at(tokens, 1, &minimum) != SD_OK ||
number_at(tokens, 2, &quantum) != SD_OK ||
number_at(tokens, 3, &ceiling) != SD_OK) {
emit(SD_EINVAL, 0, 0, 0);
return;
}
if (tokens[4].length == 1 && tokens[4].data[0] == '-') {
has_default = 0;
} else if (number_at(tokens, 4, &default_bytes) != SD_OK) {
emit(SD_EINVAL, 0, 0, 0);
return;
}
rc = sd_port_make(&state->current, minimum, quantum, ceiling,
has_default, default_bytes);
sd_port_default(&state->current, &bytes);
emit(rc, bytes, 0, 0);
}
static void dispatch(struct console_state *state, const unsigned char *line,
size_t length)
{
struct token tokens[TOKEN_COUNT];
size_t count = 0;
size_t slot;
size_t source;
enum command_kind command;
uint64_t bytes;
int rc;
rc = tokenize(line, length, tokens, &count);
if (rc != SD_OK || count == 0) {
emit(SD_EINVAL, 0, 0, 0);
return;
}
command = identify(&tokens[0]);
if (command == COMMAND_INVALID || count != argument_count(command)) {
emit(SD_EINVAL, 0, 0, 0);
return;
}
if (command == COMMAND_POLICY) {
configure(state, tokens, count);
return;
}
if (count < 2 || slot_at(tokens, 1, &slot) != SD_OK) {
emit(SD_EINVAL, 0, 0, 0);
return;
}
if (command == COMMAND_INIT && count == 2) {
rc = sd_attr_init(&state->slots[slot], &state->current);
} else if (command == COMMAND_SET && count == 3 &&
number_at(tokens, 2, &bytes) == SD_OK) {
rc = sd_attr_set(&state->slots[slot], bytes);
} else if (command == COMMAND_QUERY && count == 2) {
uint64_t revision;
int explicit_size;
rc = sd_attr_get(&state->slots[slot], &bytes, &revision, &explicit_size);
} else if (command == COMMAND_COPY && count == 3 &&
slot_at(tokens, 2, &source) == SD_OK) {
rc = sd_attr_copy(&state->slots[slot], &state->slots[source]);
} else if (command == COMMAND_RESET && count == 2) {
rc = sd_attr_reset(&state->slots[slot]);
} else if (command == COMMAND_DESTROY && count == 2) {
rc = sd_attr_destroy(&state->slots[slot]);
} else {
emit(SD_EINVAL, 0, 0, 0);
return;
}
emit_slot(state, slot, rc);
}
static int start_state(struct console_state *state)
{
int rc;
size_t slot;
memset(&state->current, 0, sizeof(state->current));
for (slot = 0; slot < SD_SLOTS; ++slot) {
memset(&state->slots[slot], 0, sizeof(state->slots[slot]));
}
rc = sd_port_make(&state->current, 16384, 4096, 1048576, 0, 0);
if (rc != SD_OK) {
return 1;
}
return 0;
}
static int finish_output(void)
{
int failed = 0;
if (ferror(stdin)) {
failed = 1;
}
if (fflush(stdout) == EOF) {
failed = 1;
}
if (ferror(stdout)) {
failed = 1;
}
return failed;
}
int sd_console(void)
{
struct console_state state;
unsigned char line[LINE_BYTES];
size_t length = 0;
int overlong = 0;
int input;
if (start_state(&state) != 0) {
return 1;
}
while ((input = fgetc(stdin)) != EOF) {
if (input == '\n') {
if (overlong) {
emit(SD_EINVAL, 0, 0, 0);
} else {
dispatch(&state, line, length);
}
length = 0;
overlong = 0;
} else if (length < LINE_BYTES) {
line[length] = (unsigned char)input;
length += 1;
} else {
overlong = 1;
}
}
if (length != 0 || overlong) {
if (overlong) {
emit(SD_EINVAL, 0, 0, 0);
} else {
dispatch(&state, line, length);
}
}
return finish_output();
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
return sd_console();
}