12 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
13 #define CL_VT100 0x0002 /* VT100 */
14 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
15 #define CL_VT102 0x0008 /* VT102 */
16 #define CL_VT220 0x0010 /* VT220 */
17 #define CL_VT320 0x0020 /* VT320 */
18 #define CL_VT420 0x0040 /* VT420 */
19 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
20 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
21 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
22 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
23 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
25 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
26 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
27 #define TM_VT102 (TM_VT100AVO|CL_VT102)
28 #define TM_VT220 (TM_VT102|CL_VT220)
29 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
30 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
32 #define TM_PUTTY (0xFFFF)
34 #define compatibility(x) \
35 if ( ((CL_##x)&compatibility_level) == 0 ) { \
39 #define compatibility2(x,y) \
40 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
45 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
47 static int compatibility_level
= TM_PUTTY
;
49 static tree234
*scrollback
; /* lines scrolled off top of screen */
50 static tree234
*screen
; /* lines on primary screen */
51 static tree234
*alt_screen
; /* lines on alternate screen */
52 static int disptop
; /* distance scrolled back (0 or -ve) */
54 static unsigned long *cpos
; /* cursor position (convenience) */
56 static unsigned long *disptext
; /* buffer of text on real screen */
57 static unsigned long *wanttext
; /* buffer of text we want on screen */
59 #define VBELL_TIMEOUT 100 /* millisecond len of visual bell */
62 struct beeptime
*next
;
65 static struct beeptime
*beephead
, *beeptail
;
70 static unsigned char *selspace
; /* buffer for building selections in */
72 #define TSIZE (sizeof(unsigned long))
73 #define fix_cpos do { cpos = lineptr(curs.y) + curs.x; } while(0)
75 static unsigned long curr_attr
, save_attr
;
76 static unsigned long erase_char
= ERASE_CHAR
;
81 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
82 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
83 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
84 #define posdiff(p1,p2) ( ((p2).y - (p1).y) * (cols+1) + (p2).x - (p1).x )
85 #define incpos(p) ( (p).x == cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
86 #define decpos(p) ( (p).x == 0 ? ((p).x = cols, (p).y--, 1) : ((p).x--, 0) )
88 static pos curs
; /* cursor */
89 static pos savecurs
; /* saved cursor position */
90 static int marg_t
, marg_b
; /* scroll margins */
91 static int dec_om
; /* DEC origin mode flag */
92 static int wrap
, wrapnext
; /* wrap flags */
93 static int insert
; /* insert-mode flag */
94 static int cset
; /* 0 or 1: which char set */
95 static int save_cset
, save_csattr
; /* saved with cursor position */
96 static int rvideo
; /* global reverse video flag */
97 static int rvbell_timeout
; /* for ESC[?5hESC[?5l vbell */
98 static int cursor_on
; /* cursor enabled flag */
99 static int reset_132
; /* Flag ESC c resets to 80 cols */
100 static int use_bce
; /* Use Background coloured erase */
101 static int blinker
; /* When blinking is the cursor on ? */
102 static int tblinker
; /* When the blinking text is on */
103 static int blink_is_real
; /* Actually blink blinking text */
104 static int term_echoing
; /* Does terminal want local echo? */
105 static int term_editing
; /* Does terminal want local edit? */
107 static unsigned long cset_attr
[2];
110 * Saved settings on the alternate screen.
112 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
113 static int alt_t
, alt_b
;
114 static int alt_which
;
116 #define ARGS_MAX 32 /* max # of esc sequence arguments */
117 #define ARG_DEFAULT 0 /* if an arg isn't specified */
118 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
119 static int esc_args
[ARGS_MAX
];
120 static int esc_nargs
;
121 static int esc_query
;
122 #define ANSI(x,y) ((x)+((y)<<8))
123 #define ANSI_QUE(x) ANSI(x,TRUE)
125 #define OSC_STR_MAX 2048
126 static int osc_strlen
;
127 static char osc_string
[OSC_STR_MAX
+1];
130 static char id_string
[1024] = "\033[?6c";
132 static unsigned char *tabs
;
146 OSC_STRING
, OSC_MAYBE_ST
,
154 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
157 SM_CHAR
, SM_WORD
, SM_LINE
159 static pos selstart
, selend
, selanchor
;
161 static short wordness
[256] = {
162 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 */
163 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 */
164 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 */
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,1, /* 67 */
166 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 */
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, /* AB */
168 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 */
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, /* EF */
172 static unsigned char sel_nl
[] = SEL_NL
;
173 static char * paste_buffer
= 0;
174 static int paste_len
, paste_pos
, paste_hold
;
177 * Internal prototypes.
179 static void do_paint (Context
, int);
180 static void erase_lots (int, int, int);
181 static void swap_screen (int);
182 static void update_sbar (void);
183 static void deselect (void);
184 /* log session to file stuff ... */
185 static FILE *lgfp
= NULL
;
186 static void logtraffic(unsigned char c
, int logmode
);
189 * Retrieve a line of the screen or of the scrollback, according to
190 * whether the y coordinate is non-negative or negative
193 unsigned long *lineptr(int y
, int lineno
) {
194 unsigned long *line
, lineattrs
;
196 int i
, treeindex
, oldlen
;
202 whichtree
= scrollback
;
203 treeindex
= y
+ count234(scrollback
);
205 line
= index234(whichtree
, treeindex
);
207 /* We assume that we don't screw up and retrieve something out of range. */
208 assert(line
!= NULL
);
210 if (line
[0] != cols
) {
212 * This line is the wrong length, which probably means it
213 * hasn't been accessed since a resize. Resize it now.
216 lineattrs
= line
[oldlen
+1];
217 delpos234(whichtree
, treeindex
);
218 line
= srealloc(line
, TSIZE
* (2+cols
));
220 for (i
= oldlen
; i
< cols
; i
++)
221 line
[i
+1] = ERASE_CHAR
;
222 line
[cols
+1] = lineattrs
& LATTR_MODE
;
223 addpos234(whichtree
, line
, treeindex
);
228 #define lineptr(x) lineptr(x,__LINE__)
230 * Set up power-on settings for the terminal.
232 static void power_on(void) {
233 curs
.x
= curs
.y
= alt_x
= alt_y
= savecurs
.x
= savecurs
.y
= 0;
236 alt_b
= marg_b
= rows
- 1;
241 for (i
= 0; i
< cols
; i
++)
242 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
244 alt_om
= dec_om
= cfg
.dec_om
;
245 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
246 alt_wrap
= wrap
= cfg
.wrap_mode
;
248 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
252 save_attr
= curr_attr
= ATTR_DEFAULT
;
253 term_editing
= term_echoing
= FALSE
;
254 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
255 app_cursor_keys
= cfg
.app_cursor
;
256 app_keypad_keys
= cfg
.app_keypad
;
258 blink_is_real
= cfg
.blinktext
;
259 erase_char
= ERASE_CHAR
;
263 for (i
= 0; i
< 256; i
++)
264 wordness
[i
] = cfg
.wordness
[i
];
268 erase_lots (FALSE
, TRUE
, TRUE
);
270 erase_lots (FALSE
, TRUE
, TRUE
);
275 * Force a screen update.
277 void term_update(void) {
281 if ( (seen_key_event
&& (cfg
.scroll_on_key
)) ||
282 (seen_disp_event
&& (cfg
.scroll_on_disp
)) ) {
283 disptop
= 0; /* return to main screen */
284 seen_disp_event
= seen_key_event
= 0;
287 do_paint (ctx
, TRUE
);
288 sys_cursor(curs
.x
, curs
.y
- disptop
);
294 * Same as power_on(), but an external function.
296 void term_pwron(void) {
305 * Clear the scrollback.
307 void term_clrsb(void) {
310 while ((line
= delpos234(scrollback
, 0)) != NULL
) {
317 * Initialise the terminal.
319 void term_init(void) {
320 screen
= alt_screen
= scrollback
= NULL
;
322 disptext
= wanttext
= NULL
;
328 beephead
= beeptail
= NULL
;
331 beep_overloaded
= FALSE
;
335 * Set up the terminal for a given size.
337 void term_size(int newrows
, int newcols
, int newsavelines
) {
338 tree234
*newsb
, *newscreen
, *newalt
;
339 unsigned long *newdisp
, *newwant
, *oldline
, *line
;
342 int save_alt_which
= alt_which
;
344 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
345 return; /* nothing to do */
351 alt_b
= marg_b
= newrows
- 1;
354 scrollback
= newtree234(NULL
);
355 screen
= newtree234(NULL
);
360 * Resize the screen and scrollback. We only need to shift
361 * lines around within our data structures, because lineptr()
362 * will take care of resizing each individual line if
365 * - If the new screen and the old screen differ in length, we
366 * must shunt some lines in from the scrollback or out to
369 * - If doing that fails to provide us with enough material to
370 * fill the new screen (i.e. the number of rows needed in
371 * the new screen exceeds the total number in the previous
372 * screen+scrollback), we must invent some blank lines to
375 * - Then, if the new scrollback length is less than the
376 * amount of scrollback we actually have, we must throw some
379 sblen
= count234(scrollback
);
380 if (newrows
> rows
) {
381 for (i
= rows
; i
< newrows
; i
++) {
383 line
= delpos234(scrollback
, --sblen
);
385 line
= smalloc(TSIZE
* (newcols
+2));
387 for (j
= 0; j
<= newcols
; j
++)
388 line
[j
+1] = ERASE_CHAR
;
390 addpos234(screen
, line
, 0);
392 } else if (newrows
< rows
) {
393 for (i
= newrows
; i
< rows
; i
++) {
394 line
= delpos234(screen
, 0);
395 addpos234(scrollback
, line
, sblen
++);
398 assert(count234(screen
) == newrows
);
399 while (sblen
> newsavelines
) {
400 line
= delpos234(scrollback
, 0);
404 assert(count234(scrollback
) <= newsavelines
);
407 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
408 for (i
=0; i
<newrows
*(newcols
+1); i
++)
409 newdisp
[i
] = ATTR_INVALID
;
413 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
414 for (i
=0; i
<newrows
*(newcols
+1); i
++)
415 newwant
[i
] = ATTR_INVALID
;
419 newalt
= newtree234(NULL
);
420 for (i
=0; i
<newrows
; i
++) {
421 line
= smalloc(TSIZE
* (newcols
+2));
423 for (j
= 0; j
<= newcols
; j
++)
424 line
[j
+1] = erase_char
;
425 addpos234(newalt
, line
, i
);
428 while (NULL
!= (line
= delpos234(alt_screen
, 0)))
430 freetree234(alt_screen
);
435 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
437 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
440 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
441 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
445 curs
.y
+= newrows
- rows
;
448 if (curs
.y
>= newrows
)
450 if (curs
.x
>= newcols
)
453 wrapnext
= alt_wnext
= FALSE
;
457 savelines
= newsavelines
;
460 swap_screen(save_alt_which
);
469 static void swap_screen (int which
) {
473 if (which
== alt_which
)
478 ttr
= alt_screen
; alt_screen
= screen
; screen
= ttr
;
479 t
= curs
.x
; curs
.x
= alt_x
; alt_x
= t
;
480 t
= curs
.y
; curs
.y
= alt_y
; alt_y
= t
;
481 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
482 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
483 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
484 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
485 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
486 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
487 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
493 * Update the scroll bar.
495 static void update_sbar(void) {
498 nscroll
= count234(scrollback
);
500 set_sbar (nscroll
+ rows
, nscroll
+ disptop
, rows
);
504 * Check whether the region bounded by the two pointers intersects
505 * the scroll region, and de-select the on-screen selection if so.
507 static void check_selection (pos from
, pos to
) {
508 if (poslt(from
, selend
) && poslt(selstart
, to
))
513 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
514 * for backward.) `sb' is TRUE if the scrolling is permitted to
515 * affect the scrollback buffer.
517 * NB this function invalidates all pointers into lines of the
518 * screen data structures. In particular, you MUST call fix_cpos
519 * after calling scroll() and before doing anything else that
520 * uses the cpos shortcut pointer.
522 static void scroll (int topline
, int botline
, int lines
, int sb
) {
523 unsigned long *line
, *line2
;
526 if (topline
!= 0 || alt_which
!= 0)
531 line
= delpos234(screen
, botline
);
532 for (i
= 0; i
< cols
; i
++)
533 line
[i
+1] = erase_char
;
535 addpos234(screen
, line
, topline
);
537 if (selstart
.y
>= topline
&& selstart
.y
<= botline
) {
539 if (selstart
.y
> botline
) {
540 selstart
.y
= botline
;
544 if (selend
.y
>= topline
&& selend
.y
<= botline
) {
546 if (selend
.y
> botline
) {
556 line
= delpos234(screen
, topline
);
557 if (sb
&& savelines
> 0) {
558 int sblen
= count234(scrollback
);
560 * We must add this line to the scrollback. We'll
561 * remove a line from the top of the scrollback to
562 * replace it, or allocate a new one if the
563 * scrollback isn't full.
565 if (sblen
== savelines
) {
566 sblen
--, line2
= delpos234(scrollback
, 0);
568 line2
= smalloc(TSIZE
* (cols
+2));
571 addpos234(scrollback
, line
, sblen
);
574 for (i
= 0; i
< cols
; i
++)
575 line
[i
+1] = erase_char
;
577 addpos234(screen
, line
, botline
);
579 if (selstart
.y
>= topline
&& selstart
.y
<= botline
) {
581 if (selstart
.y
< topline
) {
582 selstart
.y
= topline
;
586 if (selend
.y
>= topline
&& selend
.y
<= botline
) {
588 if (selend
.y
< topline
) {
600 * Move the cursor to a given position, clipping at boundaries. We
601 * may or may not want to clip at the scroll margin: marg_clip is 0
602 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
603 * even _being_ outside the margins.
605 static void move (int x
, int y
, int marg_clip
) {
611 if ((curs
.y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
613 if ((curs
.y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
627 * Save or restore the cursor and SGR mode.
629 static void save_cursor(int save
) {
632 save_attr
= curr_attr
;
634 save_csattr
= cset_attr
[cset
];
637 /* Make sure the window hasn't shrunk since the save */
638 if (curs
.x
>= cols
) curs
.x
= cols
-1;
639 if (curs
.y
>= rows
) curs
.y
= rows
-1;
641 curr_attr
= save_attr
;
643 cset_attr
[cset
] = save_csattr
;
645 if (use_bce
) erase_char
= (' ' |(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
650 * Erase a large portion of the screen: the whole screen, or the
651 * whole line, or parts thereof.
653 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
656 unsigned long *ldata
;
677 check_selection (start
, end
);
679 /* Clear screen also forces a full window redraw, just in case. */
680 if (start
.y
== 0 && start
.x
== 0 && end
.y
== rows
)
683 ldata
= lineptr(start
.y
);
684 while (poslt(start
, end
)) {
685 if (start
.y
== cols
&& !erase_lattr
)
686 ldata
[start
.x
] &= ~ATTR_WRAPPED
;
688 ldata
[start
.x
] = erase_char
;
689 if (incpos(start
) && start
.y
< rows
)
690 ldata
= lineptr(start
.y
);
695 * Insert or delete characters within the current line. n is +ve if
696 * insertion is desired, and -ve for deletion.
698 static void insch (int n
) {
699 int dir
= (n
< 0 ?
-1 : +1);
702 unsigned long *ldata
;
704 n
= (n
< 0 ?
-n
: n
);
705 if (n
> cols
- curs
.x
)
707 m
= cols
- curs
.x
- n
;
709 cursplus
.x
= curs
.x
+ n
;
710 check_selection (curs
, cursplus
);
711 ldata
= lineptr(curs
.y
);
713 memmove (ldata
+ curs
.x
, ldata
+ curs
.x
+ n
, m
*TSIZE
);
715 ldata
[curs
.x
+ m
++] = erase_char
;
717 memmove (ldata
+ curs
.x
+ n
, ldata
+ curs
.x
, m
*TSIZE
);
719 ldata
[curs
.x
+ n
] = erase_char
;
724 * Toggle terminal mode `mode' to state `state'. (`query' indicates
725 * whether the mode is a DEC private one or a normal one.)
727 static void toggle_mode (int mode
, int query
, int state
) {
729 if (query
) switch (mode
) {
730 case 1: /* application cursor keys */
731 app_cursor_keys
= state
;
733 case 2: /* VT52 mode */
736 case 3: /* 80/132 columns */
738 request_resize (state ?
132 : 80, rows
, 1);
741 case 5: /* reverse video */
743 * Toggle reverse video. If we receive an OFF within the
744 * visual bell timeout period after an ON, we trigger an
745 * effective visual bell, so that ESC[?5hESC[?5l will
746 * always be an actually _visible_ visual bell.
748 ticks
= GetTickCount();
749 if (rvideo
&& !state
&& /* we're turning it off */
750 ticks
< rvbell_timeout
) { /* and it's not long since it was turned on */
751 in_vbell
= TRUE
; /* we may clear rvideo but we set in_vbell */
752 if (vbell_timeout
< rvbell_timeout
) /* don't move vbell end forward */
753 vbell_timeout
= rvbell_timeout
; /* vbell end is at least then */
754 } else if (!rvideo
&& state
) {
755 /* This is an ON, so we notice the time and save it. */
756 rvbell_timeout
= ticks
+ VBELL_TIMEOUT
;
759 seen_disp_event
= TRUE
;
760 if (state
) term_update();
762 case 6: /* DEC origin mode */
765 case 7: /* auto wrap */
768 case 8: /* auto key repeat */
771 case 10: /* set local edit mode */
772 term_editing
= state
;
773 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
775 case 25: /* enable/disable cursor */
776 compatibility2(OTHER
,VT220
);
778 seen_disp_event
= TRUE
;
780 case 47: /* alternate screen */
781 compatibility(OTHER
);
786 } else switch (mode
) {
787 case 4: /* set insert mode */
788 compatibility(VT102
);
791 case 12: /* set echo mode */
792 term_echoing
= !state
;
793 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
795 case 20: /* Return sends ... */
796 cr_lf_return
= state
;
802 * Process an OSC sequence: set window title or icon name.
804 static void do_osc(void) {
807 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
809 osc_string
[osc_strlen
] = '\0';
810 switch (esc_args
[0]) {
813 set_icon (osc_string
);
814 if (esc_args
[0] == 1)
816 /* fall through: parameter 0 means set both */
819 set_title (osc_string
);
826 * Remove everything currently in `inbuf' and stick it up on the
827 * in-memory display. There's a big state machine in here to
828 * process escape sequences...
830 void term_out(void) {
833 for(inbuf_reap
= 0; inbuf_reap
< inbuf_head
; inbuf_reap
++)
835 c
= inbuf
[inbuf_reap
];
838 * Optionally log the session traffic to a file. Useful for
839 * debugging and possibly also useful for actual logging.
841 logtraffic((unsigned char)c
, LGTYP_DEBUG
);
843 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
844 * be able to display 8-bit characters, but I'll let that go 'cause
847 if( ( (c
&0x60) == 0 || c
== '\177') &&
848 termstate
< DO_CTRLS
&&
849 ( (c
&0x80) == 0 || has_compat(VT220
))) {
851 case '\005': /* terminal type query */
852 /* Strictly speaking this is VT100 but a VT100 defaults to
853 * no response. Other terminals respond at their option.
855 * Don't put a CR in the default string as this tends to
856 * upset some weird software.
858 * An xterm returns "xterm" (5 characters)
860 compatibility(ANSIMIN
);
862 char abuf
[256], *s
, *d
;
864 for(s
=cfg
.answerback
, d
=abuf
; *s
; s
++) {
867 if (*s
>= 'a' && *s
<= 'z')
868 *d
++ = (*s
- ('a'-1));
869 else if ((*s
>='@' && *s
<='_') ||
870 *s
== '?' || (*s
&0x80))
876 else if (*s
== '^') {
880 *d
++ = xlat_kbd2tty((unsigned char)*s
);
882 ldisc_send (abuf
, d
-abuf
);
887 struct beeptime
*newbeep
;
890 ticks
= GetTickCount();
892 if (!beep_overloaded
) {
893 newbeep
= smalloc(sizeof(struct beeptime
));
894 newbeep
->ticks
= ticks
;
895 newbeep
->next
= NULL
;
899 beeptail
->next
= newbeep
;
905 * Throw out any beeps that happened more than
909 beephead
->ticks
< ticks
- cfg
.bellovl_t
) {
910 struct beeptime
*tmp
= beephead
;
911 beephead
= tmp
->next
;
918 if (cfg
.bellovl
&& beep_overloaded
&&
919 ticks
-lastbeep
>= cfg
.bellovl_s
) {
921 * If we're currently overloaded and the
922 * last beep was more than s seconds ago,
923 * leave overload mode.
925 beep_overloaded
= FALSE
;
926 } else if (cfg
.bellovl
&& !beep_overloaded
&&
927 nbeeps
>= cfg
.bellovl_n
) {
929 * Now, if we have n or more beeps
930 * remaining in the queue, go into overload
933 beep_overloaded
= TRUE
;
938 * Perform an actual beep if we're not overloaded.
940 if ((!cfg
.bellovl
|| !beep_overloaded
) && cfg
.beep
!= 0) {
943 else if(cfg
.beep
== 2) {
945 vbell_timeout
= ticks
+ VBELL_TIMEOUT
;
953 if (curs
.x
== 0 && curs
.y
== 0)
955 else if (curs
.x
== 0 && curs
.y
> 0)
956 curs
.x
= cols
-1, curs
.y
--;
962 seen_disp_event
= TRUE
;
965 compatibility(VT100
);
969 compatibility(VT100
);
974 termstate
= VT52_ESC
;
976 compatibility(ANSIMIN
);
977 termstate
= SEEN_ESC
;
981 compatibility(VT220
);
982 termstate
= SEEN_CSI
;
984 esc_args
[0] = ARG_DEFAULT
;
988 compatibility(VT220
);
989 termstate
= SEEN_OSC
;
996 seen_disp_event
= TRUE
;
998 logtraffic((unsigned char)c
,LGTYP_ASCII
);
1002 compatibility(VT100
);
1004 if (curs
.y
== marg_b
)
1005 scroll (marg_t
, marg_b
, 1, TRUE
);
1006 else if (curs
.y
< rows
-1)
1012 seen_disp_event
= 1;
1014 logtraffic((unsigned char)c
,LGTYP_ASCII
);
1018 pos old_curs
= curs
;
1019 unsigned long *ldata
= lineptr(curs
.y
);
1023 } while (curs
.x
< cols
-1 && !tabs
[curs
.x
]);
1025 if ((ldata
[cols
] & LATTR_MODE
) != LATTR_NORM
)
1027 if (curs
.x
>= cols
/2)
1037 check_selection (old_curs
, curs
);
1039 seen_disp_event
= TRUE
;
1041 case '\177': /* Destructive backspace
1042 This does nothing on a real VT100 */
1043 compatibility(OTHER
);
1044 if (curs
.x
&& !wrapnext
) curs
.x
--;
1047 *cpos
= (' ' | curr_attr
| ATTR_ASCII
);
1051 else switch (termstate
) {
1053 /* Only graphic characters get this far, ctrls are stripped above */
1054 if (wrapnext
&& wrap
) {
1055 cpos
[1] |= ATTR_WRAPPED
;
1056 if (curs
.y
== marg_b
)
1057 scroll (marg_t
, marg_b
, 1, TRUE
);
1058 else if (curs
.y
< rows
-1)
1066 if (selstate
!= NO_SELECTION
) {
1067 pos cursplus
= curs
;
1069 check_selection (curs
, cursplus
);
1071 switch (cset_attr
[cset
]) {
1073 * Linedraw characters are different from 'ESC ( B'
1074 * only for a small range. For ones outside that
1075 * range, make sure we use the same font as well as
1076 * the same encoding.
1079 if (c
<0x5f || c
>0x7F)
1080 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
1083 *cpos
++ = ' ' | curr_attr
| ATTR_ASCII
;
1085 *cpos
++ = ((unsigned char)c
) | curr_attr
| ATTR_LINEDRW
;
1088 /* If UK-ASCII, make the '#' a LineDraw Pound */
1090 *cpos
++ = '}' | curr_attr
| ATTR_LINEDRW
;
1095 *cpos
= xlat_tty2scr((unsigned char)c
) | curr_attr
|
1096 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
1097 logtraffic((unsigned char)c
, LGTYP_ASCII
);
1102 if (curs
.x
== cols
) {
1107 seen_disp_event
= 1;
1111 termstate
= TOPLEVEL
;
1115 * This state is virtually identical to SEEN_ESC, with the
1116 * exception that we have an OSC sequence in the pipeline,
1117 * and _if_ we see a backslash, we process it.
1121 termstate
= TOPLEVEL
;
1124 /* else fall through */
1126 termstate
= TOPLEVEL
;
1128 case ' ': /* some weird sequence? */
1129 compatibility(VT220
);
1130 termstate
= IGNORE_NEXT
;
1132 case '[': /* enter CSI mode */
1133 termstate
= SEEN_CSI
;
1135 esc_args
[0] = ARG_DEFAULT
;
1138 case ']': /* xterm escape sequences */
1139 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1140 compatibility(OTHER
);
1141 termstate
= SEEN_OSC
;
1144 case '(': /* should set GL */
1145 compatibility(VT100
);
1148 case ')': /* should set GR */
1149 compatibility(VT100
);
1152 case '7': /* save cursor */
1153 compatibility(VT100
);
1156 case '8': /* restore cursor */
1157 compatibility(VT100
);
1158 save_cursor (FALSE
);
1159 seen_disp_event
= TRUE
;
1162 compatibility(VT100
);
1163 app_keypad_keys
= TRUE
;
1166 compatibility(VT100
);
1167 app_keypad_keys
= FALSE
;
1169 case 'D': /* exactly equivalent to LF */
1170 compatibility(VT100
);
1171 if (curs
.y
== marg_b
)
1172 scroll (marg_t
, marg_b
, 1, TRUE
);
1173 else if (curs
.y
< rows
-1)
1177 seen_disp_event
= TRUE
;
1179 case 'E': /* exactly equivalent to CR-LF */
1180 compatibility(VT100
);
1182 if (curs
.y
== marg_b
)
1183 scroll (marg_t
, marg_b
, 1, TRUE
);
1184 else if (curs
.y
< rows
-1)
1188 seen_disp_event
= TRUE
;
1190 case 'M': /* reverse index - backwards LF */
1191 compatibility(VT100
);
1192 if (curs
.y
== marg_t
)
1193 scroll (marg_t
, marg_b
, -1, TRUE
);
1194 else if (curs
.y
> 0)
1198 seen_disp_event
= TRUE
;
1200 case 'Z': /* terminal type query */
1201 compatibility(VT100
);
1202 ldisc_send (id_string
, strlen(id_string
));
1204 case 'c': /* restore power-on settings */
1205 compatibility(VT100
);
1208 request_resize (80, rows
, 1);
1213 seen_disp_event
= TRUE
;
1215 case '#': /* ESC # 8 fills screen with Es :-) */
1216 compatibility(VT100
);
1217 termstate
= SEEN_ESCHASH
;
1219 case 'H': /* set a tab */
1220 compatibility(VT100
);
1221 tabs
[curs
.x
] = TRUE
;
1226 termstate
= TOPLEVEL
; /* default */
1229 if (esc_nargs
<= ARGS_MAX
) {
1230 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
1231 esc_args
[esc_nargs
-1] = 0;
1232 esc_args
[esc_nargs
-1] =
1233 10 * esc_args
[esc_nargs
-1] + c
- '0';
1235 termstate
= SEEN_CSI
;
1239 if (++esc_nargs
<= ARGS_MAX
)
1240 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
1241 termstate
= SEEN_CSI
;
1245 if( esc_query
) esc_query
= -1;
1246 else if( c
== '?' ) esc_query
= TRUE
;
1248 termstate
= SEEN_CSI
;
1250 else switch (ANSI(c
,esc_query
)) {
1251 case 'A': /* move up N lines */
1252 move (curs
.x
, curs
.y
- def(esc_args
[0], 1), 1);
1253 seen_disp_event
= TRUE
;
1255 case 'e': /* move down N lines */
1256 compatibility(ANSI
);
1258 move (curs
.x
, curs
.y
+ def(esc_args
[0], 1), 1);
1259 seen_disp_event
= TRUE
;
1261 case 'a': /* move right N cols */
1262 compatibility(ANSI
);
1264 move (curs
.x
+ def(esc_args
[0], 1), curs
.y
, 1);
1265 seen_disp_event
= TRUE
;
1267 case 'D': /* move left N cols */
1268 move (curs
.x
- def(esc_args
[0], 1), curs
.y
, 1);
1269 seen_disp_event
= TRUE
;
1271 case 'E': /* move down N lines and CR */
1272 compatibility(ANSI
);
1273 move (0, curs
.y
+ def(esc_args
[0], 1), 1);
1274 seen_disp_event
= TRUE
;
1276 case 'F': /* move up N lines and CR */
1277 compatibility(ANSI
);
1278 move (0, curs
.y
- def(esc_args
[0], 1), 1);
1279 seen_disp_event
= TRUE
;
1281 case 'G': case '`': /* set horizontal posn */
1282 compatibility(ANSI
);
1283 move (def(esc_args
[0], 1) - 1, curs
.y
, 0);
1284 seen_disp_event
= TRUE
;
1286 case 'd': /* set vertical posn */
1287 compatibility(ANSI
);
1288 move (curs
.x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1290 seen_disp_event
= TRUE
;
1292 case 'H': case 'f': /* set horz and vert posns at once */
1294 esc_args
[1] = ARG_DEFAULT
;
1295 move (def(esc_args
[1], 1) - 1,
1296 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1298 seen_disp_event
= TRUE
;
1300 case 'J': /* erase screen or parts of it */
1302 unsigned int i
= def(esc_args
[0], 0) + 1;
1305 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
1308 seen_disp_event
= TRUE
;
1310 case 'K': /* erase line or parts of it */
1312 unsigned int i
= def(esc_args
[0], 0) + 1;
1315 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
1317 seen_disp_event
= TRUE
;
1319 case 'L': /* insert lines */
1320 compatibility(VT102
);
1321 if (curs
.y
<= marg_b
)
1322 scroll (curs
.y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
1324 seen_disp_event
= TRUE
;
1326 case 'M': /* delete lines */
1327 compatibility(VT102
);
1328 if (curs
.y
<= marg_b
)
1329 scroll (curs
.y
, marg_b
, def(esc_args
[0], 1), TRUE
);
1331 seen_disp_event
= TRUE
;
1333 case '@': /* insert chars */
1334 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1335 compatibility(VT102
);
1336 insch (def(esc_args
[0], 1));
1337 seen_disp_event
= TRUE
;
1339 case 'P': /* delete chars */
1340 compatibility(VT102
);
1341 insch (-def(esc_args
[0], 1));
1342 seen_disp_event
= TRUE
;
1344 case 'c': /* terminal type query */
1345 compatibility(VT100
);
1346 /* This is the response for a VT102 */
1347 ldisc_send (id_string
, strlen(id_string
));
1349 case 'n': /* cursor position query */
1350 if (esc_args
[0] == 6) {
1352 sprintf (buf
, "\033[%d;%dR", curs
.y
+ 1, curs
.x
+ 1);
1353 ldisc_send (buf
, strlen(buf
));
1355 else if (esc_args
[0] == 5) {
1356 ldisc_send ("\033[0n", 4);
1359 case 'h': /* toggle modes to high */
1361 compatibility(VT100
);
1364 for (i
=0; i
<esc_nargs
; i
++)
1365 toggle_mode (esc_args
[i
], esc_query
, TRUE
);
1368 case 'l': /* toggle modes to low */
1370 compatibility(VT100
);
1373 for (i
=0; i
<esc_nargs
; i
++)
1374 toggle_mode (esc_args
[i
], esc_query
, FALSE
);
1377 case 'g': /* clear tabs */
1378 compatibility(VT100
);
1379 if (esc_nargs
== 1) {
1380 if (esc_args
[0] == 0) {
1381 tabs
[curs
.x
] = FALSE
;
1382 } else if (esc_args
[0] == 3) {
1384 for (i
= 0; i
< cols
; i
++)
1389 case 'r': /* set scroll margins */
1390 compatibility(VT100
);
1391 if (esc_nargs
<= 2) {
1393 top
= def(esc_args
[0], 1) - 1;
1394 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
1395 def(esc_args
[1], rows
)) - 1;
1398 /* VTTEST Bug 9 - if region is less than 2 lines
1399 * don't change region.
1406 * I used to think the cursor should be
1407 * placed at the top of the newly marginned
1408 * area. Apparently not: VMS TPU falls over
1411 * Well actually it should for Origin mode - RDB
1413 curs
.y
= (dec_om ? marg_t
: 0);
1415 seen_disp_event
= TRUE
;
1419 case 'm': /* set graphics rendition */
1422 * A VT100 without the AVO only had one attribute, either
1423 * underline or reverse video depending on the cursor type,
1424 * this was selected by CSI 7m.
1427 * This is DIM on the VT100-AVO and VT102
1429 * This is BLINK on the VT100-AVO and VT102+
1431 * This is INVIS on the VT100-AVO and VT102
1433 * This like 22 disables BOLD, DIM and INVIS
1435 * The ANSI colours appear on any terminal that has colour
1436 * (obviously) but the interaction between sgr0 and the
1437 * colours varies but is usually related to the background
1438 * colour erase item.
1439 * The interaction between colour attributes and the mono
1440 * ones is also very implementation dependent.
1442 * The 39 and 49 attributes are likely to be unimplemented.
1445 for (i
=0; i
<esc_nargs
; i
++) {
1446 switch (def(esc_args
[i
], 0)) {
1447 case 0: /* restore defaults */
1448 curr_attr
= ATTR_DEFAULT
; break;
1449 case 1: /* enable bold */
1450 compatibility(VT100AVO
);
1451 curr_attr
|= ATTR_BOLD
; break;
1452 case 21: /* (enable double underline) */
1453 compatibility(OTHER
);
1454 case 4: /* enable underline */
1455 compatibility(VT100AVO
);
1456 curr_attr
|= ATTR_UNDER
; break;
1457 case 5: /* enable blink */
1458 compatibility(VT100AVO
);
1459 curr_attr
|= ATTR_BLINK
; break;
1460 case 7: /* enable reverse video */
1461 curr_attr
|= ATTR_REVERSE
; break;
1462 case 22: /* disable bold */
1463 compatibility2(OTHER
,VT220
);
1464 curr_attr
&= ~ATTR_BOLD
; break;
1465 case 24: /* disable underline */
1466 compatibility2(OTHER
,VT220
);
1467 curr_attr
&= ~ATTR_UNDER
; break;
1468 case 25: /* disable blink */
1469 compatibility2(OTHER
,VT220
);
1470 curr_attr
&= ~ATTR_BLINK
; break;
1471 case 27: /* disable reverse video */
1472 compatibility2(OTHER
,VT220
);
1473 curr_attr
&= ~ATTR_REVERSE
; break;
1474 case 30: case 31: case 32: case 33:
1475 case 34: case 35: case 36: case 37:
1477 curr_attr
&= ~ATTR_FGMASK
;
1478 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
1480 case 39: /* default-foreground */
1481 curr_attr
&= ~ATTR_FGMASK
;
1482 curr_attr
|= ATTR_DEFFG
;
1484 case 40: case 41: case 42: case 43:
1485 case 44: case 45: case 46: case 47:
1487 curr_attr
&= ~ATTR_BGMASK
;
1488 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
1490 case 49: /* default-background */
1491 curr_attr
&= ~ATTR_BGMASK
;
1492 curr_attr
|= ATTR_DEFBG
;
1499 (curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
|ATTR_BLINK
))
1503 case 's': /* save cursor */
1506 case 'u': /* restore cursor */
1507 save_cursor (FALSE
);
1508 seen_disp_event
= TRUE
;
1510 case 't': /* set page size - ie window height */
1512 * VT340/VT420 sequence DECSLPP, DEC only allows values
1513 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1514 * illegal values (eg first arg 1..9) for window changing
1517 compatibility(VT340TEXT
);
1518 if (esc_nargs
<=1 && (esc_args
[0]<1 || esc_args
[0]>=24)) {
1519 request_resize (cols
, def(esc_args
[0], 24), 0);
1523 case ANSI('|', '*'):
1524 /* VT420 sequence DECSNLS
1525 * Set number of lines on screen
1526 * VT420 uses VGA like hardware and can support any size in
1527 * reasonable range (24..49 AIUI) with no default specified.
1529 compatibility(VT420
);
1530 if (esc_nargs
==1 && esc_args
[0]>0) {
1531 request_resize (cols
, def(esc_args
[0], cfg
.height
), 0);
1535 case ANSI('|', '$'):
1536 /* VT340/VT420 sequence DECSCPP
1537 * Set number of columns per page
1538 * Docs imply range is only 80 or 132, but I'll allow any.
1540 compatibility(VT340TEXT
);
1542 request_resize (def(esc_args
[0], cfg
.width
), rows
, 0);
1546 case 'X': /* write N spaces w/o moving cursor */
1547 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1548 compatibility(ANSIMIN
);
1550 int n
= def(esc_args
[0], 1);
1552 unsigned long *p
= cpos
;
1553 if (n
> cols
- curs
.x
)
1557 check_selection (curs
, cursplus
);
1560 seen_disp_event
= TRUE
;
1563 case 'x': /* report terminal characteristics */
1564 compatibility(VT100
);
1567 int i
= def(esc_args
[0], 0);
1568 if (i
== 0 || i
== 1) {
1569 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1571 ldisc_send (buf
, 20);
1576 compatibility(OTHER
);
1577 use_bce
= (esc_args
[0]<=0);
1578 erase_char
= ERASE_CHAR
;
1580 erase_char
= (' '|(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
1583 compatibility(OTHER
);
1584 blink_is_real
= (esc_args
[0]>=1);
1587 /* Allow the host to make this emulator a 'perfect' VT102.
1588 * This first appeared in the VT220, but we do need to get
1589 * back to PuTTY mode so I won't check it.
1591 * The arg in 40..42 are a PuTTY extension.
1592 * The 2nd arg, 8bit vs 7bit is not checked.
1594 * Setting VT102 mode should also change the Fkeys to
1595 * generate PF* codes as a real VT102 has no Fkeys.
1596 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1599 * Note ESC c will NOT change this!
1602 switch (esc_args
[0]) {
1603 case 61: compatibility_level
&= ~TM_VTXXX
;
1604 compatibility_level
|= TM_VT102
; break;
1605 case 62: compatibility_level
&= ~TM_VTXXX
;
1606 compatibility_level
|= TM_VT220
; break;
1608 default: if( esc_args
[0] > 60 && esc_args
[0] < 70 )
1609 compatibility_level
|= TM_VTXXX
;
1612 case 40: compatibility_level
&= TM_VTXXX
; break;
1613 case 41: compatibility_level
= TM_PUTTY
; break;
1614 case 42: compatibility_level
= TM_SCOANSI
; break;
1617 compatibility_level
= TM_PUTTY
; break;
1621 /* Change the response to CSI c */
1622 if (esc_args
[0] == 50) {
1625 strcpy(id_string
, "\033[?");
1626 for (i
=1; i
<esc_nargs
; i
++) {
1627 if (i
!=1) strcat(id_string
, ";");
1628 sprintf(lbuf
, "%d", esc_args
[i
]);
1629 strcat(id_string
, lbuf
);
1631 strcat(id_string
, "c");
1635 /* Is this a good idea ?
1636 * Well we should do a soft reset at this point ...
1638 if (!has_compat(VT420
) && has_compat(VT100
)) {
1639 if (reset_132
) request_resize (132, 24, 1);
1640 else request_resize ( 80, 24, 1);
1648 /* VT100 only here, checked above */
1651 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1654 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1657 default: /* specifically, 'B' */
1658 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1661 if( !has_compat(VT220
) || c
!= '%' )
1662 termstate
= TOPLEVEL
;
1667 case 'P': /* Linux palette sequence */
1668 termstate
= SEEN_OSC_P
;
1671 case 'R': /* Linux palette reset */
1674 termstate
= TOPLEVEL
;
1676 case 'W': /* word-set */
1677 termstate
= SEEN_OSC_W
;
1680 case '0': case '1': case '2': case '3': case '4':
1681 case '5': case '6': case '7': case '8': case '9':
1682 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1686 * Grotty hack to support xterm and DECterm title
1687 * sequences concurrently.
1689 if (esc_args
[0] == 2) {
1693 /* else fall through */
1695 termstate
= OSC_STRING
;
1701 * This OSC stuff is EVIL. It takes just one character to get into
1702 * sysline mode and it's not initially obvious how to get out.
1703 * So I've added CR and LF as string aborts.
1704 * This shouldn't effect compatibility as I believe embedded
1705 * control characters are supposed to be interpreted (maybe?)
1706 * and they don't display anything useful anyway.
1710 if (c
== '\n' || c
== '\r') {
1711 termstate
= TOPLEVEL
;
1712 } else if (c
== 0234 || c
== '\007' ) {
1714 * These characters terminate the string; ST and BEL
1715 * terminate the sequence and trigger instant
1716 * processing of it, whereas ESC goes back to SEEN_ESC
1717 * mode unless it is followed by \, in which case it is
1718 * synonymous with ST in the first place.
1721 termstate
= TOPLEVEL
;
1722 } else if (c
== '\033')
1723 termstate
= OSC_MAYBE_ST
;
1724 else if (osc_strlen
< OSC_STR_MAX
)
1725 osc_string
[osc_strlen
++] = c
;
1729 int max
= (osc_strlen
== 0 ?
21 : 16);
1731 if (c
>= '0' && c
<= '9')
1733 else if (c
>= 'A' && c
<= 'A'+max
-10)
1735 else if (c
>= 'a' && c
<= 'a'+max
-10)
1738 termstate
= TOPLEVEL
;
1739 osc_string
[osc_strlen
++] = val
;
1740 if (osc_strlen
>= 7) {
1741 palette_set (osc_string
[0],
1742 osc_string
[1] * 16 + osc_string
[2],
1743 osc_string
[3] * 16 + osc_string
[4],
1744 osc_string
[5] * 16 + osc_string
[6]);
1746 termstate
= TOPLEVEL
;
1752 case '0': case '1': case '2': case '3': case '4':
1753 case '5': case '6': case '7': case '8': case '9':
1754 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1757 termstate
= OSC_STRING
;
1763 unsigned long nlattr
;
1764 unsigned long *ldata
;
1770 for (i
= 0; i
< rows
; i
++) {
1772 for (j
= 0; j
< cols
; j
++)
1773 ldata
[j
] = ATTR_DEFAULT
| 'E';
1777 seen_disp_event
= TRUE
;
1778 scrtop
.x
= scrtop
.y
= 0;
1779 scrbot
.x
= 0; scrbot
.y
= rows
;
1780 check_selection (scrtop
, scrbot
);
1783 case '3': nlattr
= LATTR_TOP
; goto lattr_common
;
1784 case '4': nlattr
= LATTR_BOT
; goto lattr_common
;
1785 case '5': nlattr
= LATTR_NORM
; goto lattr_common
;
1786 case '6': nlattr
= LATTR_WIDE
;
1789 ldata
= lineptr(curs
.y
);
1790 ldata
[cols
] &= ~LATTR_MODE
;
1791 ldata
[cols
] |= nlattr
;
1794 termstate
= TOPLEVEL
;
1797 termstate
= TOPLEVEL
;
1798 seen_disp_event
= TRUE
;
1801 move (curs
.x
, curs
.y
- 1, 1);
1804 move (curs
.x
, curs
.y
+ 1, 1);
1807 move (curs
.x
+ 1, curs
.y
, 1);
1810 move (curs
.x
- 1, curs
.y
, 1);
1813 cset_attr
[cset
=0] = ATTR_LINEDRW
;
1816 cset_attr
[cset
=0] = ATTR_ASCII
;
1823 scroll (0, rows
-1, -1, TRUE
);
1824 else if (curs
.y
> 0)
1830 erase_lots(FALSE
, FALSE
, TRUE
);
1834 erase_lots(TRUE
, FALSE
, TRUE
);
1837 /* XXX Print cursor line */
1840 /* XXX Start controller mode */
1843 /* XXX Stop controller mode */
1846 termstate
= VT52_Y1
;
1849 ldisc_send ("\033/Z", 3);
1852 app_keypad_keys
= TRUE
;
1855 app_keypad_keys
= FALSE
;
1858 /* XXX This should switch to VT100 mode not current or default
1859 * VT mode. But this will only have effect in a VT220+
1865 /* XXX Enter auto print mode */
1868 /* XXX Exit auto print mode */
1871 /* XXX Print screen */
1876 termstate
= VT52_Y2
;
1877 move(curs
.x
, c
-' ', 0);
1880 termstate
= TOPLEVEL
;
1881 move(c
-' ', curs
.y
, 0);
1884 if (selstate
!= NO_SELECTION
) {
1885 pos cursplus
= curs
;
1887 check_selection (curs
, cursplus
);
1894 * Compare two lines to determine whether they are sufficiently
1895 * alike to scroll-optimise one to the other. Return the degree of
1898 static int linecmp (unsigned long *a
, unsigned long *b
) {
1901 for (i
=n
=0; i
< cols
; i
++)
1902 n
+= (*a
++ == *b
++);
1907 * Given a context, update the window. Out of paranoia, we don't
1908 * allow WM_PAINT responses to do scrolling optimisations.
1910 static void do_paint (Context ctx
, int may_optimise
) {
1911 int i
, j
, start
, our_curs_y
;
1912 unsigned long attr
, rv
, cursor
;
1918 * Check the visual bell state.
1921 ticks
= GetTickCount();
1922 if (ticks
- vbell_timeout
>= 0)
1927 * screen array, disptop, scrtop,
1929 * cfg.blinkpc, blink_is_real, tblinker,
1930 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus
1934 if (blinker
|| !cfg
.blink_cur
)
1935 cursor
= ATTR_ACTCURS
;
1940 cursor
= ATTR_PASCURS
;
1942 cursor
|= ATTR_RIGHTCURS
;
1946 rv
= (!rvideo
^ !in_vbell ? ATTR_REVERSE
: 0);
1947 our_curs_y
= curs
.y
- disptop
;
1949 for (i
=0; i
<rows
; i
++) {
1950 unsigned long *ldata
;
1952 scrpos
.y
= i
+ disptop
;
1953 ldata
= lineptr(scrpos
.y
);
1954 lattr
= (ldata
[cols
] & LATTR_MODE
);
1955 for (j
=0; j
<=cols
; j
++) {
1956 unsigned long d
= ldata
[j
];
1957 int idx
= i
*(cols
+1)+j
;
1960 wanttext
[idx
] = lattr
| (((d
&~ ATTR_WRAPPED
) ^ rv
1961 ^ (posle(selstart
, scrpos
) &&
1962 poslt(scrpos
, selend
) ?
1963 ATTR_REVERSE
: 0)) |
1964 (i
==our_curs_y
&& j
==curs
.x ? cursor
: 0));
1965 if (blink_is_real
) {
1966 if (has_focus
&& tblinker
&& (wanttext
[idx
]&ATTR_BLINK
) )
1968 wanttext
[idx
] &= ATTR_MASK
;
1969 wanttext
[idx
] += ' ';
1971 wanttext
[idx
] &= ~ATTR_BLINK
;
1977 * We would perform scrolling optimisations in here, if they
1978 * didn't have a nasty tendency to cause the whole sodding
1979 * program to hang for a second at speed-critical moments.
1980 * We'll leave it well alone...
1983 for (i
=0; i
<rows
; i
++) {
1984 int idx
= i
*(cols
+1);
1985 int lattr
= (wanttext
[idx
+cols
] & LATTR_MODE
);
1987 for (j
=0; j
<=cols
; j
++,idx
++) {
1988 unsigned long t
= wanttext
[idx
];
1989 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1990 int keep_going
= (start
!= -1 && needs_update
&&
1991 (t
& ATTR_MASK
) == attr
&&
1992 j
-start
< sizeof(ch
));
1993 if (start
!= -1 && !keep_going
) {
1994 do_text (ctx
, start
, i
, ch
, j
-start
, attr
, lattr
);
2000 attr
= t
& ATTR_MASK
;
2002 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
2010 * Flick the switch that says if blinking things should be shown or hidden.
2013 void term_blink(int flg
) {
2014 static long last_blink
= 0;
2015 static long last_tblink
= 0;
2016 long now
, blink_diff
;
2018 now
= GetTickCount();
2019 blink_diff
= now
-last_tblink
;
2021 /* Make sure the text blinks no more than 2Hz */
2022 if (blink_diff
<0 || blink_diff
>450)
2025 tblinker
= !tblinker
;
2034 blink_diff
= now
-last_blink
;
2036 /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */
2037 if (blink_diff
>=0 && blink_diff
<(long)GetCaretBlinkTime())
2045 * Invalidate the whole screen so it will be repainted in full.
2047 void term_invalidate(void) {
2050 for (i
=0; i
<rows
*(cols
+1); i
++)
2051 disptext
[i
] = ATTR_INVALID
;
2055 * Paint the window in response to a WM_PAINT message.
2057 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
2058 int i
, j
, left
, top
, right
, bottom
;
2060 left
= l
/ font_width
;
2061 right
= (r
- 1) / font_width
;
2062 top
= t
/ font_height
;
2063 bottom
= (b
- 1) / font_height
;
2064 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
2066 if ( (disptext
[i
*(cols
+1)+cols
]&LATTR_MODE
) == LATTR_NORM
)
2067 for (j
= left
; j
<= right
&& j
< cols
; j
++)
2068 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
2070 for (j
= left
/2; j
<= right
/2+1 && j
< cols
; j
++)
2071 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
2074 /* This should happen soon enough, also for some reason it sometimes
2075 * fails to actually do anything when re-sizing ... painting the wrong
2077 do_paint (ctx, FALSE);
2082 * Attempt to scroll the scrollback. The second parameter gives the
2083 * position we want to scroll to; the first is +1 to denote that
2084 * this position is relative to the beginning of the scrollback, -1
2085 * to denote it is relative to the end, and 0 to denote that it is
2086 * relative to the current position.
2088 void term_scroll (int rel
, int where
) {
2089 int sbtop
= -count234(scrollback
);
2091 disptop
= (rel
< 0 ?
0 :
2092 rel
> 0 ? sbtop
: disptop
) + where
;
2093 if (disptop
< sbtop
)
2101 static void clipme(pos top
, pos bottom
, char *workbuf
) {
2102 char *wbptr
; /* where next char goes within workbuf */
2103 int wblen
= 0; /* workbuf len */
2104 int buflen
; /* amount of memory allocated to workbuf */
2106 if ( workbuf
!= NULL
) { /* user supplied buffer? */
2107 buflen
= -1; /* assume buffer passed in is big enough */
2108 wbptr
= workbuf
; /* start filling here */
2111 buflen
= 0; /* No data is available yet */
2113 while (poslt(top
, bottom
)) {
2115 unsigned long *ldata
= lineptr(top
.y
);
2121 if (!(ldata
[cols
] & ATTR_WRAPPED
)) {
2122 while ((ldata
[nlpos
.x
-1] & CHAR_MASK
) == 0x20 && poslt(top
, nlpos
))
2124 if (poslt(nlpos
, bottom
))
2127 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
2128 int ch
= (ldata
[top
.x
] & CHAR_MASK
);
2129 int set
= (ldata
[top
.x
] & CSET_MASK
);
2131 /* VT Specials -> ISO8859-1 for Cut&Paste */
2132 static const unsigned char poorman2
[] =
2133 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2135 if (set
&& !cfg
.rawcnp
) {
2136 if (set
== ATTR_LINEDRW
&& ch
>= 0x60 && ch
< 0x7F) {
2138 if ((x
= poorman2
[2*(ch
-0x60)+1]) == ' ')
2140 ch
= (x
<<8) + poorman2
[2*(ch
-0x60)];
2145 if (cfg
.rawcnp
|| !!(ch
&0xE0)) {
2146 if ( wblen
== buflen
)
2148 workbuf
= srealloc(workbuf
, buflen
+= 100);
2149 wbptr
= workbuf
+ wblen
;
2152 *wbptr
++ = (unsigned char) ch
;
2160 for (i
=0; i
<sizeof(sel_nl
); i
++)
2162 if ( wblen
== buflen
)
2164 workbuf
= srealloc(workbuf
, buflen
+= 100);
2165 wbptr
= workbuf
+ wblen
;
2168 *wbptr
++ = sel_nl
[i
];
2174 write_clip (workbuf
, wblen
, FALSE
); /* transfer to clipboard */
2175 if ( buflen
> 0 ) /* indicates we allocated this buffer */
2179 void term_copyall (void) {
2181 top
.y
= -count234(scrollback
);
2183 clipme(top
, curs
, NULL
/* dynamic allocation */);
2187 * Spread the selection outwards according to the selection mode.
2189 static pos
sel_spread_half (pos p
, int dir
) {
2190 unsigned long *ldata
;
2193 ldata
= lineptr(p
.y
);
2198 * In this mode, every character is a separate unit, except
2199 * for runs of spaces at the end of a non-wrapping line.
2201 if (!(ldata
[cols
] & ATTR_WRAPPED
)) {
2202 unsigned long *q
= ldata
+cols
;
2203 while (q
> ldata
&& (q
[-1] & CHAR_MASK
) == 0x20)
2205 if (q
== ldata
+cols
)
2208 p
.x
= (dir
== -1 ? q
-ldata
: cols
- 1);
2213 * In this mode, the units are maximal runs of characters
2214 * whose `wordness' has the same value.
2216 wvalue
= wordness
[ldata
[p
.x
] & CHAR_MASK
];
2218 while (p
.x
< cols
&& wordness
[ldata
[p
.x
+1] & CHAR_MASK
] == wvalue
)
2221 while (p
.x
> 0 && wordness
[ldata
[p
.x
-1] & CHAR_MASK
] == wvalue
)
2227 * In this mode, every line is a unit.
2229 p
.x
= (dir
== -1 ?
0 : cols
- 1);
2235 static void sel_spread (void) {
2236 selstart
= sel_spread_half (selstart
, -1);
2238 selend
= sel_spread_half (selend
, +1);
2242 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
2244 unsigned long *ldata
;
2247 if (y
>=rows
) y
= rows
-1;
2255 if (x
>=cols
) x
= cols
-1;
2257 selpoint
.y
= y
+ disptop
;
2259 ldata
= lineptr(selpoint
.y
);
2260 if ((ldata
[cols
]&LATTR_MODE
) != LATTR_NORM
)
2263 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
2265 selstate
= ABOUT_TO
;
2266 selanchor
= selpoint
;
2268 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
2270 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
2271 selstate
= DRAGGING
;
2272 selstart
= selanchor
= selpoint
;
2276 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
2277 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
2278 if (selstate
== ABOUT_TO
&& poseq(selanchor
, selpoint
))
2280 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
2281 if (posdiff(selpoint
,selstart
) < posdiff(selend
,selstart
)/2) {
2285 selanchor
= selstart
;
2287 selstate
= DRAGGING
;
2289 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
2290 selanchor
= selpoint
;
2291 selstate
= DRAGGING
;
2292 if (poslt(selpoint
, selanchor
)) {
2293 selstart
= selpoint
;
2297 selstart
= selanchor
;
2302 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
2303 if (selstate
== DRAGGING
) {
2305 * We've completed a selection. We now transfer the
2306 * data to the clipboard.
2308 clipme(selstart
, selend
, selspace
);
2309 selstate
= SELECTED
;
2311 selstate
= NO_SELECTION
;
2312 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
2316 get_clip((void **) &data
, &len
);
2320 if (paste_buffer
) sfree(paste_buffer
);
2321 paste_pos
= paste_hold
= paste_len
= 0;
2322 paste_buffer
= smalloc(len
);
2325 while (p
< data
+len
) {
2326 while (p
< data
+len
&&
2327 !(p
<= data
+len
-sizeof(sel_nl
) &&
2328 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
2336 c
=xlat_kbd2tty(q
[i
]);
2337 paste_buffer
[paste_len
++] = c
;
2341 if (p
<= data
+len
-sizeof(sel_nl
) &&
2342 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
2343 paste_buffer
[paste_len
++] = '\r';
2344 p
+= sizeof(sel_nl
);
2349 /* Assume a small paste will be OK in one go. */
2350 if (paste_len
<256) {
2351 ldisc_send (paste_buffer
, paste_len
);
2352 if (paste_buffer
) sfree(paste_buffer
);
2354 paste_pos
= paste_hold
= paste_len
= 0;
2357 get_clip(NULL
, NULL
);
2363 void term_nopaste() {
2364 if(paste_len
== 0) return;
2365 sfree(paste_buffer
);
2371 static long last_paste
= 0;
2372 long now
, paste_diff
;
2374 if(paste_len
== 0) return;
2376 /* Don't wait forever to paste */
2378 now
= GetTickCount();
2379 paste_diff
= now
-last_paste
;
2380 if (paste_diff
>=0 && paste_diff
<450)
2385 while(paste_pos
<paste_len
)
2388 while (n
+ paste_pos
< paste_len
) {
2389 if (paste_buffer
[paste_pos
+ n
++] == '\r')
2392 ldisc_send (paste_buffer
+paste_pos
, n
);
2395 if (paste_pos
< paste_len
) {
2400 sfree(paste_buffer
);
2405 static void deselect (void) {
2406 selstate
= NO_SELECTION
;
2407 selstart
.x
= selstart
.y
= selend
.x
= selend
.y
= 0;
2410 void term_deselect (void) {
2415 int term_ldisc(int option
) {
2416 if (option
== LD_ECHO
) return term_echoing
;
2417 if (option
== LD_EDIT
) return term_editing
;
2422 * from_backend(), to get data from the backend for the terminal.
2424 void from_backend(int is_stderr
, char *data
, int len
) {
2426 if (inbuf_head
>= INBUF_SIZE
)
2428 inbuf
[inbuf_head
++] = *data
++;
2433 * Log session traffic.
2435 void logtraffic(unsigned char c
, int logmode
) {
2436 if (cfg
.logtype
> 0) {
2437 if (cfg
.logtype
== logmode
) {
2438 /* deferred open file from pgm start? */
2439 if (!lgfp
) logfopen();
2440 if (lgfp
) fputc (c
, lgfp
);
2445 /* open log file append/overwrite mode */
2446 void logfopen(void) {
2454 sprintf (writemod
, "wb"); /* default to rewrite */
2455 lgfp
= fopen(cfg
.logfilename
, "r"); /* file already present? */
2459 i
= askappend(cfg
.logfilename
);
2461 writemod
[0] = 'a'; /* set append mode */
2462 else if (i
== 0) { /* cancelled */
2464 cfg
.logtype
= 0; /* disable logging */
2469 lgfp
= fopen(cfg
.logfilename
, writemod
);
2470 if (lgfp
) { /* enter into event log */
2471 sprintf(buf
, "%s session log (%s mode) to file : ",
2472 (writemod
[0] == 'a') ?
"Appending" : "Writing new",
2473 (cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
2474 cfg
.logtype
== LGTYP_DEBUG ?
"raw" : "<ukwn>") );
2475 /* Make sure we do not exceed the output buffer size */
2476 strncat (buf
, cfg
.logfilename
, 128);
2477 buf
[strlen(buf
)] = '\0';
2480 /* --- write header line iinto log file */
2481 fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp
);
2484 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", tm
);
2486 fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp
);
2490 void logfclose (void) {
2491 if (lgfp
) { fclose(lgfp
); lgfp
= NULL
; }