Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/teken.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * SPDX-License-Identifier: BSD-2-Clause
2
 *
3
 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include <sys/types.h>
31
#include <limits.h>
32
#include <stdint.h>
33
#include <stdio.h>
34
#include <string.h>
35 2626842
#define teken_assert(x)         assert(x)
36
37
#include "vdef.h"
38
#include "vas.h"
39
40
/* debug messages */
41
#define teken_printf(...)
42
43
/* Private flags for t_stateflags. */
44 105652
#define TS_FIRSTDIGIT   0x0001  /* First numeric digit in escape sequence. */
45 261751
#define TS_INSERT       0x0002  /* Insert mode. */
46 261962
#define TS_AUTOWRAP     0x0004  /* Autowrap. */
47 91
#define TS_ORIGIN       0x0008  /* Origin mode. */
48 405580
#define TS_WRAPPED      0x0010  /* Next character should be printed on col 0. */
49 131705
#define TS_8BIT         0x0020  /* UTF-8 disabled. */
50 132282
#define TS_CONS25       0x0040  /* cons25 emulation. */
51 193519
#define TS_INSTRING     0x0080  /* Inside string. */
52 263
#define TS_CURSORKEYS   0x0100  /* Cursor keys mode. */
53
54
/* Character that blanks a cell. */
55 1871
#define BLANK   ' '
56
57
#include "teken.h"
58
#include "teken_wcwidth.h"
59
#include "teken_scs.h"
60
61
static teken_state_t    teken_state_init;
62
63
/*
64
 * Wrappers for hooks.
65
 */
66
67
static inline void
68
teken_funcs_bell(const teken_t *t)
69 6
{
70
71 6
        if (t->t_funcs->tf_bell != NULL)
72 0
                t->t_funcs->tf_bell(t->t_softc);
73 6
}
74
75
static inline void
76
teken_funcs_cursor(const teken_t *t)
77 144745
{
78
79 144745
        teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
80 144745
        teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
81
82 144745
        teken_assert(t->t_funcs->tf_cursor != NULL);
83 144745
        t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
84 144745
}
85
86
static inline void
87
teken_funcs_putchar(const teken_t *t, const teken_pos_t *p, teken_char_t c,
88
    const teken_attr_t *a)
89 130899
{
90
91 130899
        teken_assert(p->tp_row < t->t_winsize.tp_row);
92 130899
        teken_assert(p->tp_col < t->t_winsize.tp_col);
93
94 130899
        teken_assert(t->t_funcs->tf_putchar != NULL);
95 130899
        t->t_funcs->tf_putchar(t->t_softc, p, c, a);
96 130899
}
97
98
static inline void
99
teken_funcs_fill(const teken_t *t, const teken_rect_t *r,
100
    const teken_char_t c, const teken_attr_t *a)
