-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvt_parser.c
8524 lines (7189 loc) · 232 KB
/
vt_parser.c
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
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
#include "vt_parser.h"
#include <stdio.h> /* sprintf */
#include <string.h> /* memmove */
#include <stdlib.h> /* atoi */
#include <fcntl.h> /* open */
#include <unistd.h> /* write/getcwd */
#include <sys/time.h> /* gettimeofday */
#include <time.h> /* clock */
#include <ctype.h> /* iscntrl */
#ifdef DEBUG
#include <stdarg.h> /* va_list */
#endif
#if defined(USE_LIBSSH2) && !defined(USE_WIN32API)
/*
* <wchar.h> has already been included without _XOPEN_SOURCE,
* so #define _XOPEN_SOURCE here doesn't work.
*/
#if 0
#define _XOPEN_SOURCE
#include <wchar.h> /* wcwidth */
#else
int wcwidth(wchar_t c);
#endif
#endif
#include <pobl/bl_debug.h>
#include <pobl/bl_mem.h> /* malloc/free */
#include <pobl/bl_util.h> /* DIGIT_STR_LEN */
#include <pobl/bl_conf_io.h>/* bl_get_user_rc_path */
#include <pobl/bl_str.h> /* strdup */
#include <pobl/bl_args.h>
#include <pobl/bl_unistd.h> /* bl_usleep */
#include <pobl/bl_locale.h> /* bl_get_locale */
#include <mef/ef_ucs4_map.h> /* ef_map_to_ucs4 */
#include <mef/ef_ucs_property.h>
#include <mef/ef_locale_ucs4_map.h>
#include <mef/ef_ko_kr_map.h>
#include "vt_iscii.h"
#include "vt_str_parser.h"
#include "vt_transfer.h"
#if defined(__CYGWIN__) || defined(__MSYS__)
#include "cygfile.h"
#endif
/*
* kterm BUF_SIZE in ptyx.h is 4096.
*/
#define PTY_RD_BUFFER_SIZE 3072
#define CTL_BEL 0x07
#define CTL_BS 0x08
#define CTL_TAB 0x09
#define CTL_LF 0x0a
#define CTL_VT 0x0b
#define CTL_FF 0x0c
#define CTL_CR 0x0d
#define CTL_SO 0x0e
#define CTL_SI 0x0f
#define CTL_ESC 0x1b
#define CURRENT_STR_P(vt_parser) \
((vt_parser)->r_buf.chars + (vt_parser)->r_buf.filled_len - (vt_parser)->r_buf.left)
#define HAS_XTERM_LISTENER(vt_parser, method) \
((vt_parser)->xterm_listener && ((vt_parser)->xterm_listener->method))
#define HAS_CONFIG_LISTENER(vt_parser, method) \
((vt_parser)->config_listener && ((vt_parser)->config_listener->method))
#if 1
#define MAX_PS_DIGIT 0xffff
#endif
#if 0
#define EDIT_DEBUG
#endif
#if 0
#define EDIT_ROUGH_DEBUG
#endif
#if 0
#define INPUT_DEBUG
#endif
#if 0
#define ESCSEQ_DEBUG
#endif
#if 0
#define OUTPUT_DEBUG
#endif
#if 0
#define DUMP_HEX
#endif
#if 0
#define SUPPORT_VTE_CJK_WIDTH
#endif
#ifndef NO_IMAGE
#define SUPPORT_ITERM2_OSC1337
#endif
/* mode must be less than 64 */
#define MOD32(mode) ((mode) >= 32 ? (mode) - 32 : (mode))
#define DIV32(mode) ((mode) >= 32) /* 1 or 0 */
#define SHIFT_FLAG_0(mode) ((mode) >= 32 ? 0 : (1 << (mode)))
#define SHIFT_FLAG_1(mode) ((mode) >= 32 ? (1 << ((mode) - 32)) : 0)
/*
* The default of DECMODE_1010 is 'set' (scroll to bottom on tty output) on xterm,
* but is 'reset' on mlterm ("exit_backscroll_by_pty" option).
*/
#define INITIAL_VTMODE_FLAGS_0 \
SHIFT_FLAG_0(DECMODE_2) | /* is_vt52_mode == 0 */ \
SHIFT_FLAG_0(DECMODE_7) | /* auto_wrap == 1 (compatible with xterm, not with VT220) */ \
SHIFT_FLAG_0(DECMODE_25) | /* is_visible_cursor == 1 */ \
SHIFT_FLAG_0(DECMODE_1034) | /* mod_meta_mode = 8bit (compatible with xterm) */ \
SHIFT_FLAG_0(VTMODE_12); /* local echo is false */
#define INITIAL_VTMODE_FLAGS_1 \
SHIFT_FLAG_1(DECMODE_2) | /* is_vt52_mode == 0 */ \
SHIFT_FLAG_1(DECMODE_7) | /* auto_wrap == 1 (compatible with xterm, not with VT220) */ \
SHIFT_FLAG_1(DECMODE_25) | /* is_visible_cursor == 1 */ \
SHIFT_FLAG_1(DECMODE_1034) | /* mod_meta_mode = 8bit (compatible with xterm) */ \
SHIFT_FLAG_1(VTMODE_12); /* local echo is false */
#define SHIFT_FLAG(mode) (1 << MOD32(mode))
#define GET_VTMODE_FLAG(vt_parser, mode) \
((vt_parser)->vtmode_flags[DIV32(mode)] & SHIFT_FLAG(mode))
/* returns 1 or 0 */
#define GET_VTMODE_FLAG2(vt_parser, mode) \
(((vt_parser)->vtmode_flags[DIV32(mode)] >> MOD32(mode)) & 1)
#define GET_SAVED_VTMODE_FLAG(vt_parser, mode) \
((vt_parser)->saved_vtmode_flags[DIV32(mode)] & SHIFT_FLAG(mode))
/* returns 1 or 0 */
#define GET_SAVED_VTMODE_FLAG2(vt_parser, mode) \
(((vt_parser)->saved_vtmode_flags[DIV32(mode)] >> MOD32(mode)) & 1)
#define IS_APP_CURSOR_KEYS(vt_parser) GET_VTMODE_FLAG(vt_parser, DECMODE_1)
#define ALLOW_DECCOLM(vt_parser) GET_VTMODE_FLAG(vt_parser, DECMODE_40)
#define KEEP_SCREEN_ON_DECCOLM(vt_parser) GET_VTMODE_FLAG(vt_parser, DECMODE_95)
#define BOLD_AFFECTS_BG(vt_parser) GET_VTMODE_FLAG(vt_parser, DECMODE_116)
#define IS_APP_ESCAPE(vt_parser) GET_VTMODE_FLAG(vt_parser, DECMODE_7727)
#define CURSOR_TO_RIGHT_OF_SIXEL(vt_parser) GET_VTMODE_FLAG(vt_parser, DECMODE_8452)
#define SEND_RECV_MODE(vt_parser) GET_VTMODE_FLAG(vt_parser, VTMODE_12)
#define AUTO_CR(vt_parser) GET_VTMODE_FLAG(vt_parser, VTMODE_20)
#define VTMODE(mode) ((mode) + 10000)
#define destroy_drcs(drcs) \
vt_drcs_destroy(drcs); \
(drcs) = NULL;
/*
* If VTMODE_NUM >= 64, enlarge the size of vt_parser_t::vtmode_flags.
* See get_initial_vtmode_flags() to check initial values of these modes.
*/
typedef enum {
/* DECSET/DECRST */
/* vtmode_flags[0] */
DECMODE_1 = 0,
DECMODE_2,
DECMODE_3,
DECMODE_5,
DECMODE_6,
DECMODE_7,
DECMODE_25,
DECMODE_40,
DECMODE_47,
DECMODE_66,
DECMODE_67,
DECMODE_69,
DECMODE_80,
DECMODE_95,
DECMODE_116,
DECMODE_117,
DECMODE_1000,
DECMODE_1002, /* Don't add an entry between 1000 and 1002 (see set_vtmode() idx - DECMODE_1000) */
DECMODE_1003,
DECMODE_1004,
DECMODE_1005,
DECMODE_1006,
DECMODE_1015, /* Don't add an entry between 1005 and 1015 (see set_vtmode() idx - DECMODE_1005) */
DECMODE_1010,
DECMODE_1034,
DECMODE_1036,
DECMODE_1042,
DECMODE_1047,
DECMODE_1048,
DECMODE_1049,
DECMODE_2004,
DECMODE_7727,
/* vtmode_flags[1] */
DECMODE_8428,
DECMODE_8452,
DECMODE_8800,
/* SM/RM */
VTMODE_2,
VTMODE_4,
VTMODE_12,
VTMODE_20,
VTMODE_33,
VTMODE_34,
VTMODE_NUM,
} vtmode_t;
typedef struct area {
u_int32_t min;
u_int32_t max;
} area_t;
/* --- static variables --- */
static u_int16_t vtmodes[] = {
/* DECSET/DECRST */
1, 2, 3, 5, 6, 7, 25, 40, 47, 66, 67, 69, 80, 95, 116, 117,
1000, 1002, /* Don't add an entry between 1000 and 1002 (see set_vtmode()) */
1003, 1004, 1005, 1006, 1015, /* Don't add an entry between 1005 and 1015 (see set_vtmode()) */
1010, 1034, 1036,
1042, 1047, 1048, 1049, 2004, 7727, 8428, 8452, 8800,
/* SM/RM */
VTMODE(2), VTMODE(4), VTMODE(12), VTMODE(20), VTMODE(33), VTMODE(34),
};
static int use_alt_buffer = 1;
static area_t *unicode_noconv_areas;
static u_int num_unicode_noconv_areas;
static area_t *full_width_areas;
static u_int num_full_width_areas;
static area_t *half_width_areas;
static u_int num_half_width_areas;
static char *auto_detect_encodings;
static struct {
vt_char_encoding_t encoding;
ef_parser_t *parser;
} * auto_detect;
static u_int num_auto_detect_encodings;
static int use_ttyrec_format;
static clock_t timeout_read_pty = CLOCKS_PER_SEC / 100; /* 0.01 sec */
static char *primary_da;
static char *secondary_da;
static int is_broadcasting;
static int old_drcs_sixel; /* Compatible behavior with RLogin 2.23.0 or before */
static u_int local_echo_wait_msec = 250;
#ifdef USE_LIBSSH2
static int use_scp_full;
#endif
static u_int8_t alt_color_idxs[] = { 0, 1, 2, 4, 8, 3, 5, 9, 6, 10, 12, 7, 11, 13, 14, 15, } ;
static char *send_file;
static char *recv_dir;
/* --- static functions --- */
#ifdef DEBUG
#if 0
#define debug_print_unknown bl_debug_printf
#else
static void debug_print_unknown(const char *format, ...) {
va_list arg_list;
va_start(arg_list, format);
fprintf(stderr, BL_DEBUG_TAG " received unknown sequence ");
vfprintf(stderr, format, arg_list);
}
#endif
#endif
/* XXX This function should be moved to pobl */
static void str_replace(char *str, int c1, int c2) {
while (*str) {
if (*str == c1) {
*str = c2;
}
str++;
}
}
static area_t *set_area_to_table(area_t *area_table, u_int *num, char *areas) {
char *area;
#ifdef __DEBUG
if (area_table == unicode_noconv_areas) {
bl_debug_printf("Unicode noconv area:");
} else if (area_table == full_width_areas) {
bl_debug_printf("Unicode full width area:");
} else {
bl_debug_printf("Unicode half width area:");
}
bl_msg_printf(" parsing %s\n", areas);
#endif
if (areas == NULL || *areas == '\0') {
free(area_table);
*num = 0;
return NULL;
} else {
void *p;
if (!(p = realloc(area_table, sizeof(*area_table) * (bl_count_char_in_str(areas, ',') + 2)))) {
return area_table;
}
area_table = p;
}
*num = 0;
while ((area = bl_str_sep(&areas, ","))) {
u_int min;
u_int max;
if (vt_parse_unicode_area(area, &min, &max)) {
u_int count = 0;
while (1) {
if (count == *num) {
area_table[*num].min = min;
area_table[(*num)++].max = max;
break;
}
if (area_table[count].min <= min) {
if (area_table[count].max + 1 >= min) {
if (area_table[count].max < max) {
area_table[count].max = max;
}
break;
}
} else {
if (area_table[count].min <= max + 1) {
if (area_table[count].min > min) {
area_table[count].min = min;
}
if (area_table[count].max < max) {
area_table[count].max = max;
}
} else {
memmove(area_table + count + 1, area_table + count,
(*num - count) * sizeof(*area_table));
area_table[count].max = max;
area_table[count].min = min;
(*num)++;
}
break;
}
count++;
}
}
}
#ifdef __DEBUG
{
u_int count;
for (count = 0; count < *num; count++) {
bl_debug_printf("AREA %x-%x\n", area_table[count].min, area_table[count].max);
}
}
#endif
return area_table;
}
static void response_area_table(vt_pty_t *pty, u_char *key, area_t *area_table, u_int num,
int to_menu) {
u_char *value;
/* 20: U+FFFFFFFF-FFFFFFFF, */
if (num > 0 && (value = alloca(20 * num))) {
u_int count;
u_char *p;
p = value;
count = 0;
while (1) {
sprintf(p, area_table[count].min == area_table[count].max ? "U+%x" : "U+%x-%x",
area_table[count].min, area_table[count].max);
p += strlen(p);
if (++count < num) {
*(p++) = ',';
} else {
break;
}
}
} else {
value = "";
}
vt_response_config(pty, key, value, to_menu);
}
static inline int hit_area(area_t *areas, u_int num, u_int code) {
if (num > 0 && areas[0].min <= code && code <= areas[num - 1].max) {
u_int count;
if (num == 1) {
return 1;
}
count = 0;
do {
if (areas[count].min <= code && code <= areas[count].max) {
return 1;
}
} while (++count < num);
}
return 0;
}
static inline int is_noconv_unicode(u_char *ch) {
if (unicode_noconv_areas || ch[2] == 0x20) {
u_int32_t code = ef_bytes_to_int(ch, 4);
if (hit_area(unicode_noconv_areas, num_unicode_noconv_areas, code)) {
return 1;
}
/*
* Don't convert these characters in order not to show them.
* see vt_char_cols().
*/
if ((0x200c <= code && code <= 0x200f) || (0x202a <= code && code <= 0x202e)) {
return 1;
}
}
return 0;
}
static inline ef_property_t modify_ucs_property(u_int32_t code, int col_size_of_width_a,
ef_property_t prop) {
if (prop & EF_AWIDTH) {
#ifdef SUPPORT_VTE_CJK_WIDTH
char *env;
#endif
if (col_size_of_width_a == 2) {
prop |= EF_FULLWIDTH;
}
#ifdef SUPPORT_VTE_CJK_WIDTH
else if ((env = getenv("VTE_CJK_WIDTH")) &&
(strcmp(env, "wide") == 0 || strcmp(env, "1") == 0)) {
prop |= EF_FULLWIDTH;
}
#endif
}
if (prop & EF_FULLWIDTH) {
if (half_width_areas && hit_area(half_width_areas, num_half_width_areas, code)) {
return prop & ~EF_FULLWIDTH;
}
} else {
if (full_width_areas && hit_area(full_width_areas, num_full_width_areas, code)) {
return prop | EF_FULLWIDTH;
}
}
return prop;
}
static void start_vt100_cmd(vt_parser_t *vt_parser,
int trigger_xterm_event /* dispatch to x_screen or not. */
) {
vt_set_use_multi_col_char(vt_parser->use_multi_col_char);
if (trigger_xterm_event && HAS_XTERM_LISTENER(vt_parser, start)) {
/*
* XXX Adhoc implementation.
* Converting visual -> logical in xterm_listener->start.
*/
(*vt_parser->xterm_listener->start)(vt_parser->xterm_listener->self);
} else {
vt_screen_logical(vt_parser->screen);
}
}
static void stop_vt100_cmd(vt_parser_t *vt_parser,
int trigger_xterm_event /* dispatch to x_screen or not. */
) {
vt_screen_render(vt_parser->screen);
vt_screen_visual(vt_parser->screen);
if (trigger_xterm_event && HAS_XTERM_LISTENER(vt_parser, stop)) {
(*vt_parser->xterm_listener->stop)(vt_parser->xterm_listener->self);
}
}
static void interrupt_vt100_cmd(vt_parser_t *vt_parser) {
if (HAS_XTERM_LISTENER(vt_parser, interrupt)) {
vt_screen_render(vt_parser->screen);
vt_screen_visual(vt_parser->screen);
(*vt_parser->xterm_listener->interrupt)(vt_parser->xterm_listener->self);
vt_screen_logical(vt_parser->screen);
}
}
static int change_read_buffer_size(vt_read_buffer_t *r_buf, size_t len) {
void *p;
if (!(p = realloc(r_buf->chars, len))) {
return 0;
}
r_buf->chars = p;
r_buf->len = len;
/*
* Not check if r_buf->left and r_buf->filled_len is larger than r_buf->len.
* It should be checked before calling this function.
*/
return 1;
}
static char* get_now_suffix(char *now /* 16 bytes */) {
time_t t;
struct tm *tm;
time(&t);
tm = localtime(&t);
sprintf(now, "-%04d%02d%02d%02d%02d%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return now;
}
static char *get_home_file_path(const char *prefix, const char *name, const char *suffix) {
char *file_name;
if (!(file_name = alloca(7 + strlen(prefix) + 1 + strlen(name) + 1 + strlen(suffix) + 1))) {
return NULL;
}
sprintf(file_name, "mlterm/%s%s.%s", prefix, name, suffix);
str_replace(file_name + 7, '/', '_');
return bl_get_user_rc_path(file_name);
}
/*
* 0: Error
* 1: No error
* >=2: Probable
*/
static int parse_string(ef_parser_t *cc_parser, u_char *str, size_t len) {
ef_char_t ch;
int ret;
u_int nfull;
u_int nkana;
ret = 1;
nfull = 0;
nkana = 0;
(*cc_parser->init)(cc_parser);
(*cc_parser->set_str)(cc_parser, str, len);
while (1) {
if (!(*cc_parser->next_char)(cc_parser, &ch)) {
if (cc_parser->is_eos) {
if (nkana * 8 > nfull) {
/* kana is over 12.5%. */
ret = 2;
}
return ret;
} else {
if (((str[len - cc_parser->left]) & 0x7f) <= 0x1f) {
/* skip C0 or C1 */
ef_parser_increment(cc_parser);
} else {
return 0;
}
}
} else if (ch.size > 1) {
if (ch.cs == ISO10646_UCS4_1) {
if (ret == 1 && (ch.property & EF_FULLWIDTH)) {
ret = 2;
}
} else {
if (IS_CS94MB(ch.cs)) {
if (ch.ch[0] <= 0x20 || ch.ch[0] == 0x7f || ch.ch[1] <= 0x20 || ch.ch[1] == 0x7f) {
/* mef can return illegal character code. */
return 0;
} else if (ret == 1 && (ch.cs == JISX0208_1983 || ch.cs == JISC6226_1978 ||
ch.cs == JISX0213_2000_1) &&
(ch.ch[0] == 0x24 || ch.ch[0] == 0x25) && 0x21 <= ch.ch[1] &&
ch.ch[1] <= 0x73) {
/* Hiragana/Katakana */
nkana++;
}
}
nfull++;
}
}
}
}
/* Check num_auto_detect_encodings > 0 before calling this function. */
static void detect_encoding(vt_parser_t *vt_parser) {
u_char *str;
size_t len;
size_t count;
u_int idx;
int cur_idx;
int cand_idx;
int threshold;
str = vt_parser->r_buf.chars;
len = vt_parser->r_buf.filled_len;
for (count = 0; count < len - 1; count++) {
if (str[count] >= 0x80 && str[count + 1] >= 0x80) {
goto detect;
}
}
return;
detect:
cur_idx = -1;
threshold = 0;
for (idx = 0; idx < num_auto_detect_encodings; idx++) {
if (auto_detect[idx].encoding == vt_parser->encoding) {
if ((threshold = parse_string(auto_detect[idx].parser, str, len)) > 1) {
return;
}
cur_idx = idx;
break;
}
}
cand_idx = -1;
for (idx = 0; idx < num_auto_detect_encodings; idx++) {
int ret;
if (idx != cur_idx && (ret = parse_string(auto_detect[idx].parser, str, len)) > threshold) {
cand_idx = idx;
if (ret > 1) {
break;
}
}
}
if (cand_idx >= 0) {
#ifdef DEBUG
bl_debug_printf(BL_DEBUG_TAG " Character encoding is changed to %s.\n",
vt_get_char_encoding_name(auto_detect[cand_idx].encoding));
#endif
vt_parser_change_encoding(vt_parser, auto_detect[cand_idx].encoding);
}
}
inline static int is_dcs_or_osc(u_char *str /* The length should be 2 or more. */
) {
return *str == 0x90 || memcmp(str, "\x1bP", 2) == 0 || memcmp(str, "\x1b]", 2) == 0;
}
static void write_ttyrec_header(int fd, size_t len, int keep_time) {
u_int32_t buf[3];
#ifdef HAVE_GETTIMEOFDAY
if (!keep_time) {
struct timeval tval;
gettimeofday(&tval, NULL);
#ifdef WORDS_BIGENDIAN
buf[0] = LE32DEC(((u_char *)&tval.tv_sec));
buf[1] = LE32DEC(((u_char *)&tval.tv_usec));
buf[2] = LE32DEC(((u_char *)&len));
#else
buf[0] = tval.tv_sec;
buf[1] = tval.tv_usec;
buf[2] = len;
#endif
#if __DEBUG
bl_debug_printf("write len %d at %d\n", len, lseek(fd, 0, SEEK_CUR));
#endif
write(fd, buf, 12);
} else
#endif
{
lseek(fd, 8, SEEK_CUR);
#ifdef WORDS_BIGENDIAN
buf[0] = LE32DEC(((u_char *)&len));
#else
buf[0] = len;
#endif
write(fd, buf, 4);
}
}
static int receive_bytes(vt_parser_t *vt_parser) {
size_t len;
if (vt_parser->r_buf.left == vt_parser->r_buf.len) {
/* Buffer is full => Expand buffer */
len = vt_parser->r_buf.len >= PTY_RD_BUFFER_SIZE * 5 ? PTY_RD_BUFFER_SIZE * 10
: PTY_RD_BUFFER_SIZE;
if (!change_read_buffer_size(&vt_parser->r_buf, vt_parser->r_buf.len + len)) {
return 0;
}
} else {
if (0 < vt_parser->r_buf.left && vt_parser->r_buf.left < vt_parser->r_buf.filled_len) {
memmove(vt_parser->r_buf.chars, CURRENT_STR_P(vt_parser),
vt_parser->r_buf.left * sizeof(u_char));
}
/* vt_parser->r_buf.left must be always less than vt_parser->r_buf.len
*/
if ((len = vt_parser->r_buf.len - vt_parser->r_buf.left) > PTY_RD_BUFFER_SIZE &&
!is_dcs_or_osc(vt_parser->r_buf.chars)) {
len = PTY_RD_BUFFER_SIZE;
}
}
if ((vt_parser->r_buf.new_len = vt_read_pty(
vt_parser->pty, vt_parser->r_buf.chars + vt_parser->r_buf.left, len)) == 0) {
vt_parser->r_buf.filled_len = vt_parser->r_buf.left;
return 0;
}
if (vt_parser->logging_vt_seq) {
if (vt_parser->log_file == -1) {
char *path;
char buf[16];
if (!(path = get_home_file_path(vt_pty_get_slave_name(vt_parser->pty) + 5,
get_now_suffix(buf), "log"))) {
goto end;
}
if ((vt_parser->log_file = open(path, O_CREAT | O_WRONLY, 0600)) == -1) {
free(path);
goto end;
}
free(path);
/*
* O_APPEND in open() forces lseek(0,SEEK_END) in write()
* and disables lseek(pos,SEEK_SET) before calling write().
* So don't specify O_APPEND in open() and call lseek(0,SEEK_END)
* manually after open().
*/
lseek(vt_parser->log_file, 0, SEEK_END);
bl_file_set_cloexec(vt_parser->log_file);
if (use_ttyrec_format) {
char seq[6 + DIGIT_STR_LEN(int)*2 + 1];
/* The height of "CSI 8 t" doesn't include status line. */
sprintf(seq, "\x1b[8;%d;%dt", vt_screen_get_logical_rows(vt_parser->screen),
vt_screen_get_logical_cols(vt_parser->screen));
write_ttyrec_header(vt_parser->log_file, strlen(seq), 0);
write(vt_parser->log_file, seq, strlen(seq));
}
}
if (use_ttyrec_format) {
if (vt_parser->r_buf.left > 0) {
lseek(vt_parser->log_file,
lseek(vt_parser->log_file, 0, SEEK_CUR) - vt_parser->r_buf.filled_len - 12,
SEEK_SET);
if (vt_parser->r_buf.left < vt_parser->r_buf.filled_len) {
write_ttyrec_header(vt_parser->log_file,
vt_parser->r_buf.filled_len - vt_parser->r_buf.left, 1);
lseek(vt_parser->log_file,
lseek(vt_parser->log_file, 0, SEEK_CUR) + vt_parser->r_buf.filled_len -
vt_parser->r_buf.left,
SEEK_SET);
}
}
write_ttyrec_header(vt_parser->log_file,
vt_parser->r_buf.left + vt_parser->r_buf.new_len, 0);
write(vt_parser->log_file, vt_parser->r_buf.chars,
vt_parser->r_buf.left + vt_parser->r_buf.new_len);
} else {
write(vt_parser->log_file, vt_parser->r_buf.chars + vt_parser->r_buf.left,
vt_parser->r_buf.new_len);
}
#ifndef USE_WIN32API
fsync(vt_parser->log_file);
#endif
} else {
if (vt_parser->log_file != -1) {
close(vt_parser->log_file);
vt_parser->log_file = -1;
}
}
end:
vt_parser->r_buf.filled_len = (vt_parser->r_buf.left += vt_parser->r_buf.new_len);
if (vt_parser->r_buf.filled_len <= PTY_RD_BUFFER_SIZE &&
vt_parser->r_buf.len > PTY_RD_BUFFER_SIZE) {
/* Shrink buffer */
change_read_buffer_size(&vt_parser->r_buf, PTY_RD_BUFFER_SIZE);
}
if (vt_parser->use_auto_detect && num_auto_detect_encodings > 0) {
detect_encoding(vt_parser);
}
#ifdef INPUT_DEBUG
{
size_t count;
bl_debug_printf(BL_DEBUG_TAG " pty msg (len %d) is received:", vt_parser->r_buf.left);
for (count = 0; count < vt_parser->r_buf.left; count++) {
#ifdef DUMP_HEX
if (isprint(vt_parser->r_buf.chars[count])) {
bl_msg_printf("%c ", vt_parser->r_buf.chars[count]);
} else {
bl_msg_printf("%.2x ", vt_parser->r_buf.chars[count]);
}
#else
bl_msg_printf("%c", vt_parser->r_buf.chars[count]);
#endif
}
bl_msg_printf("[END]\n");
}
#endif
return 1;
}
/*
* If buffer exists, vt_parser->w_buf.last_ch is cached.
* If buffer doesn't exist, vt_parser->w_buf.last_ch is cleared.
*/
static int flush_buffer(vt_parser_t *vt_parser) {
vt_write_buffer_t *buffer;
buffer = &vt_parser->w_buf;
if (buffer->filled_len == 0) {
return 0;
}
#ifdef OUTPUT_DEBUG
{
u_int count;
bl_msg_printf("\nflushing chars(%d)...==>", buffer->filled_len);
for (count = 0; count < buffer->filled_len; count++) {
u_int code = vt_char_code(&buffer->chars[count]);
#ifdef DUMP_HEX
bl_msg_printf("%x", code);
#else
bl_msg_printf("%c", code);
#endif
}
bl_msg_printf("<===\n");
}
#endif
(*buffer->output_func)(vt_parser->screen, buffer->chars, buffer->filled_len);
/* last_ch which will be used & cleared in REP sequence is cached. */
buffer->last_ch = &buffer->chars[buffer->filled_len - 1];
/* buffer is cleared. */
buffer->filled_len = 0;
#ifdef EDIT_DEBUG
vt_edit_dump(vt_parser->screen->edit);
#endif
return 1;
}
static void put_char(vt_parser_t *vt_parser, u_int32_t ch, ef_charset_t cs,
ef_property_t prop) {
vt_color_t fg_color;
vt_color_t bg_color;
int is_fullwidth;
int is_awidth;
int is_comb;
int is_bold;
int is_italic;
int line_style;
int is_blinking;
int is_protected;
int is_reversed;
if (vt_parser->w_buf.filled_len == PTY_WR_BUFFER_SIZE) {
flush_buffer(vt_parser);
}
/*
* checking width property of the char.
*/
if (prop & EF_FULLWIDTH) {
is_fullwidth = 1;
} else {
is_fullwidth = 0;
}
if (prop & EF_AWIDTH) {
is_awidth = 1;
} else {
is_awidth = 0;
}
#ifdef __DEBUG
bl_debug_printf("%x %d %x => %s\n", ch, len, cs, is_fullwidth ? "Fullwidth" : "Single");
#endif
if ((prop & EF_COMBINING)
#if !defined(NO_DYNAMIC_LOAD_CTL) || defined(USE_IND)
|| (ch == '\xe9' && IS_ISCII(cs)) /* nukta is always combined. */
#endif
) {
is_comb = 1;
} else {
is_comb = 0;
}
fg_color = vt_parser->fg_color;
bg_color = vt_parser->bg_color;
is_italic = vt_parser->is_italic ? 1 : 0;
is_blinking = vt_parser->is_blinking ? 1 : 0;
is_protected = vt_parser->is_protected ? 1 : 0;
is_reversed = vt_parser->is_reversed ? 1 : 0;
line_style = vt_parser->line_style;
if (cs == ISO10646_UCS4_1 && 0x2580 <= ch && ch <= 0x259f) {
/* prevent these block characters from being drawn doubly. */
is_bold = 0;
} else {
is_bold = vt_parser->is_bold ? 1 : 0;
}
if (fg_color == VT_FG_COLOR) {
if (is_italic && (vt_parser->alt_color_mode & ALT_COLOR_ITALIC)) {
is_italic = 0;
fg_color = VT_ITALIC_COLOR;
}
if ((line_style & LS_CROSSED_OUT) && (vt_parser->alt_color_mode & ALT_COLOR_CROSSED_OUT)) {
line_style &= ~LS_CROSSED_OUT;
fg_color = VT_CROSSED_OUT_COLOR;
}
if (is_blinking && (vt_parser->alt_color_mode & ALT_COLOR_BLINKING)) {
is_blinking = 0;