fixed indenting
[matthijs/upstream/backupninja.git] / lib / parseini.in
1
2 # parseini --- parses 'ini' style configuration files.
3 #
4 # Usage:
5 #   awk -f parseini S=<section> P=<param> <ini file>
6 #
7 # if section is an empty string, then we use the default section
8 #
9 # example ini file:
10
11 #    fruit = apple
12 #    fruit = pear
13 #    multiline = this is a multiline \
14 #    parameter
15 #
16 #    # this is a comment
17 #    [colors]  
18 #    red = yes
19 #    green = no
20 #    blue = maybe
21 #
22 #    [ocean] 
23 #    fish = red 
24 #    fish = blue
25 #       
26 # example usage:
27 #    > awk -f parseini S=ocean P=fish testfile.ini 
28 # would return: 
29 #    red
30 #    blue
31 #
32    
33 BEGIN { 
34     readlines = 1 
35     implied = 1 
36
37
38 # remove lines starting with #, but not #!
39 /^#[^!]/ {next} 
40
41 # skip blank
42 /^[ \r\t]*$/ {next} 
43
44 # we want to read the lines of the matched section
45 # and disable for other sections
46 /^\[.+\][ \r\t]*$/ { 
47     continueline = 0 
48     if (S && implied) { 
49         nline = 0 
50         implied = 0 
51     } 
52     if (S && match($0, "^\\[" S "\\][ \n]*")) { 
53         # we found the section, so start reading.
54         readlines = 1 
55     } 
56     else { 
57         # no section, so stop reading lines
58         if (readlines) readlines = 0 
59     } 
60     next 
61
62
63 # when reading, store lines.
64
65
66     if (!readlines) next 
67     line[nline++] = $0 
68     if ($0 ~ /\\[ \r\t]*$/) 
69         continueline = 1 
70     else 
71         continueline = 0 
72
73
74 # process the read lines lines, matching parameters
75
76 END { 
77     # if section is set but implied is still true
78     # then we never found the section, so use everything
79     if (S && implied) { 
80         nline = 0 
81     } 
82
83     # if have P then find P in read lines and get values 
84     if (P) { 
85         MATCH = "^[ \r\t]*" P "[ \r\t]*=" 
86         continueline = 0 
87         for (x = 0; x < nline; ++x) { 
88             v = line[x] 
89             if (continueline) { 
90                 sub(/[ \r\t]+$/, "", v) 
91                 if (v ~ /\\$/) { 
92                    v = substr(v, 1, length(v)-1) 
93                    sub(/[ \r\t]+$/, "", v) 
94                 } 
95                 if (v) value[nvalue++] = v 
96             } 
97             else if (v ~ MATCH) { 
98                 sub(MATCH, "", v) 
99                 sub(/^[ \r\t]+/, "", v) 
100                 sub(/[ \r\t]+$/, "", v) 
101                 if (v ~ /\\$/) { 
102                     continueline = 1 
103                     v = substr(v, 1, length(v)-1) 
104                     sub(/[ \r\t]+$/, "", v) 
105                 } 
106                 if (v) value[nvalue++] = v 
107             } 
108         } 
109         # copy parameter definition to output array 
110         nline = nvalue 
111         for (x = 0; x < nvalue; ++x) 
112             line[x] = value[x] 
113     } 
114
115     # trim all leading & trailing whitespace; 
116     # except for leading whitespace in continuation lines, 
117  
118     for (x = 0; x < nline; ++x) { 
119         sub(/^[ \r\t]+/, "", line[x]) 
120         sub(/[ \r\t]+$/, "", line[x]) 
121     } 
122  
123     # output the final result
124     for (x = 0; x < nline; ++x) 
125         print line[x] 
126
127     if (nline) exit 0 
128     else exit 1 
129 }