-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIBMPC_C.C
274 lines (250 loc) · 5.66 KB
/
IBMPC_C.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
/* Copyright (c) 1990,1991,1992 Chris and John Downey */
#ifndef lint
static char *sccsid = "@(#)ibmpc_c.c 2.1 (Chris & John Downey) 7/29/92";
#endif
/***
* program name:
xvi
* function:
PD version of UNIX "vi" editor, with extensions.
* module name:
ibmpc_c.c
* module function:
C part of terminal interface module for IBM PC compatibles
running MS-DOS.
* 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"
/*
* Screen dimensions, defined here so they'll go in the C default data
* segment.
*/
unsigned int Rows;
unsigned int Columns;
/*
* IBM-compatible PC's have a default typeahead buffer which is only
* big enough for 16 characters, & some 8088-based PC's are so
* unbelievably slow that xvi can't handle more than about 2
* characters a second. So we do some input buffering here to
* alleviate the problem.
*/
static char kbuf[16];
static char *kbrp = kbuf;
static char *kbwp = kbuf;
static unsigned kbcount;
static void near
kbfill()
{
register int c;
while (kbcount < sizeof kbuf && (c = getchnw()) >= 0) {
kbcount++;
*kbwp = c;
if (kbwp++ >= &kbuf[sizeof kbuf - 1])
kbwp = kbuf;
}
}
static unsigned char near
kbget()
{
for (;;) {
if (kbcount > 0) {
unsigned char c;
--kbcount;
c = *kbrp;
if (kbrp++ >= &kbuf[sizeof kbuf - 1])
kbrp = kbuf;
return c;
} else {
kbfill();
}
}
}
/*
* Convert milliseconds to clock ticks.
*/
#if CLK_TCK > 1000
# define MS2CLK(m) ((long)(m) * (CLK_TCK / 1000))
#else
# if CLK_TCK < 1000
# define MS2CLK(m) ((long)(m) / (1000 / CLK_TCK))
# else
# define MS2CLK(m) (m)
# endif
#endif
/*
* inchar() - get a character from the keyboard
*/
int
inchar(long mstimeout)
{
clock_t stoptime;
if (kbcount == 0) {
flush_output();
if (mstimeout != 0) {
stoptime = clock() + MS2CLK(mstimeout);
}
kbfill();
}
for (;;) {
static clock_t lastevent;
register int c;
if (kbcount == 0) {
unsigned prevbstate;
unsigned prevx, prevy;
bool_t isdrag;
if (State == NORMAL) {
showmouse();
prevbstate = 0;
}
for (; kbcount == 0; kbfill()) {
/*
* Time out if we have to.
*/
if (mstimeout != 0 && clock() > stoptime) {
break;
}
if (State == NORMAL) {
unsigned buttonstate;
unsigned mousex,
mousey;
/*
* If there's no keyboard input waiting to be
* read, watch out for mouse events. We don't do
* this if we're in insert or command line mode.
*/
buttonstate = mousestatus(&mousex, &mousey) & 7;
mousex /= 8;
mousey /= 8;
if (prevbstate == 0) {
isdrag = FALSE;
} else {
if (buttonstate) {
/*
* If a button is being held down, & the
* position has changed, this is a mouse
* drag event.
*/
if (mousex != prevx || mousey != prevy) {
hidemouse();
mousedrag(prevy, mousey, prevx, mousex);
showmouse();
isdrag = TRUE;
}
} else {
if (!isdrag) {
/*
* They've pressed & released a button
* without moving the mouse.
*/
hidemouse();
mouseclick(mousey, mousex);
showmouse();
}
}
}
if ((prevbstate = buttonstate) != 0) {
prevx = mousex;
prevy = mousey;
}
}
}
if (State == NORMAL) {
hidemouse();
}
if (kbcount == 0) {
/*
* We must have timed out.
*/
return EOF;
}
}
c = kbget();
/*
* On IBM compatible PC's, function keys return '\0' followed
* by another character. Check for this, and turn function key
* presses into appropriate "normal" characters to do the
* right thing in xvi.
*/
if (c != '\0') {
return(c);
}
/* else must be a function key press */
{
if (State != NORMAL) {
/*
* Function key pressed during insert or command line
* mode. Get the next character ...
*/
if (kbget() == 0x53) {
/*
* ... and if it's the delete key, return it as a
* backspace ...
*/
return '\b';
}
/*
* ... otherwise it isn't valid ...
*/
alert();
/*
* Typical MS-DOS users are fairly naive & may not
* understand how to get out of insert mode. To make
* things easier, we do it for them here.
*/
switch (State) {
case INSERT:
case REPLACE:
return ESC;
default:
continue;
}
}
/* else (State == NORMAL) ... */
switch (kbget()) {
case 0x3b: return(K_HELP); /* F1 key */
case 0x47: return('H'); /* home key */
case 0x48: return('k'); /* up arrow key */
case 0x49: return (CTRL('B')); /* page up key */
case 0x4b: return('\b'); /* left arrow key */
case 0x4d: return(' '); /* right arrow key */
case 0x4f: return('L'); /* end key */
case 0x50: return('j'); /* down arrow key */
case 0x51: return (CTRL('F')); /* page down key */
case 0x52: return('i'); /* insert key */
case 0x53: return('x'); /* delete key */
/*
* default:
* fall through and ignore both characters ...
*/
}
continue;
}
}
}
#ifdef __ZTC__
# ifdef DOS386
# define Z386
# endif
#endif
#ifndef Z386
/*
* The routines in ibmpc_a.asm need to call this because they can't
* invoke C macros directly.
*
* Return Pn(P_colour) in the al register, Pn(P_statuscolour) in ah, &
* Pb(P_vbell) in dx.
*
* This will only work with a Microsoft-compatible compiler.
*/
long
cparams()
{
return ((long) Pb(P_vbell) << 16) |
(unsigned short) ((Pn(P_statuscolour) << 8) |
(unsigned char) Pn(P_colour));
}
#endif /* not Z386 */