2 # --- SDE-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 # Filename: bin/sde-config-ini
6 # Copyright (C) 2006 - 2008 The OpenSDE Project
8 # More information can be found in the files COPYING and README.
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 ---
17 export SDEROOT
=$
( cd "${0%/*}/.."; pwd -P )
19 .
$SDEROOT/lib
/libsde.
in
22 local progname
=${0##*/}
24 Usage: $progname --file <file> --sections
25 $progname --file <file> --keys <section>
26 $progname --file <file> [--delete] section[.key] ...
27 $progname --file <file> section[[.key]=value] ...
36 # splits an .ini file in three parts:
37 # * before the section ($tmpname.0)
38 # * the section ($tmpname.1)
39 # * after the section ($tmpname.2)
41 # USAGE: ini_split "file" "section" "tmpname"
44 local file="$1" section
="$2" tmpname
="$3"
46 rm -rf $tmpname.
{0,1,2}
48 if [ ! -e "$file" ]; then
49 # create the file if it's not there
50 touch "$file" || echo_abort
1 "$file: can't write."
53 [ -f "$file" -a -w "$file" ] || echo_abort
1 "$file: can't write."
55 gawk
"BEGIN { level=0; }
57 if ( level == 0 && \$0 ~ /^\[$section\][ \t]*\$/ )
59 else if ( level == 1 )
62 { print > \"$tmpname.\" level }
65 # clean empty lines on the section to alter
66 [ ! -s "$tmpname.1" ] ||
sed -i -e "/^[ \t]*$/d;" "$tmpname.1"
70 # merge a previously split .ini file
71 # USAGE: ini_merge "file" "section" "tmpname"
74 local file="$1" tmpname
="$2"
77 # new empty line at the end of the section, if there is a next
78 if [ -s "$tmpname.1" -a -s "$tmpname.2" ]; then
83 if [ -s $tmpname.
$x ]; then
88 rm -rf $tmpname.
{0,1,2}
91 # dumps the raw content of a section
92 # USAGE: ini_section_raw "file" "section"
95 gawk
"BEGIN { show=0; }
97 if ( \$0 ~ /^\[$2\][ \t]*\$/ )
102 /^[^\[]/ { if ( show ) print; }" "$1"
106 longopts
='file:,sections,keys:,delete'
107 options
=$
( getopt
-o "$shortopts" -l "$longopts" -- "$@" )
108 if [ $?
-ne 0 ]; then
113 # load new arguments list
114 eval set -- "$options"
116 if [ $# -lt 2 ]; then
123 while [ $# -gt 0 ]; do
125 -F|
--file) shift; file="$1" ;;
126 --delete) delete
=yes ;;
131 [ -r "$file" ] || echo_abort
1 "${file:-none}: can't read."
133 exec sed -n -e 's,^\[\(.*\)\][ \t]*$,\1,p' "$file"
136 [ -r "$file" ] || echo_abort
1 "${file:-none}: can't read."
138 ini_section_raw
"$file" "$2" |
139 sed -n -e 's,^[ \t]*\([^;= ]*\)[ \t]*=.*,\1,p'
146 if [ -z "$file" ]; then
147 echo_abort
1 "no ini file specified."
152 section
= key
= value
= action
=
156 section
="${item%%.*}"
158 key
="${item#*.}"; key
="${key%%=*}"
160 [ "$section" != "$key" ] || key
=
163 if [ "$value" = "$item" ]; then
164 if [ -n "$delete" ]; then
174 get
) [ -r "$file" ] || echo_abort
1 "${file:-none}: can't read."
176 if [ -n "$key" ]; then
178 ini_section_raw
"$file" "$section" |
sed -n \
179 -e "s,^[ \t]*$key[ \t]*=\(.*\),\1,p"
181 # the entire section, as variables
182 ini_section_raw
"$file" "$section" |
sed \
183 -e '/^[ \t]*;/d;' -e '/^[ \t]*$/d;' -e 's,",\\",g' -e 's,=\(..*\)$,="\1",'
186 if [ -n "$oldsection" -a "$oldsection" != "$section" ]; then
187 # close previous split
188 ini_merge
"$file" "$tmpfile"
192 if [ -z "$oldsection" ]; then
193 # split before mangling
194 ini_split
"$file" "$section" "$tmpfile"
195 oldsection
="$section"
198 if [ ! -s "$tmpfile.1" ]; then
200 if [ -s "$tmpfile.0" ]; then
201 # empty line at the end of the last section
204 echo -e "[$section]" >> "$tmpfile.1"
207 if grep -q "^[ \t]*$key[ \t]*=" $tmpfile.1; then
208 sed -i -e "s|^[ \t]*$key[ \t]*=.*|$key=$value|" "$tmpfile.1"
210 echo "$key=$value" >> "$tmpfile.1"
214 if [ -n "$oldsection" -a "$oldsection" != "$section" ]; then
215 # close previous split
216 ini_merge
"$file" "$tmpfile"
220 if [ -z "$oldsection" ]; then
221 # split before mangling
222 ini_split
"$file" "$section" "$tmpfile"
223 oldsection
="$section"
226 if [ -n "$key" ]; then
228 grep -v "^[ \t]*$key[ \t]*=" "$tmpfile.1" > "$tmpfile.1+"
229 mv -f "$tmpfile.1+" "$tmpfile.1"
232 echo "[$section]" > "$tmpfile.1"
238 if [ -n "$oldsection" ]; then
239 ini_merge
"$file" "$tmpfile"