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
|
<?php
namespace Clue\React\Term;
use Evenement\EventEmitter;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
use React\Stream\Util;
class ControlCodeParser extends EventEmitter implements ReadableStreamInterface
{
private $input;
private $closed = false;
private $buffer = '';
/**
* we know about the following C1 types (7 bit only)
*
* followed by "[" means it's CSI (Control Sequence Introducer)
* followed by "]" means it's OSC (Operating System Controls)
* followed by "_" means it's APC (Application Program-Control)
* followed by "P" means it's DPS (Device-Control string)
* followed by "^" means it's PM (Privacy Message)
*
* Each of these will be parsed until the sequence ends and then emitted
* under their respective name.
*
* All other C1 types will be emitted under the "c1" name without any
* further processing.
*
* C1 types in 8 bit are currently not supported, as they require special
* care with regards to whether UTF-8 mode is enabled. So far this has
* turned out to be a non-issue because most terminal emulators *accept*
* boths formats, but usually *send* in 7 bit mode exclusively.
*/
private $types = array(
'[' => 'csi',
']' => 'osc',
'_' => 'apc',
'P' => 'dps',
'^' => 'pm',
);
public function __construct(ReadableStreamInterface $input)
{
$this->input = $input;
if (!$this->input->isReadable()) {
return $this->close();
}
$this->input->on('data', array($this, 'handleData'));
$this->input->on('end', array($this, 'handleEnd'));
$this->input->on('error', array($this, 'handleError'));
$this->input->on('close', array($this, 'close'));
}
public function isReadable()
{
return !$this->closed && $this->input->isReadable();
}
public function pause()
{
$this->input->pause();
}
public function resume()
{
$this->input->resume();
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
Util::pipe($this, $dest, $options);
return $dest;
}
public function close()
{
if ($this->closed) {
return;
}
$this->closed = true;
$this->buffer = '';
$this->input->close();
$this->emit('close');
$this->removeAllListeners();
}
/** @internal */
public function handleData($data)
{
$this->buffer .= $data;
while ($this->buffer !== '') {
// search for first control character (C0 and DEL)
$c0 = false;
for ($i = 0; isset($this->buffer[$i]); ++$i) {
$code = ord($this->buffer[$i]);
if ($code < 0x20 || $code === 0x7F) {
$c0 = $i;
break;
}
}
// no C0 found, emit whole buffer as data
if ($c0 === false) {
$data = $this->buffer;
$this->buffer = '';
$this->emit('data', array($data));
return;
}
// C0 found somewhere inbetween, emit everything before C0 as data
if ($c0 !== 0) {
$data = substr($this->buffer, 0, $c0);
$this->buffer = substr($this->buffer, $c0);
$this->emit('data', array($data));
continue;
}
// C0 is now at start of buffer
// check if this is a normal C0 code or an ESC (\x1B = \033)
// normal C0 will be emitted, ESC will be parsed further
if ($this->buffer[0] !== "\x1B") {
$data = $this->buffer[0];
$this->buffer = (string)substr($this->buffer, 1);
$this->emit('c0', array($data));
continue;
}
// check following byte to determine type
if (!isset($this->buffer[1])) {
// type currently unknown, wait for next data chunk
break;
}
// if this is an unknown type, just emit as "c1" without further parsing
if (!isset($this->types[$this->buffer[1]])) {
$data = substr($this->buffer, 0, 2);
$this->buffer = (string)substr($this->buffer, 2);
$this->emit('c1', array($data));
continue;
}
// this is known type, check for the sequence end
$type = $this->types[$this->buffer[1]];
$found = false;
if ($type === 'csi') {
// CSI is now at the start of the buffer, search final character
for ($i = 2; isset($this->buffer[$i]); ++$i) {
$code = ord($this->buffer[$i]);
// final character between \x40-\x7E
if ($code >= 64 && $code <= 126) {
$data = substr($this->buffer, 0, $i + 1);
$this->buffer = (string)substr($this->buffer, $i + 1);
$this->emit($type, array($data));
$found = true;
break;
}
}
} else {
// all other types are terminated by ST
// only OSC can also be terminted by BEL (whichever comes first)
$st = strpos($this->buffer, "\x1B\\");
$bel = ($type === 'osc') ? strpos($this->buffer, "\x07") : false;
if ($st !== false && ($bel === false || $bel > $st)) {
// ST comes before BEL or no BEL found
$data = substr($this->buffer, 0, $st + 2);
$this->buffer = (string)substr($this->buffer, $st + 2);
$this->emit($type, array($data));
$found = true;
} elseif ($bel !== false) {
// BEL comes before ST or no ST found
$data = substr($this->buffer, 0, $bel + 1);
$this->buffer = (string)substr($this->buffer, $bel + 1);
$this->emit($type, array($data));
$found = true;
}
}
// no final character found => wait for next data chunk
if (!$found) {
break;
}
}
}
/** @internal */
public function handleEnd()
{
if (!$this->closed) {
if ($this->buffer === '') {
$this->emit('end');
} else {
$this->emit('error', array(new \RuntimeException('Stream ended with incomplete control code sequence in buffer')));
}
$this->close();
}
}
/** @internal */
public function handleError(\Exception $e)
{
$this->emit('error', array($e));
$this->close();
}
}
|