Peter Schellenbach's patch: re-implement the PuTTY cryptographic
[u/mdw/putty] / terminal.c
CommitLineData
374330e2 1#include <windows.h>
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#include "putty.h"
7
e14a5a13 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
34static int compatibility_level = TM_PUTTY;
35
36
374330e2 37static unsigned long *text; /* buffer of text on terminal screen */
38static unsigned long *scrtop; /* top of working screen */
39static unsigned long *disptop; /* top of displayed screen */
40static unsigned long *sbtop; /* top of scrollback */
41static unsigned long *cpos; /* cursor position (convenience) */
42static unsigned long *disptext; /* buffer of text on real screen */
43static unsigned long *wanttext; /* buffer of text we want on screen */
44static unsigned long *alttext; /* buffer of text on alt. screen */
45
46static 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
51static unsigned long curr_attr, save_attr;
e14a5a13 52static unsigned long erase_char = ERASE_CHAR;
374330e2 53
54static int curs_x, curs_y; /* cursor */
55static int save_x, save_y; /* saved cursor position */
56static int marg_t, marg_b; /* scroll margins */
57static int dec_om; /* DEC origin mode flag */
58static int wrap, wrapnext; /* wrap flags */
59static int insert; /* insert-mode flag */
60static int cset; /* 0 or 1: which char set */
61static int save_cset, save_csattr; /* saved with cursor position */
62static int rvideo; /* global reverse video flag */
e14a5a13 63static int cursor_on; /* cursor enabled flag */
64static int reset_132; /* Flag ESC c resets to 80 cols */
65static int use_bce; /* Use Background coloured erase */
66static int blinker; /* When blinking is the cursor on ? */
67static int vt52_mode; /* Is VT100+ in vt52 mode ? */
374330e2 68
69static unsigned long cset_attr[2];
70
71/*
72 * Saved settings on the alternate screen.
73 */
74static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
75static int alt_t, alt_b;
76static int alt_which;
77
78#define ARGS_MAX 32 /* max # of esc sequence arguments */
064916ea 79#define ARG_DEFAULT 0 /* if an arg isn't specified */
374330e2 80#define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
81static int esc_args[ARGS_MAX];
82static int esc_nargs;
83static int esc_query;
8b811b12 84#define ANSI(x,y) ((x)+((y)<<8))
85#define ANSI_QUE(x) ANSI(x,TRUE)
374330e2 86
87#define OSC_STR_MAX 2048
88static int osc_strlen;
89static char osc_string[OSC_STR_MAX+1];
90static int osc_w;
91
92static unsigned char *tabs;
93
374330e2 94static enum {
cabfd08c 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,
e14a5a13 107 SEEN_ESCHASH,
108 VT52_ESC,
109 VT52_Y1,
110 VT52_Y2
374330e2 111} termstate;
112
113static enum {
114 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
115} selstate;
116static enum {
117 SM_CHAR, SM_WORD, SM_LINE
118} selmode;
119static unsigned long *selstart, *selend, *selanchor;
120
121static 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
132static unsigned char sel_nl[] = SEL_NL;
133
134/*
135 * Internal prototypes.
136 */
137static void do_paint (Context, int);
138static void erase_lots (int, int, int);
139static void swap_screen (int);
140static void update_sbar (void);
141static void deselect (void);
142
143/*
144 * Set up power-on settings for the terminal.
145 */
146static 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;
e14a5a13 164 cursor_on = 1;
374330e2 165 save_attr = curr_attr = ATTR_DEFAULT;
166 app_cursor_keys = cfg.app_cursor;
167 app_keypad_keys = cfg.app_keypad;
e14a5a13 168 use_bce = 0;
169 erase_char = ERASE_CHAR;
374330e2 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 */
187void term_update(void) {
188 Context ctx;
189 ctx = get_ctx();
190 if (ctx) {
0b961e0a 191 if ( (seen_key_event && (unscroll_event & US_KEY)) ||
cabfd08c 192 (seen_disp_event && (unscroll_event & US_DISP)) ) {
193 disptop = scrtop;
194 seen_disp_event = seen_key_event = 0;
195 }
374330e2 196 do_paint (ctx, TRUE);
197 free_ctx (ctx);
374330e2 198 }
199}
200
201/*
202 * Same as power_on(), but an external function.
203 */
204void 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 */
215void term_clrsb(void) {
216 disptop = sbtop = scrtop;
217 update_sbar();
218}
219
220/*
221 * Initialise the terminal.
222 */
223void 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;
374330e2 230 power_on();
231}
232
233/*
234 * Set up the terminal for a given size.
235 */
236void term_size(int newrows, int newcols, int newsavelines) {
237 unsigned long *newtext, *newdisp, *newwant, *newalt;
238 int i, j, crows, ccols;
239
d6832394 240 int save_alt_which = alt_which;
241
374330e2 242 if (newrows == rows && newcols == cols && newsavelines == savelines)
243 return; /* nothing to do */
244
d6832394 245 deselect();
246 swap_screen(0);
247
374330e2 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++)
e14a5a13 291 newalt[i] = erase_char;
374330e2 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
d6832394 321 swap_screen(save_alt_which);
322
374330e2 323 update_sbar();
324 term_update();
325}
326
327/*
328 * Swap screens.
329 */
330static 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 */
359static 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 */
372static 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 */
385static 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 */
395static 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
cabfd08c 403 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
374330e2 404 /*
cabfd08c 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.
374330e2 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++)
e14a5a13 425 scroll_top[i+scroll_size] = erase_char;
374330e2 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++)
e14a5a13 442 scroll_top[i] = erase_char;
374330e2 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 }
374330e2 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 */
464static 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 */
488static 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;
e14a5a13 502 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
374330e2 503 }
504}
505
506/*
507 * Erase a large portion of the screen: the whole screen, or the
508 * whole line, or parts thereof.
509 */
510static 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)
e14a5a13 523 endpos = cpos+1;
374330e2 524 check_selection (startpos, endpos);
e14a5a13 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
374330e2 530 while (startpos < endpos)
e14a5a13 531 *startpos++ = erase_char;
374330e2 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 */
538static 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--)
e14a5a13 550 cpos[m++] = erase_char;
374330e2 551 } else {
552 memmove (cpos+n, cpos, m*TSIZE);
553 while (n--)
e14a5a13 554 cpos[n] = erase_char;
374330e2 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 */
562static 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;
e14a5a13 567 case 2: /* VT52 mode */
568 vt52_mode = !state;
569 break;
374330e2 570 case 3: /* 80/132 columns */
571 deselect();
e14a5a13 572 request_resize (state ? 132 : 80, rows, 1);
573 reset_132 = state;
374330e2 574 break;
575 case 5: /* reverse video */
576 rvideo = state;
cabfd08c 577 seen_disp_event = TRUE;
e14a5a13 578 if (state) term_update();
374330e2 579 break;
580 case 6: /* DEC origin mode */
581 dec_om = state;
582 break;
583 case 7: /* auto wrap */
584 wrap = state;
585 break;
e14a5a13 586 case 25: /* enable/disable cursor */
587 compatibility(VT220);
588 cursor_on = state;
589 seen_disp_event = TRUE;
590 break;
374330e2 591 case 47: /* alternate screen */
e14a5a13 592 compatibility(OTHER);
374330e2 593 deselect();
594 swap_screen (state);
595 disptop = scrtop;
596 break;
597 } else switch (mode) {
598 case 4: /* set insert mode */
e14a5a13 599 compatibility(VT102);
374330e2 600 insert = state;
601 break;
e14a5a13 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;
374330e2 614 }
615}
616
617/*
618 * Process an OSC sequence: set window title or icon name.
619 */
620static 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 */
646void term_out(void) {
647 int c;
374330e2 648
649 while ( (c = inbuf_getc()) != -1) {
5fd04f07 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) {
374330e2 655 static FILE *fp = NULL;
5fd04f07 656 if (!fp) fp = fopen(logfile, "wb");
374330e2 657 if (fp) fputc (c, fp);
658 }
e14a5a13 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))) {
374330e2 665 switch (c) {
666 case '\005': /* terminal type query */
e14a5a13 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);
374330e2 677 break;
678 case '\007':
679 beep();
680 disptop = scrtop;
374330e2 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;
cabfd08c 690 seen_disp_event = TRUE;
374330e2 691 break;
692 case '\016':
e14a5a13 693 compatibility(VT100);
374330e2 694 cset = 1;
695 break;
696 case '\017':
e14a5a13 697 compatibility(VT100);
374330e2 698 cset = 0;
699 break;
700 case '\033':
e14a5a13 701 if (vt52_mode)
702 termstate = VT52_ESC;
703 else {
704 compatibility(ANSIMIN);
705 termstate = SEEN_ESC;
706 }
374330e2 707 break;
708 case 0233:
e14a5a13 709 compatibility(VT220);
374330e2 710 termstate = SEEN_CSI;
711 esc_nargs = 1;
712 esc_args[0] = ARG_DEFAULT;
713 esc_query = FALSE;
714 break;
715 case 0235:
e14a5a13 716 compatibility(VT220);
374330e2 717 termstate = SEEN_OSC;
718 esc_args[0] = 0;
719 break;
720 case '\r':
721 curs_x = 0;
722 wrapnext = FALSE;
723 fix_cpos;
cabfd08c 724 seen_disp_event = TRUE;
374330e2 725 break;
726 case '\013':
727 case '\014':
e14a5a13 728 compatibility(VT100);
374330e2 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++;
cabfd08c 734 if (cfg.lfhascr)
735 curs_x = 0;
374330e2 736 fix_cpos;
737 wrapnext = FALSE;
cabfd08c 738 seen_disp_event = 1;
374330e2 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 }
cabfd08c 751 seen_disp_event = TRUE;
374330e2 752 break;
cabfd08c 753 }
754 }
755 else switch (termstate) {
756 case TOPLEVEL:
8b811b12 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;
8b811b12 767 }
768 if (insert)
769 insch (1);
e14a5a13 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 }
8b811b12 789 curs_x++;
790 if (curs_x == cols) {
791 cpos--;
792 curs_x--;
793 wrapnext = wrap;
374330e2 794 }
8b811b12 795 seen_disp_event = 1;
374330e2 796 break;
cabfd08c 797
374330e2 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) {
374330e2 816 case ' ': /* some weird sequence? */
e14a5a13 817 compatibility(VT220);
374330e2 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 */
e14a5a13 827 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
828 compatibility(OTHER);
374330e2 829 termstate = SEEN_OSC;
830 esc_args[0] = 0;
831 break;
832 case '(': /* should set GL */
e14a5a13 833 compatibility(VT100);
374330e2 834 termstate = SET_GL;
835 break;
836 case ')': /* should set GR */
e14a5a13 837 compatibility(VT100);
374330e2 838 termstate = SET_GR;
839 break;
840 case '7': /* save cursor */
e14a5a13 841 compatibility(VT100);
374330e2 842 save_cursor (TRUE);
843 break;
844 case '8': /* restore cursor */
e14a5a13 845 compatibility(VT100);
374330e2 846 save_cursor (FALSE);
cabfd08c 847 seen_disp_event = TRUE;
374330e2 848 break;
849 case '=':
e14a5a13 850 compatibility(VT100);
374330e2 851 app_keypad_keys = TRUE;
852 break;
853 case '>':
e14a5a13 854 compatibility(VT100);
374330e2 855 app_keypad_keys = FALSE;
856 break;
857 case 'D': /* exactly equivalent to LF */
e14a5a13 858 compatibility(VT100);
374330e2 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;
cabfd08c 865 seen_disp_event = TRUE;
374330e2 866 break;
867 case 'E': /* exactly equivalent to CR-LF */
e14a5a13 868 compatibility(VT100);
374330e2 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;
cabfd08c 877 seen_disp_event = TRUE;
374330e2 878 break;
879 case 'M': /* reverse index - backwards LF */
e14a5a13 880 compatibility(VT100);
374330e2 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;
cabfd08c 887 seen_disp_event = TRUE;
374330e2 888 break;
889 case 'Z': /* terminal type query */
e14a5a13 890 compatibility(VT100);
5bc238bb 891 ldisc->send ("\033[?6c", 5);
374330e2 892 break;
893 case 'c': /* restore power-on settings */
e14a5a13 894 compatibility(VT100);
374330e2 895 power_on();
e14a5a13 896 if (reset_132) {
897 request_resize (80, rows, 1);
898 reset_132 = 0;
899 }
374330e2 900 fix_cpos;
901 disptop = scrtop;
cabfd08c 902 seen_disp_event = TRUE;
374330e2 903 break;
904 case '#': /* ESC # 8 fills screen with Es :-) */
e14a5a13 905 compatibility(VT100);
374330e2 906 termstate = SEEN_ESCHASH;
907 break;
908 case 'H': /* set a tab */
e14a5a13 909 compatibility(VT100);
374330e2 910 tabs[curs_x] = TRUE;
911 break;
912 }
913 break;
914 case SEEN_CSI:
915 termstate = TOPLEVEL; /* default */
8b811b12 916 if( isdigit(c) )
917 {
374330e2 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;
8b811b12 925 }
926 else if( c == ';' )
927 {
374330e2 928 if (++esc_nargs <= ARGS_MAX)
929 esc_args[esc_nargs-1] = ARG_DEFAULT;
930 termstate = SEEN_CSI;
8b811b12 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;
374330e2 937 termstate = SEEN_CSI;
8b811b12 938 }
939 else switch (ANSI(c,esc_query)) {
374330e2 940 case 'A': /* move up N lines */
941 move (curs_x, curs_y - def(esc_args[0], 1), 1);
cabfd08c 942 seen_disp_event = TRUE;
374330e2 943 break;
e14a5a13 944 case 'e': /* move down N lines */
945 compatibility(ANSI);
946 case 'B':
374330e2 947 move (curs_x, curs_y + def(esc_args[0], 1), 1);
cabfd08c 948 seen_disp_event = TRUE;
374330e2 949 break;
e14a5a13 950 case 'a': /* move right N cols */
951 compatibility(ANSI);
952 case 'C':
374330e2 953 move (curs_x + def(esc_args[0], 1), curs_y, 1);
cabfd08c 954 seen_disp_event = TRUE;
374330e2 955 break;
956 case 'D': /* move left N cols */
957 move (curs_x - def(esc_args[0], 1), curs_y, 1);
cabfd08c 958 seen_disp_event = TRUE;
374330e2 959 break;
960 case 'E': /* move down N lines and CR */
e14a5a13 961 compatibility(ANSI);
374330e2 962 move (0, curs_y + def(esc_args[0], 1), 1);
cabfd08c 963 seen_disp_event = TRUE;
374330e2 964 break;
965 case 'F': /* move up N lines and CR */
e14a5a13 966 compatibility(ANSI);
374330e2 967 move (0, curs_y - def(esc_args[0], 1), 1);
cabfd08c 968 seen_disp_event = TRUE;
374330e2 969 break;
970 case 'G': case '`': /* set horizontal posn */
e14a5a13 971 compatibility(ANSI);
374330e2 972 move (def(esc_args[0], 1) - 1, curs_y, 0);
cabfd08c 973 seen_disp_event = TRUE;
374330e2 974 break;
975 case 'd': /* set vertical posn */
e14a5a13 976 compatibility(ANSI);
374330e2 977 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
978 (dec_om ? 2 : 0));
cabfd08c 979 seen_disp_event = TRUE;
374330e2 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));
cabfd08c 987 seen_disp_event = TRUE;
374330e2 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;
cabfd08c 997 seen_disp_event = TRUE;
374330e2 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 }
cabfd08c 1006 seen_disp_event = TRUE;
374330e2 1007 break;
1008 case 'L': /* insert lines */
e14a5a13 1009 compatibility(VT102);
374330e2 1010 if (curs_y <= marg_b)
1011 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
cabfd08c 1012 seen_disp_event = TRUE;
374330e2 1013 break;
1014 case 'M': /* delete lines */
e14a5a13 1015 compatibility(VT102);
374330e2 1016 if (curs_y <= marg_b)
cabfd08c 1017 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1018 seen_disp_event = TRUE;
374330e2 1019 break;
1020 case '@': /* insert chars */
e14a5a13 1021 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1022 compatibility(VT102);
374330e2 1023 insch (def(esc_args[0], 1));
cabfd08c 1024 seen_disp_event = TRUE;
374330e2 1025 break;
1026 case 'P': /* delete chars */
e14a5a13 1027 compatibility(VT102);
374330e2 1028 insch (-def(esc_args[0], 1));
cabfd08c 1029 seen_disp_event = TRUE;
374330e2 1030 break;
1031 case 'c': /* terminal type query */
e14a5a13 1032 compatibility(VT100);
1033 /* This is the response for a VT102 */
5bc238bb 1034 ldisc->send ("\033[?6c", 5);
374330e2 1035 break;
1036 case 'n': /* cursor position query */
1037 if (esc_args[0] == 6) {
e14a5a13 1038 /* Wonder of wonders ANSI.SYS has this!! */
374330e2 1039 char buf[32];
1040 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
5bc238bb 1041 ldisc->send (buf, strlen(buf));
374330e2 1042 }
e14a5a13 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 }
374330e2 1049 break;
e14a5a13 1050 case 'h': /* toggle modes to high */
8b811b12 1051 case ANSI_QUE('h'):
e14a5a13 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 }
374330e2 1058 break;
e14a5a13 1059 case 'l': /* toggle modes to low */
8b811b12 1060 case ANSI_QUE('l'):
e14a5a13 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 }
374330e2 1067 break;
1068 case 'g': /* clear tabs */
e14a5a13 1069 compatibility(VT100);
374330e2 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 */
e14a5a13 1081 compatibility(VT100);
8b811b12 1082 if (esc_nargs <= 2) {
374330e2 1083 int top, bot;
e14a5a13 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 }
374330e2 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.
e14a5a13 1106 *
1107 * Well actually it should for Origin mode - RDB
374330e2 1108 */
e14a5a13 1109 curs_y = (dec_om ? marg_t : 0);
374330e2 1110 fix_cpos;
cabfd08c 1111 seen_disp_event = TRUE;
374330e2 1112 }
1113 }
1114 break;
1115 case 'm': /* set graphics rendition */
1116 {
e14a5a13 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 */
374330e2 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 */
e14a5a13 1146 compatibility(VT100AVO);
374330e2 1147 curr_attr |= ATTR_BOLD; break;
374330e2 1148 case 21: /* (enable double underline) */
e14a5a13 1149 compatibility(OTHER);
1150 case 4: /* enable underline */
1151 compatibility(VT100AVO);
374330e2 1152 curr_attr |= ATTR_UNDER; break;
e14a5a13 1153 case 5: /* enable blink */
1154 compatibility(VT100AVO);
1155 curr_attr |= ATTR_BLINK; break;
374330e2 1156 case 7: /* enable reverse video */
1157 curr_attr |= ATTR_REVERSE; break;
1158 case 22: /* disable bold */
e14a5a13 1159 compatibility(VT220);
374330e2 1160 curr_attr &= ~ATTR_BOLD; break;
1161 case 24: /* disable underline */
e14a5a13 1162 compatibility(VT220);
374330e2 1163 curr_attr &= ~ATTR_UNDER; break;
e14a5a13 1164 case 25: /* disable blink */
1165 compatibility(VT220);
1166 curr_attr &= ~ATTR_BLINK; break;
374330e2 1167 case 27: /* disable reverse video */
e14a5a13 1168 compatibility(VT220);
374330e2 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 }
e14a5a13 1192 if (use_bce)
1193 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
374330e2 1194 }
1195 break;
1196 case 's': /* save cursor */
1197 save_cursor (TRUE);
1198 break;
1199 case 'u': /* restore cursor */
1200 save_cursor (FALSE);
cabfd08c 1201 seen_disp_event = TRUE;
374330e2 1202 break;
1203 case 't': /* set page size - ie window height */
e14a5a13 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 request_resize (cols, def(esc_args[0], 24), 0);
1213 deselect();
1214 }
1215 break;
1216 case ANSI('|', '*'):
1217 /* VT420 sequence DECSNLS
1218 * Set number of lines on screen
1219 * VT420 uses VGA like hardware and can support any size in
1220 * reasonable range (24..49 AIUI) with no default specified.
1221 */
1222 compatibility(VT420);
1223 if (esc_nargs==1 && esc_args[0]>=24) {
1224 request_resize (cols, def(esc_args[0], cfg.height), 0);
1225 deselect();
1226 }
1227 break;
1228 case ANSI('|', '$'):
1229 /* VT340/VT420 sequence DECSCPP
1230 * Set number of columns per page
1231 * Docs imply range is only 80 or 132, but I'll allow any.
1232 */
1233 compatibility(VT340TEXT);
1234 if (esc_nargs<=1) {
1235 request_resize (cols, def(esc_args[0], cfg.width), 0);
1236 deselect();
1237 }
374330e2 1238 break;
1239 case 'X': /* write N spaces w/o moving cursor */
e14a5a13 1240 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1241 compatibility(VT100);
374330e2 1242 {
1243 int n = def(esc_args[0], 1);
1244 unsigned long *p = cpos;
1245 if (n > cols - curs_x)
1246 n = cols - curs_x;
1247 check_selection (cpos, cpos+n);
1248 while (n--)
e14a5a13 1249 *p++ = erase_char;
cabfd08c 1250 seen_disp_event = TRUE;
374330e2 1251 }
1252 break;
1253 case 'x': /* report terminal characteristics */
e14a5a13 1254 compatibility(VT100);
374330e2 1255 {
1256 char buf[32];
1257 int i = def(esc_args[0], 0);
1258 if (i == 0 || i == 1) {
1259 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1260 buf[2] += i;
5bc238bb 1261 ldisc->send (buf, 20);
374330e2 1262 }
1263 }
1264 break;
e14a5a13 1265 case ANSI('L','='):
1266 compatibility(OTHER);
1267 use_bce = (esc_args[0]<=0);
1268 erase_char = ERASE_CHAR;
1269 if (use_bce)
1270 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1271 break;
1272 case ANSI('p','"'):
1273 /* Allow the host to make this emulator a 'perfect' VT102.
1274 * This first appeared in the VT220, but we do need to get
1275 * back to PuTTY mode so I won't check it.
1276 *
1277 * The arg == 60 is a PuTTY extension.
1278 * The 2nd arg, 8bit vs 7bit is not obeyed.
1279 *
1280 * Setting VT102 mode should also change the Fkeys to
1281 * generate PF* codes as a real VT102 has no Fkeys.
1282 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1283 * send nothing.
1284 *
1285 * Note ESC c will NOT change this!
1286 */
1287
1288 if (esc_args[0] == 61) compatibility_level = TM_VT102;
1289 else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
1290 else compatibility_level = TM_PUTTY;
1291 break;
374330e2 1292 }
1293 break;
1294 case SET_GL:
1295 case SET_GR:
e14a5a13 1296 /* VT100 only here, checked above */
374330e2 1297 switch (c) {
1298 case 'A':
1299 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1300 break;
1301 case '0':
1302 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1303 break;
e14a5a13 1304 case 'B':
374330e2 1305 default: /* specifically, 'B' */
1306 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1307 break;
1308 }
e14a5a13 1309 if( !has_compat(VT220) || c != '%' )
8b811b12 1310 termstate = TOPLEVEL;
374330e2 1311 break;
1312 case SEEN_OSC:
1313 osc_w = FALSE;
1314 switch (c) {
374330e2 1315 case 'P': /* Linux palette sequence */
1316 termstate = SEEN_OSC_P;
1317 osc_strlen = 0;
1318 break;
1319 case 'R': /* Linux palette reset */
1320 palette_reset();
1321 term_invalidate();
1322 termstate = TOPLEVEL;
1323 break;
1324 case 'W': /* word-set */
1325 termstate = SEEN_OSC_W;
1326 osc_w = TRUE;
1327 break;
1328 case '0': case '1': case '2': case '3': case '4':
1329 case '5': case '6': case '7': case '8': case '9':
1330 esc_args[0] = 10 * esc_args[0] + c - '0';
1331 break;
1332 case 'L':
1333 /*
1334 * Grotty hack to support xterm and DECterm title
1335 * sequences concurrently.
1336 */
1337 if (esc_args[0] == 2) {
1338 esc_args[0] = 1;
1339 break;
1340 }
1341 /* else fall through */
1342 default:
1343 termstate = OSC_STRING;
1344 osc_strlen = 0;
1345 }
1346 break;
1347 case OSC_STRING:
8b811b12 1348 /*
1349 * This OSC stuff is EVIL. It takes just one character to get into
1350 * sysline mode and it's not initially obvious how to get out.
1351 * So I've added CR and LF as string aborts.
1352 * This shouldn't effect compatibility as I believe embedded
1353 * control characters are supposed to be interpreted (maybe?)
1354 * and they don't display anything useful anyway.
1355 *
1356 * -- RDB
1357 */
1358 if (c == '\n' || c == '\r') {
1359 termstate = TOPLEVEL;
1360 } else if (c == 0234 || c == '\007' ) {
374330e2 1361 /*
1362 * These characters terminate the string; ST and BEL
1363 * terminate the sequence and trigger instant
1364 * processing of it, whereas ESC goes back to SEEN_ESC
1365 * mode unless it is followed by \, in which case it is
1366 * synonymous with ST in the first place.
1367 */
1368 do_osc();
1369 termstate = TOPLEVEL;
1370 } else if (c == '\033')
1371 termstate = OSC_MAYBE_ST;
1372 else if (osc_strlen < OSC_STR_MAX)
1373 osc_string[osc_strlen++] = c;
1374 break;
1375 case SEEN_OSC_P:
1376 {
1377 int max = (osc_strlen == 0 ? 21 : 16);
1378 int val;
1379 if (c >= '0' && c <= '9')
1380 val = c - '0';
1381 else if (c >= 'A' && c <= 'A'+max-10)
1382 val = c - 'A' + 10;
1383 else if (c >= 'a' && c <= 'a'+max-10)
1384 val = c - 'a' + 10;
1385 else
1386 termstate = TOPLEVEL;
1387 osc_string[osc_strlen++] = val;
1388 if (osc_strlen >= 7) {
1389 palette_set (osc_string[0],
1390 osc_string[1] * 16 + osc_string[2],
1391 osc_string[3] * 16 + osc_string[4],
1392 osc_string[5] * 16 + osc_string[6]);
1393 term_invalidate();
1394 termstate = TOPLEVEL;
1395 }
1396 }
1397 break;
1398 case SEEN_OSC_W:
1399 switch (c) {
374330e2 1400 case '0': case '1': case '2': case '3': case '4':
1401 case '5': case '6': case '7': case '8': case '9':
1402 esc_args[0] = 10 * esc_args[0] + c - '0';
1403 break;
1404 default:
1405 termstate = OSC_STRING;
1406 osc_strlen = 0;
1407 }
1408 break;
1409 case SEEN_ESCHASH:
1410 if (c == '8') {
1411 unsigned long *p = scrtop;
1412 int n = rows * (cols+1);
1413 while (n--)
1414 *p++ = ATTR_DEFAULT | 'E';
1415 disptop = scrtop;
cabfd08c 1416 seen_disp_event = TRUE;
374330e2 1417 check_selection (scrtop, scrtop + rows * (cols+1));
1418 }
1419 termstate = TOPLEVEL;
1420 break;
e14a5a13 1421 case VT52_ESC:
1422 termstate = TOPLEVEL;
1423 seen_disp_event = TRUE;
1424 switch (c) {
1425 case 'A':
1426 move (curs_x, curs_y - 1, 1);
1427 break;
1428 case 'B':
1429 move (curs_x, curs_y + 1, 1);
1430 break;
1431 case 'C':
1432 move (curs_x + 1, curs_y, 1);
1433 break;
1434 case 'D':
1435 move (curs_x - 1, curs_y, 1);
1436 break;
1437 case 'F':
1438 cset_attr[cset=0] = ATTR_LINEDRW;
1439 break;
1440 case 'G':
1441 cset_attr[cset=0] = ATTR_ASCII;
1442 break;
1443 case 'H':
1444 move (0, 0, 0);
1445 break;
1446 case 'I':
1447 if (curs_y == 0)
1448 scroll (0, rows-1, -1, TRUE);
1449 else if (curs_y > 0)
1450 curs_y--;
1451 fix_cpos;
1452 wrapnext = FALSE;
1453 break;
1454 case 'J':
1455 erase_lots(FALSE, FALSE, TRUE);
1456 disptop = scrtop;
1457 break;
1458 case 'K':
1459 erase_lots(TRUE, FALSE, TRUE);
1460 break;
1461 case 'V':
1462 /* XXX Print cursor line */
1463 break;
1464 case 'W':
1465 /* XXX Start controller mode */
1466 break;
1467 case 'X':
1468 /* XXX Stop controller mode */
1469 break;
1470 case 'Y':
1471 termstate = VT52_Y1;
1472 break;
1473 case 'Z':
1474 ldisc->send ("\033/Z", 3);
1475 break;
1476 case '=':
1477 app_cursor_keys = TRUE;
1478 break;
1479 case '>':
1480 app_cursor_keys = FALSE;
1481 break;
1482 case '<':
1483 /* XXX This should switch to VT100 mode not current or default
1484 * VT mode. But this will only have effect in a VT220+
1485 * emulation.
1486 */
1487 vt52_mode = FALSE;
1488 break;
1489 case '^':
1490 /* XXX Enter auto print mode */
1491 break;
1492 case '_':
1493 /* XXX Exit auto print mode */
1494 break;
1495 case ']':
1496 /* XXX Print screen */
1497 break;
1498 }
1499 break;
1500 case VT52_Y1:
1501 termstate = VT52_Y2;
1502 move(curs_x, c-' ', 0);
1503 break;
1504 case VT52_Y2:
1505 termstate = TOPLEVEL;
1506 move(c-' ', curs_y, 0);
1507 break;
374330e2 1508 }
e14a5a13 1509 if (selstate != NO_SELECTION)
1510 check_selection (cpos, cpos+1);
374330e2 1511 }
374330e2 1512}
1513
1514/*
1515 * Compare two lines to determine whether they are sufficiently
1516 * alike to scroll-optimise one to the other. Return the degree of
1517 * similarity.
1518 */
1519static int linecmp (unsigned long *a, unsigned long *b) {
1520 int i, n;
1521
1522 for (i=n=0; i < cols; i++)
1523 n += (*a++ == *b++);
1524 return n;
1525}
1526
1527/*
1528 * Given a context, update the window. Out of paranoia, we don't
1529 * allow WM_PAINT responses to do scrolling optimisations.
1530 */
1531static void do_paint (Context ctx, int may_optimise){
1532 int i, j, start, our_curs_y;
1533 unsigned long attr, rv, cursor;
1534 char ch[1024];
1535
e14a5a13 1536 if (cursor_on) {
1537 if (has_focus) {
217dceef 1538 if (blinker || !cfg.blink_cur)
1539 cursor = ATTR_ACTCURS;
1540 else
1541 cursor = 0;
e14a5a13 1542 }
1543 else
1544 cursor = ATTR_PASCURS;
1545 }
1546 else cursor = 0;
374330e2 1547 rv = (rvideo ? ATTR_REVERSE : 0);
1548 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1549
1550 for (i=0; i<rows; i++) {
1551 int idx = i*(cols+1);
1552 for (j=0; j<=cols; j++,idx++) {
1553 unsigned long *d = disptop+idx;
1554 wanttext[idx] = ((*d ^ rv
1555 ^ (selstart <= d && d < selend ?
1556 ATTR_REVERSE : 0)) |
1557 (i==our_curs_y && j==curs_x ? cursor : 0));
1558 }
1559 }
1560
1561 /*
1562 * We would perform scrolling optimisations in here, if they
1563 * didn't have a nasty tendency to cause the whole sodding
1564 * program to hang for a second at speed-critical moments.
1565 * We'll leave it well alone...
1566 */
1567
1568 for (i=0; i<rows; i++) {
1569 int idx = i*(cols+1);
1570 start = -1;
1571 for (j=0; j<=cols; j++,idx++) {
1572 unsigned long t = wanttext[idx];
1573 int needs_update = (j < cols && t != disptext[idx]);
1574 int keep_going = (start != -1 && needs_update &&
1575 (t & ATTR_MASK) == attr &&
1576 j-start < sizeof(ch));
1577 if (start != -1 && !keep_going) {
1578 do_text (ctx, start, i, ch, j-start, attr);
1579 start = -1;
1580 }
1581 if (needs_update) {
1582 if (start == -1) {
1583 start = j;
1584 attr = t & ATTR_MASK;
1585 }
1586 ch[j-start] = (char) (t & CHAR_MASK);
1587 }
1588 disptext[idx] = t;
1589 }
1590 }
1591}
1592
1593/*
e14a5a13 1594 * Flick the switch that says if blinking things should be shown or hidden.
1595 */
1596
1597void term_blink(int flg) {
1598static long last_blink = 0;
1599 long now, blink_diff;
1600
1601 if (flg) {
1602 blinker = 1;
1603 last_blink = GetTickCount();
1604 return;
1605 }
1606 now = GetTickCount();
1607
1608 blink_diff = now-last_blink;
1609
1610 /* Make sure the cursor blinks no more than 2Hz */
1611 if (blink_diff>=0 && blink_diff<450)
1612 return;
1613
1614 last_blink = now;
1615 blinker = !blinker;
1616}
1617
1618/*
374330e2 1619 * Invalidate the whole screen so it will be repainted in full.
1620 */
1621void term_invalidate(void) {
1622 int i;
1623
1624 for (i=0; i<rows*(cols+1); i++)
1625 disptext[i] = ATTR_INVALID;
1626}
1627
1628/*
1629 * Paint the window in response to a WM_PAINT message.
1630 */
1631void term_paint (Context ctx, int l, int t, int r, int b) {
1632 int i, j, left, top, right, bottom;
1633
1634 left = l / font_width;
1635 right = (r - 1) / font_width;
1636 top = t / font_height;
1637 bottom = (b - 1) / font_height;
e42ad027 1638 for (i = top; i <= bottom && i < rows ; i++)
1639 for (j = left; j <= right && j < cols ; j++)
374330e2 1640 disptext[i*(cols+1)+j] = ATTR_INVALID;
1641
e14a5a13 1642 /* This should happen soon enough, also for some reason it sometimes
1643 * fails to actually do anything when re-sizing ... painting the wrong
1644 * window perhaps ?
374330e2 1645 do_paint (ctx, FALSE);
e14a5a13 1646 */
374330e2 1647}
1648
1649/*
1650 * Attempt to scroll the scrollback. The second parameter gives the
1651 * position we want to scroll to; the first is +1 to denote that
1652 * this position is relative to the beginning of the scrollback, -1
1653 * to denote it is relative to the end, and 0 to denote that it is
1654 * relative to the current position.
1655 */
1656void term_scroll (int rel, int where) {
1657 int n = where * (cols+1);
1658
1659 disptop = (rel < 0 ? scrtop :
1660 rel > 0 ? sbtop : disptop) + n;
1661 if (disptop < sbtop)
1662 disptop = sbtop;
1663 if (disptop > scrtop)
1664 disptop = scrtop;
1665 update_sbar();
1666 term_update();
1667}
1668
1669/*
1670 * Spread the selection outwards according to the selection mode.
1671 */
1672static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1673 unsigned long *linestart, *lineend;
1674 int x;
1675 short wvalue;
1676
1677 x = (p - text) % (cols+1);
1678 linestart = p - x;
1679 lineend = linestart + cols;
1680
1681 switch (selmode) {
1682 case SM_CHAR:
1683 /*
1684 * In this mode, every character is a separate unit, except
1685 * for runs of spaces at the end of a non-wrapping line.
1686 */
1687 if (!(linestart[cols] & ATTR_WRAPPED)) {
1688 unsigned long *q = lineend;
1689 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1690 q--;
1691 if (q == lineend)
1692 q--;
1693 if (p >= q)
1694 p = (dir == -1 ? q : lineend - 1);
1695 }
1696 break;
1697 case SM_WORD:
1698 /*
1699 * In this mode, the units are maximal runs of characters
1700 * whose `wordness' has the same value.
1701 */
1702 wvalue = wordness[*p & CHAR_MASK];
1703 if (dir == +1) {
1704 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1705 p++;
1706 } else {
1707 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1708 p--;
1709 }
1710 break;
1711 case SM_LINE:
1712 /*
1713 * In this mode, every line is a unit.
1714 */
1715 p = (dir == -1 ? linestart : lineend - 1);
1716 break;
1717 }
1718 return p;
1719}
1720
1721static void sel_spread (void) {
1722 selstart = sel_spread_half (selstart, -1);
1723 selend = sel_spread_half (selend - 1, +1) + 1;
1724}
1725
1726void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
37508af4 1727 unsigned long *selpoint;
1728
1729 if (y<0) y = 0;
1730 if (y>=rows) y = rows-1;
094ed2a6 1731 if (x<0) {
1732 if (y > 0) {
1733 x = cols-1;
1734 y--;
1735 } else
1736 x = 0;
1737 }
37508af4 1738 if (x>=cols) x = cols-1;
1739
1740 selpoint = disptop + y * (cols+1) + x;
374330e2 1741
1742 if (b == MB_SELECT && a == MA_CLICK) {
1743 deselect();
1744 selstate = ABOUT_TO;
1745 selanchor = selpoint;
1746 selmode = SM_CHAR;
1747 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1748 deselect();
1749 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1750 selstate = DRAGGING;
1751 selstart = selanchor = selpoint;
1752 selend = selstart + 1;
1753 sel_spread();
1754 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1755 (b == MB_EXTEND && a != MA_RELEASE)) {
1756 if (selstate == ABOUT_TO && selanchor == selpoint)
1757 return;
1758 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1759 if (selpoint-selstart < (selend-selstart)/2)
1760 selanchor = selend - 1;
1761 else
1762 selanchor = selstart;
1763 selstate = DRAGGING;
1764 }
1765 if (selstate != ABOUT_TO && selstate != DRAGGING)
1766 selanchor = selpoint;
1767 selstate = DRAGGING;
1768 if (selpoint < selanchor) {
1769 selstart = selpoint;
1770 selend = selanchor + 1;
1771 } else {
1772 selstart = selanchor;
1773 selend = selpoint + 1;
1774 }
1775 sel_spread();
1776 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1777 if (selstate == DRAGGING) {
1778 /*
1779 * We've completed a selection. We now transfer the
1780 * data to the clipboard.
1781 */
1782 unsigned char *p = selspace;
1783 unsigned long *q = selstart;
1784
1785 while (q < selend) {
1786 int nl = FALSE;
1787 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1788 unsigned long *nlpos = lineend;
1789
1790 if (!(*nlpos & ATTR_WRAPPED)) {
1791 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1792 nlpos--;
1793 if (nlpos < selend)
1794 nl = TRUE;
1795 }
1796 while (q < nlpos && q < selend)
1797 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1798 if (nl) {
1799 int i;
1800 for (i=0; i<sizeof(sel_nl); i++)
1801 *p++ = sel_nl[i];
1802 }
1803 q = lineend + 1; /* start of next line */
1804 }
1805 write_clip (selspace, p - selspace);
1806 selstate = SELECTED;
1807 } else
1808 selstate = NO_SELECTION;
1809 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1810 char *data;
1811 int len;
1812
1813 get_clip((void **) &data, &len);
1814 if (data) {
1815 char *p, *q;
1816 p = q = data;
1817 while (p < data+len) {
1818 while (p < data+len &&
1819 !(p <= data+len-sizeof(sel_nl) &&
1820 !memcmp(p, sel_nl, sizeof(sel_nl))))
1821 p++;
14963b8f 1822
1823 {
1824 int i;
1825 unsigned char c;
1826 for(i=0;i<p-q;i++)
1827 {
1828 c=xlat_kbd2tty(q[i]);
5bc238bb 1829 ldisc->send(&c,1);
14963b8f 1830 }
1831 }
1832
374330e2 1833 if (p <= data+len-sizeof(sel_nl) &&
1834 !memcmp(p, sel_nl, sizeof(sel_nl))) {
5bc238bb 1835 ldisc->send ("\r", 1);
374330e2 1836 p += sizeof(sel_nl);
1837 }
1838 q = p;
1839 }
1840 }
1841 get_clip(NULL, NULL);
1842 }
1843
1844 term_update();
1845}
1846
1847static void deselect (void) {
1848 selstate = NO_SELECTION;
1849 selstart = selend = scrtop;
1850}
1851
1852void term_deselect (void) {
1853 deselect();
1854 term_update();
1855}