Skip to content

Commit

Permalink
decode, canvas, etc.: explicitly treat CSI arguments as decimal numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jul 12, 2021
1 parent 21d636a commit c6473b7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 42 deletions.
11 changes: 6 additions & 5 deletions memo/ChangeLog.md
Expand Up @@ -61,7 +61,7 @@
- progcomp: fix a bug that command names may stray into completer function names `#D1611` 1f2d45f
- syntax: highlight quotes of the `\?` form `#D1584` 5076a03
- prompt: support a new backslash sequence `\g{...}` `#D1609` be31391
- complete: add a new option `bleopt complete_limit_auto_menu` `#D1618` 0000000
- complete: add a new option `bleopt complete_limit_auto_menu` `#D1618` 1829d80
- rlfunc: support vi word operations in `emacs` keymap (requested by SolarAquarion) `#D1624` 0000000

## Changes
Expand Down Expand Up @@ -94,7 +94,7 @@
- decode (`ble-bind`): output bindings of the specified keymaps with `ble-bind -m KEYMAP` (fixup 750ca38) `#D1559` 6e0245a
- keymap/vi: update mode names on change of `bleopt keymap_vi_mode_name_*` (motivated by huresche) `#D1565` 11ac106
- main: show notifications against debug versions of Bash `#D1612` 8f974aa
- term: update `vte` identification `#D1620` 0000000
- term: update `vte` identification `#D1620` 00e74d8

## Fixes

Expand Down Expand Up @@ -143,7 +143,8 @@
- util (`ble/string#quote-command`): remove redundant trailing spaces for single word command `#D1613` 94556b4
- util: work around the Bash 3 bug of array assignments with `^A` and `^?` in Bash 3.2 `#D1614` b9f7611
- benchmark (`ble-measure`): fix a bug that the result is always 0 in Bash 3 and 4 (fixup bbc2a904) `#D1615` a034c91
- complete: fix a bug that the shopt settings are not restored correctly (reported by Lun4m) `#D1623` 0000000
- complete: fix a bug that the shopt settings are not restored correctly (reported by Lun4m) `#D1623` 899c114
- decode, canvas, etc.: explicitly treat CSI arguments as decimal numbers (reported by GorrillaRibs) `#D1625` 0000000

## Optimization

Expand All @@ -161,7 +162,7 @@
- main (`ble/bin/.freeze-utility-path`): fix unupdated temporary implementations `#D1528` c70a3b4
- util (`ble/util/assign`): work around subshell conflicts `#D1578` 6e4bb12
- 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` 0000000
- prompt: use `${PS1@P}` when the prompt contains only safe prompt sequences `#D1617` 8b5da08

## Compatibility

Expand All @@ -175,7 +176,7 @@
- util (`modifyOtherKeys`): work around a quirk of Kitty (reported by NoahGorny) `#D1549` f599525
- global: work around empty `vi_imap` cache by `tmux-resurrect` `#D1562` 560160b
- decode: identify `kitty` and treat `\e[27u` as isolated ESC (reported by lyiriyah) `#D1585` c2a84a2
- complete: suppress known error messages of `bash-completion` (reported by oc1024, Lun4m) `#D1622` 0000000
- complete: suppress known error messages of `bash-completion` (reported by oc1024, Lun4m) `#D1622` d117973

## Internal changes and fixes

Expand Down
17 changes: 17 additions & 0 deletions note.txt
Expand Up @@ -4666,6 +4666,23 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2021-07-13

