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