Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
global: work around the arithmetic syntax error of "10#" in Bash-5.1
  • Loading branch information
akinomyoga committed Jan 10, 2022
1 parent 7900144 commit 7545ea3
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 48 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -283,6 +283,7 @@
- main: work around `XDG_RUNTIME_DIR` of a different user by `su` (reported by zim0369) `#D1712` 8d37048
- 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` 0000000

## Internal changes and fixes

Expand Down
8 changes: 4 additions & 4 deletions keymap/vi.sh
Expand Up @@ -793,7 +793,7 @@ function ble/keymap:vi/get-arg {
if [[ ! $_ble_edit_arg && ! $_ble_keymap_vi_oparg ]]; then
ARG=$default_value
else
ARG=$((10#${_ble_edit_arg:-1}*10#${_ble_keymap_vi_oparg:-1}))
ARG=$((10#0${_ble_edit_arg:-1}*10#0${_ble_keymap_vi_oparg:-1}))
fi
ble/keymap:vi/clear-arg
}
Expand Down Expand Up @@ -5810,9 +5810,9 @@ function ble/widget/vi_nmap/increment.impl {
local abs=${number#-}
if [[ $abs == 0?* ]]; then
if [[ $number == -* ]]; then
number=-$((10#$abs))
number=-$((10#0$abs))
else
number=$((10#$abs))
number=$((10#0$abs))
fi
fi

Expand Down Expand Up @@ -7467,7 +7467,7 @@ function ble/widget/vi_xmap/increment.impl {
local rematch1=${BASH_REMATCH[1]}
local rematch2=${BASH_REMATCH[2]}
local offset=${#rematch1} length=${#rematch2}
local number=$((10#$rematch2))
local number=$((10#0$rematch2))
[[ $rematch1 == *- ]] && ((number=-number,offset--,length++))

# 新しい数
Expand Down
4 changes: 2 additions & 2 deletions lib/core-syntax.sh
Expand Up @@ -2918,8 +2918,8 @@ function ble/syntax:bash/check-brace-expansion {
# リダイレクトで複数語に展開される時はエラー
local lhs=${rematch1::len2} rhs=${rematch1:len2+2}
if [[ $rematch2 ]]; then
local lhs1=$((10#${lhs#[-+]})); [[ $lhs == -* ]] && ((lhs1=-lhs1))
local rhs1=$((10#${rhs#[-+]})); [[ $rhs == -* ]] && ((rhs1=-rhs1))
local lhs1=$((10#0${lhs#[-+]})); [[ $lhs == -* ]] && ((lhs1=-lhs1))
local rhs1=$((10#0${rhs#[-+]})); [[ $rhs == -* ]] && ((rhs1=-rhs1))
lhs=$lhs1 rhs=$rhs1
fi
[[ $lhs != "$rhs" ]] && ((attr=ATTR_ERR))
Expand Down
8 changes: 8 additions & 0 deletions make_command.sh
Expand Up @@ -1135,6 +1135,13 @@ function sub:scan/bash301bug-array-element-length {
grc '\$\{#[[:alnum:]]+\[[^@*]' --exclude={test,ChangeLog.md} | grep -Ev '^([^#]*[[:space:]])?#'
}

function sub:scan/bash501-arith-base {
echo "--- $FUNCNAME ---"
# bash-3.1 で ${#arr[index]} を用いると、
# 日本語の文字数が変になる。
grc '\b10#\$' --exclude={test,ChangeLog.md}
}

function sub:scan/gawk402bug-regex-check {
echo "--- $FUNCNAME ---"
grc --color '\[\^?\][^]]*\[:[^]]*:\].[^]]*\]' --exclude={test,ext,\*.md} | grep -Ev '#D1709 safe'
Expand Down Expand Up @@ -1289,6 +1296,7 @@ function sub:scan {
sub:scan/check-todo-mark
sub:scan/bash300bug
sub:scan/bash301bug-array-element-length
sub:scan/bash501-arith-base
sub:scan/gawk402bug-regex-check
sub:scan/array-count-in-arithmetic-expression
sub:scan/unset-variable |
Expand Down
7 changes: 7 additions & 0 deletions note.txt
Expand Up @@ -5812,6 +5812,13 @@ bash_tips

2022-01-09

* global: 算術式 10# が bash-5.1 以降エラーになる [#D1734]

一度対策として書き換えを行ったが不十分だった。全ての箇所を個別にチェックす
るのは大変だし、今後算術式を書く時にまた問題になっても困るので全て
10#0$... と記述する事にした。この様にしておけば正規表現で 10#$ を検出して書
き換える様に促す事ができる。

* mandb: rsync の抽出 正しく抽出できていない [#D1733]

% と思ったが今試したらちゃんと動いている?
Expand Down
4 changes: 2 additions & 2 deletions src/benchmark.sh
Expand Up @@ -35,7 +35,7 @@ if [[ ${ZSH_VERSION-} ]]; then
fi
m=${m:-0} ms=${ms}000; ms=${ms:0:3}

((utot=((10#$m*60+10#$s)*1000+10#$ms)*1000,
((utot=((10#0$m*60+10#0$s)*1000+10#0$ms)*1000,
usec=utot/n))
return 0
else
Expand Down Expand Up @@ -75,7 +75,7 @@ else
[[ $result =~ $rex ]] || return 1
local s=${BASH_REMATCH[1]}
local ms=${BASH_REMATCH[3]}000; ms=${ms::3}
((utot=(10#$s*1000+10#$ms)*1000,usec=utot1/n))
((utot=(10#0$s*1000+10#0$ms)*1000,usec=utot1/n))
return 0
}
fi
Expand Down
2 changes: 1 addition & 1 deletion src/canvas.sh
Expand Up @@ -1534,7 +1534,7 @@ function ble/canvas/trace/.process-csi-sequence {
return 0 ;;
([ABCDEFGIZ\`ade])
local arg=0
[[ $param =~ ^[0-9]+$ ]] && ((arg=10#${param:-0}))
[[ $param =~ ^[0-9]+$ ]] && ((arg=10#0$param))
((arg==0&&(arg=1)))

local ox=$x oy=$y
Expand Down
18 changes: 9 additions & 9 deletions src/color.sh
Expand Up @@ -416,18 +416,18 @@ function ble/color/gspec2sgr {
function ble/color/.name2color/.clamp {
local text=$1 max=$2
if [[ $text == *% ]]; then
((ret=10#${text%'%'}*max/100))
((ret=10#0${text%'%'}*max/100))
else
((ret=10#$text))
((ret=10#0$text))
fi
((ret>max)) && ret=max
}
function ble/color/.name2color/.wrap {
local text=$1 max=$2
if [[ $text == *% ]]; then
((ret=10#${text%'%'}*max/100))
((ret=10#0${text%'%'}*max/100))
else
((ret=10#$text))
((ret=10#0$text))
fi
((ret%=max))
}
Expand Down Expand Up @@ -464,7 +464,7 @@ function ble/color/.hsb2color {
function ble/color/.name2color {
local colorName=$1
if [[ ! ${colorName//[0-9]} ]]; then
((ret=10#$colorName&255))
((ret=10#0$colorName&255))
elif [[ $colorName == '#'* ]]; then
if local rex='^#[0-9a-fA-F]{3}$'; [[ $colorName =~ $rex ]]; then
let "ret=1<<24|16#${colorName:1:1}*0x11<<16|16#${colorName:2:1}*0x11<<8|16#${colorName:3:1}*0x11"
Expand Down Expand Up @@ -537,7 +537,7 @@ function ble/color/.color2name {
return 0
fi

((ret=(10#$1&255)))
((ret=(10#0$1&255)))
case $ret in
(0) ret=black ;;
(1) ret=brown ;;
Expand Down Expand Up @@ -757,10 +757,10 @@ function ble/color/read-sgrspec/.arg-next {
fi

if ((j<${#fields[*]})); then
((_ret=10#${fields[j++]}))
((_ret=10#0${fields[j++]}))
else
((i++))
((_ret=10#${specs[i]%%:*}))
((_ret=10#0${specs[i]%%:*}))
fi

(($_var=_ret))
Expand All @@ -775,7 +775,7 @@ function ble/color/read-sgrspec {
for ((i=0,iN=${#specs[@]};i<iN;i++)); do
local spec=${specs[i]} fields
ble/string#split fields : "$spec"
local arg=$((10#${fields[0]}))
local arg=$((10#0${fields[0]}))
if ((arg==0)); then
g=0
continue
Expand Down
30 changes: 15 additions & 15 deletions src/decode.sh
Expand Up @@ -823,8 +823,8 @@ function ble-decode-char/csi/.decode {
if ((char==126)); then # ~
if rex='^>?27;([0-9]+);?([0-9]+)$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
# xterm "CSI 2 7 ; <mod> ; <char> ~" sequences
local param1=$((10#${BASH_REMATCH[1]:-0}))
local param2=$((10#${BASH_REMATCH[2]:-0}))
local param1=$((10#0${BASH_REMATCH[1]}))
local param2=$((10#0${BASH_REMATCH[2]}))
local key=$((param2&_ble_decode_MaskChar))
ble-decode-char/csi/.modify-key "$param1"
csistat=$key
Expand All @@ -833,8 +833,8 @@ function ble-decode-char/csi/.decode {

if rex='^>?([0-9]+)(;([0-9]+))?$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
# "CSI <key> ; <mod> ~" sequences
local param1=$((10#${BASH_REMATCH[1]:-0}))
local param3=$((10#${BASH_REMATCH[3]:-0}))
local param1=$((10#0${BASH_REMATCH[1]}))
local param3=$((10#0${BASH_REMATCH[3]}))
key=${_ble_decode_csimap_tilde[param1]}
if [[ $key ]]; then
ble-decode-char/csi/.modify-key "$param3"
Expand All @@ -848,7 +848,7 @@ function ble-decode-char/csi/.decode {
# Note: 実は "CSI 1 ; mod u" が kp5 とする端末がある事に注意する。
local rematch1=${BASH_REMATCH[1]}
if [[ $rematch1 != 1 ]]; then
local key=$((10#${rematch1:-0})) mods=$((10#0${BASH_REMATCH:${#rematch1}+1}))
local key=$((10#0$rematch1)) mods=$((10#0${BASH_REMATCH:${#rematch1}+1}))
[[ $_ble_term_TERM == kitty ]] && ble-decode/char/csi/.translate-kitty-csi-u
ble-decode-char/csi/.modify-key "$mods"
csistat=$key
Expand All @@ -858,8 +858,8 @@ function ble-decode-char/csi/.decode {
elif ((char==94||char==64)); then # ^, @
if rex='^[0-9]+$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
# rxvt "CSI <key> ^", "CSI <key> @" sequences
local param1=$((10#${BASH_REMATCH[1]:-0}))
local param3=$((10#${BASH_REMATCH[3]:-0}))
local param1=$((10#0${BASH_REMATCH[1]}))
local param3=$((10#0${BASH_REMATCH[3]}))
key=${_ble_decode_csimap_tilde[param1]}
if [[ $key ]]; then
((key|=_ble_decode_Ctrl,
Expand All @@ -885,8 +885,8 @@ 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" で返す。
local param1=$((10#${BASH_REMATCH[1]:-0}))
local param2=$((10#${BASH_REMATCH[2]:-0}))
local param1=$((10#0${BASH_REMATCH[1]}))
local param2=$((10#0${BASH_REMATCH[2]}))
ble/term/CPR/notify "$param1" "$param2"
csistat=$_ble_decode_KCODE_IGNORE
return 0
Expand All @@ -903,9 +903,9 @@ function ble-decode-char/csi/.decode {
# mouse1up mouse2up mouse3up mouse4up mouse5up
# mouse1drag mouse2drag mouse3drag mouse4drag mouse5drag
# wheelup wheeldown mouse_move
local param1=$((10#${BASH_REMATCH[1]:-0}))
local param2=$((10#${BASH_REMATCH[2]:-0}))
local param3=$((10#${BASH_REMATCH[3]:-0}))
local param1=$((10#0${BASH_REMATCH[1]}))
local param2=$((10#0${BASH_REMATCH[2]}))
local param3=$((10#0${BASH_REMATCH[3]}))
local button=$param1
((_ble_term_mouse_button=button&~0x1C,
char==109&&(_ble_term_mouse_button|=0x70),
Expand All @@ -920,8 +920,8 @@ 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]:-0}))
local param2=$((10#${BASH_REMATCH[2]:-0}))
local param1=$((10#0${BASH_REMATCH[1]}))
local param2=$((10#0${BASH_REMATCH[2]}))
((_ble_term_mouse_button=128,
_ble_term_mouse_x=param1-1,
_ble_term_mouse_y=param2-1))
Expand All @@ -934,7 +934,7 @@ function ble-decode-char/csi/.decode {
key=${_ble_decode_csimap_alpha[char]}
if [[ $key ]]; then
if rex='^(1?|>?1;([0-9]+))$' && [[ $_ble_decode_csi_args =~ $rex ]]; then
local param2=$((10#${BASH_REMATCH[2]:-0}))
local param2=$((10#0${BASH_REMATCH[2]}))
ble-decode-char/csi/.modify-key "$param2"
csistat=$key
return 0
Expand Down
10 changes: 5 additions & 5 deletions src/edit.sh
Expand Up @@ -2084,11 +2084,11 @@ function ble-edit/content/get-arg {
if [[ $value == - ]]; then
arg=-1
else
arg=$((-10#${value#-}))
arg=$((-10#0${value#-}))
fi
else
if [[ $value ]]; then
arg=$((10#$value))
arg=$((10#0$value))
else
arg=$default_value
fi
Expand Down Expand Up @@ -2301,7 +2301,7 @@ function ble-edit/eval-IGNOREEOF {

if [[ $value && ! ${value//[0-9]} ]]; then
# 正の整数は十進数で解釈
ret=$((10#$value))
ret=$((10#0$value))
else
# 負の整数、空文字列、その他
ret=10
Expand Down Expand Up @@ -6716,7 +6716,7 @@ function ble-edit/isearch/.shift-backward-references {
local buff=
while [[ $needle =~ $rex ]]; do
local mlen=${#BASH_REMATCH}
buff=$buff${BASH_REMATCH::mlen-1}$((10#${BASH_REMATCH:mlen-1}+1))
buff=$buff${BASH_REMATCH::mlen-1}$((10#0${BASH_REMATCH:mlen-1}+1))
needle=${needle:mlen}
done
needle=$buff$needle
Expand Down Expand Up @@ -8183,7 +8183,7 @@ function ble/builtin/read/.loop {
if [[ $opt_timeout == *.* ]]; then
local mantissa=${opt_timeout%%.*}
local fraction=${opt_timeout##*.}000
((timeout=mantissa*1000+10#${fraction::3}))
((timeout=mantissa*1000+10#0${fraction::3}))
else
((timeout=opt_timeout*1000))
fi
Expand Down
20 changes: 10 additions & 10 deletions src/util.sh
Expand Up @@ -3458,7 +3458,7 @@ function ble/util/msleep/.check-sleep-decimal-support {
_ble_util_msleep_delay=2000 # [usec]
function ble/util/msleep/.core {
local sec=${1%%.*}
((10#${1##*.}&&sec++)) # 小数部分は切り上げ
((10#0${1##*.}&&sec++)) # 小数部分は切り上げ
ble/bin/sleep "$sec"
}
function ble/util/msleep {
Expand Down Expand Up @@ -3650,7 +3650,7 @@ if ((_ble_bash>=40400)) && ble/util/msleep/.check-builtin-sleep; then
if local rex='^\+?([0-9]*)\.([0-9]*)([eE][-+]?[0-9]+)?([smhd]?)$'; [[ $1 =~ $rex ]]; then
a1=${BASH_REMATCH[1]}
b1=${BASH_REMATCH[2]}00000000000000
b1=$((10#${b1::14}))
b1=$((10#0${b1::14}))
exp=${BASH_REMATCH[3]}
unit=${BASH_REMATCH[4]}
elif rex='^\+?([0-9]+)([eE][-+]?[0-9]+)?([smhd]?)$'; [[ $1 =~ $rex ]]; then
Expand All @@ -3666,7 +3666,7 @@ if ((_ble_bash>=40400)) && ble/util/msleep/.check-builtin-sleep; then
if [[ $exp ]]; then
case $exp in
([eE]-*)
((exp=10#${exp:2}))
((exp=10#0${exp:2}))
while ((exp--)); do
((b1=a1%10*frac_scale/10+b1/10,a1/=10))
done ;;
Expand Down Expand Up @@ -3788,7 +3788,7 @@ function ble/util/sleep {
local msec=$((${1%%.*}*1000))
if [[ $1 == *.* ]]; then
frac=${1##*.}000
((msec+=10#${frac::3}))
((msec+=10#0${frac::3}))
fi
ble/util/msleep "$msec"
}
Expand Down Expand Up @@ -4675,15 +4675,15 @@ function ble/util/clock/.initialize {
local LC_ALL= LC_NUMERIC=C
if ((_ble_bash>=50000)) && [[ $EPOCHREALTIME == *.???* ]]; then
# implementation with EPOCHREALTIME
_ble_util_clock_base=$((10#${EPOCHREALTIME%.*}))
_ble_util_clock_base=$((10#0${EPOCHREALTIME%.*}))
_ble_util_clock_reso=1
_ble_util_clock_type=EPOCHREALTIME
function ble/util/clock {
local LC_ALL= LC_NUMERIC=C
local now=$EPOCHREALTIME
local integral=$((10#${now%%.*}-_ble_util_clock_base))
local integral=$((10#0${now%%.*}-_ble_util_clock_base))
local mantissa=${now#*.}000; mantissa=${mantissa::3}
((ret=integral*1000+10#$mantissa))
((ret=integral*1000+10#0$mantissa))
}
ble/function#suppress-stderr ble/util/clock # locale
elif [[ -r /proc/uptime ]] && {
Expand All @@ -4692,16 +4692,16 @@ function ble/util/clock/.initialize {
ble/string#split-words uptime "$uptime"
[[ $uptime == *.* ]]; }; then
# implementation with /proc/uptime
_ble_util_clock_base=$((10#${uptime%.*}))
_ble_util_clock_base=$((10#0${uptime%.*}))
_ble_util_clock_reso=10
_ble_util_clock_type=uptime
function ble/util/clock {
local now
ble/util/readfile now /proc/uptime
ble/string#split-words now "$now"
local integral=$((10#${now%%.*}-_ble_util_clock_base))
local integral=$((10#0${now%%.*}-_ble_util_clock_base))
local fraction=${now#*.}000; fraction=${fraction::3}
((ret=integral*1000+10#$fraction))
((ret=integral*1000+10#0$fraction))
}
elif ((_ble_bash>=40200)); then
printf -v _ble_util_clock_base '%(%s)T'
Expand Down

0 comments on commit 7545ea3

Please sign in to comment.