summaryrefslogtreecommitdiff
path: root/src/twopane.bash
blob: 56d462a3e94ab1bf9768db8d04391ca48d4242b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
BASH_ARGV0=twopane.bash
echo -n "$0" >/proc/$$/comm
set -e
set -f
set -o pipefail
shopt -s lastpipe

. read_chars.bash

BOT_SIZE=8
BOT_TITLE=input

if [ $# = 0 ]
then
        TOP_CMD="$SHELL -i"
        BOT_CMD=foreground
        #BOT_CMD=start
        #TOP_EXIT=quit
        #TOP_EXIT=prompt
        TOP_EXIT=restart
else
        TOP_CMD=$*
        BOT_CMD=start
        TOP_EXIT=quit
fi
TOP_TITLE="Command: $TOP_CMD"

TWOPANE=$(mktemp -d)
export TWOPANE TOP_CMD BOT_CMD TOP_TITLE BOT_TITLE BOT_SIZE
trap 'rm -r "$TWOPANE"' EXIT
# STY=$(systemd-escape -p "$TWOPANE")
systemd_escape_path()
{
        declare -n path="$1" result="$2"
        result=$(realpath "$path")
        result=${result//-/--}
        result=${result//\//-}
}
systemd_escape_path TWOPANE STY
STY=$0.$$.$STY

save_file()
{
        cat > "$TWOPANE"/"${1:?$0: Error: filename cannot be empty string}"
}

save_screenrc()
{
        save_file screenrc"${1:+.$1}"
}

save_screenrc <<'.'
# Disable keybindings.
unbindall
escape \0\0
# Disable messages.  This is needed for screen -X/-Q to work reasonably.
msgwait 0
msgminwait 0
# Try to disable blocking the terminal on ^S.
# Doesn't seem to work here.
# More is needed.  Xterm?
nonblock on

# Enable mouse focus
defmousetrack on
mousetrack on

caption string '%t'
layout new
split
focus bottom
resize $BOT_SIZE
screen -ln -t "$BOT_TITLE" 0 bash --noprofile --rcfile "$TWOPANE"/bashrc -i
layout save 0
.

check_screen_pane()
{
        screen -p "$pane" -Q info >/dev/null
}

kill_screen_pane()
{
        while check_screen_pane
        do
                screen -p "$pane" -X kill
        done
}

start_screen_pane()
{
        declare -i pane="${1:-1}"
        (( pane > 0 )) || return
        if check_screen_pane "$pane"
        then
                return
        fi
        shift
        if [ $# = 0 ]
        then
                set -f
                set -- ${TOP_CMD:-bash -i}
        fi
        TOP_TITLE="Command: ${*@Q}"
        screen -X focus top
        screen -X screen -ln -t "$TOP_TITLE" 1 "$@"
        start_screen_pane_subprocess "$pane"
}

start_screen_pane_subprocess()
{
        declare -i pane="${1:-1}"
        subproc_command=(
                exec -a top-pane-tty-forward
                        socat
                                UNIX-LISTEN:"$TWOPANE"/socket
                                STDIN,cfmakeraw,opost=1,onlcr=1!!STDOUT
        )
        screen -p "$pane" -X exec .!. bash -c "${subproc_command[*]}"
}

restart_screen_pane()
{
        kill_screen_pane
        start_screen_pane "$@"
}

connect_coproc()
{
        declare -n coproc="$1"
        shift
        declare -n std1="${!coproc}_STDOUT"
        declare -n std0="${!coproc}_STDIN"
        declare -n opid="${!coproc}_PID"

        if [ $# = 0 ]
        then
                set -- "${!coproc}"
        fi
        if ! [ "$opid" ]
        then
                local STDERR
                {
                        coproc "${!coproc}" \
                        {
                                "$@"
                        } 2>&$STDERR {STDERR}>&-
                } {STDERR}>&2 2>/dev/null
                disown
        fi
        # The copied file descriptors (unlike the original coprocess
        # file descriptors, in ${coproc[0]} and ${coproc[1]}) can be
        # passed to external processes (e.g.: socat(1) instances) and
        # used in subshells.
        {
                exec {std0}<&0 {std1}>&1
        } <&${coproc[0]} >&${coproc[1]}
}

disconnect_coproc()
{
        [ $# = 1 ] || return
        declare -n coproc="$1"
        declare -n std0="${!coproc}_STDIN"
        declare -n std1="${!coproc}_STDOUT"
        declare -n  pid="${!coproc}_PID"
        if [ "$std0" -o "$std1" ]
        then
                exec {std0}<&- {std1}>&-
        fi
        if [ "$pid" ]
        then
                kill "$pid" 2>/dev/null
                wait -f "$pid" 2>/dev/null
        fi
        unset coproc std0 std1 pid
}

connect()
{
        connect_coproc TOP_PANE
}

disconnect()
{
        : "${TOP_PANE_STDIN@A} ${TOP_PANE_STDOUT@A} ${TOP_PANE_PID@A} ${TOP_PANE[@]@A}"
        disconnect_coproc TOP_PANE
        : "${ECHOSEND@A} ${ECHOSEND_PID@A}"
        disconnect_sink ECHOSEND
}

sendc()
{
        [ "${TOP_PANE[0]}" ] || connect
        printf '%s' "$*" >&${TOP_PANE[1]}
}

send()
{
        [ "${TOP_PANE[0]}" ] || connect
        printf '%s\n' "$*" >&${TOP_PANE[1]}
}

restart()
{
        start "$@"
}

start()
{
        foreground "$@"
}

check_tty_reader()
{
        [ "$TTY_FORWARD_PID" ] || return
        TTY_FORWARD_JOBSPEC=$(jobs -sl | pid_to_jobspec "$TTY_FORWARD_PID")
        [ "$TTY_FORWARD_JOBSPEC" ]
}

pid_to_jobspec()
{
        while read jspec pid status cmd
        do
                [ "$pid" = "$1" ] || continue
                jspec=${jspec%]*}
                jspec=%${jspec#[}
                echo $jspec
                return
        done
        false
}

tty_forward()
{
        printf '%d\n' "$BASHPID" > "$TWOPANE"/tty_forward.pid
        read-tty
}

kill_tty_forward()
{
        read pid < "$TWOPANE"/tty_forward.pid
        /bin/kill -INT -- -$pid 2>/dev/null
}

TOP_PANE()
{
        (exec -a bottom-pane-tty-forward socat - UNIX-CONNECT:"$TWOPANE"/socket,forever)
        kill_tty_forward
        case "$TOP_EXIT" in
                restart )
                        start_screen_pane 1
                        kill -USR1 $$
                ;;
                quit )
                        kill -INT $$
                        quit
                ;;
                prompt | * )
                        focus bottom
                ;;
        esac
}

sink()
{
        declare -n fd="$1"
        declare -n pid="${!fd}_PID"
        disconnect_sink "$fd"
        exec {fd}> >("${@:2}")
        pid=$!
}

disconnect_sink()
{
        [ "$1" ] || return
        declare -n fd="$1"
        [ "$fd" ] || return
        declare -n pid="${!fd}_PID"
        exec {fd}>&-
        kill $pid 2>/dev/null
        unset fd pid
}

quiet_bg()
{
        local STDERR
        {
                eval "$(printf '%q ' "$@") & 2>&\$STDERR {STDERR}>&-"
        } {STDERR}>&2 2>/dev/null
}

echo_sender()
{
        (exec -a echo_sender socat - fd:${TOP_PANE_STDIN?$0: Internal error}!!-) |
        tee >(write-tty) >&${TOP_PANE_STDOUT?$0: Internal error}
}

background()
{
        start_screen_pane 1 "$@"
        focus bottom

        disconnect_sink ECHOSEND
        connect
        sink ECHOSEND echo_sender
        quiet_bg tty_forward >&$ECHOSEND

        TTY_FORWARD_PID=$!
        printf '%d\n' "$TTY_FORWARD_PID" > "$TWOPANE"/tty_forward.pid
        printf '%s\n' \
                "#!/bin/bash" \
                "kill -TSTP $TTY_FORWARD_PID" \
                "screen -X focus bottom" \
                > "$TWOPANE"/unforward
        chmod +x "$TWOPANE"/unforward
}

foreground()
{
        check_tty_reader || background "$@"
        focus top
        fg %tty_forward >/dev/null 2>&1
        focus bottom
}

twopane()
{
        start "$@"
}

quit()
{
        screen -X quit
}

focus()
{
        screen -X focus "$@"
}

resize()
{
        screen -X resize "$@"
}

SIGUSR1()
{
        i SIGUSR1 "${BASH_COMMAND@A}"
}

our_bashrc_main()
{
        set -m
        set -f
        set -o pipefail
        #trap 'focus bottom; resize 90%; read -p "Exit> "; quit' EXIT
        trap 'SIGUSR1' USR1
        export PS1="$BOT_TITLE\\\$ "
}

save_file bashrc <<.
BASH_ARGV0=twopane
echo -n "\$0" >/proc/\$\$/comm
${TOP_CMD@A}
${BOT_CMD@A}
${TOP_TITLE@A}
${BOT_TITLE@A}

${TOP_EXIT@A}

$(declare -f)
our_bashrc_main

$BOT_CMD
.

main()
{
        screen -c "$TWOPANE"/screenrc -m -S "$STY" -ln
}

main "$@"