123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #! /bin/sh
- # --- init ---
- # leave on error
- set -e
- # --- external ---
- basename='/bin/basename'
- test ! -x ${basename} && basename='/usr/bin/basename'
- egrep='/bin/egrep'
- test ! -x ${egrep} && egrep='/usr/bin/egrep'
- less='/bin/less'
- test ! -x ${less} && less='/usr/bin/less'
- test ! -x ${less} && less='/usr/local/bin/less'
- sed='/bin/sed'
- test ! -x ${sed} && sed='/usr/bin/sed'
- # --- definitions ---
- prog=`${basename} $0`
- # --- help ---
- DisplayHelp ()
- {
- ${xPAGER:-${less}} << EOF
- $prog General Objects
- NAME
- $prog - read data value from a resource file
-
- SYNOPSIS
- go_readrc <rcfile> <key> [<default>]
- go_readrc -h|--help
-
- DESCRIPTION
- A recourcefile is a text file with key/data pairs, usefull
- to initialize programs (scripts, Fortran, etc).
- The format of the <rcfile> is chosen close to the standard X resources:
- * Comment lines start with '!'
- * A key/data pair has the format:
- <key> : <value>
- where the white space (space or tabs) is optional.
- The <key> consists of letters, numbers, '_', and '.' .
-
- Example of a valid rcfile:
- ! Specify an output directory:
- output.path : d/
-
- Given a text <key>, the <rcfile> is scanned for a line starting
- with this key; all text behind the ':' is written to the standard output.
- Example of usage in sh script:
-
- output_root=\`go_readrc test.rc output.path\`
-
- If the <key> is not found, an error message is issued,
- unless a <default> is supplied which is then written to standard output.
- The <default> might be an empty string, e.g. '' .
-
- PREPROCESSING
- The rcfile might be preprocessed by go_pprc,
- to expand environment variables.
-
- EXIT STATUS
- Non zero in case of any error.
-
- SEE ALSO
- X, go_pprc
-
- AUTHOR
- Arjo Segers
- EOF
- exit 0
- }
- ErrorMessage ()
- {
- echo "ERROR in $prog: $1" 1>&2
- echo " Use '$prog -h' for information." 1>&2
- exit 1
- }
- # --- arguments ---
- rcfile=''
- rckey=''
- with_default=''
- while [ $# -gt 0 ]; do
- case "$1" in
- -h | --help )
- DisplayHelp
- ;;
- -* )
- ErrorMessage "unknown option '$1' ..."
- ;;
- * )
- if [ -z "${rcfile}" ]; then
- rcfile="$1"
- elif [ -z "${rckey}" ]; then
- rckey="$1"
- elif [ -z "${with_default}" ]; then
- default="$1"
- with_default='true'
- else
- ErrorMessage "unknown argument '$1'"
- fi
- ;;
- esac
- shift
- done
- if [ -z "${rcfile}" -o -z "${rckey}" ]; then
- ErrorMessage "missing arguments"
- fi
- # --- begin ---
- # does the rcfile exist?
- if [ ! -f ${rcfile} ]; then
- ErrorMessage "rcfile '${rcfile}' does not exist ..."
- fi
- # replace '.' in the rckey by '\.'
- rckeydots=`echo ${rckey} | ${sed} -e 's/\./\\\\./g'`
- # 10 Apr 06: Andy Jacobson
- # [[:space:]] indicates a space or tab character
- #wspace='[[:space:]]*'
- #
- # 26 Apr 06: Arjo Segers
- # The egrep on SGI system does not support the '[:space:]' ;
- # use a real tab character instead ...
- tab=' '
- wspace="[ ${tab}]*"
- # A key-data line has the following synopsis:
- #
- # <begin-of-line><key>[<wspace>]:[<wspace>]<data>
- #
- # where <wspace> denote tabs or spaces.
- # Set regular expression for such a line except the <data> part;
- # this expression is used to search for a key and to extract
- # the data part:
- #
- re="^${rckeydots}${wspace}:${wspace}"
- # set grep command to select matching lines:
- selectlinecmd="${egrep} '${re}' ${rcfile}"
- # count number of hits; should be exactely 1 ...
- nfound=`eval "${selectlinecmd}" | /usr/bin/wc -l`
- if [ ${nfound} -eq 0 ]; then
- if [ -z "${with_default}" ]; then
- ErrorMessage "key '${rckey}' not found in ${rcfile} and no default specified ..."
- else
- echo "${default}"
- exit 0
- fi
- elif [ ${nfound} -gt 1 ]; then
- ErrorMessage "key '${rckey}' found ${nfound} times in $rcfile ..."
- fi
- # extract the data part for this key;
- # substitute an empty string for the 'key : ' part;
- # remove trailing blanks;
- # output is written to standard output:
- eval "${selectlinecmd}" | ${sed} -e "s/${re}//" -e "s/${wspace}$//"
|