Skip to content

Commit

Permalink
global: work around bash-3.0 bug that single quotas remains for "${v-…
Browse files Browse the repository at this point in the history
…$''}"
  • Loading branch information
akinomyoga committed Feb 16, 2022
1 parent ec2a67a commit 9b96578
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -319,6 +319,7 @@
- util: add identification of Windows Terminal `wt` `#D1758` e332dc5
- complete: evaluate words for `noquote` (motivated by SuperSandro2000) `#D1767` 0a42299
- edit (TRAPDEBUG): preserve original `DEBUG` trap and enabled it in `PROMPT_COMMAND` (motivated by ammarooo) `#D1772` `#D1773` 0000000
- global: work around bash-3.0 bug that single quotas remains for `"${v-$''}"` `#D1774` 0000000

## Internal changes and fixes

Expand Down
10 changes: 8 additions & 2 deletions lib/core-syntax.sh
Expand Up @@ -490,7 +490,10 @@ function ble/syntax/print-status/nest.get-text {
if [[ $nparam == none ]]; then
nparam=
else
nparam=" nparam=${nparam//$_ble_term_FS/$'\e[7m^\\\e[m'}"
# Note #D1774: bash-3.0 bug "${var//../$'...'}" とすると $'' の引用符が残
# る問題の回避の為に行を分けて代入する。
nparam=${nparam//$_ble_term_FS/$'\e[7m^\\\e[m'}
nparam=" nparam=$nparam"
fi

nest=" nest=($nctx w=$nword n=$nnest t=$nchild:$nprev$nparam)"
Expand Down Expand Up @@ -544,7 +547,10 @@ function ble/syntax/print-status/stat.get-text {
if [[ $snparam == none ]]; then
snparam=
else
snparam=" nparam=${snparam//"$_ble_term_FS"/$'\e[7m^\\\e[m'}"
# Note #D1774: bash-3.0 bug "${var//$'...'}" とすると余分な引用符が残る問
# 題を回避する為に行を分けて代入している。
snparam=${snparam//"$_ble_term_FS"/$'\e[7m^\\\e[m'}
snparam=" nparam=$snparam"
fi

local stat_lookahead=
Expand Down
34 changes: 28 additions & 6 deletions make_command.sh
Expand Up @@ -1130,6 +1130,10 @@ function sub:scan/bash300bug {
grc '\$\{[a-zA-Z_0-9]+\[[*@]\]/' --exclude=./{text,ext} --exclude=./make_command.sh --exclude=\*.md --color |
grep -v '#D1570'

# bash-3.0 では "..${var-$'hello'}.." は (var が存在しない時) "..'hello'..." になる。
grc '".*\$\{[^{}]*\$'\''([^\\'\'']|\\.)*'\''\}.*"' --exclude={./make_command.sh,memo,\*.md} --color |
grep -v '#D1774'

}

function sub:scan/bash301bug-array-element-length {
Expand Down Expand Up @@ -1344,27 +1348,45 @@ function sub:scan {
sub:scan/memo-numbering
}

function sub:show-contrib/canonicalize {
sed 's/, /\n/g;s/ and /\n/g' | sed 's/[[:space:]]/_/g' | LANG=C sort
}
function sub:show-contrib/count {
LANG=C sort | uniq -c | LANG=C sort -rnk1 |
awk 'function xflush() {if(c!=""){printf("%4d %s\n",c,n);}} {if($1!=c){xflush();c=$1;n=$2}else{n=n", "$2;}}END{xflush()}' |
ifold -w 131 -s --indent=' +[0-9] +'
}
function sub:show-contrib {
local cache_contrib_github=out/contrib-github.txt
if [[ ! ( $cache_contrib_github -nt .git/refs/remotes/origin/master ) ]]; then
{
wget 'https://api.github.com/repos/akinomyoga/ble.sh/issues?state=all&per_page=100&pulls=true' -O -
wget 'https://api.github.com/repos/akinomyoga/ble.sh/issues?state=all&per_page=100&pulls=true&page=2' -O -
wget 'https://api.github.com/repos/akinomyoga/blesh-contrib/issues?state=all&per_page=100&pulls=true' -O -
} |
sed -n 's/^[[:space:]]*"login": "\(.*\)",$/\1/p' |
sort | uniq -c | sort -rn > "$cache_contrib_github"
sub:show-contrib/canonicalize > "$cache_contrib_github"
fi

echo "Contributions (from GitHub Issues/PRs)"
cat "$cache_contrib_github"
< "$cache_contrib_github" sub:show-contrib/count

echo "Contributions (from memo.txt)"
sed -En 's/^ \* .*\([^()]+ by ([^()]+)\).*/\1/p' memo/done.txt note.txt |
sort | uniq -c | sort -rn
sed -En 's/^ \* .*\([^()]+ by ([^()]+)\).*/\1/p' memo/done.txt note.txt | sub:show-contrib/canonicalize | sub:show-contrib/count

echo "Contributions (from ChangeLog.md)"
sed -n 's/.*([^()]* by \([^()]*\)).*/\1/p' memo/ChangeLog.md |
sort | uniq -c | sort -rn
sed -n 's/.*([^()]* by \([^()]*\)).*/\1/p' docs/ChangeLog.md | sub:show-contrib/canonicalize | sub:show-contrib/count

echo "Σ: Issues/PRs + max(memo.txt,ChangeLog)"

LANG=C join -j 2 -e 0 \
<(sed -En 's/^ \* .*\([^()]+ by ([^()]+)\).*/\1/p' memo/done.txt note.txt | sub:show-contrib/canonicalize | uniq -c | LANG=C sort -k2) \
<(sed -n 's/.*([^()]* by \([^()]*\)).*/\1/p' docs/ChangeLog.md | sub:show-contrib/canonicalize | uniq -c | LANG=C sort -k2) |
LANG=C join -e 0 -1 1 - -2 2 <(uniq -c "$cache_contrib_github" | LANG=C sort -k2) |
awk 'function max(x,y){return x<y?y:x;}{printf("%4d %s\n",max($2,$3)+$4,$1)}' |
sort -rnk1 |
awk 'function xflush() {if(c!=""){printf("%4d %s\n",c,n);}} {if($1!=c){xflush();c=$1;n=$2}else{n=n", "$2;}}END{xflush()}' |
ifold -w 131 -s --indent=' +[0-9] +'
echo
}

Expand Down
42 changes: 40 additions & 2 deletions note.txt
Expand Up @@ -504,6 +504,12 @@ bashbug: 実装上で注意するべき事・バグ
| A="\
| "

- "${scalar[@]/xxxx}" は全て空になる。変数名が配列である事が保証されている
必要がある。

- "${...#$'...'}" という形を使うと $'...' の展開結果が ... ではなく '...'
の様に、余分な引用符が入ってしまう。extquote を設定しても結果は変わらない。

* msys1, msys2: var='^M' とすると CR が消えてなくなる。
msys2 では var=$'\r' とすれば大丈夫。また変数に入っている物も大丈夫。
例えば var=$_ble_term_CR はOKである。
Expand Down Expand Up @@ -1772,8 +1778,6 @@ bash_tips
* ble/builtin/trap DEBUG は関数の入れ子や trace 属性などは考慮していない。こ
の事によって問題が起こる可能性もある。

* bash-3.0 でコマンドを実行するとプロンプトの前に変な文字列 '' が出力される

* DEBUG trap 以外についても元からあった trap を拾う様にしたい
Ref #D1772

Expand Down Expand Up @@ -6112,6 +6116,40 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2022-02-17

* bash-3.0 でコマンドを実行するとプロンプトの前に変な文字列 '' が出力される [#D1774]

うーん。家に帰ったら再現しなくなった。と思ったら再現の仕方が変化した。C-c
でモードを変更すると '''' という無駄な文字列が表示される。これは一体何処か
ら出てきている物だろうか。

ようやく突き止めた。_ble_term_Ss が余分な引用符で囲まれている。然し何故だろ
うか。調べると cache には含まれていない。だとすれば、blerc かもしくは内部の
端末判定である。

belrc を読み込まない様にしたら表示はされなくなったが、それは単にカーソルの
設定をしていないのでカーソル切り替えシーケンスが出力されていないだけだった。
実際に _ble_term_Ss の値を確認したら引用符が混入していた。という事は端末判
定直後の代入の際に引用符が混入している事になる。

分かった。 bash-3.0 には "${...#$'...'}" が '...' に展開されてしまうバグが
ある様だ。これはチェックの対称に追加する事にする。

? そもそも他の "${pat#"..."}" 等も大丈夫なのだろうか? 動作を確認する必要が
ある。以下は一応ちゃんと期待通りに動作する様である。

$ v=1
$ echo ${v//"1"/hello}
$ echo "${v//"1"/hello}"
$ echo "<${v#"1"}>"
$ echo "<${v#'1'}>"
$ echo "<${v#1}>"
$ echo "<${v#"1"}>"
$ echo "<${v/#"1"/hello}>"

そんなに気にしなくて良い気がしてきた。

2022-02-15

* 2022-01-23 [解消] 元々存在した DEBUG trap を保持する [#D1773]
Expand Down
2 changes: 1 addition & 1 deletion src/decode.sh
Expand Up @@ -3768,7 +3768,7 @@ function ble/builtin/bind/read-user-settings/.collect {
ble/util/print __CLEAR__
ble/util/print KEYMAP="$map"
ble/util/print __BIND0__
ble/util/print "${cache_content%$'\n'}"
ble/util/print "${cache_content%$_ble_term_nl}"
if ((_ble_bash>=40300)); then
ble/util/print __BINDX__
builtin bind -m "$map" -X
Expand Down
6 changes: 4 additions & 2 deletions src/util.sh
Expand Up @@ -2683,7 +2683,7 @@ else
local _ble_local_ret=$? TMOUT= 2>/dev/null # #D1630 WA readonly TMOUT
IFS= builtin read "${_ble_bash_tmout_wa[@]}" -r -d '' "$1" < "$_ble_local_tmpfile"
ble/util/assign/.rmtmp
builtin eval "$1=\${$1%$'\n'}"
builtin eval "$1=\${$1%$_ble_term_nl}"
return "$_ble_local_ret"
}
fi
Expand Down Expand Up @@ -5956,7 +5956,9 @@ function ble/term/DA2/notify {
ble/term/quote-passthrough $'\e[>c' $((depth+1))
ble/util/buffer "$ret" ;;
(contra:*)
: "${_ble_term_Ss:=$'\e[@1 q'}" ;;
if [[ ! ${_ble_term_Ss-} ]]; then
_ble_term_Ss=$'\e[@1 q'
fi ;;
esac

# 外側の端末情報は以降では処理しない
Expand Down

0 comments on commit 9b96578

Please sign in to comment.