utilisation forcée de gawk
|
1 |
#!/usr/bin/gawk -f |
commit initial
|
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 | ||
ajout de l'aide dans readcon...
|
38 |
function cli_error (rc) { |
39 |
print "CLI error: " rc |
|
40 |
exit 1 |
|
41 |
} |
|
42 | ||
commit initial
|
43 |
function help (rc) { |
ajout de l'aide dans readcon...
|
44 |
line = appname "\nversion " version "\n\n\033[4mOPTIONS:\033[0m" |
45 |
while (getline < appname) { |
|
46 |
if (/^#help:BEGIN/) help_started = 1 |
|
47 |
if (/^#help:END/) help_started = 0 |
|
48 |
if (help_started) { |
|
49 |
if (/^[[:blank:]]*case \/\^(.+)\$\/[[:blank:]]*\:[[:blank:]]*$/) { |
|
50 |
option = gensub(/^[[:blank:]]*case \/\^(.+)\$\/[[:blank:]]*\:[[:blank:]]*$/, "\\1", "1") |
|
51 |
line = line "\n\n\033[1m" option "\033[0m" |
|
52 |
} |
|
53 |
else if (/^#help:/ && option != "") { |
|
54 |
split($0, helpinfo, ":") |
|
55 |
line = line "\n" helpinfo[2] |
|
56 |
} |
|
57 |
} |
|
commit initial
|
58 |
} |
ajout de l'aide dans readcon...
|
59 |
print line | "pager -R" |
commit initial
|
60 |
exit 0 |
61 |
} |
|
62 | ||
63 |
BEGIN { |
|
64 |
# initialisation |
|
65 |
appname = ENVIRON["_"] |
|
ajout de l'aide dans readcon...
|
66 |
version = "1.0.0" |
commit initial
|
67 |
conf_arrays = "" |
68 |
filename = "" |
|
69 |
sections = "" |
|
70 |
dq = "\"" |
|
71 |
eol = ORS |
|
72 |
true = 1 |
|
73 |
false = 0 |
|
74 |
clean = false |
|
75 |
unset = false |
|
76 |
sectionlist = false |
|
77 |
case_sensitive = false |
|
78 |
default_case = "lower" |
|
79 |
var_delimiters_begin = "{{{" |
|
80 |
var_delimiters_end = "}}}" |
|
81 |
key_value_delimiter = "" |
|
82 |
in_var_space_remplacement = "" |
|
correction bug sur les table...
|
83 |
first_element_index = 0 |
commit initial
|
84 | |
85 |
for (arg = 1; arg < ARGC; arg++) |
|
86 |
switch (ARGV[arg]) { |
|
ajout de l'aide dans readcon...
|
87 |
#help:BEGIN |
commit initial
|
88 |
case /^--conf=["']?.+["']?$/: |
ajout de l'aide dans readcon...
|
89 |
#help:file containing the configuration to read |
fix awk message for gensub p...
|
90 |
filename = gensub(/^--conf=["']?(.+)["']?$/, "\\1", "1", ARGV[arg]) |
commit initial
|
91 |
break |
correction bug sur les table...
|
92 |
case /^--conf$/: |
ajout de l'aide dans readcon...
|
93 |
#help:file containing the configuration to read |
fix awk message for gensub p...
|
94 |
filename = gensub(/^["']?(.+)["']?$/, "\\1", "1", ARGV[++arg]) |
correction bug sur les table...
|
95 |
break |
commit initial
|
96 |
case "--sections-list": |
ajout de l'aide dans readcon...
|
97 |
#help:list all sections found in given config file |
commit initial
|
98 |
returnsectionlist = 1 |
99 |
break |
|
100 |
case /^--section=["']?.+["']?$/: |
|
ajout de l'aide dans readcon...
|
101 |
#help:retrieve information for section <section> |
fix awk message for gensub p...
|
102 |
s = gensub(/^--section=["']?(.+)["']?$/, "\\1", "1", ARGV[arg]) |
commit initial
|
103 |
onlysections[s] = true |
104 |
break |
|
ajout de l'aide dans readcon...
|
105 |
# case /^-s$/: |
106 |
##help:retrieve information for section <section> |
|
fix awk message for gensub p...
|
107 |
# s = gensub(/^["']?(.+)["']?$/, "\\1", "1", ARGV[++arg]) |
ajout de l'aide dans readcon...
|
108 |
# onlysections[s] = true |
109 |
# break |
|
commit initial
|
110 |
case /^--clean$/: |
ajout de l'aide dans readcon...
|
111 |
#help:output commands to clean all variables listed in configuration |
commit initial
|
112 |
clean = true |
113 |
break |
|
114 |
case /^--unset$/: |
|
ajout de l'aide dans readcon...
|
115 |
#help:output commands to unset variables listed in configuration file |
commit initial
|
116 |
unset = true |
117 |
break |
|
118 |
case /^--compact$/: |
|
ajout de l'aide dans readcon...
|
119 |
#help:output everything on a single line, elements seperated by semi-colon |
commit initial
|
120 |
eol = ";" |
121 |
break |
|
122 |
case /^--array=["']?.+["']?$/: |
|
ajout de l'aide dans readcon...
|
123 |
#help:learns the script this entry is intented to be treated as an array, can be used multiple times for multiples array names |
fix awk message for gensub p...
|
124 |
arrayname=gensub(/^--array=["']?(.+)["']?$/, "\\1", "1", ARGV[arg]) |
correction bug sur les table...
|
125 |
conf_arrays = conf_arrays "|" arrayname |
126 |
key_array[arrayname]++ |
|
127 |
break |
|
128 |
case /^--first-element-array=[01]$/: |
|
ajout de l'aide dans readcon...
|
129 |
#help:tells the script the array index begins at 0 or 1, defaults to 0 |
fix awk message for gensub p...
|
130 |
first_element_index = gensub(/^--first-element-array=([01])$/, "\\1", "1", ARGV[arg]) |
commit initial
|
131 |
break |
132 |
case /^--var-delim-begin=["']?.{3,}["']?$/: |
|
ajout de l'aide dans readcon...
|
133 |
#help:tells the script what is the starting chars sequence to delimitate a variable, defaults to {{{, can be -+= (for example) |
fix awk message for gensub p...
|
134 |
var_delimiters_begin = gensub(/^--var-delim-begin=["']?(.{3,})["']?$/, "\\1", "1", ARGV[arg]) |
commit initial
|
135 |
break |
136 |
case /^--var-delim-end=["']?.{3,}["']?$/: |
|
ajout de l'aide dans readcon...
|
137 |
#help:tells the script what is the ending chars sequence to delimitate a variable, defaults to }}}, can be =+- (for example) |
fix awk message for gensub p...
|
138 |
var_delimiters_end = gensub(/^--var-delim-end=["']?(.{3,})["']?$/, "\\1", "1", ARGV[arg]) |
commit initial
|
139 |
break |
140 |
case /^--case-sensitive$/: |
|
ajout de l'aide dans readcon...
|
141 |
#help:tells the script if it has to be case-sensitive, defaults to insensitive |
commit initial
|
142 |
case_sensitive = true |
143 |
break |
|
144 |
case /^--case-insensitive(|=lower|=UPPER)$/: |
|
ajout de l'aide dans readcon...
|
145 |
#help:tells the script how to behave in insensitive mode (default) |
146 |
#help: lower = variables output in lowercase (default) |
|
147 |
#help: UPPER = variables output in UPPERCASE |
|
commit initial
|
148 |
case_sensitive = false |
fix awk message for gensub p...
|
149 |
default_case = gensub(/^--case-insensitive[=]?(.*)$/, "\\1", "1", ARGV[arg]) |
commit initial
|
150 |
if (default_case == "") |
151 |
default_case = "lower" |
|
152 |
break |
|
153 |
case /^--key-value-delimiter=["']?.["']?$/: |
|
ajout de l'aide dans readcon...
|
154 |
#help:tells the script which char is used to separate the key from its assorciated value in the configuration file, defaults to blank-char |
fix awk message for gensub p...
|
155 |
key_value_delimiter = gensub(/^--key-value-delimiter=["']?(.)["']?$/, "\\1", "1", ARGV[arg]) |
commit initial
|
156 |
break |
157 |
case /^--in-var-space-replacement=["']?.["']?$/: |
|
ajout de l'aide dans readcon...
|
158 |
#help:tells the script which char to use to replace a space found in a key, defaults to empty |
fix awk message for gensub p...
|
159 |
in_var_space_remplacement = gensub(/^--in-var-space-replacement=["']?(.)["']?$/, "\\1", "1", ARGV[arg]) |
commit initial
|
160 |
break |
161 |
case /^help$/: |
|
ajout de l'aide dans readcon...
|
162 |
#help:show this help |
commit initial
|
163 |
help() |
164 |
break |
|
165 |
case /^version$/: |
|
ajout de l'aide dans readcon...
|
166 |
#help:show the version of the script |
commit initial
|
167 |
print_version() |
168 |
exit |
|
ajout de l'aide dans readcon...
|
169 |
#help:END |
commit initial
|
170 |
default: |
message en cas d'erreur sur ...
|
171 |
cli_error(ARGV[arg]) |
commit initial
|
172 |
} |
173 | ||
174 |
if (filename == "" || (getline < filename) < 0 ) exit 1 |
|
175 |
close(filename) |
|
176 | ||
177 |
if (conf_arrays != "") { |
|
fix awk message for gensub p...
|
178 |
conf_arrays = gensub("^|", "", "1", conf_arrays) |
commit initial
|
179 |
if (key_value_delimiter != "") |
180 |
conf_arrays = gensub(/\s/, in_var_space_remplacement, "g", conf_arrays) |
|
181 |
conf_arrays = apply_case_sensivity(conf_arrays) |
|
182 |
} |
|
183 | ||
184 |
l_delimiters_begin = length(var_delimiters_begin) |
|
185 |
l_delimiters_end = length(var_delimiters_end) |
|
186 | ||
187 |
while ((getline < filename) == 1) { |
|
188 |
if (/^\s*(|#.*)$/) |
|
189 |
continue |
|
190 | ||
191 |
# section |
|
192 |
if (/^\s*\[.+\]\s*$/) { |
|
193 |
name=gensub(/^\s*\[(.+)\]\s*$/, "\\1", "g") |
|
194 |
for (key in key_array) |
|
correction bug sur les table...
|
195 |
ptr[name][key] = first_element_index |
commit initial
|
196 |
continue |
197 |
} |
|
198 | ||
199 |
# key (delimiter) value |
|
200 |
else if (name != "") { |
|
201 |
if (key_value_delimiter != "") { |
|
202 |
split($0, keyvalue, "[[:blank:]]*" key_value_delimiter "[[:blank:]]*") |
|
203 |
key = gensub(/\s/, in_var_space_remplacement, "g", keyvalue[1]) |
|
204 |
value = clean ? "" : keyvalue[2] #TODO: "join" toutes les valeurs |
|
205 |
} |
|
206 |
else { |
|
207 |
key = $1 |
|
fix awk message for gensub p...
|
208 |
value = clean ? "" : gensub("^[[:blank:]]*" $1 "[[:blank:]]*", "", "1") |
commit initial
|
209 |
} |
210 |
key = apply_case_sensivity(key) |
|
correction bug sur les table...
|
211 |
if (key ~ "^(" conf_arrays ")$" && ! unset) |
212 |
conf[name][key][ptr[name][key]++] = value |
|
commit initial
|
213 |
else |
214 |
conf[name][key] = value |
|
215 |
} |
|
216 |
} |
|
217 | ||
218 |
if (returnsectionlist) |
|
219 |
for (name in conf) |
|
220 |
print name |
|
221 | ||
222 |
else for (name in onlysections) |
|
223 |
for (key in conf[name]) { |
|
224 |
if (key ~ "^(" conf_arrays ")$" && ! unset) |
|
correction bug sur les table...
|
225 |
for (i=first_element_index; i<ptr[name][key]; i++) |
commit initial
|
226 |
printf("%s[%i]=%s", key, i, evaluate(conf[name][key][i]) eol) |
227 |
|
|
228 |
else { |
|
229 |
if (unset) |
|
230 |
printf("unset %s", key, evaluate(conf[name][key]) eol) |
|
231 |
else |
|
232 |
printf("%s=%s", key, evaluate(conf[name][key]) eol) |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |