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