| 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 | local enabled |
| 39 | config_get_bool enabled "$config" enabled 1 |
| 40 | [ $enabled -eq 1 ] || return 1 |
| 41 | |
| 42 | [ "$1" == '{' ] && shift |
| 43 | while [ $# -ge 3 ]; do |
| 44 | local type=$1 |
| 45 | local name=$2 |
| 46 | local dflt=$3 |
| 47 | shift 3 |
| 48 | # TODO: Move handling of defaults to /lib/functions.sh |
| 49 | # and get replace the case block with the following |
| 50 | # two lines: |
| 51 | # type=${type#string} |
| 52 | # config_get${type:+_${type}} "${prefix}${name}" "$config" "$name" "$dflt" || return |
| 53 | case "$type" in |
| 54 | string) |
| 55 | local tmp |
| 56 | config_get tmp "$config" "$name" || return |
| 57 | [ -z "$tmp" ] && tmp=$dflt |
| 58 | export ${NO_EXPORT:+-n} -- "${prefix}${name}=${tmp}" |
| 59 | continue |
| 60 | ;; |
| 61 | boolean) |
| 62 | type=bool |
| 63 | ;; |
| 64 | esac; |
| 65 | |
| 66 | local cmd=${prefix}config_get_${type} |
| 67 | type $cmd > /dev/null || { |
| 68 | cmd=config_get_${type} |
| 69 | } |
| 70 | type $cmd > /dev/null || { |
| 71 | echo "config type $type (for $name) not supported" >&2 |
| 72 | return 1 |
| 73 | } |
| 74 | $cmd "${prefix}${name}" "$config" "$name" "$dflt" || return |
| 75 | done |
| 76 | } |
| 77 | |
| 78 | config_get_ipaddr() { |
| 79 | local varn=$1 |
| 80 | local conf=$2 |
| 81 | local name=$3 |
| 82 | local dflt=$4 |
| 83 | |
| 84 | local addr |
| 85 | config_get addr "$conf" "$name" || return |
| 86 | [ -n "$addr" ] || addr=$dflt |
| 87 | |
| 88 | local mask=${addr#*/} |
| 89 | [ "$mask" != "$addr" ] || mask= |
| 90 | addr=${addr%/*} |
| 91 | |
| 92 | local vers= |
| 93 | case "$addr" in |
| 94 | *:*) vers=6; mask="${mask:-128}" ;; |
| 95 | *.*) vers=4; mask="${mask:-32}" ;; |
| 96 | esac |
| 97 | |
| 98 | export ${NO_EXPORT:+-n} -- "${varn}=${addr}" |
| 99 | export ${NO_EXPORT:+-n} -- "${varn}_prefixlen=${mask}" |
| 100 | export ${NO_EXPORT:+-n} -- "${varn}_version=${vers}" |
| 101 | } |
| 102 | |