-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
ble.pp
1561 lines (1413 loc) · 50 KB
/
ble.pp
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
#!/bin/bash
#%$> out/ble.sh
#%[release = 0]
#%[measure_load_time = 0]
#%[debug_keylogger = 1]
#%#----------------------------------------------------------------------------
#%if measure_load_time
_ble_debug_measure_fork_count=$(echo $BASHPID)
TIMEFORMAT='[Elapsed %Rs; CPU U:%Us S:%Ss (%P%%)]'
function ble/debug/measure-set-timeformat {
local title=$1 opts=$2
local new=$(echo $BASHPID)
local fork=$(((new-_ble_debug_measure_fork_count-1)&0xFFFF))
_ble_debug_measure_fork_count=$new
TIMEFORMAT="[Elapsed %Rs; CPU U:%Us S:%Ss (%P%%)] $title"
[[ :$opts: != *:nofork:* ]] &&
TIMEFORMAT=$TIMEFORMAT" ($fork forks)"
}
#%end
#%define inc
#%%[guard_name = "@_included".replace("[^_a-zA-Z0-9]", "_")]
#%%expand
#%%%if $"guard_name" != 1
#%%%%[$"guard_name" = 1]
###############################################################################
# Included from @.sh
#%%%%if measure_load_time
time {
#%%%%%include @.sh
ble/debug/measure-set-timeformat '@.sh'
}
#%%%%else
#%%%%%include @.sh
#%%%%end
#%%%end
#%%end.i
#%end
#%#----------------------------------------------------------------------------
# ble.sh -- Bash Line Editor (https://github.com/akinomyoga/ble.sh)
#
# Bash configuration designed to be sourced in interactive bash sessions.
#
# Copyright: 2013, 2015-2019, Koichi Murase <myoga.murase@gmail.com>
#
#%if measure_load_time
echo "ble.sh: $EPOCHREALTIME load start" >&2
time {
echo "ble.sh: $EPOCHREALTIME parsed" >&2
# load_time (2015-12-03)
# core 12ms
# decode 10ms
# color 2ms
# edit 9ms
# syntax 5ms
# ble-initialize 14ms
time {
#%end
#------------------------------------------------------------------------------
# check --help or --version
{
#%$ echo "_ble_init_version=$FULLVER+$(git show -s --format=%h)"
_ble_init_exit=
_ble_init_command=
for _ble_init_arg; do
case $_ble_init_arg in
--version)
_ble_init_exit=0
echo "ble.sh (Bash Line Editor), version $_ble_init_version" ;;
--help)
_ble_init_exit=0
printf '%s\n' \
"# ble.sh (Bash Line Editor), version $_ble_init_version" \
'usage: source ble.sh [OPTION...]' \
'' \
'OPTION' \
'' \
' --help' \
' Show this help and exit' \
' --version' \
' Show version and exit' \
' --test' \
' Run test and exit' \
' --update' \
' Update ble.sh and exit' \
' --clear-cache' \
' Clear ble.sh cache and exit' \
'' \
' --rcfile=BLERC' \
' --init-file=BLERC' \
' Specify the ble init file. The default is ~/.blerc.' \
'' \
' --norc' \
' Do not load the ble init file.' \
'' \
' --attach=ATTACH' \
' --noattach' \
' The option "--attach" selects the strategy of "ble-attach" from the' \
' list: ATTACH = "attach" | "prompt" | "none". The default strategy is' \
' "prompt". The option "--noattach" is a synonym for "--attach=none".' \
'' \
' --noinputrc' \
' Do not read inputrc settings for ble.sh' \
'' \
' --keep-rlvars' \
' Do not change readline settings for ble.sh' \
'' \
' -o BLEOPT=VALUE' \
' Set a value for the specified bleopt option.' \
' --debug-bash-output' \
' Internal settings for debugging' \
'' ;;
--test | --update | --clear-cache) _ble_init_command=1 ;;
esac
done
if [ -n "$_ble_init_exit" ]; then
unset _ble_init_version
unset _ble_init_arg
unset _ble_init_exit
unset _ble_init_command
return 0 2>/dev/null || exit 0
fi
} 2>/dev/null # set -x 対策 #D0930
#------------------------------------------------------------------------------
# check shell
if [ -z "${BASH_VERSION-}" ]; then
echo "ble.sh: This shell is not Bash. Please use this script with Bash." >&3
return 1 2>/dev/null || exit 1
fi 3>&2 >/dev/null 2>&1 # set -x 対策 #D0930
if [ -z "${BASH_VERSINFO-}" ] || [ "${BASH_VERSINFO-0}" -lt 3 ]; then
echo "ble.sh: Bash with a version under 3.0 is not supported." >&3
return 1 2>/dev/null || exit 1
fi 3>&2 >/dev/null 2>&1 # set -x 対策 #D0930
if [[ ! $_ble_init_command ]]; then
if ((BASH_SUBSHELL)); then
builtin echo "ble.sh: ble.sh cannot be loaded into a subshell." >&3
return 1 2>/dev/null || builtin exit 1
elif [[ $- != *i* ]]; then
{ ((${#BASH_SOURCE[@]})) && [[ ${BASH_SOURCE[${#BASH_SOURCE[@]}-1]} == *bashrc ]]; } ||
builtin echo "ble.sh: This is not an interactive session." >&3
return 1 2>/dev/null || builtin exit 1
fi
fi 3>&2 &>/dev/null # set -x 対策 #D0930
{
## @var _ble_bash_POSIXLY_CORRECT_adjusted
## 現在 POSIXLY_CORRECT 状態を待避した状態かどうかを保持します。
## @var _ble_bash_POSIXLY_CORRECT_set
## 待避した POSIXLY_CORRECT の設定・非設定状態を保持します。
## @var _ble_bash_POSIXLY_CORRECT_set
## 待避した POSIXLY_CORRECT の値を保持します。
_ble_bash_POSIXLY_CORRECT_adjusted=1
_ble_bash_POSIXLY_CORRECT_set=${POSIXLY_CORRECT+set}
_ble_bash_POSIXLY_CORRECT=${POSIXLY_CORRECT-}
POSIXLY_CORRECT=y
# 暫定対策 expand_aliases (ble/base/adjust-bash-options を呼び出す迄の暫定)
_ble_bash_expand_aliases=
\shopt -q expand_aliases &&
_ble_bash_expand_aliases=1 &&
\shopt -u expand_aliases || ((1))
# 対策 FUNCNEST
_ble_bash_FUNCNEST_adjusted=
_ble_bash_FUNCNEST=
_ble_bash_FUNCNEST_adjust='
if [[ ! $_ble_bash_FUNCNEST_adjusted ]]; then
_ble_bash_FUNCNEST_adjusted=1
_ble_bash_FUNCNEST=$FUNCNEST FUNCNEST=
fi 2>/dev/null'
_ble_bash_FUNCNEST_restore='
if [[ $_ble_bash_FUNCNEST_adjusted ]]; then
_ble_bash_FUNCNEST_adjusted=
FUNCNEST=$_ble_bash_FUNCNEST
fi 2>/dev/null'
\builtin eval -- "$_ble_bash_FUNCNEST_adjust"
\builtin unset -v POSIXLY_CORRECT
} 2>/dev/null
function ble/base/workaround-POSIXLY_CORRECT {
# This function will be overwritten by ble-decode
true
}
function ble/base/unset-POSIXLY_CORRECT {
if [[ ${POSIXLY_CORRECT+set} ]]; then
builtin unset -v POSIXLY_CORRECT
ble/base/workaround-POSIXLY_CORRECT
fi
}
function ble/base/adjust-POSIXLY_CORRECT {
if [[ $_ble_bash_POSIXLY_CORRECT_adjusted ]]; then return 0; fi # Note: set -e 対策
_ble_bash_POSIXLY_CORRECT_adjusted=1
_ble_bash_POSIXLY_CORRECT_set=${POSIXLY_CORRECT+set}
_ble_bash_POSIXLY_CORRECT=${POSIXLY_CORRECT-}
if [[ $_ble_bash_POSIXLY_CORRECT_set ]]; then
builtin unset -v POSIXLY_CORRECT
fi
# ユーザが触ったかもしれないので何れにしても workaround を呼び出す。
ble/base/workaround-POSIXLY_CORRECT
}
function ble/base/restore-POSIXLY_CORRECT {
if [[ ! $_ble_bash_POSIXLY_CORRECT_adjusted ]]; then return 0; fi # Note: set -e の為 || は駄目
_ble_bash_POSIXLY_CORRECT_adjusted=
if [[ $_ble_bash_POSIXLY_CORRECT_set ]]; then
POSIXLY_CORRECT=$_ble_bash_POSIXLY_CORRECT
else
ble/base/unset-POSIXLY_CORRECT
fi
}
function ble/base/is-POSIXLY_CORRECT {
if [[ $_ble_bash_POSIXLY_CORRECT_adjusted ]]; then
[[ $_ble_bash_POSIXLY_CORRECT_set ]]
else
[[ ${POSIXLY_CORRECT+set} ]]
fi
}
_ble_bash_builtins_adjusted=
_ble_bash_builtins_save=
function ble/base/adjust-builtin-wrappers/.assign {
if [[ $_ble_util_assign_base ]]; then
local _ble_local_tmpfile; ble/util/assign/.mktmp
builtin eval -- "$1" >| "$_ble_local_tmpfile"
IFS= builtin read -r -d '' defs < "$_ble_local_tmpfile"
ble/util/assign/.rmtmp
else
defs=$(builtin eval -- "$1")
fi || ((1))
}
function ble/base/adjust-builtin-wrappers-1 {
# Note: 何故か local POSIXLY_CORRECT の効果が
# builtin unset -v POSIXLY_CORRECT しても残存するので関数に入れる。
# Note: set -o posix にしても read, type, builtin, local 等は上書き
# された儘なので難しい。unset -f builtin さえすれば色々動く様になる
# ので builtin は unset -f builtin してしまう。
unset -f builtin
builtin local POSIXLY_CORRECT=y builtins1 keywords1
builtins1=(builtin unset enable unalias return break continue declare local typeset readonly eval exec)
keywords1=(if then elif else case esac while until for select do done '{' '}' '[[' function)
if [[ ! $_ble_bash_builtins_adjusted ]]; then
_ble_bash_builtins_adjusted=1
builtin local defs
ble/base/adjust-builtin-wrappers/.assign '
\builtin declare -f "${builtins1[@]}" || ((1))
\builtin alias "${builtins1[@]}" "${keywords1[@]}" || ((1))' # set -e 対策
_ble_bash_builtins_save=$defs
fi
builtin unset -f "${builtins1[@]}"
builtin unalias "${builtins1[@]}" "${keywords1[@]}" || ((1)) # set -e 対策
ble/base/unset-POSIXLY_CORRECT
} 2>/dev/null
function ble/base/adjust-builtin-wrappers-2 {
# Workaround (bash-3.0..4.3) #D0722
#
# builtin unset -v POSIXLY_CORRECT でないと unset -f : できないが、
# bash-3.0 -- 4.3 のバグで、local POSIXLY_CORRECT の時、
# builtin unset -v POSIXLY_CORRECT しても POSIXLY_CORRECT が有効であると判断されるので、
# "unset -f :" (非POSIX関数名) は別関数で adjust-POSIXLY_CORRECT の後で実行することにする。
# function :, alias : の保存
local defs
ble/base/adjust-builtin-wrappers/.assign 'LC_ALL= LC_MESSAGES=C builtin type :; alias :' || ((1)) # set -e 対策
defs=${defs#$': is a function\n'}
_ble_bash_builtins_save=$_ble_bash_builtins_save$'\n'$defs
builtin unset -f :
builtin unalias : || ((1)) # set -e 対策
} 2>/dev/null
function ble/base/restore-builtin-wrappers {
if [[ $_ble_bash_builtins_adjusted ]]; then
_ble_bash_builtins_adjusted=
builtin eval -- "$_ble_bash_builtins_save"
fi
}
{
ble/base/adjust-builtin-wrappers-1
ble/base/adjust-builtin-wrappers-2
# 対策 expand_aliases (暫定) 終了
if [[ $_ble_bash_expand_aliases ]]; then
shopt -s expand_aliases
fi
} 2>/dev/null # set -x 対策
# From src/util.sh
function ble/variable#copy-state {
local src=$1 dst=$2
if [[ ${!src+set} ]]; then
builtin eval -- "$dst=\${$src}"
else
builtin unset -v "$dst[0]" 2>/dev/null || builtin unset -v "$dst"
fi
}
: "${_ble_bash_options_adjusted=}"
function ble/base/adjust-bash-options {
[[ $_ble_bash_options_adjusted ]] && return 1 || ((1)) # set -e 対策
_ble_bash_options_adjusted=1
# Note: set -e 対策が最初でないと && chaining で失敗する
_ble_bash_sete=; [[ -o errexit ]] && _ble_bash_sete=1 && set +e
_ble_bash_setx=; [[ -o xtrace ]] && _ble_bash_setx=1 && set +x
_ble_bash_setv=; [[ -o verbose ]] && _ble_bash_setv=1 && set +v
_ble_bash_setu=; [[ -o nounset ]] && _ble_bash_setu=1 && set +u
# Note: nocasematch は bash-3.0 以上
_ble_bash_nocasematch=
shopt -q nocasematch 2>/dev/null &&
_ble_bash_nocasematch=1 && shopt -u nocasematch
# Note: expand_aliases はユーザー設定を復元する為に記録する
_ble_bash_expand_aliases=
shopt -q expand_aliases 2>/dev/null &&
_ble_bash_expand_aliases=1
# locale 待避
ble/variable#copy-state LC_ALL _ble_bash_LC_ALL
if [[ ${LC_ALL-} ]]; then
ble/variable#copy-state LC_CTYPE _ble_bash_LC_CTYPE
ble/variable#copy-state LC_MESSAGES _ble_bash_LC_MESSAGES
ble/variable#copy-state LC_NUMERIC _ble_bash_LC_NUMERIC
ble/variable#copy-state LC_TIME _ble_bash_LC_TIME
ble/variable#copy-state LANG _ble_bash_LANG
[[ ${LC_CTYPE-} ]] && LC_CTYPE=$LC_ALL
[[ ${LC_MESSAGES-} ]] && LC_MESSAGES=$LC_ALL
[[ ${LC_NUMERIC-} ]] && LC_NUMERIC=$LC_ALL
[[ ${LC_TIME-} ]] && LC_TIME=$LC_ALL
LANG=$LC_ALL
LC_ALL=
fi
ble/variable#copy-state LC_COLLATE _ble_bash_LC_COLLATE
LC_COLLATE=C
} 2>/dev/null # set -x 対策 #D0930 / locale 変更
function ble/base/restore-bash-options {
[[ $_ble_bash_options_adjusted ]] || return 1
_ble_bash_options_adjusted=
# locale 復元
ble/variable#copy-state _ble_bash_LC_COLLATE LC_COLLATE
if [[ $_ble_bash_LC_ALL ]]; then
ble/variable#copy-state _ble_bash_LC_CTYPE LC_CTYPE
ble/variable#copy-state _ble_bash_LC_MESSAGES LC_MESSAGES
ble/variable#copy-state _ble_bash_LC_NUMERIC LC_NUMERIC
ble/variable#copy-state _ble_bash_LC_TIME LC_TIME
ble/variable#copy-state _ble_bash_LANG LANG
fi
ble/variable#copy-state _ble_bash_LC_ALL LC_ALL
[[ $_ble_bash_nocasematch ]] && shopt -s nocasematch
[[ $_ble_bash_setu && ! -o nounset ]] && set -u
[[ $_ble_bash_setv && ! -o verbose ]] && set -v
[[ $_ble_bash_setx && ! -o xtrace ]] && set -x
[[ $_ble_bash_sete && ! -o errexit ]] && set -e # set -e は最後
} 2>/dev/null # set -x 対策 #D0930 / locale 変更
function ble/base/recover-bash-options {
# bind -x が終わる度に設定が復元されてしまうので毎回設定し直す #D1526 #D1574
if [[ $_ble_bash_expand_aliases ]]; then
shopt -s expand_aliases
else
shopt -u expand_aliases
fi
}
{ ble/base/adjust-bash-options; } &>/dev/null # set -x 対策 #D0930
builtin bind &>/dev/null # force to load .inputrc
# WA #D1534 workaround for msys2 .inputrc
if [[ $OSTYPE == msys* ]]; then
[[ $(builtin bind -m emacs -p | grep '"\\C-?"') == '"\C-?": backward-kill-line' ]] &&
builtin bind -m emacs '"\C-?": backward-delete-char'
fi
if [[ ! -o emacs && ! -o vi && ! $_ble_init_command ]]; then
builtin echo "ble.sh: ble.sh is not intended to be used with the line-editing mode disabled (--noediting)." >&2
ble/base/restore-bash-options
ble/base/restore-builtin-wrappers
ble/base/restore-POSIXLY_CORRECT
builtin eval -- "$_ble_bash_FUNCNEST_restore"
return 1 2>/dev/null || builtin exit 1
fi
if shopt -q restricted_shell; then
builtin echo "ble.sh: ble.sh is not intended to be used in restricted shells (--restricted)." >&2
ble/base/restore-bash-options
ble/base/restore-builtin-wrappers
ble/base/restore-POSIXLY_CORRECT
builtin eval -- "$_ble_bash_FUNCNEST_restore"
return 1 2>/dev/null || builtin exit 1
fi
if [[ ${BASH_EXECUTION_STRING+set} ]]; then
# builtin echo "ble.sh: ble.sh will not be activated for Bash started with '-c' option." >&2
ble/base/restore-bash-options
ble/base/restore-builtin-wrappers
ble/base/restore-POSIXLY_CORRECT
builtin eval -- "$_ble_bash_FUNCNEST_restore"
return 1 2>/dev/null || builtin exit 1
fi
_ble_init_original_IFS_set=${IFS+set}
_ble_init_original_IFS=$IFS
IFS=$' \t\n'
#------------------------------------------------------------------------------
# read arguments
function ble/util/put { builtin printf '%s' "$1"; }
function ble/util/print { builtin printf '%s\n' "$1"; }
function ble/util/print-lines { builtin printf '%s\n' "$@"; }
_ble_base_arguments_opts=
_ble_base_arguments_attach=
_ble_base_arguments_rcfile=
## @fn ble/base/read-blesh-arguments args
## @var[out] _ble_base_arguments_opts
## @var[out] _ble_base_arguments_attach
## @var[out] _ble_base_arguments_rcfile
function ble/base/read-blesh-arguments {
local opts=
local opt_attach=prompt
_ble_init_command= # 再解析
while (($#)); do
local arg=$1; shift
case $arg in
(--noattach|noattach)
opt_attach=none ;;
(--attach=*) opt_attach=${arg#*=} ;;
(--attach)
if (($#)); then
opt_attach=$1; shift
else
opt_attach=attach
opts=$opts:E
ble/util/print "ble.sh ($arg): an option argument is missing." >&2
fi ;;
(--noinputrc)
opts=$opts:noinputrc ;;
(--rcfile=*|--init-file=*|--rcfile|--init-file)
if [[ $arg != *=* ]]; then
local rcfile=$1; shift
else
local rcfile=${arg#*=}
fi
_ble_base_arguments_rcfile=${rcfile:-/dev/null}
if [[ ! $rcfile || ! -e $rcfile ]]; then
ble/util/print "ble.sh ($arg): '$rcfile' does not exist." >&2
opts=$opts:E
elif [[ ! -r $rcfile ]]; then
ble/util/print "ble.sh ($arg): '$rcfile' is not readable." >&2
opts=$opts:E
fi ;;
(--norc)
_ble_base_arguments_rcfile=/dev/null ;;
(--keep-rlvars)
opts=$opts:keep-rlvars ;;
(--debug-bash-output)
bleopt_internal_suppress_bash_output= ;;
(--test | --update | --clear-cache)
if [[ $_ble_init_command ]]; then
ble/util/print "ble.sh ($arg): the option '--$_ble_init_command' has already been specified." >&2
opts=$opts:E
else
_ble_init_command=${arg#--}
fi ;;
(--*)
ble/util/print "ble.sh: unrecognized long option '$arg'" >&2
opts=$opts:E ;;
(-?*)
local i c
for ((i=1;i<${#arg};i++)); do
c=${arg:i:1}
case -$c in
(-o)
if ((i+1<${#arg})); then
local oarg=${arg:i+1}
i=${#arg}
elif (($#)); then
local oarg=$1; shift
else
opts=$opts:E
i=${#arg}
continue
fi
local rex='^[_a-zA-Z][_a-zA-Z0-9]*='
if [[ $oarg =~ $rex ]]; then
builtin eval -- "bleopt_${oarg%%=*}=\${oarg#*=}"
else
ble/util/print "ble.sh: unrecognized option '-o $oarg'" >&2
opts=$opts:E
fi ;;
(-*)
ble/util/print "ble.sh: unrecognized option '-$c'" >&2
opts=$opts:E ;;
esac
done
;;
(*)
ble/util/print "ble.sh: unrecognized argument '$arg'" >&2
opts=$opts:E ;;
esac
done
_ble_base_arguments_opts=$opts
_ble_base_arguments_attach=$opt_attach
[[ :$opts: != *:E:* ]]
}
if ! ble/base/read-blesh-arguments "$@"; then
builtin echo "ble.sh: cancel initialization." >&2
ble/base/restore-bash-options
ble/base/restore-builtin-wrappers
ble/base/restore-POSIXLY_CORRECT
builtin eval -- "$_ble_bash_FUNCNEST_restore"
return 2 2>/dev/null || builtin exit 2
fi
if [[ ${_ble_base-} ]]; then
[[ $_ble_init_command ]] && _ble_init_attached=$_ble_attached
if ! ble/base/unload-for-reload; then
builtin echo "ble.sh: an old version of ble.sh seems to be already loaded." >&2
ble/base/restore-bash-options
ble/base/restore-builtin-wrappers
ble/base/restore-POSIXLY_CORRECT
builtin eval -- "$_ble_bash_FUNCNEST_restore"
return 1 2>/dev/null || builtin exit 1
fi
fi
#------------------------------------------------------------------------------
# Initialize version information
_ble_bash=$((BASH_VERSINFO[0]*10000+BASH_VERSINFO[1]*100+BASH_VERSINFO[2]))
_ble_bash_loaded_in_function=0
local _ble_local_test 2>/dev/null && _ble_bash_loaded_in_function=1
_ble_version=0
BLE_VERSION=$_ble_init_version
function ble/base/initialize-version-information {
local version=$BLE_VERSION
local hash=
if [[ $version == *+* ]]; then
hash=${version#*+}
version=${version%%+*}
fi
local status=release
if [[ $version == *-* ]]; then
status=${version#*-}
version=${version%%-*}
fi
local major=${version%%.*}; version=${version#*.}
local minor=${version%%.*}; version=${version#*.}
local patch=${version%%.*}
BLE_VERSINFO=("$major" "$minor" "$patch" "$hash" "$status" noarch)
((_ble_version=major*10000+minor*100+patch))
}
ble/base/initialize-version-information
#------------------------------------------------------------------------------
# check environment
# will be overwritten by src/util.sh
function ble/util/assign { builtin eval "$1=\$(builtin eval -- \"\$2\")"; }
# ble/bin
## @fn ble/bin/.default-utility-path commands...
## 取り敢えず ble/bin/* からコマンドを呼び出せる様にします。
function ble/bin/.default-utility-path {
local cmd
for cmd; do
builtin eval "function ble/bin/$cmd { command $cmd \"\$@\"; }"
done
}
## @fn ble/bin/.freeze-utility-path commands...
## PATH が破壊された後でも ble が動作を続けられる様に、
## 現在の PATH で基本コマンドのパスを固定して ble/bin/* から使える様にする。
##
## 実装に ble/util/assign を使用しているので ble-core 初期化後に実行する必要がある。
##
function ble/bin/.freeze-utility-path {
local cmd path q=\' Q="'\''" fail=
for cmd; do
ble/bin#has "ble/bin/.frozen:$cmd" && continue
if ble/util/assign path "builtin type -P -- $cmd 2>/dev/null" && [[ $path ]]; then
builtin eval "function ble/bin/$cmd { '${path//$q/$Q}' \"\$@\"; }"
else
fail=1
fi
done
((!fail))
}
if ((_ble_bash>=40000)); then
function ble/bin#has { type "$@" &>/dev/null; }
else
function ble/bin#has {
local cmd
for cmd; do type "$cmd" || return 1; done &>/dev/null
return 0
}
fi
# POSIX utilities
_ble_init_posix_command_list=(sed date rm mkdir mkfifo sleep stty tty sort awk chmod grep cat wc mv sh od cp)
function ble/.check-environment {
if ! ble/bin#has "${_ble_init_posix_command_list[@]}"; then
local cmd commandMissing=
for cmd in "${_ble_init_posix_command_list[@]}"; do
if ! type "$cmd" &>/dev/null; then
commandMissing="$commandMissing\`$cmd', "
fi
done
ble/util/print "ble.sh: Insane environment: The command(s), ${commandMissing}not found. Check your environment variable PATH." >&2
# try to fix PATH
local default_path=$(command -p getconf PATH 2>/dev/null)
[[ $default_path ]] || return 1
local original_path=$PATH
export PATH=${default_path}${PATH:+:}${PATH}
[[ :$PATH: == *:/bin:* ]] || PATH=/bin${PATH:+:}$PATH
[[ :$PATH: == *:/usr/bin:* ]] || PATH=/usr/bin${PATH:+:}$PATH
if ! ble/bin#has "${_ble_init_posix_command_list[@]}"; then
PATH=$original_path
return 1
fi
ble/util/print "ble.sh: modified PATH=${PATH::${#PATH}-${#original_path}}\$PATH" >&2
fi
if [[ ! $USER ]]; then
ble/util/print "ble.sh: Insane environment: \$USER is empty." >&2
if type id &>/dev/null; then
export USER=$(id -un)
ble/util/print "ble.sh: modified USER=$USER" >&2
fi
fi
# 暫定的な ble/bin/$cmd 設定
ble/bin/.default-utility-path "${_ble_init_posix_command_list[@]}"
return 0
}
if ! ble/.check-environment; then
_ble_bash=
return 1
fi
# src/util で awk を使う
function ble/bin/awk/.instantiate {
local path q=\' Q="'\''"
if [[ $OSTYPE == solaris* ]] && type /usr/xpg4/bin/awk >/dev/null; then
# Solaris の既定の awk は全然駄目なので /usr/xpg4 以下の awk を使う。
function ble/bin/awk { /usr/xpg4/bin/awk -v AWKTYPE=xpg4 "$@"; }
elif ble/util/assign path "builtin type -P -- nawk 2>/dev/null" && [[ $path ]]; then
builtin eval "function ble/bin/awk { '${path//$q/$Q}' -v AWKTYPE=nawk \"\$@\"; }"
elif ble/util/assign path "builtin type -P -- mawk 2>/dev/null" && [[ $path ]]; then
builtin eval "function ble/bin/awk { '${path//$q/$Q}' -v AWKTYPE=mawk \"\$@\"; }"
elif ble/util/assign path "builtin type -P -- gawk 2>/dev/null" && [[ $path ]]; then
builtin eval "function ble/bin/awk { '${path//$q/$Q}' -v AWKTYPE=gawk \"\$@\"; }"
elif ble/util/assign path "builtin type -P -- awk 2>/dev/null" && [[ $path ]]; then
local version type
ble/util/assign version '"$path" --version 2>&1'
if [[ $version == *'GNU Awk'* ]]; then
type=gawk
elif [[ $version == *mawk* ]]; then
type=mawk
elif [[ $version == 'awk version '[12][0-9][0-9][0-9][01][0-9][0-3][0-9] ]]; then
type=nawk
else
type=unknown
fi
builtin eval "function ble/bin/awk { '${path//$q/$Q}' -v AWKTYPE=$type \"\$@\"; }"
else
return 1
fi
}
function ble/bin/awk {
ble/bin/awk/.instantiate &&
ble/bin/awk "$@"
}
# Do not overwrite by .freeze-utility-path
function ble/bin/.frozen:awk { :; }
_ble_bin_awk_supports_null_RS=
function ble/bin/awk.supports-null-record-separator {
if [[ ! $_ble_bin_awk_supports_null_RS ]]; then
local count=0 awk_script='BEGIN { RS = "\0"; } { count++; } END { print count; }'
ble/util/assign count 'printf "a\0b\0" | ble/bin/awk "$awk_script" '
if ((count==2)); then
_ble_bin_awk_supports_null_RS=yes
else
_ble_bin_awk_supports_null_RS=no
fi
fi
[[ $_ble_bin_awk_supports_null_RS == yes ]]
}
#------------------------------------------------------------------------------
# readlink -f (taken from akinomyoga/mshex.git)
## @fn ble/util/readlink path
## @var[out] ret
function ble/util/readlink {
ret=
local path=$1
case "$OSTYPE" in
(cygwin|msys|linux-gnu)
# 少なくとも cygwin, GNU/Linux では readlink -f が使える
ble/util/assign ret 'PATH=/bin:/usr/bin readlink -f "$path"' ;;
(darwin*|*)
# Mac OSX には readlink -f がない。
local PWD=$PWD OLDPWD=$OLDPWD
while [[ -h $path ]]; do
local link; ble/util/assign link 'PATH=/bin:/usr/bin readlink "$path" 2>/dev/null || true'
[[ $link ]] || break
if [[ $link = /* || $path != */* ]]; then
# * $link ~ 絶対パス の時
# * $link ~ 相対パス かつ ( $path が現在のディレクトリにある ) の時
path=$link
else
local dir=${path%/*}
path=${dir%/}/$link
fi
done
ret=$path ;;
esac
}
_ble_bash_path=
function ble/bin/.load-builtin {
local name=$1 path=$2
if [[ ! $_ble_bash_path ]]; then
local ret; ble/util/readlink "$BASH"
_ble_bash_path=$ret
fi
if [[ ! $path ]]; then
local bash_prefix=${ret%/*/*}
path=$bash_prefix/lib/bash/$name
[[ -s $path ]] || return 1
fi
if (enable -f "$path" "$name") &>/dev/null; then
enable -f "$path" "$name"
builtin eval -- "function ble/bin/$name { builtin $name \"\$@\"; }"
return 0
else
return 1
fi
}
# ble/bin/.load-builtin mkdir
# ble/bin/.load-builtin mkfifo
# ble/bin/.load-builtin rm
function ble/base/.create-user-directory {
local var=$1 dir=$2
if [[ ! -d $dir ]]; then
# dangling symlinks are silently removed
[[ ! -e $dir && -h $dir ]] && ble/bin/rm -f "$dir"
if [[ -e $dir || -h $dir ]]; then
ble/util/print "ble.sh: cannot create a directory '$dir' since there is already a file." >&2
return 1
fi
if ! (umask 077; ble/bin/mkdir -p "$dir" && [[ -O $dir ]]); then
ble/util/print "ble.sh: failed to create a directory '$dir'." >&2
return 1
fi
elif ! [[ -r $dir && -w $dir && -x $dir ]]; then
ble/util/print "ble.sh: permission of '$tmpdir' is not correct." >&2
return 1
elif [[ ! -O $dir ]]; then
ble/util/print "ble.sh: owner of '$tmpdir' is not correct." >&2
return 1
fi
builtin eval "$var=\$dir"
}
##
## @var _ble_base
##
## ble.sh のインストール先ディレクトリ。
## 読み込んだ ble.sh の実体があるディレクトリとして解決される。
##
function ble/base/initialize-base-directory {
local src=$1
local defaultDir=${2-}
# resolve symlink
if [[ -h $src ]] && type -t readlink &>/dev/null; then
local ret; ble/util/readlink "$src"; src=$ret
fi
if [[ -s $src && $src != */* ]]; then
_ble_base=$PWD
elif [[ $src == */* ]]; then
local dir=${src%/*}
if [[ ! $dir ]]; then
_ble_base=/
elif [[ $dir != /* ]]; then
_ble_base=$PWD/$dir
else
_ble_base=$dir
fi
else
_ble_base=${defaultDir:-$HOME/.local/share/blesh}
fi
[[ -d $_ble_base ]]
}
if ! ble/base/initialize-base-directory "${BASH_SOURCE[0]}"; then
ble/util/print "ble.sh: ble base directory not found!" 1>&2
builtin unset -v _ble_bash BLE_VERSION BLE_VERSINFO
return 1
fi
##
## @var _ble_base_run
##
## 実行時の一時ファイルを格納するディレクトリ。以下の手順で決定する。
##
## 1. ${XDG_RUNTIME_DIR:=/run/user/$UID} が存在すればその下に blesh を作成して使う。
## 2. /tmp/blesh/$UID を作成可能ならば、それを使う。
## 3. $_ble_base/tmp/$UID を使う。
##
function ble/base/initialize-runtime-directory/.xdg {
local runtime_dir=${XDG_RUNTIME_DIR:-/run/user/$UID}
if [[ ! -d $runtime_dir ]]; then
[[ $XDG_RUNTIME_DIR ]] &&
ble/util/print "ble.sh: XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR' is not a directory." >&2
return 1
fi
if ! [[ -r $runtime_dir && -w $runtime_dir && -x $runtime_dir ]]; then
[[ $XDG_RUNTIME_DIR ]] &&
ble/util/print "ble.sh: XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR' doesn't have a proper permission." >&2
return 1
fi
ble/base/.create-user-directory _ble_base_run "$runtime_dir/blesh"
}
function ble/base/initialize-runtime-directory/.tmp {
[[ -r /tmp && -w /tmp && -x /tmp ]] || return 1
local tmp_dir=/tmp/blesh
if [[ ! -d $tmp_dir ]]; then
[[ ! -e $tmp_dir && -h $tmp_dir ]] && ble/bin/rm -f "$tmp_dir"
if [[ -e $tmp_dir || -h $tmp_dir ]]; then
ble/util/print "ble.sh: cannot create a directory '$tmp_dir' since there is already a file." >&2
return 1
fi
ble/bin/mkdir -p "$tmp_dir" || return 1
ble/bin/chmod a+rwxt "$tmp_dir" || return 1
elif ! [[ -r $tmp_dir && -w $tmp_dir && -x $tmp_dir ]]; then
ble/util/print "ble.sh: permission of '$tmp_dir' is not correct." >&2
return 1
fi
ble/base/.create-user-directory _ble_base_run "$tmp_dir/$UID"
}
function ble/base/initialize-runtime-directory {
ble/base/initialize-runtime-directory/.xdg && return 0
ble/base/initialize-runtime-directory/.tmp && return 0
# fallback
local tmp_dir=$_ble_base/run
if [[ ! -d $tmp_dir ]]; then
ble/bin/mkdir -p "$tmp_dir" || return 1
ble/bin/chmod a+rwxt "$tmp_dir" || return 1
fi
ble/base/.create-user-directory _ble_base_run "$tmp_dir/${USER:-$UID}@$HOSTNAME"
}
if ! ble/base/initialize-runtime-directory; then
ble/util/print "ble.sh: failed to initialize \$_ble_base_run." 1>&2
builtin unset -v _ble_bash BLE_VERSION BLE_VERSINFO
return 1
fi
# ロード時刻の記録 (ble-update で使う為)
: >| "$_ble_base_run/$$.load"
function ble/base/clean-up-runtime-directory {
local file pid mark removed
mark=() removed=()
for file in "$_ble_base_run"/[1-9]*.*; do
[[ -e $file ]] || continue
pid=${file##*/}; pid=${pid%%.*}
[[ ${mark[pid]+set} ]] && continue
mark[pid]=1
if ! builtin kill -0 "$pid" &>/dev/null; then
removed=("${removed[@]}" "$_ble_base_run/$pid."*)
fi
done
((${#removed[@]})) && ble/bin/rm -rf "${removed[@]}"
}
# initialization time = 9ms (for 70 files)
if shopt -q failglob &>/dev/null; then
shopt -u failglob
ble/base/clean-up-runtime-directory
shopt -s failglob
else
ble/base/clean-up-runtime-directory
fi
##
## @var _ble_base_cache
##
## 環境毎の初期化ファイルを格納するディレクトリ。以下の手順で決定する。
##
## 1. ${XDG_CACHE_HOME:=$HOME/.cache} が存在すればその下に blesh を作成して使う。
## 2. $_ble_base/cache.d/$UID を使う。
##
function ble/base/initialize-cache-directory/.xdg {
[[ $_ble_base != */out ]] || return 1
local cache_dir=${XDG_CACHE_HOME:-$HOME/.cache}
if [[ ! -d $cache_dir ]]; then
[[ $XDG_CACHE_HOME ]] &&
ble/util/print "ble.sh: XDG_CACHE_HOME='$XDG_CACHE_HOME' is not a directory." >&2
return 1
fi
if ! [[ -r $cache_dir && -w $cache_dir && -x $cache_dir ]]; then
[[ $XDG_CACHE_HOME ]] &&
ble/util/print "ble.sh: XDG_CACHE_HOME='$XDG_CACHE_HOME' doesn't have a proper permission." >&2
return 1
fi
local ver=${BLE_VERSINFO[0]}.${BLE_VERSINFO[1]}
ble/base/.create-user-directory _ble_base_cache "$cache_dir/blesh/$ver"
}
function ble/base/initialize-cache-directory {
ble/base/initialize-cache-directory/.xdg && return 0
# fallback
local cache_dir=$_ble_base/cache.d
if [[ ! -d $cache_dir ]]; then
ble/bin/mkdir -p "$cache_dir" || return 1
ble/bin/chmod a+rwxt "$cache_dir" || return 1
# relocate an old cache directory if any
local old_cache_dir=$_ble_base/cache
if [[ -d $old_cache_dir && ! -h $old_cache_dir ]]; then
mv "$old_cache_dir" "$cache_dir/$UID"
ln -s "$cache_dir/$UID" "$old_cache_dir"
fi
fi
ble/base/.create-user-directory _ble_base_cache "$cache_dir/$UID"
}
function ble/base/migrate-cache-directory/.move {
local old=$1 new=$2
[[ -e $old ]] || return 0
if [[ -e $new || -L $old ]]; then
ble/bin/rm -rf "$old"
else
ble/bin/mv "$old" "$new"
fi
}
function ble/base/migrate-cache-directory/.check-old-prefix {
local old_prefix=$_ble_base_cache/$1
local new_prefix=$_ble_base_cache/$2
local file
for file in "$old_prefix"*; do
local old=$file
local new=$new_prefix${file#"$old_prefix"}
ble/base/migrate-cache-directory/.move "$old" "$new"
done
}
function ble/base/migrate-cache-directory {
local failglob=
shopt -q failglob && { failglob=1; shopt -u failglob; }
ble/base/migrate-cache-directory/.check-old-prefix cmap+default.binder-source decode.cmap.allseq
ble/base/migrate-cache-directory/.check-old-prefix cmap+default decode.cmap
ble/base/migrate-cache-directory/.check-old-prefix ble-decode-bind decode.bind
local file
for file in "$_ble_base_cache"/*.term; do
local old=$file
local new=$_ble_base_cache/term.${file#"$_ble_base_cache/"}; new=${new%.term}
ble/base/migrate-cache-directory/.move "$old" "$new"
done
ble/base/migrate-cache-directory/.move "$_ble_base_cache/man" "$_ble_base_cache/complete.mandb"