forked from brltty/brltty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brltty-prologue.sh
838 lines (657 loc) · 20 KB
/
brltty-prologue.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#!/bin/sh
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
# text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2024 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <[email protected]>.
###############################################################################
testMode=false
readonly initialDirectory="$(pwd)"
readonly programName="$(basename "${0}")"
programMessage() {
local message="${1}"
[ -z "${message}" ] || echo >&2 "${programName}: ${message}"
}
setVariable() {
eval "${1}"'="${2}"'
}
getVariable() {
if [ -n "${2}" ]
then
eval "${2}"'="${'"${1}"'}"'
else
eval 'echo "${'"${1}"'}"'
fi
}
defineEnumeration() {
local prefix="${1}"
shift 1
local name
local value=1
for name
do
local variable="${prefix}${name}"
readonly "${variable}"="${value}"
value=$((value + 1))
done
}
defineEnumeration programLogLevel_ error warning notice task note detail
programLogLevel=$((${programLogLevel_task}))
logMessage() {
local level="${1}"
local message="${2}"
local variable="programLogLevel_${level}"
local value=$((${variable}))
[ "${value}" -gt 0 ] || programMessage "unknown log level: ${level}"
[ "${value}" -gt "${programLogLevel}" ] || programMessage "${message}"
}
logError() {
logMessage error "${@}"
}
logWarning() {
logMessage warning "${@}"
}
logNotice() {
logMessage notice "${@}"
}
logTask() {
logMessage task "${@}"
}
logNote() {
logMessage note "${@}"
}
logDetail() {
logMessage detail "${@}"
}
programTerminationCommandCount=0
runProgramTerminationCommands() {
set +e
while [ "${programTerminationCommandCount}" -gt 0 ]
do
set -- $(getVariable "programTerminationCommand${programTerminationCommandCount}")
programTerminationCommandCount=$((programTerminationCommandCount - 1))
local process="${1}"
local directory="${2}"
shift 2
[ "${process}" = "${$}" ] && {
cd "${directory}"
"${@}"
}
done
}
pushProgramTerminationCommand() {
[ "${programTerminationCommandCount}" -gt 0 ] || trap runProgramTerminationCommands exit
setVariable "programTerminationCommand$((programTerminationCommandCount += 1))" "${$} $(pwd) ${*}"
}
needTemporaryDirectory() {
local variable="${1:-temporaryDirectory}"
local _directory
getVariable "${variable}" _directory
[ -n "${_directory}" ] || {
[ -n "${TMPDIR}" -a -d "${TMPDIR}" -a -r "${TMPDIR}" -a -w "${TMPDIR}" -a -x "${TMPDIR}" ] || export TMPDIR="/tmp"
_directory="$(mktemp -d "${TMPDIR}/${programName}.$(date +"%Y%m%d-%H%M%S").XXXXXX")"
pushProgramTerminationCommand rm -f -r -- "${_directory}"
cd "${_directory}"
setVariable "${variable}" "${_directory}"
}
}
resolveDirectory() {
local path="${1}"
local variable="${2}"
local absolute="$(cd "${path}" && pwd)"
if [ -n "${variable}" ]
then
setVariable "${variable}" "${absolute}"
else
echo "${absolute}"
fi
}
programDirectory="$(dirname "${0}")"
readonly programDirectory="$(resolveDirectory "${programDirectory}")"
parseParameterString() {
local valuesArray="${1}"
local parameters="${2}"
local code="${3}"
set -- ${parameters//,/ }
local parameter
for parameter
do
local name="${parameter%%=*}"
[ "${name}" = "${parameter}" ] && continue
[ -n "${name}" ] || continue
local value="${parameter#*=}"
local qualifier="${name%%:*}"
[ "${qualifier}" = "${name}" ] || {
[ -n "${qualifier}" ] || continue
[ "${qualifier}" = "${code}" ] || continue
name="${name#*:}"
}
setVariable "${valuesArray}[${name^^*}]" "${value}"
done
}
stringHead() {
local string="${1}"
local length="${2}"
[ "${length}" -eq 0 ] || expr substr "${string}" 1 "${length}"
}
stringTail() {
local string="${1}"
local start="${2}"
local length=$((${#string} - start))
[ "${length}" -eq 0 ] || expr substr "${string}" $((start + 1)) "${length}"
}
stringReplace() {
local string="${1}"
local pattern="${2}"
local replacement="${3}"
local flags="${4}"
echo "${string}" | sed -e "s/${pattern}/${replacement}/${flags}"
}
stringReplaceAll() {
local string="${1}"
local pattern="${2}"
local replacement="${3}"
stringReplace "${string}" "${pattern}" "${replacement}" "g"
}
stringQuoted() {
local string="${1}"
local pattern="'"
local replacement="'"'"'"'"'"'"'"
string="$(stringReplaceAll "${string}" "${pattern}" "${replacement}")"
echo "'${string}'"
}
stringWrapped() {
local string="${1}"
local width="${2}"
local result=""
local paragraph=""
while true
do
local length="$(expr "${string}" : $'[^\n]*\n')"
local line
if [ "${length}" -eq 0 ]
then
line="${string}"
string=""
else
line="$(stringHead "${string}" $((length - 1)))"
string="$(stringTail "${string}" "${length}")"
fi
[ -z "${line}" ] || [ "${line}" != "${line# }" ] || {
[ -z "${paragraph}" ] || paragraph="${paragraph} "
paragraph="${paragraph}${line}"
continue
}
while [ "${#paragraph}" -gt "${width}" ]
do
local head="$(stringHead "${paragraph}" $((width + 1)))"
head="${head% *}"
[ "${#head}" -le "${width}" ] || {
head="${paragraph%% *}"
[ "${head}" != "${paragraph}" ] || break
}
result="${result} $(stringQuoted "${head}")"
paragraph="$(stringTail "${paragraph}" $((${#head} + 1)))"
done
[ -z "${paragraph}" ] || {
result="${result} $(stringQuoted "${paragraph}")"
paragraph=""
}
[ -n "${string}" ] || {
[ -z "${line}" ] || result="${result} $(stringQuoted "${line}")"
break
}
result="${result} $(stringQuoted "${line}")"
done
echo "${result}"
}
syntaxError() {
local message="${1}"
logError "${message}"
exit 2
}
semanticError() {
local message="${1}"
logError "${message}"
exit 3
}
internalError() {
local message="${1}"
logError "${message}"
exit 4
}
findSiblingCommand() {
local resultVariable="${1}"
shift 1
local command
for command in "${@}"
do
local path="${programDirectory}/${command}"
[ -f "${path}" ] || continue
[ -x "${path}" ] || continue
setVariable "${resultVariable}" "${path}"
return 0
done
return 1
}
findHostCommand() {
local pathVariable="${1}"
local command="${2}"
local path="$(which "${command}")"
[ -n "${path}" ] || return 1
setVariable "${pathVariable}" "${path}"
return 0
}
verifyHostCommand() {
local pathVariable="${1}"
local command="${2}"
findHostCommand "${pathVariable}" "${command}" || {
semanticError "host command not found: ${command}"
}
}
executeHostCommand() {
logDetail "executing host command: ${*}"
"${testMode}" || "${@}" || {
local status="${?}"
logWarning "host command failed with exit status ${status}: ${*}"
return "${status}"
}
}
verifyActionFlags() {
local allFlag="${1}"
shift 1
local allRequested
getVariable "${allFlag}" allRequested
local actionFlag
for actionFlag in "${@}"
do
local actionRequested
getVariable "${actionFlag}" actionRequested
"${actionRequested}" && {
"${allRequested}" && syntaxError "conflicting actions"
return
}
done
"${allRequested}" || syntaxError "no actions"
for actionFlag in "${@}"
do
setVariable "${actionFlag}" true
done
}
testInteger() {
local value="${1}"
[ "${value}" = "0" ] || {
value="${value#-}"
[ -n "${value}" ] || return 1
[ "$(expr "${value}" : '^[1-9][0-9]*$')" -eq "${#value}" ] || return 1
}
return 0
}
verifyInteger() {
local label="${1}"
local value="${2}"
local minimum="${3}"
local maximum="${4}"
testInteger "${value}" || {
semanticError "${label} not an integer: ${value}"
}
[ -n "${minimum}" ] && {
[ "${value}" -lt "${minimum}" ] && {
semanticError "${label} out of range: ${value} < ${minimum}"
}
}
[ -n "${maximum}" ] && {
[ "${value}" -gt "${maximum}" ] && {
semanticError "${label} out of range: ${value} > ${maximum}"
}
}
}
testContainingDirectory() {
local directory="${1}"
shift 1
local path
for path
do
[ -e "${directory}/${path}" ] || return 1
done
return 0
}
findContainingDirectory() {
local variable="${1}"
local directory="${2}"
shift 2
local value
getVariable "${variable}" value
[ -n "${value}" ] && return 0
while :
do
testContainingDirectory "${directory}" "${@}" && break
local parent="$(dirname "${directory}")"
[ "${parent}" = "${directory}" ] && return 1
directory="${parent}"
done
export "${variable}"="${directory}"
}
testDirectory() {
local path="${1}"
[ -e "${path}" ] || return 1
[ -d "${path}" ] || semanticError "not a directory: ${path}"
return 0
}
verifyWritableDirectory() {
local path="${1}"
testDirectory "${path}" || semanticError "directory not found: ${path}"
[ -w "${path}" ] || semanticError "directory not writable: ${path}"
}
testFile() {
local path="${1}"
[ -e "${path}" ] || return 1
[ -f "${path}" ] || semanticError "not a file: ${path}"
return 0
}
verifyInputFile() {
local path="${1}"
testFile "${path}" || semanticError "file not found: ${path}"
[ -r "${path}" ] || semanticError "file not readable: ${path}"
}
verifyOutputFile() {
local path="${1}"
if testFile "${path}"
then
[ -w "${path}" ] || semanticError "file not writable: ${path}"
else
verifyWritableDirectory "$(dirname "${path}")"
fi
}
verifyExecutableFile() {
local path="${1}"
testFile "${path}" || semanticError "file not found: ${path}"
[ -x "${path}" ] || semanticError "file not executable: ${path}"
}
verifyInputDirectory() {
local path="${1}"
testDirectory "${path}" || semanticError "directory not found: ${path}"
}
verifyOutputDirectory() {
local path="${1}"
if testDirectory "${path}"
then
[ -w "${path}" ] || semanticError "directory not writable: ${path}"
rm -f -r -- "${path}/"*
else
mkdir -p "${path}"
fi
}
programParameterCount=0
programParameterCountMinimum=-1
programParameterLabelWidth=0
addProgramParameter() {
local label="${1}"
local variable="${2}"
local usage="${3}"
local default="${4}"
setVariable "programParameterLabel_${programParameterCount}" "${label}"
setVariable "programParameterVariable_${programParameterCount}" "${variable}"
setVariable "programParameterUsage_${programParameterCount}" "${usage}"
setVariable "programParameterDefault_${programParameterCount}" "${default}"
local length="${#label}"
[ "${length}" -le "${programParameterLabelWidth}" ] || programParameterLabelWidth="${length}"
setVariable "${variable}" ""
programParameterCount=$((programParameterCount + 1))
}
optionalProgramParameters() {
if [ "${programParameterCountMinimum}" -lt 0 ]
then
programParameterCountMinimum="${programParameterCount}"
optionalProgramParameterLabel="${1}"
optionalProgramParameterUsage="${2}"
else
logWarning "program parameters are already optional"
fi
}
tooManyProgramParameters() {
syntaxError "too many parameters"
}
programOptionLetters=""
programOptionString=""
programOptionOperandWidth=0
programOptionValue_counter=0
programOptionValue_flag=false
programOptionValue_list=""
programOptionValue_string=""
addProgramOption() {
local letter="${1}"
local type="${2}"
local variable="${3}"
local usage="${4}"
local default="${5}"
[ "$(expr "${letter}" : '[[:alnum:]]*$')" -eq 1 ] || internalError "invalid program option: -${letter}"
[ -z "$(getVariable "programOptionType_${letter}")" ] || internalError "duplicate program option definition: -${letter}"
local operand
case "${type}"
in
flag | counter)
operand=""
;;
string.* | list.*)
operand="${type#*.}"
type="${type%%.*}"
[ -n "${operand}" ] || internalError "missing program option operand type: -${letter}"
;;
*) internalError "invalid program option type: ${type} (-${letter})";;
esac
setVariable "programOptionType_${letter}" "${type}"
setVariable "programOptionVariable_${letter}" "${variable}"
setVariable "programOptionOperand_${letter}" "${operand}"
setVariable "programOptionUsage_${letter}" "${usage}"
setVariable "programOptionDefault_${letter}" "${default}"
local value="$(getVariable "programOptionValue_${type}")"
setVariable "${variable}" "${value}"
local length="${#operand}"
[ "${length}" -le "${programOptionOperandWidth}" ] || programOptionOperandWidth="${length}"
programOptionLetters="${programOptionLetters} ${letter}"
programOptionString="${programOptionString}${letter}"
[ "${length}" -eq 0 ] || programOptionString="${programOptionString}:"
}
addTestModeOption() {
addProgramOption t flag testMode "test mode - log (but don't execute) the host commands"
}
programUsageLineCount=0
programUsageLineWidth="${COLUMNS:-72}"
addProgramUsageLine() {
local line="${1}"
setVariable "programUsageLine_${programUsageLineCount}" "${line}"
programUsageLineCount=$((programUsageLineCount + 1))
}
addProgramUsageText() {
local text="${1}"
local prefix="${2}"
local width=$((programUsageLineWidth - ${#prefix}))
while [ "${width}" -lt 1 ]
do
[ "${prefix% }" != "${prefix}" ] || break
prefix="${prefix%?}"
width=$((width + 1))
done
local indent="$(stringReplaceAll "${prefix}" '.' ' ')"
[ "${width}" -gt 0 ] || {
addProgramUsageLine "${prefix}"
indent="$(stringTail "${indent}" $((-width + 1)))"
prefix="${indent}"
width=1
}
eval set -- "$(stringWrapped "${text}" "${width}")"
for line
do
addProgramUsageLine "${prefix}${line}"
prefix="${indent}"
done
}
writeProgramUsageLines() {
local index=0
while [ "${index}" -lt "${programUsageLineCount}" ]
do
getVariable "programUsageLine_${index}"
index=$((index + 1))
done
}
showProgramUsageSummary() {
set -- ${programOptionLetters}
local purpose="$(showProgramUsagePurpose)"
[ -z "${purpose}" ] || {
addProgramUsageText "${purpose}"
addProgramUsageLine
}
local line="Syntax: ${programName}"
[ "${#}" -eq 0 ] || line="${line} [-option ...]"
local index=0
local suffix=""
while [ "${index}" -lt "${programParameterCount}" ]
do
line="${line} "
[ "${index}" -lt "${programParameterCountMinimum}" ] || {
line="${line}["
suffix="${suffix}]"
}
line="${line}$(getVariable "programParameterLabel_${index}")"
index=$((index + 1))
done
[ -z "${optionalProgramParameterLabel}" ] || {
line="${line} [${optionalProgramParameterLabel} ...]"
[ -z "${optionalProgramParameterUsage}" ] || {
addProgramParameter "${optionalProgramParameterLabel} ..." optionalProgramParameterVariable "${optionalProgramParameterUsage}"
}
}
line="${line}${suffix}"
addProgramUsageLine "${line}"
[ "${programParameterCount}" -eq 0 ] || {
addProgramUsageLine
addProgramUsageLine "Parameters:"
local indent=$((programParameterLabelWidth + 2))
local index=0
while [ "${index}" -lt "${programParameterCount}" ]
do
local line="$(getVariable "programParameterLabel_${index}")"
while [ "${#line}" -lt "${indent}" ]
do
line="${line} "
done
local usage="$(getVariable "programParameterUsage_${index}")"
local default="$(getVariable "programParameterDefault_${index}")"
[ -z "${default}" ] || usage="${usage} - the default is ${default}"
addProgramUsageText "${usage}" " ${line}"
index=$((index + 1))
done
}
[ "${#}" -eq 0 ] || {
addProgramUsageLine
addProgramUsageLine "Options:"
local indent=$((3 + programOptionOperandWidth + 2))
local letter
for letter
do
local line="-${letter} $(getVariable "programOptionOperand_${letter}")"
while [ "${#line}" -lt "${indent}" ]
do
line="${line} "
done
usage="$(getVariable "programOptionUsage_${letter}")"
local default="$(getVariable "programOptionDefault_${letter}")"
[ -z "${default}" ] || usage="${usage} - the default is ${default}"
addProgramUsageText "${usage}" " ${line}"
done
}
local notes="$(showProgramUsageNotes)"
[ -z "${notes}" ] || {
addProgramUsageLine
addProgramUsageText "${notes}"
}
writeProgramUsageLines
}
addProgramOption h flag programOption_showUsageSummary "show this usage summary, and then exit"
addProgramOption q counter programOption_quietCount "decrease output verbosity"
addProgramOption v counter programOption_verboseCount "increase output verbosity"
parseProgramOptions() {
local letter
while getopts ":${programOptionString}" letter
do
case "${letter}"
in
\?) syntaxError "unrecognized option: -${OPTARG}";;
:) syntaxError "missing operand: -${OPTARG}";;
*) local variable type
setVariable variable "$(getVariable "programOptionVariable_${letter}")"
setVariable type "$(getVariable "programOptionType_${letter}")"
case "${type}"
in
counter) setVariable "${variable}" $((${variable} + 1));;
flag) setVariable "${variable}" true;;
list) setVariable "${variable}" "$(getVariable "${variable}") $(stringQuoted "${OPTARG}")";;
string) setVariable "${variable}" "${OPTARG}";;
*) internalError "unimplemented program option type: ${type} (-${letter})";;
esac
;;
esac
done
}
parseProgramArguments() {
[ "${programParameterCountMinimum}" -ge 0 ] || programParameterCountMinimum="${programParameterCount}"
parseProgramOptions "${@}"
shift $((OPTIND - 1))
if "${programOption_showUsageSummary}"
then
showProgramUsageSummary
exit 0
fi
local programParameterIndex=0
while [ "${#}" -gt 0 ]
do
[ "${programParameterIndex}" -lt "${programParameterCount}" ] || break
setVariable "$(getVariable "programParameterVariable_${programParameterIndex}")" "${1}"
shift 1
programParameterIndex=$((programParameterIndex + 1))
done
[ "${programParameterIndex}" -ge "${programParameterCountMinimum}" ] || {
syntaxError "$(getVariable "programParameterLabel_${programParameterIndex}") not specified"
}
readonly programLogLevel=$((programLogLevel + programOption_verboseCount - programOption_quietCount))
processExtraProgramParameters "${@}"
}
handleInitialHelpOption() {
if [ "${#}" -gt 0 ]
then
if [ "${1}" = "-h" ]
then
addProgramUsageText "$(cat)"
writeProgramUsageLines
exit 0
fi
fi
}
####################################################################
# The following functions are stubs that may be copied into the #
# main script and augmented. They need to be defined after this #
# prologue is embeded and before the program arguments are parsed. #
####################################################################
showProgramUsagePurpose() {
cat <<END_OF_PROGRAM_USAGE_PURPOSE
END_OF_PROGRAM_USAGE_PURPOSE
}
showProgramUsageNotes() {
cat <<END_OF_PROGRAM_USAGE_NOTES
END_OF_PROGRAM_USAGE_NOTES
}
processExtraProgramParameters() {
[ "${#}" -eq 0 ] || tooManyProgramParameters
}