scripts / readconf /
Newer Older
189 lines | 6.141kb
commit initial
Sébastien MARQUE authored on 2016-10-31
1
#!/usr/bin/awk -f
2

            
3
function evaluate (str) {
4
    for (j = 1; j <= length(str); j++) {
5
        if (substr(str, j, l_delimiters_begin) == var_delimiters_begin) {
6
            replacement = true
7
            k = ""
8
            j += l_delimiters_begin - 1
9
            continue
10
        }
11
        if (substr(str, j, l_delimiters_end) == var_delimiters_end) {
12
            replacement = false
13
            str = gensub(var_delimiters_begin k var_delimiters_end, conf[name][apply_case_sensivity(k)], "g", str)
14
            j -= l_delimiters_end k
15
            continue
16
        }
17
        if (replacement)
18
            k = k substr(str, j, 1)
19
    }
20
    return dq gensub(dq, "\\\\" dq, "g", str) dq
21
}
22

            
23
function apply_case_sensivity (str) {
24
    if (case_sensitive)
25
        return str
26
    else {
27
        if (default_case == "lower")
28
            return tolower(str)
29
        else
30
            return toupper(str)
31
    }
32
}
33

            
34
function print_version () {
35
    printf("%s version %s\n", appname, version)
36
}
37

            
38
function help (rc) {
39
    print_version()
40
    if (rc != "") {
41
        print "CLI error: " rc
42
        exit 1
43
    }
44
    exit 0
45
}
46

            
47
BEGIN {
48
# initialisation
49
    appname     = ENVIRON["_"]
50
    version     = "0.9"
51
    conf_arrays = ""
52
    filename    = ""
53
    sections    = ""
54
    dq          = "\""
55
    eol         = ORS
56
    true        = 1
57
    false       = 0
58
    clean       = false
59
    unset       = false
60
    sectionlist = false
61
    case_sensitive = false
62
    default_case = "lower"
63
    var_delimiters_begin = "{{{"
64
    var_delimiters_end   = "}}}"
65
    key_value_delimiter  = ""
66
    in_var_space_remplacement = ""
67

            
68
    for (arg = 1; arg < ARGC; arg++)
69
        switch (ARGV[arg]) {
70
            case /^--conf=["']?.+["']?$/:
71
                filename = gensub(/^--conf=["']?(.+)["']?$/, "\\1", "", ARGV[arg])
72
                break
73
            case "--sections-list":
74
                returnsectionlist = 1
75
                break
76
            case /^--section=["']?.+["']?$/:
77
                s = gensub(/^--section=["']?(.+)["']?$/, "\\1", "", ARGV[arg])
78
                onlysections[s] = true
79
                break
80
            case /^-s$/:
81
                s = gensub(/^["']?(.+)["']?$/, "\\1", "", ARGV[++arg])
82
                onlysections[s] = true
83
                break
84
            case /^--clean$/:
85
                clean = true
86
                break
87
            case /^--unset$/:
88
                unset = true
89
                break
90
            case /^--compact$/:
91
                eol = ";"
92
                break
93
            case /^--array=["']?.+["']?$/:
94
                conf_arrays = conf_arrays "|" gensub(/^--array=["']?(.+)["']?$/, "\\1", "", ARGV[arg])
95
                break
96
            case /^--var-delim-begin=["']?.{3,}["']?$/:
97
                var_delimiters_begin = gensub(/^--var-delim-begin=["']?(.{3,})["']?$/, "\\1", "", ARGV[arg])
98
                break
99
            case /^--var-delim-end=["']?.{3,}["']?$/:
100
                var_delimiters_end = gensub(/^--var-delim-end=["']?(.{3,})["']?$/, "\\1", "", ARGV[arg])
101
                break
102
            case /^--case-sensitive$/:
103
                 case_sensitive = true
104
                 break
105
            case /^--case-insensitive(|=lower|=UPPER)$/:
106
                 case_sensitive = false
107
                 default_case = gensub(/^--case-insensitive[=]?(.*)$/, "\\1", "", ARGV[arg])
108
                 if (default_case == "")
109
                     default_case = "lower"
110
                 break
111
            case /^--key-value-delimiter=["']?.["']?$/:
112
                 key_value_delimiter = gensub(/^--key-value-delimiter=["']?(.)["']?$/, "\\1", "", ARGV[arg])
113
                 break
114
            case /^--in-var-space-replacement=["']?.["']?$/:
115
                in_var_space_remplacement = gensub(/^--in-var-space-replacement=["']?(.)["']?$/, "\\1", "", ARGV[arg])
116
                break
117
            case /^help$/:
118
                 help()
119
                 break
120
            case /^version$/:
121
                 print_version()
122
                 exit
123
            default:
124
                help(ARGV[arg])
125
        }
126

            
127
    if (filename == "" || (getline < filename) < 0 ) exit 1
128
    close(filename)
129

            
130
    if (conf_arrays != "") {
131
        conf_arrays = gensub("^|", "", "", conf_arrays)
132
        if (key_value_delimiter != "")
133
            conf_arrays = gensub(/\s/, in_var_space_remplacement, "g", conf_arrays)
134
        conf_arrays = apply_case_sensivity(conf_arrays)
135
    }
136

            
137
    l_delimiters_begin = length(var_delimiters_begin)
138
    l_delimiters_end   = length(var_delimiters_end)
139

            
140
    while ((getline < filename) == 1) {
141
        if (/^\s*(|#.*)$/)
142
            continue
143

            
144
        # section
145
        if (/^\s*\[.+\]\s*$/) {
146
            name=gensub(/^\s*\[(.+)\]\s*$/, "\\1", "g")
147
            for (key in key_array)
148
                ptr[name][key_array[key]] = 0
149
            continue
150
        }
151

            
152
        # key (delimiter) value
153
        else if (name != "") {
154
            if (key_value_delimiter != "") {
155
                split($0, keyvalue, "[[:blank:]]*" key_value_delimiter "[[:blank:]]*")
156
                key   = gensub(/\s/, in_var_space_remplacement, "g", keyvalue[1])
157
                value = clean ? "" : keyvalue[2] #TODO: "join" toutes les valeurs
158
            }
159
            else {
160
                key = $1
161
                value = clean ? "" : gensub("^[[:blank:]]*" $1 "[[:blank:]]*", "", "")
162
            }
163
            key = apply_case_sensivity(key)
164
            if (key ~ "^(" conf_arrays ")$" && ! unset) 
165
                conf[name][key][++ptr[name][key]] = value
166
            else
167
                conf[name][key] = value
168
        }
169
    }
170

            
171
    if (returnsectionlist)
172
        for (name in conf)
173
            print name
174

            
175
    else for (name in onlysections)
176
        for (key in conf[name]) {
177
            if (key ~ "^(" conf_arrays ")$" && ! unset)
178
                for (i=1; i<=ptr[name][key]; i++) 
179
                    printf("%s[%i]=%s", key, i, evaluate(conf[name][key][i]) eol)
180
            
181
            else {
182
                if (unset)
183
                    printf("unset %s", key, evaluate(conf[name][key]) eol)
184
                else
185
                    printf("%s=%s", key, evaluate(conf[name][key]) eol)
186
            }
187
        }
188
}
189