source: libcaca/trunk/cucul/import.c @ 1313

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