[c427d7]: joe / vt.c Maximize Restore History

Download this file

vt.c    513 lines (454 with data), 10.5 kB

  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
/* Terminal emulator */
#define MAXARGS 2
enum vt_state {
vt_idle,
vt_esc,
vt_args,
};
struct vt_context {
enum vt_state state;
/* Numeric arguments */
int argv[MAXARGS + 1];
int argc;
BW *bw;
};
void vt_beep(BW *bw)
{
ttputc(7);
}
void vt_type(BW *bw, int c)
{
int o = bw->o.overtype;
int pid = bw->pid;
/* Set proper modes.. */
bw->pid = 0;
bw->o.overtype = 1;
utypebw(bw, c);
/* Restore user modes */
bw->o.overtype = o;
bw->pid = pid;
}
void vt_lf(BW *bw)
{
int col = bw->cursor->xcol;
if (!pnextl(bw->cursor)) {
binsc(bw->cursor, '\n');
pgetc(bw->cursor);
}
pcol(bw->cursor, col);
}
void vt_reverse_lf(BW *bw)
{
if (bw->cursor->line) {
int col = bw->cursor->xcol;
pprevl(bw->cursor);
pcol(bw->cursor, col);
} else {
int col = bw->cursor->xcol;
p_goto_bol(bw->cursor);
binsc(bw->cursor, '\n');
pcol(bw->cursor, col);
}
}
void vt_insert_spaces(BW *bw, int n)
{
while (n--)
binsc(bw->cursor, ' ');
}
void vt_cr(BW *bw)
{
p_goto_bol(bw->cursor);
}
void vt_tab(BW *bw)
{
vt_type(vt, '\t');
}
void vt_left(BW *bw, int n)
{
if (n > bw->cursor->xcol)
bw->cursor->xcol = 0;
else
bw->cursor->xcol -= n;
pcol(bw->cursor, bw->cursor->xcol);
}
void vt_right(BW *bw, int n)
{
bw->cursor->xcol += n;
pcol(bw->cursor, bw->cursor->xcol);
}
void vt_up(BW *bw, int n)
{
if (n > bw->cursor->line)
n = bw->cursor->line;
while (n--) {
pprevl(bw->cursor);
}
pcol(bw->cursor, bw->cursor->xcol);
}
void vt_down(BW *bw, int n)
{
if (n > bw->b->eof->line - bw->cursor->line)
n = bw->b->eof->line - bw->cursor->line;
while (n--) {
pnextl(bw->cursor);
}
pcol(bw->cursor, bw->cursor->xcol);
}
void vt_col(BW *bw, int col)
{
bw->cursor->xcol = col;
pcol(bw->cursor, bw->cursor->xcol);
}
void vt_row(BW *bw, int row)
{
// Hmm...
}
void vt_erase_eos(BW *bw)
{
}
void vt_erase_bos(BW *bw)
{
}
void vt_erase_screen(BW *bw)
{
}
void vt_erase_line(BW *bw)
{
P *p = pdup(bw->cursor, USTR "vt_erase_line");
int col = bw->cursor->xcol;
p_goto_bol(bw->cursor);
pnextl(p);
if (bw->cursor->byte == p->byte) {
/* Do nothing */
} else {
bdel(bw->cursor, p);
}
prm(p);
pcol(bw->cursor, col);
}
void vt_erase_bol(BW *bw)
{
}
void vt_erase_eol(BW *bw)
{
P *p = p_goto_eol(pdup(bw->cursor, USTR "vt_erase_eol"));
if (bw->cursor->byte == p->byte) {
/* Do nothing */
} else
bdel(bw->cursor, p);
prm(p);
return 0;
}
void vt_insert_lines(BW *bw, int n)
{
}
void vt_delete_lines(BW *bw, int n)
{
}
void vt_delete_chars(BW *bw, int n)
{
}
void vt_scroll_up(BW *bw, int n)
{
}
void vt_scroll_down(BW *bw, int n)
{
}
void vt_erase_chars(BW *bw, int n)
{
}
Odd VT100 codes:
ESC ( default font
ESC ) alternate font
ESC H Set tab
ESC [ g Clear tab at current position
ESC [ 3 g Clear all tabs
ESC [ i Print screen
ESC [ 1 i Print line
ESC [ 4 i Stop print log
ESC [ 5 i Start print log
VT100 reset sequence:
ESC [ ? 8 h
ESC [ ? 7 h
ESC [ ? 5 l
ESC [ ? 4 l
ESC [ ? 3 l
ESC >
ks
ESC [ ? 1 h application cursor keys
ESC = application keypad
ke
ESC [ ? 1 l normal cursor keys
ESC > normal keypad
eA
ESC ( B
ESC ) 0
ct
ESC [ 3 g
as alternate font
^N
ae
^O
SA (ANSI.SYS)
ESC [ ? 7 h enable line wrap
RA (ANSI.SYS)
ESC [ ? 7 l disable line wrap
ANSI.SYS
u7
ESC [ 6 n
u6
ESC [ %d ; %d R
pk
ESC [ key-ascii ; %s p define key
is
ESC [ m
ESC [ ? 7 h
ESC [ ? 25 h show cursor
ESC [ ? 25 l hide cursor
void vt_data(struct vt_context *vt, unsigned char *dat, int siz)
{
while (siz--) {
unsigned char c = *dat++;
switch (vt->state) {
case vt_idle: {
switch (c) {
case 5: { /* ENQ- Return terminal status */
break;
} case 7: { /* BEL- vt100, linux: Beep */
vt_beep(vt);
break;
} case 8: { /* BS- vt100, linux: Backspace */
vt_left(vt, 1);
break;
} case 9: { /* HT- vt100, linux: Tab */
vt_tab(vt);
break;
} case 10: { /* LF- vt100, linux: LF */
vt_lf(vt);
break;
} case 11: { /* VT- vt100: VT, same as LF */
vt_lf(vt);
} case 12: { /* FF- linux: FF, same as LF */
vt_lf(vt);
break;
} case 13: { /* CR- vt100, linux; Return */
vt_cr(vt);
break;
} case 14: { /* SO- vt100: Alt char set */
break;
} case 15: { /* SI- vt100: Regular char set */
break;
} case 27: { /* ESC */
vt->state = vt_esc;
break;
} case 0x84: { /* Same as ESC D */
vt_lf(vt);
break;
} case 0x85: { /* Same as ESC E */
vt_cr(vt);
vt_lf(vt);
break;
} case 0x88: { /* Same as ESC H */
break;
} case 0x8D: { /* Same as ESC M */
vt_reverse_lf(vt);
break;
} case 0x8E: { /* Same as ESC N */
break;
} case 0x8F: { /* Same as ESC O */
break;
} case 0x90: { /* Same as ESC P */
break;
} case 0x96: { /* Same as ESC V */
break;
} case 0x97: { /* Same as ESC W */
break;
} case 0x98: { /* Same as ESC X */
break;
} case 0x9A: { /* Same as ESC [ c (return terminal id) */
break;
} case 0x9B: { /* Same as ESC [, CSI */
vt->argc = 0;
vt->args[0] = 0;
vt->state = vt_args;
break;
} case 0x9C; { /* Same as ESC \ */
break;
} case 0x9D: { /* Same as ESC ] */
break;
} case 0x9E: { /* Same as ESC ^ */
break;
} case 0x9F: { /* Same as ESC _ */
break;
} default: { /* Type regular character */
if (c >= 32 && c <= 126)
vt_type(vt, c);
break;
}
}
break;
} case vt_esc: {
if (c == 'c') { /* vt100: Reset */
vt->state = vt_idle;
break;
} else if (c == 'M') { /* vt100: Go up, scroll if at edge */
vt_reverse_lf(vt);
vt->state = vt_idle;
break;
} else if (c == 'D') { /* vt100: Go dowm, scroll if at edge */
vt_lf(vt);
vt->state = vt_idle;
break;
} else if (c == 'E') { /* Goto down and to start of line. Scroll if at edge */
vt_cr(vt);
vt_lf(vt);
vt->state = vt_idle;
break;
} else if (c == 'F') { /* Goto lower left of screen */
} else if (c == 'H') { /* Set tab */
break;
} else if (c == '7') { /* vt100: save position and attributes */
break;
} else if (c == '8') { /* vt100: restore position and attributes (home, initially) */
break;
} else if (c == '[') { /* CSI */
vt->argc = 0;
vt->args[0] = 0;
vt->state = vt_args;
} else { /* Ignore the rest... */
vt->state = vt_idle;
}
break;
} case vt_args: {
if (c >= '0' && c <= '9') {
if (vt->argc < MAXARGS)
vt->argv[vt->argc] = vt->argv[vt->argc] * 10 + c - '0';
} else if (c == ';') {
if (vt->argc < MAXARGS) {
++vt->argc;
vt->argv[vt->argc] = 0;
}
} else {
int x;
vt->state = vt_idle;
if (vt->argc < MAXARGS) {
++vt->argc;
}
/* Set default value of arguments */
for (x = 0; x != vt->argc; ++x)
if (vt->argv[x] == 0)
vt->argv[x] = 1;
switch (c) {
case '@': { /* Insert spaces */
vt_insert_spaces(vt, vt_arg(vt, 0, 1));
break;
} case 'A': { /* VT100, ansi.sys Go up */
vt_up(vt, vt_arg(vt, 0, 1));
break;
} case 'B': { /* Go down */
vt_down(vt, vt_arg(vt, 0, 1));
break;
} case 'C': { /* Go right */
vt_right(vt, vt_arg(vt, 0, 1));
break;
} case 'D': { /* Go left */
vt_left(vt, vt_arg(vt, 0, 1));
break;
} case 'E': { /* Beginning of line, n lines down */
vt_cr(vt);
vt_down(vt, vt_arg(vt, 0, 1));
break;
} case 'F': { /* Beginning of line, n line up */
vt_cr(vt);
vt_up(vt, vt_arg(vt, 0, 1));
break;
} case 'G': { /* Goto col, keep row */
vt_col(vt, vt_arg(vt, 0, 1) - 1);
break;
} case 'f': case 'H': { /* With one args: left edge of row, with two; goto row, col */
vt_row(vt, vt_arg(vt, 0, 1) - 1);
vt_col(vt, vt_arg(vt, 1, 1) - 1);
break;
} case 'J': { /* Erase screen: default=to end of screen, 1=to beg. of screen, 2=entire screen */
switch (vt_arg(vt, 0, 0)) {
case 0: {
vt_erase_eos(vt);
break;
} case 1: {
vt_erase_bos(vt);
break;
} case 2: {
vt_erase_screen(vt);
break;
}
break;
} case 'K': { /* Erase line: default=to end of line, 1=to beg. of line, 2=entire line */
switch (vt_arg(vt, 0, 0)) {
case 0: {
vt_erase_eol(vt);
break;
} case 1: {
vt_erase_bol(vt);
break;
} case 2: {
vt_erase_line(vt);
break;
}
}
break;
} case 'L': { /* Insert lines */
vt_insert_lines(vt, vt_arg(vt, 0, 1));
break;
} case 'M': { /* Delete lines */
vt_delete_lines(vt, vt_arg(vt, 0, 1));
break;
} case 'P': { /* Delete characters */
vt_delete_chars(vt, vt_arg(vt, 0, 1));
break;
} case 'S': { /* Scroll screen n lines up (no cursor move) */
vt_scroll_up(vt, vt_arg(vt, 0, 1));
break;
} case 'T': { /* Scroll screen n lines down (no cursor move) */
vt_scroll_down(vt, vt_arg(vt, 0, 1));
break;
} case 'X': { /* Erase n characters to right */
vt_erase_chars(vt, vt_arg(vt, 0, 1));
break;
} case 'd': { /* Goto row, keep col */
vt_row(vt, vt_arg(vt, 0, 1) - 1);
break;
} case 'h': { /* Set mode: 4 for insert mode */
break;
} case 'k': { /* Ansi.sys: ce */
break;
} case 'l': { /* Clear mode: 4 for insert mode */
break;
} case 'm': { /* Attributes:
ansi.sys: 1=bold
vt100 ansi.sys: 4=understrike,
vt100 5=blink,
vt100 ansi.sys: 7=inverse,
ansi.sys: 8= conceal?
ansi.sys: 10= primary font?
ansi.sys: 11-19= alternate font?
vt100 ansi.sys: default=understrike,bold,inverse off
ansi.sys: 30 - 37: foreground color
ansi.sys: 40 - 47: background color */
break;
} case 'n': { /* 6: send cursor position as ESC [ row ; col R */
break;
} case 'r': { /* vt100: Set scrolling region */
vt_set_region(vt, vt_arg(vt, 0, 1) - 1, vt_arg(vt, 1, vt->height) - 1);
break;
} case 's': { /* ansi.sys: save cursor position */
break;
} case 'u': { /* ansi.sys: restore cursor position */
break;
}
}
}
break;
}
}
}
}