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