/
UniversalDotfilePreprocessor
executable file
·548 lines (488 loc) · 11.8 KB
/
UniversalDotfilePreprocessor
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/bin/bash
####################
# Engine Variables #
####################
engine_state="loading program"
engine_interactive=1 #enabled or disables the debugging REPL
engine_no_process_depth=0 #how deeply nested in a failing if statement are we?
engine_file=()
engine_output=()
####################
# Global Variables #
####################
#Set defaults
declare -A globals=(
["UDPComment"]="#"
["UDPOutput"]="Mini"
)
#######################
# Processor Variables #
#######################
declare -A processor=(
#Preprocessor Variables
["scripts-enabled"]="1"
["output-comments"]="None"
["section-style"]="Line"
#Postprocessor Variables
["output-location"]="./compiled configs/<#config-name#>"
["output-rotation"]="0"
)
##################
# User Variables #
##################
declare -A user_vars=()
#########################
# Convenience Functions #
# For Scripting #
#########################
#$1 named user variable
get_user(){
echo "${user_vars["$1"]}"
}
get_global(){
echo "${processor["$1"]}"
}
get_processor(){
echo "${processor["$1"]}"
}
config-name(){
echo "${user_vars["config-name"]}"
}
#$1 0 or 1
#$2 0 or 1
xor(){
local left=false; [[ $1 = "0" ]] && left=true
local right=false; [[ $2 = "0" ]] && right=true
if ($left || $right) && ! ($left && $right) then
return 0
else
return 1
fi
}
###################
# Engine Fuctions #
###################
help() {
echo 'Command Help'
}
compile_processor(){
for pvar in "${!processor[@]}"; do
processor[$pvar]=$(process_insertions "${processor[$pvar]}")
done
}
#$1 file to load into engine
load_file(){
engine_state="load file"
engine_interactive=0
local line
while IFS= read -r line
do
engine_file+=("$line")
debug_line "$line"
done < "$1"
user_vars["config-name"]=$(basename "$1")
compile_processor
}
load_globals(){
engine_state="load globals"
engine_interactive=1
local line
for line in "${engine_file[@]}"; do
read_globals "$line"
debug_line "$line"
done
compile_processor
}
preprocessor(){
engine_state="preprocessor"
engine_interactive=1
local line
for line in "${engine_file[@]}"; do
if [[ $engine_no_process_depth -eq 0 ]]; then
read_processor "$line" && compile_processor
read_conditional "$line"
debug_line "$line"
else
end_if_regex="#\\?\\?"
begin_if_regex="#\\?.+"
debug_regex="#?..?#"
if ! is_comment "$line" "$end_if_regex" && [[ $line =~ $end_if_regex ]]; then
: $((engine_no_process_depth --))
elif ! is_comment "$line" "$end_if_regex" && [[ ! $line =~ $debug_regex ]] && [[ $line =~ $begin_if_regex ]]; then
: $((engine_no_process_depth ++))
fi
debug_line "$line"
fi
output "$line"
done
}
postprocessor(){
engine_state="postprocessor"
engine_interactive=1
#make nice vars
local output_location="${processor["output-location"]}"
local comment_regex="^${globals["UDPComment"]}"
local udp_regex="#(\\?.+|.+=.+|[SI] .+|Required-commands .+)"
# File Operations
rotate_files
mkdir -p "$(dirname "$output_location")"
local line
for line in "${engine_output[@]}"; do
case "${globals["UDPOutput"]}" in
"UDP")
echo "$line" >> "$output_location"
;;
"Comment")
if [[ ! $line =~ $udp_regex ]]; then
echo "$line" >> "$output_location"
fi
;;
"Mini")
if [[ ! $line =~ $udp_regex ]]; then
echo "$line" >> "$output_location"
fi
;;
"Pico")
if [[ ! $line =~ $comment_regex ]]; then
echo "$line" >> "$output_location"
fi
;;
*)
echo "Unknown UDPOutput type \"${globals["UDPOutput"]}\""
;;
esac
done
}
#$1 line to check
#$2 regex to match comment
is_comment() {
local regex="##.*$2"
[[ $1 =~ $regex ]]
}
#$1 line to read
#$2 target
#$3 target preamble
read_line() {
local raw_regex="$2=(.+)$"
local regex="$3$raw_regex"
local comment_regex="(?:$3)?$raw_regex"
if ! (is_comment "$1" "$comment_regex") && [[ $1 =~ $regex ]]; then
echo "${BASH_REMATCH[1]}"
return 0
fi
return 1
}
#$1 line to check for globals
read_globals() {
for global in "${!globals[@]}"; do
local out
if out=$(read_line "$1" "$global" "#"); then
globals[$global]=$out
fi
done
}
#$1 line to check for processor vars
read_processor() {
for pvar in "${!processor[@]}"; do
local out
if out=$(read_line "$1" "$pvar" "#@"); then
processor[$pvar]=$out
fi
done
}
#Regex for all conditionals
comparator_regex="!?([=~])"
#$1 arguments
#$2 comparator
#$3 matcher
ask_conditional() {
local answer
read -p "$1?: " -r answer
[[ $2 =~ "!" ]]; local not=$?
[[ $2 =~ $comparator_regex ]]
case ${BASH_REMATCH[1]} in
"=")
[[ ! $answer = "$3" ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
;;
"~")
[[ ! $answer =~ $3 ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
;;
esac
}
#$1 arguments
#$2 comparator
#$3 matcher
hostname_conditional(){
[[ $2 =~ "!" ]]; local not=$?
[[ $2 =~ $comparator_regex ]]
case ${BASH_REMATCH[1]} in
"=")
[[ ! $(hostname) = "$3" ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
;;
"~")
[[ ! $(hostname) =~ $3 ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
esac
}
#$1 arguments
#$2 comparator
#$3 matcher
system_conditional(){
[[ $2 =~ "!" ]]; local not=$?
[[ $2 =~ $comparator_regex ]]
case ${BASH_REMATCH[1]} in
"=")
[[ ! $(uname) = "$3" ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
;;
"~")
[[ ! $(uname) =~ $3 ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
esac
}
#$1 arguments
#$2 comparator
#$3 matcher
arch_conditional(){
[[ $2 =~ "!" ]]; local not=$?
[[ $2 =~ $comparator_regex ]]
case ${BASH_REMATCH[1]} in
"=")
[[ ! $(uname -m) = "$3" ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
;;
"~")
[[ ! $(uname -m) =~ $3 ]]; local match=$?
if xor $match $not; then
: $((engine_no_process_depth++))
fi
esac
}
#$1 line to check for conditional
read_conditional() {
local conditional_regex="#\\?([^=~! ]+) ?([^=~!]*)(!?[=~])(.+)"
if [[ $1 =~ $conditional_regex ]]; then
local conditional="${BASH_REMATCH[1]}"
local arguments="${BASH_REMATCH[2]}"
local comparator="${BASH_REMATCH[3]}"
local matcher="${BASH_REMATCH[4]}"
if declare -f -F "$conditional"_conditional > /dev/null; then
"$conditional"_conditional "$arguments" "$comparator" "$matcher"
fi
fi
}
#################
# File Rotation #
#################
rotate_files() {
local n="${processor["output-rotation"]}"
if [[ $n != 0 ]]; then
if [ -f "${processor["output-location"]}" ]; then
mv "${processor["output-location"]}" "${processor["output-location"]}0"
fi
else
if [ -f "${processor["output-location"]}" ]; then
rm "${processor["output-location"]}" #just get rid of it
fi
fi
while [[ $((n-- > 0)) = 1 ]]; do
if [ -f "${processor["output-location"]}$n" ]; then
mv "${processor["output-location"]}$n" "${processor["output-location"]}$((n+1))"
fi
done
}
#############
# Scripting #
#############
#$1 line to process insertions
process_insertions() {
if [[ ${processor["scripts-enabled"]} = "0" ]]; then
echo "$line"
return 1
fi
local line=$1
local regex="^(.*)<#(.*)#>(.*)\$"
while [[ $line =~ $regex ]]; do
local evaled
evaled=$(eval "${BASH_REMATCH[2]}")
line="${BASH_REMATCH[1]}$evaled${BASH_REMATCH[3]}"
done
echo "$line"
}
##########
# Output #
##########
#$1 line to output as enabled
output_enabled() {
engine_output+=("$1")
}
#$1 line to output as disabled
output_disabled() {
local udp_no_output_regex="(?:Mini|Pico)"
if [[ ${globals["UDPOutput"]} =~ $udp_no_output_regex ]]; then
return 2
fi
local commented_regex="^${globals["UDPComment"]}"
if [[ $1 =~ $commented_regex ]]; then
engine_output+=("$1")
return 1
fi
local chars_regex="Chars;?(.*)"
if [[ ${processor["output-comments"]} =~ "Spaced" ]]; then
engine_output+=("${globals["UDPComment"]} $1")
elif [[ "${processor["output-comments"]}" =~ $chars_regex ]]; then
engine_output+=("${globals["UDPComment"]}${BASH_REMATCH[1]}$1")
else # None or any invalid
engine_output+=("${globals["UDPComment"]}$1")
fi
}
#$1 line to output
output() {
if [[ $engine_no_process_depth -eq 0 ]]; then
local out
out=$(process_insertions "$1")
output_enabled "$out"
else
output_disabled "$1"
fi
}
###############
# Debug State #
###############
echo_globals() {
for global in "${!globals[@]}"; do
echo "Global \"$global\"=\"${globals[$global]}\""
done
}
echo_processor_state() {
for pkey in "${!processor[@]}"; do
echo "Processor \"$pkey\"=\"${processor[$pkey]}\""
done
}
echo_engine() {
echo "Engine State: $engine_state"
if [[ $engine_interactive -eq 0 ]]; then
echo "Engine in non-interactive mode. REPL unavailable"
else
echo "Engine in interactive mode. REPL available"
fi
echo "Engine no process depth=$engine_no_process_depth"
}
debug_state() {
echo_engine
echo "Dumping Globals"
echo_globals
echo "Dumping Processor State"
echo_processor_state
}
help_udp() {
echo "Current Engine State \"$engine_state\""
echo
echo "Variables"
echo
echo "engine_state - String. Stores current engine state"
echo "engine_interactive - 0/1. Stores if a REPL can be safely started"
echo "engine_no_process_depth - Numeric. Stores how deeply nested into a failing if we are"
echo "globals - Bash Associative Array. Stores all global variables"
echo "processor - Bash associative Arry. Stores current processor state"
echo
echo "Debugging Functions"
echo
echo "help_udp - display this help prompt"
echo "debug_state - dumps entire process state"
echo "echo_engine - dumps current engine information"
echo "echo_globals - dumps all set global variables"
echo "echo_processor_state - dumps all pre/postprocessor variables"
echo
echo "exit - exit interactive debugging"
}
debug_repl() {
if [[ $engine_interactive -ne 1 ]]; then
echo "Interactive debugging not available during engine state \"$engine_state\""
else
echo "Entering interactive debugging"
echo
help_udp
echo
while true; do
read -rp "UDP$ " command
if [[ $command =~ "exit" ]]; then
echo "Exiting prompt"
return 0
fi
eval "$command"
done
fi
}
#$1 line to check for debug statements
debug_line() {
local regex="#\\?(..)\\?# ?(.*)"
if ! is_comment "$1" "$regex" && [[ $1 =~ $regex ]]; then
if [[ $engine_no_process_depth -ne 0 ]]; then
echo "Debugging inside failed if statement!"
fi
case ${BASH_REMATCH[1]} in
'DS')
debug_state
;;
'DG')
echo "Dumping Globals"
echo_globals
;;
'DP')
echo "Dumping Processor State"
echo_processor_state
;;
'DI')
debug_repl
;;
'DE')
echo_engine
;;
'DM')
echo "Reached Mark ${BASH_REMATCH[2]} while in engine_state $engine_state"
;;
*)
echo "Unknown debug option \"${BASH_REMATCH[1]}\""
;;
esac
fi
}
###############
# Early Exits #
###############
# Invalid command early exit
if [[ $# -eq 0 ]]; then
echo 'To few arguments'
help
exit 10
fi
# Invalid file early exit
if [[ ! -f $1 ]]; then
echo "file \"$1\" does not exist"
exit 12
fi
########
# Main #
########
load_file "$1"
load_globals
preprocessor
postprocessor