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