Skip to content

Commit

Permalink
main (adjust-bash-options): adjust LC_COLLATE=C
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jun 5, 2021
1 parent 8d6da16 commit e87ac21
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 3 deletions.
40 changes: 40 additions & 0 deletions ble.pp
Expand Up @@ -292,6 +292,16 @@ function ble/base/restore-builtin-wrappers {
fi
} 2>/dev/null # set -x 対策

# From src/util.sh
function ble/variable#copy-state {
local src=$1 dst=$2
if [[ ${!src+set} ]]; then
builtin eval -- "$dst=\${$src}"
else
builtin unset -v "$dst[0]" 2>/dev/null || builtin unset -v "$dst"
fi
}

: "${_ble_bash_options_adjusted=}"
function ble/base/adjust-bash-options {
[[ $_ble_bash_options_adjusted ]] && return 1 || ((1)) # set -e 対策
Expand All @@ -312,10 +322,40 @@ function ble/base/adjust-bash-options {
_ble_bash_expand_aliases=
shopt -q expand_aliases 2>/dev/null &&
_ble_bash_expand_aliases=1

# locale 待避
ble/variable#copy-state LC_ALL _ble_bash_LC_ALL
if [[ ${LC_ALL-} ]]; then
ble/variable#copy-state LC_CTYPE _ble_bash_LC_CTYPE
ble/variable#copy-state LC_MESSAGES _ble_bash_LC_MESSAGES
ble/variable#copy-state LC_NUMERIC _ble_bash_LC_NUMERIC
ble/variable#copy-state LC_TIME _ble_bash_LC_TIME
ble/variable#copy-state LANG _ble_bash_LANG
[[ ${LC_CTYPE-} ]] && LC_CTYPE=$LC_ALL
[[ ${LC_MESSAGES-} ]] && LC_MESSAGES=$LC_ALL
[[ ${LC_NUMERIC-} ]] && LC_NUMERIC=$LC_ALL
[[ ${LC_TIME-} ]] && LC_TIME=$LC_ALL
LANG=$LC_ALL
LC_ALL=
fi
ble/variable#copy-state LC_COLLATE _ble_bash_LC_COLLATE
LC_COLLATE=C
} 2>/dev/null # set -x 対策 #D0930
function ble/base/restore-bash-options {
[[ $_ble_bash_options_adjusted ]] || return 1
_ble_bash_options_adjusted=

# locale 復元
ble/variable#copy-state _ble_bash_LC_COLLATE LC_COLLATE
if [[ $_ble_bash_LC_ALL ]]; then
ble/variable#copy-state _ble_bash_LC_CTYPE LC_CTYPE
ble/variable#copy-state _ble_bash_LC_MESSAGES LC_MESSAGES
ble/variable#copy-state _ble_bash_LC_NUMERIC LC_NUMERIC
ble/variable#copy-state _ble_bash_LC_TIME LC_TIME
ble/variable#copy-state _ble_bash_LANG LANG
fi
ble/variable#copy-state _ble_bash_LC_ALL LC_ALL

[[ $_ble_bash_nocasematch ]] && shopt -s nocasematch
[[ $_ble_bash_setu && ! -o nounset ]] && set -u
[[ $_ble_bash_setv && ! -o verbose ]] && set -v
Expand Down
3 changes: 2 additions & 1 deletion memo/ChangeLog.md
Expand Up @@ -127,7 +127,8 @@
- main: fix the workaround for `set -u` `#D1575` 76073a9
- history: fix the workaround for bash-3.0 bug of reducing histories `#D1576` 15c9133
- syntax: fix a bug that argument completion is attempted in nested commands (reported by huresche) `#D1579` 301d40f
- edit (brackated-paste): fix incomplete `CR => LF` conversion (reported by alborotogarcia) `#D1587` 0000000
- edit (brackated-paste): fix incomplete `CR => LF` conversion (reported by alborotogarcia) `#D1587` 8d6da16
- main (adjust-bash-options): adjust `LC_COLLATE=C` `#D1588` 0000000

## Compatibility

Expand Down
209 changes: 209 additions & 0 deletions memo/D1588.stub-LC_COLLATE-1.patch
@@ -0,0 +1,209 @@
From 7c28903712ecc6c2709f78f94743a2049f5edb8e Mon Sep 17 00:00:00 2001
From: Koichi Murase <myoga.murase@gmail.com>
Date: Sat, 5 Jun 2021 06:44:52 +0900
Subject: [PATCH] stub: work around LC_COLLATE (1)

---
lib/core-syntax.sh | 39 ++++++++++++++++++++++-----------------
make_command.sh | 9 +++++++++
2 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/lib/core-syntax.sh b/lib/core-syntax.sh
index e6c5167..4ac774b 100644
--- a/lib/core-syntax.sh
+++ b/lib/core-syntax.sh
@@ -2057,8 +2057,8 @@ function ble/syntax:bash/check-quotes {
((_ble_syntax_attr[i]=aqdel,i+=2))
local t=${BASH_REMATCH[1]} rematch4=${BASH_REMATCH[4]}

- local rex='\\[abefnrtvE"'\''\?]|\\[0-7]{1,3}|\\c.|\\x[0-9a-fA-F]{1,2}'
- ((_ble_bash>=40200)) && rex=$rex'|\\u[0-9a-fA-F]{1,4}|\\U[0-9a-fA-F]{1,8}'
+ local rex='\\[abefnrtvE"'\''\?]|\\[0-7]{1,3}|\\c.|\\x[0-9a-fA-F]{1,2}' # #D1588 WA LC_COLLATE ok
+ ((_ble_bash>=40200)) && rex=$rex'|\\u[0-9a-fA-F]{1,4}|\\U[0-9a-fA-F]{1,8}' # #D1588 WA LC_COLLATE ok
local rex='^([^'\''\]*)('$rex'|(\\.))'
while [[ $t =~ $rex ]]; do
local m1=${BASH_REMATCH[1]} m2=${BASH_REMATCH[2]}
@@ -2801,10 +2801,10 @@ function ble/syntax:bash/ctx-expr {
local ret; ble/syntax/highlight/vartype "$BASH_REMATCH" readvar:expr
((_ble_syntax_attr[i]=ret,i+=${#rematch}))
return 0
- elif rex='^0[xX][0-9a-fA-F]*|^[0-9]+(#[0-9a-zA-Z@_]*)?'; [[ $tail =~ $rex ]]; then
+ elif rex='^0[xX][0-9a-fA-F]*|^[0-9]+(#[0-9a-zA-Z@_]*)?'; [[ $tail =~ $rex ]]; then # #D1588 WA LC_COLLATE
((_ble_syntax_attr[i]=ATTR_VAR_NUMBER,i+=${#BASH_REMATCH}))
return 0
- elif ble/syntax:bash/check-plain-with-escape "[^${_ble_syntax_bash_chars[ctx]}a-zA-Z_0-9]+" 1; then
+ elif ble/syntax:bash/check-plain-with-escape "[^${_ble_syntax_bash_chars[ctx]}a-zA-Z_0-9]+" 1; then # #D1588 WA LC_COLLATE
return 0
elif [[ $tail == ['][()}']* ]]; then
local char=${tail::1} ntype
@@ -2856,7 +2856,7 @@ function ble/syntax:bash/ctx-expr {
function ble/syntax:bash/check-brace-expansion {
[[ $tail == '{'* ]] || return 1

- local rex='^\{[-+0-9a-zA-Z.]*(\}?)'
+ local rex='^\{[-+0-9a-zA-Z.]*(\}?)' # #D1588 WA LC_COLLATE
[[ $tail =~ $rex ]]
local str=$BASH_REMATCH

@@ -2897,7 +2897,7 @@ function ble/syntax:bash/check-brace-expansion {
ctx=${_ble_syntax_bash_command_IsAssign[ctx]}

# {a..b..c} の形式のブレース展開
- if rex='^\{(([-+]?[0-9]+)\.\.[-+]?[0-9]+|[a-zA-Z]\.\.[a-zA-Z])(\.\.[-+]?[0-9]+)?\}$'; [[ $str =~ $rex ]]; then
+ if rex='^\{(([-+]?[0-9]+)\.\.[-+]?[0-9]+|[a-zA-Z]\.\.[a-zA-Z])(\.\.[-+]?[0-9]+)?\}$'; [[ $str =~ $rex ]]; then # #D1588 WA LC_COLLATE
if [[ $force_attr ]]; then
((_ble_syntax_attr[i]=force_attr,i+=${#str}))
else
@@ -3139,7 +3139,7 @@ function ble/syntax:bash/check-variable-assignment {
else
suffix="$suffix|\["
fi
- local rex_assign="^([a-zA-Z_][a-zA-Z_0-9]*)($suffix)"
+ local rex_assign="^([a-zA-Z_][a-zA-Z_0-9]*)($suffix)" # #D1588 WA LC_COLLATE
[[ $tail =~ $rex_assign ]] || return 1
local rematch=$BASH_REMATCH
local rematch1=${BASH_REMATCH[1]} # for bash-3.1 ${#arr[n]} bug
@@ -3333,7 +3333,7 @@ function ble/syntax:bash/ctx-coproc/.is-next-compound {
local is_compound=
if [[ $tail == '('* ]]; then
is_compound=1
- elif rex='^[a-z]+|^\[\[?|^[{}!]'; [[ $tail =~ $rex ]]; then
+ elif rex='^[a-z]+|^\[\[?|^[{}!]'; [[ $tail =~ $rex ]]; then # #D1588 WA LC_COLLATE
local rematch=$BASH_REMATCH

((p+=${#rematch}))
@@ -3367,7 +3367,7 @@ function ble/syntax:bash/ctx-coproc/check-word-end {
local word=${text:wbegin:wlen}
local wt=$wtype

- if local rex='^[_a-zA-Z0-9]+$'; [[ $word =~ $rex ]]; then
+ if local rex='^[_a-zA-Z0-9]+$'; [[ $word =~ $rex ]]; then # #D1588 WA LC_COLLATE
if ble/syntax:bash/ctx-coproc/.is-next-compound; then
# Note: [_a-zA-Z0-9]+ は一回の読み取りの筈なので、
# 此処で遡って代入しても問題ない筈。
@@ -3911,7 +3911,7 @@ function ble/syntax:bash/ctx-command {
local attr=$ctx
if ((attr==CTX_FARGI1)); then
# for var in ... の var の部分は変数名をチェックして着色
- if rex='^[a-zA-Z_][a-zA-Z_0-9]*'; ((i==wbegin)) && [[ $rematch =~ $rex ]]; then
+ if rex='^[a-zA-Z_][a-zA-Z_0-9]*'; ((i==wbegin)) && [[ $rematch =~ $rex ]]; then # #D1588 WA LC_COLLATE
local ret; ble/syntax/highlight/vartype "$BASH_REMATCH"; attr=$ret
else
attr=$ATTR_ERR
@@ -4653,11 +4653,11 @@ function ble/syntax:bash/find-rhs {

local rex=
if ((wtype==ATTR_VAR)); then
- rex='^[a-zA-Z0-9_]+(\+?=|\[)'
+ rex='^[a-zA-Z0-9_]+(\+?=|\[)' # #D1588 WA LC_COLLATE
elif ((wtype==CTX_VALI)); then
if [[ :$opts: == *:element-assignment:* ]]; then
# 配列要素に対しても変数代入の形式を許す
- rex='^[a-zA-Z0-9_]+(\+?=|\[)|^(\[)'
+ rex='^[a-zA-Z0-9_]+(\+?=|\[)|^(\[)' # #D1588 WA LC_COLLATE
else
rex='^(\[)'
fi
@@ -4985,6 +4985,7 @@ function ble/syntax/parse {
((end==beg&&end0==beg&&_ble_syntax_dbeg<0)) && return 0

local IFS=$_ble_term_IFS
+ local LC_ALL= LC_COLLATE=C 2>/dev/null # #D1588 WA regex range expression

local shift=$((end-end0))
#%if !release
@@ -5116,6 +5117,8 @@ function ble/syntax/parse {
'((${#_ble_syntax_stat[@]}==iN+1))' \
"unexpected array length #arr=${#_ble_syntax_stat[@]} (expected to be $iN), #proto=${#_ble_array_prototype[@]} should be >= $iN"
#%end
+
+ ble/util/unlocal LC_ALL LC_COLLATE 2>/dev/null # #D1588 WA regex range expression
}

## @fn ble/syntax/highlight text [lang]
@@ -5199,7 +5202,7 @@ function ble/syntax/completion-context/.add {
## @fn ble/syntax/completion-context/.check/parameter-expansion
## @var[in] text istat index ctx
function ble/syntax/completion-context/.check/parameter-expansion {
- local rex_paramx='^(\$(\{[!#]?)?)([a-zA-Z_][a-zA-Z_0-9]*)?$'
+ local rex_paramx='^(\$(\{[!#]?)?)([a-zA-Z_][a-zA-Z_0-9]*)?$' # #D1588 WA LC_COLLATE
if [[ ${text:istat:index-istat} =~ $rex_paramx ]]; then
local rematch1=${BASH_REMATCH[1]}
local source=variable
@@ -5314,7 +5317,7 @@ function ble/syntax/completion-context/.check-prefix/ctx:next-command {
ble/syntax/completion-context/.add command "$istat"

# 変数・代入のチェック
- if local rex='^[a-zA-Z_][a-zA-Z_0-9]*(\+?=)?$' && [[ $word =~ $rex ]]; then
+ if local rex='^[a-zA-Z_][a-zA-Z_0-9]*(\+?=)?$' && [[ $word =~ $rex ]]; then # #D1588 WA LC_COLLATE
if [[ $word == *= ]]; then
if ((_ble_bash>=30100)) || [[ $word != *+= ]]; then
# VAR=<argument>: 現在位置から argument 候補を生成する
@@ -5515,7 +5518,7 @@ function ble/syntax/completion-context/.check-prefix/ctx:rhs {
ble/syntax/completion-context/.check/parameter-expansion
if ((wlen>=0)); then
local p=$wbeg
- local rex='^[a-zA-Z0-9_]+(\+?=|\[)'
+ local rex='^[a-zA-Z0-9_]+(\+?=|\[)' # #D1588 WA LC_COLLATE
((ctx==CTX_VALR)) && rex='^(\[)'
if [[ ${text:p:index-p} =~ $rex ]]; then
if [[ ${BASH_REMATCH[1]} == '[' ]]; then
@@ -5568,7 +5571,7 @@ function ble/syntax/completion-context/.check-prefix/ctx:param {
## 数式中の変数名を補完する文脈
_ble_syntax_bash_complete_check_prefix[CTX_EXPR]=expr
function ble/syntax/completion-context/.check-prefix/ctx:expr {
- local tail=${text:istat:index-istat} rex='[a-zA-Z_]+$'
+ local tail=${text:istat:index-istat} rex='[a-zA-Z_]+$' # #D1588 WA LC_COLLATE
if [[ $tail =~ $rex ]]; then
local p=$((index-${#BASH_REMATCH}))
ble/syntax/completion-context/.add variable:a "$p"
@@ -5649,7 +5652,7 @@ function ble/syntax/completion-context/.search-last-istat {
## @var[in] index
## @var[out] sources
function ble/syntax/completion-context/.check-prefix {
- local rex_param='^[a-zA-Z_][a-zA-Z_0-9]*$'
+ local rex_param='^[a-zA-Z_][a-zA-Z_0-9]*$' # #D1588 WA LC_COLLATE
local from=${1:-$((index-1))}

local ret
@@ -5737,8 +5740,10 @@ function ble/syntax/completion-context/generate {
sources=()
((index<0&&(index=0)))

+ local LC_ALL= LC_COLLATE=C 2>/dev/null # #D1588 WA regex-ranges
ble/syntax/completion-context/.check-prefix
ble/syntax/completion-context/.check-here
+ ble/util/unlocal LC_ALL LC_COLLATE 2>/dev/null # #D1588 WA regex-ranges
}

#------------------------------------------------------------------------------
diff --git a/make_command.sh b/make_command.sh
index 1a1a629..75828f3 100755
--- a/make_command.sh
+++ b/make_command.sh
@@ -366,6 +366,14 @@ function sub:scan/mistake-_ble_bash {
grc '\(\(.*\b_ble_base\b.*\)\)'
}

+function sub:scan/WA-regex-range {
+ echo "--- $FUNCNAME ---"
+ grc --color -i 'a-fA-F|a-zA-Z' --exclude=ext |
+ sed -E 'h;s/'"$esc"'//g;s/^[^:]*:[0-9]+:[[:space:]]*//
+ \Z#D1588Zd
+ g'
+}
+
function sub:scan {
if ! type grc >/dev/null; then
echo 'blesh check: grc not found. grc can be found in github.com:akinomyoga/mshex.git/' >&2
@@ -447,6 +455,7 @@ function sub:scan {
sub:scan/eval-literal
sub:scan/WA-localvar_inherit
sub:scan/mistake-_ble_bash
+ sub:scan/WA-regex-range

sub:scan/memo-numbering
}
--
2.21.3

0 comments on commit e87ac21

Please sign in to comment.