183 lines
4.5 KiB
C

#include "lab.h"
#include <ctype.h>
static int parse_integer(const char *text, int *value)
{
char *end;
long parsed;
errno = 0;
parsed = strtol(text, &end, 10);
if (errno || *end || parsed < INT_MIN || parsed > INT_MAX) {
return 0;
}
*value = (int)parsed;
return 1;
}
int command_read(FILE *stream, struct command *command)
{
char line[2048];
char *cursor;
char *start;
size_t length;
while (fgets(line, sizeof(line), stream)) {
memset(command, 0, sizeof(*command));
cursor = line;
while (isspace((unsigned char)*cursor)) {
cursor++;
}
if (!*cursor || *cursor == '#') {
continue;
}
command->operation = *cursor++;
if (*cursor && !isspace((unsigned char)*cursor)) {
return -1;
}
while (*cursor) {
while (isspace((unsigned char)*cursor)) {
cursor++;
}
if (!*cursor) {
break;
}
start = cursor;
while (*cursor && !isspace((unsigned char)*cursor)) {
cursor++;
}
length = (size_t)(cursor - start);
if (length > 20 || command->count == ARG_LIMIT) {
return -1;
}
if (*cursor) {
*cursor++ = 0;
}
if (!parse_integer(start, &command->argument[command->count])) {
return -1;
}
command->count++;
}
return 1;
}
return ferror(stream) ? -1 : 0;
}
int command_has(const struct command *command, size_t count)
{
return command->count == count;
}
int command_byte(const struct command *command, size_t index)
{
return index < command->count && command->argument[index] >= 0 &&
command->argument[index] <= 255;
}
int bytes_valid(const struct command *command, size_t start, size_t count)
{
size_t index;
if (count > BYTE_LIMIT || start + count != command->count) {
return 0;
}
for (index = start; index < start + count; index++) {
if (!command_byte(command, index)) {
return 0;
}
}
return 1;
}
void bytes_from_command(unsigned char *target, const struct command *command,
size_t start, size_t count)
{
size_t index;
for (index = 0; index < count; index++) {
target[index] = (unsigned char)command->argument[start + index];
}
}
void reply_reset(struct reply *reply)
{
memset(reply, 0, sizeof(*reply));
}
void reply_copy(struct reply *reply, const unsigned char *data, size_t length)
{
if (length > BYTE_LIMIT) {
reply->result = -EOVERFLOW;
return;
}
memcpy(reply->bytes, data, length);
reply->length = length;
}
void reply_print(const struct reply *reply)
{
size_t index;
printf("R %d %zu", reply->result, reply->length);
for (index = 0; index < reply->length; index++) {
printf(" %u", (unsigned)reply->bytes[index]);
}
putchar('\n');
}
void trace_begin(struct recorder *recorder)
{
recorder->used = 0;
recorder->overflow = 0;
}
void trace_add(struct recorder *recorder, const char *operation, int a, int b, int c)
{
struct trace_entry *entry;
recorder->total++;
if (recorder->used == TRACE_LIMIT) {
recorder->overflow = 1;
return;
}
entry = &recorder->entries[recorder->used++];
entry->operation = operation;
entry->a = a;
entry->b = b;
entry->c = c;
}
void trace_print(const struct recorder *recorder)
{
size_t index;
for (index = 0; index < recorder->used; index++) {
const struct trace_entry *entry = &recorder->entries[index];
printf("T %s %d %d %d\n", entry->operation, entry->a, entry->b, entry->c);
}
if (recorder->overflow) {
puts("T OVERFLOW 0 0 0");
}
}
void fault_set(struct backend *backend, int index, int error, int count)
{
if (index >= 0 && index < 8 && error >= 0 && count >= 0) {
backend->faults[index].error = error;
backend->faults[index].remaining = (unsigned)count;
}
}
int fault_take(struct backend *backend, int index)
{
struct fault *fault = &backend->faults[index];
if (!fault->remaining) {
return 0;
}
fault->remaining--;
return -fault->error;
}
unsigned byte_sum(const unsigned char *bytes, size_t length)
{
unsigned sum = 0;
size_t index;
for (index = 0; index < length; index++) {
sum = (sum + bytes[index]) & 65535u;
}
return sum;
}