Skip to content

Commit

Permalink
main: check "/dev/tty" on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 24, 2022
1 parent 4b1601d commit 711c69f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
15 changes: 9 additions & 6 deletions ble.pp
Expand Up @@ -137,13 +137,13 @@ function ble/debug/measure-set-timeformat {
return 1 2>/dev/null || exit 1
fi 3>&2 >/dev/null 2>&1 # set -x 対策 #D0930

if [[ ${BASH_EXECUTION_STRING+set} || ${IN_NIX_SHELL-} ]]; then
# builtin echo "ble.sh: ble.sh will not be activated for Bash started with '-c' option." >&3
# builtin echo "ble.sh: ble.sh will not be activated for nix-shell command execution." >&3
return 1 2>/dev/null || builtin exit 1
fi 3>&2 &>/dev/null # set -x 対策 #D0930

if [[ ! $_ble_init_command ]]; then
if [[ ${BASH_EXECUTION_STRING+set} || ${IN_NIX_SHELL-} ]]; then
# builtin echo "ble.sh: ble.sh will not be activated for Bash started with '-c' option." >&3
# builtin echo "ble.sh: ble.sh will not be activated for nix-shell command execution." >&3
return 1 2>/dev/null || builtin exit 1
fi

if ((BASH_SUBSHELL)); then
builtin echo "ble.sh: ble.sh cannot be loaded into a subshell." >&3
return 1 2>/dev/null || builtin exit 1
Expand All @@ -153,6 +153,9 @@ function ble/debug/measure-set-timeformat {
esac &&
builtin echo "ble.sh: This is not an interactive session." >&3 || ((1))
return 1 2>/dev/null || builtin exit 1
elif ! [[ -t 0 && -t 1 ]] && ! ((1)) >/dev/tty; then
builtin echo "ble.sh: cannot find a controlling TTY/PTY in this session." >&3
return 1 2>/dev/null || builtin exit 1
fi
fi 3>&2 &>/dev/null # set -x 対策 #D0930

Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -297,6 +297,7 @@
- util, edit, contrib: add support for `bash-preexec` (motivated by SuperSandro2000) `#D1744` e85f52c
- main: check `IN_NIX_SHELL` to inactivate ble.sh in nix-shell `#D1747` 0000000
- canvas: test the terminal for the sequence of clearing `DECSTBM` `#D1748` 0000000
- main: check `/dev/tty` on startup (reported by andychu) `#D1749` 0000000

## Internal changes and fixes

Expand Down
9 changes: 9 additions & 0 deletions note.txt
Expand Up @@ -5894,6 +5894,15 @@ bash_tips

2022-01-24

* main: /dev/tty が割り当てられていない時にも ble.sh は初期化しない [#D1749]
https://github.com/oilshell/oil/issues/1069#issuecomment-1017189089

Docker でロードしようとしている時に失敗する。

後、--test や --update 等の時には通常コマンドとして動作するので様々のチェッ
クを無視して良い。更に言うとそもそも /dev/tty から端末を取得しようとする事
自体が不要である。取り敢えずその様に実装した。

* canvas: wezterm で DECSTBM のクリアに失敗している [#D1748]
https://github.com/akinomyoga/ble.sh/issues/96#issuecomment-1019570983

Expand Down
33 changes: 20 additions & 13 deletions src/util.sh
Expand Up @@ -3171,20 +3171,27 @@ function ble/fd#close {
## @var _ble_util_fd_null
## @var _ble_util_fd_zero
## 既に定義されている場合は継承する
if [[ -t 0 ]]; then
ble/fd#alloc _ble_util_fd_stdin '<&0' base:overwrite:export
if [[ $_ble_init_command ]]; then
# コマンドモードで実行している時には標準ストリームはそのまま使う。
_ble_util_fd_stdin=0
_ble_util_fd_stdout=1
_ble_util_fd_stderr=2
else
ble/fd#alloc _ble_util_fd_stdin '< /dev/tty' base:inherit
fi
if [[ -t 1 ]]; then
ble/fd#alloc _ble_util_fd_stdout '>&1' base:overwrite:export
else
ble/fd#alloc _ble_util_fd_stdout '> /dev/tty' base:inherit
fi
if [[ -t 2 ]]; then
ble/fd#alloc _ble_util_fd_stderr '>&2' base:overwrite:export
else
ble/fd#alloc _ble_util_fd_stderr ">&$_ble_util_fd_stdout" base:inherit:share
if [[ -t 0 ]]; then
ble/fd#alloc _ble_util_fd_stdin '<&0' base:overwrite:export
else
ble/fd#alloc _ble_util_fd_stdin '< /dev/tty' base:inherit
fi
if [[ -t 1 ]]; then
ble/fd#alloc _ble_util_fd_stdout '>&1' base:overwrite:export
else
ble/fd#alloc _ble_util_fd_stdout '> /dev/tty' base:inherit
fi
if [[ -t 2 ]]; then
ble/fd#alloc _ble_util_fd_stderr '>&2' base:overwrite:export
else
ble/fd#alloc _ble_util_fd_stderr ">&$_ble_util_fd_stdout" base:inherit:share
fi
fi
ble/fd#alloc _ble_util_fd_null '<> /dev/null' base:inherit
[[ -c /dev/zero ]] &&
Expand Down

0 comments on commit 711c69f

Please sign in to comment.