Skip to content

Commit

Permalink
util (ble/util/import): work around filenames with bash special chara…
Browse files Browse the repository at this point in the history
…cters
  • Loading branch information
akinomyoga committed Feb 13, 2022
1 parent 5065fda commit b27f758
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 20 deletions.
11 changes: 6 additions & 5 deletions docs/ChangeLog.md
Expand Up @@ -141,7 +141,7 @@
- canvas: do not insert explicit newlines on line folding if possible (reported by banoris) `#D1745` 02b9da6 dc3827b
- edit (`ble-bind -x`): preserve multiline prompts on execution of `bind -x` commands (requested by SuperSandro2000) `#D1755` 7d05a28
- util (`ble/util/buffer`): hide cursor in rendering `#D1758` e332dc5
- complete (`action:file`): always suffix `/` to complete symlinked directory names (reported by SuperSandro2000) `#D1759` 0000000
- complete (`action:file`): always suffix `/` to complete symlinked directory names (reported by SuperSandro2000) `#D1759` 397ac1f

## Fixes

Expand All @@ -163,7 +163,7 @@
- mandb: fix small issues of man-page analysis `#D1708` caa77bc
- mandb: insert a comma in brace expansions instead of a space `#D1719` 0ac7f03
- mandb: support man-page format of `rsync` `#D1733` 7900144
- mandb: fix a bug that the description is inserted for `--no-OPTION` `#D1761` 0000000
- mandb: fix a bug that the description is inserted for `--no-OPTION` `#D1761` 88614b8
- edit: work around the wrong job information of Bash in trap handlers (reported by 3ximus) `#D1435` `#D1436` bc4735e
- edit (command-help): work around the Bash bug that tempenv vanishes with `builtin eval` `#D1438` 8379d4a
- global: suppress missing locale errors (reported by 3ximus) `#D1440` 4d3c595
Expand Down Expand Up @@ -233,7 +233,9 @@
- 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
- progcomp: retry completions on `$? == 124` also for non-default completions (reported by SuperSandro2000) `#D1759` 0000000
- progcomp: retry completions on `$? == 124` also for non-default completions (reported by SuperSandro2000) `#D1759` 82b9c01
- app: work around data corruption by WINCH on intermediate state `#D1762` 5065fda
- util (`ble/util/import`): work around filenames with bash special characters `#D1763` 0000000

## Documentation

Expand Down Expand Up @@ -310,7 +312,7 @@
- main: check `IN_NIX_SHELL` to inactivate ble.sh in nix-shell (suggested by SuperSandro2000) `#D1747` b4bd955
- canvas: test the terminal for the sequence of clearing `DECSTBM` `#D1748` 4b1601d
- main: check `/dev/tty` on startup (reported by andychu) `#D1749` 711c69f
- util: add identification of Windows Terminal `wt` `#D1758` 0000000
- util: add identification of Windows Terminal `wt` `#D1758` e332dc5

## Internal changes and fixes

Expand Down Expand Up @@ -503,7 +505,6 @@
- complete: fix a problem of redundant unmatched ambiguous part with tilde expansions in the common prefix `#D1417` 5777d7f
- highlight: fix remaininig highlighting of vanishing words `#D1421` `#D1422` 1066653
- complete: fix a problem that the user setting `dotglob` is changed `#D1425` 987436d
- app: work around data corruption by WINCH on intermediate state `#D1762` 0000000

## Compatibility

Expand Down
152 changes: 146 additions & 6 deletions note.txt
Expand Up @@ -1314,6 +1314,8 @@ bash_tips

2022-01-23

* wezterm, bash-preexec に対する patch を終結させる。

* bash-preexec は既存の DEBUG trap を正しく実行できていない様に思われる

$ trap 'echo XXX' DEBUG
Expand Down Expand Up @@ -1750,10 +1752,57 @@ bash_tips
ToDo
-------------------------------------------------------------------------------

2022-01-31
2022-02-05

