[package] firewall:
[openwrt/svn-archive/archive.git] / package / firewall / files / lib / config.sh
1 # Copyright (C) 2009-2010 OpenWrt.org
2 # Copyright (C) 2009 Malte S. Stretz <http://msquadrat.de>
3 #
4 # This is a temporary file, I hope to have some of this stuff merged into
5 # /lib/functions.sh (without the fw_ prefix of course) one day.
6
7 fw_config_append() { # <package>
8 CONFIG_APPEND=1 config_load "$@"
9 unset CONFIG_APPEND
10 }
11
12 fw_config_once() { # <function> <type>
13 local func=$1
14 local type=$2
15 shift 2
16
17 local config=cfg00nil
18 fw_config__once() {
19 config=$1
20 }
21 config_foreach fw_config__once "$type"
22
23 $func $config "$@"
24 }
25
26 fw_config_get_section() { # <config> <prefix> <type> <name> <default> ...
27 local config=$1
28 local prefix=$2
29 shift 2
30
31 [ -n "$config" ] || return 1
32 [ -n "$prefix" ] && {
33 prefix="${prefix}_"
34 export ${NO_EXPORT:+-n} -- "${prefix}NAME"="${config}"
35 config_get "${prefix}TYPE" "$config" TYPE
36 }
37
38 [ "$1" == '{' ] && shift
39 while [ $# -ge 3 ]; do
40 local type=$1
41 local name=$2
42 local dflt=$3
43 shift 3
44 # TODO: Move handling of defaults to /lib/functions.sh
45 # and get replace the case block with the following
46 # two lines:
47 # type=${type#string}
48 # config_get${type:+_${type}} "${prefix}${name}" "$config" "$name" "$dflt" || return
49 case "$type" in
50 string)
51 local tmp
52 config_get tmp "$config" "$name" || return
53 [ -z "$tmp" ] && tmp=$dflt
54 export ${NO_EXPORT:+-n} -- "${prefix}${name}=${tmp}"
55 continue
56 ;;
57 boolean)
58 type=bool
59 ;;
60 esac;
61
62 local cmd=${prefix}config_get_${type}
63 type $cmd > /dev/null || {
64 cmd=config_get_${type}
65 }
66 type $cmd > /dev/null || {
67 echo "config type $type (for $name) not supported" >&2
68 return 1
69 }
70 $cmd "${prefix}${name}" "$config" "$name" "$dflt" || return
71 done
72 }
73
74 config_get_ipaddr() {
75 local varn=$1
76 local conf=$2
77 local name=$3
78 local dflt=$4
79
80 local addr
81 config_get addr "$conf" "$name" || return
82 [ -n "$addr" ] || addr=$dflt
83
84 local mask=${addr#*/}
85 [ "$mask" != "$addr" ] || mask=
86 addr=${addr%/*}
87
88 local vers=
89 case "$addr" in
90 *.*) vers=4 ;;
91 *:*) vers=6 ;;
92 esac
93
94 export ${NO_EXPORT:+-n} -- "${varn}=${addr}"
95 export ${NO_EXPORT:+-n} -- "${varn}_prefixlen=${mask}"
96 export ${NO_EXPORT:+-n} -- "${varn}_version=${vers}"
97 }