-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterm.js
1460 lines (1348 loc) · 43.4 KB
/
term.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Javascript terminal
*
* Copyright (c) 2011-2020 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
"use strict";
function Term(options)
{
var width, height, tot_height, scrollback;
function dummy_key_handler()
{
}
width = options.cols ? options.cols : 80;
height = options.rows ? options.rows : 25;
scrollback = options.scrollback ? options.scrollback : 0;
this.font_size = options.fontSize ? options.fontSize : 15;
this.w = width;
this.h = height;
this.cur_h = height; /* current height of the scroll back buffer */
tot_height = height + scrollback;
this.tot_h = tot_height; /* maximum height of the scroll back buffer */
/* y_base and y_disp are index in the circular buffer lines of
length cur_h. They are defined modulo tot_h, i.e. they wrap
when cur_h = tot_h. If cur_h < tot_h, y_base is always equal to
cur_h - h. */
this.y_base = 0; /* position of the current top screen line in the
scroll back buffer */
this.y_disp = 0; /* position of the top displayed line in the
scroll back buffer */
/* cursor position */
this.x = 0;
this.y = 0;
this.scroll_top = 0;
this.scroll_bottom = this.h;
this.cursorstate = 0;
this.handler = dummy_key_handler;
this.state = 0;
this.output_queue = "";
this.colors = [
/* normal */
"#000000",
"#aa0000",
"#00aa00",
"#aa5500",
"#0000aa",
"#aa00aa",
"#00aaaa",
"#aaaaaa",
/* bright */
"#555555",
"#ff5555",
"#55ff55",
"#ffff55",
"#5555ff",
"#ff55ff",
"#55ffff",
"#ffffff"
];
/* attributes bits:
0-3: bg
4-7: fg
8: bold
9: inverse
*/
this.def_attr = (7 << 4) | 0;
this.cur_attr = this.def_attr;
this.is_mac = (navigator.userAgent.indexOf("Mac") >=0 ) ? true : false;
this.key_rep_state = 0;
this.key_rep_str = "";
this.utf8 = true;
this.utf8_state = 0;
this.utf8_val = 0;
this.application_cursor = false;
this.application_keypad = false;
/* if true, emulate some behaviors of the Linux console */
this.linux_console = true;
this.textarea_has_focus = false;
}
Term.prototype.setKeyHandler = function(handler)
{
this.handler = handler;
}
/* return the size of a character in CSS pixels using the selected font */
function term_get_char_size(parent_el, font_size)
{
var el, g, ret;
el = document.createElement("div");
el.classList.add("term", "term_char_size");
el.style.fontSize = font_size + "px";
el.textContent = "W";
parent_el.appendChild(el);
g = el.getBoundingClientRect();
/* the character width & height may not be an integer */
ret = [g.width, g.height];
return ret;
}
Term.prototype.open = function(parent_el)
{
var y, line, i, term, c, row_el, char_size_ret;
/* set initial content */
this.lines = new Array();
c = 32 | (this.def_attr << 16);
for(y = 0; y < this.cur_h;y++) {
line = new Array();
for(i=0;i<this.w;i++)
line[i] = c;
this.lines[y] = line;
}
char_size_ret = term_get_char_size(parent_el, this.font_size);
/* size of the character in CSS pixels */
this.char_width = char_size_ret[0];
this.char_height = char_size_ret[1];
this.scrollbar_width = 15;
/* size of term_el in CSS pixels */
this.term_width = Math.ceil(this.w * this.char_width) +
this.scrollbar_width;
this.term_height = Math.ceil(this.h * this.char_height);
/* create the terminal window */
this.term_el = document.createElement("div");
this.term_el.className = "term";
/* XXX: could compute the font metrics */
this.term_el.style.fontSize = this.font_size + "px";
this.term_el.style.width = this.term_width + "px";
this.term_el.style.height = this.term_height + "px";
/* allow the terminal to take the focus */
this.term_el.setAttribute("tabindex", "0");
/* scroll bar */
this.scrollbar_el = document.createElement("div");
this.scrollbar_el.className = "term_scrollbar";
this.scrollbar_el.style.width = this.scrollbar_width + "px";
this.term_el.appendChild(this.scrollbar_el);
this.track_el = document.createElement("div");
this.track_el.className = "term_track";
this.track_el.onmousedown = this.mouseMoveHandler.bind(this);
this.scrollbar_el.appendChild(this.track_el);
this.thumb_el = document.createElement("div");
this.thumb_el.className = "term_thumb";
this.thumb_el.onmousedown = this.mouseDownHandler.bind(this);
this.track_el.appendChild(this.thumb_el);
this.end_el = document.createElement("div");
this.end_el.className = "term_end";
this.thumb_el.appendChild(this.end_el);
/* current scrollbar position */
this.thumb_size = -1;
this.thumb_pos = -1;
/* terminal content */
this.content_el = document.createElement("div");
this.content_el.className = "term_content";
this.content_el.style.width = (this.w) + "ch";
this.term_el.appendChild(this.content_el);
this.rows_el = [];
for(y=0;y<this.h;y++) {
row_el = document.createElement("div");
this.rows_el.push(row_el);
this.content_el.appendChild(row_el);
}
/* dummy textarea to get the input events and for the virtual
keyboard on mobile devices */
this.textarea_el = document.createElement("textarea");
this.textarea_el.classList.add("term_textarea");
this.textarea_el.setAttribute("autocorrect", "off");
this.textarea_el.setAttribute("autocapitalize", "off");
this.textarea_el.setAttribute("spellcheck", "false");
this.textarea_el.setAttribute("tabindex", "-1");
this.term_el.appendChild(this.textarea_el);
this.parent_el = parent_el;
parent_el.appendChild(this.term_el);
this.refresh(0, this.h - 1);
/* textarea_el events */
// key handler
this.textarea_el.addEventListener("keydown",
this.keyDownHandler.bind(this), true);
this.textarea_el.addEventListener("keyup",
this.keyUpHandler.bind(this), true);
/* keypress is deprecated, so use input */
this.textarea_el.addEventListener("input",
this.inputHandler.bind(this), true);
this.textarea_el.addEventListener("focus",
this.focusHandler.bind(this), true);
this.textarea_el.addEventListener("blur",
this.blurHandler.bind(this), true);
/* term_el events */
this.term_el.addEventListener("keydown",
this.termKeyDownHandler.bind(this),
true);
this.term_el.addEventListener("paste",
this.pasteHandler.bind(this), true);
this.term_el.addEventListener("mouseup",
this.termMouseUpHandler.bind(this),
true);
this.term_el.addEventListener("wheel",
this.wheelHandler.bind(this), false);
// cursor blinking
term = this;
setInterval(function() { term.cursor_timer_cb(); }, 1000);
this.term_el.focus();
};
Term.prototype.refresh_scrollbar = function ()
{
var total_size, thumb_pos, thumb_size, y, y0;
total_size = this.term_el.clientHeight;
thumb_size = Math.ceil(this.h * total_size / this.cur_h);
/* position of the first line of the scroll back buffer */
y0 = (this.y_base + this.h) % this.cur_h;
y = this.y_disp - y0;
if (y < 0)
y += this.cur_h;
thumb_pos = Math.floor(y * total_size / this.cur_h);
thumb_size = Math.max(thumb_size, 30);
thumb_size = Math.min(thumb_size, total_size);
thumb_pos = Math.min(thumb_pos, total_size - thumb_size);
// console.log("pos=" + thumb_pos + " size=" + thumb_size);
if (thumb_pos != this.thumb_pos || thumb_size != this.thumb_size) {
this.thumb_pos = thumb_pos;
this.thumb_size = thumb_size;
this.thumb_el.style.top = thumb_pos + "px";
this.thumb_el.style.height = thumb_size + "px";
}
}
/* move the text area at the cursor position so that the browser shows
* the correct position when the virtual keyboard is used */
Term.prototype.move_textarea = function()
{
var x, y, base_x, base_y, pos;
pos = this.term_el.getBoundingClientRect();
base_x = pos.left + window.scrollX;
base_y = pos.top + window.scrollY;
/* position relative to the body */
x = Math.ceil(this.x * this.char_width + base_x);
y = Math.ceil(this.y * this.char_height + base_y);
this.textarea_el.style.width = Math.ceil(this.char_width) + "px";
this.textarea_el.style.height = Math.ceil(this.char_height) + "px";
this.textarea_el.style.left = x + "px";
this.textarea_el.style.top = y + "px";
this.textarea_el.style.zIndex = 1000;
}
Term.prototype.refresh = function(ymin, ymax)
{
var el, y, line, outline, c, w, i, j, cx, attr, last_attr, fg, bg, y1;
var http_link_len, http_link_str, bold, tmp, inverse;
function is_http_link_char(c)
{
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=`.";
return str.indexOf(String.fromCharCode(c)) >= 0;
}
function right_trim(str, a)
{
var i, n;
n = a.length;
i = str.length;
while (i >= n && str.substr(i - n, n) == a)
i -= n;
return str.substr(0, i);
}
for(y = ymin; y <= ymax; y++) {
/* convert to HTML string */
y1 = y + this.y_disp;
if (y1 >= this.cur_h)
y1 -= this.cur_h;
line = this.lines[y1];
outline = "";
w = this.w;
if (y == this.y && this.cursor_state &&
this.y_disp == this.y_base) {
cx = this.x;
} else {
cx = -1;
}
last_attr = this.def_attr;
http_link_len = 0;
for(i = 0; i < w; i++) {
c = line[i];
attr = c >> 16;
c &= 0xffff;
/* test for http link */
if (c == 0x68 && (w - i) >= 8 && http_link_len == 0) {
/* test http:// or https:// */
if ((line[i + 1] & 0xffff) == 0x74 &&
(line[i + 2] & 0xffff) == 0x74 &&
(line[i + 3] & 0xffff) == 0x70 &&
(((line[i + 4] & 0xffff) == 0x3a &&
(line[i + 5] & 0xffff) == 0x2f &&
(line[i + 6] & 0xffff) == 0x2f) ||
((line[i + 4] & 0xffff) == 0x73 &&
(line[i + 5] & 0xffff) == 0x3a &&
(line[i + 6] & 0xffff) == 0x2f &&
(line[i + 7] & 0xffff) == 0x2f))) {
http_link_str = "";
j = 0;
while ((i + j) < w &&
is_http_link_char(line[i + j] & 0xffff)) {
http_link_str += String.fromCharCode(line[i + j] & 0xffff);
j++;
}
http_link_len = j;
if (last_attr != this.def_attr) {
outline += '</span>';
last_attr = this.def_attr;
}
outline += "<a href='" + http_link_str + "'>";
}
}
if (i == cx) {
attr = -1; /* cursor */
}
if (attr != last_attr) {
if (last_attr != this.def_attr)
outline += '</span>';
if (attr != this.def_attr) {
if (attr == -1) {
/* cursor */
outline += '<span class="term_cursor">';
} else {
outline += '<span style="';
fg = (attr >> 4) & 0xf;
bg = attr & 0xf;
bold = (attr >> 8) & 1;
inverse = (attr >> 9) & 1;
if (inverse) {
tmp = fg;
fg = bg;
bg = tmp;
}
if (bold) {
/* metrics are not OK for all fonts, so disabled */
/* outline += 'font-weight:bold;'; */
/* use the bright color */
if (fg < 8)
fg += 8;
}
if (fg != 7) {
outline += 'color:' + this.colors[fg] + ';';
}
if (bg != 0) {
outline += 'background-color:' +
this.colors[bg] + ';';
}
outline += '">';
}
}
}
switch(c) {
case 32:
outline += " ";
break;
case 38: // '&'
outline += "&";
break;
case 60: // '<'
outline += "<";
break;
case 62: // '>'
outline += ">";
break;
default:
if (c < 32) {
outline += " ";
} else {
outline += String.fromCharCode(c);
}
break;
}
last_attr = attr;
if (http_link_len != 0) {
http_link_len--;
if (http_link_len == 0) {
if (last_attr != this.def_attr) {
outline += '</span>';
last_attr = this.def_attr;
}
outline += "</a>";
}
}
}
if (last_attr != this.def_attr) {
outline += '</span>';
}
/* trim trailing spaces for copy/paste */
outline = right_trim(outline, " ");
if (outline == "")
outline = " ";
this.rows_el[y].innerHTML = outline;
}
this.refresh_scrollbar();
this.move_textarea();
};
Term.prototype.cursor_timer_cb = function()
{
this.cursor_state ^= 1;
this.refresh(this.y, this.y);
};
Term.prototype.show_cursor = function()
{
if (!this.cursor_state) {
this.cursor_state = 1;
this.refresh(this.y, this.y);
}
};
/* scroll down or up in the scroll back buffer by n lines */
Term.prototype.scroll_disp = function(n)
{
var i, y1;
/* slow but it does not really matters */
if (n >= 0) {
for(i = 0; i < n; i++) {
if (this.y_disp == this.y_base)
break;
if (++this.y_disp == this.cur_h)
this.y_disp = 0;
}
} else {
n = -n;
y1 = this.y_base + this.h;
if (y1 >= this.cur_h)
y1 -= this.cur_h;
for(i = 0; i < n; i++) {
if (this.y_disp == y1)
break;
if (--this.y_disp < 0)
this.y_disp = this.cur_h - 1;
}
}
this.refresh(0, this.h - 1);
};
Term.prototype.write = function(str)
{
var s, ymin, ymax;
function update(y)
{
ymin = Math.min(ymin, y);
ymax = Math.max(ymax, y);
}
function get_erase_char()
{
var bg_mask, attr;
bg_mask = 0xf;
attr = (s.def_attr & ~bg_mask) | (s.cur_attr & bg_mask);
return 32 | (attr << 16);
}
function erase_chars(x1, x2, y) {
var l, i, c, y1;
y1 = s.y_base + y;
if (y1 >= s.cur_h)
y1 -= s.cur_h;
l = s.lines[y1];
c = get_erase_char();
for(i = x1; i < x2; i++)
l[i] = c;
update(y);
}
function erase_to_eol(x, y) {
erase_chars(x, s.w, y);
}
function erase_in_line(n) {
switch(n) {
case 0:
erase_to_eol(s.x, s.y);
break;
case 1:
erase_chars(0, s.x + 1, s.y);
break;
case 2:
erase_chars(0, s.w, s.y);
break;
}
}
function erase_in_display(n) {
var y;
switch(n) {
case 0:
erase_to_eol(s.x, s.y);
for(y = s.y + 1; y < s.h; y++)
erase_to_eol(0, y);
break;
case 1:
erase_chars(0, s.x + 1, s.y);
for(y = 0; y < s.y; y++) {
erase_to_eol(0, y);
}
break;
case 2:
for(y = 0; y < s.h; y++) {
erase_to_eol(0, y);
}
break;
}
}
function delete_chars(n)
{
var l, i, c, y1, j;
y1 = s.y + s.y_base;
if (y1 >= s.cur_h)
y1 -= s.cur_h;
l = s.lines[y1];
if (n < 1)
n = 1;
c = get_erase_char();
j = s.x + n;
for(i = s.x; i < s.w; i++) {
if (j < s.w)
l[i] = l[j];
else
l[i] = c;
j++;
}
update(s.y);
}
function insert_chars(n)
{
var l, i, c, y1, x1;
if (n < 1)
n = 1;
if (n > s.w - s.x)
n = s.w - s.x;
y1 = s.y + s.y_base;
if (y1 >= s.cur_h)
y1 -= s.cur_h;
l = s.lines[y1];
x1 = s.x + n;
for(i = s.w - 1; i >= x1; i--)
l[i] = l[i - n];
c = get_erase_char();
for(i = s.x; i < x1; i++)
l[i] = c;
update(s.y);
}
function csi_colors(esc_params)
{
var j, n, fg, bg, mask;
if (esc_params.length == 0) {
s.cur_attr= s.def_attr;
} else {
for(j = 0; j < esc_params.length; j++) {
n = esc_params[j];
if (n >= 30 && n <= 37) {
/* foreground */
fg = n - 30;
s.cur_attr = (s.cur_attr & ~(0xf << 4)) | (fg << 4);
} else if (n >= 40 && n <= 47) {
/* background */
bg = n - 40;
s.cur_attr = (s.cur_attr & ~0xf) | bg;
} else if (n >= 90 && n <= 97) {
/* bright foreground */
fg = n - 90 + 8;
s.cur_attr = (s.cur_attr & ~(0xf << 4)) | (fg << 4);
} else if (n >= 100 && n <= 107) {
/* bright background */
bg = n - 100 + 8;
s.cur_attr = (s.cur_attr & ~0xf) | bg;
} else if (n == 1) {
/* bold + bright */
s.cur_attr |= (1 << 8);
} else if (n == 0) {
/* default attr */
s.cur_attr = s.def_attr;
} else if (n == 7) {
/* inverse */
s.cur_attr |= (1 << 9);
} else if (n == 27) {
/* not inverse */
s.cur_attr &= ~(1 << 9);
} else if (n == 39) {
/* reset fg */
mask = 0x0f << 4;
s.cur_attr = (s.cur_attr & ~mask) | (s.def_attr & mask);
} else if (n == 49) {
/* reset bg */
mask = 0x0f;
s.cur_attr = (s.cur_attr & ~mask) | (s.def_attr & mask);
}
}
}
}
function empty_line(y, use_erase_char) {
var line, c, y1, x;
if (use_erase_char)
c = get_erase_char();
else
c = 32 | (s.def_attr << 16);
line = new Array();
for(x=0;x<s.w;x++)
line[x] = c;
y1 = s.y_base + y;
if (y1 >= s.cur_h)
y1 -= s.cur_h;
s.lines[y1] = line;
}
function scroll_down(top, bottom, use_erase_char)
{
var y, line, y1, y2;
if (top == 0 && bottom == s.h) {
/* increase height of buffer if possible */
if (s.cur_h < s.tot_h) {
s.cur_h++;
}
/* move down one line */
if (++s.y_base == s.cur_h)
s.y_base = 0;
s.y_disp = s.y_base;
} else {
/* partial scroll */
for(y = top; y < bottom - 1; y++) {
y1 = s.y_base + y;
if (y1 >= s.cur_h)
y1 -= s.cur_h;
y2 = y1 + 1;
if (y2 >= s.cur_h)
y2 -= s.cur_h;
s.lines[y1] = s.lines[y2];
}
}
empty_line(bottom - 1, use_erase_char);
update(top);
update(bottom - 1);
}
function scroll_up(top, bottom, use_erase_char) {
var y, y1, y2;
/* XXX: could scroll in the history */
for(y = bottom - 1; y > top; y--) {
y1 = s.y_base + y;
if (y1 >= s.cur_h)
y1 -= s.cur_h;
y2 = y1 - 1;
if (y2 >= s.cur_h)
y2 -= s.cur_h;
s.lines[y1] = s.lines[y2];
}
empty_line(top, use_erase_char);
update(top);
update(bottom - 1);
}
function down_with_scroll() {
s.y++;
if (s.y == s.scroll_bottom) {
s.y--;
scroll_down(s.scroll_top, s.scroll_bottom, false);
} else if (s.y >= s.h) {
s.y--;
scroll_down(0, s.h, false);
}
}
function up_with_scroll() {
if (s.y == s.scroll_top) {
scroll_up(s.scroll_top, s.scroll_bottom, true);
} else if (s.y == 0) {
scroll_up(0, s.h, true);
} else {
s.y--;
}
}
function insert_lines(n) {
var y2;
if (n < 1)
n = 1;
if (s.y < s.scroll_bottom)
y2 = s.scroll_bottom;
else
y2 = s.h;
while (n != 0) {
scroll_up(s.y, y2, true);
n--;
}
}
function delete_lines(n) {
var y2;
if (n < 1)
n = 1;
if (s.y < s.scroll_bottom)
y2 = s.scroll_bottom;
else
y2 = s.h;
while (n != 0) {
scroll_down(s.y, y2, true);
n--;
}
}
var TTY_STATE_NORM = 0;
var TTY_STATE_ESC = 1;
var TTY_STATE_CSI = 2;
var TTY_STATE_CHARSET = 3;
function handle_char(c) {
var i, l, n, j, y1, y2, x1;
switch(s.state) {
case TTY_STATE_NORM:
switch(c) {
case 10:
down_with_scroll();
break;
case 13:
s.x = 0;
break;
case 8:
if (s.x > 0) {
s.x--;
}
break;
case 9: /* tab */
n = (s.x + 8) & ~7;
if (n <= s.w) {
s.x = n;
}
break;
case 27:
s.state = TTY_STATE_ESC;
break;
default:
if (c >= 32) {
if (s.x >= s.w) {
s.x = 0;
down_with_scroll();
}
y1 = s.y + s.y_base;
if (y1 >= s.cur_h)
y1 -= s.cur_h;
s.lines[y1][s.x] = (c & 0xffff) |
(s.cur_attr << 16);
s.x++;
update(s.y);
}
break;
}
break;
case TTY_STATE_ESC:
switch(c) {
case 91: // '['
s.esc_params = new Array();
s.cur_param = 0;
s.esc_prefix = 0;
s.state = TTY_STATE_CSI;
break;
case 40: // '('
case 41: // ')'
s.state = TTY_STATE_CHARSET;
break;
case 61: // '='
s.application_keypad = true;
s.state = TTY_STATE_NORM;
break;
case 62: // '>'
s.application_keypad = false;
s.state = TTY_STATE_NORM;
break;
case 77: // 'M'
up_with_scroll();
s.state = TTY_STATE_NORM;
break;
default:
s.state = TTY_STATE_NORM;
break;
}
break;
case TTY_STATE_CSI:
if (c >= 48 && c <= 57) { // '0' '9'
/* numeric */
s.cur_param = s.cur_param * 10 + c - 48;
} else {
if (c == 63) { // '?'
s.esc_prefix = c;
break;
}
/* add parsed parameter */
s.esc_params[s.esc_params.length] = s.cur_param;
s.cur_param = 0;
if (c == 59) // ;
break;
s.state = TTY_STATE_NORM;
// console.log("term: csi=" + s.esc_params + " cmd="+c);
switch(c) {
case 64: // '@' insert chars
insert_chars(s.esc_params[0]);
break;
case 65: // 'A' up
n = s.esc_params[0];
if (n < 1)
n = 1;
s.y -= n;
if (s.y < 0)
s.y = 0;
break;
case 66: // 'B' down
n = s.esc_params[0];
if (n < 1)
n = 1;
s.y += n;
if (s.y >= s.h)
s.y = s.h - 1;
break;
case 67: // 'C' right
n = s.esc_params[0];
if (n < 1)
n = 1;
s.x += n;
if (s.x >= s.w - 1)
s.x = s.w - 1;
break;
case 68: // 'D' left
n = s.esc_params[0];
if (n < 1)
n = 1;
s.x -= n;
if (s.x < 0)
s.x = 0;
break;
case 71: /* 'G' cursor character absolute */
x1 = s.esc_params[0] - 1;
if (x1 < 0)
x1 = 0;
else if (x1 >= s.w)
x1 = s.w - 1;
s.x = x1;
break;
case 72: // 'H' goto xy
y1 = s.esc_params[0] - 1;
if (s.esc_params.length >= 2)
x1 = s.esc_params[1] - 1;
else
x1 = 0;
if (y1 < 0)
y1 = 0;
else if (y1 >= s.h)
y1 = s.h - 1;
if (x1 < 0)
x1 = 0;
else if (x1 >= s.w)
x1 = s.w - 1;
s.x = x1;
s.y = y1;
break;
case 74: // 'J' erase in display
erase_in_display(s.esc_params[0]);
break;
case 75: // 'K' erase in line
erase_in_line(s.esc_params[0]);
break;
case 76: // 'L' insert lines
insert_lines(s.esc_params[0]);
break;
case 77: // 'M' insert lines
delete_lines(s.esc_params[0]);
break;
case 80: // 'P'
delete_chars(s.esc_params[0]);
break;
case 100: // 'd' line position absolute
{
y1 = s.esc_params[0] - 1;
if (y1 < 0)
y1 = 0;
else if (y1 >= s.h)
y1 = s.h - 1;
s.y = y1;
}
break;
case 104: // 'h': set mode
if (s.esc_prefix == 63 && s.esc_params[0] == 1) {
s.application_cursor = true;
}
break;
case 108: // 'l': reset mode
if (s.esc_prefix == 63 && s.esc_params[0] == 1) {
s.application_cursor = false;
}
break;
case 109: // 'm': set color
csi_colors(s.esc_params);
break;
case 110: // 'n' return the cursor position
s.queue_chars("\x1b[" + (s.y + 1) + ";" + (s.x + 1) + "R");
break;
case 114: // 'r' set scroll region
y1 = s.esc_params[0] - 1;
if (y1 < 0)
y1 = 0;
else if (y1 >= s.h)
y1 = s.h - 1;
if (s.esc_params.length >= 2)
y2 = s.esc_params[1];
else
y2 = s.h;
if (y2 >= s.h || y2 <= y1)
y2 = s.h;
s.scroll_top = y1;
s.scroll_bottom = y2;
s.x = 0;
s.y = 0;
break;
default:
break;
}
}
break;
case TTY_STATE_CHARSET:
/* just ignore */
s.state = TTY_STATE_NORM;
break;
}
}
function handle_utf8(c) {
if (s.utf8_state !== 0 && (c >= 0x80 && c < 0xc0)) {
s.utf8_val = (s.utf8_val << 6) | (c & 0x3F);
s.utf8_state--;
if (s.utf8_state === 0) {
handle_char(s.utf8_val);
}
} else if (c >= 0xc0 && c < 0xf8) {
s.utf8_state = 1 + (c >= 0xe0) + (c >= 0xf0);
s.utf8_val = c & ((1 << (6 - s.utf8_state)) - 1);
} else {
s.utf8_state = 0;
handle_char(c);
}
}