Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#! /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}$//"