Add some "#ifdef OPTIMISE_SCROLL" scar tissue to placate GCC 4.6
[u/mdw/putty] / terminal.c
1 /*
2 * Terminal emulator.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <ctype.h>
8
9 #include <time.h>
10 #include <assert.h>
11 #include "putty.h"
12 #include "terminal.h"
13
14 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
15 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
16 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
17 #define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )
18
19 /* Product-order comparisons for rectangular block selection. */
20 #define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )
21 #define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )
22
23 #define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
24 #define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )
25
26 #define VT52_PLUS
27
28 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
29 #define CL_VT100 0x0002 /* VT100 */
30 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
31 #define CL_VT102 0x0008 /* VT102 */
32 #define CL_VT220 0x0010 /* VT220 */
33 #define CL_VT320 0x0020 /* VT320 */
34 #define CL_VT420 0x0040 /* VT420 */
35 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
36 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
37 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
38 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
39 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
40
41 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
42 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
43 #define TM_VT102 (TM_VT100AVO|CL_VT102)
44 #define TM_VT220 (TM_VT102|CL_VT220)
45 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
46 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
47
48 #define TM_PUTTY (0xFFFF)
49
50 #define UPDATE_DELAY ((TICKSPERSEC+49)/50)/* ticks to defer window update */
51 #define TBLINK_DELAY ((TICKSPERSEC*9+19)/20)/* ticks between text blinks*/
52 #define CBLINK_DELAY (CURSORBLINK) /* ticks between cursor blinks */
53 #define VBELL_DELAY (VBELL_TIMEOUT) /* visual bell timeout in ticks */
54
55 #define compatibility(x) \
56 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
57 term->termstate=TOPLEVEL; \
58 break; \
59 }
60 #define compatibility2(x,y) \
61 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
62 term->termstate=TOPLEVEL; \
63 break; \
64 }
65
66 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
67
68 char *EMPTY_WINDOW_TITLE = "";
69
70 const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
71
72 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
73 const wchar_t sel_nl[] = SEL_NL;
74
75 /*
76 * Fetch the character at a particular position in a line array,
77 * for purposes of `wordtype'. The reason this isn't just a simple
78 * array reference is that if the character we find is UCSWIDE,
79 * then we must look one space further to the left.
80 */
81 #define UCSGET(a, x) \
82 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
83
84 /*
85 * Detect the various aliases of U+0020 SPACE.
86 */
87 #define IS_SPACE_CHR(chr) \
88 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
89
90 /*
91 * Spot magic CSETs.
92 */
93 #define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
94
95 /*
96 * Internal prototypes.
97 */
98 static void resizeline(Terminal *, termline *, int);
99 static termline *lineptr(Terminal *, int, int, int);
100 static void unlineptr(termline *);
101 static void do_paint(Terminal *, Context, int);
102 static void erase_lots(Terminal *, int, int, int);
103 static int find_last_nonempty_line(Terminal *, tree234 *);
104 static void swap_screen(Terminal *, int, int, int);
105 static void update_sbar(Terminal *);
106 static void deselect(Terminal *);
107 static void term_print_finish(Terminal *);
108 static void scroll(Terminal *, int, int, int, int);
109 #ifdef OPTIMISE_SCROLL
110 static void scroll_display(Terminal *, int, int, int);
111 #endif /* OPTIMISE_SCROLL */
112
113 static termline *newline(Terminal *term, int cols, int bce)
114 {
115 termline *line;
116 int j;
117
118 line = snew(termline);
119 line->chars = snewn(cols, termchar);
120 for (j = 0; j < cols; j++)
121 line->chars[j] = (bce ? term->erase_char : term->basic_erase_char);
122 line->cols = line->size = cols;
123 line->lattr = LATTR_NORM;
124 line->temporary = FALSE;
125 line->cc_free = 0;
126
127 return line;
128 }
129
130 static void freeline(termline *line)
131 {
132 if (line) {
133 sfree(line->chars);
134 sfree(line);
135 }
136 }
137
138 static void unlineptr(termline *line)
139 {
140 if (line->temporary)
141 freeline(line);
142 }
143
144 #ifdef TERM_CC_DIAGS
145 /*
146 * Diagnostic function: verify that a termline has a correct
147 * combining character structure.
148 *
149 * This is a performance-intensive check, so it's no longer enabled
150 * by default.
151 */
152 static void cc_check(termline *line)
153 {
154 unsigned char *flags;
155 int i, j;
156
157 assert(line->size >= line->cols);
158
159 flags = snewn(line->size, unsigned char);
160
161 for (i = 0; i < line->size; i++)
162 flags[i] = (i < line->cols);
163
164 for (i = 0; i < line->cols; i++) {
165 j = i;
166 while (line->chars[j].cc_next) {
167 j += line->chars[j].cc_next;
168 assert(j >= line->cols && j < line->size);
169 assert(!flags[j]);
170 flags[j] = TRUE;
171 }
172 }
173
174 j = line->cc_free;
175 if (j) {
176 while (1) {
177 assert(j >= line->cols && j < line->size);
178 assert(!flags[j]);
179 flags[j] = TRUE;
180 if (line->chars[j].cc_next)
181 j += line->chars[j].cc_next;
182 else
183 break;
184 }
185 }
186
187 j = 0;
188 for (i = 0; i < line->size; i++)
189 j += (flags[i] != 0);
190
191 assert(j == line->size);
192
193 sfree(flags);
194 }
195 #endif
196
197 /*
198 * Add a combining character to a character cell.
199 */
200 static void add_cc(termline *line, int col, unsigned long chr)
201 {
202 int newcc;
203
204 assert(col >= 0 && col < line->cols);
205
206 /*
207 * Start by extending the cols array if the free list is empty.
208 */
209 if (!line->cc_free) {
210 int n = line->size;
211 line->size += 16 + (line->size - line->cols) / 2;
212 line->chars = sresize(line->chars, line->size, termchar);
213 line->cc_free = n;
214 while (n < line->size) {
215 if (n+1 < line->size)
216 line->chars[n].cc_next = 1;
217 else
218 line->chars[n].cc_next = 0;
219 n++;
220 }
221 }
222
223 /*
224 * Now walk the cc list of the cell in question.
225 */
226 while (line->chars[col].cc_next)
227 col += line->chars[col].cc_next;
228
229 /*
230 * `col' now points at the last cc currently in this cell; so
231 * we simply add another one.
232 */
233 newcc = line->cc_free;
234 if (line->chars[newcc].cc_next)
235 line->cc_free = newcc + line->chars[newcc].cc_next;
236 else
237 line->cc_free = 0;
238 line->chars[newcc].cc_next = 0;
239 line->chars[newcc].chr = chr;
240 line->chars[col].cc_next = newcc - col;
241
242 #ifdef TERM_CC_DIAGS
243 cc_check(line);
244 #endif
245 }
246
247 /*
248 * Clear the combining character list in a character cell.
249 */
250 static void clear_cc(termline *line, int col)
251 {
252 int oldfree, origcol = col;
253
254 assert(col >= 0 && col < line->cols);
255
256 if (!line->chars[col].cc_next)
257 return; /* nothing needs doing */
258
259 oldfree = line->cc_free;
260 line->cc_free = col + line->chars[col].cc_next;
261 while (line->chars[col].cc_next)
262 col += line->chars[col].cc_next;
263 if (oldfree)
264 line->chars[col].cc_next = oldfree - col;
265 else
266 line->chars[col].cc_next = 0;
267
268 line->chars[origcol].cc_next = 0;
269
270 #ifdef TERM_CC_DIAGS
271 cc_check(line);
272 #endif
273 }
274
275 /*
276 * Compare two character cells for equality. Special case required
277 * in do_paint() where we override what we expect the chr and attr
278 * fields to be.
279 */
280 static int termchars_equal_override(termchar *a, termchar *b,
281 unsigned long bchr, unsigned long battr)
282 {
283 /* FULL-TERMCHAR */
284 if (a->chr != bchr)
285 return FALSE;
286 if ((a->attr &~ DATTR_MASK) != (battr &~ DATTR_MASK))
287 return FALSE;
288 while (a->cc_next || b->cc_next) {
289 if (!a->cc_next || !b->cc_next)
290 return FALSE; /* one cc-list ends, other does not */
291 a += a->cc_next;
292 b += b->cc_next;
293 if (a->chr != b->chr)
294 return FALSE;
295 }
296 return TRUE;
297 }
298
299 static int termchars_equal(termchar *a, termchar *b)
300 {
301 return termchars_equal_override(a, b, b->chr, b->attr);
302 }
303
304 /*
305 * Copy a character cell. (Requires a pointer to the destination
306 * termline, so as to access its free list.)
307 */
308 static void copy_termchar(termline *destline, int x, termchar *src)
309 {
310 clear_cc(destline, x);
311
312 destline->chars[x] = *src; /* copy everything except cc-list */
313 destline->chars[x].cc_next = 0; /* and make sure this is zero */
314
315 while (src->cc_next) {
316 src += src->cc_next;
317 add_cc(destline, x, src->chr);
318 }
319
320 #ifdef TERM_CC_DIAGS
321 cc_check(destline);
322 #endif
323 }
324
325 /*
326 * Move a character cell within its termline.
327 */
328 static void move_termchar(termline *line, termchar *dest, termchar *src)
329 {
330 /* First clear the cc list from the original char, just in case. */
331 clear_cc(line, dest - line->chars);
332
333 /* Move the character cell and adjust its cc_next. */
334 *dest = *src; /* copy everything except cc-list */
335 if (src->cc_next)
336 dest->cc_next = src->cc_next - (dest-src);
337
338 /* Ensure the original cell doesn't have a cc list. */
339 src->cc_next = 0;
340
341 #ifdef TERM_CC_DIAGS
342 cc_check(line);
343 #endif
344 }
345
346 /*
347 * Compress and decompress a termline into an RLE-based format for
348 * storing in scrollback. (Since scrollback almost never needs to
349 * be modified and exists in huge quantities, this is a sensible
350 * tradeoff, particularly since it allows us to continue adding
351 * features to the main termchar structure without proportionally
352 * bloating the terminal emulator's memory footprint unless those
353 * features are in constant use.)
354 */
355 struct buf {
356 unsigned char *data;
357 int len, size;
358 };
359 static void add(struct buf *b, unsigned char c)
360 {
361 if (b->len >= b->size) {
362 b->size = (b->len * 3 / 2) + 512;
363 b->data = sresize(b->data, b->size, unsigned char);
364 }
365 b->data[b->len++] = c;
366 }
367 static int get(struct buf *b)
368 {
369 return b->data[b->len++];
370 }
371 static void makerle(struct buf *b, termline *ldata,
372 void (*makeliteral)(struct buf *b, termchar *c,
373 unsigned long *state))
374 {
375 int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;
376 termchar *c = ldata->chars;
377 unsigned long state = 0, oldstate;
378
379 n = ldata->cols;
380
381 hdrpos = b->len;
382 hdrsize = 0;
383 add(b, 0);
384 prevlen = prevpos = 0;
385 prev2 = FALSE;
386
387 while (n-- > 0) {
388 thispos = b->len;
389 makeliteral(b, c++, &state);
390 thislen = b->len - thispos;
391 if (thislen == prevlen &&
392 !memcmp(b->data + prevpos, b->data + thispos, thislen)) {
393 /*
394 * This literal precisely matches the previous one.
395 * Turn it into a run if it's worthwhile.
396 *
397 * With one-byte literals, it costs us two bytes to
398 * encode a run, plus another byte to write the header
399 * to resume normal output; so a three-element run is
400 * neutral, and anything beyond that is unconditionally
401 * worthwhile. With two-byte literals or more, even a
402 * 2-run is a win.
403 */
404 if (thislen > 1 || prev2) {
405 int runpos, runlen;
406
407 /*
408 * It's worth encoding a run. Start at prevpos,
409 * unless hdrsize==0 in which case we can back up
410 * another one and start by overwriting hdrpos.
411 */
412
413 hdrsize--; /* remove the literal at prevpos */
414 if (prev2) {
415 assert(hdrsize > 0);
416 hdrsize--;
417 prevpos -= prevlen;/* and possibly another one */
418 }
419
420 if (hdrsize == 0) {
421 assert(prevpos == hdrpos + 1);
422 runpos = hdrpos;
423 b->len = prevpos+prevlen;
424 } else {
425 memmove(b->data + prevpos+1, b->data + prevpos, prevlen);
426 runpos = prevpos;
427 b->len = prevpos+prevlen+1;
428 /*
429 * Terminate the previous run of ordinary
430 * literals.
431 */
432 assert(hdrsize >= 1 && hdrsize <= 128);
433 b->data[hdrpos] = hdrsize - 1;
434 }
435
436 runlen = prev2 ? 3 : 2;
437
438 while (n > 0 && runlen < 129) {
439 int tmppos, tmplen;
440 tmppos = b->len;
441 oldstate = state;
442 makeliteral(b, c, &state);
443 tmplen = b->len - tmppos;
444 b->len = tmppos;
445 if (tmplen != thislen ||
446 memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {
447 state = oldstate;
448 break; /* run over */
449 }
450 n--, c++, runlen++;
451 }
452
453 assert(runlen >= 2 && runlen <= 129);
454 b->data[runpos] = runlen + 0x80 - 2;
455
456 hdrpos = b->len;
457 hdrsize = 0;
458 add(b, 0);
459 /* And ensure this run doesn't interfere with the next. */
460 prevlen = prevpos = 0;
461 prev2 = FALSE;
462
463 continue;
464 } else {
465 /*
466 * Just flag that the previous two literals were
467 * identical, in case we find a third identical one
468 * we want to turn into a run.
469 */
470 prev2 = TRUE;
471 prevlen = thislen;
472 prevpos = thispos;
473 }
474 } else {
475 prev2 = FALSE;
476 prevlen = thislen;
477 prevpos = thispos;
478 }
479
480 /*
481 * This character isn't (yet) part of a run. Add it to
482 * hdrsize.
483 */
484 hdrsize++;
485 if (hdrsize == 128) {
486 b->data[hdrpos] = hdrsize - 1;
487 hdrpos = b->len;
488 hdrsize = 0;
489 add(b, 0);
490 prevlen = prevpos = 0;
491 prev2 = FALSE;
492 }
493 }
494
495 /*
496 * Clean up.
497 */
498 if (hdrsize > 0) {
499 assert(hdrsize <= 128);
500 b->data[hdrpos] = hdrsize - 1;
501 } else {
502 b->len = hdrpos;
503 }
504 }
505 static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)
506 {
507 /*
508 * My encoding for characters is UTF-8-like, in that it stores
509 * 7-bit ASCII in one byte and uses high-bit-set bytes as
510 * introducers to indicate a longer sequence. However, it's
511 * unlike UTF-8 in that it doesn't need to be able to
512 * resynchronise, and therefore I don't want to waste two bits
513 * per byte on having recognisable continuation characters.
514 * Also I don't want to rule out the possibility that I may one
515 * day use values 0x80000000-0xFFFFFFFF for interesting
516 * purposes, so unlike UTF-8 I need a full 32-bit range.
517 * Accordingly, here is my encoding:
518 *
519 * 00000000-0000007F: 0xxxxxxx (but see below)
520 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
521 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
522 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
523 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
524 *
525 * (`Z' is like `x' but is always going to be zero since the
526 * values I'm encoding don't go above 2^32. In principle the
527 * five-byte form of the encoding could extend to 2^35, and
528 * there could be six-, seven-, eight- and nine-byte forms as
529 * well to allow up to 64-bit values to be encoded. But that's
530 * completely unnecessary for these purposes!)
531 *
532 * The encoding as written above would be very simple, except
533 * that 7-bit ASCII can occur in several different ways in the
534 * terminal data; sometimes it crops up in the D800 page
535 * (CSET_ASCII) but at other times it's in the 0000 page (real
536 * Unicode). Therefore, this encoding is actually _stateful_:
537 * the one-byte encoding of 00-7F actually indicates `reuse the
538 * upper three bytes of the last character', and to encode an
539 * absolute value of 00-7F you need to use the two-byte form
540 * instead.
541 */
542 if ((c->chr & ~0x7F) == *state) {
543 add(b, (unsigned char)(c->chr & 0x7F));
544 } else if (c->chr < 0x4000) {
545 add(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));
546 add(b, (unsigned char)(c->chr & 0xFF));
547 } else if (c->chr < 0x200000) {
548 add(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));
549 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
550 add(b, (unsigned char)(c->chr & 0xFF));
551 } else if (c->chr < 0x10000000) {
552 add(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));
553 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
554 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
555 add(b, (unsigned char)(c->chr & 0xFF));
556 } else {
557 add(b, 0xF0);
558 add(b, (unsigned char)((c->chr >> 24) & 0xFF));
559 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
560 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
561 add(b, (unsigned char)(c->chr & 0xFF));
562 }
563 *state = c->chr & ~0xFF;
564 }
565 static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)
566 {
567 /*
568 * My encoding for attributes is 16-bit-granular and assumes
569 * that the top bit of the word is never required. I either
570 * store a two-byte value with the top bit clear (indicating
571 * just that value), or a four-byte value with the top bit set
572 * (indicating the same value with its top bit clear).
573 *
574 * However, first I permute the bits of the attribute value, so
575 * that the eight bits of colour (four in each of fg and bg)
576 * which are never non-zero unless xterm 256-colour mode is in
577 * use are placed higher up the word than everything else. This
578 * ensures that attribute values remain 16-bit _unless_ the
579 * user uses extended colour.
580 */
581 unsigned attr, colourbits;
582
583 attr = c->attr;
584
585 assert(ATTR_BGSHIFT > ATTR_FGSHIFT);
586
587 colourbits = (attr >> (ATTR_BGSHIFT + 4)) & 0xF;
588 colourbits <<= 4;
589 colourbits |= (attr >> (ATTR_FGSHIFT + 4)) & 0xF;
590
591 attr = (((attr >> (ATTR_BGSHIFT + 8)) << (ATTR_BGSHIFT + 4)) |
592 (attr & ((1 << (ATTR_BGSHIFT + 4))-1)));
593 attr = (((attr >> (ATTR_FGSHIFT + 8)) << (ATTR_FGSHIFT + 4)) |
594 (attr & ((1 << (ATTR_FGSHIFT + 4))-1)));
595
596 attr |= (colourbits << (32-9));
597
598 if (attr < 0x8000) {
599 add(b, (unsigned char)((attr >> 8) & 0xFF));
600 add(b, (unsigned char)(attr & 0xFF));
601 } else {
602 add(b, (unsigned char)(((attr >> 24) & 0x7F) | 0x80));
603 add(b, (unsigned char)((attr >> 16) & 0xFF));
604 add(b, (unsigned char)((attr >> 8) & 0xFF));
605 add(b, (unsigned char)(attr & 0xFF));
606 }
607 }
608 static void makeliteral_cc(struct buf *b, termchar *c, unsigned long *state)
609 {
610 /*
611 * For combining characters, I just encode a bunch of ordinary
612 * chars using makeliteral_chr, and terminate with a \0
613 * character (which I know won't come up as a combining char
614 * itself).
615 *
616 * I don't use the stateful encoding in makeliteral_chr.
617 */
618 unsigned long zstate;
619 termchar z;
620
621 while (c->cc_next) {
622 c += c->cc_next;
623
624 assert(c->chr != 0);
625
626 zstate = 0;
627 makeliteral_chr(b, c, &zstate);
628 }
629
630 z.chr = 0;
631 zstate = 0;
632 makeliteral_chr(b, &z, &zstate);
633 }
634
635 static termline *decompressline(unsigned char *data, int *bytes_used);
636
637 static unsigned char *compressline(termline *ldata)
638 {
639 struct buf buffer = { NULL, 0, 0 }, *b = &buffer;
640
641 /*
642 * First, store the column count, 7 bits at a time, least
643 * significant `digit' first, with the high bit set on all but
644 * the last.
645 */
646 {
647 int n = ldata->cols;
648 while (n >= 128) {
649 add(b, (unsigned char)((n & 0x7F) | 0x80));
650 n >>= 7;
651 }
652 add(b, (unsigned char)(n));
653 }
654
655 /*
656 * Next store the lattrs; same principle.
657 */
658 {
659 int n = ldata->lattr;
660 while (n >= 128) {
661 add(b, (unsigned char)((n & 0x7F) | 0x80));
662 n >>= 7;
663 }
664 add(b, (unsigned char)(n));
665 }
666
667 /*
668 * Now we store a sequence of separate run-length encoded
669 * fragments, each containing exactly as many symbols as there
670 * are columns in the ldata.
671 *
672 * All of these have a common basic format:
673 *
674 * - a byte 00-7F indicates that X+1 literals follow it
675 * - a byte 80-FF indicates that a single literal follows it
676 * and expects to be repeated (X-0x80)+2 times.
677 *
678 * The format of the `literals' varies between the fragments.
679 */
680 makerle(b, ldata, makeliteral_chr);
681 makerle(b, ldata, makeliteral_attr);
682 makerle(b, ldata, makeliteral_cc);
683
684 /*
685 * Diagnostics: ensure that the compressed data really does
686 * decompress to the right thing.
687 *
688 * This is a bit performance-heavy for production code.
689 */
690 #ifdef TERM_CC_DIAGS
691 #ifndef CHECK_SB_COMPRESSION
692 {
693 int dused;
694 termline *dcl;
695 int i;
696
697 #ifdef DIAGNOSTIC_SB_COMPRESSION
698 for (i = 0; i < b->len; i++) {
699 printf(" %02x ", b->data[i]);
700 }
701 printf("\n");
702 #endif
703
704 dcl = decompressline(b->data, &dused);
705 assert(b->len == dused);
706 assert(ldata->cols == dcl->cols);
707 assert(ldata->lattr == dcl->lattr);
708 for (i = 0; i < ldata->cols; i++)
709 assert(termchars_equal(&ldata->chars[i], &dcl->chars[i]));
710
711 #ifdef DIAGNOSTIC_SB_COMPRESSION
712 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
713 ldata->cols, 4 * ldata->cols, dused,
714 (double)dused / (4 * ldata->cols));
715 #endif
716
717 freeline(dcl);
718 }
719 #endif
720 #endif /* TERM_CC_DIAGS */
721
722 /*
723 * Trim the allocated memory so we don't waste any, and return.
724 */
725 return sresize(b->data, b->len, unsigned char);
726 }
727
728 static void readrle(struct buf *b, termline *ldata,
729 void (*readliteral)(struct buf *b, termchar *c,
730 termline *ldata, unsigned long *state))
731 {
732 int n = 0;
733 unsigned long state = 0;
734
735 while (n < ldata->cols) {
736 int hdr = get(b);
737
738 if (hdr >= 0x80) {
739 /* A run. */
740
741 int pos = b->len, count = hdr + 2 - 0x80;
742 while (count--) {
743 assert(n < ldata->cols);
744 b->len = pos;
745 readliteral(b, ldata->chars + n, ldata, &state);
746 n++;
747 }
748 } else {
749 /* Just a sequence of consecutive literals. */
750
751 int count = hdr + 1;
752 while (count--) {
753 assert(n < ldata->cols);
754 readliteral(b, ldata->chars + n, ldata, &state);
755 n++;
756 }
757 }
758 }
759
760 assert(n == ldata->cols);
761 }
762 static void readliteral_chr(struct buf *b, termchar *c, termline *ldata,
763 unsigned long *state)
764 {
765 int byte;
766
767 /*
768 * 00000000-0000007F: 0xxxxxxx
769 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
770 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
771 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
772 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
773 */
774
775 byte = get(b);
776 if (byte < 0x80) {
777 c->chr = byte | *state;
778 } else if (byte < 0xC0) {
779 c->chr = (byte &~ 0xC0) << 8;
780 c->chr |= get(b);
781 } else if (byte < 0xE0) {
782 c->chr = (byte &~ 0xE0) << 16;
783 c->chr |= get(b) << 8;
784 c->chr |= get(b);
785 } else if (byte < 0xF0) {
786 c->chr = (byte &~ 0xF0) << 24;
787 c->chr |= get(b) << 16;
788 c->chr |= get(b) << 8;
789 c->chr |= get(b);
790 } else {
791 assert(byte == 0xF0);
792 c->chr = get(b) << 24;
793 c->chr |= get(b) << 16;
794 c->chr |= get(b) << 8;
795 c->chr |= get(b);
796 }
797 *state = c->chr & ~0xFF;
798 }
799 static void readliteral_attr(struct buf *b, termchar *c, termline *ldata,
800 unsigned long *state)
801 {
802 unsigned val, attr, colourbits;
803
804 val = get(b) << 8;
805 val |= get(b);
806
807 if (val >= 0x8000) {
808 val &= ~0x8000;
809 val <<= 16;
810 val |= get(b) << 8;
811 val |= get(b);
812 }
813
814 colourbits = (val >> (32-9)) & 0xFF;
815 attr = (val & ((1<<(32-9))-1));
816
817 attr = (((attr >> (ATTR_FGSHIFT + 4)) << (ATTR_FGSHIFT + 8)) |
818 (attr & ((1 << (ATTR_FGSHIFT + 4))-1)));
819 attr = (((attr >> (ATTR_BGSHIFT + 4)) << (ATTR_BGSHIFT + 8)) |
820 (attr & ((1 << (ATTR_BGSHIFT + 4))-1)));
821
822 attr |= (colourbits >> 4) << (ATTR_BGSHIFT + 4);
823 attr |= (colourbits & 0xF) << (ATTR_FGSHIFT + 4);
824
825 c->attr = attr;
826 }
827 static void readliteral_cc(struct buf *b, termchar *c, termline *ldata,
828 unsigned long *state)
829 {
830 termchar n;
831 unsigned long zstate;
832 int x = c - ldata->chars;
833
834 c->cc_next = 0;
835
836 while (1) {
837 zstate = 0;
838 readliteral_chr(b, &n, ldata, &zstate);
839 if (!n.chr)
840 break;
841 add_cc(ldata, x, n.chr);
842 }
843 }
844
845 static termline *decompressline(unsigned char *data, int *bytes_used)
846 {
847 int ncols, byte, shift;
848 struct buf buffer, *b = &buffer;
849 termline *ldata;
850
851 b->data = data;
852 b->len = 0;
853
854 /*
855 * First read in the column count.
856 */
857 ncols = shift = 0;
858 do {
859 byte = get(b);
860 ncols |= (byte & 0x7F) << shift;
861 shift += 7;
862 } while (byte & 0x80);
863
864 /*
865 * Now create the output termline.
866 */
867 ldata = snew(termline);
868 ldata->chars = snewn(ncols, termchar);
869 ldata->cols = ldata->size = ncols;
870 ldata->temporary = TRUE;
871 ldata->cc_free = 0;
872
873 /*
874 * We must set all the cc pointers in ldata->chars to 0 right
875 * now, so that cc diagnostics that verify the integrity of the
876 * whole line will make sense while we're in the middle of
877 * building it up.
878 */
879 {
880 int i;
881 for (i = 0; i < ldata->cols; i++)
882 ldata->chars[i].cc_next = 0;
883 }
884
885 /*
886 * Now read in the lattr.
887 */
888 ldata->lattr = shift = 0;
889 do {
890 byte = get(b);
891 ldata->lattr |= (byte & 0x7F) << shift;
892 shift += 7;
893 } while (byte & 0x80);
894
895 /*
896 * Now we read in each of the RLE streams in turn.
897 */
898 readrle(b, ldata, readliteral_chr);
899 readrle(b, ldata, readliteral_attr);
900 readrle(b, ldata, readliteral_cc);
901
902 /* Return the number of bytes read, for diagnostic purposes. */
903 if (bytes_used)
904 *bytes_used = b->len;
905
906 return ldata;
907 }
908
909 /*
910 * Resize a line to make it `cols' columns wide.
911 */
912 static void resizeline(Terminal *term, termline *line, int cols)
913 {
914 int i, oldcols;
915
916 if (line->cols != cols) {
917
918 oldcols = line->cols;
919
920 /*
921 * This line is the wrong length, which probably means it
922 * hasn't been accessed since a resize. Resize it now.
923 *
924 * First, go through all the characters that will be thrown
925 * out in the resize (if we're shrinking the line) and
926 * return their cc lists to the cc free list.
927 */
928 for (i = cols; i < oldcols; i++)
929 clear_cc(line, i);
930
931 /*
932 * If we're shrinking the line, we now bodily move the
933 * entire cc section from where it started to where it now
934 * needs to be. (We have to do this before the resize, so
935 * that the data we're copying is still there. However, if
936 * we're expanding, we have to wait until _after_ the
937 * resize so that the space we're copying into is there.)
938 */
939 if (cols < oldcols)
940 memmove(line->chars + cols, line->chars + oldcols,
941 (line->size - line->cols) * TSIZE);
942
943 /*
944 * Now do the actual resize, leaving the _same_ amount of
945 * cc space as there was to begin with.
946 */
947 line->size += cols - oldcols;
948 line->chars = sresize(line->chars, line->size, TTYPE);
949 line->cols = cols;
950
951 /*
952 * If we're expanding the line, _now_ we move the cc
953 * section.
954 */
955 if (cols > oldcols)
956 memmove(line->chars + cols, line->chars + oldcols,
957 (line->size - line->cols) * TSIZE);
958
959 /*
960 * Go through what's left of the original line, and adjust
961 * the first cc_next pointer in each list. (All the
962 * subsequent ones are still valid because they are
963 * relative offsets within the cc block.) Also do the same
964 * to the head of the cc_free list.
965 */
966 for (i = 0; i < oldcols && i < cols; i++)
967 if (line->chars[i].cc_next)
968 line->chars[i].cc_next += cols - oldcols;
969 if (line->cc_free)
970 line->cc_free += cols - oldcols;
971
972 /*
973 * And finally fill in the new space with erase chars. (We
974 * don't have to worry about cc lists here, because we
975 * _know_ the erase char doesn't have one.)
976 */
977 for (i = oldcols; i < cols; i++)
978 line->chars[i] = term->basic_erase_char;
979
980 #ifdef TERM_CC_DIAGS
981 cc_check(line);
982 #endif
983 }
984 }
985
986 /*
987 * Get the number of lines in the scrollback.
988 */
989 static int sblines(Terminal *term)
990 {
991 int sblines = count234(term->scrollback);
992 if (term->cfg.erase_to_scrollback &&
993 term->alt_which && term->alt_screen) {
994 sblines += term->alt_sblines;
995 }
996 return sblines;
997 }
998
999 /*
1000 * Retrieve a line of the screen or of the scrollback, according to
1001 * whether the y coordinate is non-negative or negative
1002 * (respectively).
1003 */
1004 static termline *lineptr(Terminal *term, int y, int lineno, int screen)
1005 {
1006 termline *line;
1007 tree234 *whichtree;
1008 int treeindex;
1009
1010 if (y >= 0) {
1011 whichtree = term->screen;
1012 treeindex = y;
1013 } else {
1014 int altlines = 0;
1015
1016 assert(!screen);
1017
1018 if (term->cfg.erase_to_scrollback &&
1019 term->alt_which && term->alt_screen) {
1020 altlines = term->alt_sblines;
1021 }
1022 if (y < -altlines) {
1023 whichtree = term->scrollback;
1024 treeindex = y + altlines + count234(term->scrollback);
1025 } else {
1026 whichtree = term->alt_screen;
1027 treeindex = y + term->alt_sblines;
1028 /* treeindex = y + count234(term->alt_screen); */
1029 }
1030 }
1031 if (whichtree == term->scrollback) {
1032 unsigned char *cline = index234(whichtree, treeindex);
1033 line = decompressline(cline, NULL);
1034 } else {
1035 line = index234(whichtree, treeindex);
1036 }
1037
1038 /* We assume that we don't screw up and retrieve something out of range. */
1039 if (line == NULL) {
1040 fatalbox("line==NULL in terminal.c\n"
1041 "lineno=%d y=%d w=%d h=%d\n"
1042 "count(scrollback=%p)=%d\n"
1043 "count(screen=%p)=%d\n"
1044 "count(alt=%p)=%d alt_sblines=%d\n"
1045 "whichtree=%p treeindex=%d\n\n"
1046 "Please contact <putty@projects.tartarus.org> "
1047 "and pass on the above information.",
1048 lineno, y, term->cols, term->rows,
1049 term->scrollback, count234(term->scrollback),
1050 term->screen, count234(term->screen),
1051 term->alt_screen, count234(term->alt_screen), term->alt_sblines,
1052 whichtree, treeindex);
1053 }
1054 assert(line != NULL);
1055
1056 resizeline(term, line, term->cols);
1057 /* FIXME: should we sort the compressed scrollback out here? */
1058
1059 return line;
1060 }
1061
1062 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
1063 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
1064
1065 static void term_schedule_tblink(Terminal *term);
1066 static void term_schedule_cblink(Terminal *term);
1067
1068 static void term_timer(void *ctx, long now)
1069 {
1070 Terminal *term = (Terminal *)ctx;
1071 int update = FALSE;
1072
1073 if (term->tblink_pending && now - term->next_tblink >= 0) {
1074 term->tblinker = !term->tblinker;
1075 term->tblink_pending = FALSE;
1076 term_schedule_tblink(term);
1077 update = TRUE;
1078 }
1079
1080 if (term->cblink_pending && now - term->next_cblink >= 0) {
1081 term->cblinker = !term->cblinker;
1082 term->cblink_pending = FALSE;
1083 term_schedule_cblink(term);
1084 update = TRUE;
1085 }
1086
1087 if (term->in_vbell && now - term->vbell_end >= 0) {
1088 term->in_vbell = FALSE;
1089 update = TRUE;
1090 }
1091
1092 if (update ||
1093 (term->window_update_pending && now - term->next_update >= 0))
1094 term_update(term);
1095 }
1096
1097 static void term_schedule_update(Terminal *term)
1098 {
1099 if (!term->window_update_pending) {
1100 term->window_update_pending = TRUE;
1101 term->next_update = schedule_timer(UPDATE_DELAY, term_timer, term);
1102 }
1103 }
1104
1105 /*
1106 * Call this whenever the terminal window state changes, to queue
1107 * an update.
1108 */
1109 static void seen_disp_event(Terminal *term)
1110 {
1111 term->seen_disp_event = TRUE; /* for scrollback-reset-on-activity */
1112 term_schedule_update(term);
1113 }
1114
1115 /*
1116 * Call when the terminal's blinking-text settings change, or when
1117 * a text blink has just occurred.
1118 */
1119 static void term_schedule_tblink(Terminal *term)
1120 {
1121 if (term->blink_is_real) {
1122 if (!term->tblink_pending)
1123 term->next_tblink = schedule_timer(TBLINK_DELAY, term_timer, term);
1124 term->tblink_pending = TRUE;
1125 } else {
1126 term->tblinker = 1; /* reset when not in use */
1127 term->tblink_pending = FALSE;
1128 }
1129 }
1130
1131 /*
1132 * Likewise with cursor blinks.
1133 */
1134 static void term_schedule_cblink(Terminal *term)
1135 {
1136 if (term->cfg.blink_cur && term->has_focus) {
1137 if (!term->cblink_pending)
1138 term->next_cblink = schedule_timer(CBLINK_DELAY, term_timer, term);
1139 term->cblink_pending = TRUE;
1140 } else {
1141 term->cblinker = 1; /* reset when not in use */
1142 term->cblink_pending = FALSE;
1143 }
1144 }
1145
1146 /*
1147 * Call to reset cursor blinking on new output.
1148 */
1149 static void term_reset_cblink(Terminal *term)
1150 {
1151 seen_disp_event(term);
1152 term->cblinker = 1;
1153 term->cblink_pending = FALSE;
1154 term_schedule_cblink(term);
1155 }
1156
1157 /*
1158 * Call to begin a visual bell.
1159 */
1160 static void term_schedule_vbell(Terminal *term, int already_started,
1161 long startpoint)
1162 {
1163 long ticks_already_gone;
1164
1165 if (already_started)
1166 ticks_already_gone = GETTICKCOUNT() - startpoint;
1167 else
1168 ticks_already_gone = 0;
1169
1170 if (ticks_already_gone < VBELL_DELAY) {
1171 term->in_vbell = TRUE;
1172 term->vbell_end = schedule_timer(VBELL_DELAY - ticks_already_gone,
1173 term_timer, term);
1174 } else {
1175 term->in_vbell = FALSE;
1176 }
1177 }
1178
1179 /*
1180 * Set up power-on settings for the terminal.
1181 * If 'clear' is false, don't actually clear the primary screen, and
1182 * position the cursor below the last non-blank line (scrolling if
1183 * necessary).
1184 */
1185 static void power_on(Terminal *term, int clear)
1186 {
1187 term->alt_x = term->alt_y = 0;
1188 term->savecurs.x = term->savecurs.y = 0;
1189 term->alt_savecurs.x = term->alt_savecurs.y = 0;
1190 term->alt_t = term->marg_t = 0;
1191 if (term->rows != -1)
1192 term->alt_b = term->marg_b = term->rows - 1;
1193 else
1194 term->alt_b = term->marg_b = 0;
1195 if (term->cols != -1) {
1196 int i;
1197 for (i = 0; i < term->cols; i++)
1198 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1199 }
1200 term->alt_om = term->dec_om = term->cfg.dec_om;
1201 term->alt_ins = term->insert = FALSE;
1202 term->alt_wnext = term->wrapnext =
1203 term->save_wnext = term->alt_save_wnext = FALSE;
1204 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1205 term->alt_cset = term->cset = term->save_cset = term->alt_save_cset = 0;
1206 term->alt_utf = term->utf = term->save_utf = term->alt_save_utf = 0;
1207 term->utf_state = 0;
1208 term->alt_sco_acs = term->sco_acs =
1209 term->save_sco_acs = term->alt_save_sco_acs = 0;
1210 term->cset_attr[0] = term->cset_attr[1] =
1211 term->save_csattr = term->alt_save_csattr = CSET_ASCII;
1212 term->rvideo = 0;
1213 term->in_vbell = FALSE;
1214 term->cursor_on = 1;
1215 term->big_cursor = 0;
1216 term->default_attr = term->save_attr =
1217 term->alt_save_attr = term->curr_attr = ATTR_DEFAULT;
1218 term->term_editing = term->term_echoing = FALSE;
1219 term->app_cursor_keys = term->cfg.app_cursor;
1220 term->app_keypad_keys = term->cfg.app_keypad;
1221 term->use_bce = term->cfg.bce;
1222 term->blink_is_real = term->cfg.blinktext;
1223 term->erase_char = term->basic_erase_char;
1224 term->alt_which = 0;
1225 term_print_finish(term);
1226 term->xterm_mouse = 0;
1227 set_raw_mouse_mode(term->frontend, FALSE);
1228 {
1229 int i;
1230 for (i = 0; i < 256; i++)
1231 term->wordness[i] = term->cfg.wordness[i];
1232 }
1233 if (term->screen) {
1234 swap_screen(term, 1, FALSE, FALSE);
1235 erase_lots(term, FALSE, TRUE, TRUE);
1236 swap_screen(term, 0, FALSE, FALSE);
1237 if (clear)
1238 erase_lots(term, FALSE, TRUE, TRUE);
1239 term->curs.y = find_last_nonempty_line(term, term->screen) + 1;
1240 if (term->curs.y == term->rows) {
1241 term->curs.y--;
1242 scroll(term, 0, term->rows - 1, 1, TRUE);
1243 }
1244 } else {
1245 term->curs.y = 0;
1246 }
1247 term->curs.x = 0;
1248 term_schedule_tblink(term);
1249 term_schedule_cblink(term);
1250 }
1251
1252 /*
1253 * Force a screen update.
1254 */
1255 void term_update(Terminal *term)
1256 {
1257 Context ctx;
1258
1259 term->window_update_pending = FALSE;
1260
1261 ctx = get_ctx(term->frontend);
1262 if (ctx) {
1263 int need_sbar_update = term->seen_disp_event;
1264 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
1265 term->disptop = 0; /* return to main screen */
1266 term->seen_disp_event = 0;
1267 need_sbar_update = TRUE;
1268 }
1269
1270 if (need_sbar_update)
1271 update_sbar(term);
1272 do_paint(term, ctx, TRUE);
1273 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
1274 free_ctx(ctx);
1275 }
1276 }
1277
1278 /*
1279 * Called from front end when a keypress occurs, to trigger
1280 * anything magical that needs to happen in that situation.
1281 */
1282 void term_seen_key_event(Terminal *term)
1283 {
1284 /*
1285 * On any keypress, clear the bell overload mechanism
1286 * completely, on the grounds that large numbers of
1287 * beeps coming from deliberate key action are likely
1288 * to be intended (e.g. beeps from filename completion
1289 * blocking repeatedly).
1290 */
1291 term->beep_overloaded = FALSE;
1292 while (term->beephead) {
1293 struct beeptime *tmp = term->beephead;
1294 term->beephead = tmp->next;
1295 sfree(tmp);
1296 }
1297 term->beeptail = NULL;
1298 term->nbeeps = 0;
1299
1300 /*
1301 * Reset the scrollback on keypress, if we're doing that.
1302 */
1303 if (term->cfg.scroll_on_key) {
1304 term->disptop = 0; /* return to main screen */
1305 seen_disp_event(term);
1306 }
1307 }
1308
1309 /*
1310 * Same as power_on(), but an external function.
1311 */
1312 void term_pwron(Terminal *term, int clear)
1313 {
1314 power_on(term, clear);
1315 if (term->ldisc) /* cause ldisc to notice changes */
1316 ldisc_send(term->ldisc, NULL, 0, 0);
1317 term->disptop = 0;
1318 deselect(term);
1319 term_update(term);
1320 }
1321
1322 static void set_erase_char(Terminal *term)
1323 {
1324 term->erase_char = term->basic_erase_char;
1325 if (term->use_bce)
1326 term->erase_char.attr = (term->curr_attr &
1327 (ATTR_FGMASK | ATTR_BGMASK));
1328 }
1329
1330 /*
1331 * When the user reconfigures us, we need to check the forbidden-
1332 * alternate-screen config option, disable raw mouse mode if the
1333 * user has disabled mouse reporting, and abandon a print job if
1334 * the user has disabled printing.
1335 */
1336 void term_reconfig(Terminal *term, Config *cfg)
1337 {
1338 /*
1339 * Before adopting the new config, check all those terminal
1340 * settings which control power-on defaults; and if they've
1341 * changed, we will modify the current state as well as the
1342 * default one. The full list is: Auto wrap mode, DEC Origin
1343 * Mode, BCE, blinking text, character classes.
1344 */
1345 int reset_wrap, reset_decom, reset_bce, reset_tblink, reset_charclass;
1346 int i;
1347
1348 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
1349 reset_decom = (term->cfg.dec_om != cfg->dec_om);
1350 reset_bce = (term->cfg.bce != cfg->bce);
1351 reset_tblink = (term->cfg.blinktext != cfg->blinktext);
1352 reset_charclass = 0;
1353 for (i = 0; i < lenof(term->cfg.wordness); i++)
1354 if (term->cfg.wordness[i] != cfg->wordness[i])
1355 reset_charclass = 1;
1356
1357 /*
1358 * If the bidi or shaping settings have changed, flush the bidi
1359 * cache completely.
1360 */
1361 if (term->cfg.arabicshaping != cfg->arabicshaping ||
1362 term->cfg.bidi != cfg->bidi) {
1363 for (i = 0; i < term->bidi_cache_size; i++) {
1364 sfree(term->pre_bidi_cache[i].chars);
1365 sfree(term->post_bidi_cache[i].chars);
1366 term->pre_bidi_cache[i].width = -1;
1367 term->pre_bidi_cache[i].chars = NULL;
1368 term->post_bidi_cache[i].width = -1;
1369 term->post_bidi_cache[i].chars = NULL;
1370 }
1371 }
1372
1373 term->cfg = *cfg; /* STRUCTURE COPY */
1374
1375 if (reset_wrap)
1376 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1377 if (reset_decom)
1378 term->alt_om = term->dec_om = term->cfg.dec_om;
1379 if (reset_bce) {
1380 term->use_bce = term->cfg.bce;
1381 set_erase_char(term);
1382 }
1383 if (reset_tblink) {
1384 term->blink_is_real = term->cfg.blinktext;
1385 }
1386 if (reset_charclass)
1387 for (i = 0; i < 256; i++)
1388 term->wordness[i] = term->cfg.wordness[i];
1389
1390 if (term->cfg.no_alt_screen)
1391 swap_screen(term, 0, FALSE, FALSE);
1392 if (term->cfg.no_mouse_rep) {
1393 term->xterm_mouse = 0;
1394 set_raw_mouse_mode(term->frontend, 0);
1395 }
1396 if (term->cfg.no_remote_charset) {
1397 term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;
1398 term->sco_acs = term->alt_sco_acs = 0;
1399 term->utf = 0;
1400 }
1401 if (!*term->cfg.printer) {
1402 term_print_finish(term);
1403 }
1404 term_schedule_tblink(term);
1405 term_schedule_cblink(term);
1406 }
1407
1408 /*
1409 * Clear the scrollback.
1410 */
1411 void term_clrsb(Terminal *term)
1412 {
1413 unsigned char *line;
1414 term->disptop = 0;
1415 while ((line = delpos234(term->scrollback, 0)) != NULL) {
1416 sfree(line); /* this is compressed data, not a termline */
1417 }
1418 term->tempsblines = 0;
1419 term->alt_sblines = 0;
1420 update_sbar(term);
1421 }
1422
1423 /*
1424 * Initialise the terminal.
1425 */
1426 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
1427 void *frontend)
1428 {
1429 Terminal *term;
1430
1431 /*
1432 * Allocate a new Terminal structure and initialise the fields
1433 * that need it.
1434 */
1435 term = snew(Terminal);
1436 term->frontend = frontend;
1437 term->ucsdata = ucsdata;
1438 term->cfg = *mycfg; /* STRUCTURE COPY */
1439 term->logctx = NULL;
1440 term->compatibility_level = TM_PUTTY;
1441 strcpy(term->id_string, "\033[?6c");
1442 term->cblink_pending = term->tblink_pending = FALSE;
1443 term->paste_buffer = NULL;
1444 term->paste_len = 0;
1445 term->last_paste = 0;
1446 bufchain_init(&term->inbuf);
1447 bufchain_init(&term->printer_buf);
1448 term->printing = term->only_printing = FALSE;
1449 term->print_job = NULL;
1450 term->vt52_mode = FALSE;
1451 term->cr_lf_return = FALSE;
1452 term->seen_disp_event = FALSE;
1453 term->mouse_is_down = FALSE;
1454 term->reset_132 = FALSE;
1455 term->cblinker = term->tblinker = 0;
1456 term->has_focus = 1;
1457 term->repeat_off = FALSE;
1458 term->termstate = TOPLEVEL;
1459 term->selstate = NO_SELECTION;
1460 term->curstype = 0;
1461
1462 term->screen = term->alt_screen = term->scrollback = NULL;
1463 term->tempsblines = 0;
1464 term->alt_sblines = 0;
1465 term->disptop = 0;
1466 term->disptext = NULL;
1467 term->dispcursx = term->dispcursy = -1;
1468 term->tabs = NULL;
1469 deselect(term);
1470 term->rows = term->cols = -1;
1471 power_on(term, TRUE);
1472 term->beephead = term->beeptail = NULL;
1473 #ifdef OPTIMISE_SCROLL
1474 term->scrollhead = term->scrolltail = NULL;
1475 #endif /* OPTIMISE_SCROLL */
1476 term->nbeeps = 0;
1477 term->lastbeep = FALSE;
1478 term->beep_overloaded = FALSE;
1479 term->attr_mask = 0xffffffff;
1480 term->resize_fn = NULL;
1481 term->resize_ctx = NULL;
1482 term->in_term_out = FALSE;
1483 term->ltemp = NULL;
1484 term->ltemp_size = 0;
1485 term->wcFrom = NULL;
1486 term->wcTo = NULL;
1487 term->wcFromTo_size = 0;
1488
1489 term->window_update_pending = FALSE;
1490
1491 term->bidi_cache_size = 0;
1492 term->pre_bidi_cache = term->post_bidi_cache = NULL;
1493
1494 /* FULL-TERMCHAR */
1495 term->basic_erase_char.chr = CSET_ASCII | ' ';
1496 term->basic_erase_char.attr = ATTR_DEFAULT;
1497 term->basic_erase_char.cc_next = 0;
1498 term->erase_char = term->basic_erase_char;
1499
1500 return term;
1501 }
1502
1503 void term_free(Terminal *term)
1504 {
1505 termline *line;
1506 struct beeptime *beep;
1507 int i;
1508
1509 while ((line = delpos234(term->scrollback, 0)) != NULL)
1510 sfree(line); /* compressed data, not a termline */
1511 freetree234(term->scrollback);
1512 while ((line = delpos234(term->screen, 0)) != NULL)
1513 freeline(line);
1514 freetree234(term->screen);
1515 while ((line = delpos234(term->alt_screen, 0)) != NULL)
1516 freeline(line);
1517 freetree234(term->alt_screen);
1518 if (term->disptext) {
1519 for (i = 0; i < term->rows; i++)
1520 freeline(term->disptext[i]);
1521 }
1522 sfree(term->disptext);
1523 while (term->beephead) {
1524 beep = term->beephead;
1525 term->beephead = beep->next;
1526 sfree(beep);
1527 }
1528 bufchain_clear(&term->inbuf);
1529 if(term->print_job)
1530 printer_finish_job(term->print_job);
1531 bufchain_clear(&term->printer_buf);
1532 sfree(term->paste_buffer);
1533 sfree(term->ltemp);
1534 sfree(term->wcFrom);
1535 sfree(term->wcTo);
1536
1537 for (i = 0; i < term->bidi_cache_size; i++) {
1538 sfree(term->pre_bidi_cache[i].chars);
1539 sfree(term->post_bidi_cache[i].chars);
1540 }
1541 sfree(term->pre_bidi_cache);
1542 sfree(term->post_bidi_cache);
1543
1544 expire_timer_context(term);
1545
1546 sfree(term);
1547 }
1548
1549 /*
1550 * Set up the terminal for a given size.
1551 */
1552 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
1553 {
1554 tree234 *newalt;
1555 termline **newdisp, *line;
1556 int i, j, oldrows = term->rows;
1557 int sblen;
1558 int save_alt_which = term->alt_which;
1559
1560 if (newrows == term->rows && newcols == term->cols &&
1561 newsavelines == term->savelines)
1562 return; /* nothing to do */
1563
1564 /* Behave sensibly if we're given zero (or negative) rows/cols */
1565
1566 if (newrows < 1) newrows = 1;
1567 if (newcols < 1) newcols = 1;
1568
1569 deselect(term);
1570 swap_screen(term, 0, FALSE, FALSE);
1571
1572 term->alt_t = term->marg_t = 0;
1573 term->alt_b = term->marg_b = newrows - 1;
1574
1575 if (term->rows == -1) {
1576 term->scrollback = newtree234(NULL);
1577 term->screen = newtree234(NULL);
1578 term->tempsblines = 0;
1579 term->rows = 0;
1580 }
1581
1582 /*
1583 * Resize the screen and scrollback. We only need to shift
1584 * lines around within our data structures, because lineptr()
1585 * will take care of resizing each individual line if
1586 * necessary. So:
1587 *
1588 * - If the new screen is longer, we shunt lines in from temporary
1589 * scrollback if possible, otherwise we add new blank lines at
1590 * the bottom.
1591 *
1592 * - If the new screen is shorter, we remove any blank lines at
1593 * the bottom if possible, otherwise shunt lines above the cursor
1594 * to scrollback if possible, otherwise delete lines below the
1595 * cursor.
1596 *
1597 * - Then, if the new scrollback length is less than the
1598 * amount of scrollback we actually have, we must throw some
1599 * away.
1600 */
1601 sblen = count234(term->scrollback);
1602 /* Do this loop to expand the screen if newrows > rows */
1603 assert(term->rows == count234(term->screen));
1604 while (term->rows < newrows) {
1605 if (term->tempsblines > 0) {
1606 unsigned char *cline;
1607 /* Insert a line from the scrollback at the top of the screen. */
1608 assert(sblen >= term->tempsblines);
1609 cline = delpos234(term->scrollback, --sblen);
1610 line = decompressline(cline, NULL);
1611 sfree(cline);
1612 line->temporary = FALSE; /* reconstituted line is now real */
1613 term->tempsblines -= 1;
1614 addpos234(term->screen, line, 0);
1615 term->curs.y += 1;
1616 term->savecurs.y += 1;
1617 term->alt_y += 1;
1618 term->alt_savecurs.y += 1;
1619 } else {
1620 /* Add a new blank line at the bottom of the screen. */
1621 line = newline(term, newcols, FALSE);
1622 addpos234(term->screen, line, count234(term->screen));
1623 }
1624 term->rows += 1;
1625 }
1626 /* Do this loop to shrink the screen if newrows < rows */
1627 while (term->rows > newrows) {
1628 if (term->curs.y < term->rows - 1) {
1629 /* delete bottom row, unless it contains the cursor */
1630 sfree(delpos234(term->screen, term->rows - 1));
1631 } else {
1632 /* push top row to scrollback */
1633 line = delpos234(term->screen, 0);
1634 addpos234(term->scrollback, compressline(line), sblen++);
1635 freeline(line);
1636 term->tempsblines += 1;
1637 term->curs.y -= 1;
1638 term->savecurs.y -= 1;
1639 term->alt_y -= 1;
1640 term->alt_savecurs.y -= 1;
1641 }
1642 term->rows -= 1;
1643 }
1644 assert(term->rows == newrows);
1645 assert(count234(term->screen) == newrows);
1646
1647 /* Delete any excess lines from the scrollback. */
1648 while (sblen > newsavelines) {
1649 line = delpos234(term->scrollback, 0);
1650 sfree(line);
1651 sblen--;
1652 }
1653 if (sblen < term->tempsblines)
1654 term->tempsblines = sblen;
1655 assert(count234(term->scrollback) <= newsavelines);
1656 assert(count234(term->scrollback) >= term->tempsblines);
1657 term->disptop = 0;
1658
1659 /* Make a new displayed text buffer. */
1660 newdisp = snewn(newrows, termline *);
1661 for (i = 0; i < newrows; i++) {
1662 newdisp[i] = newline(term, newcols, FALSE);
1663 for (j = 0; j < newcols; j++)
1664 newdisp[i]->chars[j].attr = ATTR_INVALID;
1665 }
1666 if (term->disptext) {
1667 for (i = 0; i < oldrows; i++)
1668 freeline(term->disptext[i]);
1669 }
1670 sfree(term->disptext);
1671 term->disptext = newdisp;
1672 term->dispcursx = term->dispcursy = -1;
1673
1674 /* Make a new alternate screen. */
1675 newalt = newtree234(NULL);
1676 for (i = 0; i < newrows; i++) {
1677 line = newline(term, newcols, TRUE);
1678 addpos234(newalt, line, i);
1679 }
1680 if (term->alt_screen) {
1681 while (NULL != (line = delpos234(term->alt_screen, 0)))
1682 freeline(line);
1683 freetree234(term->alt_screen);
1684 }
1685 term->alt_screen = newalt;
1686 term->alt_sblines = 0;
1687
1688 term->tabs = sresize(term->tabs, newcols, unsigned char);
1689 {
1690 int i;
1691 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
1692 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1693 }
1694
1695 /* Check that the cursor positions are still valid. */
1696 if (term->savecurs.y < 0)
1697 term->savecurs.y = 0;
1698 if (term->savecurs.y >= newrows)
1699 term->savecurs.y = newrows - 1;
1700 if (term->savecurs.x >= newcols)
1701 term->savecurs.x = newcols - 1;
1702 if (term->alt_savecurs.y < 0)
1703 term->alt_savecurs.y = 0;
1704 if (term->alt_savecurs.y >= newrows)
1705 term->alt_savecurs.y = newrows - 1;
1706 if (term->alt_savecurs.x >= newcols)
1707 term->alt_savecurs.x = newcols - 1;
1708 if (term->curs.y < 0)
1709 term->curs.y = 0;
1710 if (term->curs.y >= newrows)
1711 term->curs.y = newrows - 1;
1712 if (term->curs.x >= newcols)
1713 term->curs.x = newcols - 1;
1714 if (term->alt_y < 0)
1715 term->alt_y = 0;
1716 if (term->alt_y >= newrows)
1717 term->alt_y = newrows - 1;
1718 if (term->alt_x >= newcols)
1719 term->alt_x = newcols - 1;
1720 term->alt_x = term->alt_y = 0;
1721 term->wrapnext = term->alt_wnext = FALSE;
1722
1723 term->rows = newrows;
1724 term->cols = newcols;
1725 term->savelines = newsavelines;
1726
1727 swap_screen(term, save_alt_which, FALSE, FALSE);
1728
1729 update_sbar(term);
1730 term_update(term);
1731 if (term->resize_fn)
1732 term->resize_fn(term->resize_ctx, term->cols, term->rows);
1733 }
1734
1735 /*
1736 * Hand a function and context pointer to the terminal which it can
1737 * use to notify a back end of resizes.
1738 */
1739 void term_provide_resize_fn(Terminal *term,
1740 void (*resize_fn)(void *, int, int),
1741 void *resize_ctx)
1742 {
1743 term->resize_fn = resize_fn;
1744 term->resize_ctx = resize_ctx;
1745 if (resize_fn && term->cols > 0 && term->rows > 0)
1746 resize_fn(resize_ctx, term->cols, term->rows);
1747 }
1748
1749 /* Find the bottom line on the screen that has any content.
1750 * If only the top line has content, returns 0.
1751 * If no lines have content, return -1.
1752 */
1753 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
1754 {
1755 int i;
1756 for (i = count234(screen) - 1; i >= 0; i--) {
1757 termline *line = index234(screen, i);
1758 int j;
1759 for (j = 0; j < line->cols; j++)
1760 if (!termchars_equal(&line->chars[j], &term->erase_char))
1761 break;
1762 if (j != line->cols) break;
1763 }
1764 return i;
1765 }
1766
1767 /*
1768 * Swap screens. If `reset' is TRUE and we have been asked to
1769 * switch to the alternate screen, we must bring most of its
1770 * configuration from the main screen and erase the contents of the
1771 * alternate screen completely. (This is even true if we're already
1772 * on it! Blame xterm.)
1773 */
1774 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
1775 {
1776 int t;
1777 pos tp;
1778 tree234 *ttr;
1779
1780 if (!which)
1781 reset = FALSE; /* do no weird resetting if which==0 */
1782
1783 if (which != term->alt_which) {
1784 term->alt_which = which;
1785
1786 ttr = term->alt_screen;
1787 term->alt_screen = term->screen;
1788 term->screen = ttr;
1789 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
1790 t = term->curs.x;
1791 if (!reset && !keep_cur_pos)
1792 term->curs.x = term->alt_x;
1793 term->alt_x = t;
1794 t = term->curs.y;
1795 if (!reset && !keep_cur_pos)
1796 term->curs.y = term->alt_y;
1797 term->alt_y = t;
1798 t = term->marg_t;
1799 if (!reset) term->marg_t = term->alt_t;
1800 term->alt_t = t;
1801 t = term->marg_b;
1802 if (!reset) term->marg_b = term->alt_b;
1803 term->alt_b = t;
1804 t = term->dec_om;
1805 if (!reset) term->dec_om = term->alt_om;
1806 term->alt_om = t;
1807 t = term->wrap;
1808 if (!reset) term->wrap = term->alt_wrap;
1809 term->alt_wrap = t;
1810 t = term->wrapnext;
1811 if (!reset) term->wrapnext = term->alt_wnext;
1812 term->alt_wnext = t;
1813 t = term->insert;
1814 if (!reset) term->insert = term->alt_ins;
1815 term->alt_ins = t;
1816 t = term->cset;
1817 if (!reset) term->cset = term->alt_cset;
1818 term->alt_cset = t;
1819 t = term->utf;
1820 if (!reset) term->utf = term->alt_utf;
1821 term->alt_utf = t;
1822 t = term->sco_acs;
1823 if (!reset) term->sco_acs = term->alt_sco_acs;
1824 term->alt_sco_acs = t;
1825
1826 tp = term->savecurs;
1827 if (!reset && !keep_cur_pos)
1828 term->savecurs = term->alt_savecurs;
1829 term->alt_savecurs = tp;
1830 t = term->save_cset;
1831 if (!reset && !keep_cur_pos)
1832 term->save_cset = term->alt_save_cset;
1833 term->alt_save_cset = t;
1834 t = term->save_csattr;
1835 if (!reset && !keep_cur_pos)
1836 term->save_csattr = term->alt_save_csattr;
1837 term->alt_save_csattr = t;
1838 t = term->save_attr;
1839 if (!reset && !keep_cur_pos)
1840 term->save_attr = term->alt_save_attr;
1841 term->alt_save_attr = t;
1842 t = term->save_utf;
1843 if (!reset && !keep_cur_pos)
1844 term->save_utf = term->alt_save_utf;
1845 term->alt_save_utf = t;
1846 t = term->save_wnext;
1847 if (!reset && !keep_cur_pos)
1848 term->save_wnext = term->alt_save_wnext;
1849 term->alt_save_wnext = t;
1850 t = term->save_sco_acs;
1851 if (!reset && !keep_cur_pos)
1852 term->save_sco_acs = term->alt_save_sco_acs;
1853 term->alt_save_sco_acs = t;
1854 }
1855
1856 if (reset && term->screen) {
1857 /*
1858 * Yes, this _is_ supposed to honour background-colour-erase.
1859 */
1860 erase_lots(term, FALSE, TRUE, TRUE);
1861 }
1862 }
1863
1864 /*
1865 * Update the scroll bar.
1866 */
1867 static void update_sbar(Terminal *term)
1868 {
1869 int nscroll = sblines(term);
1870 set_sbar(term->frontend, nscroll + term->rows,
1871 nscroll + term->disptop, term->rows);
1872 }
1873
1874 /*
1875 * Check whether the region bounded by the two pointers intersects
1876 * the scroll region, and de-select the on-screen selection if so.
1877 */
1878 static void check_selection(Terminal *term, pos from, pos to)
1879 {
1880 if (poslt(from, term->selend) && poslt(term->selstart, to))
1881 deselect(term);
1882 }
1883
1884 /*
1885 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1886 * for backward.) `sb' is TRUE if the scrolling is permitted to
1887 * affect the scrollback buffer.
1888 */
1889 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
1890 {
1891 termline *line;
1892 int i, seltop;
1893 #ifdef OPTIMISE_SCROLL
1894 int olddisptop, shift;
1895 #endif /* OPTIMISE_SCROLL */
1896
1897 if (topline != 0 || term->alt_which != 0)
1898 sb = FALSE;
1899
1900 #ifdef OPTIMISE_SCROLL
1901 olddisptop = term->disptop;
1902 shift = lines;
1903 #endif /* OPTIMISE_SCROLL */
1904 if (lines < 0) {
1905 while (lines < 0) {
1906 line = delpos234(term->screen, botline);
1907 resizeline(term, line, term->cols);
1908 for (i = 0; i < term->cols; i++)
1909 copy_termchar(line, i, &term->erase_char);
1910 line->lattr = LATTR_NORM;
1911 addpos234(term->screen, line, topline);
1912
1913 if (term->selstart.y >= topline && term->selstart.y <= botline) {
1914 term->selstart.y++;
1915 if (term->selstart.y > botline) {
1916 term->selstart.y = botline + 1;
1917 term->selstart.x = 0;
1918 }
1919 }
1920 if (term->selend.y >= topline && term->selend.y <= botline) {
1921 term->selend.y++;
1922 if (term->selend.y > botline) {
1923 term->selend.y = botline + 1;
1924 term->selend.x = 0;
1925 }
1926 }
1927
1928 lines++;
1929 }
1930 } else {
1931 while (lines > 0) {
1932 line = delpos234(term->screen, topline);
1933 #ifdef TERM_CC_DIAGS
1934 cc_check(line);
1935 #endif
1936 if (sb && term->savelines > 0) {
1937 int sblen = count234(term->scrollback);
1938 /*
1939 * We must add this line to the scrollback. We'll
1940 * remove a line from the top of the scrollback if
1941 * the scrollback is full.
1942 */
1943 if (sblen == term->savelines) {
1944 unsigned char *cline;
1945
1946 sblen--;
1947 cline = delpos234(term->scrollback, 0);
1948 sfree(cline);
1949 } else
1950 term->tempsblines += 1;
1951
1952 addpos234(term->scrollback, compressline(line), sblen);
1953
1954 /* now `line' itself can be reused as the bottom line */
1955
1956 /*
1957 * If the user is currently looking at part of the
1958 * scrollback, and they haven't enabled any options
1959 * that are going to reset the scrollback as a
1960 * result of this movement, then the chances are
1961 * they'd like to keep looking at the same line. So
1962 * we move their viewpoint at the same rate as the
1963 * scroll, at least until their viewpoint hits the
1964 * top end of the scrollback buffer, at which point
1965 * we don't have the choice any more.
1966 *
1967 * Thanks to Jan Holmen Holsten for the idea and
1968 * initial implementation.
1969 */
1970 if (term->disptop > -term->savelines && term->disptop < 0)
1971 term->disptop--;
1972 }
1973 resizeline(term, line, term->cols);
1974 for (i = 0; i < term->cols; i++)
1975 copy_termchar(line, i, &term->erase_char);
1976 line->lattr = LATTR_NORM;
1977 addpos234(term->screen, line, botline);
1978
1979 /*
1980 * If the selection endpoints move into the scrollback,
1981 * we keep them moving until they hit the top. However,
1982 * of course, if the line _hasn't_ moved into the
1983 * scrollback then we don't do this, and cut them off
1984 * at the top of the scroll region.
1985 *
1986 * This applies to selstart and selend (for an existing
1987 * selection), and also selanchor (for one being
1988 * selected as we speak).
1989 */
1990 seltop = sb ? -term->savelines : topline;
1991
1992 if (term->selstate != NO_SELECTION) {
1993 if (term->selstart.y >= seltop &&
1994 term->selstart.y <= botline) {
1995 term->selstart.y--;
1996 if (term->selstart.y < seltop) {
1997 term->selstart.y = seltop;
1998 term->selstart.x = 0;
1999 }
2000 }
2001 if (term->selend.y >= seltop && term->selend.y <= botline) {
2002 term->selend.y--;
2003 if (term->selend.y < seltop) {
2004 term->selend.y = seltop;
2005 term->selend.x = 0;
2006 }
2007 }
2008 if (term->selanchor.y >= seltop &&
2009 term->selanchor.y <= botline) {
2010 term->selanchor.y--;
2011 if (term->selanchor.y < seltop) {
2012 term->selanchor.y = seltop;
2013 term->selanchor.x = 0;
2014 }
2015 }
2016 }
2017
2018 lines--;
2019 }
2020 }
2021 #ifdef OPTIMISE_SCROLL
2022 shift += term->disptop - olddisptop;
2023 if (shift < term->rows && shift > -term->rows && shift != 0)
2024 scroll_display(term, topline, botline, shift);
2025 #endif /* OPTIMISE_SCROLL */
2026 }
2027
2028 #ifdef OPTIMISE_SCROLL
2029 /*
2030 * Add a scroll of a region on the screen into the pending scroll list.
2031 * `lines' is +ve for scrolling forward, -ve for backward.
2032 *
2033 * If the scroll is on the same area as the last scroll in the list,
2034 * merge them.
2035 */
2036 static void save_scroll(Terminal *term, int topline, int botline, int lines)
2037 {
2038 struct scrollregion *newscroll;
2039 if (term->scrolltail &&
2040 term->scrolltail->topline == topline &&
2041 term->scrolltail->botline == botline) {
2042 term->scrolltail->lines += lines;
2043 } else {
2044 newscroll = snew(struct scrollregion);
2045 newscroll->topline = topline;
2046 newscroll->botline = botline;
2047 newscroll->lines = lines;
2048 newscroll->next = NULL;
2049
2050 if (!term->scrollhead)
2051 term->scrollhead = newscroll;
2052 else
2053 term->scrolltail->next = newscroll;
2054 term->scrolltail = newscroll;
2055 }
2056 }
2057
2058 /*
2059 * Scroll the physical display, and our conception of it in disptext.
2060 */
2061 static void scroll_display(Terminal *term, int topline, int botline, int lines)
2062 {
2063 int distance, nlines, i, j;
2064
2065 distance = lines > 0 ? lines : -lines;
2066 nlines = botline - topline + 1 - distance;
2067 if (lines > 0) {
2068 for (i = 0; i < nlines; i++)
2069 for (j = 0; j < term->cols; j++)
2070 copy_termchar(term->disptext[i], j,
2071 term->disptext[i+distance]->chars+j);
2072 if (term->dispcursy >= 0 &&
2073 term->dispcursy >= topline + distance &&
2074 term->dispcursy < topline + distance + nlines)
2075 term->dispcursy -= distance;
2076 for (i = 0; i < distance; i++)
2077 for (j = 0; j < term->cols; j++)
2078 term->disptext[nlines+i]->chars[j].attr |= ATTR_INVALID;
2079 } else {
2080 for (i = nlines; i-- ;)
2081 for (j = 0; j < term->cols; j++)
2082 copy_termchar(term->disptext[i+distance], j,
2083 term->disptext[i]->chars+j);
2084 if (term->dispcursy >= 0 &&
2085 term->dispcursy >= topline &&
2086 term->dispcursy < topline + nlines)
2087 term->dispcursy += distance;
2088 for (i = 0; i < distance; i++)
2089 for (j = 0; j < term->cols; j++)
2090 term->disptext[i]->chars[j].attr |= ATTR_INVALID;
2091 }
2092 save_scroll(term, topline, botline, lines);
2093 }
2094 #endif /* OPTIMISE_SCROLL */
2095
2096 /*
2097 * Move the cursor to a given position, clipping at boundaries. We
2098 * may or may not want to clip at the scroll margin: marg_clip is 0
2099 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
2100 * even _being_ outside the margins.
2101 */
2102 static void move(Terminal *term, int x, int y, int marg_clip)
2103 {
2104 if (x < 0)
2105 x = 0;
2106 if (x >= term->cols)
2107 x = term->cols - 1;
2108 if (marg_clip) {
2109 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
2110 y < term->marg_t)
2111 y = term->marg_t;
2112 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
2113 y > term->marg_b)
2114 y = term->marg_b;
2115 }
2116 if (y < 0)
2117 y = 0;
2118 if (y >= term->rows)
2119 y = term->rows - 1;
2120 term->curs.x = x;
2121 term->curs.y = y;
2122 term->wrapnext = FALSE;
2123 }
2124
2125 /*
2126 * Save or restore the cursor and SGR mode.
2127 */
2128 static void save_cursor(Terminal *term, int save)
2129 {
2130 if (save) {
2131 term->savecurs = term->curs;
2132 term->save_attr = term->curr_attr;
2133 term->save_cset = term->cset;
2134 term->save_utf = term->utf;
2135 term->save_wnext = term->wrapnext;
2136 term->save_csattr = term->cset_attr[term->cset];
2137 term->save_sco_acs = term->sco_acs;
2138 } else {
2139 term->curs = term->savecurs;
2140 /* Make sure the window hasn't shrunk since the save */
2141 if (term->curs.x >= term->cols)
2142 term->curs.x = term->cols - 1;
2143 if (term->curs.y >= term->rows)
2144 term->curs.y = term->rows - 1;
2145
2146 term->curr_attr = term->save_attr;
2147 term->cset = term->save_cset;
2148 term->utf = term->save_utf;
2149 term->wrapnext = term->save_wnext;
2150 /*
2151 * wrapnext might reset to False if the x position is no
2152 * longer at the rightmost edge.
2153 */
2154 if (term->wrapnext && term->curs.x < term->cols-1)
2155 term->wrapnext = FALSE;
2156 term->cset_attr[term->cset] = term->save_csattr;
2157 term->sco_acs = term->save_sco_acs;
2158 set_erase_char(term);
2159 }
2160 }
2161
2162 /*
2163 * This function is called before doing _anything_ which affects
2164 * only part of a line of text. It is used to mark the boundary
2165 * between two character positions, and it indicates that some sort
2166 * of effect is going to happen on only one side of that boundary.
2167 *
2168 * The effect of this function is to check whether a CJK
2169 * double-width character is straddling the boundary, and to remove
2170 * it and replace it with two spaces if so. (Of course, one or
2171 * other of those spaces is then likely to be replaced with
2172 * something else again, as a result of whatever happens next.)
2173 *
2174 * Also, if the boundary is at the right-hand _edge_ of the screen,
2175 * it implies something deliberate is being done to the rightmost
2176 * column position; hence we must clear LATTR_WRAPPED2.
2177 *
2178 * The input to the function is the coordinates of the _second_
2179 * character of the pair.
2180 */
2181 static void check_boundary(Terminal *term, int x, int y)
2182 {
2183 termline *ldata;
2184
2185 /* Validate input coordinates, just in case. */
2186 if (x == 0 || x > term->cols)
2187 return;
2188
2189 ldata = scrlineptr(y);
2190 if (x == term->cols) {
2191 ldata->lattr &= ~LATTR_WRAPPED2;
2192 } else {
2193 if (ldata->chars[x].chr == UCSWIDE) {
2194 clear_cc(ldata, x-1);
2195 clear_cc(ldata, x);
2196 ldata->chars[x-1].chr = ' ' | CSET_ASCII;
2197 ldata->chars[x] = ldata->chars[x-1];
2198 }
2199 }
2200 }
2201
2202 /*
2203 * Erase a large portion of the screen: the whole screen, or the
2204 * whole line, or parts thereof.
2205 */
2206 static void erase_lots(Terminal *term,
2207 int line_only, int from_begin, int to_end)
2208 {
2209 pos start, end;
2210 int erase_lattr;
2211 int erasing_lines_from_top = 0;
2212
2213 if (line_only) {
2214 start.y = term->curs.y;
2215 start.x = 0;
2216 end.y = term->curs.y + 1;
2217 end.x = 0;
2218 erase_lattr = FALSE;
2219 } else {
2220 start.y = 0;
2221 start.x = 0;
2222 end.y = term->rows;
2223 end.x = 0;
2224 erase_lattr = TRUE;
2225 }
2226 if (!from_begin) {
2227 start = term->curs;
2228 }
2229 if (!to_end) {
2230 end = term->curs;
2231 incpos(end);
2232 }
2233 if (!from_begin || !to_end)
2234 check_boundary(term, term->curs.x, term->curs.y);
2235 check_selection(term, start, end);
2236
2237 /* Clear screen also forces a full window redraw, just in case. */
2238 if (start.y == 0 && start.x == 0 && end.y == term->rows)
2239 term_invalidate(term);
2240
2241 /* Lines scrolled away shouldn't be brought back on if the terminal
2242 * resizes. */
2243 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
2244 erasing_lines_from_top = 1;
2245
2246 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
2247 /* If it's a whole number of lines, starting at the top, and
2248 * we're fully erasing them, erase by scrolling and keep the
2249 * lines in the scrollback. */
2250 int scrolllines = end.y;
2251 if (end.y == term->rows) {
2252 /* Shrink until we find a non-empty row.*/
2253 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
2254 }
2255 if (scrolllines > 0)
2256 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
2257 } else {
2258 termline *ldata = scrlineptr(start.y);
2259 while (poslt(start, end)) {
2260 if (start.x == term->cols) {
2261 if (!erase_lattr)
2262 ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
2263 else
2264 ldata->lattr = LATTR_NORM;
2265 } else {
2266 copy_termchar(ldata, start.x, &term->erase_char);
2267 }
2268 if (incpos(start) && start.y < term->rows) {
2269 ldata = scrlineptr(start.y);
2270 }
2271 }
2272 }
2273
2274 /* After an erase of lines from the top of the screen, we shouldn't
2275 * bring the lines back again if the terminal enlarges (since the user or
2276 * application has explictly thrown them away). */
2277 if (erasing_lines_from_top && !(term->alt_which))
2278 term->tempsblines = 0;
2279 }
2280
2281 /*
2282 * Insert or delete characters within the current line. n is +ve if
2283 * insertion is desired, and -ve for deletion.
2284 */
2285 static void insch(Terminal *term, int n)
2286 {
2287 int dir = (n < 0 ? -1 : +1);
2288 int m, j;
2289 pos cursplus;
2290 termline *ldata;
2291
2292 n = (n < 0 ? -n : n);
2293 if (n > term->cols - term->curs.x)
2294 n = term->cols - term->curs.x;
2295 m = term->cols - term->curs.x - n;
2296 cursplus.y = term->curs.y;
2297 cursplus.x = term->curs.x + n;
2298 check_selection(term, term->curs, cursplus);
2299 check_boundary(term, term->curs.x, term->curs.y);
2300 if (dir < 0)
2301 check_boundary(term, term->curs.x + n, term->curs.y);
2302 ldata = scrlineptr(term->curs.y);
2303 if (dir < 0) {
2304 for (j = 0; j < m; j++)
2305 move_termchar(ldata,
2306 ldata->chars + term->curs.x + j,
2307 ldata->chars + term->curs.x + j + n);
2308 while (n--)
2309 copy_termchar(ldata, term->curs.x + m++, &term->erase_char);
2310 } else {
2311 for (j = m; j-- ;)
2312 move_termchar(ldata,
2313 ldata->chars + term->curs.x + j + n,
2314 ldata->chars + term->curs.x + j);
2315 while (n--)
2316 copy_termchar(ldata, term->curs.x + n, &term->erase_char);
2317 }
2318 }
2319
2320 /*
2321 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2322 * whether the mode is a DEC private one or a normal one.)
2323 */
2324 static void toggle_mode(Terminal *term, int mode, int query, int state)
2325 {
2326 if (query)
2327 switch (mode) {
2328 case 1: /* DECCKM: application cursor keys */
2329 term->app_cursor_keys = state;
2330 break;
2331 case 2: /* DECANM: VT52 mode */
2332 term->vt52_mode = !state;
2333 if (term->vt52_mode) {
2334 term->blink_is_real = FALSE;
2335 term->vt52_bold = FALSE;
2336 } else {
2337 term->blink_is_real = term->cfg.blinktext;
2338 }
2339 term_schedule_tblink(term);
2340 break;
2341 case 3: /* DECCOLM: 80/132 columns */
2342 deselect(term);
2343 if (!term->cfg.no_remote_resize)
2344 request_resize(term->frontend, state ? 132 : 80, term->rows);
2345 term->reset_132 = state;
2346 term->alt_t = term->marg_t = 0;
2347 term->alt_b = term->marg_b = term->rows - 1;
2348 move(term, 0, 0, 0);
2349 erase_lots(term, FALSE, TRUE, TRUE);
2350 break;
2351 case 5: /* DECSCNM: reverse video */
2352 /*
2353 * Toggle reverse video. If we receive an OFF within the
2354 * visual bell timeout period after an ON, we trigger an
2355 * effective visual bell, so that ESC[?5hESC[?5l will
2356 * always be an actually _visible_ visual bell.
2357 */
2358 if (term->rvideo && !state) {
2359 /* This is an OFF, so set up a vbell */
2360 term_schedule_vbell(term, TRUE, term->rvbell_startpoint);
2361 } else if (!term->rvideo && state) {
2362 /* This is an ON, so we notice the time and save it. */
2363 term->rvbell_startpoint = GETTICKCOUNT();
2364 }
2365 term->rvideo = state;
2366 seen_disp_event(term);
2367 break;
2368 case 6: /* DECOM: DEC origin mode */
2369 term->dec_om = state;
2370 break;
2371 case 7: /* DECAWM: auto wrap */
2372 term->wrap = state;
2373 break;
2374 case 8: /* DECARM: auto key repeat */
2375 term->repeat_off = !state;
2376 break;
2377 case 10: /* DECEDM: set local edit mode */
2378 term->term_editing = state;
2379 if (term->ldisc) /* cause ldisc to notice changes */
2380 ldisc_send(term->ldisc, NULL, 0, 0);
2381 break;
2382 case 25: /* DECTCEM: enable/disable cursor */
2383 compatibility2(OTHER, VT220);
2384 term->cursor_on = state;
2385 seen_disp_event(term);
2386 break;
2387 case 47: /* alternate screen */
2388 compatibility(OTHER);
2389 deselect(term);
2390 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
2391 term->disptop = 0;
2392 break;
2393 case 1000: /* xterm mouse 1 (normal) */
2394 term->xterm_mouse = state ? 1 : 0;
2395 set_raw_mouse_mode(term->frontend, state);
2396 break;
2397 case 1002: /* xterm mouse 2 (inc. button drags) */
2398 term->xterm_mouse = state ? 2 : 0;
2399 set_raw_mouse_mode(term->frontend, state);
2400 break;
2401 case 1047: /* alternate screen */
2402 compatibility(OTHER);
2403 deselect(term);
2404 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
2405 term->disptop = 0;
2406 break;
2407 case 1048: /* save/restore cursor */
2408 if (!term->cfg.no_alt_screen)
2409 save_cursor(term, state);
2410 if (!state) seen_disp_event(term);
2411 break;
2412 case 1049: /* cursor & alternate screen */
2413 if (state && !term->cfg.no_alt_screen)
2414 save_cursor(term, state);
2415 if (!state) seen_disp_event(term);
2416 compatibility(OTHER);
2417 deselect(term);
2418 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
2419 if (!state && !term->cfg.no_alt_screen)
2420 save_cursor(term, state);
2421 term->disptop = 0;
2422 break;
2423 } else
2424 switch (mode) {
2425 case 4: /* IRM: set insert mode */
2426 compatibility(VT102);
2427 term->insert = state;
2428 break;
2429 case 12: /* SRM: set echo mode */
2430 term->term_echoing = !state;
2431 if (term->ldisc) /* cause ldisc to notice changes */
2432 ldisc_send(term->ldisc, NULL, 0, 0);
2433 break;
2434 case 20: /* LNM: Return sends ... */
2435 term->cr_lf_return = state;
2436 break;
2437 case 34: /* WYULCURM: Make cursor BIG */
2438 compatibility2(OTHER, VT220);
2439 term->big_cursor = !state;
2440 }
2441 }
2442
2443 /*
2444 * Process an OSC sequence: set window title or icon name.
2445 */
2446 static void do_osc(Terminal *term)
2447 {
2448 if (term->osc_w) {
2449 while (term->osc_strlen--)
2450 term->wordness[(unsigned char)
2451 term->osc_string[term->osc_strlen]] = term->esc_args[0];
2452 } else {
2453 term->osc_string[term->osc_strlen] = '\0';
2454 switch (term->esc_args[0]) {
2455 case 0:
2456 case 1:
2457 if (!term->cfg.no_remote_wintitle)
2458 set_icon(term->frontend, term->osc_string);
2459 if (term->esc_args[0] == 1)
2460 break;
2461 /* fall through: parameter 0 means set both */
2462 case 2:
2463 case 21:
2464 if (!term->cfg.no_remote_wintitle)
2465 set_title(term->frontend, term->osc_string);
2466 break;
2467 }
2468 }
2469 }
2470
2471 /*
2472 * ANSI printing routines.
2473 */
2474 static void term_print_setup(Terminal *term)
2475 {
2476 bufchain_clear(&term->printer_buf);
2477 term->print_job = printer_start_job(term->cfg.printer);
2478 }
2479 static void term_print_flush(Terminal *term)
2480 {
2481 void *data;
2482 int len;
2483 int size;
2484 while ((size = bufchain_size(&term->printer_buf)) > 5) {
2485 bufchain_prefix(&term->printer_buf, &data, &len);
2486 if (len > size-5)
2487 len = size-5;
2488 printer_job_data(term->print_job, data, len);
2489 bufchain_consume(&term->printer_buf, len);
2490 }
2491 }
2492 static void term_print_finish(Terminal *term)
2493 {
2494 void *data;
2495 int len, size;
2496 char c;
2497
2498 if (!term->printing && !term->only_printing)
2499 return; /* we need do nothing */
2500
2501 term_print_flush(term);
2502 while ((size = bufchain_size(&term->printer_buf)) > 0) {
2503 bufchain_prefix(&term->printer_buf, &data, &len);
2504 c = *(char *)data;
2505 if (c == '\033' || c == '\233') {
2506 bufchain_consume(&term->printer_buf, size);
2507 break;
2508 } else {
2509 printer_job_data(term->print_job, &c, 1);
2510 bufchain_consume(&term->printer_buf, 1);
2511 }
2512 }
2513 printer_finish_job(term->print_job);
2514 term->print_job = NULL;
2515 term->printing = term->only_printing = FALSE;
2516 }
2517
2518 /*
2519 * Remove everything currently in `inbuf' and stick it up on the
2520 * in-memory display. There's a big state machine in here to
2521 * process escape sequences...
2522 */
2523 static void term_out(Terminal *term)
2524 {
2525 unsigned long c;
2526 int unget;
2527 unsigned char localbuf[256], *chars;
2528 int nchars = 0;
2529
2530 unget = -1;
2531
2532 chars = NULL; /* placate compiler warnings */
2533 while (nchars > 0 || unget != -1 || bufchain_size(&term->inbuf) > 0) {
2534 if (unget == -1) {
2535 if (nchars == 0) {
2536 void *ret;
2537 bufchain_prefix(&term->inbuf, &ret, &nchars);
2538 if (nchars > sizeof(localbuf))
2539 nchars = sizeof(localbuf);
2540 memcpy(localbuf, ret, nchars);
2541 bufchain_consume(&term->inbuf, nchars);
2542 chars = localbuf;
2543 assert(chars != NULL);
2544 }
2545 c = *chars++;
2546 nchars--;
2547
2548 /*
2549 * Optionally log the session traffic to a file. Useful for
2550 * debugging and possibly also useful for actual logging.
2551 */
2552 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
2553 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
2554 } else {
2555 c = unget;
2556 unget = -1;
2557 }
2558
2559 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2560 * be able to display 8-bit characters, but I'll let that go 'cause
2561 * of i18n.
2562 */
2563
2564 /*
2565 * If we're printing, add the character to the printer
2566 * buffer.
2567 */
2568 if (term->printing) {
2569 bufchain_add(&term->printer_buf, &c, 1);
2570
2571 /*
2572 * If we're in print-only mode, we use a much simpler
2573 * state machine designed only to recognise the ESC[4i
2574 * termination sequence.
2575 */
2576 if (term->only_printing) {
2577 if (c == '\033')
2578 term->print_state = 1;
2579 else if (c == (unsigned char)'\233')
2580 term->print_state = 2;
2581 else if (c == '[' && term->print_state == 1)
2582 term->print_state = 2;
2583 else if (c == '4' && term->print_state == 2)
2584 term->print_state = 3;
2585 else if (c == 'i' && term->print_state == 3)
2586 term->print_state = 4;
2587 else
2588 term->print_state = 0;
2589 if (term->print_state == 4) {
2590 term_print_finish(term);
2591 }
2592 continue;
2593 }
2594 }
2595
2596 /* First see about all those translations. */
2597 if (term->termstate == TOPLEVEL) {
2598 if (in_utf(term))
2599 switch (term->utf_state) {
2600 case 0:
2601 if (c < 0x80) {
2602 /* UTF-8 must be stateless so we ignore iso2022. */
2603 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2604 c = term->ucsdata->unitab_ctrl[c];
2605 else c = ((unsigned char)c) | CSET_ASCII;
2606 break;
2607 } else if ((c & 0xe0) == 0xc0) {
2608 term->utf_size = term->utf_state = 1;
2609 term->utf_char = (c & 0x1f);
2610 } else if ((c & 0xf0) == 0xe0) {
2611 term->utf_size = term->utf_state = 2;
2612 term->utf_char = (c & 0x0f);
2613 } else if ((c & 0xf8) == 0xf0) {
2614 term->utf_size = term->utf_state = 3;
2615 term->utf_char = (c & 0x07);
2616 } else if ((c & 0xfc) == 0xf8) {
2617 term->utf_size = term->utf_state = 4;
2618 term->utf_char = (c & 0x03);
2619 } else if ((c & 0xfe) == 0xfc) {
2620 term->utf_size = term->utf_state = 5;
2621 term->utf_char = (c & 0x01);
2622 } else {
2623 c = UCSERR;
2624 break;
2625 }
2626 continue;
2627 case 1:
2628 case 2:
2629 case 3:
2630 case 4:
2631 case 5:
2632 if ((c & 0xC0) != 0x80) {
2633 unget = c;
2634 c = UCSERR;
2635 term->utf_state = 0;
2636 break;
2637 }
2638 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
2639 if (--term->utf_state)
2640 continue;
2641
2642 c = term->utf_char;
2643
2644 /* Is somebody trying to be evil! */
2645 if (c < 0x80 ||
2646 (c < 0x800 && term->utf_size >= 2) ||
2647 (c < 0x10000 && term->utf_size >= 3) ||
2648 (c < 0x200000 && term->utf_size >= 4) ||
2649 (c < 0x4000000 && term->utf_size >= 5))
2650 c = UCSERR;
2651
2652 /* Unicode line separator and paragraph separator are CR-LF */
2653 if (c == 0x2028 || c == 0x2029)
2654 c = 0x85;
2655
2656 /* High controls are probably a Baaad idea too. */
2657 if (c < 0xA0)
2658 c = 0xFFFD;
2659
2660 /* The UTF-16 surrogates are not nice either. */
2661 /* The standard give the option of decoding these:
2662 * I don't want to! */
2663 if (c >= 0xD800 && c < 0xE000)
2664 c = UCSERR;
2665
2666 /* ISO 10646 characters now limited to UTF-16 range. */
2667 if (c > 0x10FFFF)
2668 c = UCSERR;
2669
2670 /* This is currently a TagPhobic application.. */
2671 if (c >= 0xE0000 && c <= 0xE007F)
2672 continue;
2673
2674 /* U+FEFF is best seen as a null. */
2675 if (c == 0xFEFF)
2676 continue;
2677 /* But U+FFFE is an error. */
2678 if (c == 0xFFFE || c == 0xFFFF)
2679 c = UCSERR;
2680
2681 break;
2682 }
2683 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2684 else if(term->sco_acs &&
2685 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
2686 {
2687 if (term->sco_acs == 2) c |= 0x80;
2688 c |= CSET_SCOACS;
2689 } else {
2690 switch (term->cset_attr[term->cset]) {
2691 /*
2692 * Linedraw characters are different from 'ESC ( B'
2693 * only for a small range. For ones outside that
2694 * range, make sure we use the same font as well as
2695 * the same encoding.
2696 */
2697 case CSET_LINEDRW:
2698 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2699 c = term->ucsdata->unitab_ctrl[c];
2700 else
2701 c = ((unsigned char) c) | CSET_LINEDRW;
2702 break;
2703
2704 case CSET_GBCHR:
2705 /* If UK-ASCII, make the '#' a LineDraw Pound */
2706 if (c == '#') {
2707 c = '}' | CSET_LINEDRW;
2708 break;
2709 }
2710 /*FALLTHROUGH*/ case CSET_ASCII:
2711 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2712 c = term->ucsdata->unitab_ctrl[c];
2713 else
2714 c = ((unsigned char) c) | CSET_ASCII;
2715 break;
2716 case CSET_SCOACS:
2717 if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
2718 break;
2719 }
2720 }
2721 }
2722
2723 /*
2724 * How about C1 controls?
2725 * Explicitly ignore SCI (0x9a), which we don't translate to DECID.
2726 */
2727 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
2728 !term->vt52_mode && has_compat(VT220)) {
2729 if (c == 0x9a)
2730 c = 0;
2731 else {
2732 term->termstate = SEEN_ESC;
2733 term->esc_query = FALSE;
2734 c = '@' + (c & 0x1F);
2735 }
2736 }
2737
2738 /* Or the GL control. */
2739 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
2740 if (term->curs.x && !term->wrapnext)
2741 term->curs.x--;
2742 term->wrapnext = FALSE;
2743 /* destructive backspace might be disabled */
2744 if (!term->cfg.no_dbackspace) {
2745 check_boundary(term, term->curs.x, term->curs.y);
2746 check_boundary(term, term->curs.x+1, term->curs.y);
2747 copy_termchar(scrlineptr(term->curs.y),
2748 term->curs.x, &term->erase_char);
2749 }
2750 } else
2751 /* Or normal C0 controls. */
2752 if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {
2753 switch (c) {
2754 case '\005': /* ENQ: terminal type query */
2755 /*
2756 * Strictly speaking this is VT100 but a VT100 defaults to
2757 * no response. Other terminals respond at their option.
2758 *
2759 * Don't put a CR in the default string as this tends to
2760 * upset some weird software.
2761 */
2762 compatibility(ANSIMIN);
2763 if (term->ldisc) {
2764 char abuf[lenof(term->cfg.answerback)], *s, *d;
2765 for (s = term->cfg.answerback, d = abuf; *s;) {
2766 char *n;
2767 char c = ctrlparse(s, &n);
2768 if (n) {
2769 *d++ = c;
2770 s = n;
2771 } else {
2772 *d++ = *s++;
2773 }
2774 }
2775 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
2776 abuf, d - abuf, 0);
2777 }
2778 break;
2779 case '\007': /* BEL: Bell */
2780 {
2781 struct beeptime *newbeep;
2782 unsigned long ticks;
2783
2784 ticks = GETTICKCOUNT();
2785
2786 if (!term->beep_overloaded) {
2787 newbeep = snew(struct beeptime);
2788 newbeep->ticks = ticks;
2789 newbeep->next = NULL;
2790 if (!term->beephead)
2791 term->beephead = newbeep;
2792 else
2793 term->beeptail->next = newbeep;
2794 term->beeptail = newbeep;
2795 term->nbeeps++;
2796 }
2797
2798 /*
2799 * Throw out any beeps that happened more than
2800 * t seconds ago.
2801 */
2802 while (term->beephead &&
2803 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
2804 struct beeptime *tmp = term->beephead;
2805 term->beephead = tmp->next;
2806 sfree(tmp);
2807 if (!term->beephead)
2808 term->beeptail = NULL;
2809 term->nbeeps--;
2810 }
2811
2812 if (term->cfg.bellovl && term->beep_overloaded &&
2813 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
2814 /*
2815 * If we're currently overloaded and the
2816 * last beep was more than s seconds ago,
2817 * leave overload mode.
2818 */
2819 term->beep_overloaded = FALSE;
2820 } else if (term->cfg.bellovl && !term->beep_overloaded &&
2821 term->nbeeps >= term->cfg.bellovl_n) {
2822 /*
2823 * Now, if we have n or more beeps
2824 * remaining in the queue, go into overload
2825 * mode.
2826 */
2827 term->beep_overloaded = TRUE;
2828 }
2829 term->lastbeep = ticks;
2830
2831 /*
2832 * Perform an actual beep if we're not overloaded.
2833 */
2834 if (!term->cfg.bellovl || !term->beep_overloaded) {
2835 do_beep(term->frontend, term->cfg.beep);
2836
2837 if (term->cfg.beep == BELL_VISUAL) {
2838 term_schedule_vbell(term, FALSE, 0);
2839 }
2840 }
2841 seen_disp_event(term);
2842 }
2843 break;
2844 case '\b': /* BS: Back space */
2845 if (term->curs.x == 0 &&
2846 (term->curs.y == 0 || term->wrap == 0))
2847 /* do nothing */ ;
2848 else if (term->curs.x == 0 && term->curs.y > 0)
2849 term->curs.x = term->cols - 1, term->curs.y--;
2850 else if (term->wrapnext)
2851 term->wrapnext = FALSE;
2852 else
2853 term->curs.x--;
2854 seen_disp_event(term);
2855 break;
2856 case '\016': /* LS1: Locking-shift one */
2857 compatibility(VT100);
2858 term->cset = 1;
2859 break;
2860 case '\017': /* LS0: Locking-shift zero */
2861 compatibility(VT100);
2862 term->cset = 0;
2863 break;
2864 case '\033': /* ESC: Escape */
2865 if (term->vt52_mode)
2866 term->termstate = VT52_ESC;
2867 else {
2868 compatibility(ANSIMIN);
2869 term->termstate = SEEN_ESC;
2870 term->esc_query = FALSE;
2871 }
2872 break;
2873 case '\015': /* CR: Carriage return */
2874 term->curs.x = 0;
2875 term->wrapnext = FALSE;
2876 seen_disp_event(term);
2877 term->paste_hold = 0;
2878
2879 if (term->cfg.crhaslf) {
2880 if (term->curs.y == term->marg_b)
2881 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2882 else if (term->curs.y < term->rows - 1)
2883 term->curs.y++;
2884 }
2885 if (term->logctx)
2886 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2887 break;
2888 case '\014': /* FF: Form feed */
2889 if (has_compat(SCOANSI)) {
2890 move(term, 0, 0, 0);
2891 erase_lots(term, FALSE, FALSE, TRUE);
2892 term->disptop = 0;
2893 term->wrapnext = FALSE;
2894 seen_disp_event(term);
2895 break;
2896 }
2897 case '\013': /* VT: Line tabulation */
2898 compatibility(VT100);
2899 case '\012': /* LF: Line feed */
2900 if (term->curs.y == term->marg_b)
2901 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2902 else if (term->curs.y < term->rows - 1)
2903 term->curs.y++;
2904 if (term->cfg.lfhascr)
2905 term->curs.x = 0;
2906 term->wrapnext = FALSE;
2907 seen_disp_event(term);
2908 term->paste_hold = 0;
2909 if (term->logctx)
2910 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2911 break;
2912 case '\t': /* HT: Character tabulation */
2913 {
2914 pos old_curs = term->curs;
2915 termline *ldata = scrlineptr(term->curs.y);
2916
2917 do {
2918 term->curs.x++;
2919 } while (term->curs.x < term->cols - 1 &&
2920 !term->tabs[term->curs.x]);
2921
2922 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {
2923 if (term->curs.x >= term->cols / 2)
2924 term->curs.x = term->cols / 2 - 1;
2925 } else {
2926 if (term->curs.x >= term->cols)
2927 term->curs.x = term->cols - 1;
2928 }
2929
2930 check_selection(term, old_curs, term->curs);
2931 }
2932 seen_disp_event(term);
2933 break;
2934 }
2935 } else
2936 switch (term->termstate) {
2937 case TOPLEVEL:
2938 /* Only graphic characters get this far;
2939 * ctrls are stripped above */
2940 {
2941 termline *cline = scrlineptr(term->curs.y);
2942 int width = 0;
2943 if (DIRECT_CHAR(c))
2944 width = 1;
2945 if (!width)
2946 width = (term->cfg.cjk_ambig_wide ?
2947 mk_wcwidth_cjk((wchar_t) c) :
2948 mk_wcwidth((wchar_t) c));
2949
2950 if (term->wrapnext && term->wrap && width > 0) {
2951 cline->lattr |= LATTR_WRAPPED;
2952 if (term->curs.y == term->marg_b)
2953 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2954 else if (term->curs.y < term->rows - 1)
2955 term->curs.y++;
2956 term->curs.x = 0;
2957 term->wrapnext = FALSE;
2958 cline = scrlineptr(term->curs.y);
2959 }
2960 if (term->insert && width > 0)
2961 insch(term, width);
2962 if (term->selstate != NO_SELECTION) {
2963 pos cursplus = term->curs;
2964 incpos(cursplus);
2965 check_selection(term, term->curs, cursplus);
2966 }
2967 if (((c & CSET_MASK) == CSET_ASCII ||
2968 (c & CSET_MASK) == 0) &&
2969 term->logctx)
2970 logtraffic(term->logctx, (unsigned char) c,
2971 LGTYP_ASCII);
2972
2973 switch (width) {
2974 case 2:
2975 /*
2976 * If we're about to display a double-width
2977 * character starting in the rightmost
2978 * column, then we do something special
2979 * instead. We must print a space in the
2980 * last column of the screen, then wrap;
2981 * and we also set LATTR_WRAPPED2 which
2982 * instructs subsequent cut-and-pasting not
2983 * only to splice this line to the one
2984 * after it, but to ignore the space in the
2985 * last character position as well.
2986 * (Because what was actually output to the
2987 * terminal was presumably just a sequence
2988 * of CJK characters, and we don't want a
2989 * space to be pasted in the middle of
2990 * those just because they had the
2991 * misfortune to start in the wrong parity
2992 * column. xterm concurs.)
2993 */
2994 check_boundary(term, term->curs.x, term->curs.y);
2995 check_boundary(term, term->curs.x+2, term->curs.y);
2996 if (term->curs.x == term->cols-1) {
2997 copy_termchar(cline, term->curs.x,
2998 &term->erase_char);
2999 cline->lattr |= LATTR_WRAPPED | LATTR_WRAPPED2;
3000 if (term->curs.y == term->marg_b)
3001 scroll(term, term->marg_t, term->marg_b,
3002 1, TRUE);
3003 else if (term->curs.y < term->rows - 1)
3004 term->curs.y++;
3005 term->curs.x = 0;
3006 cline = scrlineptr(term->curs.y);
3007 /* Now we must check_boundary again, of course. */
3008 check_boundary(term, term->curs.x, term->curs.y);
3009 check_boundary(term, term->curs.x+2, term->curs.y);
3010 }
3011
3012 /* FULL-TERMCHAR */
3013 clear_cc(cline, term->curs.x);
3014 cline->chars[term->curs.x].chr = c;
3015 cline->chars[term->curs.x].attr = term->curr_attr;
3016
3017 term->curs.x++;
3018
3019 /* FULL-TERMCHAR */
3020 clear_cc(cline, term->curs.x);
3021 cline->chars[term->curs.x].chr = UCSWIDE;
3022 cline->chars[term->curs.x].attr = term->curr_attr;
3023
3024 break;
3025 case 1:
3026 check_boundary(term, term->curs.x, term->curs.y);
3027 check_boundary(term, term->curs.x+1, term->curs.y);
3028
3029 /* FULL-TERMCHAR */
3030 clear_cc(cline, term->curs.x);
3031 cline->chars[term->curs.x].chr = c;
3032 cline->chars[term->curs.x].attr = term->curr_attr;
3033
3034 break;
3035 case 0:
3036 if (term->curs.x > 0) {
3037 int x = term->curs.x - 1;
3038
3039 /* If we're in wrapnext state, the character
3040 * to combine with is _here_, not to our left. */
3041 if (term->wrapnext)
3042 x++;
3043
3044 /*
3045 * If the previous character is
3046 * UCSWIDE, back up another one.
3047 */
3048 if (cline->chars[x].chr == UCSWIDE) {
3049 assert(x > 0);
3050 x--;
3051 }
3052
3053 add_cc(cline, x, c);
3054 seen_disp_event(term);
3055 }
3056 continue;
3057 default:
3058 continue;
3059 }
3060 term->curs.x++;
3061 if (term->curs.x == term->cols) {
3062 term->curs.x--;
3063 term->wrapnext = TRUE;
3064 if (term->wrap && term->vt52_mode) {
3065 cline->lattr |= LATTR_WRAPPED;
3066 if (term->curs.y == term->marg_b)
3067 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
3068 else if (term->curs.y < term->rows - 1)
3069 term->curs.y++;
3070 term->curs.x = 0;
3071 term->wrapnext = FALSE;
3072 }
3073 }
3074 seen_disp_event(term);
3075 }
3076 break;
3077
3078 case OSC_MAYBE_ST:
3079 /*
3080 * This state is virtually identical to SEEN_ESC, with the
3081 * exception that we have an OSC sequence in the pipeline,
3082 * and _if_ we see a backslash, we process it.
3083 */
3084 if (c == '\\') {
3085 do_osc(term);
3086 term->termstate = TOPLEVEL;
3087 break;
3088 }
3089 /* else fall through */
3090 case SEEN_ESC:
3091 if (c >= ' ' && c <= '/') {
3092 if (term->esc_query)
3093 term->esc_query = -1;
3094 else
3095 term->esc_query = c;
3096 break;
3097 }
3098 term->termstate = TOPLEVEL;
3099 switch (ANSI(c, term->esc_query)) {
3100 case '[': /* enter CSI mode */
3101 term->termstate = SEEN_CSI;
3102 term->esc_nargs = 1;
3103 term->esc_args[0] = ARG_DEFAULT;
3104 term->esc_query = FALSE;
3105 break;
3106 case ']': /* OSC: xterm escape sequences */
3107 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
3108 compatibility(OTHER);
3109 term->termstate = SEEN_OSC;
3110 term->esc_args[0] = 0;
3111 break;
3112 case '7': /* DECSC: save cursor */
3113 compatibility(VT100);
3114 save_cursor(term, TRUE);
3115 break;
3116 case '8': /* DECRC: restore cursor */
3117 compatibility(VT100);
3118 save_cursor(term, FALSE);
3119 seen_disp_event(term);
3120 break;
3121 case '=': /* DECKPAM: Keypad application mode */
3122 compatibility(VT100);
3123 term->app_keypad_keys = TRUE;
3124 break;
3125 case '>': /* DECKPNM: Keypad numeric mode */
3126 compatibility(VT100);
3127 term->app_keypad_keys = FALSE;
3128 break;
3129 case 'D': /* IND: exactly equivalent to LF */
3130 compatibility(VT100);
3131 if (term->curs.y == term->marg_b)
3132 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
3133 else if (term->curs.y < term->rows - 1)
3134 term->curs.y++;
3135 term->wrapnext = FALSE;
3136 seen_disp_event(term);
3137 break;
3138 case 'E': /* NEL: exactly equivalent to CR-LF */
3139 compatibility(VT100);
3140 term->curs.x = 0;
3141 if (term->curs.y == term->marg_b)
3142 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
3143 else if (term->curs.y < term->rows - 1)
3144 term->curs.y++;
3145 term->wrapnext = FALSE;
3146 seen_disp_event(term);
3147 break;
3148 case 'M': /* RI: reverse index - backwards LF */
3149 compatibility(VT100);
3150 if (term->curs.y == term->marg_t)
3151 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
3152 else if (term->curs.y > 0)
3153 term->curs.y--;
3154 term->wrapnext = FALSE;
3155 seen_disp_event(term);
3156 break;
3157 case 'Z': /* DECID: terminal type query */
3158 compatibility(VT100);
3159 if (term->ldisc)
3160 ldisc_send(term->ldisc, term->id_string,
3161 strlen(term->id_string), 0);
3162 break;
3163 case 'c': /* RIS: restore power-on settings */
3164 compatibility(VT100);
3165 power_on(term, TRUE);
3166 if (term->ldisc) /* cause ldisc to notice changes */
3167 ldisc_send(term->ldisc, NULL, 0, 0);
3168 if (term->reset_132) {
3169 if (!term->cfg.no_remote_resize)
3170 request_resize(term->frontend, 80, term->rows);
3171 term->reset_132 = 0;
3172 }
3173 term->disptop = 0;
3174 seen_disp_event(term);
3175 break;
3176 case 'H': /* HTS: set a tab */
3177 compatibility(VT100);
3178 term->tabs[term->curs.x] = TRUE;
3179 break;
3180
3181 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
3182 compatibility(VT100);
3183 {
3184 termline *ldata;
3185 int i, j;
3186 pos scrtop, scrbot;
3187
3188 for (i = 0; i < term->rows; i++) {
3189 ldata = scrlineptr(i);
3190 for (j = 0; j < term->cols; j++) {
3191 copy_termchar(ldata, j,
3192 &term->basic_erase_char);
3193 ldata->chars[j].chr = 'E';
3194 }
3195 ldata->lattr = LATTR_NORM;
3196 }
3197 term->disptop = 0;
3198 seen_disp_event(term);
3199 scrtop.x = scrtop.y = 0;
3200 scrbot.x = 0;
3201 scrbot.y = term->rows;
3202 check_selection(term, scrtop, scrbot);
3203 }
3204 break;
3205
3206 case ANSI('3', '#'):
3207 case ANSI('4', '#'):
3208 case ANSI('5', '#'):
3209 case ANSI('6', '#'):
3210 compatibility(VT100);
3211 {
3212 int nlattr;
3213
3214 switch (ANSI(c, term->esc_query)) {
3215 case ANSI('3', '#'): /* DECDHL: 2*height, top */
3216 nlattr = LATTR_TOP;
3217 break;
3218 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
3219 nlattr = LATTR_BOT;
3220 break;
3221 case ANSI('5', '#'): /* DECSWL: normal */
3222 nlattr = LATTR_NORM;
3223 break;
3224 default: /* case ANSI('6', '#'): DECDWL: 2*width */
3225 nlattr = LATTR_WIDE;
3226 break;
3227 }
3228 scrlineptr(term->curs.y)->lattr = nlattr;
3229 }
3230 break;
3231 /* GZD4: G0 designate 94-set */
3232 case ANSI('A', '('):
3233 compatibility(VT100);
3234 if (!term->cfg.no_remote_charset)
3235 term->cset_attr[0] = CSET_GBCHR;
3236 break;
3237 case ANSI('B', '('):
3238 compatibility(VT100);
3239 if (!term->cfg.no_remote_charset)
3240 term->cset_attr[0] = CSET_ASCII;
3241 break;
3242 case ANSI('0', '('):
3243 compatibility(VT100);
3244 if (!term->cfg.no_remote_charset)
3245 term->cset_attr[0] = CSET_LINEDRW;
3246 break;
3247 case ANSI('U', '('):
3248 compatibility(OTHER);
3249 if (!term->cfg.no_remote_charset)
3250 term->cset_attr[0] = CSET_SCOACS;
3251 break;
3252 /* G1D4: G1-designate 94-set */
3253 case ANSI('A', ')'):
3254 compatibility(VT100);
3255 if (!term->cfg.no_remote_charset)
3256 term->cset_attr[1] = CSET_GBCHR;
3257 break;
3258 case ANSI('B', ')'):
3259 compatibility(VT100);
3260 if (!term->cfg.no_remote_charset)
3261 term->cset_attr[1] = CSET_ASCII;
3262 break;
3263 case ANSI('0', ')'):
3264 compatibility(VT100);
3265 if (!term->cfg.no_remote_charset)
3266 term->cset_attr[1] = CSET_LINEDRW;
3267 break;
3268 case ANSI('U', ')'):
3269 compatibility(OTHER);
3270 if (!term->cfg.no_remote_charset)
3271 term->cset_attr[1] = CSET_SCOACS;
3272 break;
3273 /* DOCS: Designate other coding system */
3274 case ANSI('8', '%'): /* Old Linux code */
3275 case ANSI('G', '%'):
3276 compatibility(OTHER);
3277 if (!term->cfg.no_remote_charset)
3278 term->utf = 1;
3279 break;
3280 case ANSI('@', '%'):
3281 compatibility(OTHER);
3282 if (!term->cfg.no_remote_charset)
3283 term->utf = 0;
3284 break;
3285 }
3286 break;
3287 case SEEN_CSI:
3288 term->termstate = TOPLEVEL; /* default */
3289 if (isdigit(c)) {
3290 if (term->esc_nargs <= ARGS_MAX) {
3291 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
3292 term->esc_args[term->esc_nargs - 1] = 0;
3293 term->esc_args[term->esc_nargs - 1] =
3294 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
3295 }
3296 term->termstate = SEEN_CSI;
3297 } else if (c == ';') {
3298 if (term->esc_nargs < ARGS_MAX)
3299 term->esc_args[term->esc_nargs++] = ARG_DEFAULT;
3300 term->termstate = SEEN_CSI;
3301 } else if (c < '@') {
3302 if (term->esc_query)
3303 term->esc_query = -1;
3304 else if (c == '?')
3305 term->esc_query = TRUE;
3306 else
3307 term->esc_query = c;
3308 term->termstate = SEEN_CSI;
3309 } else
3310 switch (ANSI(c, term->esc_query)) {
3311 case 'A': /* CUU: move up N lines */
3312 move(term, term->curs.x,
3313 term->curs.y - def(term->esc_args[0], 1), 1);
3314 seen_disp_event(term);
3315 break;
3316 case 'e': /* VPR: move down N lines */
3317 compatibility(ANSI);
3318 /* FALLTHROUGH */
3319 case 'B': /* CUD: Cursor down */
3320 move(term, term->curs.x,
3321 term->curs.y + def(term->esc_args[0], 1), 1);
3322 seen_disp_event(term);
3323 break;
3324 case ANSI('c', '>'): /* DA: report xterm version */
3325 compatibility(OTHER);
3326 /* this reports xterm version 136 so that VIM can
3327 use the drag messages from the mouse reporting */
3328 if (term->ldisc)
3329 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
3330 break;
3331 case 'a': /* HPR: move right N cols */
3332 compatibility(ANSI);
3333 /* FALLTHROUGH */
3334 case 'C': /* CUF: Cursor right */
3335 move(term, term->curs.x + def(term->esc_args[0], 1),
3336 term->curs.y, 1);
3337 seen_disp_event(term);
3338 break;
3339 case 'D': /* CUB: move left N cols */
3340 move(term, term->curs.x - def(term->esc_args[0], 1),
3341 term->curs.y, 1);
3342 seen_disp_event(term);
3343 break;
3344 case 'E': /* CNL: move down N lines and CR */
3345 compatibility(ANSI);
3346 move(term, 0,
3347 term->curs.y + def(term->esc_args[0], 1), 1);
3348 seen_disp_event(term);
3349 break;
3350 case 'F': /* CPL: move up N lines and CR */
3351 compatibility(ANSI);
3352 move(term, 0,
3353 term->curs.y - def(term->esc_args[0], 1), 1);
3354 seen_disp_event(term);
3355 break;
3356 case 'G': /* CHA */
3357 case '`': /* HPA: set horizontal posn */
3358 compatibility(ANSI);
3359 move(term, def(term->esc_args[0], 1) - 1,
3360 term->curs.y, 0);
3361 seen_disp_event(term);
3362 break;
3363 case 'd': /* VPA: set vertical posn */
3364 compatibility(ANSI);
3365 move(term, term->curs.x,
3366 ((term->dec_om ? term->marg_t : 0) +
3367 def(term->esc_args[0], 1) - 1),
3368 (term->dec_om ? 2 : 0));
3369 seen_disp_event(term);
3370 break;
3371 case 'H': /* CUP */
3372 case 'f': /* HVP: set horz and vert posns at once */
3373 if (term->esc_nargs < 2)
3374 term->esc_args[1] = ARG_DEFAULT;
3375 move(term, def(term->esc_args[1], 1) - 1,
3376 ((term->dec_om ? term->marg_t : 0) +
3377 def(term->esc_args[0], 1) - 1),
3378 (term->dec_om ? 2 : 0));
3379 seen_disp_event(term);
3380 break;
3381 case 'J': /* ED: erase screen or parts of it */
3382 {
3383 unsigned int i = def(term->esc_args[0], 0);
3384 if (i == 3) {
3385 /* Erase Saved Lines (xterm)
3386 * This follows Thomas Dickey's xterm. */
3387 term_clrsb(term);
3388 } else {
3389 i++;
3390 if (i > 3)
3391 i = 0;
3392 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
3393 }
3394 }
3395 term->disptop = 0;
3396 seen_disp_event(term);
3397 break;
3398 case 'K': /* EL: erase line or parts of it */
3399 {
3400 unsigned int i = def(term->esc_args[0], 0) + 1;
3401 if (i > 3)
3402 i = 0;
3403 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
3404 }
3405 seen_disp_event(term);
3406 break;
3407 case 'L': /* IL: insert lines */
3408 compatibility(VT102);
3409 if (term->curs.y <= term->marg_b)
3410 scroll(term, term->curs.y, term->marg_b,
3411 -def(term->esc_args[0], 1), FALSE);
3412 seen_disp_event(term);
3413 break;
3414 case 'M': /* DL: delete lines */
3415 compatibility(VT102);
3416 if (term->curs.y <= term->marg_b)
3417 scroll(term, term->curs.y, term->marg_b,
3418 def(term->esc_args[0], 1),
3419 TRUE);
3420 seen_disp_event(term);
3421 break;
3422 case '@': /* ICH: insert chars */
3423 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3424 compatibility(VT102);
3425 insch(term, def(term->esc_args[0], 1));
3426 seen_disp_event(term);
3427 break;
3428 case 'P': /* DCH: delete chars */
3429 compatibility(VT102);
3430 insch(term, -def(term->esc_args[0], 1));
3431 seen_disp_event(term);
3432 break;
3433 case 'c': /* DA: terminal type query */
3434 compatibility(VT100);
3435 /* This is the response for a VT102 */
3436 if (term->ldisc)
3437 ldisc_send(term->ldisc, term->id_string,
3438 strlen(term->id_string), 0);
3439 break;
3440 case 'n': /* DSR: cursor position query */
3441 if (term->ldisc) {
3442 if (term->esc_args[0] == 6) {
3443 char buf[32];
3444 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
3445 term->curs.x + 1);
3446 ldisc_send(term->ldisc, buf, strlen(buf), 0);
3447 } else if (term->esc_args[0] == 5) {
3448 ldisc_send(term->ldisc, "\033[0n", 4, 0);
3449 }
3450 }
3451 break;
3452 case 'h': /* SM: toggle modes to high */
3453 case ANSI_QUE('h'):
3454 compatibility(VT100);
3455 {
3456 int i;
3457 for (i = 0; i < term->esc_nargs; i++)
3458 toggle_mode(term, term->esc_args[i],
3459 term->esc_query, TRUE);
3460 }
3461 break;
3462 case 'i': /* MC: Media copy */
3463 case ANSI_QUE('i'):
3464 compatibility(VT100);
3465 {
3466 if (term->esc_nargs != 1) break;
3467 if (term->esc_args[0] == 5 && *term->cfg.printer) {
3468 term->printing = TRUE;
3469 term->only_printing = !term->esc_query;
3470 term->print_state = 0;
3471 term_print_setup(term);
3472 } else if (term->esc_args[0] == 4 &&
3473 term->printing) {
3474 term_print_finish(term);
3475 }
3476 }
3477 break;
3478 case 'l': /* RM: toggle modes to low */
3479 case ANSI_QUE('l'):
3480 compatibility(VT100);
3481 {
3482 int i;
3483 for (i = 0; i < term->esc_nargs; i++)
3484 toggle_mode(term, term->esc_args[i],
3485 term->esc_query, FALSE);
3486 }
3487 break;
3488 case 'g': /* TBC: clear tabs */
3489 compatibility(VT100);
3490 if (term->esc_nargs == 1) {
3491 if (term->esc_args[0] == 0) {
3492 term->tabs[term->curs.x] = FALSE;
3493 } else if (term->esc_args[0] == 3) {
3494 int i;
3495 for (i = 0; i < term->cols; i++)
3496 term->tabs[i] = FALSE;
3497 }
3498 }
3499 break;
3500 case 'r': /* DECSTBM: set scroll margins */
3501 compatibility(VT100);
3502 if (term->esc_nargs <= 2) {
3503 int top, bot;
3504 top = def(term->esc_args[0], 1) - 1;
3505 bot = (term->esc_nargs <= 1
3506 || term->esc_args[1] == 0 ?
3507 term->rows :
3508 def(term->esc_args[1], term->rows)) - 1;
3509 if (bot >= term->rows)
3510 bot = term->rows - 1;
3511 /* VTTEST Bug 9 - if region is less than 2 lines
3512 * don't change region.
3513 */
3514 if (bot - top > 0) {
3515 term->marg_t = top;
3516 term->marg_b = bot;
3517 term->curs.x = 0;
3518 /*
3519 * I used to think the cursor should be
3520 * placed at the top of the newly marginned
3521 * area. Apparently not: VMS TPU falls over
3522 * if so.
3523 *
3524 * Well actually it should for
3525 * Origin mode - RDB
3526 */
3527 term->curs.y = (term->dec_om ?
3528 term->marg_t : 0);
3529 seen_disp_event(term);
3530 }
3531 }
3532 break;
3533 case 'm': /* SGR: set graphics rendition */
3534 {
3535 /*
3536 * A VT100 without the AVO only had one
3537 * attribute, either underline or
3538 * reverse video depending on the
3539 * cursor type, this was selected by
3540 * CSI 7m.
3541 *
3542 * case 2:
3543 * This is sometimes DIM, eg on the
3544 * GIGI and Linux
3545 * case 8:
3546 * This is sometimes INVIS various ANSI.
3547 * case 21:
3548 * This like 22 disables BOLD, DIM and INVIS
3549 *
3550 * The ANSI colours appear on any
3551 * terminal that has colour (obviously)
3552 * but the interaction between sgr0 and
3553 * the colours varies but is usually
3554 * related to the background colour
3555 * erase item. The interaction between
3556 * colour attributes and the mono ones
3557 * is also very implementation
3558 * dependent.
3559 *
3560 * The 39 and 49 attributes are likely
3561 * to be unimplemented.
3562 */
3563 int i;
3564 for (i = 0; i < term->esc_nargs; i++) {
3565 switch (def(term->esc_args[i], 0)) {
3566 case 0: /* restore defaults */
3567 term->curr_attr = term->default_attr;
3568 break;
3569 case 1: /* enable bold */
3570 compatibility(VT100AVO);
3571 term->curr_attr |= ATTR_BOLD;
3572 break;
3573 case 21: /* (enable double underline) */
3574 compatibility(OTHER);
3575 case 4: /* enable underline */
3576 compatibility(VT100AVO);
3577 term->curr_attr |= ATTR_UNDER;
3578 break;
3579 case 5: /* enable blink */
3580 compatibility(VT100AVO);
3581 term->curr_attr |= ATTR_BLINK;
3582 break;
3583 case 6: /* SCO light bkgrd */
3584 compatibility(SCOANSI);
3585 term->blink_is_real = FALSE;
3586 term->curr_attr |= ATTR_BLINK;
3587 term_schedule_tblink(term);
3588 break;
3589 case 7: /* enable reverse video */
3590 term->curr_attr |= ATTR_REVERSE;
3591 break;
3592 case 10: /* SCO acs off */
3593 compatibility(SCOANSI);
3594 if (term->cfg.no_remote_charset) break;
3595 term->sco_acs = 0; break;
3596 case 11: /* SCO acs on */
3597 compatibility(SCOANSI);
3598 if (term->cfg.no_remote_charset) break;
3599 term->sco_acs = 1; break;
3600 case 12: /* SCO acs on, |0x80 */
3601 compatibility(SCOANSI);
3602 if (term->cfg.no_remote_charset) break;
3603 term->sco_acs = 2; break;
3604 case 22: /* disable bold */
3605 compatibility2(OTHER, VT220);
3606 term->curr_attr &= ~ATTR_BOLD;
3607 break;
3608 case 24: /* disable underline */
3609 compatibility2(OTHER, VT220);
3610 term->curr_attr &= ~ATTR_UNDER;
3611 break;
3612 case 25: /* disable blink */
3613 compatibility2(OTHER, VT220);
3614 term->curr_attr &= ~ATTR_BLINK;
3615 break;
3616 case 27: /* disable reverse video */
3617 compatibility2(OTHER, VT220);
3618 term->curr_attr &= ~ATTR_REVERSE;
3619 break;
3620 case 30:
3621 case 31:
3622 case 32:
3623 case 33:
3624 case 34:
3625 case 35:
3626 case 36:
3627 case 37:
3628 /* foreground */
3629 term->curr_attr &= ~ATTR_FGMASK;
3630 term->curr_attr |=
3631 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
3632 break;
3633 case 90:
3634 case 91:
3635 case 92:
3636 case 93:
3637 case 94:
3638 case 95:
3639 case 96:
3640 case 97:
3641 /* aixterm-style bright foreground */
3642 term->curr_attr &= ~ATTR_FGMASK;
3643 term->curr_attr |=
3644 ((term->esc_args[i] - 90 + 8)
3645 << ATTR_FGSHIFT);
3646 break;
3647 case 39: /* default-foreground */
3648 term->curr_attr &= ~ATTR_FGMASK;
3649 term->curr_attr |= ATTR_DEFFG;
3650 break;
3651 case 40:
3652 case 41:
3653 case 42:
3654 case 43:
3655 case 44:
3656 case 45:
3657 case 46:
3658 case 47:
3659 /* background */
3660 term->curr_attr &= ~ATTR_BGMASK;
3661 term->curr_attr |=
3662 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
3663 break;
3664 case 100:
3665 case 101:
3666 case 102:
3667 case 103:
3668 case 104:
3669 case 105:
3670 case 106:
3671 case 107:
3672 /* aixterm-style bright background */
3673 term->curr_attr &= ~ATTR_BGMASK;
3674 term->curr_attr |=
3675 ((term->esc_args[i] - 100 + 8)
3676 << ATTR_BGSHIFT);
3677 break;
3678 case 49: /* default-background */
3679 term->curr_attr &= ~ATTR_BGMASK;
3680 term->curr_attr |= ATTR_DEFBG;
3681 break;
3682 case 38: /* xterm 256-colour mode */
3683 if (i+2 < term->esc_nargs &&
3684 term->esc_args[i+1] == 5) {
3685 term->curr_attr &= ~ATTR_FGMASK;
3686 term->curr_attr |=
3687 ((term->esc_args[i+2] & 0xFF)
3688 << ATTR_FGSHIFT);
3689 i += 2;
3690 }
3691 break;
3692 case 48: /* xterm 256-colour mode */
3693 if (i+2 < term->esc_nargs &&
3694 term->esc_args[i+1] == 5) {
3695 term->curr_attr &= ~ATTR_BGMASK;
3696 term->curr_attr |=
3697 ((term->esc_args[i+2] & 0xFF)
3698 << ATTR_BGSHIFT);
3699 i += 2;
3700 }
3701 break;
3702 }
3703 }
3704 set_erase_char(term);
3705 }
3706 break;
3707 case 's': /* save cursor */
3708 save_cursor(term, TRUE);
3709 break;
3710 case 'u': /* restore cursor */
3711 save_cursor(term, FALSE);
3712 seen_disp_event(term);
3713 break;
3714 case 't': /* DECSLPP: set page size - ie window height */
3715 /*
3716 * VT340/VT420 sequence DECSLPP, DEC only allows values
3717 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3718 * illegal values (eg first arg 1..9) for window changing
3719 * and reports.
3720 */
3721 if (term->esc_nargs <= 1
3722 && (term->esc_args[0] < 1 ||
3723 term->esc_args[0] >= 24)) {
3724 compatibility(VT340TEXT);
3725 if (!term->cfg.no_remote_resize)
3726 request_resize(term->frontend, term->cols,
3727 def(term->esc_args[0], 24));
3728 deselect(term);
3729 } else if (term->esc_nargs >= 1 &&
3730 term->esc_args[0] >= 1 &&
3731 term->esc_args[0] < 24) {
3732 compatibility(OTHER);
3733
3734 switch (term->esc_args[0]) {
3735 int x, y, len;
3736 char buf[80], *p;
3737 case 1:
3738 set_iconic(term->frontend, FALSE);
3739 break;
3740 case 2:
3741 set_iconic(term->frontend, TRUE);
3742 break;
3743 case 3:
3744 if (term->esc_nargs >= 3) {
3745 if (!term->cfg.no_remote_resize)
3746 move_window(term->frontend,
3747 def(term->esc_args[1], 0),
3748 def(term->esc_args[2], 0));
3749 }
3750 break;
3751 case 4:
3752 /* We should resize the window to a given
3753 * size in pixels here, but currently our
3754 * resizing code isn't healthy enough to
3755 * manage it. */
3756 break;
3757 case 5:
3758 /* move to top */
3759 set_zorder(term->frontend, TRUE);
3760 break;
3761 case 6:
3762 /* move to bottom */
3763 set_zorder(term->frontend, FALSE);
3764 break;
3765 case 7:
3766 refresh_window(term->frontend);
3767 break;
3768 case 8:
3769 if (term->esc_nargs >= 3) {
3770 if (!term->cfg.no_remote_resize)
3771 request_resize(term->frontend,
3772 def(term->esc_args[2], term->cfg.width),
3773 def(term->esc_args[1], term->cfg.height));
3774 }
3775 break;
3776 case 9:
3777 if (term->esc_nargs >= 2)
3778 set_zoomed(term->frontend,
3779 term->esc_args[1] ?
3780 TRUE : FALSE);
3781 break;
3782 case 11:
3783 if (term->ldisc)
3784 ldisc_send(term->ldisc,
3785 is_iconic(term->frontend) ?
3786 "\033[2t" : "\033[1t", 4, 0);
3787 break;
3788 case 13:
3789 if (term->ldisc) {
3790 get_window_pos(term->frontend, &x, &y);
3791 len = sprintf(buf, "\033[3;%d;%dt", x, y);
3792 ldisc_send(term->ldisc, buf, len, 0);
3793 }
3794 break;
3795 case 14:
3796 if (term->ldisc) {
3797 get_window_pixels(term->frontend, &x, &y);
3798 len = sprintf(buf, "\033[4;%d;%dt", y, x);
3799 ldisc_send(term->ldisc, buf, len, 0);
3800 }
3801 break;
3802 case 18:
3803 if (term->ldisc) {
3804 len = sprintf(buf, "\033[8;%d;%dt",
3805 term->rows, term->cols);
3806 ldisc_send(term->ldisc, buf, len, 0);
3807 }
3808 break;
3809 case 19:
3810 /*
3811 * Hmmm. Strictly speaking we
3812 * should return `the size of the
3813 * screen in characters', but
3814 * that's not easy: (a) window
3815 * furniture being what it is it's
3816 * hard to compute, and (b) in
3817 * resize-font mode maximising the
3818 * window wouldn't change the
3819 * number of characters. *shrug*. I
3820 * think we'll ignore it for the
3821 * moment and see if anyone
3822 * complains, and then ask them
3823 * what they would like it to do.
3824 */
3825 break;
3826 case 20:
3827 if (term->ldisc &&
3828 term->cfg.remote_qtitle_action != TITLE_NONE) {
3829 if(term->cfg.remote_qtitle_action == TITLE_REAL)
3830 p = get_window_title(term->frontend, TRUE);
3831 else
3832 p = EMPTY_WINDOW_TITLE;
3833 len = strlen(p);
3834 ldisc_send(term->ldisc, "\033]L", 3, 0);
3835 ldisc_send(term->ldisc, p, len, 0);
3836 ldisc_send(term->ldisc, "\033\\", 2, 0);
3837 }
3838 break;
3839 case 21:
3840 if (term->ldisc &&
3841 term->cfg.remote_qtitle_action != TITLE_NONE) {
3842 if(term->cfg.remote_qtitle_action == TITLE_REAL)
3843 p = get_window_title(term->frontend, FALSE);
3844 else
3845 p = EMPTY_WINDOW_TITLE;
3846 len = strlen(p);
3847 ldisc_send(term->ldisc, "\033]l", 3, 0);
3848 ldisc_send(term->ldisc, p, len, 0);
3849 ldisc_send(term->ldisc, "\033\\", 2, 0);
3850 }
3851 break;
3852 }
3853 }
3854 break;
3855 case 'S': /* SU: Scroll up */
3856 compatibility(SCOANSI);
3857 scroll(term, term->marg_t, term->marg_b,
3858 def(term->esc_args[0], 1), TRUE);
3859 term->wrapnext = FALSE;
3860 seen_disp_event(term);
3861 break;
3862 case 'T': /* SD: Scroll down */
3863 compatibility(SCOANSI);
3864 scroll(term, term->marg_t, term->marg_b,
3865 -def(term->esc_args[0], 1), TRUE);
3866 term->wrapnext = FALSE;
3867 seen_disp_event(term);
3868 break;
3869 case ANSI('|', '*'): /* DECSNLS */
3870 /*
3871 * Set number of lines on screen
3872 * VT420 uses VGA like hardware and can
3873 * support any size in reasonable range
3874 * (24..49 AIUI) with no default specified.
3875 */
3876 compatibility(VT420);
3877 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
3878 if (!term->cfg.no_remote_resize)
3879 request_resize(term->frontend, term->cols,
3880 def(term->esc_args[0],
3881 term->cfg.height));
3882 deselect(term);
3883 }
3884 break;
3885 case ANSI('|', '$'): /* DECSCPP */
3886 /*
3887 * Set number of columns per page
3888 * Docs imply range is only 80 or 132, but
3889 * I'll allow any.
3890 */
3891 compatibility(VT340TEXT);
3892 if (term->esc_nargs <= 1) {
3893 if (!term->cfg.no_remote_resize)
3894 request_resize(term->frontend,
3895 def(term->esc_args[0],
3896 term->cfg.width), term->rows);
3897 deselect(term);
3898 }
3899 break;
3900 case 'X': /* ECH: write N spaces w/o moving cursor */
3901 /* XXX VTTEST says this is vt220, vt510 manual
3902 * says vt100 */
3903 compatibility(ANSIMIN);
3904 {
3905 int n = def(term->esc_args[0], 1);
3906 pos cursplus;
3907 int p = term->curs.x;
3908 termline *cline = scrlineptr(term->curs.y);
3909
3910 if (n > term->cols - term->curs.x)
3911 n = term->cols - term->curs.x;
3912 cursplus = term->curs;
3913 cursplus.x += n;
3914 check_boundary(term, term->curs.x, term->curs.y);
3915 check_boundary(term, term->curs.x+n, term->curs.y);
3916 check_selection(term, term->curs, cursplus);
3917 while (n--)
3918 copy_termchar(cline, p++,
3919 &term->erase_char);
3920 seen_disp_event(term);
3921 }
3922 break;
3923 case 'x': /* DECREQTPARM: report terminal characteristics */
3924 compatibility(VT100);
3925 if (term->ldisc) {
3926 char buf[32];
3927 int i = def(term->esc_args[0], 0);
3928 if (i == 0 || i == 1) {
3929 strcpy(buf, "\033[2;1;1;112;112;1;0x");
3930 buf[2] += i;
3931 ldisc_send(term->ldisc, buf, 20, 0);
3932 }
3933 }
3934 break;
3935 case 'Z': /* CBT */
3936 compatibility(OTHER);
3937 {
3938 int i = def(term->esc_args[0], 1);
3939 pos old_curs = term->curs;
3940
3941 for(;i>0 && term->curs.x>0; i--) {
3942 do {
3943 term->curs.x--;
3944 } while (term->curs.x >0 &&
3945 !term->tabs[term->curs.x]);
3946 }
3947 check_selection(term, old_curs, term->curs);
3948 }
3949 break;
3950 case ANSI('c', '='): /* Hide or Show Cursor */
3951 compatibility(SCOANSI);
3952 switch(term->esc_args[0]) {
3953 case 0: /* hide cursor */
3954 term->cursor_on = FALSE;
3955 break;
3956 case 1: /* restore cursor */
3957 term->big_cursor = FALSE;
3958 term->cursor_on = TRUE;
3959 break;
3960 case 2: /* block cursor */
3961 term->big_cursor = TRUE;
3962 term->cursor_on = TRUE;
3963 break;
3964 }
3965 break;
3966 case ANSI('C', '='):
3967 /*
3968 * set cursor start on scanline esc_args[0] and
3969 * end on scanline esc_args[1].If you set
3970 * the bottom scan line to a value less than
3971 * the top scan line, the cursor will disappear.
3972 */
3973 compatibility(SCOANSI);
3974 if (term->esc_nargs >= 2) {
3975 if (term->esc_args[0] > term->esc_args[1])
3976 term->cursor_on = FALSE;
3977 else
3978 term->cursor_on = TRUE;
3979 }
3980 break;
3981 case ANSI('D', '='):
3982 compatibility(SCOANSI);
3983 term->blink_is_real = FALSE;
3984 term_schedule_tblink(term);
3985 if (term->esc_args[0]>=1)
3986 term->curr_attr |= ATTR_BLINK;
3987 else
3988 term->curr_attr &= ~ATTR_BLINK;
3989 break;
3990 case ANSI('E', '='):
3991 compatibility(SCOANSI);
3992 term->blink_is_real = (term->esc_args[0] >= 1);
3993 term_schedule_tblink(term);
3994 break;
3995 case ANSI('F', '='): /* set normal foreground */
3996 compatibility(SCOANSI);
3997 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3998 long colour =
3999 (sco2ansicolour[term->esc_args[0] & 0x7] |
4000 (term->esc_args[0] & 0x8)) <<
4001 ATTR_FGSHIFT;
4002 term->curr_attr &= ~ATTR_FGMASK;
4003 term->curr_attr |= colour;
4004 term->default_attr &= ~ATTR_FGMASK;
4005 term->default_attr |= colour;
4006 set_erase_char(term);
4007 }
4008 break;
4009 case ANSI('G', '='): /* set normal background */
4010 compatibility(SCOANSI);
4011 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
4012 long colour =
4013 (sco2ansicolour[term->esc_args[0] & 0x7] |
4014 (term->esc_args[0] & 0x8)) <<
4015 ATTR_BGSHIFT;
4016 term->curr_attr &= ~ATTR_BGMASK;
4017 term->curr_attr |= colour;
4018 term->default_attr &= ~ATTR_BGMASK;
4019 term->default_attr |= colour;
4020 set_erase_char(term);
4021 }
4022 break;
4023 case ANSI('L', '='):
4024 compatibility(SCOANSI);
4025 term->use_bce = (term->esc_args[0] <= 0);
4026 set_erase_char(term);
4027 break;
4028 case ANSI('p', '"'): /* DECSCL: set compat level */
4029 /*
4030 * Allow the host to make this emulator a
4031 * 'perfect' VT102. This first appeared in
4032 * the VT220, but we do need to get back to
4033 * PuTTY mode so I won't check it.
4034 *
4035 * The arg in 40..42,50 are a PuTTY extension.
4036 * The 2nd arg, 8bit vs 7bit is not checked.
4037 *
4038 * Setting VT102 mode should also change
4039 * the Fkeys to generate PF* codes as a
4040 * real VT102 has no Fkeys. The VT220 does
4041 * this, F11..F13 become ESC,BS,LF other
4042 * Fkeys send nothing.
4043 *
4044 * Note ESC c will NOT change this!
4045 */
4046
4047 switch (term->esc_args[0]) {
4048 case 61:
4049 term->compatibility_level &= ~TM_VTXXX;
4050 term->compatibility_level |= TM_VT102;
4051 break;
4052 case 62:
4053 term->compatibility_level &= ~TM_VTXXX;
4054 term->compatibility_level |= TM_VT220;
4055 break;
4056
4057 default:
4058 if (term->esc_args[0] > 60 &&
4059 term->esc_args[0] < 70)
4060 term->compatibility_level |= TM_VTXXX;
4061 break;
4062
4063 case 40:
4064 term->compatibility_level &= TM_VTXXX;
4065 break;
4066 case 41:
4067 term->compatibility_level = TM_PUTTY;
4068 break;
4069 case 42:
4070 term->compatibility_level = TM_SCOANSI;
4071 break;
4072
4073 case ARG_DEFAULT:
4074 term->compatibility_level = TM_PUTTY;
4075 break;
4076 case 50:
4077 break;
4078 }
4079
4080 /* Change the response to CSI c */
4081 if (term->esc_args[0] == 50) {
4082 int i;
4083 char lbuf[64];
4084 strcpy(term->id_string, "\033[?");
4085 for (i = 1; i < term->esc_nargs; i++) {
4086 if (i != 1)
4087 strcat(term->id_string, ";");
4088 sprintf(lbuf, "%d", term->esc_args[i]);
4089 strcat(term->id_string, lbuf);
4090 }
4091 strcat(term->id_string, "c");
4092 }
4093 #if 0
4094 /* Is this a good idea ?
4095 * Well we should do a soft reset at this point ...
4096 */
4097 if (!has_compat(VT420) && has_compat(VT100)) {
4098 if (!term->cfg.no_remote_resize) {
4099 if (term->reset_132)
4100 request_resize(132, 24);
4101 else
4102 request_resize(80, 24);
4103 }
4104 }
4105 #endif
4106 break;
4107 }
4108 break;
4109 case SEEN_OSC:
4110 term->osc_w = FALSE;
4111 switch (c) {
4112 case 'P': /* Linux palette sequence */
4113 term->termstate = SEEN_OSC_P;
4114 term->osc_strlen = 0;
4115 break;
4116 case 'R': /* Linux palette reset */
4117 palette_reset(term->frontend);
4118 term_invalidate(term);
4119 term->termstate = TOPLEVEL;
4120 break;
4121 case 'W': /* word-set */
4122 term->termstate = SEEN_OSC_W;
4123 term->osc_w = TRUE;
4124 break;
4125 case '0':
4126 case '1':
4127 case '2':
4128 case '3':
4129 case '4':
4130 case '5':
4131 case '6':
4132 case '7':
4133 case '8':
4134 case '9':
4135 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
4136 break;
4137 case 'L':
4138 /*
4139 * Grotty hack to support xterm and DECterm title
4140 * sequences concurrently.
4141 */
4142 if (term->esc_args[0] == 2) {
4143 term->esc_args[0] = 1;
4144 break;
4145 }
4146 /* else fall through */
4147 default:
4148 term->termstate = OSC_STRING;
4149 term->osc_strlen = 0;
4150 }
4151 break;
4152 case OSC_STRING:
4153 /*
4154 * This OSC stuff is EVIL. It takes just one character to get into
4155 * sysline mode and it's not initially obvious how to get out.
4156 * So I've added CR and LF as string aborts.
4157 * This shouldn't effect compatibility as I believe embedded
4158 * control characters are supposed to be interpreted (maybe?)
4159 * and they don't display anything useful anyway.
4160 *
4161 * -- RDB
4162 */
4163 if (c == '\012' || c == '\015') {
4164 term->termstate = TOPLEVEL;
4165 } else if (c == 0234 || c == '\007') {
4166 /*
4167 * These characters terminate the string; ST and BEL
4168 * terminate the sequence and trigger instant
4169 * processing of it, whereas ESC goes back to SEEN_ESC
4170 * mode unless it is followed by \, in which case it is
4171 * synonymous with ST in the first place.
4172 */
4173 do_osc(term);
4174 term->termstate = TOPLEVEL;
4175 } else if (c == '\033')
4176 term->termstate = OSC_MAYBE_ST;
4177 else if (term->osc_strlen < OSC_STR_MAX)
4178 term->osc_string[term->osc_strlen++] = (char)c;
4179 break;
4180 case SEEN_OSC_P:
4181 {
4182 int max = (term->osc_strlen == 0 ? 21 : 15);
4183 int val;
4184 if ((int)c >= '0' && (int)c <= '9')
4185 val = c - '0';
4186 else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)
4187 val = c - 'A' + 10;
4188 else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)
4189 val = c - 'a' + 10;
4190 else {
4191 term->termstate = TOPLEVEL;
4192 break;
4193 }
4194 term->osc_string[term->osc_strlen++] = val;
4195 if (term->osc_strlen >= 7) {
4196 palette_set(term->frontend, term->osc_string[0],
4197 term->osc_string[1] * 16 + term->osc_string[2],
4198 term->osc_string[3] * 16 + term->osc_string[4],
4199 term->osc_string[5] * 16 + term->osc_string[6]);
4200 term_invalidate(term);
4201 term->termstate = TOPLEVEL;
4202 }
4203 }
4204 break;
4205 case SEEN_OSC_W:
4206 switch (c) {
4207 case '0':
4208 case '1':
4209 case '2':
4210 case '3':
4211 case '4':
4212 case '5':
4213 case '6':
4214 case '7':
4215 case '8':
4216 case '9':
4217 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
4218 break;
4219 default:
4220 term->termstate = OSC_STRING;
4221 term->osc_strlen = 0;
4222 }
4223 break;
4224 case VT52_ESC:
4225 term->termstate = TOPLEVEL;
4226 seen_disp_event(term);
4227 switch (c) {
4228 case 'A':
4229 move(term, term->curs.x, term->curs.y - 1, 1);
4230 break;
4231 case 'B':
4232 move(term, term->curs.x, term->curs.y + 1, 1);
4233 break;
4234 case 'C':
4235 move(term, term->curs.x + 1, term->curs.y, 1);
4236 break;
4237 case 'D':
4238 move(term, term->curs.x - 1, term->curs.y, 1);
4239 break;
4240 /*
4241 * From the VT100 Manual
4242 * NOTE: The special graphics characters in the VT100
4243 * are different from those in the VT52
4244 *
4245 * From VT102 manual:
4246 * 137 _ Blank - Same
4247 * 140 ` Reserved - Humm.
4248 * 141 a Solid rectangle - Similar
4249 * 142 b 1/ - Top half of fraction for the
4250 * 143 c 3/ - subscript numbers below.
4251 * 144 d 5/
4252 * 145 e 7/
4253 * 146 f Degrees - Same
4254 * 147 g Plus or minus - Same
4255 * 150 h Right arrow
4256 * 151 i Ellipsis (dots)
4257 * 152 j Divide by
4258 * 153 k Down arrow
4259 * 154 l Bar at scan 0
4260 * 155 m Bar at scan 1
4261 * 156 n Bar at scan 2
4262 * 157 o Bar at scan 3 - Similar
4263 * 160 p Bar at scan 4 - Similar
4264 * 161 q Bar at scan 5 - Similar
4265 * 162 r Bar at scan 6 - Same
4266 * 163 s Bar at scan 7 - Similar
4267 * 164 t Subscript 0
4268 * 165 u Subscript 1
4269 * 166 v Subscript 2
4270 * 167 w Subscript 3
4271 * 170 x Subscript 4
4272 * 171 y Subscript 5
4273 * 172 z Subscript 6
4274 * 173 { Subscript 7
4275 * 174 | Subscript 8
4276 * 175 } Subscript 9
4277 * 176 ~ Paragraph
4278 *
4279 */
4280 case 'F':
4281 term->cset_attr[term->cset = 0] = CSET_LINEDRW;
4282 break;
4283 case 'G':
4284 term->cset_attr[term->cset = 0] = CSET_ASCII;
4285 break;
4286 case 'H':
4287 move(term, 0, 0, 0);
4288 break;
4289 case 'I':
4290 if (term->curs.y == 0)
4291 scroll(term, 0, term->rows - 1, -1, TRUE);
4292 else if (term->curs.y > 0)
4293 term->curs.y--;
4294 term->wrapnext = FALSE;
4295 break;
4296 case 'J':
4297 erase_lots(term, FALSE, FALSE, TRUE);
4298 term->disptop = 0;
4299 break;
4300 case 'K':
4301 erase_lots(term, TRUE, FALSE, TRUE);
4302 break;
4303 #if 0
4304 case 'V':
4305 /* XXX Print cursor line */
4306 break;
4307 case 'W':
4308 /* XXX Start controller mode */
4309 break;
4310 case 'X':
4311 /* XXX Stop controller mode */
4312 break;
4313 #endif
4314 case 'Y':
4315 term->termstate = VT52_Y1;
4316 break;
4317 case 'Z':
4318 if (term->ldisc)
4319 ldisc_send(term->ldisc, "\033/Z", 3, 0);
4320 break;
4321 case '=':
4322 term->app_keypad_keys = TRUE;
4323 break;
4324 case '>':
4325 term->app_keypad_keys = FALSE;
4326 break;
4327 case '<':
4328 /* XXX This should switch to VT100 mode not current or default
4329 * VT mode. But this will only have effect in a VT220+
4330 * emulation.
4331 */
4332 term->vt52_mode = FALSE;
4333 term->blink_is_real = term->cfg.blinktext;
4334 term_schedule_tblink(term);
4335 break;
4336 #if 0
4337 case '^':
4338 /* XXX Enter auto print mode */
4339 break;
4340 case '_':
4341 /* XXX Exit auto print mode */
4342 break;
4343 case ']':
4344 /* XXX Print screen */
4345 break;
4346 #endif
4347
4348 #ifdef VT52_PLUS
4349 case 'E':
4350 /* compatibility(ATARI) */
4351 move(term, 0, 0, 0);
4352 erase_lots(term, FALSE, FALSE, TRUE);
4353 term->disptop = 0;
4354 break;
4355 case 'L':
4356 /* compatibility(ATARI) */
4357 if (term->curs.y <= term->marg_b)
4358 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
4359 break;
4360 case 'M':
4361 /* compatibility(ATARI) */
4362 if (term->curs.y <= term->marg_b)
4363 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
4364 break;
4365 case 'b':
4366 /* compatibility(ATARI) */
4367 term->termstate = VT52_FG;
4368 break;
4369 case 'c':
4370 /* compatibility(ATARI) */
4371 term->termstate = VT52_BG;
4372 break;
4373 case 'd':
4374 /* compatibility(ATARI) */
4375 erase_lots(term, FALSE, TRUE, FALSE);
4376 term->disptop = 0;
4377 break;
4378 case 'e':
4379 /* compatibility(ATARI) */
4380 term->cursor_on = TRUE;
4381 break;
4382 case 'f':
4383 /* compatibility(ATARI) */
4384 term->cursor_on = FALSE;
4385 break;
4386 /* case 'j': Save cursor position - broken on ST */
4387 /* case 'k': Restore cursor position */
4388 case 'l':
4389 /* compatibility(ATARI) */
4390 erase_lots(term, TRUE, TRUE, TRUE);
4391 term->curs.x = 0;
4392 term->wrapnext = FALSE;
4393 break;
4394 case 'o':
4395 /* compatibility(ATARI) */
4396 erase_lots(term, TRUE, TRUE, FALSE);
4397 break;
4398 case 'p':
4399 /* compatibility(ATARI) */
4400 term->curr_attr |= ATTR_REVERSE;
4401 break;
4402 case 'q':
4403 /* compatibility(ATARI) */
4404 term->curr_attr &= ~ATTR_REVERSE;
4405 break;
4406 case 'v': /* wrap Autowrap on - Wyse style */
4407 /* compatibility(ATARI) */
4408 term->wrap = 1;
4409 break;
4410 case 'w': /* Autowrap off */
4411 /* compatibility(ATARI) */
4412 term->wrap = 0;
4413 break;
4414
4415 case 'R':
4416 /* compatibility(OTHER) */
4417 term->vt52_bold = FALSE;
4418 term->curr_attr = ATTR_DEFAULT;
4419 set_erase_char(term);
4420 break;
4421 case 'S':
4422 /* compatibility(VI50) */
4423 term->curr_attr |= ATTR_UNDER;
4424 break;
4425 case 'W':
4426 /* compatibility(VI50) */
4427 term->curr_attr &= ~ATTR_UNDER;
4428 break;
4429 case 'U':
4430 /* compatibility(VI50) */
4431 term->vt52_bold = TRUE;
4432 term->curr_attr |= ATTR_BOLD;
4433 break;
4434 case 'T':
4435 /* compatibility(VI50) */
4436 term->vt52_bold = FALSE;
4437 term->curr_attr &= ~ATTR_BOLD;
4438 break;
4439 #endif
4440 }
4441 break;
4442 case VT52_Y1:
4443 term->termstate = VT52_Y2;
4444 move(term, term->curs.x, c - ' ', 0);
4445 break;
4446 case VT52_Y2:
4447 term->termstate = TOPLEVEL;
4448 move(term, c - ' ', term->curs.y, 0);
4449 break;
4450
4451 #ifdef VT52_PLUS
4452 case VT52_FG:
4453 term->termstate = TOPLEVEL;
4454 term->curr_attr &= ~ATTR_FGMASK;
4455 term->curr_attr &= ~ATTR_BOLD;
4456 term->curr_attr |= (c & 0xF) << ATTR_FGSHIFT;
4457 set_erase_char(term);
4458 break;
4459 case VT52_BG:
4460 term->termstate = TOPLEVEL;
4461 term->curr_attr &= ~ATTR_BGMASK;
4462 term->curr_attr &= ~ATTR_BLINK;
4463 term->curr_attr |= (c & 0xF) << ATTR_BGSHIFT;
4464 set_erase_char(term);
4465 break;
4466 #endif
4467 default: break; /* placate gcc warning about enum use */
4468 }
4469 if (term->selstate != NO_SELECTION) {
4470 pos cursplus = term->curs;
4471 incpos(cursplus);
4472 check_selection(term, term->curs, cursplus);
4473 }
4474 }
4475
4476 term_print_flush(term);
4477 if (term->cfg.logflush)
4478 logflush(term->logctx);
4479 }
4480
4481 /*
4482 * To prevent having to run the reasonably tricky bidi algorithm
4483 * too many times, we maintain a cache of the last lineful of data
4484 * fed to the algorithm on each line of the display.
4485 */
4486 static int term_bidi_cache_hit(Terminal *term, int line,
4487 termchar *lbefore, int width)
4488 {
4489 int i;
4490
4491 if (!term->pre_bidi_cache)
4492 return FALSE; /* cache doesn't even exist yet! */
4493
4494 if (line >= term->bidi_cache_size)
4495 return FALSE; /* cache doesn't have this many lines */
4496
4497 if (!term->pre_bidi_cache[line].chars)
4498 return FALSE; /* cache doesn't contain _this_ line */
4499
4500 if (term->pre_bidi_cache[line].width != width)
4501 return FALSE; /* line is wrong width */
4502
4503 for (i = 0; i < width; i++)
4504 if (!termchars_equal(term->pre_bidi_cache[line].chars+i, lbefore+i))
4505 return FALSE; /* line doesn't match cache */
4506
4507 return TRUE; /* it didn't match. */
4508 }
4509
4510 static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,
4511 termchar *lafter, bidi_char *wcTo,
4512 int width, int size)
4513 {
4514 int i;
4515
4516 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
4517 int j = term->bidi_cache_size;
4518 term->bidi_cache_size = line+1;
4519 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
4520 term->bidi_cache_size,
4521 struct bidi_cache_entry);
4522 term->post_bidi_cache = sresize(term->post_bidi_cache,
4523 term->bidi_cache_size,
4524 struct bidi_cache_entry);
4525 while (j < term->bidi_cache_size) {
4526 term->pre_bidi_cache[j].chars =
4527 term->post_bidi_cache[j].chars = NULL;
4528 term->pre_bidi_cache[j].width =
4529 term->post_bidi_cache[j].width = -1;
4530 term->pre_bidi_cache[j].forward =
4531 term->post_bidi_cache[j].forward = NULL;
4532 term->pre_bidi_cache[j].backward =
4533 term->post_bidi_cache[j].backward = NULL;
4534 j++;
4535 }
4536 }
4537
4538 sfree(term->pre_bidi_cache[line].chars);
4539 sfree(term->post_bidi_cache[line].chars);
4540 sfree(term->post_bidi_cache[line].forward);
4541 sfree(term->post_bidi_cache[line].backward);
4542
4543 term->pre_bidi_cache[line].width = width;
4544 term->pre_bidi_cache[line].chars = snewn(size, termchar);
4545 term->post_bidi_cache[line].width = width;
4546 term->post_bidi_cache[line].chars = snewn(size, termchar);
4547 term->post_bidi_cache[line].forward = snewn(width, int);
4548 term->post_bidi_cache[line].backward = snewn(width, int);
4549
4550 memcpy(term->pre_bidi_cache[line].chars, lbefore, size * TSIZE);
4551 memcpy(term->post_bidi_cache[line].chars, lafter, size * TSIZE);
4552 memset(term->post_bidi_cache[line].forward, 0, width * sizeof(int));
4553 memset(term->post_bidi_cache[line].backward, 0, width * sizeof(int));
4554
4555 for (i = 0; i < width; i++) {
4556 int p = wcTo[i].index;
4557
4558 assert(0 <= p && p < width);
4559
4560 term->post_bidi_cache[line].backward[i] = p;
4561 term->post_bidi_cache[line].forward[p] = i;
4562 }
4563 }
4564
4565 /*
4566 * Prepare the bidi information for a screen line. Returns the
4567 * transformed list of termchars, or NULL if no transformation at
4568 * all took place (because bidi is disabled). If return was
4569 * non-NULL, auxiliary information such as the forward and reverse
4570 * mappings of permutation position are available in
4571 * term->post_bidi_cache[scr_y].*.
4572 */
4573 static termchar *term_bidi_line(Terminal *term, struct termline *ldata,
4574 int scr_y)
4575 {
4576 termchar *lchars;
4577 int it;
4578
4579 /* Do Arabic shaping and bidi. */
4580 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
4581
4582 if (!term_bidi_cache_hit(term, scr_y, ldata->chars, term->cols)) {
4583
4584 if (term->wcFromTo_size < term->cols) {
4585 term->wcFromTo_size = term->cols;
4586 term->wcFrom = sresize(term->wcFrom, term->wcFromTo_size,
4587 bidi_char);
4588 term->wcTo = sresize(term->wcTo, term->wcFromTo_size,
4589 bidi_char);
4590 }
4591
4592 for(it=0; it<term->cols ; it++)
4593 {
4594 unsigned long uc = (ldata->chars[it].chr);
4595
4596 switch (uc & CSET_MASK) {
4597 case CSET_LINEDRW:
4598 if (!term->cfg.rawcnp) {
4599 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4600 break;
4601 }
4602 case CSET_ASCII:
4603 uc = term->ucsdata->unitab_line[uc & 0xFF];
4604 break;
4605 case CSET_SCOACS:
4606 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4607 break;
4608 }
4609 switch (uc & CSET_MASK) {
4610 case CSET_ACP:
4611 uc = term->ucsdata->unitab_font[uc & 0xFF];
4612 break;
4613 case CSET_OEMCP:
4614 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4615 break;
4616 }
4617
4618 term->wcFrom[it].origwc = term->wcFrom[it].wc =
4619 (wchar_t)uc;
4620 term->wcFrom[it].index = it;
4621 }
4622
4623 if(!term->cfg.bidi)
4624 do_bidi(term->wcFrom, term->cols);
4625
4626 /* this is saved iff done from inside the shaping */
4627 if(!term->cfg.bidi && term->cfg.arabicshaping)
4628 for(it=0; it<term->cols; it++)
4629 term->wcTo[it] = term->wcFrom[it];
4630
4631 if(!term->cfg.arabicshaping)
4632 do_shape(term->wcFrom, term->wcTo, term->cols);
4633
4634 if (term->ltemp_size < ldata->size) {
4635 term->ltemp_size = ldata->size;
4636 term->ltemp = sresize(term->ltemp, term->ltemp_size,
4637 termchar);
4638 }
4639
4640 memcpy(term->ltemp, ldata->chars, ldata->size * TSIZE);
4641
4642 for(it=0; it<term->cols ; it++)
4643 {
4644 term->ltemp[it] = ldata->chars[term->wcTo[it].index];
4645 if (term->ltemp[it].cc_next)
4646 term->ltemp[it].cc_next -=
4647 it - term->wcTo[it].index;
4648
4649 if (term->wcTo[it].origwc != term->wcTo[it].wc)
4650 term->ltemp[it].chr = term->wcTo[it].wc;
4651 }
4652 term_bidi_cache_store(term, scr_y, ldata->chars,
4653 term->ltemp, term->wcTo,
4654 term->cols, ldata->size);
4655
4656 lchars = term->ltemp;
4657 } else {
4658 lchars = term->post_bidi_cache[scr_y].chars;
4659 }
4660 } else {
4661 lchars = NULL;
4662 }
4663
4664 return lchars;
4665 }
4666
4667 /*
4668 * Given a context, update the window. Out of paranoia, we don't
4669 * allow WM_PAINT responses to do scrolling optimisations.
4670 */
4671 static void do_paint(Terminal *term, Context ctx, int may_optimise)
4672 {
4673 int i, j, our_curs_y, our_curs_x;
4674 int rv, cursor;
4675 pos scrpos;
4676 wchar_t *ch;
4677 int chlen;
4678 #ifdef OPTIMISE_SCROLL
4679 struct scrollregion *sr;
4680 #endif /* OPTIMISE_SCROLL */
4681 termchar *newline;
4682
4683 chlen = 1024;
4684 ch = snewn(chlen, wchar_t);
4685
4686 newline = snewn(term->cols, termchar);
4687
4688 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
4689
4690 /* Depends on:
4691 * screen array, disptop, scrtop,
4692 * selection, rv,
4693 * cfg.blinkpc, blink_is_real, tblinker,
4694 * curs.y, curs.x, cblinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4695 */
4696
4697 /* Has the cursor position or type changed ? */
4698 if (term->cursor_on) {
4699 if (term->has_focus) {
4700 if (term->cblinker || !term->cfg.blink_cur)
4701 cursor = TATTR_ACTCURS;
4702 else
4703 cursor = 0;
4704 } else
4705 cursor = TATTR_PASCURS;
4706 if (term->wrapnext)
4707 cursor |= TATTR_RIGHTCURS;
4708 } else
4709 cursor = 0;
4710 our_curs_y = term->curs.y - term->disptop;
4711 {
4712 /*
4713 * Adjust the cursor position:
4714 * - for bidi
4715 * - in the case where it's resting on the right-hand half
4716 * of a CJK wide character. xterm's behaviour here,
4717 * which seems adequate to me, is to display the cursor
4718 * covering the _whole_ character, exactly as if it were
4719 * one space to the left.
4720 */
4721 termline *ldata = lineptr(term->curs.y);
4722 termchar *lchars;
4723
4724 our_curs_x = term->curs.x;
4725
4726 if ( (lchars = term_bidi_line(term, ldata, our_curs_y)) != NULL) {
4727 our_curs_x = term->post_bidi_cache[our_curs_y].forward[our_curs_x];
4728 } else
4729 lchars = ldata->chars;
4730
4731 if (our_curs_x > 0 &&
4732 lchars[our_curs_x].chr == UCSWIDE)
4733 our_curs_x--;
4734
4735 unlineptr(ldata);
4736 }
4737
4738 /*
4739 * If the cursor is not where it was last time we painted, and
4740 * its previous position is visible on screen, invalidate its
4741 * previous position.
4742 */
4743 if (term->dispcursy >= 0 &&
4744 (term->curstype != cursor ||
4745 term->dispcursy != our_curs_y ||
4746 term->dispcursx != our_curs_x)) {
4747 termchar *dispcurs = term->disptext[term->dispcursy]->chars +
4748 term->dispcursx;
4749
4750 if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)
4751 dispcurs[-1].attr |= ATTR_INVALID;
4752 if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)
4753 dispcurs[1].attr |= ATTR_INVALID;
4754 dispcurs->attr |= ATTR_INVALID;
4755
4756 term->curstype = 0;
4757 }
4758 term->dispcursx = term->dispcursy = -1;
4759
4760 #ifdef OPTIMISE_SCROLL
4761 /* Do scrolls */
4762 sr = term->scrollhead;
4763 while (sr) {
4764 struct scrollregion *next = sr->next;
4765 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
4766 sfree(sr);
4767 sr = next;
4768 }
4769 term->scrollhead = term->scrolltail = NULL;
4770 #endif /* OPTIMISE_SCROLL */
4771
4772 /* The normal screen data */
4773 for (i = 0; i < term->rows; i++) {
4774 termline *ldata;
4775 termchar *lchars;
4776 int dirty_line, dirty_run, selected;
4777 unsigned long attr = 0, cset = 0;
4778 int updated_line = 0;
4779 int start = 0;
4780 int ccount = 0;
4781 int last_run_dirty = 0;
4782 int laststart, dirtyrect;
4783 int *backward;
4784
4785 scrpos.y = i + term->disptop;
4786 ldata = lineptr(scrpos.y);
4787
4788 /* Do Arabic shaping and bidi. */
4789 lchars = term_bidi_line(term, ldata, i);
4790 if (lchars) {
4791 backward = term->post_bidi_cache[i].backward;
4792 } else {
4793 lchars = ldata->chars;
4794 backward = NULL;
4795 }
4796
4797 /*
4798 * First loop: work along the line deciding what we want
4799 * each character cell to look like.
4800 */
4801 for (j = 0; j < term->cols; j++) {
4802 unsigned long tattr, tchar;
4803 termchar *d = lchars + j;
4804 scrpos.x = backward ? backward[j] : j;
4805
4806 tchar = d->chr;
4807 tattr = d->attr;
4808
4809 if (!term->cfg.ansi_colour)
4810 tattr = (tattr & ~(ATTR_FGMASK | ATTR_BGMASK)) |
4811 ATTR_DEFFG | ATTR_DEFBG;
4812
4813 if (!term->cfg.xterm_256_colour) {
4814 int colour;
4815 colour = (tattr & ATTR_FGMASK) >> ATTR_FGSHIFT;
4816 if (colour >= 16 && colour < 256)
4817 tattr = (tattr &~ ATTR_FGMASK) | ATTR_DEFFG;
4818 colour = (tattr & ATTR_BGMASK) >> ATTR_BGSHIFT;
4819 if (colour >= 16 && colour < 256)
4820 tattr = (tattr &~ ATTR_BGMASK) | ATTR_DEFBG;
4821 }
4822
4823 switch (tchar & CSET_MASK) {
4824 case CSET_ASCII:
4825 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4826 break;
4827 case CSET_LINEDRW:
4828 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4829 break;
4830 case CSET_SCOACS:
4831 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
4832 break;
4833 }
4834 if (j < term->cols-1 && d[1].chr == UCSWIDE)
4835 tattr |= ATTR_WIDE;
4836
4837 /* Video reversing things */
4838 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
4839 if (term->seltype == LEXICOGRAPHIC)
4840 selected = (posle(term->selstart, scrpos) &&
4841 poslt(scrpos, term->selend));
4842 else
4843 selected = (posPle(term->selstart, scrpos) &&
4844 posPlt(scrpos, term->selend));
4845 } else
4846 selected = FALSE;
4847 tattr = (tattr ^ rv
4848 ^ (selected ? ATTR_REVERSE : 0));
4849
4850 /* 'Real' blinking ? */
4851 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
4852 if (term->has_focus && term->tblinker) {
4853 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
4854 }
4855 tattr &= ~ATTR_BLINK;
4856 }
4857
4858 /*
4859 * Check the font we'll _probably_ be using to see if
4860 * the character is wide when we don't want it to be.
4861 */
4862 if (tchar != term->disptext[i]->chars[j].chr ||
4863 tattr != (term->disptext[i]->chars[j].attr &~
4864 (ATTR_NARROW | DATTR_MASK))) {
4865 if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
4866 tattr |= ATTR_NARROW;
4867 } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
4868 tattr |= ATTR_NARROW;
4869
4870 if (i == our_curs_y && j == our_curs_x) {
4871 tattr |= cursor;
4872 term->curstype = cursor;
4873 term->dispcursx = j;
4874 term->dispcursy = i;
4875 }
4876
4877 /* FULL-TERMCHAR */
4878 newline[j].attr = tattr;
4879 newline[j].chr = tchar;
4880 /* Combining characters are still read from lchars */
4881 newline[j].cc_next = 0;
4882 }
4883
4884 /*
4885 * Now loop over the line again, noting where things have
4886 * changed.
4887 *
4888 * During this loop, we keep track of where we last saw
4889 * DATTR_STARTRUN. Any mismatch automatically invalidates
4890 * _all_ of the containing run that was last printed: that
4891 * is, any rectangle that was drawn in one go in the
4892 * previous update should be either left completely alone
4893 * or overwritten in its entirety. This, along with the
4894 * expectation that front ends clip all text runs to their
4895 * bounding rectangle, should solve any possible problems
4896 * with fonts that overflow their character cells.
4897 */
4898 laststart = 0;
4899 dirtyrect = FALSE;
4900 for (j = 0; j < term->cols; j++) {
4901 if (term->disptext[i]->chars[j].attr & DATTR_STARTRUN) {
4902 laststart = j;
4903 dirtyrect = FALSE;
4904 }
4905
4906 if (term->disptext[i]->chars[j].chr != newline[j].chr ||
4907 (term->disptext[i]->chars[j].attr &~ DATTR_MASK)
4908 != newline[j].attr) {
4909 int k;
4910
4911 if (!dirtyrect) {
4912 for (k = laststart; k < j; k++)
4913 term->disptext[i]->chars[k].attr |= ATTR_INVALID;
4914
4915 dirtyrect = TRUE;
4916 }
4917 }
4918
4919 if (dirtyrect)
4920 term->disptext[i]->chars[j].attr |= ATTR_INVALID;
4921 }
4922
4923 /*
4924 * Finally, loop once more and actually do the drawing.
4925 */
4926 dirty_run = dirty_line = (ldata->lattr !=
4927 term->disptext[i]->lattr);
4928 term->disptext[i]->lattr = ldata->lattr;
4929
4930 for (j = 0; j < term->cols; j++) {
4931 unsigned long tattr, tchar;
4932 int break_run, do_copy;
4933 termchar *d = lchars + j;
4934
4935 tattr = newline[j].attr;
4936 tchar = newline[j].chr;
4937
4938 if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
4939 dirty_line = TRUE;
4940
4941 break_run = ((tattr ^ attr) & term->attr_mask) != 0;
4942
4943 /* Special hack for VT100 Linedraw glyphs */
4944 if (tchar >= 0x23BA && tchar <= 0x23BD)
4945 break_run = TRUE;
4946
4947 /*
4948 * Separate out sequences of characters that have the
4949 * same CSET, if that CSET is a magic one.
4950 */
4951 if (CSET_OF(tchar) != cset)
4952 break_run = TRUE;
4953
4954 /*
4955 * Break on both sides of any combined-character cell.
4956 */
4957 if (d->cc_next != 0 ||
4958 (j > 0 && d[-1].cc_next != 0))
4959 break_run = TRUE;
4960
4961 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
4962 if (term->disptext[i]->chars[j].chr == tchar &&
4963 (term->disptext[i]->chars[j].attr &~ DATTR_MASK) == tattr)
4964 break_run = TRUE;
4965 else if (!dirty_run && ccount == 1)
4966 break_run = TRUE;
4967 }
4968
4969 if (break_run) {
4970 if ((dirty_run || last_run_dirty) && ccount > 0) {
4971 do_text(ctx, start, i, ch, ccount, attr,
4972 ldata->lattr);
4973 if (attr & (TATTR_ACTCURS | TATTR_PASCURS))
4974 do_cursor(ctx, start, i, ch, ccount, attr,
4975 ldata->lattr);
4976
4977 updated_line = 1;
4978 }
4979 start = j;
4980 ccount = 0;
4981 attr = tattr;
4982 cset = CSET_OF(tchar);
4983 if (term->ucsdata->dbcs_screenfont)
4984 last_run_dirty = dirty_run;
4985 dirty_run = dirty_line;
4986 }
4987
4988 do_copy = FALSE;
4989 if (!termchars_equal_override(&term->disptext[i]->chars[j],
4990 d, tchar, tattr)) {
4991 do_copy = TRUE;
4992 dirty_run = TRUE;
4993 }
4994
4995 if (ccount >= chlen) {
4996 chlen = ccount + 256;
4997 ch = sresize(ch, chlen, wchar_t);
4998 }
4999 ch[ccount++] = (wchar_t) tchar;
5000
5001 if (d->cc_next) {
5002 termchar *dd = d;
5003
5004 while (dd->cc_next) {
5005 unsigned long schar;
5006
5007 dd += dd->cc_next;
5008
5009 schar = dd->chr;
5010 switch (schar & CSET_MASK) {
5011 case CSET_ASCII:
5012 schar = term->ucsdata->unitab_line[schar & 0xFF];
5013 break;
5014 case CSET_LINEDRW:
5015 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
5016 break;
5017 case CSET_SCOACS:
5018 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
5019 break;
5020 }
5021
5022 if (ccount >= chlen) {
5023 chlen = ccount + 256;
5024 ch = sresize(ch, chlen, wchar_t);
5025 }
5026 ch[ccount++] = (wchar_t) schar;
5027 }
5028
5029 attr |= TATTR_COMBINING;
5030 }
5031
5032 if (do_copy) {
5033 copy_termchar(term->disptext[i], j, d);
5034 term->disptext[i]->chars[j].chr = tchar;
5035 term->disptext[i]->chars[j].attr = tattr;
5036 if (start == j)
5037 term->disptext[i]->chars[j].attr |= DATTR_STARTRUN;
5038 }
5039
5040 /* If it's a wide char step along to the next one. */
5041 if (tattr & ATTR_WIDE) {
5042 if (++j < term->cols) {
5043 d++;
5044 /*
5045 * By construction above, the cursor should not
5046 * be on the right-hand half of this character.
5047 * Ever.
5048 */
5049 assert(!(i == our_curs_y && j == our_curs_x));
5050 if (!termchars_equal(&term->disptext[i]->chars[j], d))
5051 dirty_run = TRUE;
5052 copy_termchar(term->disptext[i], j, d);
5053 }
5054 }
5055 }
5056 if (dirty_run && ccount > 0) {
5057 do_text(ctx, start, i, ch, ccount, attr,
5058 ldata->lattr);
5059 if (attr & (TATTR_ACTCURS | TATTR_PASCURS))
5060 do_cursor(ctx, start, i, ch, ccount, attr,
5061 ldata->lattr);
5062
5063 updated_line = 1;
5064 }
5065
5066 unlineptr(ldata);
5067 }
5068
5069 sfree(newline);
5070 sfree(ch);
5071 }
5072
5073 /*
5074 * Invalidate the whole screen so it will be repainted in full.
5075 */
5076 void term_invalidate(Terminal *term)
5077 {
5078 int i, j;
5079
5080 for (i = 0; i < term->rows; i++)
5081 for (j = 0; j < term->cols; j++)
5082 term->disptext[i]->chars[j].attr |= ATTR_INVALID;
5083
5084 term_schedule_update(term);
5085 }
5086
5087 /*
5088 * Paint the window in response to a WM_PAINT message.
5089 */
5090 void term_paint(Terminal *term, Context ctx,
5091 int left, int top, int right, int bottom, int immediately)
5092 {
5093 int i, j;
5094 if (left < 0) left = 0;
5095 if (top < 0) top = 0;
5096 if (right >= term->cols) right = term->cols-1;
5097 if (bottom >= term->rows) bottom = term->rows-1;
5098
5099 for (i = top; i <= bottom && i < term->rows; i++) {
5100 if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM)
5101 for (j = left; j <= right && j < term->cols; j++)
5102 term->disptext[i]->chars[j].attr |= ATTR_INVALID;
5103 else
5104 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
5105 term->disptext[i]->chars[j].attr |= ATTR_INVALID;
5106 }
5107
5108 if (immediately) {
5109 do_paint (term, ctx, FALSE);
5110 } else {
5111 term_schedule_update(term);
5112 }
5113 }
5114
5115 /*
5116 * Attempt to scroll the scrollback. The second parameter gives the
5117 * position we want to scroll to; the first is +1 to denote that
5118 * this position is relative to the beginning of the scrollback, -1
5119 * to denote it is relative to the end, and 0 to denote that it is
5120 * relative to the current position.
5121 */
5122 void term_scroll(Terminal *term, int rel, int where)
5123 {
5124 int sbtop = -sblines(term);
5125 #ifdef OPTIMISE_SCROLL
5126 int olddisptop = term->disptop;
5127 int shift;
5128 #endif /* OPTIMISE_SCROLL */
5129
5130 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
5131 if (term->disptop < sbtop)
5132 term->disptop = sbtop;
5133 if (term->disptop > 0)
5134 term->disptop = 0;
5135 update_sbar(term);
5136 #ifdef OPTIMISE_SCROLL
5137 shift = (term->disptop - olddisptop);
5138 if (shift < term->rows && shift > -term->rows)
5139 scroll_display(term, 0, term->rows - 1, shift);
5140 #endif /* OPTIMISE_SCROLL */
5141 term_update(term);
5142 }
5143
5144 /*
5145 * Scroll the scrollback to centre it on the beginning or end of the
5146 * current selection, if any.
5147 */
5148 void term_scroll_to_selection(Terminal *term, int which_end)
5149 {
5150 pos target;
5151 int y;
5152 int sbtop = -sblines(term);
5153
5154 if (term->selstate != SELECTED)
5155 return;
5156 if (which_end)
5157 target = term->selend;
5158 else
5159 target = term->selstart;
5160
5161 y = target.y - term->rows/2;
5162 if (y < sbtop)
5163 y = sbtop;
5164 else if (y > 0)
5165 y = 0;
5166 term_scroll(term, -1, y);
5167 }
5168
5169 /*
5170 * Helper routine for clipme(): growing buffer.
5171 */
5172 typedef struct {
5173 int buflen; /* amount of allocated space in textbuf/attrbuf */
5174 int bufpos; /* amount of actual data */
5175 wchar_t *textbuf; /* buffer for copied text */
5176 wchar_t *textptr; /* = textbuf + bufpos (current insertion point) */
5177 int *attrbuf; /* buffer for copied attributes */
5178 int *attrptr; /* = attrbuf + bufpos */
5179 } clip_workbuf;
5180
5181 static void clip_addchar(clip_workbuf *b, wchar_t chr, int attr)
5182 {
5183 if (b->bufpos >= b->buflen) {
5184 b->buflen += 128;
5185 b->textbuf = sresize(b->textbuf, b->buflen, wchar_t);
5186 b->textptr = b->textbuf + b->bufpos;
5187 b->attrbuf = sresize(b->attrbuf, b->buflen, int);
5188 b->attrptr = b->attrbuf + b->bufpos;
5189 }
5190 *b->textptr++ = chr;
5191 *b->attrptr++ = attr;
5192 b->bufpos++;
5193 }
5194
5195 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
5196 {
5197 clip_workbuf buf;
5198 int old_top_x;
5199 int attr;
5200
5201 buf.buflen = 5120;
5202 buf.bufpos = 0;
5203 buf.textptr = buf.textbuf = snewn(buf.buflen, wchar_t);
5204 buf.attrptr = buf.attrbuf = snewn(buf.buflen, int);
5205
5206 old_top_x = top.x; /* needed for rect==1 */
5207
5208 while (poslt(top, bottom)) {
5209 int nl = FALSE;
5210 termline *ldata = lineptr(top.y);
5211 pos nlpos;
5212
5213 /*
5214 * nlpos will point at the maximum position on this line we
5215 * should copy up to. So we start it at the end of the
5216 * line...
5217 */
5218 nlpos.y = top.y;
5219 nlpos.x = term->cols;
5220
5221 /*
5222 * ... move it backwards if there's unused space at the end
5223 * of the line (and also set `nl' if this is the case,
5224 * because in normal selection mode this means we need a
5225 * newline at the end)...
5226 */
5227 if (!(ldata->lattr & LATTR_WRAPPED)) {
5228 while (nlpos.x &&
5229 IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&
5230 !ldata->chars[nlpos.x - 1].cc_next &&
5231 poslt(top, nlpos))
5232 decpos(nlpos);
5233 if (poslt(nlpos, bottom))
5234 nl = TRUE;
5235 } else if (ldata->lattr & LATTR_WRAPPED2) {
5236 /* Ignore the last char on the line in a WRAPPED2 line. */
5237 decpos(nlpos);
5238 }
5239
5240 /*
5241 * ... and then clip it to the terminal x coordinate if
5242 * we're doing rectangular selection. (In this case we
5243 * still did the above, so that copying e.g. the right-hand
5244 * column from a table doesn't fill with spaces on the
5245 * right.)
5246 */
5247 if (rect) {
5248 if (nlpos.x > bottom.x)
5249 nlpos.x = bottom.x;
5250 nl = (top.y < bottom.y);
5251 }
5252
5253 while (poslt(top, bottom) && poslt(top, nlpos)) {
5254 #if 0
5255 char cbuf[16], *p;
5256 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
5257 #else
5258 wchar_t cbuf[16], *p;
5259 int set, c;
5260 int x = top.x;
5261
5262 if (ldata->chars[x].chr == UCSWIDE) {
5263 top.x++;
5264 continue;
5265 }
5266
5267 while (1) {
5268 int uc = ldata->chars[x].chr;
5269 attr = ldata->chars[x].attr;
5270
5271 switch (uc & CSET_MASK) {
5272 case CSET_LINEDRW:
5273 if (!term->cfg.rawcnp) {
5274 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
5275 break;
5276 }
5277 case CSET_ASCII:
5278 uc = term->ucsdata->unitab_line[uc & 0xFF];
5279 break;
5280 case CSET_SCOACS:
5281 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
5282 break;
5283 }
5284 switch (uc & CSET_MASK) {
5285 case CSET_ACP:
5286 uc = term->ucsdata->unitab_font[uc & 0xFF];
5287 break;
5288 case CSET_OEMCP:
5289 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
5290 break;
5291 }
5292
5293 set = (uc & CSET_MASK);
5294 c = (uc & ~CSET_MASK);
5295 #ifdef PLATFORM_IS_UTF16
5296 if (uc > 0x10000 && uc < 0x110000) {
5297 cbuf[0] = 0xD800 | ((uc - 0x10000) >> 10);
5298 cbuf[1] = 0xDC00 | ((uc - 0x10000) & 0x3FF);
5299 cbuf[2] = 0;
5300 } else
5301 #endif
5302 {
5303 cbuf[0] = uc;
5304 cbuf[1] = 0;
5305 }
5306
5307 if (DIRECT_FONT(uc)) {
5308 if (c >= ' ' && c != 0x7F) {
5309 char buf[4];
5310 WCHAR wbuf[4];
5311 int rv;
5312 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
5313 buf[0] = c;
5314 buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);
5315 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
5316 top.x++;
5317 } else {
5318 buf[0] = c;
5319 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
5320 }
5321
5322 if (rv > 0) {
5323 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
5324 cbuf[rv] = 0;
5325 }
5326 }
5327 }
5328 #endif
5329
5330 for (p = cbuf; *p; p++)
5331 clip_addchar(&buf, *p, attr);
5332
5333 if (ldata->chars[x].cc_next)
5334 x += ldata->chars[x].cc_next;
5335 else
5336 break;
5337 }
5338 top.x++;
5339 }
5340 if (nl) {
5341 int i;
5342 for (i = 0; i < sel_nl_sz; i++)
5343 clip_addchar(&buf, sel_nl[i], 0);
5344 }
5345 top.y++;
5346 top.x = rect ? old_top_x : 0;
5347
5348 unlineptr(ldata);
5349 }
5350 #if SELECTION_NUL_TERMINATED
5351 clip_addchar(&buf, 0, 0);
5352 #endif
5353 /* Finally, transfer all that to the clipboard. */
5354 write_clip(term->frontend, buf.textbuf, buf.attrbuf, buf.bufpos, desel);
5355 sfree(buf.textbuf);
5356 sfree(buf.attrbuf);
5357 }
5358
5359 void term_copyall(Terminal *term)
5360 {
5361 pos top;
5362 pos bottom;
5363 tree234 *screen = term->screen;
5364 top.y = -sblines(term);
5365 top.x = 0;
5366 bottom.y = find_last_nonempty_line(term, screen);
5367 bottom.x = term->cols;
5368 clipme(term, top, bottom, 0, TRUE);
5369 }
5370
5371 /*
5372 * The wordness array is mainly for deciding the disposition of the
5373 * US-ASCII characters.
5374 */
5375 static int wordtype(Terminal *term, int uc)
5376 {
5377 struct ucsword {
5378 int start, end, ctype;
5379 };
5380 static const struct ucsword ucs_words[] = {
5381 {
5382 128, 160, 0}, {
5383 161, 191, 1}, {
5384 215, 215, 1}, {
5385 247, 247, 1}, {
5386 0x037e, 0x037e, 1}, /* Greek question mark */
5387 {
5388 0x0387, 0x0387, 1}, /* Greek ano teleia */
5389 {
5390 0x055a, 0x055f, 1}, /* Armenian punctuation */
5391 {
5392 0x0589, 0x0589, 1}, /* Armenian full stop */
5393 {
5394 0x0700, 0x070d, 1}, /* Syriac punctuation */
5395 {
5396 0x104a, 0x104f, 1}, /* Myanmar punctuation */
5397 {
5398 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
5399 {
5400 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
5401 {
5402 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
5403 {
5404 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
5405 {
5406 0x1800, 0x180a, 1}, /* Mongolian punctuation */
5407 {
5408 0x2000, 0x200a, 0}, /* Various spaces */
5409 {
5410 0x2070, 0x207f, 2}, /* superscript */
5411 {
5412 0x2080, 0x208f, 2}, /* subscript */
5413 {
5414 0x200b, 0x27ff, 1}, /* punctuation and symbols */
5415 {
5416 0x3000, 0x3000, 0}, /* ideographic space */
5417 {
5418 0x3001, 0x3020, 1}, /* ideographic punctuation */
5419 {
5420 0x303f, 0x309f, 3}, /* Hiragana */
5421 {
5422 0x30a0, 0x30ff, 3}, /* Katakana */
5423 {
5424 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5425 {
5426 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5427 {
5428 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5429 {
5430 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5431 {
5432 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5433 {
5434 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5435 {
5436 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5437 {
5438 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5439 {
5440 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5441 {
5442 0, 0, 0}
5443 };
5444 const struct ucsword *wptr;
5445
5446 switch (uc & CSET_MASK) {
5447 case CSET_LINEDRW:
5448 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
5449 break;
5450 case CSET_ASCII:
5451 uc = term->ucsdata->unitab_line[uc & 0xFF];
5452 break;
5453 case CSET_SCOACS:
5454 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
5455 break;
5456 }
5457 switch (uc & CSET_MASK) {
5458 case CSET_ACP:
5459 uc = term->ucsdata->unitab_font[uc & 0xFF];
5460 break;
5461 case CSET_OEMCP:
5462 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
5463 break;
5464 }
5465
5466 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5467 * fail as there's such a thing as a double width space. :-(
5468 */
5469 if (term->ucsdata->dbcs_screenfont &&
5470 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
5471 return (uc != ' ');
5472
5473 if (uc < 0x80)
5474 return term->wordness[uc];
5475
5476 for (wptr = ucs_words; wptr->start; wptr++) {
5477 if (uc >= wptr->start && uc <= wptr->end)
5478 return wptr->ctype;
5479 }
5480
5481 return 2;
5482 }
5483
5484 /*
5485 * Spread the selection outwards according to the selection mode.
5486 */
5487 static pos sel_spread_half(Terminal *term, pos p, int dir)
5488 {
5489 termline *ldata;
5490 short wvalue;
5491 int topy = -sblines(term);
5492
5493 ldata = lineptr(p.y);
5494
5495 switch (term->selmode) {
5496 case SM_CHAR:
5497 /*
5498 * In this mode, every character is a separate unit, except
5499 * for runs of spaces at the end of a non-wrapping line.
5500 */
5501 if (!(ldata->lattr & LATTR_WRAPPED)) {
5502 termchar *q = ldata->chars + term->cols;
5503 while (q > ldata->chars &&
5504 IS_SPACE_CHR(q[-1].chr) && !q[-1].cc_next)
5505 q--;
5506 if (q == ldata->chars + term->cols)
5507 q--;
5508 if (p.x >= q - ldata->chars)
5509 p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);
5510 }
5511 break;
5512 case SM_WORD:
5513 /*
5514 * In this mode, the units are maximal runs of characters
5515 * whose `wordness' has the same value.
5516 */
5517 wvalue = wordtype(term, UCSGET(ldata->chars, p.x));
5518 if (dir == +1) {
5519 while (1) {
5520 int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?
5521 term->cols-1 : term->cols);
5522 if (p.x < maxcols-1) {
5523 if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)
5524 p.x++;
5525 else
5526 break;
5527 } else {
5528 if (ldata->lattr & LATTR_WRAPPED) {
5529 termline *ldata2;
5530 ldata2 = lineptr(p.y+1);
5531 if (wordtype(term, UCSGET(ldata2->chars, 0))
5532 == wvalue) {
5533 p.x = 0;
5534 p.y++;
5535 unlineptr(ldata);
5536 ldata = ldata2;
5537 } else {
5538 unlineptr(ldata2);
5539 break;
5540 }
5541 } else
5542 break;
5543 }
5544 }
5545 } else {
5546 while (1) {
5547 if (p.x > 0) {
5548 if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)
5549 p.x--;
5550 else
5551 break;
5552 } else {
5553 termline *ldata2;
5554 int maxcols;
5555 if (p.y <= topy)
5556 break;
5557 ldata2 = lineptr(p.y-1);
5558 maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?
5559 term->cols-1 : term->cols);
5560 if (ldata2->lattr & LATTR_WRAPPED) {
5561 if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))
5562 == wvalue) {
5563 p.x = maxcols-1;
5564 p.y--;
5565 unlineptr(ldata);
5566 ldata = ldata2;
5567 } else {
5568 unlineptr(ldata2);
5569 break;
5570 }
5571 } else
5572 break;
5573 }
5574 }
5575 }
5576 break;
5577 case SM_LINE:
5578 /*
5579 * In this mode, every line is a unit.
5580 */
5581 p.x = (dir == -1 ? 0 : term->cols - 1);
5582 break;
5583 }
5584
5585 unlineptr(ldata);
5586 return p;
5587 }
5588
5589 static void sel_spread(Terminal *term)
5590 {
5591 if (term->seltype == LEXICOGRAPHIC) {
5592 term->selstart = sel_spread_half(term, term->selstart, -1);
5593 decpos(term->selend);
5594 term->selend = sel_spread_half(term, term->selend, +1);
5595 incpos(term->selend);
5596 }
5597 }
5598
5599 void term_do_paste(Terminal *term)
5600 {
5601 wchar_t *data;
5602 int len;
5603
5604 get_clip(term->frontend, &data, &len);
5605 if (data && len > 0) {
5606 wchar_t *p, *q;
5607
5608 term_seen_key_event(term); /* pasted data counts */
5609
5610 if (term->paste_buffer)
5611 sfree(term->paste_buffer);
5612 term->paste_pos = term->paste_hold = term->paste_len = 0;
5613 term->paste_buffer = snewn(len, wchar_t);
5614
5615 p = q = data;
5616 while (p < data + len) {
5617 while (p < data + len &&
5618 !(p <= data + len - sel_nl_sz &&
5619 !memcmp(p, sel_nl, sizeof(sel_nl))))
5620 p++;
5621
5622 {
5623 int i;
5624 for (i = 0; i < p - q; i++) {
5625 term->paste_buffer[term->paste_len++] = q[i];
5626 }
5627 }
5628
5629 if (p <= data + len - sel_nl_sz &&
5630 !memcmp(p, sel_nl, sizeof(sel_nl))) {
5631 term->paste_buffer[term->paste_len++] = '\015';
5632 p += sel_nl_sz;
5633 }
5634 q = p;
5635 }
5636
5637 /* Assume a small paste will be OK in one go. */
5638 if (term->paste_len < 256) {
5639 if (term->ldisc)
5640 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
5641 if (term->paste_buffer)
5642 sfree(term->paste_buffer);
5643 term->paste_buffer = 0;
5644 term->paste_pos = term->paste_hold = term->paste_len = 0;
5645 }
5646 }
5647 get_clip(term->frontend, NULL, NULL);
5648 }
5649
5650 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
5651 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
5652 {
5653 pos selpoint;
5654 termline *ldata;
5655 int raw_mouse = (term->xterm_mouse &&
5656 !term->cfg.no_mouse_rep &&
5657 !(term->cfg.mouse_override && shift));
5658 int default_seltype;
5659
5660 if (y < 0) {
5661 y = 0;
5662 if (a == MA_DRAG && !raw_mouse)
5663 term_scroll(term, 0, -1);
5664 }
5665 if (y >= term->rows) {
5666 y = term->rows - 1;
5667 if (a == MA_DRAG && !raw_mouse)
5668 term_scroll(term, 0, +1);
5669 }
5670 if (x < 0) {
5671 if (y > 0) {
5672 x = term->cols - 1;
5673 y--;
5674 } else
5675 x = 0;
5676 }
5677 if (x >= term->cols)
5678 x = term->cols - 1;
5679
5680 selpoint.y = y + term->disptop;
5681 ldata = lineptr(selpoint.y);
5682
5683 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)
5684 x /= 2;
5685
5686 /*
5687 * Transform x through the bidi algorithm to find the _logical_
5688 * click point from the physical one.
5689 */
5690 if (term_bidi_line(term, ldata, y) != NULL) {
5691 x = term->post_bidi_cache[y].backward[x];
5692 }
5693
5694 selpoint.x = x;
5695 unlineptr(ldata);
5696
5697 /*
5698 * If we're in the middle of a selection operation, we ignore raw
5699 * mouse mode until it's done (we must have been not in raw mouse
5700 * mode when it started).
5701 * This makes use of Shift for selection reliable, and avoids the
5702 * host seeing mouse releases for which they never saw corresponding
5703 * presses.
5704 */
5705 if (raw_mouse &&
5706 (term->selstate != ABOUT_TO) && (term->selstate != DRAGGING)) {
5707 int encstate = 0, r, c;
5708 char abuf[16];
5709
5710 if (term->ldisc) {
5711
5712 switch (braw) {
5713 case MBT_LEFT:
5714 encstate = 0x20; /* left button down */
5715 break;
5716 case MBT_MIDDLE:
5717 encstate = 0x21;
5718 break;
5719 case MBT_RIGHT:
5720 encstate = 0x22;
5721 break;
5722 case MBT_WHEEL_UP:
5723 encstate = 0x60;
5724 break;
5725 case MBT_WHEEL_DOWN:
5726 encstate = 0x61;
5727 break;
5728 default: break; /* placate gcc warning about enum use */
5729 }
5730 switch (a) {
5731 case MA_DRAG:
5732 if (term->xterm_mouse == 1)
5733 return;
5734 encstate += 0x20;
5735 break;
5736 case MA_RELEASE:
5737 encstate = 0x23;
5738 term->mouse_is_down = 0;
5739 break;
5740 case MA_CLICK:
5741 if (term->mouse_is_down == braw)
5742 return;
5743 term->mouse_is_down = braw;
5744 break;
5745 default: break; /* placate gcc warning about enum use */
5746 }
5747 if (shift)
5748 encstate += 0x04;
5749 if (ctrl)
5750 encstate += 0x10;
5751 r = y + 33;
5752 c = x + 33;
5753
5754 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
5755 ldisc_send(term->ldisc, abuf, 6, 0);
5756 }
5757 return;
5758 }
5759
5760 /*
5761 * Set the selection type (rectangular or normal) at the start
5762 * of a selection attempt, from the state of Alt.
5763 */
5764 if (!alt ^ !term->cfg.rect_select)
5765 default_seltype = RECTANGULAR;
5766 else
5767 default_seltype = LEXICOGRAPHIC;
5768
5769 if (term->selstate == NO_SELECTION) {
5770 term->seltype = default_seltype;
5771 }
5772
5773 if (bcooked == MBT_SELECT && a == MA_CLICK) {
5774 deselect(term);
5775 term->selstate = ABOUT_TO;
5776 term->seltype = default_seltype;
5777 term->selanchor = selpoint;
5778 term->selmode = SM_CHAR;
5779 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
5780 deselect(term);
5781 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
5782 term->selstate = DRAGGING;
5783 term->selstart = term->selanchor = selpoint;
5784 term->selend = term->selstart;
5785 incpos(term->selend);
5786 sel_spread(term);
5787 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
5788 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
5789 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
5790 return;
5791 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
5792 term->selstate == SELECTED) {
5793 if (term->seltype == LEXICOGRAPHIC) {
5794 /*
5795 * For normal selection, we extend by moving
5796 * whichever end of the current selection is closer
5797 * to the mouse.
5798 */
5799 if (posdiff(selpoint, term->selstart) <
5800 posdiff(term->selend, term->selstart) / 2) {
5801 term->selanchor = term->selend;
5802 decpos(term->selanchor);
5803 } else {
5804 term->selanchor = term->selstart;
5805 }
5806 } else {
5807 /*
5808 * For rectangular selection, we have a choice of
5809 * _four_ places to put selanchor and selpoint: the
5810 * four corners of the selection.
5811 */
5812 if (2*selpoint.x < term->selstart.x + term->selend.x)
5813 term->selanchor.x = term->selend.x-1;
5814 else
5815 term->selanchor.x = term->selstart.x;
5816
5817 if (2*selpoint.y < term->selstart.y + term->selend.y)
5818 term->selanchor.y = term->selend.y;
5819 else
5820 term->selanchor.y = term->selstart.y;
5821 }
5822 term->selstate = DRAGGING;
5823 }
5824 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
5825 term->selanchor = selpoint;
5826 term->selstate = DRAGGING;
5827 if (term->seltype == LEXICOGRAPHIC) {
5828 /*
5829 * For normal selection, we set (selstart,selend) to
5830 * (selpoint,selanchor) in some order.
5831 */
5832 if (poslt(selpoint, term->selanchor)) {
5833 term->selstart = selpoint;
5834 term->selend = term->selanchor;
5835 incpos(term->selend);
5836 } else {
5837 term->selstart = term->selanchor;
5838 term->selend = selpoint;
5839 incpos(term->selend);
5840 }
5841 } else {
5842 /*
5843 * For rectangular selection, we may need to
5844 * interchange x and y coordinates (if the user has
5845 * dragged in the -x and +y directions, or vice versa).
5846 */
5847 term->selstart.x = min(term->selanchor.x, selpoint.x);
5848 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
5849 term->selstart.y = min(term->selanchor.y, selpoint.y);
5850 term->selend.y = max(term->selanchor.y, selpoint.y);
5851 }
5852 sel_spread(term);
5853 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
5854 a == MA_RELEASE) {
5855 if (term->selstate == DRAGGING) {
5856 /*
5857 * We've completed a selection. We now transfer the
5858 * data to the clipboard.
5859 */
5860 clipme(term, term->selstart, term->selend,
5861 (term->seltype == RECTANGULAR), FALSE);
5862 term->selstate = SELECTED;
5863 } else
5864 term->selstate = NO_SELECTION;
5865 } else if (bcooked == MBT_PASTE
5866 && (a == MA_CLICK
5867 #if MULTICLICK_ONLY_EVENT
5868 || a == MA_2CLK || a == MA_3CLK
5869 #endif
5870 )) {
5871 request_paste(term->frontend);
5872 }
5873
5874 term_update(term);
5875 }
5876
5877 int format_arrow_key(char *buf, Terminal *term, int xkey, int ctrl)
5878 {
5879 char *p = buf;
5880
5881 if (term->vt52_mode)
5882 p += sprintf((char *) p, "\x1B%c", xkey);
5883 else {
5884 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
5885 #if 0
5886 /*
5887 * RDB: VT100 & VT102 manuals both state the app cursor
5888 * keys only work if the app keypad is on.
5889 *
5890 * SGT: That may well be true, but xterm disagrees and so
5891 * does at least one application, so I've #if'ed this out
5892 * and the behaviour is back to PuTTY's original: app
5893 * cursor and app keypad are independently switchable
5894 * modes. If anyone complains about _this_ I'll have to
5895 * put in a configurable option.
5896 */
5897 if (!term->app_keypad_keys)
5898 app_flg = 0;
5899 #endif
5900 /* Useful mapping of Ctrl-arrows */
5901 if (ctrl)
5902 app_flg = !app_flg;
5903
5904 if (app_flg)
5905 p += sprintf((char *) p, "\x1BO%c", xkey);
5906 else
5907 p += sprintf((char *) p, "\x1B[%c", xkey);
5908 }
5909
5910 return p - buf;
5911 }
5912
5913 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
5914 unsigned int modifiers, unsigned int flags)
5915 {
5916 char output[10];
5917 char *p = output;
5918 int prependesc = FALSE;
5919 #if 0
5920 int i;
5921
5922 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
5923 for (i = 0; i < tlen; i++)
5924 fprintf(stderr, " %04x", (unsigned)text[i]);
5925 fprintf(stderr, "\n");
5926 #endif
5927
5928 /* XXX Num Lock */
5929 if ((flags & PKF_REPEAT) && term->repeat_off)
5930 return;
5931
5932 /* Currently, Meta always just prefixes everything with ESC. */
5933 if (modifiers & PKM_META)
5934 prependesc = TRUE;
5935 modifiers &= ~PKM_META;
5936
5937 /*
5938 * Alt is only used for Alt+keypad, which isn't supported yet, so
5939 * ignore it.
5940 */
5941 modifiers &= ~PKM_ALT;
5942
5943 /* Standard local function keys */
5944 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
5945 case PKM_SHIFT:
5946 if (keysym == PK_PAGEUP)
5947 /* scroll up one page */;
5948 if (keysym == PK_PAGEDOWN)
5949 /* scroll down on page */;
5950 if (keysym == PK_INSERT)
5951 term_do_paste(term);
5952 break;
5953 case PKM_CONTROL:
5954 if (keysym == PK_PAGEUP)
5955 /* scroll up one line */;
5956 if (keysym == PK_PAGEDOWN)
5957 /* scroll down one line */;
5958 /* Control-Numlock for app-keypad mode switch */
5959 if (keysym == PK_PF1)
5960 term->app_keypad_keys ^= 1;
5961 break;
5962 }
5963
5964 if (modifiers & PKM_ALT) {
5965 /* Alt+F4 (close) */
5966 /* Alt+Return (full screen) */
5967 /* Alt+Space (system menu) */
5968 }
5969
5970 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
5971 text[0] >= 0x20 && text[0] <= 0x7e) {
5972 /* ASCII chars + Control */
5973 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
5974 (text[0] >= 0x61 && text[0] <= 0x7a))
5975 text[0] &= 0x1f;
5976 else {
5977 /*
5978 * Control-2 should return ^@ (0x00), Control-6 should return
5979 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5980 * the DOS keyboard handling did it, and we have nothing better
5981 * to do with the key combo in question, we'll also map
5982 * Control-Backquote to ^\ (0x1C).
5983 */
5984 switch (text[0]) {
5985 case ' ': text[0] = 0x00; break;
5986 case '-': text[0] = 0x1f; break;
5987 case '/': text[0] = 0x1f; break;
5988 case '2': text[0] = 0x00; break;
5989 case '3': text[0] = 0x1b; break;
5990 case '4': text[0] = 0x1c; break;
5991 case '5': text[0] = 0x1d; break;
5992 case '6': text[0] = 0x1e; break;
5993 case '7': text[0] = 0x1f; break;
5994 case '8': text[0] = 0x7f; break;
5995 case '`': text[0] = 0x1c; break;
5996 }
5997 }
5998 }
5999
6000 /* Nethack keypad */
6001 if (term->cfg.nethack_keypad) {
6002 char c = 0;
6003 switch (keysym) {
6004 case PK_KP1: c = 'b'; break;
6005 case PK_KP2: c = 'j'; break;
6006 case PK_KP3: c = 'n'; break;
6007 case PK_KP4: c = 'h'; break;
6008 case PK_KP5: c = '.'; break;
6009 case PK_KP6: c = 'l'; break;
6010 case PK_KP7: c = 'y'; break;
6011 case PK_KP8: c = 'k'; break;
6012 case PK_KP9: c = 'u'; break;
6013 default: break; /* else gcc warns `enum value not used' */
6014 }
6015 if (c != 0) {
6016 if (c != '.') {
6017 if (modifiers & PKM_CONTROL)
6018 c &= 0x1f;
6019 else if (modifiers & PKM_SHIFT)
6020 c = toupper((unsigned char)c);
6021 }
6022 *p++ = c;
6023 goto done;
6024 }
6025 }
6026
6027 /* Numeric Keypad */
6028 if (PK_ISKEYPAD(keysym)) {
6029 int xkey = 0;
6030
6031 /*
6032 * In VT400 mode, PFn always emits an escape sequence. In
6033 * Linux and tilde modes, this only happens in app keypad mode.
6034 */
6035 if (term->cfg.funky_type == FUNKY_VT400 ||
6036 ((term->cfg.funky_type == FUNKY_LINUX ||
6037 term->cfg.funky_type == FUNKY_TILDE) &&
6038 term->app_keypad_keys && !term->cfg.no_applic_k)) {
6039 switch (keysym) {
6040 case PK_PF1: xkey = 'P'; break;
6041 case PK_PF2: xkey = 'Q'; break;
6042 case PK_PF3: xkey = 'R'; break;
6043 case PK_PF4: xkey = 'S'; break;
6044 default: break; /* else gcc warns `enum value not used' */
6045 }
6046 }
6047 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
6048 switch (keysym) {
6049 case PK_KP0: xkey = 'p'; break;
6050 case PK_KP1: xkey = 'q'; break;
6051 case PK_KP2: xkey = 'r'; break;
6052 case PK_KP3: xkey = 's'; break;
6053 case PK_KP4: xkey = 't'; break;
6054 case PK_KP5: xkey = 'u'; break;
6055 case PK_KP6: xkey = 'v'; break;
6056 case PK_KP7: xkey = 'w'; break;
6057 case PK_KP8: xkey = 'x'; break;
6058 case PK_KP9: xkey = 'y'; break;
6059 case PK_KPDECIMAL: xkey = 'n'; break;
6060 case PK_KPENTER: xkey = 'M'; break;
6061 default: break; /* else gcc warns `enum value not used' */
6062 }
6063 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
6064 /*
6065 * xterm can't see the layout of the keypad, so it has
6066 * to rely on the X keysyms returned by the keys.
6067 * Hence, we look at the strings here, not the PuTTY
6068 * keysyms (which describe the layout).
6069 */
6070 switch (text[0]) {
6071 case '+':
6072 if (modifiers & PKM_SHIFT)
6073 xkey = 'l';
6074 else
6075 xkey = 'k';
6076 break;
6077 case '/': xkey = 'o'; break;
6078 case '*': xkey = 'j'; break;
6079 case '-': xkey = 'm'; break;
6080 }
6081 } else {
6082 /*
6083 * In all other modes, we try to retain the layout of
6084 * the DEC keypad in application mode.
6085 */
6086 switch (keysym) {
6087 case PK_KPBIGPLUS:
6088 /* This key covers the '-' and ',' keys on a VT220 */
6089 if (modifiers & PKM_SHIFT)
6090 xkey = 'm'; /* VT220 '-' */
6091 else
6092 xkey = 'l'; /* VT220 ',' */
6093 break;
6094 case PK_KPMINUS: xkey = 'm'; break;
6095 case PK_KPCOMMA: xkey = 'l'; break;
6096 default: break; /* else gcc warns `enum value not used' */
6097 }
6098 }
6099 }
6100 if (xkey) {
6101 if (term->vt52_mode) {
6102 if (xkey >= 'P' && xkey <= 'S')
6103 p += sprintf((char *) p, "\x1B%c", xkey);
6104 else
6105 p += sprintf((char *) p, "\x1B?%c", xkey);
6106 } else
6107 p += sprintf((char *) p, "\x1BO%c", xkey);
6108 goto done;
6109 }
6110 /* Not in application mode -- treat the number pad as arrow keys? */
6111 if ((flags & PKF_NUMLOCK) == 0) {
6112 switch (keysym) {
6113 case PK_KP0: keysym = PK_INSERT; break;
6114 case PK_KP1: keysym = PK_END; break;
6115 case PK_KP2: keysym = PK_DOWN; break;
6116 case PK_KP3: keysym = PK_PAGEDOWN; break;
6117 case PK_KP4: keysym = PK_LEFT; break;
6118 case PK_KP5: keysym = PK_REST; break;
6119 case PK_KP6: keysym = PK_RIGHT; break;
6120 case PK_KP7: keysym = PK_HOME; break;
6121 case PK_KP8: keysym = PK_UP; break;
6122 case PK_KP9: keysym = PK_PAGEUP; break;
6123 default: break; /* else gcc warns `enum value not used' */
6124 }
6125 }
6126 }
6127
6128 /* Miscellaneous keys */
6129 switch (keysym) {
6130 case PK_ESCAPE:
6131 *p++ = 0x1b;
6132 goto done;
6133 case PK_BACKSPACE:
6134 if (modifiers == 0)
6135 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
6136 else if (modifiers == PKM_SHIFT)
6137 /* We do the opposite of what is configured */
6138 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
6139 else break;
6140 goto done;
6141 case PK_TAB:
6142 if (modifiers == 0)
6143 *p++ = 0x09;
6144 else if (modifiers == PKM_SHIFT)
6145 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
6146 else break;
6147 goto done;
6148 /* XXX window.c has ctrl+shift+space sending 0xa0 */
6149 case PK_PAUSE:
6150 if (modifiers == PKM_CONTROL)
6151 *p++ = 26;
6152 else break;
6153 goto done;
6154 case PK_RETURN:
6155 case PK_KPENTER: /* Odd keypad modes handled above */
6156 if (modifiers == 0) {
6157 *p++ = 0x0d;
6158 if (term->cr_lf_return)
6159 *p++ = 0x0a;
6160 goto done;
6161 }
6162 default: break; /* else gcc warns `enum value not used' */
6163 }
6164
6165 /* SCO function keys and editing keys */
6166 if (term->cfg.funky_type == FUNKY_SCO) {
6167 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
6168 static char const codes[] =
6169 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
6170 int index = keysym - PK_F1;
6171
6172 if (modifiers & PKM_SHIFT) index += 12;
6173 if (modifiers & PKM_CONTROL) index += 24;
6174 p += sprintf((char *) p, "\x1B[%c", codes[index]);
6175 goto done;
6176 }
6177 if (PK_ISEDITING(keysym)) {
6178 int xkey = 0;
6179
6180 switch (keysym) {
6181 case PK_DELETE: *p++ = 0x7f; goto done;
6182 case PK_HOME: xkey = 'H'; break;
6183 case PK_INSERT: xkey = 'L'; break;
6184 case PK_END: xkey = 'F'; break;
6185 case PK_PAGEUP: xkey = 'I'; break;
6186 case PK_PAGEDOWN: xkey = 'G'; break;
6187 default: break; /* else gcc warns `enum value not used' */
6188 }
6189 p += sprintf((char *) p, "\x1B[%c", xkey);
6190 }
6191 }
6192
6193 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
6194 int code;
6195
6196 if (term->cfg.funky_type == FUNKY_XTERM) {
6197 /* Xterm shuffles these keys, apparently. */
6198 switch (keysym) {
6199 case PK_HOME: keysym = PK_INSERT; break;
6200 case PK_INSERT: keysym = PK_HOME; break;
6201 case PK_DELETE: keysym = PK_END; break;
6202 case PK_END: keysym = PK_PAGEUP; break;
6203 case PK_PAGEUP: keysym = PK_DELETE; break;
6204 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
6205 default: break; /* else gcc warns `enum value not used' */
6206 }
6207 }
6208
6209 /* RXVT Home/End */
6210 if (term->cfg.rxvt_homeend &&
6211 (keysym == PK_HOME || keysym == PK_END)) {
6212 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
6213 goto done;
6214 }
6215
6216 if (term->vt52_mode) {
6217 int xkey;
6218
6219 /*
6220 * A real VT52 doesn't have these, and a VT220 doesn't
6221 * send anything for them in VT52 mode.
6222 */
6223 switch (keysym) {
6224 case PK_HOME: xkey = 'H'; break;
6225 case PK_INSERT: xkey = 'L'; break;
6226 case PK_DELETE: xkey = 'M'; break;
6227 case PK_END: xkey = 'E'; break;
6228 case PK_PAGEUP: xkey = 'I'; break;
6229 case PK_PAGEDOWN: xkey = 'G'; break;
6230 default: xkey=0; break; /* else gcc warns `enum value not used'*/
6231 }
6232 p += sprintf((char *) p, "\x1B%c", xkey);
6233 goto done;
6234 }
6235
6236 switch (keysym) {
6237 case PK_HOME: code = 1; break;
6238 case PK_INSERT: code = 2; break;
6239 case PK_DELETE: code = 3; break;
6240 case PK_END: code = 4; break;
6241 case PK_PAGEUP: code = 5; break;
6242 case PK_PAGEDOWN: code = 6; break;
6243 default: code = 0; break; /* else gcc warns `enum value not used' */
6244 }
6245 p += sprintf((char *) p, "\x1B[%d~", code);
6246 goto done;
6247 }
6248
6249 if (PK_ISFKEY(keysym)) {
6250 /* Map Shift+F1-F10 to F11-F20 */
6251 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
6252 keysym += 10;
6253 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
6254 keysym <= PK_F14) {
6255 /* XXX This overrides the XTERM/VT52 mode below */
6256 int offt = 0;
6257 if (keysym >= PK_F6) offt++;
6258 if (keysym >= PK_F12) offt++;
6259 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
6260 'P' + keysym - PK_F1 - offt);
6261 goto done;
6262 }
6263 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
6264 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
6265 goto done;
6266 }
6267 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
6268 if (term->vt52_mode)
6269 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
6270 else
6271 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
6272 goto done;
6273 }
6274 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
6275 goto done;
6276 }
6277
6278 if (PK_ISCURSOR(keysym)) {
6279 int xkey;
6280
6281 switch (keysym) {
6282 case PK_UP: xkey = 'A'; break;
6283 case PK_DOWN: xkey = 'B'; break;
6284 case PK_RIGHT: xkey = 'C'; break;
6285 case PK_LEFT: xkey = 'D'; break;
6286 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
6287 default: xkey = 0; break; /* else gcc warns `enum value not used' */
6288 }
6289 p += format_arrow_key(p, term, xkey, modifiers == PKM_CONTROL);
6290 goto done;
6291 }
6292
6293 done:
6294 if (p > output || tlen > 0) {
6295 /*
6296 * Interrupt an ongoing paste. I'm not sure
6297 * this is sensible, but for the moment it's
6298 * preferable to having to faff about buffering
6299 * things.
6300 */
6301 term_nopaste(term);
6302
6303 /*
6304 * We need not bother about stdin backlogs
6305 * here, because in GUI PuTTY we can't do
6306 * anything about it anyway; there's no means
6307 * of asking Windows to hold off on KEYDOWN
6308 * messages. We _have_ to buffer everything
6309 * we're sent.
6310 */
6311 term_seen_key_event(term);
6312
6313 if (prependesc) {
6314 #if 0
6315 fprintf(stderr, "sending ESC\n");
6316 #endif
6317 ldisc_send(term->ldisc, "\x1b", 1, 1);
6318 }
6319
6320 if (p > output) {
6321 #if 0
6322 fprintf(stderr, "sending %d bytes:", p - output);
6323 for (i = 0; i < p - output; i++)
6324 fprintf(stderr, " %02x", output[i]);
6325 fprintf(stderr, "\n");
6326 #endif
6327 ldisc_send(term->ldisc, output, p - output, 1);
6328 } else if (tlen > 0) {
6329 #if 0
6330 fprintf(stderr, "sending %d unichars:", tlen);
6331 for (i = 0; i < tlen; i++)
6332 fprintf(stderr, " %04x", (unsigned) text[i]);
6333 fprintf(stderr, "\n");
6334 #endif
6335 luni_send(term->ldisc, text, tlen, 1);
6336 }
6337 }
6338 }
6339
6340 void term_nopaste(Terminal *term)
6341 {
6342 if (term->paste_len == 0)
6343 return;
6344 sfree(term->paste_buffer);
6345 term->paste_buffer = NULL;
6346 term->paste_len = 0;
6347 }
6348
6349 int term_paste_pending(Terminal *term)
6350 {
6351 return term->paste_len != 0;
6352 }
6353
6354 void term_paste(Terminal *term)
6355 {
6356 long now, paste_diff;
6357
6358 if (term->paste_len == 0)
6359 return;
6360
6361 /* Don't wait forever to paste */
6362 if (term->paste_hold) {
6363 now = GETTICKCOUNT();
6364 paste_diff = now - term->last_paste;
6365 if (paste_diff >= 0 && paste_diff < 450)
6366 return;
6367 }
6368 term->paste_hold = 0;
6369
6370 while (term->paste_pos < term->paste_len) {
6371 int n = 0;
6372 while (n + term->paste_pos < term->paste_len) {
6373 if (term->paste_buffer[term->paste_pos + n++] == '\015')
6374 break;
6375 }
6376 if (term->ldisc)
6377 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
6378 term->paste_pos += n;
6379
6380 if (term->paste_pos < term->paste_len) {
6381 term->paste_hold = 1;
6382 return;
6383 }
6384 }
6385 sfree(term->paste_buffer);
6386 term->paste_buffer = NULL;
6387 term->paste_len = 0;
6388 }
6389
6390 static void deselect(Terminal *term)
6391 {
6392 term->selstate = NO_SELECTION;
6393 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
6394 }
6395
6396 void term_deselect(Terminal *term)
6397 {
6398 deselect(term);
6399 term_update(term);
6400 }
6401
6402 int term_ldisc(Terminal *term, int option)
6403 {
6404 if (option == LD_ECHO)
6405 return term->term_echoing;
6406 if (option == LD_EDIT)
6407 return term->term_editing;
6408 return FALSE;
6409 }
6410
6411 int term_data(Terminal *term, int is_stderr, const char *data, int len)
6412 {
6413 bufchain_add(&term->inbuf, data, len);
6414
6415 if (!term->in_term_out) {
6416 term->in_term_out = TRUE;
6417 term_reset_cblink(term);
6418 /*
6419 * During drag-selects, we do not process terminal input,
6420 * because the user will want the screen to hold still to
6421 * be selected.
6422 */
6423 if (term->selstate != DRAGGING)
6424 term_out(term);
6425 term->in_term_out = FALSE;
6426 }
6427
6428 /*
6429 * term_out() always completely empties inbuf. Therefore,
6430 * there's no reason at all to return anything other than zero
6431 * from this function, because there _can't_ be a question of
6432 * the remote side needing to wait until term_out() has cleared
6433 * a backlog.
6434 *
6435 * This is a slightly suboptimal way to deal with SSH-2 - in
6436 * principle, the window mechanism would allow us to continue
6437 * to accept data on forwarded ports and X connections even
6438 * while the terminal processing was going slowly - but we
6439 * can't do the 100% right thing without moving the terminal
6440 * processing into a separate thread, and that might hurt
6441 * portability. So we manage stdout buffering the old SSH-1 way:
6442 * if the terminal processing goes slowly, the whole SSH
6443 * connection stops accepting data until it's ready.
6444 *
6445 * In practice, I can't imagine this causing serious trouble.
6446 */
6447 return 0;
6448 }
6449
6450 /*
6451 * Write untrusted data to the terminal.
6452 * The only control character that should be honoured is \n (which
6453 * will behave as a CRLF).
6454 */
6455 int term_data_untrusted(Terminal *term, const char *data, int len)
6456 {
6457 int i;
6458 /* FIXME: more sophisticated checking? */
6459 for (i = 0; i < len; i++) {
6460 if (data[i] == '\n')
6461 term_data(term, 1, "\r\n", 2);
6462 else if (data[i] & 0x60)
6463 term_data(term, 1, data + i, 1);
6464 }
6465 return 0; /* assumes that term_data() always returns 0 */
6466 }
6467
6468 void term_provide_logctx(Terminal *term, void *logctx)
6469 {
6470 term->logctx = logctx;
6471 }
6472
6473 void term_set_focus(Terminal *term, int has_focus)
6474 {
6475 term->has_focus = has_focus;
6476 term_schedule_cblink(term);
6477 }
6478
6479 /*
6480 * Provide "auto" settings for remote tty modes, suitable for an
6481 * application with a terminal window.
6482 */
6483 char *term_get_ttymode(Terminal *term, const char *mode)
6484 {
6485 char *val = NULL;
6486 if (strcmp(mode, "ERASE") == 0) {
6487 val = term->cfg.bksp_is_delete ? "^?" : "^H";
6488 }
6489 /* FIXME: perhaps we should set ONLCR based on cfg.lfhascr as well? */
6490 /* FIXME: or ECHO and friends based on local echo state? */
6491 return dupstr(val);
6492 }
6493
6494 struct term_userpass_state {
6495 size_t curr_prompt;
6496 int done_prompt; /* printed out prompt yet? */
6497 size_t pos; /* cursor position */
6498 };
6499
6500 /*
6501 * Process some terminal data in the course of username/password
6502 * input.
6503 */
6504 int term_get_userpass_input(Terminal *term, prompts_t *p,
6505 unsigned char *in, int inlen)
6506 {
6507 struct term_userpass_state *s = (struct term_userpass_state *)p->data;
6508 if (!s) {
6509 /*
6510 * First call. Set some stuff up.
6511 */
6512 p->data = s = snew(struct term_userpass_state);
6513 s->curr_prompt = 0;
6514 s->done_prompt = 0;
6515 /* We only print the `name' caption if we have to... */
6516 if (p->name_reqd && p->name) {
6517 size_t l = strlen(p->name);
6518 term_data_untrusted(term, p->name, l);
6519 if (p->name[l-1] != '\n')
6520 term_data_untrusted(term, "\n", 1);
6521 }
6522 /* ...but we always print any `instruction'. */
6523 if (p->instruction) {
6524 size_t l = strlen(p->instruction);
6525 term_data_untrusted(term, p->instruction, l);
6526 if (p->instruction[l-1] != '\n')
6527 term_data_untrusted(term, "\n", 1);
6528 }
6529 /*
6530 * Zero all the results, in case we abort half-way through.
6531 */
6532 {
6533 int i;
6534 for (i = 0; i < (int)p->n_prompts; i++)
6535 memset(p->prompts[i]->result, 0, p->prompts[i]->result_len);
6536 }
6537 }
6538
6539 while (s->curr_prompt < p->n_prompts) {
6540
6541 prompt_t *pr = p->prompts[s->curr_prompt];
6542 int finished_prompt = 0;
6543
6544 if (!s->done_prompt) {
6545 term_data_untrusted(term, pr->prompt, strlen(pr->prompt));
6546 s->done_prompt = 1;
6547 s->pos = 0;
6548 }
6549
6550 /* Breaking out here ensures that the prompt is printed even
6551 * if we're now waiting for user data. */
6552 if (!in || !inlen) break;
6553
6554 /* FIXME: should we be using local-line-editing code instead? */
6555 while (!finished_prompt && inlen) {
6556 char c = *in++;
6557 inlen--;
6558 switch (c) {
6559 case 10:
6560 case 13:
6561 term_data(term, 0, "\r\n", 2);
6562 pr->result[s->pos] = '\0';
6563 pr->result[pr->result_len - 1] = '\0';
6564 /* go to next prompt, if any */
6565 s->curr_prompt++;
6566 s->done_prompt = 0;
6567 finished_prompt = 1; /* break out */
6568 break;
6569 case 8:
6570 case 127:
6571 if (s->pos > 0) {
6572 if (pr->echo)
6573 term_data(term, 0, "\b \b", 3);
6574 s->pos--;
6575 }
6576 break;
6577 case 21:
6578 case 27:
6579 while (s->pos > 0) {
6580 if (pr->echo)
6581 term_data(term, 0, "\b \b", 3);
6582 s->pos--;
6583 }
6584 break;
6585 case 3:
6586 case 4:
6587 /* Immediate abort. */
6588 term_data(term, 0, "\r\n", 2);
6589 sfree(s);
6590 p->data = NULL;
6591 return 0; /* user abort */
6592 default:
6593 /*
6594 * This simplistic check for printability is disabled
6595 * when we're doing password input, because some people
6596 * have control characters in their passwords.
6597 */
6598 if ((!pr->echo ||
6599 (c >= ' ' && c <= '~') ||
6600 ((unsigned char) c >= 160))
6601 && s->pos < pr->result_len - 1) {
6602 pr->result[s->pos++] = c;
6603 if (pr->echo)
6604 term_data(term, 0, &c, 1);
6605 }
6606 break;
6607 }
6608 }
6609
6610 }
6611
6612 if (s->curr_prompt < p->n_prompts) {
6613 return -1; /* more data required */
6614 } else {
6615 sfree(s);
6616 p->data = NULL;
6617 return +1; /* all done */
6618 }
6619 }