Deleted Added
full compact
teken.c (261551) teken.c (286798)
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 *
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/teken/teken.c 261551 2014-02-06 13:28:06Z ray $
26 * $FreeBSD: head/sys/teken/teken.c 286798 2015-08-15 08:42:33Z ed $
27 */
28
29#include <sys/cdefs.h>
30#if defined(__FreeBSD__) && defined(_KERNEL)
31#include <sys/param.h>
27 */
28
29#include <sys/cdefs.h>
30#if defined(__FreeBSD__) && defined(_KERNEL)
31#include <sys/param.h>
32#include <sys/limits.h>
32#include <sys/lock.h>
33#include <sys/systm.h>
34#define teken_assert(x) MPASS(x)
35#else /* !(__FreeBSD__ && _KERNEL) */
36#include <sys/types.h>
37#include <assert.h>
33#include <sys/lock.h>
34#include <sys/systm.h>
35#define teken_assert(x) MPASS(x)
36#else /* !(__FreeBSD__ && _KERNEL) */
37#include <sys/types.h>
38#include <assert.h>
39#include <limits.h>
38#include <stdint.h>
39#include <stdio.h>
40#include <string.h>
41#define teken_assert(x) assert(x)
42#endif /* __FreeBSD__ && _KERNEL */
43
44/* debug messages */
45#define teken_printf(x,...)
46
47/* Private flags for t_stateflags. */
48#define TS_FIRSTDIGIT 0x0001 /* First numeric digit in escape sequence. */
49#define TS_INSERT 0x0002 /* Insert mode. */
50#define TS_AUTOWRAP 0x0004 /* Autowrap. */
51#define TS_ORIGIN 0x0008 /* Origin mode. */
52#define TS_WRAPPED 0x0010 /* Next character should be printed on col 0. */
53#define TS_8BIT 0x0020 /* UTF-8 disabled. */
54#define TS_CONS25 0x0040 /* cons25 emulation. */
55#define TS_INSTRING 0x0080 /* Inside string. */
56#define TS_CURSORKEYS 0x0100 /* Cursor keys mode. */
57
58/* Character that blanks a cell. */
59#define BLANK ' '
60
61#include "teken.h"
62#include "teken_wcwidth.h"
63#include "teken_scs.h"
64
65static teken_state_t teken_state_init;
66
67/*
68 * Wrappers for hooks.
69 */
70
71static inline void
72teken_funcs_bell(teken_t *t)
73{
74
75 t->t_funcs->tf_bell(t->t_softc);
76}
77
78static inline void
79teken_funcs_cursor(teken_t *t)
80{
81
82 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
83 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
84
85 t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
86}
87
88static inline void
89teken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
90 const teken_attr_t *a)
91{
92
93 teken_assert(p->tp_row < t->t_winsize.tp_row);
94 teken_assert(p->tp_col < t->t_winsize.tp_col);
95
96 t->t_funcs->tf_putchar(t->t_softc, p, c, a);
97}
98
99static inline void
100teken_funcs_fill(teken_t *t, const teken_rect_t *r,
101 const teken_char_t c, const teken_attr_t *a)
102{
103
104 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
105 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
106 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
107 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
108
109 t->t_funcs->tf_fill(t->t_softc, r, c, a);
110}
111
112static inline void
113teken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
114{
115
116 teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
117 teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
118 teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
119 teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
120 teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
121 teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
122
123 t->t_funcs->tf_copy(t->t_softc, r, p);
124}
125
126static inline void
127teken_funcs_param(teken_t *t, int cmd, unsigned int value)
128{
129
130 t->t_funcs->tf_param(t->t_softc, cmd, value);
131}
132
133static inline void
134teken_funcs_respond(teken_t *t, const void *buf, size_t len)
135{
136
137 t->t_funcs->tf_respond(t->t_softc, buf, len);
138}
139
140#include "teken_subr.h"
141#include "teken_subr_compat.h"
142
143/*
144 * Programming interface.
145 */
146
147void
148teken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
149{
150 teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
151
152 t->t_funcs = tf;
153 t->t_softc = softc;
154
155 t->t_nextstate = teken_state_init;
156 t->t_stateflags = 0;
157 t->t_utf8_left = 0;
158
159 t->t_defattr.ta_format = 0;
160 t->t_defattr.ta_fgcolor = TC_WHITE;
161 t->t_defattr.ta_bgcolor = TC_BLACK;
162 teken_subr_do_reset(t);
163
164 teken_set_winsize(t, &tp);
165}
166
167static void
168teken_input_char(teken_t *t, teken_char_t c)
169{
170
171 /*
172 * There is no support for DCS and OSC. Just discard strings
173 * until we receive characters that may indicate string
174 * termination.
175 */
176 if (t->t_stateflags & TS_INSTRING) {
177 switch (c) {
178 case '\x1B':
179 t->t_stateflags &= ~TS_INSTRING;
180 break;
181 case '\a':
182 t->t_stateflags &= ~TS_INSTRING;
183 return;
184 default:
185 return;
186 }
187 }
188
189 switch (c) {
190 case '\0':
191 break;
192 case '\a':
193 teken_subr_bell(t);
194 break;
195 case '\b':
196 teken_subr_backspace(t);
197 break;
198 case '\n':
199 case '\x0B':
200 teken_subr_newline(t);
201 break;
202 case '\x0C':
203 teken_subr_newpage(t);
204 break;
205 case '\x0E':
206 if (t->t_stateflags & TS_CONS25)
207 t->t_nextstate(t, c);
208 else
209 t->t_curscs = 1;
210 break;
211 case '\x0F':
212 if (t->t_stateflags & TS_CONS25)
213 t->t_nextstate(t, c);
214 else
215 t->t_curscs = 0;
216 break;
217 case '\r':
218 teken_subr_carriage_return(t);
219 break;
220 case '\t':
221 teken_subr_horizontal_tab(t);
222 break;
223 default:
224 t->t_nextstate(t, c);
225 break;
226 }
227
228 /* Post-processing assertions. */
229 teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
230 teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
231 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
232 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
233 teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
234 teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
235 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
236 teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
237 /* Origin region has to be window size or the same as scrollreg. */
238 teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
239 t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
240 (t->t_originreg.ts_begin == 0 &&
241 t->t_originreg.ts_end == t->t_winsize.tp_row));
242}
243
244static void
245teken_input_byte(teken_t *t, unsigned char c)
246{
247
248 /*
249 * UTF-8 handling.
250 */
251 if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
252 /* One-byte sequence. */
253 t->t_utf8_left = 0;
254 teken_input_char(t, c);
255 } else if ((c & 0xe0) == 0xc0) {
256 /* Two-byte sequence. */
257 t->t_utf8_left = 1;
258 t->t_utf8_partial = c & 0x1f;
259 } else if ((c & 0xf0) == 0xe0) {
260 /* Three-byte sequence. */
261 t->t_utf8_left = 2;
262 t->t_utf8_partial = c & 0x0f;
263 } else if ((c & 0xf8) == 0xf0) {
264 /* Four-byte sequence. */
265 t->t_utf8_left = 3;
266 t->t_utf8_partial = c & 0x07;
267 } else if ((c & 0xc0) == 0x80) {
268 if (t->t_utf8_left == 0)
269 return;
270 t->t_utf8_left--;
271 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
272 if (t->t_utf8_left == 0) {
273 teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
274 teken_input_char(t, t->t_utf8_partial);
275 }
276 }
277}
278
279void
280teken_input(teken_t *t, const void *buf, size_t len)
281{
282 const char *c = buf;
283
284 while (len-- > 0)
285 teken_input_byte(t, *c++);
286}
287
288const teken_pos_t *
289teken_get_cursor(teken_t *t)
290{
291
292 return (&t->t_cursor);
293}
294
295void
296teken_set_cursor(teken_t *t, const teken_pos_t *p)
297{
298
299 /* XXX: bounds checking with originreg! */
300 teken_assert(p->tp_row < t->t_winsize.tp_row);
301 teken_assert(p->tp_col < t->t_winsize.tp_col);
302
303 t->t_cursor = *p;
304}
305
306const teken_attr_t *
307teken_get_curattr(teken_t *t)
308{
309
310 return (&t->t_curattr);
311}
312
313void
314teken_set_curattr(teken_t *t, const teken_attr_t *a)
315{
316
317 t->t_curattr = *a;
318}
319
320const teken_attr_t *
321teken_get_defattr(teken_t *t)
322{
323
324 return (&t->t_defattr);
325}
326
327void
328teken_set_defattr(teken_t *t, const teken_attr_t *a)
329{
330
331 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
332}
333
334const teken_pos_t *
335teken_get_winsize(teken_t *t)
336{
337
338 return (&t->t_winsize);
339}
340
341static void
342teken_trim_cursor_pos(teken_t *t, const teken_pos_t *new)
343{
344 const teken_pos_t *cur;
345
346 cur = &t->t_winsize;
347
348 if (cur->tp_row < new->tp_row || cur->tp_col < new->tp_col)
349 return;
350 if (t->t_cursor.tp_row >= new->tp_row)
351 t->t_cursor.tp_row = new->tp_row - 1;
352 if (t->t_cursor.tp_col >= new->tp_col)
353 t->t_cursor.tp_col = new->tp_col - 1;
354}
355
356void
357teken_set_winsize(teken_t *t, const teken_pos_t *p)
358{
359
360 teken_trim_cursor_pos(t, p);
361 t->t_winsize = *p;
362 teken_subr_do_reset(t);
363}
364
365void
366teken_set_winsize_noreset(teken_t *t, const teken_pos_t *p)
367{
368
369 teken_trim_cursor_pos(t, p);
370 t->t_winsize = *p;
371 teken_subr_do_resize(t);
372}
373
374void
375teken_set_8bit(teken_t *t)
376{
377
378 t->t_stateflags |= TS_8BIT;
379}
380
381void
382teken_set_cons25(teken_t *t)
383{
384
385 t->t_stateflags |= TS_CONS25;
386}
387
388/*
389 * State machine.
390 */
391
392static void
393teken_state_switch(teken_t *t, teken_state_t *s)
394{
395
396 t->t_nextstate = s;
397 t->t_curnum = 0;
398 t->t_stateflags |= TS_FIRSTDIGIT;
399}
400
401static int
402teken_state_numbers(teken_t *t, teken_char_t c)
403{
404
405 teken_assert(t->t_curnum < T_NUMSIZE);
406
407 if (c >= '0' && c <= '9') {
40#include <stdint.h>
41#include <stdio.h>
42#include <string.h>
43#define teken_assert(x) assert(x)
44#endif /* __FreeBSD__ && _KERNEL */
45
46/* debug messages */
47#define teken_printf(x,...)
48
49/* Private flags for t_stateflags. */
50#define TS_FIRSTDIGIT 0x0001 /* First numeric digit in escape sequence. */
51#define TS_INSERT 0x0002 /* Insert mode. */
52#define TS_AUTOWRAP 0x0004 /* Autowrap. */
53#define TS_ORIGIN 0x0008 /* Origin mode. */
54#define TS_WRAPPED 0x0010 /* Next character should be printed on col 0. */
55#define TS_8BIT 0x0020 /* UTF-8 disabled. */
56#define TS_CONS25 0x0040 /* cons25 emulation. */
57#define TS_INSTRING 0x0080 /* Inside string. */
58#define TS_CURSORKEYS 0x0100 /* Cursor keys mode. */
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
67static teken_state_t teken_state_init;
68
69/*
70 * Wrappers for hooks.
71 */
72
73static inline void
74teken_funcs_bell(teken_t *t)
75{
76
77 t->t_funcs->tf_bell(t->t_softc);
78}
79
80static inline void
81teken_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
90static inline void
91teken_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
101static inline void
102teken_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
114static inline void
115teken_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
128static inline void
129teken_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
135static inline void
136teken_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
149void
150teken_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 t->t_funcs = tf;
155 t->t_softc = softc;
156
157 t->t_nextstate = teken_state_init;
158 t->t_stateflags = 0;
159 t->t_utf8_left = 0;
160
161 t->t_defattr.ta_format = 0;
162 t->t_defattr.ta_fgcolor = TC_WHITE;
163 t->t_defattr.ta_bgcolor = TC_BLACK;
164 teken_subr_do_reset(t);
165
166 teken_set_winsize(t, &tp);
167}
168
169static void
170teken_input_char(teken_t *t, teken_char_t c)
171{
172
173 /*
174 * There is no support for DCS and OSC. Just discard strings
175 * until we receive characters that may indicate string
176 * termination.
177 */
178 if (t->t_stateflags & TS_INSTRING) {
179 switch (c) {
180 case '\x1B':
181 t->t_stateflags &= ~TS_INSTRING;
182 break;
183 case '\a':
184 t->t_stateflags &= ~TS_INSTRING;
185 return;
186 default:
187 return;
188 }
189 }
190
191 switch (c) {
192 case '\0':
193 break;
194 case '\a':
195 teken_subr_bell(t);
196 break;
197 case '\b':
198 teken_subr_backspace(t);
199 break;
200 case '\n':
201 case '\x0B':
202 teken_subr_newline(t);
203 break;
204 case '\x0C':
205 teken_subr_newpage(t);
206 break;
207 case '\x0E':
208 if (t->t_stateflags & TS_CONS25)
209 t->t_nextstate(t, c);
210 else
211 t->t_curscs = 1;
212 break;
213 case '\x0F':
214 if (t->t_stateflags & TS_CONS25)
215 t->t_nextstate(t, c);
216 else
217 t->t_curscs = 0;
218 break;
219 case '\r':
220 teken_subr_carriage_return(t);
221 break;
222 case '\t':
223 teken_subr_horizontal_tab(t);
224 break;
225 default:
226 t->t_nextstate(t, c);
227 break;
228 }
229
230 /* Post-processing assertions. */
231 teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
232 teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
233 teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
234 teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
235 teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
236 teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
237 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
238 teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
239 /* Origin region has to be window size or the same as scrollreg. */
240 teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
241 t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
242 (t->t_originreg.ts_begin == 0 &&
243 t->t_originreg.ts_end == t->t_winsize.tp_row));
244}
245
246static void
247teken_input_byte(teken_t *t, unsigned char c)
248{
249
250 /*
251 * UTF-8 handling.
252 */
253 if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
254 /* One-byte sequence. */
255 t->t_utf8_left = 0;
256 teken_input_char(t, c);
257 } else if ((c & 0xe0) == 0xc0) {
258 /* Two-byte sequence. */
259 t->t_utf8_left = 1;
260 t->t_utf8_partial = c & 0x1f;
261 } else if ((c & 0xf0) == 0xe0) {
262 /* Three-byte sequence. */
263 t->t_utf8_left = 2;
264 t->t_utf8_partial = c & 0x0f;
265 } else if ((c & 0xf8) == 0xf0) {
266 /* Four-byte sequence. */
267 t->t_utf8_left = 3;
268 t->t_utf8_partial = c & 0x07;
269 } else if ((c & 0xc0) == 0x80) {
270 if (t->t_utf8_left == 0)
271 return;
272 t->t_utf8_left--;
273 t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
274 if (t->t_utf8_left == 0) {
275 teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
276 teken_input_char(t, t->t_utf8_partial);
277 }
278 }
279}
280
281void
282teken_input(teken_t *t, const void *buf, size_t len)
283{
284 const char *c = buf;
285
286 while (len-- > 0)
287 teken_input_byte(t, *c++);
288}
289
290const teken_pos_t *
291teken_get_cursor(teken_t *t)
292{
293
294 return (&t->t_cursor);
295}
296
297void
298teken_set_cursor(teken_t *t, const teken_pos_t *p)
299{
300
301 /* XXX: bounds checking with originreg! */
302 teken_assert(p->tp_row < t->t_winsize.tp_row);
303 teken_assert(p->tp_col < t->t_winsize.tp_col);
304
305 t->t_cursor = *p;
306}
307
308const teken_attr_t *
309teken_get_curattr(teken_t *t)
310{
311
312 return (&t->t_curattr);
313}
314
315void
316teken_set_curattr(teken_t *t, const teken_attr_t *a)
317{
318
319 t->t_curattr = *a;
320}
321
322const teken_attr_t *
323teken_get_defattr(teken_t *t)
324{
325
326 return (&t->t_defattr);
327}
328
329void
330teken_set_defattr(teken_t *t, const teken_attr_t *a)
331{
332
333 t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
334}
335
336const teken_pos_t *
337teken_get_winsize(teken_t *t)
338{
339
340 return (&t->t_winsize);
341}
342
343static void
344teken_trim_cursor_pos(teken_t *t, const teken_pos_t *new)
345{
346 const teken_pos_t *cur;
347
348 cur = &t->t_winsize;
349
350 if (cur->tp_row < new->tp_row || cur->tp_col < new->tp_col)
351 return;
352 if (t->t_cursor.tp_row >= new->tp_row)
353 t->t_cursor.tp_row = new->tp_row - 1;
354 if (t->t_cursor.tp_col >= new->tp_col)
355 t->t_cursor.tp_col = new->tp_col - 1;
356}
357
358void
359teken_set_winsize(teken_t *t, const teken_pos_t *p)
360{
361
362 teken_trim_cursor_pos(t, p);
363 t->t_winsize = *p;
364 teken_subr_do_reset(t);
365}
366
367void
368teken_set_winsize_noreset(teken_t *t, const teken_pos_t *p)
369{
370
371 teken_trim_cursor_pos(t, p);
372 t->t_winsize = *p;
373 teken_subr_do_resize(t);
374}
375
376void
377teken_set_8bit(teken_t *t)
378{
379
380 t->t_stateflags |= TS_8BIT;
381}
382
383void
384teken_set_cons25(teken_t *t)
385{
386
387 t->t_stateflags |= TS_CONS25;
388}
389
390/*
391 * State machine.
392 */
393
394static void
395teken_state_switch(teken_t *t, teken_state_t *s)
396{
397
398 t->t_nextstate = s;
399 t->t_curnum = 0;
400 t->t_stateflags |= TS_FIRSTDIGIT;
401}
402
403static int
404teken_state_numbers(teken_t *t, teken_char_t c)
405{
406
407 teken_assert(t->t_curnum < T_NUMSIZE);
408
409 if (c >= '0' && c <= '9') {
408 /*
409 * Don't do math with the default value of 1 when a
410 * custom number is inserted.
411 */
412 if (t->t_stateflags & TS_FIRSTDIGIT) {
410 if (t->t_stateflags & TS_FIRSTDIGIT) {
411 /* First digit. */
413 t->t_stateflags &= ~TS_FIRSTDIGIT;
412 t->t_stateflags &= ~TS_FIRSTDIGIT;
414 t->t_nums[t->t_curnum] = 0;
415 } else {
416 t->t_nums[t->t_curnum] *= 10;
413 t->t_nums[t->t_curnum] = c - '0';
414 } else if (t->t_nums[t->t_curnum] < USHRT_MAX) {
415 /*
416 * Screen positions are stored as unsigned
417 * shorts. There is no need to continue parsing
418 * input once the value exceeds USHRT_MAX. It
419 * would only allow for integer overflows when
420 * performing arithmetic on the cursor position.
421 */
422 t->t_nums[t->t_curnum] =
423 t->t_nums[t->t_curnum] * 10 + c - '0';
417 }
424 }
418
419 t->t_nums[t->t_curnum] += c - '0';
420 return (1);
421 } else if (c == ';') {
422 if (t->t_stateflags & TS_FIRSTDIGIT)
423 t->t_nums[t->t_curnum] = 0;
424
425 /* Only allow a limited set of arguments. */
426 if (++t->t_curnum == T_NUMSIZE) {
427 teken_state_switch(t, teken_state_init);
428 return (1);
429 }
430
431 t->t_stateflags |= TS_FIRSTDIGIT;
432 return (1);
433 } else {
434 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
435 /* Finish off the last empty argument. */
436 t->t_nums[t->t_curnum] = 0;
437 t->t_curnum++;
438 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
439 /* Also count the last argument. */
440 t->t_curnum++;
441 }
442 }
443
444 return (0);
445}
446
447teken_color_t
448teken_256to8(teken_color_t c)
449{
450 unsigned int r, g, b;
451
452 if (c < 16) {
453 /* Traditional color indices. */
454 return (c % 8);
455 } else if (c >= 244) {
456 /* Upper grayscale colors. */
457 return (TC_WHITE);
458 } else if (c >= 232) {
459 /* Lower grayscale colors. */
460 return (TC_BLACK);
461 }
462
463 /* Convert to RGB. */
464 c -= 16;
465 b = c % 6;
466 g = (c / 6) % 6;
467 r = c / 36;
468
469 if (r < g) {
470 /* Possibly green. */
471 if (g < b)
472 return (TC_BLUE);
473 else if (g > b)
474 return (TC_GREEN);
475 else
476 return (TC_CYAN);
477 } else if (r > g) {
478 /* Possibly red. */
479 if (r < b)
480 return (TC_BLUE);
481 else if (r > b)
482 return (TC_RED);
483 else
484 return (TC_MAGENTA);
485 } else {
486 /* Possibly brown. */
487 if (g < b)
488 return (TC_BLUE);
489 else if (g > b)
490 return (TC_BROWN);
491 else if (r < 3)
492 return (TC_BLACK);
493 else
494 return (TC_WHITE);
495 }
496}
497
498static const char * const special_strings_cons25[] = {
499 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
500 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
501
502 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
503 [TKEY_INSERT] = "\x1B[L", [TKEY_DELETE] = "\x7F",
504 [TKEY_PAGE_UP] = "\x1B[I", [TKEY_PAGE_DOWN] = "\x1B[G",
505
506 [TKEY_F1] = "\x1B[M", [TKEY_F2] = "\x1B[N",
507 [TKEY_F3] = "\x1B[O", [TKEY_F4] = "\x1B[P",
508 [TKEY_F5] = "\x1B[Q", [TKEY_F6] = "\x1B[R",
509 [TKEY_F7] = "\x1B[S", [TKEY_F8] = "\x1B[T",
510 [TKEY_F9] = "\x1B[U", [TKEY_F10] = "\x1B[V",
511 [TKEY_F11] = "\x1B[W", [TKEY_F12] = "\x1B[X",
512};
513
514static const char * const special_strings_ckeys[] = {
515 [TKEY_UP] = "\x1BOA", [TKEY_DOWN] = "\x1BOB",
516 [TKEY_LEFT] = "\x1BOD", [TKEY_RIGHT] = "\x1BOC",
517
518 [TKEY_HOME] = "\x1BOH", [TKEY_END] = "\x1BOF",
519};
520
521static const char * const special_strings_normal[] = {
522 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
523 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
524
525 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
526 [TKEY_INSERT] = "\x1B[2~", [TKEY_DELETE] = "\x1B[3~",
527 [TKEY_PAGE_UP] = "\x1B[5~", [TKEY_PAGE_DOWN] = "\x1B[6~",
528
529 [TKEY_F1] = "\x1BOP", [TKEY_F2] = "\x1BOQ",
530 [TKEY_F3] = "\x1BOR", [TKEY_F4] = "\x1BOS",
531 [TKEY_F5] = "\x1B[15~", [TKEY_F6] = "\x1B[17~",
532 [TKEY_F7] = "\x1B[18~", [TKEY_F8] = "\x1B[19~",
533 [TKEY_F9] = "\x1B[20~", [TKEY_F10] = "\x1B[21~",
534 [TKEY_F11] = "\x1B[23~", [TKEY_F12] = "\x1B[24~",
535};
536
537const char *
538teken_get_sequence(teken_t *t, unsigned int k)
539{
540
541 /* Cons25 mode. */
542 if (t->t_stateflags & TS_CONS25 &&
543 k < sizeof special_strings_cons25 / sizeof(char *))
544 return (special_strings_cons25[k]);
545
546 /* Cursor keys mode. */
547 if (t->t_stateflags & TS_CURSORKEYS &&
548 k < sizeof special_strings_ckeys / sizeof(char *))
549 return (special_strings_ckeys[k]);
550
551 /* Default xterm sequences. */
552 if (k < sizeof special_strings_normal / sizeof(char *))
553 return (special_strings_normal[k]);
554
555 return (NULL);
556}
557
558#include "teken_state.h"
425 return (1);
426 } else if (c == ';') {
427 if (t->t_stateflags & TS_FIRSTDIGIT)
428 t->t_nums[t->t_curnum] = 0;
429
430 /* Only allow a limited set of arguments. */
431 if (++t->t_curnum == T_NUMSIZE) {
432 teken_state_switch(t, teken_state_init);
433 return (1);
434 }
435
436 t->t_stateflags |= TS_FIRSTDIGIT;
437 return (1);
438 } else {
439 if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
440 /* Finish off the last empty argument. */
441 t->t_nums[t->t_curnum] = 0;
442 t->t_curnum++;
443 } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
444 /* Also count the last argument. */
445 t->t_curnum++;
446 }
447 }
448
449 return (0);
450}
451
452teken_color_t
453teken_256to8(teken_color_t c)
454{
455 unsigned int r, g, b;
456
457 if (c < 16) {
458 /* Traditional color indices. */
459 return (c % 8);
460 } else if (c >= 244) {
461 /* Upper grayscale colors. */
462 return (TC_WHITE);
463 } else if (c >= 232) {
464 /* Lower grayscale colors. */
465 return (TC_BLACK);
466 }
467
468 /* Convert to RGB. */
469 c -= 16;
470 b = c % 6;
471 g = (c / 6) % 6;
472 r = c / 36;
473
474 if (r < g) {
475 /* Possibly green. */
476 if (g < b)
477 return (TC_BLUE);
478 else if (g > b)
479 return (TC_GREEN);
480 else
481 return (TC_CYAN);
482 } else if (r > g) {
483 /* Possibly red. */
484 if (r < b)
485 return (TC_BLUE);
486 else if (r > b)
487 return (TC_RED);
488 else
489 return (TC_MAGENTA);
490 } else {
491 /* Possibly brown. */
492 if (g < b)
493 return (TC_BLUE);
494 else if (g > b)
495 return (TC_BROWN);
496 else if (r < 3)
497 return (TC_BLACK);
498 else
499 return (TC_WHITE);
500 }
501}
502
503static const char * const special_strings_cons25[] = {
504 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
505 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
506
507 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
508 [TKEY_INSERT] = "\x1B[L", [TKEY_DELETE] = "\x7F",
509 [TKEY_PAGE_UP] = "\x1B[I", [TKEY_PAGE_DOWN] = "\x1B[G",
510
511 [TKEY_F1] = "\x1B[M", [TKEY_F2] = "\x1B[N",
512 [TKEY_F3] = "\x1B[O", [TKEY_F4] = "\x1B[P",
513 [TKEY_F5] = "\x1B[Q", [TKEY_F6] = "\x1B[R",
514 [TKEY_F7] = "\x1B[S", [TKEY_F8] = "\x1B[T",
515 [TKEY_F9] = "\x1B[U", [TKEY_F10] = "\x1B[V",
516 [TKEY_F11] = "\x1B[W", [TKEY_F12] = "\x1B[X",
517};
518
519static const char * const special_strings_ckeys[] = {
520 [TKEY_UP] = "\x1BOA", [TKEY_DOWN] = "\x1BOB",
521 [TKEY_LEFT] = "\x1BOD", [TKEY_RIGHT] = "\x1BOC",
522
523 [TKEY_HOME] = "\x1BOH", [TKEY_END] = "\x1BOF",
524};
525
526static const char * const special_strings_normal[] = {
527 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B",
528 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C",
529
530 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F",
531 [TKEY_INSERT] = "\x1B[2~", [TKEY_DELETE] = "\x1B[3~",
532 [TKEY_PAGE_UP] = "\x1B[5~", [TKEY_PAGE_DOWN] = "\x1B[6~",
533
534 [TKEY_F1] = "\x1BOP", [TKEY_F2] = "\x1BOQ",
535 [TKEY_F3] = "\x1BOR", [TKEY_F4] = "\x1BOS",
536 [TKEY_F5] = "\x1B[15~", [TKEY_F6] = "\x1B[17~",
537 [TKEY_F7] = "\x1B[18~", [TKEY_F8] = "\x1B[19~",
538 [TKEY_F9] = "\x1B[20~", [TKEY_F10] = "\x1B[21~",
539 [TKEY_F11] = "\x1B[23~", [TKEY_F12] = "\x1B[24~",
540};
541
542const char *
543teken_get_sequence(teken_t *t, unsigned int k)
544{
545
546 /* Cons25 mode. */
547 if (t->t_stateflags & TS_CONS25 &&
548 k < sizeof special_strings_cons25 / sizeof(char *))
549 return (special_strings_cons25[k]);
550
551 /* Cursor keys mode. */
552 if (t->t_stateflags & TS_CURSORKEYS &&
553 k < sizeof special_strings_ckeys / sizeof(char *))
554 return (special_strings_ckeys[k]);
555
556 /* Default xterm sequences. */
557 if (k < sizeof special_strings_normal / sizeof(char *))
558 return (special_strings_normal[k]);
559
560 return (NULL);
561}
562
563#include "teken_state.h"