* compat: terminal dnkl/foot は DA2 で 010801 などの文字列を返すようだ (reported by GorrillaRibs) [#D1625]
https://github.com/akinomyoga/ble.sh/issues/128

これが DA2 の判定で8進数と解釈されてしまって問題が発生している。
escape sequence に含まれる数字は全て明示的に10進数で解釈するべきである。

因みに dnkl/foot は DA3 で FOOT という文字列を返すそうだ。

* dnkl/foot を認識する様にした。
* CSI の解釈で 0 が前置されていても良い様に書き直した。
一部の CSI については既に対策されていたが今回全てに対して処理する事にした。
* DA2 の解析はまた別の箇所で行われていた。これも対処する事にした。
* ble/canvas/trace の諸々の制御機能についても対策した。
* SGR の解析部分 (ble/color/read-sgrspec) は既に対処済みだった

2021-07-12

* emacs モードで vi-bword 使う (requested by SolarAquarion) [#D1624]
Expand Down
3 changes: 2 additions & 1 deletion src/canvas.sh
Expand Up @@ -1128,7 +1128,7 @@ function ble/canvas/trace/.process-csi-sequence {
return 0 ;;
([ABCDEFGIZ\`ade])
local arg=0
[[ $param =~ ^[0-9]+$ ]] && arg=$param
[[ $param =~ ^[0-9]+$ ]] && ((arg=10#$param))
((arg==0&&(arg=1)))

local ox=$x oy=$y
Expand Down Expand Up @@ -1212,6 +1212,7 @@ function ble/canvas/trace/.process-csi-sequence {
# HVP "CSI f"
local -a params
ble/string#split-words params "${param//[^0-9]/ }"
params=("${params[@]/#/10#}")
local dstx dsty
((dstx=params[1]-1))
((dsty=params[0]-1))
Expand Down
50 changes: 32 additions & 18 deletions src/decode.sh
Expand Up @@ -805,19 +805,23 @@ function ble-decode-char/csi/.modify-key {
function ble-decode-char/csi/.decode {
local char=$1 rex key
if ((char==126)); then # ~
if rex='^>?27;([1-9][0-9]*);?([1-9][0-9]*)$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
if rex='^>?27;([0-9]+);?([0-9]+)$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
# xterm "CSI 2 7 ; <mod> ; <char> ~" sequences
local key=$((BASH_REMATCH[2]&_ble_decode_MaskChar))
ble-decode-char/csi/.modify-key "${BASH_REMATCH[1]}"
local param1=$((10#${BASH_REMATCH[1]}))
local param2=$((10#${BASH_REMATCH[2]}))
local key=$((param2&_ble_decode_MaskChar))
ble-decode-char/csi/.modify-key "$param1"
csistat=$key
return 0
fi

if rex='^>?([1-9][0-9]*)(;([1-9][0-9]*))?$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
if rex='^>?([0-9]+)(;([0-9]+))?$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
# "CSI <key> ; <mod> ~" sequences
key=${_ble_decode_csimap_tilde[BASH_REMATCH[1]]}
local param1=$((10#${BASH_REMATCH[1]}))
local param3=$((10#${BASH_REMATCH[3]}))
key=${_ble_decode_csimap_tilde[param1]}
if [[ $key ]]; then
ble-decode-char/csi/.modify-key "${BASH_REMATCH[3]}"
ble-decode-char/csi/.modify-key "$param3"
csistat=$key
return 0
fi
Expand All @@ -828,20 +832,22 @@ function ble-decode-char/csi/.decode {
# Note: 実は "CSI 1 ; mod u" が kp5 とする端末がある事に注意する。
local rematch1=${BASH_REMATCH[1]}
if [[ $rematch1 != 1 ]]; then
local key=$rematch1 mods=${BASH_REMATCH:${#rematch1}+1}
local key=$((10#$rematch1)) mods=$((10#${BASH_REMATCH:${#rematch1}+1}))
ble-decode-char/csi/.modify-key "$mods"
csistat=$key
fi
return 0
fi
elif ((char==94||char==64)); then # ^, @
if rex='^[1-9][0-9]*$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
if rex='^[0-9]+$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
# rxvt "CSI <key> ^", "CSI <key> @" sequences
key=${_ble_decode_csimap_tilde[BASH_REMATCH[1]]}
local param1=$((10#${BASH_REMATCH[1]}))
local param3=$((10#${BASH_REMATCH[3]}))
key=${_ble_decode_csimap_tilde[param1]}
if [[ $key ]]; then
((key|=_ble_decode_Ctrl,
char==64&&(key|=_ble_decode_Shft)))
ble-decode-char/csi/.modify-key "${BASH_REMATCH[3]}"
ble-decode-char/csi/.modify-key "$param3"
csistat=$key
return 0
fi
Expand All @@ -862,7 +868,9 @@ function ble-decode-char/csi/.decode {
if rex='^([0-9]+);([0-9]+)$'; [[ $_ble_decode_csi_args =~ $rex ]]; then
# DSR(6) に対する応答 CPR "CSI Pn ; Pn R"
# Note: Poderosa は DSR(Pn;Pn) "CSI Pn ; Pn n" で返す。
ble/term/CPR/notify $((10#${BASH_REMATCH[1]})) $((10#${BASH_REMATCH[2]}))
local param1=$((10#${BASH_REMATCH[1]}))
local param2=$((10#${BASH_REMATCH[2]}))
ble/term/CPR/notify "$param1" "$param2"
csistat=$_ble_decode_KCODE_IGNORE
return 0
fi
Expand All @@ -878,11 +886,14 @@ function ble-decode-char/csi/.decode {
# mouse1up mouse2up mouse3up mouse4up mouse5up
# mouse1drag mouse2drag mouse3drag mouse4drag mouse5drag
# wheelup wheeldown mouse_move
local button=$((10#${BASH_REMATCH[1]}))
local param1=$((10#${BASH_REMATCH[1]}))
local param2=$((10#${BASH_REMATCH[2]}))
local param3=$((10#${BASH_REMATCH[3]}))
local button=$param1
((_ble_term_mouse_button=button&~0x1C,
char==109&&(_ble_term_mouse_button|=0x70),
_ble_term_mouse_x=10#${BASH_REMATCH[2]}-1,
_ble_term_mouse_y=10#${BASH_REMATCH[3]}-1))
_ble_term_mouse_x=param2-1,
_ble_term_mouse_y=param3-1))
local key=$_ble_decode_KCODE_MOUSE
((button&32)) && key=$_ble_decode_KCODE_MOUSE_MOVE
ble-decode-char/csi/.modify-key $((button>>2&0x07))
Expand All @@ -892,9 +903,11 @@ function ble-decode-char/csi/.decode {
elif ((char==116)); then # t
if rex='^<([0-9]+);([0-9]+)$'; [[ $_ble_decode_csi_args =~ $rex ]]; then
## mouse_select
local param1=$((10#${BASH_REMATCH[1]}))
local param2=$((10#${BASH_REMATCH[2]}))
((_ble_term_mouse_button=128,
_ble_term_mouse_x=10#${BASH_REMATCH[1]}-1,
_ble_term_mouse_y=10#${BASH_REMATCH[2]}-1))
_ble_term_mouse_x=param1-1,
_ble_term_mouse_y=param2-1))
local key=$_ble_decode_KCODE_MOUSE
csistat=$key
fi
Expand All @@ -903,8 +916,9 @@ function ble-decode-char/csi/.decode {
# pc-style "CSI 1; <mod> A" sequences
key=${_ble_decode_csimap_alpha[char]}
if [[ $key ]]; then
if rex='^(1?|>?1;([1-9][0-9]*))$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
ble-decode-char/csi/.modify-key "${BASH_REMATCH[2]}"
if rex='^(1?|>?1;([0-9]+))$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
local param2=$((10#${BASH_REMATCH[2]}))
ble-decode-char/csi/.modify-key "$param2"
csistat=$key
return 0
fi
Expand Down
42 changes: 24 additions & 18 deletions src/util.sh
Expand Up @@ -5420,26 +5420,13 @@ fi
_ble_term_TERM=
function ble/term/DA2/initialize-term {
local rex='^[0-9]*(;[0-9]*)*$'; [[ $_ble_term_DA2R =~ $rex ]] || return
local da2r; ble/string#split da2r ';' "$_ble_term_DA2R"

# xterm
if rex='^xterm(-|$)'; [[ $TERM =~ $rex ]]; then
local version=${da2r[1]}
if rex='^1;[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
# 2000以上は vte
((version==0||95<=version&&version<2000))
elif rex='^0;[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
((95<=version))
elif rex='^(2|24|1[89]|41|6[145]);[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
((280<=version))
elif rex='^32;[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
((354<=version&&version<2000))
else
false
fi && { _ble_term_TERM=xterm:$version; return; }
fi
local da2r
ble/string#split da2r ';' "$_ble_term_DA2R"
da2r=("${da2r[@]/#/10#}") # 0で始まっていても 10 進数で解釈

case $_ble_term_DA2R in
('1;0'?????';0')
_ble_term_TERM=foot ;;
('1;'*)
if ((4000<=da2r[1]&&da2r[1]<4100&&3<=da2r[2])); then
_ble_term_TERM=kitty
Expand All @@ -5465,6 +5452,25 @@ function ble/term/DA2/initialize-term {
('84;0;0')
_ble_term_TERM=tmux ;;
esac
[[ $_ble_term_TERM ]] && return 0

# xterm
if rex='^xterm(-|$)'; [[ $TERM =~ $rex ]]; then
local version=${da2r[1]}
if rex='^1;[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
# Note: vte (2000以上), kitty (4000以上) は処理済み
true
elif rex='^0;[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
((95<=version))
elif rex='^(2|24|1[89]|41|6[145]);[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
((280<=version))
elif rex='^32;[0-9]+;0$'; [[ $_ble_term_DA2R =~ $rex ]]; then
((354<=version&&version<2000))
else
false
fi && { _ble_term_TERM=xterm:$version; return; }
fi

return 0
}

Expand Down

0 comments on commit c6473b7

Please sign in to comment.