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