10 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
11 #define CL_VT100 0x0002 /* VT100 */
12 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
13 #define CL_VT102 0x0008 /* VT102 */
14 #define CL_VT220 0x0010 /* VT220 */
15 #define CL_VT320 0x0020 /* VT320 */
16 #define CL_VT420 0x0040 /* VT420 */
17 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
18 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
19 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
20 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
21 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
23 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
24 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
25 #define TM_VT102 (TM_VT100AVO|CL_VT102)
26 #define TM_VT220 (TM_VT102|CL_VT220)
27 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
28 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
30 #define TM_PUTTY (0xFFFF)
32 #define compatibility(x) \
33 if ( ((CL_##x)&compatibility_level) == 0 ) { \
37 #define compatibility2(x,y) \
38 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
43 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
45 static int compatibility_level
= TM_PUTTY
;
48 static unsigned long *text
; /* buffer of text on terminal screen */
49 static unsigned long *scrtop
; /* top of working screen */
50 static unsigned long *disptop
; /* top of displayed screen */
51 static unsigned long *sbtop
; /* top of scrollback */
52 static unsigned long *sbbot
; /* furthest extent of scrollback */
53 static unsigned long *cpos
; /* cursor position (convenience) */
54 static unsigned long *disptext
; /* buffer of text on real screen */
55 static unsigned long *wanttext
; /* buffer of text we want on screen */
56 static unsigned long *alttext
; /* buffer of text on alt. screen */
58 static unsigned char *selspace
; /* buffer for building selections in */
60 #define TSIZE (sizeof(*text))
61 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
63 static unsigned long curr_attr
, save_attr
;
64 static unsigned long erase_char
= ERASE_CHAR
;
66 static int curs_x
, curs_y
; /* cursor */
67 static int save_x
, save_y
; /* saved cursor position */
68 static int marg_t
, marg_b
; /* scroll margins */
69 static int dec_om
; /* DEC origin mode flag */
70 static int wrap
, wrapnext
; /* wrap flags */
71 static int insert
; /* insert-mode flag */
72 static int cset
; /* 0 or 1: which char set */
73 static int save_cset
, save_csattr
; /* saved with cursor position */
74 static int rvideo
; /* global reverse video flag */
75 static int cursor_on
; /* cursor enabled flag */
76 static int reset_132
; /* Flag ESC c resets to 80 cols */
77 static int use_bce
; /* Use Background coloured erase */
78 static int blinker
; /* When blinking is the cursor on ? */
79 static int tblinker
; /* When the blinking text is on */
80 static int blink_is_real
; /* Actually blink blinking text */
81 static int term_echoing
; /* Does terminal want local echo? */
82 static int term_editing
; /* Does terminal want local edit? */
84 static unsigned long cset_attr
[2];
87 * Saved settings on the alternate screen.
89 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
90 static int alt_t
, alt_b
;
93 #define ARGS_MAX 32 /* max # of esc sequence arguments */
94 #define ARG_DEFAULT 0 /* if an arg isn't specified */
95 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
96 static int esc_args
[ARGS_MAX
];
99 #define ANSI(x,y) ((x)+((y)<<8))
100 #define ANSI_QUE(x) ANSI(x,TRUE)
102 #define OSC_STR_MAX 2048
103 static int osc_strlen
;
104 static char osc_string
[OSC_STR_MAX
+1];
107 static char id_string
[1024] = "\033[?6c";
109 static unsigned char *tabs
;
123 OSC_STRING
, OSC_MAYBE_ST
,
131 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
134 SM_CHAR
, SM_WORD
, SM_LINE
136 static unsigned long *selstart
, *selend
, *selanchor
;
138 static short wordness
[256] = {
139 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */
140 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */
141 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */
142 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */
143 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */
144 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */
145 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */
146 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */
149 static unsigned char sel_nl
[] = SEL_NL
;
150 static char * paste_buffer
= 0;
151 static int paste_len
, paste_pos
, paste_hold
;
154 * Internal prototypes.
156 static void do_paint (Context
, int);
157 static void erase_lots (int, int, int);
158 static void swap_screen (int);
159 static void update_sbar (void);
160 static void deselect (void);
161 /* log session to file stuff ... */
162 static FILE *lgfp
= NULL
;
163 static void logtraffic(unsigned char c
, int logmode
);
166 * Set up power-on settings for the terminal.
168 static void power_on(void) {
169 curs_x
= curs_y
= alt_x
= alt_y
= save_x
= save_y
= 0;
172 alt_b
= marg_b
= rows
- 1;
177 for (i
= 0; i
< cols
; i
++)
178 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
180 alt_om
= dec_om
= cfg
.dec_om
;
181 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
182 alt_wrap
= wrap
= cfg
.wrap_mode
;
184 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
187 save_attr
= curr_attr
= ATTR_DEFAULT
;
188 term_editing
= term_echoing
= FALSE
;
189 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
190 app_cursor_keys
= cfg
.app_cursor
;
191 app_keypad_keys
= cfg
.app_keypad
;
193 blink_is_real
= cfg
.blinktext
;
194 erase_char
= ERASE_CHAR
;
198 for (i
= 0; i
< 256; i
++)
199 wordness
[i
] = cfg
.wordness
[i
];
203 erase_lots (FALSE
, TRUE
, TRUE
);
205 erase_lots (FALSE
, TRUE
, TRUE
);
210 * Force a screen update.
212 void term_update(void) {
216 if ( (seen_key_event
&& (cfg
.scroll_on_key
)) ||
217 (seen_disp_event
&& (cfg
.scroll_on_disp
)) ) {
219 seen_disp_event
= seen_key_event
= 0;
222 do_paint (ctx
, TRUE
);
223 sys_cursor(curs_x
, curs_y
+ (scrtop
- disptop
) / (cols
+1));
229 * Same as power_on(), but an external function.
231 void term_pwron(void) {
240 * Clear the scrollback.
242 void term_clrsb(void) {
243 disptop
= sbtop
= scrtop
;
248 * Initialise the terminal.
250 void term_init(void) {
251 text
= sbtop
= sbbot
= scrtop
= disptop
= cpos
= NULL
;
252 disptext
= wanttext
= NULL
;
261 * Set up the terminal for a given size.
263 void term_size(int newrows
, int newcols
, int newsavelines
) {
264 unsigned long *newtext
, *newdisp
, *newwant
, *newalt
;
265 int i
, j
, crows
, ccols
;
267 int save_alt_which
= alt_which
;
269 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
270 return; /* nothing to do */
276 alt_b
= marg_b
= newrows
- 1;
278 newtext
= smalloc ((newrows
+newsavelines
)*(newcols
+1)*TSIZE
);
279 sbbot
= newtext
+ newsavelines
*(newcols
+1);
280 for (i
=0; i
<(newrows
+newsavelines
)*(newcols
+1); i
++)
281 newtext
[i
] = erase_char
;
283 crows
= rows
+ (scrtop
- sbtop
) / (cols
+1);
284 if (crows
> newrows
+newsavelines
)
285 crows
= newrows
+newsavelines
;
289 disptop
= newtext
+ (crows
-newrows
)*(newcols
+1);
290 ccols
= (cols
< newcols ? cols
: newcols
);
291 for (i
=0; i
<crows
; i
++) {
292 int oldidx
= (rows
- crows
+ i
) * (cols
+1);
293 int newidx
= (newrows
- crows
+ i
) * (newcols
+1);
295 for (j
=0; j
<ccols
; j
++)
296 disptop
[newidx
+j
] = scrtop
[oldidx
+j
];
297 disptop
[newidx
+newcols
] =
298 (cols
== newcols ? scrtop
[oldidx
+cols
]
299 : (scrtop
[oldidx
+cols
]&LATTR_MODE
));
303 sbtop
= disptop
= newtext
;
309 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
310 for (i
=0; i
<newrows
*(newcols
+1); i
++)
311 newdisp
[i
] = ATTR_INVALID
;
315 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
316 for (i
=0; i
<newrows
*(newcols
+1); i
++)
317 newwant
[i
] = ATTR_INVALID
;
321 newalt
= smalloc (newrows
*(newcols
+1)*TSIZE
);
322 for (i
=0; i
<newrows
*(newcols
+1); i
++)
323 newalt
[i
] = erase_char
;
328 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
330 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
333 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
334 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
338 curs_y
+= newrows
- rows
;
341 if (curs_y
>= newrows
)
343 if (curs_x
>= newcols
)
346 wrapnext
= alt_wnext
= FALSE
;
350 savelines
= newsavelines
;
353 swap_screen(save_alt_which
);
362 static void swap_screen (int which
) {
366 if (which
== alt_which
)
371 for (t
=0; t
<rows
*(cols
+1); t
++) {
372 tt
= scrtop
[t
]; scrtop
[t
] = alttext
[t
]; alttext
[t
] = tt
;
375 t
= curs_x
; curs_x
= alt_x
; alt_x
= t
;
376 t
= curs_y
; curs_y
= alt_y
; alt_y
= t
;
377 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
378 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
379 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
380 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
381 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
382 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
383 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
389 * Update the scroll bar.
391 static void update_sbar(void) {
394 min
= (sbtop
- text
) / (cols
+1);
395 set_sbar ((scrtop
- text
) / (cols
+1) + rows
- min
,
396 (disptop
- text
) / (cols
+1) - min
,
401 * Check whether the region bounded by the two pointers intersects
402 * the scroll region, and de-select the on-screen selection if so.
404 static void check_selection (unsigned long *from
, unsigned long *to
) {
405 if (from
< selend
&& selstart
< to
)
410 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
411 * for backward.) `sb' is TRUE if the scrolling is permitted to
412 * affect the scrollback buffer.
414 static void scroll (int topline
, int botline
, int lines
, int sb
) {
415 unsigned long *scroll_top
;
416 unsigned long *newscr
;
417 int scroll_size
, size
, i
;
418 int scrtop_is_disptop
= (scrtop
==disptop
);
419 static int recursive
= 0;
421 /* Only scroll more than the window if we're doing a 10% scroll */
422 if (!recursive
&& lines
> botline
- topline
+ 1)
423 lines
= botline
- topline
+ 1;
425 scroll_top
= scrtop
+ topline
*(cols
+1);
426 size
= (lines
< 0 ?
-lines
: lines
) * (cols
+1);
427 scroll_size
= (botline
- topline
+ 1) * (cols
+1) - size
;
429 if (lines
> 0 && topline
== 0 && alt_which
== 0 && sb
) {
431 * Since we're going to scroll the top line and we're on the
432 * scrolling screen let's also effect the scrollback buffer.
434 * This is normally done by moving the position the screen
435 * painter reads from to reduce the amount of memory copying
438 if (scroll_size
>= 0 && !recursive
) {
439 newscr
= scrtop
+ lines
* (cols
+1);
441 if (newscr
> sbbot
&& botline
== rows
-1) {
442 /* We've hit the bottom of memory, so we have to do a
443 * physical scroll. But instead of just 1 line do it
444 * by 10% of the available memory.
446 * If the scroll region isn't the whole screen then we can't
447 * do this as it stands. We would need to recover the bottom
448 * of the screen from the scroll buffer after being sure that
449 * it doesn't get deleted.
452 i
= (rows
+savelines
)/10;
454 /* Make it simple and ensure safe recursion */
455 if ( i
<savelines
-1) {
457 scroll(topline
, botline
, i
, sb
);
460 newscr
= scrtop
- i
* (cols
+1);
461 if (scrtop_is_disptop
) disptop
= newscr
;
465 newscr
= scrtop
+ lines
* (cols
+1);
468 if (newscr
<= sbbot
) {
469 if (scrtop_is_disptop
) disptop
= newscr
;
472 if (botline
== rows
-1 )
473 for (i
= 0; i
< size
; i
++)
474 scrtop
[i
+scroll_size
] = erase_char
;
479 if (botline
!= rows
-1) {
480 /* This fastscroll only works for full window scrolls.
481 * If the caller wanted a partial one we have to reverse
482 * scroll the bottom of the screen.
484 scroll(botline
-lines
+1, rows
-1, -lines
, 0);
489 /* If we can't scroll by memory remapping do it physically.
490 * But rather than expensivly doing the scroll buffer just
491 * scroll the screen. All it means is that sometimes we choose
492 * to not add lines from a scroll region to the scroll buffer.
495 if (savelines
<= 400) {
496 sbtop
-= lines
* (cols
+1);
499 scroll_size
+= scroll_top
- sbtop
;
505 /* Ho hum, expensive scroll required. */
507 sbtop
-= lines
* (cols
+1);
510 scroll_size
+= scroll_top
- sbtop
;
517 if (scroll_size
< 0) {
524 memmove (scroll_top
, scroll_top
+ size
, scroll_size
*TSIZE
);
525 for (i
= 0; i
< size
; i
++)
526 scroll_top
[i
+scroll_size
] = erase_char
;
527 if (selstart
> scroll_top
&&
528 selstart
< scroll_top
+ size
+ scroll_size
) {
530 if (selstart
< scroll_top
)
531 selstart
= scroll_top
;
533 if (selend
> scroll_top
&&
534 selend
< scroll_top
+ size
+ scroll_size
) {
536 if (selend
< scroll_top
)
539 if (scrtop_is_disptop
)
542 if (disptop
> scroll_top
&&
543 disptop
< scroll_top
+ size
+ scroll_size
) {
545 if (disptop
< scroll_top
)
546 disptop
= scroll_top
;
550 memmove (scroll_top
+ size
, scroll_top
, scroll_size
*TSIZE
);
551 for (i
= 0; i
< size
; i
++)
552 scroll_top
[i
] = erase_char
;
553 if (selstart
> scroll_top
&&
554 selstart
< scroll_top
+ size
+ scroll_size
) {
556 if (selstart
> scroll_top
+ size
+ scroll_size
)
557 selstart
= scroll_top
+ size
+ scroll_size
;
559 if (selend
> scroll_top
&&
560 selend
< scroll_top
+ size
+ scroll_size
) {
562 if (selend
> scroll_top
+ size
+ scroll_size
)
563 selend
= scroll_top
+ size
+ scroll_size
;
565 if (scrtop_is_disptop
)
567 else if (disptop
> scroll_top
&&
568 disptop
< scroll_top
+ size
+ scroll_size
) {
570 if (disptop
> scroll_top
+ size
+ scroll_size
)
571 disptop
= scroll_top
+ size
+ scroll_size
;
577 * Move the cursor to a given position, clipping at boundaries. We
578 * may or may not want to clip at the scroll margin: marg_clip is 0
579 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
580 * even _being_ outside the margins.
582 static void move (int x
, int y
, int marg_clip
) {
588 if ((curs_y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
590 if ((curs_y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
604 * Save or restore the cursor and SGR mode.
606 static void save_cursor(int save
) {
610 save_attr
= curr_attr
;
612 save_csattr
= cset_attr
[cset
];
616 /* Make sure the window hasn't shrunk since the save */
617 if (curs_x
>= cols
) curs_x
= cols
-1;
618 if (curs_y
>= rows
) curs_y
= rows
-1;
620 curr_attr
= save_attr
;
622 cset_attr
[cset
] = save_csattr
;
624 if (use_bce
) erase_char
= (' ' |(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
629 * Erase a large portion of the screen: the whole screen, or the
630 * whole line, or parts thereof.
632 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
633 unsigned long *startpos
, *endpos
;
636 startpos
= cpos
- curs_x
;
637 endpos
= startpos
+ cols
;
638 /* I've removed the +1 so that the Wide screen stuff is not
639 * removed when it shouldn't be.
643 endpos
= startpos
+ rows
* (cols
+1);
649 check_selection (startpos
, endpos
);
651 /* Clear screen also forces a full window redraw, just in case. */
652 if (startpos
== scrtop
&& endpos
== scrtop
+ rows
* (cols
+1))
655 while (startpos
< endpos
)
656 *startpos
++ = erase_char
;
660 * Insert or delete characters within the current line. n is +ve if
661 * insertion is desired, and -ve for deletion.
663 static void insch (int n
) {
664 int dir
= (n
< 0 ?
-1 : +1);
667 n
= (n
< 0 ?
-n
: n
);
668 if (n
> cols
- curs_x
)
670 m
= cols
- curs_x
- n
;
671 check_selection (cpos
, cpos
+n
);
673 memmove (cpos
, cpos
+n
, m
*TSIZE
);
675 cpos
[m
++] = erase_char
;
677 memmove (cpos
+n
, cpos
, m
*TSIZE
);
679 cpos
[n
] = erase_char
;
684 * Toggle terminal mode `mode' to state `state'. (`query' indicates
685 * whether the mode is a DEC private one or a normal one.)
687 static void toggle_mode (int mode
, int query
, int state
) {
688 if (query
) switch (mode
) {
689 case 1: /* application cursor keys */
690 app_cursor_keys
= state
;
692 case 2: /* VT52 mode */
695 case 3: /* 80/132 columns */
697 request_resize (state ?
132 : 80, rows
, 1);
700 case 5: /* reverse video */
702 seen_disp_event
= TRUE
;
703 if (state
) term_update();
705 case 6: /* DEC origin mode */
708 case 7: /* auto wrap */
711 case 8: /* auto key repeat */
714 case 10: /* set local edit mode */
715 term_editing
= state
;
716 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
718 case 25: /* enable/disable cursor */
719 compatibility2(OTHER
,VT220
);
721 seen_disp_event
= TRUE
;
723 case 47: /* alternate screen */
724 compatibility(OTHER
);
729 } else switch (mode
) {
730 case 4: /* set insert mode */
731 compatibility(VT102
);
734 case 12: /* set echo mode */
735 term_echoing
= !state
;
736 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
738 case 20: /* Return sends ... */
739 cr_lf_return
= state
;
745 * Process an OSC sequence: set window title or icon name.
747 static void do_osc(void) {
750 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
752 osc_string
[osc_strlen
] = '\0';
753 switch (esc_args
[0]) {
756 set_icon (osc_string
);
757 if (esc_args
[0] == 1)
759 /* fall through: parameter 0 means set both */
762 set_title (osc_string
);
769 * Remove everything currently in `inbuf' and stick it up on the
770 * in-memory display. There's a big state machine in here to
771 * process escape sequences...
773 void term_out(void) {
776 static int beep_overload
= 0;
779 for(inbuf_reap
= 0; inbuf_reap
< inbuf_head
; inbuf_reap
++)
781 c
= inbuf
[inbuf_reap
];
784 * Optionally log the session traffic to a file. Useful for
785 * debugging and possibly also useful for actual logging.
787 logtraffic((unsigned char)c
, LGTYP_DEBUG
);
789 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
790 * be able to display 8-bit characters, but I'll let that go 'cause
793 if( ( (c
&0x60) == 0 || c
== '\177') &&
794 termstate
< DO_CTRLS
&&
795 ( (c
&0x80) == 0 || has_compat(VT220
))) {
797 case '\005': /* terminal type query */
798 /* Strictly speaking this is VT100 but a VT100 defaults to
799 * no response. Other terminals respond at their option.
801 * Don't put a CR in the default string as this tends to
802 * upset some weird software.
804 * An xterm returns "xterm" (5 characters)
806 compatibility(ANSIMIN
);
808 char abuf
[256], *s
, *d
;
810 for(s
=cfg
.answerback
, d
=abuf
; *s
; s
++) {
813 if (*s
>= 'a' && *s
<= 'z')
814 *d
++ = (*s
- ('a'-1));
815 else if ((*s
>='@' && *s
<='_') ||
816 *s
== '?' || (*s
&0x80))
822 else if (*s
== '^') {
826 *d
++ = xlat_kbd2tty((unsigned char)*s
);
828 ldisc_send (abuf
, d
-abuf
);
833 if(beep_count
>6) beep_overload
=1;
837 if (curs_x
== 0 && curs_y
== 0)
839 else if (curs_x
== 0 && curs_y
> 0)
840 curs_x
= cols
-1, curs_y
--;
846 seen_disp_event
= TRUE
;
849 compatibility(VT100
);
853 compatibility(VT100
);
858 termstate
= VT52_ESC
;
860 compatibility(ANSIMIN
);
861 termstate
= SEEN_ESC
;
865 compatibility(VT220
);
866 termstate
= SEEN_CSI
;
868 esc_args
[0] = ARG_DEFAULT
;
872 compatibility(VT220
);
873 termstate
= SEEN_OSC
;
880 seen_disp_event
= TRUE
;
882 logtraffic((unsigned char)c
,LGTYP_ASCII
);
886 compatibility(VT100
);
888 if (curs_y
== marg_b
)
889 scroll (marg_t
, marg_b
, 1, TRUE
);
890 else if (curs_y
< rows
-1)
898 logtraffic((unsigned char)c
,LGTYP_ASCII
);
902 unsigned long *old_cpos
= cpos
;
903 unsigned long *p
= scrtop
+ curs_y
* (cols
+1) + cols
;
907 } while (curs_x
< cols
-1 && !tabs
[curs_x
]);
909 if ((*p
& LATTR_MODE
) != LATTR_NORM
)
911 if (curs_x
>= cols
/2)
921 check_selection (old_cpos
, cpos
);
923 seen_disp_event
= TRUE
;
925 case '\177': /* Destructive backspace
926 This does nothing on a real VT100 */
927 compatibility(OTHER
);
928 if (curs_x
&& !wrapnext
) curs_x
--;
931 *cpos
= (' ' | curr_attr
| ATTR_ASCII
);
935 else switch (termstate
) {
937 /* Only graphic characters get this far, ctrls are stripped above */
938 if (wrapnext
&& wrap
) {
939 cpos
[1] |= ATTR_WRAPPED
;
940 if (curs_y
== marg_b
)
941 scroll (marg_t
, marg_b
, 1, TRUE
);
942 else if (curs_y
< rows
-1)
950 if (selstate
!= NO_SELECTION
)
951 check_selection (cpos
, cpos
+1);
952 switch (cset_attr
[cset
]) {
953 /* Linedraw characters are different from 'ESC ( B' only
954 * for a small range, for ones outside that range make sure
955 * we use the same font as well as the same encoding.
958 if (c
<0x5f || c
>0x7F)
959 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
962 *cpos
++ = ' ' | curr_attr
| ATTR_ASCII
;
964 *cpos
++ = ((unsigned char)c
) | curr_attr
| ATTR_LINEDRW
;
967 /* If UK-ASCII, make the '#' a LineDraw Pound */
969 *cpos
++ = '}' | curr_attr
| ATTR_LINEDRW
;
974 *cpos
= xlat_tty2scr((unsigned char)c
) | curr_attr
|
975 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
976 logtraffic((unsigned char)c
, LGTYP_ASCII
);
981 if (curs_x
== cols
) {
990 termstate
= TOPLEVEL
;
994 * This state is virtually identical to SEEN_ESC, with the
995 * exception that we have an OSC sequence in the pipeline,
996 * and _if_ we see a backslash, we process it.
1000 termstate
= TOPLEVEL
;
1003 /* else fall through */
1005 termstate
= TOPLEVEL
;
1007 case ' ': /* some weird sequence? */
1008 compatibility(VT220
);
1009 termstate
= IGNORE_NEXT
;
1011 case '[': /* enter CSI mode */
1012 termstate
= SEEN_CSI
;
1014 esc_args
[0] = ARG_DEFAULT
;
1017 case ']': /* xterm escape sequences */
1018 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1019 compatibility(OTHER
);
1020 termstate
= SEEN_OSC
;
1023 case '(': /* should set GL */
1024 compatibility(VT100
);
1027 case ')': /* should set GR */
1028 compatibility(VT100
);
1031 case '7': /* save cursor */
1032 compatibility(VT100
);
1035 case '8': /* restore cursor */
1036 compatibility(VT100
);
1037 save_cursor (FALSE
);
1038 seen_disp_event
= TRUE
;
1041 compatibility(VT100
);
1042 app_keypad_keys
= TRUE
;
1045 compatibility(VT100
);
1046 app_keypad_keys
= FALSE
;
1048 case 'D': /* exactly equivalent to LF */
1049 compatibility(VT100
);
1050 if (curs_y
== marg_b
)
1051 scroll (marg_t
, marg_b
, 1, TRUE
);
1052 else if (curs_y
< rows
-1)
1056 seen_disp_event
= TRUE
;
1058 case 'E': /* exactly equivalent to CR-LF */
1059 compatibility(VT100
);
1061 if (curs_y
== marg_b
)
1062 scroll (marg_t
, marg_b
, 1, TRUE
);
1063 else if (curs_y
< rows
-1)
1067 seen_disp_event
= TRUE
;
1069 case 'M': /* reverse index - backwards LF */
1070 compatibility(VT100
);
1071 if (curs_y
== marg_t
)
1072 scroll (marg_t
, marg_b
, -1, TRUE
);
1073 else if (curs_y
> 0)
1077 seen_disp_event
= TRUE
;
1079 case 'Z': /* terminal type query */
1080 compatibility(VT100
);
1081 ldisc_send (id_string
, strlen(id_string
));
1083 case 'c': /* restore power-on settings */
1084 compatibility(VT100
);
1087 request_resize (80, rows
, 1);
1092 seen_disp_event
= TRUE
;
1094 case '#': /* ESC # 8 fills screen with Es :-) */
1095 compatibility(VT100
);
1096 termstate
= SEEN_ESCHASH
;
1098 case 'H': /* set a tab */
1099 compatibility(VT100
);
1100 tabs
[curs_x
] = TRUE
;
1105 termstate
= TOPLEVEL
; /* default */
1108 if (esc_nargs
<= ARGS_MAX
) {
1109 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
1110 esc_args
[esc_nargs
-1] = 0;
1111 esc_args
[esc_nargs
-1] =
1112 10 * esc_args
[esc_nargs
-1] + c
- '0';
1114 termstate
= SEEN_CSI
;
1118 if (++esc_nargs
<= ARGS_MAX
)
1119 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
1120 termstate
= SEEN_CSI
;
1124 if( esc_query
) esc_query
= -1;
1125 else if( c
== '?' ) esc_query
= TRUE
;
1127 termstate
= SEEN_CSI
;
1129 else switch (ANSI(c
,esc_query
)) {
1130 case 'A': /* move up N lines */
1131 move (curs_x
, curs_y
- def(esc_args
[0], 1), 1);
1132 seen_disp_event
= TRUE
;
1134 case 'e': /* move down N lines */
1135 compatibility(ANSI
);
1137 move (curs_x
, curs_y
+ def(esc_args
[0], 1), 1);
1138 seen_disp_event
= TRUE
;
1140 case 'a': /* move right N cols */
1141 compatibility(ANSI
);
1143 move (curs_x
+ def(esc_args
[0], 1), curs_y
, 1);
1144 seen_disp_event
= TRUE
;
1146 case 'D': /* move left N cols */
1147 move (curs_x
- def(esc_args
[0], 1), curs_y
, 1);
1148 seen_disp_event
= TRUE
;
1150 case 'E': /* move down N lines and CR */
1151 compatibility(ANSI
);
1152 move (0, curs_y
+ def(esc_args
[0], 1), 1);
1153 seen_disp_event
= TRUE
;
1155 case 'F': /* move up N lines and CR */
1156 compatibility(ANSI
);
1157 move (0, curs_y
- def(esc_args
[0], 1), 1);
1158 seen_disp_event
= TRUE
;
1160 case 'G': case '`': /* set horizontal posn */
1161 compatibility(ANSI
);
1162 move (def(esc_args
[0], 1) - 1, curs_y
, 0);
1163 seen_disp_event
= TRUE
;
1165 case 'd': /* set vertical posn */
1166 compatibility(ANSI
);
1167 move (curs_x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1169 seen_disp_event
= TRUE
;
1171 case 'H': case 'f': /* set horz and vert posns at once */
1173 esc_args
[1] = ARG_DEFAULT
;
1174 move (def(esc_args
[1], 1) - 1,
1175 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1177 seen_disp_event
= TRUE
;
1179 case 'J': /* erase screen or parts of it */
1181 unsigned int i
= def(esc_args
[0], 0) + 1;
1184 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
1187 seen_disp_event
= TRUE
;
1189 case 'K': /* erase line or parts of it */
1191 unsigned int i
= def(esc_args
[0], 0) + 1;
1194 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
1196 seen_disp_event
= TRUE
;
1198 case 'L': /* insert lines */
1199 compatibility(VT102
);
1200 if (curs_y
<= marg_b
)
1201 scroll (curs_y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
1202 seen_disp_event
= TRUE
;
1204 case 'M': /* delete lines */
1205 compatibility(VT102
);
1206 if (curs_y
<= marg_b
)
1207 scroll (curs_y
, marg_b
, def(esc_args
[0], 1), TRUE
);
1208 seen_disp_event
= TRUE
;
1210 case '@': /* insert chars */
1211 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1212 compatibility(VT102
);
1213 insch (def(esc_args
[0], 1));
1214 seen_disp_event
= TRUE
;
1216 case 'P': /* delete chars */
1217 compatibility(VT102
);
1218 insch (-def(esc_args
[0], 1));
1219 seen_disp_event
= TRUE
;
1221 case 'c': /* terminal type query */
1222 compatibility(VT100
);
1223 /* This is the response for a VT102 */
1224 ldisc_send (id_string
, strlen(id_string
));
1226 case 'n': /* cursor position query */
1227 if (esc_args
[0] == 6) {
1229 sprintf (buf
, "\033[%d;%dR", curs_y
+ 1, curs_x
+ 1);
1230 ldisc_send (buf
, strlen(buf
));
1232 else if (esc_args
[0] == 5) {
1233 ldisc_send ("\033[0n", 4);
1236 case 'h': /* toggle modes to high */
1238 compatibility(VT100
);
1241 for (i
=0; i
<esc_nargs
; i
++)
1242 toggle_mode (esc_args
[i
], esc_query
, TRUE
);
1245 case 'l': /* toggle modes to low */
1247 compatibility(VT100
);
1250 for (i
=0; i
<esc_nargs
; i
++)
1251 toggle_mode (esc_args
[i
], esc_query
, FALSE
);
1254 case 'g': /* clear tabs */
1255 compatibility(VT100
);
1256 if (esc_nargs
== 1) {
1257 if (esc_args
[0] == 0) {
1258 tabs
[curs_x
] = FALSE
;
1259 } else if (esc_args
[0] == 3) {
1261 for (i
= 0; i
< cols
; i
++)
1266 case 'r': /* set scroll margins */
1267 compatibility(VT100
);
1268 if (esc_nargs
<= 2) {
1270 top
= def(esc_args
[0], 1) - 1;
1271 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
1272 def(esc_args
[1], rows
)) - 1;
1275 /* VTTEST Bug 9 - if region is less than 2 lines
1276 * don't change region.
1283 * I used to think the cursor should be
1284 * placed at the top of the newly marginned
1285 * area. Apparently not: VMS TPU falls over
1288 * Well actually it should for Origin mode - RDB
1290 curs_y
= (dec_om ? marg_t
: 0);
1292 seen_disp_event
= TRUE
;
1296 case 'm': /* set graphics rendition */
1299 * A VT100 without the AVO only had one attribute, either
1300 * underline or reverse video depending on the cursor type,
1301 * this was selected by CSI 7m.
1304 * This is DIM on the VT100-AVO and VT102
1306 * This is BLINK on the VT100-AVO and VT102+
1308 * This is INVIS on the VT100-AVO and VT102
1310 * This like 22 disables BOLD, DIM and INVIS
1312 * The ANSI colours appear on any terminal that has colour
1313 * (obviously) but the interaction between sgr0 and the
1314 * colours varies but is usually related to the background
1315 * colour erase item.
1316 * The interaction between colour attributes and the mono
1317 * ones is also very implementation dependent.
1319 * The 39 and 49 attributes are likely to be unimplemented.
1322 for (i
=0; i
<esc_nargs
; i
++) {
1323 switch (def(esc_args
[i
], 0)) {
1324 case 0: /* restore defaults */
1325 curr_attr
= ATTR_DEFAULT
; break;
1326 case 1: /* enable bold */
1327 compatibility(VT100AVO
);
1328 curr_attr
|= ATTR_BOLD
; break;
1329 case 21: /* (enable double underline) */
1330 compatibility(OTHER
);
1331 case 4: /* enable underline */
1332 compatibility(VT100AVO
);
1333 curr_attr
|= ATTR_UNDER
; break;
1334 case 5: /* enable blink */
1335 compatibility(VT100AVO
);
1336 curr_attr
|= ATTR_BLINK
; break;
1337 case 7: /* enable reverse video */
1338 curr_attr
|= ATTR_REVERSE
; break;
1339 case 22: /* disable bold */
1340 compatibility2(OTHER
,VT220
);
1341 curr_attr
&= ~ATTR_BOLD
; break;
1342 case 24: /* disable underline */
1343 compatibility2(OTHER
,VT220
);
1344 curr_attr
&= ~ATTR_UNDER
; break;
1345 case 25: /* disable blink */
1346 compatibility2(OTHER
,VT220
);
1347 curr_attr
&= ~ATTR_BLINK
; break;
1348 case 27: /* disable reverse video */
1349 compatibility2(OTHER
,VT220
);
1350 curr_attr
&= ~ATTR_REVERSE
; break;
1351 case 30: case 31: case 32: case 33:
1352 case 34: case 35: case 36: case 37:
1354 curr_attr
&= ~ATTR_FGMASK
;
1355 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
1357 case 39: /* default-foreground */
1358 curr_attr
&= ~ATTR_FGMASK
;
1359 curr_attr
|= ATTR_DEFFG
;
1361 case 40: case 41: case 42: case 43:
1362 case 44: case 45: case 46: case 47:
1364 curr_attr
&= ~ATTR_BGMASK
;
1365 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
1367 case 49: /* default-background */
1368 curr_attr
&= ~ATTR_BGMASK
;
1369 curr_attr
|= ATTR_DEFBG
;
1376 (curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
|ATTR_BLINK
))
1380 case 's': /* save cursor */
1383 case 'u': /* restore cursor */
1384 save_cursor (FALSE
);
1385 seen_disp_event
= TRUE
;
1387 case 't': /* set page size - ie window height */
1389 * VT340/VT420 sequence DECSLPP, DEC only allows values
1390 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1391 * illegal values (eg first arg 1..9) for window changing
1394 compatibility(VT340TEXT
);
1395 if (esc_nargs
<=1 && (esc_args
[0]<1 || esc_args
[0]>=24)) {
1396 request_resize (cols
, def(esc_args
[0], 24), 0);
1400 case ANSI('|', '*'):
1401 /* VT420 sequence DECSNLS
1402 * Set number of lines on screen
1403 * VT420 uses VGA like hardware and can support any size in
1404 * reasonable range (24..49 AIUI) with no default specified.
1406 compatibility(VT420
);
1407 if (esc_nargs
==1 && esc_args
[0]>0) {
1408 request_resize (cols
, def(esc_args
[0], cfg
.height
), 0);
1412 case ANSI('|', '$'):
1413 /* VT340/VT420 sequence DECSCPP
1414 * Set number of columns per page
1415 * Docs imply range is only 80 or 132, but I'll allow any.
1417 compatibility(VT340TEXT
);
1419 request_resize (def(esc_args
[0], cfg
.width
), rows
, 0);
1423 case 'X': /* write N spaces w/o moving cursor */
1424 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1425 compatibility(ANSIMIN
);
1427 int n
= def(esc_args
[0], 1);
1428 unsigned long *p
= cpos
;
1429 if (n
> cols
- curs_x
)
1431 check_selection (cpos
, cpos
+n
);
1434 seen_disp_event
= TRUE
;
1437 case 'x': /* report terminal characteristics */
1438 compatibility(VT100
);
1441 int i
= def(esc_args
[0], 0);
1442 if (i
== 0 || i
== 1) {
1443 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1445 ldisc_send (buf
, 20);
1450 compatibility(OTHER
);
1451 use_bce
= (esc_args
[0]<=0);
1452 erase_char
= ERASE_CHAR
;
1454 erase_char
= (' '|(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
1457 compatibility(OTHER
);
1458 blink_is_real
= (esc_args
[0]>=1);
1461 /* Allow the host to make this emulator a 'perfect' VT102.
1462 * This first appeared in the VT220, but we do need to get
1463 * back to PuTTY mode so I won't check it.
1465 * The arg in 40..42 are a PuTTY extension.
1466 * The 2nd arg, 8bit vs 7bit is not checked.
1468 * Setting VT102 mode should also change the Fkeys to
1469 * generate PF* codes as a real VT102 has no Fkeys.
1470 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1473 * Note ESC c will NOT change this!
1476 switch (esc_args
[0]) {
1477 case 61: compatibility_level
&= ~TM_VTXXX
;
1478 compatibility_level
|= TM_VT102
; break;
1479 case 62: compatibility_level
&= ~TM_VTXXX
;
1480 compatibility_level
|= TM_VT220
; break;
1482 default: if( esc_args
[0] > 60 && esc_args
[0] < 70 )
1483 compatibility_level
|= TM_VTXXX
;
1486 case 40: compatibility_level
&= TM_VTXXX
; break;
1487 case 41: compatibility_level
= TM_PUTTY
; break;
1488 case 42: compatibility_level
= TM_SCOANSI
; break;
1491 compatibility_level
= TM_PUTTY
; break;
1495 /* Change the response to CSI c */
1496 if (esc_args
[0] == 50) {
1499 strcpy(id_string
, "\033[?");
1500 for (i
=1; i
<esc_nargs
; i
++) {
1501 if (i
!=1) strcat(id_string
, ";");
1502 sprintf(lbuf
, "%d", esc_args
[i
]);
1503 strcat(id_string
, lbuf
);
1505 strcat(id_string
, "c");
1509 /* Is this a good idea ?
1510 * Well we should do a soft reset at this point ...
1512 if (!has_compat(VT420
) && has_compat(VT100
)) {
1513 if (reset_132
) request_resize (132, 24, 1);
1514 else request_resize ( 80, 24, 1);
1522 /* VT100 only here, checked above */
1525 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1528 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1531 default: /* specifically, 'B' */
1532 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1535 if( !has_compat(VT220
) || c
!= '%' )
1536 termstate
= TOPLEVEL
;
1541 case 'P': /* Linux palette sequence */
1542 termstate
= SEEN_OSC_P
;
1545 case 'R': /* Linux palette reset */
1548 termstate
= TOPLEVEL
;
1550 case 'W': /* word-set */
1551 termstate
= SEEN_OSC_W
;
1554 case '0': case '1': case '2': case '3': case '4':
1555 case '5': case '6': case '7': case '8': case '9':
1556 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1560 * Grotty hack to support xterm and DECterm title
1561 * sequences concurrently.
1563 if (esc_args
[0] == 2) {
1567 /* else fall through */
1569 termstate
= OSC_STRING
;
1575 * This OSC stuff is EVIL. It takes just one character to get into
1576 * sysline mode and it's not initially obvious how to get out.
1577 * So I've added CR and LF as string aborts.
1578 * This shouldn't effect compatibility as I believe embedded
1579 * control characters are supposed to be interpreted (maybe?)
1580 * and they don't display anything useful anyway.
1584 if (c
== '\n' || c
== '\r') {
1585 termstate
= TOPLEVEL
;
1586 } else if (c
== 0234 || c
== '\007' ) {
1588 * These characters terminate the string; ST and BEL
1589 * terminate the sequence and trigger instant
1590 * processing of it, whereas ESC goes back to SEEN_ESC
1591 * mode unless it is followed by \, in which case it is
1592 * synonymous with ST in the first place.
1595 termstate
= TOPLEVEL
;
1596 } else if (c
== '\033')
1597 termstate
= OSC_MAYBE_ST
;
1598 else if (osc_strlen
< OSC_STR_MAX
)
1599 osc_string
[osc_strlen
++] = c
;
1603 int max
= (osc_strlen
== 0 ?
21 : 16);
1605 if (c
>= '0' && c
<= '9')
1607 else if (c
>= 'A' && c
<= 'A'+max
-10)
1609 else if (c
>= 'a' && c
<= 'a'+max
-10)
1612 termstate
= TOPLEVEL
;
1613 osc_string
[osc_strlen
++] = val
;
1614 if (osc_strlen
>= 7) {
1615 palette_set (osc_string
[0],
1616 osc_string
[1] * 16 + osc_string
[2],
1617 osc_string
[3] * 16 + osc_string
[4],
1618 osc_string
[5] * 16 + osc_string
[6]);
1620 termstate
= TOPLEVEL
;
1626 case '0': case '1': case '2': case '3': case '4':
1627 case '5': case '6': case '7': case '8': case '9':
1628 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1631 termstate
= OSC_STRING
;
1638 unsigned long nlattr
;
1644 n
= rows
* (cols
+1);
1646 *p
++ = ATTR_DEFAULT
| 'E';
1648 seen_disp_event
= TRUE
;
1649 check_selection (scrtop
, scrtop
+ rows
* (cols
+1));
1652 case '3': nlattr
= LATTR_TOP
; if(0) {
1653 case '4': nlattr
= LATTR_BOT
; } if(0) {
1654 case '5': nlattr
= LATTR_NORM
; } if(0) {
1655 case '6': nlattr
= LATTR_WIDE
; }
1657 p
= scrtop
+ curs_y
* (cols
+1) + cols
;
1662 termstate
= TOPLEVEL
;
1665 termstate
= TOPLEVEL
;
1666 seen_disp_event
= TRUE
;
1669 move (curs_x
, curs_y
- 1, 1);
1672 move (curs_x
, curs_y
+ 1, 1);
1675 move (curs_x
+ 1, curs_y
, 1);
1678 move (curs_x
- 1, curs_y
, 1);
1681 cset_attr
[cset
=0] = ATTR_LINEDRW
;
1684 cset_attr
[cset
=0] = ATTR_ASCII
;
1691 scroll (0, rows
-1, -1, TRUE
);
1692 else if (curs_y
> 0)
1698 erase_lots(FALSE
, FALSE
, TRUE
);
1702 erase_lots(TRUE
, FALSE
, TRUE
);
1705 /* XXX Print cursor line */
1708 /* XXX Start controller mode */
1711 /* XXX Stop controller mode */
1714 termstate
= VT52_Y1
;
1717 ldisc_send ("\033/Z", 3);
1720 app_keypad_keys
= TRUE
;
1723 app_keypad_keys
= FALSE
;
1726 /* XXX This should switch to VT100 mode not current or default
1727 * VT mode. But this will only have effect in a VT220+
1733 /* XXX Enter auto print mode */
1736 /* XXX Exit auto print mode */
1739 /* XXX Print screen */
1744 termstate
= VT52_Y2
;
1745 move(curs_x
, c
-' ', 0);
1748 termstate
= TOPLEVEL
;
1749 move(c
-' ', curs_y
, 0);
1752 if (selstate
!= NO_SELECTION
)
1753 check_selection (cpos
, cpos
+1);
1759 if(!beep_count
) beep_overload
=0;
1761 else if(beep_count
&& beep_count
<5 && cfg
.beep
)
1766 * Compare two lines to determine whether they are sufficiently
1767 * alike to scroll-optimise one to the other. Return the degree of
1770 static int linecmp (unsigned long *a
, unsigned long *b
) {
1773 for (i
=n
=0; i
< cols
; i
++)
1774 n
+= (*a
++ == *b
++);
1779 * Given a context, update the window. Out of paranoia, we don't
1780 * allow WM_PAINT responses to do scrolling optimisations.
1782 static void do_paint (Context ctx
, int may_optimise
){
1783 int i
, j
, start
, our_curs_y
;
1784 unsigned long attr
, rv
, cursor
;
1789 if (blinker
|| !cfg
.blink_cur
)
1790 cursor
= ATTR_ACTCURS
;
1795 cursor
= ATTR_PASCURS
;
1797 cursor
|= ATTR_RIGHTCURS
;
1800 rv
= (rvideo ? ATTR_REVERSE
: 0);
1801 our_curs_y
= curs_y
+ (scrtop
- disptop
) / (cols
+1);
1803 for (i
=0; i
<rows
; i
++) {
1804 int idx
= i
*(cols
+1);
1805 int lattr
= (disptop
[idx
+cols
] & LATTR_MODE
);
1806 for (j
=0; j
<=cols
; j
++,idx
++) {
1807 unsigned long *d
= disptop
+idx
;
1808 wanttext
[idx
] = lattr
| (((*d
&~ ATTR_WRAPPED
) ^ rv
1809 ^ (selstart
<= d
&& d
< selend ?
1810 ATTR_REVERSE
: 0)) |
1811 (i
==our_curs_y
&& j
==curs_x ? cursor
: 0));
1812 if (blink_is_real
) {
1813 if (has_focus
&& tblinker
&& (wanttext
[idx
]&ATTR_BLINK
) )
1815 wanttext
[idx
] &= ATTR_MASK
;
1816 wanttext
[idx
] += ' ';
1818 wanttext
[idx
] &= ~ATTR_BLINK
;
1824 * We would perform scrolling optimisations in here, if they
1825 * didn't have a nasty tendency to cause the whole sodding
1826 * program to hang for a second at speed-critical moments.
1827 * We'll leave it well alone...
1830 for (i
=0; i
<rows
; i
++) {
1831 int idx
= i
*(cols
+1);
1832 int lattr
= (wanttext
[idx
+cols
] & LATTR_MODE
);
1834 for (j
=0; j
<=cols
; j
++,idx
++) {
1835 unsigned long t
= wanttext
[idx
];
1836 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1837 int keep_going
= (start
!= -1 && needs_update
&&
1838 (t
& ATTR_MASK
) == attr
&&
1839 j
-start
< sizeof(ch
));
1840 if (start
!= -1 && !keep_going
) {
1841 do_text (ctx
, start
, i
, ch
, j
-start
, attr
, lattr
);
1847 attr
= t
& ATTR_MASK
;
1849 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1857 * Flick the switch that says if blinking things should be shown or hidden.
1860 void term_blink(int flg
) {
1861 static long last_blink
= 0;
1862 static long last_tblink
= 0;
1863 long now
, blink_diff
;
1865 now
= GetTickCount();
1866 blink_diff
= now
-last_tblink
;
1868 /* Make sure the text blinks no more than 2Hz */
1869 if (blink_diff
<0 || blink_diff
>450)
1872 tblinker
= !tblinker
;
1881 blink_diff
= now
-last_blink
;
1883 /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */
1884 if (blink_diff
>=0 && blink_diff
<(long)GetCaretBlinkTime())
1892 * Invalidate the whole screen so it will be repainted in full.
1894 void term_invalidate(void) {
1897 for (i
=0; i
<rows
*(cols
+1); i
++)
1898 disptext
[i
] = ATTR_INVALID
;
1902 * Paint the window in response to a WM_PAINT message.
1904 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1905 int i
, j
, left
, top
, right
, bottom
;
1907 left
= l
/ font_width
;
1908 right
= (r
- 1) / font_width
;
1909 top
= t
/ font_height
;
1910 bottom
= (b
- 1) / font_height
;
1911 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1913 if ( (disptext
[i
*(cols
+1)+cols
]&LATTR_MODE
) == LATTR_NORM
)
1914 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1915 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1917 for (j
= left
/2; j
<= right
/2+1 && j
< cols
; j
++)
1918 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1921 /* This should happen soon enough, also for some reason it sometimes
1922 * fails to actually do anything when re-sizing ... painting the wrong
1924 do_paint (ctx, FALSE);
1929 * Attempt to scroll the scrollback. The second parameter gives the
1930 * position we want to scroll to; the first is +1 to denote that
1931 * this position is relative to the beginning of the scrollback, -1
1932 * to denote it is relative to the end, and 0 to denote that it is
1933 * relative to the current position.
1935 void term_scroll (int rel
, int where
) {
1936 int n
= where
* (cols
+1);
1938 disptop
= (rel
< 0 ? scrtop
:
1939 rel
> 0 ? sbtop
: disptop
) + n
;
1940 if (disptop
< sbtop
)
1942 if (disptop
> scrtop
)
1948 static void clipme(unsigned long *top
, unsigned long *bottom
, char *workbuf
) {
1949 char *wbptr
; /* where next char goes within workbuf */
1950 int wblen
= 0; /* workbuf len */
1951 int buflen
; /* amount of memory allocated to workbuf */
1953 if ( workbuf
!= NULL
) { /* user supplied buffer? */
1954 buflen
= -1; /* assume buffer passed in is big enough */
1955 wbptr
= workbuf
; /* start filling here */
1958 buflen
= 0; /* No data is available yet */
1960 while (top
< bottom
) {
1962 unsigned long *lineend
= top
- (top
-text
) % (cols
+1) + cols
;
1963 unsigned long *nlpos
= lineend
;
1965 if (!(*nlpos
& ATTR_WRAPPED
)) {
1966 while ((nlpos
[-1] & CHAR_MASK
) == 0x20 && nlpos
> top
)
1971 while (top
< nlpos
&& top
< bottom
)
1973 int ch
= (*top
& CHAR_MASK
);
1974 int set
= (*top
& CSET_MASK
);
1976 /* VT Specials -> ISO8859-1 for Cut&Paste */
1977 static const unsigned char poorman2
[] =
1978 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
1980 if (set
&& !cfg
.rawcnp
) {
1981 if (set
== ATTR_LINEDRW
&& ch
>= 0x60 && ch
< 0x7F) {
1983 if ((x
= poorman2
[2*(ch
-0x60)+1]) == ' ')
1985 ch
= (x
<<8) + poorman2
[2*(ch
-0x60)];
1990 if (cfg
.rawcnp
|| !!(ch
&0xE0)) {
1991 if ( wblen
== buflen
)
1993 workbuf
= srealloc(workbuf
, buflen
+= 100);
1994 wbptr
= workbuf
+ wblen
;
1997 *wbptr
++ = (unsigned char) ch
;
2005 for (i
=0; i
<sizeof(sel_nl
); i
++)
2007 if ( wblen
== buflen
)
2009 workbuf
= srealloc(workbuf
, buflen
+= 100);
2010 wbptr
= workbuf
+ wblen
;
2013 *wbptr
++ = sel_nl
[i
];
2016 top
= lineend
+ 1; /* start of next line */
2018 write_clip (workbuf
, wblen
, FALSE
); /* transfer to clipboard */
2019 if ( buflen
> 0 ) /* indicates we allocated this buffer */
2023 void term_copyall (void) {
2024 clipme(sbtop
, cpos
, NULL
/* dynamic allocation */);
2028 * Spread the selection outwards according to the selection mode.
2030 static unsigned long *sel_spread_half (unsigned long *p
, int dir
) {
2031 unsigned long *linestart
, *lineend
;
2035 x
= (p
- text
) % (cols
+1);
2037 lineend
= linestart
+ cols
;
2042 * In this mode, every character is a separate unit, except
2043 * for runs of spaces at the end of a non-wrapping line.
2045 if (!(linestart
[cols
] & ATTR_WRAPPED
)) {
2046 unsigned long *q
= lineend
;
2047 while (q
> linestart
&& (q
[-1] & CHAR_MASK
) == 0x20)
2052 p
= (dir
== -1 ? q
: lineend
- 1);
2057 * In this mode, the units are maximal runs of characters
2058 * whose `wordness' has the same value.
2060 wvalue
= wordness
[*p
& CHAR_MASK
];
2062 while (p
< lineend
&& wordness
[p
[1] & CHAR_MASK
] == wvalue
)
2065 while (p
> linestart
&& wordness
[p
[-1] & CHAR_MASK
] == wvalue
)
2071 * In this mode, every line is a unit.
2073 p
= (dir
== -1 ? linestart
: lineend
- 1);
2079 static void sel_spread (void) {
2080 selstart
= sel_spread_half (selstart
, -1);
2081 selend
= sel_spread_half (selend
- 1, +1) + 1;
2084 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
2085 unsigned long *selpoint
;
2088 if (y
>=rows
) y
= rows
-1;
2096 if (x
>=cols
) x
= cols
-1;
2098 selpoint
= disptop
+ y
* (cols
+1);
2099 if ((selpoint
[cols
]&LATTR_MODE
) != LATTR_NORM
)
2104 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
2106 selstate
= ABOUT_TO
;
2107 selanchor
= selpoint
;
2109 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
2111 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
2112 selstate
= DRAGGING
;
2113 selstart
= selanchor
= selpoint
;
2114 selend
= selstart
+ 1;
2116 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
2117 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
2118 if (selstate
== ABOUT_TO
&& selanchor
== selpoint
)
2120 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
2121 if (selpoint
-selstart
< (selend
-selstart
)/2)
2122 selanchor
= selend
- 1;
2124 selanchor
= selstart
;
2125 selstate
= DRAGGING
;
2127 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
2128 selanchor
= selpoint
;
2129 selstate
= DRAGGING
;
2130 if (selpoint
< selanchor
) {
2131 selstart
= selpoint
;
2132 selend
= selanchor
+ 1;
2134 selstart
= selanchor
;
2135 selend
= selpoint
+ 1;
2138 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
2139 if (selstate
== DRAGGING
) {
2141 * We've completed a selection. We now transfer the
2142 * data to the clipboard.
2144 clipme(selstart
, selend
, selspace
);
2145 selstate
= SELECTED
;
2147 selstate
= NO_SELECTION
;
2148 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
2152 get_clip((void **) &data
, &len
);
2156 if (paste_buffer
) sfree(paste_buffer
);
2157 paste_pos
= paste_hold
= paste_len
= 0;
2158 paste_buffer
= smalloc(len
);
2161 while (p
< data
+len
) {
2162 while (p
< data
+len
&&
2163 !(p
<= data
+len
-sizeof(sel_nl
) &&
2164 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
2172 c
=xlat_kbd2tty(q
[i
]);
2173 paste_buffer
[paste_len
++] = c
;
2177 if (p
<= data
+len
-sizeof(sel_nl
) &&
2178 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
2179 paste_buffer
[paste_len
++] = '\r';
2180 p
+= sizeof(sel_nl
);
2185 /* Assume a small paste will be OK in one go. */
2186 if (paste_len
<256) {
2187 ldisc_send (paste_buffer
, paste_len
);
2188 if (paste_buffer
) sfree(paste_buffer
);
2190 paste_pos
= paste_hold
= paste_len
= 0;
2193 get_clip(NULL
, NULL
);
2199 void term_nopaste() {
2200 if(paste_len
== 0) return;
2201 sfree(paste_buffer
);
2207 static long last_paste
= 0;
2208 long now
, paste_diff
;
2210 if(paste_len
== 0) return;
2212 /* Don't wait forever to paste */
2214 now
= GetTickCount();
2215 paste_diff
= now
-last_paste
;
2216 if (paste_diff
>=0 && paste_diff
<450)
2221 while(paste_pos
<paste_len
)
2224 while (n
+ paste_pos
< paste_len
) {
2225 if (paste_buffer
[paste_pos
+ n
++] == '\r')
2228 ldisc_send (paste_buffer
+paste_pos
, n
);
2231 if (paste_pos
< paste_len
) {
2236 sfree(paste_buffer
);
2241 static void deselect (void) {
2242 selstate
= NO_SELECTION
;
2243 selstart
= selend
= scrtop
;
2246 void term_deselect (void) {
2251 int term_ldisc(int option
) {
2252 if (option
== LD_ECHO
) return term_echoing
;
2253 if (option
== LD_EDIT
) return term_editing
;
2258 * from_backend(), to get data from the backend for the terminal.
2260 void from_backend(int is_stderr
, char *data
, int len
) {
2262 if (inbuf_head
>= INBUF_SIZE
)
2264 inbuf
[inbuf_head
++] = *data
++;
2269 * Log session traffic.
2271 void logtraffic(unsigned char c
, int logmode
) {
2272 if (cfg
.logtype
> 0) {
2273 if (cfg
.logtype
== logmode
) {
2274 /* deferred open file from pgm start? */
2275 if (!lgfp
) logfopen();
2276 if (lgfp
) fputc (c
, lgfp
);
2281 /* open log file append/overwrite mode */
2282 void logfopen(void) {
2290 sprintf (writemod
, "wb"); /* default to rewrite */
2291 lgfp
= fopen(cfg
.logfilename
, "r"); /* file already present? */
2295 i
= askappend(cfg
.logfilename
);
2297 writemod
[0] = 'a'; /* set append mode */
2298 else if (i
== 0) { /* cancelled */
2300 cfg
.logtype
= 0; /* disable logging */
2305 lgfp
= fopen(cfg
.logfilename
, writemod
);
2306 if (lgfp
) { /* enter into event log */
2307 sprintf(buf
, "%s session log (%s mode) to file : ",
2308 (writemod
[0] == 'a') ?
"Appending" : "Writing new",
2309 (cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
2310 cfg
.logtype
== LGTYP_DEBUG ?
"raw" : "<ukwn>") );
2311 /* Make sure we do not exceed the output buffer size */
2312 strncat (buf
, cfg
.logfilename
, 128);
2313 buf
[strlen(buf
)] = '\0';
2316 /* --- write header line iinto log file */
2317 fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp
);
2320 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", tm
);
2322 fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp
);
2326 void logfclose (void) {
2327 if (lgfp
) { fclose(lgfp
); lgfp
= NULL
; }