-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
util.sh
3198 lines (2942 loc) · 98.7 KB
/
util.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- mode:sh;mode:sh-bash -*-
# bash script to be sourced from interactive shell
#------------------------------------------------------------------------------
# ble.sh options
## 関数 bleopt args...
## @params[in] args
## args は以下の内の何れかの形式を持つ。
##
## var=value
## 既存の設定変数に値を設定する。
## 設定変数が存在しないときはエラー。
## var:=value
## 設定変数に値を設定する。
## 設定変数が存在しないときは新しく作成する。
## var
## 変数の設定内容を表示する
##
function bleopt {
local error_flag=
local -a pvars
if (($#==0)); then
pvars=("${!bleopt_@}")
else
local spec var type= value= ip=0 rex
pvars=()
for spec; do
if rex='^[[:alnum:]_]+:='; [[ $spec =~ $rex ]]; then
type=a var=${spec%%:=*} value=${spec#*:=}
elif rex='^[[:alnum:]_]+='; [[ $spec =~ $rex ]]; then
type=ac var=${spec%%=*} value=${spec#*=}
elif rex='^[[:alnum:]_]+$'; [[ $spec =~ $rex ]]; then
type=p var=$spec
else
ble/bin/echo "bleopt: unrecognized argument '$spec'" >&2
continue
fi
var=bleopt_${var#bleopt_}
if [[ $type == *c* && ! ${!var+set} ]]; then
error_flag=1
ble/bin/echo "bleopt: unknown bleopt option \`${var#bleopt_}'" >&2
continue
fi
case "$type" in
(a*)
[[ ${!var} == "$value" ]] && continue
if ble/is-function bleopt/check:"${var#bleopt_}"; then
if ! bleopt/check:"${var#bleopt_}"; then
error_flag=1
continue
fi
fi
eval "$var=\"\$value\"" ;;
(p*) pvars[ip++]=$var ;;
(*) ble/bin/echo "bleopt: unknown type '$type' of the argument \`$spec'" >&2 ;;
esac
done
fi
if ((${#pvars[@]})); then
local q="'" Q="'\''" var
for var in "${pvars[@]}"; do
if [[ ${!var+set} ]]; then
builtin printf '%s\n' "bleopt ${var#bleopt_}='${!var//$q/$Q}'"
else
builtin printf '%s\n' "bleopt: invalid ble option name '${var#bleopt_}'" >&2
fi
done
fi
[[ ! $error_flag ]]
}
function bleopt/declare {
local type=$1 name=bleopt_$2 default_value=$3
if [[ $type == -n ]]; then
eval ": \"\${$name:=\$default_value}\""
else
eval ": \"\${$name=\$default_value}\""
fi
return 0
}
## オプション input_encoding
bleopt/declare -n input_encoding UTF-8
function bleopt/check:input_encoding {
if ! ble/is-function "ble/encoding:$value/decode"; then
ble/bin/echo "bleopt: Invalid value input_encoding='$value'." \
"A function 'ble/encoding:$value/decode' is not defined." >&2
return 1
elif ! ble/is-function "ble/encoding:$value/b2c"; then
ble/bin/echo "bleopt: Invalid value input_encoding='$value'." \
"A function 'ble/encoding:$value/b2c' is not defined." >&2
return 1
elif ! ble/is-function "ble/encoding:$value/c2bc"; then
ble/bin/echo "bleopt: Invalid value input_encoding='$value'." \
"A function 'ble/encoding:$value/c2bc' is not defined." >&2
return 1
elif ! ble/is-function "ble/encoding:$value/generate-binder"; then
ble/bin/echo "bleopt: Invalid value input_encoding='$value'." \
"A function 'ble/encoding:$value/generate-binder' is not defined." >&2
return 1
elif ! ble/is-function "ble/encoding:$value/is-intermediate"; then
ble/bin/echo "bleopt: Invalid value input_encoding='$value'." \
"A function 'ble/encoding:$value/is-intermediate' is not defined." >&2
return 1
fi
# Note: ble/encoding:$value/clear は optional な設定である。
if [[ $bleopt_input_encoding != "$value" ]]; then
ble-decode/unbind
bleopt_input_encoding=$value
ble-decode/bind
fi
return 0
}
## オプション internal_stackdump_enabled
## エラーが起こった時に関数呼出の構造を標準エラー出力に出力するかどうかを制御する。
## 算術式評価によって非零の値になる場合にエラーを出力する。
## それ以外の場合にはエラーを出力しない。
bleopt/declare -v internal_stackdump_enabled 0
## オプション openat_base
## bash-4.1 未満で exec {var}>foo が使えない時に ble.sh で内部的に fd を割り当てる。
## この時の fd の base を指定する。bleopt_openat_base, bleopt_openat_base+1, ...
## という具合に順番に使用される。既定値は 30 である。
bleopt/declare -n openat_base 30
## オプション pager
bleopt/declare -v pager ''
shopt -s checkwinsize
#------------------------------------------------------------------------------
# util
## @var _ble_util_upvar_setup
## @var _ble_util_upvar
##
## これらの変数は関数を定義する時に [-v varname] の引数を認識させ、
## 関数の結果を格納する変数名を外部から指定できるようにするのに用いる。
## 使用する際は関数を以下の様に記述する。既定の格納先変数は ret となる。
##
## function MyFunction {
## eval "$_ble_util_upvar_setup"
##
## ret=... # 処理を行い、変数 ret に結果を格納するコード
## # (途中で return などすると正しく動かない事に注意)
##
## eval "$_ble_util_upvar"
## }
##
## 既定の格納先変数を別の名前 (以下の例では arg) にする場合は次の様にする。
##
## function MyFunction {
## eval "${_ble_util_upvar_setup//ret/arg}"
##
## arg=... # 処理を行い、変数 arg に結果を格納するコード
##
## eval "${_ble_util_upvar//ret/arg}"
## }
##
_ble_util_upvar_setup='local var=ret ret; [[ $1 == -v ]] && var=$2 && shift 2'
_ble_util_upvar='local "${var%%\[*\]}" && ble/util/upvar "$var" "$ret"'
if ((_ble_bash>=50000)); then
function ble/util/unlocal {
if shopt -q localvar_unset; then
shopt -u localvar_unset
builtin unset -v "$@"
shopt -s localvar_unset
else
builtin unset -v "$@"
fi
}
function ble/util/upvar { ble/util/unlocal "${1%%\[*\]}" && builtin eval "$1=\"\$2\""; }
function ble/util/uparr { ble/util/unlocal "$1" && builtin eval "$1=(\"\${@:2}\")"; }
else
function ble/util/unlocal { builtin unset -v "$@"; }
function ble/util/upvar { builtin unset -v "${1%%\[*\]}" && builtin eval "$1=\"\$2\""; }
function ble/util/uparr { builtin unset -v "$1" && builtin eval "$1=(\"\${@:2}\")"; }
fi
function ble/util/save-vars {
local name prefix=$1; shift
for name; do eval "$prefix$name=\"\$$name\""; done
}
function ble/util/save-arrs {
local name prefix=$1; shift
for name; do eval "$prefix$name=(\"\${$name[@]}\")"; done
}
function ble/util/restore-vars {
local name prefix=$1; shift
for name; do eval "$name=\"\$$prefix$name\""; done
}
function ble/util/restore-arrs {
local name prefix=$1; shift
for name; do eval "$name=(\"\${$prefix$name[@]}\")"; done
}
#%if !release
## 関数 ble/debug/.check-leak-variable
## [デバグ用] 宣言忘れに依るグローバル変数の汚染位置を特定するための関数。
##
## 使い方
##
## ```
## eval "${_ble_debug_check_leak_variable//@var/ret}"
## ...codes1...
## ble/util/.check-leak-variable ret tag1
## ...codes2...
## ble/util/.check-leak-variable ret tag2
## ...codes3...
## ble/util/.check-leak-variable ret tag3
## ```
_ble_debug_check_leak_variable='local @var=__t1wJltaP9nmow__'
function ble/debug/.check-leak-variable {
if [[ ${!1} != __t1wJltaP9nmow__ ]]; then
ble/bin/echo "$1=${!1}:${2:*}" >> a.txt
builtin eval "$1=__t1wJltaP9nmow__"
fi
}
function ble/debug/print-variables/.append {
local q=\' Q="'\''"
_ble_local_out=$_ble_local_out"$1='${2//$q/$Q}'"
}
function ble/debug/print-variables/.append-array {
local q=\' Q="''\'" arr=$1 index=0; shift
local index=0 elem out=$arr'=('
for elem; do
((index++)) && out=$out' '
out=$out$q${elem//$q/$Q}$q
done
out=$out')'
_ble_local_out=$_ble_local_out$out
}
function ble/debug/print-variables {
(($#)) || return 0
local flags= tag=
local -a _ble_local_vars=()
while (($#)); do
local arg=$1; shift
case $arg in
(-t) tag=$1; shift ;;
(-*) ble/bin/echo "print-variables: unknown option '$arg'" >&2
flags=${flags}e ;;
(*) ble/array#push _ble_local_vars "$arg" ;;
esac
done
[[ $flags == *e* ]] && return 1
local _ble_local_out= _ble_local_var=
[[ $tag ]] && _ble_local_out="$tag: "
ble/util/unlocal flags tag arg
for _ble_local_var in "${_ble_local_vars[@]}"; do
if ble/is-array "$_ble_local_var"; then
builtin eval -- "ble/debug/print-variables/.append-array \"\$_ble_local_var\" \"\${$_ble_local_var[@]}\""
else
ble/debug/print-variables/.append "$_ble_local_var" "${!_ble_local_var}"
fi
_ble_local_out=$_ble_local_out' '
done
ble/bin/echo "${_ble_local_out%' '}"
}
#%end
#
# array and strings
#
_ble_array_prototype=()
function ble/array#reserve-prototype {
local n=$1 i
for ((i=${#_ble_array_prototype[@]};i<n;i++)); do
_ble_array_prototype[i]=
done
}
## 関数 ble/is-array arr
##
## Note: これに関しては様々な実現方法が考えられるが大体余りうまく動かない。
##
## * ! declare +a arr だと現在の関数のローカル変数の判定になってしまう。
## * bash-4.2 以降では ! declare -g +a arr を使えるが、
## これだと呼び出し元の関数で定義されている配列が見えない。
## というか現在のスコープの配列も見えない。
## * 今の所は compgen -A arrayvar を用いているが、
## この方法だと bash-4.3 以降では連想配列も配列と判定され、
## bash-4.2 以下では連想配列は配列とはならない。
if ((_ble_bash>=40400)); then
function ble/is-array { [[ ${!1@a} == *a* ]]; }
else
function ble/is-array { compgen -A arrayvar -X \!"$1" "$1" &>/dev/null; }
fi
## 関数 ble/array#push arr value...
if ((_ble_bash>=30100)); then
function ble/array#push {
builtin eval "$1+=(\"\${@:2}\")"
}
else
function ble/array#push {
while (($#>=2)); do
builtin eval "$1[\${#$1[@]}]=\"\$2\""
set -- "$1" "${@:3}"
done
}
fi
## 関数 ble/array#pop arr
## @var[out] ret
function ble/array#pop {
eval "local i$1=\$((\${#$1[@]}-1))"
if ((i$1>=0)); then
eval "ret=\${$1[i$1]}"
unset -v "$1[i$1]"
else
ret=
fi
}
## 関数 ble/array#unshift arr value...
function ble/array#unshift {
builtin eval "$1=(\"\${@:2}\" \"\${$1[@]}\")"
}
## 関数 ble/array#reverse arr
function ble/array#reverse {
builtin eval "
set -- \"\${$1[@]}\"; $1=()
local e$1 i$1=\$#
for e$1; do $1[--i$1]=\"\$e$1\"; done"
}
## 関数 ble/array#insert-at arr index elements...
function ble/array#insert-at {
builtin eval "$1=(\"\${$1[@]::$2}\" \"\${@:3}\" \"\${$1[@]:$2}\")"
}
## 関数 ble/array#insert-after arr needle elements...
function ble/array#insert-after {
local _ble_local_script='
local iARR=0 eARR aARR=
for eARR in "${ARR[@]}"; do
((iARR++))
[[ $eARR == "$2" ]] && aARR=iARR && break
done
[[ $aARR ]] && ble/array#insert-at "$1" "$aARR" "${@:3}"
'; builtin eval "${_ble_local_script//ARR/$1}"
}
## 関数 ble/array#insert-before arr needle elements...
function ble/array#insert-before {
local _ble_local_script='
local iARR=0 eARR aARR=
for eARR in "${ARR[@]}"; do
[[ $eARR == "$2" ]] && aARR=iARR && break
((iARR++))
done
[[ $aARR ]] && ble/array#insert-at "$1" "$aARR" "${@:3}"
'; builtin eval "${_ble_local_script//ARR/$1}"
}
## 関数 ble/array#remove arr element
function ble/array#remove {
local _ble_local_script='
local -a aARR=() eARR
for eARR in "${ARR[@]}"; do
[[ $eARR != "$2" ]] && ble/array#push "a$1" "$eARR"
done
ARR=(${ARR[@]})
'; builtin eval "${_ble_local_script//ARR/$1}"
}
## 関数 ble/array#index arr needle
## @var[out] ret
function ble/array#index {
local _ble_local_script='
local eARR iARR=0
for eARR in "${ARR[@]}"; do
[[ $eARR == "$2" ]] && { ret=$iARR; return 0; }
((iARR++))
done
ret=-1; return 1
'; builtin eval "${_ble_local_script//ARR/$1}"
}
## 関数 ble/array#last-index arr needle
## @var[out] ret
function ble/array#last-index {
local _ble_local_script='
local eARR iARR=${#ARR[@]}
while ((iARR--)); do
[[ ${ARR[iARR]} == "$2" ]] && { ret=$iARR; return 0; }
done
ret=-1; return 1
'; builtin eval "${_ble_local_script//ARR/$1}"
}
_ble_string_prototype=' '
function ble/string#reserve-prototype {
local n=$1 c
for ((c=${#_ble_string_prototype};c<n;c*=2)); do
_ble_string_prototype=$_ble_string_prototype$_ble_string_prototype
done
}
## 関数 ble/string#repeat str count
## @param[in] str
## @param[in] count
## @var[out] ret
function ble/string#repeat {
ble/string#reserve-prototype "$2"
ret=${_ble_string_prototype::$2}
ret="${ret// /$1}"
}
## 関数 ble/string#common-prefix a b
## @param[in] a b
## @var[out] ret
function ble/string#common-prefix {
local a=$1 b=$2
((${#a}>${#b})) && local a=$b b=$a
b=${b::${#a}}
if [[ $a == "$b" ]]; then
ret=$a
return
fi
# l <= 解 < u, (${a:u}: 一致しない, ${a:l} 一致する)
local l=0 u=${#a} m
while ((l+1<u)); do
((m=(l+u)/2))
if [[ ${a::m} == "${b::m}" ]]; then
((l=m))
else
((u=m))
fi
done
ret=${a::l}
}
## 関数 ble/string#common-suffix a b
## @param[in] a b
## @var[out] ret
function ble/string#common-suffix {
local a=$1 b=$2
((${#a}>${#b})) && local a=$b b=$a
b=${b:${#b}-${#a}}
if [[ $a == "$b" ]]; then
ret=$a
return
fi
# l < 解 <= u, (${a:l}: 一致しない, ${a:u} 一致する)
local l=0 u=${#a} m
while ((l+1<u)); do
((m=(l+u+1)/2))
if [[ ${a:m} == "${b:m}" ]]; then
((u=m))
else
((l=m))
fi
done
ret=${a:u}
}
## 関数 ble/string#split arr sep str...
## 文字列を分割します。
## 空白類を分割に用いた場合は、空要素は削除されます。
##
## @param[out] arr 分割した文字列を格納する配列名を指定します。
## @param[in] sep 分割に使用する文字を指定します。
## @param[in] str 分割する文字列を指定します。
##
function ble/string#split {
if [[ -o noglob ]]; then
# Note: 末尾の sep が無視されない様に、末尾に手で sep を 1 個追加している。
IFS=$2 builtin eval "$1=(\${*:3}\$2)"
else
set -f
IFS=$2 builtin eval "$1=(\${*:3}\$2)"
set +f
fi
}
function ble/string#split-words {
if [[ -o noglob ]]; then
IFS=$' \t\n' builtin eval "$1=(\${*:2})"
else
set -f
IFS=$' \t\n' builtin eval "$1=(\${*:2})"
set +f
fi
}
## 関数 ble/string#split-lines arr text...
## 文字列を行に分割します。空行も省略されません。
##
## @param[out] arr 分割した文字列を格納する配列名を指定します。
## @param[in] text 分割する文字列を指定します。
## @var[out] ret
##
if ((_ble_bash>=40000)); then
function ble/string#split-lines {
mapfile -t "$1" <<< "${*:2}"
}
else
function ble/string#split-lines {
ble/util/mapfile "$1" <<< "${*:2}"
}
fi
## 関数 ble/string#count-char text chars
## @param[in] text
## @param[in] chars
## 検索対象の文字の集合を指定します。
## @var[out] ret
function ble/string#count-char {
local text=$1 char=$2
text=${text//[!"$char"]}
ret=${#text}
}
## 関数 ble/string#count-string text string
## @var[out] ret
function ble/string#count-string {
local text=${1//"$2"}
((ret=(${#1}-${#text})/${#2}))
}
## 関数 ble/string#index-of text needle [n]
## @param[in] text
## @param[in] needle
## @param[in] n
## この引数を指定したとき n 番目の一致を検索します。
## @var[out] ret
## 一致した場合に見つかった位置を返します。
## 見つからなかった場合に -1 を返します。
## @exit
## 一致した場合に成功し、見つからなかった場合に失敗します。
function ble/string#index-of {
local haystack=$1 needle=$2 count=${3:-1}
ble/string#repeat '*"$needle"' "$count"; local pattern=$ret
eval "local transformed=\${haystack#$pattern}"
((ret=${#haystack}-${#transformed}-${#needle},
ret<0&&(ret=-1),ret>=0))
}
## 関数 ble/string#last-index-of text needle [n]
## @param[in] text
## @param[in] needle
## @param[in] n
## この引数を指定したとき n 番目の一致を検索します。
## @var[out] ret
function ble/string#last-index-of {
local haystack=$1 needle=$2 count=${3:-1}
ble/string#repeat '"$needle"*' "$count"; local pattern=$ret
eval "local transformed=\${haystack%$pattern}"
if [[ $transformed == "$haystack" ]]; then
ret=-1
else
ret=${#transformed}
fi
((ret>=0))
}
## 関数 ble/string#toggle-case text...
## 関数 ble/string#touppwer text...
## 関数 ble/string#tolower text...
## @param[in] text
## @var[out] ret
_ble_util_string_lower_list=abcdefghijklmnopqrstuvwxyz
_ble_util_string_upper_list=ABCDEFGHIJKLMNOPQRSTUVWXYZ
function ble/string#toggle-case {
local text=$* ch i
local -a buff
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if [[ $ch == [A-Z] ]]; then
ch=${_ble_util_string_upper_list%%"$ch"*}
ch=${_ble_util_string_lower_list:${#ch}:1}
elif [[ $ch == [a-z] ]]; then
ch=${_ble_util_string_lower_list%%"$ch"*}
ch=${_ble_util_string_upper_list:${#ch}:1}
fi
ble/array#push buff "$ch"
done
IFS= eval 'ret="${buff[*]-}"'
}
## 関数 ble/string#tolower text...
## 関数 ble/string#toupper text...
## @var[out] ret
if ((_ble_bash>=40000)); then
function ble/string#tolower { ret="${*,,}"; }
function ble/string#toupper { ret="${*^^}"; }
else
function ble/string#tolower {
local text="$*"
local -a buff ch
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if [[ $ch == [A-Z] ]]; then
ch=${_ble_util_string_upper_list%%"$ch"*}
ch=${_ble_util_string_lower_list:${#ch}:1}
fi
ble/array#push buff "$ch"
done
IFS= eval 'ret="${buff[*]-}"'
}
function ble/string#toupper {
local text="$*"
local -a buff ch
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if [[ $ch == [a-z] ]]; then
ch=${_ble_util_string_lower_list%%"$ch"*}
ch=${_ble_util_string_upper_list:${#ch}:1}
fi
ble/array#push buff "$ch"
done
IFS= eval 'ret="${buff[*]-}"'
}
fi
function ble/string#capitalize {
local tail="$*"
# prefix
local rex='^[^a-zA-Z0-9]*'
[[ $tail =~ $rex ]]
local out=$BASH_REMATCH
tail=${tail:${#BASH_REMATCH}}
# words
rex='^[a-zA-Z0-9]+[^a-zA-Z0-9]*'
while [[ $tail =~ $rex ]]; do
local rematch=$BASH_REMATCH
ble/string#toupper "${rematch::1}"; out=$out$ret
ble/string#tolower "${rematch:1}" ; out=$out$ret
tail=${tail:${#rematch}}
done
ret=$out$tail
}
## 関数 ble/string#trim text...
## @var[out] ret
function ble/string#trim {
ret="$*"
local rex=$'^[ \t\n]+'
[[ $ret =~ $rex ]] && ret=${ret:${#BASH_REMATCH}}
local rex=$'[ \t\n]+$'
[[ $ret =~ $rex ]] && ret=${ret::${#ret}-${#BASH_REMATCH}}
}
## 関数 ble/string#ltrim text...
## @var[out] ret
function ble/string#ltrim {
ret="$*"
local rex=$'^[ \t\n]+'
[[ $ret =~ $rex ]] && ret=${ret:${#BASH_REMATCH}}
}
## 関数 ble/string#rtrim text...
## @var[out] ret
function ble/string#rtrim {
ret="$*"
local rex=$'[ \t\n]+$'
[[ $ret =~ $rex ]] && ret=${ret::${#ret}-${#BASH_REMATCH}}
}
## 関数 ble/string#escape-characters text chars1 [chars2]
## @param[in] text
## @param[in] chars1
## @param[in,opt] chars2
## @var[out] ret
function ble/string#escape-characters {
ret=$1
if [[ $ret == *["$2"]* ]]; then
local chars1=$2 chars2=${3:-$2}
local i n=${#chars1} a b
for ((i=0;i<n;i++)); do
a=${chars1:i:1} b=\\${chars2:i:1} ret=${ret//"$a"/$b}
done
fi
}
## 関数 ble/string#escape-for-sed-regex text...
## 関数 ble/string#escape-for-awk-regex text...
## 関数 ble/string#escape-for-extended-regex text...
## 関数 ble/string#escape-for-bash-glob text...
## 関数 ble/string#escape-for-bash-single-quote text...
## 関数 ble/string#escape-for-bash-double-quote text...
## 関数 ble/string#escape-for-bash-escape-string text...
## 関数 ble/string#escape-for-bash-specialchars text flags
## 関数 ble/string#escape-for-bash-specialchars-in-brace text flags
## @param[in] text...
## @var[out] ret
function ble/string#escape-for-sed-regex {
ble/string#escape-characters "$*" '\.[*^$/'
}
function ble/string#escape-for-awk-regex {
ble/string#escape-characters "$*" '\.[*?+|^$(){}/'
}
function ble/string#escape-for-extended-regex {
ble/string#escape-characters "$*" '\.[*?+|^$(){}'
}
function ble/string#escape-for-bash-glob {
ble/string#escape-characters "$*" '\*?[('
}
function ble/string#escape-for-bash-single-quote {
ret="$*"
local q="'" Q="'\''"
ret=${ret//"$q"/$Q}
}
function ble/string#escape-for-bash-double-quote {
ble/string#escape-characters "$*" '\"$`'
local a b
a='!' b='"\!"' ret=${ret//"$a"/$b}
}
function ble/string#escape-for-bash-escape-string {
ble/string#escape-characters "$*" $'\\\a\b\e\f\n\r\t\v'\' '\abefnrtv'\'
}
function ble/string#escape-for-bash-specialchars {
local chars='\ ["'\''`$|&;<>()*?!^'
# Note: = と : は文法的にはエスケープは不要だが
# 補完の際の COMP_WORDBREAKS を避ける為に必要である。
[[ $2 == *c* ]] && chars=$chars'=:'
[[ $2 == *b* ]] && chars=$chars'{,}'
ble/string#escape-characters "$1" "$chars"
if [[ $ret == *[$']\n\t']* ]]; then
local a b
a=']' b=\\$a ret=${ret//"$a"/$b}
a=$'\n' b="\$'\n'" ret=${ret//"$a"/$b}
a=$'\t' b=$' \t' ret=${ret//"$a"/$b}
fi
}
## 関数 ble/string#create-unicode-progress-bar value max width
## @var[out] ret
function ble/string#create-unicode-progress-bar {
local value=$1 max=$2 width=$3
local progress=$((value*8*width/max))
local progress_fraction=$((progress%8)) progress_integral=$((progress/8))
local out=
if ((progress_integral)); then
ble/util/c2s $((0x2588))
((${#ret}==1)) || ret='*' # LC_CTYPE が非対応の文字の時
ble/string#repeat "$ret" "$progress_integral"
out=$ret
fi
if ((progress_fraction)); then
ble/util/c2s $((0x2590-progress_fraction))
((${#ret}==1)) || ret=$progress_fraction # LC_CTYPE が非対応の文字の時
out=$out$ret
((progress_integral++))
fi
if ((progress_integral<width)); then
ble/util/c2w $((0x2588))
ble/string#repeat ' ' $((ret*(width-progress_integral)))
out=$out$ret
fi
ret=$out
}
function ble/path#remove {
local _ble_local_script='
opts=:$opts:
opts=${opts//:"$2":/:}
opts=${opts#:} opts=${opts%:}'
builtin eval "${_ble_local_script//opts/$1}"
}
function ble/path#remove-glob {
local _ble_local_script='
opts=:$opts:
opts=${opts//:$2:/:}
opts=${opts#:} opts=${opts%:}'
builtin eval "${_ble_local_script//opts/$1}"
}
#
# assign: reading files/streams into variables
#
## 関数 ble/util/readfile var filename
## 関数 ble/util/mapfile arr < filename
## ファイルの内容を変数または配列に読み取ります。
##
## @param[in] var
## 読み取った内容の格納先の変数名を指定します。
## @param[in] arr
## 読み取った内容を行毎に格納する配列の名前を指定します。
## @param[in] filename
## 読み取るファイルの場所を指定します。
##
if ((_ble_bash>=40000)); then
function ble/util/readfile { # 155ms for man bash
local __buffer
mapfile __buffer < "$2"
IFS= eval "$1"'="${__buffer[*]-}"'
}
function ble/util/mapfile {
mapfile -t "$1"
}
else
function ble/util/readfile { # 465ms for man bash
IFS= builtin read -r -d '' "$1" < "$2"
}
function ble/util/mapfile {
local _ble_local_i=0 _ble_local_val _ble_local_arr; _ble_local_arr=()
while builtin read -r _ble_local_val || [[ $_ble_local_val ]]; do
_ble_local_arr[_ble_local_i++]=$_ble_local_val
done
builtin eval "$1=(\"\${_ble_local_arr[@]}\")"
}
fi
## 関数 ble/util/assign var command
## var=$(command) の高速な代替です。
## command はサブシェルではなく現在のシェルで実行されます。
##
## @param[in] var
## 代入先の変数名を指定します。
## @param[in] command...
## 実行するコマンドを指定します。
##
_ble_util_assign_base=$_ble_base_run/$$.ble_util_assign.tmp
_ble_util_assign_level=0
if ((_ble_bash>=40000)); then
# mapfile の方が read より高速
function ble/util/assign {
local _ble_local_tmp=$_ble_util_assign_base.$((_ble_util_assign_level++))
builtin eval "$2" >| "$_ble_local_tmp"
local _ble_local_ret=$? _ble_local_arr=
((_ble_util_assign_level--))
mapfile -t _ble_local_arr < "$_ble_local_tmp"
IFS=$'\n' eval "$1=\"\${_ble_local_arr[*]}\""
return "$_ble_local_ret"
}
else
function ble/util/assign {
local _ble_local_tmp=$_ble_util_assign_base.$((_ble_util_assign_level++))
builtin eval "$2" >| "$_ble_local_tmp"
local _ble_local_ret=$?
((_ble_util_assign_level--))
IFS= builtin read -r -d '' "$1" < "$_ble_local_tmp"
eval "$1=\${$1%$'\n'}"
return "$_ble_local_ret"
}
fi
## 関数 ble/util/assign-array arr command args...
## mapfile -t arr <(command ...) の高速な代替です。
## command はサブシェルではなく現在のシェルで実行されます。
##
## @param[in] arr
## 代入先の配列名を指定します。
## @param[in] command
## 実行するコマンドを指定します。
## @param[in] args...
## command から参照する引数 ($3 $4 ...) を指定します。
##
if ((_ble_bash>=40000)); then
function ble/util/assign-array {
local _ble_local_tmp=$_ble_util_assign_base.$((_ble_util_assign_level++))
builtin eval "$2" >| "$_ble_local_tmp"
local _ble_local_ret=$?
((_ble_util_assign_level--))
mapfile -t "$1" < "$_ble_local_tmp"
return "$_ble_local_ret"
}
else
function ble/util/assign-array {
local _ble_local_tmp=$_ble_util_assign_base.$((_ble_util_assign_level++))
builtin eval "$2" >| "$_ble_local_tmp"
local _ble_local_ret=$?
((_ble_util_assign_level--))
ble/util/mapfile "$1" < "$_ble_local_tmp"
return "$_ble_local_ret"
}
fi
#
# functions
#
if ((_ble_bash>=30200)); then
function ble/is-function {
builtin declare -F "$1" &>/dev/null
}
else
# bash-3.1 has bug in declare -f.
# it does not accept a function name containing non-alnum chars.
function ble/is-function {
local type
ble/util/type type "$1"
[[ $type == function ]]
}
fi
function ble/function#try {
ble/is-function "$1" || return 127
"$@"
}
#
# miscallaneous utils
#
if ((_ble_bash>=40100)); then
function ble/util/set {
builtin printf -v "$1" %s "$2"
}
else
function ble/util/set {
builtin eval "$1=\"\$2\""
}
fi
if ((_ble_bash>=30100)); then
function ble/util/sprintf {
builtin printf -v "$@"
}
else
function ble/util/sprintf {
local -a args; args=("${@:2}")
ble/util/assign "$1" 'builtin printf "${args[@]}"'
}
fi
## 関数 ble/util/type varname command
## @param[out] varname
## 結果を格納する変数名を指定します。
## @param[in] command
## 種類を判定するコマンド名を指定します。
function ble/util/type {
ble/util/assign "$1" 'builtin type -t -- "$3" 2>/dev/null' "$2"
builtin eval "$1=\"\${$1%$_ble_term_nl}\""
}
## 関数 ble/util/expand-alias word
## @var[out] ret
function ble/util/expand-alias {
ret=$1
local type; ble/util/type type "$ret"
if [[ $type == alias ]]; then
local data; ble/util/assign data 'LANG=C alias "$ret"' &>/dev/null
[[ $data == 'alias '*=* ]] && eval "ret=${data#alias *=}"
fi
}
if ((_ble_bash>=40000)); then
function ble/util/is-stdin-ready { IFS= LC_ALL=C builtin read -t 0; } &>/dev/null
else
function ble/util/is-stdin-ready { false; }
fi
## 関数 ble/util/openat fdvar redirect
## "exec {fdvar}>foo" に該当する操作を実行します。
## @param[out] fdvar
## 指定した変数に使用されたファイルディスクリプタを代入します。
## @param[in] redirect
## リダイレクトを指定します。
_ble_util_openat_fdlist=()
if ((_ble_bash>=40100)); then
function ble/util/openat {
builtin eval "exec {$1}$2"; local _ble_local_ret=$?
ble/array#push _ble_util_openat_fdlist "${!1}"
return "$_ble_local_ret"
}
else
_ble_util_openat_nextfd=$bleopt_openat_base
function ble/util/openat/.nextfd {
if ((30100<=_ble_bash&&_ble_bash<30200)); then
# Bash 3.1 では exec fd>&- で明示的に閉じても駄目。
# 開いた後に読み取りプロセスで読み取りに失敗する。
# なので開いていない fd を /dev か /proc で調べる。#D0992
while [[ -e /dev/fd/$_ble_util_openat_nextfd || -e /proc/self/fd/$_ble_util_openat_nextfd ]]; do
((_ble_util_openat_nextfd++))
done
fi
(($1=_ble_util_openat_nextfd++))
}
function ble/util/openat {
local _fdvar=$1 _redirect=$2
ble/util/openat/.nextfd "$1"
# Note: Bash 3.2/3.1 のバグを避けるため、
# >&- を用いて一旦明示的に閉じる必要がある #D0857
builtin eval "exec ${!1}>&- ${!1}$2"; local _ble_local_ret=$?
ble/array#push _ble_util_openat_fdlist "${!1}"
return "$_ble_local_ret"
}
fi
function ble/util/openat/finalize {
local fd
for fd in "${_ble_util_openat_fdlist[@]}"; do
builtin eval "exec $fd>&-"
done
_ble_util_openat_fdlist=()
}
function ble/util/declare-print-definitions {