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