-
Notifications
You must be signed in to change notification settings - Fork 2
/
vt100.c
1078 lines (1030 loc) · 22.8 KB
/
vt100.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
/*
########################################################################
# This file is part of the minicom communications package for WRAMP.
#
# Copyright 1991-1995 Miquel van Smoorenburg.
# Copyright (C) 2019 The University of Waikato, Hamilton, New Zealand.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################
*/
#include "port.h"
#include "window.h"
#include "vt100.h"
#include "config.h"
#include "patchlevel.h"
#define OLD 0
/*
* The global variable esc_s holds the escape sequence status:
* 0 - normal
* 1 - ESC
* 2 - ESC [
* 3 - ESC [ ?
* 4 - ESC (
* 5 - ESC )
* 6 - ESC #
* 7 - ESC P
*/
static int esc_s = 0;
#define ESC 27
/* Structure to hold escape sequences. */
struct escseq {
int code;
char *vt100_st;
char *vt100_app;
char *ansi;
};
/* Escape sequences for different terminal types. */
static struct escseq vt_keys[] = {
#ifndef _DGUX_SOURCE
{ K_F1, "OP", "OP", "OP" },
{ K_F2, "OQ", "OQ", "OQ" },
{ K_F3, "OR", "OR", "OR" },
{ K_F4, "OS", "OS", "OS" },
{ K_F5, "[16~", "[16~", "OT" },
{ K_F6, "[17~", "[17~", "OU" },
{ K_F7, "[18~", "[18~", "OV" },
{ K_F8, "[19~", "[19~", "OW" },
{ K_F9, "[20~", "[20~", "OX" },
{ K_F10, "[21~", "[21~", "OY" },
{ K_F11, "[23~", "[23~", "OY" },
{ K_F12, "[24~", "[24~", "OY" },
{ K_HOME, "[1~", "[1~", "[H" },
{ K_PGUP, "[5~", "[5~", "[V" },
{ K_UP, "[A", "OA", "[A" },
{ K_LT, "[D", "OD", "[D" },
{ K_RT, "[C", "OC", "[C" },
{ K_DN, "[B", "OB", "[B" },
{ K_END, "[4~", "[4~", "[Y" },
{ K_PGDN, "[6~", "[6~", "[U" },
{ K_INS, "[2~", "[2~", "[@" },
{ K_DEL, "[3~", "[3~", "\177" },
{ 0, NULL, NULL, NULL }
#else
{ K_F1, "[17~", "[17~", "OP" },
{ K_F2, "[18~", "[18~", "OQ" },
{ K_F3, "[19~", "[19~", "OR" },
{ K_F4, "[20~", "[20~", "OS" },
{ K_F5, "[21~", "[21~", "OT" },
{ K_F6, "[23~", "[23~", "OU" },
{ K_F7, "[24~", "[24~", "OV" },
{ K_F8, "[25~", "[25~", "OW" },
{ K_F9, "[26~", "[26~", "OX" },
{ K_F10, "[28~", "[28~", "OY" },
{ K_F11, "[29~", "[29~", "OZ" },
{ K_F12, "[31~", "[31~", "OA" },
{ K_HOME, "OP", "OP", "[H" },
{ K_PGUP, "OQ", "OQ", "[V" },
{ K_UP, "[A", "OA", "[A" },
{ K_LT, "[D", "OD", "[D" },
{ K_RT, "[C", "OC", "[C" },
{ K_DN, "[B", "OB", "[B" },
{ K_END, "OR", "OR", "[Y" },
{ K_PGDN, "OS", "OS", "[U" },
{ K_INS, "[1~", "[1~", "[@" },
{ K_DEL, "[3~", "[3~", "\177" },
{ 0, NULL, NULL, NULL }
#endif
};
#if TRANSLATE
/* Taken from the Linux kernel source: linux/drivers/char/console.c */
static unsigned char * vt_map[] = {
/* 8-bit Latin-1 mapped to the PC character set: '.' means non-printable */
(unsigned char *)
"................"
"................"
" !\"#$%&'()*+,-./0123456789:;<=>?"
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
"`abcdefghijklmnopqrstuvwxyz{|}~."
"................"
"................"
"\377\255\233\234\376\235\174\025\376\376\246\256\252\055\376\376"
"\370\361\375\376\376\346\024\371\376\376\247\257\254\253\376\250"
"\376\376\376\376\216\217\222\200\376\220\376\376\376\376\376\376"
"\376\245\376\376\376\376\231\376\235\376\376\376\232\376\376\341"
"\205\240\203\376\204\206\221\207\212\202\210\211\215\241\214\213"
"\376\244\225\242\223\376\224\366\233\227\243\226\201\376\376\230",
/* vt100 graphics */
(unsigned char *)
"................"
"................"
" !\"#$%&'()*+,-./0123456789:;<=>?"
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^ "
"\004\261\376\376\376\376\370\361\040\376\331\277\332\300\305\376"
"\376\304\376\376\303\264\301\302\263\376\376\376\376\376\234."
"................"
"................"
"\040\255\233\234\376\235\174\025\376\376\246\256\252\055\376\376"
"\370\361\375\376\376\346\024\371\376\376\247\257\254\253\376\250"
"\376\376\376\376\216\217\222\200\376\220\376\376\376\376\376\376"
"\376\245\376\376\376\376\231\376\376\376\376\376\232\376\376\341"
"\205\240\203\376\204\206\221\207\212\202\210\211\215\241\214\213"
"\376\244\225\242\223\376\224\366\376\227\243\226\201\376\376\230"
};
static char *vt_trans[2];
static int vt_charset = 0; /* Character set. */
#endif
#ifdef _SELECT
static int vt_echo; /* Local echo on/off. */
#endif
static int vt_type = ANSI; /* Terminal type. */
static int vt_wrap = 0; /* Line wrap on/off */
static int vt_addlf = 0; /* Add linefeed on/off */
static int vt_fg; /* Standard foreground color. */
static int vt_bg; /* Standard background color. */
static int vt_keypad; /* Keypad mode. */
static int vt_cursor; /* cursor key mode. */
static int vt_bs = 8; /* Code that backspace key sends. */
static int vt_insert = 0; /* Insert mode */
static int vt_crlf = 0; /* Return sends CR/LF */
static int vt_om; /* Origin mode. */
WIN *vt_win = NIL_WIN; /* Output window. */
static int vt_docap; /* Capture on/off. */
static FILE *vt_capfp; /* Capture file. */
static void (*vt_keyb)(); /* Gets called for NORMAL/APPL switch. */
static void (*termout)(); /* Gets called to output a string. */
static int escparms[8]; /* Cumulated escape sequence. */
#if OLD
static int ptr = -2; /* Index into escparms array. */
#else
static int ptr = 0; /* Index into escparms array. */
#endif
static long vt_tabs[5]; /* Tab stops for max. 32*5 = 160 columns. */
short newy1 = 0; /* Current size of scrolling region. */
short newy2 = 23;
/* Saved color and posistions */
static short savex = 0, savey = 0, saveattr = XA_NORMAL, savecol = 112;
#if TRANSLATE
static short savecharset;
static char *savetrans[2];
#endif
/*
* Initialize the emulator once.
*/
void vt_install(fun1, fun2, win)
void (*fun1)();
void (*fun2)();
WIN *win;
{
termout = fun1;
vt_keyb = fun2;
vt_win = win;
}
/* Partial init (after screen resize) */
void vt_pinit(win, fg, bg)
WIN *win;
int fg, bg;
{
vt_win = win;
newy1 = 0;
newy2 = vt_win->ys - 1;
wresetregion(vt_win);
if (fg > 0) vt_fg = fg;
if (bg > 0) vt_bg = bg;
wsetfgcol(vt_win, vt_fg);
wsetbgcol(vt_win, vt_bg);
}
/* Set characteristics of emulator. */
void vt_init(type, fg, bg, wrap, add)
int type;
int fg;
int bg;
int wrap;
int add;
{
vt_type = type;
if (vt_type == ANSI) {
vt_fg = WHITE;
vt_bg = BLACK;
} else {
vt_fg = fg;
vt_bg = bg;
}
if (wrap >= 0) vt_win->wrap = vt_wrap = wrap;
vt_addlf = add;
vt_insert = 0;
vt_crlf = 0;
vt_om = 0;
newy1 = 0;
newy2 = vt_win->ys - 1;
wresetregion(vt_win);
vt_keypad = NORMAL;
vt_cursor = NORMAL;
#ifdef _SELECT
vt_echo = 0;
#endif
vt_tabs[0] = 0x01010100;
vt_tabs[1] =
vt_tabs[2] =
vt_tabs[3] =
vt_tabs[4] = 0x01010101;
#if TRANSLATE
vt_charset = 0;
vt_trans[0] = vt_map[0];
vt_trans[1] = vt_map[1];
#endif
#if OLD
ptr = -2;
#else
ptr = 0;
memset(escparms, 0, sizeof(escparms));
#endif
esc_s = 0;
if (vt_keyb) (*vt_keyb)(vt_keypad, vt_cursor);
wsetfgcol(vt_win, vt_fg);
wsetbgcol(vt_win, vt_bg);
}
/* Change some things on the fly. */
void vt_set(addlf, wrap, capfp, docap, bscode, echo, cursor)
int addlf;
int wrap;
FILE *capfp;
int docap;
int bscode;
int echo;
int cursor;
{
if (addlf >= 0) vt_addlf = addlf;
if (wrap >= 0) vt_win->wrap = vt_wrap = wrap;
if (capfp != (FILE *)0) vt_capfp = capfp;
if (docap >= 0) vt_docap = docap;
if (bscode >= 0) vt_bs = bscode;
#ifdef _SELECT
if (echo >= 0) vt_echo = echo;
#endif
if (cursor >= 0) vt_cursor = cursor;
}
/* Output a string to the modem. */
static void v_termout(s, len)
char *s;
int len;
{
#ifdef _SELECT
char *p;
if (vt_echo) {
for(p = s; *p; p++) {
vt_out(*p);
if (!vt_addlf && *p == '\r') vt_out('\n');
}
wflush();
}
#endif
(*termout)(s, len);
}
/*
* Escape code handling.
*/
/*
* ESC was seen the last time. Process the next character.
*/
static void state1(c)
int c;
{
short x, y, f;
switch(c) {
case '[': /* ESC [ */
esc_s = 2;
return;
case '(': /* ESC ( */
esc_s = 4;
return;
case ')': /* ESC ) */
esc_s = 5;
return;
case '#': /* ESC # */
esc_s = 6;
return;
case 'P': /* ESC P (DCS, Device Control String) */
esc_s = 7;
return;
case 'D': /* Cursor down */
case 'M': /* Cursor up */
x = vt_win->curx;
if (c == 'D') { /* Down. */
y = vt_win->cury + 1;
if (y == newy2 + 1)
wscroll(vt_win, S_UP);
else if (vt_win->cury < vt_win->ys)
wlocate(vt_win, x, y);
}
if (c == 'M') { /* Up. */
y = vt_win->cury - 1;
if (y == newy1 - 1)
wscroll(vt_win, S_DOWN);
else if (y >= 0)
wlocate(vt_win, x, y);
}
break;
case 'E': /* CR + NL */
wputs(vt_win, "\r\n");
break;
case '7': /* Save attributes and cursor position */
case 's':
savex = vt_win->curx;
savey = vt_win->cury;
saveattr = vt_win->attr;
savecol = vt_win->color;
#if TRANSLATE
savecharset = vt_charset;
savetrans[0] = vt_trans[0];
savetrans[1] = vt_trans[1];
#endif
break;
case '8': /* Restore them */
case 'u':
#if TRANSLATE
vt_charset = savecharset;
vt_trans[0] = savetrans[0];
vt_trans[1] = savetrans[1];
#endif
vt_win->color = savecol; /* HACK should use wsetfgcol etc */
wsetattr(vt_win, saveattr);
wlocate(vt_win, savex, savey);
break;
case '=': /* Keypad into applications mode */
vt_keypad = APPL;
if (vt_keyb) (*vt_keyb)(vt_keypad, vt_cursor);
break;
case '>': /* Keypad into numeric mode */
vt_keypad = NORMAL;
if (vt_keyb) (*vt_keyb)(vt_keypad, vt_cursor);
break;
case 'Z': /* Report terminal type */
if (vt_type == VT100)
v_termout("\033[?1;0c", 0);
else
v_termout("\033[?c", 0);
break;
case 'c': /* Reset to initial state */
f = XA_NORMAL;
wsetattr(vt_win, f);
vt_win->wrap = (vt_type != VT100);
if (vt_wrap != -1) vt_win->wrap = vt_wrap;
vt_crlf = vt_insert = 0;
vt_init(vt_type, vt_fg, vt_bg, vt_win->wrap, 0);
wlocate(vt_win, 0, 0);
break;
case 'H': /* Set tab in current position */
x = vt_win->curx;
if (x > 159) x = 159;
vt_tabs[x / 32] |= 1 << (x % 32);
break;
case 'N': /* G2 character set for next character only*/
case 'O': /* G3 " " */
case '<': /* Exit vt52 mode */
default:
/* ALL IGNORED */
break;
}
esc_s = 0;
return;
}
/* ESC [ ... [hl] seen. */
static void ansi_mode(on_off)
int on_off;
{
int i;
for(i = 0; i <= ptr; i++) {
switch(escparms[i]) {
case 4: /* Insert mode */
vt_insert = on_off;
break;
case 20: /* Return key mode */
vt_crlf = on_off;
break;
}
}
}
/*
* ESC [ ... was seen the last time. Process next character.
*/
static void state2(c)
int c;
{
short x, y, attr, f;
char temp[32];
/* See if a number follows */
if (c >= '0' && c <= '9') {
#if OLD
if (ptr < 0) ptr = 0;
#endif
escparms[ptr] = 10*(escparms[ptr]) + c - '0';
return;
}
/* Separation between numbers ? */
if (c == ';') {
#if OLD
if (ptr < 0) ptr = 0; /* keithr@primenet.com */
if (ptr >= 0 && ptr < 15) ptr++;
#else
if (ptr < 15) ptr++;
#endif
return;
}
/* ESC [ ? sequence */
#if OLD
if (ptr < 0 && c == '?') {
#else
if (escparms[0] == 0 && ptr == 0 && c == '?') {
#endif
esc_s = 3;
return;
}
/* Process functions with zero, one, two or more arguments */
switch(c) {
case 'A':
case 'B':
case 'C':
case 'D': /* Cursor motion */
if ((f = escparms[0]) == 0) f = 1;
x = vt_win->curx;
y = vt_win->cury;
x += f * ((c == 'C') - (c == 'D'));
if (x < 0) x = 0;
if (x >= vt_win->xs) x = vt_win->xs - 1;
if (c == 'B') { /* Down. */
y += f;
if (y >= vt_win->ys) y = vt_win->ys - 1;
if (y >= newy2 + 1) y = newy2;
}
if (c == 'A') { /* Up. */
y -= f;
if (y < 0) y = 0;
if (y <= newy1 - 1) y = newy1;
}
wlocate(vt_win, x, y);
break;
case 'K': /* Line erasing */
switch(escparms[0]) {
case 0:
wclreol(vt_win);
break;
case 1:
wclrbol(vt_win);
break;
case 2:
wclrel(vt_win);
break;
}
break;
case 'J': /* Screen erasing */
x = vt_win->color;
y = vt_win->attr;
if (vt_type == ANSI) {
wsetattr(vt_win, XA_NORMAL);
wsetfgcol(vt_win, WHITE);
wsetbgcol(vt_win, BLACK);
}
switch(escparms[0]) {
case 0:
wclreos(vt_win);
break;
case 1:
wclrbos(vt_win);
break;
case 2:
winclr(vt_win);
break;
}
if (vt_type == ANSI) {
vt_win->color = x;
vt_win->attr = y;
}
break;
case 'n': /* Requests / Reports */
switch(escparms[0]) {
case 5: /* Status */
v_termout("\033[0n", 0);
break;
case 6: /* Cursor Position */
sprintf(temp, "\033[%d;%dR",
vt_win->cury + 1, vt_win->curx + 1);
v_termout(temp, 0);
break;
}
break;
case 'c': /* Identify Terminal Type */
if (vt_type == VT100) {
v_termout("\033[?1;2c", 0);
break;
}
v_termout("\033[?c", 0);
break;
case 'x': /* Request terminal parameters. */
/* Always answers 19200-8N1 no options. */
sprintf(temp, "\033[%c;1;1;120;120;1;0x",
escparms[0] == 1 ? '3' : '2');
v_termout(temp, 0);
break;
case 's': /* Save attributes and cursor position */
savex = vt_win->curx;
savey = vt_win->cury;
saveattr = vt_win->attr;
savecol = vt_win->color;
#if TRANSLATE
savecharset = vt_charset;
savetrans[0] = vt_trans[0];
savetrans[1] = vt_trans[1];
#endif
break;
case 'u': /* Restore them */
#if TRANSLATE
vt_charset = savecharset;
vt_trans[0] = savetrans[0];
vt_trans[1] = savetrans[1];
#endif
break;
vt_win->color = savecol; /* HACK should use wsetfgcol etc */
wsetattr(vt_win, saveattr);
wlocate(vt_win, savex, savey);
break;
case 'h':
ansi_mode(1);
break;
case 'l':
ansi_mode(0);
break;
case 'H':
case 'f': /* Set cursor position */
if ((y = escparms[0]) == 0) y = 1;
if ((x = escparms[1]) == 0) x = 1;
if (vt_om) y += newy1;
wlocate(vt_win, x - 1, y - 1);
break;
case 'g': /* Clear tab stop(s) */
if (escparms[0] == 0) {
x = vt_win->curx;
if (x > 159) x = 159;
vt_tabs[x / 32] &= ~ (1 << x % 32);
}
if (escparms[0] == 3)
for(x = 0; x < 5; x++) vt_tabs[x] = 0;
break;
case 'm': /* Set attributes */
#if OLD
/* Without argument, esc-parms[0] is 0 */
if (ptr < 0) ptr = 0;
#endif
attr = wgetattr((vt_win));
for (f = 0; f <= ptr; f++) {
if (escparms[f] >= 30 && escparms[f] <= 37)
wsetfgcol(vt_win, escparms[f] - 30);
if (escparms[f] >= 40 && escparms[f] <= 47)
wsetbgcol(vt_win, escparms[f] - 40);
switch(escparms[f]) {
case 0:
attr = XA_NORMAL;
wsetfgcol(vt_win, vt_fg);
wsetbgcol(vt_win, vt_bg);
break;
case 4:
attr |= XA_UNDERLINE;
break;
case 7:
attr |= XA_REVERSE;
break;
case 1:
attr |= XA_BOLD;
break;
case 5:
attr |= XA_BLINK;
break;
case 22: /* Bold off */
attr &= ~XA_BOLD;
break;
case 24: /* Not underlined */
attr &=~XA_UNDERLINE;
break;
case 25: /* Not blinking */
attr &= ~XA_BLINK;
break;
case 27: /* Not reverse */
attr &= ~XA_REVERSE;
break;
case 39: /* Default fg color */
wsetfgcol(vt_win, vt_fg);
break;
case 49: /* Default bg color */
wsetbgcol(vt_win, vt_bg);
break;
}
}
wsetattr(vt_win, attr);
break;
case 'L': /* Insert lines */
if ((x = escparms[0]) == 0) x = 1;
for(f = 0; f < x; f++)
winsline(vt_win);
break;
case 'M': /* Delete lines */
if ((x = escparms[0]) == 0) x = 1;
for(f = 0; f < x; f++)
wdelline(vt_win);
break;
case 'P': /* Delete Characters */
if ((x = escparms[0]) == 0) x = 1;
for(f = 0; f < x; f++)
wdelchar(vt_win);
break;
case '@': /* Insert Characters */
if ((x = escparms[0]) == 0) x = 1;
for(f = 0; f < x; f++)
winschar(vt_win);
break;
case 'r': /* Set scroll region */
if ((newy1 = escparms[0]) == 0) newy1 = 1;
if ((newy2 = escparms[1]) == 0) newy2 = vt_win->ys;
newy1-- ; newy2--;
if (newy1 < 0) newy1 = 0;
if (newy2 < 0) newy2 = 0;
if (newy1 >= vt_win->ys) newy1 = vt_win->ys - 1;
if (newy2 >= vt_win->ys) newy2 = vt_win->ys - 1;
if (newy1 >= newy2) {
newy1 = 0;
newy2 = vt_win->ys - 1;
}
wsetregion(vt_win, newy1, newy2);
wlocate(vt_win, 0, newy1);
break;
case 'i': /* Printing */
case 'y': /* Self test modes */
default:
/* IGNORED */
break;
}
/* Ok, our escape sequence is all done */
esc_s = 0;
#if OLD
ptr = -2;
#else
ptr = 0;
memset(escparms, 0, sizeof(escparms));
#endif
return;
}
/* ESC [? ... [hl] seen. */
static void dec_mode(on_off)
int on_off;
{
int i;
for(i = 0; i <= ptr; i++) {
switch(escparms[i]) {
case 1: /* Cursor keys in cursor/appl mode */
vt_cursor = on_off ? APPL : NORMAL;
if (vt_keyb) (*vt_keyb)(vt_keypad, vt_cursor);
break;
case 6: /* Origin mode. */
vt_om = on_off;
wlocate(vt_win, 0, newy1);
break;
case 7: /* Auto wrap */
vt_win->wrap = on_off;
break;
case 25: /* Cursor on/off */
wcursor(vt_win, on_off ? CNORMAL : CNONE);
break;
case 67: /* Backspace key sends. (FIXME: vt420) */
/* setbackspace(on_off ? 8 : 127); */
break;
default: /* Mostly set up functions */
/* IGNORED */
break;
}
}
}
/*
* ESC [ ? ... seen.
*/
static void state3(c)
int c;
{
/* See if a number follows */
if (c >= '0' && c <= '9') {
#if OLD
if (ptr < 0) ptr = 0;
#endif
escparms[ptr] = 10*(escparms[ptr]) + c - '0';
return;
}
#if OLD
/* ESC [ ? number seen */
if (ptr < 0) {
esc_s = 0;
return;
}
#endif
switch(c) {
case 'h':
dec_mode(1);
break;
case 'l':
dec_mode(0);
break;
case 'i': /* Printing */
case 'n': /* Request printer status */
default:
/* IGNORED */
break;
}
esc_s = 0;
#if OLD
ptr = -2;
#else
ptr = 0;
memset(escparms, 0, sizeof(escparms));
#endif
return;
}
/*
* ESC ( Seen.
*/
/*ARGSUSED*/
static void state4(c)
int c;
{
/* Switch Character Sets. */
#if !TRANSLATE
/* IGNORED */
(void)c;
#else
switch(c) {
case 'A':
case 'B':
vt_trans[0] = vt_map[0];
break;
case '0':
case 'O':
vt_trans[0] = vt_map[1];
break;
}
#endif
esc_s = 0;
}
/*
* ESC ) Seen.
*/
/*ARGSUSED*/
static void state5(c)
int c;
{
/* Switch Character Sets. */
#if !TRANSLATE
/* IGNORED */
(void)c;
#else
switch(c) {
case 'A':
case 'B':
vt_trans[1] = vt_map[0];
break;
case 'O':
case '0':
vt_trans[1] = vt_map[1];
break;
}
#endif
esc_s = 0;
}
/*
* ESC # Seen.
*/
/*ARGSUSED*/
static void state6(c)
int c;
{
int x, y;
/* Double height, double width and selftests. */
switch(c) {
case '8':
/* Selftest: fill screen with E's */
vt_win->doscroll = 0;
vt_win->direct = 0;
wlocate(vt_win, 0, 0);
for(y = 0; y < vt_win->ys; y++) {
wlocate(vt_win, 0, y);
for(x = 0; x < vt_win->xs; x++)
wputc(vt_win, 'E');
}
wlocate(vt_win, 0, 0);
vt_win->doscroll = 1;
wredraw(vt_win, 1);
break;
default:
/* IGNORED */
break;
}
esc_s = 0;
}
/*
* ESC P Seen.
*/
static void state7(c)
int c;
{
/*
* Device dependant control strings. The Minix virtual console package
* uses these sequences. We can only turn cursor on or off, because
* that's the only one supported in termcap. The rest is ignored.
*/
static char buf[17];
static int pos = 0;
static int state = 0;
if (c == ESC) {
state = 1;
return;
}
if (state == 1) {
buf[pos] = 0;
pos = 0;
state = 0;
esc_s = 0;
if (c != '\\') return;
/* Process string here! */
if (!strcmp(buf, "cursor.on")) wcursor(vt_win, CNORMAL);
if (!strcmp(buf, "cursor.off")) wcursor(vt_win, CNONE);
if (!strcmp(buf, "linewrap.on")) {
vt_wrap = -1;
vt_win->wrap = 1;
}
if (!strcmp(buf, "linewrap.off")) {
vt_wrap = -1;
vt_win->wrap = 0;
}
return;
}
if (pos > 15) return;
buf[pos++] = c;
}
void vt_out(ch)
int ch;
{
int f;
unsigned char c;
int go_on = 0;
if (!ch) return;
#if OLD
if (ptr == -2) { /* Initialize */
ptr = -1;
for(f = 0; f < 8; f++) escparms[f] = 0;
}
#endif
c = (unsigned char)ch;
if (vt_docap == 2) /* Literal. */
fputc(c, vt_capfp);
/* Process <31 chars first, even in an escape sequence. */
switch(c) {
case 5: /* AnswerBack for vt100's */
if (vt_type != VT100) {
go_on = 1;
break;
}
v_termout(SCCS_ID, 0);
break;
case '\r': /* Carriage return */
wputc(vt_win, c);
if (vt_addlf) {
wputc(vt_win, '\n');
if (vt_docap == 1)
fputc('\n', vt_capfp);
}
break;
case '\t': /* Non - destructive TAB */
/* Find next tab stop. */
for(f = vt_win->curx + 1; f < 160; f++)
if (vt_tabs[f / 32] & (1 << f % 32)) break;
if (f >= vt_win->xs) f = vt_win->xs - 1;
wlocate(vt_win, f, vt_win->cury);
if (vt_docap == 1) fputc(c, vt_capfp);
break;
case 013: /* Old Minix: CTRL-K = up */
wlocate(vt_win, vt_win->curx, vt_win->cury - 1);
break;
case '\f': /* Form feed: clear screen. */
winclr(vt_win);
wlocate(vt_win, 0, 0);
break;
#if !TRANSLATE
case 14:
case 15: /* Change character set. Not supported. */
break;
#else
case 14:
vt_charset = 1;
break;
case 15:
vt_charset = 0;
break;
#endif
case 24:
case 26: /* Cancel escape sequence. */
esc_s = 0;
break;
case ESC: /* Begin escape sequence */
esc_s = 1;
break;
case 128+ESC: /* Begin ESC [ sequence. */
esc_s = 2;
break;
case '\n':
case '\b':
case 7: /* Bell */
wputc(vt_win, c);
if (vt_docap == 1)
fputc(c, vt_capfp);
break;
default:
go_on = 1;
break;
}
if (!go_on) return;
/* Now see which state we are in. */
switch(esc_s) {
case 0: /* Normal character */
#if TRANSLATE
if (vt_type == VT100) {
if (vt_insert)