New improved bell handling. Choice between visual and audible bell;
[u/mdw/putty] / terminal.c
CommitLineData
374330e2 1#include <windows.h>
2
3#include <stdio.h>
4#include <stdlib.h>
49bad831 5#include <ctype.h>
374330e2 6
e1c8e0ed 7#include <time.h>
374330e2 8#include "putty.h"
9
c9def1b8 10#define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
e14a5a13 11#define CL_VT100 0x0002 /* VT100 */
12#define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
13#define CL_VT102 0x0008 /* VT102 */
14#define CL_VT220 0x0010 /* VT220 */
15#define CL_VT320 0x0020 /* VT320 */
16#define CL_VT420 0x0040 /* VT420 */
17#define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
18#define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
c9def1b8 19#define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
20#define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
21#define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
e14a5a13 22
c9def1b8 23#define TM_VT100 (CL_ANSIMIN|CL_VT100)
24#define TM_VT100AVO (TM_VT100|CL_VT100AVO)
25#define TM_VT102 (TM_VT100AVO|CL_VT102)
26#define TM_VT220 (TM_VT102|CL_VT220)
27#define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
28#define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
29
30#define TM_PUTTY (0xFFFF)
e14a5a13 31
32#define compatibility(x) \
33 if ( ((CL_##x)&compatibility_level) == 0 ) { \
34 termstate=TOPLEVEL; \
35 break; \
36 }
c9def1b8 37#define compatibility2(x,y) \
38 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
39 termstate=TOPLEVEL; \
40 break; \
41 }
e14a5a13 42
43#define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
44
45static int compatibility_level = TM_PUTTY;
46
47
374330e2 48static unsigned long *text; /* buffer of text on terminal screen */
49static unsigned long *scrtop; /* top of working screen */
50static unsigned long *disptop; /* top of displayed screen */
51static unsigned long *sbtop; /* top of scrollback */
c9def1b8 52static unsigned long *sbbot; /* furthest extent of scrollback */
374330e2 53static unsigned long *cpos; /* cursor position (convenience) */
54static unsigned long *disptext; /* buffer of text on real screen */
55static unsigned long *wanttext; /* buffer of text we want on screen */
56static unsigned long *alttext; /* buffer of text on alt. screen */
57
156686ef 58#define VBELL_TIMEOUT 100 /* millisecond duration of visual bell */
59
60struct beeptime {
61 struct beeptime *next;
62 long ticks;
63};
64static struct beeptime *beephead, *beeptail;
65int nbeeps;
66int beep_overloaded;
67long lastbeep;
68
374330e2 69static unsigned char *selspace; /* buffer for building selections in */
70
71#define TSIZE (sizeof(*text))
72#define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
73
74static unsigned long curr_attr, save_attr;
e14a5a13 75static unsigned long erase_char = ERASE_CHAR;
374330e2 76
77static int curs_x, curs_y; /* cursor */
78static int save_x, save_y; /* saved cursor position */
79static int marg_t, marg_b; /* scroll margins */
80static int dec_om; /* DEC origin mode flag */
81static int wrap, wrapnext; /* wrap flags */
82static int insert; /* insert-mode flag */
83static int cset; /* 0 or 1: which char set */
84static int save_cset, save_csattr; /* saved with cursor position */
85static int rvideo; /* global reverse video flag */
e14a5a13 86static int cursor_on; /* cursor enabled flag */
87static int reset_132; /* Flag ESC c resets to 80 cols */
88static int use_bce; /* Use Background coloured erase */
89static int blinker; /* When blinking is the cursor on ? */
c9def1b8 90static int tblinker; /* When the blinking text is on */
91static int blink_is_real; /* Actually blink blinking text */
0965bee0 92static int term_echoing; /* Does terminal want local echo? */
93static int term_editing; /* Does terminal want local edit? */
374330e2 94
95static unsigned long cset_attr[2];
96
97/*
98 * Saved settings on the alternate screen.
99 */
100static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
101static int alt_t, alt_b;
102static int alt_which;
103
104#define ARGS_MAX 32 /* max # of esc sequence arguments */
064916ea 105#define ARG_DEFAULT 0 /* if an arg isn't specified */
374330e2 106#define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
107static int esc_args[ARGS_MAX];
108static int esc_nargs;
109static int esc_query;
8b811b12 110#define ANSI(x,y) ((x)+((y)<<8))
111#define ANSI_QUE(x) ANSI(x,TRUE)
374330e2 112
113#define OSC_STR_MAX 2048
114static int osc_strlen;
115static char osc_string[OSC_STR_MAX+1];
116static int osc_w;
117
c9def1b8 118static char id_string[1024] = "\033[?6c";
119
374330e2 120static unsigned char *tabs;
121
374330e2 122static enum {
cabfd08c 123 TOPLEVEL,
124 SEEN_ESC,
125 SEEN_CSI,
126 SEEN_OSC,
127 SEEN_OSC_W,
128
129 DO_CTRLS,
130
131 IGNORE_NEXT,
132 SET_GL, SET_GR,
133 SEEN_OSC_P,
134 OSC_STRING, OSC_MAYBE_ST,
e14a5a13 135 SEEN_ESCHASH,
136 VT52_ESC,
137 VT52_Y1,
138 VT52_Y2
374330e2 139} termstate;
140
141static enum {
142 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
143} selstate;
144static enum {
145 SM_CHAR, SM_WORD, SM_LINE
146} selmode;
147static unsigned long *selstart, *selend, *selanchor;
148
149static short wordness[256] = {
150 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 */
151 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 */
152 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 */
153 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 */
154 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 */
155 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 */
156 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 */
157 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 */
158};
159
160static unsigned char sel_nl[] = SEL_NL;
c9def1b8 161static char * paste_buffer = 0;
162static int paste_len, paste_pos, paste_hold;
374330e2 163
164/*
165 * Internal prototypes.
166 */
167static void do_paint (Context, int);
168static void erase_lots (int, int, int);
169static void swap_screen (int);
170static void update_sbar (void);
171static void deselect (void);
e1c8e0ed 172/* log session to file stuff ... */
173static FILE *lgfp = NULL;
174static void logtraffic(unsigned char c, int logmode);
374330e2 175
176/*
177 * Set up power-on settings for the terminal.
178 */
179static void power_on(void) {
180 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
181 alt_t = marg_t = 0;
182 if (rows != -1)
183 alt_b = marg_b = rows - 1;
184 else
185 alt_b = marg_b = 0;
186 if (cols != -1) {
187 int i;
188 for (i = 0; i < cols; i++)
189 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
190 }
191 alt_om = dec_om = cfg.dec_om;
192 alt_wnext = wrapnext = alt_ins = insert = FALSE;
193 alt_wrap = wrap = cfg.wrap_mode;
194 alt_cset = cset = 0;
195 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
196 rvideo = 0;
156686ef 197 in_vbell = FALSE;
e14a5a13 198 cursor_on = 1;
374330e2 199 save_attr = curr_attr = ATTR_DEFAULT;
0965bee0 200 term_editing = term_echoing = FALSE;
201 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
374330e2 202 app_cursor_keys = cfg.app_cursor;
203 app_keypad_keys = cfg.app_keypad;
c9def1b8 204 use_bce = cfg.bce;
205 blink_is_real = cfg.blinktext;
e14a5a13 206 erase_char = ERASE_CHAR;
374330e2 207 alt_which = 0;
208 {
209 int i;
210 for (i = 0; i < 256; i++)
211 wordness[i] = cfg.wordness[i];
212 }
213 if (text) {
214 swap_screen (1);
215 erase_lots (FALSE, TRUE, TRUE);
216 swap_screen (0);
217 erase_lots (FALSE, TRUE, TRUE);
218 }
219}
220
221/*
222 * Force a screen update.
223 */
224void term_update(void) {
225 Context ctx;
226 ctx = get_ctx();
227 if (ctx) {
c9def1b8 228 if ( (seen_key_event && (cfg.scroll_on_key)) ||
a094ae43 229 (seen_disp_event && (cfg.scroll_on_disp)) ) {
cabfd08c 230 disptop = scrtop;
231 seen_disp_event = seen_key_event = 0;
8ac3e544 232 update_sbar();
cabfd08c 233 }
374330e2 234 do_paint (ctx, TRUE);
ec8679e9 235 sys_cursor(curs_x, curs_y + (scrtop - disptop) / (cols+1));
374330e2 236 free_ctx (ctx);
374330e2 237 }
238}
239
240/*
241 * Same as power_on(), but an external function.
242 */
243void term_pwron(void) {
244 power_on();
245 fix_cpos;
246 disptop = scrtop;
247 deselect();
248 term_update();
249}
250
251/*
252 * Clear the scrollback.
253 */
254void term_clrsb(void) {
255 disptop = sbtop = scrtop;
256 update_sbar();
257}
258
259/*
260 * Initialise the terminal.
261 */
262void term_init(void) {
c9def1b8 263 text = sbtop = sbbot = scrtop = disptop = cpos = NULL;
374330e2 264 disptext = wanttext = NULL;
265 tabs = NULL;
266 selspace = NULL;
267 deselect();
268 rows = cols = -1;
374330e2 269 power_on();
156686ef 270 beephead = beeptail = NULL;
271 nbeeps = 0;
272 lastbeep = FALSE;
273 beep_overloaded = FALSE;
374330e2 274}
275
276/*
277 * Set up the terminal for a given size.
278 */
279void term_size(int newrows, int newcols, int newsavelines) {
280 unsigned long *newtext, *newdisp, *newwant, *newalt;
281 int i, j, crows, ccols;
282
d6832394 283 int save_alt_which = alt_which;
284
374330e2 285 if (newrows == rows && newcols == cols && newsavelines == savelines)
286 return; /* nothing to do */
287
d6832394 288 deselect();
289 swap_screen(0);
290
374330e2 291 alt_t = marg_t = 0;
292 alt_b = marg_b = newrows - 1;
293
294 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
c9def1b8 295 sbbot = newtext + newsavelines*(newcols+1);
374330e2 296 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
c9def1b8 297 newtext[i] = erase_char;
374330e2 298 if (rows != -1) {
299 crows = rows + (scrtop - sbtop) / (cols+1);
300 if (crows > newrows+newsavelines)
301 crows = newrows+newsavelines;
c9def1b8 302 if (newrows>crows)
303 disptop = newtext;
304 else
305 disptop = newtext + (crows-newrows)*(newcols+1);
374330e2 306 ccols = (cols < newcols ? cols : newcols);
307 for (i=0; i<crows; i++) {
c9def1b8 308 int oldidx = (rows - crows + i) * (cols+1);
309 int newidx = (newrows - crows + i) * (newcols+1);
310
374330e2 311 for (j=0; j<ccols; j++)
c9def1b8 312 disptop[newidx+j] = scrtop[oldidx+j];
313 disptop[newidx+newcols] =
314 (cols == newcols ? scrtop[oldidx+cols]
315 : (scrtop[oldidx+cols]&LATTR_MODE));
374330e2 316 }
c9def1b8 317 sbtop = newtext;
318 } else {
319 sbtop = disptop = newtext;
320 }
374330e2 321 scrtop = disptop;
322 sfree (text);
323 text = newtext;
324
325 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
326 for (i=0; i<newrows*(newcols+1); i++)
327 newdisp[i] = ATTR_INVALID;
328 sfree (disptext);
329 disptext = newdisp;
330
331 newwant = smalloc (newrows*(newcols+1)*TSIZE);
332 for (i=0; i<newrows*(newcols+1); i++)
333 newwant[i] = ATTR_INVALID;
334 sfree (wanttext);
335 wanttext = newwant;
336
337 newalt = smalloc (newrows*(newcols+1)*TSIZE);
338 for (i=0; i<newrows*(newcols+1); i++)
e14a5a13 339 newalt[i] = erase_char;
374330e2 340 sfree (alttext);
341 alttext = newalt;
342
343 sfree (selspace);
344 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
345
346 tabs = srealloc (tabs, newcols*sizeof(*tabs));
347 {
348 int i;
349 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
350 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
351 }
352
353 if (rows > 0)
354 curs_y += newrows - rows;
355 if (curs_y < 0)
356 curs_y = 0;
357 if (curs_y >= newrows)
358 curs_y = newrows-1;
359 if (curs_x >= newcols)
360 curs_x = newcols-1;
361 alt_x = alt_y = 0;
362 wrapnext = alt_wnext = FALSE;
363
364 rows = newrows;
365 cols = newcols;
366 savelines = newsavelines;
367 fix_cpos;
368
d6832394 369 swap_screen(save_alt_which);
370
374330e2 371 update_sbar();
372 term_update();
373}
374
375/*
376 * Swap screens.
377 */
378static void swap_screen (int which) {
379 int t;
380 unsigned long tt;
381
382 if (which == alt_which)
383 return;
384
385 alt_which = which;
386
387 for (t=0; t<rows*(cols+1); t++) {
388 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
389 }
390
391 t = curs_x; curs_x = alt_x; alt_x = t;
392 t = curs_y; curs_y = alt_y; alt_y = t;
393 t = marg_t; marg_t = alt_t; alt_t = t;
394 t = marg_b; marg_b = alt_b; alt_b = t;
395 t = dec_om; dec_om = alt_om; alt_om = t;
396 t = wrap; wrap = alt_wrap; alt_wrap = t;
397 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
398 t = insert; insert = alt_ins; alt_ins = t;
399 t = cset; cset = alt_cset; alt_cset = t;
400
401 fix_cpos;
402}
403
404/*
374330e2 405 * Update the scroll bar.
406 */
407static void update_sbar(void) {
408 int min;
409
410 min = (sbtop - text) / (cols+1);
411 set_sbar ((scrtop - text) / (cols+1) + rows - min,
412 (disptop - text) / (cols+1) - min,
413 rows);
414}
415
416/*
417 * Check whether the region bounded by the two pointers intersects
418 * the scroll region, and de-select the on-screen selection if so.
419 */
420static void check_selection (unsigned long *from, unsigned long *to) {
421 if (from < selend && selstart < to)
422 deselect();
423}
424
425/*
426 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
427 * for backward.) `sb' is TRUE if the scrolling is permitted to
428 * affect the scrollback buffer.
429 */
430static void scroll (int topline, int botline, int lines, int sb) {
431 unsigned long *scroll_top;
c9def1b8 432 unsigned long *newscr;
374330e2 433 int scroll_size, size, i;
c9def1b8 434 int scrtop_is_disptop = (scrtop==disptop);
435static int recursive = 0;
436
437 /* Only scroll more than the window if we're doing a 10% scroll */
438 if (!recursive && lines > botline - topline + 1)
439 lines = botline - topline + 1;
374330e2 440
441 scroll_top = scrtop + topline*(cols+1);
442 size = (lines < 0 ? -lines : lines) * (cols+1);
443 scroll_size = (botline - topline + 1) * (cols+1) - size;
444
cabfd08c 445 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
374330e2 446 /*
cabfd08c 447 * Since we're going to scroll the top line and we're on the
c9def1b8 448 * scrolling screen let's also effect the scrollback buffer.
449 *
450 * This is normally done by moving the position the screen
451 * painter reads from to reduce the amount of memory copying
452 * required.
374330e2 453 */
c9def1b8 454 if (scroll_size >= 0 && !recursive) {
455 newscr = scrtop + lines * (cols+1);
456
457 if (newscr > sbbot && botline == rows-1) {
458 /* We've hit the bottom of memory, so we have to do a
459 * physical scroll. But instead of just 1 line do it
460 * by 10% of the available memory.
461 *
462 * If the scroll region isn't the whole screen then we can't
463 * do this as it stands. We would need to recover the bottom
464 * of the screen from the scroll buffer after being sure that
465 * it doesn't get deleted.
466 */
467
468 i = (rows+savelines)/10;
469
470 /* Make it simple and ensure safe recursion */
471 if ( i<savelines-1) {
472 recursive ++;
473 scroll(topline, botline, i, sb);
474 recursive --;
475
476 newscr = scrtop - i * (cols+1);
477 if (scrtop_is_disptop) disptop = newscr;
478 scrtop = newscr;
479 }
480
481 newscr = scrtop + lines * (cols+1);
482 }
483
484 if (newscr <= sbbot) {
485 if (scrtop_is_disptop) disptop = newscr;
486 scrtop = newscr;
487
488 if (botline == rows-1 )
489 for (i = 0; i < size; i++)
490 scrtop[i+scroll_size] = erase_char;
491
492 update_sbar();
493 fix_cpos;
494
495 if (botline != rows-1) {
496 /* This fastscroll only works for full window scrolls.
497 * If the caller wanted a partial one we have to reverse
498 * scroll the bottom of the screen.
499 */
500 scroll(botline-lines+1, rows-1, -lines, 0);
501 }
502 return ;
503 }
504
505 /* If we can't scroll by memory remapping do it physically.
506 * But rather than expensivly doing the scroll buffer just
507 * scroll the screen. All it means is that sometimes we choose
508 * to not add lines from a scroll region to the scroll buffer.
509 */
510
511 if (savelines <= 400) {
512 sbtop -= lines * (cols+1);
513 if (sbtop < text)
514 sbtop = text;
515 scroll_size += scroll_top - sbtop;
516 scroll_top = sbtop;
517
518 update_sbar();
519 }
520 } else {
521 /* Ho hum, expensive scroll required. */
522
523 sbtop -= lines * (cols+1);
524 if (sbtop < text)
525 sbtop = text;
526 scroll_size += scroll_top - sbtop;
527 scroll_top = sbtop;
528
529 update_sbar();
530 }
374330e2 531 }
532
533 if (scroll_size < 0) {
534 size += scroll_size;
535 scroll_size = 0;
536 }
537
538 if (lines > 0) {
539 if (scroll_size)
540 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
541 for (i = 0; i < size; i++)
e14a5a13 542 scroll_top[i+scroll_size] = erase_char;
374330e2 543 if (selstart > scroll_top &&
544 selstart < scroll_top + size + scroll_size) {
545 selstart -= size;
546 if (selstart < scroll_top)
547 selstart = scroll_top;
548 }
549 if (selend > scroll_top &&
550 selend < scroll_top + size + scroll_size) {
551 selend -= size;
552 if (selend < scroll_top)
553 selend = scroll_top;
554 }
c9def1b8 555 if (scrtop_is_disptop)
556 disptop = scrtop;
557 else
558 if (disptop > scroll_top &&
559 disptop < scroll_top + size + scroll_size) {
560 disptop -= size;
561 if (disptop < scroll_top)
562 disptop = scroll_top;
563 }
374330e2 564 } else {
565 if (scroll_size)
566 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
567 for (i = 0; i < size; i++)
e14a5a13 568 scroll_top[i] = erase_char;
374330e2 569 if (selstart > scroll_top &&
570 selstart < scroll_top + size + scroll_size) {
571 selstart += size;
572 if (selstart > scroll_top + size + scroll_size)
573 selstart = scroll_top + size + scroll_size;
574 }
575 if (selend > scroll_top &&
576 selend < scroll_top + size + scroll_size) {
577 selend += size;
578 if (selend > scroll_top + size + scroll_size)
579 selend = scroll_top + size + scroll_size;
580 }
c9def1b8 581 if (scrtop_is_disptop)
582 disptop = scrtop;
583 else if (disptop > scroll_top &&
584 disptop < scroll_top + size + scroll_size) {
585 disptop += size;
586 if (disptop > scroll_top + size + scroll_size)
587 disptop = scroll_top + size + scroll_size;
588 }
374330e2 589 }
374330e2 590}
591
592/*
593 * Move the cursor to a given position, clipping at boundaries. We
594 * may or may not want to clip at the scroll margin: marg_clip is 0
595 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
596 * even _being_ outside the margins.
597 */
598static void move (int x, int y, int marg_clip) {
599 if (x < 0)
600 x = 0;
601 if (x >= cols)
602 x = cols-1;
603 if (marg_clip) {
604 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
605 y = marg_t;
606 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
607 y = marg_b;
608 }
609 if (y < 0)
610 y = 0;
611 if (y >= rows)
612 y = rows-1;
613 curs_x = x;
614 curs_y = y;
615 fix_cpos;
616 wrapnext = FALSE;
617}
618
619/*
620 * Save or restore the cursor and SGR mode.
621 */
622static void save_cursor(int save) {
623 if (save) {
624 save_x = curs_x;
625 save_y = curs_y;
626 save_attr = curr_attr;
627 save_cset = cset;
628 save_csattr = cset_attr[cset];
629 } else {
630 curs_x = save_x;
631 curs_y = save_y;
c9def1b8 632 /* Make sure the window hasn't shrunk since the save */
633 if (curs_x >= cols) curs_x = cols-1;
634 if (curs_y >= rows) curs_y = rows-1;
635
374330e2 636 curr_attr = save_attr;
637 cset = save_cset;
638 cset_attr[cset] = save_csattr;
639 fix_cpos;
e14a5a13 640 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
374330e2 641 }
642}
643
644/*
645 * Erase a large portion of the screen: the whole screen, or the
646 * whole line, or parts thereof.
647 */
648static void erase_lots (int line_only, int from_begin, int to_end) {
649 unsigned long *startpos, *endpos;
650
651 if (line_only) {
652 startpos = cpos - curs_x;
c9def1b8 653 endpos = startpos + cols;
654 /* I've removed the +1 so that the Wide screen stuff is not
655 * removed when it shouldn't be.
656 */
374330e2 657 } else {
658 startpos = scrtop;
659 endpos = startpos + rows * (cols+1);
660 }
661 if (!from_begin)
662 startpos = cpos;
663 if (!to_end)
e14a5a13 664 endpos = cpos+1;
374330e2 665 check_selection (startpos, endpos);
e14a5a13 666
667 /* Clear screen also forces a full window redraw, just in case. */
668 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
669 term_invalidate();
670
374330e2 671 while (startpos < endpos)
e14a5a13 672 *startpos++ = erase_char;
374330e2 673}
674
675/*
676 * Insert or delete characters within the current line. n is +ve if
677 * insertion is desired, and -ve for deletion.
678 */
679static void insch (int n) {
680 int dir = (n < 0 ? -1 : +1);
681 int m;
682
683 n = (n < 0 ? -n : n);
684 if (n > cols - curs_x)
685 n = cols - curs_x;
686 m = cols - curs_x - n;
687 check_selection (cpos, cpos+n);
688 if (dir < 0) {
689 memmove (cpos, cpos+n, m*TSIZE);
690 while (n--)
e14a5a13 691 cpos[m++] = erase_char;
374330e2 692 } else {
693 memmove (cpos+n, cpos, m*TSIZE);
694 while (n--)
e14a5a13 695 cpos[n] = erase_char;
374330e2 696 }
697}
698
699/*
700 * Toggle terminal mode `mode' to state `state'. (`query' indicates
701 * whether the mode is a DEC private one or a normal one.)
702 */
703static void toggle_mode (int mode, int query, int state) {
704 if (query) switch (mode) {
705 case 1: /* application cursor keys */
706 app_cursor_keys = state;
707 break;
e14a5a13 708 case 2: /* VT52 mode */
709 vt52_mode = !state;
710 break;
374330e2 711 case 3: /* 80/132 columns */
712 deselect();
e14a5a13 713 request_resize (state ? 132 : 80, rows, 1);
714 reset_132 = state;
374330e2 715 break;
716 case 5: /* reverse video */
717 rvideo = state;
cabfd08c 718 seen_disp_event = TRUE;
e14a5a13 719 if (state) term_update();
374330e2 720 break;
721 case 6: /* DEC origin mode */
722 dec_om = state;
723 break;
724 case 7: /* auto wrap */
725 wrap = state;
726 break;
c9def1b8 727 case 8: /* auto key repeat */
728 repeat_off = !state;
729 break;
0965bee0 730 case 10: /* set local edit mode */
731 term_editing = state;
732 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
733 break;
e14a5a13 734 case 25: /* enable/disable cursor */
ec55b220 735 compatibility2(OTHER,VT220);
e14a5a13 736 cursor_on = state;
737 seen_disp_event = TRUE;
738 break;
374330e2 739 case 47: /* alternate screen */
e14a5a13 740 compatibility(OTHER);
374330e2 741 deselect();
742 swap_screen (state);
743 disptop = scrtop;
744 break;
745 } else switch (mode) {
746 case 4: /* set insert mode */
e14a5a13 747 compatibility(VT102);
374330e2 748 insert = state;
749 break;
e14a5a13 750 case 12: /* set echo mode */
0965bee0 751 term_echoing = !state;
752 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
e14a5a13 753 break;
c9def1b8 754 case 20: /* Return sends ... */
755 cr_lf_return = state;
756 break;
374330e2 757 }
758}
759
760/*
761 * Process an OSC sequence: set window title or icon name.
762 */
763static void do_osc(void) {
764 if (osc_w) {
765 while (osc_strlen--)
766 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
767 } else {
768 osc_string[osc_strlen] = '\0';
769 switch (esc_args[0]) {
770 case 0:
771 case 1:
772 set_icon (osc_string);
773 if (esc_args[0] == 1)
774 break;
775 /* fall through: parameter 0 means set both */
776 case 2:
777 case 21:
778 set_title (osc_string);
779 break;
780 }
781 }
782}
783
784/*
785 * Remove everything currently in `inbuf' and stick it up on the
786 * in-memory display. There's a big state machine in here to
787 * process escape sequences...
788 */
789void term_out(void) {
c9def1b8 790 int c, inbuf_reap;
791
c9def1b8 792 for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
793 {
794 c = inbuf[inbuf_reap];
374330e2 795
c9def1b8 796 /*
5fd04f07 797 * Optionally log the session traffic to a file. Useful for
798 * debugging and possibly also useful for actual logging.
799 */
e1c8e0ed 800 logtraffic((unsigned char)c, LGTYP_DEBUG);
801
e14a5a13 802 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
803 * be able to display 8-bit characters, but I'll let that go 'cause
804 * of i18n.
805 */
c9def1b8 806 if( ( (c&0x60) == 0 || c == '\177') &&
807 termstate < DO_CTRLS &&
e14a5a13 808 ( (c&0x80) == 0 || has_compat(VT220))) {
374330e2 809 switch (c) {
810 case '\005': /* terminal type query */
e14a5a13 811 /* Strictly speaking this is VT100 but a VT100 defaults to
812 * no response. Other terminals respond at their option.
813 *
814 * Don't put a CR in the default string as this tends to
815 * upset some weird software.
816 *
817 * An xterm returns "xterm" (5 characters)
818 */
e7fbcdd8 819 compatibility(ANSIMIN);
820 {
821 char abuf[256], *s, *d;
822 int state=0;
823 for(s=cfg.answerback, d=abuf; *s; s++) {
824 if (state)
825 {
826 if (*s >= 'a' && *s <= 'z')
827 *d++ = (*s - ('a'-1));
828 else if ((*s >='@' && *s<='_') ||
829 *s == '?' || (*s&0x80))
830 *d++ = ('@'^*s);
831 else if (*s == '~')
832 *d++ = '^';
833 state = 0;
834 }
835 else if (*s == '^') {
836 state = 1;
837 }
838 else
839 *d++ = xlat_kbd2tty((unsigned char)*s);
840 }
841 ldisc_send (abuf, d-abuf);
842 }
374330e2 843 break;
844 case '\007':
156686ef 845 {
846 struct beeptime *newbeep;
847 long ticks;
848
849 ticks = GetTickCount();
850debug(("beep received at %ld, last was %ld, nbeeps=%d\n", ticks, lastbeep, nbeeps));
851
852 if (!beep_overloaded) {
853 newbeep = smalloc(sizeof(struct beeptime));
854 newbeep->ticks = ticks;
855 newbeep->next = NULL;
856 if (!beephead)
857 beephead = newbeep;
858 else
859 beeptail->next = newbeep;
860 beeptail = newbeep;
861 nbeeps++;
862 }
863
864 /*
865 * Throw out any beeps that happened more than
866 * t seconds ago.
867 */
868 while (beephead &&
869 beephead->ticks < ticks - cfg.bellovl_t*1000) {
870 struct beeptime *tmp = beephead;
871 beephead = tmp->next;
872debug(("throwing out beep received at %ld\n", tmp->ticks));
873 sfree(tmp);
874 if (!beephead)
875 beeptail = NULL;
876 nbeeps--;
877 }
878
879 if (cfg.bellovl && beep_overloaded &&
880 ticks-lastbeep >= cfg.bellovl_s * 1000) {
881 /*
882 * If we're currently overloaded and the
883 * last beep was more than s seconds ago,
884 * leave overload mode.
885 */
886debug(("silence reigns, leaving overload mode\n"));
887 beep_overloaded = FALSE;
888 } else if (cfg.bellovl && !beep_overloaded &&
889 nbeeps >= cfg.bellovl_n) {
890 /*
891 * Now, if we have n or more beeps
892 * remaining in the queue, go into overload
893 * mode.
894 */
895debug(("%d beeps between times %ld and %ld, overload!\n",
896 nbeeps, beephead->ticks, ticks));
897 beep_overloaded = TRUE;
898 }
899 lastbeep = ticks;
900
901 /*
902 * Perform an actual beep if we're not overloaded.
903 */
904 if ((!cfg.bellovl || !beep_overloaded) && cfg.beep != 0) {
905debug(("not overloaded; performing a beep\n"));
906 if (cfg.beep != 2)
907 beep(cfg.beep);
908 else if(cfg.beep == 2) {
909 in_vbell = TRUE;
910 vbell_timeout = ticks + VBELL_TIMEOUT;
911 term_update();
912 }
913 }
914 disptop = scrtop;
915 }
374330e2 916 break;
917 case '\b':
c9def1b8 918 if (curs_x == 0 && curs_y == 0)
919 ;
920 else if (curs_x == 0 && curs_y > 0)
374330e2 921 curs_x = cols-1, curs_y--;
922 else if (wrapnext)
923 wrapnext = FALSE;
924 else
925 curs_x--;
926 fix_cpos;
cabfd08c 927 seen_disp_event = TRUE;
374330e2 928 break;
929 case '\016':
e14a5a13 930 compatibility(VT100);
374330e2 931 cset = 1;
932 break;
933 case '\017':
e14a5a13 934 compatibility(VT100);
374330e2 935 cset = 0;
936 break;
937 case '\033':
e14a5a13 938 if (vt52_mode)
939 termstate = VT52_ESC;
940 else {
941 compatibility(ANSIMIN);
942 termstate = SEEN_ESC;
943 }
374330e2 944 break;
945 case 0233:
e14a5a13 946 compatibility(VT220);
374330e2 947 termstate = SEEN_CSI;
948 esc_nargs = 1;
949 esc_args[0] = ARG_DEFAULT;
950 esc_query = FALSE;
951 break;
952 case 0235:
e14a5a13 953 compatibility(VT220);
374330e2 954 termstate = SEEN_OSC;
955 esc_args[0] = 0;
956 break;
957 case '\r':
958 curs_x = 0;
959 wrapnext = FALSE;
960 fix_cpos;
cabfd08c 961 seen_disp_event = TRUE;
c9def1b8 962 paste_hold = 0;
e1c8e0ed 963 logtraffic((unsigned char)c,LGTYP_ASCII);
374330e2 964 break;
965 case '\013':
966 case '\014':
e14a5a13 967 compatibility(VT100);
374330e2 968 case '\n':
969 if (curs_y == marg_b)
970 scroll (marg_t, marg_b, 1, TRUE);
971 else if (curs_y < rows-1)
972 curs_y++;
cabfd08c 973 if (cfg.lfhascr)
974 curs_x = 0;
374330e2 975 fix_cpos;
976 wrapnext = FALSE;
cabfd08c 977 seen_disp_event = 1;
c9def1b8 978 paste_hold = 0;
e1c8e0ed 979 logtraffic((unsigned char)c,LGTYP_ASCII);
374330e2 980 break;
981 case '\t':
374330e2 982 {
983 unsigned long *old_cpos = cpos;
c9def1b8 984 unsigned long *p = scrtop + curs_y * (cols+1) + cols;
985
986 do {
987 curs_x++;
988 } while (curs_x < cols-1 && !tabs[curs_x]);
989
990 if ((*p & LATTR_MODE) != LATTR_NORM)
991 {
992 if (curs_x >= cols/2)
993 curs_x = cols/2-1;
994 }
995 else
996 {
997 if (curs_x >= cols)
998 curs_x = cols-1;
999 }
1000
374330e2 1001 fix_cpos;
1002 check_selection (old_cpos, cpos);
1003 }
cabfd08c 1004 seen_disp_event = TRUE;
374330e2 1005 break;
1ec0fa4b 1006 case '\177': /* Destructive backspace
ec55b220 1007 This does nothing on a real VT100 */
1008 compatibility(OTHER);
1009 if (curs_x && !wrapnext) curs_x--;
1010 wrapnext = FALSE;
1011 fix_cpos;
1012 *cpos = (' ' | curr_attr | ATTR_ASCII);
1013 break;
cabfd08c 1014 }
1015 }
1016 else switch (termstate) {
1017 case TOPLEVEL:
8b811b12 1018 /* Only graphic characters get this far, ctrls are stripped above */
1ec0fa4b 1019 if (wrapnext && wrap) {
c9def1b8 1020 cpos[1] |= ATTR_WRAPPED;
8b811b12 1021 if (curs_y == marg_b)
1022 scroll (marg_t, marg_b, 1, TRUE);
1023 else if (curs_y < rows-1)
1024 curs_y++;
1025 curs_x = 0;
1026 fix_cpos;
1027 wrapnext = FALSE;
8b811b12 1028 }
1029 if (insert)
1030 insch (1);
e14a5a13 1031 if (selstate != NO_SELECTION)
1032 check_selection (cpos, cpos+1);
1033 switch (cset_attr[cset]) {
1034 /* Linedraw characters are different from 'ESC ( B' only
1035 * for a small range, for ones outside that range make sure
1036 * we use the same font as well as the same encoding.
1037 */
1038 case ATTR_LINEDRW:
ec55b220 1039 if (c<0x5f || c>0x7F)
e14a5a13 1040 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
1041 ATTR_ASCII;
ec55b220 1042 else if (c==0x5F)
1043 *cpos++ = ' ' | curr_attr | ATTR_ASCII;
e14a5a13 1044 else
1045 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
1046 break;
ec55b220 1047 case ATTR_GBCHR:
1048 /* If UK-ASCII, make the '#' a LineDraw Pound */
1049 if (c == '#') {
1050 *cpos++ = '}' | curr_attr | ATTR_LINEDRW;
1051 break;
1052 }
1053 /*FALLTHROUGH*/
e14a5a13 1054 default:
e1c8e0ed 1055 *cpos = xlat_tty2scr((unsigned char)c) | curr_attr |
e14a5a13 1056 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
e1c8e0ed 1057 logtraffic((unsigned char)c, LGTYP_ASCII);
1058 cpos++;
e14a5a13 1059 break;
1060 }
8b811b12 1061 curs_x++;
1062 if (curs_x == cols) {
1063 cpos--;
1064 curs_x--;
1ec0fa4b 1065 wrapnext = TRUE;
374330e2 1066 }
8b811b12 1067 seen_disp_event = 1;
374330e2 1068 break;
cabfd08c 1069
374330e2 1070 case IGNORE_NEXT:
1071 termstate = TOPLEVEL;
1072 break;
1073 case OSC_MAYBE_ST:
1074 /*
1075 * This state is virtually identical to SEEN_ESC, with the
1076 * exception that we have an OSC sequence in the pipeline,
1077 * and _if_ we see a backslash, we process it.
1078 */
1079 if (c == '\\') {
1080 do_osc();
1081 termstate = TOPLEVEL;
1082 break;
1083 }
1084 /* else fall through */
1085 case SEEN_ESC:
1086 termstate = TOPLEVEL;
1087 switch (c) {
374330e2 1088 case ' ': /* some weird sequence? */
e14a5a13 1089 compatibility(VT220);
374330e2 1090 termstate = IGNORE_NEXT;
1091 break;
1092 case '[': /* enter CSI mode */
1093 termstate = SEEN_CSI;
1094 esc_nargs = 1;
1095 esc_args[0] = ARG_DEFAULT;
1096 esc_query = FALSE;
1097 break;
1098 case ']': /* xterm escape sequences */
e14a5a13 1099 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1100 compatibility(OTHER);
374330e2 1101 termstate = SEEN_OSC;
1102 esc_args[0] = 0;
1103 break;
1104 case '(': /* should set GL */
e14a5a13 1105 compatibility(VT100);
374330e2 1106 termstate = SET_GL;
1107 break;
1108 case ')': /* should set GR */
e14a5a13 1109 compatibility(VT100);
374330e2 1110 termstate = SET_GR;
1111 break;
1112 case '7': /* save cursor */
e14a5a13 1113 compatibility(VT100);
374330e2 1114 save_cursor (TRUE);
1115 break;
1116 case '8': /* restore cursor */
e14a5a13 1117 compatibility(VT100);
374330e2 1118 save_cursor (FALSE);
cabfd08c 1119 seen_disp_event = TRUE;
374330e2 1120 break;
1121 case '=':
e14a5a13 1122 compatibility(VT100);
374330e2 1123 app_keypad_keys = TRUE;
1124 break;
1125 case '>':
e14a5a13 1126 compatibility(VT100);
374330e2 1127 app_keypad_keys = FALSE;
1128 break;
1129 case 'D': /* exactly equivalent to LF */
e14a5a13 1130 compatibility(VT100);
374330e2 1131 if (curs_y == marg_b)
1132 scroll (marg_t, marg_b, 1, TRUE);
1133 else if (curs_y < rows-1)
1134 curs_y++;
1135 fix_cpos;
1136 wrapnext = FALSE;
cabfd08c 1137 seen_disp_event = TRUE;
374330e2 1138 break;
1139 case 'E': /* exactly equivalent to CR-LF */
e14a5a13 1140 compatibility(VT100);
374330e2 1141 curs_x = 0;
374330e2 1142 if (curs_y == marg_b)
1143 scroll (marg_t, marg_b, 1, TRUE);
1144 else if (curs_y < rows-1)
1145 curs_y++;
1146 fix_cpos;
1147 wrapnext = FALSE;
cabfd08c 1148 seen_disp_event = TRUE;
374330e2 1149 break;
1150 case 'M': /* reverse index - backwards LF */
e14a5a13 1151 compatibility(VT100);
374330e2 1152 if (curs_y == marg_t)
1153 scroll (marg_t, marg_b, -1, TRUE);
1154 else if (curs_y > 0)
1155 curs_y--;
1156 fix_cpos;
1157 wrapnext = FALSE;
cabfd08c 1158 seen_disp_event = TRUE;
374330e2 1159 break;
1160 case 'Z': /* terminal type query */
e14a5a13 1161 compatibility(VT100);
0965bee0 1162 ldisc_send (id_string, strlen(id_string));
374330e2 1163 break;
1164 case 'c': /* restore power-on settings */
e14a5a13 1165 compatibility(VT100);
374330e2 1166 power_on();
e14a5a13 1167 if (reset_132) {
1168 request_resize (80, rows, 1);
1169 reset_132 = 0;
1170 }
374330e2 1171 fix_cpos;
1172 disptop = scrtop;
cabfd08c 1173 seen_disp_event = TRUE;
374330e2 1174 break;
1175 case '#': /* ESC # 8 fills screen with Es :-) */
e14a5a13 1176 compatibility(VT100);
374330e2 1177 termstate = SEEN_ESCHASH;
1178 break;
1179 case 'H': /* set a tab */
e14a5a13 1180 compatibility(VT100);
374330e2 1181 tabs[curs_x] = TRUE;
1182 break;
1183 }
1184 break;
1185 case SEEN_CSI:
1186 termstate = TOPLEVEL; /* default */
8b811b12 1187 if( isdigit(c) )
1188 {
374330e2 1189 if (esc_nargs <= ARGS_MAX) {
1190 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1191 esc_args[esc_nargs-1] = 0;
1192 esc_args[esc_nargs-1] =
1193 10 * esc_args[esc_nargs-1] + c - '0';
1194 }
1195 termstate = SEEN_CSI;
8b811b12 1196 }
1197 else if( c == ';' )
1198 {
374330e2 1199 if (++esc_nargs <= ARGS_MAX)
1200 esc_args[esc_nargs-1] = ARG_DEFAULT;
1201 termstate = SEEN_CSI;
8b811b12 1202 }
1203 else if( c < '@' )
1204 {
1205 if( esc_query ) esc_query = -1;
1206 else if( c == '?' ) esc_query = TRUE;
1207 else esc_query = c;
374330e2 1208 termstate = SEEN_CSI;
8b811b12 1209 }
1210 else switch (ANSI(c,esc_query)) {
374330e2 1211 case 'A': /* move up N lines */
1212 move (curs_x, curs_y - def(esc_args[0], 1), 1);
cabfd08c 1213 seen_disp_event = TRUE;
374330e2 1214 break;
e14a5a13 1215 case 'e': /* move down N lines */
1216 compatibility(ANSI);
1217 case 'B':
374330e2 1218 move (curs_x, curs_y + def(esc_args[0], 1), 1);
cabfd08c 1219 seen_disp_event = TRUE;
374330e2 1220 break;
e14a5a13 1221 case 'a': /* move right N cols */
1222 compatibility(ANSI);
1223 case 'C':
374330e2 1224 move (curs_x + def(esc_args[0], 1), curs_y, 1);
cabfd08c 1225 seen_disp_event = TRUE;
374330e2 1226 break;
1227 case 'D': /* move left N cols */
1228 move (curs_x - def(esc_args[0], 1), curs_y, 1);
cabfd08c 1229 seen_disp_event = TRUE;
374330e2 1230 break;
1231 case 'E': /* move down N lines and CR */
e14a5a13 1232 compatibility(ANSI);
374330e2 1233 move (0, curs_y + def(esc_args[0], 1), 1);
cabfd08c 1234 seen_disp_event = TRUE;
374330e2 1235 break;
1236 case 'F': /* move up N lines and CR */
e14a5a13 1237 compatibility(ANSI);
374330e2 1238 move (0, curs_y - def(esc_args[0], 1), 1);
cabfd08c 1239 seen_disp_event = TRUE;
374330e2 1240 break;
1241 case 'G': case '`': /* set horizontal posn */
e14a5a13 1242 compatibility(ANSI);
374330e2 1243 move (def(esc_args[0], 1) - 1, curs_y, 0);
cabfd08c 1244 seen_disp_event = TRUE;
374330e2 1245 break;
1246 case 'd': /* set vertical posn */
e14a5a13 1247 compatibility(ANSI);
374330e2 1248 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1249 (dec_om ? 2 : 0));
cabfd08c 1250 seen_disp_event = TRUE;
374330e2 1251 break;
1252 case 'H': case 'f': /* set horz and vert posns at once */
1253 if (esc_nargs < 2)
1254 esc_args[1] = ARG_DEFAULT;
1255 move (def(esc_args[1], 1) - 1,
1256 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1257 (dec_om ? 2 : 0));
cabfd08c 1258 seen_disp_event = TRUE;
374330e2 1259 break;
1260 case 'J': /* erase screen or parts of it */
1261 {
1262 unsigned int i = def(esc_args[0], 0) + 1;
1263 if (i > 3)
1264 i = 0;
1265 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1266 }
1267 disptop = scrtop;
cabfd08c 1268 seen_disp_event = TRUE;
374330e2 1269 break;
1270 case 'K': /* erase line or parts of it */
1271 {
1272 unsigned int i = def(esc_args[0], 0) + 1;
1273 if (i > 3)
1274 i = 0;
1275 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1276 }
cabfd08c 1277 seen_disp_event = TRUE;
374330e2 1278 break;
1279 case 'L': /* insert lines */
e14a5a13 1280 compatibility(VT102);
374330e2 1281 if (curs_y <= marg_b)
1282 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
cabfd08c 1283 seen_disp_event = TRUE;
374330e2 1284 break;
1285 case 'M': /* delete lines */
e14a5a13 1286 compatibility(VT102);
374330e2 1287 if (curs_y <= marg_b)
cabfd08c 1288 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1289 seen_disp_event = TRUE;
374330e2 1290 break;
1291 case '@': /* insert chars */
e14a5a13 1292 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1293 compatibility(VT102);
374330e2 1294 insch (def(esc_args[0], 1));
cabfd08c 1295 seen_disp_event = TRUE;
374330e2 1296 break;
1297 case 'P': /* delete chars */
e14a5a13 1298 compatibility(VT102);
374330e2 1299 insch (-def(esc_args[0], 1));
cabfd08c 1300 seen_disp_event = TRUE;
374330e2 1301 break;
1302 case 'c': /* terminal type query */
e14a5a13 1303 compatibility(VT100);
1304 /* This is the response for a VT102 */
0965bee0 1305 ldisc_send (id_string, strlen(id_string));
374330e2 1306 break;
1307 case 'n': /* cursor position query */
1308 if (esc_args[0] == 6) {
1309 char buf[32];
1310 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
0965bee0 1311 ldisc_send (buf, strlen(buf));
374330e2 1312 }
e14a5a13 1313 else if (esc_args[0] == 5) {
0965bee0 1314 ldisc_send ("\033[0n", 4);
e14a5a13 1315 }
374330e2 1316 break;
e14a5a13 1317 case 'h': /* toggle modes to high */
8b811b12 1318 case ANSI_QUE('h'):
e14a5a13 1319 compatibility(VT100);
1320 {
1321 int i;
1322 for (i=0; i<esc_nargs; i++)
1323 toggle_mode (esc_args[i], esc_query, TRUE);
1324 }
374330e2 1325 break;
e14a5a13 1326 case 'l': /* toggle modes to low */
8b811b12 1327 case ANSI_QUE('l'):
e14a5a13 1328 compatibility(VT100);
1329 {
1330 int i;
1331 for (i=0; i<esc_nargs; i++)
1332 toggle_mode (esc_args[i], esc_query, FALSE);
1333 }
374330e2 1334 break;
1335 case 'g': /* clear tabs */
e14a5a13 1336 compatibility(VT100);
374330e2 1337 if (esc_nargs == 1) {
1338 if (esc_args[0] == 0) {
1339 tabs[curs_x] = FALSE;
1340 } else if (esc_args[0] == 3) {
1341 int i;
1342 for (i = 0; i < cols; i++)
1343 tabs[i] = FALSE;
1344 }
1345 }
1346 break;
1347 case 'r': /* set scroll margins */
e14a5a13 1348 compatibility(VT100);
8b811b12 1349 if (esc_nargs <= 2) {
374330e2 1350 int top, bot;
7e394092 1351 top = def(esc_args[0], 1) - 1;
1352 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1353 def(esc_args[1], rows)) - 1;
374330e2 1354 if (bot >= rows)
1355 bot = rows-1;
7e394092 1356 /* VTTEST Bug 9 - if region is less than 2 lines
1357 * don't change region.
1358 */
83ea993b 1359 if (bot-top > 0) {
374330e2 1360 marg_t = top;
1361 marg_b = bot;
1362 curs_x = 0;
1363 /*
1364 * I used to think the cursor should be
1365 * placed at the top of the newly marginned
1366 * area. Apparently not: VMS TPU falls over
1367 * if so.
e14a5a13 1368 *
1369 * Well actually it should for Origin mode - RDB
374330e2 1370 */
e14a5a13 1371 curs_y = (dec_om ? marg_t : 0);
374330e2 1372 fix_cpos;
cabfd08c 1373 seen_disp_event = TRUE;
374330e2 1374 }
1375 }
1376 break;
1377 case 'm': /* set graphics rendition */
1378 {
e14a5a13 1379 /*
1380 * A VT100 without the AVO only had one attribute, either
1381 * underline or reverse video depending on the cursor type,
1382 * this was selected by CSI 7m.
1383 *
1384 * case 2:
1385 * This is DIM on the VT100-AVO and VT102
1386 * case 5:
1387 * This is BLINK on the VT100-AVO and VT102+
1388 * case 8:
1389 * This is INVIS on the VT100-AVO and VT102
1390 * case 21:
1391 * This like 22 disables BOLD, DIM and INVIS
1392 *
1393 * The ANSI colours appear on any terminal that has colour
1394 * (obviously) but the interaction between sgr0 and the
1395 * colours varies but is usually related to the background
1396 * colour erase item.
1397 * The interaction between colour attributes and the mono
1398 * ones is also very implementation dependent.
1399 *
1400 * The 39 and 49 attributes are likely to be unimplemented.
1401 */
374330e2 1402 int i;
1403 for (i=0; i<esc_nargs; i++) {
1404 switch (def(esc_args[i], 0)) {
1405 case 0: /* restore defaults */
1406 curr_attr = ATTR_DEFAULT; break;
1407 case 1: /* enable bold */
e14a5a13 1408 compatibility(VT100AVO);
374330e2 1409 curr_attr |= ATTR_BOLD; break;
374330e2 1410 case 21: /* (enable double underline) */
e14a5a13 1411 compatibility(OTHER);
1412 case 4: /* enable underline */
1413 compatibility(VT100AVO);
374330e2 1414 curr_attr |= ATTR_UNDER; break;
e14a5a13 1415 case 5: /* enable blink */
1416 compatibility(VT100AVO);
1417 curr_attr |= ATTR_BLINK; break;
374330e2 1418 case 7: /* enable reverse video */
1419 curr_attr |= ATTR_REVERSE; break;
1420 case 22: /* disable bold */
ec55b220 1421 compatibility2(OTHER,VT220);
374330e2 1422 curr_attr &= ~ATTR_BOLD; break;
1423 case 24: /* disable underline */
ec55b220 1424 compatibility2(OTHER,VT220);
374330e2 1425 curr_attr &= ~ATTR_UNDER; break;
e14a5a13 1426 case 25: /* disable blink */
ec55b220 1427 compatibility2(OTHER,VT220);
e14a5a13 1428 curr_attr &= ~ATTR_BLINK; break;
374330e2 1429 case 27: /* disable reverse video */
ec55b220 1430 compatibility2(OTHER,VT220);
374330e2 1431 curr_attr &= ~ATTR_REVERSE; break;
1432 case 30: case 31: case 32: case 33:
1433 case 34: case 35: case 36: case 37:
1434 /* foreground */
1435 curr_attr &= ~ATTR_FGMASK;
1436 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1437 break;
1438 case 39: /* default-foreground */
1439 curr_attr &= ~ATTR_FGMASK;
1440 curr_attr |= ATTR_DEFFG;
1441 break;
1442 case 40: case 41: case 42: case 43:
1443 case 44: case 45: case 46: case 47:
1444 /* background */
1445 curr_attr &= ~ATTR_BGMASK;
1446 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1447 break;
1448 case 49: /* default-background */
1449 curr_attr &= ~ATTR_BGMASK;
1450 curr_attr |= ATTR_DEFBG;
1451 break;
1452 }
1453 }
e14a5a13 1454 if (use_bce)
c9def1b8 1455 erase_char =
1456 (' '|
1457 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1458 );
374330e2 1459 }
1460 break;
1461 case 's': /* save cursor */
1462 save_cursor (TRUE);
1463 break;
1464 case 'u': /* restore cursor */
1465 save_cursor (FALSE);
cabfd08c 1466 seen_disp_event = TRUE;
374330e2 1467 break;
1468 case 't': /* set page size - ie window height */
e14a5a13 1469 /*
1470 * VT340/VT420 sequence DECSLPP, DEC only allows values
1471 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1472 * illegal values (eg first arg 1..9) for window changing
1473 * and reports.
1474 */
1475 compatibility(VT340TEXT);
1476 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
c9def1b8 1477 request_resize (cols, def(esc_args[0], 24), 0);
e14a5a13 1478 deselect();
1479 }
1480 break;
1481 case ANSI('|', '*'):
1482 /* VT420 sequence DECSNLS
1483 * Set number of lines on screen
1484 * VT420 uses VGA like hardware and can support any size in
1485 * reasonable range (24..49 AIUI) with no default specified.
1486 */
1487 compatibility(VT420);
c9def1b8 1488 if (esc_nargs==1 && esc_args[0]>0) {
1489 request_resize (cols, def(esc_args[0], cfg.height), 0);
e14a5a13 1490 deselect();
1491 }
1492 break;
1493 case ANSI('|', '$'):
1494 /* VT340/VT420 sequence DECSCPP
1495 * Set number of columns per page
1496 * Docs imply range is only 80 or 132, but I'll allow any.
1497 */
1498 compatibility(VT340TEXT);
1499 if (esc_nargs<=1) {
c9def1b8 1500 request_resize (def(esc_args[0], cfg.width), rows, 0);
e14a5a13 1501 deselect();
1502 }
374330e2 1503 break;
1504 case 'X': /* write N spaces w/o moving cursor */
e14a5a13 1505 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
c9def1b8 1506 compatibility(ANSIMIN);
374330e2 1507 {
1508 int n = def(esc_args[0], 1);
1509 unsigned long *p = cpos;
1510 if (n > cols - curs_x)
1511 n = cols - curs_x;
1512 check_selection (cpos, cpos+n);
1513 while (n--)
e14a5a13 1514 *p++ = erase_char;
cabfd08c 1515 seen_disp_event = TRUE;
374330e2 1516 }
1517 break;
1518 case 'x': /* report terminal characteristics */
e14a5a13 1519 compatibility(VT100);
374330e2 1520 {
1521 char buf[32];
1522 int i = def(esc_args[0], 0);
1523 if (i == 0 || i == 1) {
1524 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1525 buf[2] += i;
0965bee0 1526 ldisc_send (buf, 20);
374330e2 1527 }
1528 }
1529 break;
e14a5a13 1530 case ANSI('L','='):
1531 compatibility(OTHER);
1532 use_bce = (esc_args[0]<=0);
1533 erase_char = ERASE_CHAR;
1534 if (use_bce)
1535 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1536 break;
c9def1b8 1537 case ANSI('E','='):
1538 compatibility(OTHER);
1539 blink_is_real = (esc_args[0]>=1);
1540 break;
e14a5a13 1541 case ANSI('p','"'):
1542 /* Allow the host to make this emulator a 'perfect' VT102.
1543 * This first appeared in the VT220, but we do need to get
1544 * back to PuTTY mode so I won't check it.
1545 *
ec55b220 1546 * The arg in 40..42 are a PuTTY extension.
1547 * The 2nd arg, 8bit vs 7bit is not checked.
e14a5a13 1548 *
1549 * Setting VT102 mode should also change the Fkeys to
1550 * generate PF* codes as a real VT102 has no Fkeys.
1551 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1552 * send nothing.
1553 *
1554 * Note ESC c will NOT change this!
1555 */
1556
ec55b220 1557 switch (esc_args[0]) {
1558 case 61: compatibility_level &= ~TM_VTXXX;
1559 compatibility_level |= TM_VT102; break;
1560 case 62: compatibility_level &= ~TM_VTXXX;
1561 compatibility_level |= TM_VT220; break;
1562
1563 default: if( esc_args[0] > 60 && esc_args[0] < 70 )
1564 compatibility_level |= TM_VTXXX;
1565 break;
1566
1567 case 40: compatibility_level &= TM_VTXXX; break;
1568 case 41: compatibility_level = TM_PUTTY; break;
1569 case 42: compatibility_level = TM_SCOANSI; break;
1570
1571 case ARG_DEFAULT:
1572 compatibility_level = TM_PUTTY; break;
1573 case 50: break;
1574 }
1575
1576 /* Change the response to CSI c */
1577 if (esc_args[0] == 50) {
1578 int i;
1579 char lbuf[64];
1580 strcpy(id_string, "\033[?");
1581 for (i=1; i<esc_nargs; i++) {
1582 if (i!=1) strcat(id_string, ";");
1583 sprintf(lbuf, "%d", esc_args[i]);
1584 strcat(id_string, lbuf);
1585 }
1586 strcat(id_string, "c");
1587 }
1588
1589#if 0
1590 /* Is this a good idea ?
1591 * Well we should do a soft reset at this point ...
1592 */
1593 if (!has_compat(VT420) && has_compat(VT100)) {
1594 if (reset_132) request_resize (132, 24, 1);
1595 else request_resize ( 80, 24, 1);
1596 }
1597#endif
e14a5a13 1598 break;
374330e2 1599 }
1600 break;
1601 case SET_GL:
1602 case SET_GR:
e14a5a13 1603 /* VT100 only here, checked above */
374330e2 1604 switch (c) {
1605 case 'A':
1606 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1607 break;
1608 case '0':
1609 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1610 break;
e14a5a13 1611 case 'B':
374330e2 1612 default: /* specifically, 'B' */
1613 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1614 break;
1615 }
e14a5a13 1616 if( !has_compat(VT220) || c != '%' )
8b811b12 1617 termstate = TOPLEVEL;
374330e2 1618 break;
1619 case SEEN_OSC:
1620 osc_w = FALSE;
1621 switch (c) {
374330e2 1622 case 'P': /* Linux palette sequence */
1623 termstate = SEEN_OSC_P;
1624 osc_strlen = 0;
1625 break;
1626 case 'R': /* Linux palette reset */
1627 palette_reset();
1628 term_invalidate();
1629 termstate = TOPLEVEL;
1630 break;
1631 case 'W': /* word-set */
1632 termstate = SEEN_OSC_W;
1633 osc_w = TRUE;
1634 break;
1635 case '0': case '1': case '2': case '3': case '4':
1636 case '5': case '6': case '7': case '8': case '9':
1637 esc_args[0] = 10 * esc_args[0] + c - '0';
1638 break;
1639 case 'L':
1640 /*
1641 * Grotty hack to support xterm and DECterm title
1642 * sequences concurrently.
1643 */
1644 if (esc_args[0] == 2) {
1645 esc_args[0] = 1;
1646 break;
1647 }
1648 /* else fall through */
1649 default:
1650 termstate = OSC_STRING;
1651 osc_strlen = 0;
1652 }
1653 break;
1654 case OSC_STRING:
8b811b12 1655 /*
1656 * This OSC stuff is EVIL. It takes just one character to get into
1657 * sysline mode and it's not initially obvious how to get out.
1658 * So I've added CR and LF as string aborts.
1659 * This shouldn't effect compatibility as I believe embedded
1660 * control characters are supposed to be interpreted (maybe?)
1661 * and they don't display anything useful anyway.
1662 *
1663 * -- RDB
1664 */
1665 if (c == '\n' || c == '\r') {
1666 termstate = TOPLEVEL;
1667 } else if (c == 0234 || c == '\007' ) {
374330e2 1668 /*
1669 * These characters terminate the string; ST and BEL
1670 * terminate the sequence and trigger instant
1671 * processing of it, whereas ESC goes back to SEEN_ESC
1672 * mode unless it is followed by \, in which case it is
1673 * synonymous with ST in the first place.
1674 */
1675 do_osc();
1676 termstate = TOPLEVEL;
1677 } else if (c == '\033')
1678 termstate = OSC_MAYBE_ST;
1679 else if (osc_strlen < OSC_STR_MAX)
1680 osc_string[osc_strlen++] = c;
1681 break;
1682 case SEEN_OSC_P:
1683 {
1684 int max = (osc_strlen == 0 ? 21 : 16);
1685 int val;
1686 if (c >= '0' && c <= '9')
1687 val = c - '0';
1688 else if (c >= 'A' && c <= 'A'+max-10)
1689 val = c - 'A' + 10;
1690 else if (c >= 'a' && c <= 'a'+max-10)
1691 val = c - 'a' + 10;
1692 else
1693 termstate = TOPLEVEL;
1694 osc_string[osc_strlen++] = val;
1695 if (osc_strlen >= 7) {
1696 palette_set (osc_string[0],
1697 osc_string[1] * 16 + osc_string[2],
1698 osc_string[3] * 16 + osc_string[4],
1699 osc_string[5] * 16 + osc_string[6]);
1700 term_invalidate();
1701 termstate = TOPLEVEL;
1702 }
1703 }
1704 break;
1705 case SEEN_OSC_W:
1706 switch (c) {
374330e2 1707 case '0': case '1': case '2': case '3': case '4':
1708 case '5': case '6': case '7': case '8': case '9':
1709 esc_args[0] = 10 * esc_args[0] + c - '0';
1710 break;
1711 default:
1712 termstate = OSC_STRING;
1713 osc_strlen = 0;
1714 }
1715 break;
1716 case SEEN_ESCHASH:
c9def1b8 1717 {
1718 unsigned long *p;
1719 unsigned long nlattr;
1720 int n;
1721
1722 switch (c) {
1723 case '8':
1724 p = scrtop;
1725 n = rows * (cols+1);
1726 while (n--)
1727 *p++ = ATTR_DEFAULT | 'E';
1728 disptop = scrtop;
1729 seen_disp_event = TRUE;
1730 check_selection (scrtop, scrtop + rows * (cols+1));
1731 break;
1732
1733 case '3': nlattr = LATTR_TOP; if(0) {
1734 case '4': nlattr = LATTR_BOT; } if(0) {
1735 case '5': nlattr = LATTR_NORM; } if(0) {
1736 case '6': nlattr = LATTR_WIDE; }
1737
1738 p = scrtop + curs_y * (cols+1) + cols;
1739 *p &= ~LATTR_MODE;
1740 *p |= nlattr;
1741 }
374330e2 1742 }
1743 termstate = TOPLEVEL;
1744 break;
e14a5a13 1745 case VT52_ESC:
1746 termstate = TOPLEVEL;
1747 seen_disp_event = TRUE;
1748 switch (c) {
1749 case 'A':
1750 move (curs_x, curs_y - 1, 1);
1751 break;
1752 case 'B':
1753 move (curs_x, curs_y + 1, 1);
1754 break;
1755 case 'C':
1756 move (curs_x + 1, curs_y, 1);
1757 break;
1758 case 'D':
1759 move (curs_x - 1, curs_y, 1);
1760 break;
1761 case 'F':
1762 cset_attr[cset=0] = ATTR_LINEDRW;
1763 break;
1764 case 'G':
1765 cset_attr[cset=0] = ATTR_ASCII;
1766 break;
1767 case 'H':
1768 move (0, 0, 0);
1769 break;
1770 case 'I':
1771 if (curs_y == 0)
1772 scroll (0, rows-1, -1, TRUE);
1773 else if (curs_y > 0)
1774 curs_y--;
1775 fix_cpos;
1776 wrapnext = FALSE;
1777 break;
1778 case 'J':
1779 erase_lots(FALSE, FALSE, TRUE);
1780 disptop = scrtop;
1781 break;
1782 case 'K':
1783 erase_lots(TRUE, FALSE, TRUE);
1784 break;
1785 case 'V':
1786 /* XXX Print cursor line */
1787 break;
1788 case 'W':
1789 /* XXX Start controller mode */
1790 break;
1791 case 'X':
1792 /* XXX Stop controller mode */
1793 break;
1794 case 'Y':
1795 termstate = VT52_Y1;
1796 break;
1797 case 'Z':
0965bee0 1798 ldisc_send ("\033/Z", 3);
e14a5a13 1799 break;
1800 case '=':
ec55b220 1801 app_keypad_keys = TRUE;
e14a5a13 1802 break;
1803 case '>':
ec55b220 1804 app_keypad_keys = FALSE;
e14a5a13 1805 break;
1806 case '<':
1807 /* XXX This should switch to VT100 mode not current or default
1808 * VT mode. But this will only have effect in a VT220+
1809 * emulation.
1810 */
1811 vt52_mode = FALSE;
1812 break;
1813 case '^':
1814 /* XXX Enter auto print mode */
1815 break;
1816 case '_':
1817 /* XXX Exit auto print mode */
1818 break;
1819 case ']':
1820 /* XXX Print screen */
1821 break;
1822 }
1823 break;
1824 case VT52_Y1:
1825 termstate = VT52_Y2;
1826 move(curs_x, c-' ', 0);
1827 break;
1828 case VT52_Y2:
1829 termstate = TOPLEVEL;
1830 move(c-' ', curs_y, 0);
1831 break;
374330e2 1832 }
e14a5a13 1833 if (selstate != NO_SELECTION)
1834 check_selection (cpos, cpos+1);
374330e2 1835 }
c9def1b8 1836 inbuf_head = 0;
374330e2 1837}
1838
1839/*
1840 * Compare two lines to determine whether they are sufficiently
1841 * alike to scroll-optimise one to the other. Return the degree of
1842 * similarity.
1843 */
1844static int linecmp (unsigned long *a, unsigned long *b) {
1845 int i, n;
1846
1847 for (i=n=0; i < cols; i++)
1848 n += (*a++ == *b++);
1849 return n;
1850}
1851
1852/*
1853 * Given a context, update the window. Out of paranoia, we don't
1854 * allow WM_PAINT responses to do scrolling optimisations.
1855 */
ec8679e9 1856static void do_paint (Context ctx, int may_optimise){
374330e2 1857 int i, j, start, our_curs_y;
1858 unsigned long attr, rv, cursor;
1859 char ch[1024];
156686ef 1860 long ticks;
374330e2 1861
156686ef 1862 /*
1863 * Check the visual bell state.
1864 */
1865 if (in_vbell) {
1866 ticks = GetTickCount();
1867 if (ticks - vbell_timeout >= 0)
1868 in_vbell = FALSE;
1869 }
1870
1871 /* Depends on:
1872 * screen array, disptop, scrtop,
1873 * selection, rv,
1874 * cfg.blinkpc, blink_is_real, tblinker,
1875 * curs_y, curs_x, blinker, cfg.blink_cur, cursor_on, has_focus
1876 */
e14a5a13 1877 if (cursor_on) {
1878 if (has_focus) {
217dceef 1879 if (blinker || !cfg.blink_cur)
1880 cursor = ATTR_ACTCURS;
1881 else
1882 cursor = 0;
e14a5a13 1883 }
1884 else
1885 cursor = ATTR_PASCURS;
4e30ff69 1886 if (wrapnext)
1887 cursor |= ATTR_RIGHTCURS;
e14a5a13 1888 }
156686ef 1889 else
1890 cursor = 0;
1891 rv = (!rvideo ^ !in_vbell ? ATTR_REVERSE : 0);
374330e2 1892 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1893
1894 for (i=0; i<rows; i++) {
1895 int idx = i*(cols+1);
c9def1b8 1896 int lattr = (disptop[idx+cols] & LATTR_MODE);
374330e2 1897 for (j=0; j<=cols; j++,idx++) {
1898 unsigned long *d = disptop+idx;
4e30ff69 1899 wanttext[idx] = lattr | (((*d &~ ATTR_WRAPPED) ^ rv
374330e2 1900 ^ (selstart <= d && d < selend ?
1901 ATTR_REVERSE : 0)) |
1902 (i==our_curs_y && j==curs_x ? cursor : 0));
c9def1b8 1903 if (blink_is_real) {
1904 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1905 {
1906 wanttext[idx] &= ATTR_MASK;
1907 wanttext[idx] += ' ';
1908 }
1909 wanttext[idx] &= ~ATTR_BLINK;
1910 }
374330e2 1911 }
1912 }
1913
1914 /*
1915 * We would perform scrolling optimisations in here, if they
1916 * didn't have a nasty tendency to cause the whole sodding
1917 * program to hang for a second at speed-critical moments.
1918 * We'll leave it well alone...
1919 */
1920
1921 for (i=0; i<rows; i++) {
1922 int idx = i*(cols+1);
c9def1b8 1923 int lattr = (wanttext[idx+cols] & LATTR_MODE);
374330e2 1924 start = -1;
1925 for (j=0; j<=cols; j++,idx++) {
1926 unsigned long t = wanttext[idx];
1927 int needs_update = (j < cols && t != disptext[idx]);
1928 int keep_going = (start != -1 && needs_update &&
1929 (t & ATTR_MASK) == attr &&
1930 j-start < sizeof(ch));
1931 if (start != -1 && !keep_going) {
c9def1b8 1932 do_text (ctx, start, i, ch, j-start, attr, lattr);
374330e2 1933 start = -1;
1934 }
1935 if (needs_update) {
1936 if (start == -1) {
1937 start = j;
1938 attr = t & ATTR_MASK;
1939 }
1940 ch[j-start] = (char) (t & CHAR_MASK);
1941 }
1942 disptext[idx] = t;
1943 }
1944 }
1945}
1946
1947/*
e14a5a13 1948 * Flick the switch that says if blinking things should be shown or hidden.
1949 */
1950
1951void term_blink(int flg) {
22dcdc3b 1952 static long last_blink = 0;
1953 static long last_tblink = 0;
e14a5a13 1954 long now, blink_diff;
1955
c9def1b8 1956 now = GetTickCount();
1957 blink_diff = now-last_tblink;
1958
1959 /* Make sure the text blinks no more than 2Hz */
1960 if (blink_diff<0 || blink_diff>450)
1961 {
1962 last_tblink = now;
1963 tblinker = !tblinker;
1964 }
1965
e14a5a13 1966 if (flg) {
1967 blinker = 1;
c9def1b8 1968 last_blink = now;
e14a5a13 1969 return;
1970 }
e14a5a13 1971
1972 blink_diff = now-last_blink;
1973
22dcdc3b 1974 /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */
1975 if (blink_diff>=0 && blink_diff<(long)GetCaretBlinkTime())
e14a5a13 1976 return;
1977
1978 last_blink = now;
1979 blinker = !blinker;
1980}
1981
1982/*
374330e2 1983 * Invalidate the whole screen so it will be repainted in full.
1984 */
1985void term_invalidate(void) {
1986 int i;
1987
1988 for (i=0; i<rows*(cols+1); i++)
1989 disptext[i] = ATTR_INVALID;
1990}
1991
1992/*
1993 * Paint the window in response to a WM_PAINT message.
1994 */
1995void term_paint (Context ctx, int l, int t, int r, int b) {
1996 int i, j, left, top, right, bottom;
1997
1998 left = l / font_width;
1999 right = (r - 1) / font_width;
2000 top = t / font_height;
2001 bottom = (b - 1) / font_height;
e42ad027 2002 for (i = top; i <= bottom && i < rows ; i++)
c9def1b8 2003 {
2004 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
2005 for (j = left; j <= right && j < cols ; j++)
2006 disptext[i*(cols+1)+j] = ATTR_INVALID;
2007 else
2008 for (j = left/2; j <= right/2+1 && j < cols ; j++)
2009 disptext[i*(cols+1)+j] = ATTR_INVALID;
2010 }
374330e2 2011
e14a5a13 2012 /* This should happen soon enough, also for some reason it sometimes
2013 * fails to actually do anything when re-sizing ... painting the wrong
2014 * window perhaps ?
374330e2 2015 do_paint (ctx, FALSE);
e14a5a13 2016 */
374330e2 2017}
2018
2019/*
2020 * Attempt to scroll the scrollback. The second parameter gives the
2021 * position we want to scroll to; the first is +1 to denote that
2022 * this position is relative to the beginning of the scrollback, -1
2023 * to denote it is relative to the end, and 0 to denote that it is
2024 * relative to the current position.
2025 */
2026void term_scroll (int rel, int where) {
2027 int n = where * (cols+1);
2028
2029 disptop = (rel < 0 ? scrtop :
2030 rel > 0 ? sbtop : disptop) + n;
2031 if (disptop < sbtop)
2032 disptop = sbtop;
2033 if (disptop > scrtop)
2034 disptop = scrtop;
2035 update_sbar();
2036 term_update();
2037}
2038
508a6084 2039static void clipme(unsigned long *top, unsigned long *bottom, char *workbuf) {
bc1235d4 2040 char *wbptr; /* where next char goes within workbuf */
2041 int wblen = 0; /* workbuf len */
2042 int buflen; /* amount of memory allocated to workbuf */
2043
2044 if ( workbuf != NULL ) { /* user supplied buffer? */
2045 buflen = -1; /* assume buffer passed in is big enough */
2046 wbptr = workbuf; /* start filling here */
2047 }
2048 else
2049 buflen = 0; /* No data is available yet */
2050
2051 while (top < bottom) {
2052 int nl = FALSE;
2053 unsigned long *lineend = top - (top-text) % (cols+1) + cols;
2054 unsigned long *nlpos = lineend;
2055
2056 if (!(*nlpos & ATTR_WRAPPED)) {
2057 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > top)
2058 nlpos--;
2059 if (nlpos < bottom)
2060 nl = TRUE;
2061 }
2062 while (top < nlpos && top < bottom)
2063 {
d3a22f79 2064 int ch = (*top & CHAR_MASK);
2065 int set = (*top & CSET_MASK);
2066
2067 /* VT Specials -> ISO8859-1 for Cut&Paste */
2068 static const unsigned char poorman2[] =
bc1235d4 2069"* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
bc1235d4 2070
d3a22f79 2071 if (set && !cfg.rawcnp) {
2072 if (set == ATTR_LINEDRW && ch >= 0x60 && ch < 0x7F) {
2073 int x;
2074 if ((x = poorman2[2*(ch-0x60)+1]) == ' ')
2075 x = 0;
2076 ch = (x<<8) + poorman2[2*(ch-0x60)];
2077 }
2078 }
bc1235d4 2079
d3a22f79 2080 while(ch != 0) {
2081 if (cfg.rawcnp || !!(ch&0xE0)) {
2082 if ( wblen == buflen )
2083 {
2084 workbuf = srealloc(workbuf, buflen += 100);
2085 wbptr = workbuf + wblen;
2086 }
2087 wblen++;
2088 *wbptr++ = (unsigned char) ch;
2089 }
2090 ch>>=8;
bc1235d4 2091 }
bc1235d4 2092 top++;
2093 }
2094 if (nl) {
2095 int i;
2096 for (i=0; i<sizeof(sel_nl); i++)
2097 {
2098 if ( wblen == buflen )
2099 {
2100 workbuf = srealloc(workbuf, buflen += 100);
2101 wbptr = workbuf + wblen;
2102 }
2103 wblen++;
2104 *wbptr++ = sel_nl[i];
2105 }
2106 }
2107 top = lineend + 1; /* start of next line */
2108 }
2109 write_clip (workbuf, wblen, FALSE); /* transfer to clipboard */
2110 if ( buflen > 0 ) /* indicates we allocated this buffer */
2111 sfree(workbuf);
2112
2113}
2114void term_copyall (void) {
2115 clipme(sbtop, cpos, NULL /* dynamic allocation */);
2116}
2117
374330e2 2118/*
2119 * Spread the selection outwards according to the selection mode.
2120 */
2121static unsigned long *sel_spread_half (unsigned long *p, int dir) {
2122 unsigned long *linestart, *lineend;
2123 int x;
2124 short wvalue;
2125
2126 x = (p - text) % (cols+1);
2127 linestart = p - x;
2128 lineend = linestart + cols;
2129
2130 switch (selmode) {
2131 case SM_CHAR:
2132 /*
2133 * In this mode, every character is a separate unit, except
2134 * for runs of spaces at the end of a non-wrapping line.
2135 */
2136 if (!(linestart[cols] & ATTR_WRAPPED)) {
2137 unsigned long *q = lineend;
2138 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
2139 q--;
2140 if (q == lineend)
2141 q--;
2142 if (p >= q)
2143 p = (dir == -1 ? q : lineend - 1);
2144 }
2145 break;
2146 case SM_WORD:
2147 /*
2148 * In this mode, the units are maximal runs of characters
2149 * whose `wordness' has the same value.
2150 */
2151 wvalue = wordness[*p & CHAR_MASK];
2152 if (dir == +1) {
2153 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
2154 p++;
2155 } else {
2156 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
2157 p--;
2158 }
2159 break;
2160 case SM_LINE:
2161 /*
2162 * In this mode, every line is a unit.
2163 */
2164 p = (dir == -1 ? linestart : lineend - 1);
2165 break;
2166 }
2167 return p;
2168}
2169
2170static void sel_spread (void) {
2171 selstart = sel_spread_half (selstart, -1);
2172 selend = sel_spread_half (selend - 1, +1) + 1;
2173}
2174
2175void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
37508af4 2176 unsigned long *selpoint;
2177
2178 if (y<0) y = 0;
2179 if (y>=rows) y = rows-1;
094ed2a6 2180 if (x<0) {
2181 if (y > 0) {
2182 x = cols-1;
2183 y--;
2184 } else
2185 x = 0;
2186 }
37508af4 2187 if (x>=cols) x = cols-1;
2188
c9def1b8 2189 selpoint = disptop + y * (cols+1);
2190 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
2191 selpoint += x/2;
2192 else
2193 selpoint += x;
374330e2 2194
2195 if (b == MB_SELECT && a == MA_CLICK) {
2196 deselect();
2197 selstate = ABOUT_TO;
2198 selanchor = selpoint;
2199 selmode = SM_CHAR;
2200 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
2201 deselect();
2202 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
2203 selstate = DRAGGING;
2204 selstart = selanchor = selpoint;
2205 selend = selstart + 1;
2206 sel_spread();
2207 } else if ((b == MB_SELECT && a == MA_DRAG) ||
2208 (b == MB_EXTEND && a != MA_RELEASE)) {
2209 if (selstate == ABOUT_TO && selanchor == selpoint)
2210 return;
2211 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
2212 if (selpoint-selstart < (selend-selstart)/2)
2213 selanchor = selend - 1;
2214 else
2215 selanchor = selstart;
2216 selstate = DRAGGING;
2217 }
2218 if (selstate != ABOUT_TO && selstate != DRAGGING)
2219 selanchor = selpoint;
2220 selstate = DRAGGING;
2221 if (selpoint < selanchor) {
2222 selstart = selpoint;
2223 selend = selanchor + 1;
2224 } else {
2225 selstart = selanchor;
2226 selend = selpoint + 1;
2227 }
2228 sel_spread();
2229 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
2230 if (selstate == DRAGGING) {
2231 /*
2232 * We've completed a selection. We now transfer the
2233 * data to the clipboard.
2234 */
bc1235d4 2235 clipme(selstart, selend, selspace);
374330e2 2236 selstate = SELECTED;
2237 } else
2238 selstate = NO_SELECTION;
2239 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2240 char *data;
2241 int len;
2242
2243 get_clip((void **) &data, &len);
2244 if (data) {
2245 char *p, *q;
c9def1b8 2246
2247 if (paste_buffer) sfree(paste_buffer);
2248 paste_pos = paste_hold = paste_len = 0;
2249 paste_buffer = smalloc(len);
2250
374330e2 2251 p = q = data;
2252 while (p < data+len) {
2253 while (p < data+len &&
2254 !(p <= data+len-sizeof(sel_nl) &&
2255 !memcmp(p, sel_nl, sizeof(sel_nl))))
2256 p++;
14963b8f 2257
2258 {
2259 int i;
2260 unsigned char c;
2261 for(i=0;i<p-q;i++)
2262 {
2263 c=xlat_kbd2tty(q[i]);
c9def1b8 2264 paste_buffer[paste_len++] = c;
14963b8f 2265 }
2266 }
2267
374330e2 2268 if (p <= data+len-sizeof(sel_nl) &&
2269 !memcmp(p, sel_nl, sizeof(sel_nl))) {
c9def1b8 2270 paste_buffer[paste_len++] = '\r';
374330e2 2271 p += sizeof(sel_nl);
2272 }
2273 q = p;
2274 }
c9def1b8 2275
2276 /* Assume a small paste will be OK in one go. */
2277 if (paste_len<256) {
0965bee0 2278 ldisc_send (paste_buffer, paste_len);
c9def1b8 2279 if (paste_buffer) sfree(paste_buffer);
2280 paste_buffer = 0;
2281 paste_pos = paste_hold = paste_len = 0;
2282 }
374330e2 2283 }
2284 get_clip(NULL, NULL);
2285 }
2286
2287 term_update();
2288}
2289
c9def1b8 2290void term_nopaste() {
2291 if(paste_len == 0) return;
2292 sfree(paste_buffer);
2293 paste_buffer = 0;
2294 paste_len = 0;
2295}
2296
2297void term_paste() {
8df7a775 2298 static long last_paste = 0;
c9def1b8 2299 long now, paste_diff;
2300
2301 if(paste_len == 0) return;
2302
2303 /* Don't wait forever to paste */
2304 if(paste_hold) {
2305 now = GetTickCount();
2306 paste_diff = now-last_paste;
2307 if (paste_diff>=0 && paste_diff<450)
2308 return;
2309 }
2310 paste_hold = 0;
2311
2312 while(paste_pos<paste_len)
2313 {
8df7a775 2314 int n = 0;
2315 while (n + paste_pos < paste_len) {
2316 if (paste_buffer[paste_pos + n++] == '\r')
2317 break;
2318 }
0965bee0 2319 ldisc_send (paste_buffer+paste_pos, n);
8df7a775 2320 paste_pos += n;
c9def1b8 2321
8df7a775 2322 if (paste_pos < paste_len) {
c9def1b8 2323 paste_hold = 1;
2324 return;
2325 }
2326 }
2327 sfree(paste_buffer);
2328 paste_buffer = 0;
2329 paste_len = 0;
2330}
2331
374330e2 2332static void deselect (void) {
2333 selstate = NO_SELECTION;
2334 selstart = selend = scrtop;
2335}
2336
2337void term_deselect (void) {
2338 deselect();
2339 term_update();
2340}
fe50e814 2341
0965bee0 2342int term_ldisc(int option) {
2343 if (option == LD_ECHO) return term_echoing;
2344 if (option == LD_EDIT) return term_editing;
2345 return FALSE;
2346}
2347
fe50e814 2348/*
2349 * from_backend(), to get data from the backend for the terminal.
2350 */
2351void from_backend(int is_stderr, char *data, int len) {
2352 while (len--) {
2353 if (inbuf_head >= INBUF_SIZE)
2354 term_out();
2355 inbuf[inbuf_head++] = *data++;
2356 }
2357}
e1c8e0ed 2358
2359/*
2360 * Log session traffic.
2361 */
2362void logtraffic(unsigned char c, int logmode) {
2363 if (cfg.logtype > 0) {
2364 if (cfg.logtype == logmode) {
2365 /* deferred open file from pgm start? */
2366 if (!lgfp) logfopen();
2367 if (lgfp) fputc (c, lgfp);
2368 }
2369 }
2370}
2371
2372/* open log file append/overwrite mode */
2373void logfopen(void) {
2374 char buf[256];
2375 time_t t;
2376 struct tm *tm;
2377 char writemod[4];
2378
2379 if (!cfg.logtype)
2380 return;
2381 sprintf (writemod, "wb"); /* default to rewrite */
2382 lgfp = fopen(cfg.logfilename, "r"); /* file already present? */
2383 if (lgfp) {
2384 int i;
2385 fclose(lgfp);
2386 i = askappend(cfg.logfilename);
2387 if (i == 1)
2388 writemod[0] = 'a'; /* set append mode */
2389 else if (i == 0) { /* cancelled */
2390 lgfp = NULL;
e4b0ba16 2391 cfg.logtype = 0; /* disable logging */
e1c8e0ed 2392 return;
2393 }
2394 }
2395
2396 lgfp = fopen(cfg.logfilename, writemod);
2397 if (lgfp) { /* enter into event log */
2398 sprintf(buf, "%s session log (%s mode) to file : ",
2399 (writemod[0] == 'a') ? "Appending" : "Writing new",
2400 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
2401 cfg.logtype == LGTYP_DEBUG ? "raw" : "<ukwn>") );
2402 /* Make sure we do not exceed the output buffer size */
2403 strncat (buf, cfg.logfilename, 128);
2404 buf[strlen(buf)] = '\0';
2405 logevent(buf);
2406
2407 /* --- write header line iinto log file */
2408 fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp);
2409 time(&t);
2410 tm = localtime(&t);
2411 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", tm);
2412 fputs (buf, lgfp);
2413 fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp);
2414 }
2415}
2416
2417void logfclose (void) {
2418 if (lgfp) { fclose(lgfp); lgfp = NULL; }
2419}