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: releng/11.0/sys/teken/teken.c 286827 2015-08-16 13:59:11Z ed $
27186681Sed */
28186681Sed
29186681Sed#include <sys/cdefs.h>
30186681Sed#if defined(__FreeBSD__) && defined(_KERNEL)
31186681Sed#include <sys/param.h>
32286798Sed#include <sys/limits.h>
33186681Sed#include <sys/lock.h>
34186681Sed#include <sys/systm.h>
35186681Sed#define	teken_assert(x)		MPASS(x)
36186681Sed#else /* !(__FreeBSD__ && _KERNEL) */
37186681Sed#include <sys/types.h>
38186681Sed#include <assert.h>
39286798Sed#include <limits.h>
40206141Sed#include <stdint.h>
41186681Sed#include <stdio.h>
42186681Sed#include <string.h>
43186681Sed#define	teken_assert(x)		assert(x)
44186681Sed#endif /* __FreeBSD__ && _KERNEL */
45186681Sed
46221698Sed/* debug messages */
47221698Sed#define	teken_printf(x,...)
48221698Sed
49186681Sed/* Private flags for t_stateflags. */
50199171Sed#define	TS_FIRSTDIGIT	0x0001	/* First numeric digit in escape sequence. */
51199171Sed#define	TS_INSERT	0x0002	/* Insert mode. */
52199171Sed#define	TS_AUTOWRAP	0x0004	/* Autowrap. */
53199171Sed#define	TS_ORIGIN	0x0008	/* Origin mode. */
54199171Sed#define	TS_WRAPPED	0x0010	/* Next character should be printed on col 0. */
55199171Sed#define	TS_8BIT		0x0020	/* UTF-8 disabled. */
56199171Sed#define	TS_CONS25	0x0040	/* cons25 emulation. */
57199171Sed#define	TS_INSTRING	0x0080	/* Inside string. */
58199171Sed#define	TS_CURSORKEYS	0x0100	/* Cursor keys mode. */
59186681Sed
60186681Sed/* Character that blanks a cell. */
61186681Sed#define	BLANK	' '
62186681Sed
63197470Sed#include "teken.h"
64197470Sed#include "teken_wcwidth.h"
65197470Sed#include "teken_scs.h"
66197470Sed
67186681Sedstatic teken_state_t	teken_state_init;
68186681Sed
69186681Sed/*
70186681Sed * Wrappers for hooks.
71186681Sed */
72186681Sed
73186681Sedstatic inline void
74186681Sedteken_funcs_bell(teken_t *t)
75186681Sed{
76186681Sed
77186681Sed	t->t_funcs->tf_bell(t->t_softc);
78186681Sed}
79186681Sed
80186681Sedstatic inline void
81186681Sedteken_funcs_cursor(teken_t *t)
82186681Sed{
83186681Sed
84186681Sed	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
85186681Sed	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
86186681Sed
87186681Sed	t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor);
88186681Sed}
89186681Sed
90186681Sedstatic inline void
91186681Sedteken_funcs_putchar(teken_t *t, const teken_pos_t *p, teken_char_t c,
92186681Sed    const teken_attr_t *a)
93186681Sed{
94186681Sed
95186681Sed	teken_assert(p->tp_row < t->t_winsize.tp_row);
96186681Sed	teken_assert(p->tp_col < t->t_winsize.tp_col);
97186681Sed
98186681Sed	t->t_funcs->tf_putchar(t->t_softc, p, c, a);
99186681Sed}
100186681Sed
101186681Sedstatic inline void
102186681Sedteken_funcs_fill(teken_t *t, const teken_rect_t *r,
103186681Sed    const teken_char_t c, const teken_attr_t *a)
104186681Sed{
105186681Sed
106186681Sed	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
107186681Sed	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
108186681Sed	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
109186681Sed	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
110186681Sed
111186681Sed	t->t_funcs->tf_fill(t->t_softc, r, c, a);
112186681Sed}
113186681Sed
114186681Sedstatic inline void
115186681Sedteken_funcs_copy(teken_t *t, const teken_rect_t *r, const teken_pos_t *p)
116186681Sed{
117186681Sed
118186681Sed	teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row);
119186681Sed	teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row);
120186681Sed	teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col);
121186681Sed	teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col);
122186681Sed	teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row);
123186681Sed	teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col);
124186681Sed
125186681Sed	t->t_funcs->tf_copy(t->t_softc, r, p);
126186681Sed}
127186681Sed
128186681Sedstatic inline void
129193184Sedteken_funcs_param(teken_t *t, int cmd, unsigned int value)
130186681Sed{
131186681Sed
132186681Sed	t->t_funcs->tf_param(t->t_softc, cmd, value);
133186681Sed}
134186681Sed
135186681Sedstatic inline void
136186681Sedteken_funcs_respond(teken_t *t, const void *buf, size_t len)
137186681Sed{
138186681Sed
139186681Sed	t->t_funcs->tf_respond(t->t_softc, buf, len);
140186681Sed}
141186681Sed
142186681Sed#include "teken_subr.h"
143186681Sed#include "teken_subr_compat.h"
144186681Sed
145186681Sed/*
146186681Sed * Programming interface.
147186681Sed */
148186681Sed
149186681Sedvoid
150186681Sedteken_init(teken_t *t, const teken_funcs_t *tf, void *softc)
151186681Sed{
152186681Sed	teken_pos_t tp = { .tp_row = 24, .tp_col = 80 };
153186681Sed
154186681Sed	t->t_funcs = tf;
155186681Sed	t->t_softc = softc;
156186681Sed
157186681Sed	t->t_nextstate = teken_state_init;
158197117Sed	t->t_stateflags = 0;
159197117Sed	t->t_utf8_left = 0;
160186681Sed
161186681Sed	t->t_defattr.ta_format = 0;
162186681Sed	t->t_defattr.ta_fgcolor = TC_WHITE;
163186681Sed	t->t_defattr.ta_bgcolor = TC_BLACK;
164186681Sed	teken_subr_do_reset(t);
165186681Sed
166186681Sed	teken_set_winsize(t, &tp);
167186681Sed}
168186681Sed
169186681Sedstatic void
170186681Sedteken_input_char(teken_t *t, teken_char_t c)
171186681Sed{
172186681Sed
173197853Sed	/*
174197853Sed	 * There is no support for DCS and OSC.  Just discard strings
175197853Sed	 * until we receive characters that may indicate string
176197853Sed	 * termination.
177197853Sed	 */
178197853Sed	if (t->t_stateflags & TS_INSTRING) {
179197853Sed		switch (c) {
180197853Sed		case '\x1B':
181197853Sed			t->t_stateflags &= ~TS_INSTRING;
182197853Sed			break;
183197853Sed		case '\a':
184197853Sed			t->t_stateflags &= ~TS_INSTRING;
185197853Sed			return;
186197853Sed		default:
187197853Sed			return;
188197853Sed		}
189197853Sed	}
190197853Sed
191186681Sed	switch (c) {
192186681Sed	case '\0':
193186681Sed		break;
194186681Sed	case '\a':
195186681Sed		teken_subr_bell(t);
196186681Sed		break;
197186681Sed	case '\b':
198186681Sed		teken_subr_backspace(t);
199186681Sed		break;
200186681Sed	case '\n':
201186681Sed	case '\x0B':
202186681Sed		teken_subr_newline(t);
203186681Sed		break;
204186798Sed	case '\x0C':
205186798Sed		teken_subr_newpage(t);
206186798Sed		break;
207187469Sed	case '\x0E':
208197117Sed		if (t->t_stateflags & TS_CONS25)
209197117Sed			t->t_nextstate(t, c);
210197117Sed		else
211197520Sed			t->t_curscs = 1;
212187469Sed		break;
213187469Sed	case '\x0F':
214197117Sed		if (t->t_stateflags & TS_CONS25)
215197117Sed			t->t_nextstate(t, c);
216197117Sed		else
217197520Sed			t->t_curscs = 0;
218187469Sed		break;
219186681Sed	case '\r':
220186681Sed		teken_subr_carriage_return(t);
221186681Sed		break;
222186681Sed	case '\t':
223186681Sed		teken_subr_horizontal_tab(t);
224186681Sed		break;
225186681Sed	default:
226186681Sed		t->t_nextstate(t, c);
227186681Sed		break;
228186681Sed	}
229186681Sed
230186681Sed	/* Post-processing assertions. */
231186681Sed	teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin);
232186681Sed	teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end);
233186681Sed	teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row);
234186681Sed	teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col);
235186681Sed	teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row);
236186681Sed	teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col);
237186681Sed	teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
238186681Sed	teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end);
239186681Sed	/* Origin region has to be window size or the same as scrollreg. */
240186681Sed	teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin &&
241186681Sed	    t->t_originreg.ts_end == t->t_scrollreg.ts_end) ||
242186681Sed	    (t->t_originreg.ts_begin == 0 &&
243186681Sed	    t->t_originreg.ts_end == t->t_winsize.tp_row));
244186681Sed}
245186681Sed
246186681Sedstatic void
247186681Sedteken_input_byte(teken_t *t, unsigned char c)
248186681Sed{
249186681Sed
250186681Sed	/*
251186681Sed	 * UTF-8 handling.
252186681Sed	 */
253197117Sed	if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) {
254186681Sed		/* One-byte sequence. */
255186681Sed		t->t_utf8_left = 0;
256186681Sed		teken_input_char(t, c);
257186681Sed	} else if ((c & 0xe0) == 0xc0) {
258186681Sed		/* Two-byte sequence. */
259186681Sed		t->t_utf8_left = 1;
260186681Sed		t->t_utf8_partial = c & 0x1f;
261186681Sed	} else if ((c & 0xf0) == 0xe0) {
262186681Sed		/* Three-byte sequence. */
263186681Sed		t->t_utf8_left = 2;
264186681Sed		t->t_utf8_partial = c & 0x0f;
265186681Sed	} else if ((c & 0xf8) == 0xf0) {
266186681Sed		/* Four-byte sequence. */
267186681Sed		t->t_utf8_left = 3;
268186681Sed		t->t_utf8_partial = c & 0x07;
269186681Sed	} else if ((c & 0xc0) == 0x80) {
270186681Sed		if (t->t_utf8_left == 0)
271186681Sed			return;
272186681Sed		t->t_utf8_left--;
273186681Sed		t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f);
274186681Sed		if (t->t_utf8_left == 0) {
275194293Sed			teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial);
276186681Sed			teken_input_char(t, t->t_utf8_partial);
277186681Sed		}
278186681Sed	}
279186681Sed}
280186681Sed
281186681Sedvoid
282186681Sedteken_input(teken_t *t, const void *buf, size_t len)
283186681Sed{
284186681Sed	const char *c = buf;
285186681Sed
286186681Sed	while (len-- > 0)
287186681Sed		teken_input_byte(t, *c++);
288186681Sed}
289186681Sed
290197117Sedconst teken_pos_t *
291197117Sedteken_get_cursor(teken_t *t)
292197117Sed{
293197117Sed
294197117Sed	return (&t->t_cursor);
295197117Sed}
296197117Sed
297186681Sedvoid
298186681Sedteken_set_cursor(teken_t *t, const teken_pos_t *p)
299186681Sed{
300186681Sed
301186681Sed	/* XXX: bounds checking with originreg! */
302186681Sed	teken_assert(p->tp_row < t->t_winsize.tp_row);
303186681Sed	teken_assert(p->tp_col < t->t_winsize.tp_col);
304186681Sed
305186681Sed	t->t_cursor = *p;
306186681Sed}
307186681Sed
308188391Sedconst teken_attr_t *
309188391Sedteken_get_curattr(teken_t *t)
310188391Sed{
311188391Sed
312188391Sed	return (&t->t_curattr);
313188391Sed}
314188391Sed
315189617Sedvoid
316189617Sedteken_set_curattr(teken_t *t, const teken_attr_t *a)
317189617Sed{
318189617Sed
319189617Sed	t->t_curattr = *a;
320189617Sed}
321189617Sed
322188391Sedconst teken_attr_t *
323188391Sedteken_get_defattr(teken_t *t)
324188391Sed{
325188391Sed
326188391Sed	return (&t->t_defattr);
327188391Sed}
328188391Sed
329186681Sedvoid
330186681Sedteken_set_defattr(teken_t *t, const teken_attr_t *a)
331186681Sed{
332186681Sed
333186681Sed	t->t_curattr = t->t_saved_curattr = t->t_defattr = *a;
334186681Sed}
335186681Sed
336197117Sedconst teken_pos_t *
337197117Sedteken_get_winsize(teken_t *t)
338197117Sed{
339197117Sed
340197117Sed	return (&t->t_winsize);
341197117Sed}
342197117Sed
343261547Sraystatic void
344261551Srayteken_trim_cursor_pos(teken_t *t, const teken_pos_t *new)
345261547Sray{
346261547Sray	const teken_pos_t *cur;
347261547Sray
348261547Sray	cur = &t->t_winsize;
349261547Sray
350261547Sray	if (cur->tp_row < new->tp_row || cur->tp_col < new->tp_col)
351261547Sray		return;
352261547Sray	if (t->t_cursor.tp_row >= new->tp_row)
353261547Sray		t->t_cursor.tp_row = new->tp_row - 1;
354261547Sray	if (t->t_cursor.tp_col >= new->tp_col)
355261547Sray		t->t_cursor.tp_col = new->tp_col - 1;
356261547Sray}
357261547Sray
358186681Sedvoid
359186681Sedteken_set_winsize(teken_t *t, const teken_pos_t *p)
360186681Sed{
361186681Sed
362261551Sray	teken_trim_cursor_pos(t, p);
363186681Sed	t->t_winsize = *p;
364197114Sed	teken_subr_do_reset(t);
365186681Sed}
366186681Sed
367197115Sedvoid
368259016Srayteken_set_winsize_noreset(teken_t *t, const teken_pos_t *p)
369259016Sray{
370259016Sray
371261551Sray	teken_trim_cursor_pos(t, p);
372259016Sray	t->t_winsize = *p;
373259016Sray	teken_subr_do_resize(t);
374259016Sray}
375259016Sray
376259016Srayvoid
377197115Sedteken_set_8bit(teken_t *t)
378197115Sed{
379197115Sed
380197117Sed	t->t_stateflags |= TS_8BIT;
381197115Sed}
382197115Sed
383197117Sedvoid
384197117Sedteken_set_cons25(teken_t *t)
385197117Sed{
386197117Sed
387197117Sed	t->t_stateflags |= TS_CONS25;
388197117Sed}
389197117Sed
390186681Sed/*
391186681Sed * State machine.
392186681Sed */
393186681Sed
394186681Sedstatic void
395186681Sedteken_state_switch(teken_t *t, teken_state_t *s)
396186681Sed{
397186681Sed
398186681Sed	t->t_nextstate = s;
399186681Sed	t->t_curnum = 0;
400186681Sed	t->t_stateflags |= TS_FIRSTDIGIT;
401186681Sed}
402186681Sed
403186681Sedstatic int
404186681Sedteken_state_numbers(teken_t *t, teken_char_t c)
405186681Sed{
406186681Sed
407186681Sed	teken_assert(t->t_curnum < T_NUMSIZE);
408186681Sed
409186681Sed	if (c >= '0' && c <= '9') {
410186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT) {
411286798Sed			/* First digit. */
412186681Sed			t->t_stateflags &= ~TS_FIRSTDIGIT;
413286798Sed			t->t_nums[t->t_curnum] = c - '0';
414286827Sed		} else if (t->t_nums[t->t_curnum] < UINT_MAX / 100) {
415286798Sed			/*
416286827Sed			 * There is no need to continue parsing input
417286827Sed			 * once the value exceeds the size of the
418286827Sed			 * terminal. It would only allow for integer
419286827Sed			 * overflows when performing arithmetic on the
420286827Sed			 * cursor position.
421286827Sed			 *
422286827Sed			 * Ignore any further digits if the value is
423286827Sed			 * already UINT_MAX / 100.
424286798Sed			 */
425286798Sed			t->t_nums[t->t_curnum] =
426286798Sed			    t->t_nums[t->t_curnum] * 10 + c - '0';
427186681Sed		}
428186681Sed		return (1);
429186681Sed	} else if (c == ';') {
430186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT)
431186681Sed			t->t_nums[t->t_curnum] = 0;
432186681Sed
433186681Sed		/* Only allow a limited set of arguments. */
434186681Sed		if (++t->t_curnum == T_NUMSIZE) {
435186681Sed			teken_state_switch(t, teken_state_init);
436186681Sed			return (1);
437186681Sed		}
438186681Sed
439186681Sed		t->t_stateflags |= TS_FIRSTDIGIT;
440186681Sed		return (1);
441186681Sed	} else {
442186681Sed		if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) {
443186681Sed			/* Finish off the last empty argument. */
444186681Sed			t->t_nums[t->t_curnum] = 0;
445186681Sed			t->t_curnum++;
446186681Sed		} else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) {
447186681Sed			/* Also count the last argument. */
448186681Sed			t->t_curnum++;
449186681Sed		}
450186681Sed	}
451186681Sed
452186681Sed	return (0);
453186681Sed}
454186681Sed
455197522Sedteken_color_t
456197522Sedteken_256to8(teken_color_t c)
457197522Sed{
458197522Sed	unsigned int r, g, b;
459197522Sed
460197522Sed	if (c < 16) {
461197522Sed		/* Traditional color indices. */
462197522Sed		return (c % 8);
463197522Sed	} else if (c >= 244) {
464197522Sed		/* Upper grayscale colors. */
465197522Sed		return (TC_WHITE);
466197522Sed	} else if (c >= 232) {
467197522Sed		/* Lower grayscale colors. */
468197522Sed		return (TC_BLACK);
469197522Sed	}
470197522Sed
471197522Sed	/* Convert to RGB. */
472197522Sed	c -= 16;
473197522Sed	b = c % 6;
474197522Sed	g = (c / 6) % 6;
475197522Sed	r = c / 36;
476197522Sed
477197522Sed	if (r < g) {
478197522Sed		/* Possibly green. */
479197522Sed		if (g < b)
480197522Sed			return (TC_BLUE);
481197522Sed		else if (g > b)
482197522Sed			return (TC_GREEN);
483197522Sed		else
484197522Sed			return (TC_CYAN);
485197522Sed	} else if (r > g) {
486197522Sed		/* Possibly red. */
487197522Sed		if (r < b)
488197522Sed			return (TC_BLUE);
489197522Sed		else if (r > b)
490197522Sed			return (TC_RED);
491197522Sed		else
492197522Sed			return (TC_MAGENTA);
493197522Sed	} else {
494197522Sed		/* Possibly brown. */
495197522Sed		if (g < b)
496197522Sed			return (TC_BLUE);
497197522Sed		else if (g > b)
498197522Sed			return (TC_BROWN);
499197522Sed		else if (r < 3)
500197522Sed			return (TC_BLACK);
501197522Sed		else
502197522Sed			return (TC_WHITE);
503197522Sed	}
504197522Sed}
505197522Sed
506199171Sedstatic const char * const special_strings_cons25[] = {
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[L",	[TKEY_DELETE] = "\x7F",
512199171Sed	[TKEY_PAGE_UP] = "\x1B[I",	[TKEY_PAGE_DOWN] = "\x1B[G",
513199171Sed
514199171Sed	[TKEY_F1] = "\x1B[M",		[TKEY_F2] = "\x1B[N",
515199171Sed	[TKEY_F3] = "\x1B[O",		[TKEY_F4] = "\x1B[P",
516199171Sed	[TKEY_F5] = "\x1B[Q",		[TKEY_F6] = "\x1B[R",
517199171Sed	[TKEY_F7] = "\x1B[S",		[TKEY_F8] = "\x1B[T",
518199171Sed	[TKEY_F9] = "\x1B[U",		[TKEY_F10] = "\x1B[V",
519199171Sed	[TKEY_F11] = "\x1B[W",		[TKEY_F12] = "\x1B[X",
520199171Sed};
521199171Sed
522199171Sedstatic const char * const special_strings_ckeys[] = {
523199171Sed	[TKEY_UP] = "\x1BOA",		[TKEY_DOWN] = "\x1BOB",
524199171Sed	[TKEY_LEFT] = "\x1BOD",		[TKEY_RIGHT] = "\x1BOC",
525199171Sed
526199171Sed	[TKEY_HOME] = "\x1BOH",		[TKEY_END] = "\x1BOF",
527199171Sed};
528199171Sed
529199171Sedstatic const char * const special_strings_normal[] = {
530199171Sed	[TKEY_UP] = "\x1B[A",		[TKEY_DOWN] = "\x1B[B",
531199171Sed	[TKEY_LEFT] = "\x1B[D",		[TKEY_RIGHT] = "\x1B[C",
532199171Sed
533199175Sed	[TKEY_HOME] = "\x1B[H",		[TKEY_END] = "\x1B[F",
534199171Sed	[TKEY_INSERT] = "\x1B[2~",	[TKEY_DELETE] = "\x1B[3~",
535199171Sed	[TKEY_PAGE_UP] = "\x1B[5~",	[TKEY_PAGE_DOWN] = "\x1B[6~",
536199171Sed
537199171Sed	[TKEY_F1] = "\x1BOP",		[TKEY_F2] = "\x1BOQ",
538199171Sed	[TKEY_F3] = "\x1BOR",		[TKEY_F4] = "\x1BOS",
539199171Sed	[TKEY_F5] = "\x1B[15~",		[TKEY_F6] = "\x1B[17~",
540199171Sed	[TKEY_F7] = "\x1B[18~",		[TKEY_F8] = "\x1B[19~",
541199171Sed	[TKEY_F9] = "\x1B[20~",		[TKEY_F10] = "\x1B[21~",
542199171Sed	[TKEY_F11] = "\x1B[23~",	[TKEY_F12] = "\x1B[24~",
543199171Sed};
544199171Sed
545199171Sedconst char *
546199171Sedteken_get_sequence(teken_t *t, unsigned int k)
547199171Sed{
548199171Sed
549199171Sed	/* Cons25 mode. */
550199171Sed	if (t->t_stateflags & TS_CONS25 &&
551199171Sed	    k < sizeof special_strings_cons25 / sizeof(char *))
552199171Sed		return (special_strings_cons25[k]);
553199171Sed
554199171Sed	/* Cursor keys mode. */
555199171Sed	if (t->t_stateflags & TS_CURSORKEYS &&
556199171Sed	    k < sizeof special_strings_ckeys / sizeof(char *))
557199171Sed		return (special_strings_ckeys[k]);
558199171Sed
559199171Sed	/* Default xterm sequences. */
560199171Sed	if (k < sizeof special_strings_normal / sizeof(char *))
561199171Sed		return (special_strings_normal[k]);
562223574Sed
563199171Sed	return (NULL);
564199171Sed}
565199171Sed
566186681Sed#include "teken_state.h"
567