2006-01-17 23:21:49 +01:00
|
|
|
# -*- mode: awk; indent-tabs-mode: nil; -*-
|
2021-01-22 17:24:14 +01:00
|
|
|
#
|
2004-12-09 05:37:12 +01:00
|
|
|
# parseini --- parses 'ini' style configuration files.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# awk -f parseini S=<section> P=<param> <ini file>
|
|
|
|
#
|
|
|
|
# if section is an empty string, then we use the default section
|
|
|
|
#
|
|
|
|
# example ini file:
|
2021-01-22 17:24:14 +01:00
|
|
|
#
|
2006-01-09 01:20:48 +01:00
|
|
|
# fruit = apple
|
|
|
|
# fruit = pear
|
|
|
|
# multiline = this is a multiline \
|
|
|
|
# parameter
|
2004-12-09 05:37:12 +01:00
|
|
|
#
|
2006-01-09 01:20:48 +01:00
|
|
|
# # this is a comment
|
2021-01-22 17:24:14 +01:00
|
|
|
# [colors]
|
2006-01-09 01:20:48 +01:00
|
|
|
# red = yes
|
|
|
|
# green = no
|
|
|
|
# blue = maybe
|
2004-12-09 05:37:12 +01:00
|
|
|
#
|
2021-01-22 17:24:14 +01:00
|
|
|
# [ocean]
|
|
|
|
# fish = red
|
2006-01-09 01:20:48 +01:00
|
|
|
# fish = blue
|
2021-01-22 17:24:14 +01:00
|
|
|
#
|
2004-12-09 05:37:12 +01:00
|
|
|
# example usage:
|
2021-01-22 17:24:14 +01:00
|
|
|
# > awk -f parseini S=ocean P=fish testfile.ini
|
|
|
|
# would return:
|
2006-01-09 01:20:48 +01:00
|
|
|
# red
|
|
|
|
# blue
|
2004-12-09 05:37:12 +01:00
|
|
|
#
|
2021-01-22 17:24:14 +01:00
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
readlines = 1
|
|
|
|
implied = 1
|
|
|
|
}
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
# remove lines starting with #, but not #!
|
2021-01-22 17:24:14 +01:00
|
|
|
/^#[^!]/ {next}
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
# skip blank
|
2021-01-22 17:24:14 +01:00
|
|
|
/^[ \r\t]*$/ {next}
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
# we want to read the lines of the matched section
|
|
|
|
# and disable for other sections
|
2021-01-22 17:24:14 +01:00
|
|
|
/^\[.+\][ \r\t]*$/ {
|
|
|
|
continueline = 0
|
|
|
|
if (S && implied) {
|
|
|
|
nline = 0
|
|
|
|
implied = 0
|
|
|
|
}
|
|
|
|
if (S && match($0, "^\\[" S "\\][ \n]*")) {
|
2004-12-09 05:37:12 +01:00
|
|
|
# we found the section, so start reading.
|
2021-01-22 17:24:14 +01:00
|
|
|
readlines = 1
|
|
|
|
}
|
|
|
|
else {
|
2004-12-09 05:37:12 +01:00
|
|
|
# no section, so stop reading lines
|
2021-01-22 17:24:14 +01:00
|
|
|
if (readlines) readlines = 0
|
|
|
|
}
|
|
|
|
next
|
|
|
|
}
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
# when reading, store lines.
|
|
|
|
|
2021-01-22 17:24:14 +01:00
|
|
|
{
|
|
|
|
if (!readlines) next
|
|
|
|
line[nline++] = $0
|
|
|
|
if ($0 ~ /\\[ \r\t]*$/)
|
|
|
|
continueline = 1
|
|
|
|
else
|
|
|
|
continueline = 0
|
|
|
|
}
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
# process the read lines lines, matching parameters
|
|
|
|
|
2021-01-22 17:24:14 +01:00
|
|
|
END {
|
2004-12-09 05:37:12 +01:00
|
|
|
# if section is set but implied is still true
|
|
|
|
# then we never found the section, so use everything
|
2021-01-22 17:24:14 +01:00
|
|
|
if (S && implied) {
|
|
|
|
nline = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# if have P then find P in read lines and get values
|
|
|
|
if (P) {
|
|
|
|
MATCH = "^[ \r\t]*" P "[ \r\t]*="
|
|
|
|
continueline = 0
|
|
|
|
for (x = 0; x < nline; ++x) {
|
|
|
|
v = line[x]
|
|
|
|
if (continueline) {
|
|
|
|
sub(/[ \r\t]+$/, "", v)
|
|
|
|
if (v ~ /\\$/) {
|
|
|
|
v = substr(v, 1, length(v)-1)
|
|
|
|
sub(/[ \r\t]+$/, "", v)
|
|
|
|
}
|
|
|
|
if (v) value[nvalue++] = v
|
|
|
|
}
|
|
|
|
else if (v ~ MATCH) {
|
|
|
|
sub(MATCH, "", v)
|
|
|
|
sub(/^[ \r\t]+/, "", v)
|
|
|
|
sub(/[ \r\t]+$/, "", v)
|
|
|
|
if (v ~ /\\$/) {
|
|
|
|
continueline = 1
|
|
|
|
v = substr(v, 1, length(v)-1)
|
|
|
|
sub(/[ \r\t]+$/, "", v)
|
|
|
|
}
|
|
|
|
if (v) value[nvalue++] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# copy parameter definition to output array
|
|
|
|
nline = nvalue
|
|
|
|
for (x = 0; x < nvalue; ++x)
|
|
|
|
line[x] = value[x]
|
|
|
|
}
|
|
|
|
|
|
|
|
# trim all leading & trailing whitespace;
|
|
|
|
# except for leading whitespace in continuation lines,
|
2004-12-09 05:37:12 +01:00
|
|
|
|
2021-01-22 17:24:14 +01:00
|
|
|
for (x = 0; x < nline; ++x) {
|
|
|
|
sub(/^[ \r\t]+/, "", line[x])
|
|
|
|
sub(/[ \r\t]+$/, "", line[x])
|
|
|
|
}
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
# output the final result
|
2021-01-22 17:24:14 +01:00
|
|
|
for (x = 0; x < nline; ++x)
|
|
|
|
print line[x]
|
2004-12-09 05:37:12 +01:00
|
|
|
|
2021-01-22 17:24:14 +01:00
|
|
|
if (nline) exit 0
|
|
|
|
else exit 1
|
2004-12-09 05:37:12 +01:00
|
|
|
}
|