File: ooconfig

package info (click to toggle)
oo2c 1.5.9-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,312 kB
  • ctags: 727
  • sloc: ansic: 4,679; sh: 523; makefile: 368; perl: 66; lisp: 20
file content (348 lines) | stat: -rwxr-xr-x 7,893 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/sh
# This script manipulates OOC initialization files to install new packages.
# 
# Copyright (C) 1998, 1999, 2001  Michael van Acken
# 
# You can redistribute this file it and/or modify it under the terms
# of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any
# later version.


# See usage description below for synopsis and list of commands.
# 
# Note: For this script to work, all section keywords and the keyword
# END have to appear on a line of their own in the intialization file.
# An INCLUDE section has to be written on a single line.


if test $# = 0; then
    cat <<\EOF
usage: ooconfig <file> [command]... 

commands:
--append-line <key> <value>  Insert the line <value> at the end of the section
                             denoted by <key>.
--prepend-line <key> <value> Insert the line <value> at the beginning of the 
                             section denoted by <key>.
--replace-line <key> <value> Replace line matching <key> with <value>.  Do 
                             nothing if no matching line exists.
--remove-line <wildcard>     Remove all lines whose key matches <wildcard>.
--get-line <wildcard>        Retrieve all lines whose key matches <wildcard>.
--get-value <key>            Retrieve the line whose key matches <key>, but 
                             strip leading whitespace and key from output.
--exists <wildcard>          Exit status is 0, if a line with a matching key 
                             exists, and 1 otherwise.
--define <key> <yes/no>      Append line "DEFINE PACKAGE_ID := TRUE/FALSE;".
--fix-includes               Comment out any INCLUDE statements that include a
                             relative path name, or a path name that depends on
                             the current user.
--stdout                     Do not modify file, and write result to stdout.
--file <file>                Read commands from file.

A <key> must be of the form `SECTION:PACKAGE:ID'.  Identifiers of a key must be
composed of characters, underscore, or digits.  A <wildcard> can additionally 
contain the characters `?' (matches any character) and `*' (matches an 
arbitrary number of characters).
EOF
    exit 2
fi

if test -r "$1"; then
    true
else
    echo "error: file $1 does not exist or is not readable"
    exit 2
fi

########################################################################

ws="[ 	]*" # whitespace regular expression


set_key_parts ()
{
    key_section="$1"
    key_package="$2"
    key_id="$3"
}

split_key ()
# Assign the parts of the key to the variables key_section, key_packges, amd
# key_id.
{
    OLD_IFS="$IFS"
    IFS=":"
    set_key_parts $1
    IFS="$OLD_IFS"
}

check_key ()
# If $1 is no valid key, abort with an error message and exit code 2.
{
    ident="[a-zA-Z_][a-zA-Z0-9_]*"
    if echo "$1" | grep "^$ident:$ident:$ident$" >/dev/null; then
	return 0
    else
	echo "error: invalid key $1"
	exit 2
    fi
}

check_wildcard ()
# If $1 is no valid wildcard, abort with an error message and exit code 2.
{
    wc="[a-zA-Z_*?][a-zA-Z0-9_*?]*"
    if echo "$1" | grep "^$wc:$wc:$wc$" >/dev/null; then
	return 0
    else
	echo "error: invalid wildcard $1"
	exit 2
    fi
}

transform_wildcard ()
# Transform the wildcard string in $1 into a valid regular 
# expression, then write it to stdout.
{
    echo "$1" | sed -e 's/\?/./g' -e 's/\*/.*/g' -e 's/^/##/g' -e 's/$/##/g'
}


append_line ()
# Before end of section $1, insert line $2 with key $3.
{
    cat >conftest.sed <<EOF
/^${ws}${1}${ws}$/,/^${ws}END${ws}$/{
/^${ws}END${ws}$/!p
/^${ws}END${ws}$/{
i\\
${2}  ##${3}##
p
}
}
/^${ws}${1}${ws}$/,/^${ws}END${ws}$/!p
EOF
    grep -v "##$3##" | sed -n -f conftest.sed
    rm -f conftest.sed
}

prepend_line ()
# At the beginning of section $1, insert line $2 with key $3.
{
    cat >conftest.sed <<EOF
/^${ws}${1}${ws}$/{
p
i\\
${2}  ##${3}##
}
/^${ws}${1}${ws}$/!p
EOF
    grep -v "##$3##" | sed -n -f conftest.sed
    rm -f conftest.sed
}

replace_line ()
# Replace line with key $2 with $1.  Do nothing if no matching line exists.
{
    sed -e "s/^[^#]*##${2}##${ws}$/  ${1}  ##${2}##/"
}


