]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/Parser.ts
Support more CSI codes, remove unused CSI helper functions
[mirror_xterm.js.git] / src / Parser.ts
1 import { C0 } from './EscapeSequences';
2 import { IInputHandler } from './Interfaces';
3 import { CHARSETS } from './Charsets';
4
5 const normalStateHandler: {[key: string]: (handler: IInputHandler) => void} = {};
6 normalStateHandler[C0.BEL] = (handler) => handler.bell();
7 normalStateHandler[C0.LF] = (handler) => handler.lineFeed();
8 normalStateHandler[C0.VT] = normalStateHandler[C0.LF];
9 normalStateHandler[C0.FF] = normalStateHandler[C0.LF];
10 normalStateHandler[C0.CR] = (handler) => handler.carriageReturn();
11 normalStateHandler[C0.BS] = (handler) => handler.backspace();
12 normalStateHandler[C0.HT] = (handler) => handler.tab();
13 normalStateHandler[C0.SO] = (handler) => handler.shiftOut();
14 normalStateHandler[C0.SI] = (handler) => handler.shiftIn();
15 // TODO: Add ESC and Default cases to normalStateHandler
16
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();
36
37 const csiStateHandler: {[key: string]: (handler: IInputHandler, params: number[]) => void} = {};
38 csiStateHandler['A'] = (handler, params) => handler.cursorUp(params);
39 csiStateHandler['B'] = (handler, params) => handler.cursorDown(params);
40 csiStateHandler['C'] = (handler, params) => handler.cursorForward(params);
41 csiStateHandler['D'] = (handler, params) => handler.cursorBackward(params);
42 csiStateHandler['H'] = (handler, params) => handler.cursorPosition(params);
43 csiStateHandler['J'] = (handler, params) => handler.eraseInDisplay(params);
44 csiStateHandler['K'] = (handler, params) => handler.eraseInLine(params);
45 csiStateHandler['m'] = (handler, params) => handler.charAttributes(params);
46 csiStateHandler['n'] = (handler, params) => handler.deviceStatus(params);
47
48 enum ParserState {
49 NORMAL = 0,
50 ESCAPED = 1,
51 CSI_PARAM = 2,
52 CSI = 3,
53 OSC = 4,
54 CHARSET = 5,
55 DCS = 6,
56 IGNORE = 7
57 }
58
59 export class Parser {
60 private state: ParserState;
61
62 // TODO: Remove terminal when handler can do everything
63 constructor(
64 private _inputHandler: IInputHandler,
65 private _terminal: any
66 ) {
67 this.state = ParserState.NORMAL;
68 }
69
70 public parse(data: string) {
71 let l = data.length, i = 0, j, cs, ch, code, low, ch_width, row;
72
73 // apply leftover surrogate high from last write
74 if (this._terminal.surrogate_high) {
75 data = this._terminal.surrogate_high + data;
76 this._terminal.surrogate_high = '';
77 }
78
79 for (; i < l; i++) {
80 ch = data[i];
81
82 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
83 // --> maybe move to default
84 code = data.charCodeAt(i);
85 if (0xD800 <= code && code <= 0xDBFF) {
86 // we got a surrogate high
87 // get surrogate low (next 2 bytes)
88 low = data.charCodeAt(i + 1);
89 if (isNaN(low)) {
90 // end of data stream, save surrogate high
91 this._terminal.surrogate_high = ch;
92 continue;
93 }
94 code = ((code - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
95 ch += data.charAt(i + 1);
96 }
97 // surrogate low - already handled above
98 if (0xDC00 <= code && code <= 0xDFFF)
99 continue;
100
101 if (this.state === ParserState.NORMAL) {
102 if (ch in normalStateHandler) {
103 normalStateHandler[ch](this._inputHandler);
104 // Skip switch statement (eventually everything will be handled this way
105 continue;
106 }
107 }
108
109 switch (this.state) {
110 case ParserState.NORMAL:
111 switch (ch) {
112 case C0.ESC:
113 this.state = ParserState.ESCAPED;
114 break;
115
116 default:
117 // ' '
118 // calculate print space
119 // expensive call, therefore we save width in line buffer
120 ch_width = wcwidth(code);
121
122 if (ch >= ' ') {
123 if (this._terminal.charset && this._terminal.charset[ch]) {
124 ch = this._terminal.charset[ch];
125 }
126
127 row = this._terminal.y + this._terminal.ybase;
128
129 // insert combining char in last cell
130 // FIXME: needs handling after cursor jumps
131 if (!ch_width && this._terminal.x) {
132 // dont overflow left
133 if (this._terminal.lines.get(row)[this._terminal.x - 1]) {
134 if (!this._terminal.lines.get(row)[this._terminal.x - 1][2]) {
135
136 // found empty cell after fullwidth, need to go 2 cells back
137 if (this._terminal.lines.get(row)[this._terminal.x - 2])
138 this._terminal.lines.get(row)[this._terminal.x - 2][1] += ch;
139
140 } else {
141 this._terminal.lines.get(row)[this._terminal.x - 1][1] += ch;
142 }
143 this._terminal.updateRange(this._terminal.y);
144 }
145 break;
146 }
147
148 // goto next line if ch would overflow
149 // TODO: needs a global min terminal width of 2
150 if (this._terminal.x + ch_width - 1 >= this._terminal.cols) {
151 // autowrap - DECAWM
152 if (this._terminal.wraparoundMode) {
153 this._terminal.x = 0;
154 this._terminal.y++;
155 if (this._terminal.y > this._terminal.scrollBottom) {
156 this._terminal.y--;
157 this._terminal.scroll();
158 }
159 } else {
160 this._terminal.x = this._terminal.cols - 1;
161 if (ch_width === 2) // FIXME: check for xterm behavior
162 continue;
163 }
164 }
165 row = this._terminal.y + this._terminal.ybase;
166
167 // insert mode: move characters to right
168 if (this._terminal.insertMode) {
169 // do this twice for a fullwidth char
170 for (let moves = 0; moves < ch_width; ++moves) {
171 // remove last cell, if it's width is 0
172 // we have to adjust the second last cell as well
173 const removed = this._terminal.lines.get(this._terminal.y + this._terminal.ybase).pop();
174 if (removed[2] === 0
175 && this._terminal.lines.get(row)[this._terminal.cols - 2]
176 && this._terminal.lines.get(row)[this._terminal.cols - 2][2] === 2)
177 this._terminal.lines.get(row)[this._terminal.cols - 2] = [this._terminal.curAttr, ' ', 1];
178
179 // insert empty cell at cursor
180 this._terminal.lines.get(row).splice(this._terminal.x, 0, [this._terminal.curAttr, ' ', 1]);
181 }
182 }
183
184 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, ch, ch_width];
185 this._terminal.x++;
186 this._terminal.updateRange(this._terminal.y);
187
188 // fullwidth char - set next cell width to zero and advance cursor
189 if (ch_width === 2) {
190 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, '', 0];
191 this._terminal.x++;
192 }
193 }
194 break;
195 }
196 break;
197 case ParserState.ESCAPED:
198 switch (ch) {
199 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
200 case '[':
201 this._terminal.params = [];
202 this._terminal.currentParam = 0;
203 this.state = ParserState.CSI_PARAM;
204 break;
205
206 // ESC ] Operating System Command ( OSC is 0x9d).
207 case ']':
208 this._terminal.params = [];
209 this._terminal.currentParam = 0;
210 this.state = ParserState.OSC;
211 break;
212
213 // ESC P Device Control String ( DCS is 0x90).
214 case 'P':
215 this._terminal.params = [];
216 this._terminal.currentParam = 0;
217 this.state = ParserState.DCS;
218 break;
219
220 // ESC _ Application Program Command ( APC is 0x9f).
221 case '_':
222 this.state = ParserState.IGNORE;
223 break;
224
225 // ESC ^ Privacy Message ( PM is 0x9e).
226 case '^':
227 this.state = ParserState.IGNORE;
228 break;
229
230 // ESC c Full Reset (RIS).
231 case 'c':
232 this._terminal.reset();
233 break;
234
235 // ESC E Next Line ( NEL is 0x85).
236 // ESC D Index ( IND is 0x84).
237 case 'E':
238 this._terminal.x = 0;
239 ;
240 case 'D':
241 this._terminal.index();
242 this.state = ParserState.NORMAL;
243 break;
244
245 // ESC M Reverse Index ( RI is 0x8d).
246 case 'M':
247 this._terminal.reverseIndex();
248 this.state = ParserState.NORMAL;
249 break;
250
251 // ESC % Select default/utf-8 character set.
252 // @ = default, G = utf-8
253 case '%':
254 // this.charset = null;
255 this._terminal.setgLevel(0);
256 this._terminal.setgCharset(0, CHARSETS.US);
257 this.state = ParserState.NORMAL;
258 i++;
259 break;
260
261 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
262 case '(': // <-- this seems to get all the attention
263 case ')':
264 case '*':
265 case '+':
266 case '-':
267 case '.':
268 switch (ch) {
269 case '(':
270 this._terminal.gcharset = 0;
271 break;
272 case ')':
273 this._terminal.gcharset = 1;
274 break;
275 case '*':
276 this._terminal.gcharset = 2;
277 break;
278 case '+':
279 this._terminal.gcharset = 3;
280 break;
281 case '-':
282 this._terminal.gcharset = 1;
283 break;
284 case '.':
285 this._terminal.gcharset = 2;
286 break;
287 }
288 this.state = ParserState.CHARSET;
289 break;
290
291 // Designate G3 Character Set (VT300).
292 // A = ISO Latin-1 Supplemental.
293 // Not implemented.
294 case '/':
295 this._terminal.gcharset = 3;
296 this.state = ParserState.CHARSET;
297 i--;
298 break;
299
300 // ESC N
301 // Single Shift Select of G2 Character Set
302 // ( SS2 is 0x8e). This affects next character only.
303 case 'N':
304 break;
305 // ESC O
306 // Single Shift Select of G3 Character Set
307 // ( SS3 is 0x8f). This affects next character only.
308 case 'O':
309 break;
310 // ESC n
311 // Invoke the G2 Character Set as GL (LS2).
312 case 'n':
313 this._terminal.setgLevel(2);
314 break;
315 // ESC o
316 // Invoke the G3 Character Set as GL (LS3).
317 case 'o':
318 this._terminal.setgLevel(3);
319 break;
320 // ESC |
321 // Invoke the G3 Character Set as GR (LS3R).
322 case '|':
323 this._terminal.setgLevel(3);
324 break;
325 // ESC }
326 // Invoke the G2 Character Set as GR (LS2R).
327 case '}':
328 this._terminal.setgLevel(2);
329 break;
330 // ESC ~
331 // Invoke the G1 Character Set as GR (LS1R).
332 case '~':
333 this._terminal.setgLevel(1);
334 break;
335
336 // ESC 7 Save Cursor (DECSC).
337 case '7':
338 this._terminal.saveCursor();
339 this.state = ParserState.NORMAL;
340 break;
341
342 // ESC 8 Restore Cursor (DECRC).
343 case '8':
344 this._terminal.restoreCursor();
345 this.state = ParserState.NORMAL;
346 break;
347
348 // ESC # 3 DEC line height/width
349 case '#':
350 this.state = ParserState.NORMAL;
351 i++;
352 break;
353
354 // ESC H Tab Set (HTS is 0x88).
355 case 'H':
356 this._terminal.tabSet();
357 this.state = ParserState.NORMAL;
358 break;
359
360 // ESC = Application Keypad (DECKPAM).
361 case '=':
362 this._terminal.log('Serial port requested application keypad.');
363 this._terminal.applicationKeypad = true;
364 this._terminal.viewport.syncScrollArea();
365 this.state = ParserState.NORMAL;
366 break;
367
368 // ESC > Normal Keypad (DECKPNM).
369 case '>':
370 this._terminal.log('Switching back to normal keypad.');
371 this._terminal.applicationKeypad = false;
372 this._terminal.viewport.syncScrollArea();
373 this.state = ParserState.NORMAL;
374 break;
375
376 default:
377 this.state = ParserState.NORMAL;
378 this._terminal.error('Unknown ESC control: %s.', ch);
379 break;
380 }
381 break;
382
383 case ParserState.CHARSET:
384 switch (ch) {
385 case '0': // DEC Special Character and Line Drawing Set.
386 cs = CHARSETS.SCLD;
387 break;
388 case 'A': // UK
389 cs = CHARSETS.UK;
390 break;
391 case 'B': // United States (USASCII).
392 cs = CHARSETS.US;
393 break;
394 case '4': // Dutch
395 cs = CHARSETS.Dutch;
396 break;
397 case 'C': // Finnish
398 case '5':
399 cs = CHARSETS.Finnish;
400 break;
401 case 'R': // French
402 cs = CHARSETS.French;
403 break;
404 case 'Q': // FrenchCanadian
405 cs = CHARSETS.FrenchCanadian;
406 break;
407 case 'K': // German
408 cs = CHARSETS.German;
409 break;
410 case 'Y': // Italian
411 cs = CHARSETS.Italian;
412 break;
413 case 'E': // NorwegianDanish
414 case '6':
415 cs = CHARSETS.NorwegianDanish;
416 break;
417 case 'Z': // Spanish
418 cs = CHARSETS.Spanish;
419 break;
420 case 'H': // Swedish
421 case '7':
422 cs = CHARSETS.Swedish;
423 break;
424 case '=': // Swiss
425 cs = CHARSETS.Swiss;
426 break;
427 case '/': // ISOLatin (actually /A)
428 cs = CHARSETS.ISOLatin;
429 i++;
430 break;
431 default: // Default
432 cs = CHARSETS.US;
433 break;
434 }
435 this._terminal.setgCharset(this._terminal.gcharset, cs);
436 this._terminal.gcharset = null;
437 this.state = ParserState.NORMAL;
438 break;
439
440 case ParserState.OSC:
441 // OSC Ps ; Pt ST
442 // OSC Ps ; Pt BEL
443 // Set Text Parameters.
444 if (ch === C0.ESC || ch === C0.BEL) {
445 if (ch === C0.ESC) i++;
446
447 this._terminal.params.push(this._terminal.currentParam);
448
449 switch (this._terminal.params[0]) {
450 case 0:
451 case 1:
452 case 2:
453 if (this._terminal.params[1]) {
454 this._terminal.title = this._terminal.params[1];
455 this._terminal.handleTitle(this._terminal.title);
456 }
457 break;
458 case 3:
459 // set X property
460 break;
461 case 4:
462 case 5:
463 // change dynamic colors
464 break;
465 case 10:
466 case 11:
467 case 12:
468 case 13:
469 case 14:
470 case 15:
471 case 16:
472 case 17:
473 case 18:
474 case 19:
475 // change dynamic ui colors
476 break;
477 case 46:
478 // change log file
479 break;
480 case 50:
481 // dynamic font
482 break;
483 case 51:
484 // emacs shell
485 break;
486 case 52:
487 // manipulate selection data
488 break;
489 case 104:
490 case 105:
491 case 110:
492 case 111:
493 case 112:
494 case 113:
495 case 114:
496 case 115:
497 case 116:
498 case 117:
499 case 118:
500 // reset colors
501 break;
502 }
503
504 this._terminal.params = [];
505 this._terminal.currentParam = 0;
506 this.state = ParserState.NORMAL;
507 } else {
508 if (!this._terminal.params.length) {
509 if (ch >= '0' && ch <= '9') {
510 this._terminal.currentParam =
511 this._terminal.currentParam * 10 + ch.charCodeAt(0) - 48;
512 } else if (ch === ';') {
513 this._terminal.params.push(this._terminal.currentParam);
514 this._terminal.currentParam = '';
515 }
516 } else {
517 this._terminal.currentParam += ch;
518 }
519 }
520 break;
521
522 case ParserState.CSI_PARAM:
523 if (ch in csiParamStateHandler) {
524 csiParamStateHandler[ch](this);
525 break;
526 }
527 this.finalizeParam();
528 // Fall through the CSI as this character should be the CSI code.
529 this.state = ParserState.CSI;
530
531 case ParserState.CSI:
532 this.state = ParserState.NORMAL;
533
534 if (ch in csiStateHandler) {
535 csiStateHandler[ch](this._inputHandler, this._terminal.params);
536 // Skip below switch as this has handled these codes (eventually everything will be handled here
537 break;
538 }
539
540 switch (ch) {
541
542 /**
543 * Additions
544 */
545
546 // CSI Ps @
547 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
548 case '@':
549 this._terminal.insertChars(this._terminal.params);
550 break;
551
552 // CSI Ps E
553 // Cursor Next Line Ps Times (default = 1) (CNL).
554 case 'E':
555 this._terminal.cursorNextLine(this._terminal.params);
556 break;
557
558 // CSI Ps F
559 // Cursor Preceding Line Ps Times (default = 1) (CNL).
560 case 'F':
561 this._terminal.cursorPrecedingLine(this._terminal.params);
562 break;
563
564 // CSI Ps G
565 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
566 case 'G':
567 this._terminal.cursorCharAbsolute(this._terminal.params);
568 break;
569
570 // CSI Ps L
571 // Insert Ps Line(s) (default = 1) (IL).
572 case 'L':
573 this._terminal.insertLines(this._terminal.params);
574 break;
575
576 // CSI Ps M
577 // Delete Ps Line(s) (default = 1) (DL).
578 case 'M':
579 this._terminal.deleteLines(this._terminal.params);
580 break;
581
582 // CSI Ps P
583 // Delete Ps Character(s) (default = 1) (DCH).
584 case 'P':
585 this._terminal.deleteChars(this._terminal.params);
586 break;
587
588 // CSI Ps X
589 // Erase Ps Character(s) (default = 1) (ECH).
590 case 'X':
591 this._terminal.eraseChars(this._terminal.params);
592 break;
593
594 // CSI Pm ` Character Position Absolute
595 // [column] (default = [row,1]) (HPA).
596 case '`':
597 this._terminal.charPosAbsolute(this._terminal.params);
598 break;
599
600 // 141 61 a * HPR -
601 // Horizontal Position Relative
602 case 'a':
603 this._terminal.HPositionRelative(this._terminal.params);
604 break;
605
606 // CSI P s c
607 // Send Device Attributes (Primary DA).
608 // CSI > P s c
609 // Send Device Attributes (Secondary DA)
610 case 'c':
611 this._terminal.sendDeviceAttributes(this._terminal.params);
612 break;
613
614 // CSI Pm d
615 // Line Position Absolute [row] (default = [1,column]) (VPA).
616 case 'd':
617 this._terminal.linePosAbsolute(this._terminal.params);
618 break;
619
620 // 145 65 e * VPR - Vertical Position Relative
621 case 'e':
622 this._terminal.VPositionRelative(this._terminal.params);
623 break;
624
625 // CSI Ps ; Ps f
626 // Horizontal and Vertical Position [row;column] (default =
627 // [1,1]) (HVP).
628 case 'f':
629 this._terminal.HVPosition(this._terminal.params);
630 break;
631
632 // CSI Pm h Set Mode (SM).
633 // CSI ? Pm h - mouse escape codes, cursor escape codes
634 case 'h':
635 this._terminal.setMode(this._terminal.params);
636 break;
637
638 // CSI Pm l Reset Mode (RM).
639 // CSI ? Pm l
640 case 'l':
641 this._terminal.resetMode(this._terminal.params);
642 break;
643
644 // CSI Ps ; Ps r
645 // Set Scrolling Region [top;bottom] (default = full size of win-
646 // dow) (DECSTBM).
647 // CSI ? Pm r
648 case 'r':
649 this._terminal.setScrollRegion(this._terminal.params);
650 break;
651
652 // CSI s
653 // Save cursor (ANSI.SYS).
654 case 's':
655 this._terminal.saveCursor(this._terminal.params);
656 break;
657
658 // CSI u
659 // Restore cursor (ANSI.SYS).
660 case 'u':
661 this._terminal.restoreCursor(this._terminal.params);
662 break;
663
664 /**
665 * Lesser Used
666 */
667
668 // CSI Ps I
669 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
670 case 'I':
671 this._terminal.cursorForwardTab(this._terminal.params);
672 break;
673
674 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
675 case 'S':
676 this._terminal.scrollUp(this._terminal.params);
677 break;
678
679 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
680 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
681 // CSI > Ps; Ps T
682 case 'T':
683 // if (this.prefix === '>') {
684 // this.resetTitleModes(this.params);
685 // break;
686 // }
687 // if (this.params.length > 2) {
688 // this.initMouseTracking(this.params);
689 // break;
690 // }
691 if (this._terminal.params.length < 2 && !this._terminal.prefix) {
692 this._terminal.scrollDown(this._terminal.params);
693 }
694 break;
695
696 // CSI Ps Z
697 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
698 case 'Z':
699 this._terminal.cursorBackwardTab(this._terminal.params);
700 break;
701
702 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
703 case 'b':
704 this._terminal.repeatPrecedingCharacter(this._terminal.params);
705 break;
706
707 // CSI Ps g Tab Clear (TBC).
708 case 'g':
709 this._terminal.tabClear(this._terminal.params);
710 break;
711
712 // CSI Pm i Media Copy (MC).
713 // CSI ? Pm i
714 // case 'i':
715 // this.mediaCopy(this.params);
716 // break;
717
718 // CSI Pm m Character Attributes (SGR).
719 // CSI > Ps; Ps m
720 // case 'm': // duplicate
721 // if (this.prefix === '>') {
722 // this.setResources(this.params);
723 // } else {
724 // this.charAttributes(this.params);
725 // }
726 // break;
727
728 // CSI Ps n Device Status Report (DSR).
729 // CSI > Ps n
730 // case 'n': // duplicate
731 // if (this.prefix === '>') {
732 // this.disableModifiers(this.params);
733 // } else {
734 // this.deviceStatus(this.params);
735 // }
736 // break;
737
738 // CSI > Ps p Set pointer mode.
739 // CSI ! p Soft terminal reset (DECSTR).
740 // CSI Ps$ p
741 // Request ANSI mode (DECRQM).
742 // CSI ? Ps$ p
743 // Request DEC private mode (DECRQM).
744 // CSI Ps ; Ps " p
745 case 'p':
746 switch (this._terminal.prefix) {
747 // case '>':
748 // this.setPointerMode(this.params);
749 // break;
750 case '!':
751 this._terminal.softReset(this._terminal.params);
752 break;
753 // case '?':
754 // if (this.postfix === '$') {
755 // this.requestPrivateMode(this.params);
756 // }
757 // break;
758 // default:
759 // if (this.postfix === '"') {
760 // this.setConformanceLevel(this.params);
761 // } else if (this.postfix === '$') {
762 // this.requestAnsiMode(this.params);
763 // }
764 // break;
765 }
766 break;
767
768 // CSI Ps q Load LEDs (DECLL).
769 // CSI Ps SP q
770 // CSI Ps " q
771 // case 'q':
772 // if (this.postfix === ' ') {
773 // this.setCursorStyle(this.params);
774 // break;
775 // }
776 // if (this.postfix === '"') {
777 // this.setCharProtectionAttr(this.params);
778 // break;
779 // }
780 // this.loadLEDs(this.params);
781 // break;
782
783 // CSI Ps ; Ps r
784 // Set Scrolling Region [top;bottom] (default = full size of win-
785 // dow) (DECSTBM).
786 // CSI ? Pm r
787 // CSI Pt; Pl; Pb; Pr; Ps$ r
788 // case 'r': // duplicate
789 // if (this.prefix === '?') {
790 // this.restorePrivateValues(this.params);
791 // } else if (this.postfix === '$') {
792 // this.setAttrInRectangle(this.params);
793 // } else {
794 // this.setScrollRegion(this.params);
795 // }
796 // break;
797
798 // CSI s Save cursor (ANSI.SYS).
799 // CSI ? Pm s
800 // case 's': // duplicate
801 // if (this.prefix === '?') {
802 // this.savePrivateValues(this.params);
803 // } else {
804 // this.saveCursor(this.params);
805 // }
806 // break;
807
808 // CSI Ps ; Ps ; Ps t
809 // CSI Pt; Pl; Pb; Pr; Ps$ t
810 // CSI > Ps; Ps t
811 // CSI Ps SP t
812 // case 't':
813 // if (this.postfix === '$') {
814 // this.reverseAttrInRectangle(this.params);
815 // } else if (this.postfix === ' ') {
816 // this.setWarningBellVolume(this.params);
817 // } else {
818 // if (this.prefix === '>') {
819 // this.setTitleModeFeature(this.params);
820 // } else {
821 // this.manipulateWindow(this.params);
822 // }
823 // }
824 // break;
825
826 // CSI u Restore cursor (ANSI.SYS).
827 // CSI Ps SP u
828 // case 'u': // duplicate
829 // if (this.postfix === ' ') {
830 // this.setMarginBellVolume(this.params);
831 // } else {
832 // this.restoreCursor(this.params);
833 // }
834 // break;
835
836 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
837 // case 'v':
838 // if (this.postfix === '$') {
839 // this.copyRectagle(this.params);
840 // }
841 // break;
842
843 // CSI Pt ; Pl ; Pb ; Pr ' w
844 // case 'w':
845 // if (this.postfix === '\'') {
846 // this.enableFilterRectangle(this.params);
847 // }
848 // break;
849
850 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
851 // CSI Ps x Select Attribute Change Extent (DECSACE).
852 // CSI Pc; Pt; Pl; Pb; Pr$ x
853 // case 'x':
854 // if (this.postfix === '$') {
855 // this.fillRectangle(this.params);
856 // } else {
857 // this.requestParameters(this.params);
858 // //this.__(this.params);
859 // }
860 // break;
861
862 // CSI Ps ; Pu ' z
863 // CSI Pt; Pl; Pb; Pr$ z
864 // case 'z':
865 // if (this.postfix === '\'') {
866 // this.enableLocatorReporting(this.params);
867 // } else if (this.postfix === '$') {
868 // this.eraseRectangle(this.params);
869 // }
870 // break;
871
872 // CSI Pm ' {
873 // CSI Pt; Pl; Pb; Pr$ {
874 // case '{':
875 // if (this.postfix === '\'') {
876 // this.setLocatorEvents(this.params);
877 // } else if (this.postfix === '$') {
878 // this.selectiveEraseRectangle(this.params);
879 // }
880 // break;
881
882 // CSI Ps ' |
883 // case '|':
884 // if (this.postfix === '\'') {
885 // this.requestLocatorPosition(this.params);
886 // }
887 // break;
888
889 // CSI P m SP }
890 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
891 // case '}':
892 // if (this.postfix === ' ') {
893 // this.insertColumns(this.params);
894 // }
895 // break;
896
897 // CSI P m SP ~
898 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
899 // case '~':
900 // if (this.postfix === ' ') {
901 // this.deleteColumns(this.params);
902 // }
903 // break;
904
905 default:
906 this._terminal.error('Unknown CSI code: %s.', ch);
907 break;
908 }
909
910 this._terminal.prefix = '';
911 this._terminal.postfix = '';
912 break;
913
914 case ParserState.DCS:
915 if (ch === C0.ESC || ch === C0.BEL) {
916 if (ch === C0.ESC) i++;
917
918 switch (this._terminal.prefix) {
919 // User-Defined Keys (DECUDK).
920 case '':
921 break;
922
923 // Request Status String (DECRQSS).
924 // test: echo -e '\eP$q"p\e\\'
925 case '$q':
926 let pt = this._terminal.currentParam
927 , valid = false;
928
929 switch (pt) {
930 // DECSCA
931 case '"q':
932 pt = '0"q';
933 break;
934
935 // DECSCL
936 case '"p':
937 pt = '61"p';
938 break;
939
940 // DECSTBM
941 case 'r':
942 pt = ''
943 + (this._terminal.scrollTop + 1)
944 + ';'
945 + (this._terminal.scrollBottom + 1)
946 + 'r';
947 break;
948
949 // SGR
950 case 'm':
951 pt = '0m';
952 break;
953
954 default:
955 this._terminal.error('Unknown DCS Pt: %s.', pt);
956 pt = '';
957 break;
958 }
959
960 this._terminal.send(C0.ESC + 'P' + +valid + '$r' + pt + C0.ESC + '\\');
961 break;
962
963 // Set Termcap/Terminfo Data (xterm, experimental).
964 case '+p':
965 break;
966
967 // Request Termcap/Terminfo String (xterm, experimental)
968 // Regular xterm does not even respond to this sequence.
969 // This can cause a small glitch in vim.
970 // test: echo -ne '\eP+q6b64\e\\'
971 case '+q':
972 // TODO: Don't declare pt twice
973 /*let*/ pt = this._terminal.currentParam
974 , valid = false;
975
976 this._terminal.send(C0.ESC + 'P' + +valid + '+r' + pt + C0.ESC + '\\');
977 break;
978
979 default:
980 this._terminal.error('Unknown DCS prefix: %s.', this._terminal.prefix);
981 break;
982 }
983
984 this._terminal.currentParam = 0;
985 this._terminal.prefix = '';
986 this.state = ParserState.NORMAL;
987 } else if (!this._terminal.currentParam) {
988 if (!this._terminal.prefix && ch !== '$' && ch !== '+') {
989 this._terminal.currentParam = ch;
990 } else if (this._terminal.prefix.length === 2) {
991 this._terminal.currentParam = ch;
992 } else {
993 this._terminal.prefix += ch;
994 }
995 } else {
996 this._terminal.currentParam += ch;
997 }
998 break;
999
1000 case ParserState.IGNORE:
1001 // For PM and APC.
1002 if (ch === C0.ESC || ch === C0.BEL) {
1003 if (ch === C0.ESC) i++;
1004 this.state = ParserState.NORMAL;
1005 }
1006 break;
1007 }
1008 }
1009 }
1010
1011 public setPrefix(prefix: string): void {
1012 this._terminal.prefix = prefix;
1013 }
1014
1015 public setParam(param: number) {
1016 this._terminal.currentParam = param;
1017 }
1018
1019 public getParam(): number {
1020 return this._terminal.currentParam;
1021 }
1022
1023 public finalizeParam(): void {
1024 this._terminal.params.push(this._terminal.currentParam);
1025 this._terminal.currentParam = 0;
1026 }
1027
1028 public setPostfix(postfix: string): void {
1029 this._terminal.postfix = postfix;
1030 }
1031 }
1032
1033 const wcwidth = (function(opts) {
1034 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
1035 // combining characters
1036 const COMBINING = [
1037 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1038 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1039 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1040 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1041 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1042 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1043 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1044 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1045 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1046 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1047 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1048 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1049 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1050 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1051 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1052 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1053 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1054 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1055 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1056 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1057 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1058 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1059 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1060 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1061 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1062 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1063 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1064 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1065 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1066 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1067 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1068 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1069 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1070 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1071 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1072 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1073 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1074 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1075 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1076 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1077 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1078 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1079 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
1080 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1081 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1082 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1083 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1084 [0xE0100, 0xE01EF]
1085 ];
1086 // binary search
1087 function bisearch(ucs) {
1088 let min = 0;
1089 let max = COMBINING.length - 1;
1090 let mid;
1091 if (ucs < COMBINING[0][0] || ucs > COMBINING[max][1])
1092 return false;
1093 while (max >= min) {
1094 mid = Math.floor((min + max) / 2);
1095 if (ucs > COMBINING[mid][1])
1096 min = mid + 1;
1097 else if (ucs < COMBINING[mid][0])
1098 max = mid - 1;
1099 else
1100 return true;
1101 }
1102 return false;
1103 }
1104 function wcwidth(ucs) {
1105 // test for 8-bit control characters
1106 if (ucs === 0)
1107 return opts.nul;
1108 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1109 return opts.control;
1110 // binary search in table of non-spacing characters
1111 if (bisearch(ucs))
1112 return 0;
1113 // if we arrive here, ucs is not a combining or C0/C1 control character
1114 if (isWide(ucs)) {
1115 return 2;
1116 }
1117 return 1;
1118 }
1119 function isWide(ucs) {
1120 return (
1121 ucs >= 0x1100 && (
1122 ucs <= 0x115f || // Hangul Jamo init. consonants
1123 ucs === 0x2329 ||
1124 ucs === 0x232a ||
1125 (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs !== 0x303f) || // CJK..Yi
1126 (ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
1127 (ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compat Ideographs
1128 (ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
1129 (ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compat Forms
1130 (ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
1131 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
1132 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
1133 (ucs >= 0x30000 && ucs <= 0x3fffd)));
1134 }
1135 return wcwidth;
1136 })({nul: 0, control: 0}); // configurable options