uci 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. SEP="[^0-9A-Za-z_]"
  23. do_get() {
  24. local PACKAGE
  25. local CONFIG
  26. local OPTION
  27. local DUMMY
  28. strtok "$*" PACKAGE . CONFIG . OPTION $SEP DUMMY
  29. [ $? -ne 3 ] && {
  30. uci_usage get
  31. exit 1
  32. }
  33. uci_load "$PACKAGE"
  34. config_get "$CONFIG" "$OPTION"
  35. }
  36. do_set() {
  37. local PACKAGE
  38. local CONFIG
  39. local OPTION
  40. local VALUE
  41. strtok "$1" PACKAGE . CONFIG = VALUE
  42. [ $? -ne 3 ] && {
  43. uci_usage set
  44. exit 1
  45. }
  46. strtok "$CONFIG" CONFIG . OPTION
  47. if [ $? -eq 1 ]; then
  48. uci_add "$PACKAGE" "$VALUE" "$CONFIG"
  49. else
  50. uci_set "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
  51. fi
  52. }
  53. do_rename() {
  54. [ $# -ne 3 ] && {
  55. uci_usage rename
  56. exit 1
  57. }
  58. uci_rename "$@"
  59. }
  60. do_remove() {
  61. local PACKAGE
  62. local CONFIG
  63. local OPTION
  64. local DUMMY
  65. strtok "$*" PACKAGE . CONFIG . OPTION $SEP DUMMY
  66. [ $? -ne 3 -a $? -ne 2 ] && {
  67. uci_usage rename
  68. exit 1
  69. }
  70. uci_remove "$PACKAGE" "$CONFIG" ${OPTION:+"$OPTION"}
  71. }
  72. do_commit() {
  73. local PACKAGE="$1"
  74. for package in ${PACKAGE:-$(cd /tmp/.uci; ls)}; do
  75. uci_commit "$package"
  76. done
  77. }
  78. do_show() {
  79. local PACKAGE
  80. local CONFIG
  81. local DUMMY
  82. strtok "$*" PACKAGE . CONFIG $SEP DUMMY
  83. [ $? -gt 2 ] && {
  84. uci_usage show
  85. exit 1
  86. }
  87. for package in ${PACKAGE:-$(cd /etc/config; ls)}; do
  88. SECTION=""
  89. config_cb() {
  90. if [ -z "$CONFIG" -o "$CONFIG" = "$2" ]; then
  91. append SECTION "$2"
  92. option_cb() {
  93. append "${CONFIG_SECTION}_VARS" "$1"
  94. }
  95. else
  96. option_cb() {
  97. return 0
  98. }
  99. fi
  100. }
  101. uci_load "$package"
  102. for section in $SECTION; do
  103. config_get type "$section" TYPE
  104. [ -z "$type" ] && continue
  105. echo "$package.$section=$type"
  106. eval "VARS=\"\${${section}_VARS}\""
  107. for var in $VARS; do
  108. config_get val "$section" "$var"
  109. [ -n "$val" ] && {
  110. echo "$package.$section.$var=$val"
  111. config_set "$section" "$var" ""
  112. }
  113. done
  114. config_set "$section" TYPE ""
  115. done
  116. done
  117. }
  118. uci_usage() {
  119. case "$1" in
  120. show) echo "$0 show [<package>[.<config>]]";;
  121. get) echo "$0 get <package>.<config>.<option>";;
  122. set) echo "$0 set <package>.<config>[.<option>]=<value>";;
  123. del) echo "$0 del <package>.<config>[.<option>]";;
  124. rename) echo "$0 rename <package> <config> <name>";;
  125. commit) echo "$0 commit [<package> ... ]";;
  126. *)
  127. echo "Syntax: $0 <command> <arguments...>"
  128. echo
  129. uci_usage show
  130. uci_usage get
  131. uci_usage set
  132. uci_usage del
  133. uci_usage rename
  134. uci_usage commit
  135. echo
  136. exit 1
  137. ;;
  138. esac
  139. }
  140. if [ $# -eq 0 ] ; then
  141. uci_usage
  142. exit 0
  143. fi
  144. local CMD="$1"
  145. shift
  146. case "$CMD" in
  147. set) do_set "$@";;
  148. del) do_remove "$@";;
  149. rename) do_rename "$@";;
  150. get) do_get "$@";;
  151. show) do_show "$@";;
  152. commit) do_commit "$@";;
  153. *) uci_usage;;
  154. esac
  155. exit 0