Skip to content

Commit

Permalink
util (ble/util/openat): workaround a Bash 3.2 bug that causes problem…
Browse files Browse the repository at this point in the history
…s with C-d in nested shells
  • Loading branch information
akinomyoga committed Oct 17, 2018
1 parent 6ebcb35 commit d4b39b3
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
# memo
/memo/D0702.HISTFILE*
/memo/D0727.bind.*
/memo/D0857.unbind
/memo/D0857.bind
/memo/D0857.pipe
/memo/D0857.stderr
402 changes: 337 additions & 65 deletions memo.txt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions memo/D0727.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ function ble-decode/.hook {
unset POSIXLY_CORRECT
fi
}
source D0726.bind.source1 # $_ble_base_cache/ble-decode-bind.$_ble_bash.UTF-8.unbind
source D0726.bind.source2 # $_ble_base_cache/ble-decode-bind.$_ble_bash.UTF-8.bind
source D0727.bind.source1 # $_ble_base_cache/ble-decode-bind.$_ble_bash.UTF-8.unbind
source D0727.bind.source2 # $_ble_base_cache/ble-decode-bind.$_ble_bash.UTF-8.bind
11 changes: 11 additions & 0 deletions memo/D0857.bashrc0
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- mode: sh; mode: sh-bash -*-

IGNOREEOF=999

function ble-edit/bind/stdout/TRAPUSR1 {
echo TRAPUSR1
}

trap -- ble-edit/bind/stdout/TRAPUSR1 USR1

bind -x '"\C-t": echo hello C-t'
39 changes: 39 additions & 0 deletions memo/D0857.bashrc1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

HISTFILE=A.txt
source out/ble.sh --noattach

function ble/widget/test1 {
# これでは直らない
ble-edit/bind/stdout.on
ble-edit/bind/stdout.off
}
function ble/widget/test2 {
# これを実行すると直る
ble-edit/exec:gexec/.begin
ble-edit/exec:gexec/.end
}
function ble/widget/test3 {
# 実は term/leave,enter で直る
ble/term/leave
ble/term/enter
}
function ble/widget/test4 { # これで直る
ble/term/stty/enter
ble/term/stty/leave
}
function ble/widget/test5 { # 何とこれだけで直る…。
/usr/bin/stty &>/dev/null
}

ble-bind -f 'C-t' test5

function ble/widget/check1 {
stty -a >> stty.$_ble_bash.$SHLVL.txt
}

ble-bind -f 'C-u' check1

function ble/bin/stty { :; }

((_ble_bash)) && ble-attach
9 changes: 9 additions & 0 deletions memo/D0857.bashrc2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

HISTFILE=A.txt
IGNOREEOF=9999
source out/ble.sh --noattach

function new-bash-session { bash-3.2 --rcfile test2.bashrc; }
bind -x '"\C-t": new-bash-session'
bind -x '"\C-d": echo Hello C-d'
92 changes: 92 additions & 0 deletions memo/D0857.bashrc3
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

HISTFILE=A.txt
IGNOREEOF=9999

bleopt_openat_base=33
source out/ble.sh --noattach

# p 自分で設置したパイプを使ってみる
# f 自分で作った通常ファイルを使ってみる
# r 自分で stdout.off の内部でやっているリダイレクトを実行してみる
test_flags=

pipe1=
if [[ $test_flags == *p* ]]; then
ble/bin/rm -f memo/D0857.pipe
ble/bin/mkfifo memo/D0857.pipe
pipe_file=memo/D0857.pipe
{
{
while IFS= builtin read -r line; do
echo "[pipe] $line" >> memo/D0857.stderr
done < "$pipe_file"
} &>/dev/null & disown
} &>/dev/null
ble/util/openat pipe1 '> "$pipe_file"'
echo "pipe1=$pipe1" >&2
fi

function ble-decode/.hook {
if [[ :$test_flags: == *:[spfr]:* ]]; then
ble-edit/bind/stdout.on
else
ble-decode/PROLOGUE
fi

local byte
for byte; do
case $byte in
(20) echo C-t
#ls -la /proc/self/fd/
exit ;;
(4) echo C-d ;;
(*) echo $byte;;
esac
done

if [[ :$test_flags: == *:p:* ]]; then
echo "pipe: $pipe_file (fd: $pipe1)"
exec 1>>$_ble_edit_io_fname1 2>&$pipe1
elif [[ :$test_flags: == *:f:* ]]; then
exec 1>>$_ble_edit_io_fname1 2>>memo/D0857.stderr
elif [[ :$test_flags: == *:r:* ]]; then
echo "pipe: $_ble_edit_io_fname2.pipe (fd: $_ble_edit_fd_stderr_pipe)"
exec 1>>$_ble_edit_io_fname1 2>&$_ble_edit_fd_stderr_pipe
elif [[ :$test_flags: == *:s:* ]]; then
ble-edit/bind/stdout.off
else
ble-decode/EPILOGUE
fi
}

function ble-attach.1 {
# 取り敢えずプロンプトを表示する
ble/term/enter # 3ms (起動時のずれ防止の為 stty)
ble-edit/initialize # 3ms
ble-edit/attach # 0ms (_ble_edit_PS1 他の初期化)
ble/textarea#redraw # 37ms
ble/util/buffer.flush >&2

# keymap 初期化
IFS=$' \t\n'
ble-decode/initialize # 7ms
ble-decode/reset-default-keymap # 264ms (keymap/vi.sh)

ble-decode/attach
_ble_edit_detach_flag= # do not detach or exit

ble-edit/reset-history # 27s for bash-3.0

ble-edit/info/default
ble-edit/bind/.tail
}

function ble-attach.2 {
source memo/D0857.bind # $_ble_base_cache/ble-decode-bind.$_ble_bash.UTF-8.bind
_ble_term_state=internal
}

# ble-attach.2
# ble-attach.1
ble-attach
4 changes: 3 additions & 1 deletion src/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6601,10 +6601,12 @@ if [[ $bleopt_suppress_bash_output ]]; then
# stderr に bash が文句を吐くのでそれを捕まえて C-d が押されたと見做す。
if ((_ble_bash<40000)); then
function ble-edit/bind/stdout/TRAPUSR1 {
[[ $_ble_term_state == internal ]] || return

local IFS=$' \t\n'
local file=$_ble_edit_io_fname2.proc
if [[ -s $file ]]; then
local content
local content cmd
ble/util/readfile content "$file"
: >| "$file"
for cmd in $content; do
Expand Down
2 changes: 1 addition & 1 deletion src/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ else
function ble/util/openat {
local _fdvar=$1 _redirect=$2
(($_fdvar=_ble_util_openat_nextfd++))
builtin eval "exec ${!_fdvar}$_redirect"
builtin eval "exec ${!_fdvar}>&- ${!_fdvar}$_redirect"
}
fi

Expand Down

0 comments on commit d4b39b3

Please sign in to comment.