Skip to content

Commit

Permalink
util(ble/function#evaldef): suppress alias expansions
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jul 29, 2024
1 parent b154058 commit 51e762f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 41 deletions.
51 changes: 38 additions & 13 deletions ble.pp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ function ble/util/print { printf '%s\n' "$1"; }
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
Expand Down Expand Up @@ -333,6 +335,30 @@ function ble/base/is-POSIXLY_CORRECT {
[[ $_ble_bash_POSIXLY_CORRECT_adjusted && $_ble_bash_POSIXLY_CORRECT_set ]]
}

## @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=
Expand Down Expand Up @@ -417,7 +443,8 @@ function ble/base/adjust-builtin-wrappers {
function ble/base/restore-builtin-wrappers {
if [[ $_ble_bash_builtins_adjusted ]]; then
_ble_bash_builtins_adjusted=
builtin eval -- "$_ble_bash_builtins_save"
ble/base/evaldef "$_ble_bash_builtins_save"
return 0
fi
}
{
Expand Down Expand Up @@ -463,9 +490,8 @@ function ble/base/xtrace/.fdnext {
((1))
}
function ble/base/xtrace/.log {
local bash=${_ble_bash:-$((BASH_VERSINFO[0]*10000+BASH_VERSINFO[1]*100+BASH_VERSINFO[2]))}
local open=---- close=----
if ((bash>=40200)); then
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"
Expand Down Expand Up @@ -550,20 +576,17 @@ function ble/base/xtrace/restore {
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
if ((_ble_bash>=40100)); then
shopt=$BASHOPTS
else
# Note: nocasematch は bash-3.1 以上
shopt=
shopt -q extdebug 2>/dev/null && shopt=$shopt:extdebug
shopt -q nocasematch 2>/dev/null && shopt=$shopt:nocasematch
fi
# 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
Expand Down Expand Up @@ -673,6 +696,7 @@ function ble/base/recover-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

Expand All @@ -682,6 +706,7 @@ function ble/base/recover-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

Expand All @@ -703,7 +728,7 @@ function ble/init/restore-IFS {
builtin unset -v _ble_init_original_IFS
}

if ((BASH_VERSINFO[0]>5||BASH_VERSINFO[0]==5&&BASH_VERSINFO[1]>=1)); then
if ((_ble_bash>=50100)); then
_ble_bash_BASH_REMATCH_level=0
_ble_bash_BASH_REMATCH=()
function ble/base/adjust-BASH_REMATCH {
Expand Down Expand Up @@ -834,6 +859,7 @@ function ble/init/clean-up {
ble/base/restore-builtin-wrappers
builtin eval -- "$_ble_bash_FUNCNEST_restore"
fi
builtin unset -v _ble_bash
return "$ext"
}

Expand Down Expand Up @@ -995,7 +1021,6 @@ function ble/base/read-blesh-arguments {
#------------------------------------------------------------------------------
# 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

Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
- keymap/vi: fix <kbd>C-w</kbd> not saving the word into kill ring `#D2208` aa7ca45d
- edit: fix standard streams in `EXIT` trap with `ble/widget/exit` `#D2217` 89f0dab8
- util(`ble/fd#cloexec`): check `fdflags` compatibility to avoid crash `#D2227` c3b3aaf8
- util(`ble/function#evaldef`): suppress alias expansions (reported by 103sbavert) `#D2240` xxxxxxxx

## Compatibility

Expand Down
2 changes: 1 addition & 1 deletion lib/core-syntax.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ function ble/syntax:bash/simple-word/eval/.cache-clear {
## @var[in,out] _ble_syntax_bash_simple_eval
## @var[in,out] _ble_syntax_bash_simple_eval_hash
function ble/syntax:bash/simple-word/eval/.cache-update {
local hash=$-:$BASHOPTS:$_ble_edit_lineno:$_ble_textarea_version:$PWD
local hash=$-:${BASHOPTS-}:$_ble_edit_lineno:$_ble_textarea_version:$PWD
if [[ $hash != "$_ble_syntax_bash_simple_eval_hash" ]]; then
_ble_syntax_bash_simple_eval_hash=$hash
ble/syntax:bash/simple-word/eval/.cache-clear
Expand Down
19 changes: 19 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7394,6 +7394,25 @@ bash_tips

2024-07-29

* util(ble/function#pop): エイリアス展開により push/pop の度に関数がどんどん長くなる (reported by 103sbavert) [#D2240]
https://github.com/akinomyoga/ble.sh/issues/471

報告者の場合には報告者が alias ='sudo -v; sudo ' なるエイリアスを設定してい
たために関数を push/pop する度に sudo の呼び出し回数が 2 倍になって、或る時
点で関数が長くなりすぎて bash がクラッシュするという状態だった。

* done: shopt を記録する為の関数を用意する → 用意した。_ble_bash はやはり
参照したいのでできるだけ始めに初期化してしまう。代わりに ble.sh の初期化
失敗の折には builtin unset で削除する。

? fixed: 類似の事は restore-builtin-wrappers でも発生するのでは?

→基本的に ble/function#evaldef と同じ実装で良い。然し
ble/function#evaldef は util.sh まで辿り着かないと定義されない。同じ内容
の関数が重複するのも嫌なので ble/base/evaldef という関数を ble.pp で定義
してそれを呼び出す事にした。ble/function#evaldef もその関数をそのまま呼び
出す。

* contrib/colorglass: 浮動小数点丸めが正しくない [#D2239]

後で他の演算についても間違いがないか確認するつもりだったが長らくそのまま放
Expand Down
33 changes: 6 additions & 27 deletions src/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2401,17 +2401,10 @@ else
fi

## @fn ble/function#evaldef def
## 関数を定義します。基本的に eval に等価ですが評価時に extglob を保
## 証します
## 関数を定義します。基本的に eval に等価ですが評価時に shopt -s extglob 及び
## shopt -u expand_aliases を保証します
function ble/function#evaldef {
local reset_extglob=
if ! shopt -q extglob; then
reset_extglob=1
shopt -s extglob
fi
builtin eval -- "$1"; local ext=$?
[[ ! $reset_extglob ]] || shopt -u extglob
return "$ext"
ble/base/evaldef "$1"
}

builtin eval -- "${_ble_util_gdict_declare//NAME/_ble_util_function_traced}"
Expand Down Expand Up @@ -3082,14 +3075,8 @@ if [[ -d /proc/$$/fd ]]; then
## @fn ble/fd#list/adjust-glob
## @var[out] set shopt gignore
function ble/fd#list/adjust-glob {
set=$- gignore=$GLOBIGNORE
if ((_ble_bash>=40100)); then
shopt=$BASHOPTS
else
shopt=
shopt -q failglob && shopt=$shopt:failglob
shopt -q dotglob && shopt=$shopt:dotglob
fi
set=$- shopt= gignore=$GLOBIGNORE
ble/base/list-shopt failglob dotglob
shopt -u failglob
set +f
GLOBIGNORE=
Expand Down Expand Up @@ -3800,15 +3787,7 @@ function ble/util/eval-pathname-expansion {
if [[ :$2: == *:canonical:* ]]; then
canon=1
local set=$- shopt gignore=$GLOBIGNORE
if ((_ble_bash>=40100)); then
shopt=$BASHOPTS
else
shopt=
shopt -q failglob && shopt=$shopt:failglob
shopt -q nullglob && shopt=$shopt:nullglob
shopt -q extglob && shopt=$shopt:extglob
shopt -q dotglob && shopt=$shopt:dotglob
fi
ble/base/list-shopt failglob nullglob extglob dotglob
shopt -u failglob
shopt -s nullglob
shopt -s extglob
Expand Down

0 comments on commit 51e762f

Please sign in to comment.