11 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
12 #define CL_VT100 0x0002 /* VT100 */
13 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
14 #define CL_VT102 0x0008 /* VT102 */
15 #define CL_VT220 0x0010 /* VT220 */
16 #define CL_VT320 0x0020 /* VT320 */
17 #define CL_VT420 0x0040 /* VT420 */
18 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
19 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
20 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
21 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
22 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
24 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
25 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
26 #define TM_VT102 (TM_VT100AVO|CL_VT102)
27 #define TM_VT220 (TM_VT102|CL_VT220)
28 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
29 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
31 #define TM_PUTTY (0xFFFF)
33 #define compatibility(x) \
34 if ( ((CL_##x)&compatibility_level) == 0 ) { \
38 #define compatibility2(x,y) \
39 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
44 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
46 static int compatibility_level
= TM_PUTTY
;
48 static tree234
*scrollback
; /* lines scrolled off top of screen */
49 static tree234
*screen
; /* lines on primary screen */
50 static tree234
*alt_screen
; /* lines on alternate screen */
51 static int disptop
; /* distance scrolled back (0 or negative) */
53 static unsigned long *cpos
; /* cursor position (convenience) */
55 static unsigned long *disptext
; /* buffer of text on real screen */
56 static unsigned long *wanttext
; /* buffer of text we want on screen */
58 #define VBELL_TIMEOUT 100 /* millisecond duration of visual bell */
61 struct beeptime
*next
;
64 static struct beeptime
*beephead
, *beeptail
;
69 static unsigned char *selspace
; /* buffer for building selections in */
71 #define TSIZE (sizeof(unsigned long))
72 #define lineptr(x) ( (unsigned long *) \
73 ((x) >= 0 ? index234(screen, x) : \
74 index234(scrollback, (x)+count234(scrollback)) ) )
75 #define fix_cpos do { cpos = lineptr(curs.y) + curs.x; } while(0)
77 static unsigned long curr_attr
, save_attr
;
78 static unsigned long erase_char
= ERASE_CHAR
;
83 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
84 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
85 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
86 #define posdiff(p1,p2) ( ((p2).y - (p1).y) * (cols+1) + (p2).x - (p1).x )
87 #define incpos(p) ( (p).x == cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
88 #define decpos(p) ( (p).x == 0 ? ((p).x = cols, (p).y--, 1) : ((p).x--, 0) )
90 static pos curs
; /* cursor */
91 static pos savecurs
; /* saved cursor position */
92 static int marg_t
, marg_b
; /* scroll margins */
93 static int dec_om
; /* DEC origin mode flag */
94 static int wrap
, wrapnext
; /* wrap flags */
95 static int insert
; /* insert-mode flag */
96 static int cset
; /* 0 or 1: which char set */
97 static int save_cset
, save_csattr
; /* saved with cursor position */
98 static int rvideo
; /* global reverse video flag */
99 static int cursor_on
; /* cursor enabled flag */
100 static int reset_132
; /* Flag ESC c resets to 80 cols */
101 static int use_bce
; /* Use Background coloured erase */
102 static int blinker
; /* When blinking is the cursor on ? */
103 static int tblinker
; /* When the blinking text is on */
104 static int blink_is_real
; /* Actually blink blinking text */
105 static int term_echoing
; /* Does terminal want local echo? */
106 static int term_editing
; /* Does terminal want local edit? */
108 static unsigned long cset_attr
[2];
111 * Saved settings on the alternate screen.
113 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
114 static int alt_t
, alt_b
;
115 static int alt_which
;
117 #define ARGS_MAX 32 /* max # of esc sequence arguments */
118 #define ARG_DEFAULT 0 /* if an arg isn't specified */
119 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
120 static int esc_args
[ARGS_MAX
];
121 static int esc_nargs
;
122 static int esc_query
;
123 #define ANSI(x,y) ((x)+((y)<<8))
124 #define ANSI_QUE(x) ANSI(x,TRUE)
126 #define OSC_STR_MAX 2048
127 static int osc_strlen
;
128 static char osc_string
[OSC_STR_MAX
+1];
131 static char id_string
[1024] = "\033[?6c";
133 static unsigned char *tabs
;
147 OSC_STRING
, OSC_MAYBE_ST
,
155 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
158 SM_CHAR
, SM_WORD
, SM_LINE
160 static pos selstart
, selend
, selanchor
;
162 static short wordness
[256] = {
163 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 */
164 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 */
165 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 */
166 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 */
167 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 */
168 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 */
169 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 */
170 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 */
173 static unsigned char sel_nl
[] = SEL_NL
;
174 static char * paste_buffer
= 0;
175 static int paste_len
, paste_pos
, paste_hold
;
178 * Internal prototypes.
180 static void do_paint (Context
, int);
181 static void erase_lots (int, int, int);
182 static void swap_screen (int);
183 static void update_sbar (void);
184 static void deselect (void);
185 /* log session to file stuff ... */
186 static FILE *lgfp
= NULL
;
187 static void logtraffic(unsigned char c
, int logmode
);
190 * Set up power-on settings for the terminal.
192 static void power_on(void) {
193 curs
.x
= curs
.y
= alt_x
= alt_y
= savecurs
.x
= savecurs
.y
= 0;
196 alt_b
= marg_b
= rows
- 1;
201 for (i
= 0; i
< cols
; i
++)
202 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
204 alt_om
= dec_om
= cfg
.dec_om
;
205 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
206 alt_wrap
= wrap
= cfg
.wrap_mode
;
208 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
212 save_attr
= curr_attr
= ATTR_DEFAULT
;
213 term_editing
= term_echoing
= FALSE
;
214 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
215 app_cursor_keys
= cfg
.app_cursor
;
216 app_keypad_keys
= cfg
.app_keypad
;
218 blink_is_real
= cfg
.blinktext
;
219 erase_char
= ERASE_CHAR
;
223 for (i
= 0; i
< 256; i
++)
224 wordness
[i
] = cfg
.wordness
[i
];
228 erase_lots (FALSE
, TRUE
, TRUE
);
230 erase_lots (FALSE
, TRUE
, TRUE
);
235 * Force a screen update.
237 void term_update(void) {
241 if ( (seen_key_event
&& (cfg
.scroll_on_key
)) ||
242 (seen_disp_event
&& (cfg
.scroll_on_disp
)) ) {
243 disptop
= 0; /* return to main screen */
244 seen_disp_event
= seen_key_event
= 0;
247 do_paint (ctx
, TRUE
);
248 sys_cursor(curs
.x
, curs
.y
- disptop
);
254 * Same as power_on(), but an external function.
256 void term_pwron(void) {
265 * Clear the scrollback.
267 void term_clrsb(void) {
270 while ((line
= delpos234(scrollback
, 0)) != NULL
) {
277 * Initialise the terminal.
279 void term_init(void) {
280 screen
= alt_screen
= scrollback
= NULL
;
282 disptext
= wanttext
= NULL
;
288 beephead
= beeptail
= NULL
;
291 beep_overloaded
= FALSE
;
295 * Set up the terminal for a given size.
297 void term_size(int newrows
, int newcols
, int newsavelines
) {
298 tree234
*newsb
, *newscreen
, *newalt
;
299 unsigned long *newdisp
, *newwant
, *oldline
, *line
;
301 int posn
, oldposn
, furthest_back
, oldsbsize
;
302 int save_alt_which
= alt_which
;
304 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
305 return; /* nothing to do */
311 alt_b
= marg_b
= newrows
- 1;
313 newsb
= newtree234(NULL
);
314 newscreen
= newtree234(NULL
);
315 ccols
= (cols
< newcols ? cols
: newcols
);
316 oldsbsize
= scrollback ?
count234(scrollback
) : 0;
317 furthest_back
= newrows
- rows
- oldsbsize
;
318 if (furthest_back
> 0)
320 if (furthest_back
< -newsavelines
)
321 furthest_back
= -newsavelines
;
322 for (posn
= newrows
; posn
-- > furthest_back
;) {
323 oldposn
= posn
- newrows
+ rows
;
324 if (rows
== -1 || oldposn
< -oldsbsize
) {
325 line
= smalloc(TSIZE
* (newcols
+ 1));
326 for (j
= 0; j
< newcols
; j
++)
327 line
[j
] = erase_char
;
330 oldline
= (oldposn
>= 0 ?
331 delpos234(screen
, count234(screen
)-1) :
332 delpos234(scrollback
, count234(scrollback
)-1));
333 if (newcols
!= cols
) {
334 line
= smalloc(TSIZE
* (newcols
+ 1));
335 for (j
= 0; j
< ccols
; j
++)
336 line
[j
] = oldline
[j
];
337 for (j
= ccols
; j
< newcols
; j
++)
338 line
[j
] = erase_char
;
339 line
[newcols
] = oldline
[cols
] & LATTR_MODE
;
346 addpos234(newscreen
, line
, 0);
348 addpos234(newsb
, line
, 0);
351 if (scrollback
) freetree234(scrollback
);
352 if (screen
) freetree234(screen
);
356 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
357 for (i
=0; i
<newrows
*(newcols
+1); i
++)
358 newdisp
[i
] = ATTR_INVALID
;
362 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
363 for (i
=0; i
<newrows
*(newcols
+1); i
++)
364 newwant
[i
] = ATTR_INVALID
;
368 newalt
= newtree234(NULL
);
369 for (i
=0; i
<newrows
; i
++) {
370 line
= smalloc(TSIZE
* (newcols
+1));
371 for (j
= 0; j
<= newcols
; j
++)
372 line
[i
] = erase_char
;
373 addpos234(newalt
, line
, i
);
376 while (NULL
!= (line
= delpos234(alt_screen
, 0)))
378 freetree234(alt_screen
);
383 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
385 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
388 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
389 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
393 curs
.y
+= newrows
- rows
;
396 if (curs
.y
>= newrows
)
398 if (curs
.x
>= newcols
)
401 wrapnext
= alt_wnext
= FALSE
;
405 savelines
= newsavelines
;
408 swap_screen(save_alt_which
);
417 static void swap_screen (int which
) {
421 if (which
== alt_which
)
426 ttr
= alt_screen
; alt_screen
= screen
; screen
= ttr
;
427 t
= curs
.x
; curs
.x
= alt_x
; alt_x
= t
;
428 t
= curs
.y
; curs
.y
= alt_y
; alt_y
= t
;
429 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
430 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
431 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
432 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
433 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
434 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
435 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
441 * Update the scroll bar.
443 static void update_sbar(void) {
446 nscroll
= count234(scrollback
);
448 set_sbar (nscroll
+ rows
, nscroll
+ disptop
, rows
);
452 * Check whether the region bounded by the two pointers intersects
453 * the scroll region, and de-select the on-screen selection if so.
455 static void check_selection (pos from
, pos to
) {
456 if (poslt(from
, selend
) && poslt(selstart
, to
))
461 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
462 * for backward.) `sb' is TRUE if the scrolling is permitted to
463 * affect the scrollback buffer.
465 static void scroll (int topline
, int botline
, int lines
, int sb
) {
466 unsigned long *line
, *line2
;
474 line
= delpos234(screen
, botline
);
475 for (i
= 0; i
< cols
; i
++)
476 line
[i
] = erase_char
;
478 addpos234(screen
, line
, topline
);
480 if (selstart
.y
>= topline
&& selstart
.y
<= botline
) {
482 if (selstart
.y
> botline
) {
483 selstart
.y
= botline
;
487 if (selend
.y
>= topline
&& selend
.y
<= botline
) {
489 if (selend
.y
> botline
) {
499 line
= delpos234(screen
, topline
);
501 int sblen
= count234(scrollback
);
503 * We must add this line to the scrollback. We'll
504 * remove a line from the top of the scrollback to
505 * replace it, or allocate a new one if the
506 * scrollback isn't full.
508 if (sblen
== savelines
)
509 sblen
--, line2
= delpos234(scrollback
, 0);
511 line2
= smalloc(TSIZE
* (cols
+1));
512 addpos234(scrollback
, line
, sblen
);
515 for (i
= 0; i
< cols
; i
++)
516 line
[i
] = erase_char
;
518 addpos234(screen
, line
, botline
);
520 if (selstart
.y
>= topline
&& selstart
.y
<= botline
) {
522 if (selstart
.y
< topline
) {
523 selstart
.y
= topline
;
527 if (selend
.y
>= topline
&& selend
.y
<= botline
) {
529 if (selend
.y
< topline
) {
541 * Move the cursor to a given position, clipping at boundaries. We
542 * may or may not want to clip at the scroll margin: marg_clip is 0
543 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
544 * even _being_ outside the margins.
546 static void move (int x
, int y
, int marg_clip
) {
552 if ((curs
.y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
554 if ((curs
.y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
568 * Save or restore the cursor and SGR mode.
570 static void save_cursor(int save
) {
573 save_attr
= curr_attr
;
575 save_csattr
= cset_attr
[cset
];
578 /* Make sure the window hasn't shrunk since the save */
579 if (curs
.x
>= cols
) curs
.x
= cols
-1;
580 if (curs
.y
>= rows
) curs
.y
= rows
-1;
582 curr_attr
= save_attr
;
584 cset_attr
[cset
] = save_csattr
;
586 if (use_bce
) erase_char
= (' ' |(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
591 * Erase a large portion of the screen: the whole screen, or the
592 * whole line, or parts thereof.
594 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
597 unsigned long *ldata
;
618 check_selection (start
, end
);
620 /* Clear screen also forces a full window redraw, just in case. */
621 if (start
.y
== 0 && start
.x
== 0 && end
.y
== rows
)
624 ldata
= lineptr(start
.y
);
625 while (poslt(start
, end
)) {
626 if (start
.y
== cols
&& !erase_lattr
)
627 ldata
[start
.x
] &= ~ATTR_WRAPPED
;
629 ldata
[start
.x
] = erase_char
;
631 ldata
= lineptr(start
.y
);
636 * Insert or delete characters within the current line. n is +ve if
637 * insertion is desired, and -ve for deletion.
639 static void insch (int n
) {
640 int dir
= (n
< 0 ?
-1 : +1);
643 unsigned long *ldata
;
645 n
= (n
< 0 ?
-n
: n
);
646 if (n
> cols
- curs
.x
)
648 m
= cols
- curs
.x
- n
;
650 cursplus
.x
= curs
.x
+ n
;
651 check_selection (curs
, cursplus
);
652 ldata
= lineptr(curs
.y
);
654 memmove (ldata
+ curs
.x
, ldata
+ curs
.x
+ n
, m
*TSIZE
);
656 ldata
[curs
.x
+ m
++] = erase_char
;
658 memmove (ldata
+ curs
.x
+ n
, ldata
+ curs
.x
, m
*TSIZE
);
660 ldata
[curs
.x
+ n
] = erase_char
;
665 * Toggle terminal mode `mode' to state `state'. (`query' indicates
666 * whether the mode is a DEC private one or a normal one.)
668 static void toggle_mode (int mode
, int query
, int state
) {
669 if (query
) switch (mode
) {
670 case 1: /* application cursor keys */
671 app_cursor_keys
= state
;
673 case 2: /* VT52 mode */
676 case 3: /* 80/132 columns */
678 request_resize (state ?
132 : 80, rows
, 1);
681 case 5: /* reverse video */
683 seen_disp_event
= TRUE
;
684 if (state
) term_update();
686 case 6: /* DEC origin mode */
689 case 7: /* auto wrap */
692 case 8: /* auto key repeat */
695 case 10: /* set local edit mode */
696 term_editing
= state
;
697 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
699 case 25: /* enable/disable cursor */
700 compatibility2(OTHER
,VT220
);
702 seen_disp_event
= TRUE
;
704 case 47: /* alternate screen */
705 compatibility(OTHER
);
710 } else switch (mode
) {
711 case 4: /* set insert mode */
712 compatibility(VT102
);
715 case 12: /* set echo mode */
716 term_echoing
= !state
;
717 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
719 case 20: /* Return sends ... */
720 cr_lf_return
= state
;
726 * Process an OSC sequence: set window title or icon name.
728 static void do_osc(void) {
731 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
733 osc_string
[osc_strlen
] = '\0';
734 switch (esc_args
[0]) {
737 set_icon (osc_string
);
738 if (esc_args
[0] == 1)
740 /* fall through: parameter 0 means set both */
743 set_title (osc_string
);
750 * Remove everything currently in `inbuf' and stick it up on the
751 * in-memory display. There's a big state machine in here to
752 * process escape sequences...
754 void term_out(void) {
757 for(inbuf_reap
= 0; inbuf_reap
< inbuf_head
; inbuf_reap
++)
759 c
= inbuf
[inbuf_reap
];
762 * Optionally log the session traffic to a file. Useful for
763 * debugging and possibly also useful for actual logging.
765 logtraffic((unsigned char)c
, LGTYP_DEBUG
);
767 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
768 * be able to display 8-bit characters, but I'll let that go 'cause
771 if( ( (c
&0x60) == 0 || c
== '\177') &&
772 termstate
< DO_CTRLS
&&
773 ( (c
&0x80) == 0 || has_compat(VT220
))) {
775 case '\005': /* terminal type query */
776 /* Strictly speaking this is VT100 but a VT100 defaults to
777 * no response. Other terminals respond at their option.
779 * Don't put a CR in the default string as this tends to
780 * upset some weird software.
782 * An xterm returns "xterm" (5 characters)
784 compatibility(ANSIMIN
);
786 char abuf
[256], *s
, *d
;
788 for(s
=cfg
.answerback
, d
=abuf
; *s
; s
++) {
791 if (*s
>= 'a' && *s
<= 'z')
792 *d
++ = (*s
- ('a'-1));
793 else if ((*s
>='@' && *s
<='_') ||
794 *s
== '?' || (*s
&0x80))
800 else if (*s
== '^') {
804 *d
++ = xlat_kbd2tty((unsigned char)*s
);
806 ldisc_send (abuf
, d
-abuf
);
811 struct beeptime
*newbeep
;
814 ticks
= GetTickCount();
816 if (!beep_overloaded
) {
817 newbeep
= smalloc(sizeof(struct beeptime
));
818 newbeep
->ticks
= ticks
;
819 newbeep
->next
= NULL
;
823 beeptail
->next
= newbeep
;
829 * Throw out any beeps that happened more than
833 beephead
->ticks
< ticks
- cfg
.bellovl_t
*1000) {
834 struct beeptime
*tmp
= beephead
;
835 beephead
= tmp
->next
;
842 if (cfg
.bellovl
&& beep_overloaded
&&
843 ticks
-lastbeep
>= cfg
.bellovl_s
* 1000) {
845 * If we're currently overloaded and the
846 * last beep was more than s seconds ago,
847 * leave overload mode.
849 beep_overloaded
= FALSE
;
850 } else if (cfg
.bellovl
&& !beep_overloaded
&&
851 nbeeps
>= cfg
.bellovl_n
) {
853 * Now, if we have n or more beeps
854 * remaining in the queue, go into overload
857 beep_overloaded
= TRUE
;
862 * Perform an actual beep if we're not overloaded.
864 if ((!cfg
.bellovl
|| !beep_overloaded
) && cfg
.beep
!= 0) {
867 else if(cfg
.beep
== 2) {
869 vbell_timeout
= ticks
+ VBELL_TIMEOUT
;
877 if (curs
.x
== 0 && curs
.y
== 0)
879 else if (curs
.x
== 0 && curs
.y
> 0)
880 curs
.x
= cols
-1, curs
.y
--;
886 seen_disp_event
= TRUE
;
889 compatibility(VT100
);
893 compatibility(VT100
);
898 termstate
= VT52_ESC
;
900 compatibility(ANSIMIN
);
901 termstate
= SEEN_ESC
;
905 compatibility(VT220
);
906 termstate
= SEEN_CSI
;
908 esc_args
[0] = ARG_DEFAULT
;
912 compatibility(VT220
);
913 termstate
= SEEN_OSC
;
920 seen_disp_event
= TRUE
;
922 logtraffic((unsigned char)c
,LGTYP_ASCII
);
926 compatibility(VT100
);
928 if (curs
.y
== marg_b
)
929 scroll (marg_t
, marg_b
, 1, TRUE
);
930 else if (curs
.y
< rows
-1)
938 logtraffic((unsigned char)c
,LGTYP_ASCII
);
943 unsigned long *ldata
= lineptr(curs
.y
);
947 } while (curs
.x
< cols
-1 && !tabs
[curs
.x
]);
949 if ((ldata
[cols
] & LATTR_MODE
) != LATTR_NORM
)
951 if (curs
.x
>= cols
/2)
961 check_selection (old_curs
, curs
);
963 seen_disp_event
= TRUE
;
965 case '\177': /* Destructive backspace
966 This does nothing on a real VT100 */
967 compatibility(OTHER
);
968 if (curs
.x
&& !wrapnext
) curs
.x
--;
971 *cpos
= (' ' | curr_attr
| ATTR_ASCII
);
975 else switch (termstate
) {
977 /* Only graphic characters get this far, ctrls are stripped above */
978 if (wrapnext
&& wrap
) {
979 cpos
[1] |= ATTR_WRAPPED
;
980 if (curs
.y
== marg_b
)
981 scroll (marg_t
, marg_b
, 1, TRUE
);
982 else if (curs
.y
< rows
-1)
990 if (selstate
!= NO_SELECTION
) {
993 check_selection (curs
, cursplus
);
995 switch (cset_attr
[cset
]) {
997 * Linedraw characters are different from 'ESC ( B'
998 * only for a small range. For ones outside that
999 * range, make sure we use the same font as well as
1000 * the same encoding.
1003 if (c
<0x5f || c
>0x7F)
1004 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
1007 *cpos
++ = ' ' | curr_attr
| ATTR_ASCII
;
1009 *cpos
++ = ((unsigned char)c
) | curr_attr
| ATTR_LINEDRW
;
1012 /* If UK-ASCII, make the '#' a LineDraw Pound */
1014 *cpos
++ = '}' | curr_attr
| ATTR_LINEDRW
;
1019 *cpos
= xlat_tty2scr((unsigned char)c
) | curr_attr
|
1020 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
1021 logtraffic((unsigned char)c
, LGTYP_ASCII
);
1026 if (curs
.x
== cols
) {
1031 seen_disp_event
= 1;
1035 termstate
= TOPLEVEL
;
1039 * This state is virtually identical to SEEN_ESC, with the
1040 * exception that we have an OSC sequence in the pipeline,
1041 * and _if_ we see a backslash, we process it.
1045 termstate
= TOPLEVEL
;
1048 /* else fall through */
1050 termstate
= TOPLEVEL
;
1052 case ' ': /* some weird sequence? */
1053 compatibility(VT220
);
1054 termstate
= IGNORE_NEXT
;
1056 case '[': /* enter CSI mode */
1057 termstate
= SEEN_CSI
;
1059 esc_args
[0] = ARG_DEFAULT
;
1062 case ']': /* xterm escape sequences */
1063 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1064 compatibility(OTHER
);
1065 termstate
= SEEN_OSC
;
1068 case '(': /* should set GL */
1069 compatibility(VT100
);
1072 case ')': /* should set GR */
1073 compatibility(VT100
);
1076 case '7': /* save cursor */
1077 compatibility(VT100
);
1080 case '8': /* restore cursor */
1081 compatibility(VT100
);
1082 save_cursor (FALSE
);
1083 seen_disp_event
= TRUE
;
1086 compatibility(VT100
);
1087 app_keypad_keys
= TRUE
;
1090 compatibility(VT100
);
1091 app_keypad_keys
= FALSE
;
1093 case 'D': /* exactly equivalent to LF */
1094 compatibility(VT100
);
1095 if (curs
.y
== marg_b
)
1096 scroll (marg_t
, marg_b
, 1, TRUE
);
1097 else if (curs
.y
< rows
-1)
1101 seen_disp_event
= TRUE
;
1103 case 'E': /* exactly equivalent to CR-LF */
1104 compatibility(VT100
);
1106 if (curs
.y
== marg_b
)
1107 scroll (marg_t
, marg_b
, 1, TRUE
);
1108 else if (curs
.y
< rows
-1)
1112 seen_disp_event
= TRUE
;
1114 case 'M': /* reverse index - backwards LF */
1115 compatibility(VT100
);
1116 if (curs
.y
== marg_t
)
1117 scroll (marg_t
, marg_b
, -1, TRUE
);
1118 else if (curs
.y
> 0)
1122 seen_disp_event
= TRUE
;
1124 case 'Z': /* terminal type query */
1125 compatibility(VT100
);
1126 ldisc_send (id_string
, strlen(id_string
));
1128 case 'c': /* restore power-on settings */
1129 compatibility(VT100
);
1132 request_resize (80, rows
, 1);
1137 seen_disp_event
= TRUE
;
1139 case '#': /* ESC # 8 fills screen with Es :-) */
1140 compatibility(VT100
);
1141 termstate
= SEEN_ESCHASH
;
1143 case 'H': /* set a tab */
1144 compatibility(VT100
);
1145 tabs
[curs
.x
] = TRUE
;
1150 termstate
= TOPLEVEL
; /* default */
1153 if (esc_nargs
<= ARGS_MAX
) {
1154 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
1155 esc_args
[esc_nargs
-1] = 0;
1156 esc_args
[esc_nargs
-1] =
1157 10 * esc_args
[esc_nargs
-1] + c
- '0';
1159 termstate
= SEEN_CSI
;
1163 if (++esc_nargs
<= ARGS_MAX
)
1164 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
1165 termstate
= SEEN_CSI
;
1169 if( esc_query
) esc_query
= -1;
1170 else if( c
== '?' ) esc_query
= TRUE
;
1172 termstate
= SEEN_CSI
;
1174 else switch (ANSI(c
,esc_query
)) {
1175 case 'A': /* move up N lines */
1176 move (curs
.x
, curs
.y
- def(esc_args
[0], 1), 1);
1177 seen_disp_event
= TRUE
;
1179 case 'e': /* move down N lines */
1180 compatibility(ANSI
);
1182 move (curs
.x
, curs
.y
+ def(esc_args
[0], 1), 1);
1183 seen_disp_event
= TRUE
;
1185 case 'a': /* move right N cols */
1186 compatibility(ANSI
);
1188 move (curs
.x
+ def(esc_args
[0], 1), curs
.y
, 1);
1189 seen_disp_event
= TRUE
;
1191 case 'D': /* move left N cols */
1192 move (curs
.x
- def(esc_args
[0], 1), curs
.y
, 1);
1193 seen_disp_event
= TRUE
;
1195 case 'E': /* move down N lines and CR */
1196 compatibility(ANSI
);
1197 move (0, curs
.y
+ def(esc_args
[0], 1), 1);
1198 seen_disp_event
= TRUE
;
1200 case 'F': /* move up N lines and CR */
1201 compatibility(ANSI
);
1202 move (0, curs
.y
- def(esc_args
[0], 1), 1);
1203 seen_disp_event
= TRUE
;
1205 case 'G': case '`': /* set horizontal posn */
1206 compatibility(ANSI
);
1207 move (def(esc_args
[0], 1) - 1, curs
.y
, 0);
1208 seen_disp_event
= TRUE
;
1210 case 'd': /* set vertical posn */
1211 compatibility(ANSI
);
1212 move (curs
.x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1214 seen_disp_event
= TRUE
;
1216 case 'H': case 'f': /* set horz and vert posns at once */
1218 esc_args
[1] = ARG_DEFAULT
;
1219 move (def(esc_args
[1], 1) - 1,
1220 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1222 seen_disp_event
= TRUE
;
1224 case 'J': /* erase screen or parts of it */
1226 unsigned int i
= def(esc_args
[0], 0) + 1;
1229 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
1232 seen_disp_event
= TRUE
;
1234 case 'K': /* erase line or parts of it */
1236 unsigned int i
= def(esc_args
[0], 0) + 1;
1239 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
1241 seen_disp_event
= TRUE
;
1243 case 'L': /* insert lines */
1244 compatibility(VT102
);
1245 if (curs
.y
<= marg_b
)
1246 scroll (curs
.y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
1247 seen_disp_event
= TRUE
;
1249 case 'M': /* delete lines */
1250 compatibility(VT102
);
1251 if (curs
.y
<= marg_b
)
1252 scroll (curs
.y
, marg_b
, def(esc_args
[0], 1), TRUE
);
1253 seen_disp_event
= TRUE
;
1255 case '@': /* insert chars */
1256 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1257 compatibility(VT102
);
1258 insch (def(esc_args
[0], 1));
1259 seen_disp_event
= TRUE
;
1261 case 'P': /* delete chars */
1262 compatibility(VT102
);
1263 insch (-def(esc_args
[0], 1));
1264 seen_disp_event
= TRUE
;
1266 case 'c': /* terminal type query */
1267 compatibility(VT100
);
1268 /* This is the response for a VT102 */
1269 ldisc_send (id_string
, strlen(id_string
));
1271 case 'n': /* cursor position query */
1272 if (esc_args
[0] == 6) {
1274 sprintf (buf
, "\033[%d;%dR", curs
.y
+ 1, curs
.x
+ 1);
1275 ldisc_send (buf
, strlen(buf
));
1277 else if (esc_args
[0] == 5) {
1278 ldisc_send ("\033[0n", 4);
1281 case 'h': /* toggle modes to high */
1283 compatibility(VT100
);
1286 for (i
=0; i
<esc_nargs
; i
++)
1287 toggle_mode (esc_args
[i
], esc_query
, TRUE
);
1290 case 'l': /* toggle modes to low */
1292 compatibility(VT100
);
1295 for (i
=0; i
<esc_nargs
; i
++)
1296 toggle_mode (esc_args
[i
], esc_query
, FALSE
);
1299 case 'g': /* clear tabs */
1300 compatibility(VT100
);
1301 if (esc_nargs
== 1) {
1302 if (esc_args
[0] == 0) {
1303 tabs
[curs
.x
] = FALSE
;
1304 } else if (esc_args
[0] == 3) {
1306 for (i
= 0; i
< cols
; i
++)
1311 case 'r': /* set scroll margins */
1312 compatibility(VT100
);
1313 if (esc_nargs
<= 2) {
1315 top
= def(esc_args
[0], 1) - 1;
1316 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
1317 def(esc_args
[1], rows
)) - 1;
1320 /* VTTEST Bug 9 - if region is less than 2 lines
1321 * don't change region.
1328 * I used to think the cursor should be
1329 * placed at the top of the newly marginned
1330 * area. Apparently not: VMS TPU falls over
1333 * Well actually it should for Origin mode - RDB
1335 curs
.y
= (dec_om ? marg_t
: 0);
1337 seen_disp_event
= TRUE
;
1341 case 'm': /* set graphics rendition */
1344 * A VT100 without the AVO only had one attribute, either
1345 * underline or reverse video depending on the cursor type,
1346 * this was selected by CSI 7m.
1349 * This is DIM on the VT100-AVO and VT102
1351 * This is BLINK on the VT100-AVO and VT102+
1353 * This is INVIS on the VT100-AVO and VT102
1355 * This like 22 disables BOLD, DIM and INVIS
1357 * The ANSI colours appear on any terminal that has colour
1358 * (obviously) but the interaction between sgr0 and the
1359 * colours varies but is usually related to the background
1360 * colour erase item.
1361 * The interaction between colour attributes and the mono
1362 * ones is also very implementation dependent.
1364 * The 39 and 49 attributes are likely to be unimplemented.
1367 for (i
=0; i
<esc_nargs
; i
++) {
1368 switch (def(esc_args
[i
], 0)) {
1369 case 0: /* restore defaults */
1370 curr_attr
= ATTR_DEFAULT
; break;
1371 case 1: /* enable bold */
1372 compatibility(VT100AVO
);
1373 curr_attr
|= ATTR_BOLD
; break;
1374 case 21: /* (enable double underline) */
1375 compatibility(OTHER
);
1376 case 4: /* enable underline */
1377 compatibility(VT100AVO
);
1378 curr_attr
|= ATTR_UNDER
; break;
1379 case 5: /* enable blink */
1380 compatibility(VT100AVO
);
1381 curr_attr
|= ATTR_BLINK
; break;
1382 case 7: /* enable reverse video */
1383 curr_attr
|= ATTR_REVERSE
; break;
1384 case 22: /* disable bold */
1385 compatibility2(OTHER
,VT220
);
1386 curr_attr
&= ~ATTR_BOLD
; break;
1387 case 24: /* disable underline */
1388 compatibility2(OTHER
,VT220
);
1389 curr_attr
&= ~ATTR_UNDER
; break;
1390 case 25: /* disable blink */
1391 compatibility2(OTHER
,VT220
);
1392 curr_attr
&= ~ATTR_BLINK
; break;
1393 case 27: /* disable reverse video */
1394 compatibility2(OTHER
,VT220
);
1395 curr_attr
&= ~ATTR_REVERSE
; break;
1396 case 30: case 31: case 32: case 33:
1397 case 34: case 35: case 36: case 37:
1399 curr_attr
&= ~ATTR_FGMASK
;
1400 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
1402 case 39: /* default-foreground */
1403 curr_attr
&= ~ATTR_FGMASK
;
1404 curr_attr
|= ATTR_DEFFG
;
1406 case 40: case 41: case 42: case 43:
1407 case 44: case 45: case 46: case 47:
1409 curr_attr
&= ~ATTR_BGMASK
;
1410 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
1412 case 49: /* default-background */
1413 curr_attr
&= ~ATTR_BGMASK
;
1414 curr_attr
|= ATTR_DEFBG
;
1421 (curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
|ATTR_BLINK
))
1425 case 's': /* save cursor */
1428 case 'u': /* restore cursor */
1429 save_cursor (FALSE
);
1430 seen_disp_event
= TRUE
;
1432 case 't': /* set page size - ie window height */
1434 * VT340/VT420 sequence DECSLPP, DEC only allows values
1435 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1436 * illegal values (eg first arg 1..9) for window changing
1439 compatibility(VT340TEXT
);
1440 if (esc_nargs
<=1 && (esc_args
[0]<1 || esc_args
[0]>=24)) {
1441 request_resize (cols
, def(esc_args
[0], 24), 0);
1445 case ANSI('|', '*'):
1446 /* VT420 sequence DECSNLS
1447 * Set number of lines on screen
1448 * VT420 uses VGA like hardware and can support any size in
1449 * reasonable range (24..49 AIUI) with no default specified.
1451 compatibility(VT420
);
1452 if (esc_nargs
==1 && esc_args
[0]>0) {
1453 request_resize (cols
, def(esc_args
[0], cfg
.height
), 0);
1457 case ANSI('|', '$'):
1458 /* VT340/VT420 sequence DECSCPP
1459 * Set number of columns per page
1460 * Docs imply range is only 80 or 132, but I'll allow any.
1462 compatibility(VT340TEXT
);
1464 request_resize (def(esc_args
[0], cfg
.width
), rows
, 0);
1468 case 'X': /* write N spaces w/o moving cursor */
1469 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1470 compatibility(ANSIMIN
);
1472 int n
= def(esc_args
[0], 1);
1474 unsigned long *p
= cpos
;
1475 if (n
> cols
- curs
.x
)
1479 check_selection (curs
, cursplus
);
1482 seen_disp_event
= TRUE
;
1485 case 'x': /* report terminal characteristics */
1486 compatibility(VT100
);
1489 int i
= def(esc_args
[0], 0);
1490 if (i
== 0 || i
== 1) {
1491 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1493 ldisc_send (buf
, 20);
1498 compatibility(OTHER
);
1499 use_bce
= (esc_args
[0]<=0);
1500 erase_char
= ERASE_CHAR
;
1502 erase_char
= (' '|(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
1505 compatibility(OTHER
);
1506 blink_is_real
= (esc_args
[0]>=1);
1509 /* Allow the host to make this emulator a 'perfect' VT102.
1510 * This first appeared in the VT220, but we do need to get
1511 * back to PuTTY mode so I won't check it.
1513 * The arg in 40..42 are a PuTTY extension.
1514 * The 2nd arg, 8bit vs 7bit is not checked.
1516 * Setting VT102 mode should also change the Fkeys to
1517 * generate PF* codes as a real VT102 has no Fkeys.
1518 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1521 * Note ESC c will NOT change this!
1524 switch (esc_args
[0]) {
1525 case 61: compatibility_level
&= ~TM_VTXXX
;
1526 compatibility_level
|= TM_VT102
; break;
1527 case 62: compatibility_level
&= ~TM_VTXXX
;
1528 compatibility_level
|= TM_VT220
; break;
1530 default: if( esc_args
[0] > 60 && esc_args
[0] < 70 )
1531 compatibility_level
|= TM_VTXXX
;
1534 case 40: compatibility_level
&= TM_VTXXX
; break;
1535 case 41: compatibility_level
= TM_PUTTY
; break;
1536 case 42: compatibility_level
= TM_SCOANSI
; break;
1539 compatibility_level
= TM_PUTTY
; break;
1543 /* Change the response to CSI c */
1544 if (esc_args
[0] == 50) {
1547 strcpy(id_string
, "\033[?");
1548 for (i
=1; i
<esc_nargs
; i
++) {
1549 if (i
!=1) strcat(id_string
, ";");
1550 sprintf(lbuf
, "%d", esc_args
[i
]);
1551 strcat(id_string
, lbuf
);
1553 strcat(id_string
, "c");
1557 /* Is this a good idea ?
1558 * Well we should do a soft reset at this point ...
1560 if (!has_compat(VT420
) && has_compat(VT100
)) {
1561 if (reset_132
) request_resize (132, 24, 1);
1562 else request_resize ( 80, 24, 1);
1570 /* VT100 only here, checked above */
1573 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1576 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1579 default: /* specifically, 'B' */
1580 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1583 if( !has_compat(VT220
) || c
!= '%' )
1584 termstate
= TOPLEVEL
;
1589 case 'P': /* Linux palette sequence */
1590 termstate
= SEEN_OSC_P
;
1593 case 'R': /* Linux palette reset */
1596 termstate
= TOPLEVEL
;
1598 case 'W': /* word-set */
1599 termstate
= SEEN_OSC_W
;
1602 case '0': case '1': case '2': case '3': case '4':
1603 case '5': case '6': case '7': case '8': case '9':
1604 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1608 * Grotty hack to support xterm and DECterm title
1609 * sequences concurrently.
1611 if (esc_args
[0] == 2) {
1615 /* else fall through */
1617 termstate
= OSC_STRING
;
1623 * This OSC stuff is EVIL. It takes just one character to get into
1624 * sysline mode and it's not initially obvious how to get out.
1625 * So I've added CR and LF as string aborts.
1626 * This shouldn't effect compatibility as I believe embedded
1627 * control characters are supposed to be interpreted (maybe?)
1628 * and they don't display anything useful anyway.
1632 if (c
== '\n' || c
== '\r') {
1633 termstate
= TOPLEVEL
;
1634 } else if (c
== 0234 || c
== '\007' ) {
1636 * These characters terminate the string; ST and BEL
1637 * terminate the sequence and trigger instant
1638 * processing of it, whereas ESC goes back to SEEN_ESC
1639 * mode unless it is followed by \, in which case it is
1640 * synonymous with ST in the first place.
1643 termstate
= TOPLEVEL
;
1644 } else if (c
== '\033')
1645 termstate
= OSC_MAYBE_ST
;
1646 else if (osc_strlen
< OSC_STR_MAX
)
1647 osc_string
[osc_strlen
++] = c
;
1651 int max
= (osc_strlen
== 0 ?
21 : 16);
1653 if (c
>= '0' && c
<= '9')
1655 else if (c
>= 'A' && c
<= 'A'+max
-10)
1657 else if (c
>= 'a' && c
<= 'a'+max
-10)
1660 termstate
= TOPLEVEL
;
1661 osc_string
[osc_strlen
++] = val
;
1662 if (osc_strlen
>= 7) {
1663 palette_set (osc_string
[0],
1664 osc_string
[1] * 16 + osc_string
[2],
1665 osc_string
[3] * 16 + osc_string
[4],
1666 osc_string
[5] * 16 + osc_string
[6]);
1668 termstate
= TOPLEVEL
;
1674 case '0': case '1': case '2': case '3': case '4':
1675 case '5': case '6': case '7': case '8': case '9':
1676 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1679 termstate
= OSC_STRING
;
1685 unsigned long nlattr
;
1686 unsigned long *ldata
;
1692 for (i
= 0; i
< rows
; i
++) {
1694 for (j
= 0; j
< cols
; j
++)
1695 ldata
[j
] = ATTR_DEFAULT
| 'E';
1699 seen_disp_event
= TRUE
;
1700 scrtop
.x
= scrtop
.y
= 0;
1701 scrbot
.x
= 0; scrbot
.y
= rows
;
1702 check_selection (scrtop
, scrbot
);
1705 case '3': nlattr
= LATTR_TOP
; goto lattr_common
;
1706 case '4': nlattr
= LATTR_BOT
; goto lattr_common
;
1707 case '5': nlattr
= LATTR_NORM
; goto lattr_common
;
1708 case '6': nlattr
= LATTR_WIDE
;
1711 ldata
= lineptr(curs
.y
);
1712 ldata
[cols
] &= ~LATTR_MODE
;
1713 ldata
[cols
] |= nlattr
;
1716 termstate
= TOPLEVEL
;
1719 termstate
= TOPLEVEL
;
1720 seen_disp_event
= TRUE
;
1723 move (curs
.x
, curs
.y
- 1, 1);
1726 move (curs
.x
, curs
.y
+ 1, 1);
1729 move (curs
.x
+ 1, curs
.y
, 1);
1732 move (curs
.x
- 1, curs
.y
, 1);
1735 cset_attr
[cset
=0] = ATTR_LINEDRW
;
1738 cset_attr
[cset
=0] = ATTR_ASCII
;
1745 scroll (0, rows
-1, -1, TRUE
);
1746 else if (curs
.y
> 0)
1752 erase_lots(FALSE
, FALSE
, TRUE
);
1756 erase_lots(TRUE
, FALSE
, TRUE
);
1759 /* XXX Print cursor line */
1762 /* XXX Start controller mode */
1765 /* XXX Stop controller mode */
1768 termstate
= VT52_Y1
;
1771 ldisc_send ("\033/Z", 3);
1774 app_keypad_keys
= TRUE
;
1777 app_keypad_keys
= FALSE
;
1780 /* XXX This should switch to VT100 mode not current or default
1781 * VT mode. But this will only have effect in a VT220+
1787 /* XXX Enter auto print mode */
1790 /* XXX Exit auto print mode */
1793 /* XXX Print screen */
1798 termstate
= VT52_Y2
;
1799 move(curs
.x
, c
-' ', 0);
1802 termstate
= TOPLEVEL
;
1803 move(c
-' ', curs
.y
, 0);
1806 if (selstate
!= NO_SELECTION
) {
1807 pos cursplus
= curs
;
1809 check_selection (curs
, cursplus
);
1816 * Compare two lines to determine whether they are sufficiently
1817 * alike to scroll-optimise one to the other. Return the degree of
1820 static int linecmp (unsigned long *a
, unsigned long *b
) {
1823 for (i
=n
=0; i
< cols
; i
++)
1824 n
+= (*a
++ == *b
++);
1829 * Given a context, update the window. Out of paranoia, we don't
1830 * allow WM_PAINT responses to do scrolling optimisations.
1832 static void do_paint (Context ctx
, int may_optimise
) {
1833 int i
, j
, start
, our_curs_y
;
1834 unsigned long attr
, rv
, cursor
;
1840 * Check the visual bell state.
1843 ticks
= GetTickCount();
1844 if (ticks
- vbell_timeout
>= 0)
1849 * screen array, disptop, scrtop,
1851 * cfg.blinkpc, blink_is_real, tblinker,
1852 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus
1856 if (blinker
|| !cfg
.blink_cur
)
1857 cursor
= ATTR_ACTCURS
;
1862 cursor
= ATTR_PASCURS
;
1864 cursor
|= ATTR_RIGHTCURS
;
1868 rv
= (!rvideo
^ !in_vbell ? ATTR_REVERSE
: 0);
1869 our_curs_y
= curs
.y
- disptop
;
1871 for (i
=0; i
<rows
; i
++) {
1872 unsigned long *ldata
;
1874 scrpos
.y
= i
+ disptop
;
1875 ldata
= lineptr(scrpos
.y
);
1876 lattr
= (ldata
[cols
] & LATTR_MODE
);
1877 for (j
=0; j
<=cols
; j
++) {
1878 unsigned long d
= ldata
[j
];
1879 int idx
= i
*(cols
+1)+j
;
1882 wanttext
[idx
] = lattr
| (((d
&~ ATTR_WRAPPED
) ^ rv
1883 ^ (posle(selstart
, scrpos
) &&
1884 poslt(scrpos
, selend
) ?
1885 ATTR_REVERSE
: 0)) |
1886 (i
==our_curs_y
&& j
==curs
.x ? cursor
: 0));
1887 if (blink_is_real
) {
1888 if (has_focus
&& tblinker
&& (wanttext
[idx
]&ATTR_BLINK
) )
1890 wanttext
[idx
] &= ATTR_MASK
;
1891 wanttext
[idx
] += ' ';
1893 wanttext
[idx
] &= ~ATTR_BLINK
;
1899 * We would perform scrolling optimisations in here, if they
1900 * didn't have a nasty tendency to cause the whole sodding
1901 * program to hang for a second at speed-critical moments.
1902 * We'll leave it well alone...
1905 for (i
=0; i
<rows
; i
++) {
1906 int idx
= i
*(cols
+1);
1907 int lattr
= (wanttext
[idx
+cols
] & LATTR_MODE
);
1909 for (j
=0; j
<=cols
; j
++,idx
++) {
1910 unsigned long t
= wanttext
[idx
];
1911 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1912 int keep_going
= (start
!= -1 && needs_update
&&
1913 (t
& ATTR_MASK
) == attr
&&
1914 j
-start
< sizeof(ch
));
1915 if (start
!= -1 && !keep_going
) {
1916 do_text (ctx
, start
, i
, ch
, j
-start
, attr
, lattr
);
1922 attr
= t
& ATTR_MASK
;
1924 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1932 * Flick the switch that says if blinking things should be shown or hidden.
1935 void term_blink(int flg
) {
1936 static long last_blink
= 0;
1937 static long last_tblink
= 0;
1938 long now
, blink_diff
;
1940 now
= GetTickCount();
1941 blink_diff
= now
-last_tblink
;
1943 /* Make sure the text blinks no more than 2Hz */
1944 if (blink_diff
<0 || blink_diff
>450)
1947 tblinker
= !tblinker
;
1956 blink_diff
= now
-last_blink
;
1958 /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */
1959 if (blink_diff
>=0 && blink_diff
<(long)GetCaretBlinkTime())
1967 * Invalidate the whole screen so it will be repainted in full.
1969 void term_invalidate(void) {
1972 for (i
=0; i
<rows
*(cols
+1); i
++)
1973 disptext
[i
] = ATTR_INVALID
;
1977 * Paint the window in response to a WM_PAINT message.
1979 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1980 int i
, j
, left
, top
, right
, bottom
;
1982 left
= l
/ font_width
;
1983 right
= (r
- 1) / font_width
;
1984 top
= t
/ font_height
;
1985 bottom
= (b
- 1) / font_height
;
1986 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1988 if ( (disptext
[i
*(cols
+1)+cols
]&LATTR_MODE
) == LATTR_NORM
)
1989 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1990 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1992 for (j
= left
/2; j
<= right
/2+1 && j
< cols
; j
++)
1993 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1996 /* This should happen soon enough, also for some reason it sometimes
1997 * fails to actually do anything when re-sizing ... painting the wrong
1999 do_paint (ctx, FALSE);
2004 * Attempt to scroll the scrollback. The second parameter gives the
2005 * position we want to scroll to; the first is +1 to denote that
2006 * this position is relative to the beginning of the scrollback, -1
2007 * to denote it is relative to the end, and 0 to denote that it is
2008 * relative to the current position.
2010 void term_scroll (int rel
, int where
) {
2011 int sbtop
= -count234(scrollback
);
2013 disptop
= (rel
< 0 ?
0 :
2014 rel
> 0 ? sbtop
: disptop
) + where
;
2015 if (disptop
< sbtop
)
2023 static void clipme(pos top
, pos bottom
, char *workbuf
) {
2024 char *wbptr
; /* where next char goes within workbuf */
2025 int wblen
= 0; /* workbuf len */
2026 int buflen
; /* amount of memory allocated to workbuf */
2028 if ( workbuf
!= NULL
) { /* user supplied buffer? */
2029 buflen
= -1; /* assume buffer passed in is big enough */
2030 wbptr
= workbuf
; /* start filling here */
2033 buflen
= 0; /* No data is available yet */
2035 while (poslt(top
, bottom
)) {
2037 unsigned long *ldata
= lineptr(top
.y
);
2043 if (!(ldata
[cols
] & ATTR_WRAPPED
)) {
2044 while ((ldata
[nlpos
.x
-1] & CHAR_MASK
) == 0x20 && poslt(top
, nlpos
))
2046 if (poslt(nlpos
, bottom
))
2049 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
2050 int ch
= (ldata
[top
.x
] & CHAR_MASK
);
2051 int set
= (ldata
[top
.x
] & CSET_MASK
);
2053 /* VT Specials -> ISO8859-1 for Cut&Paste */
2054 static const unsigned char poorman2
[] =
2055 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2057 if (set
&& !cfg
.rawcnp
) {
2058 if (set
== ATTR_LINEDRW
&& ch
>= 0x60 && ch
< 0x7F) {
2060 if ((x
= poorman2
[2*(ch
-0x60)+1]) == ' ')
2062 ch
= (x
<<8) + poorman2
[2*(ch
-0x60)];
2067 if (cfg
.rawcnp
|| !!(ch
&0xE0)) {
2068 if ( wblen
== buflen
)
2070 workbuf
= srealloc(workbuf
, buflen
+= 100);
2071 wbptr
= workbuf
+ wblen
;
2074 *wbptr
++ = (unsigned char) ch
;
2082 for (i
=0; i
<sizeof(sel_nl
); i
++)
2084 if ( wblen
== buflen
)
2086 workbuf
= srealloc(workbuf
, buflen
+= 100);
2087 wbptr
= workbuf
+ wblen
;
2090 *wbptr
++ = sel_nl
[i
];
2096 write_clip (workbuf
, wblen
, FALSE
); /* transfer to clipboard */
2097 if ( buflen
> 0 ) /* indicates we allocated this buffer */
2101 void term_copyall (void) {
2103 top
.y
= -count234(scrollback
);
2105 clipme(top
, curs
, NULL
/* dynamic allocation */);
2109 * Spread the selection outwards according to the selection mode.
2111 static pos
sel_spread_half (pos p
, int dir
) {
2112 unsigned long *ldata
;
2115 ldata
= lineptr(p
.y
);
2120 * In this mode, every character is a separate unit, except
2121 * for runs of spaces at the end of a non-wrapping line.
2123 if (!(ldata
[cols
] & ATTR_WRAPPED
)) {
2124 unsigned long *q
= ldata
+cols
;
2125 while (q
> ldata
&& (q
[-1] & CHAR_MASK
) == 0x20)
2127 if (q
== ldata
+cols
)
2130 p
.x
= (dir
== -1 ? q
-ldata
: cols
- 1);
2135 * In this mode, the units are maximal runs of characters
2136 * whose `wordness' has the same value.
2138 wvalue
= wordness
[ldata
[p
.x
] & CHAR_MASK
];
2140 while (p
.x
< cols
&& wordness
[ldata
[p
.x
+1] & CHAR_MASK
] == wvalue
)
2143 while (p
.x
> 0 && wordness
[ldata
[p
.x
-1] & CHAR_MASK
] == wvalue
)
2149 * In this mode, every line is a unit.
2151 p
.x
= (dir
== -1 ?
0 : cols
- 1);
2157 static void sel_spread (void) {
2158 selstart
= sel_spread_half (selstart
, -1);
2160 selend
= sel_spread_half (selend
, +1);
2164 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
2166 unsigned long *ldata
;
2169 if (y
>=rows
) y
= rows
-1;
2177 if (x
>=cols
) x
= cols
-1;
2179 selpoint
.y
= y
+ disptop
;
2181 ldata
= lineptr(selpoint
.y
);
2182 if ((ldata
[cols
]&LATTR_MODE
) != LATTR_NORM
)
2185 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
2187 selstate
= ABOUT_TO
;
2188 selanchor
= selpoint
;
2190 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
2192 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
2193 selstate
= DRAGGING
;
2194 selstart
= selanchor
= selpoint
;
2198 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
2199 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
2200 if (selstate
== ABOUT_TO
&& poseq(selanchor
, selpoint
))
2202 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
2203 if (posdiff(selpoint
,selstart
) < posdiff(selend
,selstart
)/2) {
2207 selanchor
= selstart
;
2209 selstate
= DRAGGING
;
2211 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
2212 selanchor
= selpoint
;
2213 selstate
= DRAGGING
;
2214 if (poslt(selpoint
, selanchor
)) {
2215 selstart
= selpoint
;
2219 selstart
= selanchor
;
2224 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
2225 if (selstate
== DRAGGING
) {
2227 * We've completed a selection. We now transfer the
2228 * data to the clipboard.
2230 clipme(selstart
, selend
, selspace
);
2231 selstate
= SELECTED
;
2233 selstate
= NO_SELECTION
;
2234 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
2238 get_clip((void **) &data
, &len
);
2242 if (paste_buffer
) sfree(paste_buffer
);
2243 paste_pos
= paste_hold
= paste_len
= 0;
2244 paste_buffer
= smalloc(len
);
2247 while (p
< data
+len
) {
2248 while (p
< data
+len
&&
2249 !(p
<= data
+len
-sizeof(sel_nl
) &&
2250 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
2258 c
=xlat_kbd2tty(q
[i
]);
2259 paste_buffer
[paste_len
++] = c
;
2263 if (p
<= data
+len
-sizeof(sel_nl
) &&
2264 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
2265 paste_buffer
[paste_len
++] = '\r';
2266 p
+= sizeof(sel_nl
);
2271 /* Assume a small paste will be OK in one go. */
2272 if (paste_len
<256) {
2273 ldisc_send (paste_buffer
, paste_len
);
2274 if (paste_buffer
) sfree(paste_buffer
);
2276 paste_pos
= paste_hold
= paste_len
= 0;
2279 get_clip(NULL
, NULL
);
2285 void term_nopaste() {
2286 if(paste_len
== 0) return;
2287 sfree(paste_buffer
);
2293 static long last_paste
= 0;
2294 long now
, paste_diff
;
2296 if(paste_len
== 0) return;
2298 /* Don't wait forever to paste */
2300 now
= GetTickCount();
2301 paste_diff
= now
-last_paste
;
2302 if (paste_diff
>=0 && paste_diff
<450)
2307 while(paste_pos
<paste_len
)
2310 while (n
+ paste_pos
< paste_len
) {
2311 if (paste_buffer
[paste_pos
+ n
++] == '\r')
2314 ldisc_send (paste_buffer
+paste_pos
, n
);
2317 if (paste_pos
< paste_len
) {
2322 sfree(paste_buffer
);
2327 static void deselect (void) {
2328 selstate
= NO_SELECTION
;
2329 selstart
.x
= selstart
.y
= selend
.x
= selend
.y
= 0;
2332 void term_deselect (void) {
2337 int term_ldisc(int option
) {
2338 if (option
== LD_ECHO
) return term_echoing
;
2339 if (option
== LD_EDIT
) return term_editing
;
2344 * from_backend(), to get data from the backend for the terminal.
2346 void from_backend(int is_stderr
, char *data
, int len
) {
2348 if (inbuf_head
>= INBUF_SIZE
)
2350 inbuf
[inbuf_head
++] = *data
++;
2355 * Log session traffic.
2357 void logtraffic(unsigned char c
, int logmode
) {
2358 if (cfg
.logtype
> 0) {
2359 if (cfg
.logtype
== logmode
) {
2360 /* deferred open file from pgm start? */
2361 if (!lgfp
) logfopen();
2362 if (lgfp
) fputc (c
, lgfp
);
2367 /* open log file append/overwrite mode */
2368 void logfopen(void) {
2376 sprintf (writemod
, "wb"); /* default to rewrite */
2377 lgfp
= fopen(cfg
.logfilename
, "r"); /* file already present? */
2381 i
= askappend(cfg
.logfilename
);
2383 writemod
[0] = 'a'; /* set append mode */
2384 else if (i
== 0) { /* cancelled */
2386 cfg
.logtype
= 0; /* disable logging */
2391 lgfp
= fopen(cfg
.logfilename
, writemod
);
2392 if (lgfp
) { /* enter into event log */
2393 sprintf(buf
, "%s session log (%s mode) to file : ",
2394 (writemod
[0] == 'a') ?
"Appending" : "Writing new",
2395 (cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
2396 cfg
.logtype
== LGTYP_DEBUG ?
"raw" : "<ukwn>") );
2397 /* Make sure we do not exceed the output buffer size */
2398 strncat (buf
, cfg
.logfilename
, 128);
2399 buf
[strlen(buf
)] = '\0';
2402 /* --- write header line iinto log file */
2403 fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp
);
2406 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", tm
);
2408 fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp
);
2412 void logfclose (void) {
2413 if (lgfp
) { fclose(lgfp
); lgfp
= NULL
; }