commit initial
|
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 = "" |
|
correction bug sur les table...
|
67 |
first_element_index = 0 |
commit initial
|
68 | |
69 |
for (arg = 1; arg < ARGC; arg++) |
|
70 |
switch (ARGV[arg]) { |
|
71 |
case /^--conf=["']?.+["']?$/: |
|
72 |
filename = gensub(/^--conf=["']?(.+)["']?$/, "\\1", "", ARGV[arg]) |
|
73 |
break |
|
correction bug sur les table...
|
74 |
case /^--conf$/: |
75 |
filename = gensub(/^["']?(.+)["']?$/, "\\1", "", ARGV[++arg]) |
|
76 |
break |
|
commit initial
|
77 |
case "--sections-list": |
78 |
returnsectionlist = 1 |
|
79 |
break |
|
80 |
case /^--section=["']?.+["']?$/: |
|
81 |
s = gensub(/^--section=["']?(.+)["']?$/, "\\1", "", ARGV[arg]) |
|
82 |
onlysections[s] = true |
|
83 |
break |
|
84 |
case /^-s$/: |
|
85 |
s = gensub(/^["']?(.+)["']?$/, "\\1", "", ARGV[++arg]) |
|
86 |
onlysections[s] = true |
|
87 |
break |
|
88 |
case /^--clean$/: |
|
89 |
clean = true |
|
90 |
break |
|
91 |
case /^--unset$/: |
|
92 |
unset = true |
|
93 |
break |
|
94 |
case /^--compact$/: |
|
95 |
eol = ";" |
|
96 |
break |
|
97 |
case /^--array=["']?.+["']?$/: |
|
correction bug sur les table...
|
98 |
arrayname=gensub(/^--array=["']?(.+)["']?$/, "\\1", "", ARGV[arg]) |
99 |
conf_arrays = conf_arrays "|" arrayname |
|
100 |
key_array[arrayname]++ |
|
101 |
break |
|
102 |
case /^--first-element-array=[01]$/: |
|
103 |
first_element_index = gensub(/^--first-element-array=([01])$/, "\\1", "", ARGV[arg]) |
|
commit initial
|
104 |
break |
105 |
case /^--var-delim-begin=["']?.{3,}["']?$/: |
|
106 |
var_delimiters_begin = gensub(/^--var-delim-begin=["']?(.{3,})["']?$/, "\\1", "", ARGV[arg]) |
|
107 |
break |
|
108 |
case /^--var-delim-end=["']?.{3,}["']?$/: |
|
109 |
var_delimiters_end = gensub(/^--var-delim-end=["']?(.{3,})["']?$/, "\\1", "", ARGV[arg]) |
|
110 |
break |
|
111 |
case /^--case-sensitive$/: |
|
112 |
case_sensitive = true |
|
113 |
break |
|
114 |
case /^--case-insensitive(|=lower|=UPPER)$/: |
|
115 |
case_sensitive = false |
|
116 |
default_case = gensub(/^--case-insensitive[=]?(.*)$/, "\\1", "", ARGV[arg]) |
|
117 |
if (default_case == "") |
|
118 |
default_case = "lower" |
|
119 |
break |
|
120 |
case /^--key-value-delimiter=["']?.["']?$/: |
|
121 |
key_value_delimiter = gensub(/^--key-value-delimiter=["']?(.)["']?$/, "\\1", "", ARGV[arg]) |
|
122 |
break |
|
123 |
case /^--in-var-space-replacement=["']?.["']?$/: |
|
124 |
in_var_space_remplacement = gensub(/^--in-var-space-replacement=["']?(.)["']?$/, "\\1", "", ARGV[arg]) |
|
125 |
break |
|
126 |
case /^help$/: |
|
127 |
help() |
|
128 |
break |
|
129 |
case /^version$/: |
|
130 |
print_version() |
|
131 |
exit |
|
132 |
default: |
|
133 |
help(ARGV[arg]) |
|
134 |
} |
|
135 | ||
136 |
if (filename == "" || (getline < filename) < 0 ) exit 1 |
|
137 |
close(filename) |
|
138 | ||
139 |
if (conf_arrays != "") { |
|
140 |
conf_arrays = gensub("^|", "", "", conf_arrays) |
|
141 |
if (key_value_delimiter != "") |
|
142 |
conf_arrays = gensub(/\s/, in_var_space_remplacement, "g", conf_arrays) |
|
143 |
conf_arrays = apply_case_sensivity(conf_arrays) |
|
144 |
} |
|
145 | ||
146 |
l_delimiters_begin = length(var_delimiters_begin) |
|
147 |
l_delimiters_end = length(var_delimiters_end) |
|
148 | ||
149 |
while ((getline < filename) == 1) { |
|
150 |
if (/^\s*(|#.*)$/) |
|
151 |
continue |
|
152 | ||
153 |
# section |
|
154 |
if (/^\s*\[.+\]\s*$/) { |
|
155 |
name=gensub(/^\s*\[(.+)\]\s*$/, "\\1", "g") |
|
156 |
for (key in key_array) |
|
correction bug sur les table...
|
157 |
ptr[name][key] = first_element_index |
commit initial
|
158 |
continue |
159 |
} |
|
160 | ||
161 |
# key (delimiter) value |
|
162 |
else if (name != "") { |
|
163 |
if (key_value_delimiter != "") { |
|
164 |
split($0, keyvalue, "[[:blank:]]*" key_value_delimiter "[[:blank:]]*") |
|
165 |
key = gensub(/\s/, in_var_space_remplacement, "g", keyvalue[1]) |
|
166 |
value = clean ? "" : keyvalue[2] #TODO: "join" toutes les valeurs |
|
167 |
} |
|
168 |
else { |
|
169 |
key = $1 |
|
170 |
value = clean ? "" : gensub("^[[:blank:]]*" $1 "[[:blank:]]*", "", "") |
|
171 |
} |
|
172 |
key = apply_case_sensivity(key) |
|
correction bug sur les table...
|
173 |
if (key ~ "^(" conf_arrays ")$" && ! unset) |
174 |
conf[name][key][ptr[name][key]++] = value |
|
commit initial
|
175 |
else |
176 |
conf[name][key] = value |
|
177 |
} |
|
178 |
} |
|
179 | ||
180 |
if (returnsectionlist) |
|
181 |
for (name in conf) |
|
182 |
print name |
|
183 | ||
184 |
else for (name in onlysections) |
|
185 |
for (key in conf[name]) { |
|
186 |
if (key ~ "^(" conf_arrays ")$" && ! unset) |
|
correction bug sur les table...
|
187 |
for (i=first_element_index; i<ptr[name][key]; i++) |
commit initial
|
188 |
printf("%s[%i]=%s", key, i, evaluate(conf[name][key][i]) eol) |
189 |
|
|
190 |
else { |
|
191 |
if (unset) |
|
192 |
printf("unset %s", key, evaluate(conf[name][key]) eol) |
|
193 |
else |
|
194 |
printf("%s=%s", key, evaluate(conf[name][key]) eol) |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |