-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPC386.C
283 lines (254 loc) · 5.58 KB
/
PC386.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
/* Copyright (c) 1990,1991,1992 Chris and John Downey */
#ifndef lint
static char *sccsid = "@(#)pc386.c 2.1 (Chris & John Downey) 7/29/92";
#endif
/***
* program name:
xvi
* function:
PD version of UNIX "vi" editor, with extensions.
* module name:
pc386.c
* module function:
Routines for MS-DOS 386 protected mode version.
This file implements the same routines as ibmpc_a.asm &
msdos_a.asm, which are for the real mode version.
Zortech C++ version 3.0 or later & an appropriate DOS extender
package (such as PharLap's) are required.
* history:
STEVIE - ST Editor for VI Enthusiasts, Version 3.10
Originally by Tim Thompson (twitch!tjt)
Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
Heavily modified by Chris & John Downey
***/
#include "xvi.h"
#include <cerror.h>
#include <controlc.h>
/*
* Virtual mode handling.
*
* On EGA's & VGA's, the number of rows in text modes can vary because
* fonts with different sizes can be loaded, so we use virtual modes
* to contain the additional information needed. (Actually, we handle
* it a bit simplistically because the Zortech display library only
* knows about certain cases: 25 rows (default), 43 rows (EGA with
* 8 x 8 font) & 50 rows (VGA with 8 x 8 font). The last two are
* treated as equivalent.)
*/
#define MONO25X80 2
#define COLOUR25X80 3
#define MDA25X80 7
#define MASK43 043000
#define COLOUR43X80 (COLOUR25X80 | MASK43)
#define MONO43X80 (MONO25X80 | MASK43)
#define MDA43X80 (MDA25X80 | MASK43)
static unsigned
getvmode()
{
unsigned mode;
if ((mode = disp_getmode()) == COLOUR25X80 || mode == MONO25X80 ||
mode == MDA25X80) {
if (disp_numrows > 25) {
return mode | MASK43;
}
}
return mode;
}
static void
setvmode(unsigned vmode)
{
{
int mode;
if (disp_getmode() != (mode = vmode & ~MASK43)) {
disp_close();
disp_setmode(mode);
disp_open();
}
}
{
unsigned m43;
m43 = vmode & MASK43;
if (disp_ega && ((m43 && disp_numrows <= 25) ||
(!m43 && disp_numrows > 25))) {
disp_close();
if (m43) {
disp_set43();
} else {
disp_reset43();
}
disp_open();
}
}
}
static unsigned startmode; /* system virtual mode */
#if 0
/*
* Critical error handler.
*
* See notes in msdos_a.asm.
*/
static int _far _cdecl
criterr(int *pax, int *pdi)
{
*pax = ((_osmajor >= 3) ? 3 : 0);
}
#endif
/*
* Keyboard interrupt handler.
*/
static void _cdecl
inthandler(void)
{
kbdintr = 1;
#if 0
return(-1); /* don't chain to previously installed vector */
#endif
}
/*
* Install interrupt handlers.
*/
void
msdsignal(unsigned char *flagp)
{
_controlc_handler = inthandler;
controlc_open();
#if 0
_cerror_handler = criterr;
cerror_open();
int_intercept(0x23, inthandler, 0);
int_intercept(0x24, criterr, 0);
#endif
}
void
catch_signals(void)
{
/*
* Set console break flag so that we can be interrupted even
* when we're not waiting for console input.
*/
dos_set_ctrl_break(1);
}
/*
* Pointer to copy of previous screen.
*/
static unsigned short *oldscreen = NULL;
void
tty_open(unsigned *prows, unsigned *pcolumns)
{
if (!disp_inited) {
disp_open();
}
startmode = getvmode();
if (disp_base) {
/*
* Allocate storage for copy of previous screen
* (except bottom line).
*
* We don't do this if we're using BIOS calls
* (indicated by disp_base == 0) because the BIOS
* doesn't do it very well (it causes a lot of cursor
* flicker).
*/
oldscreen = (unsigned short *)
malloc((disp_numrows - 1) * disp_numcols *
sizeof(unsigned short));
}
*prows = disp_numrows;
*pcolumns = disp_numcols;
}
static enum {
m_INITIAL = 0,
m_SYS = 1,
m_VI = 2
} curmode;
/*
* Save screen contents & set up video state for editor.
*/
void
tty_startv(void)
{
switch (curmode) {
case m_VI:
/*
* We're already in vi mode.
*/
return;
case m_SYS:
/*
* This isn't the first call. Force display
* package's variables to be re-initialized.
*/
disp_close();
disp_open();
}
curmode = m_VI;
if (getvmode() != startmode) {
/*
* The display mode has changed; set it back to what we
* started with.
*/
setvmode(startmode);
}
if (oldscreen) {
/*
* Save screen contents (except bottom line) so they
* can be restored afterwards.
*/
disp_peekbox(oldscreen, 0, 0, disp_numrows - 2, disp_numcols - 1);
}
msm_init();
/*
* Apparently, the Microsoft mouse driver sometimes
* gets the number of screen rows wrong.
*/
msm_setareay(0, (disp_numrows - 1) * 8);
}
/*
* Restore video state to what it was when we started.
*
* tty_endv() can be called after it's been called already with no
* intervening tty_startv(), so we have to check.
*/
void
tty_endv(void)
{
if (curmode != m_VI) {
return;
}
/*
* Restore contents of screen.
*/
if (oldscreen != NULL) {
disp_pokebox(oldscreen, 0, 0, disp_numrows - 2, disp_numcols - 1);
}
disp_flush();
curmode = m_SYS;
msm_term();
}
#ifndef ABS
# define ABS(n) ((n) < 0 ? -(n) : (n))
#endif
void
pc_scroll(unsigned start, unsigned end, int nlines)
{
if (ABS(nlines) > end + 1 - start) {
nlines = 0;
}
disp_scroll(nlines, start, 0, end, disp_numcols - 1, Pn(P_colour));
}
/*
* Return a character from standard input if one is immediately
* available, otherwise -1.
*
* Don't do anything special if control-C is typed. Just return it.
*/
int
getchnw()
{
union REGS r;
r.h.ah = 6;
r.h.dl = 0xff;
intdos(&r, &r);
return (r.e.flags & 0x40) ? /* zero flag */
-1 : (unsigned char) r.h.al;
}