2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #if defined(__FreeBSD__) && defined(_KERNEL)
31 #include <sys/param.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>
42 #define teken_assert(x) assert(x)
43 #define teken_printf(x,...) do { \
45 fprintf(df, x, ## __VA_ARGS__); \
49 #endif /* __FreeBSD__ && _KERNEL */
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. */
62 /* Character that blanks a cell. */
66 #include "teken_wcwidth.h"
67 #include "teken_scs.h"
69 static teken_state_t teken_state_init;
76 teken_funcs_bell(teken_t *t)
79 t->t_funcs->tf_bell(t->t_softc);
83 teken_funcs_cursor(teken_t *t)
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);
89 t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
93 teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
94 const teken_attr_t *a)
97 teken_assert(p->tp_row < t->t_winsize.tp_row);
98 teken_assert(p->tp_col < t->t_winsize.tp_col);
100 t->t_funcs->tf_putchar(t->t_softc, p, c, a);
104 teken_funcs_fill(teken_t *t, const teken_rect_t *r,
105 const teken_char_t c, const teken_attr_t *a)
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);
113 t->t_funcs->tf_fill(t->t_softc, r, c, a);
117 teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
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);
127 t->t_funcs->tf_copy(t->t_softc, r, p);
131 teken_funcs_param(teken_t *t, int cmd, unsigned int value)
134 t->t_funcs->tf_param(t->t_softc, cmd, value);
138 teken_funcs_respond(teken_t *t, const void *buf, size_t len)
141 t->t_funcs->tf_respond(t->t_softc, buf, len);
144 #include "teken_subr.h"
145 #include "teken_subr_compat.h"
148 * Programming interface.
152 teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
154 teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
156 #if !(defined(__FreeBSD__) && defined(_KERNEL))
157 df = fopen("teken.log", "w");
159 setvbuf(df, NULL, _IOLBF, BUFSIZ);
160 #endif /* !(__FreeBSD__ && _KERNEL) */
165 t->t_nextstate = teken_state_init;
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);
174 teken_set_winsize(t, &tp);
178 teken_input_char(teken_t *t, teken_char_t c)
182 * There is no support for DCS and OSC. Just discard strings
183 * until we receive characters that may indicate string
186 if (t->t_stateflags & TS_INSTRING) {
189 t->t_stateflags &= ~TS_INSTRING;
192 t->t_stateflags &= ~TS_INSTRING;
206 teken_subr_backspace(t);
210 teken_subr_newline(t);
213 teken_subr_newpage(t);
216 if (t->t_stateflags & TS_CONS25)
217 t->t_nextstate(t, c);
222 if (t->t_stateflags & TS_CONS25)
223 t->t_nextstate(t, c);
228 teken_subr_carriage_return(t);
231 teken_subr_horizontal_tab(t);
234 t->t_nextstate(t, c);
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));
255 teken_input_byte(teken_t *t, unsigned char c)
261 if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
262 /* One-byte sequence. */
264 teken_input_char(t, c);
265 } else if ((c & 0xe0) == 0xc0) {
266 /* Two-byte sequence. */
268 t->t_utf8_partial = c & 0x1f;
269 } else if ((c & 0xf0) == 0xe0) {
270 /* Three-byte sequence. */
272 t->t_utf8_partial = c & 0x0f;
273 } else if ((c & 0xf8) == 0xf0) {
274 /* Four-byte sequence. */
276 t->t_utf8_partial = c & 0x07;
277 } else if ((c & 0xc0) == 0x80) {
278 if (t->t_utf8_left == 0)
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);
290 teken_input(teken_t *t, const void *buf, size_t len)
295 teken_input_byte(t, *c++);
299 teken_get_cursor(teken_t *t)
302 return (&t->t_cursor);
306 teken_set_cursor(teken_t *t, const teken_pos_t *p)
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);
317 teken_get_curattr(teken_t *t)
320 return (&t->t_curattr);
324 teken_set_curattr(teken_t *t, const teken_attr_t *a)
331 teken_get_defattr(teken_t *t)
334 return (&t->t_defattr);
338 teken_set_defattr(teken_t *t, const teken_attr_t *a)
341 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
345 teken_get_winsize(teken_t *t)
348 return (&t->t_winsize);
352 teken_set_winsize(teken_t *t, const teken_pos_t *p)
356 teken_subr_do_reset(t);
360 teken_set_8bit(teken_t *t)
363 t->t_stateflags |= TS_8BIT;
367 teken_set_cons25(teken_t *t)
370 t->t_stateflags |= TS_CONS25;
378 teken_state_switch(teken_t *t, teken_state_t *s)
383 t->t_stateflags |= TS_FIRSTDIGIT;
387 teken_state_numbers(teken_t *t, teken_char_t c)
390 teken_assert(t->t_curnum < T_NUMSIZE);
392 if (c >= '0' && c <= '9') {
394 * Don't do math with the default value of 1 when a
395 * custom number is inserted.
397 if (t->t_stateflags & TS_FIRSTDIGIT) {
398 t->t_stateflags &= ~TS_FIRSTDIGIT;
399 t->t_nums[t->t_curnum] = 0;
401 t->t_nums[t->t_curnum] *= 10;
404 t->t_nums[t->t_curnum] += c - '0';
406 } else if (c == ';') {
407 if (t->t_stateflags & TS_FIRSTDIGIT)
408 t->t_nums[t->t_curnum] = 0;
410 /* Only allow a limited set of arguments. */
411 if (++t->t_curnum == T_NUMSIZE) {
412 teken_state_switch(t, teken_state_init);
416 t->t_stateflags |= TS_FIRSTDIGIT;
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;
423 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
424 /* Also count the last argument. */
433 teken_256to8(teken_color_t c)
435 unsigned int r, g, b;
438 /* Traditional color indices. */
440 } else if (c >= 244) {
441 /* Upper grayscale colors. */
443 } else if (c >= 232) {
444 /* Lower grayscale colors. */
448 /* Convert to RGB. */
455 /* Possibly green. */
471 /* Possibly brown. */
483 static const char * const special_strings_cons25[] = {
484 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
485 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
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",
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",
499 static const char * const special_strings_ckeys[] = {
500 [TKEY_UP] = "\x1BOA", [TKEY_DOWN] = "\x1BOB",
501 [TKEY_LEFT] = "\x1BOD", [TKEY_RIGHT] = "\x1BOC",
503 [TKEY_HOME] = "\x1BOH", [TKEY_END] = "\x1BOF",
506 static const char * const special_strings_normal[] = {
507 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
508 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
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~",
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~",
523 teken_get_sequence(teken_t *t, unsigned int k)
527 if (t->t_stateflags & TS_CONS25 &&
528 k < sizeof special_strings_cons25 / sizeof(char *))
529 return (special_strings_cons25[k]);
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]);
536 /* Default xterm sequences. */
537 if (k < sizeof special_strings_normal / sizeof(char *))
538 return (special_strings_normal[k]);
543 #include "teken_state.h"