-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathshini.sh
404 lines (340 loc) · 13 KB
/
shini.sh
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/sh
# shini - compatible INI library for sh
#
# This code is released freely under the MIT license - see the shipped LICENSE document.
# For the latest version etc, please see https://github.com/wallyhall/shini
#
# Solely for the purpose of portable performance, this script breaks good practice to
# avoid forking subshells. One such good practice is avoiding global variables.
# This variable is used to carry non-numeric results from functions to the caller.
# Alternatively an echo and "$(...)" approach could be used, but is significantly slower.
shini_setup()
{
if [ -n "$ZSH_VERSION" ]; then
RESTORE_OPTS=$(set +o)
# Enable BASH_REMATCH for zsh
setopt KSH_ARRAYS BASH_REMATCH
fi
}
shini_teardown()
{
[ -n "$ZSH_VERSION" ] && eval "$RESTORE_OPTS"
}
shini_function_exists()
{
type "$1" > /dev/null 2>&1
return $?
}
shini_regex_match()
{
# $KSH_VERSION (I'm told) only exists on ksh 93 and above, which supports regex matching.
# shellcheck disable=SC3054
if [ -n "${BASH_VERSINFO[0]}" ] && [ "${BASH_VERSINFO[0]}" -ge 3 ] || \
[ -n "$ZSH_VERSION" ] || \
[ -n "$KSH_VERSION" ]; then
# shellcheck disable=SC3010
[[ "$1" =~ $2 ]] && return 0 || return 1
fi
printf '%s' "$1" | grep -qe "$2"
return $?
}
shini_regex_extract()
{
# shellcheck disable=SC3054
if [ -n "${BASH_VERSINFO[0]}" ] && [ "${BASH_VERSINFO[0]}" -ge 3 ] || \
[ -n "$ZSH_VERSION" ]; then
# shellcheck disable=SC3010
# shellcheck disable=SC3028
# shellcheck disable=SC3054
[[ "$1" =~ $2 ]] && printf "%s" "${BASH_REMATCH[1]}" || printf "%s" "$1"
return 0
fi
printf '%s' "$1" | sed -E "s/$2/\1/" # If you have isses on older systems,
# it may be the non-newer POSIX compliant sed.
# -E should be enabling extended regex mode portably.
}
# @param inifile Filename of INI file to parse
# @param postfix Function postfix for callbacks (optional)
# @param extra Extra argument for callbacks (optional)
shini_parse()
{
shini_parse_section "$1" '' "$2" "$3" "$4" "$5"
return $?
}
# @param inifile Filename of INI file to parse
# @param section Section to parse (or empty string for entire file)
# @param postfix Function postfix for callbacks (optional)
# @param extra Extra argument for callbacks (optional)
shini_parse_section()
{
shini_setup
# ********
RX_KEY='[a-zA-Z0-9_\-\.]'
RX_SECTION='[a-zA-Z0-9_\-]'
RX_WS='[ ]'
RX_QUOTE='"'
POSTFIX=''
SKIP_TO_SECTION=''
EXTRA1=''
EXTRA2=''
EXTRA3=''
SECTION_FOUND=-1
if [ $# -ge 2 ] && [ -n "$2" ]; then
SKIP_TO_SECTION="$2"
fi
if [ $# -ge 3 ] && [ -n "$3" ]; then
POSTFIX="_$3"
fi
if [ $# -ge 4 ] && [ -n "$4" ]; then
EXTRA1="$4"
fi
if [ $# -ge 5 ] && [ -n "$5" ]; then
EXTRA2="$5"
fi
if [ $# -ge 6 ] && [ -n "$6" ]; then
EXTRA3="$6"
fi
if ! shini_function_exists "__shini_parsed${POSTFIX}"; then
printf 'shini: __shini_parsed%s function not declared.\n' "${POSTFIX}" 1>&2
return 255
fi
if [ $# -lt 1 ]; then
if shini_function_exists "__shini_no_file_passed{$POSTFIX}"; then
"__shini_no_file_passed${POSTFIX}" "$EXTRA1" "$EXTRA2" "$EXTRA3"
else
printf 'shini: Argument 1 needs to specify the INI file to parse.\n' 1>&2
return 254
fi
fi
INI_FILE="$1"
if [ ! -r "$INI_FILE" ]; then
if shini_function_exists "__shini_file_unreadable${POSTFIX}"; then
"__shini_file_unreadable${POSTFIX}" "$INI_FILE" "$EXTRA1" "$EXTRA2" "$EXTRA3"
else
# shellcheck disable=SC2016
printf 'shini: Unable to read INI file:\n `%s`\n' "$INI_FILE" 1>&2
return 253
fi
fi
# Iterate INI file line by line
LINE_NUM=-1
SECTION=''
while read -r LINE || [ -n "$LINE" ]; do # -n $LINE catches final line if not empty
LINE_NUM=$((LINE_NUM+1))
[ -z "$LINE" ] && continue
# Check for new sections
if shini_regex_match "$LINE" "^${RX_WS}*\[${RX_SECTION}${RX_SECTION}*\]${RX_WS}*$"; then
SECTION="$(shini_regex_extract "$LINE" "^${RX_WS}*\[(${RX_SECTION}${RX_SECTION}*)\]${RX_WS}*$" "\1")"
if [ "$SKIP_TO_SECTION" != '' ]; then
# stop once specific section is finished
[ "$SECTION_FOUND" -eq 0 ] && break;
# mark the specified section as found
[ "$SKIP_TO_SECTION" = "$SECTION" ] && SECTION_FOUND=0;
fi
if shini_function_exists "__shini_parsed_section${POSTFIX}"; then
"__shini_parsed_section${POSTFIX}" "$SECTION" "$EXTRA1" "$EXTRA2" "$EXTRA3" || break
fi
continue
fi
# Skip over sections we don't care about, if a specific section was specified
[ "$SKIP_TO_SECTION" != '' ] && [ "$SECTION_FOUND" -ne 0 ] && LINE_NUM=$((LINE_NUM+1)) && continue;
# Check for key/value lines
if shini_regex_match "$LINE" "^${RX_WS}*(${RX_KEY}${RX_KEY}*)${RX_WS}*=${RX_WS}*((${RX_QUOTE}[^${RX_QUOTE}]*${RX_QUOTE})|([^${RX_QUOTE};]*))${RX_WS}*(;(.*))*$"; then
# For shells supporting capturing regex ... extract the groups
# shellcheck disable=SC3054
if [ -n "${BASH_VERSINFO[0]}" ] && [ "${BASH_VERSINFO[0]}" -ge 3 ] || \
[ -n "$ZSH_VERSION" ]; then
# shellcheck disable=SC3028
# shellcheck disable=SC3010
{
KEY="${BASH_REMATCH[1]}"
COMMENT="${BASH_REMATCH[5]}"
TMPVAL="${BASH_REMATCH[2]}"
}
# Trim quotes or whitespace as applicable
TMPVAL="${TMPVAL%"${TMPVAL##*[![:space:]]}"}" # trim trailing whitespace
TMPVAL="${TMPVAL#\"}" # strip leading quote
TMPVAL="${TMPVAL%\"}" # strip trailing quote
VALUE="$TMPVAL"
else
# Fallback-mode, using string manipulation to extract
TMPKEY="${LINE%%=*}" # strip everything after key
TMPKEY="${TMPKEY#"${TMPKEY%%[![:space:]]*}"}" # trim leading whitespace
KEY="${TMPKEY%"${TMPKEY##*[![:space:]]}"}" # trim trailing whitespace
# Extract the value and (possible) comment
VALCOM="${LINE#*=}" # strip evrything before the value
VALCOM="${VALCOM#"${VALCOM%%[![:space:]]*}"}" # trim leading whitespace
# Determine if the value portion is quoted or not (POSIX-ly)
if [ "${VALCOM#\"}" != "$VALCOM" ]; then
TMPVAL="${VALCOM#\"}" # strip first quote
VALUE="${TMPVAL%%\"*}" # strip everything after and including the second (remaining) quote
TMPCOM="${VALCOM#"\"${VALUE}\""}" # strip the quoted value out
COMMENT="${TMPCOM#*\;}" # strip everything upto (and including) earliest comment marker
else
TMPVAL="${VALCOM%%\;*}" # strip everything after (and including) earliest comment marker
VALUE="${TMPVAL%"${TMPVAL##*[![:space:]]}"}" # trim trailing whitespace
COMMENT="${VALCOM#*\;}" # strip everything upto (and including) earliest comment marker
fi
fi
"__shini_parsed${POSTFIX}" "$SECTION" "$KEY" "$VALUE" "$EXTRA1" "$EXTRA2" "$EXTRA3" || break
if shini_function_exists "__shini_parsed_comment${POSTFIX}"; then
if shini_regex_match "$LINE" ";"; then
COMMENT="$(shini_regex_extract "$LINE" "^.*\;(.*)$")"
"__shini_parsed_comment${POSTFIX}" "$COMMENT" "$EXTRA1" "$EXTRA2" "$EXTRA3" || break
fi
fi
continue
fi
# Check for comment lines
if shini_regex_match "$LINE" "^${RX_WS}*\;"; then
if shini_function_exists "__shini_parsed_comment_line${POSTFIX}"; then
COMMENT="${LINE#*\;}" # strip everything upto (and including) earliest comment marker
"__shini_parsed_comment_line${POSTFIX}" "$COMMENT" "$EXTRA1" "$EXTRA2" "$EXTRA3" || break
fi
continue
fi
# Announce parse errors
if ! shini_regex_match "$LINE" "^${RX_WS}*;.*$" &&
! shini_regex_match "$LINE" "^${RX_WS}*$"; then
if shini_function_exists "__shini_parse_error${POSTFIX}"; then
"__shini_parse_error${POSTFIX}" $LINE_NUM "$LINE" "$EXTRA1" "$EXTRA2" "$EXTRA3" || break
else
# shellcheck disable=SC2016
printf 'shini: Unable to parse line %d:\n `%s`\n' $LINE_NUM "$LINE" 1>&2
fi
fi
done < "$INI_FILE"
# ********
shini_teardown
return 0
}
# SUBSHELL FUNCTION
# @param inifile Filename of INI file to write to
# @param section Section of INI file to write to
# @param variable Variable name to add/update/delete
# @param value Value to add/update, do not specify to delete
# @param quote (Double-)quote the value being written (default: false)
shini_write()
(
shini_setup
# ********
# This is not yet optimised (early write support only) -
# We actually re-parse the entire file, looking for the location in which to
# write the new value, writing out everything we parse as-is meanwhile.
# Declare the following if you want particular behaviour (like skipping
# broken INI file content or handling an unreadable file etc).
# __shini_no_file_passed__writer()
# __shini_file_unreadable__writer()
# __shini_parse_error__writer()
# Writer callbacks, used for writing the INI file content
# shellcheck disable=SC2317
__shini_parsed_section__writer()
{
# Validate the last section wasn't the target section
if [ "$LAST_SECTION" = "$WRITE_SECTION" ]; then
# If it was, and the value wasn't written already, write it
if [ "$VALUE_WRITTEN" -eq 0 ]; then
# shellcheck disable=SC2059
printf "$PRINTFMT" "$WRITE_KEY" "$WRITE_VALUE" >> "$INI_FILE_TEMP"
VALUE_WRITTEN=1
fi
fi
printf "\n[%s]" "$1" >> "$INI_FILE_TEMP"
LAST_SECTION="$1"
}
# shellcheck disable=SC2317
__shini_parsed_comment__writer()
{
printf " ;%s" "$1" >> "$INI_FILE_TEMP"
}
# shellcheck disable=SC2317
__shini_parsed_comment_line__writer()
{
printf "\n;%s" "$1" >> "$INI_FILE_TEMP"
}
# shellcheck disable=SC2317
__shini_parsed__writer()
{
if [ "$1" = "$WRITE_SECTION" ]; then
if [ "$2" = "$WRITE_KEY" ]; then
if [ -n "$WRITE_VALUE" ]; then
# shellcheck disable=SC2059
printf "$PRINTFMT" "$WRITE_KEY" "$WRITE_VALUE" >> "$INI_FILE_TEMP"
fi
VALUE_WRITTEN=1
return
fi
fi
# shellcheck disable=SC2059
printf "$PRINTFMT" "$2" "$3" >> "$INI_FILE_TEMP"
}
if [ $# -lt 3 ]; then
if shini_function_exists "__shini_no_file_passed"; then
__shini_no_file_passed
else
printf 'shini: Argument 1 needs to specify the INI file to write.\n' 1>&2
return 254
fi
fi
INI_FILE="$1"
INI_FILE_TEMP="$(mktemp -t shini_XXXXXX)"
WRITE_SECTION="$2"
WRITE_KEY="$3"
WRITE_VALUE="$4"
LAST_SECTION=""
VALUE_WRITTEN=0
# "Quote value" mode (true/false)
if [ -n "$5" ] && [ "$5" = true ]; then
PRINTFMT='\n%s="%s"'
else
PRINTFMT='\n%s=%s'
fi
shini_parse "$INI_FILE" "_writer" "$WRITE_SECTION" "$WRITE_KEY" "$WRITE_VALUE"
# Still not written out yet
if [ "$VALUE_WRITTEN" -eq 0 ]; then
# Check if final existing section was target one, add it if not
if [ "$LAST_SECTION" != "$WRITE_SECTION" ]; then
printf "\n[%s]" "$WRITE_SECTION" >> "$INI_FILE_TEMP"
fi
# Write value at end of file
# shellcheck disable=SC2059
printf "$PRINTFMT" "$WRITE_KEY" "$WRITE_VALUE" >> "$INI_FILE_TEMP"
fi
tail -n +2 "$INI_FILE_TEMP" > "$INI_FILE"
rm -f "$INI_FILE_TEMP"
# ********
shini_teardown
return 0
)
# SUBSHELL FUNCTION
# @param inifile Filename of INI file to read from
# @param section Section of INI file to read from (blank string if sectionless INI)
# @param key to read
shini_read()
(
shini_setup
# ********
# shellcheck disable=SC2317
__shini_parsed__key_read()
{
if [ "$1" = "$READ_SECTION" ]; then
if [ "$2" = "$READ_KEY" ]; then
printf "%s" "$3"
FOUND=1
return 1 # exit condition for shini_parse
fi
fi
}
INI_FILE="$1"
READ_SECTION="$2"
READ_KEY="$3"
FOUND=0
shini_parse "$INI_FILE" "_key_read" "$READ_SECTION" "$READ_KEY"
# ********
shini_teardown
if [ $FOUND -eq 0 ]; then
return 1
else
return 0
fi
)