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