proto-shell: fix parsing of long proto handler descriptions, simplify code
[project/netifd.git] / proto-shell.c
index eb60fc1..c2a6d00 100644 (file)
@@ -242,6 +242,7 @@ proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
 {
        struct interface_ip_settings *ip;
        struct blob_attr *cur;
+       int dev_create = 1;
        bool addr_ext = false;
        bool up;
 
@@ -254,6 +255,12 @@ proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
                return 0;
        }
 
+       if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
+               addr_ext = blobmsg_get_bool(cur);
+               if (addr_ext)
+                       dev_create = 2;
+       }
+
        if (!tb[NOTIFY_IFNAME]) {
                if (!state->proto.iface->main_dev.dev)
                        return UBUS_STATUS_INVALID_ARGUMENT;
@@ -262,7 +269,7 @@ proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
                        device_remove_user(&state->l3_dev);
 
                device_add_user(&state->l3_dev,
-                       device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
+                       device_get(blobmsg_data(tb[NOTIFY_IFNAME]), dev_create));
                state->proto.iface->l3_dev = &state->l3_dev;
                device_claim(&state->l3_dev);
        }
@@ -270,9 +277,6 @@ proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
        ip = &state->proto.iface->proto_ip;
        interface_update_start(state->proto.iface);
 
-       if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
-               addr_ext = blobmsg_get_bool(cur);
-
        if ((cur = tb[NOTIFY_IPADDR]) != NULL)
                proto_shell_parse_addr_list(ip, cur, false, addr_ext);
 
@@ -391,7 +395,7 @@ proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
        }
 }
 
-struct interface_proto_state *
+static struct interface_proto_state *
 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
                   struct blob_attr *attr)
 {
@@ -551,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"
 
@@ -565,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)
@@ -600,7 +596,7 @@ static void proto_shell_add_script(const char *name)
        pclose(f);
 }
 
-void __init proto_shell_init(void)
+static void __init proto_shell_init(void)
 {
        glob_t g;
        int main_fd;