1 static void escseq(void);
2 static void escseq_cs(void);
3 static void escseq_g0(void);
4 static void escseq_g1(void);
5 static void escseq_g2(void);
6 static void escseq_g3(void);
7 static void csiseq(void);
8 static void csiseq_da(int c
);
9 static void csiseq_dsr(int c
);
10 static void modeseq(int c
, int set
);
12 /* comments taken from: http://www.ivarch.com/programs/termvt102.shtml */
14 static int readutf8(int c
)
20 while (l
< 6 && c
& (0x40 >> l
))
22 result
= (0x3f >> l
) & c
;
24 result
= (result
<< 6) | (readpty() & 0x3f);
28 /* control sequences */
29 static void ctlseq(void)
33 case 0x09: /* HT horizontal tab to next tab stop */
34 advance(0, 8 - col
% 8, 0);
36 case 0x0a: /* LF line feed */
37 case 0x0b: /* VT line feed */
38 case 0x0c: /* FF line feed */
39 advance(1, (mode
& MODE_AUTOCR
) ? -col
: 0, 1);
41 case 0x08: /* BS backspace one column */
44 case 0x1b: /* ESC start escape sequence */
47 case 0x0d: /* CR carriage return */
50 case 0x00: /* NUL ignored */
51 case 0x07: /* BEL beep */
52 case 0x7f: /* DEL ignored */
54 case 0x05: /* ENQ trigger answerback message */
55 case 0x0e: /* SO activate G1 character set & newline */
56 case 0x0f: /* SI activate G0 character set */
57 case 0x11: /* XON resume transmission */
58 case 0x13: /* XOFF stop transmission, ignore characters */
59 case 0x18: /* CAN interrupt escape sequence */
60 case 0x1a: /* SUB interrupt escape sequence */
61 case 0x9b: /* CSI equivalent to ESC [ */
62 printf("ctlseq: <%d:%c>\n", c
, c
);
65 lazy_put(readutf8(c
), row
, col
);
71 #define ESCM(c) (((c) & 0xf0) == 0x20)
72 #define ESCF(c) ((c) > 0x30 && (c) < 0x7f)
74 /* escape sequences */
75 static void escseq(void)
81 case '[': /* CSI control sequence introducer */
84 case '%': /* CS... escseq_cs table */
87 case '(': /* G0... escseq_g0 table */
90 case ')': /* G1... escseq_g1 table */
93 case '*': /* G2... escseq_g2 table */
96 case '+': /* G3... escseq_g3 table */
99 case '7': /* DECSC save state (position, charset, attributes) */
100 misc_save(&term
->sav
);
102 case '8': /* DECRC restore most recently saved state */
103 misc_load(&term
->sav
);
105 case 'M': /* RI reverse line feed */
108 case 'D': /* IND line feed */
111 case 'E': /* NEL newline */
114 case 'c': /* RIS reset */
115 case 'H': /* HTS set tab stop at current column */
116 case 'Z': /* DECID DEC private ID; return ESC [ ? 6 c (VT102) */
117 case '#': /* DECALN ("#8") DEC alignment test - fill screen with E's */
118 case '>': /* DECPNM set numeric keypad mode */
119 case '=': /* DECPAM set application keypad mode */
120 case 'N': /* SS2 select G2 charset for next char only */
121 case 'O': /* SS3 select G3 charset for next char only */
122 case 'P': /* DCS device control string (ended by ST) */
123 case 'X': /* SOS start of string */
124 case '^': /* PM privacy message (ended by ST) */
125 case '_': /* APC application program command (ended by ST) */
126 case '\\': /* ST string terminator */
127 case 'n': /* LS2 invoke G2 charset */
128 case 'o': /* LS3 invoke G3 charset */
129 case '|': /* LS3R invoke G3 charset as GR */
130 case '}': /* LS2R invoke G2 charset as GR */
131 case '~': /* LS1R invoke G1 charset as GR */
132 case ']': /* OSC operating system command */
133 case 'g': /* BEL alternate BEL */
135 printf("escseq: <%d:%c>\n", c
, c
);
140 static void escseq_cs(void)
144 case '@': /* CSDFL select default charset (ISO646/8859-1) */
145 case 'G': /* CSUTF8 select UTF-8 */
146 case '8': /* CSUTF8 select UTF-8 (obsolete) */
148 printf("escseq_cs: <%d:%c>\n", c
, c
);
153 static void escseq_g0(void)
157 case '8': /* G0DFL G0 charset = default mapping (ISO8859-1) */
158 case '0': /* G0GFX G0 charset = VT100 graphics mapping */
159 case 'U': /* G0ROM G0 charset = null mapping (straight to ROM) */
160 case 'K': /* G0USR G0 charset = user defined mapping */
161 case 'B': /* G0TXT G0 charset = ASCII mapping */
163 printf("escseq_g0: <%d:%c>\n", c
, c
);
168 static void escseq_g1(void)
172 case '8': /* G1DFL G1 charset = default mapping (ISO8859-1) */
173 case '0': /* G1GFX G1 charset = VT100 graphics mapping */
174 case 'U': /* G1ROM G1 charset = null mapping (straight to ROM) */
175 case 'K': /* G1USR G1 charset = user defined mapping */
176 case 'B': /* G1TXT G1 charset = ASCII mapping */
178 printf("escseq_g1: <%d:%c>\n", c
, c
);
183 static void escseq_g2(void)
187 case '8': /* G2DFL G2 charset = default mapping (ISO8859-1) */
188 case '0': /* G2GFX G2 charset = VT100 graphics mapping */
189 case 'U': /* G2ROM G2 charset = null mapping (straight to ROM) */
190 case 'K': /* G2USR G2 charset = user defined mapping */
192 printf("escseq_g2: <%d:%c>\n", c
, c
);
197 static void escseq_g3(void)
201 case '8': /* G3DFL G3 charset = default mapping (ISO8859-1) */
202 case '0': /* G3GFX G3 charset = VT100 graphics mapping */
203 case 'U': /* G3ROM G3 charset = null mapping (straight to ROM) */
204 case 'K': /* G3USR G3 charset = user defined mapping */
206 printf("escseq_g3: <%d:%c>\n", c
, c
);
211 static int absrow(int r
)
213 return origin() ? top
+ r
: r
;
216 #define CSIP(c) (((c) & 0xf0) == 0x30)
217 #define CSII(c) (((c) & 0xf0) == 0x20)
218 #define CSIF(c) ((c) >= 0x40 && (c) < 0x80)
220 #define MAXCSIARGS 32
221 /* ECMA-48 CSI sequences */
222 static void csiseq(void)
224 int args
[MAXCSIARGS
] = {0};
231 if (strchr("<=>?", c
)) {
238 arg
= arg
* 10 + (c
- '0');
244 n
= n
< ARRAY_SIZE(args
) ? n
+ 1 : 0;
251 case 'H': /* CUP move cursor to row, column */
252 case 'f': /* HVP move cursor to row, column */
253 move_cursor(absrow(MAX(0, args
[0] - 1)), MAX(0, args
[1] - 1));
255 case 'J': /* ED erase display */
258 kill_chars(col
, pad_cols());
259 blank_rows(row
+ 1, pad_rows());
262 kill_chars(0, col
+ 1);
263 blank_rows(0, row
- 1);
270 case 'A': /* CUU move cursor up */
271 advance(-MAX(1, args
[0]), 0, 0);
273 case 'e': /* VPR move cursor down */
274 case 'B': /* CUD move cursor down */
275 advance(MAX(1, args
[0]), 0, 0);
277 case 'a': /* HPR move cursor right */
278 case 'C': /* CUF move cursor right */
279 advance(0, MAX(1, args
[0]), 0);
281 case 'D': /* CUB move cursor left */
282 advance(0, -MAX(1, args
[0]), 0);
284 case 'K': /* EL erase line */
287 kill_chars(col
, pad_cols());
290 kill_chars(0, col
+ 1);
293 kill_chars(0, pad_cols());
297 case 'L': /* IL insert blank lines */
298 if (row
>= top
&& row
< bot
)
299 insert_lines(MAX(1, args
[0]));
301 case 'M': /* DL delete lines */
302 if (row
>= top
&& row
< bot
)
303 delete_lines(MAX(1, args
[0]));
305 case 'd': /* VPA move to row (current column) */
306 move_cursor(absrow(MAX(1, args
[0]) - 1), col
);
308 case 'm': /* SGR set graphic rendition */
311 for (i
= 0; i
< n
; i
++)
314 case 'r': /* DECSTBM set scrolling region to (top, bottom) rows
316 set_region(args
[0], args
[1]);
318 case 'c': /* DA return ESC [ ? 6 c (VT102) */
319 csiseq_da(priv
== '?' ? args
[0] | 0x80 : args
[0]);
321 case 'h': /* SM set mode */
322 for (i
= 0; i
< n
; i
++)
323 modeseq(priv
== '?' ? args
[i
] | 0x80 : args
[i
], 1);
325 case 'l': /* RM reset mode */
326 for (i
= 0; i
< n
; i
++)
327 modeseq(priv
== '?' ? args
[i
] | 0x80 : args
[i
], 0);
329 case 'P': /* DCH delete characters on current line */
330 delete_chars(MIN(MAX(1, args
[0]), pad_cols() - col
));
332 case '@': /* ICH insert blank characters */
333 insert_chars(MAX(1, args
[0]));
335 case 'n': /* DSR device status report */
338 case 'G': /* CHA move cursor to column in current row */
339 advance(0, MAX(0, args
[0] - 1) - col
, 0);
341 case 'X': /* ECH erase characters on current line */
342 kill_chars(col
, MIN(col
+ MAX(1, args
[0]), pad_cols()));
344 case '[': /* IGN ignored control sequence */
345 case 'E': /* CNL move cursor down and to column 1 */
346 case 'F': /* CPL move cursor up and to column 1 */
347 case 'g': /* TBC clear tab stop (CSI 3 g = clear all stops) */
348 case 'q': /* DECLL set keyboard LEDs */
349 case 's': /* CUPSV save cursor position */
350 case 'u': /* CUPRS restore cursor position */
351 case '`': /* HPA move cursor to column in current row */
353 printf("csiseq: <%d:%c>:", c
, c
);
354 for (i
= 0; i
< n
; i
++)
355 printf(" %d", args
[i
]);
361 static void csiseq_da(int c
)
365 term_sendstr("\x1b[?6c");
368 /* we don't care much about cursor shape */
369 /* printf("csiseq_da <0x%x>\n", c); */
374 static void csiseq_dsr(int c
)
379 term_sendstr("\x1b[0n");
382 snprintf(status
, sizeof(status
), "\x1b[%d;%dR",
383 (origin() ? row
- top
: row
) + 1, col
+ 1);
384 term_sendstr(status
);
387 printf("csiseq_dsr <0x%x>\n", c
);
392 /* ANSI/DEC specified modes for SM/RM ANSI Specified Modes */
393 static void modeseq(int c
, int set
)
396 case 0x87: /* DECAWM Auto Wrap */
397 mode
= BIT_SET(mode
, MODE_WRAP
, set
);
399 case 0x99: /* DECTCEM Cursor on (set); Cursor off (reset) */
400 mode
= BIT_SET(mode
, MODE_CURSOR
, set
);
402 case 0x86: /* DECOM Sets relative coordinates (set); Sets absolute coordinates (reset) */
403 mode
= BIT_SET(mode
, MODE_ORIGIN
, set
);
405 case 0x14: /* LNM Line Feed / New Line Mode */
406 mode
= BIT_SET(mode
, MODE_AUTOCR
, set
);
408 case 0x04: /* IRM insertion/replacement mode (always reset) */
410 case 0x00: /* IGN Error (Ignored) */
411 case 0x01: /* GATM guarded-area transfer mode (ignored) */
412 case 0x02: /* KAM keyboard action mode (always reset) */
413 case 0x03: /* CRM control representation mode (always reset) */
414 case 0x05: /* SRTM status-reporting transfer mode */
415 case 0x06: /* ERM erasure mode (always set) */
416 case 0x07: /* VEM vertical editing mode (ignored) */
417 case 0x0a: /* HEM horizontal editing mode */
418 case 0x0b: /* PUM positioning unit mode */
419 case 0x0c: /* SRM send/receive mode (echo on/off) */
420 case 0x0d: /* FEAM format effector action mode */
421 case 0x0e: /* FETM format effector transfer mode */
422 case 0x0f: /* MATM multiple area transfer mode */
423 case 0x10: /* TTM transfer termination mode */
424 case 0x11: /* SATM selected area transfer mode */
425 case 0x12: /* TSM tabulation stop mode */
426 case 0x13: /* EBM editing boundary mode */
427 /* DEC Private Modes: "?NUM" -> (NUM | 0x80) */
428 case 0x80: /* IGN Error (Ignored) */
429 case 0x81: /* DECCKM Cursorkeys application (set); Cursorkeys normal (reset) */
430 case 0x82: /* DECANM ANSI (set); VT52 (reset) */
431 case 0x83: /* DECCOLM 132 columns (set); 80 columns (reset) */
432 case 0x84: /* DECSCLM Jump scroll (set); Smooth scroll (reset) */
433 case 0x85: /* DECSCNM Reverse screen (set); Normal screen (reset) */
434 case 0x88: /* DECARM Auto Repeat */
435 case 0x89: /* DECINLM Interlace */
436 case 0x92: /* DECPFF Send FF to printer after print screen (set); No char after PS (reset) */
437 case 0x93: /* DECPEX Print screen: prints full screen (set); prints scroll region (reset) */
439 printf("modeseq <0x%x>: %d\n", c
, set
);