source: neercs/trunk/src/term.c @ 3957

Last change on this file since 3957 was 3957, checked in by Jean-Yves Lamoureux, 14 years ago
  • Implemented \33M ANSI command (scroll up), less now works flawlessly while hitting up arrow
  • Property svn:keywords set to Id
File size: 31.2 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: term.c 3957 2009-11-19 13:31:37Z jylam $
7 *
8 *  This program 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#include "config.h"
16
17#define _XOPEN_SOURCE
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#if defined HAVE_PTY_H
22#   include <pty.h>  /* for openpty and forkpty */
23#else
24#   include <util.h> /* for OS X */
25#endif
26#include <unistd.h>
27#include <fcntl.h>
28
29#include <caca.h>
30#include <caca.h>
31
32#include "neercs.h"
33
34/* DEC ACS with common extensions */
35static uint32_t dec_acs(uint32_t uc)
36{
37    switch (uc)
38    {
39    case '+': return 0x2192; /* RIGHTWARDS ARROW */
40    case ',': return 0x2190; /* LEFTWARDS ARROW */
41    case '-': return 0x2191; /* UPWARDS ARROW */
42    case '.': return 0x2193; /* DOWNWARDS ARROW */
43    case '0': return 0x25AE; /* BLACK VERTICAL RECTANGLE */
44    case '_': return 0x25AE; /* BLACK VERTICAL RECTANGLE */
45    case '`': return 0x25C6; /* BLACK DIAMOND */
46    case 'a': return 0x2592; /* MEDIUM SHADE */
47    case 'b': return 0x2409; /* SYMBOL FOR HORIZONTAL TABULATION */
48    case 'c': return 0x240C; /* SYMBOL FOR FORM FEED */
49    case 'd': return 0x240D; /* SYMBOL FOR CARRIAGE RETURN */
50    case 'e': return 0x240A; /* SYMBOL FOR LINE FEED */
51    case 'f': return 0x00B0; /* DEGREE SIGN */
52    case 'g': return 0x00B1; /* PLUS-MINUS SIGN */
53    case 'h': return 0x2424; /* SYMBOL FOR NEWLINE */
54    case 'i': return 0x240B; /* SYMBOL FOR VERTICAL TABULATION */
55    case 'j': return 0x2518; /* BOX DRAWINGS LIGHT UP AND LEFT */
56    case 'k': return 0x2510; /* BOX DRAWINGS LIGHT DOWN AND LEFT */
57    case 'l': return 0x250C; /* BOX DRAWINGS LIGHT DOWN AND RIGHT */
58    case 'm': return 0x2514; /* BOX DRAWINGS LIGHT UP AND RIGHT */
59    case 'n': return 0x253C; /* BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL */
60    case 'o': return 0x23BA; /* HORIZONTAL SCAN LINE-1 */
61    case 'p': return 0x23BB; /* HORIZONTAL SCAN LINE-3 */
62    case 'q': return 0x2500; /* BOX DRAWINGS LIGHT HORIZONTAL */
63    case 'r': return 0x23BC; /* HORIZONTAL SCAN LINE-7 */
64    case 's': return 0x23BD; /* HORIZONTAL SCAN LINE-9 */
65    case 't': return 0x251C; /* BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
66    case 'u': return 0x2524; /* BOX DRAWINGS LIGHT VERTICAL AND LEFT */
67    case 'v': return 0x2534; /* BOX DRAWINGS LIGHT UP AND HORIZONTAL */
68    case 'w': return 0x252C; /* BOX DRAWINGS LIGHT DOWN AND HORIZONTAL */
69    case 'x': return 0x2502; /* BOX DRAWINGS LIGHT VERTICAL */
70    case 'y': return 0x2264; /* LESS-THAN OR EQUAL TO */
71    case 'z': return 0x2265; /* GREATER-THAN OR EQUAL TO */
72    case '{': return 0x03C0; /* GREEK SMALL LETTER PI */
73    case '|': return 0x2260; /* NOT EQUAL TO */
74    case '}': return 0x00A3; /* POUND SIGN */
75    case '~': return 0x00B7; /* MIDDLE DOT */
76    default:
77        return uc;
78    }
79};
80
81static void reset_conv_state(struct screen *);
82
83#define LITERAL2CHAR(i0,i1) (((i0) << 8) | (i1))
84
85#define LITERAL3CHAR(i0,i1,i2) LITERAL2CHAR(LITERAL2CHAR(i0, i1), i2)
86
87static void ansi_parse_grcm(struct screen *,
88                            unsigned int, unsigned int const *);
89
90long int import_term(struct screen_list *screen_list, struct screen *sc, void const *data, unsigned int size)
91{
92    unsigned char const *buffer = (unsigned char const*)data;
93    unsigned int i, j, k,skip, dummy = 0;
94    unsigned int width, height, top, bottom;
95    uint32_t savedattr;
96    int x = 0, y = 0, save_x = 0, save_y = 0;
97
98    width = caca_get_canvas_width(sc->cv);
99    height = caca_get_canvas_height(sc->cv);
100    x = caca_get_cursor_x(sc->cv);
101    y = caca_get_cursor_y(sc->cv);
102    top = 1;
103    bottom = height;
104   
105    if(!sc->init)
106    {
107        sc->dfg = CACA_LIGHTGRAY;
108        sc->dbg = CACA_BLACK;
109
110        caca_set_color_ansi(sc->cv, sc->dfg, sc->dbg);
111        sc->clearattr = caca_get_attr(sc->cv, -1, -1);
112
113        ansi_parse_grcm(sc, 1, &dummy);
114
115        reset_conv_state(sc);
116
117        sc->init = 1;
118    }
119
120    for(i = 0; i < size; i += skip)
121    {
122        uint32_t ch = 0;
123        int wch = 0;
124
125        skip = 1;
126
127        if(buffer[i] == '\r')
128        {
129            x = 0;
130        }
131
132        else if(buffer[i] == '\n')
133        {
134            x = 0;
135            y++;
136        }
137        else if(buffer[i] == '\a')
138        {
139            if(!sc->bell)
140                screen_list->in_bell++;
141            sc->bell = 1;
142        }
143
144        else if(buffer[i] == '\t')
145        {
146            x = (x + 7) & ~7;
147        }
148
149        else if(buffer[i] == '\x08')
150        {
151            if(x > 0)
152                x--;
153        }
154
155        else if(buffer[i] == '\x0e')
156        {
157            /* Shift Out (Ctrl-N) -> Switch to
158             * Alternate Character Set: invokes
159             * the G1 character set. */
160            sc->conv_state.glr[0] = 1;
161        }
162
163        else if(buffer[i] == '\x0f')
164        {
165            /* Shift In (Ctrl-O) -> Switch to
166             * Standard Character Set: invokes
167             * the G0 character set. */
168            sc->conv_state.glr[0] = 0;
169        }
170
171        /* If there are not enough characters to parse the escape sequence,
172         * wait until the next try. We require 3. */
173        else if(buffer[i] == '\033' && i + 2 >= size)
174            break;
175
176        /* Single Shift Select of G2 Character Set (SS2: 0x8e):
177         * affects next character only */
178        else if(buffer[i] == '\033' && buffer[i + 1] == 'N')
179        {
180            sc->conv_state.ss = 2;
181            skip += 1;
182        }
183        /* Reverse Index (RI) go up one line, reverse scroll if necessary */
184        else if(buffer[i] == '\033' && buffer[i + 1] == 'M')
185        {
186            /* FIXME : not sure about the meaning of 'go up one line' and
187             * 'if necessary' words. Implemented as a scroller only. */
188            for(j = height; j > 0; j--)
189            {
190                for(k = 0; k < width; k++)
191                {
192                        caca_put_char(sc->cv, k, j, caca_get_char(sc->cv, k, j-1));
193                        caca_put_attr(sc->cv, k, j, caca_get_attr(sc->cv, k, j-1));
194                }
195            }
196            caca_draw_line(sc->cv, 0, 0, width, 0, ' ');
197            skip += 1;
198        }
199       
200        /* Single Shift Select of G3 Character Set (SS2: 0x8f):
201         * affects next character only */
202        else if(buffer[i] == '\033' && buffer[i + 1] == 'O')
203        {
204            sc->conv_state.ss = 3;
205            skip += 1;
206        }
207
208        /* LOCKING-SHIFT TWO (LS2), ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */
209        else if(buffer[i] == '\033' && buffer[i + 1] == 'n')
210        {
211            sc->conv_state.glr[0] = 2;
212            skip += 1;
213        }
214
215        /* LOCKING-SHIFT THREE (LS3) ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */
216        else if(buffer[i] == '\033' && buffer[i + 1] == 'o')
217        {
218            sc->conv_state.glr[0] = 3;
219            skip += 1;
220        }
221
222        /* RESET TO INITIAL STATE (RIS), ECMA-48 (1986), ISO 6429 : 1988 */
223        else if(buffer[i] == '\033' && buffer[i + 1] == 'c')
224        {
225            sc->dfg = CACA_DEFAULT;
226            sc->dbg = CACA_DEFAULT;
227
228            caca_set_color_ansi(sc->cv, sc->dfg, sc->dbg);
229            sc->clearattr = caca_get_attr(sc->cv, -1, -1);
230            ansi_parse_grcm(sc, 1, &dummy);
231
232            reset_conv_state(sc);
233            skip += 1;
234        }
235
236        /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO IR 189) */
237        else if(buffer[i] == '\033' && buffer[i + 1] == 'd')
238        {
239            reset_conv_state(sc);
240            skip += 1;
241        }
242
243        /* GZDM4, G0-Designators, multi, 94^n chars [grandfathered short form from ISO 2022:1986] */
244        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && (buffer[i + 2] >= '@') && (buffer[i + 2] <= 'C'))
245        {
246            sc->conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 2]);
247            skip += 2;
248        }
249
250        /* GnDMx Gn-Designators, 9x^n chars; need one more char to distinguish these */
251        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && (i + 3 >= size))
252            break;
253
254        /* GZD4 G0-Designator, 94 chars */
255        else if(buffer[i] == '\033' && buffer[i + 1] == '(')
256        {
257            sc->conv_state.gn[0] = buffer[i + 2];
258            skip += 2;
259        }
260
261        /* G1D4 G1-Designator, 94 chars */
262        else if(buffer[i] == '\033' && buffer[i + 1] == ')')
263        {
264            sc->conv_state.gn[1] = buffer[i + 2];
265            skip += 2;
266        }
267
268        /* G2D4 G2-Designator, 94 chars */
269        else if(buffer[i] == '\033' && buffer[i + 1] == '*')
270        {
271            sc->conv_state.gn[2] = buffer[i + 2];
272            skip += 2;
273        }
274
275        /* G3D4 G3-Designator, 94 chars */
276        else if(buffer[i] == '\033' && buffer[i + 1] == '+')
277        {
278            sc->conv_state.gn[3] = buffer[i + 2];
279            skip += 2;
280        }
281
282        /* G2D6 G2-Designator, 96 chars */
283        else if(buffer[i] == '\033' && buffer[i + 1] == '.')
284        {
285            sc->conv_state.gn[2] = LITERAL2CHAR('.', buffer[i + 2]);
286            skip += 2;
287        }
288
289        /* G3D6 G3-Designator, 96 chars */
290        else if(buffer[i] == '\033' && buffer[i + 1] == '/')
291        {
292            sc->conv_state.gn[3] = LITERAL2CHAR('.', buffer[i + 2]);
293            skip += 2;
294        }
295
296        /* GZDM4 G0-Designator, 94^n chars */
297        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '(')
298        {
299            sc->conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 3]);
300            skip += 3;
301        }
302
303        /* G1DM4 G1-Designator, 94^n chars */
304        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == ')')
305        {
306            sc->conv_state.gn[1] = LITERAL2CHAR('$', buffer[i + 3]);
307            skip += 3;
308        }
309
310        /* G2DM4 G2-Designator, 94^n chars */
311        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '*')
312        {
313            sc->conv_state.gn[2] = LITERAL2CHAR('$', buffer[i + 3]);
314            skip += 3;
315        }
316
317        /* G3DM4 G3-Designator, 94^n chars */
318        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '+')
319        {
320            sc->conv_state.gn[3] = LITERAL2CHAR('$', buffer[i + 3]);
321            skip += 3;
322        }
323
324        /* G2DM6 G2-Designator, 96^n chars */
325        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '.')
326        {
327            sc->conv_state.gn[2] = LITERAL3CHAR('$', '.', buffer[i + 3]);
328            skip += 3;
329        }
330
331        /* G3DM6 G3-Designator, 96^n chars */
332        else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '/')
333        {
334            sc->conv_state.gn[3] = LITERAL3CHAR('$', '.', buffer[i + 3]);
335            skip += 3;
336        }
337
338        /* Interpret escape commands, as per Standard ECMA-48 "Control
339         * Functions for Coded Character Sets", 5.4. Control sequences. */
340        else if(buffer[i] == '\033' && buffer[i + 1] == '[')
341        {
342            unsigned int argc = 0, argv[101];
343            unsigned int param, inter, final;
344
345
346        /* Compute offsets to parameter bytes, intermediate bytes and
347         * to the final byte. Only the final byte is mandatory, there
348         * can be zero of the others.
349         * 0  param=2             inter                 final           final+1
350         * +-----+------------------+---------------------+-----------------+
351         * | CSI | parameter bytes  | intermediate bytes  |   final byte    |
352         * |     |   0x30 - 0x3f    |    0x20 - 0x2f      |   0x40 - 0x7e   |
353         * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
354         * +-----+------------------+---------------------+-----------------+
355         */
356            param = 2;
357
358            for(inter = param; i + inter < size; inter++)
359                if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
360                    break;
361
362            for(final = inter; i + final < size; final++)
363                if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
364                    break;
365
366            if(i + final >= size
367                || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
368                break; /* Invalid Final Byte */
369
370            skip += final;
371
372            /* Sanity checks */
373            if(param < inter && buffer[i + param] >= 0x3c)
374            {
375                /* Private sequence, only parse what we know */
376                debug("ansi import: private sequence \"^[[%.*s\"",
377                      final - param + 1, buffer + i + param);
378                continue; /* Private sequence, skip it entirely */
379            }
380
381            if(final - param > 100)
382                continue; /* Suspiciously long sequence, skip it */
383
384            /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
385             * format */
386            if(param < inter)
387            {
388                argv[0] = 0;
389                for(j = param; j < inter; j++)
390                {
391                    if(buffer[i + j] == ';')
392                        argv[++argc] = 0;
393                    else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
394                        argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
395                }
396                argc++;
397            }
398
399            /* Interpret final byte. The code representations are given in
400             * ECMA-48 5.4: Control sequences, and the code definitions are
401             * given in ECMA-48 8.3: Definition of control functions. */
402            debug("ansi import: command '%c'", buffer[i + final]);
403            switch(buffer[i + final])
404            {
405            case 'A': /* CUU (0x41) - Cursor Up */
406                y -= argc ? argv[0] : 1;
407                if(y < 0)
408                    y = 0;
409                break;
410            case 'B': /* CUD (0x42) - Cursor Down */
411                y += argc ? argv[0] : 1;
412                break;
413            case 'C': /* CUF (0x43) - Cursor Right */
414                x += argc ? argv[0] : 1;
415                break;
416            case 'D': /* CUB (0x44) - Cursor Left */
417                x -= argc ? argv[0] : 1;
418                if(x < 0)
419                    x = 0;
420                break;
421            case 'G': /* CHA (0x47) - Cursor Character Absolute */
422                x = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
423                break;
424            case 'H': /* CUP (0x48) - Cursor Position */
425                x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
426                y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
427                break;
428            case 'J': /* ED (0x4a) - Erase In Page */
429                savedattr = caca_get_attr(sc->cv, -1, -1);
430                caca_set_attr(sc->cv, sc->clearattr);
431                if(!argc || argv[0] == 0)
432                {
433                    caca_draw_line(sc->cv, x, y, width, y, ' ');
434                    caca_fill_box(sc->cv, 0, y + 1, width - 1, height - 1, ' ');
435                }
436                else if(argv[0] == 1)
437                {
438                    caca_fill_box(sc->cv, 0, 0, width - 1, y - 1, ' ');
439                    caca_draw_line(sc->cv, 0, y, x, y, ' ');
440                }
441                else if(argv[0] == 2)
442                {
443                    //x = y = 0;
444                    caca_fill_box(sc->cv, 0, 0, width, height - 1, ' ');
445                }
446                caca_set_attr(sc->cv, savedattr);
447                break;
448            case 'K': /* EL (0x4b) - Erase In Line */
449                if(!argc || argv[0] == 0)
450                {
451                    caca_draw_line(sc->cv, x, y, width, y, ' ');
452                }
453                else if(argv[0] == 1)
454                {
455                    caca_draw_line(sc->cv, 0, y, x, y, ' ');
456                }
457                else if(argv[0] == 2)
458                {
459                    if((unsigned int)x < width)
460                        caca_draw_line(sc->cv, x, y, width - 1, y, ' ');
461                }
462                //x = width;
463                break;
464            case 'L': /* IL - Insert line */
465                {
466                    unsigned int nb_lines = argc ? argv[0] : 1;
467                    debug("IL %d %d", argc, nb_lines);
468                    for(j = bottom - 1; j >= (unsigned int)y + nb_lines; j--)
469                    {
470                        for(k = 0; k < width; k++)
471                        {
472                            caca_put_char(sc->cv, k, j, caca_get_char(sc->cv, k, j - nb_lines));
473                            caca_put_attr(sc->cv, k, j, caca_get_attr(sc->cv, k, j - nb_lines));
474                         }
475                        caca_draw_line(sc->cv, 0, j - nb_lines, width, j - nb_lines, ' ');
476                    }
477                }
478                break;
479            case 'P': /* DCH (0x50) - Delete Character */
480                if(!argc || argv[0] == 0)
481                    argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */
482                /* Jylam : Start from x, not 0 */
483                for(j = x; (unsigned int)(j + argv[0]) < width; j++)
484                {
485                    caca_put_char(sc->cv, j, y,
486                                   caca_get_char(sc->cv, j + argv[0], y));
487                    caca_put_attr(sc->cv, j, y,
488                                   caca_get_attr(sc->cv, j + argv[0], y));
489                }
490                break; /* Jylam: this one was missing I guess */
491#if 0
492                savedattr = caca_get_attr(sc->cv, -1, -1);
493                caca_set_attr(sc->cv, sc->clearattr);
494                for( ; (unsigned int)j < width; j++)
495                    caca_put_char(sc->cv, j, y, ' ');
496                caca_set_attr(sc->cv, savedattr);
497#endif
498            case 'X': /* ECH (0x58) - Erase Character */
499                if(argc && argv[0])
500                {
501                    savedattr = caca_get_attr(sc->cv, -1, -1);
502                    caca_set_attr(sc->cv, sc->clearattr);
503                    caca_draw_line(sc->cv, x, y, x + argv[0] - 1, y, ' ');
504                    caca_set_attr(sc->cv, savedattr);
505                }
506            case 'd': /* VPA (0x64) - Line Position Absolute */
507                y = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
508                break;
509            case 'f': /* HVP (0x66) - Character And Line Position */
510                x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
511                y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
512                break;
513            case 'r': /* FIXME */
514                if(argc == 2) /* DCSTBM - Set top and bottom margin */
515                {
516                    debug("DCSTBM %d %d", argv[0], argv[1]);
517                    top = argv[0];
518                    bottom = argv[1];
519                }
520                else
521                    debug("ansi import: command r with %d params", argc);
522                break;
523            case 'h': /* SM (0x68) - FIXME */
524                debug("ansi import: set mode %i", argc ? (int)argv[0] : -1);
525                break;
526            case 'l': /* RM (0x6c) - FIXME */
527                debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1);
528                break;
529            case 'm': /* SGR (0x6d) - Select Graphic Rendition */
530                if(argc)
531                    ansi_parse_grcm(sc, argc, argv);
532                else
533                    ansi_parse_grcm(sc, 1, &dummy);
534                break;
535            case 's': /* Private (save cursor position) */
536                save_x = x;
537                save_y = y;
538                break;
539            case 'u': /* Private (reload cursor position) */
540                x = save_x;
541                y = save_y;
542                break;
543            default:
544                debug("ansi import: unknown command \"^[[%.*s\"",
545                      final - param + 1, buffer + i + param);
546                break;
547            }
548        }
549
550        /* Parse OSC stuff. */
551        else if(buffer[i] == '\033' && buffer[i + 1] == ']')
552        {
553            char *string;
554            unsigned int command = 0;
555            unsigned int mode = 2, semicolon, final;
556
557            for(semicolon = mode; i + semicolon < size; semicolon++)
558            {
559                if(buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')
560                    break;
561                command = 10 * command + (buffer[i + semicolon] - '0');
562            }
563
564            if(i + semicolon >= size || buffer[i + semicolon] != ';')
565                break; /* Invalid Mode */
566
567            for(final = semicolon + 1; i + final < size; final++)
568                if(buffer[i + final] < 0x20)
569                    break;
570
571            if(i + final >= size || buffer[i + final] != '\a')
572                break; /* Not enough data or no bell found */
573                /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */
574                /* FIXME: differenciate between not enough data (try again)
575                 *        and invalid data (print shit) */
576
577            skip += final;
578
579            string = malloc(final - (semicolon + 1) + 1);
580            memcpy(string, buffer + i + (semicolon + 1), final - (semicolon + 1));
581            string[final - (semicolon + 1)] = '\0';
582            debug("ansi import: got OSC command %i string '%s'", command,
583                  string);
584            if(command == 0 || command == 2)
585            {
586                if(sc->title)
587                    free(sc->title);
588                sc->title = string;
589            }
590            else
591                free(string);
592        }
593
594        /* Get the character we’re going to paste */
595        else
596        {
597            size_t bytes;
598
599            if(i + 6 < size) {
600                ch = caca_utf8_to_utf32((char const *)(buffer + i), &bytes);
601            } else
602            {
603                /* Add a trailing zero to what we're going to read */
604                char tmp[7];
605                memcpy(tmp, buffer + i, size - i);
606                tmp[size - i] = '\0';
607                ch = caca_utf8_to_utf32(tmp, &bytes);
608            }
609
610            if(!bytes)
611            {
612                /* If the Unicode is invalid, assume it was latin1. */
613                ch = buffer[i];
614                bytes = 1;
615            }
616
617            /* very incomplete ISO-2022 implementation tailored to DEC ACS */
618            if(sc->conv_state.cs == '@')
619            {
620                if (((ch > ' ') && (ch <= '~'))
621                    &&
622                    (sc->conv_state.gn[sc->conv_state.ss ? sc->conv_state.gn[sc->conv_state.ss] : sc->conv_state.glr[0]] == '0'))
623                {
624                    ch = dec_acs(ch);
625                }
626                else if (((ch > 0x80) && (ch < 0xff))
627                         &&
628                         (sc->conv_state.gn[sc->conv_state.glr[1]] == '0'))
629                {
630                    ch = dec_acs(ch + ' ' - 0x80);
631                }
632            }
633            sc->conv_state.ss = 0; /* no single-shift (GL) */
634
635            wch = caca_utf32_is_fullwidth(ch) ? 2 : 1;
636           
637            skip += bytes - 1;
638        }
639
640        /* Wrap long lines or grow horizontally */
641        while((unsigned int)x + wch > width)
642        {
643            x -= width;
644            y++;
645        }
646
647        /* Scroll or grow vertically */
648        if((unsigned int)y >= bottom)
649        {
650            int lines = (y - bottom) + 1;
651
652            savedattr = caca_get_attr(sc->cv, -1, -1);
653
654            for(j = top - 1; j + lines < bottom; j++)
655            {
656                for(k = 0; k < width; k++)
657                {
658                    caca_put_char(sc->cv, k, j, caca_get_char(sc->cv, k, j + lines));
659                    caca_put_attr(sc->cv, k, j, caca_get_attr(sc->cv, k, j + lines));
660                }
661            }
662            caca_set_attr(sc->cv, sc->clearattr);
663            caca_fill_box(sc->cv, 0, bottom - lines,
664                                   width - 1, bottom - 1, ' ');
665            y -= lines;
666            caca_set_attr(sc->cv, savedattr);
667        }
668
669        /* Now paste our character, if any */
670        if(wch)
671        {
672            caca_put_char(sc->cv, x, y, ch);
673            x += wch;
674        }
675    }
676
677    caca_gotoxy(sc->cv, x, y);
678
679    if(i)
680        sc->changed = 1;
681    return i;
682}
683
684/* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO IR 189) */
685
686static void reset_conv_state(struct screen *sc)
687{
688    sc->conv_state.cs = '@'; /* ISO-2022 coding system */
689    sc->conv_state.cn[0] = '@'; /* ISO 646 C0 control charset */
690    sc->conv_state.cn[1] = 'C'; /* ISO 6429-1983 C1 control charset */
691    sc->conv_state.glr[0] = 0; /* G0 in GL */
692    sc->conv_state.glr[1] = 2; /* G2 in GR */
693    sc->conv_state.gn[0] = 'B'; /* US-ASCII G0 charset */
694    sc->conv_state.gn[1] = '0'; /* DEC ACS G1 charset */
695    sc->conv_state.gn[2] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G2 charset */
696    sc->conv_state.gn[3] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G3 charset */
697    sc->conv_state.ss = 0; /* no single-shift (GL) */
698    sc->conv_state.ctrl8bit = 1;
699}
700
701/* XXX : ANSI loader helper */
702
703static void ansi_parse_grcm(struct screen *sc,
704                            unsigned int argc, unsigned int const *argv)
705{
706    static uint8_t const ansi2caca[] =
707    {
708        CACA_BLACK, CACA_RED, CACA_GREEN, CACA_BROWN,
709        CACA_BLUE, CACA_MAGENTA, CACA_CYAN, CACA_LIGHTGRAY
710    };
711
712    unsigned int j;
713    uint8_t efg, ebg; /* Effective (libcaca) fg/bg */
714
715    for(j = 0; j < argc; j++)
716    {
717        /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
718        if(argv[j] >= 30 && argv[j] <= 37)
719            sc->fg = ansi2caca[argv[j] - 30];
720        else if(argv[j] >= 40 && argv[j] <= 47)
721            sc->bg = ansi2caca[argv[j] - 40];
722        else if(argv[j] >= 90 && argv[j] <= 97)
723            sc->fg = ansi2caca[argv[j] - 90] + 8;
724        else if(argv[j] >= 100 && argv[j] <= 107)
725            sc->bg = ansi2caca[argv[j] - 100] + 8;
726        else switch(argv[j])
727        {
728        case 0: /* default rendition */
729            sc->fg = sc->dfg;
730            sc->bg = sc->dbg;
731            sc->bold = sc->blink = sc->italics = sc->negative
732             = sc->concealed = sc->underline = sc->faint = sc->strike
733             = sc->proportional = 0;
734            break;
735        case 1: /* bold or increased intensity */
736            sc->bold = 1;
737            break;
738        case 2: /* faint, decreased intensity or second colour */
739            sc->faint = 1;
740            break;
741        case 3: /* italicized */
742            sc->italics = 1;
743            break;
744        case 4: /* singly underlined */
745            sc->underline = 1;
746            break;
747        case 5: /* slowly blinking (less then 150 per minute) */
748        case 6: /* rapidly blinking (150 per minute or more) */
749            sc->blink = 1;
750            break;
751        case 7: /* negative image */
752            sc->negative = 1;
753            break;
754        case 8: /* concealed characters */
755            sc->concealed = 1;
756            break;
757        case 9: /* crossed-out (characters still legible but marked as to be
758                 * deleted */
759            sc->strike = 1;
760            break;
761        case 21: /* doubly underlined */
762            sc->underline = 1;
763            break;
764        case 22: /* normal colour or normal intensity (neither bold nor
765                  * faint) */
766            sc->bold = sc->faint = 0;
767            break;
768        case 23: /* not italicized, not fraktur */
769            sc->italics = 0;
770            break;
771        case 24: /* not underlined (neither singly nor doubly) */
772            sc->underline = 0;
773            break;
774        case 25: /* steady (not blinking) */
775            sc->blink = 0;
776            break;
777        case 26: /* (reserved for proportional spacing as specified in CCITT
778                  * Recommendation T.61) */
779            sc->proportional = 1;
780            break;
781        case 27: /* positive image */
782            sc->negative = 0;
783            break;
784        case 28: /* revealed characters */
785            sc->concealed = 0;
786            break;
787        case 29: /* not crossed out */
788            sc->strike = 0;
789            break;
790        case 38: /* (reserved for future standardization, intended for setting
791                  * character foreground colour as specified in ISO 8613-6
792                  * [CCITT Recommendation T.416]) */
793            break;
794        case 39: /* default display colour (implementation-defined) */
795            sc->fg = sc->dfg;
796            break;
797        case 48: /* (reserved for future standardization, intended for setting
798                  * character background colour as specified in ISO 8613-6
799                  * [CCITT Recommendation T.416]) */
800            break;
801        case 49: /* default background colour (implementation-defined) */
802            sc->bg = sc->dbg;
803            break;
804        case 50: /* (reserved for cancelling the effect of the rendering
805                  * aspect established by parameter value 26) */
806            sc->proportional = 0;
807            break;
808        default:
809            debug("ansi import: unknown sgr %i", argv[j]);
810            break;
811        }
812    }
813
814    if(sc->concealed)
815    {
816        efg = ebg = CACA_TRANSPARENT;
817    }
818    else
819    {
820        efg = sc->negative ? sc->bg : sc->fg;
821        ebg = sc->negative ? sc->fg : sc->bg;
822
823        if(sc->bold)
824        {
825            if(efg < 8)
826                efg += 8;
827            else if(efg == CACA_DEFAULT)
828                efg = CACA_WHITE;
829        }
830    }
831
832    caca_set_color_ansi(sc->cv, efg, ebg);
833}
834
835int create_pty(char *cmd, unsigned int w, unsigned int h, int *cpid)
836{
837    char **argv;
838    int fd;
839    pid_t pid;
840
841    pid = forkpty(&fd, NULL, NULL, NULL);
842    if(pid < 0)
843    {
844        fprintf(stderr, "forkpty() error\n");
845        return -1;
846    }
847    else if(pid == 0)
848    {
849        set_tty_size(0, w, h);
850        putenv("CACA_DRIVER=slang");
851        putenv("TERM=xterm");
852        argv = malloc(2 * sizeof(char *));
853        if(!argv)
854        {
855            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
856            return -1;
857        }
858        argv[0] = cmd;
859        argv[1] = NULL;
860        execvp(cmd, argv);
861        fprintf(stderr, "execvp() error\n");
862        return -1;
863    }
864
865    *cpid = pid;
866
867    fcntl(fd, F_SETFL, O_NDELAY);
868    return fd;
869}
870
871int create_pty_grab(long pid, unsigned int w, unsigned int h, int *newpid)
872{
873    int fdm, fds;
874
875    int ret = openpty(&fdm, &fds, NULL, NULL, NULL);
876
877    if(ret < 0)
878    {
879        fprintf(stderr, "open() error\n");
880        return -1;
881    }
882
883    set_tty_size(0, w, h);
884    grab_process(pid, ptsname(fdm), fds, newpid);
885
886    fcntl(fdm, F_SETFL, O_NDELAY);
887    return fdm;
888}
889
890int set_tty_size(int fd, unsigned int w, unsigned int h)
891{
892    struct winsize ws;
893
894    memset(&ws, 0, sizeof(ws));
895    ws.ws_row = h;
896    ws.ws_col = w;
897    ioctl(fd, TIOCSWINSZ, (char *)&ws);
898
899    return 0;
900}
901
902
903
904int update_terms(struct screen_list* screen_list)
905{
906    int i, refresh = 0;
907    for(i = 0; i < screen_list->count; i++)
908    {
909        if(screen_list->screen[i]->total && !screen_list->dont_update_coords)
910        {
911            unsigned long int bytes;
912
913            bytes = import_term(screen_list,
914                                screen_list->screen[i],
915                                screen_list->screen[i]->buf,
916                                screen_list->screen[i]->total);
917
918            if(bytes > 0)
919            {
920                screen_list->screen[i]->total -= bytes;
921                memmove(screen_list->screen[i]->buf,
922                        screen_list->screen[i]->buf + bytes,
923                        screen_list->screen[i]->total);
924                if(screen_list->screen[i]->visible || screen_list->mini)
925                    refresh = 1;
926            }
927        }
928    }
929    return refresh;
930}
931
Note: See TracBrowser for help on using the repository browser.