xref: /freebsd/sys/teken/teken.c (revision 4a11e7f1)
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$
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 */
48 static FILE *df;
49 #endif /* __FreeBSD__ && _KERNEL */
50 
51 /* Private flags for t_stateflags. */
52 #define	TS_FIRSTDIGIT	0x01	/* First numeric digit in escape sequence. */
53 #define	TS_INSERT	0x02	/* Insert mode. */
54 #define	TS_AUTOWRAP	0x04	/* Autowrap. */
55 #define	TS_ORIGIN	0x08	/* Origin mode. */
56 #define	TS_WRAPPED	0x10	/* Next character should be printed on col 0. */
57 #define	TS_8BIT		0x20	/* UTF-8 disabled. */
58 #define	TS_CONS25	0x40	/* cons25 emulation. */
59 #define	TS_INSTRING	0x80	/* Inside string. */
60 
61 /* Character that blanks a cell. */
62 #define	BLANK	' '
63 
64 #include "teken.h"
65 #include "teken_wcwidth.h"
66 #include "teken_scs.h"
67 
68 static teken_state_t	teken_state_init;
69 
70 /*
71  * Wrappers for hooks.
72  */
73 
74 static inline void
75 teken_funcs_bell(teken_t *t)
76 {
77 
78 	t->t_funcs->tf_bell(t->t_softc);
79 }
80 
81 static inline void
82 teken_funcs_cursor(teken_t *t)
83 {
84 
85 	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
86 	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
87 
88 	t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
89 }
90 
91 static inline void
92 teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
93     const teken_attr_t *a)
94 {
95 
96 	teken_assert(p->tp_row < t->t_winsize.tp_row);
97 	teken_assert(p->tp_col < t->t_winsize.tp_col);
98 
99 	t->t_funcs->tf_putchar(t->t_softc, p, c, a);
100 }
101 
102 static inline void
103 teken_funcs_fill(teken_t *t, const teken_rect_t *r,
104     const teken_char_t c, const teken_attr_t *a)
105 {
106 
107 	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
108 	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
109 	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
110 	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
111 
112 	t->t_funcs->tf_fill(t->t_softc, r, c, a);
113 }
114 
115 static inline void
116 teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
117 {
118 
119 	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
120 	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
121 	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
122 	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
123 	teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
124 	teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
125 
126 	t->t_funcs->tf_copy(t->t_softc, r, p);
127 }
128 
129 static inline void
130 teken_funcs_param(teken_t *t, int cmd, unsigned int value)
131 {
132 
133 	t->t_funcs->tf_param(t->t_softc, cmd, value);
134 }
135 
136 static inline void
137 teken_funcs_respond(teken_t *t, const void *buf, size_t len)
138 {
139 
140 	t->t_funcs->tf_respond(t->t_softc, buf, len);
141 }
142 
143 #include "teken_subr.h"
144 #include "teken_subr_compat.h"
145 
146 /*
147  * Programming interface.
148  */
149 
150 void
151 teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
152 {
153 	teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
154 
155 #if !(defined(__FreeBSD__) && defined(_KERNEL))
156 	df = fopen("teken.log", "w");
157 	if (df != NULL)
158 		setvbuf(df, NULL, _IOLBF, BUFSIZ);
159 #endif /* !(__FreeBSD__ && _KERNEL) */
160 
161 	t->t_funcs = tf;
162 	t->t_softc = softc;
163 
164 	t->t_nextstate = teken_state_init;
165 	t->t_stateflags = 0;
166 	t->t_utf8_left = 0;
167 
168 	t->t_defattr.ta_format = 0;
169 	t->t_defattr.ta_fgcolor = TC_WHITE;
170 	t->t_defattr.ta_bgcolor = TC_BLACK;
171 	teken_subr_do_reset(t);
172 
173 	teken_set_winsize(t, &tp);
174 }
175 
176 static void
177 teken_input_char(teken_t *t, teken_char_t c)
178 {
179 
180 	/*
181 	 * There is no support for DCS and OSC.  Just discard strings
182 	 * until we receive characters that may indicate string
183 	 * termination.
184 	 */
185 	if (t->t_stateflags & TS_INSTRING) {
186 		switch (c) {
187 		case '\x1B':
188 			t->t_stateflags &= ~TS_INSTRING;
189 			break;
190 		case '\a':
191 			t->t_stateflags &= ~TS_INSTRING;
192 			return;
193 		default:
194 			return;
195 		}
196 	}
197 
198 	switch (c) {
199 	case '\0':
200 		break;
201 	case '\a':
202 		teken_subr_bell(t);
203 		break;
204 	case '\b':
205 		teken_subr_backspace(t);
206 		break;
207 	case '\n':
208 	case '\x0B':
209 		teken_subr_newline(t);
210 		break;
211 	case '\x0C':
212 		teken_subr_newpage(t);
213 		break;
214 	case '\x0E':
215 		if (t->t_stateflags & TS_CONS25)
216 			t->t_nextstate(t, c);
217 		else
218 			t->t_curscs = 1;
219 		break;
220 	case '\x0F':
221 		if (t->t_stateflags & TS_CONS25)
222 			t->t_nextstate(t, c);
223 		else
224 			t->t_curscs = 0;
225 		break;
226 	case '\r':
227 		teken_subr_carriage_return(t);
228 		break;
229 	case '\t':
230 		teken_subr_horizontal_tab(t);
231 		break;
232 	default:
233 		t->t_nextstate(t, c);
234 		break;
235 	}
236 
237 	/* Post-processing assertions. */
238 	teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
239 	teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
240 	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
241 	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
242 	teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
243 	teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
244 	teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
245 	teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
246 	/* Origin region has to be window size or the same as scrollreg. */
247 	teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
248 	    t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
249 	    (t->t_originreg.ts_begin == 0 &&
250 	    t->t_originreg.ts_end == t->t_winsize.tp_row));
251 }
252 
253 static void
254 teken_input_byte(teken_t *t, unsigned char c)
255 {
256 
257 	/*
258 	 * UTF-8 handling.
259 	 */
260 	if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
261 		/* One-byte sequence. */
262 		t->t_utf8_left = 0;
263 		teken_input_char(t, c);
264 	} else if ((c & 0xe0) == 0xc0) {
265 		/* Two-byte sequence. */
266 		t->t_utf8_left = 1;
267 		t->t_utf8_partial = c & 0x1f;
268 	} else if ((c & 0xf0) == 0xe0) {
269 		/* Three-byte sequence. */
270 		t->t_utf8_left = 2;
271 		t->t_utf8_partial = c & 0x0f;
272 	} else if ((c & 0xf8) == 0xf0) {
273 		/* Four-byte sequence. */
274 		t->t_utf8_left = 3;
275 		t->t_utf8_partial = c & 0x07;
276 	} else if ((c & 0xc0) == 0x80) {
277 		if (t->t_utf8_left == 0)
278 			return;
279 		t->t_utf8_left--;
280 		t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
281 		if (t->t_utf8_left == 0) {
282 			teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
283 			teken_input_char(t, t->t_utf8_partial);
284 		}
285 	}
286 }
287 
288 void
289 teken_input(teken_t *t, const void *buf, size_t len)
290 {
291 	const char *c = buf;
292 
293 	while (len-- > 0)
294 		teken_input_byte(t, *c++);
295 }
296 
297 const teken_pos_t *
298 teken_get_cursor(teken_t *t)
299 {
300 
301 	return (&t->t_cursor);
302 }
303 
304 void
305 teken_set_cursor(teken_t *t, const teken_pos_t *p)
306 {
307 
308 	/* XXX: bounds checking with originreg! */
309 	teken_assert(p->tp_row < t->t_winsize.tp_row);
310 	teken_assert(p->tp_col < t->t_winsize.tp_col);
311 
312 	t->t_cursor = *p;
313 }
314 
315 const teken_attr_t *
316 teken_get_curattr(teken_t *t)
317 {
318 
319 	return (&t->t_curattr);
320 }
321 
322 void
323 teken_set_curattr(teken_t *t, const teken_attr_t *a)
324 {
325 
326 	t->t_curattr = *a;
327 }
328 
329 const teken_attr_t *
330 teken_get_defattr(teken_t *t)
331 {
332 
333 	return (&t->t_defattr);
334 }
335 
336 void
337 teken_set_defattr(teken_t *t, const teken_attr_t *a)
338 {
339 
340 	t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
341 }
342 
343 const teken_pos_t *
344 teken_get_winsize(teken_t *t)
345 {
346 
347 	return (&t->t_winsize);
348 }
349 
350 void
351 teken_set_winsize(teken_t *t, const teken_pos_t *p)
352 {
353 
354 	t->t_winsize = *p;
355 	teken_subr_do_reset(t);
356 }
357 
358 void
359 teken_set_8bit(teken_t *t)
360 {
361 
362 	t->t_stateflags |= TS_8BIT;
363 }
364 
365 void
366 teken_set_cons25(teken_t *t)
367 {
368 
369 	t->t_stateflags |= TS_CONS25;
370 }
371 
372 /*
373  * State machine.
374  */
375 
376 static void
377 teken_state_switch(teken_t *t, teken_state_t *s)
378 {
379 
380 	t->t_nextstate = s;
381 	t->t_curnum = 0;
382 	t->t_stateflags |= TS_FIRSTDIGIT;
383 }
384 
385 static int
386 teken_state_numbers(teken_t *t, teken_char_t c)
387 {
388 
389 	teken_assert(t->t_curnum < T_NUMSIZE);
390 
391 	if (c >= '0' && c <= '9') {
392 		/*
393 		 * Don't do math with the default value of 1 when a
394 		 * custom number is inserted.
395 		 */
396 		if (t->t_stateflags & TS_FIRSTDIGIT) {
397 			t->t_stateflags &= ~TS_FIRSTDIGIT;
398 			t->t_nums[t->t_curnum] = 0;
399 		} else {
400 			t->t_nums[t->t_curnum] *= 10;
401 		}
402 
403 		t->t_nums[t->t_curnum] += c - '0';
404 		return (1);
405 	} else if (c == ';') {
406 		if (t->t_stateflags & TS_FIRSTDIGIT)
407 			t->t_nums[t->t_curnum] = 0;
408 
409 		/* Only allow a limited set of arguments. */
410 		if (++t->t_curnum == T_NUMSIZE) {
411 			teken_state_switch(t, teken_state_init);
412 			return (1);
413 		}
414 
415 		t->t_stateflags |= TS_FIRSTDIGIT;
416 		return (1);
417 	} else {
418 		if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
419 			/* Finish off the last empty argument. */
420 			t->t_nums[t->t_curnum] = 0;
421 			t->t_curnum++;
422 		} else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
423 			/* Also count the last argument. */
424 			t->t_curnum++;
425 		}
426 	}
427 
428 	return (0);
429 }
430 
431 teken_color_t
432 teken_256to8(teken_color_t c)
433 {
434 	unsigned int r, g, b;
435 
436 	if (c < 16) {
437 		/* Traditional color indices. */
438 		return (c % 8);
439 	} else if (c >= 244) {
440 		/* Upper grayscale colors. */
441 		return (TC_WHITE);
442 	} else if (c >= 232) {
443 		/* Lower grayscale colors. */
444 		return (TC_BLACK);
445 	}
446 
447 	/* Convert to RGB. */
448 	c -= 16;
449 	b = c % 6;
450 	g = (c / 6) % 6;
451 	r = c / 36;
452 
453 	if (r < g) {
454 		/* Possibly green. */
455 		if (g < b)
456 			return (TC_BLUE);
457 		else if (g > b)
458 			return (TC_GREEN);
459 		else
460 			return (TC_CYAN);
461 	} else if (r > g) {
462 		/* Possibly red. */
463 		if (r < b)
464 			return (TC_BLUE);
465 		else if (r > b)
466 			return (TC_RED);
467 		else
468 			return (TC_MAGENTA);
469 	} else {
470 		/* Possibly brown. */
471 		if (g < b)
472 			return (TC_BLUE);
473 		else if (g > b)
474 			return (TC_BROWN);
475 		else if (r < 3)
476 			return (TC_BLACK);
477 		else
478 			return (TC_WHITE);
479 	}
480 }
481 
482 #include "teken_state.h"
483