procd: make mDNS TXT record parsing more solid
[openwrt/staging/blocktrron.git] / package / system / procd / files / procd.sh
index 5148b2f03c3511ee5c3de1d5d631ecd5a26c730a..d834fa601a0cc22c3c085551f60d942b2070a1de 100644 (file)
@@ -570,18 +570,21 @@ _procd_set_config_changed() {
 }
 
 procd_add_mdns_service() {
-       local service proto port
+       local service proto port txt_count=0
        service=$1; shift
        proto=$1; shift
        port=$1; shift
        json_add_object "${service}_$port"
        json_add_string "service" "_$service._$proto.local"
        json_add_int port "$port"
-       [ -n "$1" ] && {
-               json_add_array txt
-               for txt in "$@"; do json_add_string "" "$txt"; done
-               json_select ..
-       }
+       for txt in "$@"; do
+               [ -z "$txt" ] && continue
+               txt_count=$((txt_count+1))
+               [ $txt_count -eq 1 ] && json_add_array txt
+               json_add_string "" "$txt"
+       done
+       [ $txt_count -gt 0 ] && json_select ..
+
        json_select ..
 }