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.

171 lines
3.7 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 - 2007 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> [OPTIONS]
  22. Options:
  23. [-s|--set] section.key=value
  24. [-g|--get] section.key
  25. [-d|--delete] section[.key]
  26. [-S|--sections]
  27. [-K|--keys section]
  28. EOT
  29. }
  30. # dumps the raw content of a section
  31. # USAGE: ini_section_raw "file" "section"
  32. #
  33. ini_section_raw() {
  34. gawk "BEGIN { show=0; }
  35. /^\[/ {
  36. if ( \$0 ~ /^\[$2\][ \t]*\$/ )
  37. show=1;
  38. else
  39. show=0;
  40. }
  41. /^[^\[]/ { if ( show ) print; }" "$1"
  42. }
  43. # dumps the content of a section assuming variables are defined in there
  44. # USAGE: ini_section_read "file" "section"
  45. #
  46. ini_section_read() {
  47. ini_section_raw "$1" "$2" |
  48. sed -e '/^[ \t]*;/d;' -e '/^[ \t]*$/d;' -e 's,",\\",g' -e 's,=\(..*\)$,="\1",'
  49. }
  50. # this file has to be sourced only by tools which really want to
  51. # write ini formated files. reading methods are available at libsde.in
  52. # writes a list of variables into a section of an ini file
  53. # USAGE: ini_write "file" "section" var1 var2 var3 ...
  54. #
  55. ini_write() {
  56. local file="$1" section="$2" var= c=
  57. local tmpfile=
  58. shift 2
  59. # something to write?
  60. [ $# -gt 0 ] || return
  61. # make sure the file exists
  62. [ -f "$file" ] || touch "$file"
  63. tmpfile="$file.$$"
  64. # FIXME: a lock may be needed
  65. if grep -q "^\[$section\][ \t]*$" "$file"; then
  66. # known section
  67. #
  68. #FIXME: better performance may be required
  69. # split the file
  70. rm -rf $tmpfile.{0,1,2}
  71. gawk "BEGIN { level=0; }
  72. /^\[/ {
  73. if ( level == 0 && \$0 ~ /^\[$section\][ \t]*\$/ )
  74. level=1;
  75. else if ( level == 1 )
  76. level=2;
  77. }
  78. { print > \"$tmpfile.\" level }
  79. " "$file"
  80. # remove empty lines
  81. sed -i -e '/^[ \t]*$/d;' $tmpfile.1
  82. # edit the section
  83. for var; do
  84. if grep -q "^$var=" $tmpfile.1; then
  85. sed -i -e "s|^$var=.*|$var=$( eval echo \$$var )|" $tmpfile.1
  86. else
  87. echo "$var=$( eval echo \$$var )" >> $tmpfile.1
  88. fi
  89. done
  90. # and finally write the new file!
  91. (
  92. cat $tmpfile.0
  93. cat $tmpfile.1
  94. if [ -s $tmpfile.2 ]; then
  95. echo
  96. cat $tmpfile.2
  97. fi
  98. ) > "$file"
  99. rm -rf $tmpfile.{0,1,2}
  100. else
  101. # new section
  102. #
  103. echo -e "\n[$section]" >> "$file"
  104. for var; do
  105. echo "$var=$( eval echo \$$var )"
  106. done >> "$file"
  107. fi
  108. }
  109. ini_do_set() { echo_abort 1 "Not yet implemented"; }
  110. ini_do_get() { echo_abort 1 "Not yet implemented"; }
  111. ini_do_delete() { echo_abort 1 "Not yet implemented"; }
  112. shortopts='F:s:g:d:SK:'
  113. longopts='file:,set:,get:,delete:,sections,keys:'
  114. options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
  115. if [ $? -ne 0 ]; then
  116. ini_usage
  117. exit -1
  118. fi
  119. # load new arguments list
  120. eval set -- "$options"
  121. [ $# -ge 2 ] || ini_usage
  122. file=
  123. while [ $# -gt 0 ]; do
  124. case "$1" in
  125. -F|--file) shift; file="$1" ;;
  126. --) break ;;
  127. *) [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
  128. case "$1" in
  129. -s|--set) shift; ini_do_set "$1"
  130. ;;
  131. -g|--get) shift; ini_do_get "$1"
  132. ;;
  133. -d|--delete) shift; ini_do_delete "$1"
  134. ;;
  135. -S|--sections)
  136. sed -n -e 's,^\[\(.*\)\][ \t]*$,\1,p' "$file"
  137. ;;
  138. -K|--keys) shift;
  139. ini_section_raw "$file" "$1" |
  140. sed -n -e 's,^[ \t]*\([^;= ]*\)[ \t]*=.*,\1,p'
  141. ;;
  142. esac
  143. esac
  144. shift
  145. done