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)
98 if (v) value[nvalue++] = v
100 else if (v ~ MATCH) {
102 sub(/^[ \r\t]+/, "", v)
103 sub(/[ \r\t]+$/, "", v)
106 v = substr(v, 1, length(v)-1)
107 sub(/[ \r\t]+$/, "", v)
109 if (v) value[nvalue++] = v
112 # copy parameter definition to output array
114 for (x = 0; x < nvalue; ++x)
118 # trim all leading & trailing whitespace;
119 # except for leading whitespace in continuation lines,
121 for (x = 0; x < nline; ++x) {
122 sub(/^[ \r\t]+/, "", line[x])
123 sub(/[ \r\t]+$/, "", line[x])
126 # output the final result
127 for (x = 0; x < nline; ++x)