95 lines
2.2 KiB
C
95 lines
2.2 KiB
C
#include "window.h"
|
|
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
unsigned window_argument_count(char kind)
|
|
{
|
|
switch (kind) {
|
|
case 'Z':
|
|
return 2u;
|
|
case 'O':
|
|
return 3u;
|
|
case 'S':
|
|
case 'R':
|
|
return 4u;
|
|
case 'C':
|
|
case 'Q':
|
|
return 1u;
|
|
default:
|
|
return 0u;
|
|
}
|
|
}
|
|
|
|
static const char *skip_space(const char *text)
|
|
{
|
|
while (*text && isspace((unsigned char)*text)) {
|
|
text++;
|
|
}
|
|
return text;
|
|
}
|
|
|
|
static int parse_number(const char **cursor, uint32_t *value)
|
|
{
|
|
const char *text = skip_space(*cursor);
|
|
char *end;
|
|
unsigned long long number;
|
|
if (!isdigit((unsigned char)*text)) {
|
|
return 0;
|
|
}
|
|
errno = 0;
|
|
number = strtoull(text, &end, 0);
|
|
if (errno || end == text || number > UINT32_MAX) {
|
|
return 0;
|
|
}
|
|
if (*end && !isspace((unsigned char)*end)) {
|
|
return 0;
|
|
}
|
|
*cursor = end;
|
|
*value = (uint32_t)number;
|
|
return 1;
|
|
}
|
|
|
|
int window_parse(const char *line, window_command *command)
|
|
{
|
|
const char *text = skip_space(line);
|
|
unsigned count;
|
|
unsigned index;
|
|
memset(command, 0, sizeof(*command));
|
|
if (!*text) {
|
|
return 0;
|
|
}
|
|
command->kind = *text++;
|
|
if (*text && !isspace((unsigned char)*text)) {
|
|
return 0;
|
|
}
|
|
count = window_argument_count(command->kind);
|
|
if (!count) {
|
|
return 0;
|
|
}
|
|
command->count = count;
|
|
for (index = 0u; index < count; index++) {
|
|
if (!parse_number(&text, &command->arguments[index])) {
|
|
return 0;
|
|
}
|
|
}
|
|
text = skip_space(text);
|
|
return *text == '\0';
|
|
}
|
|
|
|
void window_help(FILE *output)
|
|
{
|
|
fputs("window-reader commands:\n", output);
|
|
fputs("Z capacity seed\n", output);
|
|
fputs("O id origin length\n", output);
|
|
fputs("S parent child offset length\n", output);
|
|
fputs("R id offset sample_count sample_width\n", output);
|
|
fputs("Q id\n", output);
|
|
fputs("C id\n", output);
|
|
fputs("All operands are unsigned 32-bit decimal or 0x integers.\n", output);
|
|
fputs("The session starts at capacity=1024, seed=0.\n", output);
|
|
fputs("See docs/window-protocol.md for storage and observation format.\n", output);
|
|
}
|