101 1875
{
102
103 1875
        teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
104 1875
        teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
105 1875
        teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
106 1875
        teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
107
108 1875
        teken_assert(t->t_funcs->tf_fill != NULL);
109 1875
        t->t_funcs->tf_fill(t->t_softc, r, c, a);
110 1875
}
111
112
static inline void
113
teken_funcs_copy(const teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
114 1697
{
115
116 1697
        teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
117 1697
        teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
118 1697
        teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
119 1697
        teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
120 1697
        teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
121 1697
        teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
122
123 1697
        teken_assert(t->t_funcs->tf_copy != NULL);
124 1697
        t->t_funcs->tf_copy(t->t_softc, r, p);
125 1697
}
126
127
static inline void
128
teken_funcs_pre_input(const teken_t *t)
129 3354
{
130
131 3354
        if (t->t_funcs->tf_pre_input != NULL)
132 0
                t->t_funcs->tf_pre_input(t->t_softc);
133 3354
}
134
135
static inline void
136
teken_funcs_post_input(const teken_t *t)
137 3353
{
138
139 3353
        if (t->t_funcs->tf_post_input != NULL)
140 0
                t->t_funcs->tf_post_input(t->t_softc);
141 3353
}
142
143
static inline void
144
teken_funcs_param(const teken_t *t, int cmd, unsigned int value)
145 139
{
146
147 139
        teken_assert(t->t_funcs->tf_param != NULL);
148 139
        t->t_funcs->tf_param(t->t_softc, cmd, value);
149 139
}
150
151
static inline void
152
teken_funcs_respond(const teken_t *t, const void *buf, size_t len)
153 16
{
154
155 16
        teken_assert(t->t_funcs->tf_respond != NULL);
156 16
        t->t_funcs->tf_respond(t->t_softc, buf, len);
157 16
}
158
159
#include "teken_subr.h"
160
#include "teken_subr_compat.h"
161
162
/*
163
 * Programming interface.
164
 */
165
166
void
167
teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
168 59
{
169 59
        teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
170
171 59
        t->t_funcs = tf;
172 59
        t->t_softc = softc;
173
174 59
        t->t_nextstate = teken_state_init;
175 59
        t->t_stateflags = 0;
176 59
        t->t_utf8_left = 0;
177
178 59
        t->t_defattr.ta_format = 0;
179 59
        t->t_defattr.ta_fgcolor = TC_WHITE;
180 59
        t->t_defattr.ta_bgcolor = TC_BLACK;
181 59
        teken_subr_do_reset(t);
182
183 59
        teken_set_winsize(t, &tp);
184 59
}
185
186
static void
187
teken_input_char(teken_t *t, teken_char_t c)
188 193511
{
189
190
        /*
191
         * There is no support for DCS and OSC.  Just discard strings
192
         * until we receive characters that may indicate string
193
         * termination.
194
         */
195 193511
        if (t->t_stateflags & TS_INSTRING) {
196 44
                switch (c) {
197 4
                case '\x1B':
198 4
                        t->t_stateflags &= ~TS_INSTRING;
199 4
                        break;
200 0
                case '\a':
201 0
                        t->t_stateflags &= ~TS_INSTRING;
202 0
                        return;
203 40
                default:
204 40
                        return;
205 44
                }
206 44
        }
207
208 193471
        switch (c) {
209 0
        case '\0':
210 0
                break;
211 6
        case '\a':
212 6
                teken_subr_bell(t);
213 6
                break;
214 827
        case '\b':
215 827
                teken_subr_backspace(t);
216 827
                break;
217 1839
        case '\n':
218 1857
        case '\x0B':
219 1857
                teken_subr_newline(t);
220 1857
                break;
221 8
        case '\x0C':
222 8
                teken_subr_newpage(t);
223 8
                break;
224 6
        case '\x0E':
225 6
                if (t->t_stateflags & TS_CONS25)
226 2
                        t->t_nextstate(t, c);
227 4
                else
228 4
                        t->t_curscs = 1;
229 6
                break;
230 90
        case '\x0F':
231 90
                if (t->t_stateflags & TS_CONS25)
232 2
                        t->t_nextstate(t, c);
233 88
                else
234 88
                        t->t_curscs = 0;
235 90
                break;
236 2420
        case '\r':
237 2420
                teken_subr_carriage_return(t);
238 2420
                break;
239 154
        case '\t':
240 154
                teken_subr_horizontal_tab(t);
241 154
                break;
242 188111
        default:
243 188111
                t->t_nextstate(t, c);
244 188111
                break;
245 193471
        }
246
247
        /* Post-processing assertions. */
248 193472
        teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
249 193472
        teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
250 193472
        teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
251 193472
        teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
252 193472
        teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
253 193472
        teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
254 193472
        teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
255 193472
        teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
256
        /* Origin region has to be window size or the same as scrollreg. */
257 193472
        teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
258 193472
            t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
259 193472
            (t->t_originreg.ts_begin == 0 &&
260 193472
            t->t_originreg.ts_end == t->t_winsize.tp_row));
261 193472
}
262
263
static void
264
teken_input_byte(teken_t *t, unsigned char c)
265 193547
{
266
267
        /*
268
         * UTF-8 handling.
269
         */
270 193547
        if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
271
                /* One-byte sequence. */
272 193505
                t->t_utf8_left = 0;
273 193505
                teken_input_char(t, c);
274 193505
        } else if ((c & 0xe0) == 0xc0) {
275
                /* Two-byte sequence. */
276 2
                t->t_utf8_left = 1;
277 2
                t->t_utf8_partial = c & 0x1f;
278 40
        } else if ((c & 0xf0) == 0xe0) {
279
                /* Three-byte sequence. */
280 2
                t->t_utf8_left = 2;
281 2
                t->t_utf8_partial = c & 0x0f;
282 38
        } else if ((c & 0xf8) == 0xf0) {
283
                /* Four-byte sequence. */
284 8
                t->t_utf8_left = 3;
285 8
                t->t_utf8_partial = c & 0x07;
286 30
        } else if ((c & 0xc0) == 0x80) {
287 30
                if (t->t_utf8_left == 0)
288 0
                        return;
289 30
                t->t_utf8_left--;
290 30
                t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
291 30
                if (t->t_utf8_left == 0) {
292 12
                        teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
293 12
                        teken_input_char(t, t->t_utf8_partial);
294 12
                }
295 30
        }
