Skip to content

Commit

Permalink
global: fix bugs of the adjustments for bash-5.2 "patsub_replacement"…
Browse files Browse the repository at this point in the history
… (fixup 4590997)

* global: work around compat42 quoting of "${v/pat/"$rep"}" #D1751
* prompt: fix a bug of ble/prompt/print redundantly quoting '$' #D1752
* global: identify bash-4.2 bug that internal quoting of ${v/%$empty/"$rep"} remains #D1753
* global: work around "shopt -s compat42" #D1754
  • Loading branch information
akinomyoga committed Jul 6, 2022
1 parent e6c2855 commit 1f254b5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
9 changes: 6 additions & 3 deletions keymap/vi_test.sh
Expand Up @@ -39,10 +39,13 @@ function ble/keymap:vi_test/check {
((ntest++,nsuccess++))
else
((ntest++))
local esc_in=${in//$nl/"$NL"}
local esc_fin=${fin//$nl/"$NL"}
local esc_str=${_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"}\""
ble/util/print " initial = \"$i:$esc_in\""
ble/util/print " expected = \"$f:$esc_fin\""
ble/util/print " result = \"$_ble_edit_ind:$esc_str\""
fi >&2

# restore states
Expand Down
20 changes: 10 additions & 10 deletions lib/core-syntax.sh
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"} # WA #D1751 safe
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
5 changes: 5 additions & 0 deletions make_command.sh
Expand Up @@ -169,6 +169,11 @@ function sub:check/bash502-patsub_replacement {
\Z#D1738Zd
\Z\$\{_ble_edit_str//\$'\''\\n'\''/\$'\''\\n'\''"\$comment_begin"\}Zd # edit.sh
g'

grc --color '"[^"]*\$\{[[:alnum:]_]+(\[[^][]*\])?//?([^{}]|\{[^{}]*\})+/[^{}"'\'']*"[^"]*([&$]|\\)' --exclude=./test |
sed -E 'h;s/'"$esc"'//g;s/^[^:]*:[0-9]+:[[:space:]]*//
\Z#D1751Zd
g'
}

function sub:check/gawk402bug-regex-check {
Expand Down
2 changes: 1 addition & 1 deletion src/decode.sh
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"}\" # WA #D1751 checked
LC_ALL=C ble/bin/awk -v q="$q" '
BEGIN {
Q = '"$QUOT_Q"';
Expand Down
11 changes: 3 additions & 8 deletions src/edit.sh
Expand Up @@ -263,14 +263,9 @@ function ble-edit/prompt/.load {
## @var[out] DRAW_BUFF[]
## 出力先の配列です。
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"}
fi
ble/canvas/put.draw "$text"
local ret=$1
ble/string#escape-characters "$ret" '\$"`'
ble/canvas/put.draw "$ret"
}

## 関数 ble-edit/prompt/process-prompt-string prompt_string
Expand Down
28 changes: 17 additions & 11 deletions src/util.sh
Expand Up @@ -4,7 +4,7 @@
#------------------------------------------------------------------------------
# ble.sh options

## 関数 bleopt args...
## @fn bleopt args...
## @params[in] args
## args は以下の内の何れかの形式を持つ。
##
Expand Down Expand Up @@ -402,8 +402,10 @@ function ble/array#last-index {
function ble/dense-array#fill-range {
ble/array#reserve-prototype $(($3-$2))
local _ble_script='
local -a sARR; sARR=("${_ble_array_prototype[@]::$3-$2}")
ARR=("${ARR[@]::$2}" "${sARR[@]/#/"$4"}" "${ARR[@]:$3}")' # WA #D1570 checked
local -a sARR; sARR=("${_ble_array_prototype[@]::$3-$2}")
ARR=("${ARR[@]::$2}" "${sARR[@]/#/$4}" "${ARR[@]:$3}")' # WA #D1570 #D1738 checked
((_ble_bash>=40300)) && ! shopt -q compat42 &&
_ble_script=${_ble_script//'$4'/'"$4"'}
builtin eval -- "${_ble_script//ARR/$1}"
}

Expand All @@ -422,7 +424,7 @@ function ble/string#reserve-prototype {
function ble/string#repeat {
ble/string#reserve-prototype "$2"
ret=${_ble_string_prototype::$2}
ret="${ret// /"$1"}"
ret=${ret// /"$1"}
}

## 関数 ble/string#common-prefix a b
Expand Down Expand Up @@ -731,7 +733,7 @@ function ble/string#escape-for-bash-single-quote {
function ble/string#escape-for-bash-double-quote {
ble/string#escape-characters "$1" '\"$`'
local a b
a='!' b='"\!"' ret=${ret//"$a"/"$b"}
a='!' b='"\!"' ret=${ret//"$a"/"$b"} # WA #D1751 checked
}
function ble/string#escape-for-bash-escape-string {
ble/string#escape-characters "$1" $'\\\a\b\e\f\n\r\t\v'\' '\abefnrtv'\'
Expand All @@ -758,7 +760,7 @@ function ble/string#escape-for-bash-specialchars {
if [[ $ret == *[$']\n\t']* ]]; then
local a b
a=']' b=\\$a ret=${ret//"$a"/"$b"}
a=$'\n' b="\$'\n'" ret=${ret//"$a"/"$b"}
a=$'\n' b="\$'\n'" ret=${ret//"$a"/"$b"} # WA #D1751 checked
a=$'\t' b=$'\\\t' ret=${ret//"$a"/"$b"}
fi

Expand Down Expand Up @@ -832,7 +834,7 @@ function ble/string#escape-for-bash-specialchars-in-brace {
if [[ $ret == *[$']\n\t']* ]]; then
local a b
a=']' b=\\$a ret=${ret//"$a"/"$b"}
a=$'\n' b="\$'\n'" ret=${ret//"$a"/"$b"}
a=$'\n' b="\$'\n'" ret=${ret//"$a"/"$b"} # WA #D1751 checked
a=$'\t' b=$' \t' ret=${ret//"$a"/"$b"}
fi
}
Expand Down Expand Up @@ -923,23 +925,26 @@ function ble/util/substr {

function ble/path#add {
local _ble_local_script='opts=$opts${opts:+:}$2'
builtin eval -- "${_ble_local_script//opts/"$1"}"
_ble_local_script=${_ble_local_script//opts/"$1"}
builtin eval -- "$_ble_local_script"
}
function ble/path#remove {
[[ $2 ]] || return
local _ble_local_script='
opts=:${opts//:/::}:
opts=${opts//:"$2":}
opts=${opts//::/:} opts=${opts#:} opts=${opts%:}'
builtin eval -- "${_ble_local_script//opts/"$1"}"
_ble_local_script=${_ble_local_script//opts/"$1"}
builtin eval -- "$_ble_local_script"
}
function ble/path#remove-glob {
[[ $2 ]] || return
local _ble_local_script='
opts=:${opts//:/::}:
opts=${opts//:$2:}
opts=${opts//::/:} opts=${opts#:} opts=${opts%:}'
builtin eval -- "${_ble_local_script//opts/"$1"}"
_ble_local_script=${_ble_local_script//opts/"$1"}
builtin eval -- "$_ble_local_script"
}

if ((_ble_bash>=40000)); then
Expand Down Expand Up @@ -3275,7 +3280,8 @@ function ble/term/cursor-state/.update {
local state=$(($1))
[[ $_ble_term_cursor_current == "$state" ]] && return

ble/util/buffer "${_ble_term_Ss//@1/"$state"}"
local esc=${_ble_term_Ss//@1/"$state"}
ble/util/buffer "$esc"

_ble_term_cursor_current=$state
}
Expand Down

0 comments on commit 1f254b5

Please sign in to comment.