* nix-build -A の補完が効かないという問題
https://github.com/akinomyoga/ble.sh/issues/171#issue-1113799687
* syntax: a=(%{1..10}) の % を '\x' に書き換える時文法構造が破壊 (?)

$ bash
$ rep=(%{25,08,0A,2{0..2},24,2{6..9},3B,3C,3E,5C,5E,60,7C})

として起動した直後に上記の文字列を貼り付けて、それから先頭の % を削除して
'\x' と入力したら文法構造が壊れてしまった。然し二度と再現しない。問題は '\x
迄入力した時点で発生していた。もしかすると ' か \ を入力した時点で既におか
しくなっていたのかもしれない。

2022-02-03

* キャッシュの判定でファイルを使って判定しているが、よく考えたら現在ロードさ
れているコードとファイルに含まれているコードが同じとは限らないのではないか。
もし、現在のシェルの起動時刻よりも比較に使っているファイルのほうが新しい時
は、ble.sh 自体を reload した方が良いのではないか。と思ったがそれによって失
われる設定も考えられる。という事から勝手に reload する訳にも行かない。

起動時刻(というよりble.shロード時刻)を示す一時ファイルを使うという手も考え
られるが、それだと後になってロードした module の場合には不正確になる。と思っ
たが、古いコードを使って更新を実行してしまうよりは、新しいコードなのに古い
情報を更新せずに利用するという位の方が未だましである。

実はロード時刻は $_ble_base_run/$$.load に既に存在しているのでそれを使えば
良い。

* complete: 補完している時に勝手に変数の中身が展開されてしまう事がある。展開
する条件をより制限する事はできないか。或いは、展開せずに置換できるパターン
を増やす。

最たる場合が echo $_ble_base_cache/mandb[TAB] である。曖昧一致によって遡っ
て置換が起こるが、それによって変数展開の中身まで展開されてしまう。少なくと
も / で区切ってできるだけ展開せずに一致する様にできないだろうか。

? 曖昧一致に於いて変数展開から得られた文字列は塊で取り扱う?

というかよく考えたら変数の中身に対してまで曖昧一致で一致させるのは変な気
がする。曖昧一致は元の原始的な構成要素に対しては塊で一致する様にするべき
なのではないか。と思ったが 'aaa' の中身の様にユーザーが手で入力した内容に
ついてはやはり曖昧一致であって欲しい。という事を考えると曖昧一致で塊とす
るとしてもその対象は変数展開だけに限られてくるのではないか。

* もしかしたら stty sane の頭に空白を置くと良いのでは。先頭に空白を置いたコマ
ンドの履歴登録をなしにする設定にしている人がいる。

或いは勝手に stty sane を HISTIGNORE に設定するという手もある? と思ったが、
それだとユーザーが本当に自分で stty sane を実行した時に履歴に残らないし、或
いは HISTIGNORE を一旦保存して復元する仕組みを作ったとしても何かの拍子に実
行できなかった時にやはり困るし、余り信頼性が高いとは言えない。

2022-01-23

Expand Down Expand Up @@ -1860,9 +1909,6 @@ bash_tips
* contrib/git: dirty で rps1 が更新された瞬間にカーソル位置がずれた。これは後
で調べる必要がある。

* ble/util/import でファイル名に関数名として許されない文字が入っていた時の対
策が必要では。

* deprecated functions の枠組みを整える。

散発的に deprecate して行くと毎回設定を変更しなければならず面倒なので、
Expand All @@ -1871,6 +1917,27 @@ bash_tips

((_ble_version>=500)) && ..... と言った具合。

? reject: deprecation は version ではなくて独立な番号付を使う可能性?

→omb において考えていたが、これは特定のバージョンを予め指定するよりは
deprecation level の様な 1 個ずつ増える整数にするのが良い。というのも、そ
もそもどの version で完全廃止するのかというのは廃止を決定した瞬間には分か
らないという事。そして、実際に廃止する瞬間には基本的には全て一度に廃止す
るのであって、機能によって廃止したり廃止しなかったりという事はない。なの
で基本的には、新しく廃止をマークする時には次の deprecation level を指定し
ておいて、deprecation level が上がる時に一斉に廃止するのが良いと思われる。

