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>
140 lines
4.3 KiB
C
140 lines
4.3 KiB
C
#include "descriptor.h"
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static void status_row(long long result, int error)
|
|
{
|
|
printf("{\"rc\":%lld,\"err\":%d}\n", result, result < 0 ? error : 0);
|
|
}
|
|
|
|
static void read_row(dr_bank *bank, int slot, int requested)
|
|
{
|
|
unsigned char data[DR_LIMIT];
|
|
char hex[DR_LIMIT * 2 + 1];
|
|
size_t size = requested < 0 ? (size_t)-1 : (size_t)requested;
|
|
ssize_t result = dr_take(bank, slot, data, size);
|
|
int error = result < 0 ? errno : 0;
|
|
hex[0] = '\0';
|
|
if (result >= 0 && result <= DR_LIMIT) {
|
|
dr_hex_encode(data, (size_t)result, hex, sizeof hex);
|
|
}
|
|
printf("{\"rc\":%lld,\"err\":%d,\"data\":\"%s\"}\n",
|
|
(long long)result, error, hex);
|
|
}
|
|
|
|
static void view_row(dr_bank *bank, int slot)
|
|
{
|
|
dr_view view;
|
|
int result = dr_inspect(bank, slot, &view);
|
|
int error = result < 0 ? errno : 0;
|
|
if (result < 0) {
|
|
status_row(result, error);
|
|
return;
|
|
}
|
|
printf("{\"rc\":0,\"err\":0,\"cursor\":%lld,\"size\":%lld,"
|
|
"\"cloexec\":%d,\"read_bytes\":%lu,\"written_bytes\":%lu,"
|
|
"\"name\":\"%s\"}\n", (long long)view.cursor,
|
|
(long long)view.size, view.cloexec, view.read_bytes,
|
|
view.written_bytes, view.name);
|
|
}
|
|
|
|
static int split(char *line, char **words, int capacity)
|
|
{
|
|
int count = 0;
|
|
char *cursor = line;
|
|
while (*cursor != '\0') {
|
|
while (*cursor == ' ' || *cursor == '\t' ||
|
|
*cursor == '\r' || *cursor == '\n') {
|
|
++cursor;
|
|
}
|
|
if (*cursor == '\0') {
|
|
break;
|
|
}
|
|
if (count == capacity) {
|
|
return -1;
|
|
}
|
|
words[count++] = cursor;
|
|
while (*cursor != '\0' && *cursor != ' ' && *cursor != '\t' &&
|
|
*cursor != '\r' && *cursor != '\n') {
|
|
++cursor;
|
|
}
|
|
if (*cursor != '\0') {
|
|
*cursor++ = '\0';
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
static int number(char **words, int index, int *value)
|
|
{
|
|
return dr_parse_i32(words[index], value) == 0;
|
|
}
|
|
|
|
static void dispatch(dr_bank *bank, char **words, int count)
|
|
{
|
|
int slot = 0;
|
|
int other = 0;
|
|
long long result = -1;
|
|
errno = EINVAL;
|
|
if (count == 1 && strcmp(words[0], "reset") == 0) {
|
|
result = dr_reset(bank);
|
|
} else if (count == 4 && strcmp(words[0], "seed") == 0 &&
|
|
number(words, 1, &slot)) {
|
|
unsigned char bytes[DR_LIMIT];
|
|
size_t length;
|
|
if (dr_hex_decode(words[2], bytes, sizeof bytes, &length) == 0) {
|
|
result = dr_seed(bank, slot, bytes, length, words[3]);
|
|
}
|
|
} else if (count == 3 && strcmp(words[0], "bind") == 0 &&
|
|
number(words, 1, &slot) && number(words, 2, &other)) {
|
|
result = dr_bind(bank, slot, other);
|
|
} else if (count == 3 && strcmp(words[0], "read") == 0 &&
|
|
number(words, 1, &slot) && number(words, 2, &other)) {
|
|
read_row(bank, slot, other);
|
|
return;
|
|
} else if (count == 3 && strcmp(words[0], "write") == 0 &&
|
|
number(words, 1, &slot)) {
|
|
unsigned char bytes[DR_LIMIT];
|
|
size_t length;
|
|
if (dr_hex_decode(words[2], bytes, sizeof bytes, &length) == 0) {
|
|
result = dr_emit(bank, slot, bytes, length);
|
|
}
|
|
} else if (count == 3 && strcmp(words[0], "seek") == 0 &&
|
|
number(words, 1, &slot) && number(words, 2, &other)) {
|
|
result = dr_seek(bank, slot, other);
|
|
} else if (count == 3 && strcmp(words[0], "flags") == 0 &&
|
|
number(words, 1, &slot) && number(words, 2, &other)) {
|
|
result = dr_flags(bank, slot, other);
|
|
} else if (count == 2 && strcmp(words[0], "info") == 0 &&
|
|
number(words, 1, &slot)) {
|
|
view_row(bank, slot);
|
|
return;
|
|
} else if (count == 2 && strcmp(words[0], "drop") == 0 &&
|
|
number(words, 1, &slot)) {
|
|
result = dr_drop(bank, slot);
|
|
}
|
|
int error = errno;
|
|
status_row(result, error);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
dr_bank bank;
|
|
dr_init(&bank);
|
|
char *line = NULL;
|
|
size_t capacity = 0;
|
|
while (getline(&line, &capacity, stdin) >= 0) {
|
|
char *words[5];
|
|
int count = split(line, words, 5);
|
|
if (count != 0) {
|
|
dispatch(&bank, words, count);
|
|
}
|
|
}
|
|
free(line);
|
|
int failed = ferror(stdin);
|
|
dr_reset(&bank);
|
|
return failed ? 1 : 0;
|
|
}
|