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