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>
202 lines
6.7 KiB
C
202 lines
6.7 KiB
C
#include "stream.h"
|
|
#include "adapter.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
#define CHANNELS 3
|
|
#define LINE_CAPACITY 8192
|
|
static stream channels[CHANNELS];
|
|
static adapter ports[CHANNELS];
|
|
static int parse_number(const char *text, long *out)
|
|
{
|
|
unsigned long value = 0;
|
|
unsigned long limit = 1000000;
|
|
int negative = 0;
|
|
if (*text == '-' || *text == '+') {
|
|
negative = *text == '-';
|
|
text += 1;
|
|
}
|
|
if (*text == '\0') {
|
|
return 0;
|
|
}
|
|
while (*text != '\0') {
|
|
unsigned digit;
|
|
if (*text < '0' || *text > '9') {
|
|
return 0;
|
|
}
|
|
digit = (unsigned)(*text - '0');
|
|
if (value > (limit - digit) / 10) {
|
|
return 0;
|
|
}
|
|
value = value * 10 + digit;
|
|
text += 1;
|
|
}
|
|
*out = negative ? -(long)value : (long)value;
|
|
return 1;
|
|
}
|
|
static int hex_digit(char c)
|
|
{
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
return -1;
|
|
}
|
|
static int parse_bytes(const char *text, unsigned char *bytes, size_t *count)
|
|
{
|
|
size_t length;
|
|
size_t index;
|
|
*count = 0;
|
|
if (strcmp(text, "-") == 0) {
|
|
return 1;
|
|
}
|
|
length = strlen(text);
|
|
if (length % 2 != 0 || length > 2 * (STREAM_CAPACITY + 1)) {
|
|
return 0;
|
|
}
|
|
for (index = 0; index < length; index += 2) {
|
|
int high = hex_digit(text[index]);
|
|
int low = hex_digit(text[index + 1]);
|
|
if (high < 0 || low < 0) {
|
|
return 0;
|
|
}
|
|
bytes[(*count)++] = (unsigned char)(high * 16 + low);
|
|
}
|
|
return 1;
|
|
}
|
|
static void print_hex(const unsigned char *bytes, size_t count)
|
|
{
|
|
size_t index;
|
|
putchar('"');
|
|
for (index = 0; index < count; ++index) {
|
|
printf("%02x", bytes[index]);
|
|
}
|
|
putchar('"');
|
|
}
|
|
static void print_stream(const stream *value)
|
|
{
|
|
unsigned char bytes[STREAM_CAPACITY];
|
|
size_t count;
|
|
printf("{\"state\":[%u,%d,%d,%d,%d,%llu,%llu,%llu],\"tx\":",
|
|
value->generation, value->opened, value->read_end, value->write_end,
|
|
value->last_error, (unsigned long long)value->accepted,
|
|
(unsigned long long)value->received, (unsigned long long)value->delivered);
|
|
count = ring_copy(&value->outgoing, bytes, STREAM_CAPACITY);
|
|
print_hex(bytes, count);
|
|
printf(",\"rx\":");
|
|
count = ring_copy(&value->incoming, bytes, STREAM_CAPACITY);
|
|
print_hex(bytes, count);
|
|
putchar('}');
|
|
}
|
|
static int split(char *line, char **tokens, int capacity)
|
|
{
|
|
char *next = line;
|
|
int count = 0;
|
|
while (*next != '\0') {
|
|
while (*next == ' ' || *next == '\t') next += 1;
|
|
if (*next == '\0') break;
|
|
if (count == capacity) return -1;
|
|
tokens[count++] = next;
|
|
while (*next != '\0' && *next != ' ' && *next != '\t') next += 1;
|
|
if (*next != '\0') *next++ = '\0';
|
|
}
|
|
return count;
|
|
}
|
|
static int dispatch(char **tokens, int count, unsigned char *out, size_t *progress)
|
|
{
|
|
long channel;
|
|
long value;
|
|
int operation;
|
|
stream *selected;
|
|
unsigned char bytes[STREAM_CAPACITY + 1];
|
|
size_t size;
|
|
if (count < 2 || strlen(tokens[0]) != 1 || !parse_number(tokens[1], &channel)) {
|
|
return STREAM_INVALID;
|
|
}
|
|
if (channel < 0 || channel >= CHANNELS) return STREAM_INVALID;
|
|
selected = &channels[channel];
|
|
operation = tokens[0][0];
|
|
if ((operation == 'O' || operation == 'C' || operation == 'F') && count == 2) {
|
|
if (operation == 'O') return stream_open(selected);
|
|
if (operation == 'C') return stream_close(selected);
|
|
return stream_finish(selected);
|
|
}
|
|
if (operation == 'Q' && count == 3) {
|
|
if (!parse_bytes(tokens[2], bytes, &size)) return STREAM_INVALID;
|
|
return stream_queue(selected, bytes, size);
|
|
}
|
|
if ((operation == 'T' || operation == 'R' || operation == 'D' || operation == 'H') && count == 3) {
|
|
if (!parse_number(tokens[2], &value) || value < 0) return STREAM_INVALID;
|
|
if (operation == 'T' || operation == 'R' || operation == 'D') *progress = 777;
|
|
if (operation == 'T') return stream_transmit(selected, (size_t)value, progress);
|
|
if (operation == 'R') return stream_receive(selected, (size_t)value, progress);
|
|
if (operation == 'D') return stream_take(selected, out, (size_t)value, progress);
|
|
if (value != 0 && value != EIO && value != EAGAIN) return STREAM_INVALID;
|
|
ports[channel].shutdown_error = (int)value;
|
|
return STREAM_OK;
|
|
}
|
|
if ((operation == 'S' || operation == 'V') && count >= 2 && count <= ADAPTER_STEPS + 2) {
|
|
int steps[ADAPTER_STEPS];
|
|
int index;
|
|
for (index = 2; index < count; ++index) {
|
|
if (!parse_number(tokens[index], &value) || value < -4 || value > STREAM_CAPACITY) {
|
|
return STREAM_INVALID;
|
|
}
|
|
steps[index - 2] = (int)value;
|
|
}
|
|
return adapter_load(&ports[channel], operation == 'V', steps, (size_t)(count - 2));
|
|
}
|
|
return STREAM_INVALID;
|
|
}
|
|
static int read_line(char *line, int *invalid)
|
|
{
|
|
size_t length = 0;
|
|
int c;
|
|
int seen = 0;
|
|
*invalid = 0;
|
|
while ((c = getchar()) != EOF && c != '\n') {
|
|
seen = 1;
|
|
if (c == 0) *invalid = 1;
|
|
if (length < LINE_CAPACITY) line[length++] = (char)c;
|
|
else *invalid = 1;
|
|
}
|
|
line[length] = '\0';
|
|
return seen || c == '\n';
|
|
}
|
|
int main(void)
|
|
{
|
|
char line[LINE_CAPACITY + 2];
|
|
char *tokens[ADAPTER_STEPS + 4];
|
|
int index;
|
|
int invalid;
|
|
for (index = 0; index < CHANNELS; ++index) {
|
|
adapter_init(&ports[index], (unsigned)(31 + 71 * index));
|
|
memset(&channels[index], 0xa5, sizeof channels[index]);
|
|
stream_init(&channels[index], adapter_port(&ports[index]));
|
|
}
|
|
while (read_line(line, &invalid)) {
|
|
size_t progress = 0;
|
|
unsigned char out[STREAM_CAPACITY];
|
|
int rc;
|
|
int count;
|
|
for (index = 0; index < CHANNELS; ++index) adapter_observation_reset(&ports[index]);
|
|
count = split(line, tokens, ADAPTER_STEPS + 4);
|
|
rc = invalid || count < 0 ? STREAM_INVALID : dispatch(tokens, count, out, &progress);
|
|
printf("{\"rc\":%d,\"n\":%zu,\"out\":", rc, progress);
|
|
if (count > 0 && strcmp(tokens[0], "D") == 0) print_hex(out, progress);
|
|
else print_hex(out, 0);
|
|
printf(",\"channels\":[");
|
|
for (index = 0; index < CHANNELS; ++index) {
|
|
if (index != 0) putchar(',');
|
|
print_stream(&channels[index]);
|
|
}
|
|
printf("],\"ports\":[");
|
|
for (index = 0; index < CHANNELS; ++index) {
|
|
if (index != 0) putchar(',');
|
|
adapter_print(&ports[index]);
|
|
}
|
|
printf("]}\n");
|
|
}
|
|
return ferror(stdin) ? 1 : 0;
|
|
}
|