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