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