1 | /* |
---|
2 | * neercs console-based window manager |
---|
3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
5 | * 2008-2010 Pascal Terjan <pterjan@linuxfr.org> |
---|
6 | * All Rights Reserved |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | #include "config.h" |
---|
15 | #include <string.h> |
---|
16 | #include <caca.h> |
---|
17 | #include "neercs.h" |
---|
18 | |
---|
19 | |
---|
20 | /* DEC ACS with common extensions */ |
---|
21 | static uint32_t dec_acs(uint32_t uc) |
---|
22 | { |
---|
23 | switch (uc) |
---|
24 | { |
---|
25 | case '+': |
---|
26 | return 0x2192; /* RIGHTWARDS ARROW */ |
---|
27 | case ',': |
---|
28 | return 0x2190; /* LEFTWARDS ARROW */ |
---|
29 | case '-': |
---|
30 | return 0x2191; /* UPWARDS ARROW */ |
---|
31 | case '.': |
---|
32 | return 0x2193; /* DOWNWARDS ARROW */ |
---|
33 | case '0': |
---|
34 | return 0x25AE; /* BLACK VERTICAL RECTANGLE */ |
---|
35 | case '_': |
---|
36 | return 0x25AE; /* BLACK VERTICAL RECTANGLE */ |
---|
37 | case '`': |
---|
38 | return 0x25C6; /* BLACK DIAMOND */ |
---|
39 | case 'a': |
---|
40 | return 0x2592; /* MEDIUM SHADE */ |
---|
41 | case 'b': |
---|
42 | return 0x2409; /* SYMBOL FOR HORIZONTAL TABULATION */ |
---|
43 | case 'c': |
---|
44 | return 0x240C; /* SYMBOL FOR FORM FEED */ |
---|
45 | case 'd': |
---|
46 | return 0x240D; /* SYMBOL FOR CARRIAGE RETURN */ |
---|
47 | case 'e': |
---|
48 | return 0x240A; /* SYMBOL FOR LINE FEED */ |
---|
49 | case 'f': |
---|
50 | return 0x00B0; /* DEGREE SIGN */ |
---|
51 | case 'g': |
---|
52 | return 0x00B1; /* PLUS-MINUS SIGN */ |
---|
53 | case 'h': |
---|
54 | return 0x2424; /* SYMBOL FOR NEWLINE */ |
---|
55 | case 'i': |
---|
56 | return 0x240B; /* SYMBOL FOR VERTICAL TABULATION */ |
---|
57 | case 'j': |
---|
58 | return 0x2518; /* BOX DRAWINGS LIGHT UP AND LEFT */ |
---|
59 | case 'k': |
---|
60 | return 0x2510; /* BOX DRAWINGS LIGHT DOWN AND LEFT */ |
---|
61 | case 'l': |
---|
62 | return 0x250C; /* BOX DRAWINGS LIGHT DOWN AND RIGHT */ |
---|
63 | case 'm': |
---|
64 | return 0x2514; /* BOX DRAWINGS LIGHT UP AND RIGHT */ |
---|
65 | case 'n': |
---|
66 | return 0x253C; /* BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL */ |
---|
67 | case 'o': |
---|
68 | return 0x23BA; /* HORIZONTAL SCAN LINE-1 */ |
---|
69 | case 'p': |
---|
70 | return 0x23BB; /* HORIZONTAL SCAN LINE-3 */ |
---|
71 | case 'q': |
---|
72 | return 0x2500; /* BOX DRAWINGS LIGHT HORIZONTAL */ |
---|
73 | case 'r': |
---|
74 | return 0x23BC; /* HORIZONTAL SCAN LINE-7 */ |
---|
75 | case 's': |
---|
76 | return 0x23BD; /* HORIZONTAL SCAN LINE-9 */ |
---|
77 | case 't': |
---|
78 | return 0x251C; /* BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ |
---|
79 | case 'u': |
---|
80 | return 0x2524; /* BOX DRAWINGS LIGHT VERTICAL AND LEFT */ |
---|
81 | case 'v': |
---|
82 | return 0x2534; /* BOX DRAWINGS LIGHT UP AND HORIZONTAL */ |
---|
83 | case 'w': |
---|
84 | return 0x252C; /* BOX DRAWINGS LIGHT DOWN AND HORIZONTAL */ |
---|
85 | case 'x': |
---|
86 | return 0x2502; /* BOX DRAWINGS LIGHT VERTICAL */ |
---|
87 | case 'y': |
---|
88 | return 0x2264; /* LESS-THAN OR EQUAL TO */ |
---|
89 | case 'z': |
---|
90 | return 0x2265; /* GREATER-THAN OR EQUAL TO */ |
---|
91 | case '{': |
---|
92 | return 0x03C0; /* GREEK SMALL LETTER PI */ |
---|
93 | case '|': |
---|
94 | return 0x2260; /* NOT EQUAL TO */ |
---|
95 | case '}': |
---|
96 | return 0x00A3; /* POUND SIGN */ |
---|
97 | case '~': |
---|
98 | return 0x00B7; /* MIDDLE DOT */ |
---|
99 | default: |
---|
100 | return uc; |
---|
101 | } |
---|
102 | }; |
---|
103 | |
---|
104 | static void reset_conv_state(struct screen *); |
---|
105 | |
---|
106 | #define LITERAL2CHAR(i0,i1) (((i0) << 8) | (i1)) |
---|
107 | |
---|
108 | #define LITERAL3CHAR(i0,i1,i2) LITERAL2CHAR(LITERAL2CHAR(i0, i1), i2) |
---|
109 | |
---|
110 | static void ansi_parse_grcm(struct screen *, |
---|
111 | unsigned int, unsigned int const *); |
---|
112 | |
---|
113 | inline int handle_single_char(unsigned char c, int *x, int *y, |
---|
114 | struct screen_list *screen_list, |
---|
115 | struct screen *sc); |
---|
116 | inline int handle_duplet(unsigned char const *buffer, struct screen *sc, |
---|
117 | unsigned int *skip, int top, int bottom, int width, |
---|
118 | int height); |
---|
119 | |
---|
120 | |
---|
121 | inline int handle_single_char(unsigned char c, int *x, int *y, |
---|
122 | struct screen_list *screen_list, |
---|
123 | struct screen *sc) |
---|
124 | { |
---|
125 | if (c == '\r') |
---|
126 | { |
---|
127 | *x = 0; |
---|
128 | } |
---|
129 | |
---|
130 | else if (c == '\n') |
---|
131 | { |
---|
132 | *x = 0; |
---|
133 | *y = *y + 1; |
---|
134 | } |
---|
135 | else if (c == '\a') |
---|
136 | { |
---|
137 | if (!sc->bell) |
---|
138 | screen_list->in_bell = 10; |
---|
139 | sc->bell = 1; |
---|
140 | } |
---|
141 | |
---|
142 | else if (c == '\t') |
---|
143 | { |
---|
144 | *x = (*x + 7) & ~7; |
---|
145 | } |
---|
146 | |
---|
147 | else if (c == '\x08') |
---|
148 | { |
---|
149 | if (*x > 0) |
---|
150 | *x = *x - 1; |
---|
151 | } |
---|
152 | else if (c == '\x0b') |
---|
153 | { |
---|
154 | /* Vertical tab */ |
---|
155 | /* Not sure about the real meaning of it, just y++ for now */ |
---|
156 | if (*y < caca_get_canvas_height(sc->cv)) |
---|
157 | *y = *y + 1; |
---|
158 | } |
---|
159 | else if (c == '\x0e') |
---|
160 | { |
---|
161 | /* Shift Out (Ctrl-N) -> Switch to Alternate Character Set: invokes |
---|
162 | the G1 character set. */ |
---|
163 | sc->conv_state.glr[0] = 1; |
---|
164 | } |
---|
165 | |
---|
166 | else if (c == '\x0f') |
---|
167 | { |
---|
168 | /* Shift In (Ctrl-O) -> Switch to Standard Character Set: invokes the |
---|
169 | G0 character set. */ |
---|
170 | sc->conv_state.glr[0] = 0; |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | return 1; |
---|
175 | } |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|
179 | inline int handle_duplet(unsigned char const *buffer, struct screen *sc, |
---|
180 | unsigned int *skip, int top, int bottom, int width, |
---|
181 | int height) |
---|
182 | { |
---|
183 | int i = 0, j, k; |
---|
184 | unsigned int dummy = 0; |
---|
185 | |
---|
186 | /* Single Shift Select of G2 Character Set (SS2: 0x8e): affects next |
---|
187 | character only */ |
---|
188 | if (buffer[i] == '\033' && buffer[i + 1] == 'N') |
---|
189 | { |
---|
190 | sc->conv_state.ss = 2; |
---|
191 | *skip += 1; |
---|
192 | } |
---|
193 | /* Reverse Index (RI) go up one line, reverse scroll if necessary */ |
---|
194 | else if (buffer[i] == '\033' && buffer[i + 1] == 'M') |
---|
195 | { |
---|
196 | /* FIXME : not sure about the meaning of 'go up one line' and 'if |
---|
197 | necessary' words. Implemented as a scroller only. */ |
---|
198 | for (j = bottom - 1; j > top; j--) |
---|
199 | { |
---|
200 | for (k = 0; k < width; k++) |
---|
201 | { |
---|
202 | caca_put_char(sc->cv, k, j, caca_get_char(sc->cv, k, j - 1)); |
---|
203 | caca_put_attr(sc->cv, k, j, caca_get_attr(sc->cv, k, j - 1)); |
---|
204 | } |
---|
205 | } |
---|
206 | caca_draw_line(sc->cv, 0, top - 1, width - 1, top - 1, ' '); |
---|
207 | *skip += 1; |
---|
208 | } |
---|
209 | |
---|
210 | /* Single Shift Select of G3 Character Set (SS2: 0x8f): affects next |
---|
211 | character only */ |
---|
212 | else if (buffer[i] == '\033' && buffer[i + 1] == 'O') |
---|
213 | { |
---|
214 | sc->conv_state.ss = 3; |
---|
215 | *skip += 1; |
---|
216 | } |
---|
217 | |
---|
218 | /* LOCKING-SHIFT TWO (LS2), ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */ |
---|
219 | else if (buffer[i] == '\033' && buffer[i + 1] == 'n') |
---|
220 | { |
---|
221 | sc->conv_state.glr[0] = 2; |
---|
222 | *skip += 1; |
---|
223 | } |
---|
224 | |
---|
225 | /* LOCKING-SHIFT THREE (LS3) ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */ |
---|
226 | else if (buffer[i] == '\033' && buffer[i + 1] == 'o') |
---|
227 | { |
---|
228 | sc->conv_state.glr[0] = 3; |
---|
229 | *skip += 1; |
---|
230 | } |
---|
231 | |
---|
232 | /* RESET TO INITIAL STATE (RIS), ECMA-48 (1986), ISO 6429 : 1988 */ |
---|
233 | else if (buffer[i] == '\033' && buffer[i + 1] == 'c') |
---|
234 | { |
---|
235 | sc->dfg = CACA_DEFAULT; |
---|
236 | sc->dbg = CACA_DEFAULT; |
---|
237 | |
---|
238 | caca_set_color_ansi(sc->cv, sc->dfg, sc->dbg); |
---|
239 | sc->clearattr = caca_get_attr(sc->cv, -1, -1); |
---|
240 | ansi_parse_grcm(sc, 1, &dummy); |
---|
241 | |
---|
242 | reset_conv_state(sc); |
---|
243 | *skip += 1; |
---|
244 | } |
---|
245 | |
---|
246 | /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO |
---|
247 | IR 189) */ |
---|
248 | else if (buffer[i] == '\033' && buffer[i + 1] == 'd') |
---|
249 | { |
---|
250 | reset_conv_state(sc); |
---|
251 | *skip += 1; |
---|
252 | } |
---|
253 | else |
---|
254 | { |
---|
255 | return 1; |
---|
256 | } |
---|
257 | |
---|
258 | return 0; |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | long int import_term(struct screen_list *screen_list, struct screen *sc, |
---|
263 | void const *data, unsigned int size) |
---|
264 | { |
---|
265 | unsigned char const *buffer = (unsigned char const *)data; |
---|
266 | unsigned int i, j, k, skip, dummy = 0; |
---|
267 | unsigned int width, height, top, bottom; |
---|
268 | uint32_t savedattr; |
---|
269 | int x = 0, y = 0, save_x = 0, save_y = 0; |
---|
270 | char b[100]; |
---|
271 | |
---|
272 | debug("ansi : import_term\n"); |
---|
273 | |
---|
274 | width = caca_get_canvas_width(sc->cv); |
---|
275 | height = caca_get_canvas_height(sc->cv); |
---|
276 | x = caca_get_cursor_x(sc->cv); |
---|
277 | y = caca_get_cursor_y(sc->cv); |
---|
278 | top = 1; |
---|
279 | bottom = height; |
---|
280 | |
---|
281 | if (!sc->init) |
---|
282 | { |
---|
283 | sc->dfg = CACA_LIGHTGRAY; |
---|
284 | sc->dbg = CACA_BLACK; |
---|
285 | |
---|
286 | caca_set_color_ansi(sc->cv, sc->dfg, sc->dbg); |
---|
287 | sc->clearattr = caca_get_attr(sc->cv, -1, -1); |
---|
288 | |
---|
289 | ansi_parse_grcm(sc, 1, &dummy); |
---|
290 | |
---|
291 | reset_conv_state(sc); |
---|
292 | |
---|
293 | sc->init = 1; |
---|
294 | } |
---|
295 | |
---|
296 | for (i = 0; i < size; i += skip) |
---|
297 | { |
---|
298 | uint32_t ch = 0; |
---|
299 | int wch = 0; |
---|
300 | |
---|
301 | skip = 1; |
---|
302 | |
---|
303 | /* Control codes (ASCII < \x20) */ |
---|
304 | if (!handle_single_char(buffer[i], &x, &y, screen_list, sc)) |
---|
305 | { |
---|
306 | } |
---|
307 | |
---|
308 | /* If there are not enough characters to parse the escape sequence, |
---|
309 | wait until the next try. We require 3. */ |
---|
310 | |
---|
311 | else if (buffer[i] == '\033' && i + 2 >= size) |
---|
312 | break; |
---|
313 | |
---|
314 | |
---|
315 | else if (!handle_duplet(&buffer[i], sc, &skip, |
---|
316 | top, bottom, width, height)) |
---|
317 | { |
---|
318 | |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | /* GZDM4, G0-Designators, multi, 94^n chars [grandfathered short form |
---|
323 | from ISO 2022:1986] */ |
---|
324 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
325 | && (buffer[i + 2] >= '@') && (buffer[i + 2] <= 'C')) |
---|
326 | { |
---|
327 | sc->conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 2]); |
---|
328 | skip += 2; |
---|
329 | } |
---|
330 | |
---|
331 | /* GnDMx Gn-Designators, 9x^n chars; need one more char to distinguish |
---|
332 | these */ |
---|
333 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
334 | && (i + 3 >= size)) |
---|
335 | break; |
---|
336 | |
---|
337 | /* GZD4 G0-Designator, 94 chars */ |
---|
338 | else if (buffer[i] == '\033' && buffer[i + 1] == '(') |
---|
339 | { |
---|
340 | sc->conv_state.gn[0] = buffer[i + 2]; |
---|
341 | skip += 2; |
---|
342 | } |
---|
343 | |
---|
344 | /* G1D4 G1-Designator, 94 chars */ |
---|
345 | else if (buffer[i] == '\033' && buffer[i + 1] == ')') |
---|
346 | { |
---|
347 | sc->conv_state.gn[1] = buffer[i + 2]; |
---|
348 | skip += 2; |
---|
349 | } |
---|
350 | |
---|
351 | /* G2D4 G2-Designator, 94 chars */ |
---|
352 | else if (buffer[i] == '\033' && buffer[i + 1] == '*') |
---|
353 | { |
---|
354 | sc->conv_state.gn[2] = buffer[i + 2]; |
---|
355 | skip += 2; |
---|
356 | } |
---|
357 | |
---|
358 | /* G3D4 G3-Designator, 94 chars */ |
---|
359 | else if (buffer[i] == '\033' && buffer[i + 1] == '+') |
---|
360 | { |
---|
361 | sc->conv_state.gn[3] = buffer[i + 2]; |
---|
362 | skip += 2; |
---|
363 | } |
---|
364 | |
---|
365 | /* G2D6 G2-Designator, 96 chars */ |
---|
366 | else if (buffer[i] == '\033' && buffer[i + 1] == '.') |
---|
367 | { |
---|
368 | sc->conv_state.gn[2] = LITERAL2CHAR('.', buffer[i + 2]); |
---|
369 | skip += 2; |
---|
370 | } |
---|
371 | |
---|
372 | /* G3D6 G3-Designator, 96 chars */ |
---|
373 | else if (buffer[i] == '\033' && buffer[i + 1] == '/') |
---|
374 | { |
---|
375 | sc->conv_state.gn[3] = LITERAL2CHAR('.', buffer[i + 2]); |
---|
376 | skip += 2; |
---|
377 | } |
---|
378 | |
---|
379 | /* GZDM4 G0-Designator, 94^n chars */ |
---|
380 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
381 | && buffer[i + 2] == '(') |
---|
382 | { |
---|
383 | sc->conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 3]); |
---|
384 | skip += 3; |
---|
385 | } |
---|
386 | |
---|
387 | /* G1DM4 G1-Designator, 94^n chars */ |
---|
388 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
389 | && buffer[i + 2] == ')') |
---|
390 | { |
---|
391 | sc->conv_state.gn[1] = LITERAL2CHAR('$', buffer[i + 3]); |
---|
392 | skip += 3; |
---|
393 | } |
---|
394 | |
---|
395 | /* G2DM4 G2-Designator, 94^n chars */ |
---|
396 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
397 | && buffer[i + 2] == '*') |
---|
398 | { |
---|
399 | sc->conv_state.gn[2] = LITERAL2CHAR('$', buffer[i + 3]); |
---|
400 | skip += 3; |
---|
401 | } |
---|
402 | |
---|
403 | /* G3DM4 G3-Designator, 94^n chars */ |
---|
404 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
405 | && buffer[i + 2] == '+') |
---|
406 | { |
---|
407 | sc->conv_state.gn[3] = LITERAL2CHAR('$', buffer[i + 3]); |
---|
408 | skip += 3; |
---|
409 | } |
---|
410 | |
---|
411 | /* G2DM6 G2-Designator, 96^n chars */ |
---|
412 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
413 | && buffer[i + 2] == '.') |
---|
414 | { |
---|
415 | sc->conv_state.gn[2] = LITERAL3CHAR('$', '.', buffer[i + 3]); |
---|
416 | skip += 3; |
---|
417 | } |
---|
418 | |
---|
419 | /* G3DM6 G3-Designator, 96^n chars */ |
---|
420 | else if (buffer[i] == '\033' && buffer[i + 1] == '$' |
---|
421 | && buffer[i + 2] == '/') |
---|
422 | { |
---|
423 | sc->conv_state.gn[3] = LITERAL3CHAR('$', '.', buffer[i + 3]); |
---|
424 | skip += 3; |
---|
425 | } |
---|
426 | else if (buffer[i] == '\033' && buffer[i + 1] == '#') |
---|
427 | { |
---|
428 | debug("ansi private '#' sequence\n"); |
---|
429 | |
---|
430 | switch (buffer[i + 2]) |
---|
431 | { |
---|
432 | case '8': /* DECALN Fills the entire screen area with |
---|
433 | uppercase Es for screen focus and |
---|
434 | alignment. */ |
---|
435 | for (j = 0; j < height; j++) |
---|
436 | { |
---|
437 | for (k = 0; k < width; k++) |
---|
438 | { |
---|
439 | caca_put_char(sc->cv, k, j, 'E'); |
---|
440 | } |
---|
441 | } |
---|
442 | x = 0; |
---|
443 | y = 0; |
---|
444 | skip += 2; |
---|
445 | break; |
---|
446 | |
---|
447 | default: |
---|
448 | debug("Unknow private sequence 'ESC#%c'\n", buffer[i + 2]); |
---|
449 | continue; |
---|
450 | } |
---|
451 | |
---|
452 | } |
---|
453 | /* Interpret escape commands, as per Standard ECMA-48 "Control |
---|
454 | Functions for Coded Character Sets", 5.4. Control sequences. */ |
---|
455 | else if (buffer[i] == '\033' && buffer[i + 1] == '[') |
---|
456 | { |
---|
457 | unsigned int argc = 0, argv[101]; |
---|
458 | unsigned int param, inter, junk, final; |
---|
459 | |
---|
460 | if (buffer[i + 2] == '?') |
---|
461 | { |
---|
462 | debug("CSI? %c%c%c%c%c\n", |
---|
463 | buffer[i + 3], buffer[i + 4], buffer[i + 5], |
---|
464 | buffer[i + 6], buffer[i + 7]); |
---|
465 | } |
---|
466 | |
---|
467 | /* Compute offsets to parameter bytes, intermediate bytes and to |
---|
468 | the final byte. Only the final byte is mandatory, there can be |
---|
469 | zero of the others. 0 param=2 inter final final+1 |
---|
470 | +-----+------------------+---------------------+-----------------+ |
---|
471 | | CSI | parameter bytes | intermediate bytes | final byte | | | |
---|
472 | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | | ^[[ | 0123456789:;<=>? |
---|
473 | | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ | |
---|
474 | +-----+------------------+---------------------+-----------------+ */ |
---|
475 | param = 2; |
---|
476 | |
---|
477 | /* vttest use to interleave control characters (\014 CR or \010 |
---|
478 | BS) into CSI sequences, either directly after ESC[ or after |
---|
479 | param. Can't find anything related to this in any documentation |
---|
480 | nor XTerm sources, thought. */ |
---|
481 | |
---|
482 | for (junk = param; i + junk < size; junk++) |
---|
483 | if (buffer[i + junk] < 0x20) |
---|
484 | { |
---|
485 | handle_single_char(buffer[i + junk], &x, &y, screen_list, |
---|
486 | sc); |
---|
487 | } |
---|
488 | else |
---|
489 | { |
---|
490 | break; |
---|
491 | } |
---|
492 | |
---|
493 | /* Intermediate offset */ |
---|
494 | for (inter = junk; i + inter < size; inter++) |
---|
495 | if (buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f) |
---|
496 | { |
---|
497 | break; |
---|
498 | } |
---|
499 | /* Interleaved character */ |
---|
500 | for (junk = inter; i + junk < size; junk++) |
---|
501 | if (buffer[i + junk] < 0x20) |
---|
502 | { |
---|
503 | handle_single_char(buffer[i + junk], &x, &y, screen_list, |
---|
504 | sc); |
---|
505 | } |
---|
506 | else |
---|
507 | { |
---|
508 | break; |
---|
509 | } |
---|
510 | |
---|
511 | /* Final Byte offset */ |
---|
512 | for (final = junk; i + final < size; final++) |
---|
513 | if (buffer[i + final] < 0x20 || buffer[i + final] > 0x2f) |
---|
514 | { |
---|
515 | break; |
---|
516 | } |
---|
517 | if (i + final >= size |
---|
518 | || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e) |
---|
519 | { |
---|
520 | debug("ansi Invalid Final Byte (%d %c)\n", buffer[i + final], |
---|
521 | buffer[i + final]); |
---|
522 | break; /* Invalid Final Byte */ |
---|
523 | } |
---|
524 | |
---|
525 | skip += final; |
---|
526 | |
---|
527 | /* Sanity checks */ |
---|
528 | if (param < inter && buffer[i + param] >= 0x3c) |
---|
529 | { |
---|
530 | /* Private sequence, only parse what we know */ |
---|
531 | debug("ansi import: private sequence \"^[[%.*s\"", |
---|
532 | final - param + 1, buffer + i + param); |
---|
533 | /* FIXME better parsing */ |
---|
534 | if (buffer[i + 2] == '?') |
---|
535 | { |
---|
536 | char arg[5]; |
---|
537 | int a = 0; |
---|
538 | int c, p; |
---|
539 | for (p = 0; p < 4; p++) |
---|
540 | { |
---|
541 | if (buffer[i + 3 + p] >= '0' |
---|
542 | && buffer[i + 3 + p] <= '9') |
---|
543 | { |
---|
544 | arg[a] = buffer[i + 3 + p]; |
---|
545 | arg[a + 1] = 0; |
---|
546 | a++; |
---|
547 | debug("private a now '%s'\n", arg); |
---|
548 | } |
---|
549 | else |
---|
550 | { |
---|
551 | break; |
---|
552 | } |
---|
553 | } |
---|
554 | char *end; |
---|
555 | int Pm = strtol(arg, &end, 10); |
---|
556 | |
---|
557 | c = buffer[i + 3 + (end - arg)]; |
---|
558 | |
---|
559 | debug("ansi private mouse : command %c, arg %d", c, Pm); |
---|
560 | if (c == 'h') /* DECSET DEC Private Mode Set */ |
---|
561 | { |
---|
562 | |
---|
563 | switch (Pm) |
---|
564 | { |
---|
565 | /* FIXME Handle different modes */ |
---|
566 | case 9: |
---|
567 | debug("mouse : X10 mode\n"); |
---|
568 | sc->report_mouse = MOUSE_X10; |
---|
569 | break; |
---|
570 | case 1000: /* Send Mouse X & Y on button press |
---|
571 | and release. */ |
---|
572 | debug("mouse : VT200 mode\n"); |
---|
573 | sc->report_mouse = MOUSE_VT200; |
---|
574 | break; |
---|
575 | case 1001: /* Use Hilite Mouse Tracking. */ |
---|
576 | debug("mouse : VT200_HIGHLIGHT mode\n"); |
---|
577 | sc->report_mouse = MOUSE_VT200_HIGHLIGHT; |
---|
578 | break; |
---|
579 | case 1002: /* Use Cell Motion Mouse Tracking. */ |
---|
580 | debug("mouse : BTN mode\n"); |
---|
581 | sc->report_mouse = MOUSE_BTN_EVENT; |
---|
582 | break; |
---|
583 | case 1003: /* Use All Motion Mouse Tracking. */ |
---|
584 | debug("mouse : ANY mode\n"); |
---|
585 | sc->report_mouse = MOUSE_ANY_EVENT; |
---|
586 | break; |
---|
587 | default: |
---|
588 | break; |
---|
589 | } |
---|
590 | } |
---|
591 | else if (c == 'l') /* DECRST DEC Private Mode Reset */ |
---|
592 | { |
---|
593 | Pm = atoi(arg); |
---|
594 | switch (Pm) |
---|
595 | { |
---|
596 | /* FIXME Handle different modes */ |
---|
597 | case 9: |
---|
598 | case 1000: /* Send Mouse X & Y on button press |
---|
599 | and release. */ |
---|
600 | case 1001: /* Use Hilite Mouse Tracking. */ |
---|
601 | case 1002: /* Use Cell Motion Mouse Tracking. */ |
---|
602 | case 1003: /* Use All Motion Mouse Tracking. */ |
---|
603 | sc->report_mouse = MOUSE_NONE; |
---|
604 | debug("ansi private mouse : NOT reporting mouse"); |
---|
605 | break; |
---|
606 | default: |
---|
607 | break; |
---|
608 | } |
---|
609 | } |
---|
610 | } |
---|
611 | continue; /* Private sequence, skip it entirely */ |
---|
612 | } |
---|
613 | |
---|
614 | if (final - param > 100) |
---|
615 | continue; /* Suspiciously long sequence, skip it */ |
---|
616 | |
---|
617 | /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string |
---|
618 | format */ |
---|
619 | if (param < inter) |
---|
620 | { |
---|
621 | argv[0] = 0; |
---|
622 | for (j = param; j < inter; j++) |
---|
623 | { |
---|
624 | if (buffer[i + j] == ';') |
---|
625 | argv[++argc] = 0; |
---|
626 | else if (buffer[i + j] >= '0' && buffer[i + j] <= '9') |
---|
627 | argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0'); |
---|
628 | } |
---|
629 | argc++; |
---|
630 | } |
---|
631 | |
---|
632 | /* Interpret final byte. The code representations are given in |
---|
633 | ECMA-48 5.4: Control sequences, and the code definitions are |
---|
634 | given in ECMA-48 8.3: Definition of control functions. */ |
---|
635 | debug("ansi import: command '%c'", buffer[i + final]); |
---|
636 | switch (buffer[i + final]) |
---|
637 | { |
---|
638 | case 'A': /* CUU (0x41) - Cursor Up */ |
---|
639 | y -= argc ? argv[0] : 1; |
---|
640 | if (y < 0) |
---|
641 | y = 0; |
---|
642 | break; |
---|
643 | case 'B': /* CUD (0x42) - Cursor Down */ |
---|
644 | y += argc ? argv[0] : 1; |
---|
645 | break; |
---|
646 | case 'C': /* CUF (0x43) - Cursor Right */ |
---|
647 | x += argc ? argv[0] : 1; |
---|
648 | break; |
---|
649 | case 'D': /* CUB (0x44) - Cursor Left */ |
---|
650 | x -= argc ? argv[0] : 1; |
---|
651 | if (x < 0) |
---|
652 | x = 0; |
---|
653 | break; |
---|
654 | case 'G': /* CHA (0x47) - Cursor Character Absolute */ |
---|
655 | x = (argc && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
656 | break; |
---|
657 | case 'H': /* CUP (0x48) - Cursor Position */ |
---|
658 | x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; |
---|
659 | y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
660 | debug("ansi CUP : Cursor at %dx%d\n", x, y); |
---|
661 | break; |
---|
662 | case 'J': /* ED (0x4a) - Erase In Page */ |
---|
663 | savedattr = caca_get_attr(sc->cv, -1, -1); |
---|
664 | caca_set_attr(sc->cv, sc->clearattr); |
---|
665 | if (!argc || argv[0] == 0) |
---|
666 | { |
---|
667 | caca_draw_line(sc->cv, x, y, width, y, ' '); |
---|
668 | caca_fill_box(sc->cv, 0, y + 1, width, height - 1, ' '); |
---|
669 | } |
---|
670 | else if (argv[0] == 1) |
---|
671 | { |
---|
672 | caca_fill_box(sc->cv, 0, 0, width, y, ' '); |
---|
673 | caca_draw_line(sc->cv, 0, y, x, y, ' '); |
---|
674 | } |
---|
675 | else if (argv[0] == 2) |
---|
676 | { |
---|
677 | // x = y = 0; |
---|
678 | caca_fill_box(sc->cv, 0, 0, width, height, ' '); |
---|
679 | } |
---|
680 | caca_set_attr(sc->cv, savedattr); |
---|
681 | break; |
---|
682 | case 'K': /* EL (0x4b) - Erase In Line */ |
---|
683 | debug("ansi EL : cursor at %dx%d\n", x, y); |
---|
684 | if (!argc || argv[0] == 0) |
---|
685 | { |
---|
686 | caca_draw_line(sc->cv, x, y, width, y, ' '); |
---|
687 | } |
---|
688 | else if (argv[0] == 1) |
---|
689 | { |
---|
690 | caca_draw_line(sc->cv, 0, y, x, y, ' '); |
---|
691 | } |
---|
692 | else if (argv[0] == 2) |
---|
693 | { |
---|
694 | caca_draw_line(sc->cv, 0, y, width, y, ' '); |
---|
695 | } |
---|
696 | break; |
---|
697 | case 'L': /* IL - Insert line */ |
---|
698 | { |
---|
699 | unsigned int nb_lines = argc ? argv[0] : 1; |
---|
700 | for (j = bottom - 1; j >= (unsigned int)y + nb_lines; j--) |
---|
701 | { |
---|
702 | for (k = 0; k < width; k++) |
---|
703 | { |
---|
704 | caca_put_char(sc->cv, k, j, |
---|
705 | caca_get_char(sc->cv, k, |
---|
706 | j - nb_lines)); |
---|
707 | caca_put_attr(sc->cv, k, j, |
---|
708 | caca_get_attr(sc->cv, k, |
---|
709 | j - nb_lines)); |
---|
710 | } |
---|
711 | caca_draw_line(sc->cv, 0, j - nb_lines, width, |
---|
712 | j - nb_lines, ' '); |
---|
713 | } |
---|
714 | } |
---|
715 | break; |
---|
716 | case 'P': /* DCH (0x50) - Delete Character */ |
---|
717 | if (!argc || argv[0] == 0) |
---|
718 | argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */ |
---|
719 | |
---|
720 | for (j = x; (unsigned int)(j + argv[0]) < width; j++) |
---|
721 | { |
---|
722 | caca_put_char(sc->cv, j, y, |
---|
723 | caca_get_char(sc->cv, j + argv[0], y)); |
---|
724 | caca_put_attr(sc->cv, j, y, |
---|
725 | caca_get_attr(sc->cv, j + argv[0], y)); |
---|
726 | } |
---|
727 | break; |
---|
728 | #if 0 |
---|
729 | savedattr = caca_get_attr(sc->cv, -1, -1); |
---|
730 | caca_set_attr(sc->cv, sc->clearattr); |
---|
731 | for (; (unsigned int)j < width; j++) |
---|
732 | caca_put_char(sc->cv, j, y, ' '); |
---|
733 | caca_set_attr(sc->cv, savedattr); |
---|
734 | #endif |
---|
735 | case 'X': /* ECH (0x58) - Erase Character */ |
---|
736 | if (argc && argv[0]) |
---|
737 | { |
---|
738 | savedattr = caca_get_attr(sc->cv, -1, -1); |
---|
739 | caca_set_attr(sc->cv, sc->clearattr); |
---|
740 | caca_draw_line(sc->cv, x, y, x + argv[0] - 1, y, ' '); |
---|
741 | caca_set_attr(sc->cv, savedattr); |
---|
742 | } |
---|
743 | case 'c': /* DA -- Device Attributes */ |
---|
744 | /* |
---|
745 | 0 Base VT100, no options 1 Processor options (STP) 2 |
---|
746 | Advanced video option (AVO) 3 AVO and STP 4 Graphics |
---|
747 | processor option (GPO) 5 GPO and STP 6 GPO and AVO 7 GPO, |
---|
748 | STP, and AVO */ |
---|
749 | /* Warning, argument is Pn */ |
---|
750 | debug("ansi Got command c, argc %d, argv[0] (%d)\n", argc, |
---|
751 | argv[0], argv[0]); |
---|
752 | if (!argc || argv[0] == 0) |
---|
753 | { |
---|
754 | send_ansi_sequence(screen_list, "\x1b[?1;0c"); |
---|
755 | } |
---|
756 | else |
---|
757 | { |
---|
758 | switch (argv[0]) |
---|
759 | { |
---|
760 | case 1: |
---|
761 | send_ansi_sequence(screen_list, "\x1b[?\x1;\x1c"); |
---|
762 | break; |
---|
763 | case 2: |
---|
764 | send_ansi_sequence(screen_list, "\x1b[?\x1;\x2c"); |
---|
765 | break; |
---|
766 | case 3: |
---|
767 | send_ansi_sequence(screen_list, "\x1b[?\x1;\x3c"); |
---|
768 | break; |
---|
769 | case 4: |
---|
770 | send_ansi_sequence(screen_list, "\x1b[?\x1;\x4c"); |
---|
771 | break; |
---|
772 | case 5: |
---|
773 | send_ansi_sequence(screen_list, "\x1b[?\x1;\x5c"); |
---|
774 | break; |
---|
775 | case 6: |
---|
776 | send_ansi_sequence(screen_list, "\x1b[?\x1;\x6c"); |
---|
777 | break; |
---|
778 | case 7: |
---|
779 | send_ansi_sequence(screen_list, "\x1b[?\x1;\x7c"); |
---|
780 | break; |
---|
781 | default: |
---|
782 | debug("Unsupported DA option '%d'\n", argv[0]); |
---|
783 | break; |
---|
784 | } |
---|
785 | } |
---|
786 | break; |
---|
787 | case 'd': /* VPA (0x64) - Line Position Absolute */ |
---|
788 | y = (argc && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
789 | break; |
---|
790 | case 'f': /* HVP (0x66) - Character And Line Position */ |
---|
791 | x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; |
---|
792 | y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
793 | break; |
---|
794 | case 'g': /* TBC -- Tabulation Clear */ |
---|
795 | break; |
---|
796 | case 'r': /* FIXME */ |
---|
797 | if (argc == 2) /* DCSTBM - Set top and bottom margin */ |
---|
798 | { |
---|
799 | debug("DCSTBM %d %d", argv[0], argv[1]); |
---|
800 | top = argv[0]; |
---|
801 | bottom = argv[1]; |
---|
802 | } |
---|
803 | else |
---|
804 | debug("ansi import: command r with %d params", argc); |
---|
805 | break; |
---|
806 | case 'h': /* SM (0x68) - FIXME */ |
---|
807 | debug("ansi import: set mode %i", argc ? (int)argv[0] : -1); |
---|
808 | break; |
---|
809 | case 'l': /* RM (0x6c) - FIXME */ |
---|
810 | debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1); |
---|
811 | break; |
---|
812 | case 'm': /* SGR (0x6d) - Select Graphic Rendition */ |
---|
813 | if (argc) |
---|
814 | ansi_parse_grcm(sc, argc, argv); |
---|
815 | else |
---|
816 | ansi_parse_grcm(sc, 1, &dummy); |
---|
817 | break; |
---|
818 | case 'n': |
---|
819 | debug("ansi command n, argc %d, argv[0] %d\n", argc, argv[0]); |
---|
820 | if (!argc) |
---|
821 | break; |
---|
822 | |
---|
823 | switch (argv[0]) |
---|
824 | { |
---|
825 | case 5: |
---|
826 | /* Term ok */ |
---|
827 | send_ansi_sequence(screen_list, "\x1b[0n"); |
---|
828 | break; |
---|
829 | case 6: |
---|
830 | /* Cursor Position */ |
---|
831 | sprintf(b, "\x1b[%d;%dR", y + 1, x + 1); |
---|
832 | send_ansi_sequence(screen_list, b); |
---|
833 | break; |
---|
834 | } |
---|
835 | |
---|
836 | break; |
---|
837 | case 's': /* Private (save cursor position) */ |
---|
838 | save_x = x; |
---|
839 | save_y = y; |
---|
840 | break; |
---|
841 | case 'u': /* Private (reload cursor position) */ |
---|
842 | x = save_x; |
---|
843 | y = save_y; |
---|
844 | break; |
---|
845 | default: |
---|
846 | debug("ansi import: unknown command \"^[%.*s\"", |
---|
847 | final - param + 1, buffer + i + param); |
---|
848 | break; |
---|
849 | } |
---|
850 | } |
---|
851 | |
---|
852 | /* Parse OSC stuff. */ |
---|
853 | else if (buffer[i] == '\033' && buffer[i + 1] == ']') |
---|
854 | { |
---|
855 | char *string; |
---|
856 | unsigned int command = 0; |
---|
857 | unsigned int mode = 2, semicolon, final; |
---|
858 | |
---|
859 | for (semicolon = mode; i + semicolon < size; semicolon++) |
---|
860 | { |
---|
861 | if (buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9') |
---|
862 | break; |
---|
863 | command = 10 * command + (buffer[i + semicolon] - '0'); |
---|
864 | } |
---|
865 | |
---|
866 | if (i + semicolon >= size || buffer[i + semicolon] != ';') |
---|
867 | break; /* Invalid Mode */ |
---|
868 | |
---|
869 | for (final = semicolon + 1; i + final < size; final++) |
---|
870 | if (buffer[i + final] < 0x20) |
---|
871 | break; |
---|
872 | |
---|
873 | if (i + final >= size || buffer[i + final] != '\a') |
---|
874 | break; /* Not enough data or no bell found */ |
---|
875 | /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */ |
---|
876 | /* FIXME: differenciate between not enough data (try again) and |
---|
877 | invalid data (print shit) */ |
---|
878 | |
---|
879 | skip += final; |
---|
880 | |
---|
881 | string = malloc(final - (semicolon + 1) + 1); |
---|
882 | memcpy(string, buffer + i + (semicolon + 1), |
---|
883 | final - (semicolon + 1)); |
---|
884 | string[final - (semicolon + 1)] = '\0'; |
---|
885 | debug("ansi import: got OSC command %i string '%s'", command, |
---|
886 | string); |
---|
887 | if (command == 0 || command == 2) |
---|
888 | { |
---|
889 | if (sc->title) |
---|
890 | free(sc->title); |
---|
891 | sc->title = string; |
---|
892 | } |
---|
893 | else |
---|
894 | free(string); |
---|
895 | } |
---|
896 | |
---|
897 | /* Get the character we’re going to paste */ |
---|
898 | else |
---|
899 | { |
---|
900 | size_t bytes; |
---|
901 | |
---|
902 | if (i + 6 < size) |
---|
903 | { |
---|
904 | ch = caca_utf8_to_utf32((char const *)(buffer + i), &bytes); |
---|
905 | } |
---|
906 | else |
---|
907 | { |
---|
908 | /* Add a trailing zero to what we're going to read */ |
---|
909 | char tmp[7]; |
---|
910 | memcpy(tmp, buffer + i, size - i); |
---|
911 | tmp[size - i] = '\0'; |
---|
912 | ch = caca_utf8_to_utf32(tmp, &bytes); |
---|
913 | } |
---|
914 | |
---|
915 | if (!bytes) |
---|
916 | { |
---|
917 | /* If the Unicode is invalid, assume it was latin1. */ |
---|
918 | ch = buffer[i]; |
---|
919 | bytes = 1; |
---|
920 | } |
---|
921 | |
---|
922 | /* very incomplete ISO-2022 implementation tailored to DEC ACS */ |
---|
923 | if (sc->conv_state.cs == '@') |
---|
924 | { |
---|
925 | if (((ch > ' ') && (ch <= '~')) |
---|
926 | && |
---|
927 | (sc->conv_state. |
---|
928 | gn[sc->conv_state.ss ? sc->conv_state. |
---|
929 | gn[sc->conv_state.ss] : sc->conv_state.glr[0]] == '0')) |
---|
930 | { |
---|
931 | ch = dec_acs(ch); |
---|
932 | } |
---|
933 | else if (((ch > 0x80) && (ch < 0xff)) |
---|
934 | && (sc->conv_state.gn[sc->conv_state.glr[1]] == '0')) |
---|
935 | { |
---|
936 | ch = dec_acs(ch + ' ' - 0x80); |
---|
937 | } |
---|
938 | } |
---|
939 | sc->conv_state.ss = 0; /* no single-shift (GL) */ |
---|
940 | |
---|
941 | wch = caca_utf32_is_fullwidth(ch) ? 2 : 1; |
---|
942 | |
---|
943 | skip += bytes - 1; |
---|
944 | } |
---|
945 | |
---|
946 | /* Wrap long lines or grow horizontally */ |
---|
947 | while ((unsigned int)x + wch > width) |
---|
948 | { |
---|
949 | x -= width; |
---|
950 | y++; |
---|
951 | } |
---|
952 | |
---|
953 | /* Scroll or grow vertically */ |
---|
954 | if ((unsigned int)y >= bottom) |
---|
955 | { |
---|
956 | int lines = (y - bottom) + 1; |
---|
957 | |
---|
958 | savedattr = caca_get_attr(sc->cv, -1, -1); |
---|
959 | |
---|
960 | for (j = top - 1; j + lines < bottom; j++) |
---|
961 | { |
---|
962 | for (k = 0; k < width; k++) |
---|
963 | { |
---|
964 | caca_put_char(sc->cv, k, j, |
---|
965 | caca_get_char(sc->cv, k, j + lines)); |
---|
966 | caca_put_attr(sc->cv, k, j, |
---|
967 | caca_get_attr(sc->cv, k, j + lines)); |
---|
968 | } |
---|
969 | } |
---|
970 | caca_set_attr(sc->cv, sc->clearattr); |
---|
971 | caca_fill_box(sc->cv, 0, bottom - lines, width, bottom - 1, ' '); |
---|
972 | y -= lines; |
---|
973 | caca_set_attr(sc->cv, savedattr); |
---|
974 | } |
---|
975 | |
---|
976 | /* Now paste our character, if any */ |
---|
977 | if (wch) |
---|
978 | { |
---|
979 | caca_put_char(sc->cv, x, y, ch); |
---|
980 | x += wch; |
---|
981 | } |
---|
982 | } |
---|
983 | |
---|
984 | caca_gotoxy(sc->cv, x, y); |
---|
985 | |
---|
986 | if (i) |
---|
987 | sc->changed = 1; |
---|
988 | return i; |
---|
989 | } |
---|
990 | |
---|
991 | /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO IR |
---|
992 | 189) */ |
---|
993 | |
---|
994 | static void reset_conv_state(struct screen *sc) |
---|
995 | { |
---|
996 | sc->conv_state.cs = '@'; /* ISO-2022 coding system */ |
---|
997 | sc->conv_state.cn[0] = '@'; /* ISO 646 C0 control charset */ |
---|
998 | sc->conv_state.cn[1] = 'C'; /* ISO 6429-1983 C1 control charset */ |
---|
999 | sc->conv_state.glr[0] = 0; /* G0 in GL */ |
---|
1000 | sc->conv_state.glr[1] = 2; /* G2 in GR */ |
---|
1001 | sc->conv_state.gn[0] = 'B'; /* US-ASCII G0 charset */ |
---|
1002 | sc->conv_state.gn[1] = '0'; /* DEC ACS G1 charset */ |
---|
1003 | sc->conv_state.gn[2] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G2 |
---|
1004 | charset */ |
---|
1005 | sc->conv_state.gn[3] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G3 |
---|
1006 | charset */ |
---|
1007 | sc->conv_state.ss = 0; /* no single-shift (GL) */ |
---|
1008 | sc->conv_state.ctrl8bit = 1; |
---|
1009 | } |
---|
1010 | |
---|
1011 | /* XXX : ANSI loader helper */ |
---|
1012 | |
---|
1013 | static void ansi_parse_grcm(struct screen *sc, |
---|
1014 | unsigned int argc, unsigned int const *argv) |
---|
1015 | { |
---|
1016 | static uint8_t const ansi2caca[] = { |
---|
1017 | CACA_BLACK, CACA_RED, CACA_GREEN, CACA_BROWN, |
---|
1018 | CACA_BLUE, CACA_MAGENTA, CACA_CYAN, CACA_LIGHTGRAY |
---|
1019 | }; |
---|
1020 | |
---|
1021 | unsigned int j; |
---|
1022 | uint8_t efg, ebg; /* Effective (libcaca) fg/bg */ |
---|
1023 | |
---|
1024 | for (j = 0; j < argc; j++) |
---|
1025 | { |
---|
1026 | /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */ |
---|
1027 | if (argv[j] >= 30 && argv[j] <= 37) |
---|
1028 | sc->fg = ansi2caca[argv[j] - 30]; |
---|
1029 | else if (argv[j] >= 40 && argv[j] <= 47) |
---|
1030 | sc->bg = ansi2caca[argv[j] - 40]; |
---|
1031 | else if (argv[j] >= 90 && argv[j] <= 97) |
---|
1032 | sc->fg = ansi2caca[argv[j] - 90] + 8; |
---|
1033 | else if (argv[j] >= 100 && argv[j] <= 107) |
---|
1034 | sc->bg = ansi2caca[argv[j] - 100] + 8; |
---|
1035 | else |
---|
1036 | switch (argv[j]) |
---|
1037 | { |
---|
1038 | case 0: /* default rendition */ |
---|
1039 | sc->fg = sc->dfg; |
---|
1040 | sc->bg = sc->dbg; |
---|
1041 | sc->bold = sc->blink = sc->italics = sc->negative |
---|
1042 | = sc->concealed = sc->underline = sc->faint = sc->strike |
---|
1043 | = sc->proportional = 0; |
---|
1044 | break; |
---|
1045 | case 1: /* bold or increased intensity */ |
---|
1046 | sc->bold = 1; |
---|
1047 | break; |
---|
1048 | case 2: /* faint, decreased intensity or second colour |
---|
1049 | */ |
---|
1050 | sc->faint = 1; |
---|
1051 | break; |
---|
1052 | case 3: /* italicized */ |
---|
1053 | sc->italics = 1; |
---|
1054 | break; |
---|
1055 | case 4: /* singly underlined */ |
---|
1056 | sc->underline = 1; |
---|
1057 | break; |
---|
1058 | case 5: /* slowly blinking (less then 150 per minute) */ |
---|
1059 | case 6: /* rapidly blinking (150 per minute or more) */ |
---|
1060 | sc->blink = 1; |
---|
1061 | break; |
---|
1062 | case 7: /* negative image */ |
---|
1063 | sc->negative = 1; |
---|
1064 | break; |
---|
1065 | case 8: /* concealed characters */ |
---|
1066 | sc->concealed = 1; |
---|
1067 | break; |
---|
1068 | case 9: /* crossed-out (characters still legible but |
---|
1069 | marked as to be deleted */ |
---|
1070 | sc->strike = 1; |
---|
1071 | break; |
---|
1072 | case 21: /* doubly underlined */ |
---|
1073 | sc->underline = 1; |
---|
1074 | break; |
---|
1075 | case 22: /* normal colour or normal intensity (neither |
---|
1076 | bold nor faint) */ |
---|
1077 | sc->bold = sc->faint = 0; |
---|
1078 | break; |
---|
1079 | case 23: /* not italicized, not fraktur */ |
---|
1080 | sc->italics = 0; |
---|
1081 | break; |
---|
1082 | case 24: /* not underlined (neither singly nor doubly) */ |
---|
1083 | sc->underline = 0; |
---|
1084 | break; |
---|
1085 | case 25: /* steady (not blinking) */ |
---|
1086 | sc->blink = 0; |
---|
1087 | break; |
---|
1088 | case 26: /* (reserved for proportional spacing as |
---|
1089 | specified in CCITT Recommendation T.61) */ |
---|
1090 | sc->proportional = 1; |
---|
1091 | break; |
---|
1092 | case 27: /* positive image */ |
---|
1093 | sc->negative = 0; |
---|
1094 | break; |
---|
1095 | case 28: /* revealed characters */ |
---|
1096 | sc->concealed = 0; |
---|
1097 | break; |
---|
1098 | case 29: /* not crossed out */ |
---|
1099 | sc->strike = 0; |
---|
1100 | break; |
---|
1101 | case 38: /* (reserved for future standardization, |
---|
1102 | intended for setting character foreground |
---|
1103 | colour as specified in ISO 8613-6 [CCITT |
---|
1104 | Recommendation T.416]) */ |
---|
1105 | break; |
---|
1106 | case 39: /* default display colour |
---|
1107 | (implementation-defined) */ |
---|
1108 | sc->fg = sc->dfg; |
---|
1109 | break; |
---|
1110 | case 48: /* (reserved for future standardization, |
---|
1111 | intended for setting character background |
---|
1112 | colour as specified in ISO 8613-6 [CCITT |
---|
1113 | Recommendation T.416]) */ |
---|
1114 | break; |
---|
1115 | case 49: /* default background colour |
---|
1116 | (implementation-defined) */ |
---|
1117 | sc->bg = sc->dbg; |
---|
1118 | break; |
---|
1119 | case 50: /* (reserved for cancelling the effect of the |
---|
1120 | rendering aspect established by parameter |
---|
1121 | value 26) */ |
---|
1122 | sc->proportional = 0; |
---|
1123 | break; |
---|
1124 | default: |
---|
1125 | debug("ansi import: unknown sgr %i", argv[j]); |
---|
1126 | break; |
---|
1127 | } |
---|
1128 | } |
---|
1129 | |
---|
1130 | if (sc->concealed) |
---|
1131 | { |
---|
1132 | efg = ebg = CACA_TRANSPARENT; |
---|
1133 | } |
---|
1134 | else |
---|
1135 | { |
---|
1136 | efg = sc->negative ? sc->bg : sc->fg; |
---|
1137 | ebg = sc->negative ? sc->fg : sc->bg; |
---|
1138 | |
---|
1139 | if (sc->bold) |
---|
1140 | { |
---|
1141 | if (efg < 8) |
---|
1142 | efg += 8; |
---|
1143 | else if (efg == CACA_DEFAULT) |
---|
1144 | efg = CACA_WHITE; |
---|
1145 | } |
---|
1146 | } |
---|
1147 | |
---|
1148 | caca_set_color_ansi(sc->cv, efg, ebg); |
---|
1149 | } |
---|