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