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>
217 lines
7.6 KiB
C
217 lines
7.6 KiB
C
#include "vector.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct packet {
|
|
struct vt_span spans[13];
|
|
unsigned char data[13][258];
|
|
struct packet *next;
|
|
};
|
|
|
|
struct recorder {
|
|
long values[65];
|
|
size_t count;
|
|
size_t position;
|
|
size_t calls;
|
|
unsigned char received[65536];
|
|
size_t length;
|
|
};
|
|
|
|
static struct recorder *active_recorder;
|
|
|
|
static void hex(const unsigned char *data, size_t length)
|
|
{
|
|
size_t i;
|
|
if (!length) putchar('-');
|
|
for (i = 0; i < length; i++) printf("%02x", data[i]);
|
|
}
|
|
|
|
static ssize_t send_raw(void *context, const struct vt_span *spans, size_t count)
|
|
{
|
|
struct recorder *r = context == NULL ? active_recorder : context;
|
|
size_t i, offered = 0, left;
|
|
long action;
|
|
r->calls++;
|
|
printf("CALL %zu", count);
|
|
for (i = 0; i < count; i++) {
|
|
offered += spans[i].length;
|
|
printf(" %zu:", spans[i].length);
|
|
hex(spans[i].data, spans[i].length);
|
|
}
|
|
action = r->position < r->count ? r->values[r->position++] : (long)offered;
|
|
printf(" => %ld\n", action);
|
|
left = action > 0 && (size_t)action <= offered ? (size_t)action : 0;
|
|
for (i = 0; i < count && left; i++) {
|
|
size_t n = spans[i].length < left ? spans[i].length : left;
|
|
if (n > sizeof(r->received) - r->length) exit(73);
|
|
memcpy(r->received + r->length, spans[i].data, n);
|
|
r->length += n;
|
|
left -= n;
|
|
}
|
|
return action;
|
|
}
|
|
|
|
static void state(const struct vt_job *j, const struct vt_book *b, const struct recorder *r)
|
|
{
|
|
printf("STATE %d %zu %zu %zu %zu %zu %llu %d %llu %llu %llu %llu %llu %zu %zu %zu ",
|
|
j->ready, j->count, j->cursor.index, j->cursor.offset, j->cursor.done,
|
|
j->cursor.total, (unsigned long long)j->calls, j->last_error,
|
|
(unsigned long long)b->bytes, (unsigned long long)b->requests,
|
|
(unsigned long long)b->completed, (unsigned long long)b->failures,
|
|
(unsigned long long)b->pauses, r->calls, r->position, r->length);
|
|
hex(r->received, r->length);
|
|
putchar('\n');
|
|
}
|
|
|
|
static void device_observation(long result, const struct vt_device *device)
|
|
{
|
|
printf("DEVICE %ld %llu %zu %zu ", result, (unsigned long long)device->calls,
|
|
device->script_position, device->length);
|
|
hex(device->bytes, device->length);
|
|
putchar('\n');
|
|
}
|
|
|
|
static void device_operations(void)
|
|
{
|
|
struct vt_device d;
|
|
unsigned char data[] = {0, 255, 5};
|
|
struct vt_span parts[] = {{data, 2}, {data + 2, 1}};
|
|
long values[65] = {1, -5};
|
|
long result;
|
|
vt_device_reset(&d, NULL);
|
|
device_observation(0, &d);
|
|
result = vt_device_script(&d, values, 2);
|
|
device_observation(result, &d);
|
|
result = vt_device_send(&d, parts, 2);
|
|
device_observation(result, &d);
|
|
result = vt_device_script(&d, NULL, 1);
|
|
device_observation(result, &d);
|
|
result = vt_device_script(&d, values, 65);
|
|
device_observation(result, &d);
|
|
values[0] = -1;
|
|
result = vt_device_script(&d, values, 1);
|
|
device_observation(result, &d);
|
|
result = vt_device_send(&d, parts, 2);
|
|
device_observation(result, &d);
|
|
result = vt_device_script(&d, NULL, 0);
|
|
device_observation(result, &d);
|
|
result = vt_device_send(&d, parts, 2);
|
|
device_observation(result, &d);
|
|
fputs("HEX ", stdout);
|
|
vt_print_hex(stdout, NULL, 0);
|
|
fputs(" HEX ", stdout);
|
|
vt_print_hex(stdout, data, sizeof(data));
|
|
putchar('\n');
|
|
vt_device_reset(&d, NULL);
|
|
device_observation(0, &d);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
struct vt_job job;
|
|
struct vt_book book;
|
|
struct recorder raw;
|
|
struct packet *packets = NULL;
|
|
char command;
|
|
size_t step = 0;
|
|
memset(&job, 0, sizeof(job));
|
|
memset(&book, 0, sizeof(book));
|
|
memset(&raw, 0, sizeof(raw));
|
|
active_recorder = &raw;
|
|
while (scanf(" %c", &command) == 1) {
|
|
long result = 0;
|
|
step++;
|
|
if (command == 'R') {
|
|
memset(&job, 0, sizeof(job));
|
|
vt_book_reset(&book);
|
|
memset(&raw, 0, sizeof(raw));
|
|
vt_book_reset(NULL);
|
|
} else if (command == 'O') {
|
|
size_t n, i, k;
|
|
int null_array, null_job;
|
|
struct packet *p = calloc(1, sizeof(*p));
|
|
if (p == NULL) return 74;
|
|
p->next = packets;
|
|
packets = p;
|
|
if (scanf("%zu%d%d", &n, &null_array, &null_job) != 3 || n > 13) return 75;
|
|
for (i = 0; i < n; i++) {
|
|
size_t length;
|
|
int null_base;
|
|
if (scanf("%zu%d", &length, &null_base) != 2 || length > 257) return 76;
|
|
for (k = 0; k < length; k++) {
|
|
unsigned int value;
|
|
if (scanf("%u", &value) != 1) return 77;
|
|
p->data[i][k] = (unsigned char)value;
|
|
}
|
|
p->spans[i].length = length;
|
|
p->spans[i].data = null_base ? NULL : p->data[i];
|
|
}
|
|
result = vt_job_open(null_job ? NULL : &job, null_array ? NULL : p->spans, n);
|
|
for (i = 0; i < n; i++) {
|
|
p->spans[i].length = 0;
|
|
p->spans[i].data = NULL;
|
|
}
|
|
} else if (command == 'P') {
|
|
size_t i, n;
|
|
if (scanf("%zu", &n) != 1 || n > 65) return 78;
|
|
for (i = 0; i < n; i++) if (scanf("%ld", &raw.values[i]) != 1) return 79;
|
|
raw.count = n;
|
|
raw.position = 0;
|
|
} else if (command == 'S') {
|
|
size_t quota, width;
|
|
int flags;
|
|
if (scanf("%zu%zu%d", "a, &width, &flags) != 3) return 80;
|
|
result = vt_submit(flags & 1 ? NULL : &job, flags & 2 ? NULL : &book,
|
|
quota, width, flags & 4 ? NULL : send_raw, flags & 8 ? NULL : &raw);
|
|
} else if (command == 'B') {
|
|
vt_book_reset(&book);
|
|
} else if (command == 'F') {
|
|
size_t accepted;
|
|
int error, paused, completed;
|
|
if (scanf("%zu%d%d%d", &accepted, &error, &paused, &completed) != 4) return 81;
|
|
result = vt_book_finish(&book, accepted, error, paused, completed);
|
|
} else if (command == 'W') {
|
|
struct vt_span window[4];
|
|
size_t budget, width, count, i;
|
|
if (scanf("%zu%zu", &budget, &width) != 2) return 82;
|
|
count = vt_cursor_window(&job, budget, width, window);
|
|
printf("WINDOW %zu", count);
|
|
for (i = 0; i < count; i++) {
|
|
printf(" %zu:", window[i].length);
|
|
hex(window[i].data, window[i].length);
|
|
}
|
|
putchar('\n');
|
|
} else if (command == 'A') {
|
|
size_t amount;
|
|
if (scanf("%zu", &amount) != 1) return 83;
|
|
vt_cursor_advance(&job, amount);
|
|
} else if (command == 'C') {
|
|
vt_cursor_reset(&job.cursor, job.spans, job.count);
|
|
} else if (command == 'D') {
|
|
device_operations();
|
|
} else if (command == 'I') {
|
|
FILE *input = fopen("/dev/null", "w");
|
|
FILE *output = fopen("/dev/full", "w");
|
|
FILE *query = tmpfile();
|
|
if (!input || !output || !query) return 84;
|
|
printf("IOSTATUS %d\n", vt_console(input, stdout));
|
|
fputs("Q\n", query);
|
|
rewind(query);
|
|
setvbuf(output, NULL, _IONBF, 0);
|
|
printf("IOSTATUS %d\n", vt_console(query, output));
|
|
fclose(input);
|
|
fclose(output);
|
|
fclose(query);
|
|
}
|
|
printf("RESULT %zu %ld\n", step, result);
|
|
state(&job, &book, &raw);
|
|
}
|
|
while (packets != NULL) {
|
|
struct packet *next = packets->next;
|
|
free(packets);
|
|
packets = next;
|
|
}
|
|
return 0;
|
|
}
|