aboutsummaryrefslogtreecommitdiff
path: root/lib/parseini.in
blob: b9dfd01415091f05b7fc7ebd052ba7b643f1858d (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 
# 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:
# 
#    fruit = apple
#    fruit = pear
#    multiline = this is a multiline \
#    parameter
#
#    # this is a comment
#    [colors]  
#    red = yes
#    green = no
#    blue = maybe
#
#    [ocean] 
#    fish = red 
#    fish = blue
#       
# example usage:
#    > awk -f parseini S=ocean P=fish testfile.ini 
# would return: 
#    red
#    blue
#
   
BEGIN { 
    readlines = 1 
    implied = 1 
} 

# remove lines starting with #, but not #!
/^#[^!]/ {next} 

# skip blank
/^[ \r\t]*$/ {next} 

# we want to read the lines of the matched section
# and disable for other sections
/^\[.+\][ \r\t]*$/ { 
    continueline = 0 
    if (S && implied) { 
        nline = 0 
        implied = 0 
    } 
    if (S && match($0, "^\\[" S "\\][ \n]*")) { 
        # we found the section, so start reading.
        readlines = 1 
    } 
    else { 
        # no section, so stop reading lines
        if (readlines) readlines = 0 
    } 
    next 
} 

# when reading, store lines.

{ 
    if (!readlines) next 
    line[nline++] = $0 
    if ($0 ~ /\\[ \r\t]*$/) 
        continueline = 1 
    else 
        continueline = 0 
} 

# process the read lines lines, matching parameters

END { 
    # if section is set but implied is still true
    # then we never found the section, so use everything
    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, 
 
    for (x = 0; x < nline; ++x) { 
        sub(/^[ \r\t]+/, "", line[x]) 
        sub(/[ \r\t]+$/, "", line[x]) 
    } 
 
    # output the final result
    for (x = 0; x < nline; ++x) 
        print line[x] 

    if (nline) exit 0 
    else exit 1 
}