296 193547
}
297
298
void
299
teken_input(teken_t *t, const void *buf, size_t len)
300 3354
{
301 3354
        const char *c = buf;
302
303 3354
        teken_funcs_pre_input(t);
304 196900
        while (len-- > 0)
305 193546
                teken_input_byte(t, *c++);
306 3354
        teken_funcs_post_input(t);
307 3354
}
308
309
const teken_pos_t *
310
teken_get_cursor(const teken_t *t)
311 179
{
312
313 179
        return (&t->t_cursor);
314 179
}
315
316
void
317
teken_set_cursor(teken_t *t, const teken_pos_t *p)
318 59
{
319
320
        /* XXX: bounds checking with originreg! */
321 59
        teken_assert(p->tp_row < t->t_winsize.tp_row);
322 59
        teken_assert(p->tp_col < t->t_winsize.tp_col);
323
324 59
        t->t_cursor = *p;
325 59
}
326
327
const teken_attr_t *
328
teken_get_curattr(const teken_t *t)
329 59
{
330
331 59
        return (&t->t_curattr);
332 59
}
333
334
void
335
teken_set_curattr(teken_t *t, const teken_attr_t *a)
336 59
{
337
338 59
        t->t_curattr = *a;
339 59
}
340
341
const teken_attr_t *
342
teken_get_defattr(const teken_t *t)
343 59
{
344
345 59
        return (&t->t_defattr);
346 59
}
347
348
void
349
teken_set_defattr(teken_t *t, const teken_attr_t *a)
350 59
{
351
352 59
        t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
353 59
}
354
355
const teken_pos_t *
356
teken_get_winsize(const teken_t *t)
357 59
{
358
359 59
        return (&t->t_winsize);
360 59
}
361
362
static void
363
teken_trim_cursor_pos(teken_t *t, const teken_pos_t *new)
364 163
{
365 163
        const teken_pos_t *cur;
366
367 163
        cur = &t->t_winsize;
368
369 163
        if (cur->tp_row < new->tp_row || cur->tp_col < new->tp_col)
370 79
                return;
371 84
        if (t->t_cursor.tp_row >= new->tp_row)
372 0
                t->t_cursor.tp_row = new->tp_row - 1;
373 84
        if (t->t_cursor.tp_col >= new->tp_col)
374 4
                t->t_cursor.tp_col = new->tp_col - 1;
375 84
}
376
377
void
378
teken_set_winsize(teken_t *t, const teken_pos_t *p)
379 163
{
380
381 163
        teken_trim_cursor_pos(t, p);
382 163
        t->t_winsize = *p;
383 163
        teken_subr_do_reset(t);
384 163
}
385
386
void
387
teken_set_winsize_noreset(teken_t *t, const teken_pos_t *p)
388 0
{
389
390 0
        teken_trim_cursor_pos(t, p);
391 0
        t->t_winsize = *p;
392 0
        teken_subr_do_resize(t);
393 0
}
394
395
void
396
teken_set_8bit(teken_t *t)
397 0
{
398
399 0
        t->t_stateflags |= TS_8BIT;
400 0
}
401
402
void
403
teken_set_cons25(teken_t *t)
404 0
{
405
406 0
        t->t_stateflags |= TS_CONS25;
407 0
}
408
409
/*
410
 * State machine.
411
 */
