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