Skip to content

Commit

Permalink
init: correctly restore unset "IFS" and work around non-canonical "IFS"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jul 28, 2022
1 parent 759b96d commit 2fe60b6
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 69 deletions.
1 change: 1 addition & 0 deletions ble-color.sh
Expand Up @@ -492,6 +492,7 @@ function ble-highlight-layer:region/update-dirty-range {
}

function ble-highlight-layer:region/update {
local IFS=$_ble_term_IFS
local omin=-1 omax=-1 osgr= olen=${#_ble_highlight_layer_region_osel[@]}
if ((olen)); then
omin=${_ble_highlight_layer_region_osel[0]}
Expand Down
52 changes: 27 additions & 25 deletions ble-core.sh
Expand Up @@ -88,8 +88,9 @@ function ble/util/restore-arrs {
_ble_debug_check_leak_variable='local @var=__t1wJltaP9nmow__'
function ble/debug/.check-leak-variable {
if [[ ${!1} != __t1wJltaP9nmow__ ]]; then
echo "$1=${!1}:${*:2}" >> a.txt
eval "$1=__t1wJltaP9nmow__"
local IFS=$_ble_term_IFS
ble/util/print "$1=${!1}:${*:2}" >> a.txt
builtin eval "$1=__t1wJltaP9nmow__"
fi
}
#%end
Expand Down Expand Up @@ -241,7 +242,7 @@ function ble/string#common-suffix {
ret=${a:u}
}

## 関数 ble/string#split arr sep str...
## 関数 ble/string#split arr sep str
## 文字列を分割します。
## 空白類を分割に用いた場合は、空要素は削除されます。
##
Expand All @@ -268,19 +269,19 @@ function ble/string#split-words {
set +f
fi
}
## 関数 ble/string#split-lines arr text...
## 関数 ble/string#split-lines arr text
## 文字列を行に分割します。空行も省略されません。
##
## @param[out] arr 分割した文字列を格納する配列名を指定します。
## @param[in] text 分割する文字列を指定します。
##
if ((_ble_bash>=40000)); then
function ble/string#split-lines {
mapfile -t "$1" <<< "${*:2}"
mapfile -t "$1" <<< "$2"
}
else
function ble/string#split-lines {
ble/util/mapfile "$1" <<< "${*:2}"
ble/util/mapfile "$1" <<< "$2"
}
fi
## 関数 ble/string#count-char text chars
Expand Down Expand Up @@ -330,17 +331,17 @@ function ble/string#last-index-of {
((ret>=0))
}

## 関数 ble/string#toggle-case text...
## 関数 ble/string#touppwer text...
## 関数 ble/string#tolower text...
## 関数 ble/string#toggle-case text
## 関数 ble/string#toupper 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 LC_ALL= LC_COLLATE=C
local text=$*
local -a buff ch
local text=$1 ch i
local -a buff=()
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if [[ $ch == [A-Z] ]]; then
Expand All @@ -355,13 +356,13 @@ function ble/string#toggle-case {
IFS= builtin eval 'ret="${buff[*]-}"'
} 2>/dev/null
if ((_ble_bash>=40000)); then
function ble/string#tolower { ret=${*,,}; }
function ble/string#toupper { ret=${*^^}; }
function ble/string#tolower { ret=${1,,}; }
function ble/string#toupper { ret=${1^^}; }
else
function ble/string#tolower {
local LC_ALL= LC_COLLATE=C
local text=$*
local -a buff ch
local i text=$1 ch
local -a buff=()
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if [[ $ch == [A-Z] ]]; then
Expand All @@ -374,8 +375,8 @@ else
} 2>/dev/null
function ble/string#toupper {
local LC_ALL= LC_COLLATE=C
local text=$*
local -a buff ch
local i text=$1 ch
local -a buff=()
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
if [[ $ch == [a-z] ]]; then
Expand All @@ -389,7 +390,7 @@ else
fi

