-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
ble.pp
3100 lines (2823 loc) · 105 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]
#%[leakvar = ""]
#%#----------------------------------------------------------------------------
#%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
#%if leakvar
#%%expand
$"leakvar"=__t1wJltaP9nmow__
#%%end.i
function ble/bin/grep { command grep "$@"; }
function ble/util/print { printf '%s\n' "$1"; }
source "${BASH_SOURCE%/*}/lib/core-debug.sh"
#%end
#%define inc
#%%[guard_name = "@_included".replace("[^_a-zA-Z0-9]", "_")]
#%%expand
#%%%if $"guard_name" != 1
#%%%%[$"guard_name" = 1]
###############################################################################
# Included from @.sh
#%%%%if leakvar
ble/debug/leakvar#check $"leakvar" "[before include @.sh]"
#%%%%end.i
#%%%%if measure_load_time
time {
#%%%%%include @.sh
ble/debug/measure-set-timeformat '@.sh'
}
#%%%%else
#%%%%%include @.sh
#%%%%end
#%%%%if leakvar
ble/debug/leakvar#check $"leakvar" "[after include @.sh]"
#%%%%end.i
#%%%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 <[email protected]>
#
#%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
{
#%[commit_hash = getenv("BLE_GIT_COMMIT_ID")]
#%[ble_version = getenv("FULLVER") + "+" + commit_hash]
#%expand
##%if commit_hash != ""
_ble_init_version=$"ble_version"
##%else
###%error Failed to get the commit id (version = $"ble_version").
##%end
#%end.i
_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' \
' --install PREFIX' \
' Install ble.sh and exit' \
'' \
' --rcfile=BLERC' \
' --init-file=BLERC' \
' Specify the ble init file. The default is ~/.blerc if any, or' \
' ~/.config/blesh/init.sh.' \
'' \
' --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".' \
' When "attach" is specified, ble.sh immediately attaches to the session in' \
' "source ble.sh". When "prompt" is specified, ble.sh attaches to the' \
' session before the first prompt using PROMPT_COMMAND. When "none" is' \
' specified, ble.sh does not attach to the session automatically, so' \
' ble-attach needs to be called explicitly. The option "--noattach" is a' \
' synonym for "--attach=none".' \
'' \
' --inputrc=TYPE' \
' --noinputrc' \
' The option "--inputrc" selects the strategy of reconstructing user' \
' keybindings from the list: "auto" (default), "diff", "all", "user", "none".' \
' When "diff" is specified, user keybindings are extracted by the diff of the' \
' outputs of the "bind" builtin between the current session and the plain' \
' Bash. When "all" is specified, the user keybindings are extracted from' \
' /etc/inputrc and ${INPUTRC:-~/.inputrc*}. When "user" is specified, the' \
' user keybindings are extracted from ${INPUTRC:-~/.inputrc*}. When "none"' \
' is specified, the user keybindings are not reconstructed from the state of' \
' Readline, and only the bindings by "ble-bind" are applied. The option' \
' "--noinputrc" is a synonym for "--inputrc=none".' \
'' \
' --keep-rlvars' \
' Do not change readline settings for ble.sh' \
'' \
' --bash-debug-version=TYPE' \
' This controls the warning mesage for the debug version of Bash. When' \
' "full" is specified to TYPE, ble.sh prints the full message to the terminal' \
' when it is loaded in a debug version of Bash. This is the default. When' \
' "short" is specified, a short version of the message is printed. When' \
' "once" is specified, the full message is printed only once for a specific' \
' version of debug Bash. When "ignore" is specified, the message is not' \
' printed even when ble.sh is loaded in a debug version of Bash.' \
'' \
' -o BLEOPT=VALUE' \
' Set a value for the specified bleopt option.' \
' --debug-bash-output' \
' Internal settings for debugging' \
'' ;;
--test | --update | --clear-cache | --lib | --install) _ble_init_command=1 ;;
esac
done
unset _ble_init_arg
if [ -n "$_ble_init_exit" ]; then
unset _ble_init_exit
unset _ble_init_command
unset _ble_init_version
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
unset _ble_init_exit
unset _ble_init_command
unset _ble_init_version
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
unset -v _ble_init_exit _ble_init_command _ble_init_version
return 1 2>/dev/null || exit 1
fi 3>&2 >/dev/null 2>&1 # set -x 対策 #D0930
if [[ ! $_ble_init_command ]]; then
# We here check the cases where we do not want a line editor. We first check
# the cases that Bash provides. We also check the cases where other
# frameworks try to do a hack using an interactive Bash. We honestly do not
# want to add exceptions for every random framework that tries to do a naive
# hack using interactive sessions, but it is easier than instructing users to
# add a proper workaround/check by themselves.
if [[ ${BASH_EXECUTION_STRING+set} ]]; then
# builtin echo "ble.sh: ble.sh will not be activated for Bash started with '-c' option." >&3
_ble_init_exit=1
elif ((BASH_SUBSHELL)); then
builtin echo "ble.sh: ble.sh cannot be loaded into a subshell." >&3
_ble_init_exit=1
elif [[ $- != *i* ]]; then
case " ${BASH_SOURCE[*]##*/} " in
(*' .bashrc '* | *' .bash_profile '* | *' .profile '* | *' bashrc '* | *' profile '*) ((0)) ;;
esac &&
builtin echo "ble.sh: This is not an interactive session." >&3 || ((1))
_ble_init_exit=1
elif ! [[ -t 4 && -t 5 ]] && ! { [[ ${bleopt_connect_tty-} ]] && >/dev/tty; }; then
if [[ ${bleopt_connect_tty-} ]]; then
builtin echo "ble.sh: cannot find a controlling TTY/PTY in this session." >&3
else
builtin echo "ble.sh: stdout/stdin are not connected to TTY/PTY." >&3
fi
_ble_init_exit=1
elif [[ ${NRF_CONNECT_VSCODE-} && ! -t 3 ]]; then
# Note #D2129: VS Code Extension "nRF Connect" tries to extract an
# interactive setting by sending multiline commands to an interactive
# session. We may turn off accept_line_threshold for an nRF Connect
# session as we do for Midnight Commander, but we do not need to enable the
# line editor for nRF Connect in the first place.
_ble_init_exit=1
fi
if [[ $_ble_init_exit ]]; then
builtin unset -v _ble_init_exit _ble_init_command _ble_init_version
return 1 2>/dev/null || builtin exit 1
fi
fi 3>&2 4<&0 5>&1 &>/dev/null # set -x 対策 #D0930
{
_ble_bash=$((BASH_VERSINFO[0]*10000+BASH_VERSINFO[1]*100+BASH_VERSINFO[2]))
## @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_set=
_ble_bash_FUNCNEST_adjust='
if [[ ! $_ble_bash_FUNCNEST_adjusted ]]; then
_ble_bash_FUNCNEST_adjusted=1
_ble_bash_FUNCNEST_set=${FUNCNEST+set}
_ble_bash_FUNCNEST=${FUNCNEST-}
\builtin unset -v FUNCNEST
fi 2>/dev/null'
_ble_bash_FUNCNEST_restore='
if [[ $_ble_bash_FUNCNEST_adjusted ]]; then
_ble_bash_FUNCNEST_adjusted=
if [[ $_ble_bash_FUNCNEST_set ]]; then
FUNCNEST=$_ble_bash_FUNCNEST
else
\builtin unset -v FUNCNEST
fi
fi 2>/dev/null'
_ble_bash_FUNCNEST_local_adjust='
\local _ble_local_FUNCNEST _ble_local_FUNCNEST_set
_ble_local_FUNCNEST_set=${FUNCNEST+set}
_ble_local_FUNCNEST=${FUNCNEST-}
if [[ $_ble_local_FUNCNEST_set ]]; then
\local FUNCNEST
\builtin unset -v FUNCNEST
fi'
_ble_bash_FUNCNEST_local_leave='
if [[ $_ble_local_FUNCNEST_set ]]; then
FUNCNEST=$_ble_local_FUNCNEST
fi'
\builtin eval -- "$_ble_bash_FUNCNEST_adjust"
\builtin unset -v POSIXLY_CORRECT
_ble_bash_POSIXLY_CORRECT_adjust='
if [[ ! ${_ble_bash_POSIXLY_CORRECT_adjusted-} ]]; then
_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
fi'
_ble_bash_POSIXLY_CORRECT_unset='
if [[ ${POSIXLY_CORRECT+set} ]]; then
\builtin unset -v POSIXLY_CORRECT
ble/base/workaround-POSIXLY_CORRECT
fi'
_ble_bash_POSIXLY_CORRECT_local_adjust='
\builtin local _ble_local_POSIXLY_CORRECT _ble_local_POSIXLY_CORRECT_set
_ble_local_POSIXLY_CORRECT_set=${POSIXLY_CORRECT+set}
_ble_local_POSIXLY_CORRECT=${POSIXLY_CORRECT-}
'$_ble_bash_POSIXLY_CORRECT_unset
_ble_bash_POSIXLY_CORRECT_local_leave='
if [[ $_ble_local_POSIXLY_CORRECT_set ]]; then
POSIXLY_CORRECT=$_ble_local_POSIXLY_CORRECT
fi'
_ble_bash_POSIXLY_CORRECT_local_enter='
_ble_local_POSIXLY_CORRECT_set=${POSIXLY_CORRECT+set}
_ble_local_POSIXLY_CORRECT=${POSIXLY_CORRECT-}
'$_ble_bash_POSIXLY_CORRECT_unset
_ble_bash_POSIXLY_CORRECT_local_return='
\builtin local _ble_local_POSIXLY_CORRECT_ext=$?
if [[ $_ble_local_POSIXLY_CORRECT_set ]]; then
POSIXLY_CORRECT=$_ble_local_POSIXLY_CORRECT
fi
\return "$_ble_local_POSIXLY_CORRECT_ext"'
} 2>/dev/null
function ble/base/workaround-POSIXLY_CORRECT {
# This function will be overwritten by ble-decode
true
}
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
builtin eval -- "$_ble_bash_POSIXLY_CORRECT_unset"
fi
}
## @fn ble/base/is-POSIXLY_CORRECT
## Check if the POSIX mode is enabled in the user context. This function is
## assumed to be called in the adjusted state.
function ble/base/is-POSIXLY_CORRECT {
[[ $_ble_bash_POSIXLY_CORRECT_adjusted && $_ble_bash_POSIXLY_CORRECT_set ]]
}
function ble/variable#load-user-state/variable:FUNCNEST {
if [[ $_ble_bash_FUNCNEST_adjusted ]]; then
__ble_var_set=$_ble_bash_FUNCNEST_set
__ble_var_val=$_ble_bash_FUNCNEST
return 0
elif [[ ${_ble_local_FUNCNEST_set-} ]]; then
__ble_var_set=$_ble_local_FUNCNEST_set
__ble_var_set=$_ble_local_FUNCNEST
return 0
else
return 1
fi
}
function ble/variable#load-user-state/variable:POSIXLY_CORRECT {
if [[ $_ble_bash_POSIXLY_CORRECT_adjusted ]]; then
__ble_var_set=$_ble_bash_POSIXLY_CORRECT_set
__ble_var_val=$_ble_bash_POSIXLY_CORRECT
return 0
elif [[ ${_ble_local_POSIXLY_CORRECT_set-} ]]; then
__ble_var_set=$_ble_local_POSIXLY_CORRECT_set
__ble_var_set=$_ble_local_POSIXLY_CORRECT
return 0
else
return 1
fi
}
## @fn ble/base/list-shopt names...
## @var[out] shopt
if ((_ble_bash>=40100)); then
function ble/base/list-shopt { shopt=$BASHOPTS; }
else
function ble/base/list-shopt {
shopt=
local name
for name; do
shopt -q "$name" 2>/dev/null && shopt=$shopt:$name
done
}
fi 2>/dev/null # set -x 対策
function ble/base/evaldef {
local shopt
ble/base/list-shopt extglob expand_aliases
shopt -s extglob
shopt -u expand_aliases
builtin eval -- "$1"; local ext=$?
[[ :$shopt: == *:extglob:* ]] || shopt -u extglob
[[ :$shopt: != *:expand_aliases:* ]] || shopt -s expand_aliases
return "$ext"
}
{
_ble_bash_builtins_adjusted=
_ble_bash_builtins_save=
} 2>/dev/null # set -x 対策
## @fn ble/base/adjust-builtin-wrappers/.assign
## @remarks This function may be called with POSIXLY_CORRECT=y
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"
local IFS=
ble/bash/read -d '' defs < "$_ble_local_tmpfile"
IFS=$_ble_term_IFS
ble/util/assign/rmtmp
else
defs=$(builtin eval -- "$1")
fi || ((1))
}
function ble/base/adjust-builtin-wrappers/.impl1 {
# 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 builtins1 keywords1
builtins1=(builtin unset enable unalias return break continue declare local typeset eval exec set)
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 local POSIXLY_CORRECT=y
builtin unset -f "${builtins1[@]}"
builtin unalias "${builtins1[@]}" "${keywords1[@]}" || ((1)) # set -e 対策
builtin eval -- "$_ble_bash_POSIXLY_CORRECT_unset"
}
function ble/base/adjust-builtin-wrappers/.impl2 {
# 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関数名) は別関数で実行する事にする。呼び出し元で既に
# builtin unset -v 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 shell builtin\n'}
_ble_bash_builtins_save=$_ble_bash_builtins_save$'\n'$defs
builtin unset -f :
builtin unalias : || ((1)) # set -e 対策
}
## @fn ble/base/adjust-builtin-wrappers
##
## Note: This function needs to be called after adjusting POSIXLY_CORRECT by
## calling « builtin eval -- "$_ble_bash_POSIXLY_CORRECT_adjust" »
##
## Note (#D2221) We have been delayed the execution of "unset -f :"
## (adjust-builtin-wrappers-2) until POSIXLY_CORRECT is unset. However, .
## we can now call "ble/base/adjust-builtin-wrappers/.impl2" immediately
## after "ble/base/adjust-builtin-wrappers/.impl1" because we now unset
## POSIXLY_CORRECT earlier. We combine those two functions again.
##
function ble/base/adjust-builtin-wrappers {
ble/base/adjust-builtin-wrappers/.impl1
# Note (#D2221): In Bash 3.0 and 3.1, when "local POSIXLY_CORRECT" is used in
# a function, the POSIX mode remains effective even after the function
# returns. This can be fixed by calling "unset -v POSIXLY_CORRECT".
builtin unset -v POSIXLY_CORRECT
ble/base/adjust-builtin-wrappers/.impl2
} 2>/dev/null
function ble/base/restore-builtin-wrappers {
if [[ $_ble_bash_builtins_adjusted ]]; then
_ble_bash_builtins_adjusted=
ble/base/evaldef "$_ble_bash_builtins_save"
return 0
fi
}
{
ble/base/adjust-builtin-wrappers
# 対策 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
}
# BASH_XTRACEFD は書き換えると勝手に元々設定されていた fd を閉じてしまうので、
# 元々の fd を dup しておくなど特別な配慮が必要。
{
_ble_bash_xtrace=()
_ble_bash_xtrace_debug_enabled=
_ble_bash_xtrace_debug_filename=
_ble_bash_xtrace_debug_fd=
_ble_bash_XTRACEFD=
_ble_bash_XTRACEFD_set=
_ble_bash_XTRACEFD_dup=
_ble_bash_PS4=
} 2>/dev/null # set -x 対策
# From src/util.sh (ble/fd#is-open and ble/fd#alloc/.nextfd)
function ble/base/xtrace/.fdcheck { >&"$1"; } 2>/dev/null
function ble/base/xtrace/.fdnext {
local _ble_local_init=${_ble_util_openat_nextfd:=${bleopt_openat_base:-30}}
for (($1=_ble_local_init;$1<_ble_local_init+1024;$1++)); do
ble/base/xtrace/.fdcheck "${!1}" || break
done
(($1<_ble_local_init+1024)) ||
{ (($1=_ble_local_init,_ble_util_openat_nextfd++)); builtin eval "exec ${!1}>&-"; } ||
((1))
}
function ble/base/xtrace/.log {
local open=---- close=----
if ((_ble_bash>=40200)); then
builtin printf '%s [%(%F %T %Z)T] %s %s\n' "$open" -1 "$1" "$close"
else
builtin printf '%s [%s] %s %s\n' "$open" "$(date 2>/dev/null)" "$1" "$close"
fi >&"${BASH_XTRACEFD:-2}"
}
function ble/base/xtrace/adjust {
local level=${#_ble_bash_xtrace[@]} IFS=$' \t\n'
if [[ $- == *x* ]]; then
_ble_bash_xtrace[level]=1
else
_ble_bash_xtrace[level]=
fi
set +x
((_ble_bash>=40000&&level==0)) || return 0
_ble_bash_xtrace_debug_enabled=
if [[ ${bleopt_debug_xtrace:-/dev/null} == /dev/null ]]; then
if [[ $_ble_bash_xtrace_debug_fd ]]; then
builtin eval "exec $_ble_bash_xtrace_debug_fd>&-" || return 0 # disable=#D2164 (here bash4+)
_ble_bash_xtrace_debug_filename=
_ble_bash_xtrace_debug_fd=
fi
else
if [[ $_ble_bash_xtrace_debug_filename != "$bleopt_debug_xtrace" ]]; then
_ble_bash_xtrace_debug_filename=$bleopt_debug_xtrace
[[ $_ble_bash_xtrace_debug_fd ]] || ble/base/xtrace/.fdnext _ble_bash_xtrace_debug_fd
builtin eval "exec $_ble_bash_xtrace_debug_fd>>\"$bleopt_debug_xtrace\"" || return 0
fi
_ble_bash_XTRACEFD=${BASH_XTRACEFD-}
_ble_bash_XTRACEFD_set=${BASH_XTRACEFD+set}
if [[ ${BASH_XTRACEFD-} =~ ^[0-9]+$ ]] && ble/base/xtrace/.fdcheck "$BASH_XTRACEFD"; then
ble/base/xtrace/.fdnext _ble_bash_XTRACEFD_dup
builtin eval "exec $_ble_bash_XTRACEFD_dup>&$BASH_XTRACEFD" || return 0
builtin eval "exec $BASH_XTRACEFD>&$_ble_bash_xtrace_debug_fd" || return 0
else
_ble_bash_XTRACEFD_dup=
local newfd; ble/base/xtrace/.fdnext newfd
builtin eval "exec $newfd>&$_ble_bash_xtrace_debug_fd" || return 0
BASH_XTRACEFD=$newfd
fi
ble/variable#copy-state PS4 _ble_base_PS4
PS4=${bleopt_debug_xtrace_ps4:-'+ '}
_ble_bash_xtrace_debug_enabled=1
ble/base/xtrace/.log "$FUNCNAME"
set -x
fi
}
function ble/base/xtrace/restore {
local level=$((${#_ble_bash_xtrace[@]}-1)) IFS=$' \t\n'
((level>=0)) || return 0
if [[ ${_ble_bash_xtrace[level]-} ]]; then
set -x
else
set +x
fi
builtin unset -v '_ble_bash_xtrace[level]'
((_ble_bash>=40000&&level==0)) || return 0
if [[ $_ble_bash_xtrace_debug_enabled ]]; then
ble/base/xtrace/.log "$FUNCNAME"
_ble_bash_xtrace_debug_enabled=
# Note: ユーザーの BASH_XTRACEFD にごみが混入しない様にする為、
# BASH_XTRACEFD を書き換える前に先に PS4 を戻す。
ble/variable#copy-state _ble_base_PS4 PS4
if [[ $_ble_bash_XTRACEFD_dup ]]; then
# BASH_XTRACEFD の fd を元の出力先に繋ぎ直す
builtin eval "exec $BASH_XTRACEFD>&$_ble_bash_XTRACEFD_dup" &&
builtin eval "exec $_ble_bash_XTRACEFD_dup>&-" || ((1)) # disable=#D2164 (here bash4+)
else
# BASH_XTRACEFD の fd は新しく割り当てた fd なので値上書きで閉じて良い
if [[ $_ble_bash_XTRACEFD_set ]]; then
BASH_XTRACEFD=$_ble_bash_XTRACEFD
else
builtin unset -v BASH_XTRACEFD
fi
fi
fi
}
## @fn ble/base/.adjust-bash-options vset vshopt
## @var[out] $vset
## @var[out] $vshopt
function ble/base/.adjust-bash-options {
builtin eval -- "$1=\$-"
set +evukT -B
ble/base/xtrace/adjust
[[ $2 == shopt ]] || local shopt
# Note: nocasematch は bash-3.1 以上
ble/base/list-shopt extdebug nocasematch
[[ $2 == shopt ]] || builtin eval -- "$2=\$shopt"
shopt -u extdebug
shopt -u nocasematch 2>/dev/null
return 0
} 2>/dev/null # set -x 対策
## @fn ble/base/.restore-bash-options var_set var_shopt
## @param[out] var_set var_shopt
function ble/base/.restore-bash-options {
local set=${!1} shopt=${!2}
[[ :$shopt: == *:nocasematch:* ]] && shopt -s nocasematch
[[ :$shopt: == *:extdebug:* ]] && shopt -s extdebug
ble/base/xtrace/restore
[[ $set == *B* ]] || set +B
[[ $set == *T* ]] && set -T
[[ $set == *k* ]] && set -k
[[ $set == *u* ]] && set -u
[[ $set == *v* ]] && set -v
[[ $set == *e* ]] && set -e # set -e は最後
return 0
} 2>/dev/null # set -x 対策
{
: "${_ble_bash_options_adjusted=}"
_ble_bash_set=$-
_ble_bash_shopt=${BASHOPTS-}
} 2>/dev/null # set -x 対策
function ble/base/adjust-bash-options {
[[ $_ble_bash_options_adjusted ]] && return 1 || ((1)) # set -e 対策
_ble_bash_options_adjusted=1
ble/base/.adjust-bash-options _ble_bash_set _ble_bash_shopt
# Note: expand_aliases はユーザー設定を復元する為に記録する
_ble_bash_expand_aliases=
shopt -q expand_aliases 2>/dev/null &&
_ble_bash_expand_aliases=1
# locale 待避
# Note #D1854: ble/widget/display-shell-version で此処で待避した変数を参照す
# る事に注意する。此処に新しい変数を追加する時は display-shell-version の方
# にも処理スキップを追加する必要がある。
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
# TMOUT 確認 #D1630 WA readonly TMOUT
if local TMOUT= 2>/dev/null; then # #D1630 WA
_ble_bash_tmout_wa=()
else
_ble_bash_tmout_wa=(-t 2147483647)
fi
} 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/base/.restore-bash-options _ble_bash_set _ble_bash_shopt
} 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
}
function ble/variable#load-user-state/variable:LC_ALL/.impl {
local __ble_save=_ble_bash_$1
__ble_var_set=${!__ble_save+set}
__ble_var_val=${!__ble_save-}
[[ $__ble_var_set ]] && ble/variable#get-attr -v __ble_var_att "$1"
return 0
}
function ble/variable#load-user-state/variable:LC_COLLATE {
ble/variable#load-user-state/variable:LC_ALL/.impl LC_COLLATE
}
function ble/variable#load-user-state/variable:LC_ALL {
ble/variable#load-user-state/variable:LC_ALL/.impl LC_ALL
}
function ble/variable#load-user-state/variable:LC_CTYPE {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_CTYPE
}
function ble/variable#load-user-state/variable:LC_MESSAGES {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_MESSAGES
}
function ble/variable#load-user-state/variable:LC_NUMERIC {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_NUMERIC
}
function ble/variable#load-user-state/variable:LC_TIME {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_TIME
}
function ble/variable#load-user-state/variable:LANG {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LANG
}
{ 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 2>/dev/null | grep '"\\C-?"') == '"\C-?": backward-kill-line' ]] &&
builtin bind -m emacs '"\C-?": backward-delete-char' 2>/dev/null
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"
builtin unset -v _ble_bash
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"
builtin unset -v _ble_bash
return 1 2>/dev/null || builtin exit 1
fi
#--------------------------------------
# save IFS / BASH_REMATCH
function ble/init/adjust-IFS {
_ble_init_original_IFS_set=${IFS+set}
_ble_init_original_IFS=$IFS
IFS=$' \t\n'
}
function ble/init/restore-IFS {
if [[ $_ble_init_original_IFS_set ]]; then
IFS=$_ble_init_original_IFS
else
builtin unset -v IFS
fi
builtin unset -v _ble_init_original_IFS_set
builtin unset -v _ble_init_original_IFS
}
function ble/variable#load-user-state/variable:IFS {
__ble_var_set=${_ble_init_original_IFS_set-}
__ble_var_val=${_ble_init_original_IFS-}
ble/variable#get-attr -v __ble_var_att IFS
return 0
}
if ((_ble_bash>=50100)); then
_ble_bash_BASH_REMATCH_level=0
_ble_bash_BASH_REMATCH=()
function ble/base/adjust-BASH_REMATCH {
((_ble_bash_BASH_REMATCH_level++==0)) || return 0
_ble_bash_BASH_REMATCH=("${BASH_REMATCH[@]}")
}
function ble/base/restore-BASH_REMATCH {
((_ble_bash_BASH_REMATCH_level>0&&
--_ble_bash_BASH_REMATCH_level==0)) || return 0
BASH_REMATCH=("${_ble_bash_BASH_REMATCH[@]}")
}
else
_ble_bash_BASH_REMATCH_level=0
_ble_bash_BASH_REMATCH=()
_ble_bash_BASH_REMATCH_rex=none
## @fn ble/base/adjust-BASH_REMATCH/increase delta
## @param[in] delta
## @var[in,out] i rex
function ble/base/adjust-BASH_REMATCH/increase {
local delta=$1
((delta)) || return 1
((i+=delta))
if ((delta==1)); then
rex=$rex.
else
rex=$rex.{$delta}
fi
}
function ble/base/adjust-BASH_REMATCH/is-updated {
local i n=${#_ble_bash_BASH_REMATCH[@]}
((n!=${#BASH_REMATCH[@]})) && return 0
for ((i=0;i<n;i++)); do
[[ ${_ble_bash_BASH_REMATCH[i]} != "${BASH_REMATCH[i]}" ]] && return 0
done
return 1
}
# This is a simplified version of ble/string#index-of text sub
function ble/base/adjust-BASH_REMATCH/.find-substr {
local t=${1#*"$2"}
((ret=${#1}-${#t}-${#2},ret<0&&(ret=-1),ret>=0))
}
function ble/base/adjust-BASH_REMATCH {
((_ble_bash_BASH_REMATCH_level++==0)) || return 0
ble/base/adjust-BASH_REMATCH/is-updated || return 1
local size=${#BASH_REMATCH[@]}
if ((size==0)); then
_ble_bash_BASH_REMATCH=()
_ble_bash_BASH_REMATCH_rex=none
return 0
fi
local rex= i=0
local text=$BASH_REMATCH sub ret isub
local -a rparens=()
local isub rex i=0 count=0
for ((isub=1;isub<size;isub++)); do
local sub=${BASH_REMATCH[isub]}
# 既存の子一致の孫一致になるか確認
while ((count>=1)); do
local end=${rparens[count-1]}
if ble/base/adjust-BASH_REMATCH/.find-substr "${text:i:end-i}" "$sub"; then
ble/base/adjust-BASH_REMATCH/increase "$ret"
((rparens[count++]=i+${#sub}))
rex=$rex'('
break
else
ble/base/adjust-BASH_REMATCH/increase "$((end-i))"
rex=$rex')'
builtin unset -v 'rparens[--count]'
fi
done
((count>0)) && continue
# 新しい子一致
if ble/base/adjust-BASH_REMATCH/.find-substr "${text:i}" "$sub"; then
ble/base/adjust-BASH_REMATCH/increase "$ret"
((rparens[count++]=i+${#sub}))
rex=$rex'('
else
break # 復元失敗
fi
done
while ((count>=1)); do
local end=${rparens[count-1]}
ble/base/adjust-BASH_REMATCH/increase "$((end-i))"
rex=$rex')'
builtin unset -v 'rparens[--count]'
done
ble/base/adjust-BASH_REMATCH/increase "$((${#text}-i))"
_ble_bash_BASH_REMATCH=("${BASH_REMATCH[@]}")
_ble_bash_BASH_REMATCH_rex=$rex
}
function ble/base/restore-BASH_REMATCH {
((_ble_bash_BASH_REMATCH_level>0&&
--_ble_bash_BASH_REMATCH_level==0)) || return 0
[[ ${_ble_bash_BASH_REMATCH-} =~ $_ble_bash_BASH_REMATCH_rex ]]
}
fi
function ble/variable#load-user-state/variable:BASH_REMATCH {
if ((_ble_bash_BASH_REMATCH_level)); then
__ble_var_set=${BASH_REMATCH+set}
__ble_var_val=("${_ble_bash_BASH_REMATCH[@]}")
ble/variable#get-attr -v __ble_var_att BASH_REMATCH
return 0
else
return 1
fi
}
ble/init/adjust-IFS
ble/base/adjust-BASH_REMATCH
## @fn ble/init/clean-up [opts]
function ble/init/clean-up {
local ext=$? opts=$1 # preserve exit status
# 一時グローバル変数消去
builtin unset -v _ble_init_version
builtin unset -v _ble_init_exit
builtin unset -v _ble_init_command
builtin unset -v _ble_init_attached
# 状態復元
ble/base/restore-BASH_REMATCH
ble/init/restore-IFS
if [[ :$opts: != *:check-attach:* || ! $_ble_attached ]]; then
ble/base/restore-bash-options
ble/base/restore-POSIXLY_CORRECT
ble/base/restore-builtin-wrappers
builtin eval -- "$_ble_bash_FUNCNEST_restore"
fi
return "$ext"
}
#------------------------------------------------------------------------------
# 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
local opt_inputrc=auto
builtin unset -v _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)
opt_inputrc=none ;;
(--inputrc=*) opt_inputrc=${arg#*=} ;;
(--inputrc)
if (($#)); then
opt_inputrc=$1; shift
else
opt_inputrc=inputrc
opts=$opts:E
ble/util/print "ble.sh ($arg): an option argument is missing." >&2
fi ;;
(--rcfile=*|--init-file=*|--rcfile|--init-file)
if [[ $arg != *=* ]]; then