You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

953 lines
30 KiB

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