File: vmsvt.c

package info (click to toggle)
vile 9.8za-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,644 kB
  • sloc: ansic: 120,894; lex: 14,981; sh: 4,478; perl: 3,511; cpp: 3,180; makefile: 1,425; awk: 271
file content (913 lines) | stat: -rw-r--r-- 23,229 bytes parent folder | download | duplicates (2)
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
/*
 * VMS terminal driver
 *
 * Uses SMG to lookup capabilities of the terminal (see SYS$SYSTEM:SMGTERMS.TXT
 * and SYS$SYSTEM:TERMTABLE.TXT)
 *
 * Originally written by Curtis Smith in 1987, this module has been rewritten
 * by Tom Dickey and Clark Morgan to:
 *
 *   -- simplify it,
 *   -- extend it to support a wider variety of terminal types,
 *   -- add VT220 keyboard mappings,
 *   -- support syntax highlighting,
 *   -- support wide and narrow screen resolutions,
 *   -- support visual bells.
 *
 * $Id: vmsvt.c,v 1.69 2010/02/14 18:43:48 tom Exp $
 *
 */

#include	"estruct.h"	/* Emacs' structures            */
#include	"edef.h"	/* Emacs' definitions           */
#include	"nefunc.h"	/* f_backchar_to_bol            */

#if DISP_VMSVT

#include	<descrip.h>	/* Descriptor definitions       */
#include	<iodef.h>	/* to get IO$_SENSEMODE         */
#include	<ttdef.h>	/* to get TT$_UNKNOWN           */
#include	<stsdef.h>	/* to get $VMS_STATUS_SUCCESS   */

#include	<starlet.h>
#include	<smgtrmptr.h>	/* to get SMG$K_??? definitions */
#include	<smg$routines.h>
#include	<ssdef.h>

#include	<descrip.h>
#include	<iodef.h>
#include	<ttdef.h>
#include	<tt2def.h>

/* function prototypes needed for the dispatch table */
static OUTC_DCL vmsvt_putc(int c);
static int vmsvt_typahead(void);
static void vmsvt_flush(void);
static void vmsvt_scrollregion(int top, int bot);
static void vmsvt_scroll_reg(int from, int to, int n);
static void vmsvt_scroll_delins(int from, int to, int n);

/* SMG stuff (just like termcap) */
static int already_open;
static int termtype;
static char *tc_SO;		/* begin standout (reverse) */
static char *tc_SE;		/* end standout (reverse) */
static char *erase_to_end_line;
static char *erase_whole_display;

static char *delete_line;
static char *insert_line;
static char *scroll_forw;
static char *scroll_back;
static char *scroll_regn;

static char *set_narrow;
static char *set_wide;

static long wide_cols;
static long narrow_cols;

#if OPT_VIDEO_ATTRS
static char *tc_MD;		/* begin bold */
static char *tc_ME;		/* end bold */
static char *tc_US;		/* begin underscore */
static char *tc_UE;		/* end underscore */
#endif

