proto-shell: move more core to handler.c
authorFelix Fietkau <nbd@openwrt.org>
Tue, 24 Sep 2013 09:53:48 +0000 (11:53 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 22 Oct 2013 12:10:33 +0000 (14:10 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
handler.c
handler.h
proto-shell.c

index f5db438..531b509 100644 (file)
--- a/handler.c
+++ b/handler.c
@@ -60,7 +60,24 @@ out:
 }
 
 static void
 }
 
 static void
-netifd_init_script_handler(const char *name, script_dump_cb cb)
+netifd_init_script_handler(const char *script, json_object *obj, script_dump_cb cb)
+{
+       json_object *tmp;
+       const char *name;
+
+       if (!json_check_type(obj, json_type_object))
+               return;
+
+       tmp = json_get_field(obj, "name", json_type_string);
+       if (!tmp)
+               return;
+
+       name = json_object_get_string(tmp);
+       cb(script, name, obj);
+}
+
+static void
+netifd_parse_script_handler(const char *name, script_dump_cb cb)
 {
        struct json_tokener *tok = NULL;
        json_object *obj;
 {
        struct json_tokener *tok = NULL;
        json_object *obj;
@@ -90,7 +107,7 @@ netifd_init_script_handler(const char *name, script_dump_cb cb)
 
                obj = json_tokener_parse_ex(tok, start, len);
                if (!is_error(obj)) {
 
                obj = json_tokener_parse_ex(tok, start, len);
                if (!is_error(obj)) {
-                       cb(name, obj);
+                       netifd_init_script_handler(name, obj, cb);
                        json_object_put(obj);
                        json_tokener_free(tok);
                        tok = NULL;
                        json_object_put(obj);
                        json_tokener_free(tok);
                        tok = NULL;
@@ -114,7 +131,7 @@ void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
        prev_fd = netifd_dir_push(dir_fd);
        glob("./*.sh", 0, NULL, &g);
        for (i = 0; i < g.gl_pathc; i++)
        prev_fd = netifd_dir_push(dir_fd);
        glob("./*.sh", 0, NULL, &g);
        for (i = 0; i < g.gl_pathc; i++)
-               netifd_init_script_handler(g.gl_pathv[i], cb);
+               netifd_parse_script_handler(g.gl_pathv[i], cb);
        netifd_dir_pop(prev_fd);
 }
 
        netifd_dir_pop(prev_fd);
 }
 
index de403e1..d4b0334 100644 (file)
--- a/handler.h
+++ b/handler.h
@@ -16,7 +16,7 @@
 
 #include <libubox/blobmsg_json.h>
 
 
 #include <libubox/blobmsg_json.h>
 
-typedef void (*script_dump_cb)(const char *name, json_object *obj);
+typedef void (*script_dump_cb)(const char *script, const char *name, json_object *obj);
 
 static inline json_object *
 json_check_type(json_object *obj, json_type type)
 
 static inline json_object *
 json_check_type(json_object *obj, json_type type)
@@ -30,6 +30,12 @@ json_check_type(json_object *obj, json_type type)
        return obj;
 }
 
        return obj;
 }
 
+static inline json_object *
+json_get_field(json_object *obj, const char *name, json_type type)
+{
+       return json_check_type(json_object_object_get(obj, name), type);
+}
+
 int netifd_open_subdir(const char *name);
 void netifd_init_script_handlers(int dir_fd, script_dump_cb cb);
 char *netifd_handler_parse_config(struct uci_blob_param_list *config, json_object *obj);
 int netifd_open_subdir(const char *name);
 void netifd_init_script_handlers(int dir_fd, script_dump_cb cb);
 char *netifd_handler_parse_config(struct uci_blob_param_list *config, json_object *obj);
index d383cbf..aa638ad 100644 (file)
@@ -734,30 +734,14 @@ error:
        return NULL;
 }
 
        return NULL;
 }
 
-static inline json_object *
-get_field(json_object *obj, const char *name, json_type type)
-{
-       return json_check_type(json_object_object_get(obj, name), type);
-}
-
 static void
 static void
-proto_shell_add_handler(const char *script, json_object *obj)
+proto_shell_add_handler(const char *script, const char *name, json_object *obj)
 {
        struct proto_shell_handler *handler;
        struct proto_handler *proto;
        json_object *config, *tmp;
 {
        struct proto_shell_handler *handler;
        struct proto_handler *proto;
        json_object *config, *tmp;
-       const char *name;
        char *str;
 
        char *str;
 
-       if (!json_check_type(obj, json_type_object))
-               return;
-
-       tmp = get_field(obj, "name", json_type_string);
-       if (!tmp)
-               return;
-
-       name = json_object_get_string(tmp);
-
        handler = calloc_a(sizeof(*handler) + strlen(script) + 1,
                           &str, strlen(name) + 1);
        if (!handler)
        handler = calloc_a(sizeof(*handler) + strlen(script) + 1,
                           &str, strlen(name) + 1);
        if (!handler)
@@ -771,15 +755,15 @@ proto_shell_add_handler(const char *script, json_object *obj)
        proto->config_params = &handler->config;
        proto->attach = proto_shell_attach;
 
        proto->config_params = &handler->config;
        proto->attach = proto_shell_attach;
 
-       tmp = get_field(obj, "no-device", json_type_boolean);
+       tmp = json_get_field(obj, "no-device", json_type_boolean);
        if (tmp && json_object_get_boolean(tmp))
                handler->proto.flags |= PROTO_FLAG_NODEV;
 
        if (tmp && json_object_get_boolean(tmp))
                handler->proto.flags |= PROTO_FLAG_NODEV;
 
-       tmp = get_field(obj, "available", json_type_boolean);
+       tmp = json_get_field(obj, "available", json_type_boolean);
        if (tmp && json_object_get_boolean(tmp))
                handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
 
        if (tmp && json_object_get_boolean(tmp))
                handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
 
-       config = get_field(obj, "config", json_type_array);
+       config = json_get_field(obj, "config", json_type_array);
        if (config)
                handler->config_buf = netifd_handler_parse_config(&handler->config, config);
 
        if (config)
                handler->config_buf = netifd_handler_parse_config(&handler->config, config);