summaryrefslogtreecommitdiff
path: root/twopane.bash
blob: 8eb7935ed9ebf94049d8c9fdea4d034c18f7e8cb (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
#!/bin/bash
set -e
set -f
set -o pipefail
shopt -s lastpipe
PROGNAME=${0##*/}

BOT_SIZE=8
BOT_TITLE=input
BOT_CMD=sendstream
if [ $# = 0 ]
then
        TOP_CMD="$SHELL -i"
else
        TOP_CMD=$*
fi
TOP_TITLE=$TOP_CMD

TWOPANE=$(mktemp -d)
export TWOPANE TOP_CMD BOT_CMD TOP_TITLE BOT_TITLE BOT_SIZE
trap 'rm -r "$TWOPANE"' EXIT
STY=twopane.${TWOPANE##*/}

save_file()
{
        cat > "$TWOPANE"/"${1:?$PROGNAME: 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

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

save_screenrc 'startpane' <<.
focus top
screen -ln -t "\$TOP_TITLE" 1 $TOP_CMD
exec .!. sh -c 'socat UNIX-RECV:"\$TWOPANE"/socket!!UNIX-SEND:"\$TWOPANE"/rsocket STDIN,rawer!!STDOUT'
title 'output'
focus top
.

restart()
{
        while screen -p1 -Q info >/dev/null
        do
                screen -p1 -X kill
        done
        screen -X source "$TWOPANE"/screenrc.startpane
}
sendp()
{
        trap "kill -SIGUSR1 ${1:-$$}" EXIT
        socat -u STDIN UNIX-SENDTO:"$TWOPANE"/socket 2>&20 || restart
}
sendc()
{
        [ "${SOCAT[1]}" ] || coproc SOCAT { sendp $$; }
        printf '%s' "$*" >&${SOCAT[1]}
}
send()
{
        sendc "$*"$'\n'
}

raw()
{
        stty_opts=(
                cbreak
                raw
                -isig
                -iexten
                -nl
                intr    undef
                quit    undef
               #erase   undef
               #kill    undef
                eof     undef
                start   undef
                stop    undef
                susp    undef
                lnext   undef
        )
        stty "${stty_opts[@]}"
}

:()
{
        if [ "$#" -gt 0 ]
        then
                printf '%s: %s\r\n' "$0" "$*" >&2
        fi
}

start()
{
        socat_stty_opts=(
                echo=1
                echoctl=1
                intr=0
                quit=0
                eof=0
                start=0
                stop=0
                susp=0
                lnext=0
        )
        stty=$(printf %s, "${socat_stty_opts[@]}")
        while ! socat -u \
                STDIN,rawer,"${stty}" \
                UNIX-SENDTO:"$TWOPANE"/socket
        do
                restart
        done
}

recvstream()
{
        socat -u UNIX-RECV:"$TWOPANE"/rsocket STDOUT |
        tee >(socat -u STDIN UNIX-SENDTO:"$TWOPANE"/socket) |
        cat -v
}

sendstream()
{
        while ! recvstream
        do
                continue
        done &
        trap "kill $!" EXIT
        trap '' SIGPIPE
        trap 'SOCAT=(); RETRY=$REPLY;' SIGUSR1
        raw
        while [ "$RETRY" ] || read -t 0.1 -r -N1 || [ $? -gt 128 ]
        do
                REPLY=$RETRY$REPLY
                RETRY=
                if [ "$REPLY" ]
                then
                        sendc "$REPLY"
                elif ! [ -e "$TWOPANE"/socket ]
                then
                        restart
                fi
        done
}

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

quit()
{
        screen -X quit
}

save_file bashrc <<.
set -f
set -b
set -o pipefail
shopt -s lastpipe
trap "screen -X quit" EXIT
$(declare -f)
export PS1="\$BOT_TITLE\$ "

if [ "\$SHOW_SOCAT_ERRORS" ]
then
        exec 20>&2
else
        exec 20>/dev/null
fi

\$BOT_CMD
.

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

main "$@"