uci.sh 4.2 KB

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