scripts / readconf /
4d82101 6 years ago
1 contributor
236 lines | 8.705kb
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237#!/usr/bin/gawk -f

function evaluate (str) {
    for (j = 1; j <= length(str); j++) {
        if (substr(str, j, l_delimiters_begin) == var_delimiters_begin) {
            replacement = true
            k = ""
            j += l_delimiters_begin - 1
            continue
        }
        if (substr(str, j, l_delimiters_end) == var_delimiters_end) {
            replacement = false
            str = gensub(var_delimiters_begin k var_delimiters_end, conf[name][apply_case_sensivity(k)], "g", str)
            j -= l_delimiters_end k
            continue
        }
        if (replacement)
            k = k substr(str, j, 1)
    }
    return dq gensub(dq, "\\\\" dq, "g", str) dq
}

function apply_case_sensivity (str) {
    if (case_sensitive)
        return str
    else {
        if (default_case == "lower")
            return tolower(str)
        else
            return toupper(str)
    }
}

function print_version () {
    printf("%s version %s\n", appname, version)
}

function cli_error (rc) {
    print "CLI error: " rc
    exit 1
}

function help (rc) {
    line = appname "\nversion " version "\n\n\033[4mOPTIONS:\033[0m"
    while (getline < appname) {
        if (/^#help:BEGIN/) help_started = 1
        if (/^#help:END/) help_started = 0
        if (help_started) {
            if (/^[[:blank:]]*case \/\^(.+)\$\/[[:blank:]]*\:[[:blank:]]*$/) {
                option = gensub(/^[[:blank:]]*case \/\^(.+)\$\/[[:blank:]]*\:[[:blank:]]*$/, "\\1", "1")
                line = line "\n\n\033[1m" option "\033[0m"
            }
            else if (/^#help:/ && option != "") {
                split($0, helpinfo, ":")
                line = line "\n" helpinfo[2]
            }
        }
    }
    print line | "pager -R"
    exit 0
}

BEGIN {
# initialisation
    appname     = ENVIRON["_"]
    version     = "1.0.0"
    conf_arrays = ""
    filename    = ""
    sections    = ""
    dq          = "\""
    eol         = ORS
    true        = 1
    false       = 0
    clean       = false
    unset       = false
    sectionlist = false
    case_sensitive = false
    default_case = "lower"
    var_delimiters_begin = "{{{"
    var_delimiters_end   = "}}}"
    key_value_delimiter  = ""
    in_var_space_remplacement = ""
    first_element_index = 0

    for (arg = 1; arg < ARGC; arg++)
        switch (ARGV[arg]) {
#help:BEGIN
            case /^--conf=["']?.+["']?$/:
#help:file containing the configuration to read
                filename = gensub(/^--conf=["']?(.+)["']?$/, "\\1", "1", ARGV[arg])
                break
            case /^--conf$/:
#help:file containing the configuration to read
                filename = gensub(/^["']?(.+)["']?$/, "\\1", "1", ARGV[++arg])
                break
            case "--sections-list":
#help:list all sections found in given config file
                returnsectionlist = 1
                break
            case /^--section=["']?.+["']?$/:
#help:retrieve information for section <section>
                s = gensub(/^--section=["']?(.+)["']?$/, "\\1", "1", ARGV[arg])
                onlysections[s] = true
                break
#            case /^-s$/:
##help:retrieve information for section <section>
#                s = gensub(/^["']?(.+)["']?$/, "\\1", "1", ARGV[++arg])
#                onlysections[s] = true
#                break
            case /^--clean$/:
#help:output commands to clean all variables listed in configuration
                clean = true
                break
            case /^--unset$/:
#help:output commands to unset variables listed in configuration file
                unset = true
                break
            case /^--compact$/:
#help:output everything on a single line, elements seperated by semi-colon
                eol = ";"
                break
            case /^--array=["']?.+["']?$/:
#help:learns the script this entry is intented to be treated as an array, can be used multiple times for multiples array names
                arrayname=gensub(/^--array=["']?(.+)["']?$/, "\\1", "1", ARGV[arg])
                conf_arrays = conf_arrays "|" arrayname
                key_array[arrayname]++
                break
            case /^--first-element-array=[01]$/:
#help:tells the script the array index begins at 0 or 1, defaults to 0
                first_element_index = gensub(/^--first-element-array=([01])$/, "\\1", "1", ARGV[arg])
                break
            case /^--var-delim-begin=["']?.{3,}["']?$/:
#help:tells the script what is the starting chars sequence to delimitate a variable, defaults to {{{, can be -+= (for example)
                var_delimiters_begin = gensub(/^--var-delim-begin=["']?(.{3,})["']?$/, "\\1", "1", ARGV[arg])
                break
            case /^--var-delim-end=["']?.{3,}["']?$/:
#help:tells the script what is the ending chars sequence to delimitate a variable, defaults to }}}, can be =+- (for example)
                var_delimiters_end = gensub(/^--var-delim-end=["']?(.{3,})["']?$/, "\\1", "1", ARGV[arg])
                break
            case /^--case-sensitive$/:
#help:tells the script if it has to be case-sensitive, defaults to insensitive
                 case_sensitive = true
                 break
            case /^--case-insensitive(|=lower|=UPPER)$/:
#help:tells the script how to behave in insensitive mode (default)
#help: lower = variables output in lowercase (default)
#help: UPPER = variables output in UPPERCASE
                 case_sensitive = false
                 default_case = gensub(/^--case-insensitive[=]?(.*)$/, "\\1", "1", ARGV[arg])
                 if (default_case == "")
                     default_case = "lower"
                 break
            case /^--key-value-delimiter=["']?.["']?$/:
#help:tells the script which char is used to separate the key from its assorciated value in the configuration file, defaults to blank-char
                 key_value_delimiter = gensub(/^--key-value-delimiter=["']?(.)["']?$/, "\\1", "1", ARGV[arg])
                 break
            case /^--in-var-space-replacement=["']?.["']?$/:
#help:tells the script which char to use to replace a space found in a key, defaults to empty
                in_var_space_remplacement = gensub(/^--in-var-space-replacement=["']?(.)["']?$/, "\\1", "1", ARGV[arg])
                break
            case /^help$/:
#help:show this help
                 help()
                 break
            case /^version$/:
#help:show the version of the script
                 print_version()
                 exit
#help:END
            default:
                cli_error(ARGV[arg])
        }

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

    if (conf_arrays != "") {
        conf_arrays = gensub("^|", "", "1", conf_arrays)
        if (key_value_delimiter != "")
            conf_arrays = gensub(/\s/, in_var_space_remplacement, "g", conf_arrays)
        conf_arrays = apply_case_sensivity(conf_arrays)
    }

    l_delimiters_begin = length(var_delimiters_begin)
    l_delimiters_end   = length(var_delimiters_end)

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

        # section
        if (/^\s*\[.+\]\s*$/) {
            name=gensub(/^\s*\[(.+)\]\s*$/, "\\1", "g")
            for (key in key_array)
                ptr[name][key] = first_element_index
            continue
        }

        # key (delimiter) value
        else if (name != "") {
            if (key_value_delimiter != "") {
                split($0, keyvalue, "[[:blank:]]*" key_value_delimiter "[[:blank:]]*")
                key   = gensub(/\s/, in_var_space_remplacement, "g", keyvalue[1])
                value = clean ? "" : keyvalue[2] #TODO: "join" toutes les valeurs
            }
            else {
                key = $1
                value = clean ? "" : gensub("^[[:blank:]]*" $1 "[[:blank:]]*", "", "1")
            }
            key = apply_case_sensivity(key)
            if (key ~ "^(" conf_arrays ")$" && ! unset)
                conf[name][key][ptr[name][key]++] = value
            else
                conf[name][key] = value
        }
    }

    if (returnsectionlist)
        for (name in conf)
            print name

    else for (name in onlysections)
        for (key in conf[name]) {
            if (key ~ "^(" conf_arrays ")$" && ! unset)
                for (i=first_element_index; i<ptr[name][key]; i++) 
                    printf("%s[%i]=%s", key, i, evaluate(conf[name][key][i]) eol)
            
            else {
                if (unset)
                    printf("unset %s", key, evaluate(conf[name][key]) eol)
                else
                    printf("%s=%s", key, evaluate(conf[name][key]) eol)
            }
        }
}