2
0

uci-update.awk 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Configuration update functions
  2. #
  3. # Copyright (C) 2006 by Fokus Fraunhofer <[email protected]>
  4. # Copyright (C) 2006 by Felix Fietkau <[email protected]>
  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. function read_file(filename, result) {
  20. while ((getline <filename) == 1) {
  21. result = result $0 "\n"
  22. }
  23. gsub(/\n*$/, "", result)
  24. return result
  25. }
  26. function cmd2option(str, tmp) {
  27. if (match(str,"=")!=0) {
  28. res = "\toption " substr(str,1,RSTART-1) "\t'" substr(str,RSTART+1) "'"
  29. } else {
  30. res= ""
  31. }
  32. return res
  33. }
  34. function cmd2config(atype, aname) {
  35. return "config \"" atype "\" \"" aname "\""
  36. }
  37. function update_config(cfg, update, \
  38. lines, line, l, n, i, i2, section, scnt, remove, tmp, aidx, rest) {
  39. scnt = 1
  40. linecnt=split(cfg "\n", lines, "\n")
  41. cfg = ""
  42. for (n = 1; n < linecnt; n++) {
  43. # stupid parser for quoted arguments (e.g. for the type string).
  44. # not to be used to gather variable values (backslash escaping doesn't work)
  45. line = lines[n]
  46. gsub(/^[ \t]*/, "", line)
  47. gsub(/#.*$/, "", line)
  48. i2 = 1
  49. delete l
  50. rest = line
  51. while (length(rest)) {
  52. if (match(rest, /[ \t\"]+/)) {
  53. if (RSTART>1) {
  54. l[i2] = substr(rest,1,RSTART-1)
  55. i2++
  56. }
  57. aidx=index(rest,"\"")
  58. if (aidx>=RSTART && aidx<=RSTART+RLENGTH) {
  59. rest=substr(rest,aidx+1)
  60. # find the end of the string
  61. match(rest,/\"/)
  62. l[i2]=substr(rest,1,RSTART-1)
  63. i2++
  64. }
  65. rest=substr(rest,RSTART+RLENGTH)
  66. } else {
  67. l[i2] = rest
  68. i2++
  69. rest = ""
  70. }
  71. }
  72. line = lines[n]
  73. # when a command wants to set a config value for the current
  74. # section and a blank line is encountered before an option with
  75. # the same name, insert it here to maintain some coherency between
  76. # manually and automatically created option lines
  77. # if an option with the same name appears after this point, simply
  78. # ignore it, because it is already set.
  79. if ((section != "") && (l[1] != "option")) {
  80. if (line ~ /^[ \t]*$/) {
  81. if (update ~ "^" section "\\.") {
  82. gsub("^" section ".", "", update)
  83. cfg = cfg cmd2option(update) "\n"
  84. gsub(/=.*$/, "", update)
  85. update = "-" section "." update
  86. }
  87. }
  88. }
  89. if (l[1] == "config") {
  90. # look for all unset values
  91. if (section != "") {
  92. flag=0
  93. if (update ~ "^" section "\\.") {
  94. flag=1
  95. gsub("^" section ".", "", update)
  96. cfg = cfg cmd2option(update) "\n"
  97. update = "-" section "." update
  98. }
  99. if (flag!=0) cfg = cfg "\n"
  100. }
  101. remove = ""
  102. section = l[3]
  103. if (!length(section)) {
  104. section = "cfg" scnt
  105. }
  106. scnt++
  107. if (update == "-" section) {
  108. remove = "section"
  109. update = ""
  110. } else if (update ~ "^@" section "=") {
  111. update = ""
  112. } else if (update ~ "^&" section "=") {
  113. gsub("^&" section "=", "", update)
  114. line = cmd2config(l[2],update)
  115. update = ""
  116. }
  117. }
  118. if (remove == "option") remove = ""
  119. if (l[1] == "option") {
  120. if (update ~ "^-" section "\\." l[2] "$") remove = "option"
  121. # if a supplied config value already exists, replace the whole line
  122. if (match(update, "^" section "." l[2] "=")) {
  123. gsub("^" section ".", "", update)
  124. line = cmd2option(update)
  125. update = ""
  126. }
  127. }
  128. if (remove == "") cfg = cfg line "\n"
  129. }
  130. # any new options for the last section??
  131. if (section != "") {
  132. if (update ~ "^" section "\\.") {
  133. gsub("^" section ".", "", update)
  134. cfg = cfg cmd2option(update) "\n"
  135. update = "-" section "." update
  136. }
  137. }
  138. if (update ~ "^@") {
  139. # new section
  140. section = stype = substr(update,2)
  141. gsub(/=.*$/,"",section)
  142. gsub(/^.*=/,"",stype)
  143. cfg = cfg "\nconfig \"" stype "\" \"" section "\"\n"
  144. }
  145. return cfg
  146. }