-
Notifications
You must be signed in to change notification settings - Fork 3
/
yascreen.c
1980 lines (1736 loc) · 48.4 KB
/
yascreen.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
// $Id: yascreen.c,v 1.99 2023/08/02 17:32:12 bbonev Exp $
//
// Copyright © 2015-2023 Boian Bonev ([email protected]) {{{
//
// SPDX-License-Identifer: LGPL-3.0-or-later
//
// This file is part of yascreen - yet another screen library.
//
// yascreen is free software, released under the terms of GNU Lesser General Public License v3.0 or later
// }}}
// {{{ includes
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <time.h>
#include <wchar.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <yascreen.h>
// }}}
// {{{ definitions
#define mymax(a,b) (((a)>(b))?(a):(b))
#define mymin(a,b) (((a)<(b))?(a):(b))
#define ESC "\x1b"
// size of string that can be stored immediately w/o allocation
#define PSIZE (sizeof(char *))
// step to allocate key buffer
#define KEYSTEP (4096/sizeof(int))
// default timeout in milliseconds before escape is returned
#define YAS_DEFAULT_ESCTO 300
// check if a given value is a valid simple color value
#define YAS_ISCOLOR(c) ((c)>=8&&(c)<=15)
// check if a given value is a valid extended color value
#define YAS_ISXCOLOR(c) ((c)&0x100)
#define YAS_STORAGE 0x80000000 // data is longer than PSIZE and is stored in allocated memory
#define YAS_TOUCHED 0x40000000 // there are changes in this line, update cannot skip it
#define YAS_INTERNAL (YAS_STORAGE|YAS_TOUCHED)
#define TELNET_EOSN 240 // 0xf0 // end of subnegotiation
#define TELNET_NOP 241 // 0xf1 // NOP
#define TELNET_SYNCH 242 // 0xf2 // SYNCH
#define TELNET_NVTBRK 243 //0xf3 // NVTBRK
#define TELNET_IP 244 // 0xf4 // IP
#define TELNET_AO 245 // 0xf5 // AO
#define TELNET_AYT 246 //0xf6 // AYT are you there
#define TELNET_EC 247 // 0xf7 // EC
#define TELNET_EL 248 // 0xf8 // EL
#define TELNET_GOA 249 // 0xf9 // go ahead
#define TELNET_SOSN 250 // 0xfa // start of subnegotiation
#define TELNET_WILL 251 // 0xfb // will
#define TELNET_WONT 252 // 0xfc // wont
#define TELNET_DO 253 // 0xfd // do
#define TELNET_DONT 254 // 0xfe // dont
#define TELNET_IAC 255 // 0xff // telnet protocol escape code (IAC)
#define TELNET_NOOP 0x100 // telnet protocol handler have eaten a byte w/o yielding any result
#define TELNET_SIZE 0x101 // telnet protocol handler have detected screen size change notification
#define TELNET_NAWS 31 // 0x1f // negotiate about window size
// data is kept as utf8, including its combining chars
// if it fits in PSZIE, it is in d, with 0 terminating char
// if the char at previous position requires 2 columns, current char should be empty
// after initialization all chars are set to ' ' (0x20)
typedef struct _cell {
uint32_t style; // color, style and storage type
union {
char *p;
char d[PSIZE];
};
} cell;
typedef enum { // ansi sequence state machine
ST_NORM, // normal input, check for ESC
ST_ENTER, // eat LF/NUL after CR
ST_ESC, // escape sequence
ST_ESC_SQ, // escape [ sequence
ST_ESC_SQ_D, // escape [ digit sequence
ST_ESC_O, // escape O sequence
ST_ESC_ESC, // escape escape sequence
} yas_k_state;
typedef enum { // telnet sequence state machine
T_NORM, // expect regular byte or IAC
T_IAC, // telnet IAC, expect option
T_IAC_O, // telnet IAC option, expect opt code
T_IAC_SB, // telnet IAC extended, expect IAC
T_IAC_SE, // telnet IAC extended, expect SE
} yas_t_state;
typedef enum { // utf8 sequence state machine
U_NORM, // expect single byte or leading byte
U_L2C1, // expect 1/1 continuation byte
U_L3C1, // expect 1/2 continuation bytes
U_L3C2, // expect 2/2 continuation bytes
U_L4C1, // expect 1/3 continuation bytes
U_L4C2, // expect 2/3 continuation bytes
U_L4C3, // expect 3/3 continuation bytes
U_L5C1, // expect 1/4 continuation bytes
U_L5C2, // expect 2/4 continuation bytes
U_L5C3, // expect 3/4 continuation bytes
U_L5C4, // expect 4/4 continuation bytes
U_L6C1, // expect 1/5 continuation bytes
U_L6C2, // expect 2/5 continuation bytes
U_L6C3, // expect 3/5 continuation bytes
U_L6C4, // expect 4/5 continuation bytes
U_L6C5, // expect 5/5 continuation bytes
} yas_u_state;
struct _yascreen {
int sx,sy; // size of screen
ssize_t (*outcb)(yascreen *s,const void *data,size_t len); // output callback
cell *mem; // memory state
cell *scr; // screen state
struct termios *tsstack; // saved terminal state
int tssize; // number of items in the stack
int escto; // single ESC key timeout (in milliseconds)
int keysize; // saved key storage size
int keycnt; // saved key count
int *keys; // saved key array
unsigned char ansibuf[20]; // buffer for escape sequence parsing
unsigned char ansipos; // next byte will go in this pos
unsigned char sosnbuf[20]; // buffer for telnet SOSN options parsing
unsigned char sosnpos; // next byte will go in this pos
unsigned char utf[3]; // buffer for utf8 sequence parsing; last byte is not put here, its never zero terminated
// must be increased to 4 or 5 if some day unicode permits 5 or 6 byte sequences
int64_t escts; // single ESC key timestamp
yas_k_state state; // input parser state
yas_t_state tstate; // telnet parser state
yas_u_state ustate; // utf8 parser state
int cursorx; // position to place cursor on update
int cursory; // position to place cursor on update
int scrx; // last reported screen size
int scry; // last reported screen size
uint8_t haveansi:1; // we do have a reported screen size from ansi sequence
uint8_t havenaws:1; // we do have a reported screen size from telent naws
uint8_t istelnet:1; // do process telnet sequences
uint8_t isunicode:1; // do process unicode sequences
uint8_t cursor:1; // last cursor state
uint8_t redraw:1; // flag to redraw from scratch
uint8_t lineflush:1; // always flush after line operations
int hint; // user defined hint (scalar)
void *phint; // user defined hint (pointer)
uint8_t outb[256]; // buffered output
uint16_t outp; // position in outb
};
// helpers for versioned symbols
#if YASCREEN_VERSIONED // implementation always follows the attribute
#if defined __GNUC__ && __GNUC__ >= 10 && !defined(__clang__)
#define symver_o(impl,sym,ver) __attribute__((symver(#sym"@"#ver)))
#define symver_d(impl,sym,ver) __attribute__((symver(#sym"@@"#ver)))
#else
#define symver_o(impl,sym,ver) asm(".symver "#impl","#sym"@"#ver);
#define symver_d(impl,sym,ver) asm(".symver "#impl","#sym"@@"#ver);
#endif
#define VV(foo,ver) foo##ver
#define V(foo,ver) VV(foo,ver)
#define V193 _193
#else
#define symver_o(impl,sym,ver)
#define symver_d(impl,sym,ver)
#define V(foo,ver) foo
#define V193
#endif
#define symver_V(impl,sym,ver) symver_d(impl,sym,ver)
#define YASCREEN_193 YASCREEN_1.93
// }}}
static inline int64_t mytime() { // {{{
struct timespec ts;
int64_t res;
clock_gettime(CLOCK_MONOTONIC,&ts);
res=ts.tv_sec*1000;
res+=ts.tv_nsec/1000000;
return res;
} // }}}
static inline ssize_t out(yascreen *s,const void *vbuf,size_t len) { // {{{
const uint8_t *buf=vbuf;
size_t olen=len;
repeat:
if (len) {
if (sizeof s->outb-s->outp>=len) {
memcpy(s->outb+s->outp,buf,len);
s->outp+=len;
} else {
size_t brem=sizeof s->outb-s->outp;
ssize_t wr;
memcpy(s->outb+s->outp,buf,brem);
s->outp+=brem;
buf+=brem;
len-=brem;
wr=write(STDOUT_FILENO,s->outb,s->outp);
if (wr<=0) // error
return wr;
if (wr==s->outp) {
s->outp-=wr;
goto repeat;
}
// wr>0 and wr<s->outp
memmove(s->outb,s->outb+wr,s->outp-wr);
s->outp-=wr;
goto repeat;
}
}
if (!olen&&s->outp) { // flush is requested
ssize_t wr=write(STDOUT_FILENO,s->outb,s->outp);
if (wr==s->outp)
s->outp=0;
else
if (wr>0) {
memmove(s->outb,s->outb+wr,s->outp-wr);
s->outp-=wr;
}
}
return olen;
} // }}}
static inline void outs(yascreen *s,const char *str) { // {{{
ssize_t (*o)(yascreen *s,const void *buf,size_t len);
size_t len;
if (!s)
return;
if (!str)
return;
len=strlen(str);
o=s->outcb?s->outcb:out;
if (!len) // explicit flush
o(s,"",0);
while (len) {
ssize_t r=o(s,str,len);
if (r>=0) {
len-=r;
str+=r;
} else
break;
}
} // }}}
static inline void outf(yascreen *s,const char *format,...) __attribute__((format(printf,2,3))); // {{{
// }}}
static inline void outf(yascreen *s,const char *format,...) { // {{{
va_list ap;
char *ns;
int size;
if (!s)
return;
if (!format)
return;
va_start(ap,format);
size=vasprintf(&ns,format,ap);
va_end(ap);
if (size==-1) // some error, nothing more to do
return;
outs(s,ns);
free(ns);
} // }}}
inline void yascreen_set_hint_i(yascreen *s,int hint) { // {{{
if (!s)
return;
s->hint=hint;
} // }}}
inline int yascreen_get_hint_i(yascreen *s) { // {{{
if (!s)
return 0;
return s->hint;
} // }}}
inline void yascreen_set_hint_p(yascreen *s,void *hint) { // {{{
if (!s)
return;
s->phint=hint;
} // }}}
inline void *yascreen_get_hint_p(yascreen *s) { // {{{
if (!s)
return NULL;
return s->phint;
} // }}}
static char myver[]="\0Yet another screen library (https://github.com/bbonev/yascreen) $Revision: 1.99 $\n\n"; // {{{
// }}}
inline const char *yascreen_ver(void) { // {{{
return myver;
} // }}}
inline yascreen *yascreen_init(int sx,int sy) { // {{{
yascreen *s;
int i;
if (myver[0]==0) { // reformat the static version string
char *rev=strstr(myver+1,"$Revision: ");
int vermaj,vermin;
if (rev) {
sscanf(rev+strlen("$Revision: "),"%d.%d",&vermaj,&vermin);
vermaj+=vermin/100;
vermin=vermin%100;
memmove(myver,myver+1,strlen(myver+1)+1);
snprintf(rev-1,sizeof myver-(rev-1-myver),"%d.%02d\n\n",vermaj,vermin);
}
}
if (sx<0||sy<0)
return NULL;
s=(yascreen *)calloc(1,sizeof *s);
if (!s)
return NULL;
if (/*!s->outcb&&*/isatty(STDOUT_FILENO)) { // output is a terminal
s->tsstack=(struct termios *)calloc(1,sizeof(struct termios));
if (!s->tsstack) {
free(s);
return NULL;
}
s->tssize=1;
tcgetattr(STDOUT_FILENO,s->tsstack);
if (!sx||!sy) {
struct winsize ws={0};
if (!ioctl(STDOUT_FILENO,TIOCGWINSZ,&ws)) {
if (!sx)
sx=ws.ws_col;
if (!sy)
sy=ws.ws_row;
}
}
}
if (sx<=0||sy<=0) {
if (s->tsstack)
free(s->tsstack);
free(s);
return NULL;
}
s->sx=sx;
s->sy=sy;
// s->outcb is already NULL
// s->mem is initialized below
// s->scr is initialized below
// s->tstack is initialized above
// s->tssize is initialized above
s->escto=YAS_DEFAULT_ESCTO;
// s->keysize is initialized below
// s->keycnt is already zero
// s->keys is initialized below
// s->ansibuf is already zeroes
// s->ansipos is already zero
// s->sosnbuf is already zeroes
// s->sosnpos is already zero
// s->utf is already zeroes
// s->escts is already zero
s->state=ST_NORM;
s->tstate=T_NORM;
s->ustate=U_NORM;
// s->cursorx is already zero
// s->cursory is already zero
// s->scrx is already zero
// s->scry is already zero
// s->haveansi is already zero
// s->havenaws is already zero
// s->istelnet is already zero
s->isunicode=1; // previous versions were unicode only
s->cursor=1; // cursor is visible by default
s->redraw=1; // leave scr empty, so that on first refresh everything is redrawn
s->lineflush=1; // be compatible with earlier versions that worked without output buffering normally a recent client will set this to 0 and use explicit flush
// s->hint is already zero
// s->phint is already NULL
// s->outb is already zeroes
// s->outp is already zero
s->keys=(int *)calloc(KEYSTEP,sizeof(int));
if (!s->keys) {
if (s->tsstack)
free(s->tsstack);
free(s);
return NULL;
}
s->keysize=KEYSTEP;
s->mem=(cell *)calloc(sx*sy,sizeof(cell));
s->scr=(cell *)calloc(sx*sy,sizeof(cell));
if (!s->mem||!s->scr) {
if (s->mem)
free(s->mem);
if (s->scr)
free(s->scr);
if (s->tsstack)
free(s->tsstack);
if (s->keys)
free(s->keys);
free(s);
return NULL;
}
for (i=0;i<sx*sy;i++)
strncpy(s->mem[i].d," ",sizeof s->mem[i].d);
return s;
} // }}}
inline int yascreen_setout(yascreen *s,ssize_t (*out)(yascreen *s,const void *data,size_t len)) { // {{{
if (!s)
return -1;
s->outcb=out;
s->redraw=1;
return 0;
} // }}}
inline void yascreen_set_telnet(yascreen *s,int on) { // {{{
if (!s)
return;
s->istelnet=!!on;
} // }}}
inline void yascreen_set_unicode(yascreen *s,int on) { // {{{
if (!s)
return;
s->isunicode=!!on;
s->keycnt=0; // flush input buffer - it may not be verified unicode
} // }}}
inline void yascreen_init_telnet(yascreen *s) { // {{{
if (!s)
return;
if (s->istelnet)
outs(s,
"\xff\xfb\x03" // will(251) suppress go ahead
"\xff\xfb\x01" // will(251) echo
"\xff\xfd\x03" // do(253) suppress go ahead
"\xff\xfd\x01" // do(253) echo
"\xff\xfb\x1f" // will(251) negotiate terminal size
"\xff\xfd\x1f" // do(253) negotiate terminal size
);
else
outs(s,
"\xff\xfc\x03" // wont(251) suppress go ahead
"\xff\xfc\x01" // wont(251) echo
"\xff\xfe\x03" // dont(253) suppress go ahead
"\xff\xfe\x01" // dont(253) echo
"\xff\xfc\x1f" // wont(251) negotiate terminal size
"\xff\xfe\x1f" // dont(253) negotiate terminal size
);
} // }}}
inline int yascreen_resize(yascreen *s,int sx,int sy) { // {{{
cell *mem,*scr;
int i;
if (!s)
return -1;
if (sx<0||sy<0)
return -1;
if (!sx||!sy)
if (!s->outcb&&isatty(STDOUT_FILENO)) {
struct winsize ws={0};
if (!ioctl(STDOUT_FILENO,TIOCGWINSZ,&ws)) {
if (!sx)
sx=ws.ws_col;
if (!sy)
sy=ws.ws_row;
}
}
if (sx<=0||sy<=0)
return -1;
if (s->sx==sx&&s->sy==sy)
return 0;
for (i=0;i<s->sx*s->sy;i++) { // free old allocated data and set for reusage
if (s->mem[i].style&YAS_STORAGE)
free(s->mem[i].p);
if (s->scr[i].style&YAS_STORAGE)
free(s->scr[i].p);
if (i<sx*sy) {
s->mem[i].style=s->scr[i].style=0;
s->mem[i].d[0]=' ';
s->scr[i].d[0]=0;
}
}
if (sx*sy>s->sx*s->sy) { // allocate bigger buffer
mem=(cell *)realloc(s->mem,sx*sy*sizeof(cell));
if (!mem)
return -1;
s->mem=mem;
scr=(cell *)realloc(s->scr,sx*sy*sizeof(cell));
if (!scr)
return -1;
s->scr=scr;
for (i=s->sx*s->sy;i<sx*sy;i++) { // initialize the rest of the area
s->mem[i].style=s->scr[i].style=0;
s->mem[i].d[0]=' ';
s->scr[i].d[0]=0;
}
}
s->redraw=1;
s->sx=sx;
s->sy=sy;
return 0;
} // }}}
inline void yascreen_free(yascreen *s) { // {{{
int i;
if (!s)
return;
if (!s->mem||!s->scr) { // error condition that will happen only if mem is corrupt
if (s->mem)
free(s->mem);
if (s->scr)
free(s->scr);
if (s->tsstack)
free(s->tsstack);
if (s->keys)
free(s->keys);
free(s); // most probably will crash, because there is no way to have s partally initialized
return;
}
for (i=0;i<s->sx*s->sy;i++) {
if (s->mem[i].style&YAS_STORAGE)
free(s->mem[i].p);
if (s->scr[i].style&YAS_STORAGE)
free(s->scr[i].p);
}
free(s->mem);
free(s->scr);
if (s->tsstack)
free(s->tsstack);
if (s->keys)
free(s->keys);
outs(s,ESC"[0m");
free(s);
} // }}}
inline void yascreen_update_attr(yascreen *s,uint32_t oattr,uint32_t nattr) { // {{{
if (!s)
return;
if (oattr==0xffffffff) {
oattr=~nattr; // force setting all
outs(s,ESC"[0m");
}
if ((oattr&YAS_BOLD)!=(nattr&YAS_BOLD))
outs(s,(nattr&YAS_BOLD)?ESC"[1m":ESC"[22m");
if ((oattr&YAS_ITALIC)!=(nattr&YAS_ITALIC))
outs(s,(nattr&YAS_ITALIC)?ESC"[3m":ESC"[23m");
if ((oattr&YAS_UNDERL)!=(nattr&YAS_UNDERL))
outs(s,(nattr&YAS_UNDERL)?ESC"[4m":ESC"[24m");
if ((oattr&YAS_BLINK)!=(nattr&YAS_BLINK))
outs(s,(nattr&YAS_BLINK)?ESC"[5m":ESC"[25m");
if ((oattr&YAS_INVERSE)!=(nattr&YAS_INVERSE))
outs(s,(nattr&YAS_INVERSE)?ESC"[7m":ESC"[27m");
if ((oattr&YAS_STRIKE)!=(nattr&YAS_STRIKE))
outs(s,(nattr&YAS_STRIKE)?ESC"[9m":ESC"[29m");
if (YAS_FG(oattr)!=YAS_FG(nattr)) {
if (YAS_ISXCOLOR(YAS_FG(nattr)))
outf(s,ESC"[38;5;%dm",YAS_FG(nattr)-0x100);
else {
if (YAS_ISCOLOR(YAS_FG(nattr)))
outf(s,ESC"[%dm",YAS_FG(nattr)-8+30);
else
outs(s,ESC"[39m");
}
}
if (YAS_BG(oattr)!=YAS_BG(nattr)) {
if (YAS_ISXCOLOR(YAS_BG(nattr)))
outf(s,ESC"[48;5;%dm",YAS_BG(nattr)-0x100);
else {
if (YAS_ISCOLOR(YAS_BG(nattr)))
outf(s,ESC"[%dm",YAS_BG(nattr)-8+40);
else
outs(s,ESC"[49m");
}
}
} // }}}
static inline int yascreen_update_range(yascreen *s,int y1,int y2) { // {{{
int i,j,ob=0,redraw=0;
char ra[]=ESC"[0m";
uint32_t lsty=0,nsty;
if (!s)
return -1;
if (s->redraw) {
redraw=1;
s->redraw=0;
outf(s,ESC"[2J"ESC"[H%s",ra); // clear and position on topleft
*ra=0;
}
y1=mymin(s->sy-1,mymax(0,y1));
y2=mymin(s->sy,mymax(0,y2));
for (j=y1;j<y2;j++) {
int skip=1,cnt=0;
if (!redraw&&!(s->mem[s->sx*j].style&YAS_TOUCHED)) // skip untouched lines
continue;
s->mem[s->sx*j].style&=~YAS_TOUCHED; // mark updated lines as not touched
for (i=0;i<s->sx;i++) {
int diff=redraw; // forced redraw
if (!diff) // compare attributes
diff=(s->mem[i+s->sx*j].style&~YAS_INTERNAL)!=(s->scr[i+s->sx*j].style&~YAS_INTERNAL);
if (!diff) // compare content
diff=!!strcmp((s->mem[i+s->sx*j].style&YAS_STORAGE)?s->mem[i+s->sx*j].p:s->mem[i+s->sx*j].d,(s->scr[i+s->sx*j].style&YAS_STORAGE)?s->scr[i+s->sx*j].p:s->scr[i+s->sx*j].d);
if (diff||!skip) {
if (skip) {
outf(s,ESC"[%d;%dH%s",1+j,1+i,ra);
*ra=0;
skip=0;
}
if (diff) {
if (cnt>7) {
outf(s,ESC"[%d;%dH%s",1+j,1+i,ra);
*ra=0;
cnt=0;
}
while (cnt>=0) {
nsty=s->mem[j*s->sx+i-cnt].style&~YAS_INTERNAL;
if (lsty!=nsty) {
yascreen_update_attr(s,lsty,nsty);
lsty=nsty;
}
outs(s,(s->mem[j*s->sx+i-cnt].style&YAS_STORAGE)?s->mem[j*s->sx+i-cnt].p:s->mem[j*s->sx+i-cnt].d);
cnt--;
}
cnt=0; // loop above leaves cnt at -1
} else
cnt++;
}
if (s->scr[j*s->sx+i].style&YAS_STORAGE)
free(s->scr[j*s->sx+i].p);
s->scr[j*s->sx+i].style=s->mem[j*s->sx+i].style;
if (s->mem[j*s->sx+i].style&YAS_STORAGE)
s->scr[j*s->sx+i].p=strdup(s->mem[j*s->sx+i].p);
else
strncpy(s->scr[j*s->sx+i].d,s->mem[j*s->sx+i].d,sizeof s->scr[j*s->sx+i].d);
}
}
if (s->cursor)
outf(s,ESC"[%d;%dH",s->cursory+1,s->cursorx+1);
outs(s,""); // request a flush
return ob;
} // }}}
inline int yascreen_update(yascreen *s) { // {{{
if (!s)
return -1;
return yascreen_update_range(s,0,s->sy);
} // }}}
static inline void yascreen_putcw(yascreen *s,uint32_t attr,const char *str,int width) { // {{{
if (!*str) // noop
return;
if (!str[1]) { // handle CR/LF
switch (*str) {
case '\n':
s->cursory++;
// fall through
case '\r':
s->cursorx=0;
return;
}
}
if (s->cursory<0||s->cursory>=s->sy)
return;
if (width&&s->cursorx>=0&&s->cursorx<s->sx&&s->cursorx+width<=s->sx) {
int i;
// normal char
if (s->mem[s->cursorx+s->cursory*s->sx].style&YAS_STORAGE) {
s->mem[s->cursorx+s->cursory*s->sx].style&=~YAS_STORAGE;
free(s->mem[s->cursorx+s->cursory*s->sx].p);
s->mem[s->cursorx+s->cursory*s->sx].p=0;
}
if (strlen(str)<PSIZE) {
strncpy(s->mem[s->cursorx+s->cursory*s->sx].d,str,sizeof s->mem[s->cursorx+s->cursory*s->sx].d);
s->mem[s->cursorx+s->cursory*s->sx].style=attr;
} else {
char *ts=strdup(str);
if (!ts)
return; // nothing more to do
s->mem[s->cursorx+s->cursory*s->sx].p=ts;
s->mem[s->cursorx+s->cursory*s->sx].style=YAS_STORAGE|attr;
}
s->mem[s->cursory*s->sx].style|=YAS_TOUCHED;
s->cursorx++;
for (i=1;i<width;i++) {
if (s->cursorx<s->sx) {
if (s->mem[s->cursorx+s->cursory*s->sx].style&YAS_STORAGE) {
s->mem[s->cursorx+s->cursory*s->sx].style&=~YAS_STORAGE;
free(s->mem[s->cursorx+s->cursory*s->sx].p);
s->mem[s->cursorx+s->cursory*s->sx].p=0;
}
*s->mem[s->cursorx+s->cursory*s->sx].d=0;
s->mem[s->cursorx+s->cursory*s->sx].style=attr;
}
s->cursorx++;
}
return;
}
if (s->cursorx<0&&s->cursorx+width>=0) { // wide character spanning left bound
int x;
for (x=0;x<s->cursorx+width;x++) { // zap spanned chars
if (s->mem[x+s->cursory*s->sx].style&YAS_STORAGE) {
s->mem[x+s->cursory*s->sx].style&=~YAS_STORAGE;
free(s->mem[x+s->cursory*s->sx].p);
s->mem[x+s->cursory*s->sx].p=0;
}
strncpy(s->mem[x+s->cursory*s->sx].d,"<",sizeof s->mem[x+s->cursory*s->sx].d);
s->mem[x+s->cursory*s->sx].style=attr;
s->mem[s->cursory*s->sx].style|=YAS_TOUCHED;
}
s->cursorx+=width;
return;
}
if (s->cursorx<0||s->cursorx>=s->sx) { // noop for characters out of screen
s->cursorx+=width;
return;
}
if (!width&&s->cursorx==0) // nowhere to append - noop
return;
if (!width&&s->cursorx>0&&s->cursorx<=s->sx) { // combining char, add to previous
int clen;
char *ts;
s->cursorx--;
clen=strlen((s->mem[s->cursorx+s->cursory*s->sx].style&YAS_STORAGE)?s->mem[s->cursorx+s->cursory*s->sx].p:s->mem[s->cursorx+s->cursory*s->sx].d);
if (clen+strlen(str)<PSIZE) { // new fits, so current should fit too and don't have YAS_STORAGE
strncpy(s->mem[s->cursorx+s->cursory*s->sx].d+clen,str,sizeof s->mem[s->cursorx+s->cursory*s->sx].d-clen);
s->mem[s->cursorx+s->cursory*s->sx].style=attr; // as a side effect combining chars set attr for main char
} else {
size_t tslen=clen+strlen(str)+1;
ts=malloc(tslen);
if (!ts) {
s->cursorx++;
return; // nothing more we could do
}
strcpy(ts,(s->mem[s->cursorx+s->cursory*s->sx].style&YAS_STORAGE)?s->mem[s->cursorx+s->cursory*s->sx].p:s->mem[s->cursorx+s->cursory*s->sx].d);
strcat(ts,str);
if (s->mem[s->cursorx+s->cursory*s->sx].style&YAS_STORAGE)
free(s->mem[s->cursorx+s->cursory*s->sx].p);
s->mem[s->cursorx+s->cursory*s->sx].p=ts;
s->mem[s->cursorx+s->cursory*s->sx].style=attr|YAS_STORAGE;
}
s->mem[s->cursory*s->sx].style|=YAS_TOUCHED;
s->cursorx++;
}
if (!width) // noop
return;
if (s->cursorx+width>s->sx) { // wide character spanning right bound
int x;
for (x=s->cursorx;x<s->sx;x++) { // zap spanned chars
if (s->mem[x+s->cursory*s->sx].style&YAS_STORAGE) {
s->mem[x+s->cursory*s->sx].style&=~YAS_STORAGE;
free(s->mem[x+s->cursory*s->sx].p);
s->mem[x+s->cursory*s->sx].p=0;
}
strncpy(s->mem[x+s->cursory*s->sx].d,">",sizeof s->mem[x+s->cursory*s->sx].d);
s->mem[x+s->cursory*s->sx].style=attr;
s->mem[s->cursory*s->sx].style|=YAS_TOUCHED;
}
s->cursorx+=width;
return;
}
return;
} // }}}
inline int yascreen_putsxy(yascreen *s,int x,int y,uint32_t attr,const char *str) { // {{{
yas_u_state st=U_NORM;
char utf[5]; // 4 byte sequence + 1 for terminating 0
size_t i;
if (!s)
return EOF;
if (attr&YAS_INTERNAL)
return EOF;
s->cursorx=x; // set cursor position to whatever is requested
s->cursory=y;
if (x>=s->sx||y>=s->sy) {
s->cursorx=mymax(0,mymin(s->sx-1,s->cursorx)); // fixup position to be within screen
s->cursory=mymax(0,mymin(s->sy-1,s->cursory));
return 1; // somewhat successful print outside bounds
}
for (i=0;i<strlen(str);i++) {
switch (st) {
case U_NORM:
if (str[i]&0x80) {
if ((str[i]&0xc0)==0x80) // unexpected continuation byte
break;
startbyte:
if ((str[i]&0xe0)==0xc0) { // 2 byte seq
utf[0]=str[i];
st=U_L2C1;
break;
}
if ((str[i]&0xf0)==0xe0) { // 3 byte seq
utf[0]=str[i];
st=U_L3C1;
break;
}
if ((str[i]&0xf8)==0xf0) { // 4 byte seq
utf[0]=str[i];
st=U_L4C1;
break;
}
if ((str[i]&0xfc)==0xf8) { // 5 byte seq
//utf[0]=str[i];
st=U_L5C1;
break;
}
if ((str[i]&0xfe)==0xfc) { // 6 byte seq
//utf[0]=str[i];
st=U_L6C1;
break;
}
// pass 0xff and 0xfe - violates rfc
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
st=U_NORM; // in case we come from unexpected start byte
} else {
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
}
break;
case U_L2C1:
if ((str[i]&0xc0)==0x80) { // continuation byte
wchar_t wc;
utf[1]=str[i];
utf[2]=0;
wc=((utf[0]&0x1f)<<6)|(utf[1]&0x3f);
yascreen_putcw(s,attr,utf,wcwidth(wc));
st=U_NORM;
break;
}
if (str[i]&0x80) // start another sequence
goto startbyte;
st=U_NORM; // normal byte kills current sequence and is processed
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
break;
case U_L3C1:
if ((str[i]&0xc0)==0x80) { // continuation byte
utf[1]=str[i];
st=U_L3C2;
break;
}
if (str[i]&0x80) // start another sequence
goto startbyte;
st=U_NORM; // normal byte kills current sequence and is processed
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
break;
case U_L3C2:
if ((str[i]&0xc0)==0x80) { // continuation byte
wchar_t wc;
utf[2]=str[i];
utf[3]=0;
wc=((utf[0]&0x0f)<<12)|((utf[1]&0x3f)<<6)|(utf[2]&0x3f);
yascreen_putcw(s,attr,utf,wcwidth(wc));
st=U_NORM;
break;
}
if (str[i]&0x80) // start another sequence
goto startbyte;
st=U_NORM; // normal byte kills current sequence and is processed
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
break;
case U_L4C1:
if ((str[i]&0xc0)==0x80) { // continuation byte
utf[1]=str[i];
st=U_L4C2;
break;
}
if (str[i]&0x80) // start another sequence
goto startbyte;
st=U_NORM; // normal byte kills current sequence and is processed
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
break;
case U_L4C2:
if ((str[i]&0xc0)==0x80) { // continuation byte
utf[2]=str[i];
st=U_L4C3;
break;
}
if (str[i]&0x80) // start another sequence
goto startbyte;
st=U_NORM; // normal byte kills current sequence and is processed
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
break;
case U_L4C3:
if ((str[i]&0xc0)==0x80) { // continuation byte
wchar_t wc;
utf[3]=str[i];
utf[4]=0;
wc=((utf[0]&0x07)<<18)|((utf[1]&0x3f)<<12)|((utf[2]&0x3f)<<6)|(utf[3]&0x3f);
yascreen_putcw(s,attr,utf,wcwidth(wc));
st=U_NORM;
break;
}
if (str[i]&0x80) // start another sequence
goto startbyte;
st=U_NORM; // normal byte kills current sequence and is processed
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
break;
case U_L5C1:
if ((str[i]&0xc0)==0x80) { // continuation byte
//utf[1]=str[i];
st=U_L5C2;
break;
}
if (str[i]&0x80) // start another sequence
goto startbyte;
st=U_NORM; // normal byte kills current sequence and is processed
utf[0]=str[i];
utf[1]=0;
yascreen_putcw(s,attr,utf,1); // assume width 1
break;
case U_L5C2:
if ((str[i]&0xc0)==0x80) { // continuation byte
//utf[2]=str[i];
st=U_L5C3;