aboutsummaryrefslogtreecommitdiff
path: root/lib/libcfg
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-09-16 15:38:45 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-09-17 14:53:19 +0200
commit5cd5f48f765f00b81786c6f569314474a91a06b8 (patch)
treef7cbda4a0f09ebffd8efb71295914a22557cc2b8 /lib/libcfg
parent481744a2ba4d9b795918d3bc0767321e05e10bbd (diff)
Add helper library for cron configuration
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'lib/libcfg')
-rw-r--r--lib/libcfg/rdcfg.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/libcfg/rdcfg.c b/lib/libcfg/rdcfg.c
new file mode 100644
index 0000000..71a994f
--- /dev/null
+++ b/lib/libcfg/rdcfg.c
@@ -0,0 +1,83 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * Copyright (C) 2018 - David Oberhollenzer
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+#include "libcfg.h"
+
+#include <string.h>
+#include <stdio.h>
+
+static const cfg_param_t *find_param(rdline_t *rd, const char *name,
+ const cfg_param_t *params, size_t count)
+{
+ size_t i;
+
+ for (i = 0; i < count; ++i) {
+ if (!strcmp(params[i].key, name))
+ return params + i;
+ }
+
+ fprintf(stderr, "%s: %zu: unknown keyword '%s'\n",
+ rd->filename, rd->lineno, name);
+ return NULL;
+}
+
+int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count,
+ int flags)
+{
+ const cfg_param_t *p;
+ char *key, *value;
+ int ret;
+
+ while ((ret = rdline(rd)) == 0) {
+ if (splitkv(rd, &key, &value))
+ return -1;
+
+ p = find_param(rd, key, params, count);
+ if (p == NULL)
+ return -1;
+
+ if (p->allow_block && *value == '{') {
+ for (++value; *value == ' '; ++value)
+ ;
+
+ if (*value != '\0') {
+ ret = p->handle(cfgobj, value, rd, flags);
+ if (ret)
+ return -1;
+ }
+
+ while ((ret = rdline(rd)) == 0) {
+ if (strcmp(rd->buffer, "}") == 0)
+ break;
+ if (p->handle(cfgobj, rd->buffer, rd, flags))
+ return -1;
+ }
+
+ if (ret < 0)
+ return -1;
+ if (ret > 0)
+ goto fail_bra;
+ } else if (p->handle(cfgobj, value, rd, flags)) {
+ return -1;
+ }
+ }
+
+ return ret < 0 ? -1 : 0;
+fail_bra:
+ fprintf(stderr, "%s: missing '}' before end-of-file\n", rd->filename);
+ return -1;
+}