jshn: fix invalid variable reuse
[project/libubox.git] / sh / jshn.sh
1 # functions for parsing and generating json
2
3 jshn_append() {
4         local var="$1"
5         local value="$2"
6         local sep="${3:- }"
7
8         eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
9 }
10
11 json_init() {
12         [ -n "$JSON_UNSET" ] && eval "unset $JSON_UNSET"
13         export -- JSON_SEQ=0 JSON_STACK= JSON_CUR="JSON_VAR" JSON_UNSET=
14 }
15
16 json_add_generic() {
17         local type="$1"
18         local var="$2"
19         local val="$3"
20         local cur="${4:-$JSON_CUR}"
21
22         export -- "${cur}_$var=$val"
23         export -- "TYPE_${cur}_$var=$type"
24         jshn_append JSON_UNSET "${cur}_$var TYPE_${cur}_$var"
25         jshn_append "KEYS_${cur}" "$var"
26 }
27
28 json_add_table() {
29         JSON_SEQ=$(($JSON_SEQ + 1))
30         jshn_append JSON_STACK "$JSON_CUR"
31         local table="JSON_TABLE$JSON_SEQ"
32         export -- "UP_$table=$JSON_CUR"
33         export -- "KEYS_$table="
34         jshn_append JSON_UNSET "KEYS_$table UP_$table"
35         JSON_CUR="$table"
36 }
37
38 json_add_object() {
39         local cur="$JSON_CUR"
40         json_add_table
41         json_add_generic object "$1" "$JSON_CUR" "$cur"
42 }
43
44 json_close_object() {
45         local oldstack="$JSON_STACK"
46         JSON_CUR="${JSON_STACK##* }"
47         JSON_STACK="${JSON_STACK% *}"
48         [[ "$oldstack" == "$JSON_STACK" ]] && JSON_STACK=
49 }
50
51 json_add_array() {
52         local cur="$JSON_CUR"
53         json_add_table
54         json_add_generic array "$1" "$JSON_CUR" "$cur"
55 }
56
57 json_close_array() {
58         json_close_object
59 }
60
61 json_add_string() {
62         json_add_generic string "$1" "$2"
63 }
64
65 json_add_int() {
66         json_add_generic int "$1" "$2"
67 }
68
69 json_add_boolean() {
70         json_add_generic boolean "$1" "$2"
71 }
72
73 # functions read access to json variables
74
75 json_load() {
76         eval `jshn -r "$1"`
77 }
78
79 json_dump() {
80         jshn -w
81 }
82
83 json_get_type() {
84         local dest="$1"
85         local var="$2"
86         eval "export -- \"$dest=\${TYPE_${JSON_CUR}_$var}\""
87 }
88
89 json_get_var() {
90         local dest="$1"
91         local var="$2"
92         eval "export -- \"$dest=\${${JSON_CUR}_$var}\""
93 }
94
95 json_select() {
96         local target="$1"
97         local type
98
99         [ -z "$1" ] && {
100                 JSON_CUR="JSON_VAR"
101                 return
102         }
103         [[ "$1" == ".." ]] && {
104                 eval "JSON_CUR=\"\${UP_$JSON_CUR}\""
105                 return;
106         }
107         json_get_type type "$target"
108         case "$type" in
109                 object|array)
110                         json_get_var JSON_CUR "$target"
111                 ;;
112                 *)
113                         echo "WARNING: Variable '$target' does not exist or is not an array/object"
114                 ;;
115         esac
116 }