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 */
54 #include "teken_wcwidth.h"
55 #else /* !TEKEN_UTF8 */
57 #define teken_wcwidth(c) ((c <= 0x1B) ? -1 : 1)
58 #else /* !TEKEN_XTERM */
59 #define teken_wcwidth(c) (1)
60 #endif /* TEKEN_XTERM */
61 #endif /* TEKEN_UTF8 */
63 #if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
64 #include "teken_scs.h"
65 #else /* !(TEKEN_XTERM && TEKEN_UTF8) */
66 #define teken_scs_process(t, c) (c)
67 #define teken_scs_restore(t)
68 #define teken_scs_save(t)
69 #define teken_scs_set(t, g, ts)
70 #define teken_scs_switch(t, g)
71 #endif /* TEKEN_XTERM && TEKEN_UTF8 */
73 /* Private flags for teken_format_t. */
74 #define TF_REVERSE 0x08
76 /* Private flags for t_stateflags. */
77 #define TS_FIRSTDIGIT 0x01 /* First numeric digit in escape sequence. */
78 #define TS_INSERT 0x02 /* Insert mode. */
79 #define TS_AUTOWRAP 0x04 /* Autowrap. */
80 #define TS_ORIGIN 0x08 /* Origin mode. */
82 #define TS_WRAPPED 0x10 /* Next character should be printed on col 0. */
83 #else /* !TEKEN_XTERM */
84 #define TS_WRAPPED 0x00 /* Simple line wrapping. */
85 #endif /* TEKEN_XTERM */
87 /* Character that blanks a cell. */
90 static teken_state_t teken_state_init;
97 teken_funcs_bell(teken_t *t)
100 t->t_funcs->tf_bell(t->t_softc);
104 teken_funcs_cursor(teken_t *t)
107 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
108 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
110 t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
114 teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
115 const teken_attr_t *a)
119 teken_assert(p->tp_row < t->t_winsize.tp_row);
120 teken_assert(p->tp_col < t->t_winsize.tp_col);
122 /* Apply inversion. */
123 if (a->ta_format & TF_REVERSE) {
124 ta.ta_format = a->ta_format;
125 ta.ta_fgcolor = a->ta_bgcolor;
126 ta.ta_bgcolor = a->ta_fgcolor;
130 t->t_funcs->tf_putchar(t->t_softc, p, c, a);
134 teken_funcs_fill(teken_t *t, const teken_rect_t *r,
135 const teken_char_t c, const teken_attr_t *a)
139 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
140 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
141 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
142 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
144 /* Apply inversion. */
145 if (a->ta_format & TF_REVERSE) {
146 ta.ta_format = a->ta_format;
147 ta.ta_fgcolor = a->ta_bgcolor;
148 ta.ta_bgcolor = a->ta_fgcolor;
152 t->t_funcs->tf_fill(t->t_softc, r, c, a);
156 teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
159 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
160 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
161 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
162 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
163 teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
164 teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
166 t->t_funcs->tf_copy(t->t_softc, r, p);
170 teken_funcs_param(teken_t *t, int cmd, unsigned int value)
173 t->t_funcs->tf_param(t->t_softc, cmd, value);
177 teken_funcs_respond(teken_t *t, const void *buf, size_t len)
180 t->t_funcs->tf_respond(t->t_softc, buf, len);
183 #include "teken_subr.h"
184 #include "teken_subr_compat.h"
187 * Programming interface.
191 teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
193 teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
195 #if !(defined(__FreeBSD__) && defined(_KERNEL))
196 df = fopen("teken.log", "w");
198 setvbuf(df, NULL, _IOLBF, BUFSIZ);
199 #endif /* !(__FreeBSD__ && _KERNEL) */
204 t->t_nextstate = teken_state_init;
206 t->t_defattr.ta_format = 0;
207 t->t_defattr.ta_fgcolor = TC_WHITE;
208 t->t_defattr.ta_bgcolor = TC_BLACK;
209 teken_subr_do_reset(t);
213 #endif /* TEKEN_UTF8 */
215 teken_set_winsize(t, &tp);
219 teken_input_char(teken_t *t, teken_char_t c)
229 teken_subr_backspace(t);
233 teken_subr_newline(t);
236 teken_subr_newpage(t);
238 #if defined(TEKEN_XTERM) && defined(TEKEN_UTF8)
240 teken_scs_switch(t, 1);
243 teken_scs_switch(t, 0);
245 #endif /* TEKEN_XTERM && TEKEN_UTF8 */
247 teken_subr_carriage_return(t);
250 teken_subr_horizontal_tab(t);
253 t->t_nextstate(t, c);
257 /* Post-processing assertions. */
258 teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
259 teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
260 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
261 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
262 teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
263 teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
264 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
265 teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
266 /* Origin region has to be window size or the same as scrollreg. */
267 teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
268 t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
269 (t->t_originreg.ts_begin == 0 &&
270 t->t_originreg.ts_end == t->t_winsize.tp_row));
274 teken_input_byte(teken_t *t, unsigned char c)
281 if ((c & 0x80) == 0x00) {
282 /* One-byte sequence. */
284 teken_input_char(t, c);
285 } else if ((c & 0xe0) == 0xc0) {
286 /* Two-byte sequence. */
288 t->t_utf8_partial = c & 0x1f;
289 } else if ((c & 0xf0) == 0xe0) {
290 /* Three-byte sequence. */
292 t->t_utf8_partial = c & 0x0f;
293 } else if ((c & 0xf8) == 0xf0) {
294 /* Four-byte sequence. */
296 t->t_utf8_partial = c & 0x07;
297 } else if ((c & 0xc0) == 0x80) {
298 if (t->t_utf8_left == 0)
301 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
302 if (t->t_utf8_left == 0) {
303 teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
304 teken_input_char(t, t->t_utf8_partial);
307 #else /* !TEKEN_UTF8 */
308 teken_input_char(t, c);
309 #endif /* TEKEN_UTF8 */
313 teken_input(teken_t *t, const void *buf, size_t len)
318 teken_input_byte(t, *c++);
322 teken_set_cursor(teken_t *t, const teken_pos_t *p)
325 /* XXX: bounds checking with originreg! */
326 teken_assert(p->tp_row < t->t_winsize.tp_row);
327 teken_assert(p->tp_col < t->t_winsize.tp_col);
333 teken_get_curattr(teken_t *t)
336 return (&t->t_curattr);
340 teken_set_curattr(teken_t *t, const teken_attr_t *a)
347 teken_get_defattr(teken_t *t)
350 return (&t->t_defattr);
354 teken_set_defattr(teken_t *t, const teken_attr_t *a)
357 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
361 teken_set_winsize(teken_t *t, const teken_pos_t *p)
365 /* XXX: bounds checking with cursor/etc! */
366 t->t_scrollreg.ts_begin = 0;
367 t->t_scrollreg.ts_end = t->t_winsize.tp_row;
368 t->t_originreg = t->t_scrollreg;
376 teken_state_switch(teken_t *t, teken_state_t *s)
381 t->t_stateflags |= TS_FIRSTDIGIT;
385 teken_state_numbers(teken_t *t, teken_char_t c)
388 teken_assert(t->t_curnum < T_NUMSIZE);
390 if (c >= '0' && c <= '9') {
392 * Don't do math with the default value of 1 when a
393 * custom number is inserted.
395 if (t->t_stateflags & TS_FIRSTDIGIT) {
396 t->t_stateflags &= ~TS_FIRSTDIGIT;
397 t->t_nums[t->t_curnum] = 0;
399 t->t_nums[t->t_curnum] *= 10;
402 t->t_nums[t->t_curnum] += c - '0';
404 } else if (c == ';') {
405 if (t->t_stateflags & TS_FIRSTDIGIT)
406 t->t_nums[t->t_curnum] = 0;
408 /* Only allow a limited set of arguments. */
409 if (++t->t_curnum == T_NUMSIZE) {
410 teken_state_switch(t, teken_state_init);
414 t->t_stateflags |= TS_FIRSTDIGIT;
417 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
418 /* Finish off the last empty argument. */
419 t->t_nums[t->t_curnum] = 0;
421 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
422 /* Also count the last argument. */
430 #include "teken_state.h"