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>
136 lines
3.6 KiB
C
136 lines
3.6 KiB
C
#include "internal.h"
|
|
#include <limits.h>
|
|
#include <string.h>
|
|
#define LINE_LIMIT 4095
|
|
#define TOKEN_LIMIT 5
|
|
typedef struct {
|
|
char *text[TOKEN_LIMIT];
|
|
unsigned count;
|
|
} Tokens;
|
|
static int read_line(char *buffer, size_t capacity)
|
|
{
|
|
size_t length = 0;
|
|
int overflow = 0;
|
|
int ch;
|
|
while ((ch = fgetc(stdin)) != EOF) {
|
|
if (ch == '\n') {
|
|
break;
|
|
}
|
|
if (length + 1 < capacity) {
|
|
buffer[length] = (char)ch;
|
|
length += 1;
|
|
} else {
|
|
overflow = 1;
|
|
}
|
|
if (ch == 0) {
|
|
overflow = 1;
|
|
}
|
|
}
|
|
buffer[length] = 0;
|
|
if (ch == EOF && length == 0 && !overflow) {
|
|
return 0;
|
|
}
|
|
return overflow ? -1 : 1;
|
|
}
|
|
static int split_line(char *line, Tokens *tokens)
|
|
{
|
|
char *cursor = line;
|
|
tokens->count = 0;
|
|
while (*cursor != 0) {
|
|
while (*cursor == ' ' || *cursor == '\t') {
|
|
cursor += 1;
|
|
}
|
|
if (*cursor == 0) {
|
|
break;
|
|
}
|
|
if (tokens->count == TOKEN_LIMIT) {
|
|
return 0;
|
|
}
|
|
tokens->text[tokens->count] = cursor;
|
|
tokens->count += 1;
|
|
while (*cursor != 0 && *cursor != ' ' && *cursor != '\t') {
|
|
unsigned char ch = (unsigned char)*cursor;
|
|
if (ch < 33 || ch > 126) {
|
|
return 0;
|
|
}
|
|
cursor += 1;
|
|
}
|
|
if (*cursor != 0) {
|
|
*cursor = 0;
|
|
cursor += 1;
|
|
}
|
|
}
|
|
return tokens->count != 0;
|
|
}
|
|
static int number_parse(const char *text, unsigned *value)
|
|
{
|
|
unsigned result = 0;
|
|
const unsigned char *cursor = (const unsigned char *)text;
|
|
if (*cursor == 0) {
|
|
return 0;
|
|
}
|
|
while (*cursor != 0) {
|
|
unsigned digit;
|
|
if (*cursor < '0' || *cursor > '9') {
|
|
return 0;
|
|
}
|
|
digit = (unsigned)(*cursor - '0');
|
|
if (result > (UINT_MAX - digit) / 10u) {
|
|
return 0;
|
|
}
|
|
result = result * 10u + digit;
|
|
cursor += 1;
|
|
}
|
|
*value = result;
|
|
return 1;
|
|
}
|
|
static int arguments_parse(const Tokens *tokens, unsigned *numbers)
|
|
{
|
|
unsigned index;
|
|
for (index = 1; index < tokens->count; ++index) {
|
|
if (!number_parse(tokens->text[index], &numbers[index - 1])) {
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
static int command_run(Worker *worker, Tokens *tokens, WorkerEvent *event)
|
|
{
|
|
unsigned values[3] = {0, 0, 0};
|
|
const char *command = tokens->text[0];
|
|
*event = WORKER_NONE;
|
|
if (strcmp(command, "VIEW") == 0) {
|
|
return tokens->count == 1 ? WORKER_OK : WORKER_INVALID;
|
|
}
|
|
if (tokens->count > 4 || !arguments_parse(tokens, values)) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (strcmp(command, "NEW") == 0 && tokens->count == 4) {
|
|
return worker_init(worker, values[0], values[1], values[2]);
|
|
}
|
|
if (strcmp(command, "POST") == 0 && tokens->count == 2) {
|
|
return worker_post(worker, values[0]);
|
|
}
|
|
if (strcmp(command, "STEP") == 0 && tokens->count == 4) {
|
|
return worker_step(worker, values[0], values[1], values[2], event);
|
|
}
|
|
return WORKER_INVALID;
|
|
}
|
|
int main(void)
|
|
{
|
|
Worker worker;
|
|
char line[LINE_LIMIT + 1];
|
|
int state;
|
|
memset(&worker, 0, sizeof(worker));
|
|
while ((state = read_line(line, sizeof(line))) != 0) {
|
|
Tokens tokens;
|
|
WorkerEvent event = WORKER_NONE;
|
|
int rc = WORKER_INVALID;
|
|
if (state > 0 && split_line(line, &tokens)) {
|
|
rc = command_run(&worker, &tokens, &event);
|
|
}
|
|
report_write(stdout, rc, event, &worker);
|
|
}
|
|
return ferror(stdin) || ferror(stdout) ? 1 : 0;
|
|
}
|