Deleted Added
full compact
teken.c (206141) teken.c (221698)
1/*-
2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/teken/teken.c 206141 2010-04-03 17:22:28Z ed $
26 * $FreeBSD: head/sys/teken/teken.c 221698 2011-05-09 16:27:39Z ed $
27 */
28
29#include <sys/cdefs.h>
30#if defined(__FreeBSD__) && defined(_KERNEL)
31#include <sys/param.h>
32#include <sys/lock.h>
33#include <sys/systm.h>
34#define teken_assert(x) MPASS(x)
27 */
28
29#include <sys/cdefs.h>
30#if defined(__FreeBSD__) && defined(_KERNEL)
31#include <sys/param.h>
32#include <sys/lock.h>
33#include <sys/systm.h>
34#define teken_assert(x) MPASS(x)
35#define teken_printf(x,...)
36#else /* !(__FreeBSD__ && _KERNEL) */
37#include <sys/types.h>
38#include <assert.h>
39#include <stdint.h>
40#include <stdio.h>
41#include <string.h>
42#define teken_assert(x) assert(x)
35#else /* !(__FreeBSD__ && _KERNEL) */
36#include <sys/types.h>
37#include <assert.h>
38#include <stdint.h>
39#include <stdio.h>
40#include <string.h>
41#define teken_assert(x) assert(x)
43#define teken_printf(x,...) do { \
44 if (df != NULL) \
45 fprintf(df, x, ## __VA_ARGS__); \
46} while (0)
47/* debug messages */
48static FILE *df;
49#endif /* __FreeBSD__ && _KERNEL */
50
42#endif /* __FreeBSD__ && _KERNEL */
43
44/* debug messages */
45#define teken_printf(x,...)
46
51/* Private flags for t_stateflags. */
52#define TS_FIRSTDIGIT 0x0001 /* First numeric digit in escape sequence. */
53#define TS_INSERT 0x0002 /* Insert mode. */
54#define TS_AUTOWRAP 0x0004 /* Autowrap. */
55#define TS_ORIGIN 0x0008 /* Origin mode. */
56#define TS_WRAPPED 0x0010 /* Next character should be printed on col 0. */
57#define TS_8BIT 0x0020 /* UTF-8 disabled. */
58#define TS_CONS25 0x0040 /* cons25 emulation. */
59#define TS_INSTRING 0x0080 /* Inside string. */
60#define TS_CURSORKEYS 0x0100 /* Cursor keys mode. */
61
62/* Character that blanks a cell. */
63#define BLANK ' '
64
65#include "teken.h"
66#include "teken_wcwidth.h"
67#include "teken_scs.h"
68
69static teken_state_t teken_state_init;
70
71/*
72 * Wrappers for hooks.
73 */
74
75static inline void
76teken_funcs_bell(teken_t *t)
77{
78
79 t->t_funcs->tf_bell(t->t_softc);
80}
81
82static inline void
83teken_funcs_cursor(teken_t *t)
84{
85
86 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
87 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
88
89 t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
90}
91
92static inline void
93teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
94 const teken_attr_t *a)
95{
96
97 teken_assert(p->tp_row < t->t_winsize.tp_row);
98 teken_assert(p->tp_col < t->t_winsize.tp_col);
99
100 t->t_funcs->tf_putchar(t->t_softc, p, c, a);
101}
102
103static inline void
104teken_funcs_fill(teken_t *t, const teken_rect_t *r,
105 const teken_char_t c, const teken_attr_t *a)
106{
107
108 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
109 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
110 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
111 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
112
113 t->t_funcs->tf_fill(t->t_softc, r, c, a);
114}
115
116static inline void
117teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
118{
119
120 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
121 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
122 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
123 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
124 teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
125 teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
126
127 t->t_funcs->tf_copy(t->t_softc, r, p);
128}
129
130static inline void
131teken_funcs_param(teken_t *t, int cmd, unsigned int value)
132{
133
134 t->t_funcs->tf_param(t->t_softc, cmd, value);
135}
136
137static inline void
138teken_funcs_respond(teken_t *t, const void *buf, size_t len)
139{
140
141 t->t_funcs->tf_respond(t->t_softc, buf, len);
142}
143
144#include "teken_subr.h"
145#include "teken_subr_compat.h"
146
147/*
148 * Programming interface.
149 */
150
151void
152teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
153{
154 teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
155
47/* Private flags for t_stateflags. */
48#define TS_FIRSTDIGIT 0x0001 /* First numeric digit in escape sequence. */
49#define TS_INSERT 0x0002 /* Insert mode. */
50#define TS_AUTOWRAP 0x0004 /* Autowrap. */
51#define TS_ORIGIN 0x0008 /* Origin mode. */
52#define TS_WRAPPED 0x0010 /* Next character should be printed on col 0. */
53#define TS_8BIT 0x0020 /* UTF-8 disabled. */
54#define TS_CONS25 0x0040 /* cons25 emulation. */
55#define TS_INSTRING 0x0080 /* Inside string. */
56#define TS_CURSORKEYS 0x0100 /* Cursor keys mode. */
57
58/* Character that blanks a cell. */
59#define BLANK ' '
60
61#include "teken.h"
62#include "teken_wcwidth.h"
63#include "teken_scs.h"
64
65static teken_state_t teken_state_init;
66
67/*
68 * Wrappers for hooks.
69 */
70
71static inline void
72teken_funcs_bell(teken_t *t)
73{
74
75 t->t_funcs->tf_bell(t->t_softc);
76}
77
78static inline void
79teken_funcs_cursor(teken_t *t)
80{
81
82 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
83 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
84
85 t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
86}
87
88static inline void
89teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
90 const teken_attr_t *a)
91{
92
93 teken_assert(p->tp_row < t->t_winsize.tp_row);
94 teken_assert(p->tp_col < t->t_winsize.tp_col);
95
96 t->t_funcs->tf_putchar(t->t_softc, p, c, a);
97}
98
99static inline void
100teken_funcs_fill(teken_t *t, const teken_rect_t *r,
101 const teken_char_t c, const teken_attr_t *a)
102{
103
104 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
105 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
106 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
107 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
108
109 t->t_funcs->tf_fill(t->t_softc, r, c, a);
110}
111
112static inline void
113teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
114{
115
116 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
117 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
118 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
119 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
120 teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
121 teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
122
123 t->t_funcs->tf_copy(t->t_softc, r, p);
124}
125
126static inline void
127teken_funcs_param(teken_t *t, int cmd, unsigned int value)
128{
129
130 t->t_funcs->tf_param(t->t_softc, cmd, value);
131}
132
133static inline void
134teken_funcs_respond(teken_t *t, const void *buf, size_t len)
135{
136
137 t->t_funcs->tf_respond(t->t_softc, buf, len);
138}
139
140#include "teken_subr.h"
141#include "teken_subr_compat.h"
142
143/*
144 * Programming interface.
145 */
146
147void
148teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
149{
150 teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
151
156#if !(defined(__FreeBSD__) && defined(_KERNEL))
157 df = fopen("teken.log", "w");
158 if (df != NULL)
159 setvbuf(df, NULL, _IOLBF, BUFSIZ);
160#endif /* !(__FreeBSD__ && _KERNEL) */
161
162 t->t_funcs = tf;
163 t->t_softc = softc;
164
165 t->t_nextstate = teken_state_init;
166 t->t_stateflags = 0;
167 t->t_utf8_left = 0;
168
169 t->t_defattr.ta_format = 0;
170 t->t_defattr.ta_fgcolor = TC_WHITE;
171 t->t_defattr.ta_bgcolor = TC_BLACK;
172 teken_subr_do_reset(t);
173
174 teken_set_winsize(t, &tp);
175}
176
177static void
178teken_input_char(teken_t *t, teken_char_t c)
179{
180
181 /*
182 * There is no support for DCS and OSC. Just discard strings
183 * until we receive characters that may indicate string
184 * termination.
185 */
186 if (t->t_stateflags & TS_INSTRING) {
187 switch (c) {
188 case '\x1B':
189 t->t_stateflags &= ~TS_INSTRING;
190 break;
191 case '\a':
192 t->t_stateflags &= ~TS_INSTRING;
193 return;
194 default:
195 return;
196 }
197 }
198
199 switch (c) {
200 case '\0':
201 break;
202 case '\a':
203 teken_subr_bell(t);
204 break;
205 case '\b':
206 teken_subr_backspace(t);
207 break;
208 case '\n':
209 case '\x0B':
210 teken_subr_newline(t);
211 break;
212 case '\x0C':
213 teken_subr_newpage(t);
214 break;
215 case '\x0E':
216 if (t->t_stateflags & TS_CONS25)
217 t->t_nextstate(t, c);
218 else
219 t->t_curscs = 1;
220 break;
221 case '\x0F':
222 if (t->t_stateflags & TS_CONS25)
223 t->t_nextstate(t, c);
224 else
225 t->t_curscs = 0;
226 break;
227 case '\r':
228 teken_subr_carriage_return(t);
229 break;
230 case '\t':
231 teken_subr_horizontal_tab(t);
232 break;
233 default:
234 t->t_nextstate(t, c);
235 break;
236 }
237
238 /* Post-processing assertions. */
239 teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
240 teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
241 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
242 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
243 teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
244 teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
245 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
246 teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
247 /* Origin region has to be window size or the same as scrollreg. */
248 teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
249 t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
250 (t->t_originreg.ts_begin == 0 &&
251 t->t_originreg.ts_end == t->t_winsize.tp_row));
252}
253
254static void
255teken_input_byte(teken_t *t, unsigned char c)
256{
257
258 /*
259 * UTF-8 handling.
260 */
261 if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
262 /* One-byte sequence. */
263 t->t_utf8_left = 0;
264 teken_input_char(t, c);
265 } else if ((c & 0xe0) == 0xc0) {
266 /* Two-byte sequence. */
267 t->t_utf8_left = 1;
268 t->t_utf8_partial = c & 0x1f;
269 } else if ((c & 0xf0) == 0xe0) {
270 /* Three-byte sequence. */
271 t->t_utf8_left = 2;
272 t->t_utf8_partial = c & 0x0f;
273 } else if ((c & 0xf8) == 0xf0) {
274 /* Four-byte sequence. */
275 t->t_utf8_left = 3;
276 t->t_utf8_partial = c & 0x07;
277 } else if ((c & 0xc0) == 0x80) {
278 if (t->t_utf8_left == 0)
279 return;
280 t->t_utf8_left--;
281 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
282 if (t->t_utf8_left == 0) {
283 teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
284 teken_input_char(t, t->t_utf8_partial);
285 }
286 }
287}
288
289void
290teken_input(teken_t *t, const void *buf, size_t len)
291{
292 const char *c = buf;
293
294 while (len-- > 0)
295 teken_input_byte(t, *c++);
296}
297
298const teken_pos_t *
299teken_get_cursor(teken_t *t)
300{
301
302 return (&t->t_cursor);
303}
304
305void
306teken_set_cursor(teken_t *t, const teken_pos_t *p)
307{
308
309 /* XXX: bounds checking with originreg! */
310 teken_assert(p->tp_row < t->t_winsize.tp_row);
311 teken_assert(p->tp_col < t->t_winsize.tp_col);
312
313 t->t_cursor = *p;
314}
315
316const teken_attr_t *
317teken_get_curattr(teken_t *t)
318{
319
320 return (&t->t_curattr);
321}
322
323void
324teken_set_curattr(teken_t *t, const teken_attr_t *a)
325{
326
327 t->t_curattr = *a;
328}
329
330const teken_attr_t *
331teken_get_defattr(teken_t *t)
332{
333
334 return (&t->t_defattr);
335}
336
337void
338teken_set_defattr(teken_t *t, const teken_attr_t *a)
339{
340
341 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
342}
343
344const teken_pos_t *
345teken_get_winsize(teken_t *t)
346{
347
348 return (&t->t_winsize);
349}
350
351void
352teken_set_winsize(teken_t *t, const teken_pos_t *p)
353{
354
355 t->t_winsize = *p;
356 teken_subr_do_reset(t);
357}
358
359void
360teken_set_8bit(teken_t *t)
361{
362
363 t->t_stateflags |= TS_8BIT;
364}
365
366void
367teken_set_cons25(teken_t *t)
368{
369
370 t->t_stateflags |= TS_CONS25;
371}
372
373/*
374 * State machine.
375 */
376
377static void
378teken_state_switch(teken_t *t, teken_state_t *s)
379{
380
381 t->t_nextstate = s;
382 t->t_curnum = 0;
383 t->t_stateflags |= TS_FIRSTDIGIT;
384}
385
386static int
387teken_state_numbers(teken_t *t, teken_char_t c)
388{
389
390 teken_assert(t->t_curnum < T_NUMSIZE);
391
392 if (c >= '0' && c <= '9') {
393 /*
394 * Don't do math with the default value of 1 when a
395 * custom number is inserted.
396 */
397 if (t->t_stateflags & TS_FIRSTDIGIT) {
398 t->t_stateflags &= ~TS_FIRSTDIGIT;
399 t->t_nums[t->t_curnum] = 0;
400 } else {
401 t->t_nums[t->t_curnum] *= 10;
402 }
403
404 t->t_nums[t->t_curnum] += c - '0';
405 return (1);
406 } else if (c == ';') {
407 if (t->t_stateflags & TS_FIRSTDIGIT)
408 t->t_nums[t->t_curnum] = 0;
409
410 /* Only allow a limited set of arguments. */
411 if (++t->t_curnum == T_NUMSIZE) {
412 teken_state_switch(t, teken_state_init);
413 return (1);
414 }
415
416 t->t_stateflags |= TS_FIRSTDIGIT;
417 return (1);
418 } else {
419 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
420 /* Finish off the last empty argument. */
421 t->t_nums[t->t_curnum] = 0;
422 t->t_curnum++;
423 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
424 /* Also count the last argument. */
425 t->t_curnum++;
426 }
427 }
428
429 return (0);
430}
431
432teken_color_t
433teken_256to8(teken_color_t c)
434{
435 unsigned int r, g, b;
436
437 if (c < 16) {
438 /* Traditional color indices. */
439 return (c % 8);
440 } else if (c >= 244) {
441 /* Upper grayscale colors. */
442 return (TC_WHITE);
443 } else if (c >= 232) {
444 /* Lower grayscale colors. */
445 return (TC_BLACK);
446 }
447
448 /* Convert to RGB. */
449 c -= 16;
450 b = c % 6;
451 g = (c / 6) % 6;
452 r = c / 36;
453
454 if (r < g) {
455 /* Possibly green. */
456 if (g < b)
457 return (TC_BLUE);
458 else if (g > b)
459 return (TC_GREEN);
460 else
461 return (TC_CYAN);
462 } else if (r > g) {
463 /* Possibly red. */
464 if (r < b)
465 return (TC_BLUE);
466 else if (r > b)
467 return (TC_RED);
468 else
469 return (TC_MAGENTA);
470 } else {
471 /* Possibly brown. */
472 if (g < b)
473 return (TC_BLUE);
474 else if (g > b)
475 return (TC_BROWN);
476 else if (r < 3)
477 return (TC_BLACK);
478 else
479 return (TC_WHITE);
480 }
481}
482
483static const char * const special_strings_cons25[] = {
484 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
485 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
486
487 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
488 [TKEY_INSERT] = "\x1B[L", [TKEY_DELETE] = "\x7F",
489 [TKEY_PAGE_UP] = "\x1B[I", [TKEY_PAGE_DOWN] = "\x1B[G",
490
491 [TKEY_F1] = "\x1B[M", [TKEY_F2] = "\x1B[N",
492 [TKEY_F3] = "\x1B[O", [TKEY_F4] = "\x1B[P",
493 [TKEY_F5] = "\x1B[Q", [TKEY_F6] = "\x1B[R",
494 [TKEY_F7] = "\x1B[S", [TKEY_F8] = "\x1B[T",
495 [TKEY_F9] = "\x1B[U", [TKEY_F10] = "\x1B[V",
496 [TKEY_F11] = "\x1B[W", [TKEY_F12] = "\x1B[X",
497};
498
499static const char * const special_strings_ckeys[] = {
500 [TKEY_UP] = "\x1BOA", [TKEY_DOWN] = "\x1BOB",
501 [TKEY_LEFT] = "\x1BOD", [TKEY_RIGHT] = "\x1BOC",
502
503 [TKEY_HOME] = "\x1BOH", [TKEY_END] = "\x1BOF",
504};
505
506static const char * const special_strings_normal[] = {
507 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
508 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
509
510 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
511 [TKEY_INSERT] = "\x1B[2~", [TKEY_DELETE] = "\x1B[3~",
512 [TKEY_PAGE_UP] = "\x1B[5~", [TKEY_PAGE_DOWN] = "\x1B[6~",
513
514 [TKEY_F1] = "\x1BOP", [TKEY_F2] = "\x1BOQ",
515 [TKEY_F3] = "\x1BOR", [TKEY_F4] = "\x1BOS",
516 [TKEY_F5] = "\x1B[15~", [TKEY_F6] = "\x1B[17~",
517 [TKEY_F7] = "\x1B[18~", [TKEY_F8] = "\x1B[19~",
518 [TKEY_F9] = "\x1B[20~", [TKEY_F10] = "\x1B[21~",
519 [TKEY_F11] = "\x1B[23~", [TKEY_F12] = "\x1B[24~",
520};
521
522const char *
523teken_get_sequence(teken_t *t, unsigned int k)
524{
525
526 /* Cons25 mode. */
527 if (t->t_stateflags & TS_CONS25 &&
528 k < sizeof special_strings_cons25 / sizeof(char *))
529 return (special_strings_cons25[k]);
530
531 /* Cursor keys mode. */
532 if (t->t_stateflags & TS_CURSORKEYS &&
533 k < sizeof special_strings_ckeys / sizeof(char *))
534 return (special_strings_ckeys[k]);
535
536 /* Default xterm sequences. */
537 if (k < sizeof special_strings_normal / sizeof(char *))
538 return (special_strings_normal[k]);
539
540 return (NULL);
541}
542
543#include "teken_state.h"
152 t->t_funcs = tf;
153 t->t_softc = softc;
154
155 t->t_nextstate = teken_state_init;
156 t->t_stateflags = 0;
157 t->t_utf8_left = 0;
158
159 t->t_defattr.ta_format = 0;
160 t->t_defattr.ta_fgcolor = TC_WHITE;
161 t->t_defattr.ta_bgcolor = TC_BLACK;
162 teken_subr_do_reset(t);
163
164 teken_set_winsize(t, &tp);
165}
166
167static void
168teken_input_char(teken_t *t, teken_char_t c)
169{
170
171 /*
172 * There is no support for DCS and OSC. Just discard strings
173 * until we receive characters that may indicate string
174 * termination.
175 */
176 if (t->t_stateflags & TS_INSTRING) {
177 switch (c) {
178 case '\x1B':
179 t->t_stateflags &= ~TS_INSTRING;
180 break;
181 case '\a':
182 t->t_stateflags &= ~TS_INSTRING;
183 return;
184 default:
185 return;
186 }
187 }
188
189 switch (c) {
190 case '\0':
191 break;
192 case '\a':
193 teken_subr_bell(t);
194 break;
195 case '\b':
196 teken_subr_backspace(t);
197 break;
198 case '\n':
199 case '\x0B':
200 teken_subr_newline(t);
201 break;
202 case '\x0C':
203 teken_subr_newpage(t);
204 break;
205 case '\x0E':
206 if (t->t_stateflags & TS_CONS25)
207 t->t_nextstate(t, c);
208 else
209 t->t_curscs = 1;
210 break;
211 case '\x0F':
212 if (t->t_stateflags & TS_CONS25)
213 t->t_nextstate(t, c);
214 else
215 t->t_curscs = 0;
216 break;
217 case '\r':
218 teken_subr_carriage_return(t);
219 break;
220 case '\t':
221 teken_subr_horizontal_tab(t);
222 break;
223 default:
224 t->t_nextstate(t, c);
225 break;
226 }
227
228 /* Post-processing assertions. */
229 teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
230 teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
231 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
232 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
233 teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
234 teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
235 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
236 teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
237 /* Origin region has to be window size or the same as scrollreg. */
238 teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
239 t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
240 (t->t_originreg.ts_begin == 0 &&
241 t->t_originreg.ts_end == t->t_winsize.tp_row));
242}
243
244static void
245teken_input_byte(teken_t *t, unsigned char c)
246{
247
248 /*
249 * UTF-8 handling.
250 */
251 if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
252 /* One-byte sequence. */
253 t->t_utf8_left = 0;
254 teken_input_char(t, c);
255 } else if ((c & 0xe0) == 0xc0) {
256 /* Two-byte sequence. */
257 t->t_utf8_left = 1;
258 t->t_utf8_partial = c & 0x1f;
259 } else if ((c & 0xf0) == 0xe0) {
260 /* Three-byte sequence. */
261 t->t_utf8_left = 2;
262 t->t_utf8_partial = c & 0x0f;
263 } else if ((c & 0xf8) == 0xf0) {
264 /* Four-byte sequence. */
265 t->t_utf8_left = 3;
266 t->t_utf8_partial = c & 0x07;
267 } else if ((c & 0xc0) == 0x80) {
268 if (t->t_utf8_left == 0)
269 return;
270 t->t_utf8_left--;
271 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
272 if (t->t_utf8_left == 0) {
273 teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
274 teken_input_char(t, t->t_utf8_partial);
275 }
276 }
277}
278
279void
280teken_input(teken_t *t, const void *buf, size_t len)
281{
282 const char *c = buf;
283
284 while (len-- > 0)
285 teken_input_byte(t, *c++);
286}
287
288const teken_pos_t *
289teken_get_cursor(teken_t *t)
290{
291
292 return (&t->t_cursor);
293}
294
295void
296teken_set_cursor(teken_t *t, const teken_pos_t *p)
297{
298
299 /* XXX: bounds checking with originreg! */
300 teken_assert(p->tp_row < t->t_winsize.tp_row);
301 teken_assert(p->tp_col < t->t_winsize.tp_col);
302
303 t->t_cursor = *p;
304}
305
306const teken_attr_t *
307teken_get_curattr(teken_t *t)
308{
309
310 return (&t->t_curattr);
311}
312
313void
314teken_set_curattr(teken_t *t, const teken_attr_t *a)
315{
316
317 t->t_curattr = *a;
318}
319
320const teken_attr_t *
321teken_get_defattr(teken_t *t)
322{
323
324 return (&t->t_defattr);
325}
326
327void
328teken_set_defattr(teken_t *t, const teken_attr_t *a)
329{
330
331 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
332}
333
334const teken_pos_t *
335teken_get_winsize(teken_t *t)
336{
337
338 return (&t->t_winsize);
339}
340
341void
342teken_set_winsize(teken_t *t, const teken_pos_t *p)
343{
344
345 t->t_winsize = *p;
346 teken_subr_do_reset(t);
347}
348
349void
350teken_set_8bit(teken_t *t)
351{
352
353 t->t_stateflags |= TS_8BIT;
354}
355
356void
357teken_set_cons25(teken_t *t)
358{
359
360 t->t_stateflags |= TS_CONS25;
361}
362
363/*
364 * State machine.
365 */
366
367static void
368teken_state_switch(teken_t *t, teken_state_t *s)
369{
370
371 t->t_nextstate = s;
372 t->t_curnum = 0;
373 t->t_stateflags |= TS_FIRSTDIGIT;
374}
375
376static int
377teken_state_numbers(teken_t *t, teken_char_t c)
378{
379
380 teken_assert(t->t_curnum < T_NUMSIZE);
381
382 if (c >= '0' && c <= '9') {
383 /*
384 * Don't do math with the default value of 1 when a
385 * custom number is inserted.
386 */
387 if (t->t_stateflags & TS_FIRSTDIGIT) {
388 t->t_stateflags &= ~TS_FIRSTDIGIT;
389 t->t_nums[t->t_curnum] = 0;
390 } else {
391 t->t_nums[t->t_curnum] *= 10;
392 }
393
394 t->t_nums[t->t_curnum] += c - '0';
395 return (1);
396 } else if (c == ';') {
397 if (t->t_stateflags & TS_FIRSTDIGIT)
398 t->t_nums[t->t_curnum] = 0;
399
400 /* Only allow a limited set of arguments. */
401 if (++t->t_curnum == T_NUMSIZE) {
402 teken_state_switch(t, teken_state_init);
403 return (1);
404 }
405
406 t->t_stateflags |= TS_FIRSTDIGIT;
407 return (1);
408 } else {
409 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
410 /* Finish off the last empty argument. */
411 t->t_nums[t->t_curnum] = 0;
412 t->t_curnum++;
413 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
414 /* Also count the last argument. */
415 t->t_curnum++;
416 }
417 }
418
419 return (0);
420}
421
422teken_color_t
423teken_256to8(teken_color_t c)
424{
425 unsigned int r, g, b;
426
427 if (c < 16) {
428 /* Traditional color indices. */
429 return (c % 8);
430 } else if (c >= 244) {
431 /* Upper grayscale colors. */
432 return (TC_WHITE);
433 } else if (c >= 232) {
434 /* Lower grayscale colors. */
435 return (TC_BLACK);
436 }
437
438 /* Convert to RGB. */
439 c -= 16;
440 b = c % 6;
441 g = (c / 6) % 6;
442 r = c / 36;
443
444 if (r < g) {
445 /* Possibly green. */
446 if (g < b)
447 return (TC_BLUE);
448 else if (g > b)
449 return (TC_GREEN);
450 else
451 return (TC_CYAN);
452 } else if (r > g) {
453 /* Possibly red. */
454 if (r < b)
455 return (TC_BLUE);
456 else if (r > b)
457 return (TC_RED);
458 else
459 return (TC_MAGENTA);
460 } else {
461 /* Possibly brown. */
462 if (g < b)
463 return (TC_BLUE);
464 else if (g > b)
465 return (TC_BROWN);
466 else if (r < 3)
467 return (TC_BLACK);
468 else
469 return (TC_WHITE);
470 }
471}
472
473static const char * const special_strings_cons25[] = {
474 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
475 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
476
477 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
478 [TKEY_INSERT] = "\x1B[L", [TKEY_DELETE] = "\x7F",
479 [TKEY_PAGE_UP] = "\x1B[I", [TKEY_PAGE_DOWN] = "\x1B[G",
480
481 [TKEY_F1] = "\x1B[M", [TKEY_F2] = "\x1B[N",
482 [TKEY_F3] = "\x1B[O", [TKEY_F4] = "\x1B[P",
483 [TKEY_F5] = "\x1B[Q", [TKEY_F6] = "\x1B[R",
484 [TKEY_F7] = "\x1B[S", [TKEY_F8] = "\x1B[T",
485 [TKEY_F9] = "\x1B[U", [TKEY_F10] = "\x1B[V",
486 [TKEY_F11] = "\x1B[W", [TKEY_F12] = "\x1B[X",
487};
488
489static const char * const special_strings_ckeys[] = {
490 [TKEY_UP] = "\x1BOA", [TKEY_DOWN] = "\x1BOB",
491 [TKEY_LEFT] = "\x1BOD", [TKEY_RIGHT] = "\x1BOC",
492
493 [TKEY_HOME] = "\x1BOH", [TKEY_END] = "\x1BOF",
494};
495
496static const char * const special_strings_normal[] = {
497 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
498 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
499
500 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
501 [TKEY_INSERT] = "\x1B[2~", [TKEY_DELETE] = "\x1B[3~",
502 [TKEY_PAGE_UP] = "\x1B[5~", [TKEY_PAGE_DOWN] = "\x1B[6~",
503
504 [TKEY_F1] = "\x1BOP", [TKEY_F2] = "\x1BOQ",
505 [TKEY_F3] = "\x1BOR", [TKEY_F4] = "\x1BOS",
506 [TKEY_F5] = "\x1B[15~", [TKEY_F6] = "\x1B[17~",
507 [TKEY_F7] = "\x1B[18~", [TKEY_F8] = "\x1B[19~",
508 [TKEY_F9] = "\x1B[20~", [TKEY_F10] = "\x1B[21~",
509 [TKEY_F11] = "\x1B[23~", [TKEY_F12] = "\x1B[24~",
510};
511
512const char *
513teken_get_sequence(teken_t *t, unsigned int k)
514{
515
516 /* Cons25 mode. */
517 if (t->t_stateflags & TS_CONS25 &&
518 k < sizeof special_strings_cons25 / sizeof(char *))
519 return (special_strings_cons25[k]);
520
521 /* Cursor keys mode. */
522 if (t->t_stateflags & TS_CURSORKEYS &&
523 k < sizeof special_strings_ckeys / sizeof(char *))
524 return (special_strings_ckeys[k]);
525
526 /* Default xterm sequences. */
527 if (k < sizeof special_strings_normal / sizeof(char *))
528 return (special_strings_normal[k]);
529
530 return (NULL);
531}
532
533#include "teken_state.h"