diff options
-rw-r--r-- | src/rpc.bash | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/src/rpc.bash b/src/rpc.bash index eb41538..44f7020 100644 --- a/src/rpc.bash +++ b/src/rpc.bash | |||
@@ -50,22 +50,28 @@ recursive_dependencies() | |||
50 | 50 | ||
51 | # Input: $BASH_RPC_REMOTE_DEST - hostname passed to ssh (uses ssh host aliases) | 51 | # Input: $BASH_RPC_REMOTE_DEST - hostname passed to ssh (uses ssh host aliases) |
52 | # Input: $BASH_RPC_SSH_OPTIONS - option arguments passed to ssh (they precede '--') | 52 | # Input: $BASH_RPC_SSH_OPTIONS - option arguments passed to ssh (they precede '--') |
53 | # Input: $1 - bash shell script source code to run remotely | 53 | # Input; $BASH_RPC_STDIN_IS_TTY - controls whether to allocate ssh pty. Doesn't work. |
54 | # Input: $2, $3, ... - command line arguments passed to the remote bash shell | 54 | # Input: $1 - shell script source code to run in the remote bash |
55 | # Input: stdin, stdout, stderr: passed to ssh | 55 | # Input: $2, $3, ... - command line arguments passed to the remote bash |
56 | # Input: stdin, stdout, stderr: passed to the remote bash over ssh | ||
57 | # Input: "$0" - copied to remote bash's $0 | ||
58 | # Input: "$SHELL" - remote shell to launch (better be bash) | ||
56 | bash_rpc_remote_run_script() | 59 | bash_rpc_remote_run_script() |
57 | { | 60 | { |
58 | ( | 61 | local script="$1" |
59 | local script="$1" | 62 | shift |
60 | shift | 63 | exec ssh \ |
61 | set -- "$SHELL" -c "${script@Q}" "$0" "${@@Q}" | 64 | "${BASH_RPC_SSH_OPTIONS[@]}" -- \ |
62 | set -x | 65 | "${BASH_RPC_REMOTE_DEST:-localhost}" \ |
63 | exec ssh \ | 66 | "$SHELL" -c "${script@Q}" "$0" "${@@Q}" |
64 | "${BASH_RPC_SSH_OPTIONS[@]}" \ | 67 | } |
65 | -- \ | 68 | |
66 | "${BASH_RPC_REMOTE_DEST:-localhost}" \ | 69 | with_ssh_option() |
67 | "$@" | 70 | { |
68 | ) | 71 | local BASH_RPC_SSH_OPTIONS |
72 | BASH_RPC_SSH_OPTIONS+=("$1") | ||
73 | shift | ||
74 | "$@" | ||
69 | } | 75 | } |
70 | 76 | ||
71 | # Preserves the rest of stdin for the command. | 77 | # Preserves the rest of stdin for the command. |
@@ -73,14 +79,20 @@ bash_rpc_eval_stdin() | |||
73 | { | 79 | { |
74 | [ "$1" -gt 0 ] || return | 80 | [ "$1" -gt 0 ] || return |
75 | read -N"$1" -r || return | 81 | read -N"$1" -r || return |
76 | shift # prepare "$@" for script execution context | 82 | |
77 | source <(printf '%s' "$REPLY" | tr -d '\015') | 83 | # Prepare "$@" for script execution context. |
84 | shift | ||
85 | |||
86 | # Even unset REPLY to leave pristine environment. | ||
87 | eval "unset REPLY; $(cat <<< "$REPLY")" | ||
78 | } | 88 | } |
79 | 89 | ||
80 | remote_run_function_simple() | 90 | remote_run_function_simple() |
81 | { | 91 | { |
82 | script_source=$(declare -f "$1" && printf '"$@";\n') | 92 | script_source=$(declare -f "$1" && printf '"$@";\n') |
83 | bash_rpc_remote_run_script "$script_source" "$@" | 93 | bash_rpc_remote_run_script \ |
94 | "$script_source" \ | ||
95 | "$@" | ||
84 | } | 96 | } |
85 | 97 | ||
86 | remote_run_function() | 98 | remote_run_function() |
@@ -91,15 +103,16 @@ remote_run_function() | |||
91 | declare -f $funcs | 103 | declare -f $funcs |
92 | printf '"$@"\n' "$main" | 104 | printf '"$@"\n' "$main" |
93 | ) | 105 | ) |
94 | BASH_RPC_SSH_OPTIONS=(-tt) | 106 | |
107 | local BASH_RPC_STDIN_IS_TTY=$([ ! -t 0 ] || echo y) | ||
95 | { | 108 | { |
96 | printf '%s' "$stage2_source" | 109 | printf '%s' "$stage2_source" |
97 | if [ -t 0 ] | 110 | cat |
98 | then | 111 | } | |
99 | socat stdio PTY | 112 | ${BASH_RPC_STDIN_IS_TTY:+ with_ssh_option '-tt'} \ |
100 | else | 113 | remote_run_function_simple \ |
101 | cat | 114 | bash_rpc_eval_stdin \ |
102 | fi | 115 | "${#stage2_source}" \ |
103 | } | remote_run_function_simple bash_rpc_eval_stdin "${#stage2_source}" "$@" | 116 | "$@" |
104 | } | 117 | } |
105 | 118 | ||