blob: 6abd2b6478e2b1ed719dcb15165066c45fe4bc15 (
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
|
#!/bin/bash -i
set -m
[ "${BASH_LOADABLES_PATH:=/lib/bash:/usr/lib/bash}" ]
cfmakeraw()
{
cmd=(stty -ignbrk -brkint -parmrk -istrip -inlcr -igncr -icrnl
-ixon -opost -echo -echonl -icanon -isig -iexten -parenb cs8)
"${cmd[@]}" "$@" >/dev/null
}
readchar_reset()
{
if [ "$readchar_init_stty" ]
then
stty "$readchar_init_stty" 2>/dev/null
fi
}
i()
{
if [ "$DEBUG" ]
then
printf 'I: %s\n' "$*" >&2
fi
}
x()
{
if [ "$DEBUG" ]
then
printf '+ %s\n' "${*@Q}" >&2
fi
"$@"
}
readchar_init()
{
[ $$ != $BASHPID ] || return
declare -g -a readchar_termopts
declare -g readchar_init_stty
declare -g SOCAT
declare -g t
[ -t 1 ] && t=y || t=
readchar_init_stty=$(stty --save)
readchar_termopts=(
opost onlcr isig
intr undef
quit undef
)
trap 'x readchar_reset' EXIT
trap "i CONT; readchar_SIGCONT" CONT
for sig in TSTP TTIN TTOU
do
trap "i $sig; x kill -STOP \$BASHPID \$SOCAT_PID" $sig
done
}
DEBUG=y
readchar_SIGCONT()
{
if check_foreground
then
x cfmakeraw "${readchar_termopts[@]}"
x kill -CONT $SOCAT_PID 2>/dev/null
else
x kill -STOP $BASHPID $SOCAT_PID 2>/dev/null
fi
}
check_foreground()
{
read _pid _comm _state _ppid pgrp _session _tty_nr tpgid _rest < /proc/self/stat
[ "$tpgid" = "$pgrp" ]
}
readchar()
{
declare -n REPLY="${1:-CHARACTER}"
if ! check_foreground
then
sleep .25
return
fi
if ! [ "$SOCAT_PID" ] || ! kill -CONT "$SOCAT_PID" 2>/dev/null
then
x cfmakeraw "${readchar_termopts[@]}"
exec {SOCAT}< <(trap 'sleep .25' SIGTSTP; socat - -)
SOCAT_PID=$!
fi
if read -n1 -d '' -r -s -u "$SOCAT"
then
if [ "$t" ]
then
printf -v quoted_reply "%s" "${REPLY@Q}"
if (( (count += 1 + ${#quoted_reply}) < COLUMNS - 10 ))
then
seperate=' '
else
seperate=$'\n'
count=0
fi
printf "%s" "$seperate" "$quoted_reply"
else
if [ "$REPLY" ]
then
printf "%s" "$REPLY"
elif [ "$OUTPUT_NULLS" ]
then
printf "\0"
fi
fi
else
[ $? -gt 128 ]
fi
}
read_char()
{
readchar_init
t=
c=
count=0
while ! [ "$c" ]
do
readchar c
done
readchar_reset
if [ -t 1 ]
then
printf '%s\n' "${c@Q}"
else
printf %s "$c"
fi
}
read_chars()
{
readchar_init
while readchar
do
continue
done
readchar_reset
}
reader_process()
{
enable -f sleep sleep
if [ "$SHLVL" = 1 -a -t 1 ]
then
(read_chars)
else
read_chars
fi
}
run_test()
{
enable -f sleep sleep || return
(
pid=$BASHPID
exec {HEAD1}> >(head -c1; kill -PIPE $pid)
read_chars >&$HEAD1
)
}
with_raw_tty_input()
{
enable -f sleep sleep || return
(
pid=$BASHPID
if [ $# -gt 0 ]
then
exec {CMD}> >("$@"; kill -PIPE $pid 2>/dev/null)
exec >&$CMD
fi
read_chars
)
}
run_test()
{
with_raw_tty_input head -c3 | sed -nel
}
run_test()
{
reader_process &
kill -TSTP %reader_process
bg %reader_process
kill -TSTP %reader_process
bg %reader_process
kill -TSTP %reader_process
bg %reader_process
kill -TSTP %reader_process
bg %reader_process
kill -TSTP %reader_process
fg %reader_process
}
|