-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsole.c
654 lines (601 loc) · 14.2 KB
/
console.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
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
/*
** Some support modules for (buffered) console I/O and list (printer) output.
**
** Does buffering (for efficiency), and (optionally) converts H19 terminal to
** VT100 terminal sequences on the console, since some CP/M software doesn't
** know what a VT100 is, but they all seem to know what a H19 is. Optionally
** converts ASCII DEL to BS, because the CP/M world generally doesn't use DEL.
**
** Buffers are flushed upon input, buffer full, or timeout. There are two
** levels of timeout, a 'fast' one based upon the tick count returned by
** times(), and a gross catch-all based upon alarm(1). The fast timeout is
** only used by the CP/M polling routines. The routines that block don't
** bother. No timeouts are enabled unless there is buffered output. For some
** reason, on the NeXT only the gross timeout is functioning. The times() call
** there seems to always return 0. Finally, all buffering can be disabled.
*/
#include "com.h"
/*
** Redirect simulation input (temporarily) from standard input to a file.
*/
void redirectin(name) char *name;
{
int fd;
fd = open(name, OROF);
if (fd >= 0)
infile = fd;
}
/*
** Reprogram stdin for single-character raw I/O.
*/
void setupstdin(regp) struct regs *regp;
{
signal(SIGTERM, catcher);
signal(SIGHUP, catcher);
signal(SIGINT, catcher);
signal(SIGALRM, flusher);
signal(SIGUSR1, u1catcher);
signal(SIGUSR2, u2catcher);
#if SYSV || DNIX
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
ioctl(0, TIOCGETA, &old);
#else
ioctl(0, TCGETA, &old);
#endif
new = old;
new.c_cc[VMIN] = 1;
new.c_cc[VTIME] = 0;
new.c_iflag &= ~(ICRNL | INLCR | IXON);
new.c_oflag &= ~(OCRNL | ONLCR);
new.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHONL);
if (!(regp->miscflags & NOIOCTL)) {
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
ioctl(0, TIOCSETA, &new);
#else
ioctl(0, TCSETA, &new);
#endif
}
#elif BSD
ioctl(0, TIOCGETP, &old);
new = old;
new.sg_flags |= RAW;
new.sg_flags &= ~(ECHO | CRMOD);
if (!(regp->miscflags & NOIOCTL))
ioctl(0, TIOCSETP, &new);
#endif
}
/*
** Put stdin back to normal.
*/
void restorestdin() {
#if SYSV || DNIX
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
ioctl(0, TIOCSETA, &old);
#else
ioctl(0, TCSETAW, &old);
#endif
#elif BSD
ioctl(0, TIOCSETP, &old);
#endif
}
/*
** Temporarily turn on stdin's line cooker again.
*/
void stdinlineon(regp) struct regs *regp;
{
if (regp->miscflags & NOIOCTL)
return;
#if SYSV || DNIX
new.c_iflag |= (ICRNL | IXON);
new.c_oflag |= ONLCR;
new.c_lflag |= (ICANON | ECHO | ECHOE | ECHOK | ECHONL);
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
ioctl(0, TIOCSETA, &new);
#else
ioctl(0, TCSETAW, &new);
#endif
#elif BSD
new.sg_flags |= (ECHO | CRMOD);
new.sg_flags &= ~RAW;
ioctl(0, TIOCSETP, &new);
#endif
}
/*
** Turn the line cooker back off.
*/
void stdinlineoff(regp) struct regs *regp;
{
if (regp->miscflags & NOIOCTL)
return;
#if SYSV || DNIX
new.c_iflag &= ~(ICRNL | IXON);
new.c_oflag &= ~ONLCR;
new.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL);
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
ioctl(0, TIOCSETA, &new);
#else
ioctl(0, TCSETAW, &new);
#endif
#elif BSD
new.sg_flags |= RAW;
new.sg_flags &= ~(ECHO | CRMOD);
ioctl(0, TIOCSETP, &new);
#endif
}
/*
** A rdchk() function for systems that don't have it already. The
** SYSV version is pretty inefficient (3 system calls vs 1), since many
** CP/M programs poll the console a lot. If there is a pending input
** character get it and prevent further polling until it's consumed.
** The CYGW variant only exists because Cygwin's implementation of
** VMIN/VTIME doesn't work right, so for that poll() is used instead.
**
** Returns non-zero when there's something to read.
*/
int myrdchk(fd) int fd;
{
int i;
unsigned char chr;
if (holdingchar >= 0)
return 1;
else {
#if DNIX
if (!rdchk(fd))
return 0;
#elif CYGW
{
struct pollfd pfd;
ioctl(fd, TCSETAW, &new); /* Not sure why; Cygwin insists. */
pfd.fd = fd;
pfd.events = POLLIN;
return poll(&pfd, 1, 0);
}
#elif SYSV
new.c_cc[VMIN] = 0;
new.c_cc[VTIME] = 0;
new.c_lflag &= ~ICANON;
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
ioctl(fd, FIONREAD, &i);
if (!i)
return 0;
#else
ioctl(fd, TCSETAW, &new);
#endif
#elif BSD
ioctl(fd, FIONREAD, &i);
if (!i)
return 0;
#endif
do
i = read(fd, &chr, 1);
while (i < 0 && errno == EINTR);
#if SYSV
new.c_cc[VMIN] = 1;
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
ioctl(fd, TIOCSETA, &new);
#else
ioctl(fd, TCSETAW, &new);
#endif
#endif
if (i > 0) {
holdingchar = chr;
return 1;
} else {
if (infile) {
close(infile);
infile = 0; /* Back to standard input. */
}
return 0;
}
}
}
/*
** Console-in function.
*/
void conin(regp, cp) struct regs *regp;
char *cp;
{
unsigned char chr;
int i;
if (holdingchar >= 0) {
chr = holdingchar;
holdingchar = -1;
} else {
again:
chr = 0; /* In case of error. */
do
i = read(infile, &chr, 1);
while (i < 0 && errno == EINTR);
if (i <= 0 && infile) {
close(infile);
infile = 0; /* Back to standard input. */
goto again;
}
}
if (!(regp->miscflags & NODELTOBS) && chr == 0x7F)
chr = 8; /* Convert DEL to backspace? */
*cp = chr;
}
/*
** Write character(s) to console out, with optional H19 to VT100 translation.
**
** There's a couple of other oddball translations here too, for a copy of
** PILOT I had lying around. I'm not sure exactly what terminal it wanted.
*/
void conout(regp, chr) struct regs *regp;
char chr;
{
char linebuf[20], *cp;
if (!regp->conbuf) {
regp->conbuf = malloc(SBUFSIZE);
regp->conpos = 0;
}
switch (regp->vtstate) {
case idle:
if (chr == 0x1A /* ADM-31? For PILOT. */
|| chr == 0x0C) /* Formfeed. */
goto cls;
if (chr == 0x1B) {
regp->vtstate = esc;
break;
} /* Fall through. */
case off:
bufconout(regp, &chr, 1);
break;
case esc:
switch (chr) {
case '@': /* Enter insert mode. */
regp->vtstate = idle;
bufconout(regp, "\033[4h", 4);
break;
case 'A': /* Cursor Up. */
case 'B': /* Cursor Down. */
case 'C': /* Cursor Forward. */
case 'D': /* Cursor Back. */
case 'H': /* Home. */
case 'J': /* Erase Screen from cursor. */
case 'K': /* Erase Line from cursor. */
case 'L': /* Insert Line. */
case 'M': /* Delete Line. */
case 'N': /* Delete Character. */
regp->vtstate = idle;
linebuf[0] = '\033';
linebuf[1] = '[';
linebuf[2] = chr;
bufconout(regp, linebuf, 3);
break;
case 'E': /* Home and Erase. */
cls:
regp->vtstate = idle;
bufconout(regp, "\033[H\033[J\033[m", 9);
break;
case 'O': /* Exit insert mode. */
regp->vtstate = idle;
bufconout(regp, "\033[4l", 4);
break;
case 'Y': /* Cursor Move. */
case '=': /* ADM-31-ish cursor move? For PILOT. */
regp->vtstate = waitrow;
break;
case 'p': /* Enter Reverse Video. */
regp->vtstate = idle;
bufconout(regp, "\033[7m", 4);
break;
case 'q': /* Exit Reverse Video. */
case ')': /* Exit Bold. ADM-31? For PILOT. */
regp->vtstate = idle;
bufconout(regp, "\033[m", 3);
break;
case '(': /* Enter Bold. ADM-31? For PILOT. */
regp->vtstate = idle;
bufconout(regp, "\033[1m", 4);
break;
case 'v': /* Enable Autowrap. */
regp->vtstate = idle;
bufconout(regp, "\033[?7h", 5);
break;
case 'w': /* Disable Autowrap. */
regp->vtstate = idle;
bufconout(regp, "\033[?7l", 5);
break;
case 'I': /* Reverse Index. */
regp->vtstate = idle;
chr = 'M';
default:
regp->vtstate = idle;
linebuf[0] = '\033';
linebuf[1] = chr;
bufconout(regp, linebuf, 2);
}
break;
case waitrow:
regp->vtstate = waitcol;
regp->vtrow = chr + 1 - ' ';
break;
case waitcol:
regp->vtstate = idle;
cp = linebuf;
cp += mysprintf(cp, "\033[%d;%dH", regp->vtrow, chr + 1 - ' ');
bufconout(regp, linebuf, cp - linebuf);
break;
}
}
void conoutstr(regp, str, len) struct regs *regp;
char *str;
int len;
{
if (regp->vtstate != off)
while (len--)
conout(regp, *str++);
else
bufconout(regp, str, len);
}
static void bufconout(regp, str, len) struct regs *regp;
char *str;
int len;
{
int i, chpos;
char *linebuf;
if (regp->miscflags & NOBUFFER || !regp->conbuf) {
while (len) { /* Unbuffered output. */
i = write(1, str, len);
if (i < 0) {
if (errno == EINTR)
continue;
else
break;
}
len -= i;
str += i;
}
return;
}
regp->consemaphore = 1;
chpos = regp->conpos;
linebuf = regp->conbuf;
while (len--) {
linebuf[chpos++] = *str++;
if (chpos >= SBUFSIZE) {
regp->trcreccnt = 0; /* Output forces new trace label line. */
alarm(0);
while (chpos) {
i = write(1, linebuf, chpos);
if (i < 0) {
if (errno == EINTR)
continue;
else
break;
}
chpos -= i;
linebuf += i;
}
chpos = 0;
linebuf = regp->conbuf;
} else
alarm(1);
}
regp->conpos = chpos;
regp->consemaphore = 0;
}
void flushconout(regp, dopoll) struct regs *regp;
int dopoll;
{
int tick, i, lastflushtick, chpos;
char *linebuf;
struct tms tm;
if (regp->miscflags & NOBUFFER || !regp->conbuf)
return;
regp->consemaphore = 1;
if (chpos = regp->conpos) {
regp->trcreccnt = 0; /* Output forces new trace label line. */
tick = lastflushtick = regp->lastconflushtick;
if (!(tick = times(&tm))) /* Stupid NeXT! */
tick = tm.tms_utime;
if (!dopoll || tick > lastflushtick + 5) {
alarm(0);
linebuf = regp->conbuf;
while (chpos) {
i = write(1, linebuf, chpos);
if (i < 0) {
if (errno == EINTR)
continue;
else
break;
}
chpos -= i;
linebuf += i;
}
chpos = 0;
regp->lastconflushtick = tick;
}
regp->conpos = chpos;
}
regp->consemaphore = 0;
}
void listout(regp, chr) struct regs *regp;
char chr;
{
char linebuf[20], *cp;
regp->listsemaphore = 1;
if (!regp->listbuf && regp->listname) {
regp->listbuf = malloc(SBUFSIZE);
regp->listpos = 0;
regp->listfd = open(regp->listname, OWOF, 0666);
}
buflistout(regp, &chr, 1);
regp->listsemaphore = 0;
}
static void buflistout(regp, str, len) struct regs *regp;
char *str;
int len;
{
int i, chpos;
char *linebuf;
if (regp->listfd < 0)
return;
if (regp->miscflags & NOBUFFER || !regp->listbuf) {
while (len) {
i = write(regp->listfd, str, len);
if (i < 0) {
if (errno == EINTR)
continue;
else
break;
}
len -= i;
str += i;
}
return;
}
chpos = regp->listpos;
linebuf = regp->listbuf;
while (len--) {
linebuf[chpos++] = *str++;
if (chpos >= SBUFSIZE) {
while (chpos) {
i = write(regp->listfd, linebuf, chpos);
if (i < 0) {
if (errno == EINTR)
continue;
else
break;
}
chpos -= i;
linebuf += i;
}
chpos = 0;
linebuf = regp->listbuf;
}
}
regp->listpos = chpos;
}
void flushlistout(regp, dopoll) struct regs *regp;
int dopoll;
{
int tick, i, lastflushtick, chpos;
char *linebuf;
struct tms tm;
if (regp->listfd < 0)
return;
if (regp->miscflags & NOBUFFER || !regp->listbuf)
return;
regp->listsemaphore = 1;
if (chpos = regp->listpos) {
tick = lastflushtick = regp->lastlistflushtick;
if (!dopoll || (tick = times(&tm)) > lastflushtick + 5) {
linebuf = regp->listbuf;
while (chpos) {
i = write(regp->listfd, linebuf, chpos);
if (i < 0) {
if (errno == EINTR)
continue;
else
break;
}
chpos -= i;
linebuf += i;
}
chpos = 0;
regp->lastlistflushtick = tick;
}
regp->listpos = chpos;
}
regp->listsemaphore = 0;
}
void flusher(signum) int signum;
{
struct regs *regp;
regp = gregp;
signal(SIGALRM, flusher);
if (!regp->consemaphore)
flushconout(gregp, 0);
if (!regp->listsemaphore)
flushlistout(gregp, 0);
}
#ifndef CYGW
static char *itoa(val, radix, leadfill, ndigits) int val, radix, leadfill,
ndigits;
{
short i;
char *p, *p2, out;
static char buf[20];
unsigned long test, remainder;
p = buf;
remainder = val;
i = ndigits; /* Make sure there're at least the number */
while (i--) /* of leading whatevers that we've asked for */
*p++ = leadfill; /* as the field width. */
p2 = p;
if (radix == 16) /* Hex output? */
test = 0x10000000L;
else {
test = 1000000000L; /* Decimal output? */
if (val < 0) {
remainder = -val;
*p++ = '-';
}
}
while (test > remainder) /* Scale down to match number. */
test /= radix;
if (remainder)
while (test) {
out = remainder / test;
*p++ = out + ((out >= 10) ? '7' : '0');
remainder %= test;
test /= radix;
}
else
*p++ = '0';
*p = 0;
if (ndigits) { /* A field width set? */
if ((p2 + ndigits) >= p) /* Make sure we never truncate. */
return p - ndigits;
else
return p2;
}
return buf; /* No field width, return minimum number. */
}
#endif
/*
** Provide our own atoi() function because the standard one uses ctype
** and is overly large (for some uses). Handles hexadecimal input too.
*/
int myatoi(p) /* Parse out a number. */
register char *p;
{
register int val, radix;
register char chr;
radix = 10;
val = 0;
while (1) {
if (*p == '$' || *p == 'x' || *p == 'X') {
radix = 16;
p++;
val = 0;
continue;
}
chr = *p++ - '0';
if (chr > 0x16)
chr -= 0x20;
if (chr > 9)
chr -= 7;
if (chr < 0 || chr >= radix)
break;
val = (val * radix) + chr;
}
return val;
}