uci 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/sh
  2. # Shell script for interacting with config files
  3. #
  4. # Copyright (C) 2006 by Fokus Fraunhofer <[email protected]>
  5. # Copyright (C) 2006 by Felix Fietkau <[email protected]>
  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. . /etc/functions.sh
  21. include /lib/config
  22. do_get() {
  23. [ $# -ne 3 ] && {
  24. uci_usage get
  25. exit 1
  26. }
  27. local PACKAGE="$1"
  28. local CONFIG="$2"
  29. local OPTION="$3"
  30. uci_load "$PACAKGE"
  31. config_get "$CONFIG" "$OPTION"
  32. }
  33. do_set() {
  34. [ $# -ne 4 ] && {
  35. uci_usage set
  36. exit 1
  37. }
  38. uci_set "$@"
  39. }
  40. do_add() {
  41. [ $# -ne 3 ] && {
  42. uci_usage add
  43. exit 1
  44. }
  45. uci_add "$@"
  46. }
  47. do_rename() {
  48. [ $# -ne 3 ] && {
  49. uci_usage rename
  50. exit 1
  51. }
  52. uci_rename "$@"
  53. }
  54. do_remove() {
  55. [ $# -ne 3 -a $# -ne 2 ] && {
  56. uci_usage rename
  57. exit 1
  58. }
  59. uci_remove "$@"
  60. }
  61. do_commit() {
  62. [ $# -ne 1 ] && {
  63. uci_usage commit
  64. exit 1
  65. }
  66. uci_commit "$1"
  67. }
  68. do_show() {
  69. [ $# -gt 2 -o $# -lt 1 ] && {
  70. uci_usage show
  71. exit 1
  72. }
  73. PACKAGE="$1"
  74. CONFIG="$2"
  75. SECTION=""
  76. config_cb() {
  77. if [ -z "$CONFIG" -o "$CONFIG" = "$2" ]; then
  78. append SECTION "$2"
  79. option_cb() {
  80. append "${CONFIG_SECTION}_VARS" "$1"
  81. }
  82. else
  83. option_cb() {
  84. return 0
  85. }
  86. fi
  87. }
  88. uci_load "$PACKAGE"
  89. for section in $SECTION; do
  90. config_get type "$section" TYPE
  91. [ -z "$type" ] && continue
  92. echo "@$section=$type"
  93. eval "VARS=\"\${${section}_VARS}\""
  94. for var in $VARS; do
  95. config_get val "$section" "$var"
  96. [ -n "$val" ] && {
  97. echo "${section}.${var}=${val}"
  98. config_set "$section" "$var" ""
  99. }
  100. done
  101. config_set "$section" TYPE ""
  102. done
  103. }
  104. uci_usage() {
  105. case "$1" in
  106. show) echo "$0 show <package> [<config>]";;
  107. get) echo "$0 get <package> <config> <option>";;
  108. set) echo "$0 set <package> <config> <option> <value>";;
  109. add) echo "$0 add <package> <type> <config>";;
  110. del) echo "$0 del <package> <config> [<option>]";;
  111. rename) echo "$0 rename <package> <config> <name>";;
  112. commit) echo "$0 commit <package>";;
  113. *)
  114. echo "Syntax: $0 <command> <arguments...>"
  115. echo
  116. uci_usage show
  117. uci_usage get
  118. uci_usage set
  119. uci_usage add
  120. uci_usage del
  121. uci_usage rename
  122. uci_usage commit
  123. echo
  124. exit 1
  125. ;;
  126. esac
  127. }
  128. if [ $# -eq 0 ] ; then
  129. uci_usage
  130. exit 0
  131. fi
  132. CMD="$1"
  133. shift
  134. case "$CMD" in
  135. set) do_set "$@";;
  136. add) do_add "$@";;
  137. del) do_remove "$@";;
  138. rename) do_rename "$@";;
  139. get) do_get "$@";;
  140. show) do_show "$@";;
  141. commit) do_commit "$@";;
  142. *) uci_usage;;
  143. esac
  144. exit 0