1 import { C0 } from './EscapeSequences';
2 import { IInputHandler } from './Interfaces';
3 import { CHARSETS } from './Charsets';
5 const normalStateHandler: {[key: string]: (parser: Parser, handler: IInputHandler) => void} = {};
6 normalStateHandler[C0.BEL] = (parser, handler) => handler.bell();
7 normalStateHandler[C0.LF] = (parser, handler) => handler.lineFeed();
8 normalStateHandler[C0.VT] = normalStateHandler[C0.LF];
9 normalStateHandler[C0.FF] = normalStateHandler[C0.LF];
10 normalStateHandler[C0.CR] = (parser, handler) => handler.carriageReturn();
11 normalStateHandler[C0.BS] = (parser, handler) => handler.backspace();
12 normalStateHandler[C0.HT] = (parser, handler) => handler.tab();
13 normalStateHandler[C0.SO] = (parser, handler) => handler.shiftOut();
14 normalStateHandler[C0.SI] = (parser, handler) => handler.shiftIn();
15 normalStateHandler[C0.ESC] = (parser, handler) => parser.setState(ParserState.ESCAPED);
17 const csiParamStateHandler: {[key: string]: (parser: Parser) => void} = {};
18 csiParamStateHandler['?'] = (parser) => parser.setPrefix('?');
19 csiParamStateHandler['>'] = (parser) => parser.setPrefix('>');
20 csiParamStateHandler['!'] = (parser) => parser.setPrefix('!');
21 csiParamStateHandler['0'] = (parser) => parser.setParam(parser.getParam() * 10);
22 csiParamStateHandler['1'] = (parser) => parser.setParam(parser.getParam() * 10 + 1);
23 csiParamStateHandler['2'] = (parser) => parser.setParam(parser.getParam() * 10 + 2);
24 csiParamStateHandler['3'] = (parser) => parser.setParam(parser.getParam() * 10 + 3);
25 csiParamStateHandler['4'] = (parser) => parser.setParam(parser.getParam() * 10 + 4);
26 csiParamStateHandler['5'] = (parser) => parser.setParam(parser.getParam() * 10 + 5);
27 csiParamStateHandler['6'] = (parser) => parser.setParam(parser.getParam() * 10 + 6);
28 csiParamStateHandler['7'] = (parser) => parser.setParam(parser.getParam() * 10 + 7);
29 csiParamStateHandler['8'] = (parser) => parser.setParam(parser.getParam() * 10 + 8);
30 csiParamStateHandler['9'] = (parser) => parser.setParam(parser.getParam() * 10 + 9);
31 csiParamStateHandler['$'] = (parser) => parser.setPostfix('$');
32 csiParamStateHandler['"'] = (parser) => parser.setPostfix('"');
33 csiParamStateHandler[' '] = (parser) => parser.setPostfix(' ');
34 csiParamStateHandler['\''] = (parser) => parser.setPostfix('\'');
35 csiParamStateHandler[';'] = (parser) => parser.finalizeParam();
37 const csiStateHandler: {[key: string]: (handler: IInputHandler, params: number[], prefix: string) => void} = {};
38 csiStateHandler['@'] = (handler, params, prefix) => handler.insertChars(params);
39 csiStateHandler['A'] = (handler, params, prefix) => handler.cursorUp(params);
40 csiStateHandler['B'] = (handler, params, prefix) => handler.cursorDown(params);
41 csiStateHandler['C'] = (handler, params, prefix) => handler.cursorForward(params);
42 csiStateHandler['D'] = (handler, params, prefix) => handler.cursorBackward(params);
43 csiStateHandler['E'] = (handler, params, prefix) => handler.cursorNextLine(params);
44 csiStateHandler['F'] = (handler, params, prefix) => handler.cursorPrecedingLine(params);
45 csiStateHandler['G'] = (handler, params, prefix) => handler.cursorCharAbsolute(params);
46 csiStateHandler['H'] = (handler, params, prefix) => handler.cursorPosition(params);
47 csiStateHandler['I'] = (handler, params, prefix) => handler.cursorForwardTab(params);
48 csiStateHandler['J'] = (handler, params, prefix) => handler.eraseInDisplay(params);
49 csiStateHandler['K'] = (handler, params, prefix) => handler.eraseInLine(params);
50 csiStateHandler['L'] = (handler, params, prefix) => handler.insertLines(params);
51 csiStateHandler['M'] = (handler, params, prefix) => handler.deleteLines(params);
52 csiStateHandler['P'] = (handler, params, prefix) => handler.deleteChars(params);
53 csiStateHandler['S'] = (handler, params, prefix) => handler.scrollUp(params);
54 csiStateHandler['T'] = (handler, params, prefix) => {
55 if (params.length < 2 && !prefix) {
56 handler.scrollDown(params);
59 csiStateHandler['X'] = (handler, params, prefix) => handler.eraseChars(params);
60 csiStateHandler['Z'] = (handler, params, prefix) => handler.cursorBackwardTab(params);
61 csiStateHandler['`'] = (handler, params, prefix) => handler.charPosAbsolute(params);
62 csiStateHandler['a'] = (handler, params, prefix) => handler.HPositionRelative(params);
63 csiStateHandler['b'] = (handler, params, prefix) => handler.repeatPrecedingCharacter(params);
64 csiStateHandler['c'] = (handler, params, prefix) => handler.sendDeviceAttributes(params);
65 csiStateHandler['d'] = (handler, params, prefix) => handler.linePosAbsolute(params);
66 csiStateHandler['e'] = (handler, params, prefix) => handler.VPositionRelative(params);
67 csiStateHandler['f'] = (handler, params, prefix) => handler.HVPosition(params);
68 csiStateHandler['g'] = (handler, params, prefix) => handler.tabClear(params);
69 csiStateHandler['h'] = (handler, params, prefix) => handler.setMode(params);
70 csiStateHandler['l'] = (handler, params, prefix) => handler.resetMode(params);
71 csiStateHandler['m'] = (handler, params, prefix) => handler.charAttributes(params);
72 csiStateHandler['n'] = (handler, params, prefix) => handler.deviceStatus(params);
73 csiStateHandler['p'] = (handler, params, prefix) => {
75 case '!': handler.softReset(params); break;
78 csiStateHandler['r'] = (handler, params) => handler.setScrollRegion(params);
79 csiStateHandler['s'] = (handler, params) => handler.saveCursor(params);
80 csiStateHandler['u'] = (handler, params) => handler.restoreCursor(params);
94 private state: ParserState;
96 // TODO: Remove terminal when handler can do everything
98 private _inputHandler: IInputHandler,
99 private _terminal: any
101 this.state = ParserState.NORMAL;
104 public parse(data: string) {
105 let l = data.length, i = 0, j, cs, ch, code, low;
107 // apply leftover surrogate high from last write
108 if (this._terminal.surrogate_high) {
109 data = this._terminal.surrogate_high + data;
110 this._terminal.surrogate_high = '';
116 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
117 // --> maybe move to default
118 code = data.charCodeAt(i);
119 if (0xD800 <= code && code <= 0xDBFF) {
120 // we got a surrogate high
121 // get surrogate low (next 2 bytes)
122 low = data.charCodeAt(i + 1);
124 // end of data stream, save surrogate high
125 this._terminal.surrogate_high = ch;
128 code = ((code - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
129 ch += data.charAt(i + 1);
131 // surrogate low - already handled above
132 if (0xDC00 <= code && code <= 0xDFFF)
135 switch (this.state) {
136 case ParserState.NORMAL:
137 if (ch in normalStateHandler) {
138 normalStateHandler[ch](this, this._inputHandler);
140 this._inputHandler.addChar(ch, code);
143 case ParserState.ESCAPED:
145 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
147 this._terminal.params = [];
148 this._terminal.currentParam = 0;
149 this.state = ParserState.CSI_PARAM;
152 // ESC ] Operating System Command ( OSC is 0x9d).
154 this._terminal.params = [];
155 this._terminal.currentParam = 0;
156 this.state = ParserState.OSC;
159 // ESC P Device Control String ( DCS is 0x90).
161 this._terminal.params = [];
162 this._terminal.currentParam = 0;
163 this.state = ParserState.DCS;
166 // ESC _ Application Program Command ( APC is 0x9f).
168 this.state = ParserState.IGNORE;
171 // ESC ^ Privacy Message ( PM is 0x9e).
173 this.state = ParserState.IGNORE;
176 // ESC c Full Reset (RIS).
178 this._terminal.reset();
181 // ESC E Next Line ( NEL is 0x85).
182 // ESC D Index ( IND is 0x84).
184 this._terminal.x = 0;
187 this._terminal.index();
188 this.state = ParserState.NORMAL;
191 // ESC M Reverse Index ( RI is 0x8d).
193 this._terminal.reverseIndex();
194 this.state = ParserState.NORMAL;
197 // ESC % Select default/utf-8 character set.
198 // @ = default, G = utf-8
200 // this.charset = null;
201 this._terminal.setgLevel(0);
202 this._terminal.setgCharset(0, CHARSETS.US);
203 this.state = ParserState.NORMAL;
207 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
208 case '(': // <-- this seems to get all the attention
216 this._terminal.gcharset = 0;
219 this._terminal.gcharset = 1;
222 this._terminal.gcharset = 2;
225 this._terminal.gcharset = 3;
228 this._terminal.gcharset = 1;
231 this._terminal.gcharset = 2;
234 this.state = ParserState.CHARSET;
237 // Designate G3 Character Set (VT300).
238 // A = ISO Latin-1 Supplemental.
241 this._terminal.gcharset = 3;
242 this.state = ParserState.CHARSET;
247 // Single Shift Select of G2 Character Set
248 // ( SS2 is 0x8e). This affects next character only.
252 // Single Shift Select of G3 Character Set
253 // ( SS3 is 0x8f). This affects next character only.
257 // Invoke the G2 Character Set as GL (LS2).
259 this._terminal.setgLevel(2);
262 // Invoke the G3 Character Set as GL (LS3).
264 this._terminal.setgLevel(3);
267 // Invoke the G3 Character Set as GR (LS3R).
269 this._terminal.setgLevel(3);
272 // Invoke the G2 Character Set as GR (LS2R).
274 this._terminal.setgLevel(2);
277 // Invoke the G1 Character Set as GR (LS1R).
279 this._terminal.setgLevel(1);
282 // ESC 7 Save Cursor (DECSC).
284 this._inputHandler.saveCursor();
285 this.state = ParserState.NORMAL;
288 // ESC 8 Restore Cursor (DECRC).
290 this._inputHandler.restoreCursor();
291 this.state = ParserState.NORMAL;
294 // ESC # 3 DEC line height/width
296 this.state = ParserState.NORMAL;
300 // ESC H Tab Set (HTS is 0x88).
302 this._terminal.tabSet();
303 this.state = ParserState.NORMAL;
306 // ESC = Application Keypad (DECKPAM).
308 this._terminal.log('Serial port requested application keypad.');
309 this._terminal.applicationKeypad = true;
310 this._terminal.viewport.syncScrollArea();
311 this.state = ParserState.NORMAL;
314 // ESC > Normal Keypad (DECKPNM).
316 this._terminal.log('Switching back to normal keypad.');
317 this._terminal.applicationKeypad = false;
318 this._terminal.viewport.syncScrollArea();
319 this.state = ParserState.NORMAL;
323 this.state = ParserState.NORMAL;
324 this._terminal.error('Unknown ESC control: %s.', ch);
329 case ParserState.CHARSET:
331 case '0': // DEC Special Character and Line Drawing Set.
337 case 'B': // United States (USASCII).
345 cs = CHARSETS.Finnish;
348 cs = CHARSETS.French;
350 case 'Q': // FrenchCanadian
351 cs = CHARSETS.FrenchCanadian;
354 cs = CHARSETS.German;
357 cs = CHARSETS.Italian;
359 case 'E': // NorwegianDanish
361 cs = CHARSETS.NorwegianDanish;
364 cs = CHARSETS.Spanish;
368 cs = CHARSETS.Swedish;
373 case '/': // ISOLatin (actually /A)
374 cs = CHARSETS.ISOLatin;
381 this._terminal.setgCharset(this._terminal.gcharset, cs);
382 this._terminal.gcharset = null;
383 this.state = ParserState.NORMAL;
386 case ParserState.OSC:
389 // Set Text Parameters.
390 if (ch === C0.ESC || ch === C0.BEL) {
391 if (ch === C0.ESC) i++;
393 this._terminal.params.push(this._terminal.currentParam);
395 switch (this._terminal.params[0]) {
399 if (this._terminal.params[1]) {
400 this._terminal.title = this._terminal.params[1];
401 this._terminal.handleTitle(this._terminal.title);
409 // change dynamic colors
421 // change dynamic ui colors
433 // manipulate selection data
450 this._terminal.params = [];
451 this._terminal.currentParam = 0;
452 this.state = ParserState.NORMAL;
454 if (!this._terminal.params.length) {
455 if (ch >= '0' && ch <= '9') {
456 this._terminal.currentParam =
457 this._terminal.currentParam * 10 + ch.charCodeAt(0) - 48;
458 } else if (ch === ';') {
459 this._terminal.params.push(this._terminal.currentParam);
460 this._terminal.currentParam = '';
463 this._terminal.currentParam += ch;
468 case ParserState.CSI_PARAM:
469 if (ch in csiParamStateHandler) {
470 csiParamStateHandler[ch](this);
473 this.finalizeParam();
474 // Fall through the CSI as this character should be the CSI code.
475 this.state = ParserState.CSI;
477 case ParserState.CSI:
478 if (ch in csiStateHandler) {
479 csiStateHandler[ch](this._inputHandler, this._terminal.params, this._terminal.prefix);
481 this._terminal.error('Unknown CSI code: %s.', ch);
484 this.state = ParserState.NORMAL;
485 this._terminal.prefix = '';
486 this._terminal.postfix = '';
489 case ParserState.DCS:
490 if (ch === C0.ESC || ch === C0.BEL) {
491 if (ch === C0.ESC) i++;
493 switch (this._terminal.prefix) {
494 // User-Defined Keys (DECUDK).
498 // Request Status String (DECRQSS).
499 // test: echo -e '\eP$q"p\e\\'
501 let pt = this._terminal.currentParam
518 + (this._terminal.scrollTop + 1)
520 + (this._terminal.scrollBottom + 1)
530 this._terminal.error('Unknown DCS Pt: %s.', pt);
535 this._terminal.send(C0.ESC + 'P' + +valid + '$r' + pt + C0.ESC + '\\');
538 // Set Termcap/Terminfo Data (xterm, experimental).
542 // Request Termcap/Terminfo String (xterm, experimental)
543 // Regular xterm does not even respond to this sequence.
544 // This can cause a small glitch in vim.
545 // test: echo -ne '\eP+q6b64\e\\'
547 // TODO: Don't declare pt twice
548 /*let*/ pt = this._terminal.currentParam
551 this._terminal.send(C0.ESC + 'P' + +valid + '+r' + pt + C0.ESC + '\\');
555 this._terminal.error('Unknown DCS prefix: %s.', this._terminal.prefix);
559 this._terminal.currentParam = 0;
560 this._terminal.prefix = '';
561 this.state = ParserState.NORMAL;
562 } else if (!this._terminal.currentParam) {
563 if (!this._terminal.prefix && ch !== '$' && ch !== '+') {
564 this._terminal.currentParam = ch;
565 } else if (this._terminal.prefix.length === 2) {
566 this._terminal.currentParam = ch;
568 this._terminal.prefix += ch;
571 this._terminal.currentParam += ch;
575 case ParserState.IGNORE:
577 if (ch === C0.ESC || ch === C0.BEL) {
578 if (ch === C0.ESC) i++;
579 this.state = ParserState.NORMAL;
586 public setState(state: ParserState): void {
590 public setPrefix(prefix: string): void {
591 this._terminal.prefix = prefix;
594 public setParam(param: number) {
595 this._terminal.currentParam = param;
598 public getParam(): number {
599 return this._terminal.currentParam;
602 public finalizeParam(): void {
603 this._terminal.params.push(this._terminal.currentParam);
604 this._terminal.currentParam = 0;
607 public setPostfix(postfix: string): void {
608 this._terminal.postfix = postfix;