-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvt100.c
598 lines (569 loc) · 13.5 KB
/
vt100.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
/*
* vt100.c ANSI/VT102 emulator code
*
* Copyright (c) 1991-1995 Miquel van Smoorenburg
* Copyright (c) 2015-2017, Parallels International GmbH
* Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
*
* This file is part of OpenVZ. OpenVZ 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 2 of the License,
* or (at your option) any later version.
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
* Schaffhausen, Switzerland.
*/
#include <fcntl.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include <rfb/keysym.h>
#include "vt100.h"
/*
* The global variable esc_s holds the escape sequence status:
* 0 - normal
* 1 - ESC
* 2 - ESC [
* 3 - ESC [ ?
* 4 - ESC (
* 5 - ESC )
* 6 - ESC #
* 7 - ESC P
*/
static int esc_s = 0;
#define ESC 27
#define ESCPARMS_SIZE 16
static unsigned char vt_fg; /* Standard foreground color. */
static unsigned char vt_bg; /* Standard background color. */
static unsigned short escparms[ESCPARMS_SIZE]; /* Cumulated escape sequence. */
static int ptr; /* Index into escparms array. */
static short newy1 = 0; /* Current size of scrolling region. */
static short newy2 = 23;
static void state1(vncConsole *console, unsigned char c);
static void state2(vncConsole *console, unsigned char c);
static void state3(vncConsole *console, unsigned char c);
static void state6(unsigned char c);
void vt_init(vncConsole *console)
{
memset(escparms, 0, sizeof(escparms));
memset(console->screenBuffer, ' ', console->width * console->height);
console->x=0;
console->y=0;
console->cursorActive=TRUE;
vt_fg = WHITE;
vt_bg = BLACK;
}
void vt_out(vncConsole *console, unsigned char c)
{
static unsigned char last_ch;
int go_on = 0;
if (c == 0)
return;
last_ch = c;(void)last_ch;
if (esc_s) {
fprintf(stdout, "_%c ", c);
} else {
if (isprint(c)) {
fprintf(stdout, "%c", c);
} else {
fprintf(stdout, "\n0x%x ", c);
}
}
fflush(stdout);
/* Process <31 chars first, even in an escape sequence. */
switch (c) {
case '\r': /* Carriage return */
vcPutCharColour(console, c, vt_fg, vt_bg);
break;
case '\t': /* Non - destructive TAB */
vcPutCharColour(console, c, vt_fg, vt_bg);
break;
case 013: /* Old Minix: CTRL-K = up */
fprintf(stdout, "Old Minix: CTRL-K = up\n");
break;
case '\f': /* Form feed: clear screen. */
fprintf(stdout, "Form feed: clear screen\n");
break;
case 14:
case 15: /* Change character set. Not supported. see original vt100.c */
break;
case 24:
case 26: /* Cancel escape sequence. */
esc_s = 0;
break;
case ESC: /* Begin escape sequence */
esc_s = 1;
break;
case 128+ESC: /* Begin ESC [ sequence. */
esc_s = 2;
break;
case '\b': /* Backspace */
vcHideCursor(console);
console->x--;
// vcPutCharColour(console, ' ', vt_fg, vt_bg);
// console->x--;
vcDrawCursor(console);
break;
case '\n':
vcPutCharColour(console, c, vt_fg, vt_bg);
break;
case 7: /* Bell */
rfbSendBell( console->screen );
break;
default:
go_on = 1;
break;
}
if (!go_on)
return;
/* Now see which state we are in. */
switch (esc_s) {
case 0: /* Normal character */
vcPutCharColour(console, c, vt_fg, vt_bg);
break;
case 1: /* ESC seen */
state1(console, c);
break;
case 2: /* ESC [ ... seen */
state2(console, c);
break;
case 3:
state3(console, c);
break;
case 4:
fprintf(stdout, "Switch Character Sets 4\n");
break;
case 5:
fprintf(stdout, "Switch Character Sets 5\n");
break;
case 6:
state6(c);
break;
case 7:
fprintf(stdout, "Device dependant control strings\n");
break;
}
}
/*
* Escape code handling.
*/
/*
* ESC was seen the last time. Process the next character.
*/
static void state1(vncConsole *console, unsigned char c)
{
switch(c) {
case '[': /* ESC [ */
esc_s = 2;
return;
case '(': /* ESC ( */
esc_s = 4;
return;
case ')': /* ESC ) */
esc_s = 5;
return;
case '#': /* ESC # */
esc_s = 6;
return;
case 'P': /* ESC P (DCS, Device Control String) */
esc_s = 7;
return;
case 'D': /* Cursor down */
fprintf(stdout, "Cursor down\n");
break;
case 'M': /* Cursor up */
// locate or scroll
fprintf(stdout, "Cursor up\n");
break;
case 'E': /* CR + NL */
vcPutChar(console, '\r');
vcPutChar(console, '\n');
break;
case '7': /* Save attributes and cursor position */
case 's':
fprintf(stdout, "Save attributes and cursor position\n");
break;
case '8': /* Restore them */
case 'u':
fprintf(stdout, "Restore attributes and cursor position\n");
break;
case '=': /* Keypad into applications mode */
fprintf(stdout, "Keypad into applications mode\n");
break;
case '>': /* Keypad into numeric mode */
fprintf(stdout, "Keypad into numeric mode\n");
break;
case 'Z': /* Report terminal type */
fprintf(stdout, "Report terminal type\n");
break;
case 'c': /* Reset to initial state */
fprintf(stdout, "Reset to initial state\n");
vcHideCursor(console);
vcReset(console);
break;
case 'H': /* Set tab in current position */
fprintf(stdout, "Set tab in current position\n");
break;
case 'N': /* G2 character set for next character only*/
case 'O': /* G3 " " */
case '<': /* Exit vt52 mode */
default:
/* ALL IGNORED */
break;
}
esc_s = 0;
}
/* ESC [ ... [hl] seen. */
static void ansi_mode(int on_off)
{
int i;
for (i = 0; i <= ptr; i++) {
switch (escparms[i]) {
case 4: /* Insert mode */
fprintf(stdout, "Insert mode %d\n", on_off);
break;
case 20: /* Return key mode */
fprintf(stdout, "Return key mode %d\n", on_off);
break;
}
}
}
/*
* ESC [ ... was seen the last time. Process next character.
*/
static void state2(vncConsole *console, unsigned char c)
{
unsigned short x, y, f;
unsigned char attr;
/* See if a number follows */
if (c >= '0' && c <= '9') {
escparms[ptr] = 10*escparms[ptr] + c - '0';
return;
}
/* Separation between numbers ? */
if (c == ';') {
if (ptr < ESCPARMS_SIZE - 1)
ptr++;
return;
}
/* ESC [ ? sequence */
if (escparms[0] == 0 && ptr == 0 && c == '?')
{
esc_s = 3;
return;
}
/* Process functions with zero, one, two or more arguments */
switch (c) {
case 'A':
case 'B':
case 'C':
case 'D':
fprintf(stdout, "Cursor motion [%c, %d]\n", c, escparms[0]);
if ((f = escparms[0]) == 0)
f = 1;
x = console->x;
y = console->y;
x += f * ((c == 'C') - (c == 'D'));
if (x < 0)
x = 0;
if (x >= console->width)
x = console->width - 1;
if (c == 'B') { /* Down. */
y += f;
if (y >= console->height)
y = console->height - 1;
if (y >= newy2 + 1)
y = newy2;
} else if (c == 'A') { /* Up. */
y -= f;
if (y < 0)
y = 0;
if (y <= newy1 - 1)
y = newy1;
}
vcHideCursor(console);
console->x = x;
console->y = y;
fprintf(stdout, "x=%d y=%d\n", console->x, console->y);
break;
case 'H':
fprintf(stdout, "Set cursor position (%d, %d)\n", escparms[0], escparms[1]);
if ((y = escparms[0]) == 0)
y = 1;
if ((x = escparms[1]) == 0)
x = 1;
// if (vt_om)
// y += newy1;
if (x >= console->width)
x = console->width;
if (y >= console->height)
y = console->height;
vcHideCursor(console);
console->x = x - 1;
console->y = y - 1;
break;
case 'X': /* Character erasing (ECH) */
fprintf(stdout, "Character erasing (ECH)\n");
break;
case 'K': /* Line erasing */
fprintf(stdout, "Line erasing (%d)\n", escparms[0]);
switch (escparms[0]) {
case 0:
/* Clear to end of line ??? or Backspace? */
f = console->x;
for( ; console->x < console->width; )
vcPutCharColour(console, ' ', vt_fg, vt_bg);
vcHideCursor(console);
console->x = f;
break;
case 1:
/* Clear to begin of line. */
f = console->x;
for( console->x = 0; console->x < f; )
vcPutCharColour(console, ' ', vt_fg, vt_bg);
break;
case 2:
/* Clear entire line. */
f = console->x;
for( console->x = 0; console->x < console->width; )
vcPutCharColour(console, ' ', vt_fg, vt_bg);
vcHideCursor(console);
console->x = f;
break;
}
break;
case 'J': /* Screen erasing */
{
fprintf(stdout, "Screen erasing (%d)\n", escparms[0]);
switch (escparms[0]) {
case 0:
/* Clear to end of screen */
//mc_wclreos(vt_win);
//break;
case 1:
/* Clear to begin of screen. */
//mc_wclrbos(vt_win);
//break;
case 2:
/* Clear a window. */
memset(console->screen->frameBuffer, BLACK,
console->screen->width * console->screen->height);
memset(console->screenBuffer, ' ', console->width * console->height);
#ifdef USE_ATTRIBUTE_BUFFER
memset(console->attributeBuffer, 0x07, console->width * console->height);
#endif
rfbMarkRectAsModified(console->screen, 0, 0, console->screen->width, console->screen->height);
//mc_winclr(vt_win);
break;
}
break;
}
case 'n': /* Requests / Reports */
fprintf(stdout, "Requests / Reports\n");
break;
case 'c': /* Identify Terminal Type */
fprintf(stdout, "Identify Terminal Type\n");
break;
case 'x': /* Request terminal parameters. */
fprintf(stdout, "Request terminal parameters\n");
break;
case 's': /* Save attributes and cursor position */
fprintf(stdout, "Save attributes and cursor position\n");
break;
case 'u': /* Restore them */
fprintf(stdout, "Restore attributes and cursor position\n");
break;
case 'h':
ansi_mode(1);
break;
case 'l':
ansi_mode(0);
break;
case 'g': /* Clear tab stop(s) */
fprintf(stdout, "Clear tab stop(s)\n");
break;
case 'm': /* Set attributes */
{
fprintf(stdout, "Set attributes\n");
// attr = mc_wgetattr((vt_win));
for (f = 0; f <= ptr; f++) {
if (escparms[f] >= 30 && escparms[f] <= 37)
vt_fg = (vt_fg & 15) + (escparms[f] - 30);
if (escparms[f] >= 40 && escparms[f] <= 47)
vt_bg = (vt_bg & 240) + (escparms[f] - 40);
switch (escparms[f]) {
case 0:
attr = XA_NORMAL;
vt_fg = WHITE;
vt_bg = BLACK;
break;
case 1:
attr |= XA_BOLD;
break;
case 4:
attr |= XA_UNDERLINE;
break;
case 5:
attr |= XA_BLINK;
break;
case 7:
attr |= XA_REVERSE;
break;
case 22: /* Bold off */
attr &= ~XA_BOLD;
break;
case 24: /* Not underlined */
attr &=~XA_UNDERLINE;
break;
case 25: /* Not blinking */
attr &= ~XA_BLINK;
break;
case 27: /* Not reverse */
attr &= ~XA_REVERSE;
break;
case 39: /* Default fg color */
vt_fg = 0x07;
break;
case 49: /* Default bg color */
vt_bg = 0;
break;
}
}
// mc_wsetattr(vt_win, attr);
break;
}
case 'L': /* Insert lines */
if ((f = escparms[0]) == 0)
f = 1;
vcInsertLines( console, console->y, f );
fprintf(stdout, "Insert lines\n");
break;
case 'M': /* Delete lines */
if ((f = escparms[0]) == 0)
f = 1;
vcDeleteLines( console, console->y, f );
fprintf(stdout, "Delete lines\n");
break;
case 'P': /* Delete Characters */
if ((f = escparms[0]) == 0)
f = 1;
vcDeleteCharacters( console, f );
fprintf(stdout, "Delete Characters\n");
break;
case '@': /* Insert Characters */
if ((f = escparms[0]) == 0)
f = 1;
vcInsertCharacters( console, f );
fprintf(stdout, "Insert Characters\n");
break;
case 'r': /* Set scroll region */
y = escparms[0];
x = escparms[1];
if (y == 0)
y = 1;
if (x == 0 || x > console->height)
x = console->height;
if (y > newy2)
y = newy2 + 1;
console->sstart = y - 1;
console->sheight = x;
fprintf(stdout, "Set scroll region (%d, %d)\n", console->sstart, console->sheight);
break;
case 'i': /* Printing */
case 'y': /* Self test modes */
default:
/* IGNORED */
break;
}
/* Ok, our escape sequence is all done */
esc_s = 0;
ptr = 0;
memset(escparms, 0, sizeof(escparms));
return;
}
/* ESC [? ... [hl] seen. */
static void dec_mode(vncConsole *console, int on_off)
{
int i;
(void)on_off;
for (i = 0; i <= ptr; i++) {
switch (escparms[i]) {
case 5: /* Visible Bell */
if( on_off )
rfbSendBell( console->screen );
break;
case 7: /* Auto wrap */
fprintf(stdout, "Auto wrap");
break;
case 25: /* Cursor on/off */
fprintf(stdout, "Cursor");
if( on_off )
vcDrawCursor( console );
else
vcHideCursor( console );
console->dontDrawCursor = !on_off;
break;
default: /* Mostly set up functions */
/* IGNORED */
fprintf(stdout, "%d parameter IGNORED", escparms[i]);
break;
}
}
fprintf(stdout, " %s\n", on_off?"on":"off");
}
/*
* ESC [ ? ... seen.
*/
static void state3(vncConsole *console, unsigned char c)
{
/* See if a number follows */
if (c >= '0' && c <= '9') {
escparms[ptr] = 10*escparms[ptr] + c - '0';
return;
}
switch (c) {
case 'h':
dec_mode(console, 1);
break;
case 'l':
dec_mode(console, 0);
break;
case 'i': /* Printing */
case 'n': /* Request printer status */
default:
/* IGNORED */
break;
}
esc_s = 0;
ptr = 0;
memset(escparms, 0, sizeof(escparms));
return;
}
/*
* ESC # Seen.
*/
static void state6(unsigned char c)
{
/* Double height, double width and selftests. */
switch (c) {
case '8':
fprintf(stdout, "Selftest\n");
break;
default:
/* IGNORED */
break;
}
esc_s = 0;
}