teken_subr.h revision 197470
112422Sbobv/*-
212422Sbobv * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
312422Sbobv * All rights reserved.
412422Sbobv *
512422Sbobv * Redistribution and use in source and binary forms, with or without
612422Sbobv * modification, are permitted provided that the following conditions
712422Sbobv * are met:
812422Sbobv * 1. Redistributions of source code must retain the above copyright
912422Sbobv *    notice, this list of conditions and the following disclaimer.
1012422Sbobv * 2. Redistributions in binary form must reproduce the above copyright
1112422Sbobv *    notice, this list of conditions and the following disclaimer in the
1212422Sbobv *    documentation and/or other materials provided with the distribution.
1312422Sbobv *
1412422Sbobv * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1512422Sbobv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1612422Sbobv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1712422Sbobv * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1812422Sbobv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1912422Sbobv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2012422Sbobv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2112422Sbobv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2212422Sbobv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2312422Sbobv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2412422Sbobv * SUCH DAMAGE.
2512422Sbobv *
2612422Sbobv * $FreeBSD: head/sys/teken/teken_subr.h 197470 2009-09-24 20:33:14Z ed $
2712422Sbobv */
2812422Sbobv
2912422Sbobvstatic void teken_subr_cursor_up(teken_t *, unsigned int);
3012422Sbobvstatic void teken_subr_erase_line(teken_t *, unsigned int);
3112422Sbobvstatic void teken_subr_regular_character(teken_t *, teken_char_t);
3212422Sbobvstatic void teken_subr_reset_to_initial_state(teken_t *);
3312422Sbobvstatic void teken_subr_save_cursor(teken_t *);
3412422Sbobv
3512422Sbobvstatic inline int
3612422Sbobvteken_tab_isset(teken_t *t, unsigned int col)
3712422Sbobv{
3812422Sbobv	unsigned int b, o;
3912422Sbobv
4012422Sbobv	if (col >= T_NUMCOL)
4112422Sbobv		return ((col % 8) == 0);
4212422Sbobv
4312422Sbobv	b = col / (sizeof(unsigned int) * 8);
4412422Sbobv	o = col % (sizeof(unsigned int) * 8);
4512422Sbobv
4612422Sbobv	return (t->t_tabstops[b] & (1 << o));
4712422Sbobv}
4812422Sbobv
4912422Sbobvstatic inline void
5012422Sbobvteken_tab_clear(teken_t *t, unsigned int col)
5112422Sbobv{
5212422Sbobv	unsigned int b, o;
5312422Sbobv
5412422Sbobv	if (col >= T_NUMCOL)
5512422Sbobv		return;
5612422Sbobv
5712422Sbobv	b = col / (sizeof(unsigned int) * 8);
5812422Sbobv	o = col % (sizeof(unsigned int) * 8);
5912422Sbobv
6012422Sbobv	t->t_tabstops[b] &= ~(1 << o);
6112422Sbobv}
6212422Sbobv
6312422Sbobvstatic inline void
6412422Sbobvteken_tab_set(teken_t *t, unsigned int col)
6512422Sbobv{
6612422Sbobv	unsigned int b, o;
6712422Sbobv
6812422Sbobv	if (col >= T_NUMCOL)
6912422Sbobv		return;
7012422Sbobv
7112422Sbobv	b = col / (sizeof(unsigned int) * 8);
7212422Sbobv	o = col % (sizeof(unsigned int) * 8);
7312422Sbobv
7412422Sbobv	t->t_tabstops[b] |= 1 << o;
7512422Sbobv}
7612422Sbobv
7712422Sbobvstatic void
7812422Sbobvteken_tab_default(teken_t *t)
7912422Sbobv{
8012422Sbobv	unsigned int i;
8112422Sbobv
8212422Sbobv	memset(&t->t_tabstops, 0, T_NUMCOL / 8);
8312422Sbobv
8412422Sbobv	for (i = 8; i < T_NUMCOL; i += 8)
8512422Sbobv		teken_tab_set(t, i);
8612422Sbobv}
8712422Sbobv
8812422Sbobvstatic void
8912422Sbobvteken_subr_do_scroll(teken_t *t, int amount)
9012422Sbobv{
9112422Sbobv	teken_rect_t tr;
9212422Sbobv	teken_pos_t tp;
9312422Sbobv
9412422Sbobv	teken_assert(t->t_cursor.tp_row <= t->t_winsize.tp_row);
9512422Sbobv	teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
9612422Sbobv	teken_assert(amount != 0);
9712422Sbobv
9812422Sbobv	/* Copy existing data 1 line up. */
9912422Sbobv	if (amount > 0) {
10012422Sbobv		/* Scroll down. */
10112422Sbobv
10212422Sbobv		/* Copy existing data up. */
10312422Sbobv		if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
10412422Sbobv			tr.tr_begin.tp_row = t->t_scrollreg.ts_begin + amount;
10512422Sbobv			tr.tr_begin.tp_col = 0;
10612422Sbobv			tr.tr_end.tp_row = t->t_scrollreg.ts_end;
10712422Sbobv			tr.tr_end.tp_col = t->t_winsize.tp_col;
10812422Sbobv			tp.tp_row = t->t_scrollreg.ts_begin;
10912422Sbobv			tp.tp_col = 0;
11012422Sbobv			teken_funcs_copy(t, &tr, &tp);
11112422Sbobv
11212422Sbobv			tr.tr_begin.tp_row = t->t_scrollreg.ts_end - amount;
11312422Sbobv		} else {
11412422Sbobv			tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
11512422Sbobv		}
11612422Sbobv
11712422Sbobv		/* Clear the last lines. */
11812422Sbobv		tr.tr_begin.tp_col = 0;
11912422Sbobv		tr.tr_end.tp_row = t->t_scrollreg.ts_end;
12012422Sbobv		tr.tr_end.tp_col = t->t_winsize.tp_col;
12112422Sbobv		teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
12212422Sbobv	} else {
12312422Sbobv		/* Scroll up. */
12412422Sbobv		amount = -amount;
12512422Sbobv
12612422Sbobv		/* Copy existing data down. */
12712422Sbobv		if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
12812422Sbobv			tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
12912422Sbobv			tr.tr_begin.tp_col = 0;
13012422Sbobv			tr.tr_end.tp_row = t->t_scrollreg.ts_end - amount;
13112422Sbobv			tr.tr_end.tp_col = t->t_winsize.tp_col;
13212422Sbobv			tp.tp_row = t->t_scrollreg.ts_begin + amount;
13312422Sbobv			tp.tp_col = 0;
13412422Sbobv			teken_funcs_copy(t, &tr, &tp);
13512422Sbobv
13612422Sbobv			tr.tr_end.tp_row = t->t_scrollreg.ts_begin + amount;
13712422Sbobv		} else {
13812422Sbobv			tr.tr_end.tp_row = t->t_scrollreg.ts_end;
13912422Sbobv		}
14012422Sbobv
14112422Sbobv		/* Clear the first lines. */
14212422Sbobv		tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
14312422Sbobv		tr.tr_begin.tp_col = 0;
14412422Sbobv		tr.tr_end.tp_col = t->t_winsize.tp_col;
14512422Sbobv		teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
14612422Sbobv	}
14712422Sbobv}
14812422Sbobv
14912422Sbobvstatic ssize_t
15012422Sbobvteken_subr_do_cpr(teken_t *t, unsigned int cmd, char response[16])
15112422Sbobv{
15212422Sbobv
15312422Sbobv	switch (cmd) {
15412422Sbobv	case 5: /* Operating status. */
15512422Sbobv		strcpy(response, "0n");
15612422Sbobv		return (2);
15712422Sbobv	case 6: { /* Cursor position. */
15812422Sbobv		int len;
15912422Sbobv
16012422Sbobv		len = snprintf(response, 16, "%u;%uR",
16112422Sbobv		    (t->t_cursor.tp_row - t->t_originreg.ts_begin) + 1,
16212422Sbobv		    t->t_cursor.tp_col + 1);
16312422Sbobv
16412422Sbobv		if (len >= 16)
16512422Sbobv			return (-1);
16612422Sbobv		return (len);
16712422Sbobv	}
16812422Sbobv	case 15: /* Printer status. */
16912422Sbobv		strcpy(response, "13n");
17012422Sbobv		return (3);
17112422Sbobv	case 25: /* UDK status. */
17212422Sbobv		strcpy(response, "20n");
17312422Sbobv		return (3);
17412422Sbobv	case 26: /* Keyboard status. */
17512422Sbobv		strcpy(response, "27;1n");
17612422Sbobv		return (5);
17712422Sbobv	default:
17812422Sbobv		teken_printf("Unknown DSR\n");
17912422Sbobv		return (-1);
18012422Sbobv	}
18112422Sbobv}
18212422Sbobv
18312422Sbobvstatic void
18412422Sbobvteken_subr_alignment_test(teken_t *t)
18512422Sbobv{
18612422Sbobv	teken_rect_t tr;
18712422Sbobv
18812422Sbobv	t->t_scrollreg.ts_begin = 0;
18912422Sbobv	t->t_scrollreg.ts_end = t->t_winsize.tp_row;
19012422Sbobv
19112422Sbobv	t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
19212422Sbobv	t->t_stateflags &= ~TS_WRAPPED;
19312422Sbobv	teken_funcs_cursor(t);
19412422Sbobv
19512422Sbobv	tr.tr_begin.tp_row = 0;
19612422Sbobv	tr.tr_begin.tp_col = 0;
19712422Sbobv	tr.tr_end = t->t_winsize;
19812422Sbobv	teken_funcs_fill(t, &tr, 'E', &t->t_defattr);
19912422Sbobv}
20012422Sbobv
20112422Sbobvstatic void
20212422Sbobvteken_subr_backspace(teken_t *t)
20312422Sbobv{
20412422Sbobv
20512422Sbobv	if (t->t_stateflags & TS_CONS25) {
20612422Sbobv		if (t->t_cursor.tp_col == 0) {
20712422Sbobv			if (t->t_cursor.tp_row == t->t_originreg.ts_begin)
20812422Sbobv				return;
20912422Sbobv			t->t_cursor.tp_row--;
21012422Sbobv			t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
21112422Sbobv		} else {
21212422Sbobv			t->t_cursor.tp_col--;
21312422Sbobv		}
21412422Sbobv	} else {
21512422Sbobv		if (t->t_cursor.tp_col == 0)
21612422Sbobv			return;
21712422Sbobv
21812422Sbobv		t->t_cursor.tp_col--;
21912422Sbobv		t->t_stateflags &= ~TS_WRAPPED;
22012422Sbobv	}
22112422Sbobv
22212422Sbobv	teken_funcs_cursor(t);
22312422Sbobv}
22412422Sbobv
22512422Sbobvstatic void
22612422Sbobvteken_subr_bell(teken_t *t)
22712422Sbobv{
22812422Sbobv
22912422Sbobv	teken_funcs_bell(t);
23012422Sbobv}
23112422Sbobv
23212422Sbobvstatic void
23312422Sbobvteken_subr_carriage_return(teken_t *t)
23412422Sbobv{
23512422Sbobv
23612422Sbobv	t->t_cursor.tp_col = 0;
23712422Sbobv	t->t_stateflags &= ~TS_WRAPPED;
23812422Sbobv	teken_funcs_cursor(t);
23912422Sbobv}
24012422Sbobv
24112422Sbobvstatic void
24212422Sbobvteken_subr_cursor_backward(teken_t *t, unsigned int ncols)
24312422Sbobv{
24412422Sbobv
24512422Sbobv	if (ncols > t->t_cursor.tp_col)
24612422Sbobv		t->t_cursor.tp_col = 0;
24712422Sbobv	else
24812422Sbobv		t->t_cursor.tp_col -= ncols;
24912422Sbobv	t->t_stateflags &= ~TS_WRAPPED;
25012422Sbobv	teken_funcs_cursor(t);
25112422Sbobv}
25212422Sbobv
25312422Sbobvstatic void
25412422Sbobvteken_subr_cursor_backward_tabulation(teken_t *t, unsigned int ntabs)
25512422Sbobv{
25612422Sbobv
25712422Sbobv	do {
25812422Sbobv		/* Stop when we've reached the beginning of the line. */
25912422Sbobv		if (t->t_cursor.tp_col == 0)
26012422Sbobv			break;
26112422Sbobv
26212422Sbobv		t->t_cursor.tp_col--;
26312422Sbobv
26412422Sbobv		/* Tab marker set. */
26512422Sbobv		if (teken_tab_isset(t, t->t_cursor.tp_col))
26612422Sbobv			ntabs--;
26712422Sbobv	} while (ntabs > 0);
26812422Sbobv
26912422Sbobv	teken_funcs_cursor(t);
27012422Sbobv}
27112422Sbobv
27212422Sbobvstatic void
27312422Sbobvteken_subr_cursor_down(teken_t *t, unsigned int nrows)
27412422Sbobv{
27512422Sbobv
27612422Sbobv	if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end)
27712422Sbobv		t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1;
27812422Sbobv	else
27912422Sbobv		t->t_cursor.tp_row += nrows;
28012422Sbobv	t->t_stateflags &= ~TS_WRAPPED;
28112422Sbobv	teken_funcs_cursor(t);
28212422Sbobv}
28312422Sbobv
28412422Sbobvstatic void
28512422Sbobvteken_subr_cursor_forward(teken_t *t, unsigned int ncols)
28612422Sbobv{
28712422Sbobv
28812422Sbobv	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col)
28912422Sbobv		t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
29012422Sbobv	else
29112422Sbobv		t->t_cursor.tp_col += ncols;
29212422Sbobv	t->t_stateflags &= ~TS_WRAPPED;
29312422Sbobv	teken_funcs_cursor(t);
29412422Sbobv}
29512422Sbobv
29612422Sbobvstatic void
29712422Sbobvteken_subr_cursor_forward_tabulation(teken_t *t, unsigned int ntabs)
29812422Sbobv{
29912422Sbobv
30012422Sbobv	do {
30112422Sbobv		/* Stop when we've reached the end of the line. */
30212422Sbobv		if (t->t_cursor.tp_col == t->t_winsize.tp_col - 1)
30312422Sbobv			break;
30412422Sbobv
30512422Sbobv		t->t_cursor.tp_col++;
30612422Sbobv
30712422Sbobv		/* Tab marker set. */
30812422Sbobv		if (teken_tab_isset(t, t->t_cursor.tp_col))
30912422Sbobv			ntabs--;
31012422Sbobv	} while (ntabs > 0);
31112422Sbobv
31212422Sbobv	teken_funcs_cursor(t);
31312422Sbobv}
31412422Sbobv
31512422Sbobvstatic void
31612422Sbobvteken_subr_cursor_next_line(teken_t *t, unsigned int ncols)
31712422Sbobv{
31812422Sbobv
31912422Sbobv	t->t_cursor.tp_col = 0;
32012422Sbobv	teken_subr_cursor_down(t, ncols);
32112422Sbobv}
32212422Sbobv
32312422Sbobvstatic void
32412422Sbobvteken_subr_cursor_position(teken_t *t, unsigned int row, unsigned int col)
32512422Sbobv{
32612422Sbobv
32712422Sbobv	t->t_cursor.tp_row = t->t_originreg.ts_begin + row - 1;
32812422Sbobv	if (row >= t->t_originreg.ts_end)
32912422Sbobv		t->t_cursor.tp_row = t->t_originreg.ts_end - 1;
33012422Sbobv
33112422Sbobv	t->t_cursor.tp_col = col - 1;
33212422Sbobv	if (t->t_cursor.tp_col >= t->t_winsize.tp_col)
33312422Sbobv		t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
33412422Sbobv
33512422Sbobv	t->t_stateflags &= ~TS_WRAPPED;
33612422Sbobv	teken_funcs_cursor(t);
33712422Sbobv}
33812422Sbobv
33912422Sbobvstatic void
34012422Sbobvteken_subr_cursor_position_report(teken_t *t, unsigned int cmd)
34112422Sbobv{
34212422Sbobv	char response[18] = "\x1B[";
34312422Sbobv	ssize_t len;
34412422Sbobv
34512422Sbobv	len = teken_subr_do_cpr(t, cmd, response + 2);
34612422Sbobv	if (len < 0)
34712422Sbobv		return;
34812422Sbobv
34912422Sbobv	teken_funcs_respond(t, response, len + 2);
35012422Sbobv}
35112422Sbobv
35212422Sbobvstatic void
35312422Sbobvteken_subr_cursor_previous_line(teken_t *t, unsigned int ncols)
35412422Sbobv{
35512422Sbobv
35612422Sbobv	t->t_cursor.tp_col = 0;
35712422Sbobv	teken_subr_cursor_up(t, ncols);
35812422Sbobv}
35912422Sbobv
36012422Sbobvstatic void
36112422Sbobvteken_subr_cursor_up(teken_t *t, unsigned int nrows)
36212422Sbobv{
36312422Sbobv
36412422Sbobv	if (t->t_scrollreg.ts_begin + nrows >= t->t_cursor.tp_row)
36512422Sbobv		t->t_cursor.tp_row = t->t_scrollreg.ts_begin;
36612422Sbobv	else
36712422Sbobv		t->t_cursor.tp_row -= nrows;
36812422Sbobv	t->t_stateflags &= ~TS_WRAPPED;
36912422Sbobv	teken_funcs_cursor(t);
37012422Sbobv}
37112422Sbobv
37212422Sbobvstatic void
37312422Sbobvteken_subr_delete_character(teken_t *t, unsigned int ncols)
37412422Sbobv{
37512422Sbobv	teken_rect_t tr;
37612422Sbobv
37712422Sbobv	tr.tr_begin.tp_row = t->t_cursor.tp_row;
37812422Sbobv	tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
37912422Sbobv	tr.tr_end.tp_col = t->t_winsize.tp_col;
38012422Sbobv
38112422Sbobv	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) {
38212422Sbobv		tr.tr_begin.tp_col = t->t_cursor.tp_col;
38312422Sbobv	} else {
38412422Sbobv		/* Copy characters to the left. */
38512422Sbobv		tr.tr_begin.tp_col = t->t_cursor.tp_col + ncols;
38612422Sbobv		teken_funcs_copy(t, &tr, &t->t_cursor);
38712422Sbobv
38812422Sbobv		tr.tr_begin.tp_col = t->t_winsize.tp_col - ncols;
38912422Sbobv	}
39012422Sbobv
39112422Sbobv	/* Blank trailing columns. */
39212422Sbobv	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
39312422Sbobv}
39412422Sbobv
39512422Sbobvstatic void
39612422Sbobvteken_subr_delete_line(teken_t *t, unsigned int nrows)
39712422Sbobv{
39812422Sbobv	teken_rect_t tr;
39912422Sbobv
40012422Sbobv	tr.tr_begin.tp_col = 0;
40112422Sbobv	tr.tr_end.tp_row = t->t_scrollreg.ts_end;
40212422Sbobv	tr.tr_end.tp_col = t->t_winsize.tp_col;
40312422Sbobv
40412422Sbobv	if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) {
40512422Sbobv		tr.tr_begin.tp_row = t->t_cursor.tp_row;
40612422Sbobv	} else {
40712422Sbobv		teken_pos_t tp;
40812422Sbobv
409		/* Copy rows up. */
410		tr.tr_begin.tp_row = t->t_cursor.tp_row + nrows;
411		tp.tp_row = t->t_cursor.tp_row;
412		tp.tp_col = 0;
413		teken_funcs_copy(t, &tr, &tp);
414
415		tr.tr_begin.tp_row = t->t_scrollreg.ts_end - nrows;
416	}
417
418	/* Blank trailing rows. */
419	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
420}
421
422static void
423teken_subr_device_control_string(teken_t *t __unused)
424{
425
426	teken_printf("device control string???\n");
427}
428
429static void
430teken_subr_device_status_report(teken_t *t, unsigned int cmd)
431{
432	char response[19] = "\x1B[?";
433	ssize_t len;
434
435	len = teken_subr_do_cpr(t, cmd, response + 3);
436	if (len < 0)
437		return;
438
439	teken_funcs_respond(t, response, len + 3);
440}
441
442static void
443teken_subr_double_height_double_width_line_top(teken_t *t __unused)
444{
445
446	teken_printf("double height double width top\n");
447}
448
449static void
450teken_subr_double_height_double_width_line_bottom(teken_t *t __unused)
451{
452
453	teken_printf("double height double width bottom\n");
454}
455
456static void
457teken_subr_erase_character(teken_t *t, unsigned int ncols)
458{
459	teken_rect_t tr;
460
461	tr.tr_begin = t->t_cursor;
462	tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
463
464	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col)
465		tr.tr_end.tp_col = t->t_winsize.tp_col;
466	else
467		tr.tr_end.tp_col = t->t_cursor.tp_col + ncols;
468
469	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
470}
471
472static void
473teken_subr_erase_display(teken_t *t, unsigned int mode)
474{
475	teken_rect_t r;
476
477	r.tr_begin.tp_col = 0;
478	r.tr_end.tp_col = t->t_winsize.tp_col;
479
480	switch (mode) {
481	case 1: /* Erase from the top to the cursor. */
482		teken_subr_erase_line(t, 1);
483
484		/* Erase lines above. */
485		if (t->t_cursor.tp_row == 0)
486			return;
487		r.tr_begin.tp_row = 0;
488		r.tr_end.tp_row = t->t_cursor.tp_row;
489		break;
490	case 2: /* Erase entire display. */
491		r.tr_begin.tp_row = 0;
492		r.tr_end.tp_row = t->t_winsize.tp_row;
493		break;
494	default: /* Erase from cursor to the bottom. */
495		teken_subr_erase_line(t, 0);
496
497		/* Erase lines below. */
498		if (t->t_cursor.tp_row == t->t_winsize.tp_row - 1)
499			return;
500		r.tr_begin.tp_row = t->t_cursor.tp_row + 1;
501		r.tr_end.tp_row = t->t_winsize.tp_row;
502		break;
503	}
504
505	teken_funcs_fill(t, &r, BLANK, &t->t_curattr);
506}
507
508static void
509teken_subr_erase_line(teken_t *t, unsigned int mode)
510{
511	teken_rect_t r;
512
513	r.tr_begin.tp_row = t->t_cursor.tp_row;
514	r.tr_end.tp_row = t->t_cursor.tp_row + 1;
515
516	switch (mode) {
517	case 1: /* Erase from the beginning of the line to the cursor. */
518		r.tr_begin.tp_col = 0;
519		r.tr_end.tp_col = t->t_cursor.tp_col + 1;
520		break;
521	case 2: /* Erase entire line. */
522		r.tr_begin.tp_col = 0;
523		r.tr_end.tp_col = t->t_winsize.tp_col;
524		break;
525	default: /* Erase from cursor to the end of the line. */
526		r.tr_begin.tp_col = t->t_cursor.tp_col;
527		r.tr_end.tp_col = t->t_winsize.tp_col;
528		break;
529	}
530
531	teken_funcs_fill(t, &r, BLANK, &t->t_curattr);
532}
533
534static void
535teken_subr_g0_scs_special_graphics(teken_t *t __unused)
536{
537
538	teken_scs_set(t, 0, teken_scs_special_graphics);
539}
540
541static void
542teken_subr_g0_scs_uk_national(teken_t *t __unused)
543{
544
545	teken_scs_set(t, 0, teken_scs_uk_national);
546}
547
548static void
549teken_subr_g0_scs_us_ascii(teken_t *t __unused)
550{
551
552	teken_scs_set(t, 0, teken_scs_us_ascii);
553}
554
555static void
556teken_subr_g1_scs_special_graphics(teken_t *t __unused)
557{
558
559	teken_scs_set(t, 1, teken_scs_special_graphics);
560}
561
562static void
563teken_subr_g1_scs_uk_national(teken_t *t __unused)
564{
565
566	teken_scs_set(t, 1, teken_scs_uk_national);
567}
568
569static void
570teken_subr_g1_scs_us_ascii(teken_t *t __unused)
571{
572
573	teken_scs_set(t, 1, teken_scs_us_ascii);
574}
575
576static void
577teken_subr_horizontal_position_absolute(teken_t *t, unsigned int col)
578{
579
580	t->t_cursor.tp_col = col - 1;
581	if (t->t_cursor.tp_col >= t->t_winsize.tp_col)
582		t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
583
584	t->t_stateflags &= ~TS_WRAPPED;
585	teken_funcs_cursor(t);
586}
587
588static void
589teken_subr_horizontal_tab(teken_t *t)
590{
591
592	if (t->t_stateflags & TS_CONS25) {
593		teken_subr_cursor_forward_tabulation(t, 1);
594	} else {
595		teken_rect_t tr;
596
597		tr.tr_begin = t->t_cursor;
598		teken_subr_cursor_forward_tabulation(t, 1);
599		tr.tr_end.tp_row = tr.tr_begin.tp_row + 1;
600		tr.tr_end.tp_col = t->t_cursor.tp_col;
601
602		/* Blank region that we skipped. */
603		if (tr.tr_end.tp_col > tr.tr_begin.tp_col)
604			teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
605	}
606}
607
608static void
609teken_subr_horizontal_tab_set(teken_t *t)
610{
611
612	teken_tab_set(t, t->t_cursor.tp_col);
613}
614
615static void
616teken_subr_index(teken_t *t)
617{
618
619	if (t->t_cursor.tp_row < t->t_scrollreg.ts_end - 1) {
620		t->t_cursor.tp_row++;
621		t->t_stateflags &= ~TS_WRAPPED;
622		teken_funcs_cursor(t);
623	} else {
624		teken_subr_do_scroll(t, 1);
625	}
626}
627
628static void
629teken_subr_insert_character(teken_t *t, unsigned int ncols)
630{
631	teken_rect_t tr;
632
633	tr.tr_begin = t->t_cursor;
634	tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
635
636	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) {
637		tr.tr_end.tp_col = t->t_winsize.tp_col;
638	} else {
639		teken_pos_t tp;
640
641		/* Copy characters to the right. */
642		tr.tr_end.tp_col = t->t_winsize.tp_col - ncols;
643		tp.tp_row = t->t_cursor.tp_row;
644		tp.tp_col = t->t_cursor.tp_col + ncols;
645		teken_funcs_copy(t, &tr, &tp);
646
647		tr.tr_end.tp_col = t->t_cursor.tp_col + ncols;
648	}
649
650	/* Blank current location. */
651	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
652}
653
654static void
655teken_subr_insert_line(teken_t *t, unsigned int nrows)
656{
657	teken_rect_t tr;
658
659	tr.tr_begin.tp_row = t->t_cursor.tp_row;
660	tr.tr_begin.tp_col = 0;
661	tr.tr_end.tp_col = t->t_winsize.tp_col;
662
663	if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) {
664		tr.tr_end.tp_row = t->t_scrollreg.ts_end;
665	} else {
666		teken_pos_t tp;
667
668		/* Copy lines down. */
669		tr.tr_end.tp_row = t->t_scrollreg.ts_end - nrows;
670		tp.tp_row = t->t_cursor.tp_row + nrows;
671		tp.tp_col = 0;
672		teken_funcs_copy(t, &tr, &tp);
673
674		tr.tr_end.tp_row = t->t_cursor.tp_row + nrows;
675	}
676
677	/* Blank current location. */
678	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
679}
680
681static void
682teken_subr_keypad_application_mode(teken_t *t)
683{
684
685	teken_funcs_param(t, TP_KEYPADAPP, 1);
686}
687
688static void
689teken_subr_keypad_numeric_mode(teken_t *t)
690{
691
692	teken_funcs_param(t, TP_KEYPADAPP, 0);
693}
694
695static void
696teken_subr_newline(teken_t *t)
697{
698
699	t->t_cursor.tp_row++;
700
701	if (t->t_cursor.tp_row >= t->t_scrollreg.ts_end) {
702		teken_subr_do_scroll(t, 1);
703		t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1;
704	}
705
706	t->t_stateflags &= ~TS_WRAPPED;
707	teken_funcs_cursor(t);
708}
709
710static void
711teken_subr_newpage(teken_t *t)
712{
713
714	if (t->t_stateflags & TS_CONS25) {
715		teken_rect_t tr;
716
717		tr.tr_begin.tp_row = tr.tr_begin.tp_col = 0;
718		tr.tr_end = t->t_winsize;
719		teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
720
721		t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
722		teken_funcs_cursor(t);
723	} else {
724		teken_subr_newline(t);
725	}
726}
727
728static void
729teken_subr_next_line(teken_t *t)
730{
731
732	t->t_cursor.tp_col = 0;
733	teken_subr_newline(t);
734}
735
736static void
737teken_subr_pan_down(teken_t *t, unsigned int nrows)
738{
739
740	teken_subr_do_scroll(t, (int)nrows);
741}
742
743static void
744teken_subr_pan_up(teken_t *t, unsigned int nrows)
745{
746
747	teken_subr_do_scroll(t, -(int)nrows);
748}
749
750static void
751teken_subr_primary_device_attributes(teken_t *t, unsigned int request)
752{
753
754	if (request == 0) {
755		const char response[] = "\x1B[?1;2c";
756
757		teken_funcs_respond(t, response, sizeof response - 1);
758	} else {
759		teken_printf("Unknown DA1\n");
760	}
761}
762
763static void
764teken_subr_do_putchar(teken_t *t, const teken_pos_t *tp, teken_char_t c,
765    int width)
766{
767
768	if (t->t_stateflags & TS_INSERT &&
769	    tp->tp_col < t->t_winsize.tp_col - width) {
770		teken_rect_t ctr;
771		teken_pos_t ctp;
772
773		/* Insert mode. Move existing characters to the right. */
774		ctr.tr_begin = *tp;
775		ctr.tr_end.tp_row = tp->tp_row + 1;
776		ctr.tr_end.tp_col = t->t_winsize.tp_col - width;
777		ctp.tp_row = tp->tp_row;
778		ctp.tp_col = tp->tp_col + width;
779		teken_funcs_copy(t, &ctr, &ctp);
780	}
781
782	if (width == 2 && tp->tp_col + 1 < t->t_winsize.tp_col) {
783		teken_pos_t tp2;
784
785		/*
786		 * Store a space behind double width characters before
787		 * actually printing them. This prevents artifacts when
788		 * the consumer doesn't render it using double width
789		 * glyphs.
790		 */
791		tp2.tp_row = tp->tp_row;
792		tp2.tp_col = tp->tp_col + 1;
793		teken_funcs_putchar(t, &tp2, BLANK, &t->t_curattr);
794	}
795
796	teken_funcs_putchar(t, tp, c, &t->t_curattr);
797}
798
799static void
800teken_subr_regular_character(teken_t *t, teken_char_t c)
801{
802	int width;
803
804	if (t->t_stateflags & TS_8BIT) {
805		if (!(t->t_stateflags & TS_CONS25) && (c <= 0x1b || c == 0x7f))
806			return;
807		c = teken_scs_process(t, c);
808		width = 1;
809	} else {
810		c = teken_scs_process(t, c);
811		width = teken_wcwidth(c);
812		/* XXX: Don't process zero-width characters yet. */
813		if (width <= 0)
814			return;
815	}
816
817	if (t->t_stateflags & TS_CONS25) {
818		teken_subr_do_putchar(t, &t->t_cursor, c, width);
819		t->t_cursor.tp_col += width;
820
821		if (t->t_cursor.tp_col >= t->t_winsize.tp_col) {
822			if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) {
823				/* Perform scrolling. */
824				teken_subr_do_scroll(t, 1);
825			} else {
826				/* No scrolling needed. */
827				if (t->t_cursor.tp_row <
828				    t->t_winsize.tp_row - 1)
829					t->t_cursor.tp_row++;
830			}
831			t->t_cursor.tp_col = 0;
832		}
833	} else if (t->t_cursor.tp_col == t->t_winsize.tp_col - 1 &&
834	    (t->t_stateflags & (TS_WRAPPED|TS_AUTOWRAP)) ==
835	    (TS_WRAPPED|TS_AUTOWRAP)) {
836		teken_pos_t tp;
837
838		/* Perform line wrapping. */
839
840		if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) {
841			/* Perform scrolling. */
842			teken_subr_do_scroll(t, 1);
843			tp.tp_row = t->t_scrollreg.ts_end - 1;
844		} else {
845			/* No scrolling needed. */
846			tp.tp_row = t->t_cursor.tp_row + 1;
847			if (tp.tp_row == t->t_winsize.tp_row) {
848				/*
849				 * Corner case: regular character
850				 * outside scrolling region, but at the
851				 * bottom of the screen.
852				 */
853				teken_subr_do_putchar(t, &t->t_cursor,
854				    c, width);
855				return;
856			}
857		}
858
859		tp.tp_col = 0;
860		teken_subr_do_putchar(t, &tp, c, width);
861
862		t->t_cursor.tp_row = tp.tp_row;
863		t->t_cursor.tp_col = width;
864		t->t_stateflags &= ~TS_WRAPPED;
865	} else {
866		/* No line wrapping needed. */
867		teken_subr_do_putchar(t, &t->t_cursor, c, width);
868		t->t_cursor.tp_col += width;
869
870		if (t->t_cursor.tp_col >= t->t_winsize.tp_col) {
871			t->t_stateflags |= TS_WRAPPED;
872			t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
873		} else {
874			t->t_stateflags &= ~TS_WRAPPED;
875		}
876	}
877
878	teken_funcs_cursor(t);
879}
880
881static void
882teken_subr_reset_dec_mode(teken_t *t, unsigned int cmd)
883{
884
885	switch (cmd) {
886	case 1: /* Cursor keys mode. */
887		teken_funcs_param(t, TP_CURSORKEYS, 0);
888		break;
889	case 2: /* DECANM: ANSI/VT52 mode. */
890		teken_printf("DECRST VT52\n");
891		break;
892	case 3: /* 132 column mode. */
893		teken_funcs_param(t, TP_132COLS, 0);
894		teken_subr_reset_to_initial_state(t);
895		break;
896	case 5: /* Inverse video. */
897		teken_printf("DECRST inverse video\n");
898		break;
899	case 6: /* Origin mode. */
900		t->t_stateflags &= ~TS_ORIGIN;
901		t->t_originreg.ts_begin = 0;
902		t->t_originreg.ts_end = t->t_winsize.tp_row;
903		t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
904		t->t_stateflags &= ~TS_WRAPPED;
905		teken_funcs_cursor(t);
906		break;
907	case 7: /* Autowrap mode. */
908		t->t_stateflags &= ~TS_AUTOWRAP;
909		break;
910	case 8: /* Autorepeat mode. */
911		teken_funcs_param(t, TP_AUTOREPEAT, 0);
912		break;
913	case 25: /* Hide cursor. */
914		teken_funcs_param(t, TP_SHOWCURSOR, 0);
915		break;
916	case 40: /* Disallow 132 columns. */
917		teken_printf("DECRST allow 132\n");
918		break;
919	case 45: /* Disable reverse wraparound. */
920		teken_printf("DECRST reverse wraparound\n");
921		break;
922	case 47: /* Switch to alternate buffer. */
923		teken_printf("Switch to alternate buffer\n");
924		break;
925	default:
926		teken_printf("Unknown DECRST: %u\n", cmd);
927	}
928}
929
930static void
931teken_subr_reset_mode(teken_t *t, unsigned int cmd)
932{
933
934	switch (cmd) {
935	case 4:
936		t->t_stateflags &= ~TS_INSERT;
937		break;
938	default:
939		teken_printf("Unknown reset mode: %u\n", cmd);
940	}
941}
942
943static void
944teken_subr_do_reset(teken_t *t)
945{
946
947	t->t_curattr = t->t_defattr;
948	t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
949	t->t_scrollreg.ts_begin = 0;
950	t->t_scrollreg.ts_end = t->t_winsize.tp_row;
951	t->t_originreg = t->t_scrollreg;
952	t->t_stateflags &= TS_8BIT|TS_CONS25;
953	t->t_stateflags |= TS_AUTOWRAP;
954
955	teken_scs_set(t, 0, teken_scs_us_ascii);
956	teken_scs_set(t, 1, teken_scs_us_ascii);
957	teken_scs_switch(t, 0);
958
959	teken_subr_save_cursor(t);
960	teken_tab_default(t);
961}
962
963static void
964teken_subr_reset_to_initial_state(teken_t *t)
965{
966
967	teken_subr_do_reset(t);
968	teken_subr_erase_display(t, 2);
969	teken_funcs_param(t, TP_SHOWCURSOR, 1);
970	teken_funcs_cursor(t);
971}
972
973static void
974teken_subr_restore_cursor(teken_t *t)
975{
976
977	t->t_cursor = t->t_saved_cursor;
978	t->t_curattr = t->t_saved_curattr;
979	t->t_stateflags &= ~TS_WRAPPED;
980	teken_scs_restore(t);
981	teken_funcs_cursor(t);
982}
983
984static void
985teken_subr_reverse_index(teken_t *t)
986{
987
988	if (t->t_cursor.tp_row > t->t_scrollreg.ts_begin) {
989		t->t_cursor.tp_row--;
990		t->t_stateflags &= ~TS_WRAPPED;
991		teken_funcs_cursor(t);
992	} else {
993		teken_subr_do_scroll(t, -1);
994	}
995}
996
997static void
998teken_subr_save_cursor(teken_t *t)
999{
1000
1001	t->t_saved_cursor = t->t_cursor;
1002	t->t_saved_curattr = t->t_curattr;
1003	teken_scs_save(t);
1004}
1005
1006static void
1007teken_subr_secondary_device_attributes(teken_t *t, unsigned int request)
1008{
1009
1010	if (request == 0) {
1011		const char response[] = "\x1B[>0;10;0c";
1012		teken_funcs_respond(t, response, sizeof response - 1);
1013	} else {
1014		teken_printf("Unknown DA2\n");
1015	}
1016}
1017
1018static void
1019teken_subr_set_dec_mode(teken_t *t, unsigned int cmd)
1020{
1021
1022	switch (cmd) {
1023	case 1: /* Cursor keys mode. */
1024		teken_funcs_param(t, TP_CURSORKEYS, 1);
1025		break;
1026	case 2: /* DECANM: ANSI/VT52 mode. */
1027		teken_printf("DECSET VT52\n");
1028		break;
1029	case 3: /* 132 column mode. */
1030		teken_funcs_param(t, TP_132COLS, 1);
1031		teken_subr_reset_to_initial_state(t);
1032		break;
1033	case 5: /* Inverse video. */
1034		teken_printf("DECSET inverse video\n");
1035		break;
1036	case 6: /* Origin mode. */
1037		t->t_stateflags |= TS_ORIGIN;
1038		t->t_originreg = t->t_scrollreg;
1039		t->t_cursor.tp_row = t->t_scrollreg.ts_begin;
1040		t->t_cursor.tp_col = 0;
1041		t->t_stateflags &= ~TS_WRAPPED;
1042		teken_funcs_cursor(t);
1043		break;
1044	case 7: /* Autowrap mode. */
1045		t->t_stateflags |= TS_AUTOWRAP;
1046		break;
1047	case 8: /* Autorepeat mode. */
1048		teken_funcs_param(t, TP_AUTOREPEAT, 1);
1049		break;
1050	case 25: /* Display cursor. */
1051		teken_funcs_param(t, TP_SHOWCURSOR, 1);
1052		break;
1053	case 40: /* Allow 132 columns. */
1054		teken_printf("DECSET allow 132\n");
1055		break;
1056	case 45: /* Enable reverse wraparound. */
1057		teken_printf("DECSET reverse wraparound\n");
1058		break;
1059	case 47: /* Switch to alternate buffer. */
1060		teken_printf("Switch away from alternate buffer\n");
1061		break;
1062	default:
1063		teken_printf("Unknown DECSET: %u\n", cmd);
1064	}
1065}
1066
1067static void
1068teken_subr_set_mode(teken_t *t, unsigned int cmd)
1069{
1070
1071	switch (cmd) {
1072	case 4:
1073		teken_printf("Insert mode\n");
1074		t->t_stateflags |= TS_INSERT;
1075		break;
1076	default:
1077		teken_printf("Unknown set mode: %u\n", cmd);
1078	}
1079}
1080
1081static void
1082teken_subr_set_graphic_rendition(teken_t *t, unsigned int ncmds,
1083    unsigned int cmds[])
1084{
1085	unsigned int i, n;
1086
1087	/* No attributes means reset. */
1088	if (ncmds == 0) {
1089		t->t_curattr = t->t_defattr;
1090		return;
1091	}
1092
1093	for (i = 0; i < ncmds; i++) {
1094		n = cmds[i];
1095
1096		switch (n) {
1097		case 0: /* Reset. */
1098			t->t_curattr = t->t_defattr;
1099			break;
1100		case 1: /* Bold. */
1101			t->t_curattr.ta_format |= TF_BOLD;
1102			break;
1103		case 4: /* Underline. */
1104			t->t_curattr.ta_format |= TF_UNDERLINE;
1105			break;
1106		case 5: /* Blink. */
1107			t->t_curattr.ta_format |= TF_BLINK;
1108			break;
1109		case 7: /* Reverse. */
1110			t->t_curattr.ta_format |= TF_REVERSE;
1111			break;
1112		case 22: /* Remove bold. */
1113			t->t_curattr.ta_format &= ~TF_BOLD;
1114			break;
1115		case 24: /* Remove underline. */
1116			t->t_curattr.ta_format &= ~TF_UNDERLINE;
1117			break;
1118		case 25: /* Remove blink. */
1119			t->t_curattr.ta_format &= ~TF_BLINK;
1120			break;
1121		case 27: /* Remove reverse. */
1122			t->t_curattr.ta_format &= ~TF_REVERSE;
1123			break;
1124		case 30: /* Set foreground color: black */
1125		case 31: /* Set foreground color: red */
1126		case 32: /* Set foreground color: green */
1127		case 33: /* Set foreground color: brown */
1128		case 34: /* Set foreground color: blue */
1129		case 35: /* Set foreground color: magenta */
1130		case 36: /* Set foreground color: cyan */
1131		case 37: /* Set foreground color: white */
1132			t->t_curattr.ta_fgcolor = n - 30;
1133			break;
1134		case 39: /* Set default foreground color. */
1135			t->t_curattr.ta_fgcolor = t->t_defattr.ta_fgcolor;
1136			break;
1137		case 40: /* Set background color: black */
1138		case 41: /* Set background color: red */
1139		case 42: /* Set background color: green */
1140		case 43: /* Set background color: brown */
1141		case 44: /* Set background color: blue */
1142		case 45: /* Set background color: magenta */
1143		case 46: /* Set background color: cyan */
1144		case 47: /* Set background color: white */
1145			t->t_curattr.ta_bgcolor = n - 40;
1146			break;
1147		case 49: /* Set default background color. */
1148			t->t_curattr.ta_bgcolor = t->t_defattr.ta_bgcolor;
1149			break;
1150		default:
1151			teken_printf("unsupported attribute %u\n", n);
1152		}
1153	}
1154}
1155
1156static void
1157teken_subr_set_top_and_bottom_margins(teken_t *t, unsigned int top,
1158    unsigned int bottom)
1159{
1160
1161	/* Adjust top row number. */
1162	if (top > 0)
1163		top--;
1164	/* Adjust bottom row number. */
1165	if (bottom == 0 || bottom > t->t_winsize.tp_row)
1166		bottom = t->t_winsize.tp_row;
1167
1168	/* Invalid arguments. */
1169	if (top >= bottom - 1) {
1170		top = 0;
1171		bottom = t->t_winsize.tp_row;
1172	}
1173
1174	t->t_scrollreg.ts_begin = top;
1175	t->t_scrollreg.ts_end = bottom;
1176	if (t->t_stateflags & TS_ORIGIN) {
1177		/* XXX: home cursor? */
1178		t->t_originreg = t->t_scrollreg;
1179		t->t_cursor.tp_row = t->t_originreg.ts_begin;
1180		t->t_cursor.tp_col = 0;
1181		t->t_stateflags &= ~TS_WRAPPED;
1182		teken_funcs_cursor(t);
1183	}
1184}
1185
1186static void
1187teken_subr_single_height_double_width_line(teken_t *t __unused)
1188{
1189
1190	teken_printf("single height double width???\n");
1191}
1192
1193static void
1194teken_subr_single_height_single_width_line(teken_t *t __unused)
1195{
1196
1197	teken_printf("single height single width???\n");
1198}
1199
1200static void
1201teken_subr_string_terminator(teken_t *t __unused)
1202{
1203
1204	teken_printf("string terminator???\n");
1205}
1206
1207static void
1208teken_subr_tab_clear(teken_t *t, unsigned int cmd)
1209{
1210
1211	switch (cmd) {
1212	case 0:
1213		teken_tab_clear(t, t->t_cursor.tp_col);
1214		break;
1215	case 3:
1216		memset(&t->t_tabstops, 0, T_NUMCOL / 8);
1217		break;
1218	}
1219}
1220
1221static void
1222teken_subr_vertical_position_absolute(teken_t *t, unsigned int row)
1223{
1224
1225	t->t_cursor.tp_row = t->t_originreg.ts_begin + row - 1;
1226	if (row >= t->t_originreg.ts_end)
1227		t->t_cursor.tp_row = t->t_originreg.ts_end - 1;
1228
1229
1230	t->t_stateflags &= ~TS_WRAPPED;
1231	teken_funcs_cursor(t);
1232}
1233