Skip to content

Commit

Permalink
syntax: fix a bug that "eval() { :; }" is not parsed as a function de…
Browse files Browse the repository at this point in the history
…finition
  • Loading branch information
akinomyoga committed May 6, 2021
1 parent c70a3b4 commit b429095
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
15 changes: 9 additions & 6 deletions lib/core-syntax.sh
Expand Up @@ -3510,12 +3510,6 @@ function ble/syntax:bash/ctx-command/check-word-end {
fi ;;
('then'|'elif'|'else'|'do') ((ctx=CTX_CMDX1)) ; processed=middle ;;
('}'|'done'|'fi'|'esac') ((ctx=CTX_CMDXE)) ; processed=end ;;
('declare'|'readonly'|'typeset'|'local'|'export'|'alias')
((ctx=CTX_ARGVX))
processed=builtin ;;
('eval')
((ctx=CTX_ARGEX))
processed=builtin ;;
('coproc')
if ((_ble_bash>=40000)); then
if ble/syntax:bash/ctx-coproc/.is-next-compound; then
Expand Down Expand Up @@ -3601,6 +3595,15 @@ function ble/syntax:bash/ctx-command/check-word-end {
((_ble_syntax_attr[i]=CTX_ARGX,i+=${#rematch1}))
fi
fi

# 引数の取り扱いが特別な builtin
case $word_expanded in
('declare'|'readonly'|'typeset'|'local'|'export'|'alias')
((ctx=CTX_ARGVX)) ;;
('eval')
((ctx=CTX_ARGEX)) ;;
esac

return 0
fi

Expand Down
1 change: 1 addition & 0 deletions memo/ChangeLog.md
Expand Up @@ -80,6 +80,7 @@
- main: fix a bug that unset `IFS` is not correctly restored `#D1489` 808f6f7
- edit: fix error messages on accessing undo records in emacs mode (reported by rux616) `#D1497` 61a57c0 e9be69e
- canvas: fix a glitch that SGR at the end of command line is applied to new lines `#D1498` 4bdfdbf
- syntax: fix a bug that `eval() { :; }`, `declare() { :; }` are not treated as function definition `#D1529` 0000000

## Compatibility

Expand Down
21 changes: 21 additions & 0 deletions memo/D1529.test-keyword-funcdef.sh
@@ -0,0 +1,21 @@
#!/bin/bash

keywords=('[[' 'time' '!' 'if' 'while' 'until' 'for' 'select' 'case'
'{' 'then' 'elif' 'else' 'do' '}' 'done' 'fi' 'esac'
'coproc' 'function')
builtins=('declare' 'readonly' 'typeset' 'local' 'export' 'alias'
'eval')

for word in "${keywords[@]}" "${builtins[@]}"; do
if (eval "$word () { :; }" 2>/dev/null); then
ok+=("$word")
else
ng+=("$word")
fi
done

echo "Can we define a function using 'NAME() { ... }' form with the NAME being keywords/builtins?"
echo "yes: ${ok[*]}"
echo "no: ${ng[*]}"

# 結論: builtin はできる。keyword はできない。当たり前といえば当たり前の結果である。
31 changes: 23 additions & 8 deletions note.txt
Expand Up @@ -1275,24 +1275,26 @@ bash_tips
* robustness: ble.sh では exit を上書きしているが set -o posix の時にはそれが
無効になる。

* akinomyoga.dotfiles h: 複数行コマンドを実行した後に出力内容がずれている。
ble がロードされている時は ble の内容を参照した方が良いかもしれない。

* mapfile -d "" が buffered になったら
ble/syntax:bash/simple-word/eval/.print-result (lib/core-syntax.sh) を修正

* util: builtins 復元、function#advice, etc. において functrace 属性は復元し
なくて良いのだろうか。復元という事を考えるとやはり declare -pf を使う必要が
あるのかもしれない。

bash version, posix mode も含めてどういう方法があってそれぞれどの様に振る舞
うのか調べる必要がある。

取り敢えず問題が起こらない事を確かめた上で getdef 自体を更新するのが良いの
ではないか。

2021-05-03

* rlfunc: C-x s spell-correct-word
* rlvar: enable-active-region
* rlbind: prior, next
* vi-undo

2021-04-30

* syntax: eval() { echo yes; } が構文エラーになっている。
local, declare も同じ。

2021-04-29

* robustness: main/init: readonly POSIXLY_CORRECT されていたらどうするのか。
Expand Down Expand Up @@ -1381,6 +1383,12 @@ bash_tips

* prompt を評価する時に $var が local 変数に被覆されている。

然し $var だけならば良いが $() で呼び出された関数が更に内部で変数を使用して
いる可能性等を考えると下手に変な調整はしない方が良いかもしれない。PS1 に直
接 $var と記述するかしないかで $() の内部でも変数が見えるかどうかが切り替わ
るというのは不自然すぎる。それならば ble.sh 自体が変数を定義するのでそれに
よって被覆されてしまうと説明した方が自然である。

2021-04-28

* keymap 初期化に失敗した時にそれを検出しているのにも拘らず操作不能になる
Expand Down Expand Up @@ -4288,6 +4296,13 @@ bash_tips

2021-05-05

* syntax: eval() { echo yes; } が構文エラーになっている [#D1529]
local, declare も同じ。

これは意外と簡単だった。処理順序を変えるだけで良かった。builtin の処理と
keyword の処理が結合しているから切り離すのに時間がかかるのではないかと思っ
たが、実際には builtin の処理は独立にしても問題ない様な実装になっていた。

* main: freeze-utility-path が一時実装を置き換えなくなっている [#D1528]

ble/bin/sleep を初期化する前に sleep の機能についてテストしている気がする?
Expand Down

0 comments on commit b429095

Please sign in to comment.