#if OPT_FLASH
static char *dark_on;
static char *dark_off;
#endif
/* *INDENT-OFF* */
struct vmskeyseqs {
    char *seq;
    int code;
};
static struct vmskeyseqs vt100seqs[] =
{
    { "\033[A",   KEY_Up },
    { "\033[B",   KEY_Down },
    { "\033[C",   KEY_Right },
    { "\033[D",   KEY_Left },
    { "\033OP",   KEY_KP_F1 },
    { "\033OQ",   KEY_KP_F2 },
    { "\033OR",   KEY_KP_F3 },
    { "\033OS",   KEY_KP_F4 },
    /* these are transmitted by xterm and similar programs */
    { "\033[11~", KEY_F1 },
    { "\033[12~", KEY_F2 },
    { "\033[13~", KEY_F3 },
    { "\033[14~", KEY_F4 },
    { "\033[15~", KEY_F5 },
    /* vt220 keys (F1-F5 do not transmit control sequences) include vt100 */
    { "\033[17~", KEY_F6 },
    { "\033[18~", KEY_F7 },
    { "\033[19~", KEY_F8 },
    { "\033[20~", KEY_F9 },
    { "\033[21~", KEY_F10 },
    { "\033[23~", KEY_F11 },
    { "\033[24~", KEY_F12 },
    { "\033[25~", KEY_F13 },
    { "\033[26~", KEY_F14 },
    { "\033[28~", KEY_F15 },
    { "\033[29~", KEY_F16 },
    { "\033[31~", KEY_F17 },
    { "\033[32~", KEY_F18 },
    { "\033[33~", KEY_F19 },
    { "\033[34~", KEY_F20 },
    { "\033[3~",  KEY_Delete },	/* aka "Remove" */
    { "\033[6~",  KEY_Next },
    { "\033[5~",  KEY_Prior },	/* aka "Prev" */
    { "\033[1~",  KEY_Find },
    { "\033[2~",  KEY_Insert },
    { "\033[4~",  KEY_Select },
    /* 8-bit codes are the same, substituting the first two characters */
    { "\233A",    KEY_Up },
    { "\233B",    KEY_Down },
    { "\233C",    KEY_Right },
    { "\233D",    KEY_Left },
    { "\217P",    KEY_KP_F1 },
    { "\217Q",    KEY_KP_F2 },
    { "\217R",    KEY_KP_F3 },
    { "\217S",    KEY_KP_F4 },
    /* these are transmitted by xterm */
    { "\233[11~", KEY_F1 },
    { "\233[12~", KEY_F2 },
    { "\233[13~", KEY_F3 },
    { "\233[14~", KEY_F4 },
    { "\233[15~", KEY_F5 },
    /* vt220 keys (F1-F5 do not transmit control sequences) include vt100 */
    { "\23317~",  KEY_F6 },
    { "\23318~",  KEY_F7 },
    { "\23319~",  KEY_F8 },
    { "\23320~",  KEY_F9 },
    { "\23321~",  KEY_F10 },
    { "\23323~",  KEY_F11 },
    { "\23324~",  KEY_F12 },
    { "\23325~",  KEY_F13 },
    { "\23326~",  KEY_F14 },
    { "\23328~",  KEY_F15 },
    { "\23329~",  KEY_F16 },
    { "\23331~",  KEY_F17 },
    { "\23332~",  KEY_F18 },
    { "\23333~",  KEY_F19 },
    { "\23334~",  KEY_F20 },
    { "\2333~",   KEY_Delete },	/* aka "Remove" */
    { "\2336~",   KEY_Next },
    { "\2335~",   KEY_Prior },	/* aka "Prev" */
    { "\2331~",   KEY_Find },
    { "\2332~",   KEY_Insert },
    { "\2334~",   KEY_Select },
};
static struct vmskeyseqs vt52seqs[] =
{
    { "\33A",     KEY_Up },
    { "\33B",     KEY_Down },
    { "\33C",     KEY_Right },
    { "\33D",     KEY_Left },
    { "\33P",     KEY_KP_F1 },
    { "\33Q",     KEY_KP_F2 },
    { "\33R",     KEY_KP_F3 },
    { "\33S",     KEY_KP_F4 },
};
/* *INDENT-ON* */

typedef struct {
    USHORT status;		/* I/O completion status */
    USHORT count;		/* byte transfer count   */
    int dev_dep_data;		/* device-dependent data */
} QIO_SB;			/* This is a QIO I/O Status Block */

#define NIBUF	1024		/* Input buffer size            */
#define NOBUF	1024		/* MM says big buffers win!     */
#define EFN	0		/* Event flag                   */

static char obuf[NOBUF];	/* Output buffer                */
static int nobuf;		/* # of bytes in above          */

static char ibuf[NIBUF];	/* Input buffer                 */
static int nibuf;		/* # of bytes in above          */

