-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVT100Screen.m
2239 lines (1888 loc) · 56.1 KB
/
VT100Screen.m
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
// -*- mode:objc -*-
// $Id: VT100Screen.m,v 1.289 2008-10-22 00:43:30 yfabian Exp $
//
/*
** VT100Screen.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
** Initial code by Kiichi Kusama
**
** Project: iTerm
**
** Description: Implements the VT100 screen.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// Debug option
#define DEBUG_ALLOC 0
#define DEBUG_METHOD_TRACE 0
#import <iTerm/iTerm.h>
#import <iTerm/VT100Screen.h>
#import <iTerm/NSStringITerm.h>
#import <iTerm/PseudoTerminal.h>
#import <iTerm/PTYTextView.h>
#import <iTerm/PTYScrollView.h>
#import <iTerm/charmaps.h>
#import <iTerm/PTYSession.h>
#import <iTerm/PTYTask.h>
#import <iTerm/PreferencePanel.h>
//#import <iTerm/iTermGrowlDelegate.h>
#import <iTerm/iTermTerminalProfileMgr.h>
#include <string.h>
#include <unistd.h>
#define MAX_SCROLLBACK_LINES 1000000
// we add a character at the end of line to indiacte wrapping
#define REAL_WIDTH (WIDTH+1)
/* translates normal char into graphics char */
void translate(screen_char_t *s, int len)
{
int i;
for(i=0;i<len;i++) s[i].ch = charmap[(int)(s[i].ch)];
}
/* pad the source string whenever double width character appears */
void padString(NSString *s, screen_char_t *buf, int fg, int bg, int *len, NSStringEncoding encoding)
{
unichar *sc;
int l=*len;
int i,j;
sc = (unichar *) malloc(l*sizeof(unichar));
[s getCharacters: sc];
for(i=j=0;i<l;i++,j++) {
buf[j].ch = sc[i];
buf[j].fg_color = fg;
buf[j].bg_color = bg;
if (sc[i]>0xa0 && [NSString isDoubleWidthCharacter:sc[i] encoding:encoding])
{
j++;
buf[j].ch = 0xffff;
buf[j].fg_color = fg;
buf[j].bg_color = bg;
}
else if (buf[j].ch == 0xfeff ||buf[j].ch == 0x200b || buf[j].ch == 0x200c || buf[j].ch == 0x200d) { //zero width space
j--;
}
}
*len=j;
free(sc);
}
// increments line pointer accounting for buffer wrap-around
static __inline__ screen_char_t *incrementLinePointer(screen_char_t *buf_start, screen_char_t *current_line,
int max_lines, int line_width, BOOL *wrap)
{
screen_char_t *next_line;
//include the wrapping indicator
line_width++;
next_line = current_line + line_width;
if(next_line >= (buf_start + line_width*max_lines))
{
next_line = buf_start;
if(wrap)
*wrap = YES;
}
else if(wrap)
*wrap = NO;
return (next_line);
}
@interface VT100Screen (Private)
- (screen_char_t *) _getLineAtIndex: (int) anIndex fromLine: (screen_char_t *) aLine;
- (screen_char_t *) _getDefaultLineWithWidth: (int) width;
- (BOOL) _addLineToScrollback;
@end
@implementation VT100Screen
#define DEFAULT_WIDTH 80
#define DEFAULT_HEIGHT 25
#define DEFAULT_FONTSIZE 14
#define DEFAULT_SCROLLBACK 1000
#define MIN_WIDTH 10
#define MIN_HEIGHT 3
#define TABSIZE 8
- (id)init
{
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
if ((self = [super init]) == nil)
return nil;
WIDTH = DEFAULT_WIDTH;
HEIGHT = DEFAULT_HEIGHT;
CURSOR_X = CURSOR_Y = 0;
SAVE_CURSOR_X = SAVE_CURSOR_Y = 0;
ALT_SAVE_CURSOR_X = ALT_SAVE_CURSOR_Y = 0;
SCROLL_TOP = 0;
SCROLL_BOTTOM = HEIGHT - 1;
TERMINAL = nil;
SHELL = nil;
buffer_lines = NULL;
dirty = NULL;
last_buffer_line = NULL;
screen_top = NULL;
scrollback_top = NULL;
temp_buffer=NULL;
max_scrollback_lines = DEFAULT_SCROLLBACK;
dynamic_scrollback_size = NO;
scrollback_overflow = 0;
[self clearTabStop];
// set initial tabs
int i;
for(i = TABSIZE; i < TABWINDOW; i += TABSIZE)
tabStop[i] = YES;
for(i=0;i<4;i++) saveCharset[i]=charset[i]=0;
// Need Growl plist stuff
//gd = [iTermGrowlDelegate sharedInstance];
return self;
}
- (void)dealloc
{
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
// free our character buffer
if(buffer_lines)
free(buffer_lines);
// free our "dirty flags" buffer
if(dirty)
free(dirty);
// free our default line
if(default_line)
free(default_line);
if (temp_buffer)
free(temp_buffer);
[printToAnsiString release];
[super dealloc];
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x, done", __PRETTY_FUNCTION__, self);
#endif
}
- (NSString *)description
{
NSString *basestr;
//NSString *colstr;
NSString *result;
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen description]", __FILE__, __LINE__);
#endif
basestr = [NSString stringWithFormat:@"WIDTH %d, HEIGHT %d, CURSOR (%d,%d)",
WIDTH, HEIGHT, CURSOR_X, CURSOR_Y];
result = [NSString stringWithFormat:@"%@\n%@", basestr, @""]; //colstr];
return result;
}
-(screen_char_t *) initScreenWithWidth:(int)width Height:(int)height
{
int total_height;
int i;
screen_char_t *aDefaultLine;
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen initScreenWithWidth:%d Height:%d]", __FILE__, __LINE__, width, height );
#endif
NSParameterAssert(width > 0 && height > 0);
WIDTH=width;
HEIGHT=height;
CURSOR_X = CURSOR_Y = 0;
SAVE_CURSOR_X = SAVE_CURSOR_Y = 0;
ALT_SAVE_CURSOR_X = ALT_SAVE_CURSOR_Y = 0;
SCROLL_TOP = 0;
SCROLL_BOTTOM = HEIGHT - 1;
blinkShow=YES;
// allocate our buffer to hold both scrollback and screen contents
total_height = HEIGHT + max_scrollback_lines;
buffer_lines = (screen_char_t *)malloc(total_height*REAL_WIDTH*sizeof(screen_char_t));
if (!buffer_lines) return NULL;
// set up our pointers
last_buffer_line = buffer_lines + (total_height - 1)*REAL_WIDTH;
screen_top = buffer_lines;
scrollback_top = buffer_lines;
// set all lines in buffer to default
default_fg_code = [TERMINAL foregroundColorCodeReal];
default_bg_code = [TERMINAL backgroundColorCodeReal];
default_line_width = WIDTH;
aDefaultLine = [self _getDefaultLineWithWidth: WIDTH];
for(i = 0; i < HEIGHT; i++)
memcpy([self _getLineAtIndex: i fromLine: buffer_lines], aDefaultLine, REAL_WIDTH*sizeof(screen_char_t));
// set current lines in scrollback
current_scrollback_lines = 0;
// set up our dirty flags buffer
dirty=(char*)malloc(HEIGHT*WIDTH*sizeof(char));
// force a redraw
[self setDirty];
return buffer_lines;
}
// gets line at specified index starting from scrollback_top
- (screen_char_t *) getLineAtIndex: (int) theIndex
{
screen_char_t *theLinePointer;
if(max_scrollback_lines == 0)
theLinePointer = screen_top;
else
theLinePointer = scrollback_top;
return ([self _getLineAtIndex:theIndex fromLine:theLinePointer]);
}
// gets line at specified index starting from screen_top
- (screen_char_t *) getLineAtScreenIndex: (int) theIndex
{
return ([self _getLineAtIndex:theIndex fromLine:screen_top]);
}
// returns NSString representation of line
- (NSString *) getLineString: (screen_char_t *) theLine
{
unichar *char_buf;
NSString *theString;
int i;
#if DEBUG_METHOD_TRACE
NSLog(@"%s", __PRETTY_FUNCTION__);
#endif
char_buf = malloc(REAL_WIDTH*sizeof(unichar));
for(i = 0; i < WIDTH; i++)
char_buf[i] = theLine[i].ch;
if (theLine[i].ch) {
char_buf[WIDTH]='\n';
theString = [NSString stringWithCharacters: char_buf length: REAL_WIDTH];
}
else
theString = [NSString stringWithCharacters: char_buf length: WIDTH];
return (theString);
}
- (void)setWidth:(int)width height:(int)height
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen setWidth:%d height:%d]",
__FILE__, __LINE__, width, height);
#endif
if (width >= MIN_WIDTH && height >= MIN_HEIGHT) {
WIDTH = width;
HEIGHT = height;
CURSOR_X = CURSOR_Y = 0;
SAVE_CURSOR_X = SAVE_CURSOR_Y = 0;
ALT_SAVE_CURSOR_X = ALT_SAVE_CURSOR_Y = 0;
SCROLL_TOP = 0;
SCROLL_BOTTOM = HEIGHT - 1;
}
}
- (void)resizeWidth:(int)width height:(int)height
{
int i, total_height, new_total_height;
screen_char_t *bl, *aLine, *c1, *c2, *new_scrollback_top;
#if DEBUG_METHOD_TRACE
NSLog(@"%s:%d :%d]", __PRETTY_FUNCTION__, width, height);
#endif
if (WIDTH == 0 || HEIGHT == 0 || (width==WIDTH && height==HEIGHT)) {
return;
}
total_height = max_scrollback_lines + HEIGHT;
// Try to determine how many empty trailing lines there are on screen
for(;HEIGHT>CURSOR_Y+1;HEIGHT--) {
aLine = [self getLineAtScreenIndex: HEIGHT-1];
for (i=0;i<WIDTH;i++)
if (aLine[i].ch) break;
if (i<WIDTH) break;
}
// create a new buffer
new_total_height = max_scrollback_lines + height;
new_scrollback_top = bl = (screen_char_t*)malloc(new_total_height*(width+1)*sizeof(screen_char_t));
//copy over the content
int y1, y2, x1, x2, x3;
BOOL wrapped = NO, _wrap;
screen_char_t *defaultLine = [self _getDefaultLineWithWidth: width];
c2 = bl;
for(y2=y1=0; y1<current_scrollback_lines+HEIGHT; y1++) {
c1 = [self getLineAtIndex:y1];
if (WIDTH == width) {
memcpy(c2, c1, REAL_WIDTH*sizeof(screen_char_t));
}
else if (WIDTH < width) {
memcpy(c2, c1, WIDTH*sizeof(screen_char_t));
c2[width].ch = 0; // no wrapping by default
x2 = WIDTH;
while (c1[WIDTH].ch) { //wrapping?
c1 = [self getLineAtIndex:++y1];
for(x1=0;x1<WIDTH;x1++,x2++) {
for(x3=x1; x3<=WIDTH && !c1[x3].ch; x3++);
if (x3>WIDTH) break;
if (x2>=width) {
c2[width].ch = 1;
x2 = 0;
if (wrapped) {
new_scrollback_top = incrementLinePointer(bl, new_scrollback_top, new_total_height, width, &_wrap);
}
c2 = incrementLinePointer(bl, c2, new_total_height, width, &_wrap);
wrapped = wrapped || _wrap;
if (_wrap) y2 = 0; else y2++;
c2[width].ch = 0;
}
c2[x2]=c1[x1];
}
}
if (x2<width) memcpy(c2+x2, defaultLine, (width-x2)*sizeof(screen_char_t));
}
else {
memcpy(c2, c1, width*sizeof(screen_char_t));
c2[width].ch = 0; // no wrapping by default
x1 = x2 = width;
do {
for(;x1<WIDTH;x1++,x2++) {
for(x3=x1; x3<WIDTH && !c1[x3].ch; x3++);
if (x3>=WIDTH && !c1[WIDTH].ch) break;
if (x2>=width) {
c2[width].ch = 1;
x2 = 0;
if (wrapped) {
new_scrollback_top = incrementLinePointer(bl, new_scrollback_top, new_total_height, width, &_wrap);
}
c2 = incrementLinePointer(bl, c2, new_total_height, width, &_wrap);
wrapped = wrapped || _wrap;
if (_wrap) y2 = 0; else y2++;
c2[width].ch = 0;
}
c2[x2]=c1[x1];
}
if (c1[WIDTH].ch) {
c1 = [self getLineAtIndex:++y1];
x1 = 0;
}
else
break;
} while (1);
if (x2<width) memcpy(c2+x2, defaultLine, (width-x2)*sizeof(screen_char_t));
}
if (wrapped) {
new_scrollback_top = incrementLinePointer(bl, new_scrollback_top, new_total_height, width, &_wrap);
}
c2 = incrementLinePointer(bl, c2, new_total_height, width, &_wrap);
wrapped = wrapped || _wrap;
if (_wrap) y2 = 0; else y2++;
}
// reassign our pointers
if(buffer_lines)
free(buffer_lines);
buffer_lines = bl;
scrollback_top = new_scrollback_top;
last_buffer_line = bl + (new_total_height - 1)*(width+1);
if (max_scrollback_lines > 0) {
if (wrapped) {
current_scrollback_lines = max_scrollback_lines;
CURSOR_Y = height - 1;
}
else {
if (y2 <= height) {
current_scrollback_lines = 0;
CURSOR_Y = y2 - 1;
}
else {
current_scrollback_lines = y2 - height ;
CURSOR_Y = height - 1;
}
}
}
else {
current_scrollback_lines = 0;
CURSOR_Y = wrapped ? height - 1 : y2 - 1;
}
screen_top = scrollback_top + current_scrollback_lines*(width+1);
if (screen_top > last_buffer_line)
screen_top = bl + (screen_top - last_buffer_line) - width - 1;
// set the rest of new buffer (if any) to default line
if (!wrapped) {
for(;y2 < new_total_height; y2++) {
memcpy(c2, defaultLine, (width+1)*sizeof(screen_char_t));
c2 = incrementLinePointer(bl, c2, new_total_height, width, &_wrap);
}
}
// new height and width
WIDTH = width;
HEIGHT = height;
// reset terminal scroll top and bottom
SCROLL_TOP = 0;
SCROLL_BOTTOM = HEIGHT - 1;
// adjust X coordinate of cursor
if (CURSOR_X >= width)
CURSOR_X = width-1;
if (SAVE_CURSOR_X >= width)
SAVE_CURSOR_X = width-1;
if (ALT_SAVE_CURSOR_X >= width)
ALT_SAVE_CURSOR_X = width-1;
if (CURSOR_Y >= height)
CURSOR_Y = height-1;
if (SAVE_CURSOR_Y >= height)
SAVE_CURSOR_Y = height-1;
if (ALT_SAVE_CURSOR_Y >= height)
ALT_SAVE_CURSOR_Y = height-1;
// if we did the resize in SAVE_BUFFER mode, too bad, get rid of it
if(temp_buffer) {
screen_char_t* aDefaultLine = [self _getDefaultLineWithWidth:WIDTH];
free(temp_buffer);
temp_buffer = (screen_char_t*)malloc(REAL_WIDTH*HEIGHT*(sizeof(screen_char_t)));
for(i = 0; i < HEIGHT; i++) {
memcpy(temp_buffer+i*REAL_WIDTH, aDefaultLine, REAL_WIDTH*sizeof(screen_char_t));
}
}
// force a redraw
if(dirty)
free(dirty);
dirty=(char*)malloc(height*width*sizeof(char));
// An immediate refresh is needed so that the size of TEXTVIEW can be
// adjusted to fit the new size
[self setDirty];
}
- (void) reset
{
// reset terminal scroll top and bottom
SCROLL_TOP = 0;
SCROLL_BOTTOM = HEIGHT - 1;
[self clearScreen];
[self clearTabStop];
SAVE_CURSOR_X = 0;
ALT_SAVE_CURSOR_X = 0;
CURSOR_Y = 0;
SAVE_CURSOR_Y = 0;
ALT_SAVE_CURSOR_Y = 0;
// set initial tabs
for(int i = TABSIZE; i < TABWINDOW; i += TABSIZE)
tabStop[i] = YES;
for(int i = 0; i < 4; i++)
saveCharset[i]=charset[i]=0;
[self showCursor: YES];
}
- (int)width
{
return WIDTH;
}
- (int)height
{
return HEIGHT;
}
- (unsigned int)scrollbackLines
{
return max_scrollback_lines;
}
// sets scrollback lines.
- (void)setScrollback:(unsigned int)lines;
{
// if we already have a buffer, don't allow this
if(buffer_lines != NULL)
return;
if (lines<0 || lines>MAX_SCROLLBACK_LINES) {
dynamic_scrollback_size = YES;
max_scrollback_lines = DEFAULT_SCROLLBACK;
}
else {
dynamic_scrollback_size = NO;
max_scrollback_lines = lines;
}
}
- (PTYSession *) session
{
return (SESSION);
}
- (void)setSession:(PTYSession *)session
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s", __PRETTY_FUNCTION__);
#endif
SESSION=session;
}
- (void)setTerminal:(VT100Terminal *)terminal
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen setTerminal:%@]",
__FILE__, __LINE__, terminal);
#endif
TERMINAL = terminal;
}
- (VT100Terminal *)terminal
{
return TERMINAL;
}
- (void)setShellTask:(PTYTask *)shell
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen setShellTask:%@]",
__FILE__, __LINE__, shell);
#endif
SHELL = shell;
}
- (PTYTask *)shellTask
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen shellTask]", __FILE__, __LINE__);
#endif
return SHELL;
}
- (PTYTextView *) display
{
return (display);
}
- (void) setDisplay: (PTYTextView *) aDisplay
{
display = aDisplay;
}
- (BOOL) blinkingCursor
{
return (blinkingCursor);
}
- (void) setBlinkingCursor: (BOOL) flag
{
blinkingCursor = flag;
}
- (void)putToken:(VT100TCC)token
{
NSString *newTitle;
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen putToken:%d]",__FILE__, __LINE__, token);
#endif
int i,j,k;
screen_char_t *aLine;
switch (token.type) {
// our special code
case VT100_STRING:
case VT100_ASCIISTRING:
// check if we are in print mode
if([self printToAnsi] == YES)
[self printStringToAnsi: token.u.string];
// else display string on screen
else
[self setString:token.u.string ascii: token.type == VT100_ASCIISTRING];
break;
case VT100_UNKNOWNCHAR: break;
case VT100_NOTSUPPORT: break;
// VT100 CC
case VT100CC_ENQ: break;
case VT100CC_BEL: [self activateBell]; break;
case VT100CC_BS: [self backSpace]; break;
case VT100CC_HT: [self setTab]; break;
case VT100CC_LF:
case VT100CC_VT:
case VT100CC_FF:
if([self printToAnsi] == YES)
[self printStringToAnsi: @"\n"];
else
[self setNewLine];
break;
case VT100CC_CR: CURSOR_X = 0; break;
case VT100CC_SO: break;
case VT100CC_SI: break;
case VT100CC_DC1: break;
case VT100CC_DC3: break;
case VT100CC_CAN:
case VT100CC_SUB: break;
case VT100CC_DEL: [self deleteCharacters:1];break;
// VT100 CSI
case VT100CSI_CPR: break;
case VT100CSI_CUB: [self cursorLeft:token.u.csi.p[0]]; break;
case VT100CSI_CUD: [self cursorDown:token.u.csi.p[0]]; break;
case VT100CSI_CUF: [self cursorRight:token.u.csi.p[0]]; break;
case VT100CSI_CUP: [self cursorToX:token.u.csi.p[1]
Y:token.u.csi.p[0]];
break;
case VT100CSI_CUU: [self cursorUp:token.u.csi.p[0]]; break;
case VT100CSI_DA: [self deviceAttribute:token]; break;
case VT100CSI_DECALN:
for (i = 0; i < HEIGHT; i++)
{
aLine = [self getLineAtScreenIndex: i];
for(j = 0; j < WIDTH; j++)
{
aLine[j].ch ='E';
aLine[j].fg_color = [TERMINAL foregroundColorCodeReal];
aLine[j].bg_color = [TERMINAL backgroundColorCodeReal];
}
aLine[WIDTH].ch = 0;
}
[self setDirty];
break;
case VT100CSI_DECDHL: break;
case VT100CSI_DECDWL: break;
case VT100CSI_DECID: break;
case VT100CSI_DECKPAM: break;
case VT100CSI_DECKPNM: break;
case VT100CSI_DECLL: break;
case VT100CSI_DECRC: [self restoreCursorPosition]; break;
case VT100CSI_DECREPTPARM: break;
case VT100CSI_DECREQTPARM: break;
case VT100CSI_DECSC: [self saveCursorPosition]; break;
case VT100CSI_DECSTBM: [self setTopBottom:token]; break;
case VT100CSI_DECSWL: break;
case VT100CSI_DECTST: break;
case VT100CSI_DSR: [self deviceReport:token]; break;
case VT100CSI_ED: [self eraseInDisplay:token]; break;
case VT100CSI_EL: [self eraseInLine:token]; break;
case VT100CSI_HTS: if (CURSOR_X<WIDTH) tabStop[CURSOR_X]=YES; break;
case VT100CSI_HVP: [self cursorToX:token.u.csi.p[1]
Y:token.u.csi.p[0]];
break;
case VT100CSI_NEL:
CURSOR_X=0;
case VT100CSI_IND:
if(CURSOR_Y == SCROLL_BOTTOM)
{
[self scrollUp];
}
else
{
CURSOR_Y++;
if (CURSOR_Y>=HEIGHT) {
CURSOR_Y=HEIGHT-1;
}
}
break;
case VT100CSI_RI:
if(CURSOR_Y == SCROLL_TOP)
{
[self scrollDown];
}
else
{
CURSOR_Y--;
if (CURSOR_Y<0) {
CURSOR_Y=0;
}
}
break;
case VT100CSI_RIS: break;
case VT100CSI_RM: break;
case VT100CSI_SCS0: charset[0]=(token.u.code=='0'); break;
case VT100CSI_SCS1: charset[1]=(token.u.code=='0'); break;
case VT100CSI_SCS2: charset[2]=(token.u.code=='0'); break;
case VT100CSI_SCS3: charset[3]=(token.u.code=='0'); break;
case VT100CSI_SGR: [self selectGraphicRendition:token]; break;
case VT100CSI_SM: break;
case VT100CSI_TBC:
switch (token.u.csi.p[0]) {
case 3: [self clearTabStop]; break;
case 0: if (CURSOR_X<WIDTH) tabStop[CURSOR_X]=NO;
}
break;
case VT100CSI_DECSET:
case VT100CSI_DECRST:
if (token.u.csi.p[0]==3 && [TERMINAL allowColumnMode] == YES && ![[iTermTerminalProfileMgr singleInstance] noResizingForProfile: [[SESSION addressBookEntry] objectForKey: @"Terminal Profile"]]) {
// set the column
[[SESSION parent] resizeWindow:[TERMINAL columnMode]?132:80 height:HEIGHT];
token.u.csi.p[0]=2; [self eraseInDisplay:token]; //erase the screen
token.u.csi.p[0]=token.u.csi.p[1]=0; [self setTopBottom:token]; // reset scroll;
}
break;
// ANSI CSI
case ANSICSI_CBT:
[self backTab];
break;
case ANSICSI_CHA:
[self cursorToX: token.u.csi.p[0]];
break;
case ANSICSI_VPA:
[self cursorToX: CURSOR_X+1 Y: token.u.csi.p[0]];
break;
case ANSICSI_VPR:
[self cursorToX: CURSOR_X+1 Y: token.u.csi.p[0]+CURSOR_Y+1];
break;
case ANSICSI_ECH:
if (CURSOR_X<WIDTH) {
i=WIDTH*CURSOR_Y+CURSOR_X;
j=token.u.csi.p[0];
if (j + CURSOR_X > WIDTH)
j = WIDTH - CURSOR_X;
aLine = [self getLineAtScreenIndex: CURSOR_Y];
for(k = 0; k < j; k++)
{
aLine[CURSOR_X+k].ch = 0;
aLine[CURSOR_X+k].fg_color = [TERMINAL foregroundColorCodeReal];
aLine[CURSOR_X+k].bg_color = [TERMINAL backgroundColorCodeReal];
}
memset(dirty+i,1,j);
}
break;
case STRICT_ANSI_MODE:
[TERMINAL setStrictAnsiMode: ![TERMINAL strictAnsiMode]];
break;
case ANSICSI_PRINT:
switch (token.u.csi.p[0]) {
case 4:
// print our stuff!!
[self doPrint];
break;
case 5:
// allocate a string for the stuff to be printed
if (printToAnsiString != nil)
[printToAnsiString release];
printToAnsiString = [[NSMutableString alloc] init];
[self setPrintToAnsi: YES];
break;
default:
//print out the whole screen
if (printToAnsiString != nil)
[printToAnsiString release];
printToAnsiString = nil;
[self setPrintToAnsi: NO];
[self doPrint];
}
break;
case ANSICSI_SCP:
[self saveCursorPosition];
break;
case ANSICSI_RCP:
[self restoreCursorPosition];
break;
// XTERM extensions
case XTERMCC_WIN_TITLE:
newTitle = [[token.u.string copy] autorelease];
if ([[iTermTerminalProfileMgr singleInstance] appendTitleForProfile: [[SESSION addressBookEntry] objectForKey: @"Terminal Profile"]])
newTitle = [NSString stringWithFormat:@"%@: %@", [SESSION defaultName], newTitle];
[SESSION setWindowTitle: newTitle];
break;
case XTERMCC_WINICON_TITLE:
newTitle = [[token.u.string copy] autorelease];
if ([[iTermTerminalProfileMgr singleInstance] appendTitleForProfile: [[SESSION addressBookEntry] objectForKey: @"Terminal Profile"]])
newTitle = [NSString stringWithFormat:@"%@: %@", [SESSION defaultName], newTitle];
[SESSION setWindowTitle: newTitle];
[SESSION setName: newTitle];
break;
case XTERMCC_ICON_TITLE:
newTitle = [[token.u.string copy] autorelease];
if ([[iTermTerminalProfileMgr singleInstance] appendTitleForProfile: [[SESSION addressBookEntry] objectForKey: @"Terminal Profile"]])
newTitle = [NSString stringWithFormat:@"%@: %@", [SESSION defaultName], newTitle];
[SESSION setName: newTitle];
break;
case XTERMCC_INSBLNK: [self insertBlank:token.u.csi.p[0]]; break;
case XTERMCC_INSLN: [self insertLines:token.u.csi.p[0]]; break;
case XTERMCC_DELCH: [self deleteCharacters:token.u.csi.p[0]]; break;
case XTERMCC_DELLN: [self deleteLines:token.u.csi.p[0]]; break;
case XTERMCC_WINDOWSIZE:
//NSLog(@"setting window size from (%d, %d) to (%d, %d)", WIDTH, HEIGHT, token.u.csi.p[1], token.u.csi.p[2]);
if (![[iTermTerminalProfileMgr singleInstance] noResizingForProfile: [[SESSION addressBookEntry] objectForKey: @"Terminal Profile"]] && ![[SESSION parent] fullScreen]) {
// set the column
[[SESSION parent] resizeWindow:token.u.csi.p[2] height:token.u.csi.p[1]];
}
break;
case XTERMCC_WINDOWSIZE_PIXEL:
if (![[iTermTerminalProfileMgr singleInstance] noResizingForProfile: [[SESSION addressBookEntry] objectForKey: @"Terminal Profile"]] && ![[SESSION parent] fullScreen]) {
[[SESSION parent] resizeWindowToPixelsWidth:token.u.csi.p[2] height:token.u.csi.p[1]];
}
break;
case XTERMCC_WINDOWPOS:
//NSLog(@"setting window position to Y=%d, X=%d", token.u.csi.p[1], token.u.csi.p[2]);
if (![[iTermTerminalProfileMgr singleInstance] noResizingForProfile: [[SESSION addressBookEntry] objectForKey: @"Terminal Profile"]] && ![[SESSION parent] fullScreen])
[[[SESSION parent] window] setFrameTopLeftPoint: NSMakePoint(token.u.csi.p[2], [[[[SESSION parent] window] screen] frame].size.height - token.u.csi.p[1])];
break;
case XTERMCC_ICONIFY:
if (![[SESSION parent] fullScreen])
[[[SESSION parent] window] performMiniaturize: nil];
break;
case XTERMCC_DEICONIFY:
[[[SESSION parent] window] deminiaturize: nil];
break;
case XTERMCC_RAISE:
[[[SESSION parent] window] orderFront: nil];
break;
case XTERMCC_LOWER:
if (![[SESSION parent] fullScreen])
[[[SESSION parent] window] orderBack: nil];
break;
case XTERMCC_SU:
for (i=0; i<token.u.csi.p[0]; i++) [self scrollUp];
break;
case XTERMCC_SD:
for (i=0; i<token.u.csi.p[0]; i++) [self scrollDown];
break;
case XTERMCC_REPORT_WIN_STATE:
{
char buf[64];
snprintf(buf, sizeof(buf), "\033[%dt", [[[SESSION parent] window] isMiniaturized]?2:1);
[SHELL writeTask: [NSData dataWithBytes:buf length:strlen(buf)]];
}
break;
case XTERMCC_REPORT_WIN_POS:
{
char buf[64];
NSRect frame = [[[SESSION parent] window] frame];
snprintf(buf, sizeof(buf), "\033[3;%d;%dt", (int) frame.origin.x, (int) frame.origin.y);
[SHELL writeTask: [NSData dataWithBytes:buf length:strlen(buf)]];
}
break;
case XTERMCC_REPORT_WIN_PIX_SIZE:
{
char buf[64];
NSRect frame = [[[SESSION parent] window] frame];
snprintf(buf, sizeof(buf), "\033[4;%d;%dt", (int) frame.size.height, (int) frame.size.width);
[SHELL writeTask: [NSData dataWithBytes:buf length:strlen(buf)]];
}
break;
case XTERMCC_REPORT_WIN_SIZE:
{
char buf[64];
snprintf(buf, sizeof(buf), "\033[8;%d;%dt", HEIGHT, WIDTH);
[SHELL writeTask: [NSData dataWithBytes:buf length:strlen(buf)]];
}
break;
case XTERMCC_REPORT_SCREEN_SIZE:
{
char buf[64];
NSRect screenSize = [[[[SESSION parent] window] screen] frame];
float nch = [[[SESSION parent] window] frame].size.height - [[[[SESSION parent] currentSession] SCROLLVIEW] documentVisibleRect].size.height;
float wch = [[[SESSION parent] window] frame].size.width - [[[[SESSION parent] currentSession] SCROLLVIEW] documentVisibleRect].size.width;
int h = (screenSize.size.height - nch) / [[SESSION parent] charHeight];
int w = (screenSize.size.width - wch - MARGIN * 2) / [[SESSION parent] charWidth];
snprintf(buf, sizeof(buf), "\033[9;%d;%dt", h, w);
[SHELL writeTask: [NSData dataWithBytes:buf length:strlen(buf)]];
}
break;
case XTERMCC_REPORT_ICON_TITLE:
{
char buf[64];
snprintf(buf, sizeof(buf), "\033]L%s\033\\", [[SESSION name] UTF8String]);
[SHELL writeTask: [NSData dataWithBytes:buf length:strlen(buf)]];
}
break;
case XTERMCC_REPORT_WIN_TITLE:
{
char buf[64];
snprintf(buf, sizeof(buf), "\033]l%s\033\\", [[SESSION windowTitle] UTF8String]);
[SHELL writeTask: [NSData dataWithBytes:buf length:strlen(buf)]];
}
break;
// Our iTerm specific codes
#if 0
case ITERM_GROWL:
if (GROWL) {
[gd growlNotify:NSLocalizedStringFromTableInBundle(@"Alert",@"iTerm", [NSBundle bundleForClass: [self class]], @"Growl Alerts")
withDescription:[NSString stringWithFormat:@"Session %@ #%d: %@", [SESSION name], [SESSION realObjectCount], token.u.string]
andNotification:@"Customized Message"];
}
break;
#endif
default:
/*NSLog(@"%s(%d): bug?? token.type = %d",
__FILE__, __LINE__, token.type);*/
break;
}
// NSLog(@"Done");
}
- (void)clearBuffer
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[VT100Screen clearBuffer]", __FILE__, __LINE__ );
#endif
[self clearScreen];