-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.awk
86 lines (67 loc) · 2.87 KB
/
config.awk
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
# awkbot/config.awk - A library for simple configuration file parsing
# -----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 43) borrowed from FreeBSD's jail.c:
# <tag@cpan.org> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Scott S. McCoy
# -----------------------------------------------------------------------------
# deps: assert.awk
function config_load (filename) {
_config["config", "filename"] = filename
config_parse(_config, 0, filename)
}
# Recurse over new levels
function config_parse (config_data,level,filename ,l,t,s,current,closing) {
if (config_data["debug"])
printf "config_parse(ARRAY, %s, %s)\n", level, filename
while ( getline < filename ) {
# Strip coments
sub(/^[\t ]*#.*$/, "")
if (config_data["debug"]) {
printf "read:%s:%d %s\n", filename, NR, $0 > "/dev/stderr"
print "Current namespace", level > "/dev/stderr"
}
if (/<[^\/][^>]*>/) {
# 3 arg match equil
# match($0, /<([^\/][^>]*)>/, l)
s = substr($0, RSTART + 1, RLENGTH - 2)
if (config_data["debug"]) print "Opening: ", s > "/dev/stderr"
if (level) config_parse(config_data, level SUBSEP s, filename)
else config_parse(config_data, s, filename)
}
else if (/<[\/][^>]*>/) {
match($0, /<[\/]([^>]*)>/)
s = substr($0, RSTART + 2, RLENGTH - 3)
t = split(s, l, SUBSEP)
closing = l[t]
t = split(s, l, SUBSEP)
current = l[t]
if (config_data["debug"])
print "Open", current, "close", closing > "/dev/stderr"
assert((current == closing), "Inconsistent open/close tags")
return
}
else {
if (match($0, /([^ ][^ ]*)[ ]*(..*)/)) {
# Using split rather than 3 argument match, for mawk.
split($0, l, /[ ][ ]*/)
if (config_data["debug"])
print level SUBSEP l[1], "=", l[2] > "/dev/stderr"
if (level) config_data[level, l[1]] = l[2]
else config_data[l[1]] = l[2]
}
}
}
}
function config (item, value ,element) {
# Portable equivilent...
# element = gensub(/\./, SUBSEP, "g", item)
element = item
gsub(/\./, SUBSEP, element)
if (value) _config[element] = value
if (_config["debug"])
print "config():", item, "(" element ")", value > "/dev/stderr"
# We should just treat undefined stuff as undefined *shrug*
# assert(_config[element], "config(): " element " is of no value")
return _config[element]
}