1186681Sed/*-
2186681Sed * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3186681Sed * All rights reserved.
4186681Sed *
5186681Sed * Redistribution and use in source and binary forms, with or without
6186681Sed * modification, are permitted provided that the following conditions
7186681Sed * are met:
8186681Sed * 1. Redistributions of source code must retain the above copyright
9186681Sed *    notice, this list of conditions and the following disclaimer.
10186681Sed * 2. Redistributions in binary form must reproduce the above copyright
11186681Sed *    notice, this list of conditions and the following disclaimer in the
12186681Sed *    documentation and/or other materials provided with the distribution.
13186681Sed *
14186681Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15186681Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16186681Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17186681Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18186681Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19186681Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20186681Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21186681Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22186681Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23186681Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24186681Sed * SUCH DAMAGE.
25186681Sed *
26186681Sed * $FreeBSD$
27186681Sed */
28186681Sed
29186681Sed#include <sys/cdefs.h>
30186681Sed#if defined(__FreeBSD__) && defined(_KERNEL)
31186681Sed#include <sys/param.h>
32186681Sed#include <sys/lock.h>
33186681Sed#include <sys/systm.h>
34186681Sed#define	teken_assert(x)		MPASS(x)
35186681Sed#else /* !(__FreeBSD__ && _KERNEL) */
36186681Sed#include <sys/types.h>
37186681Sed#include <assert.h>
38206141Sed#include <stdint.h>
39186681Sed#include <stdio.h>
40186681Sed#include <string.h>
41186681Sed#define	teken_assert(x)		assert(x)
42186681Sed#endif /* __FreeBSD__ && _KERNEL */
43186681Sed
44221698Sed/* debug messages */
45221698Sed#define	teken_printf(x,...)
46221698Sed
47186681Sed/* Private flags for t_stateflags. */
48199171Sed#define	TS_FIRSTDIGIT	0x0001	/* First numeric digit in escape sequence. */
49199171Sed#define	TS_INSERT	0x0002	/* Insert mode. */
50199171Sed#define	TS_AUTOWRAP	0x0004	/* Autowrap. */
51199171Sed#define	TS_ORIGIN	0x0008	/* Origin mode. */
52199171Sed#define	TS_WRAPPED	0x0010	/* Next character should be printed on col 0. */
53199171Sed#define	TS_8BIT		0x0020	/* UTF-8 disabled. */
54199171Sed#define	TS_CONS25	0x0040	/* cons25 emulation. */
55199171Sed#define	TS_INSTRING	0x0080	/* Inside string. */
56199171Sed#define	TS_CURSORKEYS	0x0100	/* Cursor keys mode. */
57186681Sed
58186681Sed/* Character that blanks a cell. */
59186681Sed#define	BLANK	' '
60186681Sed
61197470Sed#include "teken.h"
62197470Sed#include "teken_wcwidth.h"
63197470Sed#include "teken_scs.h"
64197470Sed
65186681Sedstatic teken_state_t	teken_state_init;
66186681Sed
67186681Sed/*
68186681Sed * Wrappers for hooks.
69186681Sed */
70186681Sed
71186681Sedstatic inline void
72186681Sedteken_funcs_bell(teken_t *t)
73186681Sed{
74186681Sed
75186681Sed	t->t_funcs->tf_bell(t->t_softc);
76186681Sed}
77186681Sed
78186681Sedstatic inline void
79186681Sedteken_funcs_cursor(teken_t *t)
80186681Sed{
81186681Sed
82186681Sed	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
83186681Sed	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
84186681Sed
85186681Sed	t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
86186681Sed}
87186681Sed
88186681Sedstatic inline void
89186681Sedteken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
90186681Sed    const teken_attr_t *a)
91186681Sed{
92186681Sed
93186681Sed	teken_assert(p->tp_row < t->t_winsize.tp_row);
94186681Sed	teken_assert(p->tp_col < t->t_winsize.tp_col);
95186681Sed
96186681Sed	t->t_funcs->tf_putchar(t->t_softc, p, c, a);
97186681Sed}
98186681Sed
99186681Sedstatic inline void
100186681Sedteken_funcs_fill(teken_t *t, const teken_rect_t *r,
101186681Sed    const teken_char_t c, const teken_attr_t *a)
102186681Sed{
103186681Sed
104186681Sed	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
105186681Sed	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
106186681Sed	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
107186681Sed	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
108186681Sed
109186681Sed	t->t_funcs->tf_fill(t->t_softc, r, c, a);
110186681Sed}
111186681Sed
112186681Sedstatic inline void
113186681Sedteken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
114186681Sed{
115186681Sed
116186681Sed	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
117186681Sed	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
118186681Sed	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
119186681Sed	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
120186681Sed	teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
121186681Sed	teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
122186681Sed
123186681Sed	t->t_funcs->tf_copy(t->t_softc, r, p);
124186681Sed}
125186681Sed
126186681Sedstatic inline void
127193184Sedteken_funcs_param(teken_t *t, int cmd, unsigned int value)
128186681Sed{
129186681Sed
130186681Sed	t->t_funcs->tf_param(t->t_softc, cmd, value);
131186681Sed}
132186681Sed
133186681Sedstatic inline void
134186681Sedteken_funcs_respond(teken_t *t, const void *buf, size_t len)
135186681Sed{
136186681Sed
137186681Sed	t->t_funcs->tf_respond(t->t_softc, buf, len);
138186681Sed}
139186681Sed
140186681Sed#include "teken_subr.h"
141186681Sed#include "teken_subr_compat.h"
142186681Sed
143186681Sed/*
144186681Sed * Programming interface.
145186681Sed */
146186681Sed
147186681Sedvoid
148186681Sedteken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
149186681Sed{
150186681Sed	teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
151186681Sed
152186681Sed	t->t_funcs = tf;
153186681Sed	t->t_softc = softc;
154186681Sed
155186681Sed	t->t_nextstate = teken_state_init;
156197117Sed	t->t_stateflags = 0;
157197117Sed	t->t_utf8_left = 0;
158186681Sed
159186681Sed	t->t_defattr.ta_format = 0;
160186681Sed	t->t_defattr.ta_fgcolor = TC_WHITE;
161186681Sed	t->t_defattr.ta_bgcolor = TC_BLACK;
162186681Sed	teken_subr_do_reset(t);
163186681Sed
164186681Sed	teken_set_winsize(t, &tp);
165186681Sed}
166186681Sed
167186681Sedstatic void
168186681Sedteken_input_char(teken_t *t, teken_char_t c)
169186681Sed{
170186681Sed
171197853Sed	/*
172197853Sed	 * There is no support for DCS and OSC.  Just discard strings
173197853Sed	 * until we receive characters that may indicate string
174197853Sed	 * termination.
175197853Sed	 */
176197853Sed	if (t->t_stateflags & TS_INSTRING) {
177197853Sed		switch (c) {
178197853Sed		case '\x1B':
179197853Sed			t->t_stateflags &= ~TS_INSTRING;
180197853Sed			break;
181197853Sed		case '\a':
182197853Sed			t->t_stateflags &= ~TS_INSTRING;
183197853Sed			return;
184197853Sed		default:
185197853Sed			return;
186197853Sed		}
187197853Sed	}
188197853Sed
189186681Sed	switch (c) {
190186681Sed	case '\0':
191186681Sed		break;
192186681Sed	case '\a':
193186681Sed		teken_subr_bell(t);
194186681Sed		break;
195186681Sed	case '\b':
196186681Sed		teken_subr_backspace(t);
197186681Sed		break;
198186681Sed	case '\n':
199186681Sed	case '\x0B':
200186681Sed		teken_subr_newline(t);
201186681Sed		break;
202186798Sed	case '\x0C':
203186798Sed		teken_subr_newpage(t);
204186798Sed		break;
205187469Sed	case '\x0E':
206197117Sed		if (t->t_stateflags & TS_CONS25)
207197117Sed			t->t_nextstate(t, c);
208197117Sed		else
209197520Sed			t->t_curscs = 1;
210187469Sed		break;
211187469Sed	case '\x0F':
212197117Sed		if (t->t_stateflags & TS_CONS25)
213197117Sed			t->t_nextstate(t, c);
214197117Sed		else
215197520Sed			t->t_curscs = 0;
216187469Sed		break;
217186681Sed	case '\r':
218186681Sed		teken_subr_carriage_return(t);
219186681Sed		break;
220186681Sed	case '\t':
221186681Sed		teken_subr_horizontal_tab(t);
222186681Sed		break;
223186681Sed	default:
224186681Sed		t->t_nextstate(t, c);
225186681Sed		break;
226186681Sed	}
227186681Sed
228186681Sed	/* Post-processing assertions. */
229186681Sed	teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
230186681Sed	teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
231186681Sed	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
232186681Sed	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
233186681Sed	teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
234186681Sed	teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
235186681Sed	teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
236186681Sed	teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
237186681Sed	/* Origin region has to be window size or the same as scrollreg. */
238186681Sed	teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
239186681Sed	    t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
240186681Sed	    (t->t_originreg.ts_begin == 0 &&
241186681Sed	    t->t_originreg.ts_end == t->t_winsize.tp_row));
242186681Sed}
243186681Sed
244186681Sedstatic void
245186681Sedteken_input_byte(teken_t *t, unsigned char c)
246186681Sed{
247186681Sed
248186681Sed	/*
249186681Sed	 * UTF-8 handling.
250186681Sed	 */
251197117Sed	if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
252186681Sed		/* One-byte sequence. */
253186681Sed		t->t_utf8_left = 0;
254186681Sed		teken_input_char(t, c);
255186681Sed	} else if ((c & 0xe0) == 0xc0) {
256186681Sed		/* Two-byte sequence. */
257186681Sed		t->t_utf8_left = 1;
258186681Sed		t->t_utf8_partial = c & 0x1f;
259186681Sed	} else if ((c & 0xf0) == 0xe0) {
260186681Sed		/* Three-byte sequence. */
261186681Sed		t->t_utf8_left = 2;
262186681Sed		t->t_utf8_partial = c & 0x0f;
263186681Sed	} else if ((c & 0xf8) == 0xf0) {
264186681Sed		/* Four-byte sequence. */
265186681Sed		t->t_utf8_left = 3;
266186681Sed		t->t_utf8_partial = c & 0x07;
267186681Sed	} else if ((c & 0xc0) == 0x80) {
268186681Sed		if (t->t_utf8_left == 0)
269186681Sed			return;
270186681Sed		t->t_utf8_left--;
271186681Sed		t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
272186681Sed		if (t->t_utf8_left == 0) {
273194293Sed			teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
274186681Sed			teken_input_char(t, t->t_utf8_partial);
275186681Sed		}
276186681Sed	}
277186681Sed}
278186681Sed
279186681Sedvoid
280186681Sedteken_input(teken_t *t, const void *buf, size_t len)
281186681Sed{
282186681Sed	const char *c = buf;
283186681Sed
284186681Sed	while (len-- > 0)
285186681Sed		teken_input_byte(t, *c++);
286186681Sed}
287186681Sed
288197117Sedconst teken_pos_t *
289197117Sedteken_get_cursor(teken_t *t)
290197117Sed{
291197117Sed
292197117Sed	return (&t->t_cursor);
293197117Sed}
294197117Sed
295186681Sedvoid
296186681Sedteken_set_cursor(teken_t *t, const teken_pos_t *p)
297186681Sed{
298186681Sed
299186681Sed	/* XXX: bounds checking with originreg! */
300186681Sed	teken_assert(p->tp_row < t->t_winsize.tp_row);
301186681Sed	teken_assert(p->tp_col < t->t_winsize.tp_col);
302186681Sed
303186681Sed	t->t_cursor = *p;
304186681Sed}
305186681Sed
306188391Sedconst teken_attr_t *
307188391Sedteken_get_curattr(teken_t *t)
308188391Sed{
309188391Sed
310188391Sed	return (&t->t_curattr);
311188391Sed}
312188391Sed
313189617Sedvoid
314189617Sedteken_set_curattr(teken_t *t, const teken_attr_t *a)
315189617Sed{
316189617Sed
317189617Sed	t->t_curattr = *a;
318189617Sed}
319189617Sed
320188391Sedconst teken_attr_t *
321188391Sedteken_get_defattr(teken_t *t)
322188391Sed{
323188391Sed
324188391Sed	return (&t->t_defattr);
325188391Sed}
326188391Sed
327186681Sedvoid
328186681Sedteken_set_defattr(teken_t *t, const teken_attr_t *a)
329186681Sed{
330186681Sed
331186681Sed	t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
332186681Sed}
333186681Sed
334197117Sedconst teken_pos_t *
335197117Sedteken_get_winsize(teken_t *t)
336197117Sed{
337197117Sed
338197117Sed	return (&t->t_winsize);
339197117Sed}
340197117Sed
341262861Sjhbstatic void
342262861Sjhbteken_trim_cursor_pos(teken_t *t, const teken_pos_t *new)
343262861Sjhb{
344262861Sjhb	const teken_pos_t *cur;
345262861Sjhb
346262861Sjhb	cur = &t->t_winsize;
347262861Sjhb
348262861Sjhb	if (cur->tp_row < new->tp_row || cur->tp_col < new->tp_col)
349262861Sjhb		return;
350262861Sjhb	if (t->t_cursor.tp_row >= new->tp_row)
351262861Sjhb		t->t_cursor.tp_row = new->tp_row - 1;
352262861Sjhb	if (t->t_cursor.tp_col >= new->tp_col)
353262861Sjhb		t->t_cursor.tp_col = new->tp_col - 1;
354262861Sjhb}
355262861Sjhb
356186681Sedvoid
357186681Sedteken_set_winsize(teken_t *t, const teken_pos_t *p)
358186681Sed{
359186681Sed
360262861Sjhb	teken_trim_cursor_pos(t, p);
361186681Sed	t->t_winsize = *p;
362197114Sed	teken_subr_do_reset(t);
363186681Sed}
364186681Sed
365197115Sedvoid
366262861Sjhbteken_set_winsize_noreset(teken_t *t, const teken_pos_t *p)
367262861Sjhb{
368262861Sjhb
369262861Sjhb	teken_trim_cursor_pos(t, p);
370262861Sjhb	t->t_winsize = *p;
371262861Sjhb	teken_subr_do_resize(t);
372262861Sjhb}
373262861Sjhb
374262861Sjhbvoid
375197115Sedteken_set_8bit(teken_t *t)
376197115Sed{
377197115Sed
378197117Sed	t->t_stateflags |= TS_8BIT;
379197115Sed}
380197115Sed
381197117Sedvoid
382197117Sedteken_set_cons25(teken_t *t)
383197117Sed{
384197117Sed
385197117Sed	t->t_stateflags |= TS_CONS25;
386197117Sed}
387197117Sed
388186681Sed/*
389186681Sed * State machine.
390186681Sed */
391186681Sed
392186681Sedstatic void
393186681Sedteken_state_switch(teken_t *t, teken_state_t *s)
394186681Sed{
395186681Sed
396186681Sed	t->t_nextstate = s;
397186681Sed	t->t_curnum = 0;
398186681Sed	t->t_stateflags |= TS_FIRSTDIGIT;
399186681Sed}
400186681Sed
401186681Sedstatic int
402186681Sedteken_state_numbers(teken_t *t, teken_char_t c)
403186681Sed{
404186681Sed
405186681Sed	teken_assert(t->t_curnum < T_NUMSIZE);
406186681Sed
407186681Sed	if (c >= '0' && c <= '9') {
408186681Sed		/*
409186681Sed		 * Don't do math with the default value of 1 when a
410186681Sed		 * custom number is inserted.
411186681Sed		 */
412186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT) {
413186681Sed			t->t_stateflags &= ~TS_FIRSTDIGIT;
414186681Sed			t->t_nums[t->t_curnum] = 0;
415186681Sed		} else {
416186681Sed			t->t_nums[t->t_curnum] *= 10;
417186681Sed		}
418186681Sed
419186681Sed		t->t_nums[t->t_curnum] += c - '0';
420186681Sed		return (1);
421186681Sed	} else if (c == ';') {
422186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT)
423186681Sed			t->t_nums[t->t_curnum] = 0;
424186681Sed
425186681Sed		/* Only allow a limited set of arguments. */
426186681Sed		if (++t->t_curnum == T_NUMSIZE) {
427186681Sed			teken_state_switch(t, teken_state_init);
428186681Sed			return (1);
429186681Sed		}
430186681Sed
431186681Sed		t->t_stateflags |= TS_FIRSTDIGIT;
432186681Sed		return (1);
433186681Sed	} else {
434186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
435186681Sed			/* Finish off the last empty argument. */
436186681Sed			t->t_nums[t->t_curnum] = 0;
437186681Sed			t->t_curnum++;
438186681Sed		} else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
439186681Sed			/* Also count the last argument. */
440186681Sed			t->t_curnum++;
441186681Sed		}
442186681Sed	}
443186681Sed
444186681Sed	return (0);
445186681Sed}
446186681Sed
447197522Sedteken_color_t
448197522Sedteken_256to8(teken_color_t c)
449197522Sed{
450197522Sed	unsigned int r, g, b;
451197522Sed
452197522Sed	if (c < 16) {
453197522Sed		/* Traditional color indices. */
454197522Sed		return (c % 8);
455197522Sed	} else if (c >= 244) {
456197522Sed		/* Upper grayscale colors. */
457197522Sed		return (TC_WHITE);
458197522Sed	} else if (c >= 232) {
459197522Sed		/* Lower grayscale colors. */
460197522Sed		return (TC_BLACK);
461197522Sed	}
462197522Sed
463197522Sed	/* Convert to RGB. */
464197522Sed	c -= 16;
465197522Sed	b = c % 6;
466197522Sed	g = (c / 6) % 6;
467197522Sed	r = c / 36;
468197522Sed
469197522Sed	if (r < g) {
470197522Sed		/* Possibly green. */
471197522Sed		if (g < b)
472197522Sed			return (TC_BLUE);
473197522Sed		else if (g > b)
474197522Sed			return (TC_GREEN);
475197522Sed		else
476197522Sed			return (TC_CYAN);
477197522Sed	} else if (r > g) {
478197522Sed		/* Possibly red. */
479197522Sed		if (r < b)
480197522Sed			return (TC_BLUE);
481197522Sed		else if (r > b)
482197522Sed			return (TC_RED);
483197522Sed		else
484197522Sed			return (TC_MAGENTA);
485197522Sed	} else {
486197522Sed		/* Possibly brown. */
487197522Sed		if (g < b)
488197522Sed			return (TC_BLUE);
489197522Sed		else if (g > b)
490197522Sed			return (TC_BROWN);
491197522Sed		else if (r < 3)
492197522Sed			return (TC_BLACK);
493197522Sed		else
494197522Sed			return (TC_WHITE);
495197522Sed	}
496197522Sed}
497197522Sed
498199171Sedstatic const char * const special_strings_cons25[] = {
499199171Sed	[TKEY_UP] = "\x1B[A",		[TKEY_DOWN] = "\x1B[B",
500199171Sed	[TKEY_LEFT] = "\x1B[D",		[TKEY_RIGHT] = "\x1B[C",
501199171Sed
502199175Sed	[TKEY_HOME] = "\x1B[H",		[TKEY_END] = "\x1B[F",
503199171Sed	[TKEY_INSERT] = "\x1B[L",	[TKEY_DELETE] = "\x7F",
504199171Sed	[TKEY_PAGE_UP] = "\x1B[I",	[TKEY_PAGE_DOWN] = "\x1B[G",
505199171Sed
506199171Sed	[TKEY_F1] = "\x1B[M",		[TKEY_F2] = "\x1B[N",
507199171Sed	[TKEY_F3] = "\x1B[O",		[TKEY_F4] = "\x1B[P",
508199171Sed	[TKEY_F5] = "\x1B[Q",		[TKEY_F6] = "\x1B[R",
509199171Sed	[TKEY_F7] = "\x1B[S",		[TKEY_F8] = "\x1B[T",
510199171Sed	[TKEY_F9] = "\x1B[U",		[TKEY_F10] = "\x1B[V",
511199171Sed	[TKEY_F11] = "\x1B[W",		[TKEY_F12] = "\x1B[X",
512199171Sed};
513199171Sed
514199171Sedstatic const char * const special_strings_ckeys[] = {
515199171Sed	[TKEY_UP] = "\x1BOA",		[TKEY_DOWN] = "\x1BOB",
516199171Sed	[TKEY_LEFT] = "\x1BOD",		[TKEY_RIGHT] = "\x1BOC",
517199171Sed
518199171Sed	[TKEY_HOME] = "\x1BOH",		[TKEY_END] = "\x1BOF",
519199171Sed};
520199171Sed
521199171Sedstatic const char * const special_strings_normal[] = {
522199171Sed	[TKEY_UP] = "\x1B[A",		[TKEY_DOWN] = "\x1B[B",
523199171Sed	[TKEY_LEFT] = "\x1B[D",		[TKEY_RIGHT] = "\x1B[C",
524199171Sed
525199175Sed	[TKEY_HOME] = "\x1B[H",		[TKEY_END] = "\x1B[F",
526199171Sed	[TKEY_INSERT] = "\x1B[2~",	[TKEY_DELETE] = "\x1B[3~",
527199171Sed	[TKEY_PAGE_UP] = "\x1B[5~",	[TKEY_PAGE_DOWN] = "\x1B[6~",
528199171Sed
529199171Sed	[TKEY_F1] = "\x1BOP",		[TKEY_F2] = "\x1BOQ",
530199171Sed	[TKEY_F3] = "\x1BOR",		[TKEY_F4] = "\x1BOS",
531199171Sed	[TKEY_F5] = "\x1B[15~",		[TKEY_F6] = "\x1B[17~",
532199171Sed	[TKEY_F7] = "\x1B[18~",		[TKEY_F8] = "\x1B[19~",
533199171Sed	[TKEY_F9] = "\x1B[20~",		[TKEY_F10] = "\x1B[21~",
534199171Sed	[TKEY_F11] = "\x1B[23~",	[TKEY_F12] = "\x1B[24~",
535199171Sed};
536199171Sed
537199171Sedconst char *
538199171Sedteken_get_sequence(teken_t *t, unsigned int k)
539199171Sed{
540199171Sed
541199171Sed	/* Cons25 mode. */
542199171Sed	if (t->t_stateflags & TS_CONS25 &&
543199171Sed	    k < sizeof special_strings_cons25 / sizeof(char *))
544199171Sed		return (special_strings_cons25[k]);
545199171Sed
546199171Sed	/* Cursor keys mode. */
547199171Sed	if (t->t_stateflags & TS_CURSORKEYS &&
548199171Sed	    k < sizeof special_strings_ckeys / sizeof(char *))
549199171Sed		return (special_strings_ckeys[k]);
550199171Sed
551199171Sed	/* Default xterm sequences. */
552199171Sed	if (k < sizeof special_strings_normal / sizeof(char *))
553199171Sed		return (special_strings_normal[k]);
554223574Sed
555199171Sed	return (NULL);
556199171Sed}
557199171Sed
558186681Sed#include "teken_state.h"
559