1 | /* |
---|
2 | * libcucul Canvas for ultrafast compositing of Unicode letters |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: import.c 1431 2006-11-27 01:56:37Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains various import functions. |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | #if !defined __KERNEL__ |
---|
22 | # include <stdio.h> |
---|
23 | # include <stdlib.h> |
---|
24 | # include <string.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "cucul.h" |
---|
28 | #include "cucul_internals.h" |
---|
29 | |
---|
30 | static inline uint32_t sscanu32(void const *s) |
---|
31 | { |
---|
32 | uint32_t x; |
---|
33 | memcpy(&x, s, 4); |
---|
34 | return hton32(x); |
---|
35 | } |
---|
36 | |
---|
37 | static inline uint16_t sscanu16(void const *s) |
---|
38 | { |
---|
39 | uint16_t x; |
---|
40 | memcpy(&x, s, 2); |
---|
41 | return hton16(x); |
---|
42 | } |
---|
43 | |
---|
44 | struct import |
---|
45 | { |
---|
46 | uint32_t clearattr; |
---|
47 | |
---|
48 | /* ANSI Graphic Rendition Combination Mode */ |
---|
49 | uint8_t fg, bg; /* ANSI-context fg/bg */ |
---|
50 | uint8_t dfg, dbg; /* Default fg/bg */ |
---|
51 | uint8_t bold, blink, italics, negative, concealed, underline; |
---|
52 | uint8_t faint, strike, proportional; /* unsupported */ |
---|
53 | }; |
---|
54 | |
---|
55 | static long int import_caca(cucul_canvas_t *, void const *, unsigned int); |
---|
56 | static long int import_text(cucul_canvas_t *, void const *, unsigned int); |
---|
57 | static long int import_ansi(cucul_canvas_t *, void const *, unsigned int, int); |
---|
58 | |
---|
59 | static void ansi_parse_grcm(cucul_canvas_t *, struct import *, |
---|
60 | unsigned int, unsigned int const *); |
---|
61 | |
---|
62 | /** \brief Import a memory buffer into a canvas |
---|
63 | * |
---|
64 | * Import a memory buffer into the given libcucul canvas's current |
---|
65 | * frame. The current frame is resized accordingly and its contents are |
---|
66 | * replaced with the imported data. |
---|
67 | * |
---|
68 | * Valid values for \c format are: |
---|
69 | * - \c "": attempt to autodetect the file format. |
---|
70 | * - \c "caca": import native libcaca files. |
---|
71 | * - \c "text": import ASCII text files. |
---|
72 | * - \c "ansi": import ANSI files. |
---|
73 | * - \c "utf8": import UTF-8 files with ANSI colour codes. |
---|
74 | * |
---|
75 | * The number of bytes read is returned. If the file format is valid, but |
---|
76 | * not enough data was available, 0 is returned. |
---|
77 | * |
---|
78 | * If an error occurs, -1 is returned and \b errno is set accordingly: |
---|
79 | * - \c ENOMEM Not enough memory to allocate canvas. |
---|
80 | * - \c EINVAL Invalid format requested. |
---|
81 | * |
---|
82 | * \param cv A libcucul canvas in which to import the file. |
---|
83 | * \param data A memory area containing the data to be loaded into the canvas. |
---|
84 | * \param len The size in bytes of the memory area. |
---|
85 | * \param format A string describing the input format. |
---|
86 | * \return The number of bytes read, or 0 if there was not enough data, |
---|
87 | * or -1 if an error occurred. |
---|
88 | */ |
---|
89 | long int cucul_import_memory(cucul_canvas_t *cv, void const *data, |
---|
90 | unsigned long int len, char const *format) |
---|
91 | { |
---|
92 | if(!strcasecmp("caca", format)) |
---|
93 | return import_caca(cv, data, len); |
---|
94 | if(!strcasecmp("utf8", format)) |
---|
95 | return import_ansi(cv, data, len, 1); |
---|
96 | if(!strcasecmp("text", format)) |
---|
97 | return import_text(cv, data, len); |
---|
98 | if(!strcasecmp("ansi", format)) |
---|
99 | return import_ansi(cv, data, len, 0); |
---|
100 | |
---|
101 | /* Autodetection */ |
---|
102 | if(!strcasecmp("", format)) |
---|
103 | { |
---|
104 | unsigned char const *str = data; |
---|
105 | unsigned int i; |
---|
106 | |
---|
107 | /* If 4 first bytes are 0xcaca + 'CV' */ |
---|
108 | if(len >= 4 && str[0] == 0xca && |
---|
109 | str[1] == 0xca && str[2] == 'C' && str[3] == 'V') |
---|
110 | return import_caca(cv, data, len); |
---|
111 | |
---|
112 | /* If we find ESC[ argv, we guess it's an ANSI file */ |
---|
113 | for(i = 0; i + 1 < len; i++) |
---|
114 | if((str[i] == 0x1b) && (str[i + 1] == '[')) |
---|
115 | return import_ansi(cv, data, len, 0); |
---|
116 | |
---|
117 | /* Otherwise, import it as text */ |
---|
118 | return import_text(cv, data, len); |
---|
119 | } |
---|
120 | |
---|
121 | seterrno(EINVAL); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | /** \brief Import a file into a canvas |
---|
126 | * |
---|
127 | * Import a file into the given libcucul canvas's current frame. The |
---|
128 | * current frame is resized accordingly and its contents are replaced |
---|
129 | * with the imported data. |
---|
130 | * |
---|
131 | * Valid values for \c format are: |
---|
132 | * - \c "": attempt to autodetect the file format. |
---|
133 | * - \c "caca": import native libcaca files. |
---|
134 | * - \c "text": import ASCII text files. |
---|
135 | * - \c "ansi": import ANSI files. |
---|
136 | * - \c "utf8": import UTF-8 files with ANSI colour codes. |
---|
137 | * |
---|
138 | * The number of bytes read is returned. If the file format is valid, but |
---|
139 | * not enough data was available, 0 is returned. |
---|
140 | * |
---|
141 | * If an error occurs, -1 is returned and \b errno is set accordingly: |
---|
142 | * - \c ENOSYS File access is not implemented on this system. |
---|
143 | * - \c ENOMEM Not enough memory to allocate canvas. |
---|
144 | * - \c EINVAL Invalid format requested. |
---|
145 | * cucul_import_file() may also fail and set \b errno for any of the |
---|
146 | * errors specified for the routine fopen(). |
---|
147 | * |
---|
148 | * \param cv A libcucul canvas in which to import the file. |
---|
149 | * \param filename The name of the file to load. |
---|
150 | * \param format A string describing the input format. |
---|
151 | * \return The number of bytes read, or 0 if there was not enough data, |
---|
152 | * or -1 if an error occurred. |
---|
153 | */ |
---|
154 | long int cucul_import_file(cucul_canvas_t *cv, char const *filename, |
---|
155 | char const *format) |
---|
156 | { |
---|
157 | #if defined __KERNEL__ |
---|
158 | seterrno(ENOSYS); |
---|
159 | return -1; |
---|
160 | #else |
---|
161 | FILE *fp; |
---|
162 | void *data; |
---|
163 | long int size; |
---|
164 | int ret; |
---|
165 | |
---|
166 | fp = fopen(filename, "rb"); |
---|
167 | if(!fp) |
---|
168 | return -1; /* fopen already set errno */ |
---|
169 | |
---|
170 | fseek(fp, 0, SEEK_END); |
---|
171 | size = ftell(fp); |
---|
172 | |
---|
173 | data = malloc(size); |
---|
174 | if(!data) |
---|
175 | { |
---|
176 | fclose(fp); |
---|
177 | seterrno(ENOMEM); |
---|
178 | return -1; |
---|
179 | } |
---|
180 | |
---|
181 | fseek(fp, 0, SEEK_SET); |
---|
182 | fread(data, size, 1, fp); |
---|
183 | fclose(fp); |
---|
184 | |
---|
185 | ret = cucul_import_memory(cv, data, size, format); |
---|
186 | free(data); |
---|
187 | |
---|
188 | return ret; |
---|
189 | #endif |
---|
190 | } |
---|
191 | |
---|
192 | /** \brief Get available import formats |
---|
193 | * |
---|
194 | * Return a list of available import formats. The list is a NULL-terminated |
---|
195 | * array of strings, interleaving a string containing the internal value for |
---|
196 | * the import format, to be used with cucul_import_canvas(), and a string |
---|
197 | * containing the natural language description for that import format. |
---|
198 | * |
---|
199 | * This function never fails. |
---|
200 | * |
---|
201 | * \return An array of strings. |
---|
202 | */ |
---|
203 | char const * const * cucul_get_import_list(void) |
---|
204 | { |
---|
205 | static char const * const list[] = |
---|
206 | { |
---|
207 | "", "autodetect", |
---|
208 | "caca", "native libcaca format", |
---|
209 | "text", "plain text", |
---|
210 | "ansi", "ANSI coloured text", |
---|
211 | "utf8", "UTF-8 files with ANSI colour codes", |
---|
212 | NULL, NULL |
---|
213 | }; |
---|
214 | |
---|
215 | return list; |
---|
216 | } |
---|
217 | |
---|
218 | /* |
---|
219 | * XXX: the following functions are local. |
---|
220 | */ |
---|
221 | |
---|
222 | static long int import_caca(cucul_canvas_t *cv, |
---|
223 | void const *data, unsigned int size) |
---|
224 | { |
---|
225 | uint8_t const *buf = (uint8_t const *)data; |
---|
226 | unsigned int control_size, data_size, expected_size, frames, f, n; |
---|
227 | uint16_t version, flags; |
---|
228 | |
---|
229 | if(size < 20) |
---|
230 | return 0; |
---|
231 | |
---|
232 | if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V') |
---|
233 | { |
---|
234 | debug("caca import error: expected \\xca\\xcaCV header"); |
---|
235 | goto invalid_caca; |
---|
236 | } |
---|
237 | |
---|
238 | control_size = sscanu32(buf + 4); |
---|
239 | data_size = sscanu32(buf + 8); |
---|
240 | version = sscanu16(buf + 12); |
---|
241 | frames = sscanu32(buf + 14); |
---|
242 | flags = sscanu16(buf + 18); |
---|
243 | |
---|
244 | if(size < 4 + control_size + data_size) |
---|
245 | return 0; |
---|
246 | |
---|
247 | if(control_size < 16 + frames * 32) |
---|
248 | { |
---|
249 | debug("caca import error: control size %lu < expected %lu", |
---|
250 | (unsigned long int)control_size, 16 + frames * 32); |
---|
251 | goto invalid_caca; |
---|
252 | } |
---|
253 | |
---|
254 | for(expected_size = 0, f = 0; f < frames; f++) |
---|
255 | { |
---|
256 | unsigned int width, height, duration; |
---|
257 | uint32_t attr; |
---|
258 | int x, y, handlex, handley; |
---|
259 | |
---|
260 | width = sscanu32(buf + 4 + 16 + f * 24); |
---|
261 | height = sscanu32(buf + 4 + 16 + f * 24 + 4); |
---|
262 | duration = sscanu32(buf + 4 + 16 + f * 24 + 8); |
---|
263 | attr = sscanu32(buf + 4 + 16 + f * 24 + 12); |
---|
264 | x = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 16); |
---|
265 | y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20); |
---|
266 | handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 24); |
---|
267 | handley = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 28); |
---|
268 | |
---|
269 | expected_size += width * height * 8; |
---|
270 | } |
---|
271 | |
---|
272 | if(expected_size != data_size) |
---|
273 | { |
---|
274 | debug("caca import error: data size %lu < expected %lu", |
---|
275 | (unsigned long int)data_size, (unsigned long int)expected_size); |
---|
276 | goto invalid_caca; |
---|
277 | } |
---|
278 | |
---|
279 | /* FIXME: read all frames, not only the first one */ |
---|
280 | cucul_set_canvas_size(cv, 0, 0); |
---|
281 | cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16), |
---|
282 | sscanu32(buf + 4 + 16 + 4)); |
---|
283 | |
---|
284 | /* FIXME: check for return value */ |
---|
285 | |
---|
286 | for(n = sscanu32(buf + 4 + 16) * sscanu32(buf + 4 + 16 + 4); n--; ) |
---|
287 | { |
---|
288 | cv->chars[n] = sscanu32(buf + 4 + control_size + 8 * n); |
---|
289 | cv->attrs[n] = sscanu32(buf + 4 + control_size + 8 * n + 4); |
---|
290 | } |
---|
291 | |
---|
292 | cv->curattr = sscanu32(buf + 4 + 16 + 12); |
---|
293 | cv->frames[0].x = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 16); |
---|
294 | cv->frames[0].y = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 20); |
---|
295 | cv->frames[0].handlex = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 24); |
---|
296 | cv->frames[0].handley = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 28); |
---|
297 | |
---|
298 | return 4 + control_size + data_size; |
---|
299 | |
---|
300 | invalid_caca: |
---|
301 | seterrno(EINVAL); |
---|
302 | return -1; |
---|
303 | } |
---|
304 | |
---|
305 | static long int import_text(cucul_canvas_t *cv, |
---|
306 | void const *data, unsigned int size) |
---|
307 | { |
---|
308 | char const *text = (char const *)data; |
---|
309 | unsigned int width = 0, height = 0, x = 0, y = 0, i; |
---|
310 | |
---|
311 | cucul_set_canvas_size(cv, width, height); |
---|
312 | |
---|
313 | for(i = 0; i < size; i++) |
---|
314 | { |
---|
315 | unsigned char ch = *text++; |
---|
316 | |
---|
317 | if(ch == '\r') |
---|
318 | continue; |
---|
319 | |
---|
320 | if(ch == '\n') |
---|
321 | { |
---|
322 | x = 0; |
---|
323 | y++; |
---|
324 | continue; |
---|
325 | } |
---|
326 | |
---|
327 | if(x >= width || y >= height) |
---|
328 | { |
---|
329 | if(x >= width) |
---|
330 | width = x + 1; |
---|
331 | |
---|
332 | if(y >= height) |
---|
333 | height = y + 1; |
---|
334 | |
---|
335 | cucul_set_canvas_size(cv, width, height); |
---|
336 | } |
---|
337 | |
---|
338 | cucul_put_char(cv, x, y, ch); |
---|
339 | x++; |
---|
340 | } |
---|
341 | |
---|
342 | if(y > height) |
---|
343 | cucul_set_canvas_size(cv, width, height = y); |
---|
344 | |
---|
345 | return size; |
---|
346 | } |
---|
347 | |
---|
348 | static long int import_ansi(cucul_canvas_t *cv, |
---|
349 | void const *data, unsigned int size, int utf8) |
---|
350 | { |
---|
351 | struct import *im; |
---|
352 | unsigned char const *buffer = (unsigned char const*)data; |
---|
353 | unsigned int i, j, init, skip, growx = 0, growy = 0, dummy = 0; |
---|
354 | unsigned int width, height; |
---|
355 | uint32_t savedattr; |
---|
356 | int x = 0, y = 0, save_x = 0, save_y = 0; |
---|
357 | |
---|
358 | init = !!cv->frames[cv->frame].import; |
---|
359 | |
---|
360 | if(!init) |
---|
361 | { |
---|
362 | cv->frames[cv->frame].import = malloc(sizeof(struct import)); |
---|
363 | memset(cv->frames[cv->frame].import, 0, sizeof(struct import)); |
---|
364 | } |
---|
365 | |
---|
366 | im = (struct import *)cv->frames[cv->frame].import; |
---|
367 | |
---|
368 | if(utf8) |
---|
369 | { |
---|
370 | width = cv->width; |
---|
371 | height = cv->height; |
---|
372 | growx = !width; |
---|
373 | growy = !height; |
---|
374 | x = cv->frames[cv->frame].x; |
---|
375 | y = cv->frames[cv->frame].y; |
---|
376 | } |
---|
377 | else |
---|
378 | { |
---|
379 | cucul_set_canvas_size(cv, width = 80, height = 0); |
---|
380 | growx = 0; |
---|
381 | growy = 1; |
---|
382 | } |
---|
383 | |
---|
384 | if(!init) |
---|
385 | { |
---|
386 | if(utf8) |
---|
387 | { |
---|
388 | im->dfg = CUCUL_DEFAULT; |
---|
389 | im->dbg = CUCUL_TRANSPARENT; |
---|
390 | } |
---|
391 | else |
---|
392 | { |
---|
393 | im->dfg = CUCUL_LIGHTGRAY; |
---|
394 | im->dbg = CUCUL_BLACK; |
---|
395 | } |
---|
396 | |
---|
397 | cucul_set_color_ansi(cv, im->dfg, im->dbg); |
---|
398 | im->clearattr = cucul_get_attr(cv, -1, -1); |
---|
399 | |
---|
400 | ansi_parse_grcm(cv, im, 1, &dummy); |
---|
401 | } |
---|
402 | |
---|
403 | for(i = 0; i < size; i += skip) |
---|
404 | { |
---|
405 | uint32_t ch = 0; |
---|
406 | int wch = 0; |
---|
407 | |
---|
408 | skip = 1; |
---|
409 | |
---|
410 | if(!utf8 && buffer[i] == '\x1a' && i + 7 < size |
---|
411 | && !memcmp(buffer + i + 1, "SAUCE00", 7)) |
---|
412 | break; /* End before SAUCE data */ |
---|
413 | |
---|
414 | else if(buffer[i] == '\r') |
---|
415 | { |
---|
416 | x = 0; |
---|
417 | } |
---|
418 | |
---|
419 | else if(buffer[i] == '\n') |
---|
420 | { |
---|
421 | x = 0; |
---|
422 | y++; |
---|
423 | } |
---|
424 | |
---|
425 | else if(buffer[i] == '\t') |
---|
426 | { |
---|
427 | x = (x + 7) & ~7; |
---|
428 | } |
---|
429 | |
---|
430 | else if(buffer[i] == '\x08') |
---|
431 | { |
---|
432 | if(x > 0) |
---|
433 | x--; |
---|
434 | } |
---|
435 | |
---|
436 | /* If there are not enough characters to parse the escape sequence, |
---|
437 | * wait until the next try. We require 3. */ |
---|
438 | else if(buffer[i] == '\x1b' && i + 2 >= size) |
---|
439 | break; |
---|
440 | |
---|
441 | /* XXX: What the fuck is this shit? */ |
---|
442 | else if(buffer[i] == '\x1b' && buffer[i + 1] == '(' |
---|
443 | && buffer[i + 2] == 'B') |
---|
444 | { |
---|
445 | skip += 2; |
---|
446 | } |
---|
447 | |
---|
448 | /* Interpret escape commands, as per Standard ECMA-48 "Control |
---|
449 | * Functions for Coded Character Sets", 5.4. Control sequences. */ |
---|
450 | else if(buffer[i] == '\x1b' && buffer[i + 1] == '[') |
---|
451 | { |
---|
452 | unsigned int argc = 0, argv[101]; |
---|
453 | unsigned int param, inter, final; |
---|
454 | |
---|
455 | /* Compute offsets to parameter bytes, intermediate bytes and |
---|
456 | * to the final byte. Only the final byte is mandatory, there |
---|
457 | * can be zero of the others. |
---|
458 | * 0 param=2 inter final final+1 |
---|
459 | * +-----+------------------+---------------------+-----------------+ |
---|
460 | * | CSI | parameter bytes | intermediate bytes | final byte | |
---|
461 | * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | |
---|
462 | * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ | |
---|
463 | * +-----+------------------+---------------------+-----------------+ |
---|
464 | */ |
---|
465 | param = 2; |
---|
466 | |
---|
467 | for(inter = param; i + inter < size; inter++) |
---|
468 | if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f) |
---|
469 | break; |
---|
470 | |
---|
471 | for(final = inter; i + final < size; final++) |
---|
472 | if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f) |
---|
473 | break; |
---|
474 | |
---|
475 | if(i + final >= size |
---|
476 | || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e) |
---|
477 | break; /* Invalid Final Byte */ |
---|
478 | |
---|
479 | skip += final; |
---|
480 | |
---|
481 | /* Sanity checks */ |
---|
482 | if(param < inter && buffer[i + param] >= 0x3c) |
---|
483 | { |
---|
484 | /* Private sequence, only parse what we know */ |
---|
485 | debug("ansi import: private sequence \"^[[%.*s\"", |
---|
486 | final - param + 1, buffer + i + param); |
---|
487 | continue; /* Private sequence, skip it entirely */ |
---|
488 | } |
---|
489 | |
---|
490 | if(final - param > 100) |
---|
491 | continue; /* Suspiciously long sequence, skip it */ |
---|
492 | |
---|
493 | /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string |
---|
494 | * format */ |
---|
495 | if(param < inter) |
---|
496 | { |
---|
497 | argv[0] = 0; |
---|
498 | for(j = param; j < inter; j++) |
---|
499 | { |
---|
500 | if(buffer[i + j] == ';') |
---|
501 | argv[++argc] = 0; |
---|
502 | else if(buffer[i + j] >= '0' && buffer[i + j] <= '9') |
---|
503 | argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0'); |
---|
504 | } |
---|
505 | argc++; |
---|
506 | } |
---|
507 | |
---|
508 | /* Interpret final byte. The code representations are given in |
---|
509 | * ECMA-48 5.4: Control sequences, and the code definitions are |
---|
510 | * given in ECMA-48 8.3: Definition of control functions. */ |
---|
511 | switch(buffer[i + final]) |
---|
512 | { |
---|
513 | case 'H': /* CUP (0x48) - Cursor Position */ |
---|
514 | x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; |
---|
515 | y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
516 | break; |
---|
517 | case 'A': /* CUU (0x41) - Cursor Up */ |
---|
518 | y -= argc ? argv[0] : 1; |
---|
519 | if(y < 0) |
---|
520 | y = 0; |
---|
521 | break; |
---|
522 | case 'B': /* CUD (0x42) - Cursor Down */ |
---|
523 | y += argc ? argv[0] : 1; |
---|
524 | break; |
---|
525 | case 'C': /* CUF (0x43) - Cursor Right */ |
---|
526 | x += argc ? argv[0] : 1; |
---|
527 | break; |
---|
528 | case 'D': /* CUB (0x44) - Cursor Left */ |
---|
529 | x -= argc ? argv[0] : 1; |
---|
530 | if(x < 0) |
---|
531 | x = 0; |
---|
532 | break; |
---|
533 | case 'G': /* CHA (0x47) - Cursor Character Absolute */ |
---|
534 | x = (argc && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
535 | break; |
---|
536 | case 'J': /* ED (0x4a) - Erase In Page */ |
---|
537 | savedattr = cucul_get_attr(cv, -1, -1); |
---|
538 | cucul_set_attr(cv, im->clearattr); |
---|
539 | if(!argc || argv[0] == 0) |
---|
540 | { |
---|
541 | cucul_draw_line(cv, x, y, width, y, ' '); |
---|
542 | cucul_fill_box(cv, 0, y + 1, width - 1, height - 1, ' '); |
---|
543 | } |
---|
544 | else if(argv[0] == 1) |
---|
545 | { |
---|
546 | cucul_fill_box(cv, 0, 0, width - 1, y - 1, ' '); |
---|
547 | cucul_draw_line(cv, 0, y, x, y, ' '); |
---|
548 | } |
---|
549 | else if(argv[0] == 2) |
---|
550 | //x = y = 0; |
---|
551 | cucul_fill_box(cv, 0, 0, width - 1, height - 1, ' '); |
---|
552 | cucul_set_attr(cv, savedattr); |
---|
553 | break; |
---|
554 | case 'K': /* EL (0x4b) - Erase In Line */ |
---|
555 | if(!argc || argv[0] == 0) |
---|
556 | cucul_draw_line(cv, x, y, width, y, ' '); |
---|
557 | else if(argv[0] == 1) |
---|
558 | cucul_draw_line(cv, 0, y, x, y, ' '); |
---|
559 | else if(argv[0] == 2) |
---|
560 | if((unsigned int)x < width) |
---|
561 | cucul_draw_line(cv, x, y, width - 1, y, ' '); |
---|
562 | //x = width; |
---|
563 | break; |
---|
564 | case 'P': /* DCH (0x50) - Delete Character */ |
---|
565 | if(!argc || argv[0] == 0) |
---|
566 | argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */ |
---|
567 | for(j = 0; (unsigned int)(j + argv[0]) < width; j++) |
---|
568 | { |
---|
569 | cucul_put_char(cv, j, y, |
---|
570 | cucul_get_char(cv, j + argv[0], y)); |
---|
571 | cucul_put_attr(cv, j, y, |
---|
572 | cucul_get_attr(cv, j + argv[0], y)); |
---|
573 | } |
---|
574 | #if 0 |
---|
575 | savedattr = cucul_get_attr(cv, -1, -1); |
---|
576 | cucul_set_attr(cv, im->clearattr); |
---|
577 | for( ; (unsigned int)j < width; j++) |
---|
578 | cucul_put_char(cv, j, y, ' '); |
---|
579 | cucul_set_attr(cv, savedattr); |
---|
580 | #endif |
---|
581 | case 'X': /* ECH (0x58) - Erase Character */ |
---|
582 | if(argc && argv[0]) |
---|
583 | { |
---|
584 | savedattr = cucul_get_attr(cv, -1, -1); |
---|
585 | cucul_set_attr(cv, im->clearattr); |
---|
586 | cucul_draw_line(cv, x, y, x + argv[0] - 1, y, ' '); |
---|
587 | cucul_set_attr(cv, savedattr); |
---|
588 | } |
---|
589 | case 'd': /* VPA (0x64) - Line Position Absolute */ |
---|
590 | y = (argc && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
591 | break; |
---|
592 | case 'f': /* HVP (0x66) - Character And Line Position */ |
---|
593 | x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; |
---|
594 | y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; |
---|
595 | break; |
---|
596 | case 'h': /* SM (0x68) - FIXME */ |
---|
597 | debug("ansi import: set mode %i", argc ? (int)argv[0] : -1); |
---|
598 | break; |
---|
599 | case 'l': /* RM (0x6c) - FIXME */ |
---|
600 | debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1); |
---|
601 | break; |
---|
602 | case 'm': /* SGR (0x6d) - Select Graphic Rendition */ |
---|
603 | if(argc) |
---|
604 | ansi_parse_grcm(cv, im, argc, argv); |
---|
605 | else |
---|
606 | ansi_parse_grcm(cv, im, 1, &dummy); |
---|
607 | break; |
---|
608 | case 's': /* Private (save cursor position) */ |
---|
609 | save_x = x; |
---|
610 | save_y = y; |
---|
611 | break; |
---|
612 | case 'u': /* Private (reload cursor position) */ |
---|
613 | x = save_x; |
---|
614 | y = save_y; |
---|
615 | break; |
---|
616 | default: |
---|
617 | debug("ansi import: unknown command \"^[[%.*s\"", |
---|
618 | final - param + 1, buffer + i + param); |
---|
619 | break; |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | /* Parse OSC stuff. */ |
---|
624 | else if(buffer[i] == '\x1b' && buffer[i + 1] == ']') |
---|
625 | { |
---|
626 | char *string; |
---|
627 | unsigned int command = 0; |
---|
628 | unsigned int mode = 2, semicolon, final; |
---|
629 | |
---|
630 | for(semicolon = mode; i + semicolon < size; semicolon++) |
---|
631 | { |
---|
632 | if(buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9') |
---|
633 | break; |
---|
634 | command = 10 * command + (buffer[i + semicolon] - '0'); |
---|
635 | } |
---|
636 | |
---|
637 | if(i + semicolon >= size || buffer[i + semicolon] != ';') |
---|
638 | break; /* Invalid Mode */ |
---|
639 | |
---|
640 | for(final = semicolon + 1; i + final < size; final++) |
---|
641 | if(buffer[i + final] < 0x20) |
---|
642 | break; |
---|
643 | |
---|
644 | if(i + final >= size || buffer[i + final] != '\a') |
---|
645 | break; /* Not enough data or no bell found */ |
---|
646 | /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */ |
---|
647 | /* FIXME: differenciate between not enough data (try again) |
---|
648 | * and invalid data (print shit) */ |
---|
649 | |
---|
650 | skip += final; |
---|
651 | |
---|
652 | string = malloc(final - (semicolon + 1) + 1); |
---|
653 | memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1)); |
---|
654 | string[final - (semicolon + 1)] = '\0'; |
---|
655 | debug("ansi import: got OSC command %i string '%s'", command, |
---|
656 | string); |
---|
657 | free(string); |
---|
658 | } |
---|
659 | |
---|
660 | /* Get the character we’re going to paste */ |
---|
661 | else if(utf8) |
---|
662 | { |
---|
663 | unsigned int bytes; |
---|
664 | |
---|
665 | if(i + 6 < size) |
---|
666 | ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes); |
---|
667 | else |
---|
668 | { |
---|
669 | /* Add a trailing zero to what we're going to read */ |
---|
670 | char tmp[7]; |
---|
671 | memcpy(tmp, buffer + i, size - i); |
---|
672 | tmp[size - i] = '\0'; |
---|
673 | ch = cucul_utf8_to_utf32(tmp, &bytes); |
---|
674 | } |
---|
675 | |
---|
676 | if(!bytes) |
---|
677 | { |
---|
678 | /* If the Unicode is invalid, assume it was latin1. */ |
---|
679 | ch = buffer[i]; |
---|
680 | bytes = 1; |
---|
681 | } |
---|
682 | wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1; |
---|
683 | skip += bytes - 1; |
---|
684 | } |
---|
685 | else |
---|
686 | { |
---|
687 | ch = cucul_cp437_to_utf32(buffer[i]); |
---|
688 | wch = 1; |
---|
689 | } |
---|
690 | |
---|
691 | /* Wrap long lines or grow horizontally */ |
---|
692 | while((unsigned int)x + wch > width) |
---|
693 | { |
---|
694 | if(growx) |
---|
695 | { |
---|
696 | savedattr = cucul_get_attr(cv, -1, -1); |
---|
697 | cucul_set_attr(cv, im->clearattr); |
---|
698 | cucul_set_canvas_size(cv, width = x + wch, height); |
---|
699 | cucul_set_attr(cv, savedattr); |
---|
700 | } |
---|
701 | else |
---|
702 | { |
---|
703 | x -= width; |
---|
704 | y++; |
---|
705 | } |
---|
706 | } |
---|
707 | |
---|
708 | /* Scroll or grow vertically */ |
---|
709 | if((unsigned int)y >= height) |
---|
710 | { |
---|
711 | savedattr = cucul_get_attr(cv, -1, -1); |
---|
712 | cucul_set_attr(cv, im->clearattr); |
---|
713 | if(growy) |
---|
714 | { |
---|
715 | cucul_set_canvas_size(cv, width, height = y + 1); |
---|
716 | } |
---|
717 | else |
---|
718 | { |
---|
719 | int lines = (y - height) + 1; |
---|
720 | |
---|
721 | for(j = 0; j + lines < height; j++) |
---|
722 | { |
---|
723 | memcpy(cv->attrs + j * cv->width, |
---|
724 | cv->attrs + (j + lines) * cv->width, cv->width * 4); |
---|
725 | memcpy(cv->chars + j * cv->width, |
---|
726 | cv->chars + (j + lines) * cv->width, cv->width * 4); |
---|
727 | } |
---|
728 | cucul_fill_box(cv, 0, height - lines, |
---|
729 | cv->width - 1, height - 1, ' '); |
---|
730 | y -= lines; |
---|
731 | } |
---|
732 | cucul_set_attr(cv, savedattr); |
---|
733 | } |
---|
734 | |
---|
735 | /* Now paste our character, if any */ |
---|
736 | if(wch) |
---|
737 | { |
---|
738 | cucul_put_char(cv, x, y, ch); |
---|
739 | x += wch; |
---|
740 | } |
---|
741 | } |
---|
742 | |
---|
743 | if(growy && (unsigned int)y > height) |
---|
744 | { |
---|
745 | savedattr = cucul_get_attr(cv, -1, -1); |
---|
746 | cucul_set_attr(cv, im->clearattr); |
---|
747 | cucul_set_canvas_size(cv, width, height = y); |
---|
748 | cucul_set_attr(cv, savedattr); |
---|
749 | } |
---|
750 | |
---|
751 | cv->frames[cv->frame].x = x; |
---|
752 | cv->frames[cv->frame].y = y; |
---|
753 | |
---|
754 | // if(utf8) |
---|
755 | // cucul_set_attr(cv, savedattr); |
---|
756 | |
---|
757 | return i; |
---|
758 | } |
---|
759 | |
---|
760 | /* XXX : ANSI loader helper */ |
---|
761 | |
---|
762 | static void ansi_parse_grcm(cucul_canvas_t *cv, struct import *im, |
---|
763 | unsigned int argc, unsigned int const *argv) |
---|
764 | { |
---|
765 | static uint8_t const ansi2cucul[] = |
---|
766 | { |
---|
767 | CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN, |
---|
768 | CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY |
---|
769 | }; |
---|
770 | |
---|
771 | unsigned int j; |
---|
772 | uint8_t efg, ebg; /* Effective (libcucul) fg/bg */ |
---|
773 | |
---|
774 | for(j = 0; j < argc; j++) |
---|
775 | { |
---|
776 | /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */ |
---|
777 | if(argv[j] >= 30 && argv[j] <= 37) |
---|
778 | im->fg = ansi2cucul[argv[j] - 30]; |
---|
779 | else if(argv[j] >= 40 && argv[j] <= 47) |
---|
780 | im->bg = ansi2cucul[argv[j] - 40]; |
---|
781 | else if(argv[j] >= 90 && argv[j] <= 97) |
---|
782 | im->fg = ansi2cucul[argv[j] - 90] + 8; |
---|
783 | else if(argv[j] >= 100 && argv[j] <= 107) |
---|
784 | im->bg = ansi2cucul[argv[j] - 100] + 8; |
---|
785 | else switch(argv[j]) |
---|
786 | { |
---|
787 | case 0: /* default rendition */ |
---|
788 | im->fg = im->dfg; |
---|
789 | im->bg = im->dbg; |
---|
790 | im->bold = im->blink = im->italics = im->negative |
---|
791 | = im->concealed = im->underline = im->faint = im->strike |
---|
792 | = im->proportional = 0; |
---|
793 | break; |
---|
794 | case 1: /* bold or increased intensity */ |
---|
795 | im->bold = 1; |
---|
796 | break; |
---|
797 | case 2: /* faint, decreased intensity or second colour */ |
---|
798 | im->faint = 1; |
---|
799 | break; |
---|
800 | case 3: /* italicized */ |
---|
801 | im->italics = 1; |
---|
802 | break; |
---|
803 | case 4: /* singly underlined */ |
---|
804 | im->underline = 1; |
---|
805 | break; |
---|
806 | case 5: /* slowly blinking (less then 150 per minute) */ |
---|
807 | case 6: /* rapidly blinking (150 per minute or more) */ |
---|
808 | im->blink = 1; |
---|
809 | break; |
---|
810 | case 7: /* negative image */ |
---|
811 | im->negative = 1; |
---|
812 | break; |
---|
813 | case 8: /* concealed characters */ |
---|
814 | im->concealed = 1; |
---|
815 | break; |
---|
816 | case 9: /* crossed-out (characters still legible but marked as to be |
---|
817 | * deleted */ |
---|
818 | im->strike = 1; |
---|
819 | break; |
---|
820 | case 21: /* doubly underlined */ |
---|
821 | im->underline = 1; |
---|
822 | break; |
---|
823 | case 22: /* normal colour or normal intensity (neither bold nor |
---|
824 | * faint) */ |
---|
825 | im->bold = im->faint = 0; |
---|
826 | break; |
---|
827 | case 23: /* not italicized, not fraktur */ |
---|
828 | im->italics = 0; |
---|
829 | break; |
---|
830 | case 24: /* not underlined (neither singly nor doubly) */ |
---|
831 | im->underline = 0; |
---|
832 | break; |
---|
833 | case 25: /* steady (not blinking) */ |
---|
834 | im->blink = 0; |
---|
835 | break; |
---|
836 | case 26: /* (reserved for proportional spacing as specified in CCITT |
---|
837 | * Recommendation T.61) */ |
---|
838 | im->proportional = 1; |
---|
839 | break; |
---|
840 | case 27: /* positive image */ |
---|
841 | im->negative = 0; |
---|
842 | break; |
---|
843 | case 28: /* revealed characters */ |
---|
844 | im->concealed = 0; |
---|
845 | break; |
---|
846 | case 29: /* not crossed out */ |
---|
847 | im->strike = 0; |
---|
848 | break; |
---|
849 | case 38: /* (reserved for future standardization, intended for setting |
---|
850 | * character foreground colour as specified in ISO 8613-6 |
---|
851 | * [CCITT Recommendation T.416]) */ |
---|
852 | break; |
---|
853 | case 39: /* default display colour (implementation-defined) */ |
---|
854 | im->fg = im->dfg; |
---|
855 | break; |
---|
856 | case 48: /* (reserved for future standardization, intended for setting |
---|
857 | * character background colour as specified in ISO 8613-6 |
---|
858 | * [CCITT Recommendation T.416]) */ |
---|
859 | break; |
---|
860 | case 49: /* default background colour (implementation-defined) */ |
---|
861 | im->bg = im->dbg; |
---|
862 | break; |
---|
863 | case 50: /* (reserved for cancelling the effect of the rendering |
---|
864 | * aspect established by parameter value 26) */ |
---|
865 | im->proportional = 0; |
---|
866 | break; |
---|
867 | default: |
---|
868 | debug("ansi import: unknown sgr %i", argv[j]); |
---|
869 | break; |
---|
870 | } |
---|
871 | } |
---|
872 | |
---|
873 | if(im->concealed) |
---|
874 | { |
---|
875 | efg = ebg = CUCUL_TRANSPARENT; |
---|
876 | } |
---|
877 | else |
---|
878 | { |
---|
879 | efg = im->negative ? im->bg : im->fg; |
---|
880 | ebg = im->negative ? im->fg : im->bg; |
---|
881 | |
---|
882 | if(im->bold) |
---|
883 | { |
---|
884 | if(efg < 8) |
---|
885 | efg += 8; |
---|
886 | else if(efg == CUCUL_DEFAULT) |
---|
887 | efg = CUCUL_WHITE; |
---|
888 | } |
---|
889 | } |
---|
890 | |
---|
891 | cucul_set_color_ansi(cv, efg, ebg); |
---|
892 | } |
---|
893 | |
---|