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