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