vt102: reset attributes only when no args
[fbpad.git] / vt102.c
blob99f4a8308231365ca65e31f6c8e86b09706e0d08
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)
16 int result;
17 int l = 1;
18 if (~c & 0xc0)
19 return c;
20 while (l < 6 && c & (0x40 >> l))
21 l++;
22 result = (0x3f >> l) & c;
23 while (l--)
24 result = (result << 6) | (readpty() & 0x3f);
25 return result;
28 /* control sequences */
29 static void ctlseq(void)
31 int c = readpty();
32 switch (c) {
33 case 0x09: /* HT horizontal tab to next tab stop */
34 advance(0, 8 - col % 8, 0);
35 break;
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);
40 break;
41 case 0x08: /* BS backspace one column */
42 advance(0, -1, 0);
43 break;
44 case 0x1b: /* ESC start escape sequence */
45 escseq();
46 break;
47 case 0x0d: /* CR carriage return */
48 advance(0, -col, 0);
49 break;
50 case 0x00: /* NUL ignored */
51 case 0x07: /* BEL beep */
52 case 0x7f: /* DEL ignored */
53 break;
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);
63 break;
64 default:
65 lazy_put(readutf8(c), row, col);
66 advance(0, 1, 1);
67 break;
71 #define ESCM(c) (((c) & 0xf0) == 0x20)
72 #define ESCF(c) ((c) > 0x30 && (c) < 0x7f)
74 /* escape sequences */
75 static void escseq(void)
77 int c = readpty();
78 while (ESCM(c))
79 c = readpty();
80 switch(c) {
81 case '[': /* CSI control sequence introducer */
82 csiseq();
83 break;
84 case '%': /* CS... escseq_cs table */
85 escseq_cs();
86 break;
87 case '(': /* G0... escseq_g0 table */
88 escseq_g0();
89 break;
90 case ')': /* G1... escseq_g1 table */
91 escseq_g1();
92 break;
93 case '*': /* G2... escseq_g2 table */
94 escseq_g2();
95 break;
96 case '+': /* G3... escseq_g3 table */
97 escseq_g3();
98 break;
99 case '7': /* DECSC save state (position, charset, attributes) */
100 misc_save(&term->sav);
101 break;
102 case '8': /* DECRC restore most recently saved state */
103 misc_load(&term->sav);
104 break;
105 case 'M': /* RI reverse line feed */
106 advance(-1, 0, 1);
107 break;
108 case 'D': /* IND line feed */
109 advance(1, 0, 1);
110 break;
111 case 'E': /* NEL newline */
112 advance(1, -col, 1);
113 break;
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 */
134 default:
135 printf("escseq: <%d:%c>\n", c, c);
136 break;
140 static void escseq_cs(void)
142 int c = readpty();
143 switch(c) {
144 case '@': /* CSDFL select default charset (ISO646/8859-1) */
145 case 'G': /* CSUTF8 select UTF-8 */
146 case '8': /* CSUTF8 select UTF-8 (obsolete) */
147 default:
148 printf("escseq_cs: <%d:%c>\n", c, c);
149 break;
153 static void escseq_g0(void)
155 int c = readpty();
156 switch(c) {
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 */
162 default:
163 printf("escseq_g0: <%d:%c>\n", c, c);
164 break;
168 static void escseq_g1(void)
170 int c = readpty();
171 switch(c) {
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 */
177 default:
178 printf("escseq_g1: <%d:%c>\n", c, c);
179 break;
183 static void escseq_g2(void)
185 int c = readpty();
186 switch(c) {
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 */
191 default:
192 printf("escseq_g2: <%d:%c>\n", c, c);
193 break;
197 static void escseq_g3(void)
199 int c = readpty();
200 switch(c) {
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 */
205 default:
206 printf("escseq_g3: <%d:%c>\n", c, c);
207 break;
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};
225 int i;
226 int n = 0;
227 int c = readpty();
228 int inter = 0;
229 int priv = 0;
231 if (strchr("<=>?", c)) {
232 priv = c;
233 c = readpty();
235 while (CSIP(c)) {
236 int arg = 0;
237 while (isdigit(c)) {
238 arg = arg * 10 + (c - '0');
239 c = readpty();
241 if (c == ';')
242 c = readpty();
243 args[n] = arg;
244 n = n < ARRAY_SIZE(args) ? n + 1 : 0;
246 while (CSII(c)) {
247 inter = c;
248 c = readpty();
250 switch(c) {
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));
254 break;
255 case 'J': /* ED erase display */
256 switch(args[0]) {
257 case 0:
258 kill_chars(col, pad_cols());
259 blank_rows(row + 1, pad_rows());
260 break;
261 case 1:
262 kill_chars(0, col + 1);
263 blank_rows(0, row - 1);
264 break;
265 case 2:
266 term_blank();
267 break;
269 break;
270 case 'A': /* CUU move cursor up */
271 advance(-MAX(1, args[0]), 0, 0);
272 break;
273 case 'e': /* VPR move cursor down */
274 case 'B': /* CUD move cursor down */
275 advance(MAX(1, args[0]), 0, 0);
276 break;
277 case 'a': /* HPR move cursor right */
278 case 'C': /* CUF move cursor right */
279 advance(0, MAX(1, args[0]), 0);
280 break;
281 case 'D': /* CUB move cursor left */
282 advance(0, -MAX(1, args[0]), 0);
283 break;
284 case 'K': /* EL erase line */
285 switch (args[0]) {
286 case 0:
287 kill_chars(col, pad_cols());
288 break;
289 case 1:
290 kill_chars(0, col + 1);
291 break;
292 case 2:
293 kill_chars(0, pad_cols());
294 break;
296 break;
297 case 'L': /* IL insert blank lines */
298 if (row >= top && row < bot)
299 insert_lines(MAX(1, args[0]));
300 break;
301 case 'M': /* DL delete lines */
302 if (row >= top && row < bot)
303 delete_lines(MAX(1, args[0]));
304 break;
305 case 'd': /* VPA move to row (current column) */
306 move_cursor(absrow(MAX(1, args[0]) - 1), col);
307 break;
308 case 'm': /* SGR set graphic rendition */
309 if (!n)
310 setmode(0);
311 for (i = 0; i < n; i++)
312 setmode(args[i]);
313 break;
314 case 'r': /* DECSTBM set scrolling region to (top, bottom) rows
316 set_region(args[0], args[1]);
317 break;
318 case 'c': /* DA return ESC [ ? 6 c (VT102) */
319 csiseq_da(priv == '?' ? args[0] | 0x80 : args[0]);
320 break;
321 case 'h': /* SM set mode */
322 for (i = 0; i < n; i++)
323 modeseq(priv == '?' ? args[i] | 0x80 : args[i], 1);
324 break;
325 case 'l': /* RM reset mode */
326 for (i = 0; i < n; i++)
327 modeseq(priv == '?' ? args[i] | 0x80 : args[i], 0);
328 break;
329 case 'P': /* DCH delete characters on current line */
330 delete_chars(MIN(MAX(1, args[0]), pad_cols() - col));
331 break;
332 case '@': /* ICH insert blank characters */
333 insert_chars(MAX(1, args[0]));
334 break;
335 case 'n': /* DSR device status report */
336 csiseq_dsr(args[0]);
337 break;
338 case 'G': /* CHA move cursor to column in current row */
339 advance(0, MAX(0, args[0] - 1) - col, 0);
340 break;
341 case 'X': /* ECH erase characters on current line */
342 kill_chars(col, MIN(col + MAX(1, args[0]), pad_cols()));
343 break;
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 */
352 default:
353 printf("csiseq: <%d:%c>:", c, c);
354 for (i = 0; i < n; i++)
355 printf(" %d", args[i]);
356 printf("\n");
357 break;
361 static void csiseq_da(int c)
363 switch(c) {
364 case 0x00:
365 term_sendstr("\x1b[?6c");
366 break;
367 default:
368 /* we don't care much about cursor shape */
369 /* printf("csiseq_da <0x%x>\n", c); */
370 break;
374 static void csiseq_dsr(int c)
376 char status[1 << 5];
377 switch(c) {
378 case 0x05:
379 term_sendstr("\x1b[0n");
380 break;
381 case 0x06:
382 snprintf(status, sizeof(status), "\x1b[%d;%dR",
383 (origin() ? row - top : row) + 1, col + 1);
384 term_sendstr(status);
385 break;
386 default:
387 printf("csiseq_dsr <0x%x>\n", c);
388 break;
392 /* ANSI/DEC specified modes for SM/RM ANSI Specified Modes */
393 static void modeseq(int c, int set)
395 switch(c) {
396 case 0x87: /* DECAWM Auto Wrap */
397 mode = BIT_SET(mode, MODE_WRAP, set);
398 break;
399 case 0x99: /* DECTCEM Cursor on (set); Cursor off (reset) */
400 mode = BIT_SET(mode, MODE_CURSOR, set);
401 break;
402 case 0x86: /* DECOM Sets relative coordinates (set); Sets absolute coordinates (reset) */
403 mode = BIT_SET(mode, MODE_ORIGIN, set);
404 break;
405 case 0x14: /* LNM Line Feed / New Line Mode */
406 mode = BIT_SET(mode, MODE_AUTOCR, set);
407 break;
408 case 0x04: /* IRM insertion/replacement mode (always reset) */
409 break;
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) */
438 default:
439 printf("modeseq <0x%x>: %d\n", c, set);
440 break;