fix_includes ()
# Comment out any INCLUDE statements that include a relative path name, or
# a path name that depends on the current user.
{
    sed -e "/^${ws}INCLUDE${ws}~\\//s/^/#/" \
        -e "/^${ws}INCLUDE${ws}\\.\//s/^/#/" \
        -e "/^${ws}INCLUDE${ws}\\.\\.\//s/^/#/"
}

remove_line ()
# Remove all lines that do no match the regular expression $1.
{
    grep -v "$1"
}

perform_operation ()
# Perform operation $1 on the current file.
{
    if test "$curr_input" = "$init_file"; then
	if cp "$init_file" "$acc_file"; then
	    curr_input="$acc_file"
	else
	    echo "could not create file $acc_file"
	    exit 2
	fi
    fi
    
    mv "${acc_file}" "${tmp_file}"
    if $1 "$2" "$3" "$4" <"${tmp_file}" >"${acc_file}"; then
	rm -f "$tmp_file"
    else
	exit 2
    fi
}

########################################################################

do_append_line ()
{
	check_key "$2"
	split_key "$2"
	perform_operation append_line "$key_section" "$3" "$2"
}

do_prepend_line ()
{
	check_key "$2"
	split_key "$2"
	perform_operation prepend_line "$key_section" "$3" "$2"
}

do_replace_line ()
{
	check_key "$2"
	perform_operation replace_line "$3" "$2"
}

do_remove_line ()
{
	check_wildcard "$2"
	perform_operation remove_line "`transform_wildcard "$2"`"
}

do_get_line ()
{
	check_wildcard "$2"
	grep "`transform_wildcard "$2"`" "$curr_input"
}

do_get_value ()
{
	check_key "$2"
	grep "$2" "$curr_input" | sed -e "s/^ *//g" -e "s/ *##$2##//g"
}

do_define ()
{
	check_key "$2"
	split_key "$2"
	if test x"$3" = xyes; then
	    perform_operation append_line "$key_section" \
		"DEFINE ${key_package}_${key_id} := TRUE;" "$2"
	else
	    perform_operation append_line "$key_section" \
		"DEFINE ${key_package}_${key_id} := FALSE;" "$2"
	fi
}

do_fix_includes ()
{
	perform_operation fix_includes
}

eval_line ()
{
    case $1 in
    append-line)
	do_append_line "$1" "$2" "$3" ;;
    prepend-line)
	do_prepend_line "$1" "$2" "$3" ;;
    replace-line)
	do_replace_line "$1" "$2" "$3" ;;
    remove-line)
	do_remove_line "$1" "$2" ;;
    get-line)
	do_get_line "$1" "$2" ;;
    get-value)
	do_get_value "$1" "$2" ;;
    define)
	do_define "$1" "$2" "$3" ;;
    fix-includes)
	do_fix_includes "$1" ;;
    *)
	echo "error: unknown command $1"
	exit 2;;
    esac
}

scan_file ()
{
    OLD_IFS="$IFS"
    IFS="
"
    for i in `cat $1`; do
	OLD_IFS="$IFS"
        eval eval_line $i
    done
    IFS="$OLD_IFS"
}

########################################################################

init_file="$1"
shift

acc_file="$init_file.acc"
tmp_file="$init_file.tmp"
curr_input="$init_file"
write_stdout=no

rm -f "$acc_file" "$tmp_file"

while test $# != 0; do
    case $1 in
    --append-line)
	do_append_line "$1" "$2" "$3"
	shift 3 ;;
    --prepend-line)
	do_prepend_line "$1" "$2" "$3"
	shift 3 ;;
    --replace-line)
	do_replace_line "$1" "$2" "$3"
	shift 3 ;;
    --remove-line)
	do_remove_line "$1" "$2"
	shift 2 ;;
    --get-line)
	do_get_line "$1" "$2"
	shift 2 ;;
    --get-value)
	do_get_value "$1" "$2"
	shift 2 ;;
    --define)
	do_define "$1" "$2" "$3"
	shift 3 ;;
    --fix-includes)
	do_fix_includes "$1"
	shift 1 ;;
    --file)
        scan_file "$2"
        shift 2 ;;
    --exists)
	check_wildcard "$2"
	grep "`transform_wildcard "$2"`" "$curr_input" >/dev/null
	exit $? ;;
    --stdout)
	write_stdout=yes;
	shift 1 ;;
     *)
	echo "error: unknown command $1"
	exit 2 ;;
    esac
done

if test "$curr_input" != "acc_file"; then  # file has been modified
    if test $write_stdout = yes; then
	cat "$acc_file"
	rm -f "$acc_file"
    else
	mv "$init_file" "$init_file.orig"
	mv "$acc_file" "$init_file"
    fi
elif test $write_stdout = yes; then  # no modification
    cat "$init_file"
fi

exit 0