uci.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/sh
  2. # Shell script compatibility wrappers for /sbin/uci
  3. #
  4. # Copyright (C) 2008-2010 OpenWrt.org
  5. # Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. CONFIG_APPEND=
  21. uci_load() {
  22. local PACKAGE="$1"
  23. local DATA
  24. local RET
  25. local VAR
  26. _C=0
  27. if [ -z "$CONFIG_APPEND" ]; then
  28. for VAR in $CONFIG_LIST_STATE; do
  29. export ${NO_EXPORT:+-n} CONFIG_${VAR}=
  30. export ${NO_EXPORT:+-n} CONFIG_${VAR}_LENGTH=
  31. done
  32. export ${NO_EXPORT:+-n} CONFIG_LIST_STATE=
  33. export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
  34. export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
  35. export ${NO_EXPORT:+-n} CONFIG_SECTION=
  36. fi
  37. DATA="$(/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"
  38. RET="$?"
  39. [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
  40. unset DATA
  41. ${CONFIG_SECTION:+config_cb}
  42. return "$RET"
  43. }
  44. uci_set_default() {
  45. local PACKAGE="$1"
  46. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q show "$PACKAGE" > /dev/null && return 0
  47. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} import "$PACKAGE"
  48. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit "$PACKAGE"
  49. }
  50. uci_revert_state() {
  51. local PACKAGE="$1"
  52. local CONFIG="$2"
  53. local OPTION="$3"
  54. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
  55. }
  56. uci_set_state() {
  57. local PACKAGE="$1"
  58. local CONFIG="$2"
  59. local OPTION="$3"
  60. local VALUE="$4"
  61. [ "$#" = 4 ] || return 0
  62. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
  63. }
  64. uci_toggle_state() {
  65. uci_revert_state "$1" "$2" "$3"
  66. uci_set_state "$1" "$2" "$3" "$4"
  67. }
  68. uci_set() {
  69. local PACKAGE="$1"
  70. local CONFIG="$2"
  71. local OPTION="$3"
  72. local VALUE="$4"
  73. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
  74. }
  75. uci_get_state() {
  76. uci_get "$1" "$2" "$3" "$4" "/var/state"
  77. }
  78. uci_get() {
  79. local PACKAGE="$1"
  80. local CONFIG="$2"
  81. local OPTION="$3"
  82. local DEFAULT="$4"
  83. local STATE="$5"
  84. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${STATE:+-P $STATE} -q get "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
  85. RET="$?"
  86. [ "$RET" -ne 0 ] && [ -n "$DEFAULT" ] && echo "$DEFAULT"
  87. return "$RET"
  88. }
  89. uci_add() {
  90. local PACKAGE="$1"
  91. local TYPE="$2"
  92. local CONFIG="$3"
  93. if [ -z "$CONFIG" ]; then
  94. export ${NO_EXPORT:+-n} CONFIG_SECTION="$(/sbin/uci add "$PACKAGE" "$TYPE")"
  95. else
  96. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG=$TYPE"
  97. export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
  98. fi
  99. }
  100. uci_rename() {
  101. local PACKAGE="$1"
  102. local CONFIG="$2"
  103. local OPTION="$3"
  104. local VALUE="$4"
  105. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} rename "$PACKAGE.$CONFIG${VALUE:+.$OPTION}=${VALUE:-$OPTION}"
  106. }
  107. uci_remove() {
  108. local PACKAGE="$1"
  109. local CONFIG="$2"
  110. local OPTION="$3"
  111. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
  112. }
  113. uci_commit() {
  114. local PACKAGE="$1"
  115. /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit $PACKAGE
  116. }