Having now compiled the last few days' changes with MSVC, it's turned
[u/mdw/putty] / terminal.c
1 #include <windows.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6
7 #include <time.h>
8 #include "putty.h"
9 #include "tree234.h"
10
11 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
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 */
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 */
23
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)
32
33 #define compatibility(x) \
34 if ( ((CL_##x)&compatibility_level) == 0 ) { \
35 termstate=TOPLEVEL; \
36 break; \
37 }
38 #define compatibility2(x,y) \
39 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
40 termstate=TOPLEVEL; \
41 break; \
42 }
43
44 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
45
46 static int compatibility_level = TM_PUTTY;
47
48 static tree234 *scrollback; /* lines scrolled off top of screen */
49 static tree234 *screen; /* lines on primary screen */
50 static tree234 *alt_screen; /* lines on alternate screen */
51 static int disptop; /* distance scrolled back (0 or negative) */
52
53 static unsigned long *cpos; /* cursor position (convenience) */
54
55 static unsigned long *disptext; /* buffer of text on real screen */
56 static unsigned long *wanttext; /* buffer of text we want on screen */
57
58 #define VBELL_TIMEOUT 100 /* millisecond duration of visual bell */
59
60 struct beeptime {
61 struct beeptime *next;
62 long ticks;
63 };
64 static struct beeptime *beephead, *beeptail;
65 int nbeeps;
66 int beep_overloaded;
67 long lastbeep;
68
69 static unsigned char *selspace; /* buffer for building selections in */
70
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)
76
77 static unsigned long curr_attr, save_attr;
78 static unsigned long erase_char = ERASE_CHAR;
79
80 typedef 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
90 static pos curs; /* cursor */
91 static pos savecurs; /* saved cursor position */
92 static int marg_t, marg_b; /* scroll margins */
93 static int dec_om; /* DEC origin mode flag */
94 static int wrap, wrapnext; /* wrap flags */
95 static int insert; /* insert-mode flag */
96 static int cset; /* 0 or 1: which char set */
97 static int save_cset, save_csattr; /* saved with cursor position */
98 static int rvideo; /* global reverse video flag */
99 static int cursor_on; /* cursor enabled flag */
100 static int reset_132; /* Flag ESC c resets to 80 cols */
101 static int use_bce; /* Use Background coloured erase */
102 static int blinker; /* When blinking is the cursor on ? */
103 static int tblinker; /* When the blinking text is on */
104 static int blink_is_real; /* Actually blink blinking text */
105 static int term_echoing; /* Does terminal want local echo? */
106 static int term_editing; /* Does terminal want local edit? */
107
108 static unsigned long cset_attr[2];
109
110 /*
111 * Saved settings on the alternate screen.
112 */
113 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
114 static int alt_t, alt_b;
115 static int alt_which;
116
117 #define ARGS_MAX 32 /* max # of esc sequence arguments */
118 #define ARG_DEFAULT 0 /* if an arg isn't specified */
119 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
120 static int esc_args[ARGS_MAX];
121 static int esc_nargs;
122 static int esc_query;
123 #define ANSI(x,y) ((x)+((y)<<8))
124 #define ANSI_QUE(x) ANSI(x,TRUE)
125
126 #define OSC_STR_MAX 2048
127 static int osc_strlen;
128 static char osc_string[OSC_STR_MAX+1];
129 static int osc_w;
130
131 static char id_string[1024] = "\033[?6c";
132
133 static unsigned char *tabs;
134
135 static enum {
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,
148 SEEN_ESCHASH,
149 VT52_ESC,
150 VT52_Y1,
151 VT52_Y2
152 } termstate;
153
154 static enum {
155 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
156 } selstate;
157 static enum {
158 SM_CHAR, SM_WORD, SM_LINE
159 } selmode;
160 static pos selstart, selend, selanchor;
161
162 static 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
173 static unsigned char sel_nl[] = SEL_NL;
174 static char * paste_buffer = 0;
175 static int paste_len, paste_pos, paste_hold;
176
177 /*
178 * Internal prototypes.
179 */
180 static void do_paint (Context, int);
181 static void erase_lots (int, int, int);
182 static void swap_screen (int);
183 static void update_sbar (void);
184 static void deselect (void);
185 /* log session to file stuff ... */
186 static FILE *lgfp = NULL;
187 static void logtraffic(unsigned char c, int logmode);
188
189 /*
190 * Set up power-on settings for the terminal.
191 */
192 static void power_on(void) {
193 curs.x = curs.y = alt_x = alt_y = savecurs.x = savecurs.y = 0;
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;
210 in_vbell = FALSE;
211 cursor_on = 1;
212 save_attr = curr_attr = ATTR_DEFAULT;
213 term_editing = term_echoing = FALSE;
214 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
215 app_cursor_keys = cfg.app_cursor;
216 app_keypad_keys = cfg.app_keypad;
217 use_bce = cfg.bce;
218 blink_is_real = cfg.blinktext;
219 erase_char = ERASE_CHAR;
220 alt_which = 0;
221 {
222 int i;
223 for (i = 0; i < 256; i++)
224 wordness[i] = cfg.wordness[i];
225 }
226 if (screen) {
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 */
237 void term_update(void) {
238 Context ctx;
239 ctx = get_ctx();
240 if (ctx) {
241 if ( (seen_key_event && (cfg.scroll_on_key)) ||
242 (seen_disp_event && (cfg.scroll_on_disp)) ) {
243 disptop = 0; /* return to main screen */
244 seen_disp_event = seen_key_event = 0;
245 update_sbar();
246 }
247 do_paint (ctx, TRUE);
248 sys_cursor(curs.x, curs.y - disptop);
249 free_ctx (ctx);
250 }
251 }
252
253 /*
254 * Same as power_on(), but an external function.
255 */
256 void term_pwron(void) {
257 power_on();
258 fix_cpos;
259 disptop = 0;
260 deselect();
261 term_update();
262 }
263
264 /*
265 * Clear the scrollback.
266 */
267 void term_clrsb(void) {
268 unsigned long *line;
269 disptop = 0;
270 while ((line = delpos234(scrollback, 0)) != NULL) {
271 sfree(line);
272 }
273 update_sbar();
274 }
275
276 /*
277 * Initialise the terminal.
278 */
279 void term_init(void) {
280 screen = alt_screen = scrollback = NULL;
281 disptop = 0;
282 disptext = wanttext = NULL;
283 tabs = NULL;
284 selspace = NULL;
285 deselect();
286 rows = cols = -1;
287 power_on();
288 beephead = beeptail = NULL;
289 nbeeps = 0;
290 lastbeep = FALSE;
291 beep_overloaded = FALSE;
292 }
293
294 /*
295 * Set up the terminal for a given size.
296 */
297 void term_size(int newrows, int newcols, int newsavelines) {
298 tree234 *newsb, *newscreen, *newalt;
299 unsigned long *newdisp, *newwant, *oldline, *line;
300 int i, j, ccols;
301 int posn, oldposn, furthest_back, oldsbsize;
302 int save_alt_which = alt_which;
303
304 if (newrows == rows && newcols == cols && newsavelines == savelines)
305 return; /* nothing to do */
306
307 deselect();
308 swap_screen(0);
309
310 alt_t = marg_t = 0;
311 alt_b = marg_b = newrows - 1;
312
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 }
344 }
345 if (posn >= 0)
346 addpos234(newscreen, line, 0);
347 else
348 addpos234(newsb, line, 0);
349 }
350 disptop = 0;
351 if (scrollback) freetree234(scrollback);
352 if (screen) freetree234(screen);
353 scrollback = newsb;
354 screen = newscreen;
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
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;
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)
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;
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
408 swap_screen(save_alt_which);
409
410 update_sbar();
411 term_update();
412 }
413
414 /*
415 * Swap screens.
416 */
417 static void swap_screen (int which) {
418 int t;
419 tree234 *ttr;
420
421 if (which == alt_which)
422 return;
423
424 alt_which = which;
425
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;
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 /*
441 * Update the scroll bar.
442 */
443 static void update_sbar(void) {
444 int nscroll;
445
446 nscroll = count234(scrollback);
447
448 set_sbar (nscroll + rows, nscroll + disptop, rows);
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 */
455 static void check_selection (pos from, pos to) {
456 if (poslt(from, selend) && poslt(selstart, to))
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 */
465 static void scroll (int topline, int botline, int lines, int sb) {
466 unsigned long *line, *line2;
467 int i;
468
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;
485 }
486 }
487 if (selend.y >= topline && selend.y <= botline) {
488 selend.y++;
489 if (selend.y > botline) {
490 selend.y = botline;
491 selend.x = 0;
492 }
493 }
494
495 lines++;
496 }
497 } else {
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--;
536 }
537 }
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 */
546 static 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) {
552 if ((curs.y >= marg_t || marg_clip == 2) && y < marg_t)
553 y = marg_t;
554 if ((curs.y <= marg_b || marg_clip == 2) && y > marg_b)
555 y = marg_b;
556 }
557 if (y < 0)
558 y = 0;
559 if (y >= rows)
560 y = rows-1;
561 curs.x = x;
562 curs.y = y;
563 fix_cpos;
564 wrapnext = FALSE;
565 }
566
567 /*
568 * Save or restore the cursor and SGR mode.
569 */
570 static void save_cursor(int save) {
571 if (save) {
572 savecurs = curs;
573 save_attr = curr_attr;
574 save_cset = cset;
575 save_csattr = cset_attr[cset];
576 } else {
577 curs = savecurs;
578 /* Make sure the window hasn't shrunk since the save */
579 if (curs.x >= cols) curs.x = cols-1;
580 if (curs.y >= rows) curs.y = rows-1;
581
582 curr_attr = save_attr;
583 cset = save_cset;
584 cset_attr[cset] = save_csattr;
585 fix_cpos;
586 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
587 }
588 }
589
590 /*
591 * Erase a large portion of the screen: the whole screen, or the
592 * whole line, or parts thereof.
593 */
594 static void erase_lots (int line_only, int from_begin, int to_end) {
595 pos start, end;
596 int erase_lattr;
597 unsigned long *ldata;
598
599 if (line_only) {
600 start.y = curs.y;
601 start.x = 0;
602 end.y = curs.y + 1;
603 end.x = 0;
604 erase_lattr = FALSE;
605 } else {
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;
614 }
615 if (!to_end) {
616 end = curs;
617 }
618 check_selection (start, end);
619
620 /* Clear screen also forces a full window redraw, just in case. */
621 if (start.y == 0 && start.x == 0 && end.y == rows)
622 term_invalidate();
623
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 }
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 */
639 static void insch (int n) {
640 int dir = (n < 0 ? -1 : +1);
641 int m;
642 pos cursplus;
643 unsigned long *ldata;
644
645 n = (n < 0 ? -n : n);
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);
653 if (dir < 0) {
654 memmove (ldata + curs.x, ldata + curs.x + n, m*TSIZE);
655 while (n--)
656 ldata[curs.x + m++] = erase_char;
657 } else {
658 memmove (ldata + curs.x + n, ldata + curs.x, m*TSIZE);
659 while (n--)
660 ldata[curs.x + n] = erase_char;
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 */
668 static 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;
673 case 2: /* VT52 mode */
674 vt52_mode = !state;
675 break;
676 case 3: /* 80/132 columns */
677 deselect();
678 request_resize (state ? 132 : 80, rows, 1);
679 reset_132 = state;
680 break;
681 case 5: /* reverse video */
682 rvideo = state;
683 seen_disp_event = TRUE;
684 if (state) term_update();
685 break;
686 case 6: /* DEC origin mode */
687 dec_om = state;
688 break;
689 case 7: /* auto wrap */
690 wrap = state;
691 break;
692 case 8: /* auto key repeat */
693 repeat_off = !state;
694 break;
695 case 10: /* set local edit mode */
696 term_editing = state;
697 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
698 break;
699 case 25: /* enable/disable cursor */
700 compatibility2(OTHER,VT220);
701 cursor_on = state;
702 seen_disp_event = TRUE;
703 break;
704 case 47: /* alternate screen */
705 compatibility(OTHER);
706 deselect();
707 swap_screen (state);
708 disptop = 0;
709 break;
710 } else switch (mode) {
711 case 4: /* set insert mode */
712 compatibility(VT102);
713 insert = state;
714 break;
715 case 12: /* set echo mode */
716 term_echoing = !state;
717 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
718 break;
719 case 20: /* Return sends ... */
720 cr_lf_return = state;
721 break;
722 }
723 }
724
725 /*
726 * Process an OSC sequence: set window title or icon name.
727 */
728 static 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 */
754 void term_out(void) {
755 int c, inbuf_reap;
756
757 for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
758 {
759 c = inbuf[inbuf_reap];
760
761 /*
762 * Optionally log the session traffic to a file. Useful for
763 * debugging and possibly also useful for actual logging.
764 */
765 logtraffic((unsigned char)c, LGTYP_DEBUG);
766
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 */
771 if( ( (c&0x60) == 0 || c == '\177') &&
772 termstate < DO_CTRLS &&
773 ( (c&0x80) == 0 || has_compat(VT220))) {
774 switch (c) {
775 case '\005': /* terminal type query */
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 */
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 }
808 break;
809 case '\007':
810 {
811 struct beeptime *newbeep;
812 long ticks;
813
814 ticks = GetTickCount();
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;
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 */
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 */
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) {
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 }
873 disptop = 0;
874 }
875 break;
876 case '\b':
877 if (curs.x == 0 && curs.y == 0)
878 ;
879 else if (curs.x == 0 && curs.y > 0)
880 curs.x = cols-1, curs.y--;
881 else if (wrapnext)
882 wrapnext = FALSE;
883 else
884 curs.x--;
885 fix_cpos;
886 seen_disp_event = TRUE;
887 break;
888 case '\016':
889 compatibility(VT100);
890 cset = 1;
891 break;
892 case '\017':
893 compatibility(VT100);
894 cset = 0;
895 break;
896 case '\033':
897 if (vt52_mode)
898 termstate = VT52_ESC;
899 else {
900 compatibility(ANSIMIN);
901 termstate = SEEN_ESC;
902 }
903 break;
904 case 0233:
905 compatibility(VT220);
906 termstate = SEEN_CSI;
907 esc_nargs = 1;
908 esc_args[0] = ARG_DEFAULT;
909 esc_query = FALSE;
910 break;
911 case 0235:
912 compatibility(VT220);
913 termstate = SEEN_OSC;
914 esc_args[0] = 0;
915 break;
916 case '\r':
917 curs.x = 0;
918 wrapnext = FALSE;
919 fix_cpos;
920 seen_disp_event = TRUE;
921 paste_hold = 0;
922 logtraffic((unsigned char)c,LGTYP_ASCII);
923 break;
924 case '\013':
925 case '\014':
926 compatibility(VT100);
927 case '\n':
928 if (curs.y == marg_b)
929 scroll (marg_t, marg_b, 1, TRUE);
930 else if (curs.y < rows-1)
931 curs.y++;
932 if (cfg.lfhascr)
933 curs.x = 0;
934 fix_cpos;
935 wrapnext = FALSE;
936 seen_disp_event = 1;
937 paste_hold = 0;
938 logtraffic((unsigned char)c,LGTYP_ASCII);
939 break;
940 case '\t':
941 {
942 pos old_curs = curs;
943 unsigned long *ldata = lineptr(curs.y);
944
945 do {
946 curs.x++;
947 } while (curs.x < cols-1 && !tabs[curs.x]);
948
949 if ((ldata[cols] & LATTR_MODE) != LATTR_NORM)
950 {
951 if (curs.x >= cols/2)
952 curs.x = cols/2-1;
953 }
954 else
955 {
956 if (curs.x >= cols)
957 curs.x = cols-1;
958 }
959
960 fix_cpos;
961 check_selection (old_curs, curs);
962 }
963 seen_disp_event = TRUE;
964 break;
965 case '\177': /* Destructive backspace
966 This does nothing on a real VT100 */
967 compatibility(OTHER);
968 if (curs.x && !wrapnext) curs.x--;
969 wrapnext = FALSE;
970 fix_cpos;
971 *cpos = (' ' | curr_attr | ATTR_ASCII);
972 break;
973 }
974 }
975 else switch (termstate) {
976 case TOPLEVEL:
977 /* Only graphic characters get this far, ctrls are stripped above */
978 if (wrapnext && wrap) {
979 cpos[1] |= ATTR_WRAPPED;
980 if (curs.y == marg_b)
981 scroll (marg_t, marg_b, 1, TRUE);
982 else if (curs.y < rows-1)
983 curs.y++;
984 curs.x = 0;
985 fix_cpos;
986 wrapnext = FALSE;
987 }
988 if (insert)
989 insch (1);
990 if (selstate != NO_SELECTION) {
991 pos cursplus = curs;
992 incpos(cursplus);
993 check_selection (curs, cursplus);
994 }
995 switch (cset_attr[cset]) {
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.
1001 */
1002 case ATTR_LINEDRW:
1003 if (c<0x5f || c>0x7F)
1004 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
1005 ATTR_ASCII;
1006 else if (c==0x5F)
1007 *cpos++ = ' ' | curr_attr | ATTR_ASCII;
1008 else
1009 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
1010 break;
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*/
1018 default:
1019 *cpos = xlat_tty2scr((unsigned char)c) | curr_attr |
1020 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
1021 logtraffic((unsigned char)c, LGTYP_ASCII);
1022 cpos++;
1023 break;
1024 }
1025 curs.x++;
1026 if (curs.x == cols) {
1027 cpos--;
1028 curs.x--;
1029 wrapnext = TRUE;
1030 }
1031 seen_disp_event = 1;
1032 break;
1033
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) {
1052 case ' ': /* some weird sequence? */
1053 compatibility(VT220);
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 */
1063 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1064 compatibility(OTHER);
1065 termstate = SEEN_OSC;
1066 esc_args[0] = 0;
1067 break;
1068 case '(': /* should set GL */
1069 compatibility(VT100);
1070 termstate = SET_GL;
1071 break;
1072 case ')': /* should set GR */
1073 compatibility(VT100);
1074 termstate = SET_GR;
1075 break;
1076 case '7': /* save cursor */
1077 compatibility(VT100);
1078 save_cursor (TRUE);
1079 break;
1080 case '8': /* restore cursor */
1081 compatibility(VT100);
1082 save_cursor (FALSE);
1083 seen_disp_event = TRUE;
1084 break;
1085 case '=':
1086 compatibility(VT100);
1087 app_keypad_keys = TRUE;
1088 break;
1089 case '>':
1090 compatibility(VT100);
1091 app_keypad_keys = FALSE;
1092 break;
1093 case 'D': /* exactly equivalent to LF */
1094 compatibility(VT100);
1095 if (curs.y == marg_b)
1096 scroll (marg_t, marg_b, 1, TRUE);
1097 else if (curs.y < rows-1)
1098 curs.y++;
1099 fix_cpos;
1100 wrapnext = FALSE;
1101 seen_disp_event = TRUE;
1102 break;
1103 case 'E': /* exactly equivalent to CR-LF */
1104 compatibility(VT100);
1105 curs.x = 0;
1106 if (curs.y == marg_b)
1107 scroll (marg_t, marg_b, 1, TRUE);
1108 else if (curs.y < rows-1)
1109 curs.y++;
1110 fix_cpos;
1111 wrapnext = FALSE;
1112 seen_disp_event = TRUE;
1113 break;
1114 case 'M': /* reverse index - backwards LF */
1115 compatibility(VT100);
1116 if (curs.y == marg_t)
1117 scroll (marg_t, marg_b, -1, TRUE);
1118 else if (curs.y > 0)
1119 curs.y--;
1120 fix_cpos;
1121 wrapnext = FALSE;
1122 seen_disp_event = TRUE;
1123 break;
1124 case 'Z': /* terminal type query */
1125 compatibility(VT100);
1126 ldisc_send (id_string, strlen(id_string));
1127 break;
1128 case 'c': /* restore power-on settings */
1129 compatibility(VT100);
1130 power_on();
1131 if (reset_132) {
1132 request_resize (80, rows, 1);
1133 reset_132 = 0;
1134 }
1135 fix_cpos;
1136 disptop = 0;
1137 seen_disp_event = TRUE;
1138 break;
1139 case '#': /* ESC # 8 fills screen with Es :-) */
1140 compatibility(VT100);
1141 termstate = SEEN_ESCHASH;
1142 break;
1143 case 'H': /* set a tab */
1144 compatibility(VT100);
1145 tabs[curs.x] = TRUE;
1146 break;
1147 }
1148 break;
1149 case SEEN_CSI:
1150 termstate = TOPLEVEL; /* default */
1151 if( isdigit(c) )
1152 {
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;
1160 }
1161 else if( c == ';' )
1162 {
1163 if (++esc_nargs <= ARGS_MAX)
1164 esc_args[esc_nargs-1] = ARG_DEFAULT;
1165 termstate = SEEN_CSI;
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;
1172 termstate = SEEN_CSI;
1173 }
1174 else switch (ANSI(c,esc_query)) {
1175 case 'A': /* move up N lines */
1176 move (curs.x, curs.y - def(esc_args[0], 1), 1);
1177 seen_disp_event = TRUE;
1178 break;
1179 case 'e': /* move down N lines */
1180 compatibility(ANSI);
1181 case 'B':
1182 move (curs.x, curs.y + def(esc_args[0], 1), 1);
1183 seen_disp_event = TRUE;
1184 break;
1185 case 'a': /* move right N cols */
1186 compatibility(ANSI);
1187 case 'C':
1188 move (curs.x + def(esc_args[0], 1), curs.y, 1);
1189 seen_disp_event = TRUE;
1190 break;
1191 case 'D': /* move left N cols */
1192 move (curs.x - def(esc_args[0], 1), curs.y, 1);
1193 seen_disp_event = TRUE;
1194 break;
1195 case 'E': /* move down N lines and CR */
1196 compatibility(ANSI);
1197 move (0, curs.y + def(esc_args[0], 1), 1);
1198 seen_disp_event = TRUE;
1199 break;
1200 case 'F': /* move up N lines and CR */
1201 compatibility(ANSI);
1202 move (0, curs.y - def(esc_args[0], 1), 1);
1203 seen_disp_event = TRUE;
1204 break;
1205 case 'G': case '`': /* set horizontal posn */
1206 compatibility(ANSI);
1207 move (def(esc_args[0], 1) - 1, curs.y, 0);
1208 seen_disp_event = TRUE;
1209 break;
1210 case 'd': /* set vertical posn */
1211 compatibility(ANSI);
1212 move (curs.x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1213 (dec_om ? 2 : 0));
1214 seen_disp_event = TRUE;
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));
1222 seen_disp_event = TRUE;
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 }
1231 disptop = 0;
1232 seen_disp_event = TRUE;
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 }
1241 seen_disp_event = TRUE;
1242 break;
1243 case 'L': /* insert lines */
1244 compatibility(VT102);
1245 if (curs.y <= marg_b)
1246 scroll (curs.y, marg_b, -def(esc_args[0], 1), FALSE);
1247 seen_disp_event = TRUE;
1248 break;
1249 case 'M': /* delete lines */
1250 compatibility(VT102);
1251 if (curs.y <= marg_b)
1252 scroll (curs.y, marg_b, def(esc_args[0], 1), TRUE);
1253 seen_disp_event = TRUE;
1254 break;
1255 case '@': /* insert chars */
1256 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1257 compatibility(VT102);
1258 insch (def(esc_args[0], 1));
1259 seen_disp_event = TRUE;
1260 break;
1261 case 'P': /* delete chars */
1262 compatibility(VT102);
1263 insch (-def(esc_args[0], 1));
1264 seen_disp_event = TRUE;
1265 break;
1266 case 'c': /* terminal type query */
1267 compatibility(VT100);
1268 /* This is the response for a VT102 */
1269 ldisc_send (id_string, strlen(id_string));
1270 break;
1271 case 'n': /* cursor position query */
1272 if (esc_args[0] == 6) {
1273 char buf[32];
1274 sprintf (buf, "\033[%d;%dR", curs.y + 1, curs.x + 1);
1275 ldisc_send (buf, strlen(buf));
1276 }
1277 else if (esc_args[0] == 5) {
1278 ldisc_send ("\033[0n", 4);
1279 }
1280 break;
1281 case 'h': /* toggle modes to high */
1282 case ANSI_QUE('h'):
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 }
1289 break;
1290 case 'l': /* toggle modes to low */
1291 case ANSI_QUE('l'):
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 }
1298 break;
1299 case 'g': /* clear tabs */
1300 compatibility(VT100);
1301 if (esc_nargs == 1) {
1302 if (esc_args[0] == 0) {
1303 tabs[curs.x] = FALSE;
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 */
1312 compatibility(VT100);
1313 if (esc_nargs <= 2) {
1314 int top, bot;
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;
1318 if (bot >= rows)
1319 bot = rows-1;
1320 /* VTTEST Bug 9 - if region is less than 2 lines
1321 * don't change region.
1322 */
1323 if (bot-top > 0) {
1324 marg_t = top;
1325 marg_b = bot;
1326 curs.x = 0;
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.
1332 *
1333 * Well actually it should for Origin mode - RDB
1334 */
1335 curs.y = (dec_om ? marg_t : 0);
1336 fix_cpos;
1337 seen_disp_event = TRUE;
1338 }
1339 }
1340 break;
1341 case 'm': /* set graphics rendition */
1342 {
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 */
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 */
1372 compatibility(VT100AVO);
1373 curr_attr |= ATTR_BOLD; break;
1374 case 21: /* (enable double underline) */
1375 compatibility(OTHER);
1376 case 4: /* enable underline */
1377 compatibility(VT100AVO);
1378 curr_attr |= ATTR_UNDER; break;
1379 case 5: /* enable blink */
1380 compatibility(VT100AVO);
1381 curr_attr |= ATTR_BLINK; break;
1382 case 7: /* enable reverse video */
1383 curr_attr |= ATTR_REVERSE; break;
1384 case 22: /* disable bold */
1385 compatibility2(OTHER,VT220);
1386 curr_attr &= ~ATTR_BOLD; break;
1387 case 24: /* disable underline */
1388 compatibility2(OTHER,VT220);
1389 curr_attr &= ~ATTR_UNDER; break;
1390 case 25: /* disable blink */
1391 compatibility2(OTHER,VT220);
1392 curr_attr &= ~ATTR_BLINK; break;
1393 case 27: /* disable reverse video */
1394 compatibility2(OTHER,VT220);
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 }
1418 if (use_bce)
1419 erase_char =
1420 (' '|
1421 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1422 );
1423 }
1424 break;
1425 case 's': /* save cursor */
1426 save_cursor (TRUE);
1427 break;
1428 case 'u': /* restore cursor */
1429 save_cursor (FALSE);
1430 seen_disp_event = TRUE;
1431 break;
1432 case 't': /* set page size - ie window height */
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)) {
1441 request_resize (cols, def(esc_args[0], 24), 0);
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);
1452 if (esc_nargs==1 && esc_args[0]>0) {
1453 request_resize (cols, def(esc_args[0], cfg.height), 0);
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) {
1464 request_resize (def(esc_args[0], cfg.width), rows, 0);
1465 deselect();
1466 }
1467 break;
1468 case 'X': /* write N spaces w/o moving cursor */
1469 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1470 compatibility(ANSIMIN);
1471 {
1472 int n = def(esc_args[0], 1);
1473 pos cursplus;
1474 unsigned long *p = cpos;
1475 if (n > cols - curs.x)
1476 n = cols - curs.x;
1477 cursplus = curs;
1478 cursplus.x += n;
1479 check_selection (curs, cursplus);
1480 while (n--)
1481 *p++ = erase_char;
1482 seen_disp_event = TRUE;
1483 }
1484 break;
1485 case 'x': /* report terminal characteristics */
1486 compatibility(VT100);
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;
1493 ldisc_send (buf, 20);
1494 }
1495 }
1496 break;
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;
1504 case ANSI('E','='):
1505 compatibility(OTHER);
1506 blink_is_real = (esc_args[0]>=1);
1507 break;
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 *
1513 * The arg in 40..42 are a PuTTY extension.
1514 * The 2nd arg, 8bit vs 7bit is not checked.
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
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
1565 break;
1566 }
1567 break;
1568 case SET_GL:
1569 case SET_GR:
1570 /* VT100 only here, checked above */
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;
1578 case 'B':
1579 default: /* specifically, 'B' */
1580 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1581 break;
1582 }
1583 if( !has_compat(VT220) || c != '%' )
1584 termstate = TOPLEVEL;
1585 break;
1586 case SEEN_OSC:
1587 osc_w = FALSE;
1588 switch (c) {
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:
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' ) {
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) {
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:
1684 {
1685 unsigned long nlattr;
1686 unsigned long *ldata;
1687 int i, j;
1688 pos scrtop, scrbot;
1689
1690 switch (c) {
1691 case '8':
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;
1699 seen_disp_event = TRUE;
1700 scrtop.x = scrtop.y = 0;
1701 scrbot.x = 0; scrbot.y = rows;
1702 check_selection (scrtop, scrbot);
1703 break;
1704
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:
1710
1711 ldata = lineptr(curs.y);
1712 ldata[cols] &= ~LATTR_MODE;
1713 ldata[cols] |= nlattr;
1714 }
1715 }
1716 termstate = TOPLEVEL;
1717 break;
1718 case VT52_ESC:
1719 termstate = TOPLEVEL;
1720 seen_disp_event = TRUE;
1721 switch (c) {
1722 case 'A':
1723 move (curs.x, curs.y - 1, 1);
1724 break;
1725 case 'B':
1726 move (curs.x, curs.y + 1, 1);
1727 break;
1728 case 'C':
1729 move (curs.x + 1, curs.y, 1);
1730 break;
1731 case 'D':
1732 move (curs.x - 1, curs.y, 1);
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':
1744 if (curs.y == 0)
1745 scroll (0, rows-1, -1, TRUE);
1746 else if (curs.y > 0)
1747 curs.y--;
1748 fix_cpos;
1749 wrapnext = FALSE;
1750 break;
1751 case 'J':
1752 erase_lots(FALSE, FALSE, TRUE);
1753 disptop = 0;
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':
1771 ldisc_send ("\033/Z", 3);
1772 break;
1773 case '=':
1774 app_keypad_keys = TRUE;
1775 break;
1776 case '>':
1777 app_keypad_keys = FALSE;
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;
1799 move(curs.x, c-' ', 0);
1800 break;
1801 case VT52_Y2:
1802 termstate = TOPLEVEL;
1803 move(c-' ', curs.y, 0);
1804 break;
1805 }
1806 if (selstate != NO_SELECTION) {
1807 pos cursplus = curs;
1808 incpos(cursplus);
1809 check_selection (curs, cursplus);
1810 }
1811 }
1812 inbuf_head = 0;
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 */
1820 static 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 */
1832 static void do_paint (Context ctx, int may_optimise) {
1833 int i, j, start, our_curs_y;
1834 unsigned long attr, rv, cursor;
1835 pos scrpos;
1836 char ch[1024];
1837 long ticks;
1838
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,
1852 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus
1853 */
1854 if (cursor_on) {
1855 if (has_focus) {
1856 if (blinker || !cfg.blink_cur)
1857 cursor = ATTR_ACTCURS;
1858 else
1859 cursor = 0;
1860 }
1861 else
1862 cursor = ATTR_PASCURS;
1863 if (wrapnext)
1864 cursor |= ATTR_RIGHTCURS;
1865 }
1866 else
1867 cursor = 0;
1868 rv = (!rvideo ^ !in_vbell ? ATTR_REVERSE : 0);
1869 our_curs_y = curs.y - disptop;
1870
1871 for (i=0; i<rows; i++) {
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));
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 }
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);
1907 int lattr = (wanttext[idx+cols] & LATTR_MODE);
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) {
1916 do_text (ctx, start, i, ch, j-start, attr, lattr);
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 /*
1932 * Flick the switch that says if blinking things should be shown or hidden.
1933 */
1934
1935 void term_blink(int flg) {
1936 static long last_blink = 0;
1937 static long last_tblink = 0;
1938 long now, blink_diff;
1939
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
1950 if (flg) {
1951 blinker = 1;
1952 last_blink = now;
1953 return;
1954 }
1955
1956 blink_diff = now-last_blink;
1957
1958 /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */
1959 if (blink_diff>=0 && blink_diff<(long)GetCaretBlinkTime())
1960 return;
1961
1962 last_blink = now;
1963 blinker = !blinker;
1964 }
1965
1966 /*
1967 * Invalidate the whole screen so it will be repainted in full.
1968 */
1969 void 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 */
1979 void 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;
1986 for (i = top; i <= bottom && i < rows ; i++)
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 }
1995
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 ?
1999 do_paint (ctx, FALSE);
2000 */
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 */
2010 void term_scroll (int rel, int where) {
2011 int sbtop = -count234(scrollback);
2012
2013 disptop = (rel < 0 ? 0 :
2014 rel > 0 ? sbtop : disptop) + where;
2015 if (disptop < sbtop)
2016 disptop = sbtop;
2017 if (disptop > 0)
2018 disptop = 0;
2019 update_sbar();
2020 term_update();
2021 }
2022
2023 static void clipme(pos top, pos bottom, char *workbuf) {
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
2035 while (poslt(top, bottom)) {
2036 int nl = FALSE;
2037 unsigned long *ldata = lineptr(top.y);
2038 pos nlpos;
2039
2040 nlpos.y = top.y;
2041 nlpos.x = cols;
2042
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))
2047 nl = TRUE;
2048 }
2049 while (poslt(top, bottom) && poslt(top, nlpos)) {
2050 int ch = (ldata[top.x] & CHAR_MASK);
2051 int set = (ldata[top.x] & CSET_MASK);
2052
2053 /* VT Specials -> ISO8859-1 for Cut&Paste */
2054 static const unsigned char poorman2[] =
2055 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2056
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 }
2065
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;
2077 }
2078 top.x++;
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 }
2093 top.y++;
2094 top.x = 0;
2095 }
2096 write_clip (workbuf, wblen, FALSE); /* transfer to clipboard */
2097 if ( buflen > 0 ) /* indicates we allocated this buffer */
2098 sfree(workbuf);
2099
2100 }
2101 void term_copyall (void) {
2102 pos top;
2103 top.y = -count234(scrollback);
2104 top.x = 0;
2105 clipme(top, curs, NULL /* dynamic allocation */);
2106 }
2107
2108 /*
2109 * Spread the selection outwards according to the selection mode.
2110 */
2111 static pos sel_spread_half (pos p, int dir) {
2112 unsigned long *ldata;
2113 short wvalue;
2114
2115 ldata = lineptr(p.y);
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 */
2123 if (!(ldata[cols] & ATTR_WRAPPED)) {
2124 unsigned long *q = ldata+cols;
2125 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
2126 q--;
2127 if (q == ldata+cols)
2128 q--;
2129 if (p.x >= q-ldata)
2130 p.x = (dir == -1 ? q-ldata : cols - 1);
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 */
2138 wvalue = wordness[ldata[p.x] & CHAR_MASK];
2139 if (dir == +1) {
2140 while (p.x < cols && wordness[ldata[p.x+1] & CHAR_MASK] == wvalue)
2141 p.x++;
2142 } else {
2143 while (p.x > 0 && wordness[ldata[p.x-1] & CHAR_MASK] == wvalue)
2144 p.x--;
2145 }
2146 break;
2147 case SM_LINE:
2148 /*
2149 * In this mode, every line is a unit.
2150 */
2151 p.x = (dir == -1 ? 0 : cols - 1);
2152 break;
2153 }
2154 return p;
2155 }
2156
2157 static void sel_spread (void) {
2158 selstart = sel_spread_half (selstart, -1);
2159 decpos(selend);
2160 selend = sel_spread_half (selend, +1);
2161 incpos(selend);
2162 }
2163
2164 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
2165 pos selpoint;
2166 unsigned long *ldata;
2167
2168 if (y<0) y = 0;
2169 if (y>=rows) y = rows-1;
2170 if (x<0) {
2171 if (y > 0) {
2172 x = cols-1;
2173 y--;
2174 } else
2175 x = 0;
2176 }
2177 if (x>=cols) x = cols-1;
2178
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;
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;
2195 selend = selstart;
2196 incpos(selend);
2197 sel_spread();
2198 } else if ((b == MB_SELECT && a == MA_DRAG) ||
2199 (b == MB_EXTEND && a != MA_RELEASE)) {
2200 if (selstate == ABOUT_TO && poseq(selanchor, selpoint))
2201 return;
2202 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
2203 if (posdiff(selpoint,selstart) < posdiff(selend,selstart)/2) {
2204 selanchor = selend;
2205 decpos(selanchor);
2206 } else {
2207 selanchor = selstart;
2208 }
2209 selstate = DRAGGING;
2210 }
2211 if (selstate != ABOUT_TO && selstate != DRAGGING)
2212 selanchor = selpoint;
2213 selstate = DRAGGING;
2214 if (poslt(selpoint, selanchor)) {
2215 selstart = selpoint;
2216 selend = selanchor;
2217 incpos(selend);
2218 } else {
2219 selstart = selanchor;
2220 selend = selpoint;
2221 incpos(selpoint);
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 */
2230 clipme(selstart, selend, selspace);
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;
2241
2242 if (paste_buffer) sfree(paste_buffer);
2243 paste_pos = paste_hold = paste_len = 0;
2244 paste_buffer = smalloc(len);
2245
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++;
2252
2253 {
2254 int i;
2255 unsigned char c;
2256 for(i=0;i<p-q;i++)
2257 {
2258 c=xlat_kbd2tty(q[i]);
2259 paste_buffer[paste_len++] = c;
2260 }
2261 }
2262
2263 if (p <= data+len-sizeof(sel_nl) &&
2264 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2265 paste_buffer[paste_len++] = '\r';
2266 p += sizeof(sel_nl);
2267 }
2268 q = p;
2269 }
2270
2271 /* Assume a small paste will be OK in one go. */
2272 if (paste_len<256) {
2273 ldisc_send (paste_buffer, paste_len);
2274 if (paste_buffer) sfree(paste_buffer);
2275 paste_buffer = 0;
2276 paste_pos = paste_hold = paste_len = 0;
2277 }
2278 }
2279 get_clip(NULL, NULL);
2280 }
2281
2282 term_update();
2283 }
2284
2285 void term_nopaste() {
2286 if(paste_len == 0) return;
2287 sfree(paste_buffer);
2288 paste_buffer = 0;
2289 paste_len = 0;
2290 }
2291
2292 void term_paste() {
2293 static long last_paste = 0;
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 {
2309 int n = 0;
2310 while (n + paste_pos < paste_len) {
2311 if (paste_buffer[paste_pos + n++] == '\r')
2312 break;
2313 }
2314 ldisc_send (paste_buffer+paste_pos, n);
2315 paste_pos += n;
2316
2317 if (paste_pos < paste_len) {
2318 paste_hold = 1;
2319 return;
2320 }
2321 }
2322 sfree(paste_buffer);
2323 paste_buffer = 0;
2324 paste_len = 0;
2325 }
2326
2327 static void deselect (void) {
2328 selstate = NO_SELECTION;
2329 selstart.x = selstart.y = selend.x = selend.y = 0;
2330 }
2331
2332 void term_deselect (void) {
2333 deselect();
2334 term_update();
2335 }
2336
2337 int 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
2343 /*
2344 * from_backend(), to get data from the backend for the terminal.
2345 */
2346 void 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 }
2353
2354 /*
2355 * Log session traffic.
2356 */
2357 void 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 */
2368 void 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;
2386 cfg.logtype = 0; /* disable logging */
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
2412 void logfclose (void) {
2413 if (lgfp) { fclose(lgfp); lgfp = NULL; }
2414 }