Deleted Added
full compact
teken_subr.h (197117) teken_subr.h (197470)
1/*-
2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/teken/teken_subr.h 197117 2009-09-12 12:44:21Z ed $
26 * $FreeBSD: head/sys/teken/teken_subr.h 197470 2009-09-24 20:33:14Z ed $
27 */
28
29static void teken_subr_cursor_up(teken_t *, unsigned int);
30static void teken_subr_erase_line(teken_t *, unsigned int);
31static void teken_subr_regular_character(teken_t *, teken_char_t);
32static void teken_subr_reset_to_initial_state(teken_t *);
33static void teken_subr_save_cursor(teken_t *);
34
35static inline int
36teken_tab_isset(teken_t *t, unsigned int col)
37{
38 unsigned int b, o;
39
40 if (col >= T_NUMCOL)
41 return ((col % 8) == 0);
42
43 b = col / (sizeof(unsigned int) * 8);
44 o = col % (sizeof(unsigned int) * 8);
45
46 return (t->t_tabstops[b] & (1 << o));
47}
48
49static inline void
50teken_tab_clear(teken_t *t, unsigned int col)
51{
52 unsigned int b, o;
53
54 if (col >= T_NUMCOL)
55 return;
56
57 b = col / (sizeof(unsigned int) * 8);
58 o = col % (sizeof(unsigned int) * 8);
59
60 t->t_tabstops[b] &= ~(1 << o);
61}
62
63static inline void
64teken_tab_set(teken_t *t, unsigned int col)
65{
66 unsigned int b, o;
67
68 if (col >= T_NUMCOL)
69 return;
70
71 b = col / (sizeof(unsigned int) * 8);
72 o = col % (sizeof(unsigned int) * 8);
73
74 t->t_tabstops[b] |= 1 << o;
75}
76
77static void
78teken_tab_default(teken_t *t)
79{
80 unsigned int i;
81
82 memset(&t->t_tabstops, 0, T_NUMCOL / 8);
83
84 for (i = 8; i < T_NUMCOL; i += 8)
85 teken_tab_set(t, i);
86}
87
88static void
89teken_subr_do_scroll(teken_t *t, int amount)
90{
91 teken_rect_t tr;
92 teken_pos_t tp;
93
94 teken_assert(t->t_cursor.tp_row <= t->t_winsize.tp_row);
95 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
96 teken_assert(amount != 0);
97
98 /* Copy existing data 1 line up. */
99 if (amount > 0) {
100 /* Scroll down. */
101
102 /* Copy existing data up. */
103 if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
104 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin + amount;
105 tr.tr_begin.tp_col = 0;
106 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
107 tr.tr_end.tp_col = t->t_winsize.tp_col;
108 tp.tp_row = t->t_scrollreg.ts_begin;
109 tp.tp_col = 0;
110 teken_funcs_copy(t, &tr, &tp);
111
112 tr.tr_begin.tp_row = t->t_scrollreg.ts_end - amount;
113 } else {
114 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
115 }
116
117 /* Clear the last lines. */
118 tr.tr_begin.tp_col = 0;
119 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
120 tr.tr_end.tp_col = t->t_winsize.tp_col;
121 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
122 } else {
123 /* Scroll up. */
124 amount = -amount;
125
126 /* Copy existing data down. */
127 if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
128 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
129 tr.tr_begin.tp_col = 0;
130 tr.tr_end.tp_row = t->t_scrollreg.ts_end - amount;
131 tr.tr_end.tp_col = t->t_winsize.tp_col;
132 tp.tp_row = t->t_scrollreg.ts_begin + amount;
133 tp.tp_col = 0;
134 teken_funcs_copy(t, &tr, &tp);
135
136 tr.tr_end.tp_row = t->t_scrollreg.ts_begin + amount;
137 } else {
138 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
139 }
140
141 /* Clear the first lines. */
142 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
143 tr.tr_begin.tp_col = 0;
144 tr.tr_end.tp_col = t->t_winsize.tp_col;
145 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
146 }
147}
148
149static ssize_t
150teken_subr_do_cpr(teken_t *t, unsigned int cmd, char response[16])
151{
152
153 switch (cmd) {
154 case 5: /* Operating status. */
155 strcpy(response, "0n");
156 return (2);
157 case 6: { /* Cursor position. */
158 int len;
159
160 len = snprintf(response, 16, "%u;%uR",
161 (t->t_cursor.tp_row - t->t_originreg.ts_begin) + 1,
162 t->t_cursor.tp_col + 1);
163
164 if (len >= 16)
165 return (-1);
166 return (len);
167 }
168 case 15: /* Printer status. */
169 strcpy(response, "13n");
170 return (3);
171 case 25: /* UDK status. */
172 strcpy(response, "20n");
173 return (3);
174 case 26: /* Keyboard status. */
175 strcpy(response, "27;1n");
176 return (5);
177 default:
178 teken_printf("Unknown DSR\n");
179 return (-1);
180 }
181}
182
183static void
184teken_subr_alignment_test(teken_t *t)
185{
186 teken_rect_t tr;
187
188 t->t_scrollreg.ts_begin = 0;
189 t->t_scrollreg.ts_end = t->t_winsize.tp_row;
190
191 t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
192 t->t_stateflags &= ~TS_WRAPPED;
193 teken_funcs_cursor(t);
194
195 tr.tr_begin.tp_row = 0;
196 tr.tr_begin.tp_col = 0;
197 tr.tr_end = t->t_winsize;
198 teken_funcs_fill(t, &tr, 'E', &t->t_defattr);
199}
200
201static void
202teken_subr_backspace(teken_t *t)
203{
204
205 if (t->t_stateflags & TS_CONS25) {
206 if (t->t_cursor.tp_col == 0) {
207 if (t->t_cursor.tp_row == t->t_originreg.ts_begin)
208 return;
209 t->t_cursor.tp_row--;
210 t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
211 } else {
212 t->t_cursor.tp_col--;
213 }
214 } else {
215 if (t->t_cursor.tp_col == 0)
216 return;
217
218 t->t_cursor.tp_col--;
219 t->t_stateflags &= ~TS_WRAPPED;
220 }
221
222 teken_funcs_cursor(t);
223}
224
225static void
226teken_subr_bell(teken_t *t)
227{
228
229 teken_funcs_bell(t);
230}
231
232static void
233teken_subr_carriage_return(teken_t *t)
234{
235
236 t->t_cursor.tp_col = 0;
237 t->t_stateflags &= ~TS_WRAPPED;
238 teken_funcs_cursor(t);
239}
240
241static void
242teken_subr_cursor_backward(teken_t *t, unsigned int ncols)
243{
244
245 if (ncols > t->t_cursor.tp_col)
246 t->t_cursor.tp_col = 0;
247 else
248 t->t_cursor.tp_col -= ncols;
249 t->t_stateflags &= ~TS_WRAPPED;
250 teken_funcs_cursor(t);
251}
252
253static void
254teken_subr_cursor_backward_tabulation(teken_t *t, unsigned int ntabs)
255{
256
257 do {
258 /* Stop when we've reached the beginning of the line. */
259 if (t->t_cursor.tp_col == 0)
260 break;
261
262 t->t_cursor.tp_col--;
263
264 /* Tab marker set. */
265 if (teken_tab_isset(t, t->t_cursor.tp_col))
266 ntabs--;
267 } while (ntabs > 0);
268
269 teken_funcs_cursor(t);
270}
271
272static void
273teken_subr_cursor_down(teken_t *t, unsigned int nrows)
274{
275
276 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end)
277 t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1;
278 else
279 t->t_cursor.tp_row += nrows;
280 t->t_stateflags &= ~TS_WRAPPED;
281 teken_funcs_cursor(t);
282}
283
284static void
285teken_subr_cursor_forward(teken_t *t, unsigned int ncols)
286{
287
288 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col)
289 t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
290 else
291 t->t_cursor.tp_col += ncols;
292 t->t_stateflags &= ~TS_WRAPPED;
293 teken_funcs_cursor(t);
294}
295
296static void
297teken_subr_cursor_forward_tabulation(teken_t *t, unsigned int ntabs)
298{
299
300 do {
301 /* Stop when we've reached the end of the line. */
302 if (t->t_cursor.tp_col == t->t_winsize.tp_col - 1)
303 break;
304
305 t->t_cursor.tp_col++;
306
307 /* Tab marker set. */
308 if (teken_tab_isset(t, t->t_cursor.tp_col))
309 ntabs--;
310 } while (ntabs > 0);
311
312 teken_funcs_cursor(t);
313}
314
315static void
316teken_subr_cursor_next_line(teken_t *t, unsigned int ncols)
317{
318
319 t->t_cursor.tp_col = 0;
320 teken_subr_cursor_down(t, ncols);
321}
322
323static void
324teken_subr_cursor_position(teken_t *t, unsigned int row, unsigned int col)
325{
326
327 t->t_cursor.tp_row = t->t_originreg.ts_begin + row - 1;
328 if (row >= t->t_originreg.ts_end)
329 t->t_cursor.tp_row = t->t_originreg.ts_end - 1;
330
331 t->t_cursor.tp_col = col - 1;
332 if (t->t_cursor.tp_col >= t->t_winsize.tp_col)
333 t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
334
335 t->t_stateflags &= ~TS_WRAPPED;
336 teken_funcs_cursor(t);
337}
338
339static void
340teken_subr_cursor_position_report(teken_t *t, unsigned int cmd)
341{
342 char response[18] = "\x1B[";
343 ssize_t len;
344
345 len = teken_subr_do_cpr(t, cmd, response + 2);
346 if (len < 0)
347 return;
348
349 teken_funcs_respond(t, response, len + 2);
350}
351
352static void
353teken_subr_cursor_previous_line(teken_t *t, unsigned int ncols)
354{
355
356 t->t_cursor.tp_col = 0;
357 teken_subr_cursor_up(t, ncols);
358}
359
360static void
361teken_subr_cursor_up(teken_t *t, unsigned int nrows)
362{
363
364 if (t->t_scrollreg.ts_begin + nrows >= t->t_cursor.tp_row)
365 t->t_cursor.tp_row = t->t_scrollreg.ts_begin;
366 else
367 t->t_cursor.tp_row -= nrows;
368 t->t_stateflags &= ~TS_WRAPPED;
369 teken_funcs_cursor(t);
370}
371
372static void
373teken_subr_delete_character(teken_t *t, unsigned int ncols)
374{
375 teken_rect_t tr;
376
377 tr.tr_begin.tp_row = t->t_cursor.tp_row;
378 tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
379 tr.tr_end.tp_col = t->t_winsize.tp_col;
380
381 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) {
382 tr.tr_begin.tp_col = t->t_cursor.tp_col;
383 } else {
384 /* Copy characters to the left. */
385 tr.tr_begin.tp_col = t->t_cursor.tp_col + ncols;
386 teken_funcs_copy(t, &tr, &t->t_cursor);
387
388 tr.tr_begin.tp_col = t->t_winsize.tp_col - ncols;
389 }
390
391 /* Blank trailing columns. */
392 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
393}
394
395static void
396teken_subr_delete_line(teken_t *t, unsigned int nrows)
397{
398 teken_rect_t tr;
399
400 tr.tr_begin.tp_col = 0;
401 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
402 tr.tr_end.tp_col = t->t_winsize.tp_col;
403
404 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) {
405 tr.tr_begin.tp_row = t->t_cursor.tp_row;
406 } else {
407 teken_pos_t tp;
408
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) {
27 */
28
29static void teken_subr_cursor_up(teken_t *, unsigned int);
30static void teken_subr_erase_line(teken_t *, unsigned int);
31static void teken_subr_regular_character(teken_t *, teken_char_t);
32static void teken_subr_reset_to_initial_state(teken_t *);
33static void teken_subr_save_cursor(teken_t *);
34
35static inline int
36teken_tab_isset(teken_t *t, unsigned int col)
37{
38 unsigned int b, o;
39
40 if (col >= T_NUMCOL)
41 return ((col % 8) == 0);
42
43 b = col / (sizeof(unsigned int) * 8);
44 o = col % (sizeof(unsigned int) * 8);
45
46 return (t->t_tabstops[b] & (1 << o));
47}
48
49static inline void
50teken_tab_clear(teken_t *t, unsigned int col)
51{
52 unsigned int b, o;
53
54 if (col >= T_NUMCOL)
55 return;
56
57 b = col / (sizeof(unsigned int) * 8);
58 o = col % (sizeof(unsigned int) * 8);
59
60 t->t_tabstops[b] &= ~(1 << o);
61}
62
63static inline void
64teken_tab_set(teken_t *t, unsigned int col)
65{
66 unsigned int b, o;
67
68 if (col >= T_NUMCOL)
69 return;
70
71 b = col / (sizeof(unsigned int) * 8);
72 o = col % (sizeof(unsigned int) * 8);
73
74 t->t_tabstops[b] |= 1 << o;
75}
76
77static void
78teken_tab_default(teken_t *t)
79{
80 unsigned int i;
81
82 memset(&t->t_tabstops, 0, T_NUMCOL / 8);
83
84 for (i = 8; i < T_NUMCOL; i += 8)
85 teken_tab_set(t, i);
86}
87
88static void
89teken_subr_do_scroll(teken_t *t, int amount)
90{
91 teken_rect_t tr;
92 teken_pos_t tp;
93
94 teken_assert(t->t_cursor.tp_row <= t->t_winsize.tp_row);
95 teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row);
96 teken_assert(amount != 0);
97
98 /* Copy existing data 1 line up. */
99 if (amount > 0) {
100 /* Scroll down. */
101
102 /* Copy existing data up. */
103 if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
104 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin + amount;
105 tr.tr_begin.tp_col = 0;
106 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
107 tr.tr_end.tp_col = t->t_winsize.tp_col;
108 tp.tp_row = t->t_scrollreg.ts_begin;
109 tp.tp_col = 0;
110 teken_funcs_copy(t, &tr, &tp);
111
112 tr.tr_begin.tp_row = t->t_scrollreg.ts_end - amount;
113 } else {
114 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
115 }
116
117 /* Clear the last lines. */
118 tr.tr_begin.tp_col = 0;
119 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
120 tr.tr_end.tp_col = t->t_winsize.tp_col;
121 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
122 } else {
123 /* Scroll up. */
124 amount = -amount;
125
126 /* Copy existing data down. */
127 if (t->t_scrollreg.ts_begin + amount < t->t_scrollreg.ts_end) {
128 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
129 tr.tr_begin.tp_col = 0;
130 tr.tr_end.tp_row = t->t_scrollreg.ts_end - amount;
131 tr.tr_end.tp_col = t->t_winsize.tp_col;
132 tp.tp_row = t->t_scrollreg.ts_begin + amount;
133 tp.tp_col = 0;
134 teken_funcs_copy(t, &tr, &tp);
135
136 tr.tr_end.tp_row = t->t_scrollreg.ts_begin + amount;
137 } else {
138 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
139 }
140
141 /* Clear the first lines. */
142 tr.tr_begin.tp_row = t->t_scrollreg.ts_begin;
143 tr.tr_begin.tp_col = 0;
144 tr.tr_end.tp_col = t->t_winsize.tp_col;
145 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
146 }
147}
148
149static ssize_t
150teken_subr_do_cpr(teken_t *t, unsigned int cmd, char response[16])
151{
152
153 switch (cmd) {
154 case 5: /* Operating status. */
155 strcpy(response, "0n");
156 return (2);
157 case 6: { /* Cursor position. */
158 int len;
159
160 len = snprintf(response, 16, "%u;%uR",
161 (t->t_cursor.tp_row - t->t_originreg.ts_begin) + 1,
162 t->t_cursor.tp_col + 1);
163
164 if (len >= 16)
165 return (-1);
166 return (len);
167 }
168 case 15: /* Printer status. */
169 strcpy(response, "13n");
170 return (3);
171 case 25: /* UDK status. */
172 strcpy(response, "20n");
173 return (3);
174 case 26: /* Keyboard status. */
175 strcpy(response, "27;1n");
176 return (5);
177 default:
178 teken_printf("Unknown DSR\n");
179 return (-1);
180 }
181}
182
183static void
184teken_subr_alignment_test(teken_t *t)
185{
186 teken_rect_t tr;
187
188 t->t_scrollreg.ts_begin = 0;
189 t->t_scrollreg.ts_end = t->t_winsize.tp_row;
190
191 t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
192 t->t_stateflags &= ~TS_WRAPPED;
193 teken_funcs_cursor(t);
194
195 tr.tr_begin.tp_row = 0;
196 tr.tr_begin.tp_col = 0;
197 tr.tr_end = t->t_winsize;
198 teken_funcs_fill(t, &tr, 'E', &t->t_defattr);
199}
200
201static void
202teken_subr_backspace(teken_t *t)
203{
204
205 if (t->t_stateflags & TS_CONS25) {
206 if (t->t_cursor.tp_col == 0) {
207 if (t->t_cursor.tp_row == t->t_originreg.ts_begin)
208 return;
209 t->t_cursor.tp_row--;
210 t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
211 } else {
212 t->t_cursor.tp_col--;
213 }
214 } else {
215 if (t->t_cursor.tp_col == 0)
216 return;
217
218 t->t_cursor.tp_col--;
219 t->t_stateflags &= ~TS_WRAPPED;
220 }
221
222 teken_funcs_cursor(t);
223}
224
225static void
226teken_subr_bell(teken_t *t)
227{
228
229 teken_funcs_bell(t);
230}
231
232static void
233teken_subr_carriage_return(teken_t *t)
234{
235
236 t->t_cursor.tp_col = 0;
237 t->t_stateflags &= ~TS_WRAPPED;
238 teken_funcs_cursor(t);
239}
240
241static void
242teken_subr_cursor_backward(teken_t *t, unsigned int ncols)
243{
244
245 if (ncols > t->t_cursor.tp_col)
246 t->t_cursor.tp_col = 0;
247 else
248 t->t_cursor.tp_col -= ncols;
249 t->t_stateflags &= ~TS_WRAPPED;
250 teken_funcs_cursor(t);
251}
252
253static void
254teken_subr_cursor_backward_tabulation(teken_t *t, unsigned int ntabs)
255{
256
257 do {
258 /* Stop when we've reached the beginning of the line. */
259 if (t->t_cursor.tp_col == 0)
260 break;
261
262 t->t_cursor.tp_col--;
263
264 /* Tab marker set. */
265 if (teken_tab_isset(t, t->t_cursor.tp_col))
266 ntabs--;
267 } while (ntabs > 0);
268
269 teken_funcs_cursor(t);
270}
271
272static void
273teken_subr_cursor_down(teken_t *t, unsigned int nrows)
274{
275
276 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end)
277 t->t_cursor.tp_row = t->t_scrollreg.ts_end - 1;
278 else
279 t->t_cursor.tp_row += nrows;
280 t->t_stateflags &= ~TS_WRAPPED;
281 teken_funcs_cursor(t);
282}
283
284static void
285teken_subr_cursor_forward(teken_t *t, unsigned int ncols)
286{
287
288 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col)
289 t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
290 else
291 t->t_cursor.tp_col += ncols;
292 t->t_stateflags &= ~TS_WRAPPED;
293 teken_funcs_cursor(t);
294}
295
296static void
297teken_subr_cursor_forward_tabulation(teken_t *t, unsigned int ntabs)
298{
299
300 do {
301 /* Stop when we've reached the end of the line. */
302 if (t->t_cursor.tp_col == t->t_winsize.tp_col - 1)
303 break;
304
305 t->t_cursor.tp_col++;
306
307 /* Tab marker set. */
308 if (teken_tab_isset(t, t->t_cursor.tp_col))
309 ntabs--;
310 } while (ntabs > 0);
311
312 teken_funcs_cursor(t);
313}
314
315static void
316teken_subr_cursor_next_line(teken_t *t, unsigned int ncols)
317{
318
319 t->t_cursor.tp_col = 0;
320 teken_subr_cursor_down(t, ncols);
321}
322
323static void
324teken_subr_cursor_position(teken_t *t, unsigned int row, unsigned int col)
325{
326
327 t->t_cursor.tp_row = t->t_originreg.ts_begin + row - 1;
328 if (row >= t->t_originreg.ts_end)
329 t->t_cursor.tp_row = t->t_originreg.ts_end - 1;
330
331 t->t_cursor.tp_col = col - 1;
332 if (t->t_cursor.tp_col >= t->t_winsize.tp_col)
333 t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
334
335 t->t_stateflags &= ~TS_WRAPPED;
336 teken_funcs_cursor(t);
337}
338
339static void
340teken_subr_cursor_position_report(teken_t *t, unsigned int cmd)
341{
342 char response[18] = "\x1B[";
343 ssize_t len;
344
345 len = teken_subr_do_cpr(t, cmd, response + 2);
346 if (len < 0)
347 return;
348
349 teken_funcs_respond(t, response, len + 2);
350}
351
352static void
353teken_subr_cursor_previous_line(teken_t *t, unsigned int ncols)
354{
355
356 t->t_cursor.tp_col = 0;
357 teken_subr_cursor_up(t, ncols);
358}
359
360static void
361teken_subr_cursor_up(teken_t *t, unsigned int nrows)
362{
363
364 if (t->t_scrollreg.ts_begin + nrows >= t->t_cursor.tp_row)
365 t->t_cursor.tp_row = t->t_scrollreg.ts_begin;
366 else
367 t->t_cursor.tp_row -= nrows;
368 t->t_stateflags &= ~TS_WRAPPED;
369 teken_funcs_cursor(t);
370}
371
372static void
373teken_subr_delete_character(teken_t *t, unsigned int ncols)
374{
375 teken_rect_t tr;
376
377 tr.tr_begin.tp_row = t->t_cursor.tp_row;
378 tr.tr_end.tp_row = t->t_cursor.tp_row + 1;
379 tr.tr_end.tp_col = t->t_winsize.tp_col;
380
381 if (t->t_cursor.tp_col + ncols >= t->t_winsize.tp_col) {
382 tr.tr_begin.tp_col = t->t_cursor.tp_col;
383 } else {
384 /* Copy characters to the left. */
385 tr.tr_begin.tp_col = t->t_cursor.tp_col + ncols;
386 teken_funcs_copy(t, &tr, &t->t_cursor);
387
388 tr.tr_begin.tp_col = t->t_winsize.tp_col - ncols;
389 }
390
391 /* Blank trailing columns. */
392 teken_funcs_fill(t, &tr, BLANK, &t->t_curattr);
393}
394
395static void
396teken_subr_delete_line(teken_t *t, unsigned int nrows)
397{
398 teken_rect_t tr;
399
400 tr.tr_begin.tp_col = 0;
401 tr.tr_end.tp_row = t->t_scrollreg.ts_end;
402 tr.tr_end.tp_col = t->t_winsize.tp_col;
403
404 if (t->t_cursor.tp_row + nrows >= t->t_scrollreg.ts_end) {
405 tr.tr_begin.tp_row = t->t_cursor.tp_row;
406 } else {
407 teken_pos_t tp;
408
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)
805 if (!(t->t_stateflags & TS_CONS25) && (c <= 0x1b || c == 0x7f))
806 return;
806 return;
807 c = teken_scs_process(t, c);
807 width = 1;
808 } else {
809 c = teken_scs_process(t, c);
810 width = teken_wcwidth(c);
811 /* XXX: Don't process zero-width characters yet. */
812 if (width <= 0)
813 return;
814 }
815
816 if (t->t_stateflags & TS_CONS25) {
817 teken_subr_do_putchar(t, &t->t_cursor, c, width);
818 t->t_cursor.tp_col += width;
819
820 if (t->t_cursor.tp_col >= t->t_winsize.tp_col) {
821 if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) {
822 /* Perform scrolling. */
823 teken_subr_do_scroll(t, 1);
824 } else {
825 /* No scrolling needed. */
826 if (t->t_cursor.tp_row <
827 t->t_winsize.tp_row - 1)
828 t->t_cursor.tp_row++;
829 }
830 t->t_cursor.tp_col = 0;
831 }
832 } else if (t->t_cursor.tp_col == t->t_winsize.tp_col - 1 &&
833 (t->t_stateflags & (TS_WRAPPED|TS_AUTOWRAP)) ==
834 (TS_WRAPPED|TS_AUTOWRAP)) {
835 teken_pos_t tp;
836
837 /* Perform line wrapping. */
838
839 if (t->t_cursor.tp_row == t->t_scrollreg.ts_end - 1) {
840 /* Perform scrolling. */
841 teken_subr_do_scroll(t, 1);
842 tp.tp_row = t->t_scrollreg.ts_end - 1;
843 } else {
844 /* No scrolling needed. */
845 tp.tp_row = t->t_cursor.tp_row + 1;
846 if (tp.tp_row == t->t_winsize.tp_row) {
847 /*
848 * Corner case: regular character
849 * outside scrolling region, but at the
850 * bottom of the screen.
851 */
852 teken_subr_do_putchar(t, &t->t_cursor,
853 c, width);
854 return;
855 }
856 }
857
858 tp.tp_col = 0;
859 teken_subr_do_putchar(t, &tp, c, width);
860
861 t->t_cursor.tp_row = tp.tp_row;
862 t->t_cursor.tp_col = width;
863 t->t_stateflags &= ~TS_WRAPPED;
864 } else {
865 /* No line wrapping needed. */
866 teken_subr_do_putchar(t, &t->t_cursor, c, width);
867 t->t_cursor.tp_col += width;
868
869 if (t->t_cursor.tp_col >= t->t_winsize.tp_col) {
870 t->t_stateflags |= TS_WRAPPED;
871 t->t_cursor.tp_col = t->t_winsize.tp_col - 1;
872 } else {
873 t->t_stateflags &= ~TS_WRAPPED;
874 }
875 }
876
877 teken_funcs_cursor(t);
878}
879
880static void
881teken_subr_reset_dec_mode(teken_t *t, unsigned int cmd)
882{
883
884 switch (cmd) {
885 case 1: /* Cursor keys mode. */
886 teken_funcs_param(t, TP_CURSORKEYS, 0);
887 break;
888 case 2: /* DECANM: ANSI/VT52 mode. */
889 teken_printf("DECRST VT52\n");
890 break;
891 case 3: /* 132 column mode. */
892 teken_funcs_param(t, TP_132COLS, 0);
893 teken_subr_reset_to_initial_state(t);
894 break;
895 case 5: /* Inverse video. */
896 teken_printf("DECRST inverse video\n");
897 break;
898 case 6: /* Origin mode. */
899 t->t_stateflags &= ~TS_ORIGIN;
900 t->t_originreg.ts_begin = 0;
901 t->t_originreg.ts_end = t->t_winsize.tp_row;
902 t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
903 t->t_stateflags &= ~TS_WRAPPED;
904 teken_funcs_cursor(t);
905 break;
906 case 7: /* Autowrap mode. */
907 t->t_stateflags &= ~TS_AUTOWRAP;
908 break;
909 case 8: /* Autorepeat mode. */
910 teken_funcs_param(t, TP_AUTOREPEAT, 0);
911 break;
912 case 25: /* Hide cursor. */
913 teken_funcs_param(t, TP_SHOWCURSOR, 0);
914 break;
915 case 40: /* Disallow 132 columns. */
916 teken_printf("DECRST allow 132\n");
917 break;
918 case 45: /* Disable reverse wraparound. */
919 teken_printf("DECRST reverse wraparound\n");
920 break;
921 case 47: /* Switch to alternate buffer. */
922 teken_printf("Switch to alternate buffer\n");
923 break;
924 default:
925 teken_printf("Unknown DECRST: %u\n", cmd);
926 }
927}
928
929static void
930teken_subr_reset_mode(teken_t *t, unsigned int cmd)
931{
932
933 switch (cmd) {
934 case 4:
935 t->t_stateflags &= ~TS_INSERT;
936 break;
937 default:
938 teken_printf("Unknown reset mode: %u\n", cmd);
939 }
940}
941
942static void
943teken_subr_do_reset(teken_t *t)
944{
945
946 t->t_curattr = t->t_defattr;
947 t->t_cursor.tp_row = t->t_cursor.tp_col = 0;
948 t->t_scrollreg.ts_begin = 0;
949 t->t_scrollreg.ts_end = t->t_winsize.tp_row;
950 t->t_originreg = t->t_scrollreg;
951 t->t_stateflags &= TS_8BIT|TS_CONS25;
952 t->t_stateflags |= TS_AUTOWRAP;
953
954 teken_scs_set(t, 0, teken_scs_us_ascii);
955 teken_scs_set(t, 1, teken_scs_us_ascii);
956 teken_scs_switch(t, 0);
957
958 teken_subr_save_cursor(t);
959 teken_tab_default(t);
960}
961
962static void
963teken_subr_reset_to_initial_state(teken_t *t)
964{
965
966 teken_subr_do_reset(t);
967 teken_subr_erase_display(t, 2);
968 teken_funcs_param(t, TP_SHOWCURSOR, 1);
969 teken_funcs_cursor(t);
970}
971
972static void
973teken_subr_restore_cursor(teken_t *t)
974{
975
976 t->t_cursor = t->t_saved_cursor;
977 t->t_curattr = t->t_saved_curattr;
978 t->t_stateflags &= ~TS_WRAPPED;
979 teken_scs_restore(t);
980 teken_funcs_cursor(t);
981}
982
983static void
984teken_subr_reverse_index(teken_t *t)
985{
986
987 if (t->t_cursor.tp_row > t->t_scrollreg.ts_begin) {
988 t->t_cursor.tp_row--;
989 t->t_stateflags &= ~TS_WRAPPED;
990 teken_funcs_cursor(t);
991 } else {
992 teken_subr_do_scroll(t, -1);
993 }
994}
995
996static void
997teken_subr_save_cursor(teken_t *t)
998{
999
1000 t->t_saved_cursor = t->t_cursor;
1001 t->t_saved_curattr = t->t_curattr;
1002 teken_scs_save(t);
1003}
1004
1005static void
1006teken_subr_secondary_device_attributes(teken_t *t, unsigned int request)
1007{
1008
1009 if (request == 0) {
1010 const char response[] = "\x1B[>0;10;0c";
1011 teken_funcs_respond(t, response, sizeof response - 1);
1012 } else {
1013 teken_printf("Unknown DA2\n");
1014 }
1015}
1016
1017static void
1018teken_subr_set_dec_mode(teken_t *t, unsigned int cmd)
1019{
1020
1021 switch (cmd) {
1022 case 1: /* Cursor keys mode. */
1023 teken_funcs_param(t, TP_CURSORKEYS, 1);
1024 break;
1025 case 2: /* DECANM: ANSI/VT52 mode. */
1026 teken_printf("DECSET VT52\n");
1027 break;
1028 case 3: /* 132 column mode. */
1029 teken_funcs_param(t, TP_132COLS, 1);
1030 teken_subr_reset_to_initial_state(t);
1031 break;
1032 case 5: /* Inverse video. */
1033 teken_printf("DECSET inverse video\n");
1034 break;
1035 case 6: /* Origin mode. */
1036 t->t_stateflags |= TS_ORIGIN;
1037 t->t_originreg = t->t_scrollreg;
1038 t->t_cursor.tp_row = t->t_scrollreg.ts_begin;
1039 t->t_cursor.tp_col = 0;
1040 t->t_stateflags &= ~TS_WRAPPED;
1041 teken_funcs_cursor(t);
1042 break;
1043 case 7: /* Autowrap mode. */
1044 t->t_stateflags |= TS_AUTOWRAP;
1045 break;
1046 case 8: /* Autorepeat mode. */
1047 teken_funcs_param(t, TP_AUTOREPEAT, 1);
1048 break;
1049 case 25: /* Display cursor. */
1050 teken_funcs_param(t, TP_SHOWCURSOR, 1);
1051 break;
1052 case 40: /* Allow 132 columns. */
1053 teken_printf("DECSET allow 132\n");
1054 break;
1055 case 45: /* Enable reverse wraparound. */
1056 teken_printf("DECSET reverse wraparound\n");
1057 break;
1058 case 47: /* Switch to alternate buffer. */
1059 teken_printf("Switch away from alternate buffer\n");
1060 break;
1061 default:
1062 teken_printf("Unknown DECSET: %u\n", cmd);
1063 }
1064}
1065
1066static void
1067teken_subr_set_mode(teken_t *t, unsigned int cmd)
1068{
1069
1070 switch (cmd) {
1071 case 4:
1072 teken_printf("Insert mode\n");
1073 t->t_stateflags |= TS_INSERT;
1074 break;
1075 default:
1076 teken_printf("Unknown set mode: %u\n", cmd);
1077 }
1078}
1079
1080static void
1081teken_subr_set_graphic_rendition(teken_t *t, unsigned int ncmds,
1082 unsigned int cmds[])
1083{
1084 unsigned int i, n;
1085
1086 /* No attributes means reset. */
1087 if (ncmds == 0) {
1088 t->t_curattr = t->t_defattr;
1089 return;
1090 }
1091
1092 for (i = 0; i < ncmds; i++) {
1093 n = cmds[i];
1094
1095 switch (n) {
1096 case 0: /* Reset. */
1097 t->t_curattr = t->t_defattr;
1098 break;
1099 case 1: /* Bold. */
1100 t->t_curattr.ta_format |= TF_BOLD;
1101 break;
1102 case 4: /* Underline. */
1103 t->t_curattr.ta_format |= TF_UNDERLINE;
1104 break;
1105 case 5: /* Blink. */
1106 t->t_curattr.ta_format |= TF_BLINK;
1107 break;
1108 case 7: /* Reverse. */
1109 t->t_curattr.ta_format |= TF_REVERSE;
1110 break;
1111 case 22: /* Remove bold. */
1112 t->t_curattr.ta_format &= ~TF_BOLD;
1113 break;
1114 case 24: /* Remove underline. */
1115 t->t_curattr.ta_format &= ~TF_UNDERLINE;
1116 break;
1117 case 25: /* Remove blink. */
1118 t->t_curattr.ta_format &= ~TF_BLINK;
1119 break;
1120 case 27: /* Remove reverse. */
1121 t->t_curattr.ta_format &= ~TF_REVERSE;
1122 break;
1123 case 30: /* Set foreground color: black */
1124 case 31: /* Set foreground color: red */
1125 case 32: /* Set foreground color: green */
1126 case 33: /* Set foreground color: brown */
1127 case 34: /* Set foreground color: blue */
1128 case 35: /* Set foreground color: magenta */
1129 case 36: /* Set foreground color: cyan */
1130 case 37: /* Set foreground color: white */
1131 t->t_curattr.ta_fgcolor = n - 30;
1132 break;
1133 case 39: /* Set default foreground color. */
1134 t->t_curattr.ta_fgcolor = t->t_defattr.ta_fgcolor;
1135 break;
1136 case 40: /* Set background color: black */
1137 case 41: /* Set background color: red */
1138 case 42: /* Set background color: green */
1139 case 43: /* Set background color: brown */
1140 case 44: /* Set background color: blue */
1141 case 45: /* Set background color: magenta */
1142 case 46: /* Set background color: cyan */
1143 case 47: /* Set background color: white */
1144 t->t_curattr.ta_bgcolor = n - 40;
1145 break;
1146 case 49: /* Set default background color. */
1147 t->t_curattr.ta_bgcolor = t->t_defattr.ta_bgcolor;
1148 break;
1149 default:
1150 teken_printf("unsupported attribute %u\n", n);
1151 }
1152 }
1153}
1154
1155static void
1156teken_subr_set_top_and_bottom_margins(teken_t *t, unsigned int top,
1157 unsigned int bottom)
1158{
1159
1160 /* Adjust top row number. */
1161 if (top > 0)
1162 top--;
1163 /* Adjust bottom row number. */
1164 if (bottom == 0 || bottom > t->t_winsize.tp_row)
1165 bottom = t->t_winsize.tp_row;
1166
1167 /* Invalid arguments. */
1168 if (top >= bottom - 1) {
1169 top = 0;
1170 bottom = t->t_winsize.tp_row;
1171 }
1172
1173 t->t_scrollreg.ts_begin = top;
1174 t->t_scrollreg.ts_end = bottom;
1175 if (t->t_stateflags & TS_ORIGIN) {
1176 /* XXX: home cursor? */
1177 t->t_originreg = t->t_scrollreg;
1178 t->t_cursor.tp_row = t->t_originreg.ts_begin;
1179 t->t_cursor.tp_col = 0;
1180 t->t_stateflags &= ~TS_WRAPPED;
1181 teken_funcs_cursor(t);
1182 }
1183}
1184
1185static void
1186teken_subr_single_height_double_width_line(teken_t *t __unused)
1187{
1188
1189 teken_printf("single height double width???\n");
1190}
1191
1192static void
1193teken_subr_single_height_single_width_line(teken_t *t __unused)
1194{
1195
1196 teken_printf("single height single width???\n");
1197}
1198
1199static void
1200teken_subr_string_terminator(teken_t *t __unused)
1201{
1202
1203 teken_printf("string terminator???\n");
1204}
1205
1206static void
1207teken_subr_tab_clear(teken_t *t, unsigned int cmd)
1208{
1209
1210 switch (cmd) {
1211 case 0:
1212 teken_tab_clear(t, t->t_cursor.tp_col);
1213 break;
1214 case 3:
1215 memset(&t->t_tabstops, 0, T_NUMCOL / 8);
1216 break;
1217 }
1218}
1219
1220static void
1221teken_subr_vertical_position_absolute(teken_t *t, unsigned int row)
1222{
1223
1224 t->t_cursor.tp_row = t->t_originreg.ts_begin + row - 1;
1225 if (row >= t->t_originreg.ts_end)
1226 t->t_cursor.tp_row = t->t_originreg.ts_end - 1;
1227
1228
1229 t->t_stateflags &= ~TS_WRAPPED;
1230 teken_funcs_cursor(t);
1231}
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}