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