Sfoglia il codice sorgente

ncd: allow a module to export multiple statement types

ambrop7 15 anni fa
parent
commit
de849d2500

+ 5 - 1
ncd/NCDModule.h

@@ -79,11 +79,15 @@ typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValue *out);
 
 struct NCDModule {
     const char *type;
-    NCDModule_func_globalinit func_globalinit;
     NCDModule_func_new func_new;
     NCDModule_func_free func_free;
     NCDModule_func_die func_die;
     NCDModule_func_getvar func_getvar;
 };
 
+struct NCDModuleGroup {
+    NCDModule_func_globalinit func_globalinit;
+    const struct NCDModule *modules;
+};
+
 #endif

+ 13 - 5
ncd/modules/list.c

@@ -84,9 +84,17 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
     return 0;
 }
 
-const struct NCDModule ncdmodule_list = {
-    .type = "list",
-    .func_new = func_new,
-    .func_free = func_free,
-    .func_getvar = func_getvar
+static const struct NCDModule modules[] = {
+    {
+        .type = "list",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_list = {
+    .modules = modules
 };

+ 9 - 9
ncd/modules/modules.h

@@ -25,16 +25,16 @@
 
 #include <ncd/NCDModule.h>
 
-extern const struct NCDModule ncdmodule_var;
-extern const struct NCDModule ncdmodule_list;
-extern const struct NCDModule ncdmodule_net_backend_physical;
-extern const struct NCDModule ncdmodule_net_backend_badvpn;
-extern const struct NCDModule ncdmodule_net_dns;
-extern const struct NCDModule ncdmodule_net_ipv4_addr;
-extern const struct NCDModule ncdmodule_net_ipv4_route;
-extern const struct NCDModule ncdmodule_net_ipv4_dhcp;
+extern const struct NCDModuleGroup ncdmodule_var;
+extern const struct NCDModuleGroup ncdmodule_list;
+extern const struct NCDModuleGroup ncdmodule_net_backend_physical;
+extern const struct NCDModuleGroup ncdmodule_net_backend_badvpn;
+extern const struct NCDModuleGroup ncdmodule_net_dns;
+extern const struct NCDModuleGroup ncdmodule_net_ipv4_addr;
+extern const struct NCDModuleGroup ncdmodule_net_ipv4_route;
+extern const struct NCDModuleGroup ncdmodule_net_ipv4_dhcp;
 
-static const struct NCDModule *ncd_modules[] = {
+static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_var,
     &ncdmodule_list,
     &ncdmodule_net_backend_physical,

+ 13 - 5
ncd/modules/net_backend_badvpn.c

@@ -253,9 +253,17 @@ static void func_die (void *vo)
     return;
 }
 
-const struct NCDModule ncdmodule_net_backend_badvpn = {
-    .type = "net.backend.badvpn",
-    .func_new = func_new,
-    .func_free = func_free,
-    .func_die = func_die
+static const struct NCDModule modules[] = {
+    {
+        .type = "net.backend.badvpn",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_die = func_die
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_net_backend_badvpn = {
+    .modules = modules
 };

+ 12 - 4
ncd/modules/net_backend_physical.c

@@ -195,8 +195,16 @@ static void func_free (void *vo)
     free(o);
 }
 
-const struct NCDModule ncdmodule_net_backend_physical = {
-    .type = "net.backend.physical",
-    .func_new = func_new,
-    .func_free = func_free
+static const struct NCDModule modules[] = {
+    {
+        .type = "net.backend.physical",
+        .func_new = func_new,
+        .func_free = func_free
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_net_backend_physical = {
+    .modules = modules
 };

+ 12 - 4
ncd/modules/net_dns.c

@@ -270,9 +270,17 @@ static void func_free (void *vo)
     free(o);
 }
 
-const struct NCDModule ncdmodule_net_dns = {
-    .type = "net.dns",
+static const struct NCDModule modules[] = {
+    {
+        .type = "net.dns",
+        .func_new = func_new,
+        .func_free = func_free
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_net_dns = {
     .func_globalinit = func_globalinit,
-    .func_new = func_new,
-    .func_free = func_free
+    .modules = modules
 };

+ 12 - 4
ncd/modules/net_ipv4_addr.c

@@ -105,8 +105,16 @@ static void func_free (void *vo)
     free(o);
 }
 
-const struct NCDModule ncdmodule_net_ipv4_addr = {
-    .type = "net.ipv4.addr",
-    .func_new = func_new,
-    .func_free = func_free
+static const struct NCDModule modules[] = {
+    {
+        .type = "net.ipv4.addr",
+        .func_new = func_new,
+        .func_free = func_free
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_net_ipv4_addr = {
+    .modules = modules
 };

+ 13 - 5
ncd/modules/net_ipv4_dhcp.c

@@ -203,9 +203,17 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
     return 0;
 }
 
-const struct NCDModule ncdmodule_net_ipv4_dhcp = {
-    .type = "net.ipv4.dhcp",
-    .func_new = func_new,
-    .func_free = func_free,
-    .func_getvar = func_getvar
+static const struct NCDModule modules[] = {
+    {
+        .type = "net.ipv4.dhcp",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_net_ipv4_dhcp = {
+    .modules = modules
 };

+ 12 - 4
ncd/modules/net_ipv4_route.c

@@ -130,8 +130,16 @@ static void func_free (void *vo)
     free(o);
 }
 
-const struct NCDModule ncdmodule_net_ipv4_route = {
-    .type = "net.ipv4.route",
-    .func_new = func_new,
-    .func_free = func_free
+static const struct NCDModule modules[] = {
+    {
+        .type = "net.ipv4.route",
+        .func_new = func_new,
+        .func_free = func_free
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_net_ipv4_route = {
+    .modules = modules
 };

+ 13 - 5
ncd/modules/var.c

@@ -93,9 +93,17 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
     return 0;
 }
 
-const struct NCDModule ncdmodule_var = {
-    .type = "var",
-    .func_new = func_new,
-    .func_free = func_free,
-    .func_getvar = func_getvar
+static const struct NCDModule modules[] = {
+    {
+        .type = "var",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_var = {
+    .modules = modules
 };

+ 20 - 11
ncd/ncd.c

@@ -144,6 +144,7 @@ static void print_help (const char *name);
 static void print_version (void);
 static int parse_arguments (int argc, char *argv[]);
 static void signal_handler (void *unused);
+static const struct NCDModule * find_module (const char *name);
 static int statement_init (struct statement *s, struct NCDConfig_statements *conf);
 static void statement_free (struct statement *s);
 static void statement_free_args (struct statement *s);
@@ -268,9 +269,9 @@ int main (int argc, char **argv)
     free(file);
     
     // init modules
-    for (const struct NCDModule **m = ncd_modules; *m; m++) {
-        if ((*m)->func_globalinit && !(*m)->func_globalinit()) {
-            BLog(BLOG_ERROR, "globalinit failed for module %s", (*m)->type);
+    for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
+        if ((*g)->func_globalinit && !(*g)->func_globalinit()) {
+            BLog(BLOG_ERROR, "globalinit failed for some module");
             goto fail5;
         }
     }
@@ -500,6 +501,19 @@ void signal_handler (void *unused)
     }
 }
 
+const struct NCDModule * find_module (const char *name)
+{
+    for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
+        for (const struct NCDModule *m = (*g)->modules; m->type; m++) {
+            if (!strcmp(m->type, name)) {
+                return m;
+            }
+        }
+    }
+    
+    return NULL;
+}
+
 int statement_init (struct statement *s, struct NCDConfig_statements *conf)
 {
     // find module
@@ -507,13 +521,8 @@ int statement_init (struct statement *s, struct NCDConfig_statements *conf)
     if (!module_name) {
         goto fail0;
     }
-    const struct NCDModule **m;
-    for (m = ncd_modules; *m; m++) {
-        if (!strcmp(module_name, (*m)->type)) {
-            break;
-        }
-    }
-    if (!*m) {
+    const struct NCDModule *m = find_module(module_name);
+    if (!m) {
         BLog(BLOG_ERROR, "no module for statement %s", module_name);
         free(module_name);
         goto fail0;
@@ -521,7 +530,7 @@ int statement_init (struct statement *s, struct NCDConfig_statements *conf)
     free(module_name);
     
     // set module
-    s->module = *m;
+    s->module = m;
     
     // init arguments
     s->first_arg = NULL;