2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 static void teken_subr_cursor_up(teken_t
*, unsigned int);
30 static void teken_subr_erase_line(teken_t
*, unsigned int);
31 static void teken_subr_regular_character(teken_t
*, teken_char_t
);
32 static void teken_subr_reset_to_initial_state(teken_t
*);
33 static void teken_subr_save_cursor(teken_t
*);
36 teken_tab_isset(teken_t
*t
, unsigned int col
)
41 return ((col
% 8) == 0);
43 b
= col
/ (sizeof(unsigned int) * 8);
44 o
= col
% (sizeof(unsigned int) * 8);
46 return (t
->t_tabstops
[b
] & (1 << o
));
50 teken_tab_clear(teken_t
*t
, unsigned int col
)
57 b
= col
/ (sizeof(unsigned int) * 8);
58 o
= col
% (sizeof(unsigned int) * 8);
60 t
->t_tabstops
[b
] &= ~(1 << o
);
64 teken_tab_set(teken_t
*t
, unsigned int col
)
71 b
= col
/ (sizeof(unsigned int) * 8);
72 o
= col
% (sizeof(unsigned int) * 8);
74 t
->t_tabstops
[b
] |= 1 << o
;
78 teken_tab_default(teken_t
*t
)
82 memset(&t
->t_tabstops
, 0, T_NUMCOL
/ 8);
84 for (i
= 8; i
< T_NUMCOL
; i
+= 8)
89 teken_subr_do_scroll(teken_t
*t
, int amount
)
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);
98 /* Copy existing data 1 line up. */
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
;
110 teken_funcs_copy(t
, &tr
, &tp
);
112 tr
.tr_begin
.tp_row
= t
->t_scrollreg
.ts_end
- amount
;
114 tr
.tr_begin
.tp_row
= t
->t_scrollreg
.ts_begin
;
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
);
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
;
134 teken_funcs_copy(t
, &tr
, &tp
);
136 tr
.tr_end
.tp_row
= t
->t_scrollreg
.ts_begin
+ amount
;
138 tr
.tr_end
.tp_row
= t
->t_scrollreg
.ts_end
;
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
);
150 teken_subr_do_cpr(teken_t
*t
, unsigned int cmd
, char response
[16])
154 case 5: /* Operating status. */
155 strcpy(response
, "0n");
157 case 6: { /* Cursor position. */
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);
168 case 15: /* Printer status. */
169 strcpy(response
, "13n");
171 case 25: /* UDK status. */
172 strcpy(response
, "20n");
174 case 26: /* Keyboard status. */
175 strcpy(response
, "27;1n");
178 teken_printf("Unknown DSR\n");
184 teken_subr_alignment_test(teken_t
*t
)
188 t
->t_cursor
.tp_row
= t
->t_cursor
.tp_col
= 0;
189 t
->t_scrollreg
.ts_begin
= 0;
190 t
->t_scrollreg
.ts_end
= t
->t_winsize
.tp_row
;
191 t
->t_originreg
= t
->t_scrollreg
;
192 t
->t_stateflags
&= ~(TS_WRAPPED
|TS_ORIGIN
);
193 teken_funcs_cursor(t
);
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
);
202 teken_subr_backspace(teken_t
*t
)
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
)
209 t
->t_cursor
.tp_row
--;
210 t
->t_cursor
.tp_col
= t
->t_winsize
.tp_col
- 1;
212 t
->t_cursor
.tp_col
--;
215 if (t
->t_cursor
.tp_col
== 0)
218 t
->t_cursor
.tp_col
--;
219 t
->t_stateflags
&= ~TS_WRAPPED
;
222 teken_funcs_cursor(t
);
226 teken_subr_bell(teken_t
*t
)
233 teken_subr_carriage_return(teken_t
*t
)
236 t
->t_cursor
.tp_col
= 0;
237 t
->t_stateflags
&= ~TS_WRAPPED
;
238 teken_funcs_cursor(t
);
242 teken_subr_cursor_backward(teken_t
*t
, unsigned int ncols
)
245 if (ncols
> t
->t_cursor
.tp_col
)
246 t
->t_cursor
.tp_col
= 0;
248 t
->t_cursor
.tp_col
-= ncols
;
249 t
->t_stateflags
&= ~TS_WRAPPED
;
250 teken_funcs_cursor(t
);
254 teken_subr_cursor_backward_tabulation(teken_t
*t
, unsigned int ntabs
)
258 /* Stop when we've reached the beginning of the line. */
259 if (t
->t_cursor
.tp_col
== 0)
262 t
->t_cursor
.tp_col
--;
264 /* Tab marker set. */
265 if (teken_tab_isset(t
, t
->t_cursor
.tp_col
))
269 teken_funcs_cursor(t
);
273 teken_subr_cursor_down(teken_t
*t
, unsigned int nrows
)
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;
279 t
->t_cursor
.tp_row
+= nrows
;
280 t
->t_stateflags
&= ~TS_WRAPPED
;
281 teken_funcs_cursor(t
);
285 teken_subr_cursor_forward(teken_t
*t
, unsigned int ncols
)
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;
291 t
->t_cursor
.tp_col
+= ncols
;
292 t
->t_stateflags
&= ~TS_WRAPPED
;
293 teken_funcs_cursor(t
);
297 teken_subr_cursor_forward_tabulation(teken_t
*t
, unsigned int ntabs
)
301 /* Stop when we've reached the end of the line. */
302 if (t
->t_cursor
.tp_col
== t
->t_winsize
.tp_col
- 1)
305 t
->t_cursor
.tp_col
++;
307 /* Tab marker set. */
308 if (teken_tab_isset(t
, t
->t_cursor
.tp_col
))
312 teken_funcs_cursor(t
);
316 teken_subr_cursor_next_line(teken_t
*t
, unsigned int ncols
)
319 t
->t_cursor
.tp_col
= 0;
320 teken_subr_cursor_down(t
, ncols
);
324 teken_subr_cursor_position(teken_t
*t
, unsigned int row
, unsigned int col
)
327 row
= row
- 1 + t
->t_originreg
.ts_begin
;
328 t
->t_cursor
.tp_row
= row
< t
->t_originreg
.ts_end
?
329 row
: t
->t_originreg
.ts_end
- 1;
332 t
->t_cursor
.tp_col
= col
< t
->t_winsize
.tp_col
?
333 col
: t
->t_winsize
.tp_col
- 1;
335 t
->t_stateflags
&= ~TS_WRAPPED
;
336 teken_funcs_cursor(t
);
340 teken_subr_cursor_position_report(teken_t
*t
, unsigned int cmd
)
342 char response
[18] = "\x1B[";
345 len
= teken_subr_do_cpr(t
, cmd
, response
+ 2);
349 teken_funcs_respond(t
, response
, len
+ 2);
353 teken_subr_cursor_previous_line(teken_t
*t
, unsigned int ncols
)
356 t
->t_cursor
.tp_col
= 0;
357 teken_subr_cursor_up(t
, ncols
);
361 teken_subr_cursor_up(teken_t
*t
, unsigned int nrows
)
364 if (t
->t_scrollreg
.ts_begin
+ nrows
>= t
->t_cursor
.tp_row
)
365 t
->t_cursor
.tp_row
= t
->t_scrollreg
.ts_begin
;
367 t
->t_cursor
.tp_row
-= nrows
;
368 t
->t_stateflags
&= ~TS_WRAPPED
;
369 teken_funcs_cursor(t
);
373 teken_subr_delete_character(teken_t
*t
, unsigned int ncols
)
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
;
381 if (t
->t_cursor
.tp_col
+ ncols
>= t
->t_winsize
.tp_col
) {
382 tr
.tr_begin
.tp_col
= t
->t_cursor
.tp_col
;
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
);
388 tr
.tr_begin
.tp_col
= t
->t_winsize
.tp_col
- ncols
;
391 /* Blank trailing columns. */
392 teken_funcs_fill(t
, &tr
, BLANK
, &t
->t_curattr
);
396 teken_subr_delete_line(teken_t
*t
, unsigned int nrows
)
400 /* Ignore if outside scrolling region. */
401 if (t
->t_cursor
.tp_row
< t
->t_scrollreg
.ts_begin
||
402 t
->t_cursor
.tp_row
>= t
->t_scrollreg
.ts_end
)
405 tr
.tr_begin
.tp_col
= 0;
406 tr
.tr_end
.tp_row
= t
->t_scrollreg
.ts_end
;
407 tr
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
409 if (t
->t_cursor
.tp_row
+ nrows
>= t
->t_scrollreg
.ts_end
) {
410 tr
.tr_begin
.tp_row
= t
->t_cursor
.tp_row
;
415 tr
.tr_begin
.tp_row
= t
->t_cursor
.tp_row
+ nrows
;
416 tp
.tp_row
= t
->t_cursor
.tp_row
;
418 teken_funcs_copy(t
, &tr
, &tp
);
420 tr
.tr_begin
.tp_row
= t
->t_scrollreg
.ts_end
- nrows
;
423 /* Blank trailing rows. */
424 teken_funcs_fill(t
, &tr
, BLANK
, &t
->t_curattr
);
428 teken_subr_device_control_string(teken_t
*t
)
431 teken_printf("Unsupported device control string\n");
432 t
->t_stateflags
|= TS_INSTRING
;
436 teken_subr_device_status_report(teken_t
*t
, unsigned int cmd
)
438 char response
[19] = "\x1B[?";
441 len
= teken_subr_do_cpr(t
, cmd
, response
+ 3);
445 teken_funcs_respond(t
, response
, len
+ 3);
449 teken_subr_double_height_double_width_line_top(teken_t
*t __unused
)
452 teken_printf("double height double width top\n");
456 teken_subr_double_height_double_width_line_bottom(teken_t
*t __unused
)
459 teken_printf("double height double width bottom\n");
463 teken_subr_erase_character(teken_t
*t
, unsigned int ncols
)
467 tr
.tr_begin
= t
->t_cursor
;
468 tr
.tr_end
.tp_row
= t
->t_cursor
.tp_row
+ 1;
470 if (t
->t_cursor
.tp_col
+ ncols
>= t
->t_winsize
.tp_col
)
471 tr
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
473 tr
.tr_end
.tp_col
= t
->t_cursor
.tp_col
+ ncols
;
475 teken_funcs_fill(t
, &tr
, BLANK
, &t
->t_curattr
);
479 teken_subr_erase_display(teken_t
*t
, unsigned int mode
)
483 r
.tr_begin
.tp_col
= 0;
484 r
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
487 case 1: /* Erase from the top to the cursor. */
488 teken_subr_erase_line(t
, 1);
490 /* Erase lines above. */
491 if (t
->t_cursor
.tp_row
== 0)
493 r
.tr_begin
.tp_row
= 0;
494 r
.tr_end
.tp_row
= t
->t_cursor
.tp_row
;
496 case 2: /* Erase entire display. */
497 r
.tr_begin
.tp_row
= 0;
498 r
.tr_end
.tp_row
= t
->t_winsize
.tp_row
;
500 default: /* Erase from cursor to the bottom. */
501 teken_subr_erase_line(t
, 0);
503 /* Erase lines below. */
504 if (t
->t_cursor
.tp_row
== t
->t_winsize
.tp_row
- 1)
506 r
.tr_begin
.tp_row
= t
->t_cursor
.tp_row
+ 1;
507 r
.tr_end
.tp_row
= t
->t_winsize
.tp_row
;
511 teken_funcs_fill(t
, &r
, BLANK
, &t
->t_curattr
);
515 teken_subr_erase_line(teken_t
*t
, unsigned int mode
)
519 r
.tr_begin
.tp_row
= t
->t_cursor
.tp_row
;
520 r
.tr_end
.tp_row
= t
->t_cursor
.tp_row
+ 1;
523 case 1: /* Erase from the beginning of the line to the cursor. */
524 r
.tr_begin
.tp_col
= 0;
525 r
.tr_end
.tp_col
= t
->t_cursor
.tp_col
+ 1;
527 case 2: /* Erase entire line. */
528 r
.tr_begin
.tp_col
= 0;
529 r
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
531 default: /* Erase from cursor to the end of the line. */
532 r
.tr_begin
.tp_col
= t
->t_cursor
.tp_col
;
533 r
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
537 teken_funcs_fill(t
, &r
, BLANK
, &t
->t_curattr
);
541 teken_subr_g0_scs_special_graphics(teken_t
*t __unused
)
544 t
->t_scs
[0] = teken_scs_special_graphics
;
548 teken_subr_g0_scs_uk_national(teken_t
*t __unused
)
551 t
->t_scs
[0] = teken_scs_uk_national
;
555 teken_subr_g0_scs_us_ascii(teken_t
*t __unused
)
558 t
->t_scs
[0] = teken_scs_us_ascii
;
562 teken_subr_g1_scs_special_graphics(teken_t
*t __unused
)
565 t
->t_scs
[1] = teken_scs_special_graphics
;
569 teken_subr_g1_scs_uk_national(teken_t
*t __unused
)
572 t
->t_scs
[1] = teken_scs_uk_national
;
576 teken_subr_g1_scs_us_ascii(teken_t
*t __unused
)
579 t
->t_scs
[1] = teken_scs_us_ascii
;
583 teken_subr_horizontal_position_absolute(teken_t
*t
, unsigned int col
)
587 t
->t_cursor
.tp_col
= col
< t
->t_winsize
.tp_col
?
588 col
: t
->t_winsize
.tp_col
- 1;
590 t
->t_stateflags
&= ~TS_WRAPPED
;
591 teken_funcs_cursor(t
);
595 teken_subr_horizontal_tab(teken_t
*t
)
598 teken_subr_cursor_forward_tabulation(t
, 1);
602 teken_subr_horizontal_tab_set(teken_t
*t
)
605 teken_tab_set(t
, t
->t_cursor
.tp_col
);
609 teken_subr_index(teken_t
*t
)
612 if (t
->t_cursor
.tp_row
< t
->t_scrollreg
.ts_end
- 1) {
613 t
->t_cursor
.tp_row
++;
614 t
->t_stateflags
&= ~TS_WRAPPED
;
615 teken_funcs_cursor(t
);
617 teken_subr_do_scroll(t
, 1);
622 teken_subr_insert_character(teken_t
*t
, unsigned int ncols
)
626 tr
.tr_begin
= t
->t_cursor
;
627 tr
.tr_end
.tp_row
= t
->t_cursor
.tp_row
+ 1;
629 if (t
->t_cursor
.tp_col
+ ncols
>= t
->t_winsize
.tp_col
) {
630 tr
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
634 /* Copy characters to the right. */
635 tr
.tr_end
.tp_col
= t
->t_winsize
.tp_col
- ncols
;
636 tp
.tp_row
= t
->t_cursor
.tp_row
;
637 tp
.tp_col
= t
->t_cursor
.tp_col
+ ncols
;
638 teken_funcs_copy(t
, &tr
, &tp
);
640 tr
.tr_end
.tp_col
= t
->t_cursor
.tp_col
+ ncols
;
643 /* Blank current location. */
644 teken_funcs_fill(t
, &tr
, BLANK
, &t
->t_curattr
);
648 teken_subr_insert_line(teken_t
*t
, unsigned int nrows
)
652 /* Ignore if outside scrolling region. */
653 if (t
->t_cursor
.tp_row
< t
->t_scrollreg
.ts_begin
||
654 t
->t_cursor
.tp_row
>= t
->t_scrollreg
.ts_end
)
657 tr
.tr_begin
.tp_row
= t
->t_cursor
.tp_row
;
658 tr
.tr_begin
.tp_col
= 0;
659 tr
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
661 if (t
->t_cursor
.tp_row
+ nrows
>= t
->t_scrollreg
.ts_end
) {
662 tr
.tr_end
.tp_row
= t
->t_scrollreg
.ts_end
;
666 /* Copy lines down. */
667 tr
.tr_end
.tp_row
= t
->t_scrollreg
.ts_end
- nrows
;
668 tp
.tp_row
= t
->t_cursor
.tp_row
+ nrows
;
670 teken_funcs_copy(t
, &tr
, &tp
);
672 tr
.tr_end
.tp_row
= t
->t_cursor
.tp_row
+ nrows
;
675 /* Blank current location. */
676 teken_funcs_fill(t
, &tr
, BLANK
, &t
->t_curattr
);
680 teken_subr_keypad_application_mode(teken_t
*t
)
683 teken_funcs_param(t
, TP_KEYPADAPP
, 1);
687 teken_subr_keypad_numeric_mode(teken_t
*t
)
690 teken_funcs_param(t
, TP_KEYPADAPP
, 0);
694 teken_subr_newline(teken_t
*t
)
697 t
->t_cursor
.tp_row
++;
699 if (t
->t_cursor
.tp_row
>= t
->t_scrollreg
.ts_end
) {
700 teken_subr_do_scroll(t
, 1);
701 t
->t_cursor
.tp_row
= t
->t_scrollreg
.ts_end
- 1;
704 t
->t_stateflags
&= ~TS_WRAPPED
;
705 teken_funcs_cursor(t
);
709 teken_subr_newpage(teken_t
*t
)
712 if (t
->t_stateflags
& TS_CONS25
) {
716 tr
.tr_begin
.tp_row
= t
->t_originreg
.ts_begin
;
717 tr
.tr_begin
.tp_col
= 0;
718 tr
.tr_end
.tp_row
= t
->t_originreg
.ts_end
;
719 tr
.tr_end
.tp_col
= t
->t_winsize
.tp_col
;
720 teken_funcs_fill(t
, &tr
, BLANK
, &t
->t_curattr
);
722 /* Cursor at top left. */
723 t
->t_cursor
.tp_row
= t
->t_originreg
.ts_begin
;
724 t
->t_cursor
.tp_col
= 0;
725 t
->t_stateflags
&= ~TS_WRAPPED
;
726 teken_funcs_cursor(t
);
728 teken_subr_newline(t
);
733 teken_subr_next_line(teken_t
*t
)
736 t
->t_cursor
.tp_col
= 0;
737 teken_subr_newline(t
);
741 teken_subr_operating_system_command(teken_t
*t
)
744 teken_printf("Unsupported operating system command\n");
745 t
->t_stateflags
|= TS_INSTRING
;
749 teken_subr_pan_down(teken_t
*t
, unsigned int nrows
)
752 teken_subr_do_scroll(t
, (int)nrows
);
756 teken_subr_pan_up(teken_t
*t
, unsigned int nrows
)
759 teken_subr_do_scroll(t
, -(int)nrows
);
763 teken_subr_primary_device_attributes(teken_t
*t
, unsigned int request
)
767 const char response
[] = "\x1B[?1;2c";
769 teken_funcs_respond(t
, response
, sizeof response
- 1);
771 teken_printf("Unknown DA1\n");
776 teken_subr_do_putchar(teken_t
*t
, const teken_pos_t
*tp
, teken_char_t c
,
780 if (t
->t_stateflags
& TS_INSERT
&&
781 tp
->tp_col
< t
->t_winsize
.tp_col
- width
) {
785 /* Insert mode. Move existing characters to the right. */
787 ctr
.tr_end
.tp_row
= tp
->tp_row
+ 1;
788 ctr
.tr_end
.tp_col
= t
->t_winsize
.tp_col
- width
;
789 ctp
.tp_row
= tp
->tp_row
;
790 ctp
.tp_col
= tp
->tp_col
+ width
;
791 teken_funcs_copy(t
, &ctr
, &ctp
);
794 teken_funcs_putchar(t
, tp
, c
, &t
->t_curattr
);
796 if (width
== 2 && tp
->tp_col
+ 1 < t
->t_winsize
.tp_col
) {
800 /* Print second half of CJK fullwidth character. */
801 tp2
.tp_row
= tp
->tp_row
;
802 tp2
.tp_col
= tp
->tp_col
+ 1;
804 attr
.ta_format
|= TF_CJK_RIGHT
;
805 teken_funcs_putchar(t
, &tp2
, c
, &attr
);
810 teken_subr_regular_character(teken_t
*t
, teken_char_t c
)
814 if (t
->t_stateflags
& TS_8BIT
) {
815 if (!(t
->t_stateflags
& TS_CONS25
) && (c
<= 0x1b || c
== 0x7f))
817 c
= teken_scs_process(t
, c
);
820 c
= teken_scs_process(t
, c
);
821 width
= teken_wcwidth(c
);
822 /* XXX: Don't process zero-width characters yet. */
827 if (t
->t_stateflags
& TS_CONS25
) {
828 teken_subr_do_putchar(t
, &t
->t_cursor
, c
, width
);
829 t
->t_cursor
.tp_col
+= width
;
831 if (t
->t_cursor
.tp_col
>= t
->t_winsize
.tp_col
) {
832 if (t
->t_cursor
.tp_row
== t
->t_scrollreg
.ts_end
- 1) {
833 /* Perform scrolling. */
834 teken_subr_do_scroll(t
, 1);
836 /* No scrolling needed. */
837 if (t
->t_cursor
.tp_row
<
838 t
->t_winsize
.tp_row
- 1)
839 t
->t_cursor
.tp_row
++;
841 t
->t_cursor
.tp_col
= 0;
843 } else if (t
->t_stateflags
& TS_AUTOWRAP
&&
844 ((t
->t_stateflags
& TS_WRAPPED
&&
845 t
->t_cursor
.tp_col
+ 1 == t
->t_winsize
.tp_col
) ||
846 t
->t_cursor
.tp_col
+ width
> t
->t_winsize
.tp_col
)) {
850 * Perform line wrapping, if:
851 * - Autowrapping is enabled, and
852 * - We're in the wrapped state at the last column, or
853 * - The character to be printed does not fit anymore.
855 if (t
->t_cursor
.tp_row
== t
->t_scrollreg
.ts_end
- 1) {
856 /* Perform scrolling. */
857 teken_subr_do_scroll(t
, 1);
858 tp
.tp_row
= t
->t_scrollreg
.ts_end
- 1;
860 /* No scrolling needed. */
861 tp
.tp_row
= t
->t_cursor
.tp_row
+ 1;
862 if (tp
.tp_row
== t
->t_winsize
.tp_row
) {
864 * Corner case: regular character
865 * outside scrolling region, but at the
866 * bottom of the screen.
868 teken_subr_do_putchar(t
, &t
->t_cursor
,
875 teken_subr_do_putchar(t
, &tp
, c
, width
);
877 t
->t_cursor
.tp_row
= tp
.tp_row
;
878 t
->t_cursor
.tp_col
= width
;
879 t
->t_stateflags
&= ~TS_WRAPPED
;
881 /* No line wrapping needed. */
882 teken_subr_do_putchar(t
, &t
->t_cursor
, c
, width
);
883 t
->t_cursor
.tp_col
+= width
;
885 if (t
->t_cursor
.tp_col
>= t
->t_winsize
.tp_col
) {
886 t
->t_stateflags
|= TS_WRAPPED
;
887 t
->t_cursor
.tp_col
= t
->t_winsize
.tp_col
- 1;
889 t
->t_stateflags
&= ~TS_WRAPPED
;
893 teken_funcs_cursor(t
);
897 teken_subr_reset_dec_mode(teken_t
*t
, unsigned int cmd
)
901 case 1: /* Cursor keys mode. */
902 t
->t_stateflags
&= ~TS_CURSORKEYS
;
904 case 2: /* DECANM: ANSI/VT52 mode. */
905 teken_printf("DECRST VT52\n");
907 case 3: /* 132 column mode. */
908 teken_funcs_param(t
, TP_132COLS
, 0);
909 teken_subr_reset_to_initial_state(t
);
911 case 5: /* Inverse video. */
912 teken_printf("DECRST inverse video\n");
914 case 6: /* Origin mode. */
915 t
->t_stateflags
&= ~TS_ORIGIN
;
916 t
->t_originreg
.ts_begin
= 0;
917 t
->t_originreg
.ts_end
= t
->t_winsize
.tp_row
;
918 t
->t_cursor
.tp_row
= t
->t_cursor
.tp_col
= 0;
919 t
->t_stateflags
&= ~TS_WRAPPED
;
920 teken_funcs_cursor(t
);
922 case 7: /* Autowrap mode. */
923 t
->t_stateflags
&= ~TS_AUTOWRAP
;
925 case 8: /* Autorepeat mode. */
926 teken_funcs_param(t
, TP_AUTOREPEAT
, 0);
928 case 25: /* Hide cursor. */
929 teken_funcs_param(t
, TP_SHOWCURSOR
, 0);
931 case 40: /* Disallow 132 columns. */
932 teken_printf("DECRST allow 132\n");
934 case 45: /* Disable reverse wraparound. */
935 teken_printf("DECRST reverse wraparound\n");
937 case 47: /* Switch to alternate buffer. */
938 teken_printf("Switch to alternate buffer\n");
940 case 1000: /* Mouse input. */
941 teken_funcs_param(t
, TP_MOUSE
, 0);
944 teken_printf("Unknown DECRST: %u\n", cmd
);
949 teken_subr_reset_mode(teken_t
*t
, unsigned int cmd
)
954 t
->t_stateflags
&= ~TS_INSERT
;
957 teken_printf("Unknown reset mode: %u\n", cmd
);
962 teken_subr_do_resize(teken_t
*t
)
965 t
->t_scrollreg
.ts_begin
= 0;
966 t
->t_scrollreg
.ts_end
= t
->t_winsize
.tp_row
;
967 t
->t_originreg
= t
->t_scrollreg
;
971 teken_subr_do_reset(teken_t
*t
)
974 t
->t_curattr
= t
->t_defattr
;
975 t
->t_cursor
.tp_row
= t
->t_cursor
.tp_col
= 0;
976 t
->t_scrollreg
.ts_begin
= 0;
977 t
->t_scrollreg
.ts_end
= t
->t_winsize
.tp_row
;
978 t
->t_originreg
= t
->t_scrollreg
;
979 t
->t_stateflags
&= TS_8BIT
|TS_CONS25
;
980 t
->t_stateflags
|= TS_AUTOWRAP
;
982 t
->t_scs
[0] = teken_scs_us_ascii
;
983 t
->t_scs
[1] = teken_scs_us_ascii
;
986 teken_subr_save_cursor(t
);
987 teken_tab_default(t
);
991 teken_subr_reset_to_initial_state(teken_t
*t
)
994 teken_subr_do_reset(t
);
995 teken_subr_erase_display(t
, 2);
996 teken_funcs_param(t
, TP_SHOWCURSOR
, 1);
997 teken_funcs_cursor(t
);
1001 teken_subr_restore_cursor(teken_t
*t
)
1004 t
->t_cursor
= t
->t_saved_cursor
;
1005 t
->t_curattr
= t
->t_saved_curattr
;
1006 t
->t_scs
[t
->t_curscs
] = t
->t_saved_curscs
;
1007 t
->t_stateflags
&= ~TS_WRAPPED
;
1009 /* Get out of origin mode when the cursor is moved outside. */
1010 if (t
->t_cursor
.tp_row
< t
->t_originreg
.ts_begin
||
1011 t
->t_cursor
.tp_row
>= t
->t_originreg
.ts_end
) {
1012 t
->t_stateflags
&= ~TS_ORIGIN
;
1013 t
->t_originreg
.ts_begin
= 0;
1014 t
->t_originreg
.ts_end
= t
->t_winsize
.tp_row
;
1017 teken_funcs_cursor(t
);
1021 teken_subr_reverse_index(teken_t
*t
)
1024 if (t
->t_cursor
.tp_row
> t
->t_scrollreg
.ts_begin
) {
1025 t
->t_cursor
.tp_row
--;
1026 t
->t_stateflags
&= ~TS_WRAPPED
;
1027 teken_funcs_cursor(t
);
1029 teken_subr_do_scroll(t
, -1);
1034 teken_subr_save_cursor(teken_t
*t
)
1037 t
->t_saved_cursor
= t
->t_cursor
;
1038 t
->t_saved_curattr
= t
->t_curattr
;
1039 t
->t_saved_curscs
= t
->t_scs
[t
->t_curscs
];
1043 teken_subr_secondary_device_attributes(teken_t
*t
, unsigned int request
)
1047 const char response
[] = "\x1B[>0;10;0c";
1048 teken_funcs_respond(t
, response
, sizeof response
- 1);
1050 teken_printf("Unknown DA2\n");
1055 teken_subr_set_dec_mode(teken_t
*t
, unsigned int cmd
)
1059 case 1: /* Cursor keys mode. */
1060 t
->t_stateflags
|= TS_CURSORKEYS
;
1062 case 2: /* DECANM: ANSI/VT52 mode. */
1063 teken_printf("DECSET VT52\n");
1065 case 3: /* 132 column mode. */
1066 teken_funcs_param(t
, TP_132COLS
, 1);
1067 teken_subr_reset_to_initial_state(t
);
1069 case 5: /* Inverse video. */
1070 teken_printf("DECSET inverse video\n");
1072 case 6: /* Origin mode. */
1073 t
->t_stateflags
|= TS_ORIGIN
;
1074 t
->t_originreg
= t
->t_scrollreg
;
1075 t
->t_cursor
.tp_row
= t
->t_scrollreg
.ts_begin
;
1076 t
->t_cursor
.tp_col
= 0;
1077 t
->t_stateflags
&= ~TS_WRAPPED
;
1078 teken_funcs_cursor(t
);
1080 case 7: /* Autowrap mode. */
1081 t
->t_stateflags
|= TS_AUTOWRAP
;
1083 case 8: /* Autorepeat mode. */
1084 teken_funcs_param(t
, TP_AUTOREPEAT
, 1);
1086 case 25: /* Display cursor. */
1087 teken_funcs_param(t
, TP_SHOWCURSOR
, 1);
1089 case 40: /* Allow 132 columns. */
1090 teken_printf("DECSET allow 132\n");
1092 case 45: /* Enable reverse wraparound. */
1093 teken_printf("DECSET reverse wraparound\n");
1095 case 47: /* Switch to alternate buffer. */
1096 teken_printf("Switch away from alternate buffer\n");
1098 case 1000: /* Mouse input. */
1099 teken_funcs_param(t
, TP_MOUSE
, 1);
1102 teken_printf("Unknown DECSET: %u\n", cmd
);
1107 teken_subr_set_mode(teken_t
*t
, unsigned int cmd
)
1112 teken_printf("Insert mode\n");
1113 t
->t_stateflags
|= TS_INSERT
;
1116 teken_printf("Unknown set mode: %u\n", cmd
);
1121 teken_subr_set_graphic_rendition(teken_t
*t
, unsigned int ncmds
,
1122 unsigned int cmds
[])
1126 /* No attributes means reset. */
1128 t
->t_curattr
= t
->t_defattr
;
1132 for (i
= 0; i
< ncmds
; i
++) {
1136 case 0: /* Reset. */
1137 t
->t_curattr
= t
->t_defattr
;
1140 t
->t_curattr
.ta_format
|= TF_BOLD
;
1142 case 4: /* Underline. */
1143 t
->t_curattr
.ta_format
|= TF_UNDERLINE
;
1145 case 5: /* Blink. */
1146 t
->t_curattr
.ta_format
|= TF_BLINK
;
1148 case 7: /* Reverse. */
1149 t
->t_curattr
.ta_format
|= TF_REVERSE
;
1151 case 22: /* Remove bold. */
1152 t
->t_curattr
.ta_format
&= ~TF_BOLD
;
1154 case 24: /* Remove underline. */
1155 t
->t_curattr
.ta_format
&= ~TF_UNDERLINE
;
1157 case 25: /* Remove blink. */
1158 t
->t_curattr
.ta_format
&= ~TF_BLINK
;
1160 case 27: /* Remove reverse. */
1161 t
->t_curattr
.ta_format
&= ~TF_REVERSE
;
1163 case 30: /* Set foreground color: black */
1164 case 31: /* Set foreground color: red */
1165 case 32: /* Set foreground color: green */
1166 case 33: /* Set foreground color: brown */
1167 case 34: /* Set foreground color: blue */
1168 case 35: /* Set foreground color: magenta */
1169 case 36: /* Set foreground color: cyan */
1170 case 37: /* Set foreground color: white */
1171 t
->t_curattr
.ta_fgcolor
= n
- 30;
1173 case 38: /* Set foreground color: 256 color mode */
1174 if (i
+ 2 >= ncmds
|| cmds
[i
+ 1] != 5)
1176 t
->t_curattr
.ta_fgcolor
= cmds
[i
+ 2];
1179 case 39: /* Set default foreground color. */
1180 t
->t_curattr
.ta_fgcolor
= t
->t_defattr
.ta_fgcolor
;
1182 case 40: /* Set background color: black */
1183 case 41: /* Set background color: red */
1184 case 42: /* Set background color: green */
1185 case 43: /* Set background color: brown */
1186 case 44: /* Set background color: blue */
1187 case 45: /* Set background color: magenta */
1188 case 46: /* Set background color: cyan */
1189 case 47: /* Set background color: white */
1190 t
->t_curattr
.ta_bgcolor
= n
- 40;
1192 case 48: /* Set background color: 256 color mode */
1193 if (i
+ 2 >= ncmds
|| cmds
[i
+ 1] != 5)
1195 t
->t_curattr
.ta_bgcolor
= cmds
[i
+ 2];
1198 case 49: /* Set default background color. */
1199 t
->t_curattr
.ta_bgcolor
= t
->t_defattr
.ta_bgcolor
;
1201 case 90: /* Set bright foreground color: black */
1202 case 91: /* Set bright foreground color: red */
1203 case 92: /* Set bright foreground color: green */
1204 case 93: /* Set bright foreground color: brown */
1205 case 94: /* Set bright foreground color: blue */
1206 case 95: /* Set bright foreground color: magenta */
1207 case 96: /* Set bright foreground color: cyan */
1208 case 97: /* Set bright foreground color: white */
1209 t
->t_curattr
.ta_fgcolor
= n
- 90 + 8;
1211 case 100: /* Set bright background color: black */
1212 case 101: /* Set bright background color: red */
1213 case 102: /* Set bright background color: green */
1214 case 103: /* Set bright background color: brown */
1215 case 104: /* Set bright background color: blue */
1216 case 105: /* Set bright background color: magenta */
1217 case 106: /* Set bright background color: cyan */
1218 case 107: /* Set bright background color: white */
1219 t
->t_curattr
.ta_bgcolor
= n
- 100 + 8;
1222 teken_printf("unsupported attribute %u\n", n
);
1228 teken_subr_set_top_and_bottom_margins(teken_t
*t
, unsigned int top
,
1229 unsigned int bottom
)
1232 /* Adjust top row number. */
1235 /* Adjust bottom row number. */
1236 if (bottom
== 0 || bottom
> t
->t_winsize
.tp_row
)
1237 bottom
= t
->t_winsize
.tp_row
;
1239 /* Invalid arguments. */
1240 if (top
>= bottom
- 1) {
1242 bottom
= t
->t_winsize
.tp_row
;
1245 /* Apply scrolling region. */
1246 t
->t_scrollreg
.ts_begin
= top
;
1247 t
->t_scrollreg
.ts_end
= bottom
;
1248 if (t
->t_stateflags
& TS_ORIGIN
)
1249 t
->t_originreg
= t
->t_scrollreg
;
1251 /* Home cursor to the top left of the scrolling region. */
1252 t
->t_cursor
.tp_row
= t
->t_originreg
.ts_begin
;
1253 t
->t_cursor
.tp_col
= 0;
1254 t
->t_stateflags
&= ~TS_WRAPPED
;
1255 teken_funcs_cursor(t
);
1259 teken_subr_single_height_double_width_line(teken_t
*t __unused
)
1262 teken_printf("single height double width???\n");
1266 teken_subr_single_height_single_width_line(teken_t
*t __unused
)
1269 teken_printf("single height single width???\n");
1273 teken_subr_string_terminator(teken_t
*t __unused
)
1277 * Strings are already terminated in teken_input_char() when ^[
1283 teken_subr_tab_clear(teken_t
*t
, unsigned int cmd
)
1288 teken_tab_clear(t
, t
->t_cursor
.tp_col
);
1291 memset(&t
->t_tabstops
, 0, T_NUMCOL
/ 8);
1297 teken_subr_vertical_position_absolute(teken_t
*t
, unsigned int row
)
1300 row
= row
- 1 + t
->t_originreg
.ts_begin
;
1301 t
->t_cursor
.tp_row
= row
< t
->t_originreg
.ts_end
?
1302 row
: t
->t_originreg
.ts_end
- 1;
1304 t
->t_stateflags
&= ~TS_WRAPPED
;
1305 teken_funcs_cursor(t
);