From 65b9c69bd86a167d90ac15033adcf8d1d43a7400 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 8 Mar 2022 11:05:02 +0530 Subject: [PATCH] Use python in preference to perl I know how to make python code robust, no clue about perl. --- shell-integration/ssh/bootstrap.sh | 42 +++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/shell-integration/ssh/bootstrap.sh b/shell-integration/ssh/bootstrap.sh index c0cfd61c5..81779c37d 100644 --- a/shell-integration/ssh/bootstrap.sh +++ b/shell-integration/ssh/bootstrap.sh @@ -42,13 +42,13 @@ if command -v base64 > /dev/null 2> /dev/null; then elif command -v b64encode > /dev/null 2> /dev/null; then base64_encode() { command b64encode - | command sed '1d;$d' | command tr -d \\n\\r; } base64_decode() { command fold -w 76 | command b64decode -r; } -elif detect_perl; then - base64_encode() { command "$perl" -MMIME::Base64 -0777 -ne 'print encode_base64($_)'; } - base64_decode() { command "$perl" -MMIME::Base64 -ne 'print decode_base64($_)'; } elif detect_python; then pybase64() { command "$python" -c "import sys, base64; getattr(sys.stdout, 'buffer', sys.stdout).write(base64.standard_b64$1(getattr(sys.stdin, 'buffer', sys.stdin).read()))"; } base64_encode() { pybase64 "encode"; } base64_decode() { pybase64 "decode"; } +elif detect_perl; then + base64_encode() { command "$perl" -MMIME::Base64 -0777 -ne 'print encode_base64($_)'; } + base64_decode() { command "$perl" -MMIME::Base64 -ne 'print decode_base64($_)'; } else die "base64 executable not present on remote host, ssh kitten cannot function." fi @@ -108,19 +108,41 @@ else return $? } - if detect_perl; then - read_n_bytes_from_tty() { - command "$perl" -MList::Util=min -e \ -'open(my $fh," /dev/null - } - elif [ "$(printf "%s" "test" | command head -c 3 2> /dev/null)" = "tes" ]; then + if [ "$(printf "%s" "test" | command head -c 3 2> /dev/null)" = "tes" ]; then # using dd with bs=1 is very slow, so use head. On non GNU coreutils head # does not limit itself to reading -c bytes only from the pipe so we can potentially lose # some trailing data, for instance if the user starts typing. Cant be helped. read_n_bytes_from_tty() { command head -c "$1" < /dev/tty } + elif detect_python; then + read_n_bytes_from_tty() { + command "$python" "-c" " +import sys, os, errno +def eintr_retry(func, *args): + while True: + try: + return func(*args) + except EnvironmentError as e: + if e.errno != errno.EINTR: + raise +n = $1 +in_fd = sys.stdin.fileno() +out_fd = sys.stdout.fileno() +while n > 0: + d = memoryview(eintr_retry(os.read, in_fd, n)) + n -= len(d) + while d: + nw = eintr_retry(os.write, out_fd, d) + d = d[nw:] +" < /dev/tty + } + elif detect_perl; then + read_n_bytes_from_tty() { + command "$perl" -MList::Util=min -e \ +'open(my $fh," /dev/null + } else read_n_bytes_from_tty() { command dd bs=1 count="$1" < /dev/tty 2> /dev/null