/
VT100Terminal.m
2815 lines (2593 loc) · 112 KB
/
VT100Terminal.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
#import "VT100Terminal.h"
#import "DebugLogging.h"
#import "iTermParser.h"
#import "iTermURLStore.h"
#import "NSColor+iTerm.h"
#import "NSData+iTerm.h"
#import "NSDictionary+iTerm.h"
#import "NSObject+iTerm.h"
#import "NSStringITerm.h"
#import "NSURL+iTerm.h"
#import "VT100DCSParser.h"
#import "VT100Parser.h"
#import <apr-1/apr_base64.h> // for xterm's base64 decoding (paste64)
#include <term.h>
NSString *const kGraphicRenditionBoldKey = @"Bold";
NSString *const kGraphicRenditionBlinkKey = @"Blink";
NSString *const kGraphicRenditionUnderKey = @"Underline";
NSString *const kGraphicRenditionReversedKey = @"Reversed";
NSString *const kGraphicRenditionFaintKey = @"Faint";
NSString *const kGraphicRenditionItalicKey = @"Italic";
NSString *const kGraphicRenditionForegroundColorCodeKey = @"FG Color/Red";
NSString *const kGraphicRenditionForegroundGreenKey = @"FG Green";
NSString *const kGraphicRenditionForegroundBlueKey = @"FG Blue";
NSString *const kGraphicRenditionForegroundModeKey = @"FG Mode";
NSString *const kGraphicRenditionBackgroundColorCodeKey = @"BG Color/Red";
NSString *const kGraphicRenditionBackgroundGreenKey = @"BG Green";
NSString *const kGraphicRenditionBackgroundBlueKey = @"BG Blue";
NSString *const kGraphicRenditionBackgroundModeKey = @"BG Mode";
NSString *const kSavedCursorPositionKey = @"Position";
NSString *const kSavedCursorCharsetKey = @"Charset";
NSString *const kSavedCursorLineDrawingArrayKey = @"Line Drawing Flags";
NSString *const kSavedCursorGraphicRenditionKey = @"Graphic Rendition";
NSString *const kSavedCursorOriginKey = @"Origin";
NSString *const kSavedCursorWraparoundKey = @"Wraparound";
NSString *const kSavedCursorUnicodeVersion = @"Unicode Version";
NSString *const kTerminalStateTermTypeKey = @"Term Type";
NSString *const kTerminalStateAnswerBackStringKey = @"Answerback String";
NSString *const kTerminalStateStringEncodingKey = @"String Encoding";
NSString *const kTerminalStateCanonicalEncodingKey = @"Canonical String Encoding";
NSString *const kTerminalStateReportFocusKey = @"Report Focus";
NSString *const kTerminalStateReverseVideoKey = @"Reverse Video";
NSString *const kTerminalStateOriginModeKey = @"Origin Mode";
NSString *const kTerminalStateMoreFixKey = @"More-Fix";
NSString *const kTerminalStateWraparoundModeKey = @"Wraparound Mode";
NSString *const kTerminalStateReverseWraparoundModeKey = @"Reverse Wraparound Mode";
NSString *const kTerminalStateIsAnsiKey = @"Is ANSI";
NSString *const kTerminalStateAutorepeatModeKey = @"Autorepeat Mode";
NSString *const kTerminalStateInsertModeKey = @"Insert Mode";
NSString *const kTerminalStateSendReceiveModeKey = @"Send/Receive Mode";
NSString *const kTerminalStateCharsetKey = @"Charset";
NSString *const kTerminalStateMouseModeKey = @"Mouse Mode";
NSString *const kTerminalStateMouseFormatKey = @"Mouse Format";
NSString *const kTerminalStateCursorModeKey = @"Cursor Mode";
NSString *const kTerminalStateKeypadModeKey = @"Keypad Mode";
NSString *const kTerminalStateAllowKeypadModeKey = @"Allow Keypad Mode";
NSString *const kTerminalStateBracketedPasteModeKey = @"Bracketed Paste Mode";
NSString *const kTerminalStateAnsiModeKey = @"ANSI Mode";
NSString *const kTerminalStateNumLockKey = @"Numlock";
NSString *const kTerminalStateGraphicRenditionKey = @"Graphic Rendition";
NSString *const kTerminalStateMainSavedCursorKey = @"Main Saved Cursor";
NSString *const kTerminalStateAltSavedCursorKey = @"Alt Saved Cursor";
NSString *const kTerminalStateAllowColumnModeKey = @"Allow Column Mode";
NSString *const kTerminalStateColumnModeKey = @"Column Mode";
NSString *const kTerminalStateDisableSMCUPAndRMCUPKey = @"Disable Alt Screen";
NSString *const kTerminalStateSoftAlternateScreenModeKey = @"Soft Alternate Screen Mode";
NSString *const kTerminalStateInCommandKey = @"In Command";
NSString *const kTerminalStateUnicodeVersionStack = @"Unicode Version Stack";
NSString *const kTerminalStateURL = @"URL";
NSString *const kTerminalStateURLParams = @"URL Params";
@interface VT100Terminal ()
@property(nonatomic, assign) BOOL reverseVideo;
@property(nonatomic, assign) BOOL originMode;
@property(nonatomic, assign) BOOL moreFix;
@property(nonatomic, assign) BOOL isAnsi;
@property(nonatomic, assign) BOOL autorepeatMode;
@property(nonatomic, assign) int charset;
@property(nonatomic, assign) BOOL allowColumnMode;
@property(nonatomic, assign) BOOL columnMode; // YES=132 Column, NO=80 Column
@property(nonatomic, assign) BOOL disableSmcupRmcup;
@property(nonatomic, retain) NSURL *url;
@property(nonatomic, retain) NSString *urlParams;
// A write-only property, at the moment. TODO: What should this do?
@property(nonatomic, assign) BOOL strictAnsiMode;
@end
#define NUM_CHARSETS 4
typedef struct {
BOOL bold;
BOOL blink;
BOOL under;
BOOL reversed;
BOOL faint;
BOOL italic;
// TODO: Add invisible and protected
int fgColorCode;
int fgGreen;
int fgBlue;
ColorMode fgColorMode;
int bgColorCode;
int bgGreen;
int bgBlue;
ColorMode bgColorMode;
} VT100GraphicRendition;
typedef struct {
VT100GridCoord position;
int charset;
BOOL lineDrawing[NUM_CHARSETS];
VT100GraphicRendition graphicRendition;
BOOL origin;
BOOL wraparound;
NSInteger unicodeVersion;
} VT100SavedCursor;
@implementation VT100Terminal {
// In FinalTerm command mode (user is at the prompt typing a command).
BOOL inCommand_;
id<VT100TerminalDelegate> delegate_;
BOOL ansiMode_; // YES=ANSI, NO=VT52
BOOL numLock_; // YES=ON, NO=OFF, default=YES;
VT100GraphicRendition graphicRendition_;
VT100SavedCursor mainSavedCursor_;
VT100SavedCursor altSavedCursor_;
// TODO: Actually use this.
int sendModifiers_[NUM_MODIFIABLE_RESOURCES];
NSMutableArray *_unicodeVersionStack;
// Code for the current hypertext link, or 0 if not in a hypertext link.
unsigned short _currentURLCode;
}
@synthesize delegate = delegate_;
@synthesize receivingFile = receivingFile_;
#define DEL 0x7f
// character attributes
#define VT100CHARATTR_ALLOFF 0
#define VT100CHARATTR_BOLD 1
#define VT100CHARATTR_FAINT 2
#define VT100CHARATTR_ITALIC 3
#define VT100CHARATTR_UNDER 4
#define VT100CHARATTR_BLINK 5
#define VT100CHARATTR_REVERSE 7
// xterm additions
#define VT100CHARATTR_NORMAL 22
#define VT100CHARATTR_NOT_ITALIC 23
#define VT100CHARATTR_NOT_UNDER 24
#define VT100CHARATTR_STEADY 25
#define VT100CHARATTR_POSITIVE 27
typedef enum {
COLORCODE_BLACK = 0,
COLORCODE_RED = 1,
COLORCODE_GREEN = 2,
COLORCODE_YELLOW = 3,
COLORCODE_BLUE = 4,
COLORCODE_MAGENTA = 5,
COLORCODE_WATER = 6,
COLORCODE_WHITE = 7,
COLORCODE_256 = 8,
} colorCode;
// Color constants
// Color codes for 8-color mode. Black and white are the limits; other codes can be constructed
// similarly.
#define VT100CHARATTR_FG_BASE 30
#define VT100CHARATTR_BG_BASE 40
#define VT100CHARATTR_FG_BLACK (VT100CHARATTR_FG_BASE + COLORCODE_BLACK)
#define VT100CHARATTR_FG_WHITE (VT100CHARATTR_FG_BASE + COLORCODE_WHITE)
#define VT100CHARATTR_FG_256 (VT100CHARATTR_FG_BASE + COLORCODE_256)
#define VT100CHARATTR_FG_DEFAULT (VT100CHARATTR_FG_BASE + 9)
#define VT100CHARATTR_BG_BLACK (VT100CHARATTR_BG_BASE + COLORCODE_BLACK)
#define VT100CHARATTR_BG_WHITE (VT100CHARATTR_BG_BASE + COLORCODE_WHITE)
#define VT100CHARATTR_BG_256 (VT100CHARATTR_BG_BASE + COLORCODE_256)
#define VT100CHARATTR_BG_DEFAULT (VT100CHARATTR_BG_BASE + 9)
// Color codes for 16-color mode. Black and white are the limits; other codes can be constructed
// similarly.
#define VT100CHARATTR_FG_HI_BASE 90
#define VT100CHARATTR_BG_HI_BASE 100
#define VT100CHARATTR_FG_HI_BLACK (VT100CHARATTR_FG_HI_BASE + COLORCODE_BLACK)
#define VT100CHARATTR_FG_HI_WHITE (VT100CHARATTR_FG_HI_BASE + COLORCODE_WHITE)
#define VT100CHARATTR_BG_HI_BLACK (VT100CHARATTR_BG_HI_BASE + COLORCODE_BLACK)
#define VT100CHARATTR_BG_HI_WHITE (VT100CHARATTR_BG_HI_BASE + COLORCODE_WHITE)
// Prevents runaway memory usage
static const int kMaxScreenColumns = 4096;
static const int kMaxScreenRows = 4096;
#pragma mark - Instance methods
- (instancetype)init {
self = [super init];
if (self) {
_output = [[VT100Output alloc] init];
_encoding = _canonicalEncoding = NSASCIIStringEncoding;
_parser = [[VT100Parser alloc] init];
_parser.encoding = _encoding;
_wraparoundMode = YES;
_reverseWraparoundMode = NO;
_autorepeatMode = YES;
graphicRendition_.fgColorCode = ALTSEM_DEFAULT;
graphicRendition_.fgColorMode = ColorModeAlternate;
graphicRendition_.bgColorCode = ALTSEM_DEFAULT;
graphicRendition_.bgColorMode = ColorModeAlternate;
_mouseMode = MOUSE_REPORTING_NONE;
_mouseFormat = MOUSE_FORMAT_XTERM;
_allowKeypadMode = YES;
numLock_ = YES;
[self saveCursor]; // initialize save area
_unicodeVersionStack = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[_output release];
[_parser release];
[_termType release];
[_answerBackString release];
[_unicodeVersionStack release];
[_url release];
[_urlParams release];
[super dealloc];
}
- (void)stopReceivingFile {
receivingFile_ = NO;
}
- (void)setEncoding:(NSStringEncoding)encoding {
[self setEncoding:encoding canonical:YES];
}
- (void)setEncoding:(NSStringEncoding)encoding canonical:(BOOL)canonical {
if (canonical) {
_canonicalEncoding = encoding;
}
_encoding = encoding;
_parser.encoding = encoding;
}
- (void)setTermType:(NSString *)termtype
{
[_termType autorelease];
_termType = [termtype copy];
self.allowKeypadMode = [_termType rangeOfString:@"xterm"].location != NSNotFound;
int r;
// NOTE: This seems to cause a memory leak. The setter for termTypeIsValid (below) has the
// side effect of copying various curses strings, and it depends on this. When I redo output,
// fix this disaster.
setupterm((char *)[_termType UTF8String], fileno(stdout), &r);
if (r != 1) {
NSLog(@"Terminal type %s is not defined.", [_termType UTF8String]);
}
_output.termTypeIsValid = (r == 1);
self.isAnsi = [_termType rangeOfString:@"ANSI"
options:NSCaseInsensitiveSearch | NSAnchoredSearch ].location != NSNotFound;
[delegate_ terminalTypeDidChange];
}
- (void)setAnswerBackString:(NSString *)s {
s = [s stringByExpandingVimSpecialCharacters];
_answerBackString = [s copy];
}
- (void)setForeground24BitColor:(NSColor *)color {
graphicRendition_.fgColorCode = color.redComponent * 255.0;
graphicRendition_.fgGreen = color.greenComponent * 255.0;
graphicRendition_.fgBlue = color.blueComponent * 255.0;
graphicRendition_.fgColorMode = ColorMode24bit;
}
- (void)setForegroundColor:(int)fgColorCode alternateSemantics:(BOOL)altsem
{
graphicRendition_.fgColorCode = fgColorCode;
graphicRendition_.fgColorMode = (altsem ? ColorModeAlternate : ColorModeNormal);
}
- (void)setBackgroundColor:(int)bgColorCode alternateSemantics:(BOOL)altsem
{
graphicRendition_.bgColorCode = bgColorCode;
graphicRendition_.bgColorMode = (altsem ? ColorModeAlternate : ColorModeNormal);
}
- (void)resetCharset {
_charset = 0;
for (int i = 0; i < NUM_CHARSETS; i++) {
[delegate_ terminalSetCharset:i toLineDrawingMode:NO];
}
}
- (void)resetByUserRequest:(BOOL)userInitiated {
self.cursorMode = NO;
if (_columnMode) {
[delegate_ terminalSetWidth:80];
}
self.columnMode = NO;
_reverseVideo = NO;
_originMode = NO;
_moreFix = NO;
self.wraparoundMode = YES;
self.reverseWraparoundMode = NO;
self.autorepeatMode = YES;
self.keypadMode = NO;
self.insertMode = NO;
self.sendReceiveMode = NO;
self.bracketedPasteMode = NO;
_charset = 0;
[self resetGraphicRendition];
self.mouseMode = MOUSE_REPORTING_NONE;
self.mouseFormat = MOUSE_FORMAT_XTERM;
[self saveCursor]; // reset saved text attributes
[delegate_ terminalMouseModeDidChangeTo:_mouseMode];
[delegate_ terminalSetUseColumnScrollRegion:NO];
_reportFocus = NO;
self.strictAnsiMode = NO;
self.allowColumnMode = NO;
receivingFile_ = NO;
_copyingToPasteboard = NO;
_encoding = _canonicalEncoding;
_parser.encoding = _canonicalEncoding;
for (int i = 0; i < NUM_CHARSETS; i++) {
mainSavedCursor_.lineDrawing[i] = NO;
altSavedCursor_.lineDrawing[i] = NO;
}
[self resetSavedCursorPositions];
if (userInitiated) {
[_parser reset];
}
[delegate_ terminalShowPrimaryBuffer];
_softAlternateScreenMode = NO;
[delegate_ terminalResetPreservingPrompt:userInitiated];
}
- (void)setWraparoundMode:(BOOL)mode {
if (mode != _wraparoundMode) {
_wraparoundMode = mode;
[delegate_ terminalWraparoundModeDidChangeTo:mode];
}
}
- (void)setCursorMode:(BOOL)cursorMode {
_cursorMode = cursorMode;
_output.cursorMode = cursorMode;
}
- (void)setMouseFormat:(MouseFormat)mouseFormat {
_mouseFormat = mouseFormat;
_output.mouseFormat = mouseFormat;
}
- (void)setKeypadMode:(BOOL)mode
{
_keypadMode = mode && self.allowKeypadMode;
_output.keypadMode = _keypadMode;
}
- (void)setAllowKeypadMode:(BOOL)allow
{
_allowKeypadMode = allow;
if (!allow) {
self.keypadMode = NO;
}
}
- (screen_char_t)foregroundColorCode
{
screen_char_t result = { 0 };
if (graphicRendition_.reversed) {
if (graphicRendition_.bgColorMode == ColorModeAlternate &&
graphicRendition_.bgColorCode == ALTSEM_DEFAULT) {
result.foregroundColor = ALTSEM_REVERSED_DEFAULT;
} else {
result.foregroundColor = graphicRendition_.bgColorCode;
}
result.fgGreen = graphicRendition_.bgGreen;
result.fgBlue = graphicRendition_.bgBlue;
result.foregroundColorMode = graphicRendition_.bgColorMode;
} else {
result.foregroundColor = graphicRendition_.fgColorCode;
result.fgGreen = graphicRendition_.fgGreen;
result.fgBlue = graphicRendition_.fgBlue;
result.foregroundColorMode = graphicRendition_.fgColorMode;
}
result.bold = graphicRendition_.bold;
result.faint = graphicRendition_.faint;
result.italic = graphicRendition_.italic;
result.underline = graphicRendition_.under;
result.blink = graphicRendition_.blink;
result.image = NO;
result.urlCode = _currentURLCode;
return result;
}
- (screen_char_t)backgroundColorCode
{
screen_char_t result = { 0 };
if (graphicRendition_.reversed) {
if (graphicRendition_.fgColorMode == ColorModeAlternate &&
graphicRendition_.fgColorCode == ALTSEM_DEFAULT) {
result.backgroundColor = ALTSEM_REVERSED_DEFAULT;
} else {
result.backgroundColor = graphicRendition_.fgColorCode;
}
result.bgGreen = graphicRendition_.fgGreen;
result.bgBlue = graphicRendition_.fgBlue;
result.backgroundColorMode = graphicRendition_.fgColorMode;
} else {
result.backgroundColor = graphicRendition_.bgColorCode;
result.bgGreen = graphicRendition_.bgGreen;
result.bgBlue = graphicRendition_.bgBlue;
result.backgroundColorMode = graphicRendition_.bgColorMode;
}
return result;
}
- (screen_char_t)foregroundColorCodeReal
{
screen_char_t result = { 0 };
result.foregroundColor = graphicRendition_.fgColorCode;
result.fgGreen = graphicRendition_.fgGreen;
result.fgBlue = graphicRendition_.fgBlue;
result.foregroundColorMode = graphicRendition_.fgColorMode;
result.bold = graphicRendition_.bold;
result.faint = graphicRendition_.faint;
result.italic = graphicRendition_.italic;
result.underline = graphicRendition_.under;
result.blink = graphicRendition_.blink;
result.urlCode = _currentURLCode;
return result;
}
- (screen_char_t)backgroundColorCodeReal
{
screen_char_t result = { 0 };
result.backgroundColor = graphicRendition_.bgColorCode;
result.bgGreen = graphicRendition_.bgGreen;
result.bgBlue = graphicRendition_.bgBlue;
result.backgroundColorMode = graphicRendition_.bgColorMode;
return result;
}
- (void)setInsertMode:(BOOL)mode
{
if (_insertMode != mode) {
_insertMode = mode;
[delegate_ terminalInsertModeDidChangeTo:mode];
}
}
- (void)executeDecSetReset:(VT100Token *)token {
assert(token->type == VT100CSI_DECSET ||
token->type == VT100CSI_DECRST);
BOOL mode = (token->type == VT100CSI_DECSET);
for (int i = 0; i < token.csi->count; i++) {
switch (token.csi->p[i]) {
case 1:
self.cursorMode = mode;
break;
case 2:
ansiMode_ = mode;
break;
case 3:
if (self.allowColumnMode) {
self.columnMode = mode;
[delegate_ terminalSetWidth:(self.columnMode ? 132 : 80)];
}
break;
case 4:
// Smooth vs jump scrolling. Not supported.
break;
case 5:
self.reverseVideo = mode;
[delegate_ terminalNeedsRedraw];
break;
case 6:
self.originMode = mode;
[delegate_ terminalMoveCursorToX:1 y:1];
break;
case 7:
self.wraparoundMode = mode;
break;
case 8:
self.autorepeatMode = mode;
break;
case 9:
// TODO: This should send mouse x&y on button press.
break;
case 20:
// This used to be the setter for "line mode", but it wasn't used and it's not
// supported by xterm. Seemed to have somethign to do with CR vs LF.
break;
case 25:
[delegate_ terminalSetCursorVisible:mode];
break;
case 40:
self.allowColumnMode = mode;
break;
case 41:
self.moreFix = mode;
break;
case 45:
self.reverseWraparoundMode = mode;
break;
case 47:
// alternate screen buffer mode
if (!self.disableSmcupRmcup) {
if (mode) {
int x = [delegate_ terminalCursorX];
int y = [delegate_ terminalCursorY];
[delegate_ terminalShowAltBuffer];
[delegate_ terminalSetCursorX:x];
[delegate_ terminalSetCursorY:y];
} else {
int x = [delegate_ terminalCursorX];
int y = [delegate_ terminalCursorY];
[delegate_ terminalShowPrimaryBuffer];
[delegate_ terminalSetCursorX:x];
[delegate_ terminalSetCursorY:y];
}
}
_softAlternateScreenMode = mode;
break;
case 69:
[delegate_ terminalSetUseColumnScrollRegion:mode];
break;
case 1000:
// case 1001:
// TODO: MOUSE_REPORTING_HILITE not implemented.
case 1002:
case 1003:
if (mode) {
self.mouseMode = token.csi->p[i] - 1000;
} else {
self.mouseMode = MOUSE_REPORTING_NONE;
}
[delegate_ terminalMouseModeDidChangeTo:_mouseMode];
break;
case 1004:
self.reportFocus = mode && [delegate_ terminalFocusReportingEnabled];
break;
case 1005:
if (mode) {
self.mouseFormat = MOUSE_FORMAT_XTERM_EXT;
} else {
self.mouseFormat = MOUSE_FORMAT_XTERM;
}
break;
case 1006:
if (mode) {
self.mouseFormat = MOUSE_FORMAT_SGR;
} else {
self.mouseFormat = MOUSE_FORMAT_XTERM;
}
break;
case 1015:
if (mode) {
self.mouseFormat = MOUSE_FORMAT_URXVT;
} else {
self.mouseFormat = MOUSE_FORMAT_XTERM;
}
break;
case 1049:
// From the xterm release log:
// Implement new escape sequence, private mode 1049, which combines
// the switch to/from alternate screen mode with screen clearing and
// cursor save/restore. Unlike the existing escape sequence, this
// clears the alternate screen when switching to it rather than when
// switching to the normal screen, thus retaining the alternate screen
// contents for select/paste operations.
if (!self.disableSmcupRmcup) {
if (mode) {
[self saveCursor];
[delegate_ terminalShowAltBuffer];
[delegate_ terminalClearScreen];
[delegate_ terminalMoveCursorToX:1 y:1];
} else {
[delegate_ terminalShowPrimaryBuffer];
[self restoreCursor];
}
}
_softAlternateScreenMode = mode;
break;
case 2004:
// Set bracketed paste mode
self.bracketedPasteMode = mode;
break;
}
}
}
- (void)resetGraphicRendition {
memset(&graphicRendition_, 0, sizeof(graphicRendition_));
}
- (void)executeSGR:(VT100Token *)token {
assert(token->type == VT100CSI_SGR);
if (token.csi->count == 0) {
[self resetGraphicRendition];
} else {
int i;
for (i = 0; i < token.csi->count; ++i) {
int n = token.csi->p[i];
switch (n) {
case VT100CHARATTR_ALLOFF:
[self resetGraphicRendition];
break;
case VT100CHARATTR_BOLD:
graphicRendition_.bold = YES;
break;
case VT100CHARATTR_FAINT:
graphicRendition_.faint = YES;
break;
case VT100CHARATTR_NORMAL:
graphicRendition_.faint = graphicRendition_.bold = NO;
break;
case VT100CHARATTR_ITALIC:
graphicRendition_.italic = YES;
break;
case VT100CHARATTR_NOT_ITALIC:
graphicRendition_.italic = NO;
break;
case VT100CHARATTR_UNDER:
graphicRendition_.under = YES;
break;
case VT100CHARATTR_NOT_UNDER:
graphicRendition_.under = NO;
break;
case VT100CHARATTR_BLINK:
graphicRendition_.blink = YES;
break;
case VT100CHARATTR_STEADY:
graphicRendition_.blink = NO;
break;
case VT100CHARATTR_REVERSE:
graphicRendition_.reversed = YES;
break;
case VT100CHARATTR_POSITIVE:
graphicRendition_.reversed = NO;
break;
case VT100CHARATTR_FG_DEFAULT:
graphicRendition_.fgColorCode = ALTSEM_DEFAULT;
graphicRendition_.fgGreen = 0;
graphicRendition_.fgBlue = 0;
graphicRendition_.fgColorMode = ColorModeAlternate;
break;
case VT100CHARATTR_BG_DEFAULT:
graphicRendition_.bgColorCode = ALTSEM_DEFAULT;
graphicRendition_.bgGreen = 0;
graphicRendition_.bgBlue = 0;
graphicRendition_.bgColorMode = ColorModeAlternate;
break;
case VT100CHARATTR_FG_256: {
// The actual spec for this is called ITU T.416-199303
// You can download it for free! If you prefer to spend money, ISO/IEC 8613-6
// is supposedly the same thing.
//
// Here's a sad story about CSI 38:2, which is used to do 24-bit color.
//
// Lots of terminal emulators, iTerm2 included, misunderstood the spec. That's
// easy to understand if you read it, which I can't recommend doing unless
// you're looking for inspiration for your next Bulwer-Lytton Fiction Contest
// entry.
//
// See issue 6377 for more context.
//
// Ignoring color types we don't support like CMYK, the spec says to do this:
// CSI 38:2:[color space]:[red]:[green]:[blue]:[unused]:[tolerance]:[tolerance colorspace]
//
// Everything after [blue] is optional. Values are decimal numbers in 0...255.
//
// Unfortunately, what was implemented for a long time was this:
// CSI 38:2:[red]:[green]:[blue]:[unused]:[tolerance]:[tolerance colorspace]
//
// And for xterm compatibility, the following was also accepted:
// CSI 38;2;[red];[green];[blue]
//
// The New Order
// -------------
// Tolerance never did anything, so we'll accept this non-standards compliant
// code, which people use:
// CSI 38:2:[red]:[green]:[blue]
//
// As well as the following forms:
// CSI 38:2:[colorspace]:[red]:[green]:[blue]
// CSI 38:2:[colorspace]:[red]:[green]:[blue]:<one or more additional colon-delimited arguments, all ignored>
// CSI 38;2;[red];[green];[blue] // Notice semicolons in place of colons here
int subs[VT100CSISUBPARAM_MAX];
int numberOfSubparameters = iTermParserGetAllCSISubparametersForParameter(token.csi, i, subs);
if (numberOfSubparameters > 0) {
// Preferred syntax using colons to delimit subparameters
if (numberOfSubparameters >= 2 && subs[0] == 5) {
// CSI 38:5:P m
graphicRendition_.fgColorCode = subs[1];
graphicRendition_.fgGreen = 0;
graphicRendition_.fgBlue = 0;
graphicRendition_.fgColorMode = ColorModeNormal;
} else if (numberOfSubparameters >= 4 && subs[0] == 2) {
// 24-bit color
if (numberOfSubparameters >= 5) {
// Spec-compliant. Likely rarely used in 2017.
// CSI 38:2:colorspace:R:G:B m
// TODO: Respect the color space argument. See ITU-T Rec. T.414,
// but good luck actually finding the colour space IDs.
graphicRendition_.fgColorCode = subs[2];
graphicRendition_.fgGreen = subs[3];
graphicRendition_.fgBlue = subs[4];
graphicRendition_.fgColorMode = ColorMode24bit;
} else {
// Misinterpration compliant.
// CSI 38:2:R:G:B m <- misinterpration compliant
graphicRendition_.fgColorCode = subs[1];
graphicRendition_.fgGreen = subs[2];
graphicRendition_.fgBlue = subs[3];
graphicRendition_.fgColorMode = ColorMode24bit;
}
}
} else if (token.csi->count - i >= 3 && token.csi->p[i + 1] == 5) {
// For 256-color mode (indexed) use this for the foreground:
// CSI 38;5;N m
// where N is a value between 0 and 255. See the colors described in screen_char_t
// in the comments for fgColorCode.
graphicRendition_.fgColorCode = token.csi->p[i + 2];
graphicRendition_.fgGreen = 0;
graphicRendition_.fgBlue = 0;
graphicRendition_.fgColorMode = ColorModeNormal;
i += 2;
} else if (token.csi->count - i >= 5 && token.csi->p[i + 1] == 2) {
// CSI 38;2;R;G;B m
// Hack for xterm compatibility
// 24-bit color support
graphicRendition_.fgColorCode = token.csi->p[i + 2];
graphicRendition_.fgGreen = token.csi->p[i + 3];
graphicRendition_.fgBlue = token.csi->p[i + 4];
graphicRendition_.fgColorMode = ColorMode24bit;
i += 4;
}
break;
}
case VT100CHARATTR_BG_256: {
int subs[VT100CSISUBPARAM_MAX];
int numberOfSubparameters = iTermParserGetAllCSISubparametersForParameter(token.csi, i, subs);
if (numberOfSubparameters > 0) {
// Preferred syntax using colons to delimit subparameters
if (numberOfSubparameters >= 2 && subs[0] == 5) {
// CSI 48:5:P m
graphicRendition_.bgColorCode = subs[1];
graphicRendition_.bgGreen = 0;
graphicRendition_.bgBlue = 0;
graphicRendition_.bgColorMode = ColorModeNormal;
} else if (numberOfSubparameters >= 4 && subs[0] == 2) {
// CSI 48:2:R:G:B m
// 24-bit color
graphicRendition_.bgColorCode = subs[1];
graphicRendition_.bgGreen = subs[2];
graphicRendition_.bgBlue = subs[3];
graphicRendition_.bgColorMode = ColorMode24bit;
}
} else if (token.csi->count - i >= 3 && token.csi->p[i + 1] == 5) {
// CSI 48;5;P m
graphicRendition_.bgColorCode = token.csi->p[i + 2];
graphicRendition_.bgGreen = 0;
graphicRendition_.bgBlue = 0;
graphicRendition_.bgColorMode = ColorModeNormal;
i += 2;
} else if (token.csi->count - i >= 5 && token.csi->p[i + 1] == 2) {
// CSI 48;2;R;G;B m
// 24-bit color
graphicRendition_.bgColorCode = token.csi->p[i + 2];
graphicRendition_.bgGreen = token.csi->p[i + 3];
graphicRendition_.bgBlue = token.csi->p[i + 4];
graphicRendition_.bgColorMode = ColorMode24bit;
i += 4;
}
break;
}
default:
// 8 color support
if (n >= VT100CHARATTR_FG_BLACK &&
n <= VT100CHARATTR_FG_WHITE) {
graphicRendition_.fgColorCode = n - VT100CHARATTR_FG_BASE - COLORCODE_BLACK;
graphicRendition_.fgGreen = 0;
graphicRendition_.fgBlue = 0;
graphicRendition_.fgColorMode = ColorModeNormal;
} else if (n >= VT100CHARATTR_BG_BLACK &&
n <= VT100CHARATTR_BG_WHITE) {
graphicRendition_.bgColorCode = n - VT100CHARATTR_BG_BASE - COLORCODE_BLACK;
graphicRendition_.bgGreen = 0;
graphicRendition_.bgBlue = 0;
graphicRendition_.bgColorMode = ColorModeNormal;
}
// 16 color support
if (n >= VT100CHARATTR_FG_HI_BLACK &&
n <= VT100CHARATTR_FG_HI_WHITE) {
graphicRendition_.fgColorCode = n - VT100CHARATTR_FG_HI_BASE - COLORCODE_BLACK + 8;
graphicRendition_.fgGreen = 0;
graphicRendition_.fgBlue = 0;
graphicRendition_.fgColorMode = ColorModeNormal;
} else if (n >= VT100CHARATTR_BG_HI_BLACK &&
n <= VT100CHARATTR_BG_HI_WHITE) {
graphicRendition_.bgColorCode = n - VT100CHARATTR_BG_HI_BASE - COLORCODE_BLACK + 8;
graphicRendition_.bgGreen = 0;
graphicRendition_.bgBlue = 0;
graphicRendition_.bgColorMode = ColorModeNormal;
}
}
}
}
}
- (NSColor *)colorForXtermCCSetPaletteString:(NSString *)argument colorNumberPtr:(int *)numberPtr {
if ([argument length] == 7) {
int n, r, g, b;
int count = 0;
count += sscanf([[argument substringWithRange:NSMakeRange(0, 1)] UTF8String], "%x", &n);
if (count == 0) {
unichar c = [argument characterAtIndex:0];
n = c - 'a' + 10;
// fg = 16 ('g')
// bg = 17
// bold = 18
// selection = 19
// selected text = 20
// cursor = 21
// cursor text = 22
if (n >= 16 && n <= 22) {
++count;
}
}
count += sscanf([[argument substringWithRange:NSMakeRange(1, 2)] UTF8String], "%x", &r);
count += sscanf([[argument substringWithRange:NSMakeRange(3, 2)] UTF8String], "%x", &g);
count += sscanf([[argument substringWithRange:NSMakeRange(5, 2)] UTF8String], "%x", &b);
if (count == 4 &&
n >= 0 &&
n <= 22 &&
r >= 0 &&
r <= 255 &&
g >= 0 &&
g <= 255 &&
b >= 0 &&
b <= 255) {
NSColor* srgb = [NSColor colorWithSRGBRed:((double)r)/255.0
green:((double)g)/255.0
blue:((double)b)/255.0
alpha:1];
NSColor *theColor = [srgb colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
*numberPtr = n;
return theColor;
}
}
return nil;
}
- (void)setMouseMode:(MouseMode)mode
{
_mouseMode = mode;
[delegate_ terminalMouseModeDidChangeTo:_mouseMode];
}
- (void)handleDeviceStatusReportWithToken:(VT100Token *)token withQuestion:(BOOL)withQuestion {
if ([delegate_ terminalShouldSendReport]) {
switch (token.csi->p[0]) {
case 3: // response from VT100 -- Malfunction -- retry
break;
case 5: // Command from host -- Please report status
[delegate_ terminalSendReport:[self.output reportStatus]];
break;
case 6: // Command from host -- Please report active position
if (self.originMode) {
// This is compatible with Terminal but not old xterm :(. it always did what
// we do in the else clause. This behavior of xterm is fixed by Patch #297.
[delegate_ terminalSendReport:[self.output reportActivePositionWithX:[delegate_ terminalRelativeCursorX]
Y:[delegate_ terminalRelativeCursorY]
withQuestion:withQuestion]];
} else {
[delegate_ terminalSendReport:[self.output reportActivePositionWithX:[delegate_ terminalCursorX]
Y:[delegate_ terminalCursorY]
withQuestion:withQuestion]];
}
break;
case 1337: // iTerm2 extension
[delegate_ terminalSendReport:[self.output reportiTerm2Version]];
break;
case 0: // Response from VT100 -- Ready, No malfuctions detected
default:
break;
}
}
}
- (VT100GridRect)rectangleInToken:(VT100Token *)token
startingAtIndex:(int)index
defaultRectangle:(VT100GridRect)defaultRectangle {
CSIParam *csi = token.csi;
VT100GridCoord defaultMax = VT100GridRectMax(defaultRectangle);
// First, construct a coord range from the passed-in parameters. They may be -1 for default
// values.
int top = csi->p[index];
int left = csi->p[index + 1];
int bottom = csi->p[index + 2];
int right = csi->p[index + 3];
VT100GridCoordRange coordRange = VT100GridCoordRangeMake(left, top, right, bottom);
// Replace default values with the passed-in defaults.
if (coordRange.start.x < 0) {
coordRange.start.x = defaultRectangle.origin.x + 1;
}
if (coordRange.start.y < 0) {
coordRange.start.y = defaultRectangle.origin.y + 1;
}
if (coordRange.end.x < 0) {
coordRange.end.x = defaultMax.x + 1;
}
if (coordRange.end.y < 0) {
coordRange.end.y = defaultMax.y + 1;
}
if (self.originMode) {
VT100GridRect scrollRegion = [delegate_ terminalScrollRegion];
coordRange.start.x += scrollRegion.origin.x;
coordRange.start.y += scrollRegion.origin.y;
coordRange.end.x += scrollRegion.origin.x;
coordRange.end.y += scrollRegion.origin.y;
}
// Convert the coordRange to a 0-based rect (all coords are 1-based so far) and return it.
return VT100GridRectMake(MAX(0, coordRange.start.x - 1),
MAX(0, coordRange.start.y - 1),
coordRange.end.x - coordRange.start.x + 1,
coordRange.end.y - coordRange.start.y + 1);
}
- (BOOL)rectangleIsValid:(VT100GridRect)rect {
if (self.originMode) {
VT100GridRect scrollRegion = [delegate_ terminalScrollRegion];
if (rect.origin.y < scrollRegion.origin.y ||
rect.origin.x < scrollRegion.origin.x ||
VT100GridRectMax(rect).y > VT100GridRectMax(scrollRegion).y ||
VT100GridRectMax(rect).x > VT100GridRectMax(scrollRegion).x) {
return NO;
}
}
return (rect.size.width >= 0 &&
rect.size.height >= 0);
}
- (void)sendChecksumReportWithId:(int)identifier
rectangle:(VT100GridRect)rect {
if (![delegate_ terminalShouldSendReport]) {
return;
}
if (identifier < 0) {
return;
}
if (![self rectangleIsValid:rect]) {