static int ibufi;		/* Read index                   */

/*
 * Public data also used by spawn.c
 */
int oldmode[3];			/* Old TTY mode bits            */
int newmode[3];			/* New TTY mode bits            */
short iochan;			/* TTY I/O channel              */

/***
 *  Send a string to vmsvt_putc
 *
 *  Nothing returned
 ***/
static void
vmsvt_puts(char *string)	/* String to write              */
{
    if (string)
	while (*string != EOS)
	    vmsvt_putc(*string++);
}

/***
 *  2-argument request, e.g., for cursor movement.
 *
 *  Nothing returned
 ***/
static void
vmsvt_tgoto(request_code, parm1, parm2)
{
    char buffer[32];
    int ret_length;
    static int max_buffer_length = sizeof(buffer);
    static int arg_list[3] =
    {2};

    register int i;

    if (!already_open) {
	printf("\n");
	return;
    }

    /* Set the arguments into the arg_list array
     */
    arg_list[1] = parm1;
    arg_list[2] = parm2;

    if (!$VMS_STATUS_SUCCESS(smg$get_term_data(&termtype,
					       &request_code,
					       &max_buffer_length,
					       &ret_length,
					       buffer,
					       arg_list))) {
	vmsvt_puts("OOPS");
	return;
    } else if (ret_length > 0) {
	char *cp = buffer;
	while (ret_length-- > 0)
	    vmsvt_putc(*cp++);
    }
}

static void
vmsvt_move(int row, int col)
{
    vmsvt_tgoto(SMG$K_SET_CURSOR_ABS, row + 1, col + 1);
}

#if OPT_VIDEO_ATTRS
static void
vmsvt_attr(UINT attr)
{
#define VA_SGR (VASEL|VAREV|VAUL|VAITAL|VABOLD)
    /* *INDENT-OFF* */
    static const struct {
	char	**start;
	char	**end;
	UINT	mask;
    } tbl[] = {
	{ &tc_SO, &tc_SE, VASEL|VAREV },
	{ &tc_US, &tc_UE, VAUL },
	{ &tc_US, &tc_UE, VAITAL },
	{ &tc_MD, &tc_ME, VABOLD },
    };
    /* *INDENT-ON* */

    static UINT last;

    attr = VATTRIB(attr);
    if (attr & VASPCOL) {
	attr = VCOLORATTR((VCOLORNUM(attr) & (NCOLORS - 1)));
    } else {
	attr &= ~(VAML | VAMLFOC);
    }

    if (attr != last) {
	register size_t n;
	register char *s;
	UINT diff = attr ^ last;
	int ends = FALSE;

	/* turn OFF old attributes */
	for (n = 0; n < TABLESIZE(tbl); n++) {
	    if ((tbl[n].mask & diff) != 0
		&& (tbl[n].mask & attr) == 0
		&& (s = *(tbl[n].end)) != 0) {
		vmsvt_puts(s);
#if OPT_COLOR
		if (!ends)	/* do this once */
		    reinitialize_colors();
#endif
		ends = TRUE;
		diff &= ~(tbl[n].mask);
	    }
	}

	/* turn ON new attributes */
	for (n = 0; n < TABLESIZE(tbl); n++) {
	    if ((tbl[n].mask & diff) != 0
		&& (tbl[n].mask & attr) != 0
		&& (s = *(tbl[n].start)) != 0) {
		vmsvt_puts(s);
		diff &= ~(tbl[n].mask);
	    }
	}

	if (tc_SO != 0 && tc_SE != 0) {
	    if (ends && (attr & (VAREV | VASEL))) {
		vmsvt_puts(tc_SO);
	    } else if (diff & VA_SGR) {		/* we didn't find it */
		vmsvt_puts(tc_SE);
	    }
	}
#if OPT_COLOR
	if (attr & VACOLOR)
	    tcapfcol(VCOLORNUM(attr));
	else if (given_fcolor != gfcolor)
	    tcapfcol(gfcolor);
#endif
	last = attr;
    }
}

