jshn: support using characters in elements that do not conform to shell variable...
authorFelix Fietkau <nbd@openwrt.org>
Sun, 11 Sep 2011 09:40:22 +0000 (11:40 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 11 Sep 2011 09:40:22 +0000 (11:40 +0200)
jshn.c
sh/jshn.sh

diff --git a/jshn.c b/jshn.c
index 88e31d1..f7a46de 100644 (file)
--- a/jshn.c
+++ b/jshn.c
@@ -150,14 +150,19 @@ static char *get_keys(const char *prefix)
        return getenv(keys);
 }
 
        return getenv(keys);
 }
 
-static void get_var(const char *prefix, const char *name, char **var, char **type)
+static void get_var(const char *prefix, const char **name, char **var, char **type)
 {
 {
-       char *tmpname;
+       char *tmpname, *varname;
 
 
-       tmpname = alloca(strlen(prefix) + 1 + strlen(name) + 1 + sizeof("TYPE_"));
-       sprintf(tmpname, "TYPE_%s_%s", prefix, name);
+       tmpname = alloca(strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
+       sprintf(tmpname, "TYPE_%s_%s", prefix, *name);
        *var = getenv(tmpname + 5);
        *type = getenv(tmpname);
        *var = getenv(tmpname + 5);
        *type = getenv(tmpname);
+
+       memcpy(tmpname, "NAME", 4);
+       varname = getenv(tmpname);
+       if (varname)
+               *name = varname;
 }
 
 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
 }
 
 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
@@ -167,7 +172,7 @@ static void jshn_add_object_var(json_object *obj, bool array, const char *prefix
        json_object *new;
        char *var, *type;
 
        json_object *new;
        char *var, *type;
 
-       get_var(prefix, name, &var, &type);
+       get_var(prefix, &name, &var, &type);
        if (!var || !type)
                return;
 
        if (!var || !type)
                return;
 
index 7389f8c..8ecefcf 100644 (file)
@@ -19,11 +19,15 @@ json_add_generic() {
        local val="$3"
        local cur="${4:-$JSON_CUR}"
 
        local val="$3"
        local cur="${4:-$JSON_CUR}"
 
-       [ "${cur%%[0-9]*}" = "JSON_ARRAY" ] && {
+       if [ "${cur%%[0-9]*}" = "JSON_ARRAY" ]; then
                eval "local aseq=\"\${SEQ_$cur}\""
                var=$(( ${aseq:-0} + 1 ))
                export -- "SEQ_$cur=$var"
                eval "local aseq=\"\${SEQ_$cur}\""
                var=$(( ${aseq:-0} + 1 ))
                export -- "SEQ_$cur=$var"
-       }
+       else
+               local name="$(echo -n "$var" | tr -C '[a-zA-Z_]' _)"
+               [[ "$name" == "$var" ]] || export -- "NAME_${cur}_${name}=$var"
+               var="$name"
+       fi
 
        export -- "${cur}_$var=$val"
        export -- "TYPE_${cur}_$var=$type"
 
        export -- "${cur}_$var=$val"
        export -- "TYPE_${cur}_$var=$type"