function ble/string#escape-for-sed-regex {
ret="$*"
ret=$1
if [[ $ret == *['\.[*^$/']* ]]; then
local a b
for a in \\ \. \[ \* \^ \$ \/; do
Expand All @@ -398,7 +399,7 @@ function ble/string#escape-for-sed-regex {
fi
}
function ble/string#escape-for-awk-regex {
ret="$*"
ret=$1
if [[ $ret == *['\.[*?+|^$(){}/']* ]]; then
local a b
for a in \\ \. \[ \* \? \+ \| \^ \$ \( \) \{ \} \/; do
Expand All @@ -407,7 +408,7 @@ function ble/string#escape-for-awk-regex {
fi
}
function ble/string#escape-for-extended-regex {
ret="$*"
ret=$1
if [[ $ret == *['\.[*?+|^$(){}']* ]]; then
local a b
for a in \\ \. \[ \* \? \+ \| \^ \$ \( \) \{ \}; do
Expand Down Expand Up @@ -969,13 +970,13 @@ ble/util/isfunction ble/util/getmtime ||
function ble/util/getmtime { ble/util/strftime '%s %N'; }

#------------------------------------------------------------------------------
## 関数 ble/util/buffer text...
## 関数 ble/util/buffer text
_ble_util_buffer=()
function ble/util/buffer {
_ble_util_buffer[${#_ble_util_buffer[@]}]="$*"
_ble_util_buffer[${#_ble_util_buffer[@]}]=$1
}
function ble/util/buffer.print {
ble/util/buffer "$*"$'\n'
ble/util/buffer "$1"$'\n'
}
function ble/util/buffer.flush {
IFS= builtin eval 'builtin echo -n "${_ble_util_buffer[*]-}"'
Expand Down Expand Up @@ -1336,7 +1337,7 @@ _ble_stackdump_title=stackdump
function ble-stackdump {
((bleopt_stackdump_enabled)) || return 1
# builtin echo "${BASH_SOURCE[1]} (${FUNCNAME[1]}): assertion failure $*" >&2
local i nl=$'\n'
local i nl=$'\n' IFS=$_ble_term_IFS
local message="$_ble_term_sgr0$_ble_stackdump_title: $*$nl"
for ((i=1;i<${#FUNCNAME[*]};i++)); do
message="$message @ ${BASH_SOURCE[i]}:${BASH_LINENO[i-1]} (${FUNCNAME[i]})$nl"
Expand All @@ -1348,6 +1349,7 @@ function ble-assert {
local _ble_stackdump_title='assertion failure'
if ! builtin eval -- "$expr"; then
shift
local IFS=$_ble_term_IFS
ble-stackdump "$expr$_ble_term_nl$*"
return 1
else
Expand Down Expand Up @@ -1388,7 +1390,7 @@ if ((_ble_bash>=40000)); then
[[ $_processed ]]
}
function ble/util/idle.push {
ble/array#push _ble_util_idle_task "$*"
ble/array#push _ble_util_idle_task "$1"
}
else
function ble/util/idle.do { false; }
Expand Down
19 changes: 12 additions & 7 deletions ble-decode.sh
Expand Up @@ -240,6 +240,7 @@ ble-decode-kbd/.initialize
## @var[out] ret
function ble-decode-kbd {
local keys; ble/string#split-words keys "$*"
local IFS=$_ble_term_IFS
local key code codes keys
codes=()
for key in "${keys[@]}"; do
Expand Down Expand Up @@ -313,6 +314,7 @@ function ble-decode-unkbd/.single-key {
## @var[in] keys
## @var[out] ret
function ble-decode-unkbd {
local IFS=$_ble_term_IFS
local -a kspecs
local key
for key in $*; do
Expand Down Expand Up @@ -527,9 +529,9 @@ function ble-decode-char {
# シーケンスが登録されていない時
if [[ $_ble_decode_char2_reach ]]; then
local reach rest
reach=($_ble_decode_char2_reach)
ble/string#split-words reach "$_ble_decode_char2_reach"
rest=${_ble_decode_char2_seq:reach[1]}
rest=(${rest//_/ } $char)
ble/string#split-words rest "${rest//_/ } $char"

_ble_decode_char2_reach=
_ble_decode_char2_seq=
Expand Down Expand Up @@ -719,6 +721,7 @@ function ble-decode-char/unbind {
done
}
function ble-decode-char/dump {
local IFS=$_ble_term_IFS
local tseq=$1 nseq ccode
nseq=("${@:2}")
builtin eval "local -a ccodes; ccodes=(\${!_ble_decode_cmap_$tseq[@]})"
Expand Down Expand Up @@ -804,10 +807,10 @@ function ble-decode/DEFAULT_KEYMAP {

## 設定関数 ble/widget/.SHELL_COMMAND command
## ble-bind -cf で登録されたコマンドを処理します。
function ble/widget/.SHELL_COMMAND { eval "$*"; }
function ble/widget/.SHELL_COMMAND { local IFS=$_ble_term_IFS; builtin eval -- "$*"; }
## 設定関数 ble/widget/.EDIT_COMMAND command
## ble-bind -xf で登録されたコマンドを処理します。
function ble/widget/.EDIT_COMMAND { eval "$*"; }
function ble/widget/.EDIT_COMMAND { local IFS=$_ble_term_IFS; builtin eval -- "$*"; }


## 関数 kmap ; ble-decode-key/bind keycodes command
Expand Down Expand Up @@ -1040,7 +1043,8 @@ function ble-decode-key {
if [[ $bleopt_decode_error_kseq_discard ]]; then
_ble_decode_key__seq=
else
local -a keys=(${_ble_decode_key__seq//_/ } $key)
local -a keys
ble/string#split-words keys "${_ble_decode_key__seq//_/ } $key"
_ble_decode_key__seq=
# 2文字目以降を処理
ble-decode-key "${keys[@]:1}"
Expand Down Expand Up @@ -1182,7 +1186,7 @@ function ble-decode-key/.invoke-command {

# setup variables
local WIDGET=$command KEYMAP=$_ble_decode_key__kmap
local -a KEYS=(${_ble_decode_key__seq//_/ } $key)
local -a KEYS; ble/string#split-words KEYS "${_ble_decode_key__seq//_/ } $key"
_ble_decode_key__seq=

ble-decode-key/.invoke-hook "$_ble_decode_KCODE_BEFORE_COMMAND"
Expand All @@ -1197,7 +1201,7 @@ function ble-decode-key/.call-widget {

# setup variables
local WIDGET=$1 KEYMAP=$_ble_decode_key__kmap
local -a KEYS=($2)
local -a KEYS; ble/string#split-words KEYS "$2"

builtin eval -- "$WIDGET"
}
Expand All @@ -1220,6 +1224,7 @@ function ble-decode/start-keylog {
}
function ble-decode/end-keylog {
{
local IFS=$_ble_term_IFS
echo '===== bytes ====='
printf '%s\n' "${_ble_keylogger_bytes[*]}"
echo
Expand Down

0 comments on commit 2fe60b6

Please sign in to comment.