Skip to content

Commit

Permalink
main (adjust-builtin-wrappers): fix persistent tempenv IFS= in bash-5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Apr 3, 2023
1 parent 7c4ff7b commit 5baf6f6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 5 additions & 1 deletion ble.pp
Expand Up @@ -284,11 +284,15 @@ function ble/base/is-POSIXLY_CORRECT {
_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"
IFS= ble/bash/read -d '' defs < "$_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")
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -503,6 +503,7 @@
- term (`terminology`): work around terminal glitches `#D1946` 9a1b4f9
- main (`ble/bin/awk`): add workaround for macOS `awk-32` `#D1974` e2ec89c
- util.hook: workaround bash-5.2 bug of nested read by `WINCH` `#D1981` a5b10e8
- main (`ble/base/adjust-builtin-wrappers`): fix persistent tempenv `IFS=` in bash-5.0 (reported by pt12lol) `#D2030` xxxxxxxx
- edit: always adjust the terminal states with `bind -x` (reported by linwaytin) `#D1983` 5d14cf1
- edit: restore `PS1` while processing `bind -x` (reported by adoyle-h) `#D2024` 2eadcd5b
- syntax: suppress brace expansions in designated array initialization in Bash 5.3 `#D1989` 1e7b884
Expand Down
28 changes: 28 additions & 0 deletions note.txt
Expand Up @@ -6724,6 +6724,34 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2023-04-03

* main (adjust-builtin-wrappers): IFS= が設定されてしまう @ bash-5.0 (reported by pt12lol) [#D2030]
https://github.com/akinomyoga/ble.sh/issues/311

POSIXLY_CORRECT=y の環境で IFS= ble/bash/read を実行していた為に IFS= が関
数呼び出し後も残る様になってしまったのだった。

? 然し不思議なのは何故か bash-5.0 だけで振る舞いが再現するという事。
bash-5.1 以降は関数呼び出しの変数代入は posix であっても環境に残らない様
になっているのは良い。然し、bash-4.4 以前については bash-5.0 と同様に環境
に tempenv が残る様に見える。何故この時にだけ問題がないのだろうか。或いは、
何処か別のレベルで local IFS が設定されているという事か、或いは呼び出し元
の local POSIXLY_CORRECT=y は影響を与えないという事なのだろうか。よく分か
らないが取り敢えずは問題は封じた筈なので気にしない事にする。

取り敢えず関数呼び出しの前に変数代入を置かない様にする。明らかに ble.sh の
内部だけで使われる関数については気にしなくて良い。util.sh の一般のライブラ
リ関数についてはちゃんと posix を避ける様にする事にする。

? これよりも前には問題にならなかったのかというのが疑問である→どうも試して
みた所 read や builtin read では tempenv は posix モードであったとしても
環境には残らない様である。つまり、本当にシェル関数特有の振る舞いだったと
いう事である。

builtin read から ble/bash/read に置き換えられたのが a5b10e8b である。こ
れは #D1981 に関係している。

2023-04-02

* bgproc: bash-4.2 での bgproc#start で {bgproc[0]} は存在しないというエラーメッセージ [#D2029]
Expand Down
11 changes: 7 additions & 4 deletions src/util.sh
Expand Up @@ -1656,7 +1656,8 @@ if ((_ble_bash>=40000)); then
else
function ble/util/readfile { # 465ms for man bash
[[ -r $2 && ! -d $2 ]] || return 1
IFS= ble/bash/read -d '' "$1" < "$2"
local IFS=
ble/bash/read -d '' "$1" < "$2"
return 0
}
function ble/util/mapfile {
Expand Down Expand Up @@ -2216,8 +2217,8 @@ else
function ble/util/assign {
local _ble_local_tmpfile; ble/util/assign/.mktmp
builtin eval -- "$2" >| "$_ble_local_tmpfile"
local _ble_local_ret=$?
IFS= ble/bash/read -d '' "$1" < "$_ble_local_tmpfile"
local _ble_local_ret=$? IFS=
ble/bash/read -d '' "$1" < "$_ble_local_tmpfile"
ble/util/assign/.rmtmp
builtin eval "$1=\${$1%\$_ble_term_nl}"
return "$_ble_local_ret"
Expand Down Expand Up @@ -6666,9 +6667,11 @@ else

local bytes byte
ble/util/assign bytes '
while IFS= ble/bash/read -n 1 byte; do
local IFS=
while ble/bash/read -n 1 byte; do
builtin printf "%d " "'\''$byte"
done <<< "$s"
IFS=$_ble_term_IFS
'
"ble/encoding:$bleopt_input_encoding/b2c" $bytes
}
Expand Down

0 comments on commit 5baf6f6

Please sign in to comment.