Skip to content

Commit

Permalink
global: adjust implementations for bash-5.2 "patsub_replacement"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jun 28, 2022
1 parent b321b57 commit 66ae615
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 79 deletions.
10 changes: 5 additions & 5 deletions keymap/vi_test.sh
Expand Up @@ -39,10 +39,10 @@ function ble/keymap:vi_test/check {
((ntest++,nsuccess++))
else
((ntest++))
echo "test($section/$id): keys = ($kspecs)"
echo " initial = \"$i:${in//$nl/$NL}\""
echo " expected = \"$f:${fin//$nl/$NL}\""
echo " result = \"$_ble_edit_ind:${_ble_edit_str//$nl/$NL}\""
ble/util/print "test($section/$id): keys = ($kspecs)"
ble/util/print " initial = \"$i:${in//$nl/"$NL"}\""
ble/util/print " expected = \"$f:${fin//$nl/"$NL"}\""
ble/util/print " result = \"$_ble_edit_ind:${_ble_edit_str//$nl/"$NL"}\""
fi >&2

# restore states
Expand All @@ -61,7 +61,7 @@ function ble/keymap:vi_test/show-summary {
else
local tip=$'\e[31mfailed\e[m'
fi
echo "# $title test: result $((nsuccess))/$((ntest)) $tip"
ble/util/print "# $title test: result $((nsuccess))/$((ntest)) $tip"
}

#------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion lib/core-complete.sh
Expand Up @@ -1319,7 +1319,7 @@ function ble/complete/progcomp/.parse-complete {
flag_noquote=
local compdef=${1#'complete '}

local arg optarg rex='^([^][*?;&|[:space:]<>()\`$"'\''{}#^!]|\\.|'\''[^'\'']*'\'')+[[:space:]]+'
local arg optarg rex='^([^][*?;&|[:space:]<>()\`$"'\''{}#^!]|\\.|'\''[^'\'']*'\'')+[[:space:]]+' # #D1709 safe
while ble/complete/progcomp/.parse-complete/next; do
case $arg in
(-*)
Expand Down
34 changes: 17 additions & 17 deletions lib/core-syntax.sh
Expand Up @@ -933,14 +933,14 @@ function ble/syntax:bash/cclass/update {
local histc2=${_ble_syntax_bash_histc12:1:1}
for key in "${!_ble_syntax_bash_charsFmt[@]}"; do
local a=${_ble_syntax_bash_charsFmt[key]}
a=${a//@h/$histc1}
a=${a//@q/$histc2}
a=${a//@h/"$histc1"}
a=${a//@q/"$histc2"}
_ble_syntax_bash_chars[key]=$a
done

local a=$_ble_syntax_bash_chars_simpleFmt
a=${a//@h/$histc1}
a=${a//@q/$histc2}
a=${a//@h/"$histc1"}
a=${a//@q/"$histc2"}
_ble_syntax_bashc_simple=$a
fi

Expand Down Expand Up @@ -1700,7 +1700,7 @@ function ble/syntax:bash/check-history-expansion/.initialize-event {
local A="[$histc1]"
[[ $histc1 == '^' ]] && A='\^'
rex_event=$_ble_syntax_bash_histexpand_RexEventFmt
rex_event=${rex_event//@A/$A}
rex_event=${rex_event//@A/"$A"}
fi
}
## 関数 ble/syntax:bash/check-history-expansion/.initialize-quicksub
Expand All @@ -1711,8 +1711,8 @@ function ble/syntax:bash/check-history-expansion/.initialize-quicksub {
rex_quicksub=$_ble_syntax_bash_histexpand_RexQuicksubDef
else
rex_quicksub=$_ble_syntax_bash_histexpand_RexQuicksubFmt
rex_quicksub=${rex_quicksub//@A/[$histc2]}
rex_quicksub=${rex_quicksub//@C/$histc2}
rex_quicksub=${rex_quicksub//@A/"[$histc2]"}
rex_quicksub=${rex_quicksub//@C/"$histc2"}
fi
}
function ble/syntax:bash/check-history-expansion/.check-modifiers {
Expand Down Expand Up @@ -3625,10 +3625,10 @@ function ble/syntax:bash/ctx-heredoc-word/remove-quotes {
if rex='^\$?"(([^\"]|\\.)*)(\\?$|")'; [[ $text =~ $rex ]]; then
local str=${BASH_REMATCH[1]}
local a b
b='\`' a='`'; str="${str//"$b"/$a}"
b='\"' a='"'; str="${str//"$b"/$a}"
b='\$' a='$'; str="${str//"$b"/$a}"
b='\\' a='\'; str="${str//"$b"/$a}"
b='\`' a='`'; str="${str//"$b"/"$a"}"
b='\"' a='"'; str="${str//"$b"/"$a"}"
b='\$' a='$'; str="${str//"$b"/"$a"}"
b='\\' a='\'; str="${str//"$b"/"$a"}"
result=$result$str
text=${text:${#BASH_REMATCH}}
continue
Expand Down Expand Up @@ -3664,12 +3664,12 @@ function ble/syntax:bash/ctx-heredoc-word/escape-delimiter {
local ret=$1
if [[ $ret == *[\\\'$_ble_syntax_bash_IFS$_ble_term_FS]* ]]; then
local a b fs=$_ble_term_FS
a=\\ ; b="\\$a"; ret="${ret//"$a"/$b}"
a=\' ; b="\\$a"; ret="${ret//"$a"/$b}"
a=' ' ; b="$_ble_syntax_bash_heredoc_EscSP"; ret="${ret//"$a"/$b}"
a=$'\t'; b="$_ble_syntax_bash_heredoc_EscHT"; ret="${ret//"$a"/$b}"
a=$'\n'; b="$_ble_syntax_bash_heredoc_EscLF"; ret="${ret//"$a"/$b}"
a=$fs ; b="$_ble_syntax_bash_heredoc_EscFS"; ret="${ret//"$a"/$b}"
a=\\ ; b="\\$a"; ret="${ret//"$a"/"$b"}"
a=\' ; b="\\$a"; ret="${ret//"$a"/"$b"}"
a=' ' ; b="$_ble_syntax_bash_heredoc_EscSP"; ret="${ret//"$a"/"$b"}"
a=$'\t'; b="$_ble_syntax_bash_heredoc_EscHT"; ret="${ret//"$a"/"$b"}"
a=$'\n'; b="$_ble_syntax_bash_heredoc_EscLF"; ret="${ret//"$a"/"$b"}"
a=$fs ; b="$_ble_syntax_bash_heredoc_EscFS"; ret="${ret//"$a"/"$b"}"
fi
escaped=$ret
}
Expand Down
10 changes: 5 additions & 5 deletions lib/init-bind.sh
Expand Up @@ -9,14 +9,14 @@
function ble/init:bind/append {
local xarg="\"$1\":ble-decode/.hook $2; builtin eval -- \"\$_ble_decode_bind_hook\""
local rarg=$1 condition=$3${3:+' && '}
ble/util/print "${condition}builtin bind -x '${xarg//$apos/$APOS}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$apos/$APOS}'" >> "$fbind2"
ble/util/print "${condition}builtin bind -x '${xarg//$q/$Q}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$q/$Q}'" >> "$fbind2"
}
function ble/init:bind/append-macro {
local kseq1=$1 kseq2=$2 condition=$3${3:+' && '}
local sarg="\"$kseq1\":\"$kseq2\"" rarg=$kseq1
ble/util/print "${condition}builtin bind '${sarg//$apos/$APOS}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$apos/$APOS}'" >> "$fbind2"
ble/util/print "${condition}builtin bind '${sarg//$q/$Q}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$q/$Q}'" >> "$fbind2"
}

function ble/init:bind/generate-binder {
Expand All @@ -28,7 +28,7 @@ function ble/init:bind/generate-binder {
: >| "$fbind1"
: >| "$fbind2"

local apos=\' APOS="'\\''"
local q=\' Q="'\\''"
local altdqs24='\xC0\x98'
local altdqs27='\xC0\x9B'

Expand Down
35 changes: 35 additions & 0 deletions make_command.sh
Expand Up @@ -144,6 +144,38 @@ function sub:check/bash301bug-array-element-length {
grc '\$\{#[[:alnum:]]+\[[^@*]' --exclude=test | grep -Ev '^([^#]*[[:space:]])?#'
}

function sub:check/bash501-arith-base {
echo "--- $FUNCNAME ---"
# bash-5.1 で $((10#)) の取り扱いが変わった。
grc '\b10#\$' --exclude={test,ChangeLog.md}
}

function sub:check/bash502-patsub_replacement {
echo "--- $FUNCNAME ---"
# bash-5.2 patsub_replacement で ${var/pat/string} の string 中の & が特別な
# 意味を持つ様になったので、特に意識する場合を除いては quote が必要になった。
local esc='(\[[ -?]*[@-~])*'
grc --color '\$\{[[:alnum:]_]+(\[[^][]*\])?//?([^{}]|\{[^{}]*\})+/[^{}"'\'']*([&$]|\\)' --exclude=./test |
sed -E 'h;s/'"$esc"'//g;s/^[^:]*:[0-9]+:[[:space:]]*//
\Z//?\$q/\$Q\}Zd
\Z//?\$__ble_q/\$__ble_Q\}Zd
\Z//?\$_ble_local_q/\$_ble_local_Q\}Zd
\Z/\$\(\([^()]+\)\)\}Zd
\Z/\$'\''([^\\]|\\.)+'\''\}Zd
\Z\$\{[a-zA-Z0-9_]+//(ARR|DICT|PREFIX|NAME)/\$([a-zA-Z0-9_]+|\{[a-zA-Z0-9_#:-]+\})\}Zd
\Z\$\{[a-zA-Z0-9_]+//'\''%[dlcxy]'\''/\$[a-zA-Z0-9_]+\}Zd # src/canvas.sh
\Z#D1738Zd
\Z\$\{_ble_edit_str//\$'\''\\n'\''/\$'\''\\n'\''"\$comment_begin"\}Zd # edit.sh
g'
}

function sub:check/gawk402bug-regex-check {
echo "--- $FUNCNAME ---"
grc --color '\[\^?\][^]]*\[:[^]]*:\].[^]]*\]' --exclude={test,ext,\*.md} | grep -Ev '#D1709 safe'
}

function sub:check/assign {
echo "--- $FUNCNAME ---"
local command="$1"
Expand Down Expand Up @@ -224,6 +256,9 @@ function sub:check {
sub:check/a.txt
sub:check/bash300bug
sub:check/bash301bug-array-element-length
sub:check/bash501-arith-base
sub:check/bash502-patsub_replacement
sub:check/gawk402bug-regex-check
sub:check/array-count-in-arithmetic-expression
sub:check/unset-variable

Expand Down
4 changes: 2 additions & 2 deletions memo/D0628.extract-global-values.sh
Expand Up @@ -302,7 +302,7 @@ if [[ $mode == test5d ]]; then

((__ble_i==__ble_MaxLoop)) && __ble_error=1 __ble_value=NOT_FOUND

echo "declare $__ble_name='${__ble_value//$__ble_q//$__ble_Q}'"
echo "declare $__ble_name='${__ble_value//$__ble_q/$__ble_Q}'"
done

[[ ! $__ble_error ]]
Expand Down Expand Up @@ -331,7 +331,7 @@ if [[ $mode == test5d ]]; then

((__ble_found)) || __ble_error= __ble_value=NOT_FOUND

echo "declare $__ble_name='${__ble_value//$__ble_q//$__ble_Q}'"
echo "declare $__ble_name='${__ble_value//$__ble_q/$__ble_Q}'"
done

[[ ! $__ble_error ]]
Expand Down
4 changes: 2 additions & 2 deletions src/canvas.sh
Expand Up @@ -422,10 +422,10 @@ function ble/canvas/put-vpa.draw {
function ble/canvas/put-ech.draw {
local value=${1:-1} esc
if [[ $_ble_term_ech ]]; then
esc=${_ble_term_ech/'%d'/$value}
esc=${_ble_term_ech//'%d'/$value}
else
ble/string#reserve-prototype "$value"
esc=${_ble_string_prototype::value}${_ble_term_cub/'%d'/$value}
esc=${_ble_string_prototype::value}${_ble_term_cub//'%d'/$value}
fi
DRAW_BUFF[${#DRAW_BUFF[*]}]=$esc
}
Expand Down
6 changes: 3 additions & 3 deletions src/decode.sh
Expand Up @@ -2049,8 +2049,8 @@ function ble-decode-bind/cmap/.generate-binder-template {
}

function ble-decode-bind/cmap/.emit-bindx {
local ap="'" eap="'\\''"
echo "builtin bind -x '\"${1//$ap/$eap}\":ble-decode/.hook $2; builtin eval \"\$_ble_decode_bind_hook\"'"
local q="'" Q="'\''"
echo "builtin bind -x '\"${1//$q/$Q}\":ble-decode/.hook $2; builtin eval \"\$_ble_decode_bind_hook\"'"
}
function ble-decode-bind/cmap/.emit-bindr {
echo "builtin bind -r \"$1\""
Expand Down Expand Up @@ -2119,7 +2119,7 @@ function ble-decode-bind/.generate-source-to-unbind-default/.process {
local q=\' b=\\ Q="'\''"
# Note: Solaris xpg4 awk では gsub の置換後のエスケープシーケンスも処理される
[[ $_ble_bin_awk_solaris_xpg4 == yes ]] && Q="'$b$b''"
local QUOT_Q=\"${Q//"$b"/$b$b}\"
local QUOT_Q=\"${Q//"$b"/"$b$b"}\"
LC_ALL=C ble/bin/awk -v q="$q" '
BEGIN {
Q = '"$QUOT_Q"';
Expand Down
18 changes: 9 additions & 9 deletions src/edit.sh
Expand Up @@ -199,7 +199,7 @@ function ble-edit/prompt/initialize {
c=$ret
fi
fi
windir=/cygdrive/$c/${path//$bsl/$sl}
windir=/cygdrive/$c/${path//"$bsl"/"$sl"}
fi

if [[ -e $windir && -w $windir ]]; then
Expand Down Expand Up @@ -265,10 +265,10 @@ function ble-edit/prompt/.load {
function ble-edit/prompt/print {
local text=$1 a b
if [[ $text == *['$\"`']* ]]; then
a='\' b='\\' text=${text//"$a"/$b}
a='$' b='\$' text=${text//"$a"/$b}
a='"' b='\"' text=${text//"$a"/$b}
a='`' b='\`' text=${text//"$a"/$b}
a='\' b='\\' text=${text//"$a"/"$b"}
a='$' b='\$' text=${text//"$a"/"$b"}
a='"' b='\"' text=${text//"$a"/"$b"}
a='`' b='\`' text=${text//"$a"/"$b"}
fi
ble/canvas/put.draw "$text"
}
Expand Down Expand Up @@ -3999,17 +3999,17 @@ function ble-edit/exec:gexec/.setup {
((${#_ble_edit_exec_lines[@]}==0)) && return 1
ble/util/buffer.flush >&2

local apos=\' APOS="'\\''"
local q=\' Q="'\\''"
local cmd
local -a buff
local count=0
buff[${#buff[@]}]=ble-edit/exec:gexec/.begin
for cmd in "${_ble_edit_exec_lines[@]}"; do
if [[ "$cmd" == *[^' ']* ]]; then
# Note: $_ble_edit_exec_lastarg は $_ を設定するためのものである。
local prologue="ble-edit/exec:gexec/.eval-prologue '${cmd//$apos/$APOS}' \"\$_ble_edit_exec_lastarg\""
buff[${#buff[@]}]="builtin eval -- '${prologue//$apos/$APOS}"
buff[${#buff[@]}]="${cmd//$apos/$APOS}"
local prologue="ble-edit/exec:gexec/.eval-prologue '${cmd//$q/$Q}' \"\$_ble_edit_exec_lastarg\""
buff[${#buff[@]}]="builtin eval -- '${prologue//$q/$Q}"
buff[${#buff[@]}]="${cmd//$q/$Q}"
buff[${#buff[@]}]="{ ble-edit/exec:gexec/.save-last-arg; } &>/dev/null'" # Note: &>/dev/null は set -x 対策 #D0930
buff[${#buff[@]}]="{ ble-edit/exec:gexec/.eval-epilogue; } 3>&2 &>/dev/null"
((count++))
Expand Down

0 comments on commit 66ae615

Please sign in to comment.