#! /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 [] 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 is chosen close to the standard X resources: * Comment lines start with '!' * A key/data pair has the format: : where the white space (space or tabs) is optional. The consists of letters, numbers, '_', and '.' . Example of a valid rcfile: ! Specify an output directory: output.path : d/ Given a text , the 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 is not found, an error message is issued, unless a is supplied which is then written to standard output. The 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: # # []:[] # # where denote tabs or spaces. # Set regular expression for such a line except the 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}$//"