-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt100.c
1142 lines (1079 loc) · 29 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 contains the terminal "driver" code for splitsh.
It is designed to keep track of the position of the cursor
while handling a split window of vt100 emulation.
All of the routines assume that there is no other output
going onto the standard output screen, to mess it up.
Many thanks to Matt Ostanik who wrote the ANSI Handbook.
*/
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_TERMIO_H
#include <termio.h> /* Used only for TIOCGWINSZ */
#elif defined(HAVE_TERMIOS_H)
#include <termios.h>
#else /* !HAVE_TERMIO_H && !HAVE_TERMIOS_H */
#include <sys/ioctl.h> /* Used only for TIOCGWINSZ */
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "vt100.h"
#include "video.h"
#include "terminal.h"
#include "splitvt.h"
#define SEP_CHAR ' ' /* Separator bar character */
int TABSTOP=8; /* The default tabstop value */
/* Two virtual windows + a pointer to the current one */
static window upper, lower;
window *curwin;
/* One physical window */
struct physical physical;
static char terminal_type[BUFSIZ]; /* Our terminal type */
static char *sep; /* The window separator string */
/* The various output processing functions, based on state */
void scan_for_esc(int c, int *source);
void E_(int c, int *source), E_brac(int c, int *source), E_brac_Q(int c, int *source),
E_lparen(int c, int *source), E_rparen(int c, int *source), E_pound(int c, int *source);
/* Make these four variables accessable to the calling program */
int UU_lines=0; /* The user requested lines for the upper window */
int WU_lines=0; /* The number of lines for the upper window */
int WL_lines=0; /* The number of lines for the lower window */
int W_columns=0; /* The number of columns per window */
static int LU_lines; /* A local copy of UU_lines that is modified */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Reset the escape scanning sequence: */
static void reset_escape(window *win)
{
int i;
win->process_char=scan_for_esc;
for ( i=0; i<MAX_PARAMS; ++i )
win->esc_param[i]=0;
win->param_idx=0;
win->cur_param=(&win->esc_param[win->param_idx]);
}
static void reset_esc(void)
{
reset_escape(curwin);
}
/* Initialize a window structure: */
static void vt_resetwin(window *win)
{
win->cursor.x=1;
win->cursor.y=1;
win->saved_cursor.x=1;
win->saved_cursor.y=1;
win->cols=physical.cols;
win->scr_upper=1;
win->scr_lower=win->rows;
win->saved_cursor.x=0;
win->saved_cursor.y=0;
win->key_state=normal;
win->textattr=NORMAL;
win->saved_textattr=NORMAL;
win->charset[G0]=US_CHARSET;
win->charset[G1]=US_CHARSET;
reset_escape(win);
}
/* Check to make sure the window cursor values are sane. */
static void vt_checkwin(window *win)
{
if ( win->cursor.x > win->rows )
win->cursor.x=win->rows;
if ( win->cursor.y > win->cols )
win->cursor.y=win->cols;
if ( win->scr_lower > win->rows )
win->scr_lower=win->rows;
}
/* Set the current window: */
static int lastwin = (-1);
void set_win(int which)
{
window *other;
int i;
/* Are we already in position? */
if ( which == lastwin )
return;
/* Set the current window and move the cursor into position */
curwin = physical.subwins[which];
other = physical.subwins[!which];
vt_setscroll(curwin->scr_upper+curwin->row_offset,
curwin->scr_lower+curwin->row_offset);
vt_goto(curwin->cursor.x+curwin->row_offset, curwin->cursor.y);
/* Make sure the terminal is in the current window's state */
for ( i=0; i<NCHARSETS; ++i ) {
if ( curwin->charset[i] != other->charset[i] )
vt_altcharset(i, curwin->charset[i]);
}
if ( curwin->key_state != other->key_state )
vt_keystate(curwin->key_state);
if ( curwin->textattr != other->textattr )
vt_setattr((int)curwin->textattr);
vt_update();
lastwin=which;
}
/* Set the terminal attributes to those of the specified window */
/* This must be called _after_ vt_restcursor(), or it won't work */
static void set_attr(window *win)
{
unsigned char on=NORMAL;
vt_resetattr();
(void) check_attr(0, win->textattr, on);
}
/* Process the ^[[X;Xm escape. Made into a separate routine to support
ansi color. */
static void process_m(window *win, int n)
{
switch (n) {
case 0: /* Turn all attributes off */
win->textattr=NORMAL;
vt_resetattr();
break;
case 1: /* Turn on bold */
win->textattr |= BOLD;
vt_bold(1);
break;
case 2: /* Half brightness */
/* UNSUPPORTED */
break;
case 4: /* Turn on underlining */
win->textattr |= UNDERLINE;
vt_underline(1);
break;
case 5: /* Turn on blinking */
win->textattr |= BLINK;
vt_blink(1);
break;
case 7: /* Turn on reverse */
win->textattr |= REVERSE;
vt_reverse(1);
break;
case 21: /* Normal brightness */
/* UNSUPPORTED */
break;
case 22: /* Turn off bold */
win->textattr &= ~BOLD;
vt_bold(0);
break;
case 24: /* Turn off underlining */
win->textattr &= ~UNDERLINE;
vt_underline(0);
break;
case 25: /* Turn off blinking */
win->textattr &= ~BLINK;
vt_blink(0);
break;
case 27: /* Turn off reverse */
win->textattr &= ~REVERSE;
vt_reverse(0);
break;
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37: /* Set foreground color */
vt_setfg(n-30);
break;
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47: /* Set background color */
vt_setbg(n-40);
break;
default: /* Unknown escape */
break;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void scan_for_esc(int c, int *source)
{
int i;
switch (c) {
/* Taken from vt102.codes */
case '\000': /* NULL (fill character) */
case '\003': /* EXT (half duplex turnaround) */
case '\004': /* EOT (can be disconnect char) */
case '\005': /* ENQ (generate answerback) */
printf("%c", c);
break;
case '\007': /* BEL (sound terminal bell) */
vt_bell();
break;
case '\b': /* Backspace; move left one character */
if ( curwin->cursor.y != 1 ) {
--curwin->cursor.y;
vt_left(curwin->esc_param[0]);
}
break;
case '\t': /* Tab. Handle with direct motion (Buggy) */
i=curwin->cursor.y;
do {
++curwin->cursor.y;
} while ( !curwin->tabstops[curwin->cursor.y-1]
&& (curwin->cursor.y < curwin->cols) );
vt_right(curwin->cursor.y - i);
break;
case '\013': /* Processed as linefeeds */
case '\014': /* Don't let the cursor move below window or scrolling region */
case '\n': if ( curwin->cursor.x < curwin->scr_lower )
++curwin->cursor.x;
else
scroll_video(curwin, 1);
printf("\n");
break;
case '\r': /* Move cursor to left margin */
curwin->cursor.y = 1;
printf("\r");
break;
case '\016': /* S0 (selects G1 charset) */
case '\017': /* S1 (selects G0 charset) */
case '\021': /* XON (continue transmission) */
case '\022': /* XOFF (stop transmission) */
printf("%c", c);
break;
case '\030': /* Processed as escape cancel */
case '\032': reset_esc();
break;
case '\033': curwin->process_char=E_;
break;
default: if ( curwin->cursor.y > curwin->cols )
{ /* Wrap */
if (curwin->cursor.x<curwin->scr_lower)
{
++curwin->cursor.x;
} else
scroll_video(curwin, 1);
printf("\r\n");
curwin->cursor.y=1;
}
add_video(curwin, c); printf("%c", c);
++curwin->cursor.y;
break;
}
return;
}
void E_(int c, int *source)
{
/* Return inside the switch to prevent reset_esc() */
switch (c) {
case '\030': /* Processed as escape cancel */
case '\032': reset_esc();
return;
case '[': curwin->process_char=E_brac;
return;
case '(': curwin->process_char=E_lparen;
return;
case ')': curwin->process_char=E_rparen;
return;
case '#': curwin->process_char=E_pound;
return;
case 'D': /* Cursor down with scroll up at margin */
if ( curwin->cursor.x < curwin->scr_lower ) {
++curwin->cursor.x;
} else
scroll_video(curwin, 1);
printf("\n");
break;
case 'M': /* Reverse scroll (move up; scroll at top) */
if ( (curwin->cursor.x > curwin->scr_upper) )
--curwin->cursor.x;
else
revscroll_video(curwin, 1);
vt_revscroll();
break;
case 'E': /* Next line (CR-LF) */
if ( curwin->cursor.x < curwin->scr_lower )
++curwin->cursor.x;
else
scroll_video(curwin, 1);
curwin->cursor.y = 1;
printf("\r\n");
break;
case '7': /* Save cursor and attribute */
curwin->saved_cursor=curwin->cursor;
curwin->saved_textattr=curwin->textattr;
break;
case '8': /* Restore saved cursor and attribute */
curwin->cursor=curwin->saved_cursor;
if ( curwin->cursor.x > curwin->rows )
curwin->cursor.x = curwin->rows;
if ( curwin->cursor.y > curwin->cols )
curwin->cursor.y = curwin->cols;
vt_goto(curwin->cursor.x+curwin->row_offset,
curwin->cursor.y);
curwin->textattr=curwin->saved_textattr;
set_attr(curwin);
break;
case '=': /* Set application keypad mode */
curwin->key_state=application;
vt_keystate(curwin->key_state);
break;
case '>': /* Set numeric keypad mode */
curwin->key_state=normal;
vt_keystate(curwin->key_state);
break;
case 'N': /* Select charset G2 for one character */
/* UNSUPPORTED */
break;
case 'O': /* Select charset G3 for one character */
/* UNSUPPORTED */
break;
case 'H': /* Set horizontal tab */
curwin->tabstops[curwin->cursor.y-1]=1;
break;
case 'Z': /* Request terminal identification string */
/* Respond with "I am a vt102" */
write(*source, "\033[?6c", 5);
break;
case 'c': /* Terminal reset */
vt_resetwin(curwin);
break;
default: /* Unrecognized escape: ignore */
break;
}
reset_esc();
}
void E_brac(int c, int *source)
{
int newx, newy, i;
char reply[128];
/* Check for numeric argument first */
if ( isdigit(c) ) {
*curwin->cur_param *= 10;
*curwin->cur_param += (c-'0');
return;
}
/* Return inside the switch to prevent reset_esc() */
switch (c) {
case '\030': /* Processed as escape cancel */
case '\032': reset_esc();
return;
case '?': /* Format should be \E[?<n> */
if ( *curwin->cur_param )
reset_esc();
else
curwin->process_char=E_brac_Q;
return;
case ';': if ( ++curwin->param_idx < MAX_PARAMS ) {
curwin->cur_param =
&curwin->esc_param[curwin->param_idx];
}
return;
case 'h': /* Set modes */
switch (curwin->esc_param[0]) {
case 2: /* Lock keyboard */
/* UNSUPPORTED */
case 4: /* Character insert mode */
/* UNSUPPORTED */
case 12: /* Local echo on */
/* UNSUPPORTED (easily supported) */
case 20: /* <Return> = CR */
/* UNSUPPORTED (easily supported) */
default: break;
}
break;
case 'l': /* Reset modes */
switch (curwin->esc_param[0]) {
case 2: /* Unlock keyboard */
/* UNSUPPORTED */
case 4: /* Character overstrike mode */
/* UNSUPPORTED */
case 12: /* Local echo off */
/* UNSUPPORTED (easily supported) */
case 20: /* <Return> = CR-LF */
/* UNSUPPORTED (easily supported) */
default: break;
}
break;
case 'r': /* Set scroll region */
if ( ! curwin->esc_param[0] && ! curwin->esc_param[1] ) {
curwin->scr_upper=1;
curwin->scr_lower=curwin->rows;
} else {
/* Check parameters: VERY important. :) */
if (curwin->esc_param[0] < 1) /* Not needed */
curwin->scr_upper=1;
else
curwin->scr_upper=curwin->esc_param[0];
if ( curwin->esc_param[1] > curwin->rows )
curwin->scr_lower=curwin->rows;
else
curwin->scr_lower=curwin->esc_param[1];
if ( curwin->scr_upper > curwin->scr_lower ) {
/* Reset scroll region */
curwin->scr_upper=1;
curwin->scr_lower=curwin->rows;
}
}
curwin->cursor.x=1;
curwin->cursor.y=1;
vt_setscroll(curwin->scr_upper+curwin->row_offset,
curwin->scr_lower+curwin->row_offset);
vt_goto(curwin->cursor.x+curwin->row_offset, 1);
break;
case 'A': /* Cursor UP */
if ( curwin->cursor.x == curwin->scr_upper )
break;
if ( ! curwin->esc_param[0] )
curwin->esc_param[0]=1;
newx = (curwin->cursor.x - curwin->esc_param[0]);
if ( newx > curwin->scr_upper ) {
curwin->cursor.x=newx;
vt_up(curwin->esc_param[0]);
} else {
curwin->cursor.x=curwin->scr_upper;
vt_goto(curwin->cursor.x+curwin->row_offset,
curwin->cursor.y);
}
break;
case 'B': /* Cursor DOWN */
if ( curwin->cursor.x == curwin->scr_lower )
break;
if ( ! curwin->esc_param[0] )
curwin->esc_param[0]=1;
newx = (curwin->cursor.x + curwin->esc_param[0]);
if ( newx <= curwin->scr_lower ) {
curwin->cursor.x=newx;
vt_down(curwin->esc_param[0]);
} else {
curwin->cursor.x=curwin->scr_lower;
vt_goto(curwin->cursor.x+curwin->row_offset,
curwin->cursor.y);
}
break;
case 'C': /* Cursor RIGHT */
if ( curwin->cursor.y == curwin->cols )
break;
if ( ! curwin->esc_param[0] )
curwin->esc_param[0]=1;
newy = (curwin->cursor.y + curwin->esc_param[0]);
if ( newy < curwin->cols ) {
curwin->cursor.y=newy;
vt_right(curwin->esc_param[0]);
} else {
curwin->cursor.y=curwin->cols;
vt_goto(curwin->cursor.x+curwin->row_offset,
curwin->cursor.y);
}
break;
case 'D': /* Cursor LEFT */
if ( curwin->cursor.y == 1 )
break;
if ( ! curwin->esc_param[0] )
curwin->esc_param[0]=1;
newy = (curwin->cursor.y - curwin->esc_param[0]);
if ( newy > 1 ) {
curwin->cursor.y=newy;
vt_left(curwin->esc_param[0]);
} else {
curwin->cursor.y=1;
printf("\r");
}
break;
case 'f':
case 'H': /* Move cursor to coordinates */
if ( ! curwin->esc_param[0] )
curwin->esc_param[0]=1;
if ( ! curwin->esc_param[1] )
curwin->esc_param[1]=1;
if ( (curwin->cursor.x=curwin->esc_param[0]) >
curwin->rows )
curwin->cursor.x=curwin->rows;
if ( (curwin->cursor.y=curwin->esc_param[1]) >
curwin->cols )
curwin->cursor.y=curwin->cols;
vt_goto(curwin->cursor.x+curwin->row_offset,
curwin->cursor.y);
break;
case 'g': /* Clear tabstops */
switch (curwin->esc_param[0]) {
case 0: /* Clear a tabstop */
curwin->tabstops[curwin->cursor.y-1]=0;
break;
case 3: /* Clear all tabstops */
for (newy=0; newy<curwin->cols; ++newy)
curwin->tabstops[newy]=0;
break;
default: break;
}
break;
case 'm': /* Set terminal attributes */
process_m(curwin, curwin->esc_param[0]);
for ( i=1; curwin->esc_param[i] && i<MAX_PARAMS; ++i )
process_m(curwin, curwin->esc_param[i]);
break;
case 'J': /* Clear screen */
switch (curwin->esc_param[0]) {
case 0: /* Clear from cursor down */
erase_video(curwin,
curwin->cursor.x, curwin->rows,
1, curwin->cols);
newx=curwin->cursor.x;
vt_savecursor();
printf("\r");
while ( newx++ < curwin->rows ) {
vt_clreol();
printf("\n");
}
vt_clreol();
vt_restcursor();
break;
case 1: /* Clear from cursor up */
erase_video(curwin,
1, curwin->cursor.x,
1, curwin->cols);
newx=curwin->cursor.x;
vt_savecursor();
printf("\r");
while ( --newx > 0 ) {
vt_clreol();
vt_up(1);
}
vt_clreol();
vt_restcursor();
break;
case 2: /* Clear whole screen */
erase_video(curwin,
1, curwin->rows,
1, curwin->cols);
vt_goto(curwin->row_offset+1, 1);
curwin->cursor.x=1;
curwin->cursor.y=1;
newx=curwin->cursor.x;
vt_savecursor();
printf("\r");
while ( newx++ < curwin->rows ) {
vt_clreol();
printf("\n");
}
vt_clreol();
vt_restcursor();
break;
default: break;
}
break;
case 'K': /* Clear line */
switch (curwin->esc_param[0]) {
case 0: /* Clear to end of line */
erase_video(curwin,
curwin->cursor.x, curwin->cursor.x,
curwin->cursor.y, curwin->cols);
vt_clreol();
break;
case 1: /* Clear to beginning of line */
erase_video(curwin,
curwin->cursor.x, curwin->cursor.x,
1, curwin->cursor.y);
vt_clrbgl();
break;
case 2: /* Clear whole line */
erase_video(curwin,
curwin->cursor.x, curwin->cursor.x,
1, curwin->cols);
vt_clrline();
break;
}
break;
case 'P': /* Delete under cursor */
erase_video(curwin,
curwin->cursor.x, curwin->cursor.x,
curwin->cursor.y, curwin->cursor.y);
vt_delunder(curwin->esc_param[0]);
break;
case 'M': /* Delete lines */
revscroll_video(curwin, 1);
vt_delline(curwin->esc_param[0]);
break;
case 'L': /* Insert lines */
vt_insline(curwin->esc_param[0]);
break;
case '@': /* Insert characters */
if ( ! curwin->esc_param[0] )
curwin->esc_param[0] = 1;
vt_insertchar(curwin->esc_param[0]);
rshift_video(curwin, curwin->esc_param[0]);
break;
case 'i': /* Printing (UNSUPPORTED) */
break;
case 'n': /* Device status request */
switch (curwin->esc_param[0]) {
case 5: /* Status report request */
/* Say we're just fine. */
write(*source, "\033[0n", 4);
break;
case 6: /* Cursor position request */
sprintf(reply, "\033[%d;%dR",
curwin->cursor.x,
curwin->cursor.y);
write(*source, reply, strlen(reply));
break;
}
break;
case 'c': /* Request terminal identification string */
/* Respond with "I am a vt102" */
write(*source, "\033[?6c", 5);
break;
default: /* Unrecognized escape: ignore */
break;
}
reset_esc();
}
void E_brac_Q(int c, int *source)
{
/* Check for numeric argument first */
if ( isdigit(c) ) {
*curwin->cur_param *= 10;
*curwin->cur_param += (c-'0');
return;
}
/* Return inside the switch to prevent reset_esc() */
switch (c) {
case '\030': /* Processed as escape cancel */
case '\032': reset_esc();
return;
case 'h': /* Set modes */
switch (curwin->esc_param[0]) {
case 1: /* Cursorkeys in application mode */
curwin->key_state=application;
vt_keystate(curwin->key_state);
break;
case 2: /* Set ansi mode */
/* UNSUPPORTED */
break;
case 3: /* 132 char/row */
if ( physical.cols != 132 ) {
physical.cols=132;
vt_widemode(1);
init_vt100(0);
}
break;
case 4: /* Set jump scroll */
/* UNSUPPORTED */
break;
case 5: /* Set reverse screen */
/* UNSUPPORTED */
break;
case 6: /* Set relative coordinates */
/* UNSUPPORTED */
break;
case 7: /* Set auto wrap on */
/* UNSUPPORTED */
break;
case 8: /* Set auto repeat on */
/* UNSUPPORTED */
break;
case 25: /* Set cursor on */
/* UNSUPPORTED */
break;
case 47: /* Switch to alternate buffer */
/* UNSUPPORTED (xterm sequence) */
break;
default:
/* UNSUPPORTED */
break;
}
break;
case 'l': /* Reset modes */
switch (curwin->esc_param[0]) {
case 1: /* Cursorkeys in normal mode */
curwin->key_state=normal;
vt_keystate(curwin->key_state);
break;
case 2: /* Set VT52 mode */
/* UNSUPPORTED */
break;
case 3: /* 80 char/row */
if ( physical.cols == 132 ) {
physical.cols = 80;
vt_widemode(0);
init_vt100(0);
}
break;
case 4: /* Set smooth scroll */
/* UNSUPPORTED */
break;
case 5: /* Set non-reversed (normal) screen */
/* UNSUPPORTED */
break;
case 6: /* Set absolute coordinates */
/* UNSUPPORTED */
break;
case 7: /* Set auto wrap off */
/* UNSUPPORTED */
break;
case 8: /* Set auto repeat off */
/* UNSUPPORTED */
break;
case 25: /* Set cursor off */
/* UNSUPPORTED */
break;
case 47: /* Switch from alternate buffer */
/* UNSUPPORTED (xterm sequence) */
break;
default:
/* UNSUPPORTED */
break;
}
break;
default: /* Unrecognized escape: ignore */
break;
}
reset_esc();
}
void E_lparen(int c, int *source)
{
/* Return inside the switch to prevent reset_esc() */
switch (c) {
case '\030': /* Processed as escape cancel */
case '\032': reset_esc();
return;
/* Select character sets */
case 'A': /* UK as G0 */
curwin->charset[G0]=UK_CHARSET;
vt_altcharset(G0, UK_CHARSET);
break;
case 'B': /* US as G0 */
curwin->charset[G0]=US_CHARSET;
vt_altcharset(G0, US_CHARSET);
break;
case '0': /* Special character set as G0 */
curwin->charset[G0]=GRAPHICS;
vt_altcharset(G0, GRAPHICS);
break;
case '1': /* Alternate ROM as G0 */
case '2': /* Alternate ROM special character set as G0 */
default: /* Unrecognized escape: ignore */
break;
}
reset_esc();
}
void E_rparen(int c, int *source)
{
/* Return inside the switch to prevent reset_esc() */
switch (c) {
case '\030': /* Processed as escape cancel */
case '\032': reset_esc();
return;
/* Select character sets */
case 'A': /* UK as G1 */
curwin->charset[G1]=UK_CHARSET;
vt_altcharset(G1, UK_CHARSET);
break;
case 'B': /* US as G1 */
curwin->charset[G1]=US_CHARSET;
vt_altcharset(G1, US_CHARSET);
break;
case '0': /* Special character set as G1 */
curwin->charset[G1]=GRAPHICS;
vt_altcharset(G1, GRAPHICS);
case '1': /* Alternate ROM as G1 */
case '2': /* Alternate ROM special character set as G1 */
default: /* Unrecognized escape: ignore */
break;
}
reset_esc();
}
void E_pound(int c, int *source)
{
switch (c) { /* Line attributes not supported */
case '3': /* Double height (top half) */
case '4': /* Double height (bottom half) */
case '5': /* Single width, single height */
case '6': /* Double width */
default:
reset_esc();
break;
}
return;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Routine to initialize the vt100 screening, returning an error message,
or NULL if all went well. */
static int setup_vt100 = 0; /* Have we initialized the vt100 system? */
char *init_vt100(int reread_tsize)
{
#ifdef TIOCGWINSZ
struct /* winsize */ {
unsigned short ws_row; /* rows, in characters */
unsigned short ws_col; /* columns, in characters */
unsigned short ws_xpixel; /* horizontal size - not used */
unsigned short ws_ypixel; /* vertical size - not used */
} mywinz;
#endif
int i, **videomem, oldrows = 0, newrows, newcols;
position newpos;
char *ptr, *errmesg;
/* Check to make sure it's okay to run */
if ( ! isatty(0) || ! isatty(1) )
return("Standard input and output must be a tty");
/* Initialize the termcap environment */
if ( ! setup_vt100 ) {
if ( (errmesg=vt_initterm(terminal_type,
&physical.rows, &physical.cols)) != NULL )
return(errmesg);
vt_initsel();
}
if (reread_tsize)
{
#ifdef TIOCGWINSZ
if ( ioctl(0, TIOCGWINSZ, &mywinz) == 0 ) {
if ( mywinz.ws_row )
physical.rows=mywinz.ws_row;
if ( mywinz.ws_col )
physical.cols=mywinz.ws_col;
}
#endif
if ( (ptr=(char *)getenv("LINES")) != NULL )
physical.rows=atoi(ptr);
if ( (ptr=(char *)getenv("COLUMNS")) != NULL )
physical.cols=atoi(ptr);
}
/* Now set defaults if we can't find the window size */
if ( ! physical.rows ) physical.rows=24;
if ( ! physical.cols ) physical.cols=80;
physical.subwins[UPPER] = &upper;
physical.subwins[LOWER] = &lower;
/* Check that each window is at least 3 lines tall */
if ( physical.rows < 7 )
return("Screen is not tall enough to split.");
if ( ! setup_vt100 ) {
/* If physical.cols has been set to 132, assume we are on a
vt100 wide terminal, and set 132 column mode. Note that
setting COLUMNS in the environment will override termcap */
if ( physical.cols == 132 )
vt_widemode(1);
}
/* Set the exportable variables */
if ( UU_lines ) {
/* Check the user set # of lines */
if ( UU_lines > (physical.rows-1-3+(2*force_height)) )
LU_lines=(physical.rows-1-3+(2*force_height));
else if ( UU_lines < 3 - (2*force_height))
LU_lines=3 - (2*force_height);
else
LU_lines=UU_lines;
WU_lines=LU_lines;
WL_lines=(physical.rows-1-LU_lines);
} else
WL_lines=WU_lines=((physical.rows-1)/2);
W_columns=physical.cols;
/* Set up the window structures */
newcols=physical.cols;
newrows=WU_lines;
if ( (videomem=alloc_video(newrows, newcols)) == NULL )
return("Out of memory");
if ( setup_vt100 ) {
oldrows=upper.rows;
copy_video(&upper, videomem, newrows, newcols, &newpos);
for ( i=0; i<upper.rows; ++i )
(void) free(upper.videomem[i]);
(void) free(upper.videomem);
(void) free(upper.tabstops);
}
if ( (upper.tabstops=(int *)malloc(newcols*sizeof(int))) == NULL )
return("Out of memory");
for ( i=0; i<newcols; ++i ) {
if ( (i%TABSTOP) == 0 )
upper.tabstops[i]=1;
else
upper.tabstops[i]=0;
}
upper.videomem=videomem;
upper.cols=physical.cols;
upper.rows=newrows;
upper.row_offset=0;
if ( setup_vt100 ) {
if ( upper.scr_lower == oldrows )
upper.scr_lower=newrows;
upper.cursor=newpos;
vt_checkwin(&upper);
} else
vt_resetwin(&upper);
newrows=WL_lines;
if ( (videomem=alloc_video(newrows, newcols)) == NULL )
return("Out of memory");
if ( setup_vt100 ) {
oldrows=lower.rows;
copy_video(&lower, videomem, newrows, newcols, &newpos);
for ( i=0; i<lower.rows; ++i )
(void) free(lower.videomem[i]);
(void) free(lower.videomem);
(void) free(lower.tabstops);
}
if ( (lower.tabstops=(int *)malloc(newcols*sizeof(int))) == NULL )
return("Out of memory");
for ( i=0; i<newcols; ++i ) {
if ( (i%TABSTOP) == 0 )
lower.tabstops[i]=1;
else
lower.tabstops[i]=0;
}
lower.videomem=videomem;
lower.cols=physical.cols;
lower.rows=newrows;
lower.row_offset=(upper.rows+1);
if ( setup_vt100 ) {
if ( lower.scr_lower == oldrows )
lower.scr_lower=newrows;
lower.cursor=newpos;
vt_checkwin(&lower);
} else
vt_resetwin(&lower);
/* Set up the separator (cols-1) */
if ( setup_vt100 )
(void) free(sep);
if ( (sep=(char *)malloc(physical.cols+1)) == NULL )
return("Out of memory");
for ( i=0; i<(physical.cols-1); ++i )
sep[i]=SEP_CHAR;
sep[i]='\0';
/* Clear the screen, and set up the windows */
if ( ! setup_vt100 ) {
curwin=(&upper);
vt_clrscr();
vt_goto((WU_lines+1), 1); /* Move past the top win */
vt_reverse(1);
printf("%s", sep); /* Print separator */
vt_resetattr();
vt_update();
lastwin=(-1);
} else
vt_redraw();
++setup_vt100; /* Set setup_vt100 flag */
return(NULL);
}
int vt_write(int win, char *data, int len, int *source)
/*int win; The window; 0 if top, 1 if bottom
char *data; The data to write
int len; The amount of data to write