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>
273 lines
9.3 KiB
C
273 lines
9.3 KiB
C
#include "console.h"
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
static char *skip_spaces(char *cursor)
|
|
{
|
|
while (*cursor && isspace((unsigned char)*cursor))
|
|
++cursor;
|
|
return cursor;
|
|
}
|
|
static int parse_integer(char **cursor, int *value)
|
|
{
|
|
char *end = 0;
|
|
errno = 0;
|
|
long parsed = strtol(*cursor, &end, 10);
|
|
if (end == *cursor || errno != 0)
|
|
return -1;
|
|
if (parsed < -32768 || parsed > 32767)
|
|
return -1;
|
|
if (*end && !isspace((unsigned char)*end) && *end != '#')
|
|
return -1;
|
|
*value = (int)parsed;
|
|
*cursor = end;
|
|
return 0;
|
|
}
|
|
int console_parse(char *line, struct command *command)
|
|
{
|
|
char *cursor = skip_spaces(line);
|
|
memset(command, 0, sizeof(*command));
|
|
if (*cursor == 0 || *cursor == '#')
|
|
return 0;
|
|
command->name = *cursor++;
|
|
if (*cursor && !isspace((unsigned char)*cursor) && *cursor != '#')
|
|
return -1;
|
|
for (;;) {
|
|
cursor = skip_spaces(cursor);
|
|
if (*cursor == 0 || *cursor == '#')
|
|
return 1;
|
|
if (command->count == INPUT_VALUES)
|
|
return -1;
|
|
if (parse_integer(&cursor, &command->values[command->count]) != 0)
|
|
return -1;
|
|
++command->count;
|
|
}
|
|
}
|
|
void console_init(struct console *console)
|
|
{
|
|
lb_session_init(&console->session);
|
|
sim_init(&console->simulator);
|
|
console->bus = sim_bus(&console->simulator);
|
|
console->sequence = 0;
|
|
console->argument_count = 0;
|
|
memset(console->arguments, 0, sizeof(console->arguments));
|
|
}
|
|
static int decode_lines(const struct command *command, int offset,
|
|
int count, struct lb_line *lines)
|
|
{
|
|
if (count < 0 || count > LB_MAX_LINES)
|
|
return command->count == offset;
|
|
if (command->count != offset + count * 3)
|
|
return 0;
|
|
for (int index = 0; index < count; ++index) {
|
|
lines[index].pin = command->values[offset + index * 3];
|
|
lines[index].active_low = command->values[offset + index * 3 + 1];
|
|
lines[index].initial = command->values[offset + index * 3 + 2];
|
|
}
|
|
return 1;
|
|
}
|
|
static int failure_index(int index)
|
|
{
|
|
return index >= 0 && index <= LB_MAX_LINES;
|
|
}
|
|
static int dispatch_open(struct console *console, const struct command *command,
|
|
long *result)
|
|
{
|
|
if (command->count < 3)
|
|
return 0;
|
|
int count = command->values[0];
|
|
int request = command->values[1];
|
|
int direction = command->values[2];
|
|
struct lb_line lines[LB_MAX_LINES];
|
|
if (!failure_index(request) || !failure_index(direction))
|
|
return 0;
|
|
if (!decode_lines(command, 3, count, lines))
|
|
return 0;
|
|
sim_frame(&console->simulator, request, direction, 0);
|
|
*result = lb_open(&console->session, &console->bus,
|
|
count == 0 || count < 0 || count > LB_MAX_LINES ? 0 : lines,
|
|
count);
|
|
for (int i = 0; i < count && count <= LB_MAX_LINES; ++i) {
|
|
console->arguments[3 + i * 3] = lines[i].pin;
|
|
console->arguments[4 + i * 3] = lines[i].active_low;
|
|
console->arguments[5 + i * 3] = lines[i].initial;
|
|
}
|
|
return 1;
|
|
}
|
|
static int dispatch_plan(struct console *console, const struct command *command, long *result)
|
|
{
|
|
if (command->count < 1)
|
|
return 0;
|
|
int count = command->values[0];
|
|
struct lb_line lines[LB_MAX_LINES];
|
|
if (!decode_lines(command, 1, count, lines))
|
|
return 0;
|
|
const struct lb_line *pointer = count <= 0 || count > LB_MAX_LINES ? 0 : lines;
|
|
if (command->name == 'V')
|
|
*result = lb_plan_validate(pointer, count);
|
|
else
|
|
*result = (long)lb_plan_mask(pointer, count);
|
|
for (int i = 0; i < count && count <= LB_MAX_LINES; ++i) {
|
|
console->arguments[1 + i * 3] = lines[i].pin;
|
|
console->arguments[2 + i * 3] = lines[i].active_low;
|
|
console->arguments[3 + i * 3] = lines[i].initial;
|
|
}
|
|
return 1;
|
|
}
|
|
static int dispatch_apply(struct console *console, const struct command *command,
|
|
long *result)
|
|
{
|
|
if (command->count < 2)
|
|
return 0;
|
|
int count = command->values[0];
|
|
int fail = command->values[1];
|
|
if (!failure_index(fail))
|
|
return 0;
|
|
if (count >= 0 && count <= LB_MAX_LINES) {
|
|
if (command->count != count + 2)
|
|
return 0;
|
|
} else if (command->count != 2) {
|
|
return 0;
|
|
}
|
|
sim_frame(&console->simulator, 0, 0, fail);
|
|
*result = lb_apply(&console->session, &console->bus,
|
|
count <= 0 || count > LB_MAX_LINES ? 0 : command->values + 2,
|
|
count);
|
|
memcpy(console->arguments, command->values, (size_t)command->count * sizeof(int));
|
|
return 1;
|
|
}
|
|
int console_dispatch(struct console *console, const struct command *command,
|
|
long *result)
|
|
{
|
|
++console->sequence;
|
|
console->argument_count = command->count;
|
|
memcpy(console->arguments, command->values, (size_t)command->count * sizeof(int));
|
|
sim_frame(&console->simulator, 0, 0, 0);
|
|
*result = -4;
|
|
if (command->name == 'N' && command->count == 0) {
|
|
unsigned sequence = console->sequence;
|
|
console_init(console);
|
|
console->sequence = sequence;
|
|
*result = 0;
|
|
return 1;
|
|
}
|
|
if (command->name == 'C' && command->count == 0) {
|
|
*result = lb_close(&console->session, &console->bus);
|
|
return 1;
|
|
}
|
|
if (command->name == 'O')
|
|
return dispatch_open(console, command, result);
|
|
if (command->name == 'A')
|
|
return dispatch_apply(console, command, result);
|
|
if (command->name == 'V' || command->name == 'M')
|
|
return dispatch_plan(console, command, result);
|
|
if (command->name == 'L' && command->count == 2) {
|
|
*result = lb_physical(command->values[0], command->values[1]);
|
|
return 1;
|
|
}
|
|
if (command->name == 'Z' && command->count == 2) {
|
|
int operation = command->values[0];
|
|
int count = command->values[1];
|
|
if (operation == 0)
|
|
*result = lb_plan_validate(0, count);
|
|
else if (operation == 1)
|
|
*result = (long)lb_plan_mask(0, count);
|
|
else if (operation == 2)
|
|
*result = lb_open(&console->session, &console->bus, 0, count);
|
|
else if (operation == 3)
|
|
*result = lb_apply(&console->session, &console->bus, 0, count);
|
|
else
|
|
return 0;
|
|
return 1;
|
|
}
|
|
if (command->name == 'B' && command->count == 2) {
|
|
int pin = command->values[0];
|
|
int raw = command->values[1];
|
|
if (pin < 0 || pin >= LB_PIN_COUNT || (raw != 0 && raw != 1))
|
|
return 0;
|
|
if (console->simulator.pins[pin].owner == 1)
|
|
return 0;
|
|
sim_external(&console->simulator, pin, raw);
|
|
*result = 0;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
static int print_vector(FILE *stream, const int *values, int count)
|
|
{
|
|
if (fputc('[', stream) == EOF)
|
|
return -1;
|
|
for (int index = 0; index < count; ++index) {
|
|
if (index && fputc(',', stream) == EOF)
|
|
return -1;
|
|
if (fprintf(stream, "%d", values[index]) < 0)
|
|
return -1;
|
|
}
|
|
return fputc(']', stream) == EOF ? -1 : 0;
|
|
}
|
|
static int print_session(FILE *stream, const struct lb_session *session)
|
|
{
|
|
if (fprintf(stream, "[%d,%d,%u,", session->active,
|
|
session->count, session->generation) < 0)
|
|
return -1;
|
|
if (print_vector(stream, session->pins, LB_MAX_LINES) != 0)
|
|
return -1;
|
|
if (fputc(',', stream) == EOF)
|
|
return -1;
|
|
if (print_vector(stream, session->active_low, LB_MAX_LINES) != 0)
|
|
return -1;
|
|
if (fputc(',', stream) == EOF)
|
|
return -1;
|
|
if (print_vector(stream, session->logical, LB_MAX_LINES) != 0)
|
|
return -1;
|
|
return fputc(']', stream) == EOF ? -1 : 0;
|
|
}
|
|
static int print_pins(FILE *stream, const struct simulator *sim)
|
|
{
|
|
if (fputc('[', stream) == EOF)
|
|
return -1;
|
|
for (int pin = 0; pin < LB_PIN_COUNT; ++pin) {
|
|
const struct sim_pin *state = &sim->pins[pin];
|
|
if (fprintf(stream, "%s[%d,%d,%d]", pin ? "," : "",
|
|
state->owner, state->direction, state->raw) < 0)
|
|
return -1;
|
|
}
|
|
return fputc(']', stream) == EOF ? -1 : 0;
|
|
}
|
|
static int print_events(FILE *stream, const struct simulator *sim)
|
|
{
|
|
if (fputc('[', stream) == EOF)
|
|
return -1;
|
|
for (int index = 0; index < sim->event_count; ++index) {
|
|
const struct sim_event *event = &sim->events[index];
|
|
if (fprintf(stream, "%s[\"%c\",%d,%d,%d]", index ? "," : "",
|
|
event->operation, event->pin, event->raw, event->result) < 0)
|
|
return -1;
|
|
}
|
|
return fputc(']', stream) == EOF ? -1 : 0;
|
|
}
|
|
int console_observe(FILE *stream, const struct console *console, long result)
|
|
{
|
|
if (fprintf(stream, "{\"sequence\":%u,\"result\":%ld,\"session\":",
|
|
console->sequence, result) < 0)
|
|
return -1;
|
|
if (print_session(stream, &console->session) != 0)
|
|
return -1;
|
|
if (fputs(",\"arguments\":", stream) == EOF)
|
|
return -1;
|
|
if (print_vector(stream, console->arguments, console->argument_count) != 0)
|
|
return -1;
|
|
if (fputs(",\"pins\":", stream) == EOF)
|
|
return -1;
|
|
if (print_pins(stream, &console->simulator) != 0)
|
|
return -1;
|
|
if (fputs(",\"events\":", stream) == EOF)
|
|
return -1;
|
|
if (print_events(stream, &console->simulator) != 0)
|
|
return -1;
|
|
if (fputs("}\n", stream) == EOF)
|
|
return -1;
|
|
return 0;
|
|
}
|