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