From 9e8f5958c77ad8f68b28d45e07d096d2087c28f5 Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 15 Dec 2025 18:52:47 +0100 Subject: [PATCH] write conf scripts --- src/io/conf.c | 35 +++++++++++++++++++++++++++++++++++ src/io/conf.h | 12 ++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/io/conf.c create mode 100644 src/io/conf.h diff --git a/src/io/conf.c b/src/io/conf.c new file mode 100644 index 0000000..082557f --- /dev/null +++ b/src/io/conf.c @@ -0,0 +1,35 @@ +#include "conf.h" + +#include + +#include "../types.h" +#include "../util/atrb.h" + +/* Matches s1 with s2, returns a pointer to s1 where the match stopped. */ +static const char *strmat(const char *s1, const char *s2) PURE NONNULL((1, 2)); +static const char *strmat(const char *s1, const char *s2) +{ + while ((*s1 == *s2) & !!*s1) + s1++, s2++; + return s1; +} + +int conf_getkeyval(const char *restrict buf, const char *const restrict *restrict keys, int klen, const char *restrict *restrict out) +{ + const char *tmp = NULL; + + ASSUME((klen > 0)); + int i = 0; + for (; i < klen && !tmp; i++) { + tmp = strmat(buf, keys[i]); + tmp = keys[i][buf - tmp] ? tmp : 0; + } + + if (!tmp || *tmp != '=') + return -1; + + *out = tmp + 1; + return i; +} + +int conf_val(int); diff --git a/src/io/conf.h b/src/io/conf.h new file mode 100644 index 0000000..06e4462 --- /dev/null +++ b/src/io/conf.h @@ -0,0 +1,12 @@ +#pragma once +#include "../types.h" +#include "../util/atrb.h" + +/* Gets the key and value, if present. Writes the pointer for the value to `out`. + * Returns the key index, or <0 upon failure. */ +int conf_getkeyval(const char *restrict buf, const char *const restrict *restrict keys, int klen, + const char *restrict *restrict out) NONNULL((1, 2, 4)); + +/* Processes the value of `type` in `val`. Outputs to `out`. + * Returns non-zero on failure. */ +int conf_procval(u8 type, const char *restrict val, void *restrict out) NONNULL((2, 3));