fbpad: add copyright notice
[fbpad.git] / vt102.c
blobf96869e1c2d358e17b2f1bc4b2e18ab68ff01ee7
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 static int readutf8(int c)
14 int result;
15 int l = 0;
16 int z = 0x20;
17 int m = 0x1f;
18 if (~c & 0xc0)
19 return c;
20 while (++l < 6 && c & z) {
21 m >>= 1;
22 z >>= 1;
24 result = m & c;
25 while (l--)
26 result = (result << 6) | (readpty() & 0x3f);
27 return result;
30 /* control sequences */
31 static void ctlseq(void)
33 int c = readpty();
34 switch (c) {
35 case 0x09: /* HT horizontal tab to next tab stop */
36 advance(0, 8 - col % 8, 0);
37 break;
38 case 0x0a: /* LF line feed */
39 case 0x0b: /* VT line feed */
40 case 0x0c: /* FF line feed */
41 advance(1, (mode & MODE_NOAUTOCR) ? 0 : -col, 1);
42 break;
43 case 0x08: /* BS backspace one column */
44 advance(0, -1, 0);
45 break;
46 case 0x1b: /* ESC start escape sequence */
47 escseq();
48 break;
49 case 0x0d: /* CR carriage return */
50 advance(0, -col, 0);
51 break;
52 case 0x00: /* NUL ignored */
53 case 0x07: /* BEL beep */
54 case 0x7f: /* DEL ignored */
55 break;
56 case 0x05: /* ENQ trigger answerback message */
57 case 0x0e: /* SO activate G1 character set & newline */
58 case 0x0f: /* SI activate G0 character set */
59 case 0x11: /* XON resume transmission */
60 case 0x13: /* XOFF stop transmission, ignore characters */
61 case 0x18: /* CAN interrupt escape sequence */
62 case 0x1a: /* SUB interrupt escape sequence */
63 case 0x9b: /* CSI equivalent to ESC [ */
64 printf("ctlseq: <%d:%c>\n", c, c);
65 break;
66 default:
67 term_put(readutf8(c), row, col);
68 advance(0, 1, 1);
69 break;
73 #define ESCM(c) (((c) & 0xf0) == 0x20)
74 #define ESCF(c) ((c) > 0x30 && (c) < 0x7f)
76 /* escape sequences */
77 static void escseq(void)
79 int c = readpty();
80 while (ESCM(c))
81 c = readpty();
82 switch(c) {
83 case '[': /* CSI control sequence introducer */
84 csiseq();
85 break;
86 case '%': /* CS... escseq_cs table */
87 escseq_cs();
88 break;
89 case '(': /* G0... escseq_g0 table */
90 escseq_g0();
91 break;
92 case ')': /* G1... escseq_g1 table */
93 escseq_g1();
94 break;
95 case '*': /* G2... escseq_g2 table */
96 escseq_g2();
97 break;
98 case '+': /* G3... escseq_g3 table */
99 escseq_g3();
100 break;
101 case '7': /* DECSC save state (position, charset, attributes) */
102 misc_save(&term->sav);
103 break;
104 case '8': /* DECRC restore most recently saved state */
105 misc_load(&term->sav);
106 break;
107 case 'M': /* RI reverse line feed */
108 advance(-1, 0, 1);
109 break;
110 case 'D': /* IND line feed */
111 advance(1, 0, 1);
112 break;
113 case 'E': /* NEL newline */
114 advance(1, -col, 1);
115 break;
116 case 'c': /* RIS reset */
117 case 'H': /* HTS set tab stop at current column */
118 case 'Z': /* DECID DEC private ID; return ESC [ ? 6 c (VT102) */
119 case '#': /* DECALN ("#8") DEC alignment test - fill screen with E's */
120 case '>': /* DECPNM set numeric keypad mode */
121 case '=': /* DECPAM set application keypad mode */
122 case 'N': /* SS2 select G2 charset for next char only */
123 case 'O': /* SS3 select G3 charset for next char only */
124 case 'P': /* DCS device control string (ended by ST) */
125 case 'X': /* SOS start of string */
126 case '^': /* PM privacy message (ended by ST) */
127 case '_': /* APC application program command (ended by ST) */
128 case '\\': /* ST string terminator */
129 case 'n': /* LS2 invoke G2 charset */
130 case 'o': /* LS3 invoke G3 charset */
131 case '|': /* LS3R invoke G3 charset as GR */
132 case '}': /* LS2R invoke G2 charset as GR */
133 case '~': /* LS1R invoke G1 charset as GR */
134 case ']': /* OSC operating system command */
135 case 'g': /* BEL alternate BEL */
136 default:
137 printf("escseq: <%d:%c>\n", c, c);
138 break;
142 static void escseq_cs(void)
144 int c = readpty();
145 switch(c) {
146 case '@': /* CSDFL select default charset (ISO646/8859-1) */
147 case 'G': /* CSUTF8 select UTF-8 */
148 case '8': /* CSUTF8 select UTF-8 (obsolete) */
149 default:
150 printf("escseq_cs: <%d:%c>\n", c, c);
151 break;
155 static void escseq_g0(void)
157 int c = readpty();
158 switch(c) {
159 case '8': /* G0DFL G0 charset = default mapping (ISO8859-1) */
160 case '0': /* G0GFX G0 charset = VT100 graphics mapping */
161 case 'U': /* G0ROM G0 charset = null mapping (straight to ROM) */
162 case 'K': /* G0USR G0 charset = user defined mapping */
163 case 'B': /* G0TXT G0 charset = ASCII mapping */
164 default:
165 printf("escseq_g0: <%d:%c>\n", c, c);
166 break;
170 static void escseq_g1(void)
172 int c = readpty();
173 switch(c) {
174 case '8': /* G1DFL G1 charset = default mapping (ISO8859-1) */
175 case '0': /* G1GFX G1 charset = VT100 graphics mapping */
176 case 'U': /* G1ROM G1 charset = null mapping (straight to ROM) */
177 case 'K': /* G1USR G1 charset = user defined mapping */
178 case 'B': /* G1TXT G1 charset = ASCII mapping */
179 default:
180 printf("escseq_g1: <%d:%c>\n", c, c);
181 break;
185 static void escseq_g2(void)
187 int c = readpty();
188 switch(c) {
189 case '8': /* G2DFL G2 charset = default mapping (ISO8859-1) */
190 case '0': /* G2GFX G2 charset = VT100 graphics mapping */
191 case 'U': /* G2ROM G2 charset = null mapping (straight to ROM) */
192 case 'K': /* G2USR G2 charset = user defined mapping */
193 default:
194 printf("escseq_g2: <%d:%c>\n", c, c);
195 break;
199 static void escseq_g3(void)
201 int c = readpty();
202 switch(c) {
203 case '8': /* G3DFL G3 charset = default mapping (ISO8859-1) */
204 case '0': /* G3GFX G3 charset = VT100 graphics mapping */
205 case 'U': /* G3ROM G3 charset = null mapping (straight to ROM) */
206 case 'K': /* G3USR G3 charset = user defined mapping */
207 default:
208 printf("escseq_g3: <%d:%c>\n", c, c);
209 break;
213 static int absrow(int r)
215 return origin() ? top + r : r;
218 #define CSIP(c) (((c) & 0xf0) == 0x30)
219 #define CSII(c) (((c) & 0xf0) == 0x20)
220 #define CSIF(c) ((c) >= 0x40 && (c) < 0x80)
222 #define MAXCSIARGS 32
223 /* ECMA-48 CSI sequences */
224 static void csiseq(void)
226 int args[MAXCSIARGS] = {0};
227 int i;
228 int n = 0;
229 int c = readpty();
230 int inter = 0;
231 int priv = 0;
233 if (strchr("<=>?", c)) {
234 priv = c;
235 c = readpty();
237 while (CSIP(c)) {
238 int arg = 0;
239 while (isdigit(c)) {
240 arg = arg * 10 + (c - '0');
241 c = readpty();
243 if (c == ';')
244 c = readpty();
245 args[n] = arg;
246 n = n < ARRAY_SIZE(args) ? n + 1 : 0;
248 while (CSII(c)) {
249 inter = c;
250 c = readpty();
252 switch(c) {
253 case 'H': /* CUP move cursor to row, column */
254 case 'f': /* HVP move cursor to row, column */
255 move_cursor(absrow(MAX(0, args[0] - 1)), MAX(0, args[1] - 1));
256 break;
257 case 'J': /* ED erase display */
258 switch(args[0]) {
259 case 0:
260 kill_chars(col, pad_cols());
261 blank_rows(row + 1, pad_rows());
262 break;
263 case 1:
264 kill_chars(0, col + 1);
265 blank_rows(0, row - 1);
266 break;
267 case 2:
268 term_blank();
269 break;
271 break;
272 case 'A': /* CUU move cursor up */
273 advance(-MAX(1, args[0]), 0, 0);
274 break;
275 case 'e': /* VPR move cursor down */
276 case 'B': /* CUD move cursor down */
277 advance(MAX(1, args[0]), 0, 0);
278 break;
279 case 'a': /* HPR move cursor right */
280 case 'C': /* CUF move cursor right */
281 advance(0, MAX(1, args[0]), 0);
282 break;
283 case 'D': /* CUB move cursor left */
284 advance(0, -MAX(1, args[0]), 0);
285 break;
286 case 'K': /* EL erase line */
287 switch (args[0]) {
288 case 0:
289 kill_chars(col, pad_cols());
290 break;
291 case 1:
292 kill_chars(0, col + 1);
293 break;
294 case 2:
295 kill_chars(0, pad_cols());
296 break;
298 break;
299 case 'L': /* IL insert blank lines */
300 if (row >= top && row < bot)
301 insert_lines(MAX(1, args[0]));
302 break;
303 case 'M': /* DL delete lines */
304 if (row >= top && row < bot)
305 delete_lines(MAX(1, args[0]));
306 break;
307 case 'd': /* VPA move to row (current column) */
308 move_cursor(absrow(MAX(1, args[0]) - 1), col);
309 break;
310 case 'm': /* SGR set graphic rendition */
311 setmode(0);
312 for (i = 0; i < n; i++)
313 setmode(args[i]);
314 break;
315 case 'r': /* DECSTBM set scrolling region to (top, bottom) rows
317 set_region(args[0], args[1]);
318 break;
319 case 'c': /* DA return ESC [ ? 6 c (VT102) */
320 csiseq_da(priv == '?' ? args[0] | 0x80 : args[0]);
321 break;
322 case 'h': /* SM set mode */
323 for (i = 0; i < n; i++)
324 modeseq(priv == '?' ? args[i] | 0x80 : args[i], 1);
325 break;
326 case 'l': /* RM reset mode */
327 for (i = 0; i < n; i++)
328 modeseq(priv == '?' ? args[i] | 0x80 : args[i], 0);
329 break;
330 case 'P': /* DCH delete characters on current line */
331 delete_chars(MAX(1, args[0]));
332 break;
333 case '@': /* ICH insert blank characters */
334 insert_chars(MAX(1, args[0]));
335 break;
336 case 'n': /* DSR device status report */
337 csiseq_dsr(args[0]);
338 break;
339 case 'G': /* CHA move cursor to column in current row */
340 advance(0, MAX(0, args[0] - 1) - col, 0);
341 break;
342 case 'X': /* ECH erase characters on current line */
343 kill_chars(col, MAX(col + MAX(1, args[0]), pad_cols()));
344 break;
345 case '[': /* IGN ignored control sequence */
346 case 'E': /* CNL move cursor down and to column 1 */
347 case 'F': /* CPL move cursor up and to column 1 */
348 case 'g': /* TBC clear tab stop (CSI 3 g = clear all stops) */
349 case 'q': /* DECLL set keyboard LEDs */
350 case 's': /* CUPSV save cursor position */
351 case 'u': /* CUPRS restore cursor position */
352 case '`': /* HPA move cursor to column in current row */
353 default:
354 printf("csiseq: <%d:%c>:", c, c);
355 for (i = 0; i < n; i++)
356 printf(" %d", args[i]);
357 printf("\n");
358 break;
362 static void csiseq_da(int c)
364 switch(c) {
365 case 0x00:
366 term_sendstr("\x1b[?6c");
367 break;
368 default:
369 /* we don't care much about cursor shape */
370 /* printf("csiseq_da <0x%x>\n", c); */
371 break;
375 static void csiseq_dsr(int c)
377 char status[1 << 5];
378 switch(c) {
379 case 0x05:
380 term_sendstr("\x1b[0n");
381 break;
382 case 0x06:
383 snprintf(status, sizeof(status), "\x1b[%d;%dR",
384 (origin() ? row - top : row) + 1, col + 1);
385 term_sendstr(status);
386 break;
387 default:
388 printf("csiseq_dsr <0x%x>\n", c);
389 break;
393 /* ANSI/DEC specified modes for SM/RM ANSI Specified Modes */
394 static void modeseq(int c, int set)
396 switch(c) {
397 case 0x87: /* DECAWM Auto Wrap */
398 mode = BIT_SET(mode, MODE_NOWRAP, !set);
399 break;
400 case 0x99: /* DECTCEM Cursor on (set); Cursor off (reset) */
401 mode = BIT_SET(mode, MODE_NOCURSOR, !set);
402 break;
403 case 0x86: /* DECOM Sets relative coordinates (set); Sets absolute coordinates (reset) */
404 mode = BIT_SET(mode, MODE_ORIGIN, set);
405 break;
406 case 0x14: /* LNM Line Feed / New Line Mode */
407 mode = BIT_SET(mode, MODE_NOAUTOCR, !set);
408 break;
409 case 0x04: /* IRM insertion/replacement mode (always reset) */
410 break;
411 case 0x00: /* IGN Error (Ignored) */
412 case 0x01: /* GATM guarded-area transfer mode (ignored) */
413 case 0x02: /* KAM keyboard action mode (always reset) */
414 case 0x03: /* CRM control representation mode (always reset) */
415 case 0x05: /* SRTM status-reporting transfer mode */
416 case 0x06: /* ERM erasure mode (always set) */
417 case 0x07: /* VEM vertical editing mode (ignored) */
418 case 0x0a: /* HEM horizontal editing mode */
419 case 0x0b: /* PUM positioning unit mode */
420 case 0x0c: /* SRM send/receive mode (echo on/off) */
421 case 0x0d: /* FEAM format effector action mode */
422 case 0x0e: /* FETM format effector transfer mode */
423 case 0x0f: /* MATM multiple area transfer mode */
424 case 0x10: /* TTM transfer termination mode */
425 case 0x11: /* SATM selected area transfer mode */
426 case 0x12: /* TSM tabulation stop mode */
427 case 0x13: /* EBM editing boundary mode */
428 /* DEC Private Modes: "?NUM" -> (NUM | 0x80) */
429 case 0x80: /* IGN Error (Ignored) */
430 case 0x81: /* DECCKM Cursorkeys application (set); Cursorkeys normal (reset) */
431 case 0x82: /* DECANM ANSI (set); VT52 (reset) */
432 case 0x83: /* DECCOLM 132 columns (set); 80 columns (reset) */
433 case 0x84: /* DECSCLM Jump scroll (set); Smooth scroll (reset) */
434 case 0x85: /* DECSCNM Reverse screen (set); Normal screen (reset) */
435 case 0x88: /* DECARM Auto Repeat */
436 case 0x89: /* DECINLM Interlace */
437 case 0x92: /* DECPFF Send FF to printer after print screen (set); No char after PS (reset) */
438 case 0x93: /* DECPEX Print screen: prints full screen (set); prints scroll region (reset) */
439 default:
440 printf("modeseq <0x%x>: %d\n", c, set);
441 break;