-
Notifications
You must be signed in to change notification settings - Fork 15
/
term.c
1324 lines (1228 loc) · 29.8 KB
/
term.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
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include "conf.h"
#include "fbpad.h"
#define MODE_CURSOR 0x01
#define MODE_WRAP 0x02
#define MODE_ORIGIN 0x04
#define MODE_AUTOCR 0x08
#define ATTR_BOLD 0x10
#define ATTR_ITALIC 0x20
#define ATTR_REV 0x40
#define ATTR_ALL (ATTR_BOLD | ATTR_ITALIC | ATTR_REV)
#define MODE_INSERT 0x100
#define MODE_WRAPREADY 0x200
#define MODE_CLR8 0x400 /* colours 0-7 */
#define CLR_MK(fg, bg) ((fg) | ((bg) << 10))
#define CLR_FG(c) ((c) & 0x3ff)
#define CLR_BG(c) (((c) >> 10) & 0x3ff)
#define CLR_M(c) ((c) & (CLR_B | CLR_I))
#define CLR_B FN_B
#define CLR_I FN_I
#define FG 0x100
#define BG 0x101
#define LIMIT(n, a, b) ((n) < (a) ? (a) : ((n) > (b) ? (b) : (n)))
#define BIT_SET(i, b, val) ((val) ? ((i) | (b)) : ((i) & ~(b)))
#define OFFSET(r, c) ((r) * pad_cols() + (c))
struct term_state {
int row, col;
int fg, bg;
int mode;
};
struct term {
int *screen; /* screen content */
int *hist; /* scrolling history */
int *clr; /* foreground/background color */
int *dirty; /* changed rows in lazy mode */
struct term_state cur, sav; /* terminal saved state */
int fd; /* terminal file descriptor */
int hrow; /* the next history row in hist[] */
int hpos; /* scrolling history; position */
int lazy; /* lazy mode */
int pid; /* pid of the terminal program */
int top, bot; /* terminal scrolling region */
int rows, cols;
int signal; /* send SIGUSR1 and SIGUSR2 */
};
static struct term *term;
static int *screen;
static int *clr;
static int *dirty;
static int lazy;
static int row, col;
static int fg, bg;
static int top, bot;
static int mode;
static int visible;
static int clrfg = FGCOLOR;
static int clrbg = BGCOLOR;
static unsigned int clr16[16] = {
COLOR0, COLOR1, COLOR2, COLOR3, COLOR4, COLOR5, COLOR6, COLOR7,
COLOR8, COLOR9, COLORA, COLORB, COLORC, COLORD, COLORE, COLORF,
};
static int clrmap(int c)
{
int g = (c - 232) * 10 + 8;
if (c < 16)
return clr16[c];
if (c == FG)
return clrfg;
if (c == BG)
return clrbg;
if (c < 232) {
int ri = (c - 16) / 36 ;
int gi = (c - 16) % 36 / 6;
int bi = (c - 16) % 6;
int rc = ri ? (ri * 40 + 55) : 0;
int gc = gi ? (gi * 40 + 55) : 0;
int bc = bi ? (bi * 40 + 55) : 0;
return (rc << 16) | (gc << 8) | bc;
}
return (g << 16) | (g << 8) | g;
}
/* low level drawing and lazy updating */
static int color(void)
{
int c = mode & ATTR_REV ? CLR_MK(bg, fg) : CLR_MK(fg, bg);
if (mode & ATTR_BOLD)
c |= CLR_B;
if (mode & ATTR_ITALIC)
c |= CLR_I;
return c;
}
/* assumes visible && !lazy */
static void _draw_pos(int r, int c, int cursor)
{
int rev = cursor && mode & MODE_CURSOR;
int i = OFFSET(r, c);
int fg = rev ? CLR_BG(clr[i]) : CLR_FG(clr[i]);
int bg = rev ? CLR_FG(clr[i]) : CLR_BG(clr[i]);
pad_put(screen[i], r, c, CLR_M(clr[i]) | clrmap(fg), clrmap(bg));
}
/* assumes visible && !lazy */
static void _draw_row(int r)
{
int cbg, cch; /* current background and character */
int fbg, fsc = -1; /* filling background and start column */
int i;
/* call pad_fill() only once for blank columns with identical backgrounds */
for (i = 0; i < pad_cols(); i++) {
cbg = CLR_BG(clr[OFFSET(r, i)]);
cch = screen[OFFSET(r, i)] ? screen[OFFSET(r, i)] : ' ';
if (fsc >= 0 && (cbg != fbg || cch != ' ')) {
pad_fill(r, r + 1, fsc, i, clrmap(fbg));
fsc = -1;
}
if (cch != ' ') {
_draw_pos(r, i, 0);
} else if (fsc < 0) {
fsc = i;
fbg = cbg;
}
}
pad_fill(r, r + 1, fsc >= 0 ? fsc : pad_cols(), -1, clrmap(cbg));
}
static int candraw(int sr, int er)
{
int i;
if (lazy)
for (i = sr; i < er; i++)
dirty[i] = 1;
return visible && !lazy;
}
static void draw_rows(int sr, int er)
{
int i;
if (candraw(sr, er))
for (i = sr; i < er; i++)
_draw_row(i);
}
static void draw_cols(int r, int sc, int ec)
{
int i;
if (candraw(r, r + 1))
for (i = sc; i < ec; i++)
_draw_pos(r, i, 0);
}
static void draw_char(int ch, int r, int c)
{
int i = OFFSET(r, c);
screen[i] = ch;
clr[i] = color();
if (candraw(r, r + 1))
_draw_pos(r, c, 0);
}
static void draw_cursor(int put)
{
if (candraw(row, row + 1))
_draw_pos(row, col, put);
}
static void lazy_start(void)
{
memset(dirty, 0, pad_rows() * sizeof(*dirty));
lazy = 1;
}
static void lazy_flush(void)
{
int i;
if (!visible || !lazy)
return;
for (i = 0; i < pad_rows(); i++)
if (dirty[i])
_draw_row(i);
if (dirty[row])
_draw_pos(row, col, 1);
lazy = 0;
term->hpos = 0;
}
static void screen_reset(int i, int n)
{
int c;
candraw(i / pad_cols(), (i + n) / pad_cols());
memset(screen + i, 0, n * sizeof(*screen));
for (c = 0; c < n; c++)
clr[i + c] = CLR_MK(fg, bg);
}
static void screen_move(int dst, int src, int n)
{
int srow = (MIN(src, dst) + (n > 0 ? 0 : n)) / pad_cols();
int drow = (MAX(src, dst) + (n > 0 ? n : 0)) / pad_cols();
candraw(srow, drow);
memmove(screen + dst, screen + src, n * sizeof(*screen));
memmove(clr + dst, clr + src, n * sizeof(*clr));
}
/* terminal input buffering */
#define PTYLEN (1 << 16)
static char ptybuf[PTYLEN]; /* always emptied in term_read() */
static int ptylen; /* buffer length */
static int ptycur; /* current offset */
static int waitpty(int us)
{
struct pollfd ufds[1];
ufds[0].fd = term->fd;
ufds[0].events = POLLIN;
return poll(ufds, 1, us) <= 0;
}
static int readpty(void)
{
int nr;
if (ptycur < ptylen)
return (unsigned char) ptybuf[ptycur++];
if (!term->fd)
return -1;
ptylen = 0;
while ((nr = read(term->fd, ptybuf + ptylen, PTYLEN - ptylen)) > 0)
ptylen += nr;
if (!ptylen && errno == EAGAIN && !waitpty(100))
ptylen = read(term->fd, ptybuf, PTYLEN);
ptycur = 1;
return ptylen > 0 ? (unsigned char) ptybuf[0] : -1;
}
/* term interface functions */
static void term_zero(struct term *term)
{
memset(term->screen, 0, pad_rows() * pad_cols() * sizeof(term->screen[0]));
memset(term->hist, 0, NHIST * pad_cols() * sizeof(term->hist[0]));
memset(term->clr, 0, pad_rows() * pad_cols() * sizeof(term->clr[0]));
memset(term->dirty, 0, pad_rows() * sizeof(term->dirty[0]));
memset(&term->cur, 0, sizeof(term->cur));
memset(&term->sav, 0, sizeof(term->sav));
term->fd = 0;
term->hrow = 0;
term->hpos = 0;
term->lazy = 0;
term->pid = 0;
term->top = 0;
term->bot = 0;
term->rows = 0;
term->cols = 0;
term->signal = 0;
}
struct term *term_make(void)
{
struct term *term = malloc(sizeof(*term));
term->screen = malloc(pad_rows() * pad_cols() * sizeof(term->screen[0]));
term->hist = malloc(NHIST * pad_cols() * sizeof(term->hist[0]));
term->clr = malloc(pad_rows() * pad_cols() * sizeof(term->clr[0]));
term->dirty = malloc(pad_rows() * sizeof(term->dirty[0]));
term_zero(term);
return term;
}
void term_free(struct term *term)
{
free(term->screen);
free(term->hist);
free(term->clr);
free(term->dirty);
free(term);
}
int term_fd(struct term *term)
{
return term->fd;
}
void term_send(int c)
{
char b = c;
if (term->fd)
write(term->fd, &b, 1);
}
static void term_sendstr(char *s)
{
if (term->fd)
write(term->fd, s, strlen(s));
}
static void term_blank(void)
{
screen_reset(0, pad_rows() * pad_cols());
if (visible)
pad_fill(0, -1, 0, -1, clrmap(CLR_BG(color())));
}
static void ctlseq(void);
void term_read(void)
{
ctlseq();
while (ptycur < ptylen) {
if (visible && !lazy && ptylen - ptycur > 15)
lazy_start();
ctlseq();
}
lazy_flush();
}
static void term_reset(void)
{
row = col = 0;
top = 0;
bot = pad_rows();
mode = MODE_CURSOR | MODE_WRAP | MODE_CLR8;
fg = FG;
bg = BG;
term_blank();
}
static int _openpty(int *master, int *slave)
{
int unlock = 0;
int ptyno = 0;
char name[20];
if ((*master = open("/dev/ptmx", O_RDWR)) == -1)
return -1;
if (ioctl(*master, TIOCSPTLCK, &unlock) == -1)
return -1;
if (ioctl(*master, TIOCGPTN, &ptyno) == -1)
return -1;
sprintf(name, "/dev/pts/%d", ptyno);
*slave = open(name, O_RDWR | O_NOCTTY);
return 0;
}
static void tio_setsize(int fd)
{
struct winsize winp;
winp.ws_col = pad_cols();
winp.ws_row = pad_rows();
winp.ws_xpixel = 0;
winp.ws_ypixel = 0;
ioctl(fd, TIOCSWINSZ, &winp);
}
static void tio_login(int fd)
{
setsid();
ioctl(fd, TIOCSCTTY, NULL);
tio_setsize(fd);
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
}
static void execvep(char *cmd, char **argv, char **envp)
{
char path[512];
char *p = getenv("PATH");
execve(cmd, argv, envp);
while (*p) {
char *s = path;
while (*p && *p != ':')
*s++ = *p++;
*s++ = '/';
strcpy(s, cmd);
execve(path, argv, envp);
while (*p == ':')
p++;
}
}
static void envcpy(char **d, char **s, int len)
{
int i = 0;
for (i = 0; i < len - 1 && s[i]; i++)
d[i] = s[i];
d[i] = NULL;
}
static void envset(char **d, char *env)
{
int i;
int len = strchr(env, '=') - env;
for (i = 0; d[i]; i++) {
if (memcmp(d[i], env, len))
break;
}
d[i] = env;
}
extern char **environ;
void term_exec(char **args, int swsig)
{
int master, slave;
term_zero(term);
if (_openpty(&master, &slave) == -1)
return;
if ((term->pid = fork()) == -1)
return;
if (!term->pid) {
char *envp[256] = {NULL};
char pgid[32];
snprintf(pgid, sizeof(pgid), "TERM_PGID=%d", getpid());
envcpy(envp, environ, LEN(envp) - 3);
envset(envp, "TERM=" TERM);
envset(envp, pad_fbdev());
if (swsig)
envset(envp, pgid);
tio_login(slave);
close(master);
execvep(args[0], args, envp);
exit(1);
}
close(slave);
term->fd = master;
term->rows = pad_rows();
term->cols = pad_cols();
term->signal = swsig;
fcntl(term->fd, F_SETFD, fcntl(term->fd, F_GETFD) | FD_CLOEXEC);
fcntl(term->fd, F_SETFL, fcntl(term->fd, F_GETFL) | O_NONBLOCK);
term_reset();
memset(term->hist, 0, NHIST * pad_cols() * sizeof(term->hist[0]));
}
static void misc_save(struct term_state *state)
{
state->row = row;
state->col = col;
state->fg = fg;
state->bg = bg;
state->mode = mode;
}
static void misc_load(struct term_state *state)
{
row = state->row;
col = state->col;
fg = state->fg;
bg = state->bg;
mode = state->mode;
}
void term_save(struct term *term)
{
visible = 0;
if (!lazy)
lazy_start();
misc_save(&term->cur);
term->top = top;
term->bot = bot;
term->lazy = lazy;
}
void term_hide(struct term *term)
{
if (term->pid > 0 && term->signal)
kill(-term->pid, SIGUSR1);
}
void term_show(struct term *term)
{
if (term->pid > 0 && term->signal)
kill(-term->pid, SIGUSR2);
}
void term_signal(struct term *term)
{
term->signal = 1;
}
static void resizeupdate(int or, int oc, int nr, int nc)
{
int dr = row >= nr ? row - nr + 1 : 0;
int dst = nc <= oc ? 0 : nr * nc - 1;
while (dst >= 0 && dst < nr * nc) {
int r = dst / nc;
int c = dst % nc;
int src = dr + r < or && c < oc ? (dr + r) * oc + c : -1;
term->screen[dst] = src >= 0 ? term->screen[src] : 0;
term->clr[dst] = src >= 0 ? term->clr[src] : color();
dst = nc <= oc ? dst + 1 : dst - 1;
}
}
/* redraw the screen; if all is zero, update changed lines only */
void term_redraw(int all)
{
if (term->fd) {
if (term->rows != pad_rows() || term->cols != pad_cols()) {
tio_setsize(term->fd);
resizeupdate(term->rows, term->cols, pad_rows(), pad_cols());
if (bot == term->rows)
bot = pad_rows();
term->rows = pad_rows();
term->cols = pad_cols();
top = MIN(top, term->rows);
bot = MIN(bot, term->rows);
row = MIN(row, term->rows - 1);
col = MIN(col, term->cols - 1);
}
if (all) {
pad_fill(pad_rows(), -1, 0, -1, clrbg);
lazy_start();
memset(dirty, 1, pad_rows() * sizeof(*dirty));
}
if (all || !term->hpos)
lazy_flush();
} else {
if (all)
pad_fill(0, -1, 0, -1, 0);
}
}
void term_load(struct term *t, int flags)
{
term = t;
misc_load(&term->cur);
screen = term->screen;
clr = term->clr;
visible = flags;
top = term->top;
bot = term->bot;
lazy = term->lazy;
dirty = term->dirty;
}
void term_end(void)
{
if (term->fd)
close(term->fd);
term_zero(term);
term_load(term, visible);
if (visible)
term_redraw(1);
}
static int writeutf8(char *dst, int c)
{
char *d = dst;
if (c < 0x80) {
*d++ = c > 0 ? c : ' ';
return 1;
}
if (c < 0x800) {
*d++ = 0xc0 | (c >> 6);
*d++ = 0x80 | (c & 0x3f);
return 2;
}
if (c < 0xffff) {
*d++ = 0xe0 | (c >> 12);
*d++ = 0x80 | ((c >> 6) & 0x3f);
*d++ = 0x80 | (c & 0x3f);
return 3;
}
*d++ = 0xf0 | (c >> 18);
*d++ = 0x80 | ((c >> 12) & 0x3f);
*d++ = 0x80 | ((c >> 6) & 0x3f);
*d++ = 0x80 | (c & 0x3f);
return 4;
}
void term_screenshot(char *path)
{
char buf[1 << 11];
int fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
int i, j;
for (i = 0; i < pad_rows(); i++) {
char *s = buf;
for (j = 0; j < pad_cols(); j++)
if (~screen[OFFSET(i, j)] & DWCHAR)
s += writeutf8(s, screen[OFFSET(i, j)]);
*s++ = '\n';
write(fd, buf, s - buf);
}
close(fd);
}
void term_colors(char *path)
{
FILE *fp = fopen(path, "r");
if (fp != NULL) {
int i;
fscanf(fp, "%x %x", &clrfg, &clrbg);
for (i = 0; i < 16; i++)
fscanf(fp, "%x", &clr16[i]);
fclose(fp);
}
}
/* high-level drawing functions */
static void empty_rows(int sr, int er)
{
screen_reset(OFFSET(sr, 0), (er - sr) * pad_cols());
}
static void blank_rows(int sr, int er)
{
empty_rows(sr, er);
draw_rows(sr, er);
draw_cursor(1);
}
#define HISTROW(pos) (term->hist + ((term->hrow + NHIST - (pos)) % NHIST) * pad_cols())
static void scrl_rows(int nr)
{
int i;
for (i = 0; i < nr; i++) {
memcpy(HISTROW(0), screen + i * pad_cols(),
pad_cols() * sizeof(screen[0]));
term->hrow = (term->hrow + 1) % NHIST;
}
}
void term_scrl(int scrl)
{
int i, j;
int hpos = LIMIT(term->hpos + scrl, 0, NHIST);
term->hpos = hpos;
if (!hpos) {
lazy_flush();
return;
}
lazy_start();
memset(dirty, 1, pad_rows() * sizeof(*dirty));
for (i = 0; i < pad_rows(); i++) {
int off = (i - hpos) * pad_cols();
int *_scr = i < hpos ? HISTROW(hpos - i) : term->screen + off;
int *_clr = i < hpos ? NULL : term->clr + off;
for (j = 0; j < pad_cols(); j++) {
int c = _clr ? _clr[j] : CLR_MK(BG, FG);
pad_put(_scr[j], i, j, CLR_M(c) | clrmap(CLR_FG(c)), clrmap(CLR_BG(c)));
}
}
}
static void scroll_screen(int sr, int nr, int n)
{
draw_cursor(0);
if (sr + n == 0)
scrl_rows(sr);
screen_move(OFFSET(sr + n, 0), OFFSET(sr, 0), nr * pad_cols());
if (n > 0)
empty_rows(sr, sr + n);
else
empty_rows(sr + nr + n, sr + nr);
draw_rows(MIN(sr, sr + n), MAX(sr + nr, sr + nr + n));
draw_cursor(1);
}
static void insert_lines(int n)
{
int sr = MAX(top, row);
int nr = bot - row - n;
if (nr > 0)
scroll_screen(sr, nr, n);
}
static void delete_lines(int n)
{
int r = MAX(top, row);
int sr = r + n;
int nr = bot - r - n;
if (nr > 0)
scroll_screen(sr, nr, -n);
}
static int origin(void)
{
return mode & MODE_ORIGIN;
}
static void move_cursor(int r, int c)
{
int t, b;
draw_cursor(0);
t = origin() ? top : 0;
b = origin() ? bot : pad_rows();
row = LIMIT(r, t, b - 1);
col = LIMIT(c, 0, pad_cols() - 1);
draw_cursor(1);
mode = BIT_SET(mode, MODE_WRAPREADY, 0);
}
static void set_region(int t, int b)
{
top = LIMIT(t - 1, 0, pad_rows() - 1);
bot = LIMIT(b ? b : pad_rows(), top + 1, pad_rows());
if (origin())
move_cursor(top, 0);
}
static void setattr(int m)
{
if (!m || (m / 10) == 3)
mode |= MODE_CLR8;
switch (m) {
case 0:
fg = FG;
bg = BG;
mode &= ~ATTR_ALL;
break;
case 1:
mode |= ATTR_BOLD;
break;
case 3:
mode |= ATTR_ITALIC;
break;
case 7:
mode |= ATTR_REV;
break;
case 22:
mode &= ~ATTR_BOLD;
break;
case 23:
mode &= ~ATTR_ITALIC;
break;
case 27:
mode &= ~ATTR_REV;
break;
default:
if ((m / 10) == 3)
fg = m > 37 ? FG : m - 30;
if ((m / 10) == 4)
bg = m > 47 ? BG : m - 40;
if ((m / 10) == 9)
fg = 8 + m - 90;
if ((m / 10) == 10)
bg = 8 + m - 100;
}
}
static void kill_chars(int sc, int ec)
{
int i;
for (i = sc; i < ec; i++)
draw_char(0, row, i);
draw_cursor(1);
}
static void move_chars(int sc, int nc, int n)
{
draw_cursor(0);
screen_move(OFFSET(row, sc + n), OFFSET(row, sc), nc);
if (n > 0)
screen_reset(OFFSET(row, sc), n);
else
screen_reset(OFFSET(row, pad_cols() + n), -n);
draw_cols(row, MIN(sc, sc + n), pad_cols());
draw_cursor(1);
}
static void delete_chars(int n)
{
int sc = col + n;
int nc = pad_cols() - sc;
move_chars(sc, nc, -n);
}
static void insert_chars(int n)
{
int nc = pad_cols() - col - n;
move_chars(col, nc, n);
}
static void advance(int dr, int dc, int scrl)
{
int r = row + dr;
int c = col + dc;
if (dr && r >= bot && scrl) {
int n = bot - r - 1;
int nr = (bot - top) + n;
if (nr > 0)
scroll_screen(top + -n, nr, n);
}
if (dr && r < top && scrl) {
int n = top - r;
int nr = (bot - top) - n;
if (nr > 0)
scroll_screen(top, nr, n);
}
r = dr ? LIMIT(r, top, bot - 1) : r;
c = LIMIT(c, 0, pad_cols() - 1);
move_cursor(r, c);
}
static void insertchar(int c)
{
if (mode & MODE_WRAPREADY)
advance(1, -col, 1);
if (mode & MODE_INSERT)
insert_chars(1);
draw_char(c, row, col);
if (col == pad_cols() - 1)
mode = BIT_SET(mode, MODE_WRAPREADY, 1);
else
advance(0, 1, 1);
}
/* partial vt102 implementation */
static void escseq(void);
static void escseq_cs(void);
static void escseq_g0(void);
static void escseq_g1(void);
static void escseq_g2(void);
static void escseq_g3(void);
static void csiseq(void);
static void csiseq_da(int c);
static void csiseq_dsr(int c);
static void modeseq(int c, int set);
/* comments taken from: http://www.ivarch.com/programs/termvt102.shtml */
static int readutf8(int c)
{
int c1, c2, c3;
if (~c & 0xc0) /* ASCII or invalid */
return c;
c1 = readpty();
if (~c & 0x20)
return ((c & 0x1f) << 6) | (c1 & 0x3f);
c2 = readpty();
if (~c & 0x10)
return ((c & 0x0f) << 12) | ((c1 & 0x3f) << 6) | (c2 & 0x3f);
c3 = readpty();
if (~c & 0x08)
return ((c & 0x07) << 18) | ((c1 & 0x3f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f);
return c;
}
#define unknown(ctl, c)
/* control sequences */
static void ctlseq(void)
{
int c = readpty();
switch (c) {
case 0x09: /* HT horizontal tab to next tab stop */
advance(0, 8 - col % 8, 0);
break;
case 0x0a: /* LF line feed */
case 0x0b: /* VT line feed */
case 0x0c: /* FF line feed */
advance(1, (mode & MODE_AUTOCR) ? -col : 0, 1);
break;
case 0x08: /* BS backspace one column */
advance(0, -1, 0);
break;
case 0x1b: /* ESC start escape sequence */
escseq();
break;
case 0x0d: /* CR carriage return */
advance(0, -col, 0);
break;
case 0x9b: /* CSI equivalent to ESC [ */
csiseq();
break;
case 0x00: /* NUL ignored */
case 0x07: /* BEL beep */
case 0x7f: /* DEL ignored */
break;
case 0x05: /* ENQ trigger answerback message */
case 0x0e: /* SO activate G1 character set & newline */
case 0x0f: /* SI activate G0 character set */
case 0x11: /* XON resume transmission */
case 0x13: /* XOFF stop transmission, ignore characters */
case 0x18: /* CAN interrupt escape sequence */
case 0x1a: /* SUB interrupt escape sequence */
unknown("ctlseq", c);
break;
default:
c = readutf8(c);
if (isdw(c) && col + 1 == pad_cols() && ~mode & MODE_WRAPREADY)
insertchar(0);
if (!iszw(c))
insertchar(c);
if (isdw(c))
insertchar(c | DWCHAR);
break;
}
}
#define ESCM(c) (((c) & 0xf0) == 0x20)
#define ESCF(c) ((c) > 0x30 && (c) < 0x7f)
/* escape sequences */
static void escseq(void)
{
int c = readpty();
while (ESCM(c))
c = readpty();
switch (c) {
case '[': /* CSI control sequence introducer */
csiseq();
break;
case '%': /* CS... escseq_cs table */
escseq_cs();
break;
case '(': /* G0... escseq_g0 table */
escseq_g0();
break;
case ')': /* G1... escseq_g1 table */
escseq_g1();
break;
case '*': /* G2... escseq_g2 table */
escseq_g2();
break;
case '+': /* G3... escseq_g3 table */
escseq_g3();
break;
case '7': /* DECSC save state (position, charset, attributes) */
misc_save(&term->sav);
break;
case '8': /* DECRC restore most recently saved state */
misc_load(&term->sav);
break;
case 'M': /* RI reverse line feed */
advance(-1, 0, 1);
break;
case 'D': /* IND line feed */
advance(1, 0, 1);
break;
case 'E': /* NEL newline */
advance(1, -col, 1);
break;
case 'c': /* RIS reset */
term_reset();
break;
case 'H': /* HTS set tab stop at current column */
case 'Z': /* DECID DEC private ID; return ESC [ ? 6 c (VT102) */
case '#': /* DECALN ("#8") DEC alignment test - fill screen with E's */
case '>': /* DECPNM set numeric keypad mode */
case '=': /* DECPAM set application keypad mode */
case 'N': /* SS2 select G2 charset for next char only */
case 'O': /* SS3 select G3 charset for next char only */
case 'P': /* DCS device control string (ended by ST) */
case 'X': /* SOS start of string */
case '^': /* PM privacy message (ended by ST) */
case '_': /* APC application program command (ended by ST) */
case '\\': /* ST string terminator */
case 'n': /* LS2 invoke G2 charset */
case 'o': /* LS3 invoke G3 charset */
case '|': /* LS3R invoke G3 charset as GR */
case '}': /* LS2R invoke G2 charset as GR */
case '~': /* LS1R invoke G1 charset as GR */
case ']': /* OSC operating system command */
case 'g': /* BEL alternate BEL */
default:
unknown("escseq", c);
break;
}
}
static void escseq_cs(void)
{
int c = readpty();
switch (c) {
case '@': /* CSDFL select default charset (ISO646/8859-1) */
case 'G': /* CSUTF8 select UTF-8 */
case '8': /* CSUTF8 select UTF-8 (obsolete) */
default:
unknown("escseq_cs", c);
break;
}
}