#else /* highlighting is a minimum attribute */

/*
 * Change reverse video status.
 * FALSE = normal video, TRUE = reverse video
 */
static void
tcaprev(UINT state)
{
    static int revstate = -1;

    if (state == revstate)
	return;
    revstate = state;
    if (state)
	vmsvt_puts(tc_SO);
    else
	vmsvt_puts(tc_SE);
}

#endif /* OPT_VIDEO_ATTRS */

static void
vmsvt_eeol(void)
{
    vmsvt_puts(erase_to_end_line);
}

static void
vmsvt_eeop(void)
{
    vmsvt_puts(erase_whole_display);
}

static void
cache_capabilities(void)
{
    /* *INDENT-OFF* */
    static struct caps_struct {
	long  code;
	char **cap_string;
    } tbl[] = {
	{ SMG$K_BEGIN_REVERSE,	     &tc_SO },
	{ SMG$K_END_REVERSE,	     &tc_SE },
#if OPT_VIDEO_ATTRS
	{ SMG$K_BEGIN_BOLD,	     &tc_MD },
	{ SMG$K_END_BOLD,	     &tc_ME },
	{ SMG$K_BEGIN_UNDERSCORE,    &tc_US },
	{ SMG$K_END_UNDERSCORE,	     &tc_UE },
#endif
#if OPT_FLASH
	{ SMG$K_DARK_SCREEN,	     &dark_on },
	{ SMG$K_LIGHT_SCREEN,	     &dark_off },
#endif
	{ SMG$K_ERASE_TO_END_LINE,   &erase_to_end_line },
	{ SMG$K_ERASE_WHOLE_DISPLAY, &erase_whole_display },
	{ SMG$K_INSERT_LINE,	     &insert_line },	/* al */
	{ SMG$K_DELETE_LINE,	     &delete_line },	/* dl */
	{ SMG$K_SCROLL_FORWARD,	     &scroll_forw },	/* SF */
	{ SMG$K_SCROLL_REVERSE,	     &scroll_back },	/* SR */
	{ SMG$K_SET_SCROLL_REGION,   &scroll_regn },	/* CS */
	{ SMG$K_WIDTH_NARROW,	     &set_narrow },	/* 80 columns */
	{ SMG$K_WIDTH_WIDE,	     &set_wide },	/* 132 columns */
    };
    /* *INDENT-ON* */

    char *buf;
    struct caps_struct *csp;
    int i, buf_len, ret_len;
    long rqst_code;
    static char storage[1024];

    for (i = 0, buf = storage, csp = tbl; i < TABLESIZE(tbl); i++, csp++) {
	buf_len = (storage + sizeof(storage)) - buf;
	if (!$VMS_STATUS_SUCCESS(smg$get_term_data(&termtype,
						   &csp->code,
						   &buf_len,
						   &ret_len,
						   buf)) || ret_len == 0) {
	    *(csp->cap_string) = NULL;
	} else {
	    buf[ret_len] = 0;
	    *(csp->cap_string) = buf;
	    buf += ret_len + 1;
	}
    }
    rqst_code = SMG$K_COLUMNS;
    if (!$VMS_STATUS_SUCCESS(smg$get_numeric_data(&termtype,
						  &rqst_code,
						  &narrow_cols))) {
	narrow_cols = 80;
    }
    rqst_code = SMG$K_WIDE_SCREEN_COLUMNS;
    if (!$VMS_STATUS_SUCCESS(smg$get_numeric_data(&termtype,
						  &rqst_code,
						  &wide_cols))) {
	wide_cols = 132;
    }
    term.maxcols = wide_cols;
}

