source: libcaca/trunk/caca/codec/import.c @ 3451

Last change on this file since 3451 was 3451, checked in by Sam Hocevar, 11 years ago

Fix a few implicit cast related warnings on VC++.

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