forked from jhallen/exorsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exorterm.c
1311 lines (1209 loc) · 21.3 KB
/
exorterm.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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* EXORterm simulator
* Copyright
* (C) 2016 Joseph H. Allen
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 1, or (at your option) any later version.
*
* It is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this software; see the file COPYING. If not, write to the Free Software Foundation,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/poll.h>
#include "exorterm.h"
/* State of real terminal */
#define WIDTH 80
#define HEIGHT 24
int scrn[WIDTH*HEIGHT]; /* Screen contents */
int scrn_x = 0; /* Screen cursor position */
int scrn_y = 0;
/* Set real cursor position */
void cpos(int y, int x)
{
if (y > scrn_y) { /* Need to go down */
/* if (y - scrn_y <= 4) {
while (y > scrn_y) {
putchar(10);
++scrn_y;
}
} else */
if (y - scrn_y == 1) {
printf("\033[B");
++scrn_y;
} else {
printf("\033[%dB", y - scrn_y);
scrn_y += y - scrn_y;
}
}
if (y < scrn_y) { /* Need to go up */
if (scrn_y - y == 1) {
printf("\033[A");
--scrn_y;
} else {
printf("\033[%dA", scrn_y - y);
scrn_y -= scrn_y - y;
}
}
if (x > scrn_x) { /* Need to go right */
/* if (x - scrn_x <= 4) {
while (x > scrn_x) {
putchar(scrn[scrn_y * WIDTH + scrn_x]);
++scrn_x;
}
} */
if (x - scrn_x == 1) {
printf("\033[C");
scrn_x += 1;
} else {
printf("\033[%dC", x - scrn_x);
scrn_x += x - scrn_x;
}
}
if (x < scrn_x) { /* Need to go left */
if (x == 0) {
putchar(13);
scrn_x = 0;
} else if (scrn_x - x <= 4) {
while (x < scrn_x) {
putchar(8);
--scrn_x;
}
} else {
printf("\033[%dD", scrn_x - x);
scrn_x -= scrn_x - x;
}
}
}
/* Output character at a position */
void out(int y, int x, int c)
{
cpos(y, x);
putchar(c);
scrn[scrn_y * WIDTH + scrn_x] = c;
++scrn_x;
if (scrn_x == WIDTH) {
putchar(13);
scrn_x = 0;
}
}
/* Initialize screen */
void izscrn()
{
int y;
putchar('\r');
for (y = 0; y != HEIGHT; ++y) {
int x;
for (x = 0; x != WIDTH; ++x) {
scrn[y*WIDTH + x] = ' ';
}
putchar('\n');
}
scrn_x = 0;
scrn_y = HEIGHT - 1;
// printf("\033[H\033[J");
// scrn_x = 0;
// scrn_y = 0;
}
/* Modes */
enum {
SCROLL,
PAGE,
PROTECT
};
/* Current mode */
int mode;
/* Current virtual view area */
int xleft = 0;
int xright = WIDTH;
int ytop = 0;
int ybottom = HEIGHT;
/* Current cursor position */
int xpos = 0;
int ypos = 0;
/* Buffer */
int screen[WIDTH*HEIGHT];
/* Magic cookie bits */
#define BLINK 0x01
#define INVERSE 0x02
#define DIM 0x04
#define UNDERLINE 0x08
#define BLANK 0x10
#define PROTECT 0x20
/* Terminal input FIFO */
#define FIFOSIZE 8192
unsigned char fifo[FIFOSIZE];
int fifo_old = 0;
int fifo_new = 0;
void term_put(int c)
{
fifo[fifo_new++] = c;
if (fifo_new == FIFOSIZE)
fifo_new = 0;
}
void term_reset()
{
int y;
for (y = 0; y != HEIGHT; ++y) {
int x;
for (x = 0; x != WIDTH; ++x) {
screen[y*WIDTH + x] = ' ';
}
}
xleft = 0;
xright = WIDTH;
ytop = 0;
ybottom = HEIGHT;
mode = SCROLL;
xpos = 0;
ypos = 0;
}
/* Make real screen look like buffer */
void update()
{
int y;
for (y = 0; y != HEIGHT; ++y) {
int x;
for (x = 0; x != WIDTH; ++x) {
if (scrn[y*WIDTH + x] != screen[y*WIDTH + x]) {
out(y, x, screen[y*WIDTH + x]);
}
}
}
cpos(ypos, xpos);
fflush(stdout);
}
/* Initialize emulator */
void izexorterm()
{
izscrn();
term_reset();
update();
}
/* Delete line in virtual area */
void del(int line)
{
int y;
int x;
for (y = ytop + line + 1; y != ybottom; ++y) {
memcpy(screen + (y - 1) * WIDTH + xleft,
screen + (y ) * WIDTH + xleft,
(xright - xleft) * sizeof(screen[0]));
}
for (x = xleft; x != xright; ++x) {
screen[(y - 1) * WIDTH + x] = ' ';
}
}
/* Insert list in virtual area */
void ins(int line)
{
int y;
int x;
for (y = ybottom - 1; y > ytop + line; --y) {
memcpy(screen + (y ) * WIDTH + xleft,
screen + (y - 1) * WIDTH + xleft,
(xright - xleft) * sizeof(screen[0]));
}
for (x = xleft; x != xright; ++x) {
screen[y * WIDTH + x] = ' ';
}
}
/* Scroll virtual area up */
void scrup()
{
/* Scroll the real terminal if we can */
if (xleft == 0 && xright == WIDTH && ytop == 0 && ybottom == HEIGHT) {
int y, x;
update();
cpos(HEIGHT - 1, 0);
putchar(10);
for (y = 1; y != HEIGHT; ++y) {
memcpy(scrn + (y - 1) * WIDTH,
scrn + (y ) * WIDTH,
WIDTH * sizeof(screen[0]));
}
for (x = 0; x != WIDTH; ++x) {
scrn[(y - 1) * WIDTH + x] = ' ';
}
}
del(0);
}
/* Type a character */
void term_type(int c)
{
switch (mode) {
case SCROLL: { /* Scroll */
screen[ypos * WIDTH + xpos] = c;
if (++xpos == xright)
xpos = xright - 1;
break;
} case PAGE: { /* Page */
screen[ypos * WIDTH + xpos] = c;
if (++xpos == xright) {
xpos = xleft;
if (++ypos == ybottom) {
xpos = ytop;
}
}
break;
} case PROTECT: { /* Protect */
break;
}
}
}
void term_up()
{
switch (mode) {
case SCROLL: {
if (ypos != ytop)
--ypos;
break;
} case PAGE: {
if (ypos != ytop) {
--ypos;
} else {
ypos = ybottom - 1;
}
break;
} case PROTECT: {
break;
}
}
}
void term_down()
{
switch (mode) {
case SCROLL: {
if (ypos + 1 == ybottom) {
scrup();
} else {
++ypos;
}
break;
} case PAGE: {
if (ypos + 1 == ybottom) {
ypos = ytop;
} else {
++ypos;
}
break;
} case PROTECT: {
break;
}
}
}
void term_left()
{
switch (mode) {
case SCROLL: {
if (xpos != xleft) {
--xpos;
}
break;
} case PAGE: {
if (xpos != xleft) {
--xpos;
} else {
xpos = xright - 1;
term_up();
}
break;
} case PROTECT: {
break;
}
}
}
void term_right()
{
switch (mode) {
case SCROLL: {
if (xpos + 1 != xright) {
++xpos;
} else {
}
break;
} case PAGE: {
if (xpos + 1 != xright) {
++xpos;
} else {
xpos = 0;
term_down();
}
break;
} case PROTECT: {
break;
}
}
}
void term_goto(int row, int col)
{
if (row >= ybottom || row < ytop)
return;
if (col >= xright || col < xleft)
return;
xpos = col;
ypos = row;
/*
if (row >= ybottom - ytop)
return;
if (col >= xright - xleft)
return;
ypos = ytop + row;
xpos = xleft + col;
*/
}
void term_home()
{
xpos = xleft;
ypos = ytop;
}
void term_clear()
{
int y, x;
for (y = ytop; y != ybottom; ++y) {
for (x = xleft; x != xright; ++x)
screen[y * WIDTH + x] = ' ';
}
term_home();
}
void term_set_left(int col)
{
if (col >= 0 && col < xright) {
xleft = col;
if (xpos < xleft) {
xpos = xleft;
}
}
}
void term_set_right(int col)
{
if (col > xleft && col < WIDTH) {
xright = col + 1;
if (xpos >= xright) {
xpos = xright - 1;
}
}
}
void term_set_top(int row)
{
if (row >= 0 && row < ybottom) {
ytop = row;
if (ypos < ytop) {
ypos = ytop;
}
}
}
void term_set_bottom(int row)
{
if (row > ytop && row < HEIGHT) {
ybottom = row + 1;
if (ypos >= ybottom) {
ypos = ybottom - 1;
}
}
}
void term_reset_virtual()
{
xleft = 0;
xright = WIDTH;
ytop = 0;
ybottom = HEIGHT;
}
void term_return()
{
xpos = xleft;
}
void term_read_cursor()
{
term_put(0xDD);
term_put(0x20 + ypos);
term_put(0x20 + xpos);
term_put(0xDE);
}
void term_set_page_mode()
{
if (mode != PAGE) {
mode = PAGE;
term_reset_virtual();
term_home();
}
}
void term_set_scroll_mode()
{
if (mode != SCROLL) {
mode = SCROLL;
term_reset_virtual();
term_home();
}
}
void term_set_protect_mode()
{
mode = PROTECT;
term_reset_virtual();
term_home();
}
void term_delete_char()
{
}
void term_enable_keyboard()
{
}
void term_disable_keyboard()
{
}
void term_page_erase()
{
}
void term_line_erase()
{
}
void term_line_insert()
{
if (mode == PAGE) {
ins(ypos - ytop);
term_return();
}
}
void term_line_delete()
{
if (mode == PAGE) {
del(ypos - ytop);
term_return();
}
}
void term_send_page()
{
}
void term_send_line()
{
}
void term_forward_tab()
{
}
void term_backward_tab()
{
}
void term_fac_set(int fac)
{
}
void term_fac_reset(int fac)
{
}
void term_set_transparent()
{
}
void term_reset_transparent()
{
}
void term_set_inverse()
{
}
void term_reset_inverse()
{
}
void term_set_status()
{
}
void term_reset_status()
{
}
void term_autolinefeed()
{
}
void term_set_special()
{
}
void term_reset_special()
{
}
/* Parser states */
enum {
IDLE,
LOAD_CURSOR_1,
LOAD_CURSOR_2,
SET_TOP,
SET_BOTTOM,
SET_LEFT,
SET_RIGHT,
WRITE_ABSOLUTE_1,
WRITE_ABSOLUTE_2,
WRITE_ABSOLUTE_3,
WRITE_ABSOLUTE_4,
READ_ABSOLUTE_1,
INSERT_CHAR,
SET_TABS,
};
/* Current state */
int state = IDLE;
int inbuf[3];
FILE *logfile;
int reveal;
char *decode(int c)
{
switch (c) {
case 0x08: {
return "cursor left";
break;
} case 0x0A: {
return "cursor down";
break;
} case 0x0B: {
return "cursor up";
break;
} case 0x0C: {
return "cursor right";
break;
} case 0x0D: {
return "return";
break;
} case 0xC0: {
return "home";
break;
} case 0xC1: {
return "cursor up";
break;
} case 0xC2: {
return "cursor down";
break;
} case 0xC3: {
return "cursor left";
break;
} case 0xC4: {
return "cursor right";
break;
} case 0xC5: {
return "load cursor";
break;
} case 0xC6: {
return "read cursor";
break;
} case 0xC7: {
return "set page mode";
break;
} case 0xC8: {
return "set scroll mode";
break;
} case 0xC9: {
return "set top";
break;
} case 0xCA: {
return "set bottom";
break;
} case 0xCB: {
return "set left";
break;
} case 0xCC: {
return "set right";
break;
} case 0xCD: {
return "set protect mode";
break;
} case 0xCE: {
return "write absolute";
break;
} case 0xCF: {
return "read absolute";
break;
} case 0xD0: {
return "insert char";
break;
} case 0xD1: {
return "delete char";
break;
} case 0xD2: {
return "enable keyboard";
break;
} case 0xD3: {
return "disable keyboard";
break;
} case 0xD4: {
return "page erase";
break;
} case 0xD5: {
return "line erase";
break;
} case 0xD6: {
return "line insert";
break;
} case 0xD7: {
return "line delete";
break;
} case 0xD8: {
return "clear";
break;
} case 0xD9: {
return "send page";
break;
} case 0xDA: {
return "forward tab";
break;
} case 0xDB: {
return "backware tab";
break;
} case 0xDC: {
return "set tabs";
break;
} case 0xDF: {
return "send line";
break;
} case 0xE0: {
return "term_fac_set(BLINK)";
break;
} case 0xE1: {
return "term_fac_reset(BLINK);";
break;
} case 0xE2: {
return "term_fac_set(INVERSE);";
break;
} case 0xE3: {
return "term_fac_reset(INVERSE);";
break;
} case 0xE4: {
return "term_fac_set(DIM);";
break;
} case 0xE5: {
return "term_fac_reset(DIM);";
break;
} case 0xE6: {
return "term_fac_set(UNDERLINE);";
break;
} case 0xE7: {
return "term_fac_reset(UNDERLINE);";
break;
} case 0xE8: {
return "term_fac_set(BLANK);";
break;
} case 0xE9: {
return "term_fac_reset(BLANK);";
break;
} case 0xEA: {
return "term_fac_set(PROTECT);";
break;
} case 0xEB: {
return "term_fac_reset(PROTECT);";
break;
} case 0xEC: {
return "term_set_transparent();";
break;
} case 0xED: {
return "term_reset_transparent();";
break;
} case 0xEE: {
return "term_set_inverse();";
break;
} case 0xEF: {
return "term_reset_inverse();";
break;
} case 0xF1: {
return "term_reset();";
break;
} case 0xF2: {
return "term_set_status();";
break;
} case 0xF3: {
return "term_reset_status();";
break;
} case 0xF7: {
return "term_autolinefeed();";
break;
} case 0xFC: {
return "term_set_special();";
break;
} case 0xFD: {
return "term_reset_special();";
break;
} default: {
if (c >= 0x20 && c <= 0x7E) {
return "term_type(c);";
} else {
return "huh?";
}
break;
}
}
}
void term_out(int c)
{
if (reveal) {
if (!logfile) {
logfile = fopen("logfile", "w");
}
fprintf(logfile, "%2.2x '%c' %s\n", c, c, decode(c));
fflush(logfile);
}
// putchar(c); return;
switch (state) {
case IDLE: {
switch (c) {
case 0x08: {
term_left();
break;
} case 0x0A: {
term_down();
break;
} case 0x0B: {
term_up();
break;
} case 0x0C: {
term_right();
break;
} case 0x0D: {
term_return();
break;
} case 0xC0: {
term_home();
break;
} case 0xC1: {
term_up();
break;
} case 0xC2: {
term_down();
break;
} case 0xC3: {
term_left();
break;
} case 0xC4: {
term_right();
break;
} case 0xC5: {
state = LOAD_CURSOR_1;
break;
} case 0xC6: {
term_read_cursor();
break;
} case 0xC7: {
term_set_page_mode();
break;
} case 0xC8: {
term_set_scroll_mode();
break;
} case 0xC9: {
state = SET_TOP;
break;
} case 0xCA: {
state = SET_BOTTOM;
break;
} case 0xCB: {
state = SET_LEFT;
break;
} case 0xCC: {
state = SET_RIGHT;
break;
} case 0xCD: {
term_set_protect_mode();
break;
} case 0xCE: {
state = WRITE_ABSOLUTE_1;
break;
} case 0xCF: {
state = READ_ABSOLUTE_1;
break;
} case 0xD0: {
state = INSERT_CHAR;
break;
} case 0xD1: {
term_delete_char();
break;
} case 0xD2: {
term_enable_keyboard();
break;
} case 0xD3: {
term_disable_keyboard();
break;
} case 0xD4: {
term_page_erase();
break;
} case 0xD5: {
term_line_erase();
break;
} case 0xD6: {
term_line_insert();
break;
} case 0xD7: {
term_line_delete();
break;
} case 0xD8: {
term_clear();
break;
} case 0xD9: {
term_send_page();
break;
} case 0xDA: {
term_forward_tab();
break;
} case 0xDB: {
term_backward_tab();
break;
} case 0xDC: {
state = SET_TABS;
break;
} case 0xDF: {
term_send_line();
break;
} case 0xE0: {
term_fac_set(BLINK);
break;
} case 0xE1: {
term_fac_reset(BLINK);
break;
} case 0xE2: {
term_fac_set(INVERSE);
break;
} case 0xE3: {
term_fac_reset(INVERSE);
break;
} case 0xE4: {
term_fac_set(DIM);
break;
} case 0xE5: {
term_fac_reset(DIM);
break;
} case 0xE6: {
term_fac_set(UNDERLINE);
break;
} case 0xE7: {
term_fac_reset(UNDERLINE);
break;
} case 0xE8: {
term_fac_set(BLANK);
break;
} case 0xE9: {
term_fac_reset(BLANK);
break;
} case 0xEA: {
term_fac_set(PROTECT);
break;
} case 0xEB: {
term_fac_reset(PROTECT);
break;
} case 0xEC: {
term_set_transparent();
break;
} case 0xED: {
term_reset_transparent();
break;
} case 0xEE: {
term_set_inverse();
break;
} case 0xEF: {
term_reset_inverse();
break;
} case 0xF1: {
term_reset();
break;
} case 0xF2: {
term_set_status();
break;
} case 0xF3: {
term_reset_status();
break;
} case 0xF7: {
term_autolinefeed();
break;