From: Felix Fietkau Date: Sun, 16 Dec 2012 16:46:02 +0000 (+0100) Subject: jshn: add support for namespaces X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=e16fa068a57318fff073da4a0f8f0535a97fe208;p=oweals%2Flibubox.git jshn: add support for namespaces Can be used to fix variable namespace clashes in library code using jshn Signed-off-by: Felix Fietkau --- diff --git a/jshn.c b/jshn.c index f7a46de..dc002c1 100644 --- a/jshn.c +++ b/jshn.c @@ -9,6 +9,9 @@ #define MAX_VARLEN 256 +static const char *var_prefix = ""; +static int var_prefix_len = 0; + static int add_json_element(const char *key, json_object *obj); static int add_json_object(json_object *obj) @@ -145,8 +148,8 @@ static char *get_keys(const char *prefix) { char *keys; - keys = alloca(strlen(prefix) + sizeof("KEYS_") + 1); - sprintf(keys, "KEYS_%s", prefix); + keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1); + sprintf(keys, "%sKEYS_%s", var_prefix, prefix); return getenv(keys); } @@ -154,12 +157,15 @@ static void get_var(const char *prefix, const char **name, char **var, char **ty { char *tmpname, *varname; - tmpname = alloca(strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_")); - sprintf(tmpname, "TYPE_%s_%s", prefix, *name); - *var = getenv(tmpname + 5); + tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_")); + + sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name); + *var = getenv(tmpname); + + sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name); *type = getenv(tmpname); - memcpy(tmpname, "NAME", 4); + sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name); varname = getenv(tmpname); if (varname) *name = varname; @@ -238,8 +244,12 @@ int main(int argc, char **argv) bool no_newline = false; int ch; - while ((ch = getopt(argc, argv, "nr:w")) != -1) { + while ((ch = getopt(argc, argv, "p:nr:w")) != -1) { switch(ch) { + case 'p': + var_prefix = optarg; + var_prefix_len = strlen(var_prefix); + break; case 'r': return jshn_parse(optarg); case 'w': diff --git a/sh/jshn.sh b/sh/jshn.sh index 0f209d3..242e5a4 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -1,85 +1,184 @@ # functions for parsing and generating json -jshn_append() { - local var="$1" - local value="$2" - local sep="${3:- }" +_json_get_var() { + local ___dest="$1" + local ___var="$2" + eval "$___dest=\"\$${JSON_PREFIX}$___var\"" +} - eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\"" +_json_set_var() { + local ___var="$1" + local ___val="$2" + eval "${JSON_PREFIX}$___var=\"\$___val\"" } -json_init() { - [ -n "$JSON_UNSET" ] && eval "unset $JSON_UNSET" - export -- JSON_SEQ=0 JSON_STACK= JSON_CUR="JSON_VAR" JSON_UNSET="" KEYS_JSON_VAR= TYPE_JSON_VAR= +_jshn_append() { + local __var="$1" + local __value="$2" + local __sep="${3:- }" + local __old_val + + _json_get_var __old_val "$__var" + __value="${__old_val:+$__old_val$__sep}$__value" + _json_set_var "$__var" "$__value" +} + +_json_export() { + local __var="${JSON_PREFIX}$1" + local __val="$2" + + export -- "$__var=$__val" + _jshn_append "JSON_UNSET" "$__var" +} + +_json_add_key() { + local table="$1" + local var="$2" + _jshn_append "KEYS_${table}" "$var" +} + +_get_var() { + local __dest="$1" + local __var="$2" + eval "$__dest=\"\$$__var\"" +} + +_set_var() { + local __var="$1" + local __val="$2" + eval "$__var=\"\$__val\"" +} + +_json_inc() { + local _var="$1" + local _dest="$2" + local seq + + _json_get_var seq "$_var" + seq="$((${seq:-0} + 1))" + _json_set_var "$_var" "$seq" + [ -n "$dest" ] && _set_var "$_dest" "$seq" } -json_add_generic() { +_json_stack_push() { + local new_cur="$1" + local cur + + _json_get_var cur JSON_CUR + _jshn_append JSON_STACK "$cur" + _json_set_var JSON_CUR "$new_cur" +} + +_json_add_generic() { local type="$1" local var="$2" local val="$3" - local cur="${4:-$JSON_CUR}" + local cur="$4" + + [ -n "$cur" ] || _json_get_var cur JSON_CUR if [ "${cur%%[0-9]*}" = "JSON_ARRAY" ]; then - eval "local aseq=\"\${SEQ_$cur}\"" - var=$(( ${aseq:-0} + 1 )) - export -- "SEQ_$cur=$var" + _json_inc "SEQ_$cur" var else local name="${var//[^a-zA-Z0-9_]/_}" - [[ "$name" == "$var" ]] || export -- "NAME_${cur}_${name}=$var" + [[ "$name" == "$var" ]] || _json_export "NAME_${cur}_${name}" "$var" var="$name" fi - export -- "${cur}_$var=$val" - export -- "TYPE_${cur}_$var=$type" - jshn_append JSON_UNSET "${cur}_$var TYPE_${cur}_$var" - jshn_append "KEYS_${cur}" "$var" + _json_export "${cur}_$var" "$val" + _json_export "TYPE_${cur}_$var" "$type" + _json_add_key "$cur" "$var" } -json_add_table() { - local TYPE="$1" - JSON_SEQ=$(($JSON_SEQ + 1)) - jshn_append JSON_STACK "$JSON_CUR" - local table="JSON_$TYPE$JSON_SEQ" - export -- "UP_$table=$JSON_CUR" - export -- "KEYS_$table=" - jshn_append JSON_UNSET "KEYS_$table UP_$table" - [ "$TYPE" = "ARRAY" ] && jshn_append JSON_UNSET "SEQ_$table" - JSON_CUR="$table" +_json_add_table() { + local name="$1" + local type="$2" + local itype="$3" + local cur new_cur + + _json_get_var cur JSON_CUR + _json_inc JSON_SEQ seq + + local table="JSON_$itype$seq" + _json_export "UP_$table" "$cur" + _json_export "KEYS_$table" "" + [ "$TYPE" = "ARRAY" ] && _json_export "KEYS_$table" "" + _json_stack_push "$table" + + _json_get_var new_cur JSON_CUR + _json_add_generic object "$1" "$new_cur" "$cur" +} + +_json_close_table() { + local stack new_stack + + _json_get_var stack JSON_STACK + _json_set_var cur "${JSON_STACK##* }" + new_stack="${stack% *}" + [[ "$stack" == "$new_stack" ]] && new_stack= + _json_set_var JSON_STACK "$new_stack" +} + +json_set_namespace() { + local _new="$1" + local _old="$2" + + [ -n "$_old" ] && _set_var "$_old" "$JSON_PREFIX" + JSON_PREFIX="$_new" +} + +json_cleanup() { + local unset + + _json_get_var unset JSON_UNSET + [ -n "$unset" ] && eval "unset $unset" + + unset \ + ${JSON_PREFIX}JSON_SEQ \ + ${JSON_PREFIX}JSON_STACK \ + ${JSON_PREFIX}JSON_CUR \ + ${JSON_PREFIX}JSON_UNSET \ + ${JSON_PREFIX}KEYS_JSON_VAR \ + ${JSON_PREFIX}TYPE_JSON_VAR +} + +json_init() { + json_cleanup + export -- \ + ${JSON_PREFIX}JSON_SEQ=0 \ + ${JSON_PREFIX}JSON_STACK= \ + ${JSON_PREFIX}JSON_CUR="JSON_VAR" \ + ${JSON_PREFIX}JSON_UNSET="" \ + ${JSON_PREFIX}KEYS_JSON_VAR= \ + ${JSON_PREFIX}TYPE_JSON_VAR= } json_add_object() { - local cur="$JSON_CUR" - json_add_table TABLE - json_add_generic object "$1" "$JSON_CUR" "$cur" + _json_add_table "$1" object TABLE } json_close_object() { - local oldstack="$JSON_STACK" - JSON_CUR="${JSON_STACK##* }" - JSON_STACK="${JSON_STACK% *}" - [[ "$oldstack" == "$JSON_STACK" ]] && JSON_STACK= + _json_close_table } json_add_array() { - local cur="$JSON_CUR" - json_add_table ARRAY - json_add_generic array "$1" "$JSON_CUR" "$cur" + _json_add_table "$1" array ARRAY } json_close_array() { - json_close_object + _json_close_table } json_add_string() { - json_add_generic string "$1" "$2" + _json_add_generic string "$1" "$2" } json_add_int() { - json_add_generic int "$1" "$2" + _json_add_generic int "$1" "$2" } json_add_boolean() { - json_add_generic boolean "$1" "$2" + _json_add_generic boolean "$1" "$2" } # functions read access to json variables @@ -89,19 +188,25 @@ json_load() { } json_dump() { - jshn "$@" -w + jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w } json_get_type() { local dest="$1" - local var="TYPE_${JSON_CUR}_$2" + local cur + + _json_get_var cur JSON_CUR + local var="${JSON_PREFIX}TYPE_${cur}_$2" eval "export -- \"$dest=\${$var}\"; [ -n \"\${$var+x}\" ]" } json_get_var() { - local dest="$1" - local var="${JSON_CUR}_${2//[^a-zA-Z0-9_]/_}" - eval "export -- \"$dest=\${$var}\"; [ -n \"\${$var+x}\" ]" + local __dest="$1" + local __cur + + _json_get_var __cur JSON_CUR + local __var="${JSON_PREFIX}${__cur}_${2//[^a-zA-Z0-9_]/_}" + eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" } json_get_vars() { @@ -116,7 +221,7 @@ json_select() { local type [ -z "$1" ] && { - JSON_CUR="JSON_VAR" + _json_set_var JSON_CUR "JSON_VAR" return 0 } [[ "$1" == ".." ]] && { @@ -126,7 +231,7 @@ json_select() { json_get_type type "$target" case "$type" in object|array) - json_get_var JSON_CUR "$target" + _json_get_var JSON_CUR "$target" ;; *) echo "WARNING: Variable '$target' does not exist or is not an array/object"