/** I/O information block definitions **/
struct iosb {			/* I/O status block                     */
    short i_cond;		/* Condition value                      */
    short i_xfer;		/* Transfer count                       */
    long i_info;		/* Device information                   */
};
struct termchar {		/* Terminal characteristics             */
    char t_class;		/* Terminal class                       */
    char t_type;		/* Terminal type                        */
    short t_width;		/* Terminal width in characters         */
    long t_mandl;		/* Terminal's mode and length           */
    long t_extend;		/* Extended terminal characteristics    */
};
static struct termchar tc;	/* Terminal characteristics             */

static void
get_terminal_type(void)
{
    short fd;
    int status, deassign_status;
    struct iosb iostatus;
    $DESCRIPTOR(devnam, "SYS$COMMAND");

    /* Assign input to a channel */
    if (!$VMS_STATUS_SUCCESS(status = sys$assign(&devnam, &fd, 0, 0)))
	tidy_exit(status);

    /* Get terminal characteristics */
    status = sys$qiow(0,	/* Wait on event flag zero      */
		      fd,	/* Channel to input terminal    */
		      IO$_SENSEMODE,	/* Get current characteristic   */
		      &iostatus,	/* Status after operation       */
		      0, 0,	/* No AST service               */
		      &tc,	/* Terminal characteristics buf */
		      sizeof(tc),	/* Size of the buffer           */
		      0, 0, 0, 0);	/* P3-P6 unused                 */

    /* De-assign the input device */
    if (!$VMS_STATUS_SUCCESS(deassign_status = sys$dassgn(fd)))
	tidy_exit(deassign_status);

    /* Jump out if bad qiow status */
    if (!$VMS_STATUS_SUCCESS(status))
	tidy_exit(status);
    if ((iostatus.i_cond & 1) == 0)
	tidy_exit(iostatus.i_cond);
}

static void
vmsvt_open(void)
{
    QIO_SB iosb;
    int status;
    $DESCRIPTOR(odsc, "SYS$COMMAND");

    int i, keyseq_tablesize;
    struct vmskeyseqs *keyseqs;

    if (!already_open) {
	already_open = TRUE;

	get_terminal_type();
	if (tc.t_type == TT$_UNKNOWN) {
	    printf("Terminal type is unknown!\n");
	    printf("Set your terminal type using $ SET TERMINAL/INQUIRE\n");
	    tidy_exit(3);
	}
	if (tc.t_type != TT$_VT52) {
	    keyseqs = vt100seqs;
	    keyseq_tablesize = TABLESIZE(vt100seqs);
	} else {
	    keyseqs = vt52seqs;
	    keyseq_tablesize = TABLESIZE(vt52seqs);
	}

	/* Access the system terminal definition table for the          */
	/* information of the terminal type returned by IO$_SENSEMODE   */
	if (!$VMS_STATUS_SUCCESS(smg$init_term_table_by_type(&tc.t_type, &termtype)))
	    return;

	/* Set sizes */
	term.rows = ((UINT) tc.t_mandl >> 24);
	term.cols = tc.t_width;

	if (term.maxrows < term.rows)
	    term.maxrows = term.rows;

	if (term.maxcols < term.cols)
	    term.maxcols = term.cols;

	cache_capabilities();

	revexist = (tc_SO != NULL && tc_SE != NULL);
	eolexist = erase_whole_display != NULL;

	/*
	 * I tried 'vmsgetstr()' for a VT100 terminal and it had no codes
	 * for insert_line or for delete_line.  (Have to work up a test for
	 * that) - TD
	 */

	if (scroll_regn && scroll_back) {
	    if (scroll_forw == NULL)	/* assume '\n' scrolls forward */
		scroll_forw = "\n";
	    term.scroll = vmsvt_scroll_reg;
	} else if (delete_line && insert_line)
	    term.scroll = vmsvt_scroll_delins;
	else
	    term.scroll = nullterm_scroll;

	/* Set resolution */
	(void) strcpy(screen_desc,
		      (term.cols == narrow_cols)
		      ? "NORMAL"
		      : "WIDE");

	/* Open terminal I/O drivers */
	status = sys$assign(&odsc, &iochan, 0, 0);
	if (status != SS$_NORMAL)
	    tidy_exit(status);
	status = sys$qiow(EFN, iochan, IO$_SENSEMODE, &iosb, 0, 0,
			  oldmode, sizeof(oldmode), 0, 0, 0, 0);
	if (status != SS$_NORMAL
	    || iosb.status != SS$_NORMAL)
	    tidy_exit(status);
	newmode[0] = oldmode[0];
	newmode[1] = oldmode[1];
	newmode[1] |= TT$M_NOBRDCST;	/* turn on no-broadcast */
	newmode[1] &= ~TT$M_TTSYNC;
	newmode[1] &= ~TT$M_ESCAPE;	/* turn off escape-processing */
	newmode[1] &= ~TT$M_HOSTSYNC;
	newmode[1] &= ~TT$M_NOTYPEAHD;	/* turn off no-typeahead */
	newmode[2] = oldmode[2];
	newmode[2] |= TT2$M_PASTHRU;	/* turn on pass-through */
	newmode[2] |= TT2$M_ALTYPEAHD;	/* turn on big typeahead buffer */
	status = sys$qiow(EFN, iochan, IO$_SETMODE, &iosb, 0, 0,
			  newmode, sizeof(newmode), 0, 0, 0, 0);
	if (status != SS$_NORMAL
	    || iosb.status != SS$_NORMAL)
	    tidy_exit(status);
	term.rows = (newmode[1] >> 24);
	term.cols = newmode[0] >> 16;

	/* make sure backspace is bound to backspace */
	asciitbl[backspc] = &f_backchar_to_bol;

	/* Set predefined keys */
	for (i = keyseq_tablesize; i--;)
	    addtosysmap(keyseqs[i].seq, strlen(keyseqs[i].seq), keyseqs[i].code);
    }
}

