xref: /freebsd/sys/teken/teken_subr.h (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 static void teken_subr_cursor_up(teken_t *, unsigned int);
32 static void teken_subr_erase_line(teken_t *, unsigned int);
33 static void teken_subr_regular_character(teken_t *, teken_char_t);
34 static void teken_subr_reset_to_initial_state(teken_t *);
35 static void teken_subr_save_cursor(teken_t *);
36 
37 static inline int
38 teken_tab_isset(teken_t *t, unsigned int col)
39 {
40 	unsigned int b, o;
41 
42 	if (col >= T_NUMCOL)
43 		return ((col % 8) == 0);
44 
45 	b = col / (sizeof(unsigned int) * 8);
46 	o = col % (sizeof(unsigned int) * 8);
47 
48 	return (t->t_tabstops[b] & (1 << o));
49 }
50 
51 static inline void
52 teken_tab_clear(teken_t *t, unsigned int col)
53 {
54 	unsigned int b, o;
55 
56 	if (col >= T_NUMCOL)
57 		return;
58 
59 	b = col / (sizeof(unsigned int) * 8);
60 	o = col % (sizeof(unsigned int) * 8);
61 
62 	t->t_tabstops[b] &= ~(1 << o);
63 }
64 
65 static inline void
66 teken_tab_set(teken_t *t, unsigned int col)
67 {
68 	unsigned int b, o;
69 
70 	if (col >= T_NUMCOL)
71 		return;
72 
73 	b = col / (sizeof(unsigned int) * 8);
74 	o = col % (sizeof(unsigned int) * 8);
75 
76 	t->t_tabstops[b] |= 1 << o;
77 }
78 
79 static void
80 teken_tab_default(teken_t *t)
81 {
82 	unsigned int i;
83 
84 	memset(&t->t_tabstops, 0, T_NUMCOL / 8);
85 
86 	for (i = 8; i < T_NUMCOL; i += 8)
87 		teken_tab_set(t, i);
88 }
89 
90 static void
91 teken_subr_do_scroll(teken_t *t, int amount)
92 {
93 	teken_rect_t tr;
94 	teken_pos_t tp;
95 
96 	teken_assert(t->t_cursor.tp_row <= t->t_winsize.tp_row);
97 	teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
98 	teken_assert(amount != 0);
99 
100 	/* Copy existing data 1 line up. */
101 	if (amount > 0) {
102 		/* Scroll down. */
103 
104 		/* Copy existing data up. */
105 		if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
106 			tr.tr_begin.tp_row = t->t_scrollreg.ts_begin + amount;
107 			tr.tr_begin.tp_col = 0;
108 			tr.tr_end.tp_row = t->t_scrollreg.ts_end;
109 			tr.tr_end.tp_col = t->t_winsize.tp_col;
110 			tp.tp_row = t->t_scrollreg.ts_begin;
111 			tp.tp_col = 0;
112 			teken_funcs_copy(t, &tr, &tp);
113 
114 			tr.tr_begin.tp_row = t->t_scrollreg.ts_end - amount;
115 		} else {
116 			tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
117 		}
118 
119 		/* Clear the last lines. */
120 		tr.tr_begin.tp_col = 0;
121 		tr.tr_end.tp_row = t->t_scrollreg.ts_end;
122 		tr.tr_end.tp_col = t->t_winsize.tp_col;
123 		teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
124 	} else {
125 		/* Scroll up. */
126 		amount = -amount;
127 
128 		/* Copy existing data down. */
129 		if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
130 			tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
131 			tr.tr_begin.tp_col = 0;
132 			tr.tr_end.tp_row = t->t_scrollreg.ts_end - amount;
133 			tr.tr_end.tp_col = t->t_winsize.tp_col;
134 			tp.tp_row = t->t_scrollreg.ts_begin + amount;
135 			tp.tp_col = 0;
136 			teken_funcs_copy(t, &tr, &tp);
137 
138 			tr.tr_end.tp_row = t->t_scrollreg.ts_begin + amount;
139 		} else {
140 			tr.tr_end.tp_row = t->t_scrollreg.ts_end;
141 		}
142 
143 		/* Clear the first lines. */
144 		tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
145 		tr.tr_begin.tp_col = 0;
146 		tr.tr_end.tp_col = t->t_winsize.tp_col;
147 		teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
148 	}
149 }
150 
151 static ssize_t
152 teken_subr_do_cpr(teken_t *t, unsigned int cmd, char response[16])
153 {
154 
155 	switch (cmd) {
156 	case 5: /* Operating status. */
157 		strcpy(response, "0n");
158 		return (2);
159 	case 6: { /* Cursor position. */
160 		int len;
161 
162 		len = snprintf(response, 16, "%u;%uR",
163 		    (t->t_cursor.tp_row - t->t_originreg.ts_begin) + 1,
164 		    t->t_cursor.tp_col + 1);
165 
166 		if (len >= 16)
167 			return (-1);
168 		return (len);
169 	}
170 	case 15: /* Printer status. */
171 		strcpy(response, "13n");
172 		return (3);
173 	case 25: /* UDK status. */
174 		strcpy(response, "20n");
175 		return (3);
176 	case 26: /* Keyboard status. */
177 		strcpy(response, "27;1n");
178 		return (5);
179 	default:
180 		teken_printf("Unknown DSR\n");
181 		return (-1);
182 	}
183 }
184 
185 static void
186 teken_subr_alignment_test(teken_t *t)
187 {
188 	teken_rect_t tr;
189 
190 	t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
191 	t->t_scrollreg.ts_begin = 0;
192 	t->t_scrollreg.ts_end = t->t_winsize.tp_row;
193 	t->t_originreg = t->t_scrollreg;
194 	t->t_stateflags &= ~(TS_WRAPPED|TS_ORIGIN);
195 	teken_funcs_cursor(t);
196 
197 	tr.tr_begin.tp_row = 0;
198 	tr.tr_begin.tp_col = 0;
199 	tr.tr_end = t->t_winsize;
200 	teken_funcs_fill(t, &tr, 'E', &t->t_defattr);
201 }
202 
203 static void
204 teken_subr_backspace(teken_t *t)
205 {
206 
207 	if (t->t_stateflags & TS_CONS25) {
208 		if (t->t_cursor.tp_col == 0) {
209 			if (t->t_cursor.tp_row == t->t_originreg.ts_begin)
210 				return;
211 			t->t_cursor.tp_row--;
212 			t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
213 		} else {
214 			t->t_cursor.tp_col--;
215 		}
216 	} else {
217 		if (t->t_cursor.tp_col == 0)
218 			return;
219 
220 		t->t_cursor.tp_col--;
221 		t->t_stateflags &= ~TS_WRAPPED;
222 	}
223 
224 	teken_funcs_cursor(t);
225 }
226 
227 static void
228 teken_subr_bell(teken_t *t)
229 {
230 
231 	teken_funcs_bell(t);
232 }
233 
234 static void
235 teken_subr_carriage_return(teken_t *t)
236 {
237 
238 	t->t_cursor.tp_col = 0;
239 	t->t_stateflags &= ~TS_WRAPPED;
240 	teken_funcs_cursor(t);
241 }
242 
243 static void
244 teken_subr_cursor_backward(teken_t *t, unsigned int ncols)
245 {
246 
247 	if (ncols > t->t_cursor.tp_col)
248 		t->t_cursor.tp_col = 0;
249 	else
250 		t->t_cursor.tp_col -= ncols;
251 	t->t_stateflags &= ~TS_WRAPPED;
252 	teken_funcs_cursor(t);
253 }
254 
255 static void
256 teken_subr_cursor_backward_tabulation(teken_t *t, unsigned int ntabs)
257 {
258 
259 	do {
260 		/* Stop when we've reached the beginning of the line. */
261 		if (t->t_cursor.tp_col == 0)
262 			break;
263 
264 		t->t_cursor.tp_col--;
265 
266 		/* Tab marker set. */
267 		if (teken_tab_isset(t, t->t_cursor.tp_col))
268 			ntabs--;
269 	} while (ntabs > 0);
270 
271 	teken_funcs_cursor(t);
272 }
273 
274 static void
275 teken_subr_cursor_down(teken_t *t, unsigned int nrows)
276 {
277 
278 	if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end)
279 		t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1;
280 	else
281 		t->t_cursor.tp_row += nrows;
282 	t->t_stateflags &= ~TS_WRAPPED;
283 	teken_funcs_cursor(t);
284 }
285 
286 static void
287 teken_subr_cursor_forward(teken_t *t, unsigned int ncols)
288 {
289 
290 	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col)
291 		t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
292 	else
293 		t->t_cursor.tp_col += ncols;
294 	t->t_stateflags &= ~TS_WRAPPED;
295 	teken_funcs_cursor(t);
296 }
297 
298 static void
299 teken_subr_cursor_forward_tabulation(teken_t *t, unsigned int ntabs)
300 {
301 
302 	do {
303 		/* Stop when we've reached the end of the line. */
304 		if (t->t_cursor.tp_col == t->t_winsize.tp_col - 1)
305 			break;
306 
307 		t->t_cursor.tp_col++;
308 
309 		/* Tab marker set. */
310 		if (teken_tab_isset(t, t->t_cursor.tp_col))
311 			ntabs--;
312 	} while (ntabs > 0);
313 
314 	teken_funcs_cursor(t);
315 }
316 
317 static void
318 teken_subr_cursor_next_line(teken_t *t, unsigned int ncols)
319 {
320 
321 	t->t_cursor.tp_col = 0;
322 	teken_subr_cursor_down(t, ncols);
323 }
324 
325 static void
326 teken_subr_cursor_position(teken_t *t, unsigned int row, unsigned int col)
327 {
328 
329 	row = row - 1 + t->t_originreg.ts_begin;
330 	t->t_cursor.tp_row = row < t->t_originreg.ts_end ?
331 	    row : t->t_originreg.ts_end - 1;
332 
333 	col--;
334 	t->t_cursor.tp_col = col < t->t_winsize.tp_col ?
335 	    col : t->t_winsize.tp_col - 1;
336 
337 	t->t_stateflags &= ~TS_WRAPPED;
338 	teken_funcs_cursor(t);
339 }
340 
341 static void
342 teken_subr_cursor_position_report(teken_t *t, unsigned int cmd)
343 {
344 	char response[18] = "\x1B[";
345 	ssize_t len;
346 
347 	len = teken_subr_do_cpr(t, cmd, response + 2);
348 	if (len < 0)
349 		return;
350 
351 	teken_funcs_respond(t, response, len + 2);
352 }
353 
354 static void
355 teken_subr_cursor_previous_line(teken_t *t, unsigned int ncols)
356 {
357 
358 	t->t_cursor.tp_col = 0;
359 	teken_subr_cursor_up(t, ncols);
360 }
361 
362 static void
363 teken_subr_cursor_up(teken_t *t, unsigned int nrows)
364 {
365 
366 	if (t->t_scrollreg.ts_begin + nrows >= t->t_cursor.tp_row)
367 		t->t_cursor.tp_row = t->t_scrollreg.ts_begin;
368 	else
369 		t->t_cursor.tp_row -= nrows;
370 	t->t_stateflags &= ~TS_WRAPPED;
371 	teken_funcs_cursor(t);
372 }
373 
374 static void
375 teken_subr_delete_character(teken_t *t, unsigned int ncols)
376 {
377 	teken_rect_t tr;
378 
379 	tr.tr_begin.tp_row = t->t_cursor.tp_row;
380 	tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
381 	tr.tr_end.tp_col = t->t_winsize.tp_col;
382 
383 	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) {
384 		tr.tr_begin.tp_col = t->t_cursor.tp_col;
385 	} else {
386 		/* Copy characters to the left. */
387 		tr.tr_begin.tp_col = t->t_cursor.tp_col + ncols;
388 		teken_funcs_copy(t, &tr, &t->t_cursor);
389 
390 		tr.tr_begin.tp_col = t->t_winsize.tp_col - ncols;
391 	}
392 
393 	/* Blank trailing columns. */
394 	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
395 }
396 
397 static void
398 teken_subr_delete_line(teken_t *t, unsigned int nrows)
399 {
400 	teken_rect_t tr;
401 
402 	/* Ignore if outside scrolling region. */
403 	if (t->t_cursor.tp_row < t->t_scrollreg.ts_begin ||
404 	    t->t_cursor.tp_row >= t->t_scrollreg.ts_end)
405 		return;
406 
407 	tr.tr_begin.tp_col = 0;
408 	tr.tr_end.tp_row = t->t_scrollreg.ts_end;
409 	tr.tr_end.tp_col = t->t_winsize.tp_col;
410 
411 	if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) {
412 		tr.tr_begin.tp_row = t->t_cursor.tp_row;
413 	} else {
414 		teken_pos_t tp;
415 
416 		/* Copy rows up. */
417 		tr.tr_begin.tp_row = t->t_cursor.tp_row + nrows;
418 		tp.tp_row = t->t_cursor.tp_row;
419 		tp.tp_col = 0;
420 		teken_funcs_copy(t, &tr, &tp);
421 
422 		tr.tr_begin.tp_row = t->t_scrollreg.ts_end - nrows;
423 	}
424 
425 	/* Blank trailing rows. */
426 	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
427 }
428 
429 static void
430 teken_subr_device_control_string(teken_t *t)
431 {
432 
433 	teken_printf("Unsupported device control string\n");
434 	t->t_stateflags |= TS_INSTRING;
435 }
436 
437 static void
438 teken_subr_device_status_report(teken_t *t, unsigned int cmd)
439 {
440 	char response[19] = "\x1B[?";
441 	ssize_t len;
442 
443 	len = teken_subr_do_cpr(t, cmd, response + 3);
444 	if (len < 0)
445 		return;
446 
447 	teken_funcs_respond(t, response, len + 3);
448 }
449 
450 static void
451 teken_subr_double_height_double_width_line_top(teken_t *t __unused)
452 {
453 
454 	teken_printf("double height double width top\n");
455 }
456 
457 static void
458 teken_subr_double_height_double_width_line_bottom(teken_t *t __unused)
459 {
460 
461 	teken_printf("double height double width bottom\n");
462 }
463 
464 static void
465 teken_subr_erase_character(teken_t *t, unsigned int ncols)
466 {
467 	teken_rect_t tr;
468 
469 	tr.tr_begin = t->t_cursor;
470 	tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
471 
472 	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col)
473 		tr.tr_end.tp_col = t->t_winsize.tp_col;
474 	else
475 		tr.tr_end.tp_col = t->t_cursor.tp_col + ncols;
476 
477 	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
478 }
479 
480 static void
481 teken_subr_erase_display(teken_t *t, unsigned int mode)
482 {
483 	teken_rect_t r;
484 
485 	r.tr_begin.tp_col = 0;
486 	r.tr_end.tp_col = t->t_winsize.tp_col;
487 
488 	switch (mode) {
489 	case 1: /* Erase from the top to the cursor. */
490 		teken_subr_erase_line(t, 1);
491 
492 		/* Erase lines above. */
493 		if (t->t_cursor.tp_row == 0)
494 			return;
495 		r.tr_begin.tp_row = 0;
496 		r.tr_end.tp_row = t->t_cursor.tp_row;
497 		break;
498 	case 2: /* Erase entire display. */
499 		r.tr_begin.tp_row = 0;
500 		r.tr_end.tp_row = t->t_winsize.tp_row;
501 		break;
502 	default: /* Erase from cursor to the bottom. */
503 		teken_subr_erase_line(t, 0);
504 
505 		/* Erase lines below. */
506 		if (t->t_cursor.tp_row == t->t_winsize.tp_row - 1)
507 			return;
508 		r.tr_begin.tp_row = t->t_cursor.tp_row + 1;
509 		r.tr_end.tp_row = t->t_winsize.tp_row;
510 		break;
511 	}
512 
513 	teken_funcs_fill(t, &r, BLANK, &t->t_curattr);
514 }
515 
516 static void
517 teken_subr_erase_line(teken_t *t, unsigned int mode)
518 {
519 	teken_rect_t r;
520 
521 	r.tr_begin.tp_row = t->t_cursor.tp_row;
522 	r.tr_end.tp_row = t->t_cursor.tp_row + 1;
523 
524 	switch (mode) {
525 	case 1: /* Erase from the beginning of the line to the cursor. */
526 		r.tr_begin.tp_col = 0;
527 		r.tr_end.tp_col = t->t_cursor.tp_col + 1;
528 		break;
529 	case 2: /* Erase entire line. */
530 		r.tr_begin.tp_col = 0;
531 		r.tr_end.tp_col = t->t_winsize.tp_col;
532 		break;
533 	default: /* Erase from cursor to the end of the line. */
534 		r.tr_begin.tp_col = t->t_cursor.tp_col;
535 		r.tr_end.tp_col = t->t_winsize.tp_col;
536 		break;
537 	}
538 
539 	teken_funcs_fill(t, &r, BLANK, &t->t_curattr);
540 }
541 
542 static void
543 teken_subr_g0_scs_special_graphics(teken_t *t __unused)
544 {
545 
546 	t->t_scs[0] = teken_scs_special_graphics;
547 }
548 
549 static void
550 teken_subr_g0_scs_uk_national(teken_t *t __unused)
551 {
552 
553 	t->t_scs[0] = teken_scs_uk_national;
554 }
555 
556 static void
557 teken_subr_g0_scs_us_ascii(teken_t *t __unused)
558 {
559 
560 	t->t_scs[0] = teken_scs_us_ascii;
561 }
562 
563 static void
564 teken_subr_g1_scs_special_graphics(teken_t *t __unused)
565 {
566 
567 	t->t_scs[1] = teken_scs_special_graphics;
568 }
569 
570 static void
571 teken_subr_g1_scs_uk_national(teken_t *t __unused)
572 {
573 
574 	t->t_scs[1] = teken_scs_uk_national;
575 }
576 
577 static void
578 teken_subr_g1_scs_us_ascii(teken_t *t __unused)
579 {
580 
581 	t->t_scs[1] = teken_scs_us_ascii;
582 }
583 
584 static void
585 teken_subr_horizontal_position_absolute(teken_t *t, unsigned int col)
586 {
587 
588 	col--;
589 	t->t_cursor.tp_col = col < t->t_winsize.tp_col ?
590 	    col : t->t_winsize.tp_col - 1;
591 
592 	t->t_stateflags &= ~TS_WRAPPED;
593 	teken_funcs_cursor(t);
594 }
595 
596 static void
597 teken_subr_horizontal_tab(teken_t *t)
598 {
599 
600 	teken_subr_cursor_forward_tabulation(t, 1);
601 }
602 
603 static void
604 teken_subr_horizontal_tab_set(teken_t *t)
605 {
606 
607 	teken_tab_set(t, t->t_cursor.tp_col);
608 }
609 
610 static void
611 teken_subr_index(teken_t *t)
612 {
613 
614 	if (t->t_cursor.tp_row < t->t_scrollreg.ts_end - 1) {
615 		t->t_cursor.tp_row++;
616 		t->t_stateflags &= ~TS_WRAPPED;
617 		teken_funcs_cursor(t);
618 	} else {
619 		teken_subr_do_scroll(t, 1);
620 	}
621 }
622 
623 static void
624 teken_subr_insert_character(teken_t *t, unsigned int ncols)
625 {
626 	teken_rect_t tr;
627 
628 	tr.tr_begin = t->t_cursor;
629 	tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
630 
631 	if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) {
632 		tr.tr_end.tp_col = t->t_winsize.tp_col;
633 	} else {
634 		teken_pos_t tp;
635 
636 		/* Copy characters to the right. */
637 		tr.tr_end.tp_col = t->t_winsize.tp_col - ncols;
638 		tp.tp_row = t->t_cursor.tp_row;
639 		tp.tp_col = t->t_cursor.tp_col + ncols;
640 		teken_funcs_copy(t, &tr, &tp);
641 
642 		tr.tr_end.tp_col = t->t_cursor.tp_col + ncols;
643 	}
644 
645 	/* Blank current location. */
646 	teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
647 }
648 
649 static void
650 teken_subr_insert_line(teken_t *t, unsigned int nrows)
651 {
652 	teken_rect_t tr;
653 
654 	/* Ignore if outside scrolling region. */
655 	if (t->t_cursor.tp_row < t->t_scrollreg.ts_begin ||
656 	    t->t_cursor.tp_row >= t->t_scrollreg.ts_end)
657 		return;
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 
681 static void
682 teken_subr_keypad_application_mode(teken_t *t)
683 {
684 
685 	teken_funcs_param(t, TP_KEYPADAPP, 1);
686 }
687 
688 static void
689 teken_subr_keypad_numeric_mode(teken_t *t)
690 {
691 
692 	teken_funcs_param(t, TP_KEYPADAPP, 0);
693 }
694 
695 static void
696 teken_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 
710 static void
711 teken_subr_newpage(teken_t *t)
712 {
713 
714 	if (t->t_stateflags & TS_CONS25) {
715 		teken_rect_t tr;
716 
717 		/* Clear screen. */
718 		tr.tr_begin.tp_row = t->t_originreg.ts_begin;
719 		tr.tr_begin.tp_col = 0;
720 		tr.tr_end.tp_row = t->t_originreg.ts_end;
721 		tr.tr_end.tp_col = t->t_winsize.tp_col;
722 		teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
723 
724 		/* Cursor at top left. */
725 		t->t_cursor.tp_row = t->t_originreg.ts_begin;
726 		t->t_cursor.tp_col = 0;
727 		t->t_stateflags &= ~TS_WRAPPED;
728 		teken_funcs_cursor(t);
729 	} else {
730 		teken_subr_newline(t);
731 	}
732 }
733 
734 static void
735 teken_subr_next_line(teken_t *t)
736 {
737 
738 	t->t_cursor.tp_col = 0;
739 	teken_subr_newline(t);
740 }
741 
742 static void
743 teken_subr_operating_system_command(teken_t *t)
744 {
745 
746 	teken_printf("Unsupported operating system command\n");
747 	t->t_stateflags |= TS_INSTRING;
748 }
749 
750 static void
751 teken_subr_pan_down(teken_t *t, unsigned int nrows)
752 {
753 
754 	teken_subr_do_scroll(t, (int)nrows);
755 }
756 
757 static void
758 teken_subr_pan_up(teken_t *t, unsigned int nrows)
759 {
760 
761 	teken_subr_do_scroll(t, -(int)nrows);
762 }
763 
764 static void
765 teken_subr_primary_device_attributes(teken_t *t, unsigned int request)
766 {
767 
768 	if (request == 0) {
769 		const char response[] = "\x1B[?1;2c";
770 
771 		teken_funcs_respond(t, response, sizeof response - 1);
772 	} else {
773 		teken_printf("Unknown DA1\n");
774 	}
775 }
776 
777 static void
778 teken_subr_do_putchar(teken_t *t, const teken_pos_t *tp, teken_char_t c,
779     int width)
780 {
781 
782 	if (t->t_stateflags & TS_INSERT &&
783 	    tp->tp_col < t->t_winsize.tp_col - width) {
784 		teken_rect_t ctr;
785 		teken_pos_t ctp;
786 
787 		/* Insert mode. Move existing characters to the right. */
788 		ctr.tr_begin = *tp;
789 		ctr.tr_end.tp_row = tp->tp_row + 1;
790 		ctr.tr_end.tp_col = t->t_winsize.tp_col - width;
791 		ctp.tp_row = tp->tp_row;
792 		ctp.tp_col = tp->tp_col + width;
793 		teken_funcs_copy(t, &ctr, &ctp);
794 	}
795 
796 	teken_funcs_putchar(t, tp, c, &t->t_curattr);
797 
798 	if (width == 2 && tp->tp_col + 1 < t->t_winsize.tp_col) {
799 		teken_pos_t tp2;
800 		teken_attr_t attr;
801 
802 		/* Print second half of CJK fullwidth character. */
803 		tp2.tp_row = tp->tp_row;
804 		tp2.tp_col = tp->tp_col + 1;
805 		attr = t->t_curattr;
806 		attr.ta_format |= TF_CJK_RIGHT;
807 		teken_funcs_putchar(t, &tp2, c, &attr);
808 	}
809 }
810 
811 static void
812 teken_subr_regular_character(teken_t *t, teken_char_t c)
813 {
814 	int width;
815 
816 	if (t->t_stateflags & TS_8BIT) {
817 		if (!(t->t_stateflags & TS_CONS25) && (c <= 0x1b || c == 0x7f))
818 			return;
819 		c = teken_scs_process(t, c);
820 		width = 1;
821 	} else {
822 		c = teken_scs_process(t, c);
823 		width = teken_wcwidth(c);
824 		/* XXX: Don't process zero-width characters yet. */
825 		if (width <= 0)
826 			return;
827 	}
828 
829 	if (t->t_stateflags & TS_CONS25) {
830 		teken_subr_do_putchar(t, &t->t_cursor, c, width);
831 		t->t_cursor.tp_col += width;
832 
833 		if (t->t_cursor.tp_col >= t->t_winsize.tp_col) {
834 			if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) {
835 				/* Perform scrolling. */
836 				teken_subr_do_scroll(t, 1);
837 			} else {
838 				/* No scrolling needed. */
839 				if (t->t_cursor.tp_row <
840 				    t->t_winsize.tp_row - 1)
841 					t->t_cursor.tp_row++;
842 			}
843 			t->t_cursor.tp_col = 0;
844 		}
845 	} else if (t->t_stateflags & TS_AUTOWRAP &&
846 	    ((t->t_stateflags & TS_WRAPPED &&
847 	    t->t_cursor.tp_col + 1 == t->t_winsize.tp_col) ||
848 	    t->t_cursor.tp_col + width > t->t_winsize.tp_col)) {
849 		teken_pos_t tp;
850 
851 		/*
852 		 * Perform line wrapping, if:
853 		 * - Autowrapping is enabled, and
854 		 *   - We're in the wrapped state at the last column, or
855 		 *   - The character to be printed does not fit anymore.
856 		 */
857 		if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) {
858 			/* Perform scrolling. */
859 			teken_subr_do_scroll(t, 1);
860 			tp.tp_row = t->t_scrollreg.ts_end - 1;
861 		} else {
862 			/* No scrolling needed. */
863 			tp.tp_row = t->t_cursor.tp_row + 1;
864 			if (tp.tp_row == t->t_winsize.tp_row) {
865 				/*
866 				 * Corner case: regular character
867 				 * outside scrolling region, but at the
868 				 * bottom of the screen.
869 				 */
870 				teken_subr_do_putchar(t, &t->t_cursor,
871 				    c, width);
872 				return;
873 			}
874 		}
875 
876 		tp.tp_col = 0;
877 		teken_subr_do_putchar(t, &tp, c, width);
878 
879 		t->t_cursor.tp_row = tp.tp_row;
880 		t->t_cursor.tp_col = width;
881 		t->t_stateflags &= ~TS_WRAPPED;
882 	} else {
883 		/* No line wrapping needed. */
884 		teken_subr_do_putchar(t, &t->t_cursor, c, width);
885 		t->t_cursor.tp_col += width;
886 
887 		if (t->t_cursor.tp_col >= t->t_winsize.tp_col) {
888 			t->t_stateflags |= TS_WRAPPED;
889 			t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
890 		} else {
891 			t->t_stateflags &= ~TS_WRAPPED;
892 		}
893 	}
894 
895 	teken_funcs_cursor(t);
896 }
897 
898 static void
899 teken_subr_reset_dec_mode(teken_t *t, unsigned int cmd)
900 {
901 
902 	switch (cmd) {
903 	case 1: /* Cursor keys mode. */
904 		t->t_stateflags &= ~TS_CURSORKEYS;
905 		break;
906 	case 2: /* DECANM: ANSI/VT52 mode. */
907 		teken_printf("DECRST VT52\n");
908 		break;
909 	case 3: /* 132 column mode. */
910 		teken_funcs_param(t, TP_132COLS, 0);
911 		teken_subr_reset_to_initial_state(t);
912 		break;
913 	case 5: /* Inverse video. */
914 		teken_printf("DECRST inverse video\n");
915 		break;
916 	case 6: /* Origin mode. */
917 		t->t_stateflags &= ~TS_ORIGIN;
918 		t->t_originreg.ts_begin = 0;
919 		t->t_originreg.ts_end = t->t_winsize.tp_row;
920 		t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
921 		t->t_stateflags &= ~TS_WRAPPED;
922 		teken_funcs_cursor(t);
923 		break;
924 	case 7: /* Autowrap mode. */
925 		t->t_stateflags &= ~TS_AUTOWRAP;
926 		break;
927 	case 8: /* Autorepeat mode. */
928 		teken_funcs_param(t, TP_AUTOREPEAT, 0);
929 		break;
930 	case 25: /* Hide cursor. */
931 		teken_funcs_param(t, TP_SHOWCURSOR, 0);
932 		break;
933 	case 40: /* Disallow 132 columns. */
934 		teken_printf("DECRST allow 132\n");
935 		break;
936 	case 45: /* Disable reverse wraparound. */
937 		teken_printf("DECRST reverse wraparound\n");
938 		break;
939 	case 47: /* Switch to alternate buffer. */
940 		teken_printf("Switch to alternate buffer\n");
941 		break;
942 	case 1000: /* Mouse input. */
943 		teken_funcs_param(t, TP_MOUSE, 0);
944 		break;
945 	default:
946 		teken_printf("Unknown DECRST: %u\n", cmd);
947 	}
948 }
949 
950 static void
951 teken_subr_reset_mode(teken_t *t, unsigned int cmd)
952 {
953 
954 	switch (cmd) {
955 	case 4:
956 		t->t_stateflags &= ~TS_INSERT;
957 		break;
958 	default:
959 		teken_printf("Unknown reset mode: %u\n", cmd);
960 	}
961 }
962 
963 static void
964 teken_subr_do_resize(teken_t *t)
965 {
966 
967 	t->t_scrollreg.ts_begin = 0;
968 	t->t_scrollreg.ts_end = t->t_winsize.tp_row;
969 	t->t_originreg = t->t_scrollreg;
970 }
971 
972 static void
973 teken_subr_do_reset(teken_t *t)
974 {
975 
976 	t->t_curattr = t->t_defattr;
977 	t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
978 	t->t_scrollreg.ts_begin = 0;
979 	t->t_scrollreg.ts_end = t->t_winsize.tp_row;
980 	t->t_originreg = t->t_scrollreg;
981 	t->t_stateflags &= TS_8BIT|TS_CONS25;
982 	t->t_stateflags |= TS_AUTOWRAP;
983 
984 	t->t_scs[0] = teken_scs_us_ascii;
985 	t->t_scs[1] = teken_scs_us_ascii;
986 	t->t_curscs = 0;
987 
988 	teken_subr_save_cursor(t);
989 	teken_tab_default(t);
990 }
991 
992 static void
993 teken_subr_reset_to_initial_state(teken_t *t)
994 {
995 
996 	teken_subr_do_reset(t);
997 	teken_subr_erase_display(t, 2);
998 	teken_funcs_param(t, TP_SHOWCURSOR, 1);
999 	teken_funcs_cursor(t);
1000 }
1001 
1002 static void
1003 teken_subr_restore_cursor(teken_t *t)
1004 {
1005 
1006 	t->t_cursor = t->t_saved_cursor;
1007 	t->t_curattr = t->t_saved_curattr;
1008 	t->t_scs[t->t_curscs] = t->t_saved_curscs;
1009 	t->t_stateflags &= ~TS_WRAPPED;
1010 
1011 	/* Get out of origin mode when the cursor is moved outside. */
1012 	if (t->t_cursor.tp_row < t->t_originreg.ts_begin ||
1013 	    t->t_cursor.tp_row >= t->t_originreg.ts_end) {
1014 		t->t_stateflags &= ~TS_ORIGIN;
1015 		t->t_originreg.ts_begin = 0;
1016 		t->t_originreg.ts_end = t->t_winsize.tp_row;
1017 	}
1018 
1019 	teken_funcs_cursor(t);
1020 }
1021 
1022 static void
1023 teken_subr_reverse_index(teken_t *t)
1024 {
1025 
1026 	if (t->t_cursor.tp_row > t->t_scrollreg.ts_begin) {
1027 		t->t_cursor.tp_row--;
1028 		t->t_stateflags &= ~TS_WRAPPED;
1029 		teken_funcs_cursor(t);
1030 	} else {
1031 		teken_subr_do_scroll(t, -1);
1032 	}
1033 }
1034 
1035 static void
1036 teken_subr_save_cursor(teken_t *t)
1037 {
1038 
1039 	t->t_saved_cursor = t->t_cursor;
1040 	t->t_saved_curattr = t->t_curattr;
1041 	t->t_saved_curscs = t->t_scs[t->t_curscs];
1042 }
1043 
1044 static void
1045 teken_subr_secondary_device_attributes(teken_t *t, unsigned int request)
1046 {
1047 
1048 	if (request == 0) {
1049 		const char response[] = "\x1B[>0;10;0c";
1050 		teken_funcs_respond(t, response, sizeof response - 1);
1051 	} else {
1052 		teken_printf("Unknown DA2\n");
1053 	}
1054 }
1055 
1056 static void
1057 teken_subr_set_dec_mode(teken_t *t, unsigned int cmd)
1058 {
1059 
1060 	switch (cmd) {
1061 	case 1: /* Cursor keys mode. */
1062 		t->t_stateflags |= TS_CURSORKEYS;
1063 		break;
1064 	case 2: /* DECANM: ANSI/VT52 mode. */
1065 		teken_printf("DECSET VT52\n");
1066 		break;
1067 	case 3: /* 132 column mode. */
1068 		teken_funcs_param(t, TP_132COLS, 1);
1069 		teken_subr_reset_to_initial_state(t);
1070 		break;
1071 	case 5: /* Inverse video. */
1072 		teken_printf("DECSET inverse video\n");
1073 		break;
1074 	case 6: /* Origin mode. */
1075 		t->t_stateflags |= TS_ORIGIN;
1076 		t->t_originreg = t->t_scrollreg;
1077 		t->t_cursor.tp_row = t->t_scrollreg.ts_begin;
1078 		t->t_cursor.tp_col = 0;
1079 		t->t_stateflags &= ~TS_WRAPPED;
1080 		teken_funcs_cursor(t);
1081 		break;
1082 	case 7: /* Autowrap mode. */
1083 		t->t_stateflags |= TS_AUTOWRAP;
1084 		break;
1085 	case 8: /* Autorepeat mode. */
1086 		teken_funcs_param(t, TP_AUTOREPEAT, 1);
1087 		break;
1088 	case 25: /* Display cursor. */
1089 		teken_funcs_param(t, TP_SHOWCURSOR, 1);
1090 		break;
1091 	case 40: /* Allow 132 columns. */
1092 		teken_printf("DECSET allow 132\n");
1093 		break;
1094 	case 45: /* Enable reverse wraparound. */
1095 		teken_printf("DECSET reverse wraparound\n");
1096 		break;
1097 	case 47: /* Switch to alternate buffer. */
1098 		teken_printf("Switch away from alternate buffer\n");
1099 		break;
1100 	case 1000: /* Mouse input. */
1101 		teken_funcs_param(t, TP_MOUSE, 1);
1102 		break;
1103 	default:
1104 		teken_printf("Unknown DECSET: %u\n", cmd);
1105 	}
1106 }
1107 
1108 static void
1109 teken_subr_set_mode(teken_t *t, unsigned int cmd)
1110 {
1111 
1112 	switch (cmd) {
1113 	case 4:
1114 		teken_printf("Insert mode\n");
1115 		t->t_stateflags |= TS_INSERT;
1116 		break;
1117 	default:
1118 		teken_printf("Unknown set mode: %u\n", cmd);
1119 	}
1120 }
1121 
1122 static void
1123 teken_subr_set_graphic_rendition(teken_t *t, unsigned int ncmds,
1124     unsigned int cmds[])
1125 {
1126 	unsigned int i, n;
1127 
1128 	/* No attributes means reset. */
1129 	if (ncmds == 0) {
1130 		t->t_curattr = t->t_defattr;
1131 		return;
1132 	}
1133 
1134 	for (i = 0; i < ncmds; i++) {
1135 		n = cmds[i];
1136 
1137 		switch (n) {
1138 		case 0: /* Reset. */
1139 			t->t_curattr = t->t_defattr;
1140 			break;
1141 		case 1: /* Bold. */
1142 			t->t_curattr.ta_format |= TF_BOLD;
1143 			break;
1144 		case 4: /* Underline. */
1145 			t->t_curattr.ta_format |= TF_UNDERLINE;
1146 			break;
1147 		case 5: /* Blink. */
1148 			t->t_curattr.ta_format |= TF_BLINK;
1149 			break;
1150 		case 7: /* Reverse. */
1151 			t->t_curattr.ta_format |= TF_REVERSE;
1152 			break;
1153 		case 22: /* Remove bold. */
1154 			t->t_curattr.ta_format &= ~TF_BOLD;
1155 			break;
1156 		case 24: /* Remove underline. */
1157 			t->t_curattr.ta_format &= ~TF_UNDERLINE;
1158 			break;
1159 		case 25: /* Remove blink. */
1160 			t->t_curattr.ta_format &= ~TF_BLINK;
1161 			break;
1162 		case 27: /* Remove reverse. */
1163 			t->t_curattr.ta_format &= ~TF_REVERSE;
1164 			break;
1165 		case 30: /* Set foreground color: black */
1166 		case 31: /* Set foreground color: red */
1167 		case 32: /* Set foreground color: green */
1168 		case 33: /* Set foreground color: brown */
1169 		case 34: /* Set foreground color: blue */
1170 		case 35: /* Set foreground color: magenta */
1171 		case 36: /* Set foreground color: cyan */
1172 		case 37: /* Set foreground color: white */
1173 			t->t_curattr.ta_fgcolor = n - 30;
1174 			break;
1175 		case 38: /* Set foreground color: 256 color mode */
1176 			if (i + 2 >= ncmds || cmds[i + 1] != 5)
1177 				continue;
1178 			t->t_curattr.ta_fgcolor = cmds[i + 2];
1179 			i += 2;
1180 			break;
1181 		case 39: /* Set default foreground color. */
1182 			t->t_curattr.ta_fgcolor = t->t_defattr.ta_fgcolor;
1183 			break;
1184 		case 40: /* Set background color: black */
1185 		case 41: /* Set background color: red */
1186 		case 42: /* Set background color: green */
1187 		case 43: /* Set background color: brown */
1188 		case 44: /* Set background color: blue */
1189 		case 45: /* Set background color: magenta */
1190 		case 46: /* Set background color: cyan */
1191 		case 47: /* Set background color: white */
1192 			t->t_curattr.ta_bgcolor = n - 40;
1193 			break;
1194 		case 48: /* Set background color: 256 color mode */
1195 			if (i + 2 >= ncmds || cmds[i + 1] != 5)
1196 				continue;
1197 			t->t_curattr.ta_bgcolor = cmds[i + 2];
1198 			i += 2;
1199 			break;
1200 		case 49: /* Set default background color. */
1201 			t->t_curattr.ta_bgcolor = t->t_defattr.ta_bgcolor;
1202 			break;
1203 		case 90: /* Set bright foreground color: black */
1204 		case 91: /* Set bright foreground color: red */
1205 		case 92: /* Set bright foreground color: green */
1206 		case 93: /* Set bright foreground color: brown */
1207 		case 94: /* Set bright foreground color: blue */
1208 		case 95: /* Set bright foreground color: magenta */
1209 		case 96: /* Set bright foreground color: cyan */
1210 		case 97: /* Set bright foreground color: white */
1211 			t->t_curattr.ta_fgcolor = n - 90 + 8;
1212 			break;
1213 		case 100: /* Set bright background color: black */
1214 		case 101: /* Set bright background color: red */
1215 		case 102: /* Set bright background color: green */
1216 		case 103: /* Set bright background color: brown */
1217 		case 104: /* Set bright background color: blue */
1218 		case 105: /* Set bright background color: magenta */
1219 		case 106: /* Set bright background color: cyan */
1220 		case 107: /* Set bright background color: white */
1221 			t->t_curattr.ta_bgcolor = n - 100 + 8;
1222 			break;
1223 		default:
1224 			teken_printf("unsupported attribute %u\n", n);
1225 		}
1226 	}
1227 }
1228 
1229 static void
1230 teken_subr_set_top_and_bottom_margins(teken_t *t, unsigned int top,
1231     unsigned int bottom)
1232 {
1233 
1234 	/* Adjust top row number. */
1235 	if (top > 0)
1236 		top--;
1237 	/* Adjust bottom row number. */
1238 	if (bottom == 0 || bottom > t->t_winsize.tp_row)
1239 		bottom = t->t_winsize.tp_row;
1240 
1241 	/* Invalid arguments. */
1242 	if (top >= bottom - 1) {
1243 		top = 0;
1244 		bottom = t->t_winsize.tp_row;
1245 	}
1246 
1247 	/* Apply scrolling region. */
1248 	t->t_scrollreg.ts_begin = top;
1249 	t->t_scrollreg.ts_end = bottom;
1250 	if (t->t_stateflags & TS_ORIGIN)
1251 		t->t_originreg = t->t_scrollreg;
1252 
1253 	/* Home cursor to the top left of the scrolling region. */
1254 	t->t_cursor.tp_row = t->t_originreg.ts_begin;
1255 	t->t_cursor.tp_col = 0;
1256 	t->t_stateflags &= ~TS_WRAPPED;
1257 	teken_funcs_cursor(t);
1258 }
1259 
1260 static void
1261 teken_subr_single_height_double_width_line(teken_t *t __unused)
1262 {
1263 
1264 	teken_printf("single height double width???\n");
1265 }
1266 
1267 static void
1268 teken_subr_single_height_single_width_line(teken_t *t __unused)
1269 {
1270 
1271 	teken_printf("single height single width???\n");
1272 }
1273 
1274 static void
1275 teken_subr_string_terminator(teken_t *t __unused)
1276 {
1277 
1278 	/*
1279 	 * Strings are already terminated in teken_input_char() when ^[
1280 	 * is inserted.
1281 	 */
1282 }
1283 
1284 static void
1285 teken_subr_tab_clear(teken_t *t, unsigned int cmd)
1286 {
1287 
1288 	switch (cmd) {
1289 	case 0:
1290 		teken_tab_clear(t, t->t_cursor.tp_col);
1291 		break;
1292 	case 3:
1293 		memset(&t->t_tabstops, 0, T_NUMCOL / 8);
1294 		break;
1295 	}
1296 }
1297 
1298 static void
1299 teken_subr_vertical_position_absolute(teken_t *t, unsigned int row)
1300 {
1301 
1302 	row = row - 1 + t->t_originreg.ts_begin;
1303 	t->t_cursor.tp_row = row < t->t_originreg.ts_end ?
1304 	    row : t->t_originreg.ts_end - 1;
1305 
1306 	t->t_stateflags &= ~TS_WRAPPED;
1307 	teken_funcs_cursor(t);
1308 }
1309