-
Notifications
You must be signed in to change notification settings - Fork 4
/
st.cpp
2280 lines (2023 loc) · 65.8 KB
/
st.cpp
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 "st.h"
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <signal.h>
#include <stdlib.h>
#include <QString>
#include <QApplication>
#if defined(__linux)
#include <pty.h>
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <util.h>
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include <libutil.h>
#endif
SimpleTerminal::SimpleTerminal(QObject *parent) : QObject(parent) {
readBufSize = sizeof(readBuf) / sizeof(readBuf[0]);
tnew(80, 80);
ttynew();
readNotifier = new QSocketNotifier(master, QSocketNotifier::Read);
readNotifier->setEnabled(true);
connect(readNotifier, &QSocketNotifier::activated, this, &SimpleTerminal::ttyread);
// Fix for Zorin OS (error: invalid old space)
// Needed since we only call realloc later
strescseq.buf = (char *) malloc(STR_BUF_SIZ);
}
SimpleTerminal::~SimpleTerminal() {
disconnect(readNotifier);
for (int i = 0; i <= term.row; i++) {
free(term.line[i]);
free(term.alt[i]);
}
free(term.dirty);
free(term.tabs);
free(strescseq.buf);
delete readNotifier;
}
void SimpleTerminal::tnew(int col, int row) {
term = (Term) {.c = {.attr = {.u = defaultCursor, .fg = defaultfg, .bg = defaultbg,}}};
tresize(col, row);
treset();
}
void SimpleTerminal::closePty() {
if (slave > 1) {
::close(slave);
slave = -1;
}
if (master > -1) {
::close(master);
master = -1;
}
emit s_closed();
}
void SimpleTerminal::ttynew() {
master = -1;
slave = -1;
/* seems to work fine on linux, openbsd and freebsd */
if (::openpty(&master, &slave, NULL, NULL, NULL) < 0) {
emit s_error("Could not open new file descriptor.");
return;
}
switch (processId = fork()) {
case -1:
closePty();
emit s_error("Could not fork process.");
return;
break;
case 0:
::close(master);
::setsid(); /* create a new process group */
::dup2(slave, 0);
::dup2(slave, 1);
::dup2(slave, 2);
if (::ioctl(slave, TIOCSCTTY, NULL) < 0) {
closePty();
emit s_error("Error setting the controlling terminal.");
return;
}
if (slave > 2)
::close(slave);
#ifdef __OpenBSD__
if (::pledge("stdio getpw proc exec", NULL) == -1){
closePty();
emit s_error("Error on pledge.");
return;
}
#endif
execsh();
break;
default:
#ifdef __OpenBSD__
if (::pledge("stdio rpath tty proc", NULL) == -1){
closePty();
emit s_error("Error on pledge.");
return;
}
#endif
::close(slave);
break;
}
}
void SimpleTerminal::execsh() {
const struct passwd *pw;
errno = 0;
if ((pw = ::getpwuid(getuid())) == NULL) {
if (errno) {
closePty();
emit s_error("Error on getpwuid.");
return;
} else {
closePty();
emit s_error("Could not retrive user identity.");
return;
}
}
QByteArray shell = qgetenv("SHELL");
if (shell.size() == 0) {
shell = (pw->pw_shell[0]) ? pw->pw_shell : "/bin/sh";
}
::unsetenv("COLUMNS");
::unsetenv("LINES");
::unsetenv("TERMCAP");
::setenv("LOGNAME", pw->pw_name, 1);
::setenv("USER", pw->pw_name, 1);
::setenv("SHELL", shell, 1);
::setenv("HOME", pw->pw_dir, 1);
// TODO figure out a way to use tic command with custom term info file
::setenv("TERM", "xterm-256color", 1);
char *args[] = {shell.data(), NULL, NULL};
::execvp(shell.constData(), args);
}
size_t SimpleTerminal::ttyread() {
int ret, written;
/* append read bytes to unprocessed bytes */
ret = ::read(master, readBuf + readBufPos, readBufSize - readBufPos);
switch (ret) {
case 0:
return 0;
case -1:
closePty();
emit s_error("Could not read from shell.");
return 0;
default:
readBufPos += ret;
written = twrite(readBuf, readBufPos, 0);
readBufPos -= written;
/* keep any incomplete UTF-8 byte sequence for the next call */
if (readBufPos > 0) {
::memmove(readBuf, readBuf + written, readBufPos);
}
emit s_updateView(&term);
return ret;
}
}
void SimpleTerminal::tresize(int col, int row) {
int i, j;
int minrow = MIN(row, term.row);
int mincol = MIN(col, term.col);
int *bp;
TCursor c;
if (col < 1 || row < 1) {
emit s_error("tresize: error resizing to x: " + QString::number(col) + ", y: " + QString::number(row));
return;
}
/*
* slide screen to keep cursor where we expect it -
* tscrollup would work here, but we can optimize to
* memmove because we're freeing the earlier lines
*/
for (i = 0; i <= term.c.y - row; i++) {
free(term.line[i]);
free(term.alt[i]);
}
/* ensure that both src and dst are not NULL */
if (i > 0) {
memmove(term.line, term.line + i, row * sizeof(Line));
memmove(term.alt, term.alt + i, row * sizeof(Line));
}
for (i += row; i < term.row; i++) {
free(term.line[i]);
free(term.alt[i]);
}
/* resize to new height */
term.line = (Line *) realloc(term.line, row * sizeof(Line));
term.alt = (Line *) realloc(term.alt, row * sizeof(Line));
term.dirty = (int *) realloc(term.dirty, row * sizeof(*term.dirty));
term.tabs = (int *) realloc(term.tabs, col * sizeof(*term.tabs));
if (term.line == NULL || term.alt == NULL || term.dirty == NULL || term.tabs == NULL) {
emit s_error("Error on resize");
return;
}
for (i = 0; i < HISTSIZE; i++) {
term.hist[i] = (Line) ::realloc(term.hist[i], col * sizeof(Glyph));
if (term.hist[i] == NULL) {
emit s_error("Error on resize");
return;
}
for (j = mincol; j < col; j++) {
term.hist[i][j] = term.c.attr;
term.hist[i][j].u = ' ';
}
}
/* resize each row to new width, zero-pad if needed */
for (i = 0; i < minrow; i++) {
term.line[i] = (Glyph_ *) realloc(term.line[i], col * sizeof(Glyph));
term.alt[i] = (Glyph_ *) realloc(term.alt[i], col * sizeof(Glyph));
if (term.line[i] == NULL || term.alt[i] == NULL) {
emit s_error("Error on resize");
return;
}
}
/* allocate any new rows */
for (/* i = minrow */; i < row; i++) {
term.line[i] = (Glyph_ *) malloc(col * sizeof(Glyph));
term.alt[i] = (Glyph_ *) malloc(col * sizeof(Glyph));
if (term.line[i] == NULL || term.alt[i] == NULL) {
emit s_error("Error on resize");
return;
}
}
if (col > term.col) {
bp = term.tabs + term.col;
memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
while (--bp > term.tabs && !*bp)
/* nothing */ ;
for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
*bp = 1;
}
/* update terminal size */
term.col = col;
term.row = row;
/* reset scrolling region */
tsetscroll(0, row - 1);
/* make use of the LIMIT in tmoveto */
tmoveto(term.c.x, term.c.y);
/* Clearing both screens (it makes dirty all lines) */
c = term.c;
for (i = 0; i < 2; i++) {
if (mincol < col && 0 < minrow) {
tclearregion(mincol, 0, col - 1, minrow - 1);
}
if (0 < col && minrow < row) {
tclearregion(0, minrow, col - 1, row - 1);
}
tswapscreen();
tcursor(CURSOR_LOAD);
}
term.c = c;
}
void SimpleTerminal::tputc(Rune u) {
char c[UTF_SIZ];
int control;
int width, len;
Glyph *gp;
control = ISCONTROL(u);
if (u < 127 || !IS_SET(term.mode, MODE_UTF8)) {
c[0] = u;
width = len = 1;
} else {
len = utf8encode(u, c);
if (!control && (width = wcwidth(u)) == -1)
width = 1;
}
if (IS_SET(term.mode, MODE_PRINT)) {
tprinter(c, len);
}
/*
* STR sequence must be checked before anything else
* because it uses all following characters until it
* receives a ESC, a SUB, a ST or any other C1 control
* character.
*/
if (term.esc & ESC_STR) {
if (u == '\a' || u == 030 || u == 032 || u == 033 ||
ISCONTROLC1(u)) {
term.esc &= ~(ESC_START | ESC_STR);
term.esc |= ESC_STR_END;
goto check_control_code;
}
if (strescseq.len + len >= strescseq.siz) {
/*
* Here is a bug in terminals. If the user never sends
* some code to stop the str or esc command, then st
* will stop responding. But this is better than
* silently failing with unknown characters. At least
* then users will report back.
*
* In the case users ever get fixed, here is the code:
*/
/*
* term.esc = 0;
* strhandle();
*/
if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
return;
strescseq.siz *= 2;
strescseq.buf = (char *) realloc(strescseq.buf, strescseq.siz);
if (strescseq.buf == NULL) {
emit s_error("Could not realloc buffer.");
return;
}
}
memmove(&strescseq.buf[strescseq.len], c, len);
strescseq.len += len;
return;
}
check_control_code:
/*
* Actions of control codes must be performed as soon they arrive
* because they can be embedded inside a control sequence, and
* they must not cause conflicts with sequences.
*/
if (control) {
/* in UTF-8 mode ignore handling C1 control characters */
if (IS_SET(term.mode, MODE_UTF8) && ISCONTROLC1(u))
return;
tcontrolcode(u);
/*
* control codes are not shown ever
*/
if (!term.esc)
term.lastc = 0;
return;
} else if (term.esc & ESC_START) {
if (term.esc & ESC_CSI) {
csiescseq.buf[csiescseq.len++] = u;
if (BETWEEN(u, 0x40, 0x7E)
|| csiescseq.len >= \
sizeof(csiescseq.buf) - 1) {
term.esc = 0;
csiparse();
csihandle();
}
return;
} else if (term.esc & ESC_UTF8) {
tdefutf8(u);
} else if (term.esc & ESC_ALTCHARSET) {
tdeftran(u);
} else if (term.esc & ESC_TEST) {
tdectest(u);
} else {
if (!eschandle(u))
return;
/* sequence already finished */
}
term.esc = 0;
/*
* All characters which form part of a sequence are not
* printed
*/
return;
}
if (selected(term.c.x, term.c.y))
selclear();
gp = &term.line[term.c.y][term.c.x];
if (IS_SET(term.mode, MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
gp->mode |= ATTR_WRAP;
tnewline(1);
gp = &term.line[term.c.y][term.c.x];
}
if (IS_SET(term.mode, MODE_INSERT) && term.c.x + width < term.col) {
memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
gp->mode &= ~ATTR_WIDE;
}
if (term.c.x + width > term.col) {
if (IS_SET(term.mode, MODE_WRAP))
tnewline(1);
else
tmoveto(term.col - width, term.c.y);
gp = &term.line[term.c.y][term.c.x];
}
tsetchar(u, &term.c.attr, term.c.x, term.c.y);
term.lastc = u;
if (width == 2) {
gp->mode |= ATTR_WIDE;
if (term.c.x + 1 < term.col) {
if (gp[1].mode == ATTR_WIDE && term.c.x + 2 < term.col) {
gp[2].u = ' ';
gp[2].mode &= ~ATTR_WDUMMY;
}
gp[1].u = '\0';
gp[1].mode = ATTR_WDUMMY;
}
}
if (term.c.x + width < term.col) {
tmoveto(term.c.x + width, term.c.y);
} else {
term.c.state |= CURSOR_WRAPNEXT;
}
}
int SimpleTerminal::twrite(const char *buf, int size, int show_ctrl) {
int charsize;
Rune u;
int n;
for (n = 0; n < size; n += charsize) {
if (IS_SET(term.mode, MODE_UTF8)) {
/* process a complete utf8 char */
charsize = utf8decode(buf + n, &u, size - n);
if (charsize == 0)
break;
} else {
u = buf[n] & 0xFF;
charsize = 1;
}
if (show_ctrl && ISCONTROL(u)) {
if (u & 0x80) {
u &= 0x7f;
tputc('^');
tputc('[');
} else if (u != '\n' && u != '\r' && u != '\t') {
u ^= 0x40;
tputc('^');
}
}
tputc(u);
}
return n;
}
void SimpleTerminal::ttywrite(const char *s, size_t n, int may_echo) {
const char *next;
kscrolldown(term.scr);
if (may_echo && IS_SET(term.mode, MODE_ECHO))
twrite(s, n, 1);
if (!IS_SET(term.mode, MODE_CRLF)) {
ttywriteraw(s, n);
return;
}
/* This is similar to how the kernel handles ONLCR for ttys */
while (n > 0) {
if (*s == '\r') {
next = s + 1;
ttywriteraw("\r\n", 2);
} else {
next = (char *) memchr(s, '\r', n);
DEFAULT(next, s + n);
ttywriteraw(s, next - s);
}
n -= next - s;
s = next;
}
}
void SimpleTerminal::ttywriteraw(const char *s, size_t n) {
fd_set wfd, rfd;
ssize_t r;
size_t lim = 256;
/*
* Remember that we are using a pty, which might be a modem line.
* Writing too much will clog the line. That's why we are doing this
* dance.
* FIXME: Migrate the world to Plan 9.
*/
while (n > 0) {
FD_ZERO(&wfd);
FD_ZERO(&rfd);
FD_SET(master, &wfd);
FD_SET(master, &rfd);
/* Check if we can write. */
if (pselect(master + 1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
if (errno == EINTR) {
continue;
}
emit s_error("Pselect failed in ttywriteraw");
return;
}
if (FD_ISSET(master, &wfd)) {
/*
* Only write the bytes written by ttywrite() or the
* default of 256. This seems to be a reasonable value
* for a serial line. Bigger values might clog the I/O.
*/
if ((r = write(master, s, (n < lim) ? n : lim)) < 0) {
emit s_error("Error on write in ttywriteraw.");
return;
}
if (r < n) {
/*
* We weren't able to write out everything.
* This means the buffer is getting full
* again. Empty it.
*/
if (n < lim)
lim = ttyread();
n -= r;
s += r;
} else {
/* All bytes have been written. */
break;
}
}
if (FD_ISSET(master, &rfd))
lim = ttyread();
}
return;
}
size_t SimpleTerminal::utf8decode(const char *c, Rune *u, size_t clen) {
size_t i, j, len, type;
Rune udecoded;
*u = UTF_INVALID;
if (!clen)
return 0;
udecoded = utf8decodebyte(c[0], &len);
if (!BETWEEN(len, 1, UTF_SIZ))
return 1;
for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
if (type != 0)
return j;
}
if (j < len)
return 0;
*u = udecoded;
utf8validate(u, len);
return len;
}
Rune SimpleTerminal::utf8decodebyte(char c, size_t *i) {
for (*i = 0; *i < LEN(utfmask);
++(*i))
if (((uchar) c & utfmask[*i]) == utfbyte[*i])
return (uchar) c & ~utfmask[*i];
return 0;
}
size_t SimpleTerminal::utf8encode(Rune u, char *c) {
size_t len, i;
len = utf8validate(&u, 0);
if (len > UTF_SIZ)
return 0;
for (i = len - 1; i != 0; --i) {
c[i] = utf8encodebyte(u, 0);
u >>= 6;
}
c[0] = utf8encodebyte(u, len);
return len;
}
char SimpleTerminal::utf8encodebyte(Rune u, size_t i) {
return utfbyte[i] | (u & ~utfmask[i]);
}
size_t SimpleTerminal::utf8validate(Rune *u, size_t i) {
if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
*u = UTF_INVALID;
for (i = 1; *u > utfmax[i]; ++i);
return i;
}
void SimpleTerminal::tcontrolcode(uchar ascii) {
switch (ascii) {
case '\t': /* HT */
tputtab(1);
return;
case '\b': /* BS */
tmoveto(term.c.x - 1, term.c.y);
return;
case '\r': /* CR */
tmoveto(0, term.c.y);
return;
case '\f': /* LF */
case '\v': /* VT */
case '\n': /* LF */
/* go to first col if the mode is set */
tnewline(IS_SET(term.mode, MODE_CRLF));
return;
case '\a': /* BEL */
if (term.esc & ESC_STR_END) {
/* backwards compatibility to xterm */
strhandle();
} else {
bell();
}
break;
case '\033': /* ESC */
csireset();
term.esc &= ~(ESC_CSI | ESC_ALTCHARSET | ESC_TEST);
term.esc |= ESC_START;
return;
case '\016': /* SO (LS1 -- Locking shift 1) */
case '\017': /* SI (LS0 -- Locking shift 0) */
term.charset = 1 - (ascii - '\016');
return;
case '\032': /* SUB */
tsetchar('?', &term.c.attr, term.c.x, term.c.y);
/* FALLTHROUGH */
case '\030': /* CAN */
csireset();
break;
case '\005': /* ENQ (IGNORED) */
case '\000': /* NUL (IGNORED) */
case '\021': /* XON (IGNORED) */
case '\023': /* XOFF (IGNORED) */
case 0177: /* DEL (IGNORED) */
return;
case 0x80: /* TODO: PAD */
case 0x81: /* TODO: HOP */
case 0x82: /* TODO: BPH */
case 0x83: /* TODO: NBH */
case 0x84: /* TODO: IND */
break;
case 0x85: /* NEL -- Next line */
tnewline(1); /* always go to first col */
break;
case 0x86: /* TODO: SSA */
case 0x87: /* TODO: ESA */
break;
case 0x88: /* HTS -- Horizontal tab stop */
term.tabs[term.c.x] = 1;
break;
case 0x89: /* TODO: HTJ */
case 0x8a: /* TODO: VTS */
case 0x8b: /* TODO: PLD */
case 0x8c: /* TODO: PLU */
case 0x8d: /* TODO: RI */
case 0x8e: /* TODO: SS2 */
case 0x8f: /* TODO: SS3 */
case 0x91: /* TODO: PU1 */
case 0x92: /* TODO: PU2 */
case 0x93: /* TODO: STS */
case 0x94: /* TODO: CCH */
case 0x95: /* TODO: MW */
case 0x96: /* TODO: SPA */
case 0x97: /* TODO: EPA */
case 0x98: /* TODO: SOS */
case 0x99: /* TODO: SGCI */
break;
case 0x9a: /* DECID -- Identify Terminal */
ttywrite(vtiden, strlen(vtiden), 0);
break;
case 0x9b: /* TODO: CSI */
case 0x9c: /* TODO: ST */
break;
case 0x90: /* DCS -- Device Control String */
case 0x9d: /* OSC -- Operating System Command */
case 0x9e: /* PM -- Privacy Message */
case 0x9f: /* APC -- Application Program Command */
tstrsequence(ascii);
return;
}
/* only CAN, SUB, \a and C1 chars interrupt a sequence */
term.esc &= ~(ESC_STR_END | ESC_STR);
}
void SimpleTerminal::tputtab(int n) {
uint x = term.c.x;
if (n > 0) {
while (x < term.col && n--)
for (++x; x < term.col && !term.tabs[x]; ++x)
/* nothing */ ;
} else if (n < 0) {
while (x > 0 && n++)
for (--x; x > 0 && !term.tabs[x]; --x)
/* nothing */ ;
}
term.c.x = LIMIT(x, 0, term.col - 1);
}
void SimpleTerminal::tmoveto(int x, int y) {
int miny, maxy;
if (term.c.state & CURSOR_ORIGIN) {
miny = term.top;
maxy = term.bot;
} else {
miny = 0;
maxy = term.row - 1;
}
term.c.state &= ~CURSOR_WRAPNEXT;
term.c.x = LIMIT(x, 0, term.col - 1);
term.c.y = LIMIT(y, miny, maxy);
}
void SimpleTerminal::tstrsequence(uchar c) {
switch (c) {
case 0x90: /* DCS -- Device Control String */
c = 'P';
break;
case 0x9f: /* APC -- Application Program Command */
c = '_';
break;
case 0x9e: /* PM -- Privacy Message */
c = '^';
break;
case 0x9d: /* OSC -- Operating System Command */
c = ']';
break;
}
strreset();
strescseq.type = c;
term.esc |= ESC_STR;
}
void SimpleTerminal::strreset(void) {
char *buf = (char *) realloc(strescseq.buf, STR_BUF_SIZ);
if (buf == NULL) {
emit s_error("Error while realloc in strreset.");
return;
}
strescseq = (STREscape) {
.buf = buf,
.siz = STR_BUF_SIZ,
};
}
void SimpleTerminal::tnewline(int first_col) {
int y = term.c.y;
if (y == term.bot) {
tscrollup(term.top, 1, 1);
} else {
y++;
}
tmoveto(first_col ? 0 : term.c.x, y);
}
void SimpleTerminal::tclearregion(int x1, int y1, int x2, int y2) {
int x, y, temp;
Glyph *gp;
if (x1 > x2)
temp = x1, x1 = x2, x2 = temp;
if (y1 > y2)
temp = y1, y1 = y2, y2 = temp;
LIMIT(x1, 0, term.col - 1);
LIMIT(x2, 0, term.col - 1);
LIMIT(y1, 0, term.row - 1);
LIMIT(y2, 0, term.row - 1);
for (y = y1; y <= y2; y++) {
term.dirty[y] = 1;
for (x = x1; x <= x2; x++) {
gp = &term.line[y][x];
if (selected(x, y))
selclear();
gp->fg = term.c.attr.fg;
gp->bg = term.c.attr.bg;
gp->mode = 0;
gp->u = ' ';
}
}
}
int SimpleTerminal::selected(int x, int y) {
if (sel.mode == SEL_EMPTY || sel.ob.x == -1 ||
sel.alt != IS_SET(term.mode, MODE_ALTSCREEN))
return 0;
if (sel.type == SEL_RECTANGULAR)
return BETWEEN(y, sel.nb.y, sel.ne.y)
&& BETWEEN(x, sel.nb.x, sel.ne.x);
return BETWEEN(y, sel.nb.y, sel.ne.y)
&& (y != sel.nb.y || x >= sel.nb.x)
&& (y != sel.ne.y || x <= sel.ne.x);
}
void SimpleTerminal::selclear(void) {
if (sel.ob.x == -1)
return;
sel.mode = SEL_IDLE;
sel.ob.x = -1;
tsetdirt(sel.nb.y, sel.ne.y);
}
void SimpleTerminal::selscroll(int orig, int n) {
if (sel.ob.x == -1 || sel.alt != IS_SET(term.mode, MODE_ALTSCREEN))
return;
sel.ob.y += n;
sel.oe.y += n;
selnormalize();
}
void SimpleTerminal::selnormalize(void) {
int i;
if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
} else {
sel.nb.x = MIN(sel.ob.x, sel.oe.x);
sel.ne.x = MAX(sel.ob.x, sel.oe.x);
}
sel.nb.y = MIN(sel.ob.y, sel.oe.y);
sel.ne.y = MAX(sel.ob.y, sel.oe.y);
selsnap(&sel.nb.x, &sel.nb.y, -1);
selsnap(&sel.ne.x, &sel.ne.y, +1);
/* expand selection over line breaks */
if (sel.type == SEL_RECTANGULAR)
return;
i = tlinelen(sel.nb.y);
if (i < sel.nb.x)
sel.nb.x = i;
if (tlinelen(sel.ne.y) <= sel.ne.x)
sel.ne.x = term.col - 1;
}
void SimpleTerminal::tsetdirt(int top, int bot) {
int i;
LIMIT(top, 0, term.row - 1);
LIMIT(bot, 0, term.row - 1);
for (i = top; i <= bot; i++)
term.dirty[i] = 1;
}
void SimpleTerminal::strhandle(void) {
char *p = NULL, *dec;
int j, narg, par;
const struct { unsigned int idx; char *str; } osc_table[] = {
{ defaultfg, "foreground" },
{ defaultbg, "background" },
{ defaultcs, "cursor" }
};
term.esc &= ~(ESC_STR_END | ESC_STR);
strparse();
par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
switch (strescseq.type) {
case ']': /* OSC -- Operating System Command */
switch (par) {
// Florian Plesker: removed set title since it is a widget
case 0:
case 1:
case 2:
return;
case 52:
if (narg > 2 && allowwindowops) {
dec = base64dec(strescseq.args[2]);
if (dec) {
xsetsel(dec);
xclipcopy();
} else {
fprintf(stderr, "erresc: invalid base64\n");
}
}
return;
case 10:
case 11:
case 12:
if (narg < 2)
break;
p = strescseq.args[1];
if ((j = par - 10) < 0 || j >= LEN(osc_table))
break; /* shouldn't be possible */
if (!strcmp(p, "?")) {
osc_color_response(par, osc_table[j].idx, 0);
} else if (xsetcolorname(osc_table[j].idx, p)) {
fprintf(stderr, "erresc: invalid %s color: %s\n",
osc_table[j].str, p);
} else {
tfulldirt();
}
return;
case 4: /* color set */
if (narg < 3)
break;
p = strescseq.args[2];
/* FALLTHROUGH */
case 104: /* color reset */
j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
if (p && !strcmp(p, "?")) {
osc_color_response(j, 0, 1);
} else if (xsetcolorname(j, p)) {
if (par == 104 && narg <= 1) {
xloadcols();
return; /* color reset without parameter */
}
fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
j, p ? p : "(null)");
} else {
/*
* TODO if defaultbg color is changed, borders
* are dirty
*/
tfulldirt();
}
return;
}
break;
case 'k': /* old title set compatibility */
return;
case 'P': /* DCS -- Device Control String */
case '_': /* APC -- Application Program Command */
case '^': /* PM -- Privacy Message */
return;
}
fprintf(stderr, "erresc: unknown str ");
strdump();
}
void SimpleTerminal::strparse(void) {
int c;
char *p = strescseq.buf;
strescseq.narg = 0;
strescseq.buf[strescseq.len] = '\0';
if (*p == '\0')
return;
while (strescseq.narg < STR_ARG_SIZ) {
strescseq.args[strescseq.narg++] = p;
while ((c = *p) != ';' && c != '\0')
++p;
if (c == '\0')
return;
*p++ = '\0';
}
}