/* copied/adapted from 'tcap.c' 19-apr-1993 dickey@software.org */

/* move howmany lines starting at from to to */
static void
vmsvt_scroll_reg(int from, int to, int n)
{
    int i;
    if (to == from)
	return;
    if (to < from) {
	vmsvt_scrollregion(to, from + n - 1);
	vmsvt_move(from + n - 1, 0);
	for (i = from - to; i > 0; i--)
	    vmsvt_puts(scroll_forw);
    } else {			/* from < to */
	vmsvt_scrollregion(from, to + n - 1);
	vmsvt_move(from, 0);
	for (i = to - from; i > 0; i--)
	    vmsvt_puts(scroll_back);
    }
    vmsvt_scrollregion(0, term.rows - 1);
}

/*
OPT_PRETTIER_SCROLL is prettier but slower -- it scrolls
		a line at a time instead of all at once.
*/

/* move howmany lines starting at from to to */
static void
vmsvt_scroll_delins(int from, int to, int n)
{
    int i;
    if (to == from)
	return;
    /* patch: should make this more like 'tcap.c', or merge logic somehow */
#if OPT_PRETTIER_SCROLL
    if (absol(from - to) > 1) {
	vmsvt_scroll_delins(from, (from < to) ? to - 1 : to + 1, n);
	if (from < to)
	    from = to - 1;
	else
	    from = to + 1;
    }
#endif
    if (to < from) {
	vmsvt_move(to, 0);
	for (i = from - to; i > 0; i--)
	    vmsvt_puts(delete_line);
	vmsvt_move(to + n, 0);
	for (i = from - to; i > 0; i--)
	    vmsvt_puts(insert_line);
    } else {
	vmsvt_move(from + n, 0);
	for (i = to - from; i > 0; i--)
	    vmsvt_puts(delete_line);
	vmsvt_move(from, 0);
	for (i = to - from; i > 0; i--)
	    vmsvt_puts(insert_line);
    }
}

