8 #include <libubox/blobmsg_json.h>
11 #include "interface.h"
12 #include "interface-ip.h"
15 static LIST_HEAD(handlers);
16 static int proto_fd, main_fd;
18 struct proto_shell_handler {
19 struct list_head list;
20 struct proto_handler proto;
23 #define DUMP_SUFFIX " dump"
25 static void proto_shell_add_handler(const char *script, struct json_object *obj)
27 if (json_object_get_type(obj) != json_type_object)
30 fprintf(stderr, "Add handler for script %s: %s\n", script, json_object_to_json_string(obj));
33 static void proto_shell_add_script(const char *name)
35 struct json_tokener *tok = NULL;
36 struct json_object *obj;
38 char *start, *end, *cmd;
42 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
43 sprintf(cmd, "%s" DUMP_SUFFIX, name);
50 buflen = fread(buf, 1, sizeof(buf) - 1, f);
57 end = memchr(start, '\n', len);
62 tok = json_tokener_new();
64 obj = json_tokener_parse_ex(tok, start, len);
66 proto_shell_add_handler(name, obj);
68 json_tokener_free(tok);
74 len = buflen - (start - buf);
77 } while (!feof(f) && !ferror(f));
80 json_tokener_free(tok);
85 void __init proto_shell_init(void)
90 main_fd = open(".", O_RDONLY | O_DIRECTORY);
94 if (chdir(main_path)) {
95 perror("chdir(main path)");
102 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
106 glob("./*.sh", 0, NULL, &g);
107 for (i = 0; i < g.gl_pathc; i++)
108 proto_shell_add_script(g.gl_pathv[i]);
110 if (list_empty(&handlers))
115 if (list_empty(&handlers))