189 lines
4.3 KiB
C

#include "command.h"
#include <ctype.h>
#include <limits.h>
#include <string.h>
void input_init(input_t *input, FILE *stream)
{
input->stream = stream;
input->line = 0u;
input->commands = 0u;
input->rejected = 0u;
input->exhausted = 0u;
}
int parse_uint32(const char *start, const char **end, uint32_t *value)
{
uint32_t accumulated = 0u;
unsigned digits = 0u;
const char *cursor = start;
while (*cursor >= '0' && *cursor <= '9') {
unsigned digit = (unsigned)(*cursor - '0');
if (accumulated > (UINT32_MAX - digit) / 10u) {
return 0;
}
accumulated = accumulated * 10u + digit;
cursor++;
digits++;
}
if (digits == 0u) {
return 0;
}
*value = accumulated;
*end = cursor;
return 1;
}
static const char *skip_space(const char *cursor)
{
while (*cursor != '\0' && isspace((unsigned char)*cursor)) {
cursor++;
}
return cursor;
}
static int read_line(input_t *input, char *buffer, unsigned capacity)
{
unsigned length = 0u;
unsigned overflow = 0u;
unsigned binary = 0u;
unsigned saw_byte = 0u;
int byte;
if (capacity < 2u) {
return -1;
}
for (;;) {
byte = fgetc(input->stream);
if (byte == EOF) {
if (ferror(input->stream)) {
return -1;
}
if (!saw_byte) {
return 0;
}
break;
}
saw_byte = 1u;
if (byte == '\n') {
break;
}
if (byte == 0) {
binary = 1u;
continue;
}
if (length + 1u >= capacity) {
overflow = 1u;
continue;
}
buffer[length] = (char)byte;
length++;
}
input->line++;
if (length != 0u && buffer[length - 1u] == '\r') {
length--;
}
buffer[length] = '\0';
if (overflow || binary) {
return -1;
}
return 1;
}
static int decode_line(input_t *input, const char *buffer, command_t *command)
{
const char *cursor = skip_space(buffer);
int expected;
unsigned index;
if (*cursor == '\0' || *cursor == '#') {
return 0;
}
command->code = *cursor;
command->count = 0u;
command->line = input->line;
cursor++;
if (*cursor != '\0' && !isspace((unsigned char)*cursor)) {
return -1;
}
expected = command_arity(command->code);
if (expected < 0 || (unsigned)expected > COMMAND_ARGUMENTS) {
return -1;
}
for (index = 0u; index < COMMAND_ARGUMENTS; index++) {
command->argument[index] = 0u;
}
for (index = 0u; index < (unsigned)expected; index++) {
const char *end;
uint32_t value;
cursor = skip_space(cursor);
if (!parse_uint32(cursor, &end, &value)) {
return -1;
}
if (*end != '\0' && *end != '#' && !isspace((unsigned char)*end)) {
return -1;
}
command->argument[index] = value;
command->count++;
cursor = end;
}
cursor = skip_space(cursor);
if (*cursor != '\0' && *cursor != '#') {
return -1;
}
return 1;
}
int input_next(input_t *input, command_t *command)
{
char buffer[INPUT_CAPACITY];
if (input->exhausted) {
return 0;
}
for (;;) {
int decoded;
int available = read_line(input, buffer, sizeof(buffer));
if (available == 0) {
input->exhausted = 1u;
return 0;
}
if (available < 0) {
input->rejected++;
return -1;
}
decoded = decode_line(input, buffer, command);
if (decoded < 0) {
input->rejected++;
return -1;
}
if (decoded > 0) {
input->commands++;
return 1;
}
}
}
unsigned byte_pattern(unsigned seed, unsigned index)
{
return (seed + index) & 255u;
}
unsigned checksum_bytes(const unsigned char *data, unsigned length)
{
unsigned index;
unsigned result = 0u;
for (index = 0u; index < length; index++) {
result = (result + (index + 1u) * data[index]) % 65521u;
}
return result;
}
int range_valid(unsigned offset, unsigned length, unsigned capacity)
{
if (offset > capacity) {
return 0;
}
if (length > capacity - offset) {
return 0;
}
return length != 0u;
}