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