Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

914 righe
29 KiB

  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$
  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 <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_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(cucul_canvas_t *, void const *, size_t);
  47. static ssize_t import_text(cucul_canvas_t *, void const *, size_t);
  48. static ssize_t import_ansi(cucul_canvas_t *, void const *, size_t, int);
  49. static void ansi_parse_grcm(cucul_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 libcucul 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 libcucul 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 cucul_import_memory(cucul_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] == 0x1b) && (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 libcucul 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. * cucul_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 libcucul 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 cucul_import_file(cucul_canvas_t *cv, char const *filename,
  138. char const *format)
  139. {
  140. #if defined __KERNEL__
  141. seterrno(ENOSYS);
  142. return -1;
  143. #else
  144. FILE *fp;
  145. void *data;
  146. ssize_t size;
  147. int ret;
  148. fp = fopen(filename, "rb");
  149. if(!fp)
  150. return -1; /* fopen already set errno */
  151. fseek(fp, 0, SEEK_END);
  152. size = ftell(fp);
  153. data = malloc(size);
  154. if(!data)
  155. {
  156. fclose(fp);
  157. seterrno(ENOMEM);
  158. return -1;
  159. }
  160. fseek(fp, 0, SEEK_SET);
  161. fread(data, size, 1, fp);
  162. fclose(fp);
  163. ret = cucul_import_memory(cv, data, size, format);
  164. free(data);
  165. return ret;
  166. #endif
  167. }
  168. /** \brief Get available import formats
  169. *
  170. * Return a list of available import formats. The list is a NULL-terminated
  171. * array of strings, interleaving a string containing the internal value for
  172. * the import format, to be used with cucul_import_canvas(), and a string
  173. * containing the natural language description for that import format.
  174. *
  175. * This function never fails.
  176. *
  177. * \return An array of strings.
  178. */
  179. char const * const * cucul_get_import_list(void)
  180. {
  181. static char const * const list[] =
  182. {
  183. "", "autodetect",
  184. "caca", "native libcaca format",
  185. "text", "plain text",
  186. "ansi", "ANSI coloured text",
  187. "utf8", "UTF-8 files with ANSI colour codes",
  188. NULL, NULL
  189. };
  190. return list;
  191. }
  192. /*
  193. * XXX: the following functions are local.
  194. */
  195. static ssize_t import_caca(cucul_canvas_t *cv, void const *data, size_t size)
  196. {
  197. uint8_t const *buf = (uint8_t const *)data;
  198. size_t control_size, data_size, expected_size;
  199. unsigned int frames, f, n, offset;
  200. uint16_t version, flags;
  201. int32_t xmin = 0, ymin = 0, xmax = 0, ymax = 0;
  202. if(size < 20)
  203. return 0;
  204. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  205. {
  206. debug("caca import error: expected \\xca\\xcaCV header");
  207. goto invalid_caca;
  208. }
  209. control_size = sscanu32(buf + 4);
  210. data_size = sscanu32(buf + 8);
  211. version = sscanu16(buf + 12);
  212. frames = sscanu32(buf + 14);
  213. flags = sscanu16(buf + 18);
  214. if(size < 4 + control_size + data_size)
  215. return 0;
  216. if(control_size < 16 + frames * 32)
  217. {
  218. debug("caca import error: control size %u < expected %u",
  219. (unsigned int)control_size, 16 + frames * 32);
  220. goto invalid_caca;
  221. }
  222. for(expected_size = 0, f = 0; f < frames; f++)
  223. {
  224. unsigned int width, height, duration;
  225. uint32_t attr;
  226. int x, y, handlex, handley;
  227. width = sscanu32(buf + 4 + 16 + f * 32);
  228. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  229. duration = sscanu32(buf + 4 + 16 + f * 32 + 8);
  230. attr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  231. x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  232. y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  233. handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  234. handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  235. expected_size += width * height * 8;
  236. if(-handlex < xmin)
  237. xmin = -handlex;
  238. if(-handley < ymin)
  239. ymin = -handley;
  240. if((((int32_t) width) - handlex) > xmax)
  241. xmax = ((int32_t) width) - handlex;
  242. if((((int32_t) height) - handley) > ymax)
  243. ymax = ((int32_t) height) - handley;
  244. }
  245. if(expected_size != data_size)
  246. {
  247. debug("caca import error: data size %u < expected %u",
  248. (unsigned int)data_size, (unsigned int)expected_size);
  249. goto invalid_caca;
  250. }
  251. cucul_set_canvas_size(cv, 0, 0);
  252. cucul_set_canvas_size(cv, xmax - xmin, ymax - ymin);
  253. for (f = cucul_get_frame_count(cv); f--; )
  254. {
  255. cucul_free_frame(cv, f);
  256. }
  257. for (offset = 0, f = 0; f < frames; f ++)
  258. {
  259. unsigned int width, height;
  260. width = sscanu32(buf + 4 + 16 + f * 32);
  261. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  262. cucul_create_frame(cv, f);
  263. cucul_set_frame(cv, f);
  264. cv->curattr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  265. cv->frames[f].x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  266. cv->frames[f].y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  267. cv->frames[f].handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  268. cv->frames[f].handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  269. /* FIXME: check for return value */
  270. for(n = width * height; n--; )
  271. {
  272. int x = (n % width) - cv->frames[f].handlex - xmin;
  273. int y = (n / width) - cv->frames[f].handley - ymin;
  274. cucul_put_char(cv, x, y, sscanu32(buf + 4 + control_size
  275. + offset + 8 * n));
  276. cucul_put_attr(cv, x, y, sscanu32(buf + 4 + control_size
  277. + offset + 8 * n + 4));
  278. }
  279. offset += width * height * 8;
  280. cv->frames[f].x -= cv->frames[f].handlex;
  281. cv->frames[f].y -= cv->frames[f].handley;
  282. cv->frames[f].handlex = -xmin;
  283. cv->frames[f].handley = -ymin;
  284. }
  285. cucul_set_frame(cv, 0);
  286. return 4 + control_size + data_size;
  287. invalid_caca:
  288. seterrno(EINVAL);
  289. return -1;
  290. }
  291. static ssize_t import_text(cucul_canvas_t *cv, void const *data, size_t size)
  292. {
  293. char const *text = (char const *)data;
  294. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  295. cucul_set_canvas_size(cv, width, height);
  296. for(i = 0; i < size; i++)
  297. {
  298. unsigned char ch = *text++;
  299. if(ch == '\r')
  300. continue;
  301. if(ch == '\n')
  302. {
  303. x = 0;
  304. y++;
  305. continue;
  306. }
  307. if(x >= width || y >= height)
  308. {
  309. if(x >= width)
  310. width = x + 1;
  311. if(y >= height)
  312. height = y + 1;
  313. cucul_set_canvas_size(cv, width, height);
  314. }
  315. cucul_put_char(cv, x, y, ch);
  316. x++;
  317. }
  318. if(y > height)
  319. cucul_set_canvas_size(cv, width, height = y);
  320. return size;
  321. }
  322. static ssize_t import_ansi(cucul_canvas_t *cv, void const *data,
  323. size_t size, int utf8)
  324. {
  325. struct import im;
  326. unsigned char const *buffer = (unsigned char const*)data;
  327. unsigned int i, j, skip, growx = 0, growy = 0, dummy = 0;
  328. unsigned int width, height;
  329. uint32_t savedattr;
  330. int x = 0, y = 0, save_x = 0, save_y = 0;
  331. if(utf8)
  332. {
  333. width = cv->width;
  334. height = cv->height;
  335. growx = !width;
  336. growy = !height;
  337. x = cv->frames[cv->frame].x;
  338. y = cv->frames[cv->frame].y;
  339. }
  340. else
  341. {
  342. cucul_set_canvas_size(cv, width = 80, height = 0);
  343. growx = 0;
  344. growy = 1;
  345. }
  346. if(utf8)
  347. {
  348. im.dfg = CUCUL_DEFAULT;
  349. im.dbg = CUCUL_TRANSPARENT;
  350. }
  351. else
  352. {
  353. im.dfg = CUCUL_LIGHTGRAY;
  354. im.dbg = CUCUL_BLACK;
  355. }
  356. cucul_set_color_ansi(cv, im.dfg, im.dbg);
  357. im.clearattr = cucul_get_attr(cv, -1, -1);
  358. ansi_parse_grcm(cv, &im, 1, &dummy);
  359. for(i = 0; i < size; i += skip)
  360. {
  361. uint32_t ch = 0;
  362. int wch = 0;
  363. skip = 1;
  364. if(!utf8 && buffer[i] == '\x1a' && i + 7 < size
  365. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  366. break; /* End before SAUCE data */
  367. else if(buffer[i] == '\r')
  368. {
  369. x = 0;
  370. }
  371. else if(buffer[i] == '\n')
  372. {
  373. x = 0;
  374. y++;
  375. }
  376. else if(buffer[i] == '\t')
  377. {
  378. x = (x + 7) & ~7;
  379. }
  380. else if(buffer[i] == '\x08')
  381. {
  382. if(x > 0)
  383. x--;
  384. }
  385. /* If there are not enough characters to parse the escape sequence,
  386. * wait until the next try. We require 3. */
  387. else if(buffer[i] == '\x1b' && i + 2 >= size)
  388. break;
  389. /* XXX: What the fuck is this shit? */
  390. else if(buffer[i] == '\x1b' && buffer[i + 1] == '('
  391. && buffer[i + 2] == 'B')
  392. {
  393. skip += 2;
  394. }
  395. /* Interpret escape commands, as per Standard ECMA-48 "Control
  396. * Functions for Coded Character Sets", 5.4. Control sequences. */
  397. else if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  398. {
  399. unsigned int argc = 0, argv[101];
  400. unsigned int param, inter, final;
  401. /* Compute offsets to parameter bytes, intermediate bytes and
  402. * to the final byte. Only the final byte is mandatory, there
  403. * can be zero of the others.
  404. * 0 param=2 inter final final+1
  405. * +-----+------------------+---------------------+-----------------+
  406. * | CSI | parameter bytes | intermediate bytes | final byte |
  407. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  408. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  409. * +-----+------------------+---------------------+-----------------+
  410. */
  411. param = 2;
  412. for(inter = param; i + inter < size; inter++)
  413. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  414. break;
  415. for(final = inter; i + final < size; final++)
  416. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  417. break;
  418. if(i + final >= size
  419. || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  420. break; /* Invalid Final Byte */
  421. skip += final;
  422. /* Sanity checks */
  423. if(param < inter && buffer[i + param] >= 0x3c)
  424. {
  425. /* Private sequence, only parse what we know */
  426. debug("ansi import: private sequence \"^[[%.*s\"",
  427. final - param + 1, buffer + i + param);
  428. continue; /* Private sequence, skip it entirely */
  429. }
  430. if(final - param > 100)
  431. continue; /* Suspiciously long sequence, skip it */
  432. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  433. * format */
  434. if(param < inter)
  435. {
  436. argv[0] = 0;
  437. for(j = param; j < inter; j++)
  438. {
  439. if(buffer[i + j] == ';')
  440. argv[++argc] = 0;
  441. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  442. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  443. }
  444. argc++;
  445. }
  446. /* Interpret final byte. The code representations are given in
  447. * ECMA-48 5.4: Control sequences, and the code definitions are
  448. * given in ECMA-48 8.3: Definition of control functions. */
  449. switch(buffer[i + final])
  450. {
  451. case 'H': /* CUP (0x48) - Cursor Position */
  452. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  453. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  454. break;
  455. case 'A': /* CUU (0x41) - Cursor Up */
  456. y -= argc ? argv[0] : 1;
  457. if(y < 0)
  458. y = 0;
  459. break;
  460. case 'B': /* CUD (0x42) - Cursor Down */
  461. y += argc ? argv[0] : 1;
  462. break;
  463. case 'C': /* CUF (0x43) - Cursor Right */
  464. x += argc ? argv[0] : 1;
  465. break;
  466. case 'D': /* CUB (0x44) - Cursor Left */
  467. x -= argc ? argv[0] : 1;
  468. if(x < 0)
  469. x = 0;
  470. break;
  471. case 'G': /* CHA (0x47) - Cursor Character Absolute */
  472. x = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  473. break;
  474. case 'J': /* ED (0x4a) - Erase In Page */
  475. savedattr = cucul_get_attr(cv, -1, -1);
  476. cucul_set_attr(cv, im.clearattr);
  477. if(!argc || argv[0] == 0)
  478. {
  479. cucul_draw_line(cv, x, y, width, y, ' ');
  480. cucul_fill_box(cv, 0, y + 1, width - 1, height - 1, ' ');
  481. }
  482. else if(argv[0] == 1)
  483. {
  484. cucul_fill_box(cv, 0, 0, width - 1, y - 1, ' ');
  485. cucul_draw_line(cv, 0, y, x, y, ' ');
  486. }
  487. else if(argv[0] == 2)
  488. //x = y = 0;
  489. cucul_fill_box(cv, 0, 0, width - 1, height - 1, ' ');
  490. cucul_set_attr(cv, savedattr);
  491. break;
  492. case 'K': /* EL (0x4b) - Erase In Line */
  493. if(!argc || argv[0] == 0)
  494. cucul_draw_line(cv, x, y, width, y, ' ');
  495. else if(argv[0] == 1)
  496. cucul_draw_line(cv, 0, y, x, y, ' ');
  497. else if(argv[0] == 2)
  498. if((unsigned int)x < width)
  499. cucul_draw_line(cv, x, y, width - 1, y, ' ');
  500. //x = width;
  501. break;
  502. case 'P': /* DCH (0x50) - Delete Character */
  503. if(!argc || argv[0] == 0)
  504. argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */
  505. for(j = 0; (unsigned int)(j + argv[0]) < width; j++)
  506. {
  507. cucul_put_char(cv, j, y,
  508. cucul_get_char(cv, j + argv[0], y));
  509. cucul_put_attr(cv, j, y,
  510. cucul_get_attr(cv, j + argv[0], y));
  511. }
  512. #if 0
  513. savedattr = cucul_get_attr(cv, -1, -1);
  514. cucul_set_attr(cv, im.clearattr);
  515. for( ; (unsigned int)j < width; j++)
  516. cucul_put_char(cv, j, y, ' ');
  517. cucul_set_attr(cv, savedattr);
  518. #endif
  519. case 'X': /* ECH (0x58) - Erase Character */
  520. if(argc && argv[0])
  521. {
  522. savedattr = cucul_get_attr(cv, -1, -1);
  523. cucul_set_attr(cv, im.clearattr);
  524. cucul_draw_line(cv, x, y, x + argv[0] - 1, y, ' ');
  525. cucul_set_attr(cv, savedattr);
  526. }
  527. case 'd': /* VPA (0x64) - Line Position Absolute */
  528. y = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  529. break;
  530. case 'f': /* HVP (0x66) - Character And Line Position */
  531. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  532. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  533. break;
  534. case 'h': /* SM (0x68) - FIXME */
  535. debug("ansi import: set mode %i", argc ? (int)argv[0] : -1);
  536. break;
  537. case 'l': /* RM (0x6c) - FIXME */
  538. debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1);
  539. break;
  540. case 'm': /* SGR (0x6d) - Select Graphic Rendition */
  541. if(argc)
  542. ansi_parse_grcm(cv, &im, argc, argv);
  543. else
  544. ansi_parse_grcm(cv, &im, 1, &dummy);
  545. break;
  546. case 's': /* Private (save cursor position) */
  547. save_x = x;
  548. save_y = y;
  549. break;
  550. case 'u': /* Private (reload cursor position) */
  551. x = save_x;
  552. y = save_y;
  553. break;
  554. default:
  555. debug("ansi import: unknown command \"^[[%.*s\"",
  556. final - param + 1, buffer + i + param);
  557. break;
  558. }
  559. }
  560. /* Parse OSC stuff. */
  561. else if(buffer[i] == '\x1b' && buffer[i + 1] == ']')
  562. {
  563. char *string;
  564. unsigned int command = 0;
  565. unsigned int mode = 2, semicolon, final;
  566. for(semicolon = mode; i + semicolon < size; semicolon++)
  567. {
  568. if(buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')
  569. break;
  570. command = 10 * command + (buffer[i + semicolon] - '0');
  571. }
  572. if(i + semicolon >= size || buffer[i + semicolon] != ';')
  573. break; /* Invalid Mode */
  574. for(final = semicolon + 1; i + final < size; final++)
  575. if(buffer[i + final] < 0x20)
  576. break;
  577. if(i + final >= size || buffer[i + final] != '\a')
  578. break; /* Not enough data or no bell found */
  579. /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */
  580. /* FIXME: differenciate between not enough data (try again)
  581. * and invalid data (print shit) */
  582. skip += final;
  583. string = malloc(final - (semicolon + 1) + 1);
  584. memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1));
  585. string[final - (semicolon + 1)] = '\0';
  586. debug("ansi import: got OSC command %i string '%s'", command,
  587. string);
  588. free(string);
  589. }
  590. /* Get the character we’re going to paste */
  591. else if(utf8)
  592. {
  593. size_t bytes;
  594. if(i + 6 < size)
  595. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  596. else
  597. {
  598. /* Add a trailing zero to what we're going to read */
  599. char tmp[7];
  600. memcpy(tmp, buffer + i, size - i);
  601. tmp[size - i] = '\0';
  602. ch = cucul_utf8_to_utf32(tmp, &bytes);
  603. }
  604. if(!bytes)
  605. {
  606. /* If the Unicode is invalid, assume it was latin1. */
  607. ch = buffer[i];
  608. bytes = 1;
  609. }
  610. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  611. skip += bytes - 1;
  612. }
  613. else
  614. {
  615. ch = cucul_cp437_to_utf32(buffer[i]);
  616. wch = 1;
  617. }
  618. /* Wrap long lines or grow horizontally */
  619. while((unsigned int)x + wch > width)
  620. {
  621. if(growx)
  622. {
  623. savedattr = cucul_get_attr(cv, -1, -1);
  624. cucul_set_attr(cv, im.clearattr);
  625. cucul_set_canvas_size(cv, width = x + wch, height);
  626. cucul_set_attr(cv, savedattr);
  627. }
  628. else
  629. {
  630. x -= width;
  631. y++;
  632. }
  633. }
  634. /* Scroll or grow vertically */
  635. if((unsigned int)y >= height)
  636. {
  637. savedattr = cucul_get_attr(cv, -1, -1);
  638. cucul_set_attr(cv, im.clearattr);
  639. if(growy)
  640. {
  641. cucul_set_canvas_size(cv, width, height = y + 1);
  642. }
  643. else
  644. {
  645. int lines = (y - height) + 1;
  646. for(j = 0; j + lines < height; j++)
  647. {
  648. memcpy(cv->attrs + j * cv->width,
  649. cv->attrs + (j + lines) * cv->width, cv->width * 4);
  650. memcpy(cv->chars + j * cv->width,
  651. cv->chars + (j + lines) * cv->width, cv->width * 4);
  652. }
  653. cucul_fill_box(cv, 0, height - lines,
  654. cv->width - 1, height - 1, ' ');
  655. y -= lines;
  656. }
  657. cucul_set_attr(cv, savedattr);
  658. }
  659. /* Now paste our character, if any */
  660. if(wch)
  661. {
  662. cucul_put_char(cv, x, y, ch);
  663. x += wch;
  664. }
  665. }
  666. if(growy && (unsigned int)y > height)
  667. {
  668. savedattr = cucul_get_attr(cv, -1, -1);
  669. cucul_set_attr(cv, im.clearattr);
  670. cucul_set_canvas_size(cv, width, height = y);
  671. cucul_set_attr(cv, savedattr);
  672. }
  673. cv->frames[cv->frame].x = x;
  674. cv->frames[cv->frame].y = y;
  675. // if(utf8)
  676. // cucul_set_attr(cv, savedattr);
  677. return i;
  678. }
  679. /* XXX : ANSI loader helper */
  680. static void ansi_parse_grcm(cucul_canvas_t *cv, struct import *im,
  681. unsigned int argc, unsigned int const *argv)
  682. {
  683. static uint8_t const ansi2cucul[] =
  684. {
  685. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  686. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  687. };
  688. unsigned int j;
  689. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  690. for(j = 0; j < argc; j++)
  691. {
  692. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  693. if(argv[j] >= 30 && argv[j] <= 37)
  694. im->fg = ansi2cucul[argv[j] - 30];
  695. else if(argv[j] >= 40 && argv[j] <= 47)
  696. im->bg = ansi2cucul[argv[j] - 40];
  697. else if(argv[j] >= 90 && argv[j] <= 97)
  698. im->fg = ansi2cucul[argv[j] - 90] + 8;
  699. else if(argv[j] >= 100 && argv[j] <= 107)
  700. im->bg = ansi2cucul[argv[j] - 100] + 8;
  701. else switch(argv[j])
  702. {
  703. case 0: /* default rendition */
  704. im->fg = im->dfg;
  705. im->bg = im->dbg;
  706. im->bold = im->blink = im->italics = im->negative
  707. = im->concealed = im->underline = im->faint = im->strike
  708. = im->proportional = 0;
  709. break;
  710. case 1: /* bold or increased intensity */
  711. im->bold = 1;
  712. break;
  713. case 2: /* faint, decreased intensity or second colour */
  714. im->faint = 1;
  715. break;
  716. case 3: /* italicized */
  717. im->italics = 1;
  718. break;
  719. case 4: /* singly underlined */
  720. im->underline = 1;
  721. break;
  722. case 5: /* slowly blinking (less then 150 per minute) */
  723. case 6: /* rapidly blinking (150 per minute or more) */
  724. im->blink = 1;
  725. break;
  726. case 7: /* negative image */
  727. im->negative = 1;
  728. break;
  729. case 8: /* concealed characters */
  730. im->concealed = 1;
  731. break;
  732. case 9: /* crossed-out (characters still legible but marked as to be
  733. * deleted */
  734. im->strike = 1;
  735. break;
  736. case 21: /* doubly underlined */
  737. im->underline = 1;
  738. break;
  739. case 22: /* normal colour or normal intensity (neither bold nor
  740. * faint) */
  741. im->bold = im->faint = 0;
  742. break;
  743. case 23: /* not italicized, not fraktur */
  744. im->italics = 0;
  745. break;
  746. case 24: /* not underlined (neither singly nor doubly) */
  747. im->underline = 0;
  748. break;
  749. case 25: /* steady (not blinking) */
  750. im->blink = 0;
  751. break;
  752. case 26: /* (reserved for proportional spacing as specified in CCITT
  753. * Recommendation T.61) */
  754. im->proportional = 1;
  755. break;
  756. case 27: /* positive image */
  757. im->negative = 0;
  758. break;
  759. case 28: /* revealed characters */
  760. im->concealed = 0;
  761. break;
  762. case 29: /* not crossed out */
  763. im->strike = 0;
  764. break;
  765. case 38: /* (reserved for future standardization, intended for setting
  766. * character foreground colour as specified in ISO 8613-6
  767. * [CCITT Recommendation T.416]) */
  768. break;
  769. case 39: /* default display colour (implementation-defined) */
  770. im->fg = im->dfg;
  771. break;
  772. case 48: /* (reserved for future standardization, intended for setting
  773. * character background colour as specified in ISO 8613-6
  774. * [CCITT Recommendation T.416]) */
  775. break;
  776. case 49: /* default background colour (implementation-defined) */
  777. im->bg = im->dbg;
  778. break;
  779. case 50: /* (reserved for cancelling the effect of the rendering
  780. * aspect established by parameter value 26) */
  781. im->proportional = 0;
  782. break;
  783. default:
  784. debug("ansi import: unknown sgr %i", argv[j]);
  785. break;
  786. }
  787. }
  788. if(im->concealed)
  789. {
  790. efg = ebg = CUCUL_TRANSPARENT;
  791. }
  792. else
  793. {
  794. efg = im->negative ? im->bg : im->fg;
  795. ebg = im->negative ? im->fg : im->bg;
  796. if(im->bold)
  797. {
  798. if(efg < 8)
  799. efg += 8;
  800. else if(efg == CUCUL_DEFAULT)
  801. efg = CUCUL_WHITE;
  802. }
  803. }
  804. cucul_set_color_ansi(cv, efg, ebg);
  805. }