diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2019-05-31 18:42:01 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2019-05-31 18:42:01 +0200 |
commit | bcef77a7fb3b73d2a2fbcea51012014b62755bb5 (patch) | |
tree | 3a5935014a6a2569e592f40086f7858d1207c66c /src/config-process.c | |
parent | 44cb17317c40fa9d39b3402f0826006f20387be5 (diff) | |
download | neco-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-process.c')
-rw-r--r-- | src/config-process.c | 115 |
1 files changed, 19 insertions, 96 deletions
diff --git a/src/config-process.c b/src/config-process.c index e71b228..a0671cb 100644 --- a/src/config-process.c +++ b/src/config-process.c @@ -1,6 +1,5 @@ #include "config-process.h" #include "device.h" -#include "keywords.h" #include "util.h" #include <libubox/avl-cmp.h> @@ -9,6 +8,7 @@ #include <stdlib.h> #include <string.h> + typedef struct _process_ctx { struct avl_tree *ret; } process_ctx_t; @@ -17,92 +17,28 @@ typedef struct _config_subtype { struct avl_node node; } config_subtype_t; -void config_object_free(config_object_t *obj) { - free_ini_file(obj->data); - free(obj->type); - free(NODE_NAME(obj)); - free(obj); -} - -static bool subtype_supported(const process_ctx_t *ctx, const char *type) { - switch (lookup_keyword(type)) { - case KW_Properties: - case KW_Generate: - case KW_Static: - case KW_DHCP: - case KW_DHCPv6: - return true; - - default: - return avl_find(ctx->ret, type); - } -} -static config_subtype_t * config_process_subtype(const process_ctx_t *ctx, config_object_t *obj) { - ini_section_t *section; - list_for_each_entry(section, &obj->data->sections, node) { - printf("%s %s %i\n", NODE_NAME(obj), section->name, subtype_supported(ctx, section->name)); +static device_t * config_process_device(const char *name, struct json_object *obj) { + const char *typename = NULL; + struct json_object *device = neco_json_get_value(obj, "device", json_type_object); + if (device) + typename = neco_json_get_string(obj, "type"); - if (!subtype_supported(ctx, section->name)) - return NULL; - } - - config_subtype_t *ret = calloc(1, sizeof(*ret)); - if (!ret) + const device_type_t *type = get_device_type(typename ?: "interface"); + if (!type) return NULL; - char *name = strdup(NODE_NAME(obj)); - if (!name) { - free(ret); - return NULL; - } - - NODE_NAME(ret) = name; - - return ret; + return type->process_config(name, obj); } -struct avl_tree * config_process_subtypes(struct avl_tree *sub) { - process_ctx_t ctx; - ctx.ret = calloc(1, sizeof(*ctx.ret)); - if (!ctx.ret) +struct avl_tree * config_process(struct json_object *config) { + if (!json_object_is_type(config, json_type_object)) return NULL; - avl_init(ctx.ret, avl_strcmp, false, NULL); - - while (true) { - size_t processed = 0; - - config_object_t *obj, *tmp; - avl_for_each_element_safe(sub, obj, node, tmp) { - config_subtype_t *t = config_process_subtype(&ctx, obj); - if (!t) - continue; - - avl_delete(sub, &obj->node); - config_object_free(obj); - - avl_insert(ctx.ret, &t->node); - - processed++; - } - - if (!processed) - break; - } - - return ctx.ret; -} - -static device_t * config_process_device(config_object_t *obj) { - const device_type_t *type = get_device_type(obj->type); - - assert(type != NULL); - - return type->process_config(NODE_NAME(obj), obj->data); -} + struct json_object *devices = neco_json_get_value(config, "devices", json_type_object); + if (!devices) + return NULL; -struct avl_tree * config_process_devices(struct avl_tree *devices) { process_ctx_t ctx; ctx.ret = calloc(1, sizeof(*ctx.ret)); if (!ctx.ret) @@ -110,25 +46,12 @@ struct avl_tree * config_process_devices(struct avl_tree *devices) { avl_init(ctx.ret, avl_strcmp, false, NULL); - while (true) { - size_t processed = 0; - - config_object_t *obj, *tmp; - avl_for_each_element_safe(devices, obj, node, tmp) { - device_t *device = config_process_device(obj); - if (!device) - continue; - - avl_delete(devices, &obj->node); - config_object_free(obj); - - avl_insert(ctx.ret, &device->node); - - processed++; - } + json_object_object_foreach(devices, name, obj) { + device_t *device = config_process_device(name, obj); + if (!device) + continue; - if (!processed) - break; + avl_insert(ctx.ret, &device->node); } return ctx.ret; |