-
Notifications
You must be signed in to change notification settings - Fork 0
/
conio.c
367 lines (341 loc) · 5.43 KB
/
conio.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
/*
Licensed under the MIT license. See LICENSE file in the project root for details.
*/
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#ifdef __STDC__
#include <stdlib.h>
#else
char * malloc();
#endif
int kbhit() {
return bdos(6,254);
}
#define GETCH_BUFLEN (64)
static char * getch_buffer=0;
#ifndef __STDC__
int cputc(c)
char c;
#else
int cputc(char c)
#endif
{
return bdos(2,c);
}
#ifndef __STDC__
int cputs(str)
char *str;
#else
int cputs(char *str)
#endif
{
while (*str) {
bdos(2,*str++);
}
return 0;
}
int getch()
{
int i,c,d;
static int s=0;
static int o=0;
if(getch_buffer==0) {
getch_buffer=(char*)malloc(GETCH_BUFLEN+1);
}
if(s>0) {
c=getch_buffer[o];s--;o++;
o=o%GETCH_BUFLEN;
return c;
}
while(!(c=bdos(6,255)))
continue;
while(s<GETCH_BUFLEN && (d=bdos(6,255))) {
if(1) {
getch_buffer[(o+s)%GETCH_BUFLEN]=d;
s++;
}
}
return c;
}
#ifndef __STDC__
crtreset()
#else
void crtreset()
#endif
{
if(osver()>0x22) {
cputs("\x1be\x1bz");
} else {
cputs("\x1bm\x1b0\x1b1\x1bq\x1bt\x1bu");
}
}
#ifndef __STDC__
clrscr()
#else
void clrscr()
#endif
{
cputs("\x1bE");
}
#ifndef __STDC__
clreos()
#else
void clreos()
#endif
{
cputs("\x1bJ");
}
#ifndef __STDC__
clreol()
#else
void clreol()
#endif
{
cputs("\x1bK");
}
#ifndef __STDC__
delchar()
#else
void delchar()
#endif
{
cputs("\x1bN");
}
#ifndef __STDC__
insline()
#else
void insline()
#endif
{
cputs("\x1bL");
}
#ifndef __STDC__
delline()
#else
void delline()
#endif
{
cputs("\x1bM");
}
#ifndef __STDC__
gotoxy(x, y)
int x;
int y;
#else
void gotoxy(int x, int y)
#endif
{
char msg[5];
msg[0]=27;
msg[1]='Y';
msg[2]=x+32;
msg[3]=y+32;
msg[4]=0;
cputs(msg);
}
#ifndef __STDC__
cursor(cmd)
int cmd;
#else
void cursor(int cmd)
#endif
{
switch(cmd) {
case CURSOR_ON:
if(osver()>0x22)
cputs("\x1be");
else
cputs("\x1bm");
break;
case CURSOR_OFF:
if(osver()>0x22)
cputs("\x1bf");
else
cputs("\x1bn");
break;
case CURSOR_SAVE:
cputs("\x1bj");
break;
case CURSOR_RESTORE:
cputs("\x1bk");
break;
}
}
#ifndef __STDC__
wrapline(on)
int on;
#else
void wrapline(int on)
#endif
{
cputs(on?"\x1bv":"\x1bw");
}
#ifndef __STDC__
scrmode(m)
int m;
#else
void scrmode(int m)
#endif
{
if(osver()>0x22) {
return;
}
switch(m) {
case SCRMODE_DEFAULT:
crtreset();
clrscr();
break;
case SCRMODE_COL80:
#asm
push ax
push bx
push cx
push dx
mov al, 3
mov ah, 0
int 10h
pop dx
pop cx
pop bx
pop ax
#endasm
statline(STATLINE_OFF);
statline(STATLINE_ON);
clrscr();
break;
case SCRMODE_COL40:
#asm
push ax
push bx
push cx
push dx
mov ax, 0
int 10h
pop dx
pop cx
pop bx
pop ax
#endasm
statline(STATLINE_OFF);
clrscr();
break;
case SCRMODE_MONO:
cputs("\x1by");
break;
case SCRMODE_COLOR:
cputs("\x1bx");
break;
}
}
#ifndef __STDC__
statline(on)
int on;
#else
void statline(int on)
#endif
{
int os=osver();
if(os>0x31) {
xstatline(on?2:1);
return;
}
if(os==0x22) {
cputs(on?"\x1b1":"\x1b0");
return;
}
}
#ifndef __STDC__
textcolor(fg)
int fg;
#else
void textcolor(int fg)
#endif
{
unsigned char msg[8];
if(fg<1) {
return;
}
msg[0]=27;
msg[1]='j';
msg[2]=27;
msg[3]='b';
msg[4]=((unsigned char)fg);
msg[5]=27;
msg[6]='k';
msg[7]=0;
cputs((char*)msg);
}
#ifndef __STDC__
setstatus(arg)
char* arg;
#else
void setstatus(char* arg)
#endif
{
int i=0;
int es;
int bx;
int ax;
int segs[4];
int escape=0;
int upper=0;
if(osver()>0x22) {
return;
}
segread(segs);
ax=bdosx(49,0,&es,&bx);
while(i<14) {
if(*arg=='\\') {
escape=1;
arg++;
continue;
}
if(*arg) {
char c;
c=*arg;
if(escape) {
escape=0;
switch(c) {
case 'u':
case 'U':
upper=1;
c=0;
break;
case 'l':
case 'L':
upper=0;
c=0;
break;
case 's':
case 'S':
c=' ';
break;
case '\\':
c=' ';
break;
default:
c=0;
break;
}
}
if(!c) {
arg++;
continue;
}
if(c<' ') {
c=' ';
}
if(isalpha(c) && !upper) {
pokeb(bx+0x32+i,es,tolower(c));
} else {
pokeb(bx+0x32+i,es,c);
}
i++;
arg++;
} else {
pokeb(bx+0x32+i,es,' ');
i++;
}
}
}