219119205e32d6036497540044a30f280811017f
[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_ANSIMIN (CL_ANSIMIN)
22 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
23 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
24 #define TM_VT102 (TM_VT100AVO|CL_VT102)
25 #define TM_VT220 (TM_VT102|CL_VT220)
26 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
27 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
28
29 #define TM_PUTTY (0xFFFF)
30
31 #define compatibility(x) \
32 if ( ((CL_##x)&compatibility_level) == 0 ) { \
33 termstate=TOPLEVEL; \
34 break; \
35 }
36 #define compatibility2(x,y) \
37 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
38 termstate=TOPLEVEL; \
39 break; \
40 }
41
42 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
43
44 static int compatibility_level = TM_PUTTY;
45
46
47 static unsigned long *text; /* buffer of text on terminal screen */
48 static unsigned long *scrtop; /* top of working screen */
49 static unsigned long *disptop; /* top of displayed screen */
50 static unsigned long *sbtop; /* top of scrollback */
51 static unsigned long *sbbot; /* furthest extent of scrollback */
52 static unsigned long *cpos; /* cursor position (convenience) */
53 static unsigned long *disptext; /* buffer of text on real screen */
54 static unsigned long *wanttext; /* buffer of text we want on screen */
55 static unsigned long *alttext; /* buffer of text on alt. screen */
56
57 static unsigned char *selspace; /* buffer for building selections in */
58
59 #define TSIZE (sizeof(*text))
60 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
61
62 static unsigned long curr_attr, save_attr;
63 static unsigned long erase_char = ERASE_CHAR;
64
65 static int curs_x, curs_y; /* cursor */
66 static int save_x, save_y; /* saved cursor position */
67 static int marg_t, marg_b; /* scroll margins */
68 static int dec_om; /* DEC origin mode flag */
69 static int wrap, wrapnext; /* wrap flags */
70 static int insert; /* insert-mode flag */
71 static int cset; /* 0 or 1: which char set */
72 static int save_cset, save_csattr; /* saved with cursor position */
73 static int rvideo; /* global reverse video flag */
74 static int cursor_on; /* cursor enabled flag */
75 static int reset_132; /* Flag ESC c resets to 80 cols */
76 static int use_bce; /* Use Background coloured erase */
77 static int blinker; /* When blinking is the cursor on ? */
78 static int tblinker; /* When the blinking text is on */
79 static int blink_is_real; /* Actually blink blinking text */
80
81 static unsigned long cset_attr[2];
82
83 /*
84 * Saved settings on the alternate screen.
85 */
86 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
87 static int alt_t, alt_b;
88 static int alt_which;
89
90 #define ARGS_MAX 32 /* max # of esc sequence arguments */
91 #define ARG_DEFAULT 0 /* if an arg isn't specified */
92 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
93 static int esc_args[ARGS_MAX];
94 static int esc_nargs;
95 static int esc_query;
96 #define ANSI(x,y) ((x)+((y)<<8))
97 #define ANSI_QUE(x) ANSI(x,TRUE)
98
99 #define OSC_STR_MAX 2048
100 static int osc_strlen;
101 static char osc_string[OSC_STR_MAX+1];
102 static int osc_w;
103
104 static char id_string[1024] = "\033[?6c";
105
106 static unsigned char *tabs;
107
108 static enum {
109 TOPLEVEL,
110 SEEN_ESC,
111 SEEN_CSI,
112 SEEN_OSC,
113 SEEN_OSC_W,
114
115 DO_CTRLS,
116
117 IGNORE_NEXT,
118 SET_GL, SET_GR,
119 SEEN_OSC_P,
120 OSC_STRING, OSC_MAYBE_ST,
121 SEEN_ESCHASH,
122 VT52_ESC,
123 VT52_Y1,
124 VT52_Y2
125 } termstate;
126
127 static enum {
128 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
129 } selstate;
130 static enum {
131 SM_CHAR, SM_WORD, SM_LINE
132 } selmode;
133 static unsigned long *selstart, *selend, *selanchor;
134
135 static short wordness[256] = {
136 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 */
137 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 */
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,2, /* 45 */
139 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 */
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, /* 89 */
141 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 */
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, /* CD */
143 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 */
144 };
145
146 static unsigned char sel_nl[] = SEL_NL;
147 static char * paste_buffer = 0;
148 static int paste_len, paste_pos, paste_hold;
149
150 /*
151 * Internal prototypes.
152 */
153 static void do_paint (Context, int);
154 static void erase_lots (int, int, int);
155 static void swap_screen (int);
156 static void update_sbar (void);
157 static void deselect (void);
158
159 /*
160 * Set up power-on settings for the terminal.
161 */
162 static void power_on(void) {
163 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
164 alt_t = marg_t = 0;
165 if (rows != -1)
166 alt_b = marg_b = rows - 1;
167 else
168 alt_b = marg_b = 0;
169 if (cols != -1) {
170 int i;
171 for (i = 0; i < cols; i++)
172 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
173 }
174 alt_om = dec_om = cfg.dec_om;
175 alt_wnext = wrapnext = alt_ins = insert = FALSE;
176 alt_wrap = wrap = cfg.wrap_mode;
177 alt_cset = cset = 0;
178 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
179 rvideo = 0;
180 cursor_on = 1;
181 save_attr = curr_attr = ATTR_DEFAULT;
182 app_cursor_keys = cfg.app_cursor;
183 app_keypad_keys = cfg.app_keypad;
184 use_bce = cfg.bce;
185 blink_is_real = cfg.blinktext;
186 erase_char = ERASE_CHAR;
187 alt_which = 0;
188 {
189 int i;
190 for (i = 0; i < 256; i++)
191 wordness[i] = cfg.wordness[i];
192 }
193 if (text) {
194 swap_screen (1);
195 erase_lots (FALSE, TRUE, TRUE);
196 swap_screen (0);
197 erase_lots (FALSE, TRUE, TRUE);
198 }
199 }
200
201 /*
202 * Force a screen update.
203 */
204 void term_update(void) {
205 Context ctx;
206 ctx = get_ctx();
207 if (ctx) {
208 if ( (seen_key_event && (cfg.scroll_on_key)) ||
209 (seen_disp_event && (!cfg.scroll_on_key)) ) {
210 disptop = scrtop;
211 seen_disp_event = seen_key_event = 0;
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 compatibility(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 compatibility(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 }
900 }
901 else switch (termstate) {
902 case TOPLEVEL:
903 /* Only graphic characters get this far, ctrls are stripped above */
904 if (wrapnext) {
905 cpos[1] |= ATTR_WRAPPED;
906 if (curs_y == marg_b)
907 scroll (marg_t, marg_b, 1, TRUE);
908 else if (curs_y < rows-1)
909 curs_y++;
910 curs_x = 0;
911 fix_cpos;
912 wrapnext = FALSE;
913 }
914 if (insert)
915 insch (1);
916 if (selstate != NO_SELECTION)
917 check_selection (cpos, cpos+1);
918 switch (cset_attr[cset]) {
919 /* Linedraw characters are different from 'ESC ( B' only
920 * for a small range, for ones outside that range make sure
921 * we use the same font as well as the same encoding.
922 */
923 case ATTR_LINEDRW:
924 if (c<0x60 || c>0x7F)
925 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
926 ATTR_ASCII;
927 else
928 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
929 break;
930 default:
931 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
932 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
933 break;
934 }
935 curs_x++;
936 if (curs_x == cols) {
937 cpos--;
938 curs_x--;
939 wrapnext = wrap;
940 }
941 seen_disp_event = 1;
942 break;
943
944 case IGNORE_NEXT:
945 termstate = TOPLEVEL;
946 break;
947 case OSC_MAYBE_ST:
948 /*
949 * This state is virtually identical to SEEN_ESC, with the
950 * exception that we have an OSC sequence in the pipeline,
951 * and _if_ we see a backslash, we process it.
952 */
953 if (c == '\\') {
954 do_osc();
955 termstate = TOPLEVEL;
956 break;
957 }
958 /* else fall through */
959 case SEEN_ESC:
960 termstate = TOPLEVEL;
961 switch (c) {
962 case ' ': /* some weird sequence? */
963 compatibility(VT220);
964 termstate = IGNORE_NEXT;
965 break;
966 case '[': /* enter CSI mode */
967 termstate = SEEN_CSI;
968 esc_nargs = 1;
969 esc_args[0] = ARG_DEFAULT;
970 esc_query = FALSE;
971 break;
972 case ']': /* xterm escape sequences */
973 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
974 compatibility(OTHER);
975 termstate = SEEN_OSC;
976 esc_args[0] = 0;
977 break;
978 case '(': /* should set GL */
979 compatibility(VT100);
980 termstate = SET_GL;
981 break;
982 case ')': /* should set GR */
983 compatibility(VT100);
984 termstate = SET_GR;
985 break;
986 case '7': /* save cursor */
987 compatibility(VT100);
988 save_cursor (TRUE);
989 break;
990 case '8': /* restore cursor */
991 compatibility(VT100);
992 save_cursor (FALSE);
993 seen_disp_event = TRUE;
994 break;
995 case '=':
996 compatibility(VT100);
997 app_keypad_keys = TRUE;
998 break;
999 case '>':
1000 compatibility(VT100);
1001 app_keypad_keys = FALSE;
1002 break;
1003 case 'D': /* exactly equivalent to LF */
1004 compatibility(VT100);
1005 if (curs_y == marg_b)
1006 scroll (marg_t, marg_b, 1, TRUE);
1007 else if (curs_y < rows-1)
1008 curs_y++;
1009 fix_cpos;
1010 wrapnext = FALSE;
1011 seen_disp_event = TRUE;
1012 break;
1013 case 'E': /* exactly equivalent to CR-LF */
1014 compatibility(VT100);
1015 curs_x = 0;
1016 if (curs_y == marg_b)
1017 scroll (marg_t, marg_b, 1, TRUE);
1018 else if (curs_y < rows-1)
1019 curs_y++;
1020 fix_cpos;
1021 wrapnext = FALSE;
1022 seen_disp_event = TRUE;
1023 break;
1024 case 'M': /* reverse index - backwards LF */
1025 compatibility(VT100);
1026 if (curs_y == marg_t)
1027 scroll (marg_t, marg_b, -1, TRUE);
1028 else if (curs_y > 0)
1029 curs_y--;
1030 fix_cpos;
1031 wrapnext = FALSE;
1032 seen_disp_event = TRUE;
1033 break;
1034 case 'Z': /* terminal type query */
1035 compatibility(VT100);
1036 ldisc->send (id_string, strlen(id_string));
1037 break;
1038 case 'c': /* restore power-on settings */
1039 compatibility(VT100);
1040 power_on();
1041 if (reset_132) {
1042 request_resize (80, rows, 1);
1043 reset_132 = 0;
1044 }
1045 fix_cpos;
1046 disptop = scrtop;
1047 seen_disp_event = TRUE;
1048 break;
1049 case '#': /* ESC # 8 fills screen with Es :-) */
1050 compatibility(VT100);
1051 termstate = SEEN_ESCHASH;
1052 break;
1053 case 'H': /* set a tab */
1054 compatibility(VT100);
1055 tabs[curs_x] = TRUE;
1056 break;
1057 }
1058 break;
1059 case SEEN_CSI:
1060 termstate = TOPLEVEL; /* default */
1061 if( isdigit(c) )
1062 {
1063 if (esc_nargs <= ARGS_MAX) {
1064 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1065 esc_args[esc_nargs-1] = 0;
1066 esc_args[esc_nargs-1] =
1067 10 * esc_args[esc_nargs-1] + c - '0';
1068 }
1069 termstate = SEEN_CSI;
1070 }
1071 else if( c == ';' )
1072 {
1073 if (++esc_nargs <= ARGS_MAX)
1074 esc_args[esc_nargs-1] = ARG_DEFAULT;
1075 termstate = SEEN_CSI;
1076 }
1077 else if( c < '@' )
1078 {
1079 if( esc_query ) esc_query = -1;
1080 else if( c == '?' ) esc_query = TRUE;
1081 else esc_query = c;
1082 termstate = SEEN_CSI;
1083 }
1084 else switch (ANSI(c,esc_query)) {
1085 case 'A': /* move up N lines */
1086 move (curs_x, curs_y - def(esc_args[0], 1), 1);
1087 seen_disp_event = TRUE;
1088 break;
1089 case 'e': /* move down N lines */
1090 compatibility(ANSI);
1091 case 'B':
1092 move (curs_x, curs_y + def(esc_args[0], 1), 1);
1093 seen_disp_event = TRUE;
1094 break;
1095 case 'a': /* move right N cols */
1096 compatibility(ANSI);
1097 case 'C':
1098 move (curs_x + def(esc_args[0], 1), curs_y, 1);
1099 seen_disp_event = TRUE;
1100 break;
1101 case 'D': /* move left N cols */
1102 move (curs_x - def(esc_args[0], 1), curs_y, 1);
1103 seen_disp_event = TRUE;
1104 break;
1105 case 'E': /* move down N lines and CR */
1106 compatibility(ANSI);
1107 move (0, curs_y + def(esc_args[0], 1), 1);
1108 seen_disp_event = TRUE;
1109 break;
1110 case 'F': /* move up N lines and CR */
1111 compatibility(ANSI);
1112 move (0, curs_y - def(esc_args[0], 1), 1);
1113 seen_disp_event = TRUE;
1114 break;
1115 case 'G': case '`': /* set horizontal posn */
1116 compatibility(ANSI);
1117 move (def(esc_args[0], 1) - 1, curs_y, 0);
1118 seen_disp_event = TRUE;
1119 break;
1120 case 'd': /* set vertical posn */
1121 compatibility(ANSI);
1122 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1123 (dec_om ? 2 : 0));
1124 seen_disp_event = TRUE;
1125 break;
1126 case 'H': case 'f': /* set horz and vert posns at once */
1127 if (esc_nargs < 2)
1128 esc_args[1] = ARG_DEFAULT;
1129 move (def(esc_args[1], 1) - 1,
1130 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1131 (dec_om ? 2 : 0));
1132 seen_disp_event = TRUE;
1133 break;
1134 case 'J': /* erase screen or parts of it */
1135 {
1136 unsigned int i = def(esc_args[0], 0) + 1;
1137 if (i > 3)
1138 i = 0;
1139 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1140 }
1141 disptop = scrtop;
1142 seen_disp_event = TRUE;
1143 break;
1144 case 'K': /* erase line or parts of it */
1145 {
1146 unsigned int i = def(esc_args[0], 0) + 1;
1147 if (i > 3)
1148 i = 0;
1149 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1150 }
1151 seen_disp_event = TRUE;
1152 break;
1153 case 'L': /* insert lines */
1154 compatibility(VT102);
1155 if (curs_y <= marg_b)
1156 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1157 seen_disp_event = TRUE;
1158 break;
1159 case 'M': /* delete lines */
1160 compatibility(VT102);
1161 if (curs_y <= marg_b)
1162 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1163 seen_disp_event = TRUE;
1164 break;
1165 case '@': /* insert chars */
1166 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1167 compatibility(VT102);
1168 insch (def(esc_args[0], 1));
1169 seen_disp_event = TRUE;
1170 break;
1171 case 'P': /* delete chars */
1172 compatibility(VT102);
1173 insch (-def(esc_args[0], 1));
1174 seen_disp_event = TRUE;
1175 break;
1176 case 'c': /* terminal type query */
1177 compatibility(VT100);
1178 /* This is the response for a VT102 */
1179 ldisc->send (id_string, strlen(id_string));
1180 break;
1181 case 'n': /* cursor position query */
1182 if (esc_args[0] == 6) {
1183 char buf[32];
1184 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1185 ldisc->send (buf, strlen(buf));
1186 }
1187 else if (esc_args[0] == 5) {
1188 ldisc->send ("\033[0n", 4);
1189 }
1190 break;
1191 case 'h': /* toggle modes to high */
1192 case ANSI_QUE('h'):
1193 compatibility(VT100);
1194 {
1195 int i;
1196 for (i=0; i<esc_nargs; i++)
1197 toggle_mode (esc_args[i], esc_query, TRUE);
1198 }
1199 break;
1200 case 'l': /* toggle modes to low */
1201 case ANSI_QUE('l'):
1202 compatibility(VT100);
1203 {
1204 int i;
1205 for (i=0; i<esc_nargs; i++)
1206 toggle_mode (esc_args[i], esc_query, FALSE);
1207 }
1208 break;
1209 case 'g': /* clear tabs */
1210 compatibility(VT100);
1211 if (esc_nargs == 1) {
1212 if (esc_args[0] == 0) {
1213 tabs[curs_x] = FALSE;
1214 } else if (esc_args[0] == 3) {
1215 int i;
1216 for (i = 0; i < cols; i++)
1217 tabs[i] = FALSE;
1218 }
1219 }
1220 break;
1221 case 'r': /* set scroll margins */
1222 compatibility(VT100);
1223 if (esc_nargs <= 2) {
1224 int top, bot;
1225 top = def(esc_args[0], 1) - 1;
1226 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1227 def(esc_args[1], rows)) - 1;
1228 if (bot >= rows)
1229 bot = rows-1;
1230 /* VTTEST Bug 9 - if region is less than 2 lines
1231 * don't change region.
1232 */
1233 if (bot-top > 1) {
1234 marg_t = top;
1235 marg_b = bot;
1236 curs_x = 0;
1237 /*
1238 * I used to think the cursor should be
1239 * placed at the top of the newly marginned
1240 * area. Apparently not: VMS TPU falls over
1241 * if so.
1242 *
1243 * Well actually it should for Origin mode - RDB
1244 */
1245 curs_y = (dec_om ? marg_t : 0);
1246 fix_cpos;
1247 seen_disp_event = TRUE;
1248 }
1249 }
1250 break;
1251 case 'm': /* set graphics rendition */
1252 {
1253 /*
1254 * A VT100 without the AVO only had one attribute, either
1255 * underline or reverse video depending on the cursor type,
1256 * this was selected by CSI 7m.
1257 *
1258 * case 2:
1259 * This is DIM on the VT100-AVO and VT102
1260 * case 5:
1261 * This is BLINK on the VT100-AVO and VT102+
1262 * case 8:
1263 * This is INVIS on the VT100-AVO and VT102
1264 * case 21:
1265 * This like 22 disables BOLD, DIM and INVIS
1266 *
1267 * The ANSI colours appear on any terminal that has colour
1268 * (obviously) but the interaction between sgr0 and the
1269 * colours varies but is usually related to the background
1270 * colour erase item.
1271 * The interaction between colour attributes and the mono
1272 * ones is also very implementation dependent.
1273 *
1274 * The 39 and 49 attributes are likely to be unimplemented.
1275 */
1276 int i;
1277 for (i=0; i<esc_nargs; i++) {
1278 switch (def(esc_args[i], 0)) {
1279 case 0: /* restore defaults */
1280 curr_attr = ATTR_DEFAULT; break;
1281 case 1: /* enable bold */
1282 compatibility(VT100AVO);
1283 curr_attr |= ATTR_BOLD; break;
1284 case 21: /* (enable double underline) */
1285 compatibility(OTHER);
1286 case 4: /* enable underline */
1287 compatibility(VT100AVO);
1288 curr_attr |= ATTR_UNDER; break;
1289 case 5: /* enable blink */
1290 compatibility(VT100AVO);
1291 curr_attr |= ATTR_BLINK; break;
1292 case 7: /* enable reverse video */
1293 curr_attr |= ATTR_REVERSE; break;
1294 case 22: /* disable bold */
1295 compatibility(VT220);
1296 curr_attr &= ~ATTR_BOLD; break;
1297 case 24: /* disable underline */
1298 compatibility(VT220);
1299 curr_attr &= ~ATTR_UNDER; break;
1300 case 25: /* disable blink */
1301 compatibility(VT220);
1302 curr_attr &= ~ATTR_BLINK; break;
1303 case 27: /* disable reverse video */
1304 compatibility(VT220);
1305 curr_attr &= ~ATTR_REVERSE; break;
1306 case 30: case 31: case 32: case 33:
1307 case 34: case 35: case 36: case 37:
1308 /* foreground */
1309 curr_attr &= ~ATTR_FGMASK;
1310 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1311 break;
1312 case 39: /* default-foreground */
1313 curr_attr &= ~ATTR_FGMASK;
1314 curr_attr |= ATTR_DEFFG;
1315 break;
1316 case 40: case 41: case 42: case 43:
1317 case 44: case 45: case 46: case 47:
1318 /* background */
1319 curr_attr &= ~ATTR_BGMASK;
1320 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1321 break;
1322 case 49: /* default-background */
1323 curr_attr &= ~ATTR_BGMASK;
1324 curr_attr |= ATTR_DEFBG;
1325 break;
1326 }
1327 }
1328 if (use_bce)
1329 erase_char =
1330 (' '|
1331 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1332 );
1333 }
1334 break;
1335 case 's': /* save cursor */
1336 save_cursor (TRUE);
1337 break;
1338 case 'u': /* restore cursor */
1339 save_cursor (FALSE);
1340 seen_disp_event = TRUE;
1341 break;
1342 case 't': /* set page size - ie window height */
1343 /*
1344 * VT340/VT420 sequence DECSLPP, DEC only allows values
1345 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1346 * illegal values (eg first arg 1..9) for window changing
1347 * and reports.
1348 */
1349 compatibility(VT340TEXT);
1350 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1351 request_resize (cols, def(esc_args[0], 24), 0);
1352 deselect();
1353 }
1354 break;
1355 case ANSI('|', '*'):
1356 /* VT420 sequence DECSNLS
1357 * Set number of lines on screen
1358 * VT420 uses VGA like hardware and can support any size in
1359 * reasonable range (24..49 AIUI) with no default specified.
1360 */
1361 compatibility(VT420);
1362 if (esc_nargs==1 && esc_args[0]>0) {
1363 request_resize (cols, def(esc_args[0], cfg.height), 0);
1364 deselect();
1365 }
1366 break;
1367 case ANSI('|', '$'):
1368 /* VT340/VT420 sequence DECSCPP
1369 * Set number of columns per page
1370 * Docs imply range is only 80 or 132, but I'll allow any.
1371 */
1372 compatibility(VT340TEXT);
1373 if (esc_nargs<=1) {
1374 request_resize (def(esc_args[0], cfg.width), rows, 0);
1375 deselect();
1376 }
1377 break;
1378 case 'X': /* write N spaces w/o moving cursor */
1379 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1380 compatibility(ANSIMIN);
1381 {
1382 int n = def(esc_args[0], 1);
1383 unsigned long *p = cpos;
1384 if (n > cols - curs_x)
1385 n = cols - curs_x;
1386 check_selection (cpos, cpos+n);
1387 while (n--)
1388 *p++ = erase_char;
1389 seen_disp_event = TRUE;
1390 }
1391 break;
1392 case 'x': /* report terminal characteristics */
1393 compatibility(VT100);
1394 {
1395 char buf[32];
1396 int i = def(esc_args[0], 0);
1397 if (i == 0 || i == 1) {
1398 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1399 buf[2] += i;
1400 ldisc->send (buf, 20);
1401 }
1402 }
1403 break;
1404 case ANSI('L','='):
1405 compatibility(OTHER);
1406 use_bce = (esc_args[0]<=0);
1407 erase_char = ERASE_CHAR;
1408 if (use_bce)
1409 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1410 break;
1411 case ANSI('E','='):
1412 compatibility(OTHER);
1413 blink_is_real = (esc_args[0]>=1);
1414 break;
1415 case ANSI('p','"'):
1416 /* Allow the host to make this emulator a 'perfect' VT102.
1417 * This first appeared in the VT220, but we do need to get
1418 * back to PuTTY mode so I won't check it.
1419 *
1420 * The arg == 60 is a PuTTY extension.
1421 * The 2nd arg, 8bit vs 7bit is not obeyed.
1422 *
1423 * Setting VT102 mode should also change the Fkeys to
1424 * generate PF* codes as a real VT102 has no Fkeys.
1425 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1426 * send nothing.
1427 *
1428 * Note ESC c will NOT change this!
1429 */
1430
1431 if (esc_args[0] == 61) compatibility_level = TM_VT102;
1432 else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
1433 else compatibility_level = TM_PUTTY;
1434 break;
1435 }
1436 break;
1437 case SET_GL:
1438 case SET_GR:
1439 /* VT100 only here, checked above */
1440 switch (c) {
1441 case 'A':
1442 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1443 break;
1444 case '0':
1445 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1446 break;
1447 case 'B':
1448 default: /* specifically, 'B' */
1449 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1450 break;
1451 }
1452 if( !has_compat(VT220) || c != '%' )
1453 termstate = TOPLEVEL;
1454 break;
1455 case SEEN_OSC:
1456 osc_w = FALSE;
1457 switch (c) {
1458 case 'P': /* Linux palette sequence */
1459 termstate = SEEN_OSC_P;
1460 osc_strlen = 0;
1461 break;
1462 case 'R': /* Linux palette reset */
1463 palette_reset();
1464 term_invalidate();
1465 termstate = TOPLEVEL;
1466 break;
1467 case 'W': /* word-set */
1468 termstate = SEEN_OSC_W;
1469 osc_w = TRUE;
1470 break;
1471 case '0': case '1': case '2': case '3': case '4':
1472 case '5': case '6': case '7': case '8': case '9':
1473 esc_args[0] = 10 * esc_args[0] + c - '0';
1474 break;
1475 case 'L':
1476 /*
1477 * Grotty hack to support xterm and DECterm title
1478 * sequences concurrently.
1479 */
1480 if (esc_args[0] == 2) {
1481 esc_args[0] = 1;
1482 break;
1483 }
1484 /* else fall through */
1485 default:
1486 termstate = OSC_STRING;
1487 osc_strlen = 0;
1488 }
1489 break;
1490 case OSC_STRING:
1491 /*
1492 * This OSC stuff is EVIL. It takes just one character to get into
1493 * sysline mode and it's not initially obvious how to get out.
1494 * So I've added CR and LF as string aborts.
1495 * This shouldn't effect compatibility as I believe embedded
1496 * control characters are supposed to be interpreted (maybe?)
1497 * and they don't display anything useful anyway.
1498 *
1499 * -- RDB
1500 */
1501 if (c == '\n' || c == '\r') {
1502 termstate = TOPLEVEL;
1503 } else if (c == 0234 || c == '\007' ) {
1504 /*
1505 * These characters terminate the string; ST and BEL
1506 * terminate the sequence and trigger instant
1507 * processing of it, whereas ESC goes back to SEEN_ESC
1508 * mode unless it is followed by \, in which case it is
1509 * synonymous with ST in the first place.
1510 */
1511 do_osc();
1512 termstate = TOPLEVEL;
1513 } else if (c == '\033')
1514 termstate = OSC_MAYBE_ST;
1515 else if (osc_strlen < OSC_STR_MAX)
1516 osc_string[osc_strlen++] = c;
1517 break;
1518 case SEEN_OSC_P:
1519 {
1520 int max = (osc_strlen == 0 ? 21 : 16);
1521 int val;
1522 if (c >= '0' && c <= '9')
1523 val = c - '0';
1524 else if (c >= 'A' && c <= 'A'+max-10)
1525 val = c - 'A' + 10;
1526 else if (c >= 'a' && c <= 'a'+max-10)
1527 val = c - 'a' + 10;
1528 else
1529 termstate = TOPLEVEL;
1530 osc_string[osc_strlen++] = val;
1531 if (osc_strlen >= 7) {
1532 palette_set (osc_string[0],
1533 osc_string[1] * 16 + osc_string[2],
1534 osc_string[3] * 16 + osc_string[4],
1535 osc_string[5] * 16 + osc_string[6]);
1536 term_invalidate();
1537 termstate = TOPLEVEL;
1538 }
1539 }
1540 break;
1541 case SEEN_OSC_W:
1542 switch (c) {
1543 case '0': case '1': case '2': case '3': case '4':
1544 case '5': case '6': case '7': case '8': case '9':
1545 esc_args[0] = 10 * esc_args[0] + c - '0';
1546 break;
1547 default:
1548 termstate = OSC_STRING;
1549 osc_strlen = 0;
1550 }
1551 break;
1552 case SEEN_ESCHASH:
1553 {
1554 unsigned long *p;
1555 unsigned long nlattr;
1556 int n;
1557
1558 switch (c) {
1559 case '8':
1560 p = scrtop;
1561 n = rows * (cols+1);
1562 while (n--)
1563 *p++ = ATTR_DEFAULT | 'E';
1564 disptop = scrtop;
1565 seen_disp_event = TRUE;
1566 check_selection (scrtop, scrtop + rows * (cols+1));
1567 break;
1568
1569 case '3': nlattr = LATTR_TOP; if(0) {
1570 case '4': nlattr = LATTR_BOT; } if(0) {
1571 case '5': nlattr = LATTR_NORM; } if(0) {
1572 case '6': nlattr = LATTR_WIDE; }
1573
1574 p = scrtop + curs_y * (cols+1) + cols;
1575 *p &= ~LATTR_MODE;
1576 *p |= nlattr;
1577 }
1578 }
1579 termstate = TOPLEVEL;
1580 break;
1581 case VT52_ESC:
1582 termstate = TOPLEVEL;
1583 seen_disp_event = TRUE;
1584 switch (c) {
1585 case 'A':
1586 move (curs_x, curs_y - 1, 1);
1587 break;
1588 case 'B':
1589 move (curs_x, curs_y + 1, 1);
1590 break;
1591 case 'C':
1592 move (curs_x + 1, curs_y, 1);
1593 break;
1594 case 'D':
1595 move (curs_x - 1, curs_y, 1);
1596 break;
1597 case 'F':
1598 cset_attr[cset=0] = ATTR_LINEDRW;
1599 break;
1600 case 'G':
1601 cset_attr[cset=0] = ATTR_ASCII;
1602 break;
1603 case 'H':
1604 move (0, 0, 0);
1605 break;
1606 case 'I':
1607 if (curs_y == 0)
1608 scroll (0, rows-1, -1, TRUE);
1609 else if (curs_y > 0)
1610 curs_y--;
1611 fix_cpos;
1612 wrapnext = FALSE;
1613 break;
1614 case 'J':
1615 erase_lots(FALSE, FALSE, TRUE);
1616 disptop = scrtop;
1617 break;
1618 case 'K':
1619 erase_lots(TRUE, FALSE, TRUE);
1620 break;
1621 case 'V':
1622 /* XXX Print cursor line */
1623 break;
1624 case 'W':
1625 /* XXX Start controller mode */
1626 break;
1627 case 'X':
1628 /* XXX Stop controller mode */
1629 break;
1630 case 'Y':
1631 termstate = VT52_Y1;
1632 break;
1633 case 'Z':
1634 ldisc->send ("\033/Z", 3);
1635 break;
1636 case '=':
1637 app_cursor_keys = TRUE;
1638 break;
1639 case '>':
1640 app_cursor_keys = FALSE;
1641 break;
1642 case '<':
1643 /* XXX This should switch to VT100 mode not current or default
1644 * VT mode. But this will only have effect in a VT220+
1645 * emulation.
1646 */
1647 vt52_mode = FALSE;
1648 break;
1649 case '^':
1650 /* XXX Enter auto print mode */
1651 break;
1652 case '_':
1653 /* XXX Exit auto print mode */
1654 break;
1655 case ']':
1656 /* XXX Print screen */
1657 break;
1658 }
1659 break;
1660 case VT52_Y1:
1661 termstate = VT52_Y2;
1662 move(curs_x, c-' ', 0);
1663 break;
1664 case VT52_Y2:
1665 termstate = TOPLEVEL;
1666 move(c-' ', curs_y, 0);
1667 break;
1668 }
1669 if (selstate != NO_SELECTION)
1670 check_selection (cpos, cpos+1);
1671 }
1672 inbuf_head = 0;
1673
1674 if (beep_overload)
1675 {
1676 if(!beep_count) beep_overload=0;
1677 }
1678 else if(beep_count && beep_count<5 && cfg.beep)
1679 beep(beep_count/3);
1680 }
1681
1682 /*
1683 * Compare two lines to determine whether they are sufficiently
1684 * alike to scroll-optimise one to the other. Return the degree of
1685 * similarity.
1686 */
1687 static int linecmp (unsigned long *a, unsigned long *b) {
1688 int i, n;
1689
1690 for (i=n=0; i < cols; i++)
1691 n += (*a++ == *b++);
1692 return n;
1693 }
1694
1695 /*
1696 * Given a context, update the window. Out of paranoia, we don't
1697 * allow WM_PAINT responses to do scrolling optimisations.
1698 */
1699 static void do_paint (Context ctx, int may_optimise){
1700 int i, j, start, our_curs_y;
1701 unsigned long attr, rv, cursor;
1702 char ch[1024];
1703
1704 if (cursor_on) {
1705 if (has_focus) {
1706 if (blinker || !cfg.blink_cur)
1707 cursor = ATTR_ACTCURS;
1708 else
1709 cursor = 0;
1710 }
1711 else
1712 cursor = ATTR_PASCURS;
1713 }
1714 else cursor = 0;
1715 rv = (rvideo ? ATTR_REVERSE : 0);
1716 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1717
1718 for (i=0; i<rows; i++) {
1719 int idx = i*(cols+1);
1720 int lattr = (disptop[idx+cols] & LATTR_MODE);
1721 for (j=0; j<=cols; j++,idx++) {
1722 unsigned long *d = disptop+idx;
1723 wanttext[idx] = lattr | ((*d ^ rv
1724 ^ (selstart <= d && d < selend ?
1725 ATTR_REVERSE : 0)) |
1726 (i==our_curs_y && j==curs_x ? cursor : 0));
1727
1728 if (blink_is_real) {
1729 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1730 {
1731 wanttext[idx] &= ATTR_MASK;
1732 wanttext[idx] += ' ';
1733 }
1734 wanttext[idx] &= ~ATTR_BLINK;
1735 }
1736 }
1737 }
1738
1739 /*
1740 * We would perform scrolling optimisations in here, if they
1741 * didn't have a nasty tendency to cause the whole sodding
1742 * program to hang for a second at speed-critical moments.
1743 * We'll leave it well alone...
1744 */
1745
1746 for (i=0; i<rows; i++) {
1747 int idx = i*(cols+1);
1748 int lattr = (wanttext[idx+cols] & LATTR_MODE);
1749 start = -1;
1750 for (j=0; j<=cols; j++,idx++) {
1751 unsigned long t = wanttext[idx];
1752 int needs_update = (j < cols && t != disptext[idx]);
1753 int keep_going = (start != -1 && needs_update &&
1754 (t & ATTR_MASK) == attr &&
1755 j-start < sizeof(ch));
1756 if (start != -1 && !keep_going) {
1757 do_text (ctx, start, i, ch, j-start, attr, lattr);
1758 start = -1;
1759 }
1760 if (needs_update) {
1761 if (start == -1) {
1762 start = j;
1763 attr = t & ATTR_MASK;
1764 }
1765 ch[j-start] = (char) (t & CHAR_MASK);
1766 }
1767 disptext[idx] = t;
1768 }
1769 }
1770 }
1771
1772 /*
1773 * Flick the switch that says if blinking things should be shown or hidden.
1774 */
1775
1776 void term_blink(int flg) {
1777 static long last_blink = 0;
1778 static long last_tblink = 0;
1779 long now, blink_diff;
1780
1781 now = GetTickCount();
1782 blink_diff = now-last_tblink;
1783
1784 /* Make sure the text blinks no more than 2Hz */
1785 if (blink_diff<0 || blink_diff>450)
1786 {
1787 last_tblink = now;
1788 tblinker = !tblinker;
1789 }
1790
1791 if (flg) {
1792 blinker = 1;
1793 last_blink = now;
1794 return;
1795 }
1796
1797 blink_diff = now-last_blink;
1798
1799 /* Make sure the cursor blinks no more than 2Hz */
1800 if (blink_diff>=0 && blink_diff<450)
1801 return;
1802
1803 last_blink = now;
1804 blinker = !blinker;
1805 }
1806
1807 /*
1808 * Invalidate the whole screen so it will be repainted in full.
1809 */
1810 void term_invalidate(void) {
1811 int i;
1812
1813 for (i=0; i<rows*(cols+1); i++)
1814 disptext[i] = ATTR_INVALID;
1815 }
1816
1817 /*
1818 * Paint the window in response to a WM_PAINT message.
1819 */
1820 void term_paint (Context ctx, int l, int t, int r, int b) {
1821 int i, j, left, top, right, bottom;
1822
1823 left = l / font_width;
1824 right = (r - 1) / font_width;
1825 top = t / font_height;
1826 bottom = (b - 1) / font_height;
1827 for (i = top; i <= bottom && i < rows ; i++)
1828 {
1829 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
1830 for (j = left; j <= right && j < cols ; j++)
1831 disptext[i*(cols+1)+j] = ATTR_INVALID;
1832 else
1833 for (j = left/2; j <= right/2+1 && j < cols ; j++)
1834 disptext[i*(cols+1)+j] = ATTR_INVALID;
1835 }
1836
1837 /* This should happen soon enough, also for some reason it sometimes
1838 * fails to actually do anything when re-sizing ... painting the wrong
1839 * window perhaps ?
1840 do_paint (ctx, FALSE);
1841 */
1842 }
1843
1844 /*
1845 * Attempt to scroll the scrollback. The second parameter gives the
1846 * position we want to scroll to; the first is +1 to denote that
1847 * this position is relative to the beginning of the scrollback, -1
1848 * to denote it is relative to the end, and 0 to denote that it is
1849 * relative to the current position.
1850 */
1851 void term_scroll (int rel, int where) {
1852 int n = where * (cols+1);
1853
1854 disptop = (rel < 0 ? scrtop :
1855 rel > 0 ? sbtop : disptop) + n;
1856 if (disptop < sbtop)
1857 disptop = sbtop;
1858 if (disptop > scrtop)
1859 disptop = scrtop;
1860 update_sbar();
1861 term_update();
1862 }
1863
1864 /*
1865 * Spread the selection outwards according to the selection mode.
1866 */
1867 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1868 unsigned long *linestart, *lineend;
1869 int x;
1870 short wvalue;
1871
1872 x = (p - text) % (cols+1);
1873 linestart = p - x;
1874 lineend = linestart + cols;
1875
1876 switch (selmode) {
1877 case SM_CHAR:
1878 /*
1879 * In this mode, every character is a separate unit, except
1880 * for runs of spaces at the end of a non-wrapping line.
1881 */
1882 if (!(linestart[cols] & ATTR_WRAPPED)) {
1883 unsigned long *q = lineend;
1884 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1885 q--;
1886 if (q == lineend)
1887 q--;
1888 if (p >= q)
1889 p = (dir == -1 ? q : lineend - 1);
1890 }
1891 break;
1892 case SM_WORD:
1893 /*
1894 * In this mode, the units are maximal runs of characters
1895 * whose `wordness' has the same value.
1896 */
1897 wvalue = wordness[*p & CHAR_MASK];
1898 if (dir == +1) {
1899 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1900 p++;
1901 } else {
1902 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1903 p--;
1904 }
1905 break;
1906 case SM_LINE:
1907 /*
1908 * In this mode, every line is a unit.
1909 */
1910 p = (dir == -1 ? linestart : lineend - 1);
1911 break;
1912 }
1913 return p;
1914 }
1915
1916 static void sel_spread (void) {
1917 selstart = sel_spread_half (selstart, -1);
1918 selend = sel_spread_half (selend - 1, +1) + 1;
1919 }
1920
1921 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1922 unsigned long *selpoint;
1923
1924 if (y<0) y = 0;
1925 if (y>=rows) y = rows-1;
1926 if (x<0) {
1927 if (y > 0) {
1928 x = cols-1;
1929 y--;
1930 } else
1931 x = 0;
1932 }
1933 if (x>=cols) x = cols-1;
1934
1935 selpoint = disptop + y * (cols+1);
1936 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
1937 selpoint += x/2;
1938 else
1939 selpoint += x;
1940
1941 if (b == MB_SELECT && a == MA_CLICK) {
1942 deselect();
1943 selstate = ABOUT_TO;
1944 selanchor = selpoint;
1945 selmode = SM_CHAR;
1946 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1947 deselect();
1948 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1949 selstate = DRAGGING;
1950 selstart = selanchor = selpoint;
1951 selend = selstart + 1;
1952 sel_spread();
1953 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1954 (b == MB_EXTEND && a != MA_RELEASE)) {
1955 if (selstate == ABOUT_TO && selanchor == selpoint)
1956 return;
1957 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1958 if (selpoint-selstart < (selend-selstart)/2)
1959 selanchor = selend - 1;
1960 else
1961 selanchor = selstart;
1962 selstate = DRAGGING;
1963 }
1964 if (selstate != ABOUT_TO && selstate != DRAGGING)
1965 selanchor = selpoint;
1966 selstate = DRAGGING;
1967 if (selpoint < selanchor) {
1968 selstart = selpoint;
1969 selend = selanchor + 1;
1970 } else {
1971 selstart = selanchor;
1972 selend = selpoint + 1;
1973 }
1974 sel_spread();
1975 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1976 if (selstate == DRAGGING) {
1977 /*
1978 * We've completed a selection. We now transfer the
1979 * data to the clipboard.
1980 */
1981 unsigned char *p = selspace;
1982 unsigned long *q = selstart;
1983
1984 while (q < selend) {
1985 int nl = FALSE;
1986 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1987 unsigned long *nlpos = lineend;
1988
1989 if (!(*nlpos & ATTR_WRAPPED)) {
1990 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1991 nlpos--;
1992 if (nlpos < selend)
1993 nl = TRUE;
1994 }
1995 while (q < nlpos && q < selend)
1996 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1997 if (nl) {
1998 int i;
1999 for (i=0; i<sizeof(sel_nl); i++)
2000 *p++ = sel_nl[i];
2001 }
2002 q = lineend + 1; /* start of next line */
2003 }
2004 write_clip (selspace, p - selspace, FALSE);
2005 selstate = SELECTED;
2006 } else
2007 selstate = NO_SELECTION;
2008 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2009 char *data;
2010 int len;
2011
2012 get_clip((void **) &data, &len);
2013 if (data) {
2014 char *p, *q;
2015
2016 if (paste_buffer) sfree(paste_buffer);
2017 paste_pos = paste_hold = paste_len = 0;
2018 paste_buffer = smalloc(len);
2019
2020 p = q = data;
2021 while (p < data+len) {
2022 while (p < data+len &&
2023 !(p <= data+len-sizeof(sel_nl) &&
2024 !memcmp(p, sel_nl, sizeof(sel_nl))))
2025 p++;
2026
2027 {
2028 int i;
2029 unsigned char c;
2030 for(i=0;i<p-q;i++)
2031 {
2032 c=xlat_kbd2tty(q[i]);
2033 paste_buffer[paste_len++] = c;
2034 }
2035 }
2036
2037 if (p <= data+len-sizeof(sel_nl) &&
2038 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2039 paste_buffer[paste_len++] = '\r';
2040 p += sizeof(sel_nl);
2041 }
2042 q = p;
2043 }
2044
2045 /* Assume a small paste will be OK in one go. */
2046 if (paste_len<256) {
2047 ldisc->send (paste_buffer, paste_len);
2048 if (paste_buffer) sfree(paste_buffer);
2049 paste_buffer = 0;
2050 paste_pos = paste_hold = paste_len = 0;
2051 }
2052 }
2053 get_clip(NULL, NULL);
2054 }
2055
2056 term_update();
2057 }
2058
2059 void term_nopaste() {
2060 if(paste_len == 0) return;
2061 sfree(paste_buffer);
2062 paste_buffer = 0;
2063 paste_len = 0;
2064 }
2065
2066 void term_paste() {
2067 static long last_paste = 0;
2068 long now, paste_diff;
2069
2070 if(paste_len == 0) return;
2071
2072 /* Don't wait forever to paste */
2073 if(paste_hold) {
2074 now = GetTickCount();
2075 paste_diff = now-last_paste;
2076 if (paste_diff>=0 && paste_diff<450)
2077 return;
2078 }
2079 paste_hold = 0;
2080
2081 while(paste_pos<paste_len)
2082 {
2083 char c = paste_buffer[paste_pos++];
2084 ldisc->send (&c, 1);
2085
2086 if (c =='\r') {
2087 paste_hold = 1;
2088 return;
2089 }
2090 }
2091 sfree(paste_buffer);
2092 paste_buffer = 0;
2093 paste_len = 0;
2094 }
2095
2096 static void deselect (void) {
2097 selstate = NO_SELECTION;
2098 selstart = selend = scrtop;
2099 }
2100
2101 void term_deselect (void) {
2102 deselect();
2103 term_update();
2104 }