-
Notifications
You must be signed in to change notification settings - Fork 2
/
twterm.c
2043 lines (1971 loc) · 50.2 KB
/
twterm.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
/* Emulator for DEC VT100 series terminal for MiNT.
* Copyright (C) 2001, Guido Flohr <guido@freemint.de>,
* all rights reserved.
*
* detailed descriptions of vt100-alike control sequences
* can be found at:
* https://vt100.net/docs/vt100-ug/chapter3.html
* https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
*/
#include <mintbind.h>
#include <support.h>
#include <string.h>
#include "textwin.h"
#include "vt.h"
#include "window.h"
#include "ansicol.h"
#include "console.h"
/* Local prototypes. */
static void escy_putch (TEXTWIN* tw, unsigned int c);
static void vt100_putesc (TEXTWIN* tw, unsigned int c);
static void clearalltabs (TEXTWIN* tw);
static void fill_window (TEXTWIN* tw, unsigned int c);
#undef DUMP
#ifdef DUMP
static int dump_pos = 1;
static void dump(char c)
{
if (dump_pos % 80 == 0) {
puts("");
dump_pos = 1;
}
printf("%c", c);
++dump_pos;
}
#else
#define dump(c)
#endif
/* Move cursor in textwin TW from position (X|Y)
* to next tab position. */
static void gototab(TEXTWIN* tw, int x, int y)
{
TABLIST *tab;
if (tw->tabs)
{
for (tab = tw->tabs; tab != NULL && tab->tabpos <= x; tab = tab->nexttab)
;
if (tab != NULL && tab->tabpos > x)
x = tab->tabpos;
else if (tab == NULL)
x = ((x + 8) / 8) * 8;
gotoxy (tw, x, y - RELOFFSET (tw));
}
}
/* Insert a space at position (X|Y). Scroll rest of the line
* to the right. Discard characters pushed out of the screen. */
static void insert_char(TEXTWIN* tw, int x, int y)
{
int i;
unsigned char *twdata;
unsigned long *twcflag;
if (x < 0 || x >= tw->maxx || y < 0 || y >= tw->maxy)
return;
twdata = tw->cdata[y];
twcflag = tw->cflags[y];
memmove (twdata + x + 1, twdata + x, (tw->maxx - x - 1) * sizeof(tw->cdata[0][0]));
for (i = NCOLS (tw) - 1; i > x; --i)
{
twcflag[i] = twcflag[i - 1] | CDIRTY;
}
twdata[x] = ' ';
twcflag[x] = CDIRTY | (tw->curr_cattr & (CBGCOL | CFGCOL));
tw->dirty[y] |= SOMEDIRTY;
tw->do_wrap = 0;
}
/* Store character C in the internal capture buffer. If
* C is a carriage return, the buffer is closed and passed
* to the callback function.
*/
static void
capture (TEXTWIN* tw, unsigned int c)
{
int i = tw->captsiz;
if (c == '\r')
c = 0;
if (i < CAPTURESIZE || c == 0)
{
tw->captbuf[i++] = c;
tw->captsiz = i;
if (c == 0) {
tw->output = vt100_putch;
(*tw->callback) (tw);
}
}
}
static void fgcol_putch (TEXTWIN *v, unsigned int c)
{
v->curr_cattr = (v->curr_cattr & ~CFGCOL) | ((c & 0x0f) << 4);
v->output = vt100_putch;
}
static void bgcol_putch (TEXTWIN *v, unsigned int c)
{
v->curr_cattr = (v->curr_cattr & ~CBGCOL) | (c & 0x0f);
v->output = vt100_putch;
}
static void ansi_fgcol_putch (TEXTWIN *v, unsigned int c)
{
set_ansi_fg_color (v, c);
v->output = vt100_putch;
}
static void ansi_bgcol_putch (TEXTWIN *v, unsigned int c)
{
set_ansi_bg_color (v, c);
v->output = vt100_putch;
}
/* Set special effects. FIXME: A separate function is waste
* of time for this. */
static void
seffect_putch (TEXTWIN* tw, unsigned int c)
{
tw->curr_cattr |= (c << 8) & (CEFFECTS | CINVERSE);
tw->output = vt100_putch;
}
/* clear special effects */
static void
ceffect_putch(TEXTWIN* tw, unsigned int c)
{
tw->curr_cattr &= ~((c << 8) & (CEFFECTS | CINVERSE));
tw->output = vt100_putch;
}
/* Add character C to buffer for extended escapes:
* if we have an escape sequence > 127 characters, character 127 will be
* overwritten with each character in turn
*/
static void
pushescbuf (unsigned char* escbuf, unsigned int c)
{
int i = 0;
while (i < (ESCBUFSIZE - 2) && escbuf[i] != '\0')
i++;
/* Two semi-colons in a row are the same as '0;'. */
if (c == ';' && i >= 1 && escbuf[i - 1] == ';') {
pushescbuf (escbuf, '0');
pushescbuf (escbuf, c);
return;
}
escbuf[i++] = c;
escbuf[i] = '\0';
}
/* Fill window TW with character C. */
static void
fill_window (TEXTWIN* tw, unsigned int c)
{
int row;
unsigned long new_attribute = tw->curr_cattr &
(CBGCOL | CFGCOL);
int cols = NCOLS (tw);
for (row = tw->miny; row < tw->maxy; ++row) {
memset (tw->cdata[row], c, cols);
memulset (tw->cflags[row], new_attribute, cols);
}
memset (tw->dirty + tw->miny, ALLDIRTY, NROWS (tw));
}
/* Return int (n) from [?n, #n, }n or (n esc sequence:
* Removes first n; from buf (to handle multiple n;n;...;n)
*/
static int
popescbuf (TEXTWIN* tw, unsigned char* escbuf)
{
int n = 0;
char s[ESCBUFSIZE];
char *sp = s;
unsigned char *ep1, *ep2;
(void) tw;
ep2 = ep1 = escbuf;
while (*ep1 != '\0' && *ep1 != ';')
*sp++ = *ep1++;
*sp = '\0';
while (*ep1++ != '\0')
*ep2++ = *ep1;
*ep2 = '\0';
if (s[0] != '\0') {
n = atoi(s);
return n;
}
return -1;
}
/* Discard all keys but l. */
static void
keylockedl (TEXTWIN* tw, unsigned int c)
{
switch (c) {
case 'l': /* Unlock keyboard */
tw->output = vt100_putch;
break;
}
}
/* Discard all characters but '2l'. */
static void
keylocked2 (TEXTWIN* tw, unsigned int c)
{
switch (c) {
case '2':
tw->output = keylockedl;
break;
}
}
/* Discard all characters but ESC2l. */
static void
keylocked (TEXTWIN* tw, unsigned int c)
{
switch (c) {
case '\033': /* ESC */
tw->output = keylocked2;
break;
}
}
/* Handle control sequence ESC [?...n. */
static void vt100_esc_mode(TEXTWIN* tw, unsigned int c)
{
int count, ei;
SYSLOG((LOG_ERR, "vt100_esc_mode: %c (%u)", c, c));
dump(c);
switch (c) {
case '\000':
case '\001':
case '\002':
case '\003':
case '\004':
case '\006':
case '\020':
case '\021':
case '\022':
case '\023':
case '\024':
case '\025':
case '\026':
case '\027':
case '\030':
case '\031':
case '\032':
case '\034':
case '\035':
case '\036':
case '\037':
/* ignore */
return;
case '\005':
/* ENQ */
return;
case '\010': /* ANSI spec - ^H in middle of ESC (yuck!) */
cub1 (tw);
return;
case '\012': /* ANSI spec - ^J in middle of ESC (yuck!) */
case '\013': /* ANSI spec - ^K in middle of ESC (yuck!) */
case '\014': /* ANSI spec - ^L in middle of ESC (yuck!) */
cud1 (tw);
return;
case '\015': /* ANSI spec - ^M in middle of ESC (yuck!) */
carriage_return(tw);
return;
case '\016':
/* SO */
return;
case '\017':
/* SO */
return;
case '\033': /* ESC - restart sequence */
tw->output = vt100_putesc;
tw->escbuf[0] = '\0';
return;
case 'J': /* Erase in Display (DECSED). */
case 'K': /* Erase in Line (DECSEL). */
case 'S': /* Set or request graphics attribute, xterm */
/* Not yet implemented. */
tw->escbuf[0] = '\0';
break;
case 'c': /* Extended private modes. */
count = 0;
do {
ei = popescbuf (tw, tw->escbuf);
if (count == 0 && ei == -1)
ei = 0;
switch (ei) {
case 0:
tw->curr_tflags &= ~TCURS_VVIS;
break;
case 8:
tw->curr_tflags |= (TCURS_ON | TCURS_VVIS);
break;
}
++count;
} while (ei >= 0);
break;
case 'h': /* mode set */
count = 0;
do {
ei = popescbuf(tw, tw->escbuf);
if (count == 0 && ei == -1)
ei = 0;
switch (ei) {
case 1: /* Application Cursor Keys (DECCKM). */
tw->curs_mode = CURSOR_TRANSMIT;
break;
case 2: /* Designate USASCII for character
sets G0-G3 (DECANM), and set VT100
mode. */
memcpy (tw->gsets, "BBBB",
sizeof tw->gsets);
break;
case 3: /* 132 column mode (DECCOLM). */
/* The original VT100
also did a clear/home on that. */
if (tw->curr_tflags & TDECCOLM) {
resize_textwin (tw, 132,
NROWS (tw),
SCROLLBACK (tw));
clear (tw);
gotoxy (tw, 0, 0);
}
break;
case 4: /* Smooth (slow) scroll (DECSCLM). */
/* Not supported since version 2.0. */
break;
case 5: /* Reverse Video (DECSCNM). */
inverse_video (tw, 1);
break;
case 6: /* Origin Mode (DECOM). */
tw->curr_tflags |= TORIGIN;
gotoxy (tw, 0, 0);
break;
case 7: /* Wraparound Mode (DECAWM). */
tw->curr_tflags |= TWRAPAROUND;
break;
case 8: /* Auto-repeat Keys (DECARM). */
/* Not yet implemented */
break;
case 9: /* Send Mouse X & Y on button press. */
/* Not yet implemented */
break;
case 18: /* Print Form Feed (DECPFF). */
/* Not yet implemented */
break;
case 19: /* Set print extent to full screen (DECPEX). */
/* Not yet implemented */
break;
case 25: /* Show Cursor (DECTCEM). */
tw->curr_tflags |= TCURS_ON;
break;
case 35: /* Enable font-shifting functions (rxvt). */
/* Not yet implemented */
break;
case 38: /* Enter Tektronix Mode (DECTEK). */
/* Beurk! :-( */
break;
case 40: /* Allow 80 -> 132 Mode (xterm?). */
tw->curr_tflags |= TDECCOLM;
break;
case 41: /* more(1) fix (xterm?). */
/* Dunno. */
break;
case 42: /* Enable Nation Replacement Character sets (DECNRCM). */
/* Dunno. */
break;
case 44: /* Turn On Margin Bell. */
/* Would be funny ... */
break;
case 45: /* Reverse-wraparound Mode (xterm?). */
/* Dunno. */
break;
case 46: /* Start logging, xterm */
/* Not yet implemented. */
break;
case 47: /* Use Alternate Screen Buffer. */
/* Not yet implemented. */
break;
case 66: /* Application Keypad (DECNKM). */
case 67: /* Backarrow key sends backspace (DECBKM). */
case 69: /* Enable left and right margin mode (DECLRMM) */
case 80: /* Enable Sixel Scrolling (DECSDM) */
case 95: /* Do not clear screen when DECCOLM is set/reset (DECNCSM) */
case 1000: /* Send Mouse X & Y on button press and release */
case 1001: /* Use Hilite Mouse Tracking. */
case 1002: /* Use Cell Motion Mouse Tracking. */
case 1003: /* Use All Motion Mouse Tracking. */
case 1004: /* Send FocusIn/FocusOut events */
case 1005: /* Enable UTF-8 Mouse Mode */
case 1006: /* Enable SGR Mouse Mode */
case 1007: /* Enable Alternate Scroll Mode */
case 1010: /* Scroll to bottom on tty output (rxvt). */
case 1011: /* Scroll to bottom on key press (rxvt). */
case 1034: /* Interpret "meta" key, xterm */
case 1035: /* Enable special modifiers for Alt and NumLock keys. */
case 1036: /* Send ESC when Meta modifies a key. */
case 1037: /* Send DEL from the editing-keypad Delete key. */
case 1039: /* Send ESC when Alt modifies a key, xterm. */
case 1040: /* Keep selection even if not highlighted */
case 1041: /* Use the CLIPBOARD selection, xterm */
case 1042: /* Enable Urgency window manager hint when Control-G is received */
case 1043: /* Enable raising of the window when Control-G is received */
case 1044: /* Reuse the most recent data copied to CLIPBOARD */
case 1046: /* Enable switching to/from Alternate Screen */
case 1047: /* Use Alternate Screen Buffer. */
case 1048: /* Save cursor as in DECSC. */
case 1049: /* Save cursor as in DECSC and use Alternate Screen Buffer, clearing it first. */
case 1050: /* Set terminfo/termcap function-key mode */
case 1051: /* Set Sun function-key mode. */
case 1052: /* Set HP function-key mode. */
case 1053: /* Set SCO function-key mode */
case 1060: /* Set legacy keyboard emulation (X11R6). */
case 1061: /* Set Sun/PC keyboard emulation of VT220 keyboard. */
case 2004: /* Set bracketed paste mode */
/* Not yet implemented. */
break;
}
count++;
}
while (ei >= 0);
break;
case 'i': /* Media Copy (MC), DEC-specific. */
break;
case 'l': /* mode reset */
count = 0;
do {
ei = popescbuf(tw, tw->escbuf);
if (count == 0 && ei == -1)
ei = 0;
switch (ei) {
case 1: /* Cursor keys in cursor mode */
tw->curs_mode = CURSOR_NORMAL;
break;
case 2: /* VT52 mode */
tw->vt_mode = MODE_VT52;
tw->scroll_top = tw->miny;
tw->scroll_bottom = tw->maxy;
break;
case 3: /* DECCOLM - 80 column mode. The original VT100
also did a clear/home on that. */
if (tw->curr_tflags & TDECCOLM) {
resize_textwin (tw, 80, NROWS (tw),
SCROLLBACK (tw));
clear (tw);
gotoxy (tw, 0, 0);
}
break;
case 4: /* DECSCLM: jump scroll */
/* Not supported since version 2.0 */
break;
case 5: /* DECSCNM: inverse video off. */
inverse_video (tw, 0);
break;
case 6: /* relative mode */
tw->curr_tflags &= ~TORIGIN;
gotoxy (tw, 0, 0);
break;
case 7: /* wrap off */
tw->curr_tflags &= ~TWRAPAROUND;
break;
case 8: /* autorepeat off */
/* Not yet implemented */
break;
case 9: /* Don't send Mouse X & Y on button press */
/* Not yet implemented */
break;
case 10: /* Hide toolbar */
break;
case 12: /* Stop Blinking Cursor */
break;
case 13: /* Disable Blinking Cursor */
break;
case 14: /* Disable XOR of Blinking Cursor control sequence and menu */
/* Not yet implemented */
break;
case 16: /* edit selection deferred */
/* Not yet implemented */
break;
case 18: /* Don't print form feed (DECPFF) */
break;
case 19: /* Limit print to scrolling region (DECPEX) */
break;
case 25: /* cursor off */
tw->curr_tflags &= ~TCURS_ON;
break;
case 30: /* Don't show scrollbar */
break;
case 35: /* Disable font-shifting function */
break;
case 40: /* Reset column mode. */
tw->curr_tflags &= ~TDECCOLM;
break;
case 41: /* No more(1) fix */
case 42: /* Disable National Replacement Character sets (DECNRCM), VT220 */
case 44: /* Turn Off Margin Bell */
case 45: /* No Reverse-wraparound Mode */
case 46: /* Stop Logging */
case 47: /* Use Normal Screen Buffer */
case 66: /* Numeric keypad (DECNKM), VT320 */
case 67: /* Backarrow key sends delete (DECBKM), VT340 */
case 69: /* Disable left and right margin mode (DECLRMM) */
case 80: /* Disable Sixel Scrolling (DECSDM) */
case 95: /* Clear screen when DECCOLM is set/reset (DECNCSM) */
case 1000: /* Don't send Mouse X & Y on button press and release */
case 1001: /* Don't use Hilite Mouse Tracking. */
case 1002: /* Don't use Cell Motion Mouse Tracking. */
case 1003: /* Don't use All Motion Mouse Tracking. */
case 1004: /* Don't send FocusIn/FocusOut events */
case 1005: /* Disable UTF-8 Mouse Mode */
case 1006: /* Disable SGR Mouse Mode */
case 1007: /* Disable Alternate Scroll Mode */
case 1010: /* Don't scroll to bottom on tty output (rxvt). */
case 1011: /* Don't scroll to bottom on key press (rxvt). */
case 1034: /* Don't interpret "meta" key, xterm */
case 1035: /* Disable special modifiers for Alt and NumLock keys. */
case 1036: /* Don't send ESC when Meta modifies a key. */
case 1037: /* Send VT220 Remove from the editing-keypad Delete key */
case 1039: /* Send ESC when Alt modifies a key, xterm. */
case 1040: /* Keep selection even if not highlighted */
case 1041: /* Use the PRIMARY selection, xterm */
case 1042: /* Disable Urgency window manager hint when Control-G is received */
case 1043: /* Disable raising of the window when Control-G is received */
case 1044: /* Don't reuse the most recent data copied to CLIPBOARD */
case 1046: /* disable switching to/from Alternate Screen */
case 1047: /* Use Normal Screen Buffer. */
case 1048: /* Restore cursor as in DECSC. */
case 1049: /* Use Normal Screen Buffer as in DECSC and restore cursor */
case 1050: /* Reset terminfo/termcap function-key mode */
case 1051: /* Reset Sun function-key mode. */
case 1052: /* Reset HP function-key mode. */
case 1053: /* Reset SCO function-key mode */
case 1060: /* Reset legacy keyboard emulation (X11R6). */
case 1061: /* Reset Sun/PC keyboard emulation of VT220 keyboard. */
case 2004: /* Reset bracketed paste mode */
break;
}
count++;
}
while (ei >= 0);
break;
case 'n': /* Device Status Report (DSR, DEC-specific) */
break;
case 'r': /* mode restore */
/* Not yet implemented */
break;
case 's': /* mode save */
/* Not yet implemented */
break;
case 'y': /* invoke test(s) */
/* Not yet implemented */
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ';': /* parameters for extended escape */
pushescbuf(tw->escbuf, c);
return;
default:
SYSLOG((LOG_ERR, "vt100_esc_mode: unknown %c", c));
break;
}
tw->output = vt100_putch;
}
/*
* cleartab(tw, x): clear tab at position x
*/
static void cleartab(TEXTWIN* tw, int x)
{
TABLIST *tab, *oldtab;
oldtab = tab = tw->tabs;
while (tab != NULL && tab->tabpos < x)
{
oldtab = tab;
tab = tab->nexttab;
}
if (tab != NULL && tab->tabpos == x)
{
if (tab == tw->tabs)
tw->tabs = tab->nexttab;
else
oldtab->nexttab = tab->nexttab;
free(tab);
}
}
/*
* settab(tw, x): set tab at position x
*/
static void settab(TEXTWIN* tw, int x)
{
TABLIST *tab, *oldtab, *newtab;
oldtab = tab = tw->tabs;
while (tab != NULL && tab->tabpos < x) {
oldtab = tab;
tab = tab->nexttab;
}
if (tab == NULL || (tab->tabpos != x)) {
if ((newtab = (TABLIST *) malloc(sizeof(TABLIST))) != NULL) {
newtab->tabpos = x;
newtab->nexttab = tab;
if (tab == tw->tabs)
tw->tabs = newtab;
else
oldtab->nexttab = newtab;
}
}
}
/*
* Clear all tabs and reset them at 8 character intervals.
* Also referenced from textwin.c:create_textwin.
*/
void
reset_tabs (TEXTWIN* tw)
{
int x = 0;
clearalltabs (tw);
for (x = 0; x < tw->maxx; x+= 8) {
settab(tw, x);
}
}
static void
clearalltabs (TEXTWIN* tw)
{
TABLIST* tab;
TABLIST* oldtab;
oldtab = tab = tw->tabs;
while (tab != NULL) {
oldtab = tab;
tab = tab->nexttab;
free (oldtab);
}
tw->tabs = NULL;
}
/* Answer back. Send the answer back string. */
static
void answerback (TEXTWIN* tw)
{
sendstr (tw, tw100_env);
}
/*
* reportxy (tw): Report cursor position as ESCy;xR
*/
static void reportxy(TEXTWIN* tw)
{
char r[ESCBUFSIZE];
/* Convert from internal to screen coordinates */
int x = tw->cx + 1;
int y = tw->cy + 1 - tw->miny - RELOFFSET(tw);
sendstr(tw, "\033[");
_ltoa(y, r, 10);
sendstr(tw, r);
sendstr(tw, ";");
_ltoa(x, r, 10);
sendstr(tw, r);
sendstr(tw, "R");
}
/* Handle the control sequence ESC [...n. */
static void vt100_esc_attr(TEXTWIN* tw, unsigned int c)
{
int cx, cy, count, ei;
long param1, param2, param3;
cx = tw->cx;
cy = tw->cy;
SYSLOG((LOG_ERR, "vt100_esc_attr: %c (%d)", c, c));
dump(c);
switch (c) {
case '\000':
case '\001':
case '\002':
case '\003':
case '\004':
case '\006':
case '\020':
case '\021':
case '\022':
case '\023':
case '\024':
case '\025':
case '\026':
case '\027':
case '\030':
case '\031':
case '\032':
case '\034':
case '\035':
case '\036':
case '\037':
/* ignore */
return;
case '\005':
/* ENQ */
return;
case '\010': /* ANSI spec - ^H in middle of ESC (yuck!) */
cub1 (tw);
return;
case '\012': /* ANSI spec - ^J in middle of ESC (yuck!) */
case '\013': /* ANSI spec - ^K in middle of ESC (yuck!) */
case '\014': /* ANSI spec - ^L in middle of ESC (yuck!) */
cud1 (tw);
return;
case '\015': /* ANSI spec - ^M in middle of ESC (yuck!) */
carriage_return(tw);
return;
case '\016':
/* SO */
return;
case '\017':
/* SO */
return;
case '\033': /* ESC - restart sequence */
tw->output = vt100_putesc;
tw->escbuf[0] = '\0';
return;
case '!': /* Maybe Soft terminal reset (DECSTR) if followed by p. */
pushescbuf (tw->escbuf, c);
return;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ';': /* parameters for extended escape */
pushescbuf (tw->escbuf, c);
return;
case '?': /* DEC Private Mode Set (DECSET). */
tw->output = vt100_esc_mode;
return;
case '@': /* Insert Ps (Blank) Character(s) (default = 1) (ICH). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
while (count) {
/* FIXME: Make insert_char take a fourth parameter
with the number of characters to insert. */
insert_char (tw, cx, cy);
--count;
}
break;
case 'A': /* Cursor Up Ps Times (default = 1) (CUU). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
cuu (tw, count);
break;
case 'B': /* Cursor Down Ps Times (default = 1) (CUD). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
cud (tw, count);
break;
case 'C': /* Cursor Forward Ps Times (default = 1) (CUF). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
cuf (tw, count);
break;
case 'D': /* Cursor Backward Ps Times (default = 1) (CUD). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
cub (tw, count);
break;
case 'E': /* Cursor Next Line Ps Times (default = 1) (CNL). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
while (--count >= 0)
{
new_line(tw);
}
gotoxy (tw, 0, tw->cy + RELOFFSET (tw));
break;
case 'F': /* Cursor Preceding Line Ps Times (default = 1) (CPL). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
while (--count >= 0)
{
reverse_cr(tw);
}
gotoxy (tw, 0, tw->cy + RELOFFSET (tw));
break;
case 'G': /* Cursor Character Absolute [column] (default = [row,1]) (CHA). */
cx = popescbuf(tw, tw->escbuf);
if (cx < 1)
cx = 1;
gotoxy (tw, cx - 1, cy + RELOFFSET (tw));
break;
case 'H': /* Cursor Position [row;column] (default = [1,1]) (CUP). */
case 'f': /* Horizontal and Vertical Position [row;column] (default = [1,1]) (HVP). */
cy = popescbuf (tw, tw->escbuf);
if (cy < 1)
cy = 1;
cx = popescbuf(tw, tw->escbuf);
if (cx < 1)
cx = 1;
gotoxy (tw, cx - 1, tw->miny + cy - 1);
break;
case 'I': /* Cursor Forward Tabulation Ps tab stops (default = 1) (CHT). */
break;
case 'J': /* Erase in Display (ED). */
count = popescbuf (tw, tw->escbuf);
if (count < 0)
count = 0;
switch (count) {
case 0: /* Erase Below (default). */
clrfrom (tw, cx, cy, tw->maxx - 1, tw->maxy - 1);
break;
case 1: /* Erase Above. */
clrfrom (tw, 0, tw->miny, cx, cy);
break;
case 2: /* Erase All. */
clrfrom (tw, 0, tw->miny,
tw->maxx - 1, tw->maxy - 1);
break;
case 3: /* Erase Saved Lines (xterm). */
clrfrom (tw, 0, 0, tw->miny, tw->maxx - 1);
break;
}
break;
case 'K': /* Erase in Line (EL). */
count = popescbuf (tw, tw->escbuf);
if (count < 0)
count = 0;
switch (count) {
case 0: /* Erase to Right (default). */
clrfrom (tw, cx, cy, tw->maxx - 1, cy);
break;
case 1: /* Erase to Left. */
clrfrom (tw, 0, cy, cx, cy);
break;
case 2: /* Erase All. */
clrfrom (tw, 0, cy, tw->maxx - 1, cy);
break;
}
break;
case 'L': /* Insert Ps line(s) (default = 1) (IL). */
count = popescbuf(tw, tw->escbuf);
if (count < 1)
count = 1;
while (count > 0) {
insert_line (tw);
--count;
}
tw->do_wrap = 0;
break;
case 'M': /* Delete Ps line(s) (default = 1) (DL). */
count = popescbuf(tw, tw->escbuf);
if (count < 1)
count = 1;
while (count > 0) {
delete_line (tw);
--count;
}
break;
case 'P': /* Delete Ps Character(s) (default = 1) (DCH). */
count = popescbuf(tw, tw->escbuf);
if (count < 1)
count = 1;
while (count) {
delete_char(tw, cx, cy);
count--;
}
break;
case 'R': /* ??? Cursor position response */
break;
case 'S': /* Scroll up Ps (line(s) (default = 1) (SU). */
case 'T': /* Scroll down Ps (line(s) (default = 1) (SD). */
/* Not yet implemented. */
/* If CSI Ps ; Ps ; Ps ; Ps ; Ps T then mouse tracking is requested. */
break;
case '^': /* Scroll down Ps lines (default = 1) (SD), ECMA-48. */
/* This was a publication error in the original ECMA-48 5th edition */
/* (1991) corrected in 2003. */
break;
case 'X': /* Erase Ps character(s) (default = 1) (ECH). */
count = popescbuf (tw, tw->escbuf);
ei = tw->cx;
do {
paint (tw, ' ');
--count;
++tw->cx;
} while (tw->cx < tw->maxx && count > 1);
tw->cx = ei;
break;
case 'Z': /* Cursor Backward Tabulation Ps tab stops (default = 1) (CBT). */
/* Not yet implemented. */
break;
case '`': /* Character Position Absolute [column] (default = [row,1]) (HPA). */
cx = popescbuf(tw, tw->escbuf);
if (cx < 1)
cx = 1;
gotoxy (tw, cx - 1, cy - RELOFFSET (tw));
break;
case 'a': /* Character Position Relative [columns] (default = [row,col+1]) (HPR). */
count = popescbuf(tw, tw->escbuf);
if (count < 1)
count = 1;
gotoxy (tw, cx + count, cy - RELOFFSET (tw));
break;
case 'b': /* Repeat the preceding graphic character Ps times (REP). */
/* Not yet implemented. */
break;
case 'c': /* Send Device Attributes (Primary DA). */
ei = popescbuf (tw, tw->escbuf);
if (ei <= 0) {
if (tw->vt_mode == MODE_VT52)
sendstr(tw, "\033/Z");
else
/* 2 = base vt100 with advanced video option (AVO) */
sendstr (tw, "\033[?1;2c");
/* FIXME: Should be configurable. */
}
/* FIXME: CSI > Ps c requests Secondary DA. Insert into caller ... */
break;
case 'd': /* Line Position Absolute [row] (default = [1,column]) (VPA). */
cy = popescbuf (tw, tw->escbuf);
if (cy < 1)
cy = 1;
gotoxy (tw, cx, cy - RELOFFSET (tw));
break;
case 'e': /* Line Position Relative [rows] (default = [row+1,column]) (VPR). */
count = popescbuf (tw, tw->escbuf);
if (count < 1)
count = 1;
gotoxy (tw, cx, cy + count - RELOFFSET (tw));
break;
case 'g': /* Tab Clear (TBC). */
count = popescbuf(tw, tw->escbuf);
if (count < 0)
count = 0;
switch (count) {
case 0: /* Clear Current Column (default). */
cleartab(tw, cx);
break;
case 3: /* Clear All. */
clearalltabs(tw);
break;
}
break;
case 'h': /* various keyboard & screen attributes */
count = 0;
do {
ei = popescbuf(tw, tw->escbuf);
if (count == 0 && ei < 0)