一方で、もしかすると緊急で廃止したいという状況が現れるかもしれないので、
deprecation level は基本的には 100 の倍数にして置いて、細かい廃止を実行し
たい時に限って 1 ずつ上げる様にするというのも手である。と思ったがその様に
考えると結局それは _ble_version と同じになるのではないかという気もする。
但し、基本的に廃止等は master でしか起こらない気がするので patch level と
deprecation level に関してはずれが生じるだろう。

また minor version とは無関係に廃止するという事があるのかも分からない。と
いう事を考えるとやはりその意味に於いても ble.sh に於いては deprecation
level と version は一致させて良いのではないか。という気がしてきた。

* complete: FIGNORE と -o filenames

どうやら元の bash では -o filenames が指定された時にのみ FIGNORE が使われる
Expand Down Expand Up @@ -1931,6 +1998,9 @@ bash_tips
というかオプション名も含む事ができる? (declare -F -f とした時の解釈はどちら
になるのだろうか?)

* 変数代入の形式をしていない引数に関しては中身によってOKかもしれないし駄目
かもしれない。

* refactor: highlight-variable というインターフェイスを作る。

と、思ったが quote 等も考えるとそういった関数を提供する事に意味があるのか
Expand Down Expand Up @@ -5956,6 +6026,76 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2022-02-05

