-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVT100Terminal.m
2406 lines (2135 loc) · 60.2 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
// -*- mode:objc -*-
// $Id: VT100Terminal.m,v 1.136 2008-10-21 05:43:52 yfabian Exp $
//
/*
** VT100Terminal.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
** Initial code by Kiichi Kusama
**
** Project: iTerm
**
** Description: Implements the model class VT100 terminal.
**
** 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.
*/
#import <iTerm/VT100Terminal.h>
#import <iTerm/PseudoTerminal.h>
#import <iTerm/PTYSession.h>
#import <iTerm/VT100Screen.h>
#import <iTerm/NSStringITerm.h>
#include <term.h>
#define DEBUG_ALLOC 0
#define LOG_UNKNOWN 0
#define STANDARD_STREAM_SIZE 100000
#define UNKNOWN ('#')
@implementation VT100Terminal
#define iscontrol(c) ((c) <= 0x1f)
/*
Traditional Chinese (Big5)
1st 0xa1-0xfe
2nd 0x40-0x7e || 0xa1-0xfe
Simplifed Chinese (EUC_CN)
1st 0x81-0xfe
2nd 0x40-0x7e || 0x80-0xfe
*/
#define iseuccn(c) ((c) >= 0x81 && (c) <= 0xfe)
#define isbig5(c) ((c) >= 0xa1 && (c) <= 0xfe)
#define issjiskanji(c) (((c) >= 0x81 && (c) <= 0x9f) || \
((c) >= 0xe0 && (c) <= 0xef))
#define iseuckr(c) ((c) >= 0xa1 && (c) <= 0xfe)
#define isGBEncoding(e) ((e)==0x80000019||(e)==0x80000421|| \
(e)==0x80000631||(e)==0x80000632|| \
(e)==0x80000930)
#define isBig5Encoding(e) ((e)==0x80000002||(e)==0x80000423|| \
(e)==0x80000931||(e)==0x80000a03|| \
(e)==0x80000a06)
#define isJPEncoding(e) ((e)==0x80000001||(e)==0x8||(e)==0x15)
#define isSJISEncoding(e) ((e)==0x80000628||(e)==0x80000a01)
#define isKREncoding(e) ((e)==0x80000422||(e)==0x80000003|| \
(e)==0x80000840||(e)==0x80000940)
#define ESC 0x1b
#define DEL 0x7f
#define CURSOR_SET_DOWN "\033OB"
#define CURSOR_SET_UP "\033OA"
#define CURSOR_SET_RIGHT "\033OC"
#define CURSOR_SET_LEFT "\033OD"
#define CURSOR_SET_HOME "\033OH"
#define CURSOR_SET_END "\033OF"
#define CURSOR_RESET_DOWN "\033[B"
#define CURSOR_RESET_UP "\033[A"
#define CURSOR_RESET_RIGHT "\033[C"
#define CURSOR_RESET_LEFT "\033[D"
#define CURSOR_RESET_HOME "\033[H"
#define CURSOR_RESET_END "\033[F"
#define CURSOR_MOD_DOWN "\033[1;%dB"
#define CURSOR_MOD_UP "\033[1;%dA"
#define CURSOR_MOD_RIGHT "\033[1;%dC"
#define CURSOR_MOD_LEFT "\033[1;%dD"
#define CURSOR_MOD_HOME "\033[1;%dH"
#define CURSOR_MOD_END "\033[1;%dF"
#define KEY_INSERT "\033[2~"
#define KEY_PAGE_UP "\033[5~"
#define KEY_PAGE_DOWN "\033[6~"
#define KEY_DEL "\033[3~"
#define KEY_BACKSPACE "\010"
#define ALT_KP_0 "\033Op"
#define ALT_KP_1 "\033Oq"
#define ALT_KP_2 "\033Or"
#define ALT_KP_3 "\033Os"
#define ALT_KP_4 "\033Ot"
#define ALT_KP_5 "\033Ou"
#define ALT_KP_6 "\033Ov"
#define ALT_KP_7 "\033Ow"
#define ALT_KP_8 "\033Ox"
#define ALT_KP_9 "\033Oy"
#define ALT_KP_MINUS "\033Om"
#define ALT_KP_PLUS "\033Ok"
#define ALT_KP_PERIOD "\033On"
#define ALT_KP_SLASH "\033Oo"
#define ALT_KP_STAR "\033Oj"
#define ALT_KP_EQUALS "\033OX"
#define ALT_KP_ENTER "\033OM"
#define KEY_FUNCTION_FORMAT "\033[%d~"
#define REPORT_POSITION "\033[%d;%dR"
#define REPORT_POSITION_Q "\033[?%d;%dR"
#define REPORT_STATUS "\033[0n"
// Device Attribute : VT100 with Advanced Video Option
#define REPORT_WHATAREYOU "\033[?1;2c"
// Secondary Device Attribute: VT100
#define REPORT_SDA "\033[>0;95;c"
#define REPORT_VT52 "\033/Z"
#define MOUSE_REPORT_FORMAT "\033[M%c%c%c"
#define conststr_sizeof(n) ((sizeof(n)) - 1)
typedef struct {
int p[VT100CSIPARAM_MAX];
int count;
int cmd;
BOOL question;
int modifier;
} CSIParam;
// functions
static BOOL isCSI(unsigned char *, size_t);
static BOOL isXTERM(unsigned char *, size_t);
static BOOL isString(unsigned char *, NSStringEncoding);
static size_t getCSIParam(unsigned char *, size_t, CSIParam *, VT100Screen *);
static VT100TCC decode_csi(unsigned char *, size_t, size_t *,VT100Screen *);
static VT100TCC decode_xterm(unsigned char *, size_t, size_t *,NSStringEncoding);
static VT100TCC decode_other(unsigned char *, size_t, size_t *);
static VT100TCC decode_control(unsigned char *, size_t, size_t *,NSStringEncoding,VT100Screen *);
static int utf8_reqbyte(unsigned char);
static VT100TCC decode_utf8(unsigned char *, size_t, size_t *);
static VT100TCC decode_euccn(unsigned char *, size_t, size_t *);
static VT100TCC decode_big5(unsigned char *,size_t, size_t *);
static VT100TCC decode_string(unsigned char *, size_t, size_t *,
NSStringEncoding);
static BOOL isCSI(unsigned char *code, size_t len)
{
if (len >= 2 && code[0] == ESC && (code[1] == '['))
return YES;
return NO;
}
static BOOL isXTERM(unsigned char *code, size_t len)
{
if (len >= 2 && code[0] == ESC && (code[1] == ']'))
return YES;
return NO;
}
static BOOL isString(unsigned char *code,
NSStringEncoding encoding)
{
BOOL result = NO;
// NSLog(@"%@",[NSString localizedNameOfStringEncoding:encoding]);
if (encoding== NSUTF8StringEncoding) {
if (*code >= 0x80)
result = YES;
}
else if (isGBEncoding(encoding)) {
if (iseuccn(*code))
result = YES;
}
else if (isBig5Encoding(encoding)) {
if (isbig5(*code))
result = YES;
}
else if (isJPEncoding(encoding)) {
if (*code ==0x8e || *code==0x8f|| (*code>=0xa1&&*code<=0xfe))
result = YES;
}
else if (isSJISEncoding(encoding)) {
if (*code >= 0x80)
result = YES;
}
else if (isKREncoding(encoding)) {
if (iseuckr(*code))
result = YES;
}
else if (*code>=0x20) {
result = YES;
}
return result;
}
static size_t getCSIParam(unsigned char *datap,
size_t datalen,
CSIParam *param, VT100Screen *SCREEN)
{
int i;
BOOL unrecognized=NO;
unsigned char *orgp = datap;
BOOL readNumericParameter = NO;
NSCParameterAssert(datap != NULL);
NSCParameterAssert(datalen >= 2);
NSCParameterAssert(param != NULL);
param->count = 0;
param->cmd = 0;
for (i = 0; i < VT100CSIPARAM_MAX; ++i )
param->p[i] = -1;
NSCParameterAssert(datap[0] == ESC);
NSCParameterAssert(datap[1] == '[');
datap += 2;
datalen -= 2;
if (datalen > 0 && *datap == '?') {
param->question = YES;
datap ++;
datalen --;
}
// check for secondsry device attribute modifier
else if (datalen > 0 && *datap == '>')
{
param->modifier = '>';
param->question = NO;
datap++;
datalen--;
}
else
param->question = NO;
while (datalen > 0) {
if (isdigit(*datap)) {
int n = *datap - '0';
datap++;
datalen--;
while (datalen > 0 && isdigit(*datap)) {
n = n * 10 + *datap - '0';
datap++;
datalen--;
}
//if (param->count == 0 )
//param->count = 1;
//param->p[param->count - 1] = n;
if(param->count < VT100CSIPARAM_MAX)
param->p[param->count] = n;
// increment the parameter count
param->count++;
// set the numeric parameter flag
readNumericParameter = YES;
}
else if (*datap == ';') {
datap++;
datalen--;
// If we got an implied (blank) parameter, increment the parameter count again
if(readNumericParameter == NO)
param->count++;
// reset the parameter flag
readNumericParameter = NO;
if (param->count >= VT100CSIPARAM_MAX) {
// broken
//param->cmd = 0xff;
unrecognized=YES;
//break;
}
}
else if (isalpha(*datap)||*datap=='@') {
datalen--;
param->cmd = unrecognized?0xff:*datap;
datap++;
break;
}
else if (*datap=='\'') {
datap++;
datalen--;
switch (*datap) {
case 'z':
case '|':
case 'w':
//NSLog(@"Unsupported locator sequence");
param->cmd=0xff;
datap++;
datalen--;
break;
default:
//NSLog(@"Unrecognized locator sequence");
datap++;
datalen--;
param->cmd=0xff;
break;
}
break;
}
else if (*datap=='&') {
datap++;
datalen--;
switch (*datap) {
case 'w':
//NSLog(@"Unsupported locator sequence");
param->cmd=0xff;
datap++;
datalen--;
break;
default:
//NSLog(@"Unrecognized locator sequence");
datap++;
datalen--;
param->cmd=0xff;
break;
}
break;
}
else {
switch (*datap) {
case VT100CC_ENQ: break;
case VT100CC_BEL: [SCREEN activateBell]; break;
case VT100CC_BS: [SCREEN backSpace]; break;
case VT100CC_HT: [SCREEN setTab]; break;
case VT100CC_LF:
case VT100CC_VT:
case VT100CC_FF: [SCREEN setNewLine]; break;
case VT100CC_CR: [SCREEN cursorToX:1 Y:[SCREEN cursorY]]; 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: [SCREEN deleteCharacters:1];break;
default:
//NSLog(@"Unrecognized escape sequence: %c (0x%x)", *datap, *datap);
param->cmd=0xff;
unrecognized=YES;
break;
}
if(unrecognized == NO)
{
datalen--;
datap++;
}
}
if (unrecognized) break;
}
return datap - orgp;
}
#define SET_PARAM_DEFAULT(pm,n,d) \
(((pm).p[(n)] = (pm).p[(n)] < 0 ? (d):(pm).p[(n)]), \
((pm).count = (pm).count > (n) + 1 ? (pm).count : (n) + 1 ))
static VT100TCC decode_csi(unsigned char *datap,
size_t datalen,
size_t *rmlen,VT100Screen *SCREEN)
{
VT100TCC result;
CSIParam param={{0},0};
size_t paramlen;
int i;
paramlen = getCSIParam(datap, datalen, ¶m, SCREEN);
result.type = VT100_WAIT;
// Check for unkown
if(param.cmd == 0xff)
{
result.type = VT100_UNKNOWNCHAR;
*rmlen = paramlen;
}
// process
else if (paramlen > 0 && param.cmd > 0) {
if (!param.question) {
switch (param.cmd) {
case 'D': // Cursor Backward
result.type = VT100CSI_CUB;
SET_PARAM_DEFAULT(param, 0, 1);
break;
case 'B': // Cursor Down
result.type = VT100CSI_CUD;
SET_PARAM_DEFAULT(param, 0, 1);
break;
case 'C': // Cursor Forward
result.type = VT100CSI_CUF;
SET_PARAM_DEFAULT(param, 0, 1);
break;
case 'A': // Cursor Up
result.type = VT100CSI_CUU;
SET_PARAM_DEFAULT(param, 0, 1);
break;
case 'H':
result.type = VT100CSI_CUP;
SET_PARAM_DEFAULT(param, 0, 1);
SET_PARAM_DEFAULT(param, 1, 1);
break;
case 'c':
result.type = VT100CSI_DA;
SET_PARAM_DEFAULT(param, 0, 0);
break;
case 'q':
result.type = VT100CSI_DECLL;
SET_PARAM_DEFAULT(param, 0, 0);
break;
case 'x':
if (param.count == 1)
result.type = VT100CSI_DECREQTPARM;
else
result.type = VT100CSI_DECREPTPARM;
break;
case 'r':
result.type = VT100CSI_DECSTBM;
SET_PARAM_DEFAULT(param, 0, 1);
SET_PARAM_DEFAULT(param, 1, [SCREEN height]);
break;
case 'y':
if (param.count == 2)
result.type = VT100CSI_DECTST;
else
{
#if LOG_UNKNOWN
NSLog(@"1: Unknown token %c", param.cmd);
#endif
result.type = VT100_NOTSUPPORT;
}
break;
case 'n':
result.type = VT100CSI_DSR;
SET_PARAM_DEFAULT(param, 0, 0);
break;
case 'J':
result.type = VT100CSI_ED;
SET_PARAM_DEFAULT(param, 0, 0);
break;
case 'K':
result.type = VT100CSI_EL;
SET_PARAM_DEFAULT(param, 0, 0);
break;
case 'f':
result.type = VT100CSI_HVP;
SET_PARAM_DEFAULT(param, 0, 1);
SET_PARAM_DEFAULT(param, 1, 1);
break;
case 'l':
result.type = VT100CSI_RM;
break;
case 'm':
result.type = VT100CSI_SGR;
for (i = 0; i < param.count; ++i) {
SET_PARAM_DEFAULT(param, i, 0);
// NSLog(@"m[%d]=%d",i,param.p[i]);
}
break;
case 'h':
result.type = VT100CSI_SM;
break;
case 'g':
result.type = VT100CSI_TBC;
SET_PARAM_DEFAULT(param, 0, 0);
break;
// these are xterm controls
case '@':
result.type = XTERMCC_INSBLNK;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'L':
result.type = XTERMCC_INSLN;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'P':
result.type = XTERMCC_DELCH;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'M':
result.type = XTERMCC_DELLN;
SET_PARAM_DEFAULT(param,0,1);
break;
case 't':
switch (param.p[0]) {
case 8:
result.type = XTERMCC_WINDOWSIZE;
SET_PARAM_DEFAULT(param, 1, 0); // columns or Y
SET_PARAM_DEFAULT(param, 2, 0); // rows or X
break;
case 3:
result.type = XTERMCC_WINDOWPOS;
SET_PARAM_DEFAULT(param, 1, 0); // columns or Y
SET_PARAM_DEFAULT(param, 2, 0); // rows or X
break;
case 4:
result.type = XTERMCC_WINDOWSIZE_PIXEL;
SET_PARAM_DEFAULT(param, 1, 0); // columns or Y
SET_PARAM_DEFAULT(param, 2, 0); // rows or X
break;
case 2:
result.type = XTERMCC_ICONIFY;
break;
case 1:
result.type = XTERMCC_DEICONIFY;
break;
case 5:
result.type = XTERMCC_RAISE;
break;
case 6:
result.type = XTERMCC_LOWER;
break;
case 11:
result.type = XTERMCC_REPORT_WIN_STATE;
break;
case 13:
result.type = XTERMCC_REPORT_WIN_POS;
break;
case 14:
result.type = XTERMCC_REPORT_WIN_PIX_SIZE;
break;
case 18:
result.type = XTERMCC_REPORT_WIN_SIZE;
break;
case 19:
result.type = XTERMCC_REPORT_SCREEN_SIZE;
break;
case 20:
result.type = XTERMCC_REPORT_ICON_TITLE;
break;
case 21:
result.type = XTERMCC_REPORT_WIN_TITLE;
break;
default:
result.type = VT100_NOTSUPPORT;
break;
}
break;
case 'S':
result.type = XTERMCC_SU;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'T':
if (param.count<2) {
result.type = XTERMCC_SD;
SET_PARAM_DEFAULT(param,0,1);
}
else
result.type = VT100_NOTSUPPORT;
break;
// ANSI
case 'Z':
result.type = ANSICSI_CBT;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'G':
result.type = ANSICSI_CHA;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'd':
result.type = ANSICSI_VPA;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'e':
result.type = ANSICSI_VPR;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'X':
result.type = ANSICSI_ECH;
SET_PARAM_DEFAULT(param,0,1);
break;
case 'i':
result.type = ANSICSI_PRINT;
SET_PARAM_DEFAULT(param,0,0);
break;
case 's':
result.type = ANSICSI_SCP;
SET_PARAM_DEFAULT(param,0,0);
break;
case 'u':
result.type = ANSICSI_RCP;
SET_PARAM_DEFAULT(param,0,0);
break;
default:
#if LOG_UNKNOWN
NSLog(@"2: Unknown token (%c); %s", param.cmd, datap);
#endif
result.type = VT100_NOTSUPPORT;
break;
}
}
else {
switch (param.cmd) {
case 'h': // Dec private mode set
result.type = VT100CSI_DECSET;
SET_PARAM_DEFAULT(param, 0, 0);
break;
case 'l': // Dec private mode reset
result.type = VT100CSI_DECRST;
SET_PARAM_DEFAULT(param, 0, 0);
break;
default:
#if LOG_UNKNOWN
NSLog(@"3: Unknown token %c", param.cmd);
#endif
result.type = VT100_NOTSUPPORT;
break;
}
}
// copy CSI parameter
for (i = 0; i < VT100CSIPARAM_MAX; ++i)
result.u.csi.p[i] = param.p[i];
result.u.csi.count = param.count;
result.u.csi.question = param.question;
result.u.csi.modifier = param.modifier;
*rmlen = paramlen;
}
return result;
}
static VT100TCC decode_xterm(unsigned char *datap,
size_t datalen,
size_t *rmlen,
NSStringEncoding enc)
{
#define MAX_BUFFER_LENGTH 1024
int mode=0;
VT100TCC result;
NSData *data;
BOOL unrecognized=NO;
char s[MAX_BUFFER_LENGTH]={0}, *c=nil;
NSCParameterAssert(datap != NULL);
NSCParameterAssert(datalen >= 2);
NSCParameterAssert(datap[0] == ESC);
NSCParameterAssert(datap[1] == ']');
datap += 2;
datalen -= 2;
*rmlen=2;
if (datalen>0 && isdigit(*datap)) {
int n = *datap++ - '0';
datalen--;
(*rmlen)++;
while (datalen > 0 && isdigit(*datap)) {
n = n * 10 + *datap - '0';
(*rmlen)++;
datap++;
datalen--;
}
mode=n;
}
if (datalen>0) {
if (*datap != ';') {
unrecognized=YES;
}
else {
BOOL str_end=NO;;
c=s;
datalen--;
datap++;
(*rmlen)++;
while (*datap!=0x007&&datalen>0) {
if (*datap==0x1b && datalen>2 && *(datap+1)=='\\') {
datap++;
datalen--;
(*rmlen)++;
str_end=YES;
break;
}
if (c-s<MAX_BUFFER_LENGTH) {
*c=*datap;
c++;
}
datalen--;
datap++;
(*rmlen)++;
}
if ((*datap!=0x007 && !str_end) || datalen==0) {
if (datalen>0) unrecognized=YES;
else {
*rmlen=0;
}
}
else {
*datap++;
datalen--;
(*rmlen)++;
}
}
}
else {
*rmlen=0;
}
if (unrecognized) {
//NSLog(@"invalid: %d",*rmlen);
result.type = VT100_NOTSUPPORT;
*rmlen = 2;
}
else if (!(*rmlen)) {
result.type = VT100_WAIT;
}
else {
data = [NSData dataWithBytes:s length:c-s];
result.u.string = [[[NSString alloc] initWithData:data
encoding:enc] autorelease];
switch (mode) {
case 0:
result.type = XTERMCC_WINICON_TITLE;
break;
case 1:
result.type = XTERMCC_ICON_TITLE;
break;
case 9:
result.type = ITERM_GROWL;
break;
case 2:
result.type = XTERMCC_WIN_TITLE;
break;
case 4:
result.type = XTERMCC_SET_RGB;
break;
default:
result.type = VT100_NOTSUPPORT;
break;
}
// NSLog(@"result: %d[%@],%d",result.type,result.u.string,*rmlen);
}
return result;
}
static VT100TCC decode_other(unsigned char *datap,
size_t datalen,
size_t *rmlen)
{
VT100TCC result;
int c1, c2, c3;
NSCParameterAssert(datap[0] == ESC);
NSCParameterAssert(datalen > 1);
c1 = (datalen >= 2 ? datap[1]: -1);
c2 = (datalen >= 3 ? datap[2]: -1);
c3 = (datalen >= 4 ? datap[3]: -1);
switch (c1) {
case '#':
if (c2 < 0) {
result.type = VT100_WAIT;
}
else {
switch (c2) {
case '8': result.type=VT100CSI_DECALN; break;
default:
#if LOG_UNKNOWN
NSLog(@"4: Unknown token ESC # %c", c2);
#endif
result.type = VT100_NOTSUPPORT;
}
*rmlen = 3;
}
break;
case '=':
result.type = VT100CSI_DECKPAM;
*rmlen = 2;
break;
case '>':
result.type = VT100CSI_DECKPNM;
*rmlen = 2;
break;
case '<':
result.type = STRICT_ANSI_MODE;
*rmlen = 2;
break;
case '(':
if (c2 < 0) {
result.type = VT100_WAIT;
}
else {
result.type = VT100CSI_SCS0;
result.u.code=c2;
*rmlen = 3;
}
break;
case ')':
if (c2 < 0) {
result.type = VT100_WAIT;
}
else {
result.type = VT100CSI_SCS1;
result.u.code=c2;
*rmlen = 3;
}
break;
case '*':
if (c2 < 0) {
result.type = VT100_WAIT;
}
else {
result.type = VT100CSI_SCS2;
result.u.code=c2;
*rmlen = 3;
}
break;
case '+':
if (c2 < 0) {
result.type = VT100_WAIT;
}
else {
result.type = VT100CSI_SCS3;
result.u.code=c2;
*rmlen = 3;
}
break;
case '8':
result.type = VT100CSI_DECRC;
*rmlen = 2;
break;
case '7':
result.type = VT100CSI_DECSC;
*rmlen = 2;
break;
case 'D':
result.type = VT100CSI_IND;
*rmlen = 2;
break;
case 'E':
result.type = VT100CSI_NEL;
*rmlen = 2;
break;
case 'H':
result.type = VT100CSI_HTS;
*rmlen = 2;
break;
case 'M':
result.type = VT100CSI_RI;
*rmlen = 2;
break;
case 'Z':
result.type = VT100CSI_DECID;
*rmlen = 2;
break;
case 'c':
result.type = VT100CSI_RIS;
*rmlen = 2;
break;
case ' ':
if (c2<0) {
result.type = VT100_WAIT;
}
else {
switch (c2) {
case 'L':
case 'M':
case 'N':
case 'F':
case 'G':
*rmlen = 3;
result.type = VT100_NOTSUPPORT;
break;
default:
*rmlen = 1;
result.type = VT100_NOTSUPPORT;
break;
}
}
break;
default:
#if LOG_UNKNOWN
NSLog(@"5: Unknown token %c(%x)", c1, c1);
#endif
result.type = VT100_NOTSUPPORT;
*rmlen = 2;
break;
}
return result;
}
static VT100TCC decode_control(unsigned char *datap,
size_t datalen,
size_t *rmlen,
NSStringEncoding enc, VT100Screen *SCREEN)
{
VT100TCC result;
if (isCSI(datap, datalen)) {
result = decode_csi(datap, datalen, rmlen, SCREEN);
}
else if (isXTERM(datap,datalen)) {
result = decode_xterm(datap,datalen,rmlen,enc);
}
else {
NSCParameterAssert(datalen > 0);
switch ( *datap ) {
case VT100CC_NULL:
result.type = VT100_SKIP;
*rmlen = 0;
while (datalen > 0 && *datap == '\0') {
++datap;
--datalen;
++ *rmlen;
}
break;
case VT100CC_ESC:
if (datalen == 1) {
result.type = VT100_WAIT;
}
else {
result = decode_other(datap, datalen, rmlen);
}
break;
default:
result.type = *datap;
*rmlen = 1;
break;
}
}
return result;
}
static int utf8_reqbyte(unsigned char f)
{
int result;
if (isascii(f))
result = 1;
else if ((f & 0xe0) == 0xc0)
result = 2;
else if ((f & 0xf0) == 0xe0)
result = 3;
else if ((f & 0xf8) == 0xf0)
result = 4;
else if ((f & 0xfc) == 0xf8)
result = 5;
else if ((f & 0xfe) == 0xfc)
result = 6;
else