147 lines
3.6 KiB
C

#include "probe.h"
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static const char *skip_space(const char *cursor)
{
while (*cursor && isspace((unsigned char)*cursor)) {
cursor++;
}
return cursor;
}
static int read_unsigned(const char **cursor, uint32_t *value)
{
const char *start = skip_space(*cursor);
unsigned long long parsed;
char *end;
if (!isdigit((unsigned char)*start)) {
return 0;
}
errno = 0;
parsed = strtoull(start, &end, 0);
if (end == start || errno != 0 || parsed > UINT32_MAX) {
return 0;
}
if (*end && !isspace((unsigned char)*end) && *end != '#') {
return 0;
}
*value = (uint32_t)parsed;
*cursor = end;
return 1;
}
int parse_command(const char *line, Command *command)
{
const char *cursor = skip_space(line);
unsigned required;
unsigned index;
memset(command, 0, sizeof(*command));
if (!*cursor || *cursor == '#') {
return 0;
}
command->name = *cursor++;
if (*cursor && !isspace((unsigned char)*cursor)) {
return 0;
}
required = service_arity(command->name);
if (required > PROBE_ARGS) {
return 0;
}
for (index = 0u; index < required; index++) {
if (!read_unsigned(&cursor, &command->args[index])) {
return 0;
}
}
cursor = skip_space(cursor);
if (*cursor && *cursor != '#') {
return 0;
}
command->count = required;
return 1;
}
int line_ignored(const char *line)
{
const char *cursor = skip_space(line);
return !*cursor || *cursor == '#';
}
int read_line(FILE *input, char *buffer, size_t capacity, size_t *bytes)
{
int ch;
size_t used = 0u;
int overflow = 0;
*bytes = 0u;
if (capacity < 2u) {
return -1;
}
while ((ch = fgetc(input)) != EOF) {
(*bytes)++;
if (ch == '\n') {
break;
}
if (used + 1u < capacity) {
buffer[used++] = (char)ch;
} else {
overflow = 1;
}
}
buffer[used] = '\0';
if (ch == EOF && !*bytes) {
return 0;
}
return overflow ? -1 : 1;
}
int parse_options(int argc, char **argv, Options *options)
{
int index;
memset(options, 0, sizeof(*options));
for (index = 1; index < argc; index++) {
if (strcmp(argv[index], "--json") == 0) {
options->json = 1;
} else if (strcmp(argv[index], "--brief") == 0) {
options->brief = 1;
} else if (strcmp(argv[index], "--history") == 0) {
options->history = 1;
} else if (strcmp(argv[index], "--stats") == 0) {
options->statistics = 1;
} else if (strcmp(argv[index], "--help") == 0) {
options->help = 1;
} else {
return 0;
}
}
return 1;
}
void print_help(FILE *out)
{
fprintf(out, "%s deterministic service desk\n", service_name());
fputs("Usage: ./probe [--json] [--brief] [--history] [--stats]\n", out);
fputs("Commands read from stdin; # begins a comment.\n", out);
fputs("Numbers use unsigned 32-bit decimal or 0x notation.\n", out);
fputs("--json prints structured observations.\n", out);
fputs("--brief omits raw snapshot words.\n", out);
fputs("--history prints the last sixteen outcomes to stderr.\n", out);
fputs("--stats prints stream counters to stderr.\n", out);
service_help(out);
}
int stream_finish(FILE *input, FILE *output)
{
if (ferror(input)) {
return 2;
}
if (fflush(output) == EOF) {
return 2;
}
if (ferror(output)) {
return 2;
}
return 0;
}