Skip to content

Commit

Permalink
main: fix the workaround for "set -eu" and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 22, 2022
1 parent 8dbefe0 commit 6a946f0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 37 deletions.
79 changes: 54 additions & 25 deletions ble.pp
Expand Up @@ -172,15 +172,22 @@ function ble/debug/measure-set-timeformat {
# 対策 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=$FUNCNEST FUNCNEST=
_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=
FUNCNEST=$_ble_bash_FUNCNEST
if [[ $_ble_bash_FUNCNEST_set ]]; then
FUNCNEST=$_ble_bash_FUNCNEST
else
builtin unset -v FUNCNEST
fi
fi 2>/dev/null'
\builtin eval -- "$_ble_bash_FUNCNEST_adjust"

Expand Down Expand Up @@ -226,10 +233,12 @@ function ble/base/is-POSIXLY_CORRECT {
fi
}

_ble_bash_builtins_adjusted=
_ble_bash_builtins_save=
{
_ble_bash_builtins_adjusted=
_ble_bash_builtins_save=
} 2>/dev/null # set -x 対策
function ble/base/adjust-builtin-wrappers/.assign {
if [[ $_ble_util_assign_base ]]; then
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"
Expand Down Expand Up @@ -304,23 +313,47 @@ function ble/variable#copy-state {
fi
}

: "${_ble_bash_options_adjusted=}"
function ble/base/.adjust-bash-options {
builtin eval -- "$1=\$-"
set +exvuk -B

[[ $2 == shopt ]] || local shopt
if ((_ble_bash>=40100)); then
shopt=$BASHOPTS
else
shopt=
# Note: nocasematch は bash-3.1 以上
shopt -q nocasematch 2>/dev/null && shopt=nocasematch
fi
[[ $2 == shopt ]] || builtin eval -- "$2=\$shopt"
shopt -u nocasematch 2>/dev/null
return 0
fi
} 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
[[ $set == *B* ]] || set +B
[[ $set == *k* ]] && set -k
[[ $set == *u* ]] && set -u
[[ $set == *v* ]] && set -v
[[ $set == *x* ]] && set -x
[[ $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

# 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
_ble_bash_setk=; [[ -o keyword ]] && _ble_bash_setk=1 && set +k
_ble_bash_setB=; [[ -o braceexpand ]] && _ble_bash_setB=1 || set -B

# Note: nocasematch は bash-3.0 以上
_ble_bash_nocasematch=
shopt -q nocasematch 2>/dev/null &&
_ble_bash_nocasematch=1 && shopt -u nocasematch
ble/base/.adjust-bash-options _ble_bash_set _ble_bash_shopt

# Note: expand_aliases はユーザー設定を復元する為に記録する
_ble_bash_expand_aliases=
Expand Down Expand Up @@ -368,12 +401,8 @@ function ble/base/restore-bash-options {
ble/variable#copy-state _ble_bash_LC_ALL LC_ALL

[[ $_ble_bash_nocasematch ]] && shopt -s nocasematch
[[ ! $_ble_bash_setB && -o braceexpand ]] && set +B
[[ $_ble_bash_setk && ! -o keyword ]] && set -k
[[ $_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 は最後

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
Expand Down Expand Up @@ -1529,7 +1558,7 @@ function ble/base/attach-from-PROMPT_COMMAND {
{
if (($#==0)); then
ble/array#replace PROMPT_COMMAND ble/base/attach-from-PROMPT_COMMAND
blehook PRECMD-=ble/base/attach-from-PROMPT_COMMAND
blehook PRECMD-=ble/base/attach-from-PROMPT_COMMAND || ((1)) # set -e 対策
else
local save_index=$1 lambda=$2

Expand Down
17 changes: 9 additions & 8 deletions docs/ChangeLog.md
Expand Up @@ -134,7 +134,7 @@
- complete (`source:command`): not quote aliases and keywords `#D1715` d6242a7
- highlight (`wtype=CTX_CMDI`): check alias names before shell expansions `#D1715` d6242a7
- util (`ble/is-alias`): fix a bug of unredirected error messages for bash-3.2 (fixup d6242a7) `#D1730` 31372cb
- edit (`history_share`): update history on `discard-line` (reported by SuperSandro2000) `#D1742` 0000000
- edit (`history_share`): update history on `discard-line` (reported by SuperSandro2000) `#D1742` 8dbefe0

## Fixes

Expand Down Expand Up @@ -187,9 +187,10 @@
- main: work around `. ble.sh --{test,update,clear-cache}` in intereactive sessions `#D1555` bbc2a90
- Makefile: create `run` directory instead of `tmp` `#D1557` 9bdb37d
- main: fix the workaround for `set -e` `#D1564` ab2f70b
- main: fix the workaround for `set -u` `#D1575` 76073a9
- main: fix the workaround for `set -eu` and refactor `#D1743` 0000000
- util: work around bash-3.0 bug `"${scal[@]/xxx}"` `#D1570` 24f79da
- sabbrev (`ble-sabbrev`): fix delayed output before the initialization `#D1573` 5d85238
- main: fix the workaround for `set -u` `#D1575` 76073a9
- history: fix the workaround for bash-3.0 bug of reducing histories `#D1576` 15c9133
- syntax: fix a bug that argument completion is attempted in nested commands (reported by huresche) `#D1579` 301d40f
- edit (brackated-paste): fix incomplete `CR => LF` conversion (reported by alborotogarcia) `#D1587` 8d6da16
Expand Down Expand Up @@ -221,9 +222,9 @@
- util (visible-bell): erase visible-bell before running external commands `#D1723` 0da0c1c
- util (`ble/function`): work around `shopt -u extglob` `#D1725` 952c388
- syntax: fix uninitialized syntax-highlighting in bash-3.2 `#D1731` e3f5bf7
- make: fix a bug that config update messages are removed on install `#D1736` 0000000
- util: fix bugs in conversions from `'` to `\''` `#D1739` 0000000
- canvas: fix unupdated prompt on async wcwidth resolution `#D1740` 0000000
- make: fix a bug that config update messages are removed on install `#D1736` 72d968f
- util: fix bugs in conversions from `'` to `\''` `#D1739` 6d15782
- canvas: fix unupdated prompt on async wcwidth resolution `#D1740` e14fa5d

## Documentation

Expand All @@ -250,7 +251,7 @@
- history: use `mapfile -d ''` to load history in Bash 5.2 `#D1603` 72c274e
- prompt: use `${PS1@P}` when the prompt contains only safe prompt sequences `#D1617` 8b5da08
- prompt: fix not properly set `$?` in `${PS1@P}` evaluation (reported by nihilismus) `#D1644` 521aff9
- prompt: fix a bug that the special treatment of `\$` in Cygwin/MSYS is disabled `#D1741` 0000000
- prompt: fix a bug that the special treatment of `\$` in Cygwin/MSYS is disabled `#D1741` 4782a33
- decode: cache `inputrc` translations `#D1652` 994e2a5
- complete: use `awk` for batch `quote-insert` (motivated by banoris) `#D1714` a0b2ad2 92d9734
- complete (quote-insert.batch): fix regex escaping in bracket expr of awk (reported by telometto) `#D1729` 8039b77
Expand Down Expand Up @@ -289,7 +290,7 @@
- main (`ble/util/readlink`): work around non-standard or missing `readlink` (motivated by peterzky) `#D1720` a41279e
- menu (`menu-style:desc`): work around xenl quirks for relative cursor movements (reported by telometto) `#D1728` 3e136a6
- global: work around the arithmetic syntax error of `10#` in Bash-5.1 `#D1734` 7545ea3
- global: adjust implementations for Bash 5.2 `patsub_replacement` `#D1738` 0000000
- global: adjust implementations for Bash 5.2 `patsub_replacement` `#D1738` 4590997

## Internal changes and fixes

Expand Down Expand Up @@ -319,7 +320,7 @@
- edit (`command-help`): use `ble/util/assign/.mktmp` to determine the temporary filename `#D1663` 1af0800
- make: update lint check `#D1709` 7e26dcd
- test: save the test log to a file `#D1735` d8e6ea7
- benchmark: improve determination of the base time `#D1737` 0000000
- benchmark: improve determination of the base time `#D1737` ad866c1

## Contrib

Expand Down
2 changes: 1 addition & 1 deletion lib/core-syntax.sh
Expand Up @@ -1874,7 +1874,7 @@ function ble/syntax/highlight/vartype {
fi
else
# set -u のチェック
if [[ :$opts: == *:readvar:* && $_ble_bash_setu ]]; then
if [[ :$opts: == *:readvar:* && $_ble_bash_set == *u* ]]; then
if [[ ! $tail ]] || {
local rex='^:?[-+?=]'
[[ $tail == :* ]] && lookahead=2
Expand Down
15 changes: 12 additions & 3 deletions src/util.sh
Expand Up @@ -1787,6 +1787,9 @@ function blehook/.read-arguments {
}

function blehook {
local set shopt
ble/base/.adjust-bash-options set shopt

local flags print process
local rex1='^([a-zA-Z_][a-zA-Z_0-9]*)$'
local rex2='^([a-zA-Z_][a-zA-Z_0-9]*)(:?-?\+?=)(.*)$'
Expand All @@ -1797,7 +1800,9 @@ function blehook {
ble/util/print >&2
blehook/.print-help
fi
[[ $flags != *E* ]]; return "$?"
[[ $flags != *E* ]]; local ext=$?
ble/base/.restore-bash-options set shopt
return "$ext"
fi

if ((${#print[@]}==0&&${#process[@]}==0)); then
Expand Down Expand Up @@ -1829,6 +1834,7 @@ function blehook {
blehook/.print "${print[@]}"
fi

ble/base/.restore-bash-options set shopt
return "$ext"
}
blehook/.compatibility-ble-0.3
Expand Down Expand Up @@ -2060,6 +2066,7 @@ function ble/builtin/trap/.invoke {
return "$_ble_trap_ext"
}
function ble/builtin/trap/.handler {
local set shopt; ble/base/.adjust-bash-options set shopt
local _ble_trap_ext=$? _ble_trap_sig=$1 _ble_trap_name=$2

# ble.sh hook
Expand All @@ -2069,9 +2076,9 @@ function ble/builtin/trap/.handler {
ble/util/joblist.check ignore-volatile-jobs

# user hook
local _ble_trap_handler=${_ble_builtin_trap_handlers[_ble_trap_sig]}
local _ble_trap_handler=${_ble_builtin_trap_handlers[_ble_trap_sig]-}
local _ble_trap_done=
ble/builtin/trap/.invoke
ble/builtin/trap/.invoke # Note: 空コマンドでも実行する
_ble_trap_ext=$?
case $_ble_trap_done in
(break)
Expand All @@ -2083,6 +2090,8 @@ function ble/builtin/trap/.handler {
(*)
_ble_builtin_trap_hook="return $_ble_trap_ext" ;;
esac

ble/base/.restore-bash-options set shopt
}

function ble/builtin/trap/install-hook {
Expand Down

0 comments on commit 6a946f0

Please sign in to comment.