/* cs is set up just like cm, so we use tgoto... */
static void
vmsvt_scrollregion(int top, int bot)
{
    vmsvt_tgoto(SMG$K_SET_SCROLL_REGION, top + 1, bot + 1);
}

/***
 *  Change screen resolution
 *
 *  support these values:   WIDE -> 132 columns, NORMAL -> 80 columns
 *
 *  T -> if resolution successfully changed, F otherwise.
 ***/
static int
vmsvt_cres(const char *res)
{
    char buf[NLINE];
    int rc = FALSE;

    if (tc.t_type == TT$_VT52) {
	mlforce("[sres not supported for VT52-style terminals]");
	return (rc);
    }

    strcpy(buf, res);
    mkupper(buf);
    if (strcmp(buf, "WIDE") == 0 && set_wide != 0) {
	vmsvt_puts(set_wide);
	term.cols = wide_cols;
	rc = TRUE;
    } else if (strcmp(buf, "NORMAL") == 0 && set_narrow != 0) {
	vmsvt_puts(set_narrow);
	term.cols = narrow_cols;
	rc = TRUE;
    } else
	mlforce("[invalid sres value (use NORMAL or WIDE)]");
    if (rc)
	newwidth(1, term.cols);
    return (rc);
}

static void
vmsvt_close(void)
{
    if (already_open) {
	/*
	 * Note: this code used to check for errors when closing the output,
	 * but it didn't work properly (left the screen set in 1-line mode)
	 * when I was running as system manager, so I took out the error
	 * checking -- T.Dickey 94/7/15.
	 */
	int status;
	QIO_SB iosb;

	if (tc.t_type != TT$_VT52) {
	    /*
	     * Restore terminal width to previous state (if necessary) and then
	     * cleanup as usual.
	     */
	    if (tc.t_width != term.cols)
		vmsvt_cres((tc.t_width == narrow_cols) ? "NORMAL" : "WIDE");
	}
	vmsvt_flush();
	status = sys$qiow(EFN, iochan, IO$_SETMODE, &iosb, 0, 0,
			  oldmode, sizeof(oldmode), 0, 0, 0, 0);
	if (status != SS$_IVCHAN)
	    (void) sys$dassgn(iochan);

	already_open = FALSE;
    }
}

/***
 *  Ring the bell
 *
 *  Nothing returned
 ***/
static void
vmsvt_beep(void)
{
#if OPT_FLASH
    int hit = 0;

    if (global_g_val(GMDFLASH) && dark_off != NULL && dark_on != NULL) {
	hit = 1;
	vmsvt_puts(dark_off);
	term.flush();
	catnap(200, FALSE);
	vmsvt_puts(dark_on);
    }
    if (!hit && tc.t_type != TT$_VT52) {
	/* *INDENT-OFF* */
	static char *seq[][2] = {
	    { NULL, NULL },		       /* vtflash = off */
	    { VTFLASH_NORMSEQ, VTFLASH_REVSEQ }, /* reverse */
	    { VTFLASH_REVSEQ, VTFLASH_NORMSEQ }, /* normal	*/
	};
	/* *INDENT-ON* */

	char *str1, *str2;
	int val;

	val = global_g_val(GVAL_VTFLASH);
	str1 = seq[val][0];
	if (str1) {
	    str2 = seq[val][1];
	    vmsvt_puts(str1);
	    term.flush();
	    catnap(200, FALSE);
	    vmsvt_puts(str2);
	    hit = 1;
	}
    }
    if (!hit)
#endif
	vmsvt_putc(BEL);
}

static void
read_vms_tty(int length)
{
    int status;
    QIO_SB iosb;
    int term[2] =
    {0, 0};
    unsigned mask = (IO$_READVBLK
		     | IO$M_NOECHO
		     | IO$M_NOFILTR
		     | IO$M_TRMNOECHO);

    status = sys$qiow(EFN, iochan,
		      ((length == 1)
		       ? mask
		       : mask | IO$M_TIMED),
		      &iosb, 0, 0, ibuf, length, 0, term, 0, 0);

    if (status != SS$_NORMAL)
	tidy_exit(status);
    if (iosb.status == SS$_ENDOFFILE)
	tidy_exit(status);

    nibuf = iosb.count;
    ibufi = 0;
}

