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