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