OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
5.1 KiB

  1. #!/bin/sh
  2. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # Filename: bin/sde-config-ini
  6. # Copyright (C) 2006 - 2008 The OpenSDE Project
  7. #
  8. # More information can be found in the files COPYING and README.
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; version 2 of the License. A copy of the
  13. # GNU General Public License can be found in the file COPYING.
  14. # --- SDE-COPYRIGHT-NOTE-END ---
  15. [ -n "$SDEROOT" ] ||
  16. export SDEROOT=$( cd "${0%/*}/.."; pwd -P )
  17. . $SDEROOT/lib/libsde.in
  18. ini_usage() {
  19. local progname=${0##*/}
  20. cat <<EOT
  21. Usage: $progname --file <file> --sections
  22. $progname --file <file> --keys <section>
  23. $progname --file <file> [--delete] section[.key] ...
  24. $progname --file <file> section[[.key]=value] ...
  25. Valid aliases:
  26. --file (-F)
  27. --sections (-S)
  28. --keys (-K)
  29. EOT
  30. }
  31. # splits an .ini file in three parts:
  32. # * before the section ($tmpname.0)
  33. # * the section ($tmpname.1)
  34. # * after the section ($tmpname.2)
  35. #
  36. # USAGE: ini_split "file" "section" "tmpname"
  37. #
  38. ini_split() {
  39. local file="$1" section="$2" tmpname="$3"
  40. rm -rf $tmpname.{0,1,2}
  41. if [ ! -e "$file" ]; then
  42. # create the file if it's not there
  43. touch "$file" || echo_abort 1 "$file: can't write."
  44. fi
  45. [ -f "$file" -a -w "$file" ] || echo_abort 1 "$file: can't write."
  46. gawk "BEGIN { level=0; }
  47. /^\[/ {
  48. if ( level == 0 && \$0 ~ /^\[$section\][ \t]*\$/ )
  49. level=1;
  50. else if ( level == 1 )
  51. level=2;
  52. }
  53. { print > \"$tmpname.\" level }
  54. " "$file"
  55. # clean empty lines on the section to alter
  56. [ ! -s "$tmpname.1" ] || sed -i -e "/^[ \t]*$/d;" "$tmpname.1"
  57. }
  58. # merge a previously split .ini file
  59. # USAGE: ini_merge "file" "section" "tmpname"
  60. #
  61. ini_merge() {
  62. local file="$1" tmpname="$2"
  63. local x=
  64. # new empty line at the end of the section, if there is a next
  65. if [ -s "$tmpname.1" -a -s "$tmpname.2" ]; then
  66. echo >> "$tmpname.1"
  67. fi
  68. for x in 0 1 2; do
  69. if [ -s $tmpname.$x ]; then
  70. cat $tmpname.$x
  71. fi
  72. done > "$file"
  73. rm -rf $tmpname.{0,1,2}
  74. }
  75. # dumps the raw content of a section
  76. # USAGE: ini_section_raw "file" "section"
  77. #
  78. ini_section_raw() {
  79. gawk "BEGIN { show=0; }
  80. /^\[/ {
  81. if ( \$0 ~ /^\[$2\][ \t]*\$/ )
  82. show=1;
  83. else
  84. show=0;
  85. }
  86. /^[^\[]/ { if ( show ) print; }" "$1"
  87. }
  88. shortopts='F:SK:'
  89. longopts='file:,sections,keys:,delete'
  90. options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
  91. if [ $? -ne 0 ]; then
  92. ini_usage
  93. exit -1
  94. fi
  95. # load new arguments list
  96. eval set -- "$options"
  97. if [ $# -lt 2 ]; then
  98. ini_usage
  99. exit -1
  100. fi
  101. file=
  102. delete=
  103. while [ $# -gt 0 ]; do
  104. case "$1" in
  105. -F|--file) shift; file="$1" ;;
  106. --delete) delete=yes ;;
  107. --) shift; break ;;
  108. -S|--sections)
  109. [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
  110. exec sed -n -e 's,^\[\(.*\)\][ \t]*$,\1,p' "$file"
  111. ;;
  112. -K|--keys)
  113. [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
  114. ini_section_raw "$file" "$2" |
  115. sed -n -e 's,^[ \t]*\([^;= ]*\)[ \t]*=.*,\1,p'
  116. exit $?
  117. ;;
  118. esac
  119. shift
  120. done
  121. if [ -z "$file" ]; then
  122. echo_abort 1 "no ini file specified."
  123. fi
  124. oldsection=
  125. tmpfile="$file.$$"
  126. section= key= value= action=
  127. for item; do
  128. # parse 'item'
  129. section="${item%%.*}"
  130. value="${item#*=}"
  131. key="${item#*.}"; key="${key%%=*}"
  132. [ "$section" != "$key" ] || key=
  133. # choose an action
  134. if [ "$value" = "$item" ]; then
  135. if [ -n "$delete" ]; then
  136. action='delete'
  137. else
  138. action='get'
  139. fi
  140. else
  141. action='set'
  142. fi
  143. case "$action" in
  144. get) [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
  145. if [ -n "$key" ]; then
  146. # just on key
  147. ini_section_raw "$file" "$section" | sed -n \
  148. -e "s,^[ \t]*$key[ \t]*=\(.*\),\1,p"
  149. else
  150. # the entire section, as variables
  151. ini_section_raw "$file" "$section" | sed \
  152. -e '/^[ \t]*;/d;' -e '/^[ \t]*$/d;' -e 's,",\\",g' -e 's,=\(..*\)$,="\1",'
  153. fi ;;
  154. set)
  155. if [ -n "$oldsection" -a "$oldsection" != "$section" ]; then
  156. # close previous split
  157. ini_merge "$file" "$tmpfile"
  158. oldsection=
  159. fi
  160. if [ -z "$oldsection" ]; then
  161. # split before mangling
  162. ini_split "$file" "$section" "$tmpfile"
  163. oldsection="$section"
  164. fi
  165. if [ ! -s "$tmpfile.1" ]; then
  166. # new section
  167. if [ -s "$tmpfile.0" ]; then
  168. # empty line at the end of the last section
  169. echo >> "$tmpfile.0"
  170. fi
  171. $ECHO_E "[$section]" >> "$tmpfile.1"
  172. fi
  173. if grep -q "^[ \t]*$key[ \t]*=" $tmpfile.1; then
  174. sed -i -e "s|^[ \t]*$key[ \t]*=.*|$key=$value|" "$tmpfile.1"
  175. else
  176. echo "$key=$value" >> "$tmpfile.1"
  177. fi
  178. ;;
  179. delete)
  180. if [ -n "$oldsection" -a "$oldsection" != "$section" ]; then
  181. # close previous split
  182. ini_merge "$file" "$tmpfile"
  183. oldsection=
  184. fi
  185. if [ -z "$oldsection" ]; then
  186. # split before mangling
  187. ini_split "$file" "$section" "$tmpfile"
  188. oldsection="$section"
  189. fi
  190. if [ -n "$key" ]; then
  191. # just on key
  192. grep -v "^[ \t]*$key[ \t]*=" "$tmpfile.1" > "$tmpfile.1+"
  193. mv -f "$tmpfile.1+" "$tmpfile.1"
  194. else
  195. # the entire section
  196. echo "[$section]" > "$tmpfile.1"
  197. fi
  198. ;;
  199. esac
  200. done
  201. if [ -n "$oldsection" ]; then
  202. ini_merge "$file" "$tmpfile"
  203. fi