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