diff options
author | Felix Fietkau <nbd@openwrt.org> | 2011-10-20 22:09:33 +0200 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2011-10-20 22:09:33 +0200 |
commit | 4b1a0ddadc70fbce76844bf09af261296ef4fd9f (patch) | |
tree | bad893d814185bdea7f6caa7e204d36c726b76c4 | |
parent | fb0e4138070d7c2ce723af5780e763af3a1353d8 (diff) |
proto-shell: fix parsing of long proto handler descriptions, simplify code
-rw-r--r-- | proto-shell.c | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/proto-shell.c b/proto-shell.c index a22c0e3..c2a6d00 100644 --- a/proto-shell.c +++ b/proto-shell.c @@ -555,9 +555,9 @@ static void proto_shell_add_script(const char *name) struct json_tokener *tok = NULL; json_object *obj; static char buf[512]; - char *start, *end, *cmd; + char *start, *cmd; FILE *f; - int buflen, len; + int len; #define DUMP_SUFFIX " '' dump" @@ -569,33 +569,25 @@ static void proto_shell_add_script(const char *name) return; do { - buflen = fread(buf, 1, sizeof(buf) - 1, f); - if (buflen <= 0) + start = fgets(buf, sizeof(buf), f); + if (!start) continue; - start = buf; - len = buflen; - do { - end = memchr(start, '\n', len); - if (end) - len = end - start; - - if (!tok) - tok = json_tokener_new(); - - obj = json_tokener_parse_ex(tok, start, len); - if (!is_error(obj)) { - proto_shell_add_handler(name, obj); - json_object_put(obj); - json_tokener_free(tok); - tok = NULL; - } - - if (end) { - start = end + 1; - len = buflen - (start - buf); - } - } while (len > 0); + len = strlen(start); + + if (!tok) + tok = json_tokener_new(); + + obj = json_tokener_parse_ex(tok, start, len); + if (!is_error(obj)) { + proto_shell_add_handler(name, obj); + json_object_put(obj); + json_tokener_free(tok); + tok = NULL; + } else if (start[len - 1] == '\n') { + json_tokener_free(tok); + tok = NULL; + } } while (!feof(f) && !ferror(f)); if (tok) |