412
413
static void
414
teken_state_switch(teken_t *t, teken_state_t *s)
415 33227
{
416
417 33227
        t->t_nextstate = s;
418 33227
        t->t_curnum = 0;
419 33227
        t->t_stateflags |= TS_FIRSTDIGIT;
420 33227
}
421
422
static int
423
teken_state_numbers(teken_t *t, teken_char_t c)
424 34699
{
425
426 34699
        teken_assert(t->t_curnum < T_NUMSIZE);
427
428 34700
        if (c >= '0' && c <= '9') {
429 20493
                if (t->t_stateflags & TS_FIRSTDIGIT) {
430
                        /* First digit. */
431 13403
                        t->t_stateflags &= ~TS_FIRSTDIGIT;
432 13403
                        t->t_nums[t->t_curnum] = c - '0';
433 13403
                } else if (t->t_nums[t->t_curnum] < UINT_MAX / 100) {
434
                        /*
435
                         * There is no need to continue parsing input
436
                         * once the value exceeds the size of the
437
                         * terminal. It would only allow for integer
438
                         * overflows when performing arithmetic on the
439
                         * cursor position.
440
                         *
441
                         * Ignore any further digits if the value is
442
                         * already UINT_MAX / 100.
443
                         */
444 7106
                        t->t_nums[t->t_curnum] =
445 7106
                            t->t_nums[t->t_curnum] * 10 + c - '0';
446 7106
                }
447 20493
                return (1);
448 20493
        } else if (c == ';') {
449 4085
                if (t->t_stateflags & TS_FIRSTDIGIT)
450 48
                        t->t_nums[t->t_curnum] = 0;
451
452
                /* Only allow a limited set of arguments. */
453 4085
                if (++t->t_curnum == T_NUMSIZE) {
454 2
                        teken_state_switch(t, teken_state_init);
455 2
                        return (1);
456 2
                }
457
458 4083
                t->t_stateflags |= TS_FIRSTDIGIT;
459 4083
                return (1);
460 10121
        } else {
461 10121
                if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
462
                        /* Finish off the last empty argument. */
463 2
                        t->t_nums[t->t_curnum] = 0;
464 2
                        t->t_curnum++;
465 10119
                } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
466
                        /* Also count the last argument. */
467 9372
                        t->t_curnum++;
468 9372
                }
469 10121
        }
470
471 10121
        return (0);
472 34699
}
473
474
#define k       TC_BLACK
475
#define b       TC_BLUE
476
#define y       TC_YELLOW
477
#define c       TC_CYAN
478
#define g       TC_GREEN
479
#define m       TC_MAGENTA
480
#define r       TC_RED
481
#define w       TC_WHITE
482
#define K       (TC_BLACK | TC_LIGHT)
483
#define B       (TC_BLUE | TC_LIGHT)
484
#define Y       (TC_YELLOW | TC_LIGHT)
485
#define C       (TC_CYAN | TC_LIGHT)
486
#define G       (TC_GREEN | TC_LIGHT)
487
#define M       (TC_MAGENTA | TC_LIGHT)
488
#define R       (TC_RED | TC_LIGHT)
489
#define W       (TC_WHITE | TC_LIGHT)
490
491
/**
492
 * The xterm-256 color map has steps of 0x28 (in the range 0-0xff), except
493
 * for the first step which is 0x5f.  Scale to the range 0-6 by dividing
494
 * by 0x28 and rounding down.  The range of 0-5 cannot represent the
495
 * larger first step.
496
 *
497
 * This table is generated by the follow rules:
498
 * - if all components are equal, the result is black for (0, 0, 0) and
499
 *   (2, 2, 2), else white; otherwise:
500
 * - subtract the smallest component from all components
501
 * - if this gives only one nonzero component, then that is the color
502
 * - else if one component is 2 or more larger than the other nonzero one,
503
 *   then that component gives the color
504
 * - else there are 2 nonzero components.  The color is that of a small
505
 *   equal mixture of these components (cyan, yellow or magenta).  E.g.,
506
 *   (0, 5, 6) (Turquoise2) is a much purer cyan than (0, 2, 3)
507
 *   (DeepSkyBlue4), but we map both to cyan since we can't represent
508
 *   delicate shades of either blue or cyan and blue would be worse.
509
 *   Here it is important that components of 1 never occur.  Blue would
510
 *   be twice as large as green in (0, 1, 2).
511
 */
