uci.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/sh
  2. # Shell script defining macros for manipulating 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. uci_load() {
  21. config_load "$PACKAGE"
  22. local PACKAGE_BASE="$(basename "$PACKAGE")"
  23. [ -f "/tmp/.uci/${PACKAGE_BASE}" ] && {
  24. . "/tmp/.uci/${PACKAGE_BASE}"
  25. config_cb
  26. }
  27. }
  28. uci_do_update() {
  29. local FILENAME="$1"
  30. local UPDATE="$2"
  31. awk -f /lib/config/uci-update.awk -f - <<EOF
  32. BEGIN {
  33. config = read_file("$FILENAME")
  34. $UPDATE
  35. print config
  36. }
  37. EOF
  38. }
  39. uci_add_update() {
  40. local PACKAGE="$1"
  41. local UPDATE="$2"
  42. local PACKAGE_BASE="$(basename "$PACKAGE")"
  43. # FIXME: add locking?
  44. mkdir -p "/tmp/.uci"
  45. echo "$UPDATE" >> "/tmp/.uci/${PACKAGE_BASE}"
  46. }
  47. uci_set() {
  48. local PACKAGE="$1"
  49. local CONFIG="$2"
  50. local OPTION="$3"
  51. local VALUE="$4"
  52. ( # spawn a subshell so you don't mess up the current environment
  53. uci_load "$PACKAGE"
  54. config_get type "$CONFIG" TYPE
  55. [ -z "$type" ]
  56. ) || uci_add_update "$PACKAGE" "CONFIG_SECTION='$CONFIG'${N}option '$OPTION' '$VALUE'"
  57. }
  58. uci_add() {
  59. local PACKAGE="$1"
  60. local TYPE="$2"
  61. local CONFIG="$3"
  62. uci_add_update "$PACKAGE" "config '$TYPE' '$CONFIG'"
  63. }
  64. uci_rename() {
  65. local PACKAGE="$1"
  66. local CONFIG="$2"
  67. local VALUE="$3"
  68. uci_add_update "$PACKAGE" "config_rename '$CONFIG' '$VALUE'"
  69. }
  70. uci_remove() {
  71. local PACKAGE="$1"
  72. local CONFIG="$2"
  73. local OPTION="$3"
  74. if [ -z "$OPTION" ]; then
  75. uci_add_update "$PACKAGE" "config_clear '$CONFIG'"
  76. else
  77. uci_add_update "$PACKAGE" "config_unset '$CONFIG' '$OPTION'"
  78. fi
  79. }
  80. uci_commit() {
  81. local PACKAGE="$1"
  82. local PACKAGE_BASE="$(basename "$PACKAGE")"
  83. mkdir -p /tmp/.uci
  84. lock "/tmp/.uci/$PACKAGE_BASE.lock"
  85. [ -f "/tmp/.uci/$PACKAGE_BASE" ] && (
  86. updatestr=""
  87. # replace handlers
  88. config() {
  89. append updatestr "config = update_config(config, \"@$2=$1\")" "$N"
  90. }
  91. option() {
  92. append updatestr "config = update_config(config, \"$CONFIG_SECTION.$1=$2\")" "$N"
  93. }
  94. config_rename() {
  95. append updatestr "config = update_config(config, \"&$1=$2\")" "$N"
  96. }
  97. config_unset() {
  98. append updatestr "config = update_config(config, \"-$1.$2\")" "$N"
  99. }
  100. config_clear() {
  101. append updatestr "config = update_config(config, \"-$1\")" "$N"
  102. }
  103. . "/tmp/.uci/$PACKAGE_BASE"
  104. # completely disable handlers so that they don't get in the way
  105. config() {
  106. return 0
  107. }
  108. option() {
  109. return 0
  110. }
  111. config_load "$PACKAGE" || CONFIG_FILENAME="$ROOT/etc/config/$PACKAGE_BASE"
  112. uci_do_update "$CONFIG_FILENAME" "$updatestr" > "/tmp/.uci/$PACKAGE_BASE.new" && {
  113. mv -f "/tmp/.uci/$PACKAGE_BASE.new" "$CONFIG_FILENAME" && \
  114. rm -f "/tmp/.uci/$PACKAGE_BASE"
  115. }
  116. )
  117. lock -u "/tmp/.uci/$PACKAGE_BASE.lock"
  118. }