-
Notifications
You must be signed in to change notification settings - Fork 7
/
terminal.c
8719 lines (7896 loc) · 319 KB
/
terminal.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
/*
* Terminal emulator.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <wchar.h>
#include <time.h>
#include <assert.h>
#include "putty.h"
#include "terminal.h"
/* base64 for far2l */
#include "../b64/cencode.h"
#include "../b64/cdecode.h"
/* for far2l system tray notifications support */
#include <windows.h>
#include <shellapi.h>
#include <string.h>
#define VT52_PLUS
#define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
#define CL_VT100 0x0002 /* VT100 */
#define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
#define CL_VT102 0x0008 /* VT102 */
#define CL_VT220 0x0010 /* VT220 */
#define CL_VT320 0x0020 /* VT320 */
#define CL_VT420 0x0040 /* VT420 */
#define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
#define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
#define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
#define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
#define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
#define TM_VT100 (CL_ANSIMIN|CL_VT100)
#define TM_VT100AVO (TM_VT100|CL_VT100AVO)
#define TM_VT102 (TM_VT100AVO|CL_VT102)
#define TM_VT220 (TM_VT102|CL_VT220)
#define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
#define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
#define TM_PUTTY (0xFFFF)
#define UPDATE_DELAY ((TICKSPERSEC+49)/50)/* ticks to defer window update */
#define TBLINK_DELAY ((TICKSPERSEC*9+19)/20)/* ticks between text blinks*/
#define CBLINK_DELAY (CURSORBLINK) /* ticks between cursor blinks */
#define VBELL_DELAY (VBELL_TIMEOUT) /* visual bell timeout in ticks */
#define compatibility(x) \
if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
term->termstate=TOPLEVEL; \
break; \
}
#define compatibility2(x,y) \
if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
term->termstate=TOPLEVEL; \
break; \
}
#define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
static const char *const EMPTY_WINDOW_TITLE = "";
static const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
#define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
static const wchar_t sel_nl[] = SEL_NL;
/* forward declaration */
static void term_userpass_state_free(struct term_userpass_state *s);
/*
* Fetch the character at a particular position in a line array,
* for purposes of `wordtype'. The reason this isn't just a simple
* array reference is that if the character we find is UCSWIDE,
* then we must look one space further to the left.
*/
#define UCSGET(a, x) \
( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
/*
* Detect the various aliases of U+0020 SPACE.
*/
#define IS_SPACE_CHR(chr) \
((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
/*
* Spot magic CSETs.
*/
#define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
/*
* Internal prototypes.
*/
static void resizeline(Terminal *, termline *, int);
static termline *lineptr(Terminal *, int, int);
static void check_line_size(Terminal *, termline *);
static void do_paint(Terminal *);
static void erase_lots(Terminal *, bool, bool, bool);
static int find_last_nonempty_line(Terminal *, tree234 *);
static void swap_screen(Terminal *, int, bool, bool);
static void update_sbar(Terminal *);
static void deselect(Terminal *);
static void term_print_finish(Terminal *);
static void scroll(Terminal *, int, int, int, bool);
static void parse_optionalrgb(optionalrgb *out, unsigned *values);
static void term_added_data(Terminal *term, bool);
static void term_update_raw_mouse_mode(Terminal *term);
static void term_out_cb(void *);
static termline *newtermline(Terminal *term, int cols, bool bce)
{
termline *line;
int j;
line = snew(termline);
line->chars = snewn(cols, termchar);
for (j = 0; j < cols; j++)
line->chars[j] = (bce ? term->erase_char : term->basic_erase_char);
line->cols = line->size = cols;
line->lattr = LATTR_NORM;
line->trusted = false;
line->temporary = false;
line->cc_free = 0;
return line;
}
static void freetermline(termline *line)
{
if (line) {
sfree(line->chars);
sfree(line);
}
}
void term_release_line(termline *line)
{
if (line->temporary)
freetermline(line);
}
const int colour_indices_conf_to_oscp[CONF_NCOLOURS] = {
#define COLOUR_ENTRY(id,name) OSCP_COLOUR_##id,
CONF_COLOUR_LIST(COLOUR_ENTRY)
#undef COLOUR_ENTRY
};
const int colour_indices_conf_to_osc4[CONF_NCOLOURS] = {
#define COLOUR_ENTRY(id,name) OSC4_COLOUR_##id,
CONF_COLOUR_LIST(COLOUR_ENTRY)
#undef COLOUR_ENTRY
};
const int colour_indices_oscp_to_osc4[OSCP_NCOLOURS] = {
#define COLOUR_ENTRY(id) OSC4_COLOUR_##id,
OSCP_COLOUR_LIST(COLOUR_ENTRY)
#undef COLOUR_ENTRY
};
#ifdef TERM_CC_DIAGS
/*
* Diagnostic function: verify that a termline has a correct
* combining character structure.
*
* This is a performance-intensive check, so it's no longer enabled
* by default.
*/
static void cc_check(termline *line)
{
unsigned char *flags;
int i, j;
assert(line->size >= line->cols);
flags = snewn(line->size, unsigned char);
for (i = 0; i < line->size; i++)
flags[i] = (i < line->cols);
for (i = 0; i < line->cols; i++) {
j = i;
while (line->chars[j].cc_next) {
j += line->chars[j].cc_next;
assert(j >= line->cols && j < line->size);
assert(!flags[j]);
flags[j] = true;
}
}
j = line->cc_free;
if (j) {
while (1) {
assert(j >= line->cols && j < line->size);
assert(!flags[j]);
flags[j] = true;
if (line->chars[j].cc_next)
j += line->chars[j].cc_next;
else
break;
}
}
j = 0;
for (i = 0; i < line->size; i++)
j += (flags[i] != 0);
assert(j == line->size);
sfree(flags);
}
#endif
static void clear_cc(termline *line, int col);
/*
* Add a combining character to a character cell.
*/
static void add_cc(termline *line, int col, unsigned long chr)
{
int newcc;
assert(col >= 0 && col < line->cols);
/*
* Don't add combining characters at all to U+FFFD REPLACEMENT
* CHARACTER. (Partly it's a slightly incoherent idea in the first
* place; mostly, U+FFFD is what we generate if a cell already has
* too many ccs, in which case we want it to be a fixed point when
* further ccs are added.)
*/
if (line->chars[col].chr == 0xFFFD)
return;
/*
* Walk the cc list of the cell in question to find its current
* end point.
*/
size_t ncc = 0;
int origcol = col;
while (line->chars[col].cc_next) {
col += line->chars[col].cc_next;
if (++ncc >= CC_LIMIT) {
/*
* There are already too many combining characters in this
* character cell. Change strategy: throw out the entire
* chain and replace the main character with U+FFFD.
*
* (Rationale: extrapolating from UTR #36 section 3.6.2
* suggests the principle that it's better to substitute
* U+FFFD than to _ignore_ input completely. Also, if the
* user copies and pastes an overcombined character cell,
* this way it will clearly indicate that we haven't
* reproduced the writer's original intentions, instead of
* looking as if it was the _writer's_ fault that the 33rd
* cc is missing.)
*
* Per the code above, this will also prevent any further
* ccs from being added to this cell.
*/
clear_cc(line, origcol);
line->chars[origcol].chr = 0xFFFD;
return;
}
}
/*
* Extend the cols array if the free list is empty.
*/
if (!line->cc_free) {
int n = line->size;
size_t tmpsize = line->size;
sgrowarray(line->chars, tmpsize, tmpsize);
assert(tmpsize <= INT_MAX);
line->size = tmpsize;
line->cc_free = n;
while (n < line->size) {
if (n+1 < line->size)
line->chars[n].cc_next = 1;
else
line->chars[n].cc_next = 0;
n++;
}
}
/*
* `col' now points at the last cc currently in this cell; so
* we simply add another one.
*/
newcc = line->cc_free;
if (line->chars[newcc].cc_next)
line->cc_free = newcc + line->chars[newcc].cc_next;
else
line->cc_free = 0;
line->chars[newcc].cc_next = 0;
line->chars[newcc].chr = chr;
line->chars[col].cc_next = newcc - col;
#ifdef TERM_CC_DIAGS
cc_check(line);
#endif
}
/*
* Clear the combining character list in a character cell.
*/
static void clear_cc(termline *line, int col)
{
int oldfree, origcol = col;
assert(col >= 0 && col < line->cols);
if (!line->chars[col].cc_next)
return; /* nothing needs doing */
oldfree = line->cc_free;
line->cc_free = col + line->chars[col].cc_next;
while (line->chars[col].cc_next)
col += line->chars[col].cc_next;
if (oldfree)
line->chars[col].cc_next = oldfree - col;
else
line->chars[col].cc_next = 0;
line->chars[origcol].cc_next = 0;
#ifdef TERM_CC_DIAGS
cc_check(line);
#endif
}
/*
* Compare two character cells for equality. Special case required
* in do_paint() where we override what we expect the chr and attr
* fields to be.
*/
static bool termchars_equal_override(termchar *a, termchar *b,
unsigned long bchr, unsigned long battr)
{
/* FULL-TERMCHAR */
if (!truecolour_equal(a->truecolour, b->truecolour))
return false;
if (a->chr != bchr)
return false;
if ((a->attr &~ DATTR_MASK) != (battr &~ DATTR_MASK))
return false;
while (a->cc_next || b->cc_next) {
if (!a->cc_next || !b->cc_next)
return false; /* one cc-list ends, other does not */
a += a->cc_next;
b += b->cc_next;
if (a->chr != b->chr)
return false;
}
return true;
}
static bool termchars_equal(termchar *a, termchar *b)
{
return termchars_equal_override(a, b, b->chr, b->attr);
}
/*
* Copy a character cell. (Requires a pointer to the destination
* termline, so as to access its free list.)
*/
static void copy_termchar(termline *destline, int x, termchar *src)
{
clear_cc(destline, x);
destline->chars[x] = *src; /* copy everything except cc-list */
destline->chars[x].cc_next = 0; /* and make sure this is zero */
while (src->cc_next) {
src += src->cc_next;
add_cc(destline, x, src->chr);
}
#ifdef TERM_CC_DIAGS
cc_check(destline);
#endif
}
/*
* Move a character cell within its termline.
*/
static void move_termchar(termline *line, termchar *dest, termchar *src)
{
/* First clear the cc list from the original char, just in case. */
clear_cc(line, dest - line->chars);
/* Move the character cell and adjust its cc_next. */
*dest = *src; /* copy everything except cc-list */
if (src->cc_next)
dest->cc_next = src->cc_next - (dest-src);
/* Ensure the original cell doesn't have a cc list. */
src->cc_next = 0;
#ifdef TERM_CC_DIAGS
cc_check(line);
#endif
}
#ifndef NO_SCROLLBACK_COMPRESSION
/*
* Compress and decompress a termline into an RLE-based format for
* storing in scrollback. (Since scrollback almost never needs to
* be modified and exists in huge quantities, this is a sensible
* tradeoff, particularly since it allows us to continue adding
* features to the main termchar structure without proportionally
* bloating the terminal emulator's memory footprint unless those
* features are in constant use.)
*/
static void makerle(strbuf *b, termline *ldata,
void (*makeliteral)(strbuf *b, termchar *c,
unsigned long *state))
{
int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos;
bool prev2;
termchar *c = ldata->chars;
unsigned long state = 0, oldstate;
n = ldata->cols;
hdrpos = b->len;
hdrsize = 0;
put_byte(b, 0);
prevlen = prevpos = 0;
prev2 = false;
while (n-- > 0) {
thispos = b->len;
makeliteral(b, c++, &state);
thislen = b->len - thispos;
if (thislen == prevlen &&
!memcmp(b->u + prevpos, b->u + thispos, thislen)) {
/*
* This literal precisely matches the previous one.
* Turn it into a run if it's worthwhile.
*
* With one-byte literals, it costs us two bytes to
* encode a run, plus another byte to write the header
* to resume normal output; so a three-element run is
* neutral, and anything beyond that is unconditionally
* worthwhile. With two-byte literals or more, even a
* 2-run is a win.
*/
if (thislen > 1 || prev2) {
int runpos, runlen;
/*
* It's worth encoding a run. Start at prevpos,
* unless hdrsize==0 in which case we can back up
* another one and start by overwriting hdrpos.
*/
hdrsize--; /* remove the literal at prevpos */
if (prev2) {
assert(hdrsize > 0);
hdrsize--;
prevpos -= prevlen;/* and possibly another one */
}
if (hdrsize == 0) {
assert(prevpos == hdrpos + 1);
runpos = hdrpos;
strbuf_shrink_to(b, prevpos+prevlen);
} else {
memmove(b->u + prevpos+1, b->u + prevpos, prevlen);
runpos = prevpos;
strbuf_shrink_to(b, prevpos+prevlen+1);
/*
* Terminate the previous run of ordinary
* literals.
*/
assert(hdrsize >= 1 && hdrsize <= 128);
b->u[hdrpos] = hdrsize - 1;
}
runlen = prev2 ? 3 : 2;
while (n > 0 && runlen < 129) {
int tmppos, tmplen;
tmppos = b->len;
oldstate = state;
makeliteral(b, c, &state);
tmplen = b->len - tmppos;
bool match = tmplen == thislen &&
!memcmp(b->u + runpos+1, b->u + tmppos, tmplen);
strbuf_shrink_to(b, tmppos);
if (!match) {
state = oldstate;
break; /* run over */
}
n--, c++, runlen++;
}
assert(runlen >= 2 && runlen <= 129);
b->u[runpos] = runlen + 0x80 - 2;
hdrpos = b->len;
hdrsize = 0;
put_byte(b, 0);
/* And ensure this run doesn't interfere with the next. */
prevlen = prevpos = 0;
prev2 = false;
continue;
} else {
/*
* Just flag that the previous two literals were
* identical, in case we find a third identical one
* we want to turn into a run.
*/
prev2 = true;
prevlen = thislen;
prevpos = thispos;
}
} else {
prev2 = false;
prevlen = thislen;
prevpos = thispos;
}
/*
* This character isn't (yet) part of a run. Add it to
* hdrsize.
*/
hdrsize++;
if (hdrsize == 128) {
b->u[hdrpos] = hdrsize - 1;
hdrpos = b->len;
hdrsize = 0;
put_byte(b, 0);
prevlen = prevpos = 0;
prev2 = false;
}
}
/*
* Clean up.
*/
if (hdrsize > 0) {
assert(hdrsize <= 128);
b->u[hdrpos] = hdrsize - 1;
} else {
strbuf_shrink_to(b, hdrpos);
}
}
static void makeliteral_chr(strbuf *b, termchar *c, unsigned long *state)
{
/*
* My encoding for characters is UTF-8-like, in that it stores
* 7-bit ASCII in one byte and uses high-bit-set bytes as
* introducers to indicate a longer sequence. However, it's
* unlike UTF-8 in that it doesn't need to be able to
* resynchronise, and therefore I don't want to waste two bits
* per byte on having recognisable continuation characters.
* Also I don't want to rule out the possibility that I may one
* day use values 0x80000000-0xFFFFFFFF for interesting
* purposes, so unlike UTF-8 I need a full 32-bit range.
* Accordingly, here is my encoding:
*
* 00000000-0000007F: 0xxxxxxx (but see below)
* 00000080-00003FFF: 10xxxxxx xxxxxxxx
* 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
* 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
* 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
*
* (`Z' is like `x' but is always going to be zero since the
* values I'm encoding don't go above 2^32. In principle the
* five-byte form of the encoding could extend to 2^35, and
* there could be six-, seven-, eight- and nine-byte forms as
* well to allow up to 64-bit values to be encoded. But that's
* completely unnecessary for these purposes!)
*
* The encoding as written above would be very simple, except
* that 7-bit ASCII can occur in several different ways in the
* terminal data; sometimes it crops up in the D800 page
* (CSET_ASCII) but at other times it's in the 0000 page (real
* Unicode). Therefore, this encoding is actually _stateful_:
* the one-byte encoding of 00-7F actually indicates `reuse the
* upper three bytes of the last character', and to encode an
* absolute value of 00-7F you need to use the two-byte form
* instead.
*/
if ((c->chr & ~0x7F) == *state) {
put_byte(b, (unsigned char)(c->chr & 0x7F));
} else if (c->chr < 0x4000) {
put_byte(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));
put_byte(b, (unsigned char)(c->chr & 0xFF));
} else if (c->chr < 0x200000) {
put_byte(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));
put_uint16(b, c->chr & 0xFFFF);
} else if (c->chr < 0x10000000) {
put_byte(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));
put_byte(b, (unsigned char)((c->chr >> 16) & 0xFF));
put_uint16(b, c->chr & 0xFFFF);
} else {
put_byte(b, 0xF0);
put_uint32(b, c->chr);
}
*state = c->chr & ~0xFF;
}
static void makeliteral_attr(strbuf *b, termchar *c, unsigned long *state)
{
/*
* My encoding for attributes is 16-bit-granular and assumes
* that the top bit of the word is never required. I either
* store a two-byte value with the top bit clear (indicating
* just that value), or a four-byte value with the top bit set
* (indicating the same value with its top bit clear).
*
* However, first I permute the bits of the attribute value, so
* that the eight bits of colour (four in each of fg and bg)
* which are never non-zero unless xterm 256-colour mode is in
* use are placed higher up the word than everything else. This
* ensures that attribute values remain 16-bit _unless_ the
* user uses extended colour.
*/
unsigned attr, colourbits;
attr = c->attr;
assert(ATTR_BGSHIFT > ATTR_FGSHIFT);
colourbits = (attr >> (ATTR_BGSHIFT + 4)) & 0xF;
colourbits <<= 4;
colourbits |= (attr >> (ATTR_FGSHIFT + 4)) & 0xF;
attr = (((attr >> (ATTR_BGSHIFT + 8)) << (ATTR_BGSHIFT + 4)) |
(attr & ((1 << (ATTR_BGSHIFT + 4))-1)));
attr = (((attr >> (ATTR_FGSHIFT + 8)) << (ATTR_FGSHIFT + 4)) |
(attr & ((1 << (ATTR_FGSHIFT + 4))-1)));
attr |= (colourbits << (32-9));
if (attr < 0x8000) {
put_byte(b, (unsigned char)((attr >> 8) & 0xFF));
put_byte(b, (unsigned char)(attr & 0xFF));
} else {
put_byte(b, (unsigned char)(((attr >> 24) & 0x7F) | 0x80));
put_byte(b, (unsigned char)((attr >> 16) & 0xFF));
put_byte(b, (unsigned char)((attr >> 8) & 0xFF));
put_byte(b, (unsigned char)(attr & 0xFF));
}
}
static void makeliteral_truecolour(strbuf *b, termchar *c, unsigned long *state)
{
/*
* Put the used parts of the colour info into the buffer.
*/
put_byte(b, ((c->truecolour.fg.enabled ? 1 : 0) |
(c->truecolour.bg.enabled ? 2 : 0)));
if (c->truecolour.fg.enabled) {
put_byte(b, c->truecolour.fg.r);
put_byte(b, c->truecolour.fg.g);
put_byte(b, c->truecolour.fg.b);
}
if (c->truecolour.bg.enabled) {
put_byte(b, c->truecolour.bg.r);
put_byte(b, c->truecolour.bg.g);
put_byte(b, c->truecolour.bg.b);
}
}
static void makeliteral_cc(strbuf *b, termchar *c, unsigned long *state)
{
/*
* For combining characters, I just encode a bunch of ordinary
* chars using makeliteral_chr, and terminate with a \0
* character (which I know won't come up as a combining char
* itself).
*
* I don't use the stateful encoding in makeliteral_chr.
*/
unsigned long zstate;
termchar z;
while (c->cc_next) {
c += c->cc_next;
assert(c->chr != 0);
zstate = 0;
makeliteral_chr(b, c, &zstate);
}
z.chr = 0;
zstate = 0;
makeliteral_chr(b, &z, &zstate);
}
typedef struct compressed_scrollback_line {
size_t len;
/* compressed data follows after this */
} compressed_scrollback_line;
static termline *decompressline_no_free(compressed_scrollback_line *line);
static compressed_scrollback_line *compressline_no_free(termline *ldata)
{
strbuf *b = strbuf_new();
/* Leave space for the header structure */
strbuf_append(b, sizeof(compressed_scrollback_line));
/*
* First, store the column count, 7 bits at a time, least
* significant `digit' first, with the high bit set on all but
* the last.
*/
{
int n = ldata->cols;
while (n >= 128) {
put_byte(b, (unsigned char)((n & 0x7F) | 0x80));
n >>= 7;
}
put_byte(b, (unsigned char)(n));
}
/*
* Next store the lattrs; same principle. We add one extra bit to
* this to indicate the trust state of the line.
*/
{
int n = ldata->lattr | (ldata->trusted ? 0x10000 : 0);
while (n >= 128) {
put_byte(b, (unsigned char)((n & 0x7F) | 0x80));
n >>= 7;
}
put_byte(b, (unsigned char)(n));
}
/*
* Now we store a sequence of separate run-length encoded
* fragments, each containing exactly as many symbols as there
* are columns in the ldata.
*
* All of these have a common basic format:
*
* - a byte 00-7F indicates that X+1 literals follow it
* - a byte 80-FF indicates that a single literal follows it
* and expects to be repeated (X-0x80)+2 times.
*
* The format of the `literals' varies between the fragments.
*/
makerle(b, ldata, makeliteral_chr);
makerle(b, ldata, makeliteral_attr);
makerle(b, ldata, makeliteral_truecolour);
makerle(b, ldata, makeliteral_cc);
size_t linelen = b->len - sizeof(compressed_scrollback_line);
compressed_scrollback_line *line =
(compressed_scrollback_line *)strbuf_to_str(b);
line->len = linelen;
/*
* Diagnostics: ensure that the compressed data really does
* decompress to the right thing.
*
* This is a bit performance-heavy for production code.
*/
#ifdef TERM_CC_DIAGS
#ifndef CHECK_SB_COMPRESSION
{
termline *dcl;
int i;
#ifdef DIAGNOSTIC_SB_COMPRESSION
for (i = 0; i < b->len; i++) {
printf(" %02x ", b->data[i]);
}
printf("\n");
#endif
dcl = decompressline_no_free(line);
assert(ldata->cols == dcl->cols);
assert(ldata->lattr == dcl->lattr);
for (i = 0; i < ldata->cols; i++)
assert(termchars_equal(&ldata->chars[i], &dcl->chars[i]));
#ifdef DIAGNOSTIC_SB_COMPRESSION
printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
ldata->cols, 4 * ldata->cols, dused,
(double)dused / (4 * ldata->cols));
#endif
freetermline(dcl);
}
#endif
#endif /* TERM_CC_DIAGS */
return line;
}
static compressed_scrollback_line *compressline_and_free(termline *ldata)
{
compressed_scrollback_line *cline = compressline_no_free(ldata);
freetermline(ldata);
return cline;
}
static void readrle(BinarySource *bs, termline *ldata,
void (*readliteral)(BinarySource *bs, termchar *c,
termline *ldata, unsigned long *state))
{
int n = 0;
unsigned long state = 0;
while (n < ldata->cols) {
int hdr = get_byte(bs);
if (hdr >= 0x80) {
/* A run. */
size_t pos = bs->pos, count = hdr + 2 - 0x80;
while (count--) {
assert(n < ldata->cols);
bs->pos = pos;
readliteral(bs, ldata->chars + n, ldata, &state);
n++;
}
} else {
/* Just a sequence of consecutive literals. */
int count = hdr + 1;
while (count--) {
assert(n < ldata->cols);
readliteral(bs, ldata->chars + n, ldata, &state);
n++;
}
}
}
assert(n == ldata->cols);
}
static void readliteral_chr(BinarySource *bs, termchar *c, termline *ldata,
unsigned long *state)
{
int byte;
/*
* 00000000-0000007F: 0xxxxxxx
* 00000080-00003FFF: 10xxxxxx xxxxxxxx
* 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
* 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
* 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
*/
byte = get_byte(bs);
if (byte < 0x80) {
c->chr = byte | *state;
} else if (byte < 0xC0) {
c->chr = (byte &~ 0xC0) << 8;
c->chr |= get_byte(bs);
} else if (byte < 0xE0) {
c->chr = (byte &~ 0xE0) << 16;
c->chr |= get_uint16(bs);
} else if (byte < 0xF0) {
c->chr = (byte &~ 0xF0) << 24;
c->chr |= get_byte(bs) << 16;
c->chr |= get_uint16(bs);
} else {
assert(byte == 0xF0);
c->chr = get_uint32(bs);
}
*state = c->chr & ~0xFF;
}
static void readliteral_attr(BinarySource *bs, termchar *c, termline *ldata,
unsigned long *state)
{
unsigned val, attr, colourbits;
val = get_uint16(bs);
if (val >= 0x8000) {
val &= ~0x8000;
val <<= 16;
val |= get_uint16(bs);
}
colourbits = (val >> (32-9)) & 0xFF;
attr = (val & ((1<<(32-9))-1));
attr = (((attr >> (ATTR_FGSHIFT + 4)) << (ATTR_FGSHIFT + 8)) |
(attr & ((1 << (ATTR_FGSHIFT + 4))-1)));
attr = (((attr >> (ATTR_BGSHIFT + 4)) << (ATTR_BGSHIFT + 8)) |
(attr & ((1 << (ATTR_BGSHIFT + 4))-1)));
attr |= (colourbits >> 4) << (ATTR_BGSHIFT + 4);
attr |= (colourbits & 0xF) << (ATTR_FGSHIFT + 4);
c->attr = attr;
}
static void readliteral_truecolour(
BinarySource *bs, termchar *c, termline *ldata, unsigned long *state)
{
int flags = get_byte(bs);
if (flags & 1) {
c->truecolour.fg.enabled = true;
c->truecolour.fg.r = get_byte(bs);
c->truecolour.fg.g = get_byte(bs);
c->truecolour.fg.b = get_byte(bs);
} else {
c->truecolour.fg = optionalrgb_none;
}
if (flags & 2) {
c->truecolour.bg.enabled = true;
c->truecolour.bg.r = get_byte(bs);
c->truecolour.bg.g = get_byte(bs);
c->truecolour.bg.b = get_byte(bs);
} else {
c->truecolour.bg = optionalrgb_none;
}
}
static void readliteral_cc(BinarySource *bs, termchar *c, termline *ldata,
unsigned long *state)
{
termchar n;
unsigned long zstate;
int x = c - ldata->chars;
c->cc_next = 0;
while (1) {
zstate = 0;
readliteral_chr(bs, &n, ldata, &zstate);
if (!n.chr)
break;
add_cc(ldata, x, n.chr);
}
}
static termline *decompressline_no_free(compressed_scrollback_line *line)
{
int ncols, byte, shift;
BinarySource bs[1];
termline *ldata;
BinarySource_BARE_INIT(bs, line+1, line->len);
/*
* First read in the column count.
*/
ncols = shift = 0;
do {
byte = get_byte(bs);
ncols |= (byte & 0x7F) << shift;
shift += 7;
} while (byte & 0x80);
/*
* Now create the output termline.
*/
ldata = snew(termline);
ldata->chars = snewn(ncols, termchar);
ldata->cols = ldata->size = ncols;
ldata->temporary = true;
ldata->cc_free = 0;
/*
* We must set all the cc pointers in ldata->chars to 0 right
* now, so that cc diagnostics that verify the integrity of the
* whole line will make sense while we're in the middle of
* building it up.
*/
{
int i;
for (i = 0; i < ldata->cols; i++)
ldata->chars[i].cc_next = 0;
}
/*
* Now read in the lattr.
*/
int lattr = shift = 0;
do {
byte = get_byte(bs);
lattr |= (byte & 0x7F) << shift;
shift += 7;
} while (byte & 0x80);
ldata->lattr = lattr & 0xFFFF;
ldata->trusted = (lattr & 0x10000) != 0;
/*
* Now we read in each of the RLE streams in turn.
*/
readrle(bs, ldata, readliteral_chr);
readrle(bs, ldata, readliteral_attr);