Major destabilisation, phase 1. In this phase I've moved (I think)
[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 compatibility(x) \
47 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
48 term->termstate=TOPLEVEL; \
49 break; \
50 }
51 #define compatibility2(x,y) \
52 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
54 break; \
55 }
56
57 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
58
59 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
60 const wchar_t sel_nl[] = SEL_NL;
61
62 /*
63 * Internal prototypes.
64 */
65 static void do_paint(Terminal *, Context, int);
66 static void erase_lots(Terminal *, int, int, int);
67 static void swap_screen(Terminal *, int, int, int);
68 static void update_sbar(Terminal *);
69 static void deselect(Terminal *);
70 static void term_print_finish(Terminal *);
71
72 /*
73 * Resize a line to make it `cols' columns wide.
74 */
75 unsigned long *resizeline(unsigned long *line, int cols)
76 {
77 int i, oldlen;
78 unsigned long lineattrs;
79
80 if (line[0] != (unsigned long)cols) {
81 /*
82 * This line is the wrong length, which probably means it
83 * hasn't been accessed since a resize. Resize it now.
84 */
85 oldlen = line[0];
86 lineattrs = line[oldlen + 1];
87 line = srealloc(line, TSIZE * (2 + cols));
88 line[0] = cols;
89 for (i = oldlen; i < cols; i++)
90 line[i + 1] = ERASE_CHAR;
91 line[cols + 1] = lineattrs & LATTR_MODE;
92 }
93
94 return line;
95 }
96
97 /*
98 * Retrieve a line of the screen or of the scrollback, according to
99 * whether the y coordinate is non-negative or negative
100 * (respectively).
101 */
102 unsigned long *lineptr(Terminal *term, int y, int lineno)
103 {
104 unsigned long *line, *newline;
105 tree234 *whichtree;
106 int treeindex;
107
108 if (y >= 0) {
109 whichtree = term->screen;
110 treeindex = y;
111 } else {
112 whichtree = term->scrollback;
113 treeindex = y + count234(term->scrollback);
114 }
115 line = index234(whichtree, treeindex);
116
117 /* We assume that we don't screw up and retrieve something out of range. */
118 assert(line != NULL);
119
120 newline = resizeline(line, term->cols);
121 if (newline != line) {
122 delpos234(whichtree, treeindex);
123 addpos234(whichtree, newline, treeindex);
124 line = newline;
125 }
126
127 return line + 1;
128 }
129
130 #define lineptr(x) lineptr(term,x,__LINE__)
131
132 /*
133 * Set up power-on settings for the terminal.
134 */
135 static void power_on(Terminal *term)
136 {
137 term->curs.x = term->curs.y = 0;
138 term->alt_x = term->alt_y = 0;
139 term->savecurs.x = term->savecurs.y = 0;
140 term->alt_t = term->marg_t = 0;
141 if (term->rows != -1)
142 term->alt_b = term->marg_b = term->rows - 1;
143 else
144 term->alt_b = term->marg_b = 0;
145 if (term->cols != -1) {
146 int i;
147 for (i = 0; i < term->cols; i++)
148 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
149 }
150 term->alt_om = term->dec_om = cfg.dec_om;
151 term->alt_wnext = term->wrapnext = term->alt_ins = term->insert = FALSE;
152 term->alt_wrap = term->wrap = cfg.wrap_mode;
153 term->alt_cset = term->cset = 0;
154 term->alt_utf = term->utf = 0;
155 term->alt_sco_acs = term->sco_acs = 0;
156 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
157 term->rvideo = 0;
158 term->in_vbell = FALSE;
159 term->cursor_on = 1;
160 term->big_cursor = 0;
161 term->save_attr = term->curr_attr = ATTR_DEFAULT;
162 term->term_editing = term->term_echoing = FALSE;
163 term->app_cursor_keys = cfg.app_cursor;
164 term->app_keypad_keys = cfg.app_keypad;
165 term->use_bce = cfg.bce;
166 term->blink_is_real = cfg.blinktext;
167 term->erase_char = ERASE_CHAR;
168 term->alt_which = 0;
169 term_print_finish(term);
170 {
171 int i;
172 for (i = 0; i < 256; i++)
173 term->wordness[i] = cfg.wordness[i];
174 }
175 if (term->screen) {
176 swap_screen(term, 1, FALSE, FALSE);
177 erase_lots(term, FALSE, TRUE, TRUE);
178 swap_screen(term, 0, FALSE, FALSE);
179 erase_lots(term, FALSE, TRUE, TRUE);
180 }
181 }
182
183 /*
184 * Force a screen update.
185 */
186 void term_update(Terminal *term)
187 {
188 Context ctx;
189 ctx = get_ctx();
190 if (ctx) {
191 int need_sbar_update = term->seen_disp_event;
192 if (term->seen_disp_event && cfg.scroll_on_disp) {
193 term->disptop = 0; /* return to main screen */
194 term->seen_disp_event = 0;
195 need_sbar_update = TRUE;
196 }
197 if (need_sbar_update)
198 update_sbar(term);
199 do_paint(term, ctx, TRUE);
200 sys_cursor(term->curs.x, term->curs.y - term->disptop);
201 free_ctx(ctx);
202 }
203 }
204
205 /*
206 * Called from front end when a keypress occurs, to trigger
207 * anything magical that needs to happen in that situation.
208 */
209 void term_seen_key_event(Terminal *term)
210 {
211 /*
212 * On any keypress, clear the bell overload mechanism
213 * completely, on the grounds that large numbers of
214 * beeps coming from deliberate key action are likely
215 * to be intended (e.g. beeps from filename completion
216 * blocking repeatedly).
217 */
218 term->beep_overloaded = FALSE;
219 while (term->beephead) {
220 struct beeptime *tmp = term->beephead;
221 term->beephead = tmp->next;
222 sfree(tmp);
223 }
224 term->beeptail = NULL;
225 term->nbeeps = 0;
226
227 /*
228 * Reset the scrollback on keypress, if we're doing that.
229 */
230 if (cfg.scroll_on_key) {
231 term->disptop = 0; /* return to main screen */
232 term->seen_disp_event = 1;
233 }
234 }
235
236 /*
237 * Same as power_on(), but an external function.
238 */
239 void term_pwron(Terminal *term)
240 {
241 power_on(term);
242 ldisc_send(NULL, 0, 0); /* cause ldisc to notice changes */
243 fix_cpos;
244 term->disptop = 0;
245 deselect(term);
246 term_update(term);
247 }
248
249 /*
250 * When the user reconfigures us, we need to check the forbidden-
251 * alternate-screen config option, disable raw mouse mode if the
252 * user has disabled mouse reporting, and abandon a print job if
253 * the user has disabled printing.
254 */
255 void term_reconfig(Terminal *term)
256 {
257 if (cfg.no_alt_screen)
258 swap_screen(term, 0, FALSE, FALSE);
259 if (cfg.no_mouse_rep) {
260 term->xterm_mouse = 0;
261 set_raw_mouse_mode(0);
262 }
263 if (cfg.no_remote_charset) {
264 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
265 term->sco_acs = term->alt_sco_acs = 0;
266 term->utf = 0;
267 }
268 if (!*cfg.printer) {
269 term_print_finish(term);
270 }
271 }
272
273 /*
274 * Clear the scrollback.
275 */
276 void term_clrsb(Terminal *term)
277 {
278 unsigned long *line;
279 term->disptop = 0;
280 while ((line = delpos234(term->scrollback, 0)) != NULL) {
281 sfree(line);
282 }
283 update_sbar(term);
284 }
285
286 /*
287 * Initialise the terminal.
288 */
289 Terminal *term_init(void)
290 {
291 Terminal *term;
292
293 /*
294 * Allocate a new Terminal structure and initialise the fields
295 * that need it.
296 */
297 term = smalloc(sizeof(Terminal));
298 term->compatibility_level = TM_PUTTY;
299 strcpy(term->id_string, "\033[?6c");
300 term->last_blink = term->last_tblink = 0;
301 term->paste_buffer = NULL;
302 term->last_paste = 0;
303 bufchain_init(&term->inbuf);
304 bufchain_init(&term->printer_buf);
305
306 term->screen = term->alt_screen = term->scrollback = NULL;
307 term->disptop = 0;
308 term->disptext = term->dispcurs = NULL;
309 term->tabs = NULL;
310 deselect(term);
311 term->rows = term->cols = -1;
312 power_on(term);
313 term->beephead = term->beeptail = NULL;
314 term->nbeeps = 0;
315 term->lastbeep = FALSE;
316 term->beep_overloaded = FALSE;
317
318 return term;
319 }
320
321 /*
322 * Set up the terminal for a given size.
323 */
324 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
325 {
326 tree234 *newalt;
327 unsigned long *newdisp, *line;
328 int i, j;
329 int sblen;
330 int save_alt_which = term->alt_which;
331
332 if (newrows == term->rows && newcols == term->cols &&
333 newsavelines == term->savelines)
334 return; /* nothing to do */
335
336 deselect(term);
337 swap_screen(term, 0, FALSE, FALSE);
338
339 term->alt_t = term->marg_t = 0;
340 term->alt_b = term->marg_b = newrows - 1;
341
342 if (term->rows == -1) {
343 term->scrollback = newtree234(NULL);
344 term->screen = newtree234(NULL);
345 term->rows = 0;
346 }
347
348 /*
349 * Resize the screen and scrollback. We only need to shift
350 * lines around within our data structures, because lineptr()
351 * will take care of resizing each individual line if
352 * necessary. So:
353 *
354 * - If the new screen and the old screen differ in length, we
355 * must shunt some lines in from the scrollback or out to
356 * the scrollback.
357 *
358 * - If doing that fails to provide us with enough material to
359 * fill the new screen (i.e. the number of rows needed in
360 * the new screen exceeds the total number in the previous
361 * screen+scrollback), we must invent some blank lines to
362 * cover the gap.
363 *
364 * - Then, if the new scrollback length is less than the
365 * amount of scrollback we actually have, we must throw some
366 * away.
367 */
368 sblen = count234(term->scrollback);
369 /* Do this loop to expand the screen if newrows > rows */
370 for (i = term->rows; i < newrows; i++) {
371 if (sblen > 0) {
372 line = delpos234(term->scrollback, --sblen);
373 } else {
374 line = smalloc(TSIZE * (newcols + 2));
375 line[0] = newcols;
376 for (j = 0; j <= newcols; j++)
377 line[j + 1] = ERASE_CHAR;
378 }
379 addpos234(term->screen, line, 0);
380 }
381 /* Do this loop to shrink the screen if newrows < rows */
382 for (i = newrows; i < term->rows; i++) {
383 line = delpos234(term->screen, 0);
384 addpos234(term->scrollback, line, sblen++);
385 }
386 assert(count234(term->screen) == newrows);
387 while (sblen > newsavelines) {
388 line = delpos234(term->scrollback, 0);
389 sfree(line);
390 sblen--;
391 }
392 assert(count234(term->scrollback) <= newsavelines);
393 term->disptop = 0;
394
395 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
396 for (i = 0; i < newrows * (newcols + 1); i++)
397 newdisp[i] = ATTR_INVALID;
398 sfree(term->disptext);
399 term->disptext = newdisp;
400 term->dispcurs = NULL;
401
402 newalt = newtree234(NULL);
403 for (i = 0; i < newrows; i++) {
404 line = smalloc(TSIZE * (newcols + 2));
405 line[0] = newcols;
406 for (j = 0; j <= newcols; j++)
407 line[j + 1] = term->erase_char;
408 addpos234(newalt, line, i);
409 }
410 if (term->alt_screen) {
411 while (NULL != (line = delpos234(term->alt_screen, 0)))
412 sfree(line);
413 freetree234(term->alt_screen);
414 }
415 term->alt_screen = newalt;
416
417 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
418 {
419 int i;
420 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
421 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
422 }
423
424 if (term->rows > 0)
425 term->curs.y += newrows - term->rows;
426 if (term->curs.y < 0)
427 term->curs.y = 0;
428 if (term->curs.y >= newrows)
429 term->curs.y = newrows - 1;
430 if (term->curs.x >= newcols)
431 term->curs.x = newcols - 1;
432 term->alt_x = term->alt_y = 0;
433 term->wrapnext = term->alt_wnext = FALSE;
434
435 term->rows = newrows;
436 term->cols = newcols;
437 term->savelines = newsavelines;
438 fix_cpos;
439
440 swap_screen(term, save_alt_which, FALSE, FALSE);
441
442 update_sbar(term);
443 term_update(term);
444 back->size();
445 }
446
447 /*
448 * Swap screens. If `reset' is TRUE and we have been asked to
449 * switch to the alternate screen, we must bring most of its
450 * configuration from the main screen and erase the contents of the
451 * alternate screen completely. (This is even true if we're already
452 * on it! Blame xterm.)
453 */
454 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
455 {
456 int t;
457 tree234 *ttr;
458
459 if (!which)
460 reset = FALSE; /* do no weird resetting if which==0 */
461
462 if (which != term->alt_which) {
463 term->alt_which = which;
464
465 ttr = term->alt_screen;
466 term->alt_screen = term->screen;
467 term->screen = ttr;
468 t = term->curs.x;
469 if (!reset && !keep_cur_pos)
470 term->curs.x = term->alt_x;
471 term->alt_x = t;
472 t = term->curs.y;
473 if (!reset && !keep_cur_pos)
474 term->curs.y = term->alt_y;
475 term->alt_y = t;
476 t = term->marg_t;
477 if (!reset) term->marg_t = term->alt_t;
478 term->alt_t = t;
479 t = term->marg_b;
480 if (!reset) term->marg_b = term->alt_b;
481 term->alt_b = t;
482 t = term->dec_om;
483 if (!reset) term->dec_om = term->alt_om;
484 term->alt_om = t;
485 t = term->wrap;
486 if (!reset) term->wrap = term->alt_wrap;
487 term->alt_wrap = t;
488 t = term->wrapnext;
489 if (!reset) term->wrapnext = term->alt_wnext;
490 term->alt_wnext = t;
491 t = term->insert;
492 if (!reset) term->insert = term->alt_ins;
493 term->alt_ins = t;
494 t = term->cset;
495 if (!reset) term->cset = term->alt_cset;
496 term->alt_cset = t;
497 t = term->utf;
498 if (!reset) term->utf = term->alt_utf;
499 term->alt_utf = t;
500 t = term->sco_acs;
501 if (!reset) term->sco_acs = term->alt_sco_acs;
502 term->alt_sco_acs = t;
503 }
504
505 if (reset && term->screen) {
506 /*
507 * Yes, this _is_ supposed to honour background-colour-erase.
508 */
509 erase_lots(term, FALSE, TRUE, TRUE);
510 }
511
512 /*
513 * This might not be possible if we're called during
514 * initialisation.
515 */
516 if (term->screen)
517 fix_cpos;
518 }
519
520 /*
521 * Update the scroll bar.
522 */
523 static void update_sbar(Terminal *term)
524 {
525 int nscroll;
526
527 nscroll = count234(term->scrollback);
528
529 set_sbar(nscroll + term->rows, nscroll + term->disptop, term->rows);
530 }
531
532 /*
533 * Check whether the region bounded by the two pointers intersects
534 * the scroll region, and de-select the on-screen selection if so.
535 */
536 static void check_selection(Terminal *term, pos from, pos to)
537 {
538 if (poslt(from, term->selend) && poslt(term->selstart, to))
539 deselect(term);
540 }
541
542 /*
543 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
544 * for backward.) `sb' is TRUE if the scrolling is permitted to
545 * affect the scrollback buffer.
546 *
547 * NB this function invalidates all pointers into lines of the
548 * screen data structures. In particular, you MUST call fix_cpos
549 * after calling scroll() and before doing anything else that
550 * uses the cpos shortcut pointer.
551 */
552 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
553 {
554 unsigned long *line, *line2;
555 int i, seltop;
556
557 if (topline != 0 || term->alt_which != 0)
558 sb = FALSE;
559
560 if (lines < 0) {
561 while (lines < 0) {
562 line = delpos234(term->screen, botline);
563 line = resizeline(line, term->cols);
564 for (i = 0; i < term->cols; i++)
565 line[i + 1] = term->erase_char;
566 line[term->cols + 1] = 0;
567 addpos234(term->screen, line, topline);
568
569 if (term->selstart.y >= topline && term->selstart.y <= botline) {
570 term->selstart.y++;
571 if (term->selstart.y > botline) {
572 term->selstart.y = botline;
573 term->selstart.x = 0;
574 }
575 }
576 if (term->selend.y >= topline && term->selend.y <= botline) {
577 term->selend.y++;
578 if (term->selend.y > botline) {
579 term->selend.y = botline;
580 term->selend.x = 0;
581 }
582 }
583
584 lines++;
585 }
586 } else {
587 while (lines > 0) {
588 line = delpos234(term->screen, topline);
589 if (sb && term->savelines > 0) {
590 int sblen = count234(term->scrollback);
591 /*
592 * We must add this line to the scrollback. We'll
593 * remove a line from the top of the scrollback to
594 * replace it, or allocate a new one if the
595 * scrollback isn't full.
596 */
597 if (sblen == term->savelines) {
598 sblen--, line2 = delpos234(term->scrollback, 0);
599 } else {
600 line2 = smalloc(TSIZE * (term->cols + 2));
601 line2[0] = term->cols;
602 }
603 addpos234(term->scrollback, line, sblen);
604 line = line2;
605
606 /*
607 * If the user is currently looking at part of the
608 * scrollback, and they haven't enabled any options
609 * that are going to reset the scrollback as a
610 * result of this movement, then the chances are
611 * they'd like to keep looking at the same line. So
612 * we move their viewpoint at the same rate as the
613 * scroll, at least until their viewpoint hits the
614 * top end of the scrollback buffer, at which point
615 * we don't have the choice any more.
616 *
617 * Thanks to Jan Holmen Holsten for the idea and
618 * initial implementation.
619 */
620 if (term->disptop > -term->savelines && term->disptop < 0)
621 term->disptop--;
622 }
623 line = resizeline(line, term->cols);
624 for (i = 0; i < term->cols; i++)
625 line[i + 1] = term->erase_char;
626 line[term->cols + 1] = 0;
627 addpos234(term->screen, line, botline);
628
629 /*
630 * If the selection endpoints move into the scrollback,
631 * we keep them moving until they hit the top. However,
632 * of course, if the line _hasn't_ moved into the
633 * scrollback then we don't do this, and cut them off
634 * at the top of the scroll region.
635 *
636 * This applies to selstart and selend (for an existing
637 * selection), and also selanchor (for one being
638 * selected as we speak).
639 */
640 seltop = sb ? -term->savelines : topline;
641
642 if (term->selstart.y >= seltop &&
643 term->selstart.y <= botline) {
644 term->selstart.y--;
645 if (term->selstart.y < seltop) {
646 term->selstart.y = seltop;
647 term->selstart.x = 0;
648 }
649 }
650 if (term->selend.y >= seltop && term->selend.y <= botline) {
651 term->selend.y--;
652 if (term->selend.y < seltop) {
653 term->selend.y = seltop;
654 term->selend.x = 0;
655 }
656 }
657 if (term->selanchor.y >= seltop && term->selanchor.y <= botline) {
658 term->selanchor.y--;
659 if (term->selanchor.y < seltop) {
660 term->selanchor.y = seltop;
661 term->selanchor.x = 0;
662 }
663 }
664
665 lines--;
666 }
667 }
668 }
669
670 /*
671 * Move the cursor to a given position, clipping at boundaries. We
672 * may or may not want to clip at the scroll margin: marg_clip is 0
673 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
674 * even _being_ outside the margins.
675 */
676 static void move(Terminal *term, int x, int y, int marg_clip)
677 {
678 if (x < 0)
679 x = 0;
680 if (x >= term->cols)
681 x = term->cols - 1;
682 if (marg_clip) {
683 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
684 y < term->marg_t)
685 y = term->marg_t;
686 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
687 y > term->marg_b)
688 y = term->marg_b;
689 }
690 if (y < 0)
691 y = 0;
692 if (y >= term->rows)
693 y = term->rows - 1;
694 term->curs.x = x;
695 term->curs.y = y;
696 fix_cpos;
697 term->wrapnext = FALSE;
698 }
699
700 /*
701 * Save or restore the cursor and SGR mode.
702 */
703 static void save_cursor(Terminal *term, int save)
704 {
705 if (save) {
706 term->savecurs = term->curs;
707 term->save_attr = term->curr_attr;
708 term->save_cset = term->cset;
709 term->save_utf = term->utf;
710 term->save_wnext = term->wrapnext;
711 term->save_csattr = term->cset_attr[term->cset];
712 term->save_sco_acs = term->sco_acs;
713 } else {
714 term->curs = term->savecurs;
715 /* Make sure the window hasn't shrunk since the save */
716 if (term->curs.x >= term->cols)
717 term->curs.x = term->cols - 1;
718 if (term->curs.y >= term->rows)
719 term->curs.y = term->rows - 1;
720
721 term->curr_attr = term->save_attr;
722 term->cset = term->save_cset;
723 term->utf = term->save_utf;
724 term->wrapnext = term->save_wnext;
725 /*
726 * wrapnext might reset to False if the x position is no
727 * longer at the rightmost edge.
728 */
729 if (term->wrapnext && term->curs.x < term->cols-1)
730 term->wrapnext = FALSE;
731 term->cset_attr[term->cset] = term->save_csattr;
732 term->sco_acs = term->save_sco_acs;
733 fix_cpos;
734 if (term->use_bce)
735 term->erase_char = (' ' | ATTR_ASCII |
736 (term->curr_attr &
737 (ATTR_FGMASK | ATTR_BGMASK)));
738 }
739 }
740
741 /*
742 * Erase a large portion of the screen: the whole screen, or the
743 * whole line, or parts thereof.
744 */
745 static void erase_lots(Terminal *term,
746 int line_only, int from_begin, int to_end)
747 {
748 pos start, end;
749 int erase_lattr;
750 unsigned long *ldata;
751
752 if (line_only) {
753 start.y = term->curs.y;
754 start.x = 0;
755 end.y = term->curs.y + 1;
756 end.x = 0;
757 erase_lattr = FALSE;
758 } else {
759 start.y = 0;
760 start.x = 0;
761 end.y = term->rows;
762 end.x = 0;
763 erase_lattr = TRUE;
764 }
765 if (!from_begin) {
766 start = term->curs;
767 }
768 if (!to_end) {
769 end = term->curs;
770 incpos(end);
771 }
772 check_selection(term, start, end);
773
774 /* Clear screen also forces a full window redraw, just in case. */
775 if (start.y == 0 && start.x == 0 && end.y == term->rows)
776 term_invalidate(term);
777
778 ldata = lineptr(start.y);
779 while (poslt(start, end)) {
780 if (start.x == term->cols && !erase_lattr)
781 ldata[start.x] &= ~LATTR_WRAPPED;
782 else
783 ldata[start.x] = term->erase_char;
784 if (incpos(start) && start.y < term->rows)
785 ldata = lineptr(start.y);
786 }
787 }
788
789 /*
790 * Insert or delete characters within the current line. n is +ve if
791 * insertion is desired, and -ve for deletion.
792 */
793 static void insch(Terminal *term, int n)
794 {
795 int dir = (n < 0 ? -1 : +1);
796 int m;
797 pos cursplus;
798 unsigned long *ldata;
799
800 n = (n < 0 ? -n : n);
801 if (n > term->cols - term->curs.x)
802 n = term->cols - term->curs.x;
803 m = term->cols - term->curs.x - n;
804 cursplus.y = term->curs.y;
805 cursplus.x = term->curs.x + n;
806 check_selection(term, term->curs, cursplus);
807 ldata = lineptr(term->curs.y);
808 if (dir < 0) {
809 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
810 while (n--)
811 ldata[term->curs.x + m++] = term->erase_char;
812 } else {
813 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
814 while (n--)
815 ldata[term->curs.x + n] = term->erase_char;
816 }
817 }
818
819 /*
820 * Toggle terminal mode `mode' to state `state'. (`query' indicates
821 * whether the mode is a DEC private one or a normal one.)
822 */
823 static void toggle_mode(Terminal *term, int mode, int query, int state)
824 {
825 unsigned long ticks;
826
827 if (query)
828 switch (mode) {
829 case 1: /* application cursor keys */
830 term->app_cursor_keys = state;
831 break;
832 case 2: /* VT52 mode */
833 term->vt52_mode = !state;
834 if (term->vt52_mode) {
835 term->blink_is_real = FALSE;
836 term->vt52_bold = FALSE;
837 } else {
838 term->blink_is_real = cfg.blinktext;
839 }
840 break;
841 case 3: /* 80/132 columns */
842 deselect(term);
843 if (!cfg.no_remote_resize)
844 request_resize(state ? 132 : 80, term->rows);
845 term->reset_132 = state;
846 break;
847 case 5: /* reverse video */
848 /*
849 * Toggle reverse video. If we receive an OFF within the
850 * visual bell timeout period after an ON, we trigger an
851 * effective visual bell, so that ESC[?5hESC[?5l will
852 * always be an actually _visible_ visual bell.
853 */
854 ticks = GETTICKCOUNT();
855 /* turn off a previous vbell to avoid inconsistencies */
856 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
857 term->in_vbell = FALSE;
858 if (term->rvideo && !state && /* we're turning it off... */
859 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
860 /* If there's no vbell timeout already, or this one lasts
861 * longer, replace vbell_timeout with ours. */
862 if (!term->in_vbell ||
863 (term->rvbell_startpoint - term->vbell_startpoint <
864 VBELL_TIMEOUT))
865 term->vbell_startpoint = term->rvbell_startpoint;
866 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
867 } else if (!term->rvideo && state) {
868 /* This is an ON, so we notice the time and save it. */
869 term->rvbell_startpoint = ticks;
870 }
871 term->rvideo = state;
872 term->seen_disp_event = TRUE;
873 if (state)
874 term_update(term);
875 break;
876 case 6: /* DEC origin mode */
877 term->dec_om = state;
878 break;
879 case 7: /* auto wrap */
880 term->wrap = state;
881 break;
882 case 8: /* auto key repeat */
883 term->repeat_off = !state;
884 break;
885 case 10: /* set local edit mode */
886 term->term_editing = state;
887 ldisc_send(NULL, 0, 0); /* cause ldisc to notice changes */
888 break;
889 case 25: /* enable/disable cursor */
890 compatibility2(OTHER, VT220);
891 term->cursor_on = state;
892 term->seen_disp_event = TRUE;
893 break;
894 case 47: /* alternate screen */
895 compatibility(OTHER);
896 deselect(term);
897 swap_screen(term, cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
898 term->disptop = 0;
899 break;
900 case 1000: /* xterm mouse 1 */
901 term->xterm_mouse = state ? 1 : 0;
902 set_raw_mouse_mode(state);
903 break;
904 case 1002: /* xterm mouse 2 */
905 term->xterm_mouse = state ? 2 : 0;
906 set_raw_mouse_mode(state);
907 break;
908 case 1047: /* alternate screen */
909 compatibility(OTHER);
910 deselect(term);
911 swap_screen(term, cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
912 term->disptop = 0;
913 break;
914 case 1048: /* save/restore cursor */
915 save_cursor(term, state);
916 if (!state) term->seen_disp_event = TRUE;
917 break;
918 case 1049: /* cursor & alternate screen */
919 if (state)
920 save_cursor(term, state);
921 if (!state) term->seen_disp_event = TRUE;
922 compatibility(OTHER);
923 deselect(term);
924 swap_screen(term, cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
925 if (!state)
926 save_cursor(term, state);
927 term->disptop = 0;
928 break;
929 } else
930 switch (mode) {
931 case 4: /* set insert mode */
932 compatibility(VT102);
933 term->insert = state;
934 break;
935 case 12: /* set echo mode */
936 term->term_echoing = !state;
937 ldisc_send(NULL, 0, 0); /* cause ldisc to notice changes */
938 break;
939 case 20: /* Return sends ... */
940 term->cr_lf_return = state;
941 break;
942 case 34: /* Make cursor BIG */
943 compatibility2(OTHER, VT220);
944 term->big_cursor = !state;
945 }
946 }
947
948 /*
949 * Process an OSC sequence: set window title or icon name.
950 */
951 static void do_osc(Terminal *term)
952 {
953 if (term->osc_w) {
954 while (term->osc_strlen--)
955 term->wordness[(unsigned char)
956 term->osc_string[term->osc_strlen]] = term->esc_args[0];
957 } else {
958 term->osc_string[term->osc_strlen] = '\0';
959 switch (term->esc_args[0]) {
960 case 0:
961 case 1:
962 if (!cfg.no_remote_wintitle)
963 set_icon(term->osc_string);
964 if (term->esc_args[0] == 1)
965 break;
966 /* fall through: parameter 0 means set both */
967 case 2:
968 case 21:
969 if (!cfg.no_remote_wintitle)
970 set_title(term->osc_string);
971 break;
972 }
973 }
974 }
975
976 /*
977 * ANSI printing routines.
978 */
979 static void term_print_setup(Terminal *term)
980 {
981 bufchain_clear(&term->printer_buf);
982 term->print_job = printer_start_job(cfg.printer);
983 }
984 static void term_print_flush(Terminal *term)
985 {
986 void *data;
987 int len;
988 int size;
989 while ((size = bufchain_size(&term->printer_buf)) > 5) {
990 bufchain_prefix(&term->printer_buf, &data, &len);
991 if (len > size-5)
992 len = size-5;
993 printer_job_data(term->print_job, data, len);
994 bufchain_consume(&term->printer_buf, len);
995 }
996 }
997 static void term_print_finish(Terminal *term)
998 {
999 void *data;
1000 int len, size;
1001 char c;
1002
1003 if (!term->printing && !term->only_printing)
1004 return; /* we need do nothing */
1005
1006 term_print_flush(term);
1007 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1008 bufchain_prefix(&term->printer_buf, &data, &len);
1009 c = *(char *)data;
1010 if (c == '\033' || c == '\233') {
1011 bufchain_consume(&term->printer_buf, size);
1012 break;
1013 } else {
1014 printer_job_data(term->print_job, &c, 1);
1015 bufchain_consume(&term->printer_buf, 1);
1016 }
1017 }
1018 printer_finish_job(term->print_job);
1019 term->print_job = NULL;
1020 term->printing = term->only_printing = FALSE;
1021 }
1022
1023 /*
1024 * Remove everything currently in `inbuf' and stick it up on the
1025 * in-memory display. There's a big state machine in here to
1026 * process escape sequences...
1027 */
1028 void term_out(Terminal *term)
1029 {
1030 int c, unget;
1031 unsigned char localbuf[256], *chars;
1032 int nchars = 0;
1033
1034 unget = -1;
1035
1036 chars = NULL; /* placate compiler warnings */
1037 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1038 if (unget == -1) {
1039 if (nchars == 0) {
1040 void *ret;
1041 bufchain_prefix(&term->inbuf, &ret, &nchars);
1042 if (nchars > sizeof(localbuf))
1043 nchars = sizeof(localbuf);
1044 memcpy(localbuf, ret, nchars);
1045 bufchain_consume(&term->inbuf, nchars);
1046 chars = localbuf;
1047 assert(chars != NULL);
1048 }
1049 c = *chars++;
1050 nchars--;
1051
1052 /*
1053 * Optionally log the session traffic to a file. Useful for
1054 * debugging and possibly also useful for actual logging.
1055 */
1056 if (cfg.logtype == LGTYP_DEBUG)
1057 logtraffic((unsigned char) c, LGTYP_DEBUG);
1058 } else {
1059 c = unget;
1060 unget = -1;
1061 }
1062
1063 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1064 * be able to display 8-bit characters, but I'll let that go 'cause
1065 * of i18n.
1066 */
1067
1068 /*
1069 * If we're printing, add the character to the printer
1070 * buffer.
1071 */
1072 if (term->printing) {
1073 bufchain_add(&term->printer_buf, &c, 1);
1074
1075 /*
1076 * If we're in print-only mode, we use a much simpler
1077 * state machine designed only to recognise the ESC[4i
1078 * termination sequence.
1079 */
1080 if (term->only_printing) {
1081 if (c == '\033')
1082 term->print_state = 1;
1083 else if (c == (unsigned char)'\233')
1084 term->print_state = 2;
1085 else if (c == '[' && term->print_state == 1)
1086 term->print_state = 2;
1087 else if (c == '4' && term->print_state == 2)
1088 term->print_state = 3;
1089 else if (c == 'i' && term->print_state == 3)
1090 term->print_state = 4;
1091 else
1092 term->print_state = 0;
1093 if (term->print_state == 4) {
1094 term_print_finish(term);
1095 }
1096 continue;
1097 }
1098 }
1099
1100 /* First see about all those translations. */
1101 if (term->termstate == TOPLEVEL) {
1102 if (in_utf(term))
1103 switch (term->utf_state) {
1104 case 0:
1105 if (c < 0x80) {
1106 /* UTF-8 must be stateless so we ignore iso2022. */
1107 if (unitab_ctrl[c] != 0xFF)
1108 c = unitab_ctrl[c];
1109 else c = ((unsigned char)c) | ATTR_ASCII;
1110 break;
1111 } else if ((c & 0xe0) == 0xc0) {
1112 term->utf_size = term->utf_state = 1;
1113 term->utf_char = (c & 0x1f);
1114 } else if ((c & 0xf0) == 0xe0) {
1115 term->utf_size = term->utf_state = 2;
1116 term->utf_char = (c & 0x0f);
1117 } else if ((c & 0xf8) == 0xf0) {
1118 term->utf_size = term->utf_state = 3;
1119 term->utf_char = (c & 0x07);
1120 } else if ((c & 0xfc) == 0xf8) {
1121 term->utf_size = term->utf_state = 4;
1122 term->utf_char = (c & 0x03);
1123 } else if ((c & 0xfe) == 0xfc) {
1124 term->utf_size = term->utf_state = 5;
1125 term->utf_char = (c & 0x01);
1126 } else {
1127 c = UCSERR;
1128 break;
1129 }
1130 continue;
1131 case 1:
1132 case 2:
1133 case 3:
1134 case 4:
1135 case 5:
1136 if ((c & 0xC0) != 0x80) {
1137 unget = c;
1138 c = UCSERR;
1139 term->utf_state = 0;
1140 break;
1141 }
1142 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1143 if (--term->utf_state)
1144 continue;
1145
1146 c = term->utf_char;
1147
1148 /* Is somebody trying to be evil! */
1149 if (c < 0x80 ||
1150 (c < 0x800 && term->utf_size >= 2) ||
1151 (c < 0x10000 && term->utf_size >= 3) ||
1152 (c < 0x200000 && term->utf_size >= 4) ||
1153 (c < 0x4000000 && term->utf_size >= 5))
1154 c = UCSERR;
1155
1156 /* Unicode line separator and paragraph separator are CR-LF */
1157 if (c == 0x2028 || c == 0x2029)
1158 c = 0x85;
1159
1160 /* High controls are probably a Baaad idea too. */
1161 if (c < 0xA0)
1162 c = 0xFFFD;
1163
1164 /* The UTF-16 surrogates are not nice either. */
1165 /* The standard give the option of decoding these:
1166 * I don't want to! */
1167 if (c >= 0xD800 && c < 0xE000)
1168 c = UCSERR;
1169
1170 /* ISO 10646 characters now limited to UTF-16 range. */
1171 if (c > 0x10FFFF)
1172 c = UCSERR;
1173
1174 /* This is currently a TagPhobic application.. */
1175 if (c >= 0xE0000 && c <= 0xE007F)
1176 continue;
1177
1178 /* U+FEFF is best seen as a null. */
1179 if (c == 0xFEFF)
1180 continue;
1181 /* But U+FFFE is an error. */
1182 if (c == 0xFFFE || c == 0xFFFF)
1183 c = UCSERR;
1184
1185 /* Oops this is a 16bit implementation */
1186 if (c >= 0x10000)
1187 c = 0xFFFD;
1188 break;
1189 }
1190 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1191 else if(term->sco_acs &&
1192 (c!='\033' && c!='\n' && c!='\r' && c!='\b'))
1193 {
1194 if (term->sco_acs == 2) c ^= 0x80;
1195 c |= ATTR_SCOACS;
1196 } else {
1197 switch (term->cset_attr[term->cset]) {
1198 /*
1199 * Linedraw characters are different from 'ESC ( B'
1200 * only for a small range. For ones outside that
1201 * range, make sure we use the same font as well as
1202 * the same encoding.
1203 */
1204 case ATTR_LINEDRW:
1205 if (unitab_ctrl[c] != 0xFF)
1206 c = unitab_ctrl[c];
1207 else
1208 c = ((unsigned char) c) | ATTR_LINEDRW;
1209 break;
1210
1211 case ATTR_GBCHR:
1212 /* If UK-ASCII, make the '#' a LineDraw Pound */
1213 if (c == '#') {
1214 c = '}' | ATTR_LINEDRW;
1215 break;
1216 }
1217 /*FALLTHROUGH*/ case ATTR_ASCII:
1218 if (unitab_ctrl[c] != 0xFF)
1219 c = unitab_ctrl[c];
1220 else
1221 c = ((unsigned char) c) | ATTR_ASCII;
1222 break;
1223 case ATTR_SCOACS:
1224 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1225 break;
1226 }
1227 }
1228 }
1229
1230 /* How about C1 controls ? */
1231 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1232 !term->vt52_mode && has_compat(VT220)) {
1233 term->termstate = SEEN_ESC;
1234 term->esc_query = FALSE;
1235 c = '@' + (c & 0x1F);
1236 }
1237
1238 /* Or the GL control. */
1239 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1240 if (term->curs.x && !term->wrapnext)
1241 term->curs.x--;
1242 term->wrapnext = FALSE;
1243 fix_cpos;
1244 if (!cfg.no_dbackspace) /* destructive bksp might be disabled */
1245 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1246 } else
1247 /* Or normal C0 controls. */
1248 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1249 switch (c) {
1250 case '\005': /* terminal type query */
1251 /* Strictly speaking this is VT100 but a VT100 defaults to
1252 * no response. Other terminals respond at their option.
1253 *
1254 * Don't put a CR in the default string as this tends to
1255 * upset some weird software.
1256 *
1257 * An xterm returns "xterm" (5 characters)
1258 */
1259 compatibility(ANSIMIN);
1260 {
1261 char abuf[256], *s, *d;
1262 int state = 0;
1263 for (s = cfg.answerback, d = abuf; *s; s++) {
1264 if (state) {
1265 if (*s >= 'a' && *s <= 'z')
1266 *d++ = (*s - ('a' - 1));
1267 else if ((*s >= '@' && *s <= '_') ||
1268 *s == '?' || (*s & 0x80))
1269 *d++ = ('@' ^ *s);
1270 else if (*s == '~')
1271 *d++ = '^';
1272 state = 0;
1273 } else if (*s == '^') {
1274 state = 1;
1275 } else
1276 *d++ = *s;
1277 }
1278 lpage_send(DEFAULT_CODEPAGE, abuf, d - abuf, 0);
1279 }
1280 break;
1281 case '\007':
1282 {
1283 struct beeptime *newbeep;
1284 unsigned long ticks;
1285
1286 ticks = GETTICKCOUNT();
1287
1288 if (!term->beep_overloaded) {
1289 newbeep = smalloc(sizeof(struct beeptime));
1290 newbeep->ticks = ticks;
1291 newbeep->next = NULL;
1292 if (!term->beephead)
1293 term->beephead = newbeep;
1294 else
1295 term->beeptail->next = newbeep;
1296 term->beeptail = newbeep;
1297 term->nbeeps++;
1298 }
1299
1300 /*
1301 * Throw out any beeps that happened more than
1302 * t seconds ago.
1303 */
1304 while (term->beephead &&
1305 term->beephead->ticks < ticks - cfg.bellovl_t) {
1306 struct beeptime *tmp = term->beephead;
1307 term->beephead = tmp->next;
1308 sfree(tmp);
1309 if (!term->beephead)
1310 term->beeptail = NULL;
1311 term->nbeeps--;
1312 }
1313
1314 if (cfg.bellovl && term->beep_overloaded &&
1315 ticks - term->lastbeep >= (unsigned)cfg.bellovl_s) {
1316 /*
1317 * If we're currently overloaded and the
1318 * last beep was more than s seconds ago,
1319 * leave overload mode.
1320 */
1321 term->beep_overloaded = FALSE;
1322 } else if (cfg.bellovl && !term->beep_overloaded &&
1323 term->nbeeps >= cfg.bellovl_n) {
1324 /*
1325 * Now, if we have n or more beeps
1326 * remaining in the queue, go into overload
1327 * mode.
1328 */
1329 term->beep_overloaded = TRUE;
1330 }
1331 term->lastbeep = ticks;
1332
1333 /*
1334 * Perform an actual beep if we're not overloaded.
1335 */
1336 if (!cfg.bellovl || !term->beep_overloaded) {
1337 if (cfg.beep == BELL_VISUAL) {
1338 term->in_vbell = TRUE;
1339 term->vbell_startpoint = ticks;
1340 term_update(term);
1341 } else
1342 beep(cfg.beep);
1343 }
1344 term->disptop = 0;
1345 }
1346 break;
1347 case '\b':
1348 if (term->curs.x == 0 &&
1349 (term->curs.y == 0 || term->wrap == 0))
1350 /* do nothing */ ;
1351 else if (term->curs.x == 0 && term->curs.y > 0)
1352 term->curs.x = term->cols - 1, term->curs.y--;
1353 else if (term->wrapnext)
1354 term->wrapnext = FALSE;
1355 else
1356 term->curs.x--;
1357 fix_cpos;
1358 term->seen_disp_event = TRUE;
1359 break;
1360 case '\016':
1361 compatibility(VT100);
1362 term->cset = 1;
1363 break;
1364 case '\017':
1365 compatibility(VT100);
1366 term->cset = 0;
1367 break;
1368 case '\033':
1369 if (term->vt52_mode)
1370 term->termstate = VT52_ESC;
1371 else {
1372 compatibility(ANSIMIN);
1373 term->termstate = SEEN_ESC;
1374 term->esc_query = FALSE;
1375 }
1376 break;
1377 case '\r':
1378 term->curs.x = 0;
1379 term->wrapnext = FALSE;
1380 fix_cpos;
1381 term->seen_disp_event = TRUE;
1382 term->paste_hold = 0;
1383 logtraffic((unsigned char) c, LGTYP_ASCII);
1384 break;
1385 case '\014':
1386 if (has_compat(SCOANSI)) {
1387 move(term, 0, 0, 0);
1388 erase_lots(term, FALSE, FALSE, TRUE);
1389 term->disptop = 0;
1390 term->wrapnext = FALSE;
1391 term->seen_disp_event = 1;
1392 break;
1393 }
1394 case '\013':
1395 compatibility(VT100);
1396 case '\n':
1397 if (term->curs.y == term->marg_b)
1398 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1399 else if (term->curs.y < term->rows - 1)
1400 term->curs.y++;
1401 if (cfg.lfhascr)
1402 term->curs.x = 0;
1403 fix_cpos;
1404 term->wrapnext = FALSE;
1405 term->seen_disp_event = 1;
1406 term->paste_hold = 0;
1407 logtraffic((unsigned char) c, LGTYP_ASCII);
1408 break;
1409 case '\t':
1410 {
1411 pos old_curs = term->curs;
1412 unsigned long *ldata = lineptr(term->curs.y);
1413
1414 do {
1415 term->curs.x++;
1416 } while (term->curs.x < term->cols - 1 &&
1417 !term->tabs[term->curs.x]);
1418
1419 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1420 if (term->curs.x >= term->cols / 2)
1421 term->curs.x = term->cols / 2 - 1;
1422 } else {
1423 if (term->curs.x >= term->cols)
1424 term->curs.x = term->cols - 1;
1425 }
1426
1427 fix_cpos;
1428 check_selection(term, old_curs, term->curs);
1429 }
1430 term->seen_disp_event = TRUE;
1431 break;
1432 }
1433 } else
1434 switch (term->termstate) {
1435 case TOPLEVEL:
1436 /* Only graphic characters get this far;
1437 * ctrls are stripped above */
1438 if (term->wrapnext && term->wrap) {
1439 term->cpos[1] |= LATTR_WRAPPED;
1440 if (term->curs.y == term->marg_b)
1441 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1442 else if (term->curs.y < term->rows - 1)
1443 term->curs.y++;
1444 term->curs.x = 0;
1445 fix_cpos;
1446 term->wrapnext = FALSE;
1447 }
1448 if (term->insert)
1449 insch(term, 1);
1450 if (term->selstate != NO_SELECTION) {
1451 pos cursplus = term->curs;
1452 incpos(cursplus);
1453 check_selection(term, term->curs, cursplus);
1454 }
1455 if ((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0)
1456 logtraffic((unsigned char) c, LGTYP_ASCII);
1457 {
1458 extern int wcwidth(wchar_t ucs);
1459 int width = 0;
1460 if (DIRECT_CHAR(c))
1461 width = 1;
1462 if (!width)
1463 width = wcwidth((wchar_t) c);
1464 switch (width) {
1465 case 2:
1466 *term->cpos++ = c | term->curr_attr;
1467 if (++term->curs.x == term->cols) {
1468 *term->cpos |= LATTR_WRAPPED;
1469 if (term->curs.y == term->marg_b)
1470 scroll(term, term->marg_t, term->marg_b,
1471 1, TRUE);
1472 else if (term->curs.y < term->rows - 1)
1473 term->curs.y++;
1474 term->curs.x = 0;
1475 fix_cpos;
1476 }
1477 *term->cpos++ = UCSWIDE | term->curr_attr;
1478 break;
1479 case 1:
1480 *term->cpos++ = c | term->curr_attr;
1481 break;
1482 default:
1483 continue;
1484 }
1485 }
1486 term->curs.x++;
1487 if (term->curs.x == term->cols) {
1488 term->cpos--;
1489 term->curs.x--;
1490 term->wrapnext = TRUE;
1491 if (term->wrap && term->vt52_mode) {
1492 term->cpos[1] |= LATTR_WRAPPED;
1493 if (term->curs.y == term->marg_b)
1494 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1495 else if (term->curs.y < term->rows - 1)
1496 term->curs.y++;
1497 term->curs.x = 0;
1498 fix_cpos;
1499 term->wrapnext = FALSE;
1500 }
1501 }
1502 term->seen_disp_event = 1;
1503 break;
1504
1505 case OSC_MAYBE_ST:
1506 /*
1507 * This state is virtually identical to SEEN_ESC, with the
1508 * exception that we have an OSC sequence in the pipeline,
1509 * and _if_ we see a backslash, we process it.
1510 */
1511 if (c == '\\') {
1512 do_osc(term);
1513 term->termstate = TOPLEVEL;
1514 break;
1515 }
1516 /* else fall through */
1517 case SEEN_ESC:
1518 if (c >= ' ' && c <= '/') {
1519 if (term->esc_query)
1520 term->esc_query = -1;
1521 else
1522 term->esc_query = c;
1523 break;
1524 }
1525 term->termstate = TOPLEVEL;
1526 switch (ANSI(c, term->esc_query)) {
1527 case '[': /* enter CSI mode */
1528 term->termstate = SEEN_CSI;
1529 term->esc_nargs = 1;
1530 term->esc_args[0] = ARG_DEFAULT;
1531 term->esc_query = FALSE;
1532 break;
1533 case ']': /* xterm escape sequences */
1534 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1535 compatibility(OTHER);
1536 term->termstate = SEEN_OSC;
1537 term->esc_args[0] = 0;
1538 break;
1539 case '7': /* save cursor */
1540 compatibility(VT100);
1541 save_cursor(term, TRUE);
1542 break;
1543 case '8': /* restore cursor */
1544 compatibility(VT100);
1545 save_cursor(term, FALSE);
1546 term->seen_disp_event = TRUE;
1547 break;
1548 case '=':
1549 compatibility(VT100);
1550 term->app_keypad_keys = TRUE;
1551 break;
1552 case '>':
1553 compatibility(VT100);
1554 term->app_keypad_keys = FALSE;
1555 break;
1556 case 'D': /* exactly equivalent to LF */
1557 compatibility(VT100);
1558 if (term->curs.y == term->marg_b)
1559 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1560 else if (term->curs.y < term->rows - 1)
1561 term->curs.y++;
1562 fix_cpos;
1563 term->wrapnext = FALSE;
1564 term->seen_disp_event = TRUE;
1565 break;
1566 case 'E': /* exactly equivalent to CR-LF */
1567 compatibility(VT100);
1568 term->curs.x = 0;
1569 if (term->curs.y == term->marg_b)
1570 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1571 else if (term->curs.y < term->rows - 1)
1572 term->curs.y++;
1573 fix_cpos;
1574 term->wrapnext = FALSE;
1575 term->seen_disp_event = TRUE;
1576 break;
1577 case 'M': /* reverse index - backwards LF */
1578 compatibility(VT100);
1579 if (term->curs.y == term->marg_t)
1580 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1581 else if (term->curs.y > 0)
1582 term->curs.y--;
1583 fix_cpos;
1584 term->wrapnext = FALSE;
1585 term->seen_disp_event = TRUE;
1586 break;
1587 case 'Z': /* terminal type query */
1588 compatibility(VT100);
1589 ldisc_send(term->id_string, strlen(term->id_string), 0);
1590 break;
1591 case 'c': /* restore power-on settings */
1592 compatibility(VT100);
1593 power_on(term);
1594 ldisc_send(NULL, 0, 0);/* cause ldisc to notice changes */
1595 if (term->reset_132) {
1596 if (!cfg.no_remote_resize)
1597 request_resize(80, term->rows);
1598 term->reset_132 = 0;
1599 }
1600 fix_cpos;
1601 term->disptop = 0;
1602 term->seen_disp_event = TRUE;
1603 break;
1604 case 'H': /* set a tab */
1605 compatibility(VT100);
1606 term->tabs[term->curs.x] = TRUE;
1607 break;
1608
1609 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1610 compatibility(VT100);
1611 {
1612 unsigned long *ldata;
1613 int i, j;
1614 pos scrtop, scrbot;
1615
1616 for (i = 0; i < term->rows; i++) {
1617 ldata = lineptr(i);
1618 for (j = 0; j < term->cols; j++)
1619 ldata[j] = ATTR_DEFAULT | 'E';
1620 ldata[term->cols] = 0;
1621 }
1622 term->disptop = 0;
1623 term->seen_disp_event = TRUE;
1624 scrtop.x = scrtop.y = 0;
1625 scrbot.x = 0;
1626 scrbot.y = term->rows;
1627 check_selection(term, scrtop, scrbot);
1628 }
1629 break;
1630
1631 case ANSI('3', '#'):
1632 case ANSI('4', '#'):
1633 case ANSI('5', '#'):
1634 case ANSI('6', '#'):
1635 compatibility(VT100);
1636 {
1637 unsigned long nlattr;
1638 unsigned long *ldata;
1639 switch (ANSI(c, term->esc_query)) {
1640 case ANSI('3', '#'):
1641 nlattr = LATTR_TOP;
1642 break;
1643 case ANSI('4', '#'):
1644 nlattr = LATTR_BOT;
1645 break;
1646 case ANSI('5', '#'):
1647 nlattr = LATTR_NORM;
1648 break;
1649 default: /* spiritually case ANSI('6', '#'): */
1650 nlattr = LATTR_WIDE;
1651 break;
1652 }
1653 ldata = lineptr(term->curs.y);
1654 ldata[term->cols] &= ~LATTR_MODE;
1655 ldata[term->cols] |= nlattr;
1656 }
1657 break;
1658
1659 case ANSI('A', '('):
1660 compatibility(VT100);
1661 if (!cfg.no_remote_charset)
1662 term->cset_attr[0] = ATTR_GBCHR;
1663 break;
1664 case ANSI('B', '('):
1665 compatibility(VT100);
1666 if (!cfg.no_remote_charset)
1667 term->cset_attr[0] = ATTR_ASCII;
1668 break;
1669 case ANSI('0', '('):
1670 compatibility(VT100);
1671 if (!cfg.no_remote_charset)
1672 term->cset_attr[0] = ATTR_LINEDRW;
1673 break;
1674 case ANSI('U', '('):
1675 compatibility(OTHER);
1676 if (!cfg.no_remote_charset)
1677 term->cset_attr[0] = ATTR_SCOACS;
1678 break;
1679
1680 case ANSI('A', ')'):
1681 compatibility(VT100);
1682 if (!cfg.no_remote_charset)
1683 term->cset_attr[1] = ATTR_GBCHR;
1684 break;
1685 case ANSI('B', ')'):
1686 compatibility(VT100);
1687 if (!cfg.no_remote_charset)
1688 term->cset_attr[1] = ATTR_ASCII;
1689 break;
1690 case ANSI('0', ')'):
1691 compatibility(VT100);
1692 if (!cfg.no_remote_charset)
1693 term->cset_attr[1] = ATTR_LINEDRW;
1694 break;
1695 case ANSI('U', ')'):
1696 compatibility(OTHER);
1697 if (!cfg.no_remote_charset)
1698 term->cset_attr[1] = ATTR_SCOACS;
1699 break;
1700
1701 case ANSI('8', '%'): /* Old Linux code */
1702 case ANSI('G', '%'):
1703 compatibility(OTHER);
1704 if (!cfg.no_remote_charset)
1705 term->utf = 1;
1706 break;
1707 case ANSI('@', '%'):
1708 compatibility(OTHER);
1709 if (!cfg.no_remote_charset)
1710 term->utf = 0;
1711 break;
1712 }
1713 break;
1714 case SEEN_CSI:
1715 term->termstate = TOPLEVEL; /* default */
1716 if (isdigit(c)) {
1717 if (term->esc_nargs <= ARGS_MAX) {
1718 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
1719 term->esc_args[term->esc_nargs - 1] = 0;
1720 term->esc_args[term->esc_nargs - 1] =
1721 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
1722 }
1723 term->termstate = SEEN_CSI;
1724 } else if (c == ';') {
1725 if (++term->esc_nargs <= ARGS_MAX)
1726 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1727 term->termstate = SEEN_CSI;
1728 } else if (c < '@') {
1729 if (term->esc_query)
1730 term->esc_query = -1;
1731 else if (c == '?')
1732 term->esc_query = TRUE;
1733 else
1734 term->esc_query = c;
1735 term->termstate = SEEN_CSI;
1736 } else
1737 switch (ANSI(c, term->esc_query)) {
1738 case 'A': /* move up N lines */
1739 move(term, term->curs.x,
1740 term->curs.y - def(term->esc_args[0], 1), 1);
1741 term->seen_disp_event = TRUE;
1742 break;
1743 case 'e': /* move down N lines */
1744 compatibility(ANSI);
1745 /* FALLTHROUGH */
1746 case 'B':
1747 move(term, term->curs.x,
1748 term->curs.y + def(term->esc_args[0], 1), 1);
1749 term->seen_disp_event = TRUE;
1750 break;
1751 case ANSI('c', '>'): /* report xterm version */
1752 compatibility(OTHER);
1753 /* this reports xterm version 136 so that VIM can
1754 use the drag messages from the mouse reporting */
1755 ldisc_send("\033[>0;136;0c", 11, 0);
1756 break;
1757 case 'a': /* move right N cols */
1758 compatibility(ANSI);
1759 /* FALLTHROUGH */
1760 case 'C':
1761 move(term, term->curs.x + def(term->esc_args[0], 1),
1762 term->curs.y, 1);
1763 term->seen_disp_event = TRUE;
1764 break;
1765 case 'D': /* move left N cols */
1766 move(term, term->curs.x - def(term->esc_args[0], 1),
1767 term->curs.y, 1);
1768 term->seen_disp_event = TRUE;
1769 break;
1770 case 'E': /* move down N lines and CR */
1771 compatibility(ANSI);
1772 move(term, 0,
1773 term->curs.y + def(term->esc_args[0], 1), 1);
1774 term->seen_disp_event = TRUE;
1775 break;
1776 case 'F': /* move up N lines and CR */
1777 compatibility(ANSI);
1778 move(term, 0,
1779 term->curs.y - def(term->esc_args[0], 1), 1);
1780 term->seen_disp_event = TRUE;
1781 break;
1782 case 'G':
1783 case '`': /* set horizontal posn */
1784 compatibility(ANSI);
1785 move(term, def(term->esc_args[0], 1) - 1,
1786 term->curs.y, 0);
1787 term->seen_disp_event = TRUE;
1788 break;
1789 case 'd': /* set vertical posn */
1790 compatibility(ANSI);
1791 move(term, term->curs.x,
1792 ((term->dec_om ? term->marg_t : 0) +
1793 def(term->esc_args[0], 1) - 1),
1794 (term->dec_om ? 2 : 0));
1795 term->seen_disp_event = TRUE;
1796 break;
1797 case 'H':
1798 case 'f': /* set horz and vert posns at once */
1799 if (term->esc_nargs < 2)
1800 term->esc_args[1] = ARG_DEFAULT;
1801 move(term, def(term->esc_args[1], 1) - 1,
1802 ((term->dec_om ? term->marg_t : 0) +
1803 def(term->esc_args[0], 1) - 1),
1804 (term->dec_om ? 2 : 0));
1805 term->seen_disp_event = TRUE;
1806 break;
1807 case 'J': /* erase screen or parts of it */
1808 {
1809 unsigned int i = def(term->esc_args[0], 0) + 1;
1810 if (i > 3)
1811 i = 0;
1812 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
1813 }
1814 term->disptop = 0;
1815 term->seen_disp_event = TRUE;
1816 break;
1817 case 'K': /* erase line or parts of it */
1818 {
1819 unsigned int i = def(term->esc_args[0], 0) + 1;
1820 if (i > 3)
1821 i = 0;
1822 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
1823 }
1824 term->seen_disp_event = TRUE;
1825 break;
1826 case 'L': /* insert lines */
1827 compatibility(VT102);
1828 if (term->curs.y <= term->marg_b)
1829 scroll(term, term->curs.y, term->marg_b,
1830 -def(term->esc_args[0], 1), FALSE);
1831 fix_cpos;
1832 term->seen_disp_event = TRUE;
1833 break;
1834 case 'M': /* delete lines */
1835 compatibility(VT102);
1836 if (term->curs.y <= term->marg_b)
1837 scroll(term, term->curs.y, term->marg_b,
1838 def(term->esc_args[0], 1),
1839 TRUE);
1840 fix_cpos;
1841 term->seen_disp_event = TRUE;
1842 break;
1843 case '@': /* insert chars */
1844 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1845 compatibility(VT102);
1846 insch(term, def(term->esc_args[0], 1));
1847 term->seen_disp_event = TRUE;
1848 break;
1849 case 'P': /* delete chars */
1850 compatibility(VT102);
1851 insch(term, -def(term->esc_args[0], 1));
1852 term->seen_disp_event = TRUE;
1853 break;
1854 case 'c': /* terminal type query */
1855 compatibility(VT100);
1856 /* This is the response for a VT102 */
1857 ldisc_send(term->id_string,
1858 strlen(term->id_string), 0);
1859 break;
1860 case 'n': /* cursor position query */
1861 if (term->esc_args[0] == 6) {
1862 char buf[32];
1863 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
1864 term->curs.x + 1);
1865 ldisc_send(buf, strlen(buf), 0);
1866 } else if (term->esc_args[0] == 5) {
1867 ldisc_send("\033[0n", 4, 0);
1868 }
1869 break;
1870 case 'h': /* toggle modes to high */
1871 case ANSI_QUE('h'):
1872 compatibility(VT100);
1873 {
1874 int i;
1875 for (i = 0; i < term->esc_nargs; i++)
1876 toggle_mode(term, term->esc_args[i],
1877 term->esc_query, TRUE);
1878 }
1879 break;
1880 case 'i':
1881 case ANSI_QUE('i'):
1882 compatibility(VT100);
1883 {
1884 if (term->esc_nargs != 1) break;
1885 if (term->esc_args[0] == 5 && *cfg.printer) {
1886 term->printing = TRUE;
1887 term->only_printing = !term->esc_query;
1888 term->print_state = 0;
1889 term_print_setup(term);
1890 } else if (term->esc_args[0] == 4 &&
1891 term->printing) {
1892 term_print_finish(term);
1893 }
1894 }
1895 break;
1896 case 'l': /* toggle modes to low */
1897 case ANSI_QUE('l'):
1898 compatibility(VT100);
1899 {
1900 int i;
1901 for (i = 0; i < term->esc_nargs; i++)
1902 toggle_mode(term, term->esc_args[i],
1903 term->esc_query, FALSE);
1904 }
1905 break;
1906 case 'g': /* clear tabs */
1907 compatibility(VT100);
1908 if (term->esc_nargs == 1) {
1909 if (term->esc_args[0] == 0) {
1910 term->tabs[term->curs.x] = FALSE;
1911 } else if (term->esc_args[0] == 3) {
1912 int i;
1913 for (i = 0; i < term->cols; i++)
1914 term->tabs[i] = FALSE;
1915 }
1916 }
1917 break;
1918 case 'r': /* set scroll margins */
1919 compatibility(VT100);
1920 if (term->esc_nargs <= 2) {
1921 int top, bot;
1922 top = def(term->esc_args[0], 1) - 1;
1923 bot = (term->esc_nargs <= 1
1924 || term->esc_args[1] == 0 ?
1925 term->rows :
1926 def(term->esc_args[1], term->rows)) - 1;
1927 if (bot >= term->rows)
1928 bot = term->rows - 1;
1929 /* VTTEST Bug 9 - if region is less than 2 lines
1930 * don't change region.
1931 */
1932 if (bot - top > 0) {
1933 term->marg_t = top;
1934 term->marg_b = bot;
1935 term->curs.x = 0;
1936 /*
1937 * I used to think the cursor should be
1938 * placed at the top of the newly marginned
1939 * area. Apparently not: VMS TPU falls over
1940 * if so.
1941 *
1942 * Well actually it should for
1943 * Origin mode - RDB
1944 */
1945 term->curs.y = (term->dec_om ?
1946 term->marg_t : 0);
1947 fix_cpos;
1948 term->seen_disp_event = TRUE;
1949 }
1950 }
1951 break;
1952 case 'm': /* set graphics rendition */
1953 {
1954 /*
1955 * A VT100 without the AVO only had one
1956 * attribute, either underline or
1957 * reverse video depending on the
1958 * cursor type, this was selected by
1959 * CSI 7m.
1960 *
1961 * case 2:
1962 * This is sometimes DIM, eg on the
1963 * GIGI and Linux
1964 * case 8:
1965 * This is sometimes INVIS various ANSI.
1966 * case 21:
1967 * This like 22 disables BOLD, DIM and INVIS
1968 *
1969 * The ANSI colours appear on any
1970 * terminal that has colour (obviously)
1971 * but the interaction between sgr0 and
1972 * the colours varies but is usually
1973 * related to the background colour
1974 * erase item. The interaction between
1975 * colour attributes and the mono ones
1976 * is also very implementation
1977 * dependent.
1978 *
1979 * The 39 and 49 attributes are likely
1980 * to be unimplemented.
1981 */
1982 int i;
1983 for (i = 0; i < term->esc_nargs; i++) {
1984 switch (def(term->esc_args[i], 0)) {
1985 case 0: /* restore defaults */
1986 term->curr_attr = ATTR_DEFAULT;
1987 break;
1988 case 1: /* enable bold */
1989 compatibility(VT100AVO);
1990 term->curr_attr |= ATTR_BOLD;
1991 break;
1992 case 21: /* (enable double underline) */
1993 compatibility(OTHER);
1994 case 4: /* enable underline */
1995 compatibility(VT100AVO);
1996 term->curr_attr |= ATTR_UNDER;
1997 break;
1998 case 5: /* enable blink */
1999 compatibility(VT100AVO);
2000 term->curr_attr |= ATTR_BLINK;
2001 break;
2002 case 7: /* enable reverse video */
2003 term->curr_attr |= ATTR_REVERSE;
2004 break;
2005 case 10: /* SCO acs off */
2006 compatibility(SCOANSI);
2007 if (cfg.no_remote_charset) break;
2008 term->sco_acs = 0; break;
2009 case 11: /* SCO acs on */
2010 compatibility(SCOANSI);
2011 if (cfg.no_remote_charset) break;
2012 term->sco_acs = 1; break;
2013 case 12: /* SCO acs on flipped */
2014 compatibility(SCOANSI);
2015 if (cfg.no_remote_charset) break;
2016 term->sco_acs = 2; break;
2017 case 22: /* disable bold */
2018 compatibility2(OTHER, VT220);
2019 term->curr_attr &= ~ATTR_BOLD;
2020 break;
2021 case 24: /* disable underline */
2022 compatibility2(OTHER, VT220);
2023 term->curr_attr &= ~ATTR_UNDER;
2024 break;
2025 case 25: /* disable blink */
2026 compatibility2(OTHER, VT220);
2027 term->curr_attr &= ~ATTR_BLINK;
2028 break;
2029 case 27: /* disable reverse video */
2030 compatibility2(OTHER, VT220);
2031 term->curr_attr &= ~ATTR_REVERSE;
2032 break;
2033 case 30:
2034 case 31:
2035 case 32:
2036 case 33:
2037 case 34:
2038 case 35:
2039 case 36:
2040 case 37:
2041 /* foreground */
2042 term->curr_attr &= ~ATTR_FGMASK;
2043 term->curr_attr |=
2044 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2045 break;
2046 case 39: /* default-foreground */
2047 term->curr_attr &= ~ATTR_FGMASK;
2048 term->curr_attr |= ATTR_DEFFG;
2049 break;
2050 case 40:
2051 case 41:
2052 case 42:
2053 case 43:
2054 case 44:
2055 case 45:
2056 case 46:
2057 case 47:
2058 /* background */
2059 term->curr_attr &= ~ATTR_BGMASK;
2060 term->curr_attr |=
2061 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2062 break;
2063 case 49: /* default-background */
2064 term->curr_attr &= ~ATTR_BGMASK;
2065 term->curr_attr |= ATTR_DEFBG;
2066 break;
2067 }
2068 }
2069 if (term->use_bce)
2070 term->erase_char = (' ' | ATTR_ASCII |
2071 (term->curr_attr &
2072 (ATTR_FGMASK |
2073 ATTR_BGMASK)));
2074 }
2075 break;
2076 case 's': /* save cursor */
2077 save_cursor(term, TRUE);
2078 break;
2079 case 'u': /* restore cursor */
2080 save_cursor(term, FALSE);
2081 term->seen_disp_event = TRUE;
2082 break;
2083 case 't': /* set page size - ie window height */
2084 /*
2085 * VT340/VT420 sequence DECSLPP, DEC only allows values
2086 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2087 * illegal values (eg first arg 1..9) for window changing
2088 * and reports.
2089 */
2090 if (term->esc_nargs <= 1
2091 && (term->esc_args[0] < 1 ||
2092 term->esc_args[0] >= 24)) {
2093 compatibility(VT340TEXT);
2094 if (!cfg.no_remote_resize)
2095 request_resize(term->cols,
2096 def(term->esc_args[0], 24));
2097 deselect(term);
2098 } else if (term->esc_nargs >= 1 &&
2099 term->esc_args[0] >= 1 &&
2100 term->esc_args[0] < 24) {
2101 compatibility(OTHER);
2102
2103 switch (term->esc_args[0]) {
2104 int x, y, len;
2105 char buf[80], *p;
2106 case 1:
2107 set_iconic(FALSE);
2108 break;
2109 case 2:
2110 set_iconic(TRUE);
2111 break;
2112 case 3:
2113 if (term->esc_nargs >= 3) {
2114 if (!cfg.no_remote_resize)
2115 move_window(def(term->esc_args[1], 0),
2116 def(term->esc_args[2], 0));
2117 }
2118 break;
2119 case 4:
2120 /* We should resize the window to a given
2121 * size in pixels here, but currently our
2122 * resizing code isn't healthy enough to
2123 * manage it. */
2124 break;
2125 case 5:
2126 set_zorder(TRUE); /* move to top */
2127 break;
2128 case 6:
2129 set_zorder(FALSE); /* move to bottom */
2130 break;
2131 case 7:
2132 refresh_window();
2133 break;
2134 case 8:
2135 if (term->esc_nargs >= 3) {
2136 if (!cfg.no_remote_resize)
2137 request_resize(def(term->esc_args[2], cfg.width),
2138 def(term->esc_args[1], cfg.height));
2139 }
2140 break;
2141 case 9:
2142 if (term->esc_nargs >= 2)
2143 set_zoomed(term->esc_args[1] ?
2144 TRUE : FALSE);
2145 break;
2146 case 11:
2147 ldisc_send(is_iconic() ? "\033[1t" : "\033[2t",
2148 4, 0);
2149 break;
2150 case 13:
2151 get_window_pos(&x, &y);
2152 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2153 ldisc_send(buf, len, 0);
2154 break;
2155 case 14:
2156 get_window_pixels(&x, &y);
2157 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2158 ldisc_send(buf, len, 0);
2159 break;
2160 case 18:
2161 len = sprintf(buf, "\033[8;%d;%dt",
2162 term->rows, term->cols);
2163 ldisc_send(buf, len, 0);
2164 break;
2165 case 19:
2166 /*
2167 * Hmmm. Strictly speaking we
2168 * should return `the size of the
2169 * screen in characters', but
2170 * that's not easy: (a) window
2171 * furniture being what it is it's
2172 * hard to compute, and (b) in
2173 * resize-font mode maximising the
2174 * window wouldn't change the
2175 * number of characters. *shrug*. I
2176 * think we'll ignore it for the
2177 * moment and see if anyone
2178 * complains, and then ask them
2179 * what they would like it to do.
2180 */
2181 break;
2182 case 20:
2183 p = get_window_title(TRUE);
2184 len = strlen(p);
2185 ldisc_send("\033]L", 3, 0);
2186 ldisc_send(p, len, 0);
2187 ldisc_send("\033\\", 2, 0);
2188 break;
2189 case 21:
2190 p = get_window_title(FALSE);
2191 len = strlen(p);
2192 ldisc_send("\033]l", 3, 0);
2193 ldisc_send(p, len, 0);
2194 ldisc_send("\033\\", 2, 0);
2195 break;
2196 }
2197 }
2198 break;
2199 case 'S':
2200 compatibility(SCOANSI);
2201 scroll(term, term->marg_t, term->marg_b,
2202 def(term->esc_args[0], 1), TRUE);
2203 fix_cpos;
2204 term->wrapnext = FALSE;
2205 term->seen_disp_event = TRUE;
2206 break;
2207 case 'T':
2208 compatibility(SCOANSI);
2209 scroll(term, term->marg_t, term->marg_b,
2210 -def(term->esc_args[0], 1), TRUE);
2211 fix_cpos;
2212 term->wrapnext = FALSE;
2213 term->seen_disp_event = TRUE;
2214 break;
2215 case ANSI('|', '*'):
2216 /* VT420 sequence DECSNLS
2217 * Set number of lines on screen
2218 * VT420 uses VGA like hardware and can support any size in
2219 * reasonable range (24..49 AIUI) with no default specified.
2220 */
2221 compatibility(VT420);
2222 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2223 if (!cfg.no_remote_resize)
2224 request_resize(term->cols,
2225 def(term->esc_args[0],
2226 cfg.height));
2227 deselect(term);
2228 }
2229 break;
2230 case ANSI('|', '$'):
2231 /* VT340/VT420 sequence DECSCPP
2232 * Set number of columns per page
2233 * Docs imply range is only 80 or 132, but I'll allow any.
2234 */
2235 compatibility(VT340TEXT);
2236 if (term->esc_nargs <= 1) {
2237 if (!cfg.no_remote_resize)
2238 request_resize(def(term->esc_args[0],
2239 cfg.width), term->rows);
2240 deselect(term);
2241 }
2242 break;
2243 case 'X': /* write N spaces w/o moving cursor */
2244 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2245 compatibility(ANSIMIN);
2246 {
2247 int n = def(term->esc_args[0], 1);
2248 pos cursplus;
2249 unsigned long *p = term->cpos;
2250 if (n > term->cols - term->curs.x)
2251 n = term->cols - term->curs.x;
2252 cursplus = term->curs;
2253 cursplus.x += n;
2254 check_selection(term, term->curs, cursplus);
2255 while (n--)
2256 *p++ = term->erase_char;
2257 term->seen_disp_event = TRUE;
2258 }
2259 break;
2260 case 'x': /* report terminal characteristics */
2261 compatibility(VT100);
2262 {
2263 char buf[32];
2264 int i = def(term->esc_args[0], 0);
2265 if (i == 0 || i == 1) {
2266 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2267 buf[2] += i;
2268 ldisc_send(buf, 20, 0);
2269 }
2270 }
2271 break;
2272 case 'Z': /* BackTab for xterm */
2273 compatibility(OTHER);
2274 {
2275 int i = def(term->esc_args[0], 1);
2276 pos old_curs = term->curs;
2277
2278 for(;i>0 && term->curs.x>0; i--) {
2279 do {
2280 term->curs.x--;
2281 } while (term->curs.x >0 &&
2282 !term->tabs[term->curs.x]);
2283 }
2284 fix_cpos;
2285 check_selection(term, old_curs, term->curs);
2286 }
2287 break;
2288 case ANSI('L', '='):
2289 compatibility(OTHER);
2290 term->use_bce = (term->esc_args[0] <= 0);
2291 term->erase_char = ERASE_CHAR;
2292 if (term->use_bce)
2293 term->erase_char = (' ' | ATTR_ASCII |
2294 (term->curr_attr &
2295 (ATTR_FGMASK | ATTR_BGMASK)));
2296 break;
2297 case ANSI('E', '='):
2298 compatibility(OTHER);
2299 term->blink_is_real = (term->esc_args[0] >= 1);
2300 break;
2301 case ANSI('p', '"'):
2302 /*
2303 * Allow the host to make this emulator a
2304 * 'perfect' VT102. This first appeared in
2305 * the VT220, but we do need to get back to
2306 * PuTTY mode so I won't check it.
2307 *
2308 * The arg in 40..42,50 are a PuTTY extension.
2309 * The 2nd arg, 8bit vs 7bit is not checked.
2310 *
2311 * Setting VT102 mode should also change
2312 * the Fkeys to generate PF* codes as a
2313 * real VT102 has no Fkeys. The VT220 does
2314 * this, F11..F13 become ESC,BS,LF other
2315 * Fkeys send nothing.
2316 *
2317 * Note ESC c will NOT change this!
2318 */
2319
2320 switch (term->esc_args[0]) {
2321 case 61:
2322 term->compatibility_level &= ~TM_VTXXX;
2323 term->compatibility_level |= TM_VT102;
2324 break;
2325 case 62:
2326 term->compatibility_level &= ~TM_VTXXX;
2327 term->compatibility_level |= TM_VT220;
2328 break;
2329
2330 default:
2331 if (term->esc_args[0] > 60 &&
2332 term->esc_args[0] < 70)
2333 term->compatibility_level |= TM_VTXXX;
2334 break;
2335
2336 case 40:
2337 term->compatibility_level &= TM_VTXXX;
2338 break;
2339 case 41:
2340 term->compatibility_level = TM_PUTTY;
2341 break;
2342 case 42:
2343 term->compatibility_level = TM_SCOANSI;
2344 break;
2345
2346 case ARG_DEFAULT:
2347 term->compatibility_level = TM_PUTTY;
2348 break;
2349 case 50:
2350 break;
2351 }
2352
2353 /* Change the response to CSI c */
2354 if (term->esc_args[0] == 50) {
2355 int i;
2356 char lbuf[64];
2357 strcpy(term->id_string, "\033[?");
2358 for (i = 1; i < term->esc_nargs; i++) {
2359 if (i != 1)
2360 strcat(term->id_string, ";");
2361 sprintf(lbuf, "%d", term->esc_args[i]);
2362 strcat(term->id_string, lbuf);
2363 }
2364 strcat(term->id_string, "c");
2365 }
2366 #if 0
2367 /* Is this a good idea ?
2368 * Well we should do a soft reset at this point ...
2369 */
2370 if (!has_compat(VT420) && has_compat(VT100)) {
2371 if (!cfg.no_remote_resize) {
2372 if (term->reset_132)
2373 request_resize(132, 24);
2374 else
2375 request_resize(80, 24);
2376 }
2377 }
2378 #endif
2379 break;
2380 }
2381 break;
2382 case SEEN_OSC:
2383 term->osc_w = FALSE;
2384 switch (c) {
2385 case 'P': /* Linux palette sequence */
2386 term->termstate = SEEN_OSC_P;
2387 term->osc_strlen = 0;
2388 break;
2389 case 'R': /* Linux palette reset */
2390 palette_reset();
2391 term_invalidate(term);
2392 term->termstate = TOPLEVEL;
2393 break;
2394 case 'W': /* word-set */
2395 term->termstate = SEEN_OSC_W;
2396 term->osc_w = TRUE;
2397 break;
2398 case '0':
2399 case '1':
2400 case '2':
2401 case '3':
2402 case '4':
2403 case '5':
2404 case '6':
2405 case '7':
2406 case '8':
2407 case '9':
2408 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2409 break;
2410 case 'L':
2411 /*
2412 * Grotty hack to support xterm and DECterm title
2413 * sequences concurrently.
2414 */
2415 if (term->esc_args[0] == 2) {
2416 term->esc_args[0] = 1;
2417 break;
2418 }
2419 /* else fall through */
2420 default:
2421 term->termstate = OSC_STRING;
2422 term->osc_strlen = 0;
2423 }
2424 break;
2425 case OSC_STRING:
2426 /*
2427 * This OSC stuff is EVIL. It takes just one character to get into
2428 * sysline mode and it's not initially obvious how to get out.
2429 * So I've added CR and LF as string aborts.
2430 * This shouldn't effect compatibility as I believe embedded
2431 * control characters are supposed to be interpreted (maybe?)
2432 * and they don't display anything useful anyway.
2433 *
2434 * -- RDB
2435 */
2436 if (c == '\n' || c == '\r') {
2437 term->termstate = TOPLEVEL;
2438 } else if (c == 0234 || c == '\007') {
2439 /*
2440 * These characters terminate the string; ST and BEL
2441 * terminate the sequence and trigger instant
2442 * processing of it, whereas ESC goes back to SEEN_ESC
2443 * mode unless it is followed by \, in which case it is
2444 * synonymous with ST in the first place.
2445 */
2446 do_osc(term);
2447 term->termstate = TOPLEVEL;
2448 } else if (c == '\033')
2449 term->termstate = OSC_MAYBE_ST;
2450 else if (term->osc_strlen < OSC_STR_MAX)
2451 term->osc_string[term->osc_strlen++] = c;
2452 break;
2453 case SEEN_OSC_P:
2454 {
2455 int max = (term->osc_strlen == 0 ? 21 : 16);
2456 int val;
2457 if (c >= '0' && c <= '9')
2458 val = c - '0';
2459 else if (c >= 'A' && c <= 'A' + max - 10)
2460 val = c - 'A' + 10;
2461 else if (c >= 'a' && c <= 'a' + max - 10)
2462 val = c - 'a' + 10;
2463 else {
2464 term->termstate = TOPLEVEL;
2465 break;
2466 }
2467 term->osc_string[term->osc_strlen++] = val;
2468 if (term->osc_strlen >= 7) {
2469 palette_set(term->osc_string[0],
2470 term->osc_string[1] * 16 + term->osc_string[2],
2471 term->osc_string[3] * 16 + term->osc_string[4],
2472 term->osc_string[5] * 16 + term->osc_string[6]);
2473 term_invalidate(term);
2474 term->termstate = TOPLEVEL;
2475 }
2476 }
2477 break;
2478 case SEEN_OSC_W:
2479 switch (c) {
2480 case '0':
2481 case '1':
2482 case '2':
2483 case '3':
2484 case '4':
2485 case '5':
2486 case '6':
2487 case '7':
2488 case '8':
2489 case '9':
2490 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2491 break;
2492 default:
2493 term->termstate = OSC_STRING;
2494 term->osc_strlen = 0;
2495 }
2496 break;
2497 case VT52_ESC:
2498 term->termstate = TOPLEVEL;
2499 term->seen_disp_event = TRUE;
2500 switch (c) {
2501 case 'A':
2502 move(term, term->curs.x, term->curs.y - 1, 1);
2503 break;
2504 case 'B':
2505 move(term, term->curs.x, term->curs.y + 1, 1);
2506 break;
2507 case 'C':
2508 move(term, term->curs.x + 1, term->curs.y, 1);
2509 break;
2510 case 'D':
2511 move(term, term->curs.x - 1, term->curs.y, 1);
2512 break;
2513 /*
2514 * From the VT100 Manual
2515 * NOTE: The special graphics characters in the VT100
2516 * are different from those in the VT52
2517 *
2518 * From VT102 manual:
2519 * 137 _ Blank - Same
2520 * 140 ` Reserved - Humm.
2521 * 141 a Solid rectangle - Similar
2522 * 142 b 1/ - Top half of fraction for the
2523 * 143 c 3/ - subscript numbers below.
2524 * 144 d 5/
2525 * 145 e 7/
2526 * 146 f Degrees - Same
2527 * 147 g Plus or minus - Same
2528 * 150 h Right arrow
2529 * 151 i Ellipsis (dots)
2530 * 152 j Divide by
2531 * 153 k Down arrow
2532 * 154 l Bar at scan 0
2533 * 155 m Bar at scan 1
2534 * 156 n Bar at scan 2
2535 * 157 o Bar at scan 3 - Similar
2536 * 160 p Bar at scan 4 - Similar
2537 * 161 q Bar at scan 5 - Similar
2538 * 162 r Bar at scan 6 - Same
2539 * 163 s Bar at scan 7 - Similar
2540 * 164 t Subscript 0
2541 * 165 u Subscript 1
2542 * 166 v Subscript 2
2543 * 167 w Subscript 3
2544 * 170 x Subscript 4
2545 * 171 y Subscript 5
2546 * 172 z Subscript 6
2547 * 173 { Subscript 7
2548 * 174 | Subscript 8
2549 * 175 } Subscript 9
2550 * 176 ~ Paragraph
2551 *
2552 */
2553 case 'F':
2554 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2555 break;
2556 case 'G':
2557 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2558 break;
2559 case 'H':
2560 move(term, 0, 0, 0);
2561 break;
2562 case 'I':
2563 if (term->curs.y == 0)
2564 scroll(term, 0, term->rows - 1, -1, TRUE);
2565 else if (term->curs.y > 0)
2566 term->curs.y--;
2567 fix_cpos;
2568 term->wrapnext = FALSE;
2569 break;
2570 case 'J':
2571 erase_lots(term, FALSE, FALSE, TRUE);
2572 term->disptop = 0;
2573 break;
2574 case 'K':
2575 erase_lots(term, TRUE, FALSE, TRUE);
2576 break;
2577 #if 0
2578 case 'V':
2579 /* XXX Print cursor line */
2580 break;
2581 case 'W':
2582 /* XXX Start controller mode */
2583 break;
2584 case 'X':
2585 /* XXX Stop controller mode */
2586 break;
2587 #endif
2588 case 'Y':
2589 term->termstate = VT52_Y1;
2590 break;
2591 case 'Z':
2592 ldisc_send("\033/Z", 3, 0);
2593 break;
2594 case '=':
2595 term->app_keypad_keys = TRUE;
2596 break;
2597 case '>':
2598 term->app_keypad_keys = FALSE;
2599 break;
2600 case '<':
2601 /* XXX This should switch to VT100 mode not current or default
2602 * VT mode. But this will only have effect in a VT220+
2603 * emulation.
2604 */
2605 term->vt52_mode = FALSE;
2606 term->blink_is_real = cfg.blinktext;
2607 break;
2608 #if 0
2609 case '^':
2610 /* XXX Enter auto print mode */
2611 break;
2612 case '_':
2613 /* XXX Exit auto print mode */
2614 break;
2615 case ']':
2616 /* XXX Print screen */
2617 break;
2618 #endif
2619
2620 #ifdef VT52_PLUS
2621 case 'E':
2622 /* compatibility(ATARI) */
2623 move(term, 0, 0, 0);
2624 erase_lots(term, FALSE, FALSE, TRUE);
2625 term->disptop = 0;
2626 break;
2627 case 'L':
2628 /* compatibility(ATARI) */
2629 if (term->curs.y <= term->marg_b)
2630 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2631 break;
2632 case 'M':
2633 /* compatibility(ATARI) */
2634 if (term->curs.y <= term->marg_b)
2635 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2636 break;
2637 case 'b':
2638 /* compatibility(ATARI) */
2639 term->termstate = VT52_FG;
2640 break;
2641 case 'c':
2642 /* compatibility(ATARI) */
2643 term->termstate = VT52_BG;
2644 break;
2645 case 'd':
2646 /* compatibility(ATARI) */
2647 erase_lots(term, FALSE, TRUE, FALSE);
2648 term->disptop = 0;
2649 break;
2650 case 'e':
2651 /* compatibility(ATARI) */
2652 term->cursor_on = TRUE;
2653 break;
2654 case 'f':
2655 /* compatibility(ATARI) */
2656 term->cursor_on = FALSE;
2657 break;
2658 /* case 'j': Save cursor position - broken on ST */
2659 /* case 'k': Restore cursor position */
2660 case 'l':
2661 /* compatibility(ATARI) */
2662 erase_lots(term, TRUE, TRUE, TRUE);
2663 term->curs.x = 0;
2664 term->wrapnext = FALSE;
2665 fix_cpos;
2666 break;
2667 case 'o':
2668 /* compatibility(ATARI) */
2669 erase_lots(term, TRUE, TRUE, FALSE);
2670 break;
2671 case 'p':
2672 /* compatibility(ATARI) */
2673 term->curr_attr |= ATTR_REVERSE;
2674 break;
2675 case 'q':
2676 /* compatibility(ATARI) */
2677 term->curr_attr &= ~ATTR_REVERSE;
2678 break;
2679 case 'v': /* wrap Autowrap on - Wyse style */
2680 /* compatibility(ATARI) */
2681 term->wrap = 1;
2682 break;
2683 case 'w': /* Autowrap off */
2684 /* compatibility(ATARI) */
2685 term->wrap = 0;
2686 break;
2687
2688 case 'R':
2689 /* compatibility(OTHER) */
2690 term->vt52_bold = FALSE;
2691 term->curr_attr = ATTR_DEFAULT;
2692 if (term->use_bce)
2693 term->erase_char = (' ' | ATTR_ASCII |
2694 (term->curr_attr &
2695 (ATTR_FGMASK | ATTR_BGMASK)));
2696 break;
2697 case 'S':
2698 /* compatibility(VI50) */
2699 term->curr_attr |= ATTR_UNDER;
2700 break;
2701 case 'W':
2702 /* compatibility(VI50) */
2703 term->curr_attr &= ~ATTR_UNDER;
2704 break;
2705 case 'U':
2706 /* compatibility(VI50) */
2707 term->vt52_bold = TRUE;
2708 term->curr_attr |= ATTR_BOLD;
2709 break;
2710 case 'T':
2711 /* compatibility(VI50) */
2712 term->vt52_bold = FALSE;
2713 term->curr_attr &= ~ATTR_BOLD;
2714 break;
2715 #endif
2716 }
2717 break;
2718 case VT52_Y1:
2719 term->termstate = VT52_Y2;
2720 move(term, term->curs.x, c - ' ', 0);
2721 break;
2722 case VT52_Y2:
2723 term->termstate = TOPLEVEL;
2724 move(term, c - ' ', term->curs.y, 0);
2725 break;
2726
2727 #ifdef VT52_PLUS
2728 case VT52_FG:
2729 term->termstate = TOPLEVEL;
2730 term->curr_attr &= ~ATTR_FGMASK;
2731 term->curr_attr &= ~ATTR_BOLD;
2732 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
2733 if ((c & 0x8) || term->vt52_bold)
2734 term->curr_attr |= ATTR_BOLD;
2735
2736 if (term->use_bce)
2737 term->erase_char = (' ' | ATTR_ASCII |
2738 (term->curr_attr &
2739 (ATTR_FGMASK | ATTR_BGMASK)));
2740 break;
2741 case VT52_BG:
2742 term->termstate = TOPLEVEL;
2743 term->curr_attr &= ~ATTR_BGMASK;
2744 term->curr_attr &= ~ATTR_BLINK;
2745 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
2746
2747 /* Note: bold background */
2748 if (c & 0x8)
2749 term->curr_attr |= ATTR_BLINK;
2750
2751 if (term->use_bce)
2752 term->erase_char = (' ' | ATTR_ASCII |
2753 (term->curr_attr &
2754 (ATTR_FGMASK | ATTR_BGMASK)));
2755 break;
2756 #endif
2757 default: break; /* placate gcc warning about enum use */
2758 }
2759 if (term->selstate != NO_SELECTION) {
2760 pos cursplus = term->curs;
2761 incpos(cursplus);
2762 check_selection(term, term->curs, cursplus);
2763 }
2764 }
2765
2766 term_print_flush(term);
2767 }
2768
2769 #if 0
2770 /*
2771 * Compare two lines to determine whether they are sufficiently
2772 * alike to scroll-optimise one to the other. Return the degree of
2773 * similarity.
2774 */
2775 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
2776 {
2777 int i, n;
2778
2779 for (i = n = 0; i < term->cols; i++)
2780 n += (*a++ == *b++);
2781 return n;
2782 }
2783 #endif
2784
2785 /*
2786 * Given a context, update the window. Out of paranoia, we don't
2787 * allow WM_PAINT responses to do scrolling optimisations.
2788 */
2789 static void do_paint(Terminal *term, Context ctx, int may_optimise)
2790 {
2791 int i, j, our_curs_y;
2792 unsigned long rv, cursor;
2793 pos scrpos;
2794 char ch[1024];
2795 long cursor_background = ERASE_CHAR;
2796 unsigned long ticks;
2797
2798 /*
2799 * Check the visual bell state.
2800 */
2801 if (term->in_vbell) {
2802 ticks = GETTICKCOUNT();
2803 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
2804 term->in_vbell = FALSE;
2805 }
2806
2807 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
2808
2809 /* Depends on:
2810 * screen array, disptop, scrtop,
2811 * selection, rv,
2812 * cfg.blinkpc, blink_is_real, tblinker,
2813 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
2814 */
2815
2816 /* Has the cursor position or type changed ? */
2817 if (term->cursor_on) {
2818 if (term->has_focus) {
2819 if (term->blinker || !cfg.blink_cur)
2820 cursor = TATTR_ACTCURS;
2821 else
2822 cursor = 0;
2823 } else
2824 cursor = TATTR_PASCURS;
2825 if (term->wrapnext)
2826 cursor |= TATTR_RIGHTCURS;
2827 } else
2828 cursor = 0;
2829 our_curs_y = term->curs.y - term->disptop;
2830
2831 if (term->dispcurs && (term->curstype != cursor ||
2832 term->dispcurs !=
2833 term->disptext + our_curs_y * (term->cols + 1) +
2834 term->curs.x)) {
2835 if (term->dispcurs > term->disptext &&
2836 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2837 term->dispcurs[-1] |= ATTR_INVALID;
2838 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2839 term->dispcurs[1] |= ATTR_INVALID;
2840 *term->dispcurs |= ATTR_INVALID;
2841 term->curstype = 0;
2842 }
2843 term->dispcurs = NULL;
2844
2845 /* The normal screen data */
2846 for (i = 0; i < term->rows; i++) {
2847 unsigned long *ldata;
2848 int lattr;
2849 int idx, dirty_line, dirty_run, selected;
2850 unsigned long attr = 0;
2851 int updated_line = 0;
2852 int start = 0;
2853 int ccount = 0;
2854 int last_run_dirty = 0;
2855
2856 scrpos.y = i + term->disptop;
2857 ldata = lineptr(scrpos.y);
2858 lattr = (ldata[term->cols] & LATTR_MODE);
2859
2860 idx = i * (term->cols + 1);
2861 dirty_run = dirty_line = (ldata[term->cols] !=
2862 term->disptext[idx + term->cols]);
2863 term->disptext[idx + term->cols] = ldata[term->cols];
2864
2865 for (j = 0; j < term->cols; j++, idx++) {
2866 unsigned long tattr, tchar;
2867 unsigned long *d = ldata + j;
2868 int break_run;
2869 scrpos.x = j;
2870
2871 tchar = (*d & (CHAR_MASK | CSET_MASK));
2872 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
2873 switch (tchar & CSET_MASK) {
2874 case ATTR_ASCII:
2875 tchar = unitab_line[tchar & 0xFF];
2876 break;
2877 case ATTR_LINEDRW:
2878 tchar = unitab_xterm[tchar & 0xFF];
2879 break;
2880 case ATTR_SCOACS:
2881 tchar = unitab_scoacs[tchar&0xFF];
2882 break;
2883 }
2884 tattr |= (tchar & CSET_MASK);
2885 tchar &= CHAR_MASK;
2886 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2887 tattr |= ATTR_WIDE;
2888
2889 /* Video reversing things */
2890 if (term->seltype == LEXICOGRAPHIC)
2891 selected = (posle(term->selstart, scrpos) &&
2892 poslt(scrpos, term->selend));
2893 else
2894 selected = (posPle(term->selstart, scrpos) &&
2895 posPlt(scrpos, term->selend));
2896 tattr = (tattr ^ rv
2897 ^ (selected ? ATTR_REVERSE : 0));
2898
2899 /* 'Real' blinking ? */
2900 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
2901 if (term->has_focus && term->tblinker) {
2902 tchar = ' ';
2903 tattr &= ~CSET_MASK;
2904 tattr |= ATTR_ACP;
2905 }
2906 tattr &= ~ATTR_BLINK;
2907 }
2908
2909 /*
2910 * Check the font we'll _probably_ be using to see if
2911 * the character is wide when we don't want it to be.
2912 */
2913 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
2914 if ((tattr & ATTR_WIDE) == 0 &&
2915 CharWidth(ctx, (tchar | tattr) & 0xFFFF) == 2)
2916 tattr |= ATTR_NARROW;
2917 } else if (term->disptext[idx]&ATTR_NARROW)
2918 tattr |= ATTR_NARROW;
2919
2920 /* Cursor here ? Save the 'background' */
2921 if (i == our_curs_y && j == term->curs.x) {
2922 cursor_background = tattr | tchar;
2923 term->dispcurs = term->disptext + idx;
2924 }
2925
2926 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
2927 dirty_line = TRUE;
2928
2929 break_run = (tattr != attr || j - start >= sizeof(ch));
2930
2931 /* Special hack for VT100 Linedraw glyphs */
2932 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
2933 && tchar <= 0xBD) break_run = TRUE;
2934
2935 if (!dbcs_screenfont && !dirty_line) {
2936 if ((tchar | tattr) == term->disptext[idx])
2937 break_run = TRUE;
2938 else if (!dirty_run && ccount == 1)
2939 break_run = TRUE;
2940 }
2941
2942 if (break_run) {
2943 if ((dirty_run || last_run_dirty) && ccount > 0) {
2944 do_text(ctx, start, i, ch, ccount, attr, lattr);
2945 updated_line = 1;
2946 }
2947 start = j;
2948 ccount = 0;
2949 attr = tattr;
2950 if (dbcs_screenfont)
2951 last_run_dirty = dirty_run;
2952 dirty_run = dirty_line;
2953 }
2954
2955 if ((tchar | tattr) != term->disptext[idx])
2956 dirty_run = TRUE;
2957 ch[ccount++] = (char) tchar;
2958 term->disptext[idx] = tchar | tattr;
2959
2960 /* If it's a wide char step along to the next one. */
2961 if (tattr & ATTR_WIDE) {
2962 if (++j < term->cols) {
2963 idx++;
2964 d++;
2965 /* Cursor is here ? Ouch! */
2966 if (i == our_curs_y && j == term->curs.x) {
2967 cursor_background = *d;
2968 term->dispcurs = term->disptext + idx;
2969 }
2970 if (term->disptext[idx] != *d)
2971 dirty_run = TRUE;
2972 term->disptext[idx] = *d;
2973 }
2974 }
2975 }
2976 if (dirty_run && ccount > 0) {
2977 do_text(ctx, start, i, ch, ccount, attr, lattr);
2978 updated_line = 1;
2979 }
2980
2981 /* Cursor on this line ? (and changed) */
2982 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
2983 ch[0] = (char) (cursor_background & CHAR_MASK);
2984 attr = (cursor_background & ATTR_MASK) | cursor;
2985 do_cursor(ctx, term->curs.x, i, ch, 1, attr, lattr);
2986 term->curstype = cursor;
2987 }
2988 }
2989 }
2990
2991 /*
2992 * Flick the switch that says if blinking things should be shown or hidden.
2993 */
2994
2995 void term_blink(Terminal *term, int flg)
2996 {
2997 long now, blink_diff;
2998
2999 now = GETTICKCOUNT();
3000 blink_diff = now - term->last_tblink;
3001
3002 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3003 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3004 term->last_tblink = now;
3005 term->tblinker = !term->tblinker;
3006 }
3007
3008 if (flg) {
3009 term->blinker = 1;
3010 term->last_blink = now;
3011 return;
3012 }
3013
3014 blink_diff = now - term->last_blink;
3015
3016 /* Make sure the cursor blinks no faster than system blink rate */
3017 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3018 return;
3019
3020 term->last_blink = now;
3021 term->blinker = !term->blinker;
3022 }
3023
3024 /*
3025 * Invalidate the whole screen so it will be repainted in full.
3026 */
3027 void term_invalidate(Terminal *term)
3028 {
3029 int i;
3030
3031 for (i = 0; i < term->rows * (term->cols + 1); i++)
3032 term->disptext[i] = ATTR_INVALID;
3033 }
3034
3035 /*
3036 * Paint the window in response to a WM_PAINT message.
3037 */
3038 void term_paint(Terminal *term, Context ctx,
3039 int left, int top, int right, int bottom)
3040 {
3041 int i, j;
3042 if (left < 0) left = 0;
3043 if (top < 0) top = 0;
3044 if (right >= term->cols) right = term->cols-1;
3045 if (bottom >= term->rows) bottom = term->rows-1;
3046
3047 for (i = top; i <= bottom && i < term->rows; i++) {
3048 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3049 LATTR_MODE) == LATTR_NORM)
3050 for (j = left; j <= right && j < term->cols; j++)
3051 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3052 else
3053 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3054 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3055 }
3056
3057 /* This should happen soon enough, also for some reason it sometimes
3058 * fails to actually do anything when re-sizing ... painting the wrong
3059 * window perhaps ?
3060 */
3061 if (alt_pressed)
3062 do_paint (term, ctx, FALSE);
3063 }
3064
3065 /*
3066 * Attempt to scroll the scrollback. The second parameter gives the
3067 * position we want to scroll to; the first is +1 to denote that
3068 * this position is relative to the beginning of the scrollback, -1
3069 * to denote it is relative to the end, and 0 to denote that it is
3070 * relative to the current position.
3071 */
3072 void term_scroll(Terminal *term, int rel, int where)
3073 {
3074 int sbtop = -count234(term->scrollback);
3075
3076 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3077 if (term->disptop < sbtop)
3078 term->disptop = sbtop;
3079 if (term->disptop > 0)
3080 term->disptop = 0;
3081 update_sbar(term);
3082 term_update(term);
3083 }
3084
3085 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3086 {
3087 wchar_t *workbuf;
3088 wchar_t *wbptr; /* where next char goes within workbuf */
3089 int old_top_x;
3090 int wblen = 0; /* workbuf len */
3091 int buflen; /* amount of memory allocated to workbuf */
3092
3093 buflen = 5120; /* Default size */
3094 workbuf = smalloc(buflen * sizeof(wchar_t));
3095 wbptr = workbuf; /* start filling here */
3096 old_top_x = top.x; /* needed for rect==1 */
3097
3098 while (poslt(top, bottom)) {
3099 int nl = FALSE;
3100 unsigned long *ldata = lineptr(top.y);
3101 pos nlpos;
3102
3103 /*
3104 * nlpos will point at the maximum position on this line we
3105 * should copy up to. So we start it at the end of the
3106 * line...
3107 */
3108 nlpos.y = top.y;
3109 nlpos.x = term->cols;
3110
3111 /*
3112 * ... move it backwards if there's unused space at the end
3113 * of the line (and also set `nl' if this is the case,
3114 * because in normal selection mode this means we need a
3115 * newline at the end)...
3116 */
3117 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3118 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3119 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3120 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3121 && poslt(top, nlpos))
3122 decpos(nlpos);
3123 if (poslt(nlpos, bottom))
3124 nl = TRUE;
3125 }
3126
3127 /*
3128 * ... and then clip it to the terminal x coordinate if
3129 * we're doing rectangular selection. (In this case we
3130 * still did the above, so that copying e.g. the right-hand
3131 * column from a table doesn't fill with spaces on the
3132 * right.)
3133 */
3134 if (rect) {
3135 if (nlpos.x > bottom.x)
3136 nlpos.x = bottom.x;
3137 nl = (top.y < bottom.y);
3138 }
3139
3140 while (poslt(top, bottom) && poslt(top, nlpos)) {
3141 #if 0
3142 char cbuf[16], *p;
3143 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3144 #else
3145 wchar_t cbuf[16], *p;
3146 int uc = (ldata[top.x] & 0xFFFF);
3147 int set, c;
3148
3149 if (uc == UCSWIDE) {
3150 top.x++;
3151 continue;
3152 }
3153
3154 switch (uc & CSET_MASK) {
3155 case ATTR_LINEDRW:
3156 if (!cfg.rawcnp) {
3157 uc = unitab_xterm[uc & 0xFF];
3158 break;
3159 }
3160 case ATTR_ASCII:
3161 uc = unitab_line[uc & 0xFF];
3162 break;
3163 case ATTR_SCOACS:
3164 uc = unitab_scoacs[uc&0xFF];
3165 break;
3166 }
3167 switch (uc & CSET_MASK) {
3168 case ATTR_ACP:
3169 uc = unitab_font[uc & 0xFF];
3170 break;
3171 case ATTR_OEMCP:
3172 uc = unitab_oemcp[uc & 0xFF];
3173 break;
3174 }
3175
3176 set = (uc & CSET_MASK);
3177 c = (uc & CHAR_MASK);
3178 cbuf[0] = uc;
3179 cbuf[1] = 0;
3180
3181 if (DIRECT_FONT(uc)) {
3182 if (c >= ' ' && c != 0x7F) {
3183 unsigned char buf[4];
3184 WCHAR wbuf[4];
3185 int rv;
3186 if (is_dbcs_leadbyte(font_codepage, (BYTE) c)) {
3187 buf[0] = c;
3188 buf[1] = (unsigned char) ldata[top.x + 1];
3189 rv = mb_to_wc(font_codepage, 0, buf, 2, wbuf, 4);
3190 top.x++;
3191 } else {
3192 buf[0] = c;
3193 rv = mb_to_wc(font_codepage, 0, buf, 1, wbuf, 4);
3194 }
3195
3196 if (rv > 0) {
3197 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3198 cbuf[rv] = 0;
3199 }
3200 }
3201 }
3202 #endif
3203
3204 for (p = cbuf; *p; p++) {
3205 /* Enough overhead for trailing NL and nul */
3206 if (wblen >= buflen - 16) {
3207 workbuf =
3208 srealloc(workbuf,
3209 sizeof(wchar_t) * (buflen += 100));
3210 wbptr = workbuf + wblen;
3211 }
3212 wblen++;
3213 *wbptr++ = *p;
3214 }
3215 top.x++;
3216 }
3217 if (nl) {
3218 int i;
3219 for (i = 0; i < sel_nl_sz; i++) {
3220 wblen++;
3221 *wbptr++ = sel_nl[i];
3222 }
3223 }
3224 top.y++;
3225 top.x = rect ? old_top_x : 0;
3226 }
3227 #if SELECTION_NUL_TERMINATED
3228 wblen++;
3229 *wbptr++ = 0;
3230 #endif
3231 write_clip(workbuf, wblen, FALSE); /* transfer to clipboard */
3232 if (buflen > 0) /* indicates we allocated this buffer */
3233 sfree(workbuf);
3234 }
3235
3236 void term_copyall(Terminal *term)
3237 {
3238 pos top;
3239 top.y = -count234(term->scrollback);
3240 top.x = 0;
3241 clipme(term, top, term->curs, 0);
3242 }
3243
3244 /*
3245 * The wordness array is mainly for deciding the disposition of the
3246 * US-ASCII characters.
3247 */
3248 static int wordtype(Terminal *term, int uc)
3249 {
3250 static struct {
3251 int start, end, ctype;
3252 } *wptr, ucs_words[] = {
3253 {
3254 128, 160, 0}, {
3255 161, 191, 1}, {
3256 215, 215, 1}, {
3257 247, 247, 1}, {
3258 0x037e, 0x037e, 1}, /* Greek question mark */
3259 {
3260 0x0387, 0x0387, 1}, /* Greek ano teleia */
3261 {
3262 0x055a, 0x055f, 1}, /* Armenian punctuation */
3263 {
3264 0x0589, 0x0589, 1}, /* Armenian full stop */
3265 {
3266 0x0700, 0x070d, 1}, /* Syriac punctuation */
3267 {
3268 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3269 {
3270 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3271 {
3272 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3273 {
3274 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3275 {
3276 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3277 {
3278 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3279 {
3280 0x2000, 0x200a, 0}, /* Various spaces */
3281 {
3282 0x2070, 0x207f, 2}, /* superscript */
3283 {
3284 0x2080, 0x208f, 2}, /* subscript */
3285 {
3286 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3287 {
3288 0x3000, 0x3000, 0}, /* ideographic space */
3289 {
3290 0x3001, 0x3020, 1}, /* ideographic punctuation */
3291 {
3292 0x303f, 0x309f, 3}, /* Hiragana */
3293 {
3294 0x30a0, 0x30ff, 3}, /* Katakana */
3295 {
3296 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3297 {
3298 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3299 {
3300 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3301 {
3302 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3303 {
3304 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3305 {
3306 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3307 {
3308 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3309 {
3310 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3311 {
3312 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3313 {
3314 0, 0, 0}
3315 };
3316
3317 uc &= (CSET_MASK | CHAR_MASK);
3318
3319 switch (uc & CSET_MASK) {
3320 case ATTR_LINEDRW:
3321 uc = unitab_xterm[uc & 0xFF];
3322 break;
3323 case ATTR_ASCII:
3324 uc = unitab_line[uc & 0xFF];
3325 break;
3326 case ATTR_SCOACS:
3327 uc = unitab_scoacs[uc&0xFF];
3328 break;
3329 }
3330 switch (uc & CSET_MASK) {
3331 case ATTR_ACP:
3332 uc = unitab_font[uc & 0xFF];
3333 break;
3334 case ATTR_OEMCP:
3335 uc = unitab_oemcp[uc & 0xFF];
3336 break;
3337 }
3338
3339 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3340 * fail as there's such a thing as a double width space. :-(
3341 */
3342 if (dbcs_screenfont && font_codepage == line_codepage)
3343 return (uc != ' ');
3344
3345 if (uc < 0x80)
3346 return term->wordness[uc];
3347
3348 for (wptr = ucs_words; wptr->start; wptr++) {
3349 if (uc >= wptr->start && uc <= wptr->end)
3350 return wptr->ctype;
3351 }
3352
3353 return 2;
3354 }
3355
3356 /*
3357 * Spread the selection outwards according to the selection mode.
3358 */
3359 static pos sel_spread_half(Terminal *term, pos p, int dir)
3360 {
3361 unsigned long *ldata;
3362 short wvalue;
3363 int topy = -count234(term->scrollback);
3364
3365 ldata = lineptr(p.y);
3366
3367 switch (term->selmode) {
3368 case SM_CHAR:
3369 /*
3370 * In this mode, every character is a separate unit, except
3371 * for runs of spaces at the end of a non-wrapping line.
3372 */
3373 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3374 unsigned long *q = ldata + term->cols;
3375 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3376 q--;
3377 if (q == ldata + term->cols)
3378 q--;
3379 if (p.x >= q - ldata)
3380 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3381 }
3382 break;
3383 case SM_WORD:
3384 /*
3385 * In this mode, the units are maximal runs of characters
3386 * whose `wordness' has the same value.
3387 */
3388 wvalue = wordtype(term, ldata[p.x]);
3389 if (dir == +1) {
3390 while (1) {
3391 if (p.x < term->cols-1) {
3392 if (wordtype(term, ldata[p.x + 1]) == wvalue)
3393 p.x++;
3394 else
3395 break;
3396 } else {
3397 if (ldata[term->cols] & LATTR_WRAPPED) {
3398 unsigned long *ldata2;
3399 ldata2 = lineptr(p.y+1);
3400 if (wordtype(term, ldata2[0]) == wvalue) {
3401 p.x = 0;
3402 p.y++;
3403 ldata = ldata2;
3404 } else
3405 break;
3406 } else
3407 break;
3408 }
3409 }
3410 } else {
3411 while (1) {
3412 if (p.x > 0) {
3413 if (wordtype(term, ldata[p.x - 1]) == wvalue)
3414 p.x--;
3415 else
3416 break;
3417 } else {
3418 unsigned long *ldata2;
3419 if (p.y <= topy)
3420 break;
3421 ldata2 = lineptr(p.y-1);
3422 if ((ldata2[term->cols] & LATTR_WRAPPED) &&
3423 wordtype(term, ldata2[term->cols-1]) == wvalue) {
3424 p.x = term->cols-1;
3425 p.y--;
3426 ldata = ldata2;
3427 } else
3428 break;
3429 }
3430 }
3431 }
3432 break;
3433 case SM_LINE:
3434 /*
3435 * In this mode, every line is a unit.
3436 */
3437 p.x = (dir == -1 ? 0 : term->cols - 1);
3438 break;
3439 }
3440 return p;
3441 }
3442
3443 static void sel_spread(Terminal *term)
3444 {
3445 if (term->seltype == LEXICOGRAPHIC) {
3446 term->selstart = sel_spread_half(term, term->selstart, -1);
3447 decpos(term->selend);
3448 term->selend = sel_spread_half(term, term->selend, +1);
3449 incpos(term->selend);
3450 }
3451 }
3452
3453 void term_do_paste(Terminal *term)
3454 {
3455 wchar_t *data;
3456 int len;
3457
3458 get_clip(&data, &len);
3459 if (data && len > 0) {
3460 wchar_t *p, *q;
3461
3462 term_seen_key_event(term); /* pasted data counts */
3463
3464 if (term->paste_buffer)
3465 sfree(term->paste_buffer);
3466 term->paste_pos = term->paste_hold = term->paste_len = 0;
3467 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3468
3469 p = q = data;
3470 while (p < data + len) {
3471 while (p < data + len &&
3472 !(p <= data + len - sel_nl_sz &&
3473 !memcmp(p, sel_nl, sizeof(sel_nl))))
3474 p++;
3475
3476 {
3477 int i;
3478 for (i = 0; i < p - q; i++) {
3479 term->paste_buffer[term->paste_len++] = q[i];
3480 }
3481 }
3482
3483 if (p <= data + len - sel_nl_sz &&
3484 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3485 term->paste_buffer[term->paste_len++] = '\r';
3486 p += sel_nl_sz;
3487 }
3488 q = p;
3489 }
3490
3491 /* Assume a small paste will be OK in one go. */
3492 if (term->paste_len < 256) {
3493 luni_send(term->paste_buffer, term->paste_len, 0);
3494 if (term->paste_buffer)
3495 sfree(term->paste_buffer);
3496 term->paste_buffer = 0;
3497 term->paste_pos = term->paste_hold = term->paste_len = 0;
3498 }
3499 }
3500 get_clip(NULL, NULL);
3501 }
3502
3503 void term_mouse(Terminal *term, Mouse_Button b, Mouse_Action a, int x, int y,
3504 int shift, int ctrl, int alt)
3505 {
3506 pos selpoint;
3507 unsigned long *ldata;
3508 int raw_mouse = (term->xterm_mouse &&
3509 !cfg.no_mouse_rep &&
3510 !(cfg.mouse_override && shift));
3511 int default_seltype;
3512
3513 if (y < 0) {
3514 y = 0;
3515 if (a == MA_DRAG && !raw_mouse)
3516 term_scroll(term, 0, -1);
3517 }
3518 if (y >= term->rows) {
3519 y = term->rows - 1;
3520 if (a == MA_DRAG && !raw_mouse)
3521 term_scroll(term, 0, +1);
3522 }
3523 if (x < 0) {
3524 if (y > 0) {
3525 x = term->cols - 1;
3526 y--;
3527 } else
3528 x = 0;
3529 }
3530 if (x >= term->cols)
3531 x = term->cols - 1;
3532
3533 selpoint.y = y + term->disptop;
3534 selpoint.x = x;
3535 ldata = lineptr(selpoint.y);
3536 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3537 selpoint.x /= 2;
3538
3539 if (raw_mouse) {
3540 int encstate = 0, r, c;
3541 char abuf[16];
3542 static int is_down = 0;
3543
3544 switch (b) {
3545 case MBT_LEFT:
3546 encstate = 0x20; /* left button down */
3547 break;
3548 case MBT_MIDDLE:
3549 encstate = 0x21;
3550 break;
3551 case MBT_RIGHT:
3552 encstate = 0x22;
3553 break;
3554 case MBT_WHEEL_UP:
3555 encstate = 0x60;
3556 break;
3557 case MBT_WHEEL_DOWN:
3558 encstate = 0x61;
3559 break;
3560 default: break; /* placate gcc warning about enum use */
3561 }
3562 switch (a) {
3563 case MA_DRAG:
3564 if (term->xterm_mouse == 1)
3565 return;
3566 encstate += 0x20;
3567 break;
3568 case MA_RELEASE:
3569 encstate = 0x23;
3570 is_down = 0;
3571 break;
3572 case MA_CLICK:
3573 if (is_down == b)
3574 return;
3575 is_down = b;
3576 break;
3577 default: break; /* placate gcc warning about enum use */
3578 }
3579 if (shift)
3580 encstate += 0x04;
3581 if (ctrl)
3582 encstate += 0x10;
3583 r = y + 33;
3584 c = x + 33;
3585
3586 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3587 ldisc_send(abuf, 6, 0);
3588 return;
3589 }
3590
3591 b = translate_button(b);
3592
3593 /*
3594 * Set the selection type (rectangular or normal) at the start
3595 * of a selection attempt, from the state of Alt.
3596 */
3597 if (!alt ^ !cfg.rect_select)
3598 default_seltype = RECTANGULAR;
3599 else
3600 default_seltype = LEXICOGRAPHIC;
3601
3602 if (term->selstate == NO_SELECTION) {
3603 term->seltype = default_seltype;
3604 }
3605
3606 if (b == MBT_SELECT && a == MA_CLICK) {
3607 deselect(term);
3608 term->selstate = ABOUT_TO;
3609 term->seltype = default_seltype;
3610 term->selanchor = selpoint;
3611 term->selmode = SM_CHAR;
3612 } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3613 deselect(term);
3614 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3615 term->selstate = DRAGGING;
3616 term->selstart = term->selanchor = selpoint;
3617 term->selend = term->selstart;
3618 incpos(term->selend);
3619 sel_spread(term);
3620 } else if ((b == MBT_SELECT && a == MA_DRAG) ||
3621 (b == MBT_EXTEND && a != MA_RELEASE)) {
3622 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3623 return;
3624 if (b == MBT_EXTEND && a != MA_DRAG && term->selstate == SELECTED) {
3625 if (term->seltype == LEXICOGRAPHIC) {
3626 /*
3627 * For normal selection, we extend by moving
3628 * whichever end of the current selection is closer
3629 * to the mouse.
3630 */
3631 if (posdiff(selpoint, term->selstart) <
3632 posdiff(term->selend, term->selstart) / 2) {
3633 term->selanchor = term->selend;
3634 decpos(term->selanchor);
3635 } else {
3636 term->selanchor = term->selstart;
3637 }
3638 } else {
3639 /*
3640 * For rectangular selection, we have a choice of
3641 * _four_ places to put selanchor and selpoint: the
3642 * four corners of the selection.
3643 */
3644 if (2*selpoint.x < term->selstart.x + term->selend.x)
3645 term->selanchor.x = term->selend.x-1;
3646 else
3647 term->selanchor.x = term->selstart.x;
3648
3649 if (2*selpoint.y < term->selstart.y + term->selend.y)
3650 term->selanchor.y = term->selend.y;
3651 else
3652 term->selanchor.y = term->selstart.y;
3653 }
3654 term->selstate = DRAGGING;
3655 }
3656 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3657 term->selanchor = selpoint;
3658 term->selstate = DRAGGING;
3659 if (term->seltype == LEXICOGRAPHIC) {
3660 /*
3661 * For normal selection, we set (selstart,selend) to
3662 * (selpoint,selanchor) in some order.
3663 */
3664 if (poslt(selpoint, term->selanchor)) {
3665 term->selstart = selpoint;
3666 term->selend = term->selanchor;
3667 incpos(term->selend);
3668 } else {
3669 term->selstart = term->selanchor;
3670 term->selend = selpoint;
3671 incpos(term->selend);
3672 }
3673 } else {
3674 /*
3675 * For rectangular selection, we may need to
3676 * interchange x and y coordinates (if the user has
3677 * dragged in the -x and +y directions, or vice versa).
3678 */
3679 term->selstart.x = min(term->selanchor.x, selpoint.x);
3680 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3681 term->selstart.y = min(term->selanchor.y, selpoint.y);
3682 term->selend.y = max(term->selanchor.y, selpoint.y);
3683 }
3684 sel_spread(term);
3685 } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {
3686 if (term->selstate == DRAGGING) {
3687 /*
3688 * We've completed a selection. We now transfer the
3689 * data to the clipboard.
3690 */
3691 clipme(term, term->selstart, term->selend,
3692 (term->seltype == RECTANGULAR));
3693 term->selstate = SELECTED;
3694 } else
3695 term->selstate = NO_SELECTION;
3696 } else if (b == MBT_PASTE
3697 && (a == MA_CLICK
3698 #if MULTICLICK_ONLY_EVENT
3699 || a == MA_2CLK || a == MA_3CLK
3700 #endif
3701 )) {
3702 request_paste();
3703 }
3704
3705 term_update(term);
3706 }
3707
3708 void term_nopaste(Terminal *term)
3709 {
3710 if (term->paste_len == 0)
3711 return;
3712 sfree(term->paste_buffer);
3713 term->paste_buffer = 0;
3714 term->paste_len = 0;
3715 }
3716
3717 int term_paste_pending(Terminal *term)
3718 {
3719 return term->paste_len != 0;
3720 }
3721
3722 void term_paste(Terminal *term)
3723 {
3724 long now, paste_diff;
3725
3726 if (term->paste_len == 0)
3727 return;
3728
3729 /* Don't wait forever to paste */
3730 if (term->paste_hold) {
3731 now = GETTICKCOUNT();
3732 paste_diff = now - term->last_paste;
3733 if (paste_diff >= 0 && paste_diff < 450)
3734 return;
3735 }
3736 term->paste_hold = 0;
3737
3738 while (term->paste_pos < term->paste_len) {
3739 int n = 0;
3740 while (n + term->paste_pos < term->paste_len) {
3741 if (term->paste_buffer[term->paste_pos + n++] == '\r')
3742 break;
3743 }
3744 luni_send(term->paste_buffer + term->paste_pos, n, 0);
3745 term->paste_pos += n;
3746
3747 if (term->paste_pos < term->paste_len) {
3748 term->paste_hold = 1;
3749 return;
3750 }
3751 }
3752 sfree(term->paste_buffer);
3753 term->paste_buffer = NULL;
3754 term->paste_len = 0;
3755 }
3756
3757 static void deselect(Terminal *term)
3758 {
3759 term->selstate = NO_SELECTION;
3760 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
3761 }
3762
3763 void term_deselect(Terminal *term)
3764 {
3765 deselect(term);
3766 term_update(term);
3767 }
3768
3769 int term_ldisc(Terminal *term, int option)
3770 {
3771 if (option == LD_ECHO)
3772 return term->term_echoing;
3773 if (option == LD_EDIT)
3774 return term->term_editing;
3775 return FALSE;
3776 }
3777
3778 /*
3779 * from_backend(), to get data from the backend for the terminal.
3780 */
3781 int from_backend(void *vterm, int is_stderr, char *data, int len)
3782 {
3783 Terminal *term = (Terminal *)vterm;
3784
3785 assert(len > 0);
3786
3787 bufchain_add(&term->inbuf, data, len);
3788
3789 /*
3790 * term_out() always completely empties inbuf. Therefore,
3791 * there's no reason at all to return anything other than zero
3792 * from this function, because there _can't_ be a question of
3793 * the remote side needing to wait until term_out() has cleared
3794 * a backlog.
3795 *
3796 * This is a slightly suboptimal way to deal with SSH2 - in
3797 * principle, the window mechanism would allow us to continue
3798 * to accept data on forwarded ports and X connections even
3799 * while the terminal processing was going slowly - but we
3800 * can't do the 100% right thing without moving the terminal
3801 * processing into a separate thread, and that might hurt
3802 * portability. So we manage stdout buffering the old SSH1 way:
3803 * if the terminal processing goes slowly, the whole SSH
3804 * connection stops accepting data until it's ready.
3805 *
3806 * In practice, I can't imagine this causing serious trouble.
3807 */
3808 return 0;
3809 }