functions.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. # Copyright (C) 2006 Fokus Fraunhofer <[email protected]>
  4. alias debug=${DEBUG:-:}
  5. # newline
  6. N="
  7. "
  8. _C=0
  9. NO_EXPORT=1
  10. hotplug_dev() {
  11. env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug net
  12. }
  13. append() {
  14. local var="$1"
  15. local value="$2"
  16. local sep="${3:- }"
  17. eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
  18. }
  19. reset_cb() {
  20. config_cb() { return 0; }
  21. option_cb() { return 0; }
  22. }
  23. reset_cb
  24. config () {
  25. local cfgtype="$1"
  26. local name="$2"
  27. export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
  28. name="${name:-cfg$CONFIG_NUM_SECTIONS}"
  29. append CONFIG_SECTIONS "$name"
  30. config_cb "$cfgtype" "$name"
  31. export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
  32. export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
  33. }
  34. option () {
  35. local varname="$1"; shift
  36. local value="$*"
  37. export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
  38. option_cb "$varname" "$*"
  39. }
  40. config_rename() {
  41. local OLD="$1"
  42. local NEW="$2"
  43. local oldvar
  44. local newvar
  45. [ "$OLD" -a "$NEW" ] || return
  46. for oldvar in `set | grep ^CONFIG_${OLD}_ | \
  47. sed -e 's/\(.*\)=.*$/\1/'` ; do
  48. newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
  49. eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
  50. unset "$oldvar"
  51. done
  52. export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
  53. [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
  54. }
  55. config_unset() {
  56. config_set "$1" "$2" ""
  57. }
  58. config_clear() {
  59. local SECTION="$1"
  60. local oldvar
  61. export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , ,")"
  62. export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
  63. for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
  64. sed -e 's/\(.*\)=.*$/\1/'` ; do
  65. unset $oldvar
  66. done
  67. }
  68. config_load() {
  69. local file="$UCI_ROOT/etc/config/$1"
  70. _C=0
  71. export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
  72. export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
  73. export ${NO_EXPORT:+-n} CONFIG_SECTION=
  74. [ -e "$file" ] && {
  75. . $file
  76. } || return 1
  77. ${CONFIG_SECTION:+config_cb}
  78. }
  79. config_get() {
  80. case "$3" in
  81. "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
  82. *) eval "export ${NO_EXPORT:+-n} -- \"$1=\${CONFIG_${2}_${3}}\"";;
  83. esac
  84. }
  85. # config_get_bool <variable> <section> <option> [<default>]
  86. config_get_bool() {
  87. local _tmp
  88. config_get "_tmp" "$2" "$3"
  89. case "$_tmp" in
  90. 1|on|enabled) export ${NO_EXPORT:+-n} "$1=1";;
  91. 0|off|disabled) export ${NO_EXPORT:+-n} "$1=0";;
  92. *) eval "$1=${4:-0}";;
  93. esac
  94. }
  95. config_set() {
  96. local section="$1"
  97. local option="$2"
  98. local value="$3"
  99. export ${NO_EXPORT:+-n} "CONFIG_${section}_${option}=$value"
  100. }
  101. config_foreach() {
  102. local function="$1"
  103. local type="$2"
  104. local section cfgtype
  105. [ -z "$CONFIG_SECTIONS" ] && return 0
  106. for section in ${CONFIG_SECTIONS}; do
  107. config_get cfgtype "$section" TYPE
  108. [ -n "$type" -a "$cfgtype" != "$type" ] && continue
  109. eval "$function \"\$section\""
  110. done
  111. }
  112. load_modules() {
  113. cd /etc/modules.d
  114. sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
  115. }
  116. include() {
  117. local file
  118. for file in $(ls $1/*.sh 2>/dev/null); do
  119. . $file
  120. done
  121. }
  122. find_mtd_part() {
  123. local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
  124. PART="${PART##mtd}"
  125. echo "${PART:+/dev/mtdblock/$PART}"
  126. }
  127. strtok() { # <string> { <variable> [<separator>] ... }
  128. local tmp
  129. local val="$1"
  130. local count=0
  131. shift
  132. while [ $# -gt 1 ]; do
  133. tmp="${val%%$2*}"
  134. [ "$tmp" = "$val" ] && break
  135. val="${val#$tmp$2}"
  136. export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
  137. shift 2
  138. done
  139. if [ $# -gt 0 -a "$val" ]; then
  140. export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
  141. fi
  142. return $count
  143. }