Skip to content

Commit

Permalink
util: fix bugs in conversions from "'" to "'\''"
Browse files Browse the repository at this point in the history
- util (ble/builtin/trap): fix a bug that "'\''" are replaced by "'" (in opposite direction)
- util (ble/util/print-global-definitions): fix a bug that ' are replaced with "/'\''"
  • Loading branch information
akinomyoga committed Jan 20, 2022
1 parent 4590997 commit 6d15782
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -221,6 +221,7 @@
- util (`ble/function`): work around `shopt -u extglob` `#D1725` 952c388
- syntax: fix uninitialized syntax-highlighting in bash-3.2 `#D1731` 0000000
- make: fix a bug that config update messages are removed on install `#D1736` 0000000
- util: fix bugs in conversions from `'` to `\''` `#D1739` 0000000

## Documentation

Expand Down
10 changes: 5 additions & 5 deletions lib/init-bind.sh
Expand Up @@ -14,14 +14,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 @@ -33,7 +33,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
3 changes: 3 additions & 0 deletions lib/test-util.sh
Expand Up @@ -1505,6 +1505,9 @@ ble/test ble/util/is-running-in-subshell exit=1
ble/test 'status v2' stdout='1:(3)'
}
f1

value="hello 'world'"
ble/test 'ble/util/print-global-definitions value' stdout="declare value='hello '\''world'\'''"
)

# ble/util/has-glob-pattern
Expand Down
6 changes: 3 additions & 3 deletions make_command.sh
Expand Up @@ -273,7 +273,7 @@ function sub:generate-grapheme-cluster-table {
BEGIN {
#ITEMS_PER_LINE = 6;
MAX_COLUMNS = 160;
apos = "'\''";
Q = "'\''";
out = " ";
out_length = 3;
out_count = 0;
Expand Down Expand Up @@ -306,7 +306,7 @@ function sub:generate-grapheme-cluster-table {
} else
print "GraphemeBreakTest.txt: Unexpected line (" $0 ")" >"/dev/stderr";
ent = ans ":" apos str apos;
ent = ans ":" Q str Q;
entlen = length(ent) + 1
if (out_length + entlen >= MAX_COLUMNS) out_flush();
Expand Down Expand Up @@ -1310,7 +1310,7 @@ function sub:scan {
#sub:scan/assign
sub:scan/builtin trap |
sed -E 'h;s/'"$esc"'//g;s/^[^:]*:[0-9]+:[[:space:]]*//
\Zble/util/print "trap -- '\''\$\{h//\$Q/\$q}'\'' \$nZd
\Zble/util/print "trap -- '\''\$\{h//\$q/\$Q}'\'' \$nZd
\Zline = "bind"Zd
\Zlocal trap_command="trap -- Zd
\Zlocal trap$Zd
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
9 changes: 9 additions & 0 deletions note.txt
Expand Up @@ -5825,6 +5825,15 @@ bash_tips

2022-01-20

* bash-5.2 shopt patsub_replacement 対策 [#D1739]

これは #D1738 の一環として発見されたバグであるが bash-0.3 も修正しなければ
ならないので、独立した項目にする事にする。

- util (ble/builtin/trap): $Q から $q へ逆方向で変換されていた。
- util (ble/util/print-global-definitions): ${v//$q//$Q} の様になっていた為
に置換後に余分な / が混入していた。

* 2022-01-13 bash-5.2 shopt patsub_replacement 対策 [#D1738]

% 取り敢えず危なそうな箇所を抽出しておく。うーん。やはり可也面倒である。
Expand Down
4 changes: 2 additions & 2 deletions src/decode.sh
Expand Up @@ -2592,8 +2592,8 @@ function ble/decode/cmap/.generate-binder-template {
}

function ble/decode/cmap/.emit-bindx {
local ap="'" eap="'\\''"
ble/util/print "builtin bind -x '\"${1//$ap/$eap}\":ble-decode/.hook $2; builtin eval -- \"\$_ble_decode_bind_hook\"'"
local q="'" Q="'\''"
ble/util/print "builtin bind -x '\"${1//$q/$Q}\":ble-decode/.hook $2; builtin eval -- \"\$_ble_decode_bind_hook\"'"
}
function ble/decode/cmap/.emit-bindr {
ble/util/print "builtin bind -r \"$1\""
Expand Down
8 changes: 4 additions & 4 deletions src/edit.sh
Expand Up @@ -5582,17 +5582,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/.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/.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/.epilogue; } 3>&2 &>/dev/null"
((count++))
Expand Down
4 changes: 2 additions & 2 deletions src/util.sh
Expand Up @@ -2010,7 +2010,7 @@ function ble/builtin/trap {
if [[ ${_ble_builtin_trap_handlers[index]+set} ]]; then
local h=${_ble_builtin_trap_handlers[index]}
local n=${_ble_builtin_trap_signames[index]}
ble/util/print "trap -- '${h//$Q/$q}' $n"
ble/util/print "trap -- '${h//$q/$Q}' $n"
fi
done
else
Expand Down Expand Up @@ -3277,7 +3277,7 @@ function ble/util/print-global-definitions/.save-decl {
fi
else
__ble_decl=${!__ble_name}
__ble_decl="declare $__ble_name='${__ble_decl//$__ble_q//$__ble_Q}'"
__ble_decl="declare $__ble_name='${__ble_decl//$__ble_q/$__ble_Q}'"
fi
}
## @fn ble/util/print-global-definitions varnames...
Expand Down

0 comments on commit 6d15782

Please sign in to comment.