-
Notifications
You must be signed in to change notification settings - Fork 2
/
vt52x.c
2846 lines (2584 loc) · 69.4 KB
/
vt52x.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
/*
* vt52x emulation core
*/
#include <stdio.h>
#include <string.h>
#include <glib/gprintf.h>
#include <gdk/gdkkeysyms.h>
#include <unistd.h>
#include "terminal.h"
#include "screen.h"
#include "emul.h"
#define WARN_NOTIMPL 1
#undef SHOW_IMPL
#undef OUTPUT_EVERYTHING
#if WARN_NOTIMPL
#define NOTIMPL(id,desc,reason) g_warning("%s (%s) not implemented%s" reason , id, desc, reason?"; confidence: ":"")
#define NOTIMPL_UNKNOWN(fmt,args...) g_warning("UNKNOWN (" fmt ") not implemented" , ## args )
#else
#define NOTIMPL(id,desc,reason) do{}while(0)
#define NOTIMPL_UNKNOWN(fmt,args...) do{}while(0)
#endif
#if SHOW_IMPL
#define IMPL(id,desc,support) g_warning("Implemented %s: %s (" support ")", id, desc)
#else
#if WARN_NOTIMPL
#define IMPL(id,desc,support) do { if (strcmp(support, "fully")) g_warning("Implemented %s: %s (" support ")", id, desc); } while (0)
#else
#define IMPL(id,desc,support) do{}while(0)
#endif
#endif
#define EMUL_ENQ_REPLY "temu"
#define VTPARSE_MAX_PARAMS 17 /* We overwrite the last one endlessly */
#define VTPARSE_MAX_STR 1024
#define VT_GLYPH_BACKWARDS_QUESTION 0x61f
#define T (S->owner)
#define TP (T->priv)
#define WIDTH (temu_screen_get_cols(T))
#define HEIGHT (temu_screen_get_rows(T))
#define CURSOR_X_TRANS(x) (x)
#define CURSOR_Y_TRANS(y) (S->o_DECOM?((y) + S->scroll_top):(y))
#define CURSOR_X_RTRANS(x) (x)
#define CURSOR_Y_RTRANS(y) (S->o_DECOM?((y) - S->scroll_top):(y))
#define P(number) (S->parm[number])
#define PARM_DEF(number, def) do{ \
if (P(number) == 0) \
P(number) = (def); \
} while(0)
#define ECELL (S->cell)
#define ATTR (S->attr)
#define ATTR_CLEAR(attr) memset(attr, 0, sizeof(temu_attr_t))
#define ATTR_NORMAL() do { \
ATTR_CLEAR(&ATTR); \
ATTR.fg = TEMU_SCREEN_FG_DEFAULT; \
ATTR.bg = TEMU_SCREEN_BG_DEFAULT; \
} while(0)
#define E(strt,pre,intr,fin) (((strt)<<24) | ((pre)<<16) | ((intr)<<8) |((fin)<<0))
enum C0 {
C0_FIRST=0,
C_NUL = C0_FIRST,
C_SOH, C_STX, C_ETX,
C_EOT, C_ENQ, C_ACK, C_BEL,
C_BS, C_HT, C_LF, C_VT,
C_FF, C_CR, C_SO, C_SI,
C_DLO, C_DC1, C_DC2, C_DC3,
C_DC4, C_NAK, C_SYN, C_ETB,
C_CAN, C_EM, C_SUB, C_ESC,
C_FS, C_GS, C_RS, C_US,
C0_PAST
};
enum C1 {
C1_FIRST=0x80,
C_80 = C1_FIRST,
C_81, C_82, C_83,
C_IND, C_NEL, C_SSA, C_ESA,
C_HTS, C_HTJ, C_VTS, C_PLD,
C_PLU, C_RI, C_SS2, C_SS3,
C_DCS, C_PU1, C_PU2, C_STS,
C_CRH, C_MW, C_SPA, C_EPA,
C_SOS, C_99, C_DECID,C_CSI,
C_ST, C_OSC, C_PM, C_APC,
C1_PAST
};
#define C_DEL 0x7f
typedef enum _vtstate vtstate;
enum _vtstate {
VT_GROUND,
VT_ESC, VT_ESC_IGNORE,
VT_CSI, VT_CSI_FIN, VT_CSI_IGNORE,
VT_STR, VT_STR_FIN, VT_STR_IGNORE, VT_STR_DATA,
VT_STRX
};
struct _TemuEmul {
vtstate state;
TemuScreen *owner;
gboolean allow_resize;
gboolean do_C1, do_C1_lo;
gboolean send_C1_lo;
temu_cset_t charset;
temu_cell_t cell;
temu_attr_t attr;
gboolean scroll_height_full;
gint scroll_top, scroll_height;
gint saved_cursor_x, saved_cursor_y;
temu_attr_t saved_attr;
gint cursor_x, cursor_y;
gboolean cursor_redraw, cursor_hide;
guchar strt, pre, intr, fin;
gint parm[VTPARSE_MAX_PARAMS];
gint parms;
gchar char_buf[6];
gint chars;
gint outmax;
gchar *out;
gint outs;
gboolean tabclear;
gint tabstops;
guint8 *tabstop;
gchar str[VTPARSE_MAX_STR];
gint strs;
/* */
gboolean conformance;
gboolean o_DECBKM, o_DECKPAM;
gboolean o_KAM, o_CRM, o_IRM, o_SRM, o_LNM, o_DECKPM;
gboolean o_DECCKM, o_DECOM, o_DECAWM, o_DECNCSM;
};
static void vt52x_parse(TemuEmul *S, const gchar *text, gint len);
static void vt52x_C0(TemuEmul *S, guchar c0);
static void vt52x_C1(TemuEmul *S, guchar c1);
static void vt52x_esc(TemuEmul *S);
static void vt52x_csi(TemuEmul *S);
static void vt52x_dcs(TemuEmul *S);
static void vt52x_apc(TemuEmul *S);
static void vt52x_osc(TemuEmul *S);
static void emul_SM_ansi(TemuEmul *S, gboolean set);
static void emul_SM_dec(TemuEmul *S, gboolean set);
static void emul_init(TemuEmul *S);
static void emul_reset(TemuEmul *S);
static void emul_reset_soft(TemuEmul *S);
static void emul_resize(TemuEmul *S, gint width, gint height);
static void emul_cursor_cleared(TemuEmul *S);
static void emul_cursor_clear(TemuEmul *S);
static void emul_cursor_draw(TemuEmul *S);
static void emul_cursor_save(TemuEmul *S);
static void emul_cursor_restore(TemuEmul *S);
static void emul_tab_reset(TemuEmul *S, gboolean clear);
static void emul_tab_set(TemuEmul *S, gint col);
static void emul_tab_clear(TemuEmul *S, gint col);
static void emul_tab_next(TemuEmul *S);
static void emul_tab_prev(TemuEmul *S);
static void emul_check_cursor_y(TemuEmul *S);
static void emul_move_cursor(TemuEmul *S, gint x, gint y);
static void emul_scroll(TemuEmul *S, gint rows);
static void emul_BS(TemuEmul *S);
static void emul_CR(TemuEmul *S);
static void emul_DA1(TemuEmul *S);
static void emul_DECBI(TemuEmul *S);
static void emul_DECFI(TemuEmul *S);
static void emul_DECRQSS(TemuEmul *S);
static void emul_DECSCL(TemuEmul *S);
static void emul_DSR_ansi(TemuEmul *S);
static void emul_ED(TemuEmul *S, gint func);
static void emul_EL(TemuEmul *S, gint func);
static void emul_ICH(TemuEmul *S, gint cols);
static void emul_IND(TemuEmul *S);
static void emul_LF(TemuEmul *S);
static void emul_RI(TemuEmul *S);
static void emul_SGR(TemuEmul *S);
static void emul_add_char(TemuEmul *S, guchar ch);
static void emul_add_glyph(TemuEmul *S, gunichar glyph);
/*
*
*/
static void emul_rect_coords(TemuEmul *S, gint first, gint *x, gint *y, gint *w, gint *h)
{
PARM_DEF(first, 1);
PARM_DEF(first+1, 1);
PARM_DEF(first+2, HEIGHT-1);
PARM_DEF(first+3, WIDTH-1);
*y = CURSOR_Y_TRANS(P(first)-1);
*x = CURSOR_X_TRANS(P(first+1)-1);
*h = P(first+2) - P(first) + 1;
*w = P(first+3) - P(first+1) + 1;
}
/*
*
*/
static void emul_screen_size_allocate(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
{
TemuEmul *S = user_data;
/* Uhm. Hack. */
if (S->scroll_height_full || (S->scroll_top+S->scroll_height) > HEIGHT)
S->scroll_height = HEIGHT - S->scroll_top;
emul_cursor_cleared(S);
emul_move_cursor(S, S->cursor_x, S->cursor_y);
emul_cursor_draw(S);
S->cursor_redraw = FALSE;
S->cursor_hide = FALSE;
}
TemuEmul *temu_emul_new(TemuScreen *screen)
{
TemuEmul *S;
S = g_new(TemuEmul, 1);
S->owner = screen;
emul_init(S);
g_signal_connect(G_OBJECT(screen), "size-allocate",
G_CALLBACK(emul_screen_size_allocate), S);
return S;
}
void temu_emul_destroy(TemuEmul *emul)
{
if (emul->out)
g_free (emul->out);
g_free(emul);
}
void temu_emul_emulate(TemuEmul *S, const gchar *text, gint length)
{
vt52x_parse(S, text, length);
if (S->cursor_redraw) {
emul_cursor_draw(S);
S->cursor_redraw = FALSE;
}
}
/* *** KEYCODE TRANSLATION / RESPONSES *** */
/* Ensure there are at least two bytes free in the buffer. */
static gchar *emul_stpcpy_c1(TemuEmul *S, gchar *buffer, gint c1)
{
if (!S->send_C1_lo) {
*buffer++ = c1;
} else {
*buffer++ = C_ESC;
*buffer++ = c1 - 0x40;
}
return buffer;
}
void emul_add_output(TemuEmul *S, const gchar *out, gint count)
{
if ((S->outs+count) > S->outmax) {
S->out = g_realloc(S->out, S->outs + count);
S->outmax = S->outs + count;
}
memcpy(S->out + S->outs, out, count);
S->outs += count;
}
gssize temu_emul_get_responses(TemuEmul *S, gchar *buffer, gint len)
{
if (S->outs < len)
len = S->outs;
memcpy(buffer, S->out, len);
S->outs -= len;
g_memmove(S->out, S->out + len, S->outs);
return len;
}
gboolean temu_emul_translate(TemuEmul *S, GdkEventKey *key, gchar buffer[16], gint *count)
{
gchar *p = buffer;
gint n;
#define STRCAT_C1(c1) (p = emul_stpcpy_c1(S, p, (c1)))
#define STRCAT_MODIFIER(extra) do{ \
n = 1; \
if (key->state & GDK_CONTROL_MASK) n += 4; \
if (key->state & GDK_MOD1_MASK) n += 2; \
if (key->state & GDK_SHIFT_MASK) n += 1; \
if (n != 1) p += g_sprintf(p, "%s;%d", (extra), n);\
} while(0)
switch (key->keyval) {
case GDK_BackSpace:
if (!!(S->o_DECBKM) ^ !!(key->state & GDK_CONTROL_MASK))
key->keyval = C_BS;
else
key->keyval = C_DEL;
break;
case GDK_Return:
key->keyval = C_CR;
break;
case GDK_Tab:
key->keyval = C_HT;
break;
case GDK_Escape:
*p++ = C_ESC;
goto done;
case GDK_at: case GDK_2: case GDK_space:
if (key->state & GDK_CONTROL_MASK) {
*p++ = '\0';
goto done;
}
break;
case GDK_minus:
if (key->state & GDK_CONTROL_MASK) key->keyval = '_' - GDK_A + 1;
break;
case GDK_grave:
if (key->state & GDK_CONTROL_MASK) key->keyval = '~' - GDK_a + 1;
break;
case GDK_A ... GDK_underscore:
if (key->state & GDK_CONTROL_MASK) key->keyval -= GDK_A - 1;
break;
case GDK_a ... GDK_asciitilde:
if (key->state & GDK_CONTROL_MASK) key->keyval -= GDK_a - 1;
break;
/* Navigation keys */
case GDK_Insert:
STRCAT_C1(C_CSI); *p++ = '2'; STRCAT_MODIFIER(""); *p++ = '~';
goto done;
case GDK_Delete:
STRCAT_C1(C_CSI); *p++ = '3'; STRCAT_MODIFIER(""); *p++ = '~';
goto done;
case GDK_Page_Up:
STRCAT_C1(C_CSI); *p++ = '5'; STRCAT_MODIFIER(""); *p++ = '~';
goto done;
case GDK_Page_Down:
STRCAT_C1(C_CSI); *p++ = '6'; STRCAT_MODIFIER(""); *p++ = '~';
goto done;
case GDK_End:
if (S->o_DECCKM) {
STRCAT_C1(C_SS3);
STRCAT_MODIFIER("1");
*p++ = 'F';
} else {
/* FIXME: xterm/aterm say CSI <mod> F */
STRCAT_C1(C_CSI);
*p++ = '4';
STRCAT_MODIFIER("");
*p++ = '~';
}
goto done;
case GDK_Home:
if (S->o_DECCKM) STRCAT_C1(C_SS3);
else STRCAT_C1(C_CSI);
STRCAT_MODIFIER("1"); *p++ = 'H';
goto done;
/* Arrow keys */
case GDK_Up:
if (S->o_DECCKM) STRCAT_C1(C_SS3);
else STRCAT_C1(C_CSI);
STRCAT_MODIFIER("1"); *p++ = 'A';
goto done;
case GDK_Down:
if (S->o_DECCKM) STRCAT_C1(C_SS3);
else STRCAT_C1(C_CSI);
STRCAT_MODIFIER("1"); *p++ = 'B';
goto done;
case GDK_Right:
if (S->o_DECCKM) STRCAT_C1(C_SS3);
else STRCAT_C1(C_CSI);
STRCAT_MODIFIER("1"); *p++ = 'C';
goto done;
case GDK_Left:
if (S->o_DECCKM) STRCAT_C1(C_SS3);
else STRCAT_C1(C_CSI);
STRCAT_MODIFIER("1"); *p++ = 'D';
goto done;
/* Key pad */
case GDK_KP_F1 ... GDK_KP_F4:
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = key->keyval - GDK_KP_F1 + 'P';
goto done;
case GDK_KP_0 ... GDK_KP_9:
if (S->o_DECKPAM) {
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = key->keyval - GDK_KP_0 + 'p';
goto done;
}
key->keyval = key->keyval - GDK_KP_0 + '0';
break;
#define HANDLE_KP_KEY(kpam,shift,pre,mid,end) \
if (S->o_DECKPAM) { \
if (key->state & GDK_MOD1_MASK) \
*p++ = C_ESC; \
\
STRCAT_C1(C_SS3); \
*p++ = (kpam); \
goto done; \
} \
if (key->state & GDK_SHIFT_MASK) { \
key->keyval = (shift); \
break; \
} \
STRCAT_C1(C_CSI); \
p = g_stpcpy(p, (pre)); \
STRCAT_MODIFIER(mid); \
p = g_stpcpy(p, (end)); \
goto done
case GDK_KP_Insert: HANDLE_KP_KEY('p', '0', "2", "", "~");
case GDK_KP_End: HANDLE_KP_KEY('q', '1', "4", "", "~");
case GDK_KP_Down: HANDLE_KP_KEY('r', '2', "", "1", "B");
case GDK_KP_Page_Down:HANDLE_KP_KEY('s', '3', "6", "", "~");
case GDK_KP_Left: HANDLE_KP_KEY('t', '4', "", "1", "D");
case GDK_KP_Begin: /* 5... */
/* FIXME: DEC says send nothing */
/* xterm says send CSI <mod> E */
/* aterm says send SS3 u */
HANDLE_KP_KEY('u', '5', "", "1", "E");
case GDK_KP_Right: HANDLE_KP_KEY('v', '6', "", "1", "C");
case GDK_KP_Home: HANDLE_KP_KEY('w', '7', "", "1", "H");
case GDK_KP_Up: HANDLE_KP_KEY('x', '8', "", "1", "A");
case GDK_KP_Page_Up: HANDLE_KP_KEY('y', '9', "5", "", "~");
case GDK_KP_Enter:
if (S->o_DECKPAM) {
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = 'M';
goto done;
}
key->keyval = '\r';
break;
case GDK_KP_Multiply:
if (S->o_DECKPAM) {
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = 'j'; /* FIXME: DEC manual does not say what this is. */
goto done;
}
key->keyval = '*';
break;
case GDK_KP_Add:
if (S->o_DECKPAM) {
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = 'l'; /* FIXME: xterm/aterm say k.. */
goto done;
}
key->keyval = '+';
break;
case GDK_KP_Subtract:
if (S->o_DECKPAM) {
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = 'm';
goto done;
}
key->keyval = '-';
break;
case GDK_KP_Decimal:
if (S->o_DECKPAM) {
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = 'n';
goto done;
}
if (key->state & GDK_SHIFT_MASK) {
key->keyval = '.';
break;
}
key->keyval = C_DEL;
break;
case GDK_KP_Divide:
if (S->o_DECKPAM) {
if (key->state & GDK_MOD1_MASK)
*p++ = C_ESC;
STRCAT_C1(C_SS3);
*p++ = 'o'; /* FIXME: DEC manual does not say what this is. */
goto done;
}
key->keyval = '/';
break;
/* Fn */
case GDK_F1 ... GDK_F35: /* ... GDK_F20 */
/* There are several breaks... */
n = key->keyval - GDK_F1 + 11;
if (key->keyval > GDK_F5) n++;
if (key->keyval > GDK_F10) n++;
if (key->keyval > GDK_F14) n++;
if (key->keyval > GDK_F16) n += 2;
STRCAT_C1(C_CSI);
p += g_sprintf(p, "%d", n);
STRCAT_MODIFIER("");
*p++ += '~';
goto done;
case GDK_Break:
if (key->state & GDK_CONTROL_MASK) {
p = g_stpcpy(p, EMUL_ENQ_REPLY);
goto done;
}
}
if (key->keyval >= 0x00 && key->keyval <= 0xff) {
if (key->state & GDK_MOD1_MASK) *p++ = C_ESC;
*p++ = key->keyval;
if (S->o_LNM && key->keyval == C_CR)
*p++ = '\n';
goto done;
}
done:
*count = p - buffer;
if (!S->o_SRM)
emul_add_output(S, buffer, *count);
return (*count)?TRUE:FALSE;
}
/* *** PARSER *** */
/* I would love to have this part auto-generated.
That's not the way it turned out, though. */
#define BEGIN_STR(start,type) do { \
S->state = type; \
S->strt = start; \
S->pre = S->intr = S->fin = 0; \
memset(S->parm, 0, sizeof(S->parm)); \
S->parms = 0; \
S->strs = 0; \
} while (0)
#define BEGIN_ESC() do { \
S->state = VT_ESC; \
S->strt = 0; \
S->pre = S->intr = S->fin = 0; \
} while (0);
#define global_C0_cases \
case C_NUL: \
continue; \
case C_ESC: \
BEGIN_ESC(); continue; \
case (C_NUL+1) ... (C_CAN-1): \
case (C_CAN+1) ... (C_SUB-1): \
case (C_ESC+1) ... (C0_PAST-1): \
vt52x_C0(S, ch); \
continue;
#define data_C0() \
switch (ch) { \
case C_CAN: \
S->state = VT_GROUND; continue; \
case C_SUB: \
emul_add_glyph(S, VT_GLYPH_BACKWARDS_QUESTION); \
S->state = VT_GROUND; continue; \
}
#define nonground_C0() \
switch (ch) { \
case C_CAN: \
S->state = VT_GROUND; continue; \
case C_SUB: \
emul_add_glyph(S, VT_GLYPH_BACKWARDS_QUESTION); \
S->state = VT_GROUND; continue; \
case C_DEL: \
continue; \
global_C0_cases \
}
#define state_parms(PREFIX) \
nonground_C0(); \
switch (ch) { \
case '0' ... '9': \
S->parm[S->parms] *= 10; \
S->parm[S->parms] += ch - '0'; \
continue; \
case ';': \
if (S->parms < (VTPARSE_MAX_PARAMS-1)) \
++S->parms; \
continue; \
case '?': \
case ':': case '<': case '=': case '>': \
if (S->pre && S->pre != ch) { \
S->state = PREFIX ## _IGNORE; \
continue; \
} \
S->pre = ch; \
continue; \
case 0x20 ... 0x2f: \
if (S->intr && S->intr != ch) { \
S->state = PREFIX ## _IGNORE; \
continue; \
} \
S->intr = ch; \
continue; \
default: \
S->state = PREFIX ## _FIN; \
goto PREFIX ## _fin; \
}
static void vt52x_parse(TemuEmul *S, const gchar *text, gint len)
{
for (; len--; text++) {
guchar ch = (guchar)*text;
#if OUTPUT_EVERYTHING
write(STDOUT_FILENO, &ch, 1);
#endif
switch (S->state) {
case VT_GROUND:
/* FIXME: Doesn't handle invalid sequences properly,
and may produce undesired effects, the extents of
which I am not fully aware.
*/
if (S->chars) {
/* We're in the middle of a multibyte sequence,
assume the host is kind enough to send them
without _real_ escapes between them.. */
emul_add_char(S, ch);
continue;
}
switch (ch) {
global_C0_cases
case C_CAN:
continue;
case C_SUB:
emul_add_glyph(S, VT_GLYPH_BACKWARDS_QUESTION);
continue;
}
if (S->do_C1)
if (ch >= 0x80 && ch <= 0x9f) {
C1:
switch (ch) {
case C_CSI: BEGIN_STR(C_CSI, VT_CSI); continue;
case C_DCS:
case C_APC:
case C_PM: BEGIN_STR(ch, VT_STR); continue;
case C_OSC: BEGIN_STR(ch, VT_STRX); continue;
default:
vt52x_C1(S, ch);
continue;
}
}
emul_add_char(S, ch);
continue;
case VT_ESC:
nonground_C0();
switch (ch) {
case 0x20 ... 0x2f:
if (S->intr && S->intr != ch) {
S->state = VT_ESC_IGNORE;
continue;
}
S->intr = ch;
continue;
case 0x30 ... 0x7e:
case 0x80 ... 0xff:
if (!S->intr && S->do_C1_lo)
if (ch >= 0x40 && ch <= 0x5f) {
ch = (ch&0x1f) | 0x80;
S->state = VT_GROUND;
goto C1;
}
S->state = VT_GROUND;
S->fin = ch;
vt52x_esc(S);
continue;
default:
S->state = VT_GROUND;
continue;
}
case VT_ESC_IGNORE:
nonground_C0();
switch (ch) {
case 0x20 ... 0x2f:
continue;
case 0x30 ... 0x7e:
case 0x80 ... 0xff:
default:
S->state = VT_GROUND;
continue;
}
case VT_CSI: state_parms(VT_CSI);
case VT_CSI_FIN:
nonground_C0();
switch (ch) {
case 0x40 ... 0x7e:
case 0x80 ... 0xff: VT_CSI_fin:
S->state = VT_GROUND;
S->fin = ch;
vt52x_csi(S);
continue;
default:
S->state = VT_CSI_IGNORE;
continue;
}
case VT_CSI_IGNORE:
nonground_C0();
switch (ch) {
case 0x20 ... 0x2f:
case 0x30 ... 0x3f:
continue;
case 0x40 ... 0x7e:
case 0x80 ... 0xff:
default:
S->state = VT_GROUND;
continue;
}
case VT_STR: state_parms(VT_STR);
case VT_STR_FIN:
nonground_C0();
switch (ch) {
case 0x40 ... 0x7e:
case 0x80 ... 0xff: VT_STR_fin:
S->fin = ch;
S->state = VT_STR_DATA;
continue;
default:
S->state = VT_STR_IGNORE;
continue;
}
case VT_STR_DATA:
data_C0();
switch (ch) {
case C_NUL ... (C_CAN-1):
case (C_CAN+1) ... (C_SUB-1):
case (C_ESC+1) ... (C_ST-1):
case (C_ST+1) ... 0xff:
if (S->strs < VTPARSE_MAX_STR)
S->str[S->strs++] = ch;
continue;
case C_ST:
S->state = VT_GROUND;
switch (S->strt) {
case C_DCS: vt52x_dcs(S); break;
case C_APC: vt52x_apc(S); break;
}
continue;
case C_ESC:
switch (S->strt) {
case C_DCS: vt52x_dcs(S); break;
case C_APC: vt52x_apc(S); break;
}
BEGIN_ESC();
continue;
}
case VT_STR_IGNORE:
data_C0();
switch (ch) {
case C_ST:
S->state = VT_GROUND;
continue;
default:
continue;
}
case VT_STRX:
data_C0();
switch (ch) {
case C_NUL ... (C_BEL-1):
case (C_BEL+1) ... (C_CAN-1):
case (C_CAN+1) ... (C_SUB-1):
case (C_ESC+1) ... (C_ST-1):
case (C_ST+1) ... 0xff:
if (S->strs < VTPARSE_MAX_STR)
S->str[S->strs++] = ch;
continue;
case C_ST:
case C_BEL:
S->state = VT_GROUND;
vt52x_osc(S);
continue;
case C_ESC:
vt52x_osc(S);
BEGIN_ESC();
continue;
}
}
}
}
/* *** ESCAPES *** */
static void vt52x_C0(TemuEmul *S, guchar c0)
{
switch (c0) {
case C_ENQ:
//IMPL("ENQ", "Enquiry", "partially");
IMPL("ENQ", "Enquiry", "maybe");
/* FIXME:? xterm (and a real VT) will let you configure the ENQ reply */
/* OTOH, usually this just fracks stuff up. */
//emul_add_output(S, EMUL_ENQ_REPLY, 4);
break;
case C_BEL:
IMPL("BEL", "Audible bell", "fully");
temu_screen_emit_bell(T);
break;
case C_BS:
IMPL("BS", "Backspace", "fully");
emul_BS(S);
break;
case C_HT: /* HT: Horizontal Tab */
emul_tab_next(S);
break;
case C_VT:
case C_FF:
case C_LF: emul_LF(S); break;
case C_CR: emul_CR(S); break;
case C_SO:
IMPL("SO", "Shift-Out", "fully");
S->charset = CHARSET_GRAPHIC;
break;
case C_SI:
IMPL("SI", "Shift-In", "fully");
S->charset = CHARSET_UTF8;
break;
case C_DC1:
NOTIMPL("DC1", "aka XON", "maybe");
break;
case C_DC3:
NOTIMPL("DC3", "aka XOFF", "maybe");
break;
/* C_CAN/C_SUB/C_ESC/C_DEL handled elsewhere */
}
}
static void vt52x_C1(TemuEmul *S, guchar c1)
{
switch (c1) {
case C_IND: emul_IND(S); break;
case C_NEL: emul_CR(S); emul_IND(S); break;
case C_HTS: /* HTS: Horizontal Tab Set */
emul_tab_set(S, S->cursor_x);
break;
case C_RI: emul_RI(S); break;
case C_SS2:
NOTIMPL("SS2", "Single Shift G2", "definitely");
break;
case C_SS3:
NOTIMPL("SS3", "Single Shift G3", "definitely");
break;
case C_SPA:
NOTIMPL("SPA", "Start Guarded Area", "maybe");
break;
case C_EPA:
NOTIMPL("EPA", "End Guarded Area", "maybe");
break;
case C_SOS: /* ignored */ break;
case C_DECID:
NOTIMPL("DECID", "Send Identification", "definitely");
break;
case C_ST: /* ignored */ break;
/* C_DCS/C_CSI/C_OSC/C_PM/C_APC handled elsewhere */
}
}
static void vt52x_esc(TemuEmul *S)
{
switch (E(S->strt,S->pre,S->intr,S->fin)) {
case E(0,0,'(','1'):
NOTIMPL("DDD3", "ascii -> G0", "mmaybe");
break;
case E(0,0,')','1'): /* NOTIMPL, DDD1: */
NOTIMPL("DDD1", "set DECRLM/DECHEBM/DECHEM", "mmaybe");
break;
case E(0,0,'#','3'):
NOTIMPL("DECDHL", "Double-Width, Double-Height line (Top Half)", "probably");
break;
case E(0,0,'#','4'):
NOTIMPL("DECDHL", "Double-Width, Double-Height line (Bottom Half)", "probably");
break;
case E(0,0,'#','5'):
NOTIMPL("DECSWL", "Single-Width, Single-Height line", "probably");
break;
case E(0,0,'#','6'):
NOTIMPL("DECDWL", "Double-Width, Single-Height line", "probably");
break;
case E(0,0,0,'6'):
IMPL("DECBI", "Back Index", "fully");
emul_DECBI(S);
break;
case E(0,0,0,'7'): /* DECSC: Save Cursor */
emul_cursor_save(S);
break;
case E(0,0,0,'8'): /* DECRC: Restore Cursor */
emul_cursor_restore(S);
break;
case E(0,0,0,'9'):
IMPL("DECFI", "Forward Index", "fully");
emul_DECFI(S);
break;
case E(0,0,'#','8'): /* DECALN: Screen Alignment Pattern */
/* Actually, the VT520/525 documentation says
this displays greyscale and color bars */
/* But, what the hey. */
{
temu_cell_t cell;
S->scroll_top = 0; S->scroll_height = HEIGHT;
emul_move_cursor(S, 0, 0);
cell = ECELL;
cell.ch.glyph = L'E';
temu_screen_fill_rect(T, 0, 0, WIDTH, HEIGHT, &cell);
}
break;
case E(0,0,0,'='):
IMPL("DECKPAM", "Keypad Application Modes", "somewhat; GtkIMContext intercepts; inconsistent docs");
S->o_DECKPAM = TRUE;
break;
case E(0,0,0,'>'):