teken.c revision 206141
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: head/sys/teken/teken.c 206141 2010-04-03 17:22:28Z ed $
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#define	teken_printf(x,...)
36186681Sed#else /* !(__FreeBSD__ && _KERNEL) */
37186681Sed#include <sys/types.h>
38186681Sed#include <assert.h>
39206141Sed#include <stdint.h>
40186681Sed#include <stdio.h>
41186681Sed#include <string.h>
42186681Sed#define	teken_assert(x)		assert(x)
43186681Sed#define	teken_printf(x,...)	do { \
44186681Sed	if (df != NULL) \
45186681Sed		fprintf(df, x, ## __VA_ARGS__); \
46186681Sed} while (0)
47186681Sed/* debug messages */
48186681Sedstatic FILE *df;
49186681Sed#endif /* __FreeBSD__ && _KERNEL */
50186681Sed
51186681Sed/* Private flags for t_stateflags. */
52199171Sed#define	TS_FIRSTDIGIT	0x0001	/* First numeric digit in escape sequence. */
53199171Sed#define	TS_INSERT	0x0002	/* Insert mode. */
54199171Sed#define	TS_AUTOWRAP	0x0004	/* Autowrap. */
55199171Sed#define	TS_ORIGIN	0x0008	/* Origin mode. */
56199171Sed#define	TS_WRAPPED	0x0010	/* Next character should be printed on col 0. */
57199171Sed#define	TS_8BIT		0x0020	/* UTF-8 disabled. */
58199171Sed#define	TS_CONS25	0x0040	/* cons25 emulation. */
59199171Sed#define	TS_INSTRING	0x0080	/* Inside string. */
60199171Sed#define	TS_CURSORKEYS	0x0100	/* Cursor keys mode. */
61186681Sed
62186681Sed/* Character that blanks a cell. */
63186681Sed#define	BLANK	' '
64186681Sed
65197470Sed#include "teken.h"
66197470Sed#include "teken_wcwidth.h"
67197470Sed#include "teken_scs.h"
68197470Sed
69186681Sedstatic teken_state_t	teken_state_init;
70186681Sed
71186681Sed/*
72186681Sed * Wrappers for hooks.
73186681Sed */
74186681Sed
75186681Sedstatic inline void
76186681Sedteken_funcs_bell(teken_t *t)
77186681Sed{
78186681Sed
79186681Sed	t->t_funcs->tf_bell(t->t_softc);
80186681Sed}
81186681Sed
82186681Sedstatic inline void
83186681Sedteken_funcs_cursor(teken_t *t)
84186681Sed{
85186681Sed
86186681Sed	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
87186681Sed	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
88186681Sed
89186681Sed	t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
90186681Sed}
91186681Sed
92186681Sedstatic inline void
93186681Sedteken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
94186681Sed    const teken_attr_t *a)
95186681Sed{
96186681Sed
97186681Sed	teken_assert(p->tp_row < t->t_winsize.tp_row);
98186681Sed	teken_assert(p->tp_col < t->t_winsize.tp_col);
99186681Sed
100186681Sed	t->t_funcs->tf_putchar(t->t_softc, p, c, a);
101186681Sed}
102186681Sed
103186681Sedstatic inline void
104186681Sedteken_funcs_fill(teken_t *t, const teken_rect_t *r,
105186681Sed    const teken_char_t c, const teken_attr_t *a)
106186681Sed{
107186681Sed
108186681Sed	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
109186681Sed	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
110186681Sed	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
111186681Sed	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
112186681Sed
113186681Sed	t->t_funcs->tf_fill(t->t_softc, r, c, a);
114186681Sed}
115186681Sed
116186681Sedstatic inline void
117186681Sedteken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
118186681Sed{
119186681Sed
120186681Sed	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
121186681Sed	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
122186681Sed	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
123186681Sed	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
124186681Sed	teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
125186681Sed	teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
126186681Sed
127186681Sed	t->t_funcs->tf_copy(t->t_softc, r, p);
128186681Sed}
129186681Sed
130186681Sedstatic inline void
131193184Sedteken_funcs_param(teken_t *t, int cmd, unsigned int value)
132186681Sed{
133186681Sed
134186681Sed	t->t_funcs->tf_param(t->t_softc, cmd, value);
135186681Sed}
136186681Sed
137186681Sedstatic inline void
138186681Sedteken_funcs_respond(teken_t *t, const void *buf, size_t len)
139186681Sed{
140186681Sed
141186681Sed	t->t_funcs->tf_respond(t->t_softc, buf, len);
142186681Sed}
143186681Sed
144186681Sed#include "teken_subr.h"
145186681Sed#include "teken_subr_compat.h"
146186681Sed
147186681Sed/*
148186681Sed * Programming interface.
149186681Sed */
150186681Sed
151186681Sedvoid
152186681Sedteken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
153186681Sed{
154186681Sed	teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
155186681Sed
156186681Sed#if !(defined(__FreeBSD__) && defined(_KERNEL))
157186681Sed	df = fopen("teken.log", "w");
158186681Sed	if (df != NULL)
159186681Sed		setvbuf(df, NULL, _IOLBF, BUFSIZ);
160186681Sed#endif /* !(__FreeBSD__ && _KERNEL) */
161186681Sed
162186681Sed	t->t_funcs = tf;
163186681Sed	t->t_softc = softc;
164186681Sed
165186681Sed	t->t_nextstate = teken_state_init;
166197117Sed	t->t_stateflags = 0;
167197117Sed	t->t_utf8_left = 0;
168186681Sed
169186681Sed	t->t_defattr.ta_format = 0;
170186681Sed	t->t_defattr.ta_fgcolor = TC_WHITE;
171186681Sed	t->t_defattr.ta_bgcolor = TC_BLACK;
172186681Sed	teken_subr_do_reset(t);
173186681Sed
174186681Sed	teken_set_winsize(t, &tp);
175186681Sed}
176186681Sed
177186681Sedstatic void
178186681Sedteken_input_char(teken_t *t, teken_char_t c)
179186681Sed{
180186681Sed
181197853Sed	/*
182197853Sed	 * There is no support for DCS and OSC.  Just discard strings
183197853Sed	 * until we receive characters that may indicate string
184197853Sed	 * termination.
185197853Sed	 */
186197853Sed	if (t->t_stateflags & TS_INSTRING) {
187197853Sed		switch (c) {
188197853Sed		case '\x1B':
189197853Sed			t->t_stateflags &= ~TS_INSTRING;
190197853Sed			break;
191197853Sed		case '\a':
192197853Sed			t->t_stateflags &= ~TS_INSTRING;
193197853Sed			return;
194197853Sed		default:
195197853Sed			return;
196197853Sed		}
197197853Sed	}
198197853Sed
199186681Sed	switch (c) {
200186681Sed	case '\0':
201186681Sed		break;
202186681Sed	case '\a':
203186681Sed		teken_subr_bell(t);
204186681Sed		break;
205186681Sed	case '\b':
206186681Sed		teken_subr_backspace(t);
207186681Sed		break;
208186681Sed	case '\n':
209186681Sed	case '\x0B':
210186681Sed		teken_subr_newline(t);
211186681Sed		break;
212186798Sed	case '\x0C':
213186798Sed		teken_subr_newpage(t);
214186798Sed		break;
215187469Sed	case '\x0E':
216197117Sed		if (t->t_stateflags & TS_CONS25)
217197117Sed			t->t_nextstate(t, c);
218197117Sed		else
219197520Sed			t->t_curscs = 1;
220187469Sed		break;
221187469Sed	case '\x0F':
222197117Sed		if (t->t_stateflags & TS_CONS25)
223197117Sed			t->t_nextstate(t, c);
224197117Sed		else
225197520Sed			t->t_curscs = 0;
226187469Sed		break;
227186681Sed	case '\r':
228186681Sed		teken_subr_carriage_return(t);
229186681Sed		break;
230186681Sed	case '\t':
231186681Sed		teken_subr_horizontal_tab(t);
232186681Sed		break;
233186681Sed	default:
234186681Sed		t->t_nextstate(t, c);
235186681Sed		break;
236186681Sed	}
237186681Sed
238186681Sed	/* Post-processing assertions. */
239186681Sed	teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
240186681Sed	teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
241186681Sed	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
242186681Sed	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
243186681Sed	teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
244186681Sed	teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
245186681Sed	teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
246186681Sed	teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
247186681Sed	/* Origin region has to be window size or the same as scrollreg. */
248186681Sed	teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
249186681Sed	    t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
250186681Sed	    (t->t_originreg.ts_begin == 0 &&
251186681Sed	    t->t_originreg.ts_end == t->t_winsize.tp_row));
252186681Sed}
253186681Sed
254186681Sedstatic void
255186681Sedteken_input_byte(teken_t *t, unsigned char c)
256186681Sed{
257186681Sed
258186681Sed	/*
259186681Sed	 * UTF-8 handling.
260186681Sed	 */
261197117Sed	if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
262186681Sed		/* One-byte sequence. */
263186681Sed		t->t_utf8_left = 0;
264186681Sed		teken_input_char(t, c);
265186681Sed	} else if ((c & 0xe0) == 0xc0) {
266186681Sed		/* Two-byte sequence. */
267186681Sed		t->t_utf8_left = 1;
268186681Sed		t->t_utf8_partial = c & 0x1f;
269186681Sed	} else if ((c & 0xf0) == 0xe0) {
270186681Sed		/* Three-byte sequence. */
271186681Sed		t->t_utf8_left = 2;
272186681Sed		t->t_utf8_partial = c & 0x0f;
273186681Sed	} else if ((c & 0xf8) == 0xf0) {
274186681Sed		/* Four-byte sequence. */
275186681Sed		t->t_utf8_left = 3;
276186681Sed		t->t_utf8_partial = c & 0x07;
277186681Sed	} else if ((c & 0xc0) == 0x80) {
278186681Sed		if (t->t_utf8_left == 0)
279186681Sed			return;
280186681Sed		t->t_utf8_left--;
281186681Sed		t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
282186681Sed		if (t->t_utf8_left == 0) {
283194293Sed			teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
284186681Sed			teken_input_char(t, t->t_utf8_partial);
285186681Sed		}
286186681Sed	}
287186681Sed}
288186681Sed
289186681Sedvoid
290186681Sedteken_input(teken_t *t, const void *buf, size_t len)
291186681Sed{
292186681Sed	const char *c = buf;
293186681Sed
294186681Sed	while (len-- > 0)
295186681Sed		teken_input_byte(t, *c++);
296186681Sed}
297186681Sed
298197117Sedconst teken_pos_t *
299197117Sedteken_get_cursor(teken_t *t)
300197117Sed{
301197117Sed
302197117Sed	return (&t->t_cursor);
303197117Sed}
304197117Sed
305186681Sedvoid
306186681Sedteken_set_cursor(teken_t *t, const teken_pos_t *p)
307186681Sed{
308186681Sed
309186681Sed	/* XXX: bounds checking with originreg! */
310186681Sed	teken_assert(p->tp_row < t->t_winsize.tp_row);
311186681Sed	teken_assert(p->tp_col < t->t_winsize.tp_col);
312186681Sed
313186681Sed	t->t_cursor = *p;
314186681Sed}
315186681Sed
316188391Sedconst teken_attr_t *
317188391Sedteken_get_curattr(teken_t *t)
318188391Sed{
319188391Sed
320188391Sed	return (&t->t_curattr);
321188391Sed}
322188391Sed
323189617Sedvoid
324189617Sedteken_set_curattr(teken_t *t, const teken_attr_t *a)
325189617Sed{
326189617Sed
327189617Sed	t->t_curattr = *a;
328189617Sed}
329189617Sed
330188391Sedconst teken_attr_t *
331188391Sedteken_get_defattr(teken_t *t)
332188391Sed{
333188391Sed
334188391Sed	return (&t->t_defattr);
335188391Sed}
336188391Sed
337186681Sedvoid
338186681Sedteken_set_defattr(teken_t *t, const teken_attr_t *a)
339186681Sed{
340186681Sed
341186681Sed	t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
342186681Sed}
343186681Sed
344197117Sedconst teken_pos_t *
345197117Sedteken_get_winsize(teken_t *t)
346197117Sed{
347197117Sed
348197117Sed	return (&t->t_winsize);
349197117Sed}
350197117Sed
351186681Sedvoid
352186681Sedteken_set_winsize(teken_t *t, const teken_pos_t *p)
353186681Sed{
354186681Sed
355186681Sed	t->t_winsize = *p;
356197114Sed	teken_subr_do_reset(t);
357186681Sed}
358186681Sed
359197115Sedvoid
360197115Sedteken_set_8bit(teken_t *t)
361197115Sed{
362197115Sed
363197117Sed	t->t_stateflags |= TS_8BIT;
364197115Sed}
365197115Sed
366197117Sedvoid
367197117Sedteken_set_cons25(teken_t *t)
368197117Sed{
369197117Sed
370197117Sed	t->t_stateflags |= TS_CONS25;
371197117Sed}
372197117Sed
373186681Sed/*
374186681Sed * State machine.
375186681Sed */
376186681Sed
377186681Sedstatic void
378186681Sedteken_state_switch(teken_t *t, teken_state_t *s)
379186681Sed{
380186681Sed
381186681Sed	t->t_nextstate = s;
382186681Sed	t->t_curnum = 0;
383186681Sed	t->t_stateflags |= TS_FIRSTDIGIT;
384186681Sed}
385186681Sed
386186681Sedstatic int
387186681Sedteken_state_numbers(teken_t *t, teken_char_t c)
388186681Sed{
389186681Sed
390186681Sed	teken_assert(t->t_curnum < T_NUMSIZE);
391186681Sed
392186681Sed	if (c >= '0' && c <= '9') {
393186681Sed		/*
394186681Sed		 * Don't do math with the default value of 1 when a
395186681Sed		 * custom number is inserted.
396186681Sed		 */
397186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT) {
398186681Sed			t->t_stateflags &= ~TS_FIRSTDIGIT;
399186681Sed			t->t_nums[t->t_curnum] = 0;
400186681Sed		} else {
401186681Sed			t->t_nums[t->t_curnum] *= 10;
402186681Sed		}
403186681Sed
404186681Sed		t->t_nums[t->t_curnum] += c - '0';
405186681Sed		return (1);
406186681Sed	} else if (c == ';') {
407186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT)
408186681Sed			t->t_nums[t->t_curnum] = 0;
409186681Sed
410186681Sed		/* Only allow a limited set of arguments. */
411186681Sed		if (++t->t_curnum == T_NUMSIZE) {
412186681Sed			teken_state_switch(t, teken_state_init);
413186681Sed			return (1);
414186681Sed		}
415186681Sed
416186681Sed		t->t_stateflags |= TS_FIRSTDIGIT;
417186681Sed		return (1);
418186681Sed	} else {
419186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
420186681Sed			/* Finish off the last empty argument. */
421186681Sed			t->t_nums[t->t_curnum] = 0;
422186681Sed			t->t_curnum++;
423186681Sed		} else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
424186681Sed			/* Also count the last argument. */
425186681Sed			t->t_curnum++;
426186681Sed		}
427186681Sed	}
428186681Sed
429186681Sed	return (0);
430186681Sed}
431186681Sed
432197522Sedteken_color_t
433197522Sedteken_256to8(teken_color_t c)
434197522Sed{
435197522Sed	unsigned int r, g, b;
436197522Sed
437197522Sed	if (c < 16) {
438197522Sed		/* Traditional color indices. */
439197522Sed		return (c % 8);
440197522Sed	} else if (c >= 244) {
441197522Sed		/* Upper grayscale colors. */
442197522Sed		return (TC_WHITE);
443197522Sed	} else if (c >= 232) {
444197522Sed		/* Lower grayscale colors. */
445197522Sed		return (TC_BLACK);
446197522Sed	}
447197522Sed
448197522Sed	/* Convert to RGB. */
449197522Sed	c -= 16;
450197522Sed	b = c % 6;
451197522Sed	g = (c / 6) % 6;
452197522Sed	r = c / 36;
453197522Sed
454197522Sed	if (r < g) {
455197522Sed		/* Possibly green. */
456197522Sed		if (g < b)
457197522Sed			return (TC_BLUE);
458197522Sed		else if (g > b)
459197522Sed			return (TC_GREEN);
460197522Sed		else
461197522Sed			return (TC_CYAN);
462197522Sed	} else if (r > g) {
463197522Sed		/* Possibly red. */
464197522Sed		if (r < b)
465197522Sed			return (TC_BLUE);
466197522Sed		else if (r > b)
467197522Sed			return (TC_RED);
468197522Sed		else
469197522Sed			return (TC_MAGENTA);
470197522Sed	} else {
471197522Sed		/* Possibly brown. */
472197522Sed		if (g < b)
473197522Sed			return (TC_BLUE);
474197522Sed		else if (g > b)
475197522Sed			return (TC_BROWN);
476197522Sed		else if (r < 3)
477197522Sed			return (TC_BLACK);
478197522Sed		else
479197522Sed			return (TC_WHITE);
480197522Sed	}
481197522Sed}
482197522Sed
483199171Sedstatic const char * const special_strings_cons25[] = {
484199171Sed	[TKEY_UP] = "\x1B[A",		[TKEY_DOWN] = "\x1B[B",
485199171Sed	[TKEY_LEFT] = "\x1B[D",		[TKEY_RIGHT] = "\x1B[C",
486199171Sed
487199175Sed	[TKEY_HOME] = "\x1B[H",		[TKEY_END] = "\x1B[F",
488199171Sed	[TKEY_INSERT] = "\x1B[L",	[TKEY_DELETE] = "\x7F",
489199171Sed	[TKEY_PAGE_UP] = "\x1B[I",	[TKEY_PAGE_DOWN] = "\x1B[G",
490199171Sed
491199171Sed	[TKEY_F1] = "\x1B[M",		[TKEY_F2] = "\x1B[N",
492199171Sed	[TKEY_F3] = "\x1B[O",		[TKEY_F4] = "\x1B[P",
493199171Sed	[TKEY_F5] = "\x1B[Q",		[TKEY_F6] = "\x1B[R",
494199171Sed	[TKEY_F7] = "\x1B[S",		[TKEY_F8] = "\x1B[T",
495199171Sed	[TKEY_F9] = "\x1B[U",		[TKEY_F10] = "\x1B[V",
496199171Sed	[TKEY_F11] = "\x1B[W",		[TKEY_F12] = "\x1B[X",
497199171Sed};
498199171Sed
499199171Sedstatic const char * const special_strings_ckeys[] = {
500199171Sed	[TKEY_UP] = "\x1BOA",		[TKEY_DOWN] = "\x1BOB",
501199171Sed	[TKEY_LEFT] = "\x1BOD",		[TKEY_RIGHT] = "\x1BOC",
502199171Sed
503199171Sed	[TKEY_HOME] = "\x1BOH",		[TKEY_END] = "\x1BOF",
504199171Sed};
505199171Sed
506199171Sedstatic const char * const special_strings_normal[] = {
507199171Sed	[TKEY_UP] = "\x1B[A",		[TKEY_DOWN] = "\x1B[B",
508199171Sed	[TKEY_LEFT] = "\x1B[D",		[TKEY_RIGHT] = "\x1B[C",
509199171Sed
510199175Sed	[TKEY_HOME] = "\x1B[H",		[TKEY_END] = "\x1B[F",
511199171Sed	[TKEY_INSERT] = "\x1B[2~",	[TKEY_DELETE] = "\x1B[3~",
512199171Sed	[TKEY_PAGE_UP] = "\x1B[5~",	[TKEY_PAGE_DOWN] = "\x1B[6~",
513199171Sed
514199171Sed	[TKEY_F1] = "\x1BOP",		[TKEY_F2] = "\x1BOQ",
515199171Sed	[TKEY_F3] = "\x1BOR",		[TKEY_F4] = "\x1BOS",
516199171Sed	[TKEY_F5] = "\x1B[15~",		[TKEY_F6] = "\x1B[17~",
517199171Sed	[TKEY_F7] = "\x1B[18~",		[TKEY_F8] = "\x1B[19~",
518199171Sed	[TKEY_F9] = "\x1B[20~",		[TKEY_F10] = "\x1B[21~",
519199171Sed	[TKEY_F11] = "\x1B[23~",	[TKEY_F12] = "\x1B[24~",
520199171Sed};
521199171Sed
522199171Sedconst char *
523199171Sedteken_get_sequence(teken_t *t, unsigned int k)
524199171Sed{
525199171Sed
526199171Sed	/* Cons25 mode. */
527199171Sed	if (t->t_stateflags & TS_CONS25 &&
528199171Sed	    k < sizeof special_strings_cons25 / sizeof(char *))
529199171Sed		return (special_strings_cons25[k]);
530199171Sed
531199171Sed	/* Cursor keys mode. */
532199171Sed	if (t->t_stateflags & TS_CURSORKEYS &&
533199171Sed	    k < sizeof special_strings_ckeys / sizeof(char *))
534199171Sed		return (special_strings_ckeys[k]);
535199171Sed
536199171Sed	/* Default xterm sequences. */
537199171Sed	if (k < sizeof special_strings_normal / sizeof(char *))
538199171Sed		return (special_strings_normal[k]);
539199171Sed
540199171Sed	return (NULL);
541199171Sed}
542199171Sed
543186681Sed#include "teken_state.h"
544