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