1 # -*- mode: awk; indent-tabs-mode: nil; -*-
3 # parseini --- parses 'ini' style configuration files.
6 # awk -f parseini S=<section> P=<param> <ini file>
8 # if section is an empty string, then we use the default section
14 # multiline = this is a multiline \
28 # > awk -f parseini S=ocean P=fish testfile.ini
39 # remove lines starting with #, but not #!
45 # we want to read the lines of the matched section
46 # and disable for other sections
53 if (S && match($0, "^\\[" S "\\][ \n]*")) {
54 # we found the section, so start reading.
58 # no section, so stop reading lines
59 if (readlines) readlines = 0
64 # when reading, store lines.
69 if ($0 ~ /\\[ \r\t]*$/)
75 # process the read lines lines, matching parameters
78 # if section is set but implied is still true
79 # then we never found the section, so use everything
84 # if have P then find P in read lines and get values
86 MATCH = "^[ \r\t]*" P "[ \r\t]*="
88 for (x = 0; x < nline; ++x) {
91 sub(/[ \r\t]+$/, "", v)
93 v = substr(v, 1, length(v)-1)
94 sub(/[ \r\t]+$/, "", v)
96 if (v) value[nvalue++] = v
100 sub(/^[ \r\t]+/, "", v)
101 sub(/[ \r\t]+$/, "", v)
104 v = substr(v, 1, length(v)-1)
105 sub(/[ \r\t]+$/, "", v)
107 if (v) value[nvalue++] = v
110 # copy parameter definition to output array
112 for (x = 0; x < nvalue; ++x)
116 # trim all leading & trailing whitespace;
117 # except for leading whitespace in continuation lines,
119 for (x = 0; x < nline; ++x) {
120 sub(/^[ \r\t]+/, "", line[x])
121 sub(/[ \r\t]+$/, "", line[x])
124 # output the final result
125 for (x = 0; x < nline; ++x)