Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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