jshn: add support for namespaces

Can be used to fix variable namespace clashes in library code using jshn

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
This commit is contained in:
Felix Fietkau 2012-12-16 17:46:02 +01:00
parent 9d8f2dd47b
commit e16fa068a5
2 changed files with 172 additions and 57 deletions

24
jshn.c
View file

@ -9,6 +9,9 @@
#define MAX_VARLEN 256 #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_element(const char *key, json_object *obj);
static int add_json_object(json_object *obj) static int add_json_object(json_object *obj)
@ -145,8 +148,8 @@ static char *get_keys(const char *prefix)
{ {
char *keys; char *keys;
keys = alloca(strlen(prefix) + sizeof("KEYS_") + 1); keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1);
sprintf(keys, "KEYS_%s", prefix); sprintf(keys, "%sKEYS_%s", var_prefix, prefix);
return getenv(keys); return getenv(keys);
} }
@ -154,12 +157,15 @@ static void get_var(const char *prefix, const char **name, char **var, char **ty
{ {
char *tmpname, *varname; char *tmpname, *varname;
tmpname = alloca(strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_")); tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
sprintf(tmpname, "TYPE_%s_%s", prefix, *name);
*var = getenv(tmpname + 5); sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
*var = getenv(tmpname);
sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name);
*type = getenv(tmpname); *type = getenv(tmpname);
memcpy(tmpname, "NAME", 4); sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name);
varname = getenv(tmpname); varname = getenv(tmpname);
if (varname) if (varname)
*name = varname; *name = varname;
@ -238,8 +244,12 @@ int main(int argc, char **argv)
bool no_newline = false; bool no_newline = false;
int ch; int ch;
while ((ch = getopt(argc, argv, "nr:w")) != -1) { while ((ch = getopt(argc, argv, "p:nr:w")) != -1) {
switch(ch) { switch(ch) {
case 'p':
var_prefix = optarg;
var_prefix_len = strlen(var_prefix);
break;
case 'r': case 'r':
return jshn_parse(optarg); return jshn_parse(optarg);
case 'w': case 'w':

View file

@ -1,85 +1,184 @@
# functions for parsing and generating json # functions for parsing and generating json
jshn_append() { _json_get_var() {
local var="$1" local ___dest="$1"
local value="$2" local ___var="$2"
local sep="${3:- }" eval "$___dest=\"\$${JSON_PREFIX}$___var\""
eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
} }
json_init() { _json_set_var() {
[ -n "$JSON_UNSET" ] && eval "unset $JSON_UNSET" local ___var="$1"
export -- JSON_SEQ=0 JSON_STACK= JSON_CUR="JSON_VAR" JSON_UNSET="" KEYS_JSON_VAR= TYPE_JSON_VAR= local ___val="$2"
eval "${JSON_PREFIX}$___var=\"\$___val\""
} }
json_add_generic() { _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_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 type="$1"
local var="$2" local var="$2"
local val="$3" 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 if [ "${cur%%[0-9]*}" = "JSON_ARRAY" ]; then
eval "local aseq=\"\${SEQ_$cur}\"" _json_inc "SEQ_$cur" var
var=$(( ${aseq:-0} + 1 ))
export -- "SEQ_$cur=$var"
else else
local name="${var//[^a-zA-Z0-9_]/_}" local name="${var//[^a-zA-Z0-9_]/_}"
[[ "$name" == "$var" ]] || export -- "NAME_${cur}_${name}=$var" [[ "$name" == "$var" ]] || _json_export "NAME_${cur}_${name}" "$var"
var="$name" var="$name"
fi fi
export -- "${cur}_$var=$val" _json_export "${cur}_$var" "$val"
export -- "TYPE_${cur}_$var=$type" _json_export "TYPE_${cur}_$var" "$type"
jshn_append JSON_UNSET "${cur}_$var TYPE_${cur}_$var" _json_add_key "$cur" "$var"
jshn_append "KEYS_${cur}" "$var"
} }
json_add_table() { _json_add_table() {
local TYPE="$1" local name="$1"
JSON_SEQ=$(($JSON_SEQ + 1)) local type="$2"
jshn_append JSON_STACK "$JSON_CUR" local itype="$3"
local table="JSON_$TYPE$JSON_SEQ" local cur new_cur
export -- "UP_$table=$JSON_CUR"
export -- "KEYS_$table=" _json_get_var cur JSON_CUR
jshn_append JSON_UNSET "KEYS_$table UP_$table" _json_inc JSON_SEQ seq
[ "$TYPE" = "ARRAY" ] && jshn_append JSON_UNSET "SEQ_$table"
JSON_CUR="$table" 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() { json_add_object() {
local cur="$JSON_CUR" _json_add_table "$1" object TABLE
json_add_table TABLE
json_add_generic object "$1" "$JSON_CUR" "$cur"
} }
json_close_object() { json_close_object() {
local oldstack="$JSON_STACK" _json_close_table
JSON_CUR="${JSON_STACK##* }"
JSON_STACK="${JSON_STACK% *}"
[[ "$oldstack" == "$JSON_STACK" ]] && JSON_STACK=
} }
json_add_array() { json_add_array() {
local cur="$JSON_CUR" _json_add_table "$1" array ARRAY
json_add_table ARRAY
json_add_generic array "$1" "$JSON_CUR" "$cur"
} }
json_close_array() { json_close_array() {
json_close_object _json_close_table
} }
json_add_string() { json_add_string() {
json_add_generic string "$1" "$2" _json_add_generic string "$1" "$2"
} }
json_add_int() { json_add_int() {
json_add_generic int "$1" "$2" _json_add_generic int "$1" "$2"
} }
json_add_boolean() { json_add_boolean() {
json_add_generic boolean "$1" "$2" _json_add_generic boolean "$1" "$2"
} }
# functions read access to json variables # functions read access to json variables
@ -89,19 +188,25 @@ json_load() {
} }
json_dump() { json_dump() {
jshn "$@" -w jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w
} }
json_get_type() { json_get_type() {
local dest="$1" 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}\" ]" eval "export -- \"$dest=\${$var}\"; [ -n \"\${$var+x}\" ]"
} }
json_get_var() { json_get_var() {
local dest="$1" local __dest="$1"
local var="${JSON_CUR}_${2//[^a-zA-Z0-9_]/_}" local __cur
eval "export -- \"$dest=\${$var}\"; [ -n \"\${$var+x}\" ]"
_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() { json_get_vars() {
@ -116,7 +221,7 @@ json_select() {
local type local type
[ -z "$1" ] && { [ -z "$1" ] && {
JSON_CUR="JSON_VAR" _json_set_var JSON_CUR "JSON_VAR"
return 0 return 0
} }
[[ "$1" == ".." ]] && { [[ "$1" == ".." ]] && {
@ -126,7 +231,7 @@ json_select() {
json_get_type type "$target" json_get_type type "$target"
case "$type" in case "$type" in
object|array) 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" echo "WARNING: Variable '$target' does not exist or is not an array/object"