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.
 
 
 
 
 
 

216 lines
4.4 KiB

#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: bin/sde-config-ini
# Copyright (C) 2006 - 2007 The OpenSDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---
[ -n "$SDEROOT" ] ||
export SDEROOT=$( cd "${0%/*}/.."; pwd -P )
. $SDEROOT/lib/libsde.in
ini_usage() {
local progname=${0##*/}
cat <<EOT
Usage: $progname --file <file> --sections
$progname --file <file> --keys <section>
$progname --file <file> [--delete] section[.key] ...
$progname --file <file> section.key=value ...
Valid aliases:
--file (-F)
--sections (-S)
--keys (-K)
EOT
}
# splits an .ini file in three parts:
# * before the section ($tmpname.1)
# * the section ($tmpname.2)
# * after the section ($tmpname.3)
#
# USAGE: ini_split "file" "section" "tmpname"
#
ini_split() {
local file="$1" section="$2" tmpname="$3"
rm -rf $tmpfile.{0,1,2}
gawk "BEGIN { level=0; }
/^\[/ {
if ( level == 0 && \$0 ~ /^\[$section\][ \t]*\$/ )
level=1;
else if ( level == 1 )
level=2;
}
{ print > \"$tmpfile.\" level }
" "$file"
}
# dumps the raw content of a section
# USAGE: ini_section_raw "file" "section"
#
ini_section_raw() {
gawk "BEGIN { show=0; }
/^\[/ {
if ( \$0 ~ /^\[$2\][ \t]*\$/ )
show=1;
else
show=0;
}
/^[^\[]/ { if ( show ) print; }" "$1"
}
# this file has to be sourced only by tools which really want to
# write ini formated files. reading methods are available at libsde.in
# writes a list of variables into a section of an ini file
# USAGE: ini_write "file" "section" var1 var2 var3 ...
#
ini_write() {
local file="$1" section="$2" var= c=
local tmpfile=
shift 2
# something to write?
[ $# -gt 0 ] || return
# make sure the file exists
[ -f "$file" ] || touch "$file"
tmpfile="$file.$$"
# FIXME: a lock may be needed
if grep -q "^\[$section\][ \t]*$" "$file"; then
# known section
#
# split the file
ini_split_section "$file" "$section" "$tmpfile"
# remove empty lines
sed -i -e '/^[ \t]*$/d;' $tmpfile.1
# edit the section
for var; do
if grep -q "^$var=" $tmpfile.1; then
sed -i -e "s|^$var=.*|$var=$( eval echo \$$var )|" $tmpfile.1
else
echo "$var=$( eval echo \$$var )" >> $tmpfile.1
fi
done
# and finally write the new file!
(
cat $tmpfile.0
cat $tmpfile.1
if [ -s $tmpfile.2 ]; then
echo
cat $tmpfile.2
fi
) > "$file"
rm -rf $tmpfile.{0,1,2}
else
# new section
#
echo -e "\n[$section]" >> "$file"
for var; do
echo "$var=$( eval echo \$$var )"
done >> "$file"
fi
}
shortopts='F:SK:'
longopts='file:,sections,keys:,delete'
options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
if [ $? -ne 0 ]; then
ini_usage
exit -1
fi
# load new arguments list
eval set -- "$options"
if [ $# -lt 2 ]; then
ini_usage
exit -1
fi
file=
delete=
while [ $# -gt 0 ]; do
case "$1" in
-F|--file) shift; file="$1" ;;
--delete) delete=yes ;;
--) shift; break ;;
-S|--sections)
[ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
exec sed -n -e 's,^\[\(.*\)\][ \t]*$,\1,p' "$file"
;;
-K|--keys)
[ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
ini_section_raw "$file" "$2" |
sed -n -e 's,^[ \t]*\([^;= ]*\)[ \t]*=.*,\1,p'
exit $?
;;
esac
shift
done
if [ -z "$file" ]; then
echo_abort 1 "no ini file specified."
elif [ ! -r "$file" ]; then
echo_abort 1 "${file}: can't read."
fi
section= key= value= action=
for item; do
# parse 'item'
section="${item%%.*}"
value="${item#*=}"
key="${item#*.}"; key="${key%%=*}"
[ "$section" != "$key" ] || key=
# choose an action
if [ "$value" == "$item" ]; then
if [ -n "$delete" ]; then
action='delete'
else
action='get'
fi
else
action='set'
fi
case "$action" in
get) if [ -n "$key" ]; then
# just on key
ini_section_raw "$file" "$section" | sed -n \
-e "s,^[ \t]*$key[ \t]*=\(.*\),\1,p"
else
# the entire section, as variables
ini_section_raw "$file" "$section" | sed \
-e '/^[ \t]*;/d;' -e '/^[ \t]*$/d;' -e 's,",\\",g' -e 's,=\(..*\)$,="\1",'
fi ;;
set)
echo_abort 1 "Not yet implemented";
;;
delete)
echo_abort 1 "Not yet implemented";
;;
esac
done