* [棄却] 2022-01-31 nix-build -A の補完が効かないという問題 [#D1764]
https://github.com/akinomyoga/ble.sh/issues/171#issue-1113799687

取り敢えず nix は入れたが nix コマンドや nix-build コマンドは見つかるのだろ
うか。また nix-shell コマンドも使えるのか気になる所である。

動かない。そもそも通常の bash で動くのだろうか。実際に単に source するとエ
ラーが出る。bash-completion を一緒にロードしないといけない様だ。然し結局通
常の bash で実行しても nix-build -A [TAB] で補完がされる事はなかった。

$ bash --norc
$ source ~/.mwg/git/scop/bash-completion/bash_completion
$ source _nix

一体どの様にするのが良いのだろうか。

→これは結局よく分からないが今は動いている様である。手元でもちゃんと動いて
いる事を確認した。向こうの勘違いか或いは他の問題と関係していたかという事だ
ろう。

* 2021-12-18 ble/util/import でファイル名に関数名として許されない文字が入っていた時の対策が必要では [#D1763]

関数名として許されない文字は実はそんなに多くはない気がする。Unicode 文字の
殆どは関数名に使える。特殊な意味を持つのは ASCII の範囲内に限られるからであ
る。

$' \t\n;|&<>()$`"\'\\'

履歴展開文字 !^ に関しては一時的に履歴展開を無効にして関数を定義すれば行け
る気もしないでもないが面倒なので考えない事にする。

特定の文字 (例えば _) に一律に置き換えてしまって衝突は已む無しとするという
事を考えたが、実は % (url) encoding みたいな事をすれば良いのではないかとい
う気がする。但しその為には沢山置換を実行しなければならない。新しい
strreplace は使えない。置換前の文字を直接使える訳ではないので。

| a 直接ハードコードする
|
| a=' ' b=%20
| a=$'\t' b=%08
| a=$'\n' b=%0A
| ...
|
| b 次の様に pack してループを回す。然し何だか変な気がする。
|
| '%25' ' 20' $'\t08' $'\n0A' ';3B' '&26' '|7C' '<3C' '>3E' '(28' ')29' '$24' '`60' '"22' \'27 \\5C
|
| c それよりは個別にした方がすっきりするのではないか。でも対応が分かりにくい。
|
| $'% \t\n;|&<>()$`"\'\\'
| 25,20,08,0A,3B,26,7C,3C,3E,28,29,24,60,22,27,5C
|
| 然しこれならファイル名にこれらの文字が含まれていないかどうかの確認も簡単
| である。改めて文字コード順に並べる事にする。
|
| chars=%$'\t\n !"$&\'();<>\\^`|'
| rep=(%{25,08,0A,20,21,22,24,26,27,28,29,3B,3C,3E,5C,5E,60,7C})
| rep=(%{25,08,0A,2{0..2},24,2{6..9},3B,3C,3E,5C,5E,60,7C})
|
| d 或いは次の様にする。
|
| '%':%25 $'\t':%08 $'\n':%0A ' ':%20 '!':%21 '"':%22 '$':%24 '&':%26
| \':%27 '(':%28 ')':%29 ';':%3B '<':%3C '>':%3E \\:%5C '^':%5E '`':%60
| '|':%7C

c の方針で考える事にした。実装した。取り敢えず空白を含むファイル名で試した
ら動いているのでOK.

2022-02-02

* render: 処理途中に WINCH が来ると途中状態のデータに対して処理が走ってデータが壊れる [#D1762]
Expand Down
34 changes: 25 additions & 9 deletions src/util.sh
Expand Up @@ -4560,19 +4560,33 @@ function ble/util/import/search {
fi
[[ -e $ret && ! -d $ret ]]
}
function ble/util/import/encode-filename {
ret=$1
local chars=%$'\t\n !"$&\'();<>\\^`|' # <emacs bug `>
if [[ $ret == *["$chars"]* ]]; then
local i n=${#chars} reps a b
reps=(%{25,08,0A,2{0..2},24,2{6..9},3B,3C,3E,5C,5E,60,7C})
for ((i=0;i<n;i++)); do
a=${chars:i:1} b=${reps[i]} ret=${ret//"$a"/"$b"}
done
fi
return 0
}
function ble/util/import/is-loaded {
local ret
ble/util/import/search "$1" &&
ble/is-function "ble/util/import/guard:$ret"
ble/util/import/encode-filename "$ret" &&
ble/is-function ble/util/import/guard:"$ret"
}
# called by ble/base/unload (ble.pp)
function ble/util/import/finalize {
local file
local file ret
for file in "${_ble_util_import_files[@]}"; do
local guard=ble/util/import/guard:$file
ble/util/import/encode-filename "$file"; local enc=$ret
local guard=ble/util/import/guard:$enc
builtin unset -f "$guard"

local onload=ble/util/import/onload:$file
local onload=ble/util/import/onload:$enc
if ble/is-function "$onload"; then
"$onload" ble/util/unlocal
builtin unset -f "$onload"
Expand Down Expand Up @@ -4642,16 +4656,17 @@ function ble/util/import/.read-arguments {
return 0
}
function ble/util/import {
local file ext=0
local file ext=0 ret enc
for file; do
local guard=ble/util/import/guard:$file
ble/util/import/encode-filename "$file"; enc=$ret
local guard=ble/util/import/guard:$enc
ble/is-function "$guard" && return 0
[[ -e $file ]] || return 1
source "$file" || { ext=$?; continue; }
builtin eval "function $guard { :; }"
ble/array#push _ble_util_import_files "$file"

local onload=ble/util/import/onload:$file
local onload=ble/util/import/onload:$enc
ble/function#try "$onload" ble/util/invoke-hook
done
return "$ext"
Expand Down Expand Up @@ -4691,11 +4706,12 @@ function ble/util/import/eval-after-load {
return 2
fi; file=$ret

local guard=ble/util/import/guard:$file
ble/util/import/encode-filename "$file"; local enc=$ret
local guard=ble/util/import/guard:$enc
if ble/is-function "$guard"; then
builtin eval -- "$2"
else
local onload=ble/util/import/onload:$file
local onload=ble/util/import/onload:$enc
if ! ble/is-function "$onload"; then
local q=\' Q="'\''" list=_ble_util_import_onload_$((_ble_util_import_onload_count++))
builtin eval -- "$list=(); function $onload { \"\$1\" $list \"\${@:2}\"; }"
Expand Down

0 comments on commit b27f758

Please sign in to comment.