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.

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