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