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