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