summaryrefslogtreecommitdiffstats
path: root/src/config-load.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2019-05-31 18:42:01 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2019-05-31 18:42:01 +0200
commitbcef77a7fb3b73d2a2fbcea51012014b62755bb5 (patch)
tree3a5935014a6a2569e592f40086f7858d1207c66c /src/config-load.c
parent44cb17317c40fa9d39b3402f0826006f20387be5 (diff)
downloadneco-bcef77a7fb3b73d2a2fbcea51012014b62755bb5.tar
neco-bcef77a7fb3b73d2a2fbcea51012014b62755bb5.zip
Switch to a JSON-based config format
We can get rid of a lot of code by ditching out INI format parser. We also remove the support for "subtype" and "generator" templates for now; rather than implementing this in NeCo itself, templates could be implemented as a Lua DSL later.
Diffstat (limited to 'src/config-load.c')
-rw-r--r--src/config-load.c108
1 files changed, 7 insertions, 101 deletions
diff --git a/src/config-load.c b/src/config-load.c
index 63a87b4..19b9d3a 100644
--- a/src/config-load.c
+++ b/src/config-load.c
@@ -14,110 +14,16 @@
#include <string.h>
#include <unistd.h>
-typedef struct _load_ctx {
- struct avl_tree subtypes;
- struct avl_tree devices;
-} load_ctx_t;
-
-static const char * extname(const char *filename) {
- const char *dot = strrchr(filename, '.');
- return dot ? (dot+1) : NULL;
-}
-
-static bool isfile(int fd) {
- struct stat buf;
- if (fstat(fd, &buf) < 0)
- return false;
-
- return (buf.st_mode & S_IFMT) == S_IFREG;
-}
-
-static bool read_config_file(load_ctx_t *ctx, int dirfd, const char *filename) {
- const char *ext = extname(filename);
- if (!ext)
- return true;
-
- struct avl_tree *target_tree;
- if (strcmp(ext, "sub") == 0 || strcmp(ext, "gen") == 0)
- target_tree = &ctx->subtypes;
- else if (get_device_type(ext))
- target_tree = &ctx->devices;
- else
- return true;
-
- int fd = openat(dirfd, filename, O_RDONLY);
- if (fd < 0)
- return false;
-
- if (!isfile(fd)) {
- close(fd);
- return true;
- }
-
- FILE *f = fdopen(fd, "r");
- if (!f) {
- close(fd);
- return false;
- }
-
- ini_file_t *data = read_ini_file(f);
- fclose(f);
-
- if (!data)
- return false;
-
- config_object_t *obj = calloc(1, sizeof(*obj));
- if (!obj) {
- free_ini_file(data);
- return false;
- }
- obj->data = data;
-
- char *name = strndup(filename, (ext - filename) - 1);
- if (!name) {
- config_object_free(obj);
- return false;
- }
- NODE_NAME(obj) = name;
-
- obj->type = strdup(ext);
- if (!obj->type) {
- config_object_free(obj);
+bool read_config(const char *path) {
+ struct json_object *config = json_object_from_file(path);
+ if (!config)
return false;
- }
- avl_insert(target_tree, &obj->node);
-
- return true;
-}
+ struct avl_tree *devices = config_process(config);
+ json_object_put(config);
-static bool read_config_dir(load_ctx_t *ctx, const char *path) {
- DIR *dir = opendir(path);
- if (!dir)
+ if (!devices)
return false;
-
- int fd = dirfd(dir);
-
- struct dirent *ent;
- while ((ent = readdir(dir)) != NULL)
- read_config_file(ctx, fd, ent->d_name);
-
- closedir(dir);
-
- return true;
-}
-
-bool read_config(const char *path) {
- load_ctx_t ctx;
- avl_init(&ctx.subtypes, avl_strcmp, true, NULL);
- avl_init(&ctx.devices, avl_strcmp, true, NULL);
-
- bool ret = read_config_dir(&ctx, path);
-
- struct avl_tree *subtypes = config_process_subtypes(&ctx.subtypes);
- struct avl_tree *devices = config_process_devices(&ctx.devices);
-
- free(subtypes);
device_t *dev, *tmp;
avl_for_each_element(devices, dev, node)
@@ -131,5 +37,5 @@ bool read_config(const char *path) {
free(devices);
- return ret;
+ return true;
}