512
static const teken_color_t teken_256to8tab[] = {
513
        /* xterm normal colors: */
514
        k, r, g, y, b, m, c, w,
515
516
        /* xterm bright colors: */
517
        k, r, g, y, b, m, c, w,
518
519
        /* Red0 submap. */
520
        k, b, b, b, b, b,
521
        g, c, c, b, b, b,
522
        g, c, c, c, b, b,
523
        g, g, c, c, c, b,
524
        g, g, g, c, c, c,
525
        g, g, g, g, c, c,
526
527
        /* Red2 submap. */
528
        r, m, m, b, b, b,
529
        y, k, b, b, b, b,
530
        y, g, c, c, b, b,
531
        g, g, c, c, c, b,
532
        g, g, g, c, c, c,
533
        g, g, g, g, c, c,
534
535
        /* Red3 submap. */
536
        r, m, m, m, b, b,
537
        y, r, m, m, b, b,
538
        y, y, w, b, b, b,
539
        y, y, g, c, c, b,
540
        g, g, g, c, c, c,
541
        g, g, g, g, c, c,
542
543
        /* Red4 submap. */
544
        r, r, m, m, m, b,
545
        r, r, m, m, m, b,
546
        y, y, r, m, m, b,
547
        y, y, y, w, b, b,
548
        y, y, y, g, c, c,
549
        g, g, g, g, c, c,
550
551
        /* Red5 submap. */
552
        r, r, r, m, m, m,
553
        r, r, r, m, m, m,
554
        r, r, r, m, m, m,
555
        y, y, y, r, m, m,
556
        y, y, y, y, w, b,
557
        y, y, y, y, g, c,
558
559
        /* Red6 submap. */
560
        r, r, r, r, m, m,
561
        r, r, r, r, m, m,
562
        r, r, r, r, m, m,
563
        r, r, r, r, m, m,
564
        y, y, y, y, r, m,
565
        y, y, y, y, y, w,
566
567
        /* Grey submap. */
568
        k, k, k, k, k, k,
569
        k, k, k, k, k, k,
570
        w, w, w, w, w, w,
571
        w, w, w, w, w, w,
572
};
573
574
/*
575
 * This table is generated from the previous one by setting TC_LIGHT for
576
 * entries whose luminosity in the xterm256 color map is 60% or larger.
577
 * Thus the previous table is currently not really needed.  It will be
578
 * used for different fine tuning of the tables.
579
 */
