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>
118 lines
3.4 KiB
C
118 lines
3.4 KiB
C
#include "parse.h"
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <inttypes.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static const char *skip_space(const char *text)
|
|
{
|
|
while (*text != '\0' && isspace((unsigned char)*text)) {
|
|
text++;
|
|
}
|
|
return text;
|
|
}
|
|
|
|
static void invalid_command(Command *command)
|
|
{
|
|
command->operation = '?';
|
|
command->arity = 0;
|
|
command->arguments[0] = 0;
|
|
command->arguments[1] = 0;
|
|
}
|
|
|
|
int command_parse(const char *text, Command *command)
|
|
{
|
|
const char *cursor = skip_space(text);
|
|
char *end;
|
|
int count = 0;
|
|
invalid_command(command);
|
|
if (*cursor == '\0' || !isupper((unsigned char)*cursor)) {
|
|
return 0;
|
|
}
|
|
command->operation = *cursor++;
|
|
if (*cursor != '\0' && !isspace((unsigned char)*cursor)) {
|
|
invalid_command(command);
|
|
return 0;
|
|
}
|
|
cursor = skip_space(cursor);
|
|
while (*cursor != '\0') {
|
|
long value;
|
|
if (count == 2) {
|
|
invalid_command(command);
|
|
return 0;
|
|
}
|
|
if (*cursor != '+' && *cursor != '-' && !isdigit((unsigned char)*cursor)) {
|
|
invalid_command(command);
|
|
return 0;
|
|
}
|
|
errno = 0;
|
|
value = strtol(cursor, &end, 10);
|
|
if (end == cursor || errno == ERANGE) {
|
|
invalid_command(command);
|
|
return 0;
|
|
}
|
|
if (*end != '\0' && !isspace((unsigned char)*end)) {
|
|
invalid_command(command);
|
|
return 0;
|
|
}
|
|
command->arguments[count] = value;
|
|
count++;
|
|
cursor = skip_space(end);
|
|
}
|
|
command->arity = count;
|
|
return 1;
|
|
}
|
|
|
|
static void event_print(FILE *stream, const Event *event)
|
|
{
|
|
fprintf(stream, "[\"%" PRIu64 "\",%d,%d]", event->serial,
|
|
event->source, event->value);
|
|
}
|
|
|
|
static void events_print(FILE *stream, const Event *events, unsigned count)
|
|
{
|
|
unsigned index;
|
|
fputc('[', stream);
|
|
for (index = 0; index < count; index++) {
|
|
if (index != 0) {
|
|
fputc(',', stream);
|
|
}
|
|
event_print(stream, &events[index]);
|
|
}
|
|
fputc(']', stream);
|
|
}
|
|
|
|
static void masks_print(FILE *stream, const Snapshot *snapshot)
|
|
{
|
|
unsigned index;
|
|
fputc('[', stream);
|
|
for (index = 0; index < snapshot->frame_count; index++) {
|
|
if (index != 0) {
|
|
fputc(',', stream);
|
|
}
|
|
fprintf(stream, "%u", snapshot->saved_masks[index]);
|
|
}
|
|
fputc(']', stream);
|
|
}
|
|
|
|
void reply_print(FILE *stream, unsigned step, const Reply *reply,
|
|
const Snapshot *snapshot)
|
|
{
|
|
fprintf(stream, "{\"step\":%u,\"rc\":%d,\"read\":", step, reply->status);
|
|
events_print(stream, reply->events, reply->count);
|
|
fprintf(stream, ",\"capacity\":%u,\"mask\":%u,\"depth\":%u",
|
|
snapshot->capacity, snapshot->mask, snapshot->depth);
|
|
fprintf(stream, ",\"queued\":%u,\"pending\":%u,\"next\":\"%" PRIu64 "\"",
|
|
snapshot->queued, snapshot->pending, snapshot->next_serial);
|
|
fprintf(stream, ",\"delivered\":\"%" PRIu64 "\",\"dropped\":\"%" PRIu64 "\"",
|
|
snapshot->delivered, snapshot->dropped);
|
|
fprintf(stream, ",\"consumed\":\"%" PRIu64 "\",\"ring\":", snapshot->consumed);
|
|
events_print(stream, snapshot->visible, snapshot->visible_count);
|
|
fputs(",\"waiting\":", stream);
|
|
events_print(stream, snapshot->waiting, snapshot->waiting_count);
|
|
fputs(",\"saved\":", stream);
|
|
masks_print(stream, snapshot);
|
|
fputs("}\n", stream);
|
|
}
|