| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Term; |
| 4 |
|
| 5 |
use Evenement\EventEmitter; |
| 6 |
use React\Stream\ReadableStreamInterface; |
| 7 |
use React\Stream\WritableStreamInterface; |
| 8 |
use React\Stream\Util; |
| 9 |
|
| 10 |
class ControlCodeParser extends EventEmitter implements ReadableStreamInterface |
| 11 |
{ |
| 12 |
private $input; |
| 13 |
private $closed = false; |
| 14 |
private $buffer = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* we know about the following C1 types (7 bit only) |
| 18 |
* |
| 19 |
* followed by "[" means it's CSI (Control Sequence Introducer) |
| 20 |
* followed by "]" means it's OSC (Operating System Controls) |
| 21 |
* followed by "_" means it's APC (Application Program-Control) |
| 22 |
* followed by "P" means it's DPS (Device-Control string) |
| 23 |
* followed by "^" means it's PM (Privacy Message) |
| 24 |
* |
| 25 |
* Each of these will be parsed until the sequence ends and then emitted |
| 26 |
* under their respective name. |
| 27 |
* |
| 28 |
* All other C1 types will be emitted under the "c1" name without any |
| 29 |
* further processing. |
| 30 |
* |
| 31 |
* C1 types in 8 bit are currently not supported, as they require special |
| 32 |
* care with regards to whether UTF-8 mode is enabled. So far this has |
| 33 |
* turned out to be a non-issue because most terminal emulators *accept* |
| 34 |
* both formats, but usually *send* in 7 bit mode exclusively. |
| 35 |
*/ |
| 36 |
private $types = array( |
| 37 |
'[' => 'csi', |
| 38 |
']' => 'osc', |
| 39 |
'_' => 'apc', |
| 40 |
'P' => 'dps', |
| 41 |
'^' => 'pm', |
| 42 |
); |
| 43 |
|
| 44 |
public function __construct(ReadableStreamInterface $input) |
| 45 |
{ |
| 46 |
$this->input = $input; |
| 47 |
|
| 48 |
if (!$this->input->isReadable()) { |
| 49 |
return $this->close(); |
| 50 |
} |
| 51 |
|
| 52 |
$this->input->on('data', array($this, 'handleData')); |
| 53 |
$this->input->on('end', array($this, 'handleEnd')); |
| 54 |
$this->input->on('error', array($this, 'handleError')); |
| 55 |
$this->input->on('close', array($this, 'close')); |
| 56 |
} |
| 57 |
|
| 58 |
public function isReadable() |
| 59 |
{ |
| 60 |
return !$this->closed && $this->input->isReadable(); |
| 61 |
} |
| 62 |
|
| 63 |
public function pause() |
| 64 |
{ |
| 65 |
$this->input->pause(); |
| 66 |
} |
| 67 |
|
| 68 |
public function resume() |
| 69 |
{ |
| 70 |
$this->input->resume(); |
| 71 |
} |
| 72 |
|
| 73 |
public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 74 |
{ |
| 75 |
Util::pipe($this, $dest, $options); |
| 76 |
|
| 77 |
return $dest; |
| 78 |
} |
| 79 |
|
| 80 |
public function close() |
| 81 |
{ |
| 82 |
if ($this->closed) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$this->closed = true; |
| 87 |
$this->buffer = ''; |
| 88 |
|
| 89 |
$this->input->close(); |
| 90 |
|
| 91 |
$this->emit('close'); |
| 92 |
$this->removeAllListeners(); |
| 93 |
} |
| 94 |
|
| 95 |
/** @internal */ |
| 96 |
public function handleData($data) |
| 97 |
{ |
| 98 |
$this->buffer .= $data; |
| 99 |
|
| 100 |
while ($this->buffer !== '') { |
| 101 |
// search for first control character (C0 and DEL) |
| 102 |
$c0 = false; |
| 103 |
for ($i = 0; isset($this->buffer[$i]); ++$i) { |
| 104 |
$code = ord($this->buffer[$i]); |
| 105 |
if ($code < 0x20 || $code === 0x7F) { |
| 106 |
$c0 = $i; |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// no C0 found, emit whole buffer as data |
| 112 |
if ($c0 === false) { |
| 113 |
$data = $this->buffer; |
| 114 |
$this->buffer = ''; |
| 115 |
|
| 116 |
$this->emit('data', array($data)); |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
// C0 found somewhere inbetween, emit everything before C0 as data |
| 121 |
if ($c0 !== 0) { |
| 122 |
$data = substr($this->buffer, 0, $c0); |
| 123 |
$this->buffer = substr($this->buffer, $c0); |
| 124 |
|
| 125 |
$this->emit('data', array($data)); |
| 126 |
continue; |
| 127 |
} |
| 128 |
|
| 129 |
// C0 is now at start of buffer |
| 130 |
// check if this is a normal C0 code or an ESC (\x1B = \033) |
| 131 |
// normal C0 will be emitted, ESC will be parsed further |
| 132 |
if ($this->buffer[0] !== "\x1B") { |
| 133 |
$data = $this->buffer[0]; |
| 134 |
$this->buffer = (string)substr($this->buffer, 1); |
| 135 |
|
| 136 |
$this->emit('c0', array($data)); |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
// check following byte to determine type |
| 141 |
if (!isset($this->buffer[1])) { |
| 142 |
// type currently unknown, wait for next data chunk |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
// if this is an unknown type, just emit as "c1" without further parsing |
| 147 |
if (!isset($this->types[$this->buffer[1]])) { |
| 148 |
$data = substr($this->buffer, 0, 2); |
| 149 |
$this->buffer = (string)substr($this->buffer, 2); |
| 150 |
|
| 151 |
$this->emit('c1', array($data)); |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
// this is known type, check for the sequence end |
| 156 |
$type = $this->types[$this->buffer[1]]; |
| 157 |
$found = false; |
| 158 |
|
| 159 |
if ($type === 'csi') { |
| 160 |
// CSI is now at the start of the buffer, search final character |
| 161 |
for ($i = 2; isset($this->buffer[$i]); ++$i) { |
| 162 |
$code = ord($this->buffer[$i]); |
| 163 |
|
| 164 |
// final character between \x40-\x7E |
| 165 |
if ($code >= 64 && $code <= 126) { |
| 166 |
$data = substr($this->buffer, 0, $i + 1); |
| 167 |
$this->buffer = (string)substr($this->buffer, $i + 1); |
| 168 |
|
| 169 |
$this->emit($type, array($data)); |
| 170 |
$found = true; |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
} else { |
| 175 |
// all other types are terminated by ST |
| 176 |
// only OSC can also be terminted by BEL (whichever comes first) |
| 177 |
$st = strpos($this->buffer, "\x1B\\"); |
| 178 |
$bel = ($type === 'osc') ? strpos($this->buffer, "\x07") : false; |
| 179 |
|
| 180 |
if ($st !== false && ($bel === false || $bel > $st)) { |
| 181 |
// ST comes before BEL or no BEL found |
| 182 |
$data = substr($this->buffer, 0, $st + 2); |
| 183 |
$this->buffer = (string)substr($this->buffer, $st + 2); |
| 184 |
|
| 185 |
$this->emit($type, array($data)); |
| 186 |
$found = true; |
| 187 |
} elseif ($bel !== false) { |
| 188 |
// BEL comes before ST or no ST found |
| 189 |
$data = substr($this->buffer, 0, $bel + 1); |
| 190 |
$this->buffer = (string)substr($this->buffer, $bel + 1); |
| 191 |
|
| 192 |
$this->emit($type, array($data)); |
| 193 |
$found = true; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// no final character found => wait for next data chunk |
| 198 |
if (!$found) { |
| 199 |
break; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** @internal */ |
| 205 |
public function handleEnd() |
| 206 |
{ |
| 207 |
if (!$this->closed) { |
| 208 |
if ($this->buffer === '') { |
| 209 |
$this->emit('end'); |
| 210 |
} else { |
| 211 |
$this->emit('error', array(new \RuntimeException('Stream ended with incomplete control code sequence in buffer'))); |
| 212 |
} |
| 213 |
$this->close(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** @internal */ |
| 218 |
public function handleError(\Exception $e) |
| 219 |
{ |
| 220 |
$this->emit('error', array($e)); |
| 221 |
$this->close(); |
| 222 |
} |
| 223 |
} |