580
static const teken_color_t teken_256to16tab[] = {
581
        /* xterm normal colors: */
582
        k, r, g, y, b, m, c, w,
583
584
        /* xterm bright colors: */
585
        K, R, G, Y, B, M, C, W,
586
587
        /* Red0 submap. */
588
        k, b, b, b, b, b,
589
        g, c, c, b, b, b,
590
        g, c, c, c, b, b,
591
        g, g, c, c, c, b,
592
        g, g, g, c, c, c,
593
        g, g, g, g, c, c,
594
595
        /* Red2 submap. */
596
        r, m, m, b, b, b,
597
        y, K, b, b, B, B,
598
        y, g, c, c, B, B,
599
        g, g, c, c, C, B,
600
        g, G, G, C, C, C,
601
        g, G, G, G, C, C,
602
603
        /* Red3 submap. */
604
        r, m, m, m, b, b,
605
        y, r, m, m, B, B,
606
        y, y, w, B, B, B,
607
        y, y, G, C, C, B,
608
        g, G, G, C, C, C,
609
        g, G, G, G, C, C,
610
611
        /* Red4 submap. */
612
        r, r, m, m, m, b,
613
        r, r, m, m, M, B,
614
        y, y, R, M, M, B,
615
        y, y, Y, W, B, B,
616
        y, Y, Y, G, C, C,
617
        g, G, G, G, C, C,
618
619
        /* Red5 submap. */
620
        r, r, r, m, m, m,
621
        r, R, R, M, M, M,
622
        r, R, R, M, M, M,
623
        y, Y, Y, R, M, M,
624
        y, Y, Y, Y, W, B,
625
        y, Y, Y, Y, G, C,
626
627
        /* Red6 submap. */
628
        r, r, r, r, m, m,
629
        r, R, R, R, M, M,
630
        r, R, R, R, M, M,
631
        r, R, R, R, M, M,
632
        y, Y, Y, Y, R, M,
633
        y, Y, Y, Y, Y, W,
634
635
        /* Grey submap. */
636
        k, k, k, k, k, k,
637
        K, K, K, K, K, K,
638
        w, w, w, w, w, w,
639
        W, W, W, W, W, W,
640
};
641
642
#undef  k
643
#undef  b
644
#undef  y
645
#undef  c
646
#undef  g
647
#undef  m
648
#undef  r
649
#undef  w
650
#undef  K
651
#undef  B
652
#undef  Y
653
#undef  C
654
#undef  G
655
#undef  M
656
#undef  R
657
#undef  W
658
659
teken_color_t
660
teken_256to8(teken_color_t c)
661 177
{
662
663 177
        return (teken_256to8tab[c % 256]);
664 177
}
665
666
teken_color_t
667
teken_256to16(teken_color_t c)
668 59
{
669
670 59
        return (teken_256to16tab[c % 256]);
671 59
}
672
673
static const char * const special_strings_cons25[] = {
674
        [TKEY_UP] = "\x1B[A",           [TKEY_DOWN] = "\x1B[B",
675
        [TKEY_LEFT] = "\x1B[D",         [TKEY_RIGHT] = "\x1B[C",
676
677
        [TKEY_HOME] = "\x1B[H",         [TKEY_END] = "\x1B[F",
678
        [TKEY_INSERT] = "\x1B[L",       [TKEY_DELETE] = "\x7F",
679
        [TKEY_PAGE_UP] = "\x1B[I",      [TKEY_PAGE_DOWN] = "\x1B[G",
680
681
        [TKEY_F1] = "\x1B[M",           [TKEY_F2] = "\x1B[N",
682
        [TKEY_F3] = "\x1B[O",           [TKEY_F4] = "\x1B[P",
683
        [TKEY_F5] = "\x1B[Q",           [TKEY_F6] = "\x1B[R",
684
        [TKEY_F7] = "\x1B[S",           [TKEY_F8] = "\x1B[T",
685
        [TKEY_F9] = "\x1B[U",           [TKEY_F10] = "\x1B[V",
686
        [TKEY_F11] = "\x1B[W",          [TKEY_F12] = "\x1B[X",
687
};
688
689
static const char * const special_strings_ckeys[] = {
690
        [TKEY_UP] = "\x1BOA",           [TKEY_DOWN] = "\x1BOB",
691
        [TKEY_LEFT] = "\x1BOD",         [TKEY_RIGHT] = "\x1BOC",
692
693
        [TKEY_HOME] = "\x1BOH",         [TKEY_END] = "\x1BOF",
694
};
695
696
static const char * const special_strings_normal[] = {
697
        [TKEY_UP] = "\x1B[A",           [TKEY_DOWN] = "\x1B[B",
698
        [TKEY_LEFT] = "\x1B[D",         [TKEY_RIGHT] = "\x1B[C",
699
700
        [TKEY_HOME] = "\x1B[H",         [TKEY_END] = "\x1B[F",
701
        [TKEY_INSERT] = "\x1B[2~",      [TKEY_DELETE] = "\x1B[3~",
702
        [TKEY_PAGE_UP] = "\x1B[5~",     [TKEY_PAGE_DOWN] = "\x1B[6~",
703
704
        [TKEY_F1] = "\x1BOP",           [TKEY_F2] = "\x1BOQ",
705
        [TKEY_F3] = "\x1BOR",           [TKEY_F4] = "\x1BOS",
706
        [TKEY_F5] = "\x1B[15~",         [TKEY_F6] = "\x1B[17~",
707
        [TKEY_F7] = "\x1B[18~",         [TKEY_F8] = "\x1B[19~",
708
        [TKEY_F9] = "\x1B[20~",         [TKEY_F10] = "\x1B[21~",
709
        [TKEY_F11] = "\x1B[23~",        [TKEY_F12] = "\x1B[24~",
710
};
711
712
const char *
713
teken_get_sequence(const teken_t *t, unsigned int k)
714 118
{
715
716
        /* Cons25 mode. */
717 118
        if (t->t_stateflags & TS_CONS25 &&
718 118
            k < sizeof special_strings_cons25 / sizeof(char *))
719 0
                return (special_strings_cons25[k]);
720
721
        /* Cursor keys mode. */
722 118
        if (t->t_stateflags & TS_CURSORKEYS &&
723 118
            k < sizeof special_strings_ckeys / sizeof(char *))
724 0
                return (special_strings_ckeys[k]);
725
726
        /* Default xterm sequences. */
727 118
        if (k < sizeof special_strings_normal / sizeof(char *))
728 118
                return (special_strings_normal[k]);
729
730 0
        return (NULL);
731 118
}
732
733
#include "teken_state.h"