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_PREFIX "./"
24 #define DUMP_SUFFIX " dump"
26 static void proto_shell_add_handler(const char *script
, struct json_object
*obj
)
28 if (json_object_get_type(obj
) != json_type_object
)
31 fprintf(stderr
, "Add handler for script %s: %s\n", script
, json_object_to_json_string(obj
));
34 static void proto_shell_add_script(const char *name
)
36 struct json_tokener
*tok
= NULL
;
37 struct json_object
*obj
;
39 char *start
, *end
, *cmd
;
43 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_PREFIX
) + sizeof(DUMP_SUFFIX
));
44 sprintf(cmd
, DUMP_PREFIX
"%s" DUMP_SUFFIX
, name
);
51 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
58 end
= memchr(start
, '\n', len
);
63 tok
= json_tokener_new();
65 obj
= json_tokener_parse_ex(tok
, start
, len
);
67 proto_shell_add_handler(name
, obj
);
69 json_tokener_free(tok
);
75 len
= buflen
- (start
- buf
);
78 } while (!feof(f
) && !ferror(f
));
81 json_tokener_free(tok
);
86 void __init
proto_shell_init(void)
91 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
95 if (chdir(main_path
)) {
96 perror("chdir(main path)");
100 if (chdir("./proto"))
103 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
107 glob("*.sh", 0, NULL
, &g
);
108 for (i
= 0; i
< g
.gl_pathc
; i
++)
109 proto_shell_add_script(g
.gl_pathv
[i
]);
111 if (list_empty(&handlers
))
116 if (list_empty(&handlers
))