uci.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/sh
  2. # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
  3. # Copyright (C) 2006-2008 OpenWrt.org
  4. # newline
  5. N="
  6. "
  7. _C=0
  8. NO_EXPORT=1
  9. LOAD_STATE=1
  10. LIST_SEP=" "
  11. hotplug_dev() {
  12. env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
  13. }
  14. append() {
  15. local var="$1"
  16. local value="$2"
  17. local sep="${3:- }"
  18. eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
  19. }
  20. list_contains() {
  21. local var="$1"
  22. local str="$2"
  23. local val
  24. eval "val=\" \${$var} \""
  25. [ "${val%% $str *}" != "$val" ]
  26. }
  27. list_remove() {
  28. local var="$1"
  29. local remove="$2"
  30. local val
  31. eval "val=\" \${$var} \""
  32. val1="${val%% $remove *}"
  33. [ "$val1" = "$val" ] && return
  34. val2="${val##* $remove }"
  35. [ "$val2" = "$val" ] && return
  36. val="${val1## } ${val2%% }"
  37. val="${val%% }"
  38. eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
  39. }
  40. config_load() {
  41. [ -n "$IPKG_INSTROOT" ] && return 0
  42. uci_load "$@"
  43. }
  44. reset_cb() {
  45. config_cb() { return 0; }
  46. option_cb() { return 0; }
  47. list_cb() { return 0; }
  48. }
  49. reset_cb
  50. package() {
  51. return 0
  52. }
  53. config () {
  54. local cfgtype="$1"
  55. local name="$2"
  56. export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
  57. name="${name:-cfg$CONFIG_NUM_SECTIONS}"
  58. append CONFIG_SECTIONS "$name"
  59. [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
  60. export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
  61. export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
  62. }
  63. option () {
  64. local varname="$1"; shift
  65. local value="$*"
  66. export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
  67. [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
  68. }
  69. list() {
  70. local varname="$1"; shift
  71. local value="$*"
  72. local len
  73. config_get len "$CONFIG_SECTION" "${varname}_LENGTH"
  74. len="$((${len:-0} + 1))"
  75. config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
  76. config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
  77. append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
  78. list_cb "$varname" "$*"
  79. }
  80. config_unset() {
  81. config_set "$1" "$2" ""
  82. }
  83. config_clear() {
  84. local SECTION="$1"
  85. local oldvar
  86. list_remove CONFIG_SECTIONS "$SECTION"
  87. export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
  88. for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
  89. sed -e 's/\(.*\)=.*$/\1/'` ; do
  90. unset $oldvar
  91. done
  92. }
  93. config_get() {
  94. case "$3" in
  95. "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
  96. *) eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
  97. esac
  98. }
  99. # config_get_bool <variable> <section> <option> [<default>]
  100. config_get_bool() {
  101. local _tmp
  102. config_get "_tmp" "$2" "$3"
  103. case "$_tmp" in
  104. 1|on|true|enabled) export ${NO_EXPORT:+-n} "$1=1";;
  105. 0|off|false|disabled) export ${NO_EXPORT:+-n} "$1=0";;
  106. *) eval "$1=$4";;
  107. esac
  108. }
  109. config_set() {
  110. local section="$1"
  111. local option="$2"
  112. local value="$3"
  113. local old_section="$CONFIG_SECTION"
  114. CONFIG_SECTION="$section"
  115. option "$option" "$value"
  116. CONFIG_SECTION="$old_section"
  117. }
  118. config_foreach() {
  119. local function="$1"
  120. [ "$#" -ge 1 ] && shift
  121. local type="$1"
  122. [ "$#" -ge 1 ] && shift
  123. local section cfgtype
  124. [ -z "$CONFIG_SECTIONS" ] && return 0
  125. for section in ${CONFIG_SECTIONS}; do
  126. config_get cfgtype "$section" TYPE
  127. [ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
  128. eval "$function \"\$section\" \"\$@\""
  129. done
  130. }
  131. config_list_foreach() {
  132. [ "$#" -ge 3 ] || return 0
  133. local section="$1"; shift
  134. local option="$1"; shift
  135. local function="$1"; shift
  136. local val
  137. local len
  138. local c=1
  139. config_get len "${section}" "${option}_LENGTH"
  140. [ -z "$len" ] && return 0
  141. while [ $c -le "$len" ]; do
  142. config_get val "${section}" "${option}_ITEM$c"
  143. eval "$function \"\$val\" \"$@\""
  144. c="$(($c + 1))"
  145. done
  146. }