-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc.c
482 lines (402 loc) · 10.4 KB
/
pc.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
/* pc.c */
/* Author:
* Guntram Blohm
* Buchenstrasse 19
* 7904 Erbach, West Germany
* Tel. ++49-7305-6997
* sorry - no regular network connection
*/
/* This file implements the ibm pc bios interface. See IBM documentation
* for details.
* If TERM is set upon invocation of elvis, this code is ignored completely,
* and the standard termcap functions are used, thus, even not-so-close
* compatibles can run elvis. For close compatibles however, bios output
* is much faster (and permits reverse scrolling, adding and deleting lines,
* and much more ansi.sys isn't capable of). GB.
*/
/* The routines for setting raw mode were written by Dan Kegel. They were
* taken [with slight modifications] from the "raw.c" file that he distributes
* with his NANSI.SYS console driver. Email: [email protected]
*/
#include "config.h"
#include "vi.h"
#if MSDOS
#include <dos.h>
#if TURBOC
#include <conio.h>
#endif
static void video P_((int, int *, int *));
/* vmode contains the screen attribute index and is set by attrset.*/
int vmode;
/* The following array contains attribute definitions for
* color/monochrome attributes. Screen selects one of the sets.
* Maybe i'll put them into elvis options one day.
*/
static int screen;
static char attr[2][8] =
{
/* :se: :so: :VB: quit :ul: :as: popup visible */
{ 0x1f, 0x1d, 0x1e, 0x07, 0x1a, 0x1c, 0x2f, 0x3f}, /* color */
{ 0x07, 0x70, 0x0f, 0x07, 0x01, 0x0f, 0x70, 0x70}, /* mono */
};
/*
* bios interface functions for elvis - pc version
*/
int biosquit()
{
int cx = 1;
vmode = A_QUIT;
v_ce();
return TRUE;
}
/* This function changes the table of attribute bytes used during BIOS output.
*/
int bioscolor(mode, attrbyte)
int mode; /* e.g. A_NORMAL */
int attrbyte;/* color code, as a PC attribute byte */
{
attr[0][mode] = attrbyte;
return 0;
}
/* IOCTL GETBITS/SETBITS bits. */
#define DEVICE 0x80
#define RAW 0x20
/* IOCTL operations */
#define GETBITS 0
#define SETBITS 1
#define GETINSTATUS 6
/* DOS function numbers. */
#define BREAKCHECK 0x33
#define IOCTL 0x44
/* A nice way to call the DOS IOCTL function */
static int
elvis_ioctl(int handle, int mode, unsigned setvalue)
{
union REGS regs;
regs.h.ah = IOCTL;
regs.h.al = (char) mode;
regs.x.bx = handle;
regs.h.dl = (char) setvalue;
regs.h.dh = 0; /* Zero out dh */
intdos(®s, ®s);
return (regs.x.dx);
}
/*--------------------------------------------------------------------------
Call this routine to set or clear RAW mode for the device associated with
the given file.
Example: raw_set(1, TRUE);
--------------------------------------------------------------------------*/
void raw_set(fd, rawstate)
int fd;
int rawstate;
{
int bits;
bits = elvis_ioctl(fd, GETBITS, 0);
if (DEVICE & bits) {
if (rawstate)
bits |= RAW;
else
bits &= ~RAW;
(void) elvis_ioctl(fd, SETBITS, bits);
}
}
/* A nice way to call the DOS BREAKCHECK function */
static int breakctl(int mode, int setvalue)
{
union REGS regs;
regs.h.ah = BREAKCHECK;
regs.h.al = (char) mode;
regs.h.dl = (char) setvalue;
intdos(®s, ®s);
return (regs.x.dx & 0xff);
}
/*--------------------------------------------------------------------------
Call this routine to determine whether DOS is checking for break (Control-C)
before it executes any DOS function call.
Return value is FALSE if it only checks before console I/O function calls,
TRUE if it checks before any function call.
--------------------------------------------------------------------------*/
int break_get(void)
{
return ( 0 != breakctl(GETBITS, 0));
}
/*--------------------------------------------------------------------------
Call this routine with TRUE to tell DOS to check for break (Control-C)
before it executes any DOS function call.
Call this routine with FALSE to tell DOS to only check for break before
it executes console I/O function calls.
--------------------------------------------------------------------------*/
void break_set(check)
int check;
{
(void) breakctl(SETBITS, check);
}
/*--------------------------------------------------------------------------
One routine to set (or clear) raw mode on stdin and stdout,
clear (or restore) break checking, and turn off input buffering on stdin.
This is the most common configuration; under MS-DOS, since setting raw mode
on stdout sometimes sets it on stdin, it's best to set it on both & be done
with it.
--------------------------------------------------------------------------*/
void raw_set_stdio(rawstate)
int rawstate; /* TRUE -> set raw mode; FALSE -> clear raw mode */
{
static int was_break_checking = 0;
raw_set(0, rawstate);
raw_set(1, rawstate);
if (rawstate) {
was_break_checking = break_get();
break_set(0);
} else {
break_set(was_break_checking);
}
}
/* cursor up: determine current position, decrement row, set position */
void v_up()
{
int dx;
video(0x300,(int *)0,&dx);
dx-=0x100;
video(0x200,(int *)0,&dx);
}
#ifndef NO_CURSORSHAPE
/* cursor big: set begin scan to end scan - 4 */
void v_cb()
{
int cx;
video(0x300, &cx, (int *)0);
cx=((cx&0xff)|(((cx&0xff)-4)<<8));
video(0x100, &cx, (int *)0);
}
/* cursor small: set begin scan to end scan - 1 */
void v_cs()
{
int cx;
video(0x300, &cx, (int *)0);
cx=((cx&0xff)|(((cx&0xff)-1)<<8));
video(0x100, &cx, (int *)0);
}
#endif
/* clear to end: get cursor position and emit the aproppriate number
* of spaces, without moving cursor.
*/
void v_ce()
{
int cx, dx;
video(0x300,(int *)0,&dx);
cx=COLS-(dx&0xff);
video(0x920,&cx,(int *)0);
}
/* clear screen: clear all and set cursor home */
void v_cl()
{
int cx=0, dx=((LINES-1)<<8)+COLS-1;
video(0x0600,&cx,&dx);
dx=0;
video(0x0200,&cx,&dx);
}
/* clear to bottom: get position, clear to eol, clear next line to end */
void v_cd()
{
int cx, dx, dxtmp;
video(0x0300,(int *)0,&dx);
dxtmp=(dx&0xff00)|(COLS-1);
cx=dx;
video(0x0600,&cx,&dxtmp);
cx=(dx&0xff00)+0x100;
dx=((LINES-1)<<8)+COLS-1;
video(0x600,&cx,&dx);
}
/* add line: scroll rest of screen down */
void v_al()
{
int cx,dx;
video(0x0300,(int *)0,&dx);
cx=(dx&0xff00);
dx=((LINES-1)<<8)+COLS-1;
video(0x701,&cx,&dx);
}
/* delete line: scroll rest up */
void v_dl()
{
int cx,dx;
video(0x0300,(int *)0,&dx);
cx=(dx&0xff00)/*+0x100*/;
dx=((LINES-1)<<8)+COLS-1;
video(0x601,&cx,&dx);
}
/* scroll reverse: scroll whole screen */
void v_sr()
{
int cx=0, dx=((LINES-1)<<8)+COLS-1;
video(0x0701,&cx,&dx);
}
/* set cursor */
void v_move(x,y)
int x, y;
{
int dx=(y<<8)+x;
video(0x200,(int *)0,&dx);
}
/* put character: set attribute first, then execute char.
* Also remember if current line has changed.
*/
int v_put(ch)
int ch;
{
int cx=1;
ch&=0xff;
if (ch>=' ')
video(0x900|ch,&cx,(int *)0);
video(0xe00|ch,(int *)0, (int *)0);
if (ch=='\n')
{ exwrote = TRUE;
video(0xe0d, (int *)0, (int *)0);
}
return ch;
}
/* determine number of screen columns. Also set attrset according
* to monochrome/color screen.
*/
int v_cols()
{
union REGS regs;
regs.h.ah=0x0f;
int86(0x10, ®s, ®s);
if (regs.h.al==7) /* monochrome mode ? */
screen=1;
else
screen=0;
return regs.h.ah;
}
/* Getting the number of rows is hard. Most screens support 25 only,
* EGA/VGA also support 43/50 lines, and some OEM's even more.
* Unfortunately, there is no standard bios variable for the number
* of lines, and the bios screen memory size is always rounded up
* to 0x1000. So, we'll really have to cheat.
* When using the screen memory size, keep in mind that each character
* byte has an associated attribute byte.
*
* uses: word at 40:4c contains memory size
* byte at 40:84 # of rows-1 (sometimes)
* byte at 40:4a # of columns
*/
int v_rows()
{
int line, oldline;
/* screen size less then 4K? then we have 25 lines only */
if (*(int far *)(0x0040004cl)<=4096)
return 25;
/* VEGA vga uses the bios byte at 0x40:0x84 for # of rows.
* Use that byte, if it makes sense together with memory size.
*/
if ((((*(unsigned char far *)(0x0040004aL)*2*
(*(unsigned char far *)(0x00400084L)+1))+0xfff)&(~0xfff))==
*(unsigned int far *)(0x0040004cL))
return *(unsigned char far *)(0x00400084L)+1;
/* uh oh. Emit '\n's until screen starts scrolling. */
v_move(oldline=0, 0);
for (;;)
{
video(0xe0a,(int *)0,(int *)0);
video(0x300,(int *)0,&line);
line>>=8;
if (oldline==line)
return line+1;
oldline=line;
}
}
/* the REAL bios interface -- used internally only. */
static void video(ax, cx, dx)
int ax, *cx, *dx;
{
union REGS regs;
regs.x.ax=ax;
if ((ax&0xff00)==0x600 || (ax&0xff00)==0x700)
regs.h.bh=attr[screen][vmode];
else
{
regs.h.bh=0;
regs.h.bl=attr[screen][vmode];
}
if (cx) regs.x.cx=*cx;
if (dx) regs.x.dx=*dx;
int86(0x10, ®s, ®s);
if (dx) *dx=regs.x.dx;
if (cx) *cx=regs.x.cx;
}
/* The following function determines which character is used for
* commandline-options by command.com. This system call is undocumented
* and valid for versions < 4.00 only.
*/
int switchar()
{
union REGS regs;
regs.x.ax=0x3700;
int86(0x21, ®s, ®s);
return regs.h.dl;
}
/* this function returns the DOS time, as a 32-bit long int representing
* hundredths of a second since midnight. Some systems may be limited to
* a resolution of whole seconds, but the values will still represent
* hundredths.
*/
static long dostime P_((void))
{
union REGS regs;
regs.h.ah = 0x2c; /* MS-DOS "get time" service */
intdos(®s, ®s);
return (((regs.h.ch * 60L) + regs.h.cl) * 60L + regs.h.dh) * 100L + regs.h.dl;
}
/*ARGSUSED*/
/* This function implements a raw read from the keyboard, with timeout. */
int ttyread(buf, len, time)
char *buf; /* where to store the keystrokes */
int len; /* maximum number of characters to get -- ignored */
int time; /* maximum time to wait, in 1/9th second increments */
{
long stop;
/* are we going to timeout? */
if (time != 0)
{
/* compute the time when we'll give up */
stop = dostime() + time * 10L;
/* wait for either keystroke or timeout */
while (!kbhit())
{
if (dostime() > stop)
{
/* we couldn't read any characters
* before timeout
*/
return 0;
}
}
}
/* get a keystroke */
buf[0] = getch();
if (buf[0] == 0) /* function key? */
{
buf[0] = '#';
buf[1] = getch();
return 2;
}
else
{
return 1;
}
}
#if !TURBOC
/* Turboc provides sleep, declared as void sleep(unsigned seconds) */
int sleep(seconds)
unsigned seconds;
{
long stop;
stop = dostime() + 100L * seconds;
while (dostime() < stop)
{
}
return 0;
}
#endif
#endif