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