go_readrc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #! /bin/sh
  2. # --- init ---
  3. # leave on error
  4. set -e
  5. # --- external ---
  6. basename='/bin/basename'
  7. test ! -x ${basename} && basename='/usr/bin/basename'
  8. egrep='/bin/egrep'
  9. test ! -x ${egrep} && egrep='/usr/bin/egrep'
  10. less='/bin/less'
  11. test ! -x ${less} && less='/usr/bin/less'
  12. test ! -x ${less} && less='/usr/local/bin/less'
  13. sed='/bin/sed'
  14. test ! -x ${sed} && sed='/usr/bin/sed'
  15. # --- definitions ---
  16. prog=`${basename} $0`
  17. # --- help ---
  18. DisplayHelp ()
  19. {
  20. ${xPAGER:-${less}} << EOF
  21. $prog General Objects
  22. NAME
  23. $prog - read data value from a resource file
  24. SYNOPSIS
  25. go_readrc <rcfile> <key> [<default>]
  26. go_readrc -h|--help
  27. DESCRIPTION
  28. A recourcefile is a text file with key/data pairs, usefull
  29. to initialize programs (scripts, Fortran, etc).
  30. The format of the <rcfile> is chosen close to the standard X resources:
  31. * Comment lines start with '!'
  32. * A key/data pair has the format:
  33. <key> : <value>
  34. where the white space (space or tabs) is optional.
  35. The <key> consists of letters, numbers, '_', and '.' .
  36. Example of a valid rcfile:
  37. ! Specify an output directory:
  38. output.path : d/
  39. Given a text <key>, the <rcfile> is scanned for a line starting
  40. with this key; all text behind the ':' is written to the standard output.
  41. Example of usage in sh script:
  42. output_root=\`go_readrc test.rc output.path\`
  43. If the <key> is not found, an error message is issued,
  44. unless a <default> is supplied which is then written to standard output.
  45. The <default> might be an empty string, e.g. '' .
  46. PREPROCESSING
  47. The rcfile might be preprocessed by go_pprc,
  48. to expand environment variables.
  49. EXIT STATUS
  50. Non zero in case of any error.
  51. SEE ALSO
  52. X, go_pprc
  53. AUTHOR
  54. Arjo Segers
  55. EOF
  56. exit 0
  57. }
  58. ErrorMessage ()
  59. {
  60. echo "ERROR in $prog: $1" 1>&2
  61. echo " Use '$prog -h' for information." 1>&2
  62. exit 1
  63. }
  64. # --- arguments ---
  65. rcfile=''
  66. rckey=''
  67. with_default=''
  68. while [ $# -gt 0 ]; do
  69. case "$1" in
  70. -h | --help )
  71. DisplayHelp
  72. ;;
  73. -* )
  74. ErrorMessage "unknown option '$1' ..."
  75. ;;
  76. * )
  77. if [ -z "${rcfile}" ]; then
  78. rcfile="$1"
  79. elif [ -z "${rckey}" ]; then
  80. rckey="$1"
  81. elif [ -z "${with_default}" ]; then
  82. default="$1"
  83. with_default='true'
  84. else
  85. ErrorMessage "unknown argument '$1'"
  86. fi
  87. ;;
  88. esac
  89. shift
  90. done
  91. if [ -z "${rcfile}" -o -z "${rckey}" ]; then
  92. ErrorMessage "missing arguments"
  93. fi
  94. # --- begin ---
  95. # does the rcfile exist?
  96. if [ ! -f ${rcfile} ]; then
  97. ErrorMessage "rcfile '${rcfile}' does not exist ..."
  98. fi
  99. # replace '.' in the rckey by '\.'
  100. rckeydots=`echo ${rckey} | ${sed} -e 's/\./\\\\./g'`
  101. # 10 Apr 06: Andy Jacobson
  102. # [[:space:]] indicates a space or tab character
  103. #wspace='[[:space:]]*'
  104. #
  105. # 26 Apr 06: Arjo Segers
  106. # The egrep on SGI system does not support the '[:space:]' ;
  107. # use a real tab character instead ...
  108. tab=' '
  109. wspace="[ ${tab}]*"
  110. # A key-data line has the following synopsis:
  111. #
  112. # <begin-of-line><key>[<wspace>]:[<wspace>]<data>
  113. #
  114. # where <wspace> denote tabs or spaces.
  115. # Set regular expression for such a line except the <data> part;
  116. # this expression is used to search for a key and to extract
  117. # the data part:
  118. #
  119. re="^${rckeydots}${wspace}:${wspace}"
  120. # set grep command to select matching lines:
  121. selectlinecmd="${egrep} '${re}' ${rcfile}"
  122. # count number of hits; should be exactely 1 ...
  123. nfound=`eval "${selectlinecmd}" | /usr/bin/wc -l`
  124. if [ ${nfound} -eq 0 ]; then
  125. if [ -z "${with_default}" ]; then
  126. ErrorMessage "key '${rckey}' not found in ${rcfile} and no default specified ..."
  127. else
  128. echo "${default}"
  129. exit 0
  130. fi
  131. elif [ ${nfound} -gt 1 ]; then
  132. ErrorMessage "key '${rckey}' found ${nfound} times in $rcfile ..."
  133. fi
  134. # extract the data part for this key;
  135. # substitute an empty string for the 'key : ' part;
  136. # remove trailing blanks;
  137. # output is written to standard output:
  138. eval "${selectlinecmd}" | ${sed} -e "s/${re}//" -e "s/${wspace}$//"