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