From: Felix Fietkau Date: Thu, 20 Oct 2011 20:09:33 +0000 (+0200) Subject: proto-shell: fix parsing of long proto handler descriptions, simplify code X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=commitdiff_plain;h=4b1a0ddadc70fbce76844bf09af261296ef4fd9f;hp=fb0e4138070d7c2ce723af5780e763af3a1353d8 proto-shell: fix parsing of long proto handler descriptions, simplify code --- 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)