/*
 * Read a character from the terminal, performing no editing and doing no echo
 * at all.  More complex in VMS than almost anyplace else, which figures.
 */
static int
vmsvt_getc(void)
{
    while (ibufi >= nibuf) {
	if (!vmsvt_typahead())
	    read_vms_tty(1);
    }
    return (ibuf[ibufi++] & 0xFF);	/* Allow multinational  */
}

/*
 * Write a character to the display. On VMS, terminal output is buffered, and
 * we just put the characters in the big array, after checking for overflow.
 */
static OUTC_DCL
vmsvt_putc(int c)
{
    if (nobuf >= NOBUF)
	vmsvt_flush();
    obuf[nobuf++] = c;
    OUTC_RET c;
}

static int
vmsvt_typahead(void)
{
    if (ibufi >= nibuf) {
	read_vms_tty(NIBUF);
	return (nibuf > 0);
    }
    return TRUE;
}

/*
 * Flush terminal buffer. Does real work where the terminal output is buffered
 * up. A no-operation on systems where byte at a time terminal I/O is done.
 */
static void
vmsvt_flush(void)
{
    QIO_SB iosb;

    if (nobuf != 0) {
	(void) sys$qiow(EFN, iochan, IO$_WRITELBLK | IO$M_NOFORMAT,
			&iosb, 0, 0, obuf, nobuf, 0, 0, 0, 0);
	nobuf = 0;
    }
}

static void
vmsvt_clean(int f)
{
    if (f)
	term.openup();

    term.flush();
    term.close();
    term.kclose();
}

static void
vmsvt_unclean(void)
{
}

/* Dispatch table. */
TERM term =
{
    24,				/* Max number of rows allowable */
				/* Filled in */ 0,
				/* Current number of rows used  */
    132,			/* Max number of columns        */
				/* Filled in */ 0,
				/* Current number of columns    */
    dumb_set_encoding,
    dumb_get_encoding,
    vmsvt_open,			/* Open terminal at the start   */
    vmsvt_close,		/* Close terminal at end        */
    nullterm_kopen,		/* Open keyboard                */
    nullterm_kclose,		/* Close keyboard               */
    vmsvt_clean,		/* cleanup keyboard             */
    vmsvt_unclean,		/* uncleanup keyboard           */
    nullterm_openup,
    vmsvt_getc,			/* Get character from keyboard  */
    vmsvt_putc,			/* Put character to display     */
    vmsvt_typahead,		/* char ready for reading       */
    vmsvt_flush,		/* Flush output buffers         */
    vmsvt_move,			/* Move cursor, origin 0        */
    vmsvt_eeol,			/* Erase to end of line         */
    vmsvt_eeop,			/* Erase to end of page         */
    vmsvt_beep,			/* Beep                         */
#if OPT_VIDEO_ATTRS
    vmsvt_attr,			/* Set attribute video state    */
#else
    vmsvt_rev,			/* Set reverse video state      */
#endif
    vmsvt_cres,			/* Change screen resolution     */
    nullterm_setfore,		/* N/A: Set foreground color    */
    nullterm_setback,		/* N/A: Set background color    */
    nullterm_setpal,		/* N/A: Set palette colors      */
    nullterm_setccol,
    nullterm_scroll,		/* set at init-time             */
    nullterm_pflush,
    nullterm_icursor,
    nullterm_settitle,
    nullterm_watchfd,
    nullterm_unwatchfd,
    nullterm_cursorvis,
    nullterm_mopen,
    nullterm_mclose,
    nullterm_mevent,
};

#endif