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.
 
 
 
 
 
 

624 righe
18 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; 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. * This file contains various import functions.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined __KERNEL__
  19. # if defined HAVE_ERRNO_H
  20. # include <errno.h>
  21. # endif
  22. # include <stdio.h>
  23. # include <stdlib.h>
  24. # include <string.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. static inline uint32_t sscanu32(void const *s)
  29. {
  30. uint32_t x;
  31. memcpy(&x, s, 4);
  32. return hton32(x);
  33. }
  34. static inline uint16_t sscanu16(void const *s)
  35. {
  36. uint16_t x;
  37. memcpy(&x, s, 2);
  38. return hton16(x);
  39. }
  40. /* ANSI Graphic Rendition Combination Mode */
  41. struct ansi_grcm
  42. {
  43. uint8_t fg, bg; /* ANSI-context fg/bg */
  44. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  45. uint8_t bold, negative, concealed;
  46. };
  47. static long int import_caca(cucul_canvas_t *, void const *, unsigned int);
  48. static long int import_text(cucul_canvas_t *, void const *, unsigned int);
  49. static long int import_ansi(cucul_canvas_t *, void const *, unsigned int, int);
  50. static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
  51. unsigned int, unsigned int const *);
  52. /** \brief Import a memory buffer into a canvas
  53. *
  54. * Import a memory buffer into the given libcucul canvas's current
  55. * frame. The current frame is resized accordingly and its contents are
  56. * replaced with the imported data.
  57. *
  58. * Valid values for \c format are:
  59. * - \c "": attempt to autodetect the file format.
  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. * - \c "caca": import native libcaca files.
  64. *
  65. * If an error occurs, -1 is returned and \b errno is set accordingly:
  66. * - \c ENOMEM Not enough memory to allocate canvas.
  67. * - \c EINVAL Invalid format requested.
  68. *
  69. * \param A libcucul canvas in which to import the file.
  70. * \param buffer A \e libcucul buffer containing the data to be loaded
  71. * into a canvas.
  72. * \param format A string describing the input format.
  73. * \return The number of bytes read, or -1 if an error occurred.
  74. */
  75. long int cucul_import_memory(cucul_canvas_t *cv, void const *buf,
  76. unsigned long int len, char const *format)
  77. {
  78. if(!strcasecmp("caca", format))
  79. return import_caca(cv, buf, len);
  80. if(!strcasecmp("utf8", format))
  81. return import_ansi(cv, buf, len, 1);
  82. if(!strcasecmp("text", format))
  83. return import_text(cv, buf, len);
  84. if(!strcasecmp("ansi", format))
  85. return import_ansi(cv, buf, len, 0);
  86. /* Autodetection */
  87. if(!strcasecmp("", format))
  88. {
  89. unsigned char const *str = buf;
  90. unsigned int i;
  91. /* If 4 first bytes are 0xcaca + 'CV' */
  92. if(len >= 4 && str[0] == 0xca &&
  93. str[1] == 0xca && str[2] == 'C' && str[3] == 'V')
  94. return import_caca(cv, buf, len);
  95. /* If we find ESC[ argv, we guess it's an ANSI file */
  96. for(i = 0; i + 1 < len; i++)
  97. if((str[i] == 0x1b) && (str[i + 1] == '['))
  98. return import_ansi(cv, buf, len, 0);
  99. /* Otherwise, import it as text */
  100. return import_text(cv, buf, len);
  101. }
  102. #if defined HAVE_ERRNO_H
  103. errno = EINVAL;
  104. #endif
  105. return -1;
  106. }
  107. /** \brief Import a file into a canvas
  108. *
  109. * Import a file into the given libcucul canvas's current frame. The
  110. * current frame is resized accordingly and its contents are replaced
  111. * with the imported data.
  112. *
  113. * Valid values for \c format are:
  114. * - \c "": attempt to autodetect the file format.
  115. * - \c "text": import ASCII text files.
  116. * - \c "ansi": import ANSI files.
  117. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  118. * - \c "caca": import native libcaca files.
  119. *
  120. * If an error occurs, -1 is returned and \b errno is set accordingly:
  121. * - \c ENOSYS File access is not implemented on this system.
  122. * - \c ENOMEM Not enough memory to allocate canvas.
  123. * - \c EINVAL Invalid format requested.
  124. * cucul_import_file() may also fail and set \b errno for any of the
  125. * errors specified for the routine fopen().
  126. *
  127. * \param A libcucul canvas in which to import the file.
  128. * \param filename The name of the file to load.
  129. * \param format A string describing the input format.
  130. * \return The number of bytes read, or -1 if an error occurred.
  131. */
  132. long int cucul_import_file(cucul_canvas_t *cv, char const *filename,
  133. char const *format)
  134. {
  135. #if defined __KERNEL__
  136. # if defined HAVE_ERRNO_H
  137. errno = ENOSYS;
  138. # endif
  139. return -1;
  140. #else
  141. FILE *fp;
  142. void *data;
  143. long int size;
  144. int ret;
  145. fp = fopen(filename, "rb");
  146. if(!fp)
  147. return -1; /* fopen already set errno */
  148. fseek(fp, 0, SEEK_END);
  149. size = ftell(fp);
  150. data = malloc(size);
  151. if(!data)
  152. {
  153. fclose(fp);
  154. # if defined HAVE_ERRNO_H
  155. errno = ENOMEM;
  156. # endif
  157. return -1;
  158. }
  159. fseek(fp, 0, SEEK_SET);
  160. fread(data, size, 1, fp);
  161. fclose(fp);
  162. ret = cucul_import_memory(cv, data, size, format);
  163. free(data);
  164. return ret;
  165. #endif
  166. }
  167. /** \brief Get available import formats
  168. *
  169. * Return a list of available import formats. The list is a NULL-terminated
  170. * array of strings, interleaving a string containing the internal value for
  171. * the import format, to be used with cucul_import_canvas(), and a string
  172. * containing the natural language description for that import format.
  173. *
  174. * This function never fails.
  175. *
  176. * \return An array of strings.
  177. */
  178. char const * const * cucul_get_import_list(void)
  179. {
  180. static char const * const list[] =
  181. {
  182. "", "autodetect",
  183. "text", "plain text",
  184. "caca", "native libcaca format",
  185. "ansi", "ANSI coloured text",
  186. NULL, NULL
  187. };
  188. return list;
  189. }
  190. /*
  191. * XXX: the following functions are local.
  192. */
  193. static long int import_caca(cucul_canvas_t *cv,
  194. void const *data, unsigned int size)
  195. {
  196. uint8_t const *buf = (uint8_t const *)data;
  197. unsigned int control_size, data_size, full_size, frames, f, n;
  198. uint16_t version, flags;
  199. cucul_set_canvas_size(cv, 0, 0);
  200. if(size < 20)
  201. goto invalid_caca;
  202. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  203. goto invalid_caca;
  204. control_size = sscanu32(buf + 4);
  205. data_size = sscanu32(buf + 8);
  206. version = sscanu16(buf + 12);
  207. frames = sscanu32(buf + 14);
  208. flags = sscanu16(buf + 18);
  209. if(size != 4 + control_size + data_size)
  210. goto invalid_caca;
  211. if(control_size < 16 + frames * 24)
  212. goto invalid_caca;
  213. for(full_size = 0, f = 0; f < frames; f++)
  214. {
  215. unsigned int width, height, duration;
  216. uint32_t attr;
  217. int x, y;
  218. width = sscanu32(buf + 4 + 16 + f * 24);
  219. height = sscanu32(buf + 4 + 16 + f * 24 + 4);
  220. duration = sscanu32(buf + 4 + 16 + f * 24 + 8);
  221. attr = sscanu32(buf + 4 + 16 + f * 24 + 12);
  222. x = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 16);
  223. y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20);
  224. full_size += width * height * 8;
  225. }
  226. if(full_size != data_size)
  227. goto invalid_caca;
  228. /* FIXME: read all frames, not only the first one */
  229. cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16),
  230. sscanu32(buf + 4 + 16 + 4));
  231. /* FIXME: check for return value */
  232. for(n = sscanu32(buf + 4 + 16) * sscanu32(buf + 4 + 16 + 4); n--; )
  233. {
  234. cv->chars[n] = sscanu32(buf + 4 + control_size + 8 * n);
  235. cv->attrs[n] = sscanu32(buf + 4 + control_size + 8 * n + 4);
  236. }
  237. cv->curattr = sscanu32(buf + 4 + 16 + 12);
  238. return size;
  239. invalid_caca:
  240. #if defined HAVE_ERRNO_H
  241. errno = EINVAL;
  242. #endif
  243. return -1;
  244. }
  245. static long int import_text(cucul_canvas_t *cv,
  246. void const *data, unsigned int size)
  247. {
  248. char const *text = (char const *)data;
  249. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  250. cucul_set_canvas_size(cv, width, height);
  251. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  252. for(i = 0; i < size; i++)
  253. {
  254. unsigned char ch = *text++;
  255. if(ch == '\r')
  256. continue;
  257. if(ch == '\n')
  258. {
  259. x = 0;
  260. y++;
  261. continue;
  262. }
  263. if(x >= width || y >= height)
  264. {
  265. if(x >= width)
  266. width = x + 1;
  267. if(y >= height)
  268. height = y + 1;
  269. cucul_set_canvas_size(cv, width, height);
  270. }
  271. cucul_putchar(cv, x, y, ch);
  272. x++;
  273. }
  274. if(y > height)
  275. cucul_set_canvas_size(cv, width, height = y);
  276. return size;
  277. }
  278. static long int import_ansi(cucul_canvas_t *cv,
  279. void const *data, unsigned int size, int utf8)
  280. {
  281. struct ansi_grcm grcm;
  282. unsigned char const *buffer = (unsigned char const*)data;
  283. unsigned int i, j, skip, dummy = 0;
  284. unsigned int width = 0, height = 0, wch = 1;
  285. unsigned long int ch;
  286. int x = 0, y = 0, save_x = 0, save_y = 0;
  287. cucul_set_canvas_size(cv, width, height);
  288. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  289. for(i = 0; i < size; i += skip)
  290. {
  291. skip = 1;
  292. /* Wrap long lines */
  293. if((unsigned int)x >= 80)
  294. {
  295. x = 0;
  296. y++;
  297. }
  298. if(buffer[i] == '\x1a' && size - i >= 8
  299. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  300. break; /* End before SAUCE data */
  301. if(buffer[i] == '\r')
  302. continue; /* DOS sucks */
  303. if(buffer[i] == '\n')
  304. {
  305. x = 0;
  306. y++;
  307. continue;
  308. }
  309. /* Interpret escape commands, as per Standard ECMA-48 "Control
  310. * Functions for Coded Character Sets", 5.4. Control sequences. */
  311. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  312. {
  313. unsigned int argc = 0, argv[101];
  314. unsigned int param, inter, final;
  315. /* Compute offsets to parameter bytes, intermediate bytes and
  316. * to the final byte. Only the final byte is mandatory, there
  317. * can be zero of the others.
  318. * 0 param=2 inter final final+1
  319. * +-----+------------------+---------------------+-----------------+
  320. * | CSI | parameter bytes | intermediate bytes | final byte |
  321. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  322. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  323. * +-----+------------------+---------------------+-----------------+
  324. */
  325. param = 2;
  326. for(inter = param; i + inter < size; inter++)
  327. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  328. break;
  329. for(final = inter; i + final < size; final++)
  330. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  331. break;
  332. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  333. break; /* Invalid Final Byte */
  334. skip += final;
  335. /* Sanity checks */
  336. if(param < inter && buffer[i + param] >= 0x3c)
  337. {
  338. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  339. final - param + 1, buffer + i + param);
  340. continue; /* Private sequence, skip it entirely */
  341. }
  342. if(final - param > 100)
  343. continue; /* Suspiciously long sequence, skip it */
  344. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  345. * format */
  346. if(param < inter)
  347. {
  348. argv[0] = 0;
  349. for(j = param; j < inter; j++)
  350. {
  351. if(buffer[i + j] == ';')
  352. argv[++argc] = 0;
  353. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  354. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  355. }
  356. argc++;
  357. }
  358. /* Interpret final byte. The code representations are given in
  359. * ECMA-48 5.4: Control sequences, and the code definitions are
  360. * given in ECMA-48 8.3: Definition of control functions. */
  361. switch(buffer[i + final])
  362. {
  363. case 'f': /* CUP - Cursor Position */
  364. case 'H': /* HVP - Character And Line Position */
  365. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  366. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  367. break;
  368. case 'A': /* CUU - Cursor Up */
  369. y -= argc ? argv[0] : 1;
  370. if(y < 0)
  371. y = 0;
  372. break;
  373. case 'B': /* CUD - Cursor Down */
  374. y += argc ? argv[0] : 1;
  375. break;
  376. case 'C': /* CUF - Cursor Right */
  377. x += argc ? argv[0] : 1;
  378. break;
  379. case 'D': /* CUB - Cursor Left */
  380. x -= argc ? argv[0] : 1;
  381. if(x < 0)
  382. x = 0;
  383. break;
  384. case 's': /* Private (save cursor position) */
  385. save_x = x;
  386. save_y = y;
  387. break;
  388. case 'u': /* Private (reload cursor position) */
  389. x = save_x;
  390. y = save_y;
  391. break;
  392. case 'J': /* ED - Erase In Page */
  393. if(argv[0] == 2)
  394. x = y = 0;
  395. break;
  396. case 'K': /* EL - Erase In Line */
  397. if(width < 80)
  398. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  399. cucul_set_canvas_size(cv, width = 80, height);
  400. for(j = x; j < 80; j++)
  401. cucul_putchar(cv, j, y, ' ');
  402. x = 80;
  403. break;
  404. case 'm': /* SGR - Select Graphic Rendition */
  405. ansi_parse_grcm(cv, &grcm, argc, argv);
  406. break;
  407. default:
  408. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  409. break;
  410. }
  411. continue;
  412. }
  413. /* Get the character we’re going to paste */
  414. if(utf8)
  415. {
  416. unsigned int bytes;
  417. if(i + 6 < size)
  418. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  419. else
  420. {
  421. /* Add a trailing zero to what we're going to read */
  422. char tmp[7];
  423. memcpy(tmp, buffer + i, size - i);
  424. tmp[size - i] = '\0';
  425. ch = cucul_utf8_to_utf32(tmp, &bytes);
  426. }
  427. if(!bytes)
  428. {
  429. /* If the Unicode is invalid, assume it was latin1. */
  430. ch = buffer[i];
  431. bytes = 1;
  432. }
  433. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  434. skip += bytes - 1;
  435. }
  436. else
  437. {
  438. ch = cucul_cp437_to_utf32(buffer[i]);
  439. }
  440. /* Make sure the canvas is big enough. */
  441. if((unsigned int)x + wch > width)
  442. {
  443. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  444. cucul_set_canvas_size(cv, width = x + wch, height);
  445. }
  446. if((unsigned int)y >= height)
  447. {
  448. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  449. cucul_set_canvas_size(cv, width, height = y + 1);
  450. }
  451. /* Now paste our character */
  452. cucul_set_color_ansi(cv, grcm.efg, grcm.ebg);
  453. cucul_putchar(cv, x, y, ch);
  454. x += wch;
  455. }
  456. if((unsigned int)y > height)
  457. {
  458. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  459. cucul_set_canvas_size(cv, width, height = y);
  460. }
  461. return size;
  462. }
  463. /* XXX : ANSI loader helper */
  464. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  465. unsigned int argc, unsigned int const *argv)
  466. {
  467. static uint8_t const ansi2cucul[] =
  468. {
  469. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  470. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  471. };
  472. unsigned int j;
  473. for(j = 0; j < argc; j++)
  474. {
  475. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  476. if(argv[j] >= 30 && argv[j] <= 37)
  477. g->fg = ansi2cucul[argv[j] - 30];
  478. else if(argv[j] >= 40 && argv[j] <= 47)
  479. g->bg = ansi2cucul[argv[j] - 40];
  480. else if(argv[j] >= 90 && argv[j] <= 97)
  481. g->fg = ansi2cucul[argv[j] - 90] + 8;
  482. else if(argv[j] >= 100 && argv[j] <= 107)
  483. g->bg = ansi2cucul[argv[j] - 100] + 8;
  484. else switch(argv[j])
  485. {
  486. case 0: /* default rendition */
  487. g->fg = CUCUL_DEFAULT;
  488. g->bg = CUCUL_TRANSPARENT;
  489. g->bold = g->negative = g->concealed = 0;
  490. break;
  491. case 1: /* bold or increased intensity */
  492. g->bold = 1;
  493. break;
  494. case 4: /* singly underlined */
  495. break;
  496. case 5: /* slowly blinking (less then 150 per minute) */
  497. break;
  498. case 7: /* negative image */
  499. g->negative = 1;
  500. break;
  501. case 8: /* concealed characters */
  502. g->concealed = 1;
  503. break;
  504. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  505. g->bold = 0;
  506. break;
  507. case 28: /* revealed characters */
  508. g->concealed = 0;
  509. break;
  510. case 39: /* default display colour (implementation-defined) */
  511. g->fg = CUCUL_DEFAULT;
  512. break;
  513. case 49: /* default background colour (implementation-defined) */
  514. g->bg = CUCUL_TRANSPARENT;
  515. break;
  516. default:
  517. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  518. break;
  519. }
  520. }
  521. if(g->concealed)
  522. {
  523. g->efg = g->ebg = CUCUL_TRANSPARENT;
  524. }
  525. else
  526. {
  527. g->efg = g->negative ? g->bg : g->fg;
  528. g->ebg = g->negative ? g->fg : g->bg;
  529. if(g->bold)
  530. {
  531. if(g->efg < 8)
  532. g->efg += 8;
  533. else if(g->efg == CUCUL_DEFAULT)
  534. g->efg = CUCUL_WHITE;
  535. }
  536. }
  537. }