teken.c revision 186681
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/dev/syscons/teken/teken.c 186681 2009-01-01 13:26:53Z 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)
35#define	teken_printf(x,...)
36#else /* !(__FreeBSD__ && _KERNEL) */
37#include <sys/types.h>
38#include <assert.h>
39#include <inttypes.h>
40#include <stdio.h>
41#include <string.h>
42#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
51#include "teken.h"
52#ifdef TEKEN_UTF8
53#include "teken_wcwidth.h"
54#else /* !TEKEN_UTF8 */
55static inline int
56teken_wcwidth(teken_char_t c)
57{
58
59	return (c <= 0x1B) ? -1 : 1;
60}
61#endif /* TEKEN_UTF8 */
62
63/* Private flags for teken_format_t. */
64#define	TF_REVERSE	0x08
65
66/* Private flags for t_stateflags. */
67#define	TS_FIRSTDIGIT	0x01	/* First numeric digit in escape sequence. */
68#define	TS_INSERT	0x02	/* Insert mode. */
69#define	TS_AUTOWRAP	0x04	/* Autowrap. */
70#define	TS_ORIGIN	0x08	/* Origin mode. */
71#define	TS_WRAPPED	0x10	/* Next character should be printed on col 0. */
72
73/* Character that blanks a cell. */
74#define	BLANK	' '
75
76static teken_state_t	teken_state_init;
77
78/*
79 * Wrappers for hooks.
80 */
81
82static inline void
83teken_funcs_bell(teken_t *t)
84{
85
86	t->t_funcs->tf_bell(t->t_softc);
87}
88
89static inline void
90teken_funcs_cursor(teken_t *t)
91{
92
93	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
94	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
95
96	t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
97}
98
99static inline void
100teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
101    const teken_attr_t *a)
102{
103	teken_attr_t ta;
104
105	teken_assert(p->tp_row < t->t_winsize.tp_row);
106	teken_assert(p->tp_col < t->t_winsize.tp_col);
107
108	/* Apply inversion. */
109	if (a->ta_format & TF_REVERSE) {
110		ta.ta_format = a->ta_format;
111		ta.ta_fgcolor = a->ta_bgcolor;
112		ta.ta_bgcolor = a->ta_fgcolor;
113		a = &ta;
114	}
115
116	t->t_funcs->tf_putchar(t->t_softc, p, c, a);
117}
118
119static inline void
120teken_funcs_fill(teken_t *t, const teken_rect_t *r,
121    const teken_char_t c, const teken_attr_t *a)
122{
123	teken_attr_t ta;
124
125	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
126	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
127	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
128	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
129
130	/* Apply inversion. */
131	if (a->ta_format & TF_REVERSE) {
132		ta.ta_format = a->ta_format;
133		ta.ta_fgcolor = a->ta_bgcolor;
134		ta.ta_bgcolor = a->ta_fgcolor;
135		a = &ta;
136	}
137
138	t->t_funcs->tf_fill(t->t_softc, r, c, a);
139}
140
141static inline void
142teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
143{
144
145	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
146	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
147	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
148	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
149	teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
150	teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
151
152	t->t_funcs->tf_copy(t->t_softc, r, p);
153}
154
155static inline void
156teken_funcs_param(teken_t *t, int cmd, int value)
157{
158
159	t->t_funcs->tf_param(t->t_softc, cmd, value);
160}
161
162static inline void
163teken_funcs_respond(teken_t *t, const void *buf, size_t len)
164{
165
166	t->t_funcs->tf_respond(t->t_softc, buf, len);
167}
168
169#include "teken_subr.h"
170#include "teken_subr_compat.h"
171
172/*
173 * Programming interface.
174 */
175
176void
177teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
178{
179	teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
180
181#if !(defined(__FreeBSD__) && defined(_KERNEL))
182	df = fopen("teken.log", "w");
183	if (df != NULL)
184		setvbuf(df, NULL, _IOLBF, BUFSIZ);
185#endif /* !(__FreeBSD__ && _KERNEL) */
186
187	t->t_funcs = tf;
188	t->t_softc = softc;
189
190	t->t_nextstate = teken_state_init;
191
192	t->t_defattr.ta_format = 0;
193	t->t_defattr.ta_fgcolor = TC_WHITE;
194	t->t_defattr.ta_bgcolor = TC_BLACK;
195	teken_subr_do_reset(t);
196
197#ifdef TEKEN_UTF8
198	t->t_utf8_left = 0;
199#endif /* TEKEN_UTF8 */
200
201	teken_set_winsize(t, &tp);
202}
203
204static void
205teken_input_char(teken_t *t, teken_char_t c)
206{
207
208	switch (c) {
209	case '\0':
210		break;
211	case '\a':
212		teken_subr_bell(t);
213		break;
214	case '\b':
215		teken_subr_backspace(t);
216		break;
217	case '\n':
218	case '\x0B':
219		teken_subr_newline(t);
220		break;
221	case '\r':
222		teken_subr_carriage_return(t);
223		break;
224	case '\t':
225		teken_subr_horizontal_tab(t);
226		break;
227	default:
228		t->t_nextstate(t, c);
229		break;
230	}
231
232	/* Post-processing assertions. */
233	teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
234	teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
235	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
236	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
237	teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
238	teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
239	teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
240	teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
241	/* Origin region has to be window size or the same as scrollreg. */
242	teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
243	    t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
244	    (t->t_originreg.ts_begin == 0 &&
245	    t->t_originreg.ts_end == t->t_winsize.tp_row));
246}
247
248static void
249teken_input_byte(teken_t *t, unsigned char c)
250{
251
252#ifdef TEKEN_UTF8
253	/*
254	 * UTF-8 handling.
255	 */
256	if ((c & 0x80) == 0x00) {
257		/* One-byte sequence. */
258		t->t_utf8_left = 0;
259		teken_input_char(t, c);
260	} else if ((c & 0xe0) == 0xc0) {
261		/* Two-byte sequence. */
262		t->t_utf8_left = 1;
263		t->t_utf8_partial = c & 0x1f;
264	} else if ((c & 0xf0) == 0xe0) {
265		/* Three-byte sequence. */
266		t->t_utf8_left = 2;
267		t->t_utf8_partial = c & 0x0f;
268	} else if ((c & 0xf8) == 0xf0) {
269		/* Four-byte sequence. */
270		t->t_utf8_left = 3;
271		t->t_utf8_partial = c & 0x07;
272	} else if ((c & 0xc0) == 0x80) {
273		if (t->t_utf8_left == 0)
274			return;
275		t->t_utf8_left--;
276		t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
277		if (t->t_utf8_left == 0) {
278			teken_printf("Got UTF-8 char %u\n", t->t_utf8_partial);
279			teken_input_char(t, t->t_utf8_partial);
280		}
281	}
282#else /* !TEKEN_UTF8 */
283	teken_input_char(t, c);
284#endif /* TEKEN_UTF8 */
285}
286
287void
288teken_input(teken_t *t, const void *buf, size_t len)
289{
290	const char *c = buf;
291
292	while (len-- > 0)
293		teken_input_byte(t, *c++);
294}
295
296void
297teken_set_cursor(teken_t *t, const teken_pos_t *p)
298{
299
300	/* XXX: bounds checking with originreg! */
301	teken_assert(p->tp_row < t->t_winsize.tp_row);
302	teken_assert(p->tp_col < t->t_winsize.tp_col);
303
304	t->t_cursor = *p;
305}
306
307void
308teken_set_defattr(teken_t *t, const teken_attr_t *a)
309{
310
311	t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
312}
313
314void
315teken_set_winsize(teken_t *t, const teken_pos_t *p)
316{
317
318	teken_assert(p->tp_col <= T_NUMCOL);
319
320	t->t_winsize = *p;
321	/* XXX: bounds checking with cursor/etc! */
322	t->t_scrollreg.ts_begin = 0;
323	t->t_scrollreg.ts_end = t->t_winsize.tp_row;
324	t->t_originreg = t->t_scrollreg;
325}
326
327/*
328 * State machine.
329 */
330
331static void
332teken_state_switch(teken_t *t, teken_state_t *s)
333{
334
335	t->t_nextstate = s;
336	t->t_curnum = 0;
337	t->t_stateflags |= TS_FIRSTDIGIT;
338}
339
340static int
341teken_state_numbers(teken_t *t, teken_char_t c)
342{
343
344	teken_assert(t->t_curnum < T_NUMSIZE);
345
346	if (c >= '0' && c <= '9') {
347		/*
348		 * Don't do math with the default value of 1 when a
349		 * custom number is inserted.
350		 */
351		if (t->t_stateflags & TS_FIRSTDIGIT) {
352			t->t_stateflags &= ~TS_FIRSTDIGIT;
353			t->t_nums[t->t_curnum] = 0;
354		} else {
355			t->t_nums[t->t_curnum] *= 10;
356		}
357
358		t->t_nums[t->t_curnum] += c - '0';
359		return (1);
360	} else if (c == ';') {
361		if (t->t_stateflags & TS_FIRSTDIGIT)
362			t->t_nums[t->t_curnum] = 0;
363
364		/* Only allow a limited set of arguments. */
365		if (++t->t_curnum == T_NUMSIZE) {
366			teken_state_switch(t, teken_state_init);
367			return (1);
368		}
369
370		t->t_stateflags |= TS_FIRSTDIGIT;
371		return (1);
372	} else {
373		if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
374			/* Finish off the last empty argument. */
375			t->t_nums[t->t_curnum] = 0;
376			t->t_curnum++;
377		} else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
378			/* Also count the last argument. */
379			t->t_curnum++;
380		}
381	}
382
383	return (0);
384}
385
386#include "teken_state.h"
387