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.
 
 
 
 
 
 

183 lines
3.9 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> [OPTIONS]
Options:
[-s|--set] section.key=value
[-g|--get] section.key
[-d|--delete] section[.key]
[-S|--sections]
[-K|--keys] section
EOT
}
# 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"
}
# dumps the content of a section assuming variables are defined in there
# USAGE: ini_section_read "file" "section"
#
ini_section_read() {
ini_section_raw "$1" "$2" |
sed -e '/^[ \t]*;/d;' -e '/^[ \t]*$/d;' -e 's,",\\",g' -e 's,=\(..*\)$,="\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
#
#FIXME: better performance may be required
# split the file
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"
# 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
}
ini_do_set() { echo_abort 1 "Not yet implemented"; }
ini_do_get() {
local section="${1%%.*}"
local key="${1#*.}"
if [ "$key" != "$section" ]; then
# just one key
ini_section_raw "$file" "$section" | sed -n \
-e "s,^[ \t]*$key[ \t]*=\(.*\),\1,p"
else
ini_section_read "$file" "$section"
fi
}
ini_do_delete() { echo_abort 1 "Not yet implemented"; }
shortopts='F:s:g:d:SK:'
longopts='file:,set:,get:,delete:,sections,keys:'
options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
if [ $? -ne 0 ]; then
ini_usage
exit -1
fi
# load new arguments list
eval set -- "$options"
[ $# -ge 2 ] || ini_usage
file=
while [ $# -gt 0 ]; do
case "$1" in
-F|--file) shift; file="$1" ;;
--) break ;;
*) [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
case "$1" in
-s|--set) shift; ini_do_set "$1"
;;
-g|--get) shift; ini_do_get "$1"
;;
-d|--delete) shift; ini_do_delete "$1"
;;
-S|--sections)
sed -n -e 's,^\[\(.*\)\][ \t]*$,\1,p' "$file"
;;
-K|--keys) shift;
ini_section_raw "$file" "$1" |
sed -n -e 's,^[ \t]*\([^;= ]*\)[ \t]*=.*,\